nos0s/sniff/ip_ctype.py

44 lines
1.2 KiB
Python

from ctypes import *
import socket
import struct
import ipaddress
class IP_ctype(Structure):
_fields_ = [
("version", c_ubyte, 4),
("ihl", c_ubyte, 4),
("tos", c_ubyte, 8),
("len", c_ushort, 16),
("id", c_ushort, 16),
("offset", c_ushort, 16),
("ttl", c_ubyte, 8),
("protocol_num", c_ubyte, 8),
("sum", c_ushort, 16),
("src", c_uint32, 32),
("dst", c_uint32, 32)
]
def __new__(cls, socket_buffer=None):
return cls.from_buffer_copy(socket_buffer)
def __init__(self, socket_buffer=None):
self.src_address = socket.inet_ntoa(struct.pack("<L",self.src))
self.dst_address = socket.inet_ntoa(struct.pack("<L",self.dst))
# ICMP response packet
class ICMP_ctype(Structure):
_fields_ = [
("type", c_ubyte, 8),
("code", c_ubyte, 8),
("sum", c_ushort, 16),
("id", c_ushort, 16),
("seq", c_ushort, 16)
]
def __new__(cls, socket_buffer=None):
return cls.from_buffer_copy(socket_buffer)