Advertisement
DarkProgrammer000

Sniffer [simple]

Aug 4th, 2021
845
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.70 KB | None | 0 0
  1. """
  2. Programa: Sniffer com pacotes cru decodificando o protocolo ip
  3. """
  4. """
  5. # Cabecalho:
  6.  
  7. 0                   1                   2                   3
  8. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  9. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  10. |Version|  IHL  |Type of Service|          Total Length         |
  11. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  12. |         Identification        |Flags|      Fragment Offset    |
  13. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  14. |  Time to Live |    Protocol   |         Header Checksum       |
  15. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  16. |                       Source Address                          |
  17. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  18. |                    Destination Address                        |
  19. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  20. |                    Options                    |    Padding    |
  21. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  22.  
  23. struct ipheader
  24. {
  25.     unsigned char ip_hl:4, ip_v:4; /* this means that each member is 4 bits */
  26.     unsigned char ip_tos;      -> B (format)
  27.     unsigned short int ip_len; -> B (format)
  28.     unsigned short int ip_id;  -> H (format)
  29.     unsigned short int ip_off; -> H (format)
  30.     unsigned char ip_ttl;      -> H (format)
  31.     unsigned char ip_p;        -> B (format)
  32.     unsigned short int ip_sum; -> B (format)
  33.     unsigned int ip_src;       -> I (format) => 4s
  34.     unsigned int ip_dst;       -> I (format) => 4s
  35. };
  36. """
  37.  
  38. # Bibliotecas
  39. import socket
  40. import struct
  41.  
  42. # Criando conexao: TCP/IP [raw] (pacote cru)
  43. sniffer = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
  44.  
  45. # Estrutura em loop
  46. while True:
  47.  
  48.     # Capturando trafego
  49.     pacote = sniffer.recvfrom(99999)
  50.  
  51.     # Decodificar os primeiros 20 bytes do pacote
  52.     decodificado = struct.unpack('!BBHHHBBH4s4s', pacote[0][0:20])
  53.  
  54.     # Binario -> 69 = 01000101  ==> Shift Right (>>) 4: 69 = 0100
  55.     print("\n - Versao IP: " + str(int(decodificado[0]) >> 4))
  56.  
  57.     # Binario -> 69 = 01000101  ==> Shift Left (<<) 4: 5 = 0101
  58.     print(" - IP Header: " + str(int(decodificado[0]) << 4))
  59.  
  60.     # TTL
  61.     print(" - Tempo de vida: " + str(int(decodificado[5])))
  62.  
  63.     # Tipo de protocolo
  64.     print(" - Protocolo: " + str(int(decodificado[6])))
  65.  
  66.     # Source: ip_src
  67.     print(" # IP (origem do trafego): " + str(socket.inet_ntoa(decodificado[8])))
  68.  
  69.     # Destination: ip_dst
  70.     print(" * Meu IP (destino do pacote): " + str(socket.inet_ntoa(decodificado[9])))
  71.  
  72.     # Depois dos 20 bytes
  73.     print(" - Pacote (extras): " + str(pacote[0][20:]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement