Created
April 20, 2017 06:02
-
-
Save wbchn/926bcde22614779268d2d617faf4a1e5 to your computer and use it in GitHub Desktop.
抓包发包,来自网络
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: UTF-8 -*- | |
#Resend packets in pcap file | |
#Created 7/30/2016, thimer | |
import struct | |
import socket | |
import sys | |
import time | |
def hexofbin(bin, show=4): | |
size = len(bin) | |
s = '%4d - ' % size | |
if size > show * 2: | |
s += bin[:show].encode('hex') | |
s += ' ... ' | |
s += bin[-show:].encode('hex') | |
else: | |
s+= bin.encode('hex') | |
return s | |
def send_udp(data, host, port=80): | |
print 'Send UDP: %s/%d <= %s' % (host, port, hexofbin(data)) | |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
sock.sendto(data, (host, port)) | |
class Parser: | |
def __init__(self, filename): | |
self.fh = open(filename, 'rb') | |
self.global_header() | |
def __del__(self): | |
self.fh.close() | |
def global_header(self): | |
hdr_fmt = '=IHHiIII' | |
hdr_len = struct.calcsize(hdr_fmt) | |
hdr_unpack = struct.Struct(hdr_fmt).unpack_from | |
data = self.fh.read(hdr_len) | |
if not data: | |
return None | |
s = hdr_unpack(data) | |
#print s | |
(magic_number, ver_major, ver_minor, thiszone, sigfigs, snaplen, network) = s | |
self.version = (ver_major, ver_minor) | |
self.snaplen = snaplen | |
self.ts0 = 0 | |
self.index = 0 | |
return magic_number | |
def packet(self): | |
hdr_fmt = '=4I' | |
hdr_len = struct.calcsize(hdr_fmt) | |
hdr_unpack = struct.Struct(hdr_fmt).unpack_from | |
data = self.fh.read(hdr_len) | |
if not data: | |
return None | |
s = hdr_unpack(data) | |
#print s | |
(ts_sec, ts_usec, incl_len, orig_len) = s | |
data = self.fh.read(incl_len) | |
ts = ts_sec + ts_usec / 1000000.0 | |
if self.index == 0: | |
self.ts0 = ts | |
time = ts - self.ts0 | |
self.index += 1 | |
payload = data[42:] | |
print '%4d, %7.3f, %4d > %s' % (self.index, time, orig_len, hexofbin(payload)) | |
if (incl_len != orig_len): | |
print 'Warning: incl_len(%d) != orig_len(%d)' % (incl_len, orig_len) | |
return (self.index, payload) | |
def help(): | |
print 'Usage: %s target-host pcap-file packet-index1 packet-index2 ...' % sys.argv[0] | |
return -1 | |
def sendpcap(host, pcapfile, indexes): | |
#print indexes | |
parser = Parser(pcapfile) | |
while True: | |
info = parser.packet() | |
if info is None: | |
break | |
(index, payload) = info | |
if index in indexes: | |
send_udp(payload, host) | |
#time.sleep(1) | |
return 0 | |
def main(): | |
if len(sys.argv) < 3: | |
return help() | |
host = sys.argv[1] | |
pcapfile = sys.argv[2] | |
indexes = map(int, sys.argv[3:]) | |
return sendpcap(host, pcapfile, indexes) | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment