def buildRequest(src, dst, payload='', ipId=0): udpPkt = Udp() udpPkt.srcport = src[2] udpPkt.dstport = dst[2] if isinstance(payload, str): udpPkt.len += len(payload) else: udpPkt.len += len(payload.hdr()) udpPkt.set_payload(payload) udpPkt.parsed = True ipPkt = Ipv4() ipPkt.flags = Ipv4.DF_FLAG ipPkt.srcip = src[1] ipPkt.dstip = dst[1] ipPkt.id = ipId ipPkt.protocol = Ipv4.UDP_PROTOCOL ipPkt.set_payload(udpPkt) ipPkt.iplen += udpPkt.len ipPkt.parsed = True udpPkt.csum = checksum(udpPkt) ethPkt = Ethernet() ethPkt.src = src[0] ethPkt.dst = dst[0] ethPkt.type = Ethernet.IP_TYPE ethPkt.set_payload(ipPkt) ethPkt.parsed = True return ethPkt
def buildRequest(src=ETHER_ANY, dst=ETHER_ANY, typ=Ethernet.IP_TYPE, payload=''): packet = Ethernet() packet.src = src packet.dst = dst packet.type = typ packet.next = payload # TODO build packet.arr packet.parsed = True return packet
def buildResponse(req, packet, **kwargs): resp = Ethernet() resp.src = req.dst resp.dst = req.src resp.type = req.type resp.set_payload(packet) if len(kwargs) != 0: for key in kwargs.keys(): if key not in resp.__dict__.keys(): continue setattr(resp, key, kwargs[key]) resp.parsed = True return resp
def buildArpRequest(srcMac, srcIp, dstIp): arpPkt = Arp() arpPkt.hwsrc = srcMac arpPkt.hwlen = 6 arpPkt.protolen = 4 arpPkt.opcode = Arp.REQUEST arpPkt.protosrc = srcIp arpPkt.protodst = dstIp arpPkt.parsed = True ethPkt = Ethernet() ethPkt.src = srcMac ethPkt.dst = ETHER_BROADCAST ethPkt.type = Ethernet.ARP_TYPE ethPkt.set_payload(arpPkt) ethPkt.parsed = True return ethPkt
def buildFrameFromConn(conn, tcpPkt): arr = getattr(tcpPkt, "arr", None) if arr is None: tcpPkt.arr = array("B", tcpPkt.hdr(tcpPkt.payload, calc_checksum=False)) tcpPkt.arr.fromstring(tcpPkt.payload) tcpPkt.tcplen = len(tcpPkt.arr) offres = len(tcpPkt.hdr(tcpPkt.payload, calc_checksum=False)) * 4 tcpPkt.off = offres >> 4 tcpPkt.res = offres & 0x0F ipPkt = Ipv4() ipPkt.flags = Ipv4.DF_FLAG ipPkt.srcip = conn.my_ip ipPkt.dstip = conn.other_ip ipPkt.protocol = Ipv4.TCP_PROTOCOL log.info("tcp packet = %s" % str(tcpPkt)) log.info("srcip = %s" % str(ipPkt.srcip)) log.info("dstip = %s, unsigned = %x" % (str(ipPkt.dstip), ipPkt.dstip.toUnsigned())) if not (isSyn(tcpPkt) and isAck(tcpPkt)): ipPkt.id = conn.ipId if conn.ipId is not None else random.randint(0, 2 ** 16 - 1) conn.ipId = ipPkt.id + 1 else: ipPkt.id = 0 conn.ipId = random.randint(0, 2 ** 16 - 1) tcpPkt.payload_len = len(tcpPkt.next) tcpPkt.prev = ipPkt ipPkt.iplen = len(ipPkt.hdr(tcpPkt)) + tcpPkt.tcplen tcpPkt.csum = tcpPkt.checksum() ipPkt.set_payload(tcpPkt) ethPkt = Ethernet() ethPkt.src = conn.my_mac ethPkt.dst = conn.other_mac ethPkt.type = Ethernet.IP_TYPE ethPkt.set_payload(ipPkt) return ethPkt