Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Programa: Sniffer com pacotes cru decodificando o protocolo ip
- """
- """
- # Cabecalho:
- 0 1 2 3
- 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
- +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- |Version| IHL |Type of Service| Total Length |
- +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- | Identification |Flags| Fragment Offset |
- +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- | Time to Live | Protocol | Header Checksum |
- +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- | Source Address |
- +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- | Destination Address |
- +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- | Options | Padding |
- +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- struct ipheader
- {
- unsigned char ip_hl:4, ip_v:4; /* this means that each member is 4 bits */
- unsigned char ip_tos; -> B (format)
- unsigned short int ip_len; -> B (format)
- unsigned short int ip_id; -> H (format)
- unsigned short int ip_off; -> H (format)
- unsigned char ip_ttl; -> H (format)
- unsigned char ip_p; -> B (format)
- unsigned short int ip_sum; -> B (format)
- unsigned int ip_src; -> I (format) => 4s
- unsigned int ip_dst; -> I (format) => 4s
- };
- """
- # Bibliotecas
- import socket
- import struct
- # Criando conexao: TCP/IP [raw] (pacote cru)
- sniffer = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
- # Estrutura em loop
- while True:
- # Capturando trafego
- pacote = sniffer.recvfrom(99999)
- # Decodificar os primeiros 20 bytes do pacote
- decodificado = struct.unpack('!BBHHHBBH4s4s', pacote[0][0:20])
- # Binario -> 69 = 01000101 ==> Shift Right (>>) 4: 69 = 0100
- print("\n - Versao IP: " + str(int(decodificado[0]) >> 4))
- # Binario -> 69 = 01000101 ==> Shift Left (<<) 4: 5 = 0101
- print(" - IP Header: " + str(int(decodificado[0]) << 4))
- # TTL
- print(" - Tempo de vida: " + str(int(decodificado[5])))
- # Tipo de protocolo
- print(" - Protocolo: " + str(int(decodificado[6])))
- # Source: ip_src
- print(" # IP (origem do trafego): " + str(socket.inet_ntoa(decodificado[8])))
- # Destination: ip_dst
- print(" * Meu IP (destino do pacote): " + str(socket.inet_ntoa(decodificado[9])))
- # Depois dos 20 bytes
- print(" - Pacote (extras): " + str(pacote[0][20:]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement