Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
535 views
in Technique[技术] by (71.8m points)

python - 如何在python中发送原始以太网帧?(How do I send a raw ethernet frame in python?)

I need to have a project done in a few days, its a basic client and server interface.

(我需要在几天内完成一个项目,它是基本的客户端和服务器界面。)

The catch is that it needs to be all raw sockets.

(问题是它必须全部是原始套接字。)

I have no problem with creating that, I am just stuck on sending the packets.

(我创建它没有问题,只是停留在发送数据包上。)

First I tried to bind it to an interface 'en1' but it keeps giving me an error nodename not known .

(首先,我尝试将其绑定到接口“ en1”,但它一直给我一个nodename not known的错误nodename not known 。)

When I bind it to my local ip address it works fine.

(当我将其绑定到本地IP地址时,它可以正常工作。)

After completing this I created a raw packet class, its all in hex.

(完成此操作后,我创建了一个原始数据包类,全部为十六进制。)

I then did a sendto call to send it on the wire.

(然后,我进行了sendto呼叫以在线发送它。)

The problem is that when I capture the packet by using wireshark it shows up as being the payload of a ipv4 packet.

(问题是,当我使用Wireshark捕获数据包时,它显示为ipv4数据包的有效负载。)

I don't want it to make the headers automatically, that is what my raw packet class was for anyway.

(我不希望它自动生成标头,无论如何我的原始数据包类都是这样。)

Do you know of any way I can take out these headers?

(您知道我可以取出这些标头的任何方式吗?)

Here is my code - only the raw function:

(这是我的代码-仅原始函数:)

def raw():
    HOST = gethostbyname('192.168.1.10')

    s = socket(AF_INET, SOCK_RAW, IPPROTO_IP)
    s.bind((HOST, 0))

    s.setsockopt(IPPROTO_IP, IP_HDRINCL, 0) #no headers - it wont work!!

    pckt = packet("x68x65x6cx6cx6f")
    netpacket = pckt.getpacket()

    print "Sending.. "
    print ""

    s.sendto(netpacket, ('192.168.1.1', 80))
    data = s.recv(4096)
    print data

And here is the captured packet with a hello at the end:

(这是捕获的数据包,末尾有一个问候 :)

007f 2809 6da2 28cf daee 2156 0800 4500 004d 1bfc 0000 4000 db59 c0a8 010a c0a8 0101* 007f     

2809 6da2 28cf daee 2156 0800 4500 0036 2352 4000 4006 0000 c0a8 010a c0a8 0101 15c0 0050 

0000 0000 0000 0000 8010 813b 0000 68656c6c6f -hello

*This (the 0101 ) is the start of the payload even though it was supposed to be the start of the packet.

(*即使它被认为是数据包的开始,它( 0101 )还是有效载荷的开始。)

Also I am not going to use any other modules, I have to use socket.

(另外,我将不使用任何其他模块,而必须使用套接字。)

  ask by Noah Koster translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

thanks to the comments for this question, i managed to get a connection going with a server.

(感谢对此问题的评论,我设法与服务器建立了连接。)

all it took was changing the address family to af_packet in linux.

(要做的就是在Linux中将地址族更改为af_packet。)

then i binded it to my nic and sent it.

(然后我将其绑定到我的网卡上并发送了。)

it worked.

(有效。)

thanks for the help people!

(感谢您的帮助!)

here is some example code:

(这是一些示例代码:)

s = socket(AF_PACKET, SOCK_RAW)
s.bind(("en1", 0))
pckt = packet() #my class that initializes the raw hex
data = pckt.getpacket()
s.send(data)
message = s.recv(4096)
print s
print s.decode('hex')

It needs to be in linux or debian.

(它必须在linux或debian中。)

to my knowldege it doesnt work in mac osx.

(据我所知,它在Mac OS X中不起作用。)

idk about windows.

(关于Windows的idk。)

if u have a mac use pycap or scapy, they work fine.

(如果您的Mac使用pycap或scapy,它们可以正常工作。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...