clamav: clamscanとclamdscanの違いってなんなんだよ

clamavはフリーのアンチウイルスソフトウェア
最近はOS Xとかでも有名ですな。

公式サイト
http://www.clamav.net/lang/en/

インストールはportsから。

概要

一部モジュールのみ以下にまとめます。

  • clamscan
  • clamdscan
  • clamd
  • freshclam

・clamscan
 単品で動くウイルススキャナ。
 一回だけのスキャンに使用する。
 遅い。


・clamd
 ウイルススキャナデーモン
 後述のclamdscanとセットで使う。
 あるいはメールのスキャンなどに。


・clamdscan
 clamdとセットで動くウイルススキャナ。
 指定されたファイルのパスをclamdに教えると、clamdがスキャンしてくれる。
 clamdは定義ファイルをメモリ展開しているのでclamscan(clam*d*scanじゃなく)よりもスキャンが断然速い。


・freshclam
 定義ファイルのダウンローダ
 オンデマンドで実行もできるし、常駐化させておいて定期的にダウンロードさせることもできる。



使い方: ウイルスデータベース更新


インストールすれば、すぐに使える。
まずは念のためウイルスデータベースを更新。freshclamを実行するだけ。


# freshclam

ClamAV update process started at Wed Feb 29 14:22:04 2012
Reading CVD header (main.cvd): OK
main.cvd is up to date (version: 54, sigs: 1044387, f-level: 60, builder: sven)
Reading CVD header (daily.cvd): OK
Downloading daily-14555.cdiff [100%]
daily.cld updated (version: 14555, sigs: 108764, f-level: 63, builder: guitar)
Reading CVD header (bytecode.cvd): OK
bytecode.cvd is up to date (version: 167, sigs: 40, f-level: 63, builder: edwin)
Database updated (1153191 signatures) from database.clamav.net
Clamd successfully notified about the update.

使い方: スキャン: clamscan


あとはclamscanにファイル名を引数で渡せばよい。

$ clamscan <file>
$ clamscan -r <dir>

そのほか、よく使用するオプションとしては、

-i: 感染したファイルのみ表示
--move <dirA> : 感染したファイルを<dirA>に移動
--copy <dirB> : 感染したファイルを<dirB>に移動
-d [<file>|<dir>] : デフォルトと異なる場所に置いたウイルスデータベースを指定。
-l <logfile> : スキャンログを<logfile>に書き出し。
-f <filelist> : <filelist>にリストされたファイルをスキャン


clamscanとclamdscan


先述の通り、clamscanとclamdscanではスピードが違う。
実験をしてみるとその差は歴然。
というより、clamscanが遅すぎる。
定期的にスキャンするなら、絶対にclamdscanがよい。
注意点はあるが、それは後述。

$ clamscan ./messages ./lpd-errs
./messages: OK
./lpd-errs: OK

----------- SCAN SUMMARY -----------
(中略)
Infected files: 0
Data scanned: 0.02 MB
Data read: 0.01 MB (ratio 2.00:1)
Time: 7.878 sec (0 m 7 s)


$ clamdscan ./messages ./lpd-errs
/var/log/./messages: OK
/var/log/./lpd-errs: OK

----------- SCAN SUMMARY -----------
Infected files: 0
Time: 0.033 sec (0 m 0 s)


使い方:clamdscan


仕組みは以下の通りと理解している。
スキャンするときには、ファイルの場所をclamdscanに伝える。
clamdscanはdaemonとして常駐しているので、いちいちウイルスデータベースを読み込まなくてよい。
だから速い。
しかしNFSマウントしたファイルのスキャンなど、ファイルへのアクセス権限が特殊な場合に問題となるケースがあった。


clamdscan起動の準備をする。/etc/rc.confに以下を追加。
ついでにfreshclamもdaemonモードで起動するように。

#clamd
clamav_clamd_enable="YES"
clamav_freshclam_enable="YES"

