Skip to content

Commit 00d6c02

Browse files
committed
Merge pull request #1 from n1nj4sec/feature-winpcap
fix support for powershell and fixed some warnings when sending packets on XP
2 parents a3859f1 + 6840d30 commit 00d6c02

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

scapy/arch/windows/__init__.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,18 @@
4545

4646
def _exec_query_ps(cmd, fields):
4747
"""Execute a PowerShell query"""
48-
### XXX NOT TESTED AT ALL, WILL NOT WORK
4948
ps = sp.Popen([conf.prog.powershell] + cmd +
5049
['|', 'select %s' % ', '.join(fields), '|', 'fl'],
5150
stdout=sp.PIPE,
5251
universal_newlines=True)
53-
while True:
54-
line = [ps.stdout.readline().split(':', 1)[1].strip() for _ in fields]
55-
if not line[0]:
56-
break
57-
yield line
52+
l=[]
53+
for line in ps.stdout:
54+
if not line.strip(): #skip empty lines
55+
continue
56+
l.append(line.split(':', 1)[1].strip())
57+
if len(l) == len(fields):
58+
yield l
59+
l=[]
5860

5961
def _vbs_exec_code(code):
6062
tmpfile = tempfile.NamedTemporaryFile(suffix=".vbs", delete=False)
@@ -72,7 +74,7 @@ def _vbs_exec_code(code):
7274

7375
def _vbs_get_iface_guid(devid):
7476
try:
75-
# devid = str(int(devid) + 1)
77+
devid = str(int(devid) + 1)
7678
guid = _vbs_exec_code("""WScript.Echo CreateObject("WScript.Shell").RegRead("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards\\%s\\ServiceName")
7779
""" % devid).__iter__().next()
7880
if guid.startswith('{') and guid.endswith('}\n'):
@@ -347,11 +349,10 @@ def show_interfaces(resolve_mac=True):
347349

348350
_orig_get_if_raw_hwaddr = pcapdnet.get_if_raw_hwaddr
349351
pcapdnet.get_if_raw_hwaddr = lambda iface, *args, **kargs: (
350-
ARPHDR_ETHER, IFACES[iface].mac.replace(':', '').decode('hex')
352+
ARPHDR_ETHER, IFACES.dev_from_pcapname(iface.pcap_name).mac.replace(':', '').decode('hex')
351353
)
352354
get_if_raw_hwaddr = pcapdnet.get_if_raw_hwaddr
353355

354-
355356
def read_routes_xp():
356357
# The InterfaceIndex in Win32_IP4RouteTable does not match the
357358
# InterfaceIndex in Win32_NetworkAdapter under some platforms
@@ -663,7 +664,7 @@ def get_working_if():
663664
try:
664665
# return the interface associated with the route with smallest
665666
# mask (route by default if it exists)
666-
return min(read_routes(), key=lambda x: x[1])[2]
667+
return min(read_routes(), key=lambda x: x[1])[3]
667668
except ValueError:
668669
# no route
669670
return LOOPBACK_NAME

0 commit comments

Comments
 (0)