我正在尝试制作一个 TCP 端口扫描器,但我坚持使用一个非常简单的示例,该示例是我从网上找到的一个更高级的示例中整理出来的。
我没有收到任何错误。
自从我在 Linux 机器上启动 Apache 服务器以来,我希望代码能够显示端口 80 已打开。
代码如下:
#!/usr/bin/python
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import *
ip = "127.0.0.1"
port = 80
response = sr1(IP(dst=ip)/TCP(dport=port, flags="S"),verbose=False, timeout=0.2)
if response :
if response[TCP].flags == 18 :
print "Port open"
我有警告(但不再出现):
WARNING: No route found for IPv6 destination :: (no default route?)
我读到,包含下面这两行将有助于解决该错误:
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
Nmap 扫描:
STATE SERVICE
80/tcp open http
输出是……什么也没有。
我尝试了一些方法,例如将端口更改为不同的其他端口,其中一些是我打开的,一些是我没有打开的。
关于我做错了什么有什么想法吗?
最佳答案
scapy docs提到环回地址是一种特殊情况
The loopback interface is a very special interface. Packets going through it are not really assembled and dissassembled. The kernel routes the packet to its destination while it is still stored an internal structure. What you see with tcpdump -i lo is only a fake to make you think everything is normal. The kernel is not aware of what Scapy is doing behind his back, so what you see on the loopback interface is also a fake. Except this one did not come from a local structure. Thus the kernel will never receive it.
In order to speak to local applications, you need to build your packets one layer upper, using a PF_INET/SOCK_RAW socket instead of a PF_PACKET/SOCK_RAW (or its equivalent on other systems that Linux):
>>> conf.L3socket <class __main__.L3PacketSocket at 0xb7bdf5fc> >>> conf.L3socket=L3RawSocket >>> sr1(IP(dst="127.0.0.1")/ICMP()) <IP version=4L ihl=5L tos=0x0 len=28 id=40953 flags= frag=0L ttl=64 proto=ICMP chksum=0xdce5 src=127.0.0.1 dst=127.0.0.1 options=''
|\>
但是在我的 OS-X 机器上进行测试会导致以下错误:
>>> conf.L3socket=L3RawSocket
>>> sr1(IP(dst="127.0.0.1")/ICMP())
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scapy/sendrecv.py", line 334, in sr1
s=conf.L3socket(filter=filter, nofilter=nofilter, iface=iface)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scapy/supersocket.py", line 64, in __init__
self.ins = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(type))
AttributeError: 'module' object has no attribute 'AF_PACKET'
所以你的里程可能会有所不同
编辑
显然这是 BSD 类系统(包括 OS-X)上 scapy 中的一个已知错误: http://bb.secdev.org/scapy/issue/174/sniffing-loopback-in-mac-os-x-darwin
关于python - 使用 Scapy 用 Python 编写的 TCP 端口扫描器没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24471865/