在 python 中通过原始套接字发送 scapy 数据包

Send scapy packets through raw sockets in python

可能吗?如是?怎么样?

这是我的脚本(不起作用):

from scapy.all import *
import socket

try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    p=IP(dst="192.168.1.254")/TCP(flags="S", sport=RandShort(), dport=80)
    s.connect(("192.168.1.254",80))
    s.send(p)
    print ("Request sent!")
except:
    print ("An error occurred.")

--更新--

p = bytes(IP(dst="DESTINATIONIP")/TCP(flags="S", sport=RandShort(), dport=80))
    while True:
        try:
            socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, sockip, sockport, True)
            s = socks.socksocket()
            s.connect((DESTINATIONIP,DESTINATIONPORT))
            s.send(p)
            print ("Request Sent!")
        except:
            print ("An error occurred.")

是否可以通过 http 代理而不是 socks 发送此 syn 数据包?

要使用原始套接字发送 scapy 数据包,您必须先将数据包转换为原始字节。例如,使用 scapy 制作的数据包是这样的:

p = IP(dst="192.168.1.254")/TCP(flags="S", sport=RandShort(),dport=80)

应使用 bytes(p) 转换为原始字节。 这会给你类似的东西:

'E\x00\x00(\x00\x01\x00\x00@\x06\xf6w\xc0\xa8\x01\t\xc0\xa8\x01\xfe\x97%\x00P\x00\x00\x00\x00\x00\x00\x00\x00P\x02 \x00t\x15\x00\x00'

然后您可以使用原始套接字发送它。因此,对于您的示例,您可以像这样修改一些代码:

from scapy.all import *
import socket

try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    p = IP(dst="192.168.1.254")/TCP(flags="S", sport=RandShort(),dport=80)/Raw("Hallo world!")
    s.connect(("192.168.1.254",80))
    s.send(bytes(p))
    print "[+] Request Sent!"
except Exception, e:
    raise e

这应该有效!

注意!!! 请记住,当您使用套接字(模块)与另一台计算机通信时 套接字自动构建您的数据包(headers,等等)并发送您想要的内容 发送。但是当你用 scapy 构建一个数据包时,你会从头开始制作它,所以 您定义其内容及其 headers、层等。因此在您发送数据包时的示例中 您将 'all' 作为 content-payload 发送,甚至 packet-headers(ip-header,tcp-header)。 您可以通过 运行ning 下面的嗅探器来测试它:

#!/usr/bin/env python

from scapy.all import *

def printer(packet):
    if packet.haslayer(Raw):
        print packet.getlayer(Raw).load

print "[+] Sniff started"
while True:
    sniff(store=0, filter="host 192.168.1.254 and port 80", prn=printer, iface="your_interface_here")

嗯,当嗅探器 运行 正在尝试 运行 我的 post 中的第一段代码(当我更新packet with a raw layer=tcp.payload) 你会发现不仅 数据,但整个数据包作为数据传输。所以你有点发送 headers 两次。这就是为什么 sockets 有自己的发送方法和 scapy 的原因。