portsから入れたなら、clamd.conf、freshclam.confは特にデフォルトでよい。
起動させる。

 # /usr/local/etc/rc.d/clamav-clamd start
 # /usr/local/etc/rc.d/clamav-freshclam start

以上。

以下は参考。


/usr/local/etc/freshclam.conf

影響しそうなところのみピックアップ

・ウイルスデータベース格納場所

# Path to the database directory.
# WARNING: It must match clamd.conf's directive!
# Default: hardcoded (depends on installation options)
DatabaseDirectory /var/db/clamav

・freshclamのログファイル場所

# Path to the log file (make sure it has proper permissions)
# Default: disabled
UpdateLogFile /var/log/clamav/freshclam.log

・定義ファイル更新を試みる頻度

# Number of database checks per day.
# Default: 12 (every two hours)
#Checks 24

・プロキシ設定。これはありがたい。

# Proxy settings
# Default: disabled
#HTTPProxyServer myproxy.com
#HTTPProxyPort 1234
#HTTPProxyUsername myusername
#HTTPProxyPassword mypass

・データベース更新時にclamdに再読み込みさせる。

# Send the RELOAD command to clamd.
# Default: no
NotifyClamd /usr/local/etc/clamd.conf

・データベース更新が失敗したときに実行するコマンド
 興味あるが今は保留

# Run command when database update process fails.
# Default: disabled
#OnErrorExecute command

・データベースが古い場合に実行するコマンド
 興味あるが今は保留

# Run command when freshclam reports outdated version.
# In the command string %v will be replaced by the new version number.
# Default: disabled
#OnOutdatedExecute command

/usr/local/etc/clamd.conf

基本デフォルト。MaxScanSize、MaxFileSizeくらいか。

# Uncomment this option to enable logging.
# LogFile must be writable for the user running daemon.
# A full path is required.
# Default: disabled
LogFile /var/log/clamav/clamd.log

# Maximum size of the log file.
# Value of 0 disables the limit.
# You may use 'M' or 'm' for megabytes (1M = 1m = 1048576 bytes)
# and 'K' or 'k' for kilobytes (1K = 1k = 1024 bytes). To specify the size
# in bytes just don't use modifiers.
# Default: 1M
#LogFileMaxSize 2M


# Specify the type of syslog messages - please refer to 'man syslog'
# for facility names.
# Default: LOG_LOCAL6
#LogFacility LOG_MAIL


# Path to the database directory.
# Default: hardcoded (depends on installation options)
DatabaseDirectory /var/db/clamav

# Don't scan files and directories matching regex
# This directive can be used multiple times
# Default: scan all
#ExcludePath ^/proc/
#ExcludePath ^/sys/

# Maximum depth directories are scanned at.
# Default: 15
#MaxDirectoryRecursion 20

# Follow directory symlinks.
# Default: no
#FollowDirectorySymlinks yes


# Perform a database check.
# Default: 600 (10 min)
#SelfCheck 600

# Execute a command when virus is found. In the command string %v will
# be replaced with the virus name.
# Default: no
#VirusEvent /usr/local/bin/send_sms 123456789 "VIRUS ALERT: %v"

# Run as another user (clamd must be started by root for this option to work)
# Default: don't drop privileges
User clamav

# PE stands for Portable Executable - it's an executable file format used
# in all 32 and 64-bit versions of Windows operating systems. This option allows
# ClamAV to perform a deeper analysis of executable files and it's also
# required for decompression of popular executable packers such as UPX, FSG,
# and Petite. If you turn off this option, the original files will still be
# scanned, but without additional processing.
# Default: yes
#ScanPE yes

# Executable and Linking Format is a standard format for UN*X executables.
# This option allows you to control the scanning of ELF files.
# If you turn off this option, the original files will still be scanned, but
# without additional processing.
# Default: yes
#ScanELF yes

## Documents
##

