如何在c中创建自定义数据包? [英] How to create a custom packet in c?

查看:25
本文介绍了如何在c中创建自定义数据包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 TCP/IP 协议使用 C 制作自定义数据包.当我说自定义时,我的意思是能够更改数据包中的任何值;例如:MAC、IP地址等.

我尝试四处搜索,但找不到任何实际指导我或给我示例源代码的内容.

如何创建自定义数据包或我应该在哪里寻找指导?

解决方案

一个相对容易且可移植的工具是 libpcap.它以接收原始数据包而闻名(实际上最好先使用它,因为您可以将收到的数据包与手工制作的数据包进行比较),但鲜为人知的 pcap_sendpacket 实际上会发送原始数据包.

如果您想自己从头开始,请使用 AF_PACKETSOCK_RAW 打开一个套接字(适用于 Linux,其他操作系统可能会有所不同) - 例如参见 http://austinmarton.wordpress.com/2011/09/14/sending-raw-ethernet-packets-from-a-specific-interface-in-c-on-linux/ 以及 https://gist.github.com/austinmarton/1922600.请注意,您需要是 root 用户(或更准确地说是拥有适当的能力)才能执行此操作.

另请注意,如果您尝试发送原始 tcp/udp 数据包,您将遇到的一个问题是禁用网络堆栈自动处理回复(将其视为指向现有 IP 地址或尝试转发).

I'm trying to make a custom packet using C using the TCP/IP protocol. When I say custom, I mean being able to change any value from the packet; ex: MAC, IP address and so on.

I tried searching around but I can't find anything that is actually guiding me or giving me example source codes.

How can I create a custom packet or where should I look for guidance?

解决方案

A relatively easy tool to do this that is portable is libpcap. It's better known for receiving raw packets (and indeed it's better you play with that first as you can compare received packets with your hand crafted ones) but the little known pcap_sendpacket will actually send a raw packet.

If you want to do it from scratch yourself, open a socket with AF_PACKET and SOCK_RAW (that's for Linux, other OS's may vary) - for example see http://austinmarton.wordpress.com/2011/09/14/sending-raw-ethernet-packets-from-a-specific-interface-in-c-on-linux/ and the full code at https://gist.github.com/austinmarton/1922600 . Note you need to be root (or more accurately have the appropriate capability) to do this.

Also note that if you are trying to send raw tcp/udp packets, one problem you will have is disabling the network stack automatically processing the reply (either by treating it as addressed to an existing IP address or attempting to forward it).

这篇关于如何在c中创建自定义数据包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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