原始套接字和python中的sendto [英] Raw sockets and sendto in python

查看:328
本文介绍了原始套接字和python中的sendto的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力整合scapy和twisted,但我遇到了OSX上这个非常奇怪的错误,我似乎无法弄明白。

I am working on integrating scapy with twisted, but I ran into this very weird bug on OSX that I can't seem to figure out.

基本上我是无法通过原始套接字发送有效的TCP数据包(包括IP标头)。这就是我正在做的事情:

Basically I am unable to send a valid TCP packet (inclusive of IP headers) via a raw socket. This is what I am doing:

import socket
from scapy.all import IP, TCP
pkt = IP(src='0.0.0.0', dst='127.0.0.1')/TCP()
spkt1 = str(pkt)
outs = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
outs.setsockopt(socket.SOL_IP, socket.IP_HDRINCL, 1)
outs.sendto(spkt1, ('127.0.0.1', 0))

当我运行此操作时,我收到以下错误:

When I run this I get the following error:

outs.sendto(spkt1,('127.0.0.1',0))
socket.error:[Errno 22]参数无效

如果您没有scapy on,不想使用它,这是数据包base64编码:

In case you don't have scapy on don't want to use it this is the packet base64 encoded:

import base64
spkt1 = base64.b64decode("RQAAKAABAABABvvOAAAAAH8AAAEAFABQAAAAAAAAAABQAiAAEH4AAA==")

非常奇怪的是,几乎相同的数据包似乎正确发送:

The very strange thing is that a packet that is nearly identical appears to be sent properly:

spkt2 = base64.b64decode("RQBAAAWwAAACBgAAAAAAAH8AAAEAyAOEAAAAAAAAAACwAgDIAHsAAAIEBbQBAwMBAQEICk3PUjMAAAAABAIAAA==")

这是两个数据包的样子:

This is how the two packets look like:

SPKT1
0000   45 00 00 28 00 01 00 00  40 06 FB CE 00 00 00 00   E..(....@.......
0010   7F 00 00 01 00 14 00 50  00 00 00 00 00 00 00 00   .......P........
0020   50 02 20 00 10 7E 00 00                            P. ..~..
SPKT2
0000   45 00 40 00 05 B0 00 00  02 06 00 00 00 00 00 00   E.@.............
0010   7F 00 00 01 00 C8 03 84  00 00 00 00 00 00 00 00   ................
0020   B0 02 00 C8 00 7B 00 00  02 04 05 B4 01 03 03 01   .....{..........
0030   01 01 08 0A 4D CF 52 33  00 00 00 00 04 02 00 00   ....M.R3........

通过在wireshark中查看它们只有TCP部分不同。

By checking them out in wireshark they only differ in the TCP part.

我做了很多不同的实验,最后我通过设置某些特定的TCP选项来发送数据包,但是这样做是没有意义的。数据包不起作用。

I have done a lot of different experiments and I was able in the end by setting certain specific TCP options to get the packet sent, but it does not make sense that such a packet should not work.

有没有人知道为什么会发生这种情况?

Does anybody have an idea why this may be happening?

编辑:

此数据包确实有效:

pkt = IP(len=16384, src='0.0.0.0', dst='127.0.0.1',
     id=RandShort(), ttl=2)/TCP(sport=255,
      dport=900, flags="S", window=200,
      options=[('MSS', 1460), ('WScale', 2)])
spkt = bytes(pkt)
spkt += '\x00'*20

如果你不添加零,它就不起作用。

If you don't add the zeros it does not work.

推荐答案

我最终决定将Raw Sockets改为可用。特别是因为这个软件需要跨平台,OSX的怪癖可能不适用于其他操作系统。

I ended up deciding that Raw Sockets are just to bugged to be usable. Especially since this software needs to be cross platform, quirks for OSX may not be applicable to other OSs.

暂时我只是包装提供的套接字通过scapy。在将来我会写一些只依赖于libdnet的东西(因为这就是scapy写的原始帧)。

For the time being I simply wrapped the "sockets" that are provided by scapy. In the future I will write something that only depends on libdnet (as that is what scapy does to write raw frames).

你可以在这里找到这个:

You can find this implemented here:

https://github.com/hellais/txscapy

这篇关于原始套接字和python中的sendto的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