Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import binascii
- from bitstring import BitArray
- class IpPacket(object):
- """
- Represents the *required* data to be extracted from an IP packet.
- """
- def __init__(self, protocol, ihl, source_address, destination_address, payload):
- self.protocol = protocol
- self.ihl = ihl
- self.source_address = source_address
- self.destination_address = destination_address
- self.payload = payload
- class TcpPacket(object):
- """
- Represents the *required* data to be extracted from a TCP packet.
- """
- def __init__(self, src_port, dst_port, data_offset, payload):
- self.src_port = src_port
- self.dst_port = dst_port
- # As far as I know, this field doesn't appear in Wireshark for some reason.
- self.data_offset = data_offset
- self.payload = payload
- def parse_raw_ip_addr(raw_ip_addr: bytes) -> str:
- ip_addr = ""
- i = 0
- while i < 4:
- ip_addr += str(raw_ip_addr[i]) + "."
- i += 1
- ip_addr = ip_addr[:len(ip_addr) - 1]
- return ip_addr
- def parse_application_layer_packet(ip_packet_payload: bytes) -> TcpPacket:
- # Parses raw bytes of a TCP packet
- # That's a byte literal (~byte array) check resources section
- return TcpPacket(-1, -1, -1, b'')
- def parse_network_layer_packet(ip_packet: bytes) -> IpPacket:
- # Parses raw bytes of an IPv4 packet
- # That's a byte literal (~byte array) check resources section
- rec_packet_hex = binascii.hexlify(ip_packet)
- print(rec_packet_hex)
- rec_packet_bits = BitArray(rec_packet_hex)
- print(rec_packet_bits)
- ip_version = int(rec_packet_bits[0:3])
- if ip_version != 4:
- print("IP Version = ", ip_version)
- ip_header_len = int(rec_packet_bits[4:7])
- src_address = rec_packet_bits[96:127]
- dst_address = rec_packet_bits[128:159]
- payload = rec_packet_bits[ip_header_len:]
- print("length: ", ip_header_len, " Src: ", src_address, " Dst: ", dst_address, " payload: ", payload)
- return IpPacket(-1, -1, "0.0.0.0", "0.0.0.0", b'')
- def main():
- # Un-comment this line if you're getting too much noisy traffic.
- # to bind to an interface on your PC. (or you can simply disconnect from the internet)
- # iface_name = "lo"
- # stealer.setsockopt(socket.SOL_SOCKET,
- # socket.SO_BINDTODEVICE, bytes(iface_name, "ASCII"))
- stealer_sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, 0x0011)
- while True:
- rec_packet, address = stealer_sock.recvfrom(4096)
- some_packet = parse_network_layer_packet(rec_packet)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment