示例#1
0
def sendPerSecondc(timestamp,ip,dataSize,mac,packets):  
  numPacket=int(dataSize/pktSize)
  if (numPacket>0):
    if (ip in packetsMap):
	p=packetsMap[ip];
    else:      
      p=Ether(dst=mac)/IP(src=ip)/TCP()/completePayload;
      packetsMap[ip]=p;
    p.time=timestamp;
    for i in range(numPacket):
      packets.append(p);
  
  remainedData=dataSize-numPacket*pktSize
  if (remainedData>0):
    if (ip in packetsMap2):
      p=packetsMap2[ip];
      p['Raw'].load=''.zfill(remainedData);
    else:      
      p=Ether(dst=mac)/IP(src=ip)/TCP()/''.zfill(remainedData)
      packetsMap2[ip]=p
    p.time=timestamp;
    packets.append(p);
示例#2
0
def sendPerSecondc(timestamp, ip, dataSize, mac, packets):
    numPacket = int(dataSize / (pktSize + pktOverhead))
    if (numPacket > 0):
        if (ip in packetsMap):
            p = packetsMap[ip]
        else:
            p = Ether(dst=mac) / IP(src=ip) / TCP() / completePayload
            packetsMap[ip] = p
        p.time = timestamp
        for i in range(numPacket):
            packets.append(p)

    remainedData = dataSize - numPacket * (pktSize + pktOverhead)
    if (remainedData > 0):
        if (ip in packetsMap2):
            p = packetsMap2[ip]
        else:
            p = Ether(dst=mac) / IP(src=ip) / TCP()
            packetsMap2[ip] = p
        p = p / ''.zfill(min(pktSize, remainedData))
        p.time = timestamp
        packets.append(p)
示例#3
0
def sendPerSecondc(timestamp,ip,dataSize,mac,packets):
  numPacket=int(dataSize/(pktSize+pktOverhead))
  if (numPacket>0):
    if (ip in packetsMap):
        p=packetsMap[ip];
    else:
      p=Ether(dst=mac)/IP(src=ip)/TCP()/completePayload;
      packetsMap[ip]=p;
    p.time=timestamp;
    for i in range(numPacket):
      packets.append(p);

  remainedData=dataSize-numPacket*(pktSize+pktOverhead)
  if (remainedData>0):
    if (ip in packetsMap2):
      p=packetsMap2[ip];
    else:
      p=Ether(dst=mac)/IP(src=ip)/TCP()
      packetsMap2[ip]=p
    p=p/''.zfill(min(pktSize,remainedData));
    p.time=timestamp;
    packets.append(p);
示例#4
0
def add_load_to_pkt(pkt, alpha):
    if pkt.haslayer(Raw):
        load = pkt[Raw].load
        l = len(load)
        n = np.random.randint(alpha * l)
        pad = Padding()
        pad.load = '\x00' * n
        pkt = pkt / pad
        del pkt[IP].len
        del pkt[IP].chksum
        if pkt.haslayer(TCP):
            del pkt[TCP].chksum
        elif pkt.haslayer(UDP):
            del pkt[UDP].chksum
            del pkt[UDP].len
        t = pkt.time
        pkt = Ether(pkt.build())
        pkt.time = t
    return pkt
示例#5
0
def dump_pcap(reven_server, output_file="output.pcap", fix_checksum=False):
    if os.path.isfile(output_file):
        raise RuntimeError(
            '\"{}\" already exists. Choose an other output file or remove it before running the script.'
            .format(output_file))

    print("[+] Creating pcap from trace...")

    # Get all send and recv from the trace
    results = list(get_all_send_recv(reven_server))
    if len(results) == 0:
        print(
            "[+] Finished: no network packets were sent/received in the trace")
        return

    # Get packets buffers and create the pcap file.
    print("[+] Convert packets to pcap format and write to file...")
    for ctx, ty in results:
        # Just detect if send or recv context
        if ty == "send":
            buf, size = get_network_buffer_send_NdisSendNetBufferLists(ctx)
        else:
            buf, size = get_network_buffer_recv_RxPacketAssemble(ctx)

        if buf is not None:
            packet = Ether(str(buf))

            # Here we check wether or not we have to fix checksum.
            if fix_checksum:
                if TCP in packet:
                    del packet[TCP].chksum

            # Replace the time in the packet by the transition ID, so that we get
            # it in Wireshark in a nice way.
            packet.time = ctx.transition_before().id

            # Write packet to pcap file
            wrpcap(output_file, packet, append=True)

    print("[+] Finished: PCAP file is \'{}\'.".format(output_file))
示例#6
0
def gen_pkts(src_addr,
             dst_addr,
             src_port,
             dst_port,
             yfunc,
             lorem,
             seconds,
             msglen,
             hdslen,
             add_noise=True):
    random.seed(42)
    x = 0
    i = 0
    pkts = []
    while (x < 60.0):
        # build packet
        beg = random.randint(0, 1e6 - msglen - 1)
        pkt = Ether() / IP(src=src_addr, dst=dst_addr) / \
            UDP(sport=src_port, dport=dst_port) / lorem[beg:beg+msglen]
        pkt.time = x
        pkts.append(pkt)
        # calculate arrival time for next packet
        delay = 1.0 / ((yfunc(x) * 1e6) / (8 * (hdslen + msglen)))
        if add_noise is True:
            noise = random.gauss(1, 0.1)
        else:
            noise = 1.0
        x = x + noise * delay
        # count pkts
        i = i + 1
        if i % 1000 == 0:
            print(i, end='', flush=True)
        elif i % 100 == 0:
            print(end='.', flush=True)
    print('done', flush=True)
    return pkts
示例#7
0
            runconf['range'] = rs
        else:
            assert False, "unhandled option"
    return runconf

if __name__ == '__main__':
    runconf=get_runtime_options()
    pcapackets=rdpcap(runconf['input'])
    plist=[]
    count=0
    for p in pcapackets:
        if not 'UDP' in p: continue
        vpd=False
        for r in runconf['range']:
            if p['UDP'].dport >= r[0] and p['UDP'].dport <= r[1]:
                vpd=True
                break
        if not vpd: continue
        d=[]
        for i in range(6): d.append("%02x" % p['Raw'].load[i])
        dst=":".join(d)
        d=[]
        for i in range(6): d.append("%02x" % p['Raw'].load[i+6])
        src=":".join(d)
        pro=p['Raw'].load[12]*0x100 + p['Raw'].load[13]
        np=Ether(dst=dst, src=src, type=pro)/Raw(load=p['Raw'].load[14:])
        np.time=p.time
        plist.append(np)
    if runconf['output']:
        wrpcap(runconf['output'], plist)