def option(self): """分析tcp的可选项字段(分析了常用字段)""" size = len(self._option) ret = [] if size > 0: option = self._option while size > 0: if option[0] == 0x00: ret.append({"END": option[0]}) break if option[0] == 0x01: ret.append({"NOP": option[0]}) size -= 1 option = option[1:] elif option[0] == 0x02: # MSS ret.append({ "MSS": { "length": option[1], "value": BytesOrder.bytes2int(option[2:4], "big") } }) size -= 4 option = option[4:] elif option[0] == 0x03: # 窗口扩大因子 ret.append({ "WSALE": { "length": option[1], "shift_count": option[2] } }) size -= 3 option = option[3:] elif option[0] == 0x04: # SACK ret.append({"SACK": {"length": option[1]}}) size -= 2 option = option[2:] elif option[0] == 0x08: # 时间戳 ret.append({ "TIMESTAMP": { "length": option[1], "value": BytesOrder.bytes2int(option[2:6], "big"), "repl_value": BytesOrder.bytes2int(option[6:10], "big") } }) size -= 10 option = option[10:] else: break else: ret = None return ret
def signature(data): """验证签名同时确定排序,虽然还无法读取到大小端但不影响""" sig = BytesOrder.bytes2int(data) if sig == 0xa1b2c3d4: BytesOrder.order = "big" return True elif sig == 0xd4c3b2a1: BytesOrder.order = "little" return True return False
def __init__(self, data): assert len(data) == 24 self._magic_number = data[:4] if PcapHead.signature(self._magic_number) is False: raise Exception("不支持的文件格式") self._version_major = BytesOrder.bytes2int(data[4:6]) self._version_minor = BytesOrder.bytes2int(data[6:8]) self._thiszone = BytesOrder.bytes2int(data[8:12]) self._sigfigs = BytesOrder.bytes2int(data[12:16]) self._snaplen = BytesOrder.bytes2int(data[16:20]) self._link_type = BytesOrder.bytes2int(data[20:24])
def header_len(self): return BytesOrder.bytes2int(self._header_len, "big")
def dst(self): return BytesOrder.bytes2int(self._dst, "big")
def src(self): return BytesOrder.bytes2int(self._src, "big")
def total_len(self): return BytesOrder.bytes2int(self._total_len, "big")
def id(self): """IP序号""" return BytesOrder.bytes2int(self._id, "big")
def __init__(self, data): self._ts_sec = BytesOrder.bytes2int(data[:4]) self._ts_usec = BytesOrder.bytes2int(data[4:8]) self._incl_len = BytesOrder.bytes2int(data[8:12]) self._orig_len = BytesOrder.bytes2int(data[12:16])
def operate(self): return BytesOrder.bytes2int(self._operate, "big")
def urqt_p(self): """获取紧急指针""" return BytesOrder.bytes2int(self._urqt_p, "big")
def wnd_size(self): """获取滑动窗口大小""" return BytesOrder.bytes2int(self._wnd_size, "big")
def ack(self): """获取确认号""" return BytesOrder.bytes2int(self._ack_no, "big")
def seq(self): """获取序列号""" return BytesOrder.bytes2int(self._seq_no, "big")