#!C:\Python25_32bit\python.exe # (c) 2010 Adam Pridgen adam@praetoriangrp.com, adam.pridgen@thecoverofnight.com # mini_image_cap_server.py: # Basic Server to capture a hexlified binary image, and unhexlify the file # and write it to disk # # # GPLv3 License # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . from binascii import unhexlify from socket import * import sys, os def create_bin(fname): f = open(fname).readlines() data = "" for i in f: i = i.strip() if i =="": continue data += unhexlify(i) return data def convert_write_bin(infile, outfile): data = create_bin(infile) f = open(outfile, "wb") f.write(data) f.close() def read_all(in_): sock,a = in_.accept() temp = None data = '' while temp != '': temp = sock.recv(1024) if temp == '': break data += temp return data def listen_and_capture(server, dfilename, end): f = open(dfilename, 'w') QUIT = False while not QUIT: data = read_all(server) sys.stdout.write("Recieved %u bytes of data.\n"%(len(data))) if data.find(end) > -1: data = data[:data.find(end)] QUIT = True f.write(data) f.close() sys.stdout.write("Done listening for hexlified data") if __name__ == "__main__": usage = "%s dest_filename end_str host port\n"%(sys.argv[0]) if len(sys.argv) != 5: sys.stderr.write(usage) sys.exit(0) dfilename = sys.argv[1] end = sys.argv[2] host = sys.argv[3] port = int(sys.argv[4]) server = socket() server.bind((host,port)) server.listen(100) sys.stdout.write("Starting the capture server.\n") listen_and_capture(server, dfilename, end)