# This option enables scanning of OLE2 files, such as Microsoft Office
# documents and .msi files.
# If you turn off this option, the original files will still be scanned, but
# without additional processing.
# Default: yes
#ScanOLE2 yes


# With this option enabled OLE2 files with VBA macros, which were not
# detected by signatures will be marked as "Heuristics.OLE2.ContainsMacros".
# Default: no
#OLE2BlockMacros no

# This option enables scanning within PDF files.
# If you turn off this option, the original files will still be scanned, but
# without decoding and additional processing.
# Default: yes
#ScanPDF yes

##
## Archives
##

# ClamAV can scan within archives and compressed files.
# If you turn off this option, the original files will still be scanned, but
# without unpacking and additional processing.
# Default: yes
#ScanArchive yes

# Mark encrypted archives as viruses (Encrypted.Zip, Encrypted.RAR).
# Default: no
#ArchiveBlockEncrypted no



# This option sets the maximum amount of data to be scanned for each input file.
# Archives and other containers are recursively extracted and scanned up to this
# value.
# Value of 0 disables the limit
# Note: disabling this limit or setting it too high may result in severe damage
# to the system.
# Default: 100M
#MaxScanSize 150M

# Files larger than this limit won't be scanned. Affects the input file itself
# as well as files contained inside it (when the input file is an archive, a
# document or some other kind of container).
# Value of 0 disables the limit.
# Note: disabling this limit or setting it too high may result in severe damage
# to the system.
# Default: 25M
#MaxFileSize 30M


# Nested archives are scanned recursively, e.g. if a Zip archive contains a RAR
# file, all files within it will also be scanned. This options specifies how
# deeply the process should be continued.
# Note: setting this limit too high may result in severe damage to the system.
# Default: 16
#MaxRecursion 10

# Number of files to be scanned within an archive, a document, or any other
# container file.
# Value of 0 disables the limit.
# Note: disabling this limit or setting it too high may result in severe damage
# to the system.
# Default: 10000
#MaxFiles 15000

複数ページのpdfをjpeg,pngに変換するには。

ImageMagickのconvertを使う。
Ubuntuには最初から入ってた。
FreeBSDの場合はports, packageでgraphics/ImageMagickを入れる。pkg_add -r ImageMagickでも/usr/ports/graphics/ImageMagickでmake install cleanでもお好きなように。

書式

convert allinone.pdf image_%04d.png

複数ページのpdfをimage_0001.png, image_0002.png...と変換してくれる。
%dは10進数を示し、%dの間の4は4桁を、0は桁を0埋めすることを示す。

元ネタはこちら。
http://www.linux.com/archive/feature/113978

Xorg7.5は意外にスムーズ

portsXorg 7.5に上げた。
この1,2ヶ月でいろんなportsが更新されており、下手に個別にバージョンアップすると面倒なことになりかねないので、いったんportsを全削除して再インストールする方法をとった。

pkg_info -Ea > portslist.txtとでもしてインストール済みのportsリストを作ったうえで、pkg_info -Ea|xargs -n1 pkg_delete -f として全削除。

注意すべきは、この状態ではhalとかのデーモンプロセスがまだ動いていること。
個別にkillするのは面倒なので、single modeに落とすか、いっそのこと再起動して、それから再インストールを始める。

さすがに時間はかかるものの、コケるportsもなく、まったく問題なく7.5へ移行できた。
拍子抜けして、2chの関連スレも覗いてみたが、誰も同じような感想で、むしろ寂しそうに見えた。

あ。xorg-server 1.8.0から、やっぱりHALは無くなるんだ…。

改行コード

Redhat Linuxのvi上で、改行コードをキーにして置換しようとおもったのだが。
もともとの改行は\nとし、差し替える改行は^Mじゃないとダメくさい。

例えばこんな風に(改行の前にtestを入れる場合)。

:%s/\\n/test^M/g

なんでだろ?

HAL Removal

