RAW客户端-服务器套接字python [英] RAW client-server socket python

查看:112
本文介绍了RAW客户端-服务器套接字python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Python中实现RAW套接字,然后将数据从客户端发送到服务器。

I would like to realize a RAW socket in Python and then send data from the Client to the Server.

与普通套接字不同,我尝试使用以下定义

Unlike a normal socket I've tried to use the following definition

s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW)

,但命令为

s.listen(1), s.connect()

无法正常工作。
我不知道如何对Client.py和Server.py进行编程。
有人可以帮我吗?

are not working. I have no clue how to program both the Client.py and the Server.py. Can someone help me?

推荐答案

那是因为原始套接字无法实现以太网/ TCP / IP库的唯一化完全没有这是一个RAW套接字,您可以负责发送的任何数据。您还负责通过发送正确的SYN / ACK订单与对等方建立连接。

That's because a raw socket doesn't utelize the Ethernet/TCP/IP library at all. It's a RAW socket, you're in charge of whatever data you send. You're also in charge of connecting to your peer by sending the right SYN/ACK order.

传统套接字是抽象层,可用于发送有效负载(数据)。
表示您将套接字 connect 连接到目标,您告诉 socket 要发送什么数据,假设您使用的是基于TCP的套接字,那么数据将放在前面使用与TCP协议和版本相对应的标头,并且您的数据可能会根据您要推送的数据量进行细分。

Traditional sockets is an "abstraction" layer for you to send your payload (data). Meaning you connect your socket to a destination, you tell the socket what data to send, assuming you're using a TCP based socket your data will be prepended with a header corresponding to the TCP protocol and version and your data might get segmented based on how much data you're trying to push through.

传统的套接字。

大致是这样的TCP头(不带上下文,但是会给您一个想法):

This is what a TCP header looks like, roughly (taken out of context but it will give you an idea):

   0                   1                   2                   3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |          Source Port          |       Destination Port        |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                        Sequence Number                        |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                    Acknowledgment Number                      |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |  Data |           |U|A|P|R|S|F|                               |
   | Offset| Reserved  |R|C|S|S|Y|I|            Window             |
   |       |           |G|K|H|T|N|N|                               |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |           Checksum            |         Urgent Pointer        |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                    Options                    |    Padding    |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                             data                              |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

数据是通常的操作,但是使用RAW套接字时,您需要将所有这些信息块发送到以太网电缆上

Note that data is what you normally do, but when working with RAW sockets, you need to send all these information blocks on to your Ethernet cable.

如果有人没有击败我,我可以在今晚晚些时候发布一些代码,但这是一个很好的简短用法示例:

I could post some code later tonight perhaps if someone doesn't beat me to it, but here's a good short usage example: How Do I Use Raw Socket in Python?

