MarouaneTheViper

Untitled

Apr 14th, 2020
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. import socket
  2. import binascii
  3. from bitstring import BitArray
  4.  
  5.  
  6. class IpPacket(object):
  7. """
  8. Represents the *required* data to be extracted from an IP packet.
  9. """
  10.  
  11. def __init__(self, protocol, ihl, source_address, destination_address, payload):
  12. self.protocol = protocol
  13. self.ihl = ihl
  14. self.source_address = source_address
  15. self.destination_address = destination_address
  16. self.payload = payload
  17.  
  18.  
  19. class TcpPacket(object):
  20. """
  21. Represents the *required* data to be extracted from a TCP packet.
  22. """
  23.  
  24. def __init__(self, src_port, dst_port, data_offset, payload):
  25. self.src_port = src_port
  26. self.dst_port = dst_port
  27. # As far as I know, this field doesn't appear in Wireshark for some reason.
  28. self.data_offset = data_offset
  29. self.payload = payload
  30.  
  31.  
  32. def parse_raw_ip_addr(raw_ip_addr: bytes) -> str:
  33. ip_addr = ""
  34. i = 0
  35. while i < 4:
  36. ip_addr += str(raw_ip_addr[i]) + "."
  37. i += 1
  38. ip_addr = ip_addr[:len(ip_addr) - 1]
  39. return ip_addr
  40.  
  41.  
  42. def parse_application_layer_packet(ip_packet_payload: bytes) -> TcpPacket:
  43. # Parses raw bytes of a TCP packet
  44. # That's a byte literal (~byte array) check resources section
  45. return TcpPacket(-1, -1, -1, b'')
  46.  
  47.  
  48. def parse_network_layer_packet(ip_packet: bytes) -> IpPacket:
  49. # Parses raw bytes of an IPv4 packet
  50. # That's a byte literal (~byte array) check resources section
  51. rec_packet_hex = binascii.hexlify(ip_packet)
  52. print(rec_packet_hex)
  53. rec_packet_bits = BitArray(rec_packet_hex)
  54. print(rec_packet_bits)
  55. ip_version = int(rec_packet_bits[0:3])
  56. if ip_version != 4:
  57. print("IP Version = ", ip_version)
  58. ip_header_len = int(rec_packet_bits[4:7])
  59. src_address = rec_packet_bits[96:127]
  60. dst_address = rec_packet_bits[128:159]
  61. payload = rec_packet_bits[ip_header_len:]
  62. print("length: ", ip_header_len, " Src: ", src_address, " Dst: ", dst_address, " payload: ", payload)
  63. return IpPacket(-1, -1, "0.0.0.0", "0.0.0.0", b'')
  64.  
  65.  
  66. def main():
  67. # Un-comment this line if you're getting too much noisy traffic.
  68. # to bind to an interface on your PC. (or you can simply disconnect from the internet)
  69.  
  70. # iface_name = "lo"
  71. # stealer.setsockopt(socket.SOL_SOCKET,
  72. # socket.SO_BINDTODEVICE, bytes(iface_name, "ASCII"))
  73.  
  74. stealer_sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, 0x0011)
  75. while True:
  76. rec_packet, address = stealer_sock.recvfrom(4096)
  77. some_packet = parse_network_layer_packet(rec_packet)
  78.  
  79.  
  80. if __name__ == "__main__":
  81. main()
Advertisement
Add Comment
Please, Sign In to add comment