pynetwork / pypcap

pypcap - python libpcap module, forked from code.google.com/p/pypcap
Other
299 stars 74 forks source link

function pcap.pcap() throw exception, win7 64bit python3.6.3, pypcap 1.2.0 #64

Open cuphrody opened 6 years ago

cuphrody commented 6 years ago

Traceback (most recent call last): File "D:/python_proj/link_analyze/test3.py", line 10, in pc=pcap.pcap() File "pcap.pyx", line 211, in pcap.pcap.init OSError: b'Error opening adapter: \xcf\xb5\xcd\xb3\xd5\xd2\xb2\xbb\xb5\xbd\xd6\xb8\xb6\xa8\xb5\xc4\xc9\xe8\xb1\xb8\xa1\xa3 (20)'

guyharris commented 6 years ago

\xcf\xb5\xcd\xb3\xd5\xd2\xb2\xbb\xb5\xbd\xd6\xb8\xb6\xa8\xb5\xc4\xc9\xe8\xb1\xb8\xa1\xa3

That doesn't look like a valid UTF-8 representation of the name for a network interface or a valid UTF-16 representation of the name for a network interface.

Where are you getting the name from?

AnuyMik commented 6 years ago

I use python 2.7.13, win10 64bit, install success (use npcap) but execute the testsniff.py, I got the below message.

Traceback (most recent call last): File "C:\Desktop\pypcap\tests\testsniff.py", line 68, in main() File "C:\Desktop\pypcap\tests\testsniff.py", line 48, in main pc = pcap.pcap(name, timeout_ms=50) File "pcap.pyx", line 211, in pcap.pcap.init OSError: The interface name has not been specified in the source string.

jackadam1981 commented 6 years ago

same as my problem。 The interface name has not been specified in the source string. I use python 3.6.3, win10 64bit, install success (use npcap)

test source code:

import pcap
sniff=pcap.pcap()
print(sniff)

it work great on my pycharm,and exe with pyinstaller。 <pcap.pcap object at 0x000002447F6D2620>

the other computer install npcap with winpcap api.(windows 7 64bit) run the exe error : File "test.py",line 3 ,in File "pcap.pyx" ,line 211 , in pcap.pcap.init OSError:b'\x96:The interface name has not been specified in the source string.' Failed to execute script test

Another one use windows10 64bit install npcap with winpcap api work great。 WinPcap for Windows 10: Npcap works on Windows 7 and later by making use of the new NDIS 6 Light-Weight Filter (LWF) API. but work great wiht windows10 。 you can try it on windows 10。

hellais commented 6 years ago

Thanks for reporting this issue.

I don't have a windows machine to test this and have very limited windows knowledge, so I fear I cannot be of that much help.

One thing to try, though, is maybe to specify the interface name when creating pcap.pcap

jackadam1981 commented 6 years ago

if you use pypcap in windows。 point: 1,install npcap with winpcap api。 2,use windows10 64bit。(you can try windows10 32bit) i follow point ,all work great。

pylin-1164 commented 6 years ago

when i run the simple demo after install npcap success ,I have encountered the same problem ; the code like this : pc = pcap.pcap(name='Realtek PCIe GBE Family Controller'); pc.setfilter('arp')

and the error at pc = pcap.pcap(name='Realtek PCIe GBE Family Controller'):

OSError: Activateing packet capture failed. Error returned by packet capture library was b'Error opening adapter: \xcf\xb5\xcd\xb3\xd5\xd2\xb2\xbb\xb5\xbd\xd6\xb8\xb6\xa8\xb5\xc4\xc9\xe8\xb1\xb8\xa1\xa3 (20)'

thanks for guyharris i solve the problem . because of my args name error. the "Realtek PCIe GBE Family Controller" is a description ,and the true should be as "\Device\NPF_{38B0203D-F3E3-4DB8-8FB9-9F8892699C93}" ,it can be find in wireshark or WinPcap to find it

guyharris commented 6 years ago

pc = pcap.pcap(name='Realtek PCIe GBE Family Controller')

That string is a description, not an interface name, and the error WinPcap/Npcap returned was probably a "no such interface" name.

As for why the message itself is bogus, pcap_win32_err_to_str() calls FormatMessage(); if it's calling FormatMessageU() rather than FormatMessageA(), the string returned will be in UTF-16, but all error strings from pcap are in an encoding in which ASCII characters are represented as 1 byte, not 2 bytes. It should probably be changed to explicltly call FormatMessageA(); I'll check that into the libpcap source.

(Dear Microsoft: as there are a lot of libraries that started out on UN*X, and thus deal with strings in encodings of the sort described above, so perhaps you need to make it easier to handle UTF-8, that being an encoding of Unicode in which ASCII characters are represented as 1 byte. Having Win32 APIs in "ANSI", which means "ASCII plus extended characters in the current code page", and "Unicode", which means "Unicode in the UTF-16 encoding, little-endian", forms doesn't suffice any more. There's no guarantee that every user will be using 65001 as the current code page, so that "ANSI" means "UTF-8", especially given that not everything works in all versions of Windows when that's the current code page.)

guyharris commented 6 years ago

(Dear Microsoft: ...)

See, for example, https://en.wikipedia.org/wiki/Unicode_in_Microsoft_Windows#UTF-8.

guyharris commented 6 years ago

That doesn't look like a valid UTF-8 representation of the name for a network interface or a valid UTF-16 representation of the name for a network interface.

It may, however, be a valid UTF-16 representation of an error message that's not in a Roman-alphabet-based script (Cyrillic alphabet, Hebrew alphabet, Arabic alphabet, Chinese, Japanese, Korean, various Indian alphabets, etc., etc., etc.), so that's probably the result of calling FormatMessage() and compiling with "use Unicode" defined. The fix to use FormatMessageA() will mean that the error message will be reported in the current code page; hopefully that'll work.

guyharris commented 6 years ago

The interface name has not been specified in the source string

That's an error reported by pcap_parsesrcstr () for an rpcap:// URL that doesn't have an interface name. It appears that pcap.pcap is, when not given an interface name, providing something that's not valid to pcap.

Litreily commented 5 years ago

I also met this problem, then find the way to get NIC name

import pcap
pcap.findalldevs()