Esempio n. 1
0
def buildResponse(req, packet, serverMac, serverIp, **kwargs):
    resp = None

    typ  = extractMsgType(packet)
    if typ == Dhcp.DISCOVER_MSG:
        dhcpPkt = buildOfferMsg(packet, **kwargs)
    elif typ == Dhcp.REQUEST_MSG:
        dhcpPkt = buildAckMsg(packet, **kwargs)
    else:
        return None

    udpPkt = udp.buildUdpResponse(req, dhcpPkt)

    ipKwargs = {}
    ipKwargs['srcip'] = serverIp
    if (isinstance(dhcpPkt.yiaddr, (int, long))):
        ipKwargs['dstip'] = IPAddr(dhcpPkt.yiaddr)
    else:
        ipKwargs['dstip'] = dhcpPkt.yiaddr

    ipKwargs['protocol'] = Ipv4.UDP_PROTOCOL
    ipPkt = ipv4.buildIpResponse(req, udpPkt, **ipKwargs)
    udpPkt.csum = udp.checksum(udpPkt)

    resp = ethernet.buildResponse(req, ipPkt, src=serverMac)

    return resp
Esempio n. 2
0
def buildUnreachResp(req, packet, unreachCode):
    icmpPkt = Icmp()
    icmpPkt.type = TYPE_DEST_UNREACH
    icmpPkt.code = unreachCode
    icmpPkt.set_payload(packet)
    icmpPkt.parsed = True

    ipPkt = ipv4.buildIpResponse(req, icmpPkt, protocol=Ipv4.ICMP_PROTOCOL, tos=0xC0)

    dummy = DummyHdr("\x00\x00\x00\x00")
    icmpPkt.set_payload(dummy)
    dummy.set_payload(packet)

    ipLen = 0
    a = ipPkt
    while a is not None:
        m = getattr(a, "hdr", None)
        if m:
            n = len(a.hdr())
        else:
            n = len(a)
        ipLen += n
        a = getattr(a, "next", None)

    ipPkt.iplen = ipLen

    icmpPkt.prev = ipPkt
    ethPkt = ethernet.buildResponse(req, ipPkt)
    icmpPkt.csum = 0
    icmpPkt.csum = checksum(icmpPkt)

    return ethPkt
Esempio n. 3
0
def buildResponse(req, packet, payload=""):
    resp = None
    if payload is None:
        payload = ""
    tcpPkt = buildTcpResponse(req, packet, payload=payload)
    ipPkt = ipv4.buildResponse(req, tcpPkt, protocol=Ipv4.TCP_PROTOCOL)
    resp = ethernet.buildResponse(req, ipPkt)
    return resp
Esempio n. 4
0
def buildResponse(req, packet, payload=''):
    resp = None
    if payload is None:
        payload = ''

    udpPkt = buildUdpResponse(req, packet, payload=payload)
    ipPkt  = ipv4.buildResponse(req, udpPkt, protocol=Ipv4.UDP_PROTOCOL)
    resp   = ethernet.buildResponse(req, ipPkt)

    return resp
Esempio n. 5
0
def buildResponse(req, packet, mac):
    resp = None

    arpPkt = buildArpResponse(packet, mac)
    if arpPkt is None:
        return None

    resp = ethernet.buildResponse(req, arpPkt, src=mac)

    return resp
Esempio n. 6
0
def buildResponse(req, packet, typ):
    resp = None
    if typ == Echo:
        icmpPkt = _buildEcho(packet)
    else:
        return None

    if icmpPkt is None:
        return None

    ipPkt = ipv4.buildIpResponse(req, icmpPkt, protocol=Ipv4.ICMP_PROTOCOL)
    resp = ethernet.buildResponse(req, ipPkt)

    return resp
Esempio n. 7
0
def buildStandardTcpResponse(req, tcpPkt, payload="", ipId=None):
    tcpPkt.next = payload
    offres = 20 * 4  # header length = 20
    tcpPkt.off = offres >> 4
    tcpPkt.res = offres & 0x0F
    tcpPkt.parsed = True
    hdr = tcpPkt.hdr(payload, calc_checksum=False)
    tcpPkt.arr = hdr
    tcpPkt.tcplen = len(hdr)

    kwargs = {}
    if ipId is None and isSyn(tcpPkt) and isAck(tcpPkt):
        kwargs["id"] = 0

    kwargs["protocol"] = Ipv4.TCP_PROTOCOL
    ipPkt = ipv4.buildIpResponse(req, tcpPkt, **kwargs)
    if ipId is not None:
        ipPkt.id = ipId

    ipPkt.iplen = len(hdr) + tcpPkt.tcplen
    tcpPkt.csum = tcpPkt.checksum()
    resp = ethernet.buildResponse(req, ipPkt)

    return resp