Advertisement
Guest User

Untitled

a guest
Jun 13th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. /* Simple Raw Sniffer */
  2. /* Author: Luis Martin Garcia. luis.martingarcia [.at.] gmail [d0t] com */
  3. /* */
  4. /* This code is distributed under the GPL License. For more info check: */
  5. /* http://www.gnu.org/copyleft/gpl.html */
  6.  
  7. //bibliografia:
  8. //sacado de: https://github.com/lsanotes/libpcap-tutorial
  9. //explicacion en: http://recursos.aldabaknocking.com/libpcapHakin9LuisMartinGarcia.pdf
  10.  
  11. //comandos utiles:
  12. //para ver los nombres de interfaz de red disponibles: /sbin/ifconfig -a
  13. //para compilar: gcc sniffer.c -o sniffer -lpcap
  14. //para ejecutar: sudo ./sniffer
  15. //para ejecutar sobre una interaz de red en particular (wlan0): sudo ./sniffer wlan0
  16. //para generar captura con tcpdump: sudo tcpdump -i lo -v
  17. //para obtener trafico remoto: ssh --passsword andrew andrew@localhost "sudo /usr/sbin/tcpdump -s0 -w - 'port 8080'" | wireshark -k -i -
  18.  
  19. #include <pcap.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22.  
  23. #define MAXBYTES2CAPTURE 2048
  24.  
  25. /////////////////////////////////////////////////////////////////////////////////
  26. /* procesarPaquete(): Callback function called by pcap_loop() everytime a packet */
  27. /* arrives to the network card. This function prints the captured raw data in */
  28. /* hexadecimal. */
  29. /////////////////////////////////////////////////////////////////////////////////
  30. void procesarPaquete(u_char *arg, const struct pcap_pkthdr* pkthdr, const u_char * packet){
  31.  
  32. int i=0, *counter = (int *)arg;
  33.  
  34. printf("Packet Count: %d\n", ++(*counter));
  35. printf("Received Packet Size: %d\n", pkthdr->len);
  36. printf("Payload:\n");
  37. for (i=0; i<pkthdr->len; i++){
  38.  
  39. if ( isprint(packet[i]) ) /* If it is a printable character, print it */
  40. printf("%c ", packet[i]);
  41. else
  42. printf(". ");
  43.  
  44. if( (i%16 == 0 && i!=0) || i==pkthdr->len-1 )
  45. printf("\n");
  46. }
  47. return;
  48. }
  49.  
  50.  
  51.  
  52. /////////////////////////////////////////////////////////////////////////////////
  53. /* main(): Main function. Opens network interface and calls pcap_loop() */
  54. /////////////////////////////////////////////////////////////////////////////////
  55.  
  56. int main(int argc, char *argv[] ){
  57.  
  58. int i=0, count=0;
  59. pcap_t *descr = NULL;
  60. char errbuf[PCAP_ERRBUF_SIZE], *device=NULL;
  61. memset(errbuf,0,PCAP_ERRBUF_SIZE);
  62.  
  63. if( argc > 1){ /* If user supplied interface name, use it. */
  64. device = argv[1];
  65. }
  66. else{ /* Get the name of the first device suitable for capture */
  67.  
  68. if ( (device = pcap_lookupdev(errbuf)) == NULL){
  69. fprintf(stderr, "ERROR: %s\n", errbuf);
  70. exit(1);
  71. }
  72. }
  73.  
  74. /* Open device in promiscuous mode */
  75. printf("Abriendo (en modo promiscuo) dispositivo de red: %s\n", device);
  76. if ( (descr = pcap_open_live(device, MAXBYTES2CAPTURE, 1, 512, errbuf)) == NULL){
  77. fprintf(stderr, "ERROR: %s\n", errbuf);
  78. exit(1);
  79. }
  80.  
  81. /* Loop forever & call processPacket() for every received packet*/
  82. printf("Ciclar y llamar a procesarPaquete() por cada paquete recibido \n");
  83. if ( pcap_loop(descr, -1, procesarPaquete, (u_char *)&count) == -1){
  84. fprintf(stderr, "ERROR: %s\n", pcap_geterr(descr) );
  85. exit(1);
  86. }
  87.  
  88. return 0;
  89. }
  90.  
  91. /* EOF*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement