Pages

Complete Tutorial On Sniffing

This tutorial given you a complete introduction about Sniffing.Sniffers mainly developed for debugging network problems.Sniffers could capture,interpret and save packets sent across a network.This helpful for admin to later analyze captured packets and debug or troubleshoot network problems.


In market you found many type of sniffers but most used sniffer is Ethernet-based Sniffer.This Sniffer works with NIC(Network Interface Card).With Ethernet-based Sniffer NIC works in a special state called Promiscuous Mode.This mode ensure that your sniffer receives all the packets within specified range of system.
Sniffers can't catch packets traversing beyond switches and routers.

Sniffer dangerous for you,if an attacker used it against you after install on your system,your all data,password,private messages,e-mail and so on -get captured.
Protocol like FTP,POP,IMAP and HTTP are vulnerable to the sniffers.

Type of Attack:

Sniffer Attack consider as a Passive attack.A passive attack is one that doesn't directly intrude onto a foreign network or computer.

Popular Sniffer

1.Name: tcpdump
Download Here

2:Name:Ethereal
Download Here

3:Name:DSniff
Download Here

4:Name:Sniffer
Download Here

5.Name:SSLdump
Download Here

Code Your Own Sniffer In 'C'

  1. //sniffer.c
  2. //To compile : gcc -o sniffer sniffer.c
  3. //To run : ./sniffer [interface-name]
  4. #include
  5. #include
  6. #include
  7. #include
  8. #include
  9. #include
  10. #include
  11. #include
  12. #include
  13. #include
  14. /* default snap length (maximum bytes per packet to capture) */
  15. #define SNAP_LEN 1518
  16. /* ethernet headers are always exactly 14 bytes [1] */
  17. #define SIZE_ETHERNET 14
  18. /* Ethernet addresses are 6 bytes */
  19. #define ETHER_ADDR_LEN 6
  20. /* Ethernet header */
  21. struct sniff_ethernet {
  22. u_char ether_dhost[ETHER_ADDR_LEN]; /* destination host address */
  23. u_char ether_shost[ETHER_ADDR_LEN]; /* source host address */
  24. u_short ether_type; /* IP? ARP? RARP? etc */
  25. };
  26. /* IP header */
  27. struct sniff_ip {
  28. u_char ip_vhl; /* version << 4 | header length >> 2 */
  29. u_char ip_tos; /* type of service */
  30. u_short ip_len; /* total length */
  31. u_short ip_id; /* identification */
  32. u_short ip_off; /* fragment offset field */
  33. #define IP_RF 0x8000 /* reserved fragment flag */
  34. #define IP_DF 0x4000 /* dont fragment flag */
  35. #define IP_MF 0x2000 /* more fragments flag */
  36. #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
  37. u_char ip_ttl; /* time to live */
  38. u_char ip_p; /* protocol */
  39. u_short ip_sum; /* checksum */
  40. struct in_addr ip_src,ip_dst; /* source and dest address */
  41. };
  42. #define IP_HL(ip) (((ip)->ip_vhl) & 0x0f)
  43. #define IP_V(ip) (((ip)->ip_vhl) >> 4)
  44. /* TCP header */
  45. typedef u_int tcp_seq;
  46. struct sniff_tcp {
  47. u_short th_sport; /* source port */
  48. u_short th_dport; /* destination port */
  49. tcp_seq th_seq; /* sequence number */
  50. tcp_seq th_ack; /* acknowledgement number */
  51. u_char th_offx2; /* data offset, rsvd */
  52. #define TH_OFF(th) (((th)->th_offx2 & 0xf0) >> 4)
  53. u_char th_flags;
  54. #define TH_FIN 0x01
  55. #define TH_SYN 0x02
  56. #define TH_RST 0x04
  57. #define TH_PUSH 0x08
  58. #define TH_ACK 0x10
  59. #define TH_URG 0x20
  60. #define TH_ECE 0x40
  61. #define TH_CWR 0x80
  62. #define TH_FLAGS (TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG|TH_ECE|TH_CWR)
  63. u_short th_win; /* window */
  64. u_short th_sum; /* checksum */
  65. u_short th_urp; /* urgent pointer */
  66. };
  67. void
  68. got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet);
  69. void
  70. print_payload(const u_char *payload, int len);
  71. void
  72. print_hex_ascii_line(const u_char *payload, int len, int offset);
  73. /*
  74. * print data in rows of 16 bytes: offset hex ascii
  75. *
  76. * 00000 47 45 54 20 2f 20 48 54 54 50 2f 31 2e 31 0d 0a GET / HTTP/1.1..
  77. */
  78. void
  79. print_hex_ascii_line(const u_char *payload, int len, int offset)
  80. {
  81. int i;
  82. int gap;
  83. const u_char *ch;
  84. /* offset */
  85. printf("%05d ", offset);
  86. /* hex */
  87. ch = payload;
  88. for(i = 0; i < len; i++) {
  89. printf("%02x ", *ch);
  90. ch++;
  91. /* print extra space after 8th byte for visual aid */
  92. if (i == 7)
  93. printf(" ");
  94. }
  95. /* print space to handle line less than 8 bytes */
  96. if (len < 8)
  97. printf(" ");
  98. /* fill hex gap with spaces if not full line */
  99. if (len < 16) {
  100. gap = 16 - len;
  101. for (i = 0; i < gap; i++) {
  102. printf(" ");
  103. }
  104. }
  105. printf(" ");
  106. /* ascii (if printable) */
  107. ch = payload;
  108. for(i = 0; i < len; i++) {
  109. if (isprint(*ch))
  110. printf("%c", *ch);
  111. else
  112. printf(".");
  113. ch++;
  114. }
  115. printf("\n");
  116. return;
  117. }
  118. /*
  119. * print packet payload data (avoid printing binary data)
  120. */
  121. void
  122. print_payload(const u_char *payload, int len)
  123. {
  124. int len_rem = len;
  125. int line_width = 16; /* number of bytes per line */
  126. int line_len;
  127. int offset = 0; /* zero-based offset counter */
  128. const u_char *ch = payload;
  129. if (len <= 0)
  130. return;
  131. /* data fits on one line */
  132. if (len <= line_width) {
  133. print_hex_ascii_line(ch, len, offset);
  134. return;
  135. }
  136. /* data spans multiple lines */
  137. for ( ;; ) {
  138. /* compute current line length */
  139. line_len = line_width % len_rem;
  140. /* print line */
  141. print_hex_ascii_line(ch, line_len, offset);
  142. /* compute total remaining */
  143. len_rem = len_rem - line_len;
  144. /* shift pointer to remaining bytes to print */
  145. ch = ch + line_len;
  146. /* add offset */
  147. offset = offset + line_width;
  148. /* check if we have line width chars or less */
  149. if (len_rem <= line_width) {
  150. /* print last line and get out */
  151. print_hex_ascii_line(ch, len_rem, offset);
  152. break;
  153. }
  154. }
  155. return;
  156. }
  157. /*
  158. * dissect/print packet
  159. */
  160. void
  161. got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
  162. {
  163. static int count = 1; /* packet counter */
  164. /* declare pointers to packet headers */
  165. const struct sniff_ethernet *ethernet; /* The ethernet header [1] */
  166. const struct sniff_ip *ip; /* The IP header */
  167. const struct sniff_tcp *tcp; /* The TCP header */
  168. const char *payload; /* Packet payload */
  169. int size_ip;
  170. int size_tcp;
  171. int size_payload;
  172. printf("\nPacket number %d:\n", count);
  173. count++;
  174. /* define ethernet header */
  175. ethernet = (struct sniff_ethernet*)(packet);
  176. /* define/compute ip header offset */
  177. ip = (struct sniff_ip*)(packet + SIZE_ETHERNET);
  178. size_ip = IP_HL(ip)*4;
  179. if (size_ip < 20) {
  180. printf(" * Invalid IP header length: %u bytes\n", size_ip);
  181. return;
  182. }
  183. /* print source and destination IP addresses */
  184. printf(" From: %s\n", inet_ntoa(ip->ip_src));
  185. printf(" To: %s\n", inet_ntoa(ip->ip_dst));
  186. /* determine protocol */
  187. switch(ip->ip_p) {
  188. case IPPROTO_TCP:
  189. printf(" Protocol: TCP\n");
  190. break;
  191. case IPPROTO_UDP:
  192. printf(" Protocol: UDP\n");
  193. return;
  194. case IPPROTO_ICMP:
  195. printf(" Protocol: ICMP\n");
  196. return;
  197. case IPPROTO_IP:
  198. printf(" Protocol: IP\n");
  199. return;
  200. default:
  201. printf(" Protocol: unknown\n");
  202. return;
  203. }
  204. /*
  205. * OK, this packet is TCP.
  206. */
  207. /* define/compute tcp header offset */
  208. tcp = (struct sniff_tcp*)(packet + SIZE_ETHERNET + size_ip);
  209. size_tcp = TH_OFF(tcp)*4;
  210. if (size_tcp < 20) {
  211. printf(" * Invalid TCP header length: %u bytes\n", size_tcp);
  212. return;
  213. }
  214. printf(" Src port: %d\n", ntohs(tcp->th_sport));
  215. printf(" Dst port: %d\n", ntohs(tcp->th_dport));
  216. /* define/compute tcp payload (segment) offset */
  217. payload = (u_char *)(packet + SIZE_ETHERNET + size_ip + size_tcp);
  218. /* compute tcp payload (segment) size */
  219. size_payload = ntohs(ip->ip_len) - (size_ip + size_tcp);
  220. /*
  221. * Print payload data; it might be binary, so don't just
  222. * treat it as a string.
  223. */
  224. if (size_payload > 0) {
  225. printf(" Payload (%d bytes):\n", size_payload);
  226. print_payload(payload, size_payload);
  227. }
  228. return;
  229. }
  230. int main(int argc, char **argv)
  231. {
  232. char *dev = NULL; /* capture device name */
  233. char errbuf[PCAP_ERRBUF_SIZE]; /* error buffer */
  234. pcap_t *handle; /* packet capture handle */
  235. char filter_exp[] = "ip"; /* filter expression */
  236. struct bpf_program fp; /* compiled filter program (expression) */
  237. bpf_u_int32 mask; /* subnet mask */
  238. bpf_u_int32 net; /* ip */
  239. int num_packets ; /* number of packets to capture */
  240. /* check for capture device name on command-line */
  241. if (argc == 2) {
  242. dev = argv[1];
  243. }
  244. else if (argc > 3) {
  245. fprintf(stderr, "error: unrecognized command-line options\n\n");
  246. printf("Usage: %s [interface]\n", argv[0]);
  247. printf("\n");
  248. printf("Options:\n");
  249. printf(" interface Listen on for packets.\n");
  250. printf("\n");
  251. exit(EXIT_FAILURE);
  252. }
  253. else {
  254. /* find a capture device if not specified on command-line */
  255. dev = pcap_lookupdev(errbuf);
  256. if (dev == NULL) {
  257. fprintf(stderr, "Couldn't find default device: %s\n",
  258. errbuf);
  259. exit(EXIT_FAILURE);
  260. }
  261. }
  262. printf("\nEnter no. of packets you want to capture: ");
  263. scanf("%d",&num_packets);
  264. printf("\nWhich kind of packets you want to capture : ");
  265. scanf("%s",filter_exp);
  266. /* get network number and mask associated with capture device */
  267. if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1) {
  268. fprintf(stderr, "Couldn't get netmask for device %s: %s\n",
  269. dev, errbuf);
  270. net = 0;
  271. mask = 0;
  272. }
  273. /* print capture info */
  274. printf("Device: %s\n", dev);
  275. printf("Number of packets: %d\n", num_packets);
  276. printf("Filter expression: %s\n", filter_exp);
  277. /* open capture device */
  278. handle = pcap_open_live(dev, SNAP_LEN, 1, 1000, errbuf);
  279. if (handle == NULL) {
  280. fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
  281. exit(EXIT_FAILURE);
  282. }
  283. /* make sure we're capturing on an Ethernet device [2] */
  284. if (pcap_datalink(handle) != DLT_EN10MB) {
  285. fprintf(stderr, "%s is not an Ethernet\n", dev);
  286. exit(EXIT_FAILURE);
  287. }
  288. /* compile the filter expression */
  289. if (pcap_compile(handle, &fp, filter_exp, 0, net) == -1) {
  290. fprintf(stderr, "Couldn't parse filter %s: %s\n",
  291. filter_exp, pcap_geterr(handle));
  292. exit(EXIT_FAILURE);
  293. }
  294. /* apply the compiled filter */
  295. if (pcap_setfilter(handle, &fp) == -1) {
  296. fprintf(stderr, "Couldn't install filter %s: %s\n",
  297. filter_exp, pcap_geterr(handle));
  298. exit(EXIT_FAILURE);
  299. }
  300. /* now we can set our callback function */
  301. pcap_loop(handle, num_packets, got_packet, NULL);
  302. /* cleanup */
  303. pcap_freecode(&fp);
  304. pcap_close(handle);
  305. printf("\nCapture complete.\n");
  306. return 0;
362 }
(My Thanks to Varun Gupta for save my time)

Making Sniffer Hard To Detect:

There is a method to help make it more difficult to detect a sniffer on a network. For this to work, you have to deploy two NICs in on computer. For the first NIC, configure the interface with the address of 0.0.0.0. This will allow the sniffer to monitor traffic but to not be detected. But there is still the issue of messages and alerts which will be handed off from the card the sniffer is on to another card to finish being delivered. The second card has a regular address but is not in promiscuous mode so it will be very hard for someone to detect this type of setup.

Save Yourself:

Kitty Litter The Anti-Sniffer is a handy application that can prevent hackers from capturing logins and passwords and viewing traffic on your wireless network by spamming thousands of false-leads per second that are unidentifiable from your regular browsing habits.

Free Download Here


I am Spend 2 hr for this complete article,you spend only 1 Minute for your good Comment.









1 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

Hackarde's Search Engine- Search Hacking Tutorial,Tool and eBook

Loading
 
HACKARDE © 2011 | Designed by HrDe