ページ更新: 2010-02-07 (日) (4795日前)

関連: ソフト/Wireshark, Windows/パケットキャプチャ, ソフト/Script言語/Python/scapy, libpcap, WinPcap, jNetPcap

(2010-02-03 新規作成)

libpcap/WinPcapをPythonで使うためのライブラリpcapy と、 パケットを解析するためのライブラリ impacket に関するメモ。

目次

[編集]

情報源 #

[編集]

Windows上のPython 2.6.4にインストールしてみた (2010-02-03) #

現時点では、pcapy にはPython 2.6.x 用のバイナリは提供されていないようなので、コンパイラ (Visual Studio 2008) を用意しておき、pcapy の setup.pyでコンパイルする。

以下の3つの環境を用いた:

  1. Windows 7 Professional 64bit, Visual Studio 2008 Standard
  2. Windows XP Professional SP3, Visual Studio 2008 Professional
  3. Windows XP Professional SP3, Visual Studio 2008 Express (2010-02-07)

手順の概要:

  1. Visual Studio 2008 をインストールする。 (Visual Studio C++ 2008 Express Editionでよい)
  2. Python 2.6.4 ReleasePython 2.6.4 (32bit版) をインストールする。
  3. C:\Python26 と C:\Python26\Scripts (この時点では存在しないが) にパスを通す。
  4. easy_install をインストールする。 (ez_setup.pyをダウンロードして実行)
  5. Wiresharkをインストールする。
    1. あるいは WinPcap の WinPcap と windump をインストールして、windump にパスを通す。
  6. WinPcap Developer's Pack を C:\devel\WpdPack に展開する。
  7. Pcapy をインストールする (easy_install -U http://oss.coresecurity.com/repo/pcapy-0.10.5.zip)
  8. impacket をインストールする (easy_install -U Impacket)
  9. tshark でデバイスを調べる ("c:\Program Files\Wireshark\tshark.exe" -D)
  10. 試しに sniff.py を実行する (start sniff.py)

以下、WinPcap Developer's Pack、pcapy、impacket のインストールと、動作確認について詳しく述べる。

[編集]

WinPcap Developer's Pack #

WinPcap Development から「WinPcap 4.1.1 Developer's Pack」をダウンロードして、c:\devel\WpdPack に置く。

(pcapy-0.10.5のsetup.pyが「c:\devel\include」と「c:\devel\lib」を用いているため)

C:\DEVEL
 └─WpdPack
     ├─docs
     ├─Examples-pcap
     ├─Examples-remote
     ├─Include
     └─Lib
[編集]

pcapy #

pcapy-0.10.5.zip を easy_install でインストールする。

C:> easy_install -U http://oss.coresecurity.com/repo/pcapy-0.10.5.zip
Downloading http://oss.coresecurity.com/repo/pcapy-0.10.5.zip
Processing pcapy-0.10.5.zip
Running pcapy-0.10.5\setup.py -q bdist_egg --dist-dir c:\temp\easy_install-ois1dw\pcapy-0.10.5\egg-dist-tmp-dkde3qpcapdumper.cc
bpfobj.cc
pcapy.cc
pcapobj.cc
pcap_pkthdr.cc
dllmain.cc
   ライブラリ build\temp.win32-2.6\Release\pcapy.lib とオブジェクト build\temp.win32-2.6\Release\pcapy.exp を作成中
zip_safe flag not set; analyzing archive contents...
Adding pcapy 0.10.5 to easy-install.pth file

Installed c:\python26\lib\site-packages\pcapy-0.10.5-py2.6-win32.egg
Processing dependencies for pcapy==0.10.5
Finished processing dependencies for pcapy==0.10.5

importしてみる。特にエラーは発生しない。

C:> python -c "import pcapy"
C:>

なお、以下のようにeasy_installにpcapy を探させると失敗する。

C:> easy_install -U pcapy
Searching for pcapy
Reading http://pypi.python.org/simple/pcapy/
Reading http://oss.coresecurity.com/projects/pcapy.html
Best match: pcapy 0.10.5
Downloading http://oss.coresecurity.com/rep
o/pcapy-0.10.5.zip
error: Download error for http://oss.coresecurity.com/rep
o/pcapy-0.10.5.zip: no host given

また、WpdPackが所定のディレクトリに存在しない場合は、コンパイルエラー (pcap.hが見つからない) が発生して失敗する。

C:> easy_install -U http://oss.coresecurity.com/repo/pcapy-0.10.5.zip
Downloading http://oss.coresecurity.com/repo/pcapy-0.10.5.zip
Processing pcapy-0.10.5.zip
Running pcapy-0.10.5\setup.py -q bdist_egg --dist-dir c:\temp\easy_install-sfi96p\pcapy-0.10.5\egg-dist-tmp-fnldys
pcapdumper.cc
pcapdumper.cc(12) : fatal error C1083: include ファイルを開けません。'pcap.h': No such file or directory
error: Setup script exited with error: command '"C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\cl.exe"' failed with exit status 2
[編集]

impacket #

下記の手順でインストールする。

C:> easy_install -U Impacket
Searching for Impacket
Reading http://pypi.python.org/simple/Impacket/
Reading http://oss.coresecurity.com/projects/impacket.html
Best match: Impacket 0.9.6.0
Downloading http://oss.coresecurity.com/repo/Impacket-0.9.6.0.zip
Processing Impacket-0.9.6.0.zip
Running Impacket-0.9.6.0\setup.py -q bdist_egg --dist-dir c:\temp\easy_install-siojpg\Impacket-0.9.6.0\egg-dist-tmp-ovqadw
warning: no files found matching '*.txt' under directory 'examples'
zip_safe flag not set; analyzing archive contents...
Adding impacket 0.9.6.0 to easy-install.pth file
Installing oochain.py script to C:\Python26\Scripts
Installing win_echod.py script to C:\Python26\Scripts
Installing sniff.py script to C:\Python26\Scripts
Installing loopchain.py script to C:\Python26\Scripts
Installing sniffer.py script to C:\Python26\Scripts
Installing crapchain.py script to C:\Python26\Scripts
Installing New SMB and DCERPC features in Impacket.pdf script to C:\Python26\Scripts
Installing exploit.py script to C:\Python26\Scripts
Installing split.py script to C:\Python26\Scripts
Installing samrdump.py script to C:\Python26\Scripts
Installing SMBCommands.dot script to C:\Python26\Scripts
Installing SMBCommands.png script to C:\Python26\Scripts
Installing smbclient.py script to C:\Python26\Scripts
Installing tracer.py script to C:\Python26\Scripts
Installing ms05-039-crash.py script to C:\Python26\Scripts
Installing chain.py script to C:\Python26\Scripts
Installing smbcat.py script to C:\Python26\Scripts
Installing ping.py script to C:\Python26\Scripts
Installing rpcdump.py script to C:\Python26\Scripts

Installed c:\python26\lib\site-packages\impacket-0.9.6.0-py2.6.egg
Processing dependencies for Impacket
Finished processing dependencies for Impacket

importしてみる。特にエラーは発生しない。

C:> python -c "import impacket"
C:>
[編集]

動作確認 #

あらかじめ、windump や tshark (Wiresharkに付属) でデバイスを調べておく。

C:> windump -D
1.\Device\NPF_{6B597EF7-BC28-4DDF-B8FD-593454C76401} (VMware Virtual Ethernet Adapter)
2.\Device\NPF_{39A43EA5-F472-4144-B513-7DAE534E6F4E} (Realtek RTL8168C/8111C PCI-E Gigabit Ethernet NIC)
3.\Device\NPF_{5358C949-7F97-4797-97E5-CCB924F779EF} (VMware Virtual Ethernet Adapter)
4.\Device\NPF_{EEA9598E-E76A-48BA-84B3-749CB44DC76C} (Intel(R) Gigabit CT Desktop Adapter)
5.\Device\NPF_{6694916A-888D-49D2-B4F4-CA0AF876BE0F} (Realtek RTL8168C/8111C PCI-E Gigabit Ethernet NIC)
C:> "c:\Program Files\Wireshark\tshark.exe" -D
1. \Device\NPF_{6B597EF7-BC28-4DDF-B8FD-593454C76401} (VMware Virtual Ethernet Adapter)
2. \Device\NPF_{39A43EA5-F472-4144-B513-7DAE534E6F4E} (Realtek RTL8168C/8111C PCI-E Gigabit Ethernet NIC)
3. \Device\NPF_{5358C949-7F97-4797-97E5-CCB924F779EF} (VMware Virtual Ethernet Adapter)
4. \Device\NPF_{EEA9598E-E76A-48BA-84B3-749CB44DC76C} (Intel(R) Gigabit CT Desktop Adapter)
5. \Device\NPF_{6694916A-888D-49D2-B4F4-CA0AF876BE0F} (Realtek RTL8168C/8111C PCI-E Gigabit Ethernet NIC)

pcapy.findalldevs() の結果:

C:> python -c "import pcapy; print '\n'.join(pcapy.findalldevs())"
\Device\NPF_{6B597EF7-BC28-4DDF-B8FD-593454C76401}
\Device\NPF_{39A43EA5-F472-4144-B513-7DAE534E6F4E}
\Device\NPF_{5358C949-7F97-4797-97E5-CCB924F779EF}
\Device\NPF_{EEA9598E-E76A-48BA-84B3-749CB44DC76C}
\Device\NPF_{6694916A-888D-49D2-B4F4-CA0AF876BE0F}

Impacket-0.9.6.0 をインストールしたときに、C:\Python26\Scripts にスクリプトがインストールされる。このスクリプトを用いてみる。 試しに、sniff.py を実行してみる。なお、このスクリプトはctrl-Cでは止められないので、startコマンドで他のコマンドプロンプトを起動している。また、元のコマンドプロンプトでpingを実行している (=ICMP ECHO)。

C:> start sniff.py

0 - \Device\NPF_{6B597EF7-BC28-4DDF-B8FD-593454C76401}
1 - \Device\NPF_{39A43EA5-F472-4144-B513-7DAE534E6F4E}
2 - \Device\NPF_{5358C949-7F97-4797-97E5-CCB924F779EF}
3 - \Device\NPF_{EEA9598E-E76A-48BA-84B3-749CB44DC76C}  ★Intel Gigati CT Desktop Adapter
4 - \Device\NPF_{6694916A-888D-49D2-B4F4-CA0AF876BE0F}
Please select an interface: 3  ★3のIntel Gigabit CT Adapterを用いてみる。
Listening on \Device\NPF_{EEA9598E-E76A-48BA-84B3-749CB44DC76C}: net=192.168.1.0, mask=255.255.255.0, linktype=1
Ether: 0:1b:21:49:82:37 -> 0:0:39:29:98:1a
IP 192.168.1.4 -> 192.168.1.2
ICMP type: ECHO code: UNKNOWN

6162 6364 6566 6768 696a 6b6c 6d6e 6f70    abcdefghijklmnop
7172 7374 7576 7761 6263 6465 6667 6869
Ether: 0:0:39:29:98:1a -> 0:1b:21:49:82:37
IP 192.168.1.2 -> 192.168.1.4
ICMP type: ECHOREPLY code: UNKNOWN

6162 6364 6566 6768 696a 6b6c 6d6e 6f70    abcdefghijklmnop
7172 7374 7576 7761 6263 6465 6667 6869
Ether: 0:1b:21:49:82:37 -> 0:0:39:29:98:1a
IP 192.168.1.4 -> 192.168.1.2
ICMP type: ECHO code: UNKNOWN