用C语言和python分别生成抓取ip报文的代码
时间: 2024-05-15 13:19:23 浏览: 176
C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
int main(int argc, char const *argv[])
{
int sockfd;
struct sockaddr_in addr;
char buffer[1024];
struct iphdr *ip;
struct tcphdr *tcp;
// 创建原始套接字
sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
if (sockfd < 0)
{
perror("socket");
exit(1);
}
// 设置目标IP地址
addr.sin_family = AF_INET;
addr.sin_port = htons(80);
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
// 构造IP头部
ip = (struct iphdr *)buffer;
ip->ihl = 5;
ip->version = 4;
ip->tos = 0;
ip->tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
ip->id = htons(54321);
ip->frag_off = 0;
ip->ttl = 255;
ip->protocol = IPPROTO_TCP;
ip->check = 0;
ip->saddr = inet_addr("192.168.1.100");
ip->daddr = addr.sin_addr.s_addr;
// 构造TCP头部
tcp = (struct tcphdr *)(buffer + sizeof(struct iphdr));
tcp->source = htons(8888);
tcp->dest = addr.sin_port;
tcp->seq = htonl(1105024978);
tcp->ack_seq = 0;
tcp->doff = 5;
tcp->syn = 1;
tcp->window = htons(14600);
tcp->check = 0;
tcp->urg_ptr = 0;
// 计算IP校验和
ip->check = htons((unsigned short)checksum((unsigned short *)ip, sizeof(struct iphdr)));
// 发送报文
if (sendto(sockfd, buffer, ip->tot_len, 0, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
perror("sendto");
exit(1);
}
printf("Packet sent successfully!\n");
// 关闭套接字
close(sockfd);
return 0;
}
// 计算校验和
unsigned short checksum(unsigned short *ptr, int nbytes)
{
register long sum;
unsigned short oddbyte;
register short answer;
sum = 0;
while (nbytes > 1)
{
sum += *ptr++;
nbytes -= 2;
}
if (nbytes == 1)
{
oddbyte = 0;
*((unsigned char *)&oddbyte) = *(unsigned char *)ptr;
sum += oddbyte;
}
sum = (sum >> 16) + (sum & 0xffff);
sum = sum + (sum >> 16);
answer = (short)~sum;
return answer;
}
```
Python代码:
```python
import socket
import struct
# 构造IP头部
def make_ip_header(src_ip, dst_ip):
version = 4 # 版本号
ihl = 5 # IP头部长度
tos = 0 # 服务类型
tot_len = 20 + 20 # 总长度
id = 54321 # 标识符
frag_off = 0 # 分段标识和偏移量
ttl = 255 # 生存时间
protocol = socket.IPPROTO_TCP # 协议类型
check = 0 # 校验和
saddr = socket.inet_aton(src_ip) # 源IP地址
daddr = socket.inet_aton(dst_ip) # 目标IP地址
ip_header = struct.pack('!BBHHHBBH4s4s', (version << 4) + ihl, tos, tot_len, id, frag_off, ttl, protocol, check, saddr, daddr)
return ip_header
# 构造TCP头部
def make_tcp_header(src_port, dst_port, seq, ack_seq, syn, window):
doff = 5 # TCP头部长度
res1 = 0 # 保留位
res2 = 0 # 保留位
urg_ptr = 0 # 紧急指针
offset_res = (doff << 4) + res1 # 偏移量和保留位
flags = syn # 标志位
window_size = window # 窗口大小
check = 0 # 校验和
tcp_header = struct.pack('!HHLLBBHHH', src_port, dst_port, seq, ack_seq, offset_res, flags, window_size, check, urg_ptr)
return tcp_header
# 计算校验和
def checksum(msg):
s = 0
for i in range(0, len(msg), 2):
w = (msg[i] << 8) + (msg[i+1])
s += w
s = (s >> 16) + (s & 0xffff)
s = ~s & 0xffff
return s
# 发送报文
def send_packet(src_ip, dst_ip, src_port, dst_port, seq, syn, window):
# 创建原始套接字
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
# 构造IP头部和TCP头部
ip_header = make_ip_header(src_ip, dst_ip)
tcp_header = make_tcp_header(src_port, dst_port, seq, 0, syn, window)
# 计算校验和
pseudo_header = struct.pack('!4s4sBBH', socket.inet_aton(src_ip), socket.inet_aton(dst_ip), 0, socket.IPPROTO_TCP, len(tcp_header))
checksum_tcp = checksum(pseudo_header + tcp_header)
tcp_header = struct.pack('!HHLLBBHHH', src_port, dst_port, seq, 0, (5 << 4), syn, window, checksum_tcp, 0)
# 构造最终报文
packet = ip_header + tcp_header
# 发送报文
s.sendto(packet, (dst_ip, 0))
print('Packet sent successfully!')
# 关闭套接字
s.close()
# 测试代码
if __name__ == '__main__':
src_ip = '192.168.1.100'
dst_ip = '127.0.0.1'
src_port = 8888
dst_port = 80
seq = 1105024978
syn = 1
window = 14600
send_packet(src_ip, dst_ip, src_port, dst_port, seq, syn, window)
```
阅读全文