您需要构建一个以太网头和一个TCP头,并根据RFC标准将数据添加到其中(这可能成为一个不错的起点: https://tools.ietf.org/html/rfc793 )。然后,您需要简单将其发送到套接字。 RAW套接字没有任何魔术,您可以使用源+目的地地址构建标头,然后将有效载荷发送到电缆上,以希望正确构建了数据包。

You need to build a Ethernet header and a TCP header and add your data to it according to the RFC standard (this might be a good place to start: https://tools.ietf.org/html/rfc793). Then you need to "simply" send that out on to your "socket". There's no magic involved with RAW sockets, you build your header with a source+destination address, and you send your payload out onto the cable hoping you built the packet correctly.

socket.accept()-此函数在传统套接字中用于存储会话信息(源:端口->目标:端口)。此功能从传入连接尝试的缓冲队列中获取客户端并激活它们。这不适用于原始套接字,原因是来自普通套接字的抽象层仍然不存在。您的RAW套接字将监听所有传入的数据(不是连接),这意味着您负责首先接收一个 SYN 数据包,回复 SYN-ACK ,您将收到最终的 ACK 。此时,您可以选择使用正确的信息(源端口等)在彼此之间发送数据。

socket.accept() - This function is used in traditional sockets to "store" session information (Source:Port -> Destination:Port). This function takes clients from a buffered queue of incoming connection attempts and "activates" them. This does not apply to raw sockets, the reason being is that the abstraction layer from normal sockets is again, not present. Your RAW socket will listen to any incoming data (not connections), meaning you're in charge of receiving first a SYN packet which you need to respond with a SYN-ACK in which you'll receive a final ACK. At this point, you're good to go for sending data between you with the correct information (source port etc).

这是普通套接字中提供的抽象层的良好(ASCII)流程图:

Here's a good (ASCII) flow-chart of the abstraction layer provided in a normal socket:

                              +---------+ ---------\      active OPEN  
                              |  CLOSED |            \    -----------  
                              +---------+<---------\   \   create TCB  
                                |     ^              \   \  snd SYN    
                   passive OPEN |     |   CLOSE        \   \           
                   ------------ |     | ----------       \   \         
                    create TCB  |     | delete TCB         \   \       
                                V     |                      \   \     
                              +---------+            CLOSE    |    \   
                              |  LISTEN |          ---------- |     |  
                              +---------+          delete TCB |     |  
                   rcv SYN      |     |     SEND              |     |  
                  -----------   |     |    -------            |     V  
 +---------+      snd SYN,ACK  /       \   snd SYN          +---------+
 |         |<-----------------           ------------------>|         |
 |   SYN   |                    rcv SYN                     |   SYN   |
 |   RCVD  |<-----------------------------------------------|   SENT  |
 |         |                    snd ACK                     |         |
 |         |------------------           -------------------|         |
 +---------+   rcv ACK of SYN  \       /  rcv SYN,ACK       +---------+
   |           --------------   |     |   -----------                  
   |                  x         |     |     snd ACK                    
   |                            V     V                                
   |  CLOSE                   +---------+                              
   | -------                  |  ESTAB  |                              
   | snd FIN                  +---------+                              
   |                   CLOSE    |     |    rcv FIN                     
   V                  -------   |     |    -------                     
 +---------+          snd FIN  /       \   snd ACK          +---------+
 |  FIN    |<-----------------           ------------------>|  CLOSE  |
 | WAIT-1  |------------------                              |   WAIT  |
 +---------+          rcv FIN  \                            +---------+
   | rcv ACK of FIN   -------   |                            CLOSE  |  
   | --------------   snd ACK   |                           ------- |  
   V        x                   V                           snd FIN V  
 +---------+                  +---------+                   +---------+
 |FINWAIT-2|                  | CLOSING |                   | LAST-ACK|
 +---------+                  +---------+                   +---------+
   |                rcv ACK of FIN |                 rcv ACK of FIN |  
   |  rcv FIN       -------------- |    Timeout=2MSL -------------- |  
   |  -------              x       V    ------------        x       V  
    \ snd ACK                 +---------+delete TCB         +---------+
     ------------------------>|TIME WAIT|------------------>| CLOSED  |
                              +---------+                   +---------+



这是一个服务器示例:



Here's a server example:

#!/usr/bin/env python
from socket import socket, AF_PACKET, SOCK_RAW
s = socket(AF_PACKET, SOCK_RAW)
#s.bind(("eth1", 0))

# We're putting together an ethernet frame here, 
# NOTE: Not a full TCP frame, this is important to remember!
src_addr = "\x01\x02\x03\x04\x05\x06"
dst_addr = "\x01\x02\x03\x04\x05\x06"
payload = ("["*30)+"PAYLOAD"+("]"*30)
checksum = "\x1a\x2b\x3c\x4d"
ethertype = "\x08\x01"

s.send(dst_addr+src_addr+ethertype+payload+checksum)

找到了一些我刚开始使用的旧代码,可能会派上用场:> https://github.com/Torxed/Scripts/tree/master/python/Laboratory

Found some old code that I barely got started on, might come in handy: https://github.com/Torxed/Scripts/tree/master/python/Laboratory

这篇关于RAW客户端-服务器套接字python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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