FreeBSDではXorg7.4への移行の際にHALがデフォルトでONになった。
このときにFreeBSD界では、マウスが動かねーとか何とか死屍累々で、血を吐きながら乗り越えてきた経緯がある。
そんな背景のもと、Ubuntuの10.04で追加される機能を見ていたところ、「HAL Removal」という一言があったので吹いた。

UbuntuはHALを止めちゃうのか!?
よく見たらブート時間短縮のために、ブートプロセスからHALを削除したよ、ということだった。
http://www.ubuntu.com/testing/lucid/beta1#HAL%20removal

HAL removal
This beta sports full removal of HAL from the boot process, making Ubuntu
faster to boot and faster to resume from suspend.


確かに、10.04では、ブートもシャットダウンもすっごく速い。びっくりするくらい速い。9.10でも速かったけど。
デーモンの起動、シャットダウンや、ファイルのsyncとか、どうしてるんだろう?

perlを5.8から5.10へ。

perlのデフォルトバージョンが5.8から5.10に上がった。
/usr/ports/UPDATINGによれば2/5なんで、一ヶ月以上まえのことなんだが、perlと、perlに依存しているパッケージのリビルドが面倒でずっと放置していた。
さすがに今であればpackagesも揃ってきたはずなので、重い腰をあげて作業。
portsでなくpackagesで入れ替え。

portupgradeとほぼ同等の機能をもっているのに、シェルスクリプトで書かれているので単体でインストールできるpkg_replaceを使った。

$ sudo pkg_replace -P perl=/usr/ports/lang/perl5.10
--->  Replacing 'perl-5.8.9_3' with 'perl-5.10.1'
--->  Fetching 'ftp://ftp1.jp.freebsd.org/pub/FreeBSD/ports/i386/packages-7.3-re
lease/All/perl-5.10.1.tbz'
/usr/ports/packages/All/perl-5.10.1.tbz       100% of   13 MB  763 kBps 00m00s
--->  Found a package of 'perl-5.10.1': /usr/ports/packages/All/perl-5.10.1.tbz
--->  Backing up the old version
(略)

perlに依存しているものはすべて作り直せと書かれているが、必要なサービスは問題なく動いているようだったので、目についたものだけpkg_replace -Pで再インストールした。

ネットワークのリンク速度を変えるには。

Linuxでネットワークデバイスが意図しないリンク速度になったときの対処方法。
といっても、下記のサイトどおりに操作するだけ。


「ETHTOOLを使用して NIC を 100Mbps/full-dupex 固定に設定する方法を教えてください。」
http://kbase.redhat.com/faq/docs/DOC-14838


具体的には、ethtoolというコマンドを使う。


Redhatのサイトから手順を以下に抜き出す。

  • ・/sbin/ethtoolがあるか確認し、なければインストール
  • ・/etc/sysconfig/network-scripts/ifcfg-eth0に以下の行を追加

  (1Gbps, 全二重、オートネゴシエーションオフの場合)

ETHTOOL_OPTS="speed 1000 duplex full autoneg off"
  • ・sudo service network restartとする。
  • ・sudo ethtool <ネットワークデバイス名>で結果確認


ethtoolに与えられるオプションは以下の通り。

speed の設定値
10      10Mbps
100    100Mbps
1000  1000Mbps

duplex の設定値
half half-duplex
full full-duplex

autoneg の設定値
on    Autonegotiation 有効
off   Autonegotiation 無効

上記は/etc/sysconfig/network-scripts/ifcfg-eth0にethtool向けのコマンドを記載しておき、ネットワークを再起動する方法。
別にethtoolコマンドを叩いてもよいが、ネットワークが再起動されるのは同じなので、redhatの方法のほうがスマート。

注意点としては、ハードウェアによって、ethtoolからのコマンドを受け付けないケースがあること。
実際、俺の管理しているマシンだと、ethtoolからオートネゴシエーションをオフにできなかった。