<s> from kippo . core . honeypot import HoneyPotCommand <EOL> from kippo . core . fs import * <EOL> from twisted . web import client <EOL> from twisted . internet import reactor <EOL> import stat <EOL> import time <EOL> import urlparse <EOL> import random <EOL> import re <EOL> import exceptions <EOL> import os . path <EOL> import getopt <EOL> commands = { } <EOL> def tdiff ( seconds ) : <EOL> t = seconds <EOL> days = int ( t / ( <NUM_LIT> * <NUM_LIT> * <NUM_LIT> ) ) <EOL> t -= ( days * <NUM_LIT> * <NUM_LIT> * <NUM_LIT> ) <EOL> hours = int ( t / ( <NUM_LIT> * <NUM_LIT> ) ) <EOL> t -= ( hours * <NUM_LIT> * <NUM_LIT> ) <EOL> minutes = int ( t / <NUM_LIT> ) <EOL> t -= ( minutes * <NUM_LIT> ) <EOL> s = '<STR_LIT>' % int ( t ) <EOL> if minutes >= <NUM_LIT:1> : s = '<STR_LIT>' % ( minutes , s ) <EOL> if hours >= <NUM_LIT:1> : s = '<STR_LIT>' % ( hours , s ) <EOL> if days >= <NUM_LIT:1> : s = '<STR_LIT>' % ( days , s ) <EOL> return s <EOL> def sizeof_fmt ( num ) : <EOL> for x in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:M>' , '<STR_LIT>' , '<STR_LIT:T>' ] : <EOL> if num < <NUM_LIT> : <EOL> return "<STR_LIT>" % ( num , x ) <EOL> num /= <NUM_LIT> <EOL> def splitthousands ( s , sep = '<STR_LIT:U+002C>' ) : <EOL> if len ( s ) <= <NUM_LIT:3> : return s <EOL> return splitthousands ( s [ : - <NUM_LIT:3> ] , sep ) + sep + s [ - <NUM_LIT:3> : ] <EOL> class command_wget ( HoneyPotCommand ) : <EOL> def start ( self ) : <EOL> try : <EOL> optlist , args = getopt . getopt ( self . args , '<STR_LIT>' ) <EOL> except getopt . GetoptError , err : <EOL> self . writeln ( '<STR_LIT>' ) <EOL> self . exit ( ) <EOL> return <EOL> if len ( args ) : <EOL> url = args [ <NUM_LIT:0> ] . strip ( ) <EOL> else : <EOL> self . writeln ( '<STR_LIT>' ) <EOL> self . writeln ( '<STR_LIT>' ) <EOL> self . nextLine ( ) <EOL> self . writeln ( '<STR_LIT>' ) <EOL> self . exit ( ) <EOL> return <EOL> outfile = None <EOL> for opt in optlist : <EOL> if opt [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> outfile = opt [ <NUM_LIT:1> ] <EOL> if '<STR_LIT>' not in url : <EOL> url = '<STR_LIT>' % url <EOL> urldata = urlparse . urlparse ( url ) <EOL> if outfile is None : <EOL> outfile = urldata . path . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] <EOL> if not len ( outfile . strip ( ) ) or not urldata . path . count ( '<STR_LIT:/>' ) : <EOL> outfile = '<STR_LIT>' <EOL> outfile = self . fs . resolve_path ( outfile , self . honeypot . cwd ) <EOL> path = os . path . dirname ( outfile ) <EOL> if not path or not self . fs . exists ( path ) or not self . fs . is_dir ( path ) : <EOL> self . writeln ( '<STR_LIT>' % outfile ) <EOL> self . exit ( ) <EOL> return <EOL> self . url = url <EOL> self . limit_size = <NUM_LIT:0> <EOL> cfg = self . honeypot . env . cfg <EOL> if cfg . has_option ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . limit_size = int ( cfg . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . safeoutfile = '<STR_LIT>' % ( cfg . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> time . strftime ( '<STR_LIT>' ) , <EOL> re . sub ( '<STR_LIT>' , '<STR_LIT:_>' , url ) ) <EOL> self . deferred = self . download ( url , outfile , self . safeoutfile ) <EOL> if self . deferred : <EOL> self . deferred . addCallback ( self . success ) <EOL> self . deferred . addErrback ( self . error , url ) <EOL> def download ( self , url , fakeoutfile , outputfile , * args , ** kwargs ) : <EOL> try : <EOL> parsed = urlparse . urlparse ( url ) <EOL> scheme = parsed . scheme <EOL> host = parsed . hostname <EOL> port = parsed . port or ( <NUM_LIT> if scheme == '<STR_LIT>' else <NUM_LIT> ) <EOL> path = parsed . path or '<STR_LIT:/>' <EOL> if scheme == '<STR_LIT>' or port != <NUM_LIT> : <EOL> self . writeln ( '<STR_LIT>' ) <EOL> self . exit ( ) <EOL> return None <EOL> elif scheme != '<STR_LIT:http>' : <EOL> raise exceptions . NotImplementedError <EOL> except : <EOL> self . writeln ( '<STR_LIT>' % ( url , ) ) <EOL> self . exit ( ) <EOL> return None <EOL> self . writeln ( '<STR_LIT>' % ( time . strftime ( '<STR_LIT>' ) , url ) ) <EOL> self . writeln ( '<STR_LIT>' % ( host , port ) ) <EOL> self . write ( '<STR_LIT>' ) <EOL> factory = HTTPProgressDownloader ( <EOL> self , fakeoutfile , url , outputfile , * args , ** kwargs ) <EOL> out_addr = None <EOL> if self . honeypot . env . cfg . has_option ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> out_addr = ( self . honeypot . env . cfg . get ( '<STR_LIT>' , '<STR_LIT>' ) , <NUM_LIT:0> ) <EOL> self . connection = reactor . connectTCP ( <EOL> host , port , factory , bindAddress = out_addr ) <EOL> return factory . deferred <EOL> def ctrl_c ( self ) : <EOL> self . writeln ( '<STR_LIT>' ) <EOL> self . connection . transport . loseConnection ( ) <EOL> def success ( self , data ) : <EOL> self . exit ( ) <EOL> def error ( self , error , url ) : <EOL> if hasattr ( error , '<STR_LIT>' ) : <EOL> error = error . getErrorMessage ( ) <EOL> self . writeln ( error ) <EOL> self . exit ( ) <EOL> commands [ '<STR_LIT>' ] = command_wget <EOL> class HTTPProgressDownloader ( client . HTTPDownloader ) : <EOL> def __init__ ( self , wget , fakeoutfile , url , outfile , headers = None ) : <EOL> client . HTTPDownloader . __init__ ( self , url , outfile , headers = headers , <EOL> agent = '<STR_LIT>' ) <EOL> self . status = None <EOL> self . wget = wget <EOL> self . fakeoutfile = fakeoutfile <EOL> self . lastupdate = <NUM_LIT:0> <EOL> self . started = time . time ( ) <EOL> self . proglen = <NUM_LIT:0> <EOL> self . nomore = False <EOL> def noPage ( self , reason ) : <EOL> if self . status == '<STR_LIT>' : <EOL> client . HTTPDownloader . page ( self , '<STR_LIT>' ) <EOL> else : <EOL> client . HTTPDownloader . noPage ( self , reason ) <EOL> def gotHeaders ( self , headers ) : <EOL> if self . status == '<STR_LIT>' : <EOL> self . wget . writeln ( '<STR_LIT>' ) <EOL> if headers . has_key ( '<STR_LIT>' ) : <EOL> self . totallength = int ( headers [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ) <EOL> else : <EOL> self . totallength = <NUM_LIT:0> <EOL> if headers . has_key ( '<STR_LIT>' ) : <EOL> self . contenttype = headers [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> else : <EOL> self . contenttype = '<STR_LIT>' <EOL> self . currentlength = <NUM_LIT:0.0> <EOL> if self . totallength > <NUM_LIT:0> : <EOL> self . wget . writeln ( '<STR_LIT>' % ( self . totallength , <EOL> sizeof_fmt ( self . totallength ) , <EOL> self . contenttype ) ) <EOL> else : <EOL> self . wget . writeln ( '<STR_LIT>' % ( self . contenttype ) ) <EOL> if self . wget . limit_size > <NUM_LIT:0> and self . totallength > self . wget . limit_size : <EOL> print '<STR_LIT>' % ( self . wget . url , ) <EOL> self . fileName = os . path . devnull <EOL> self . nomore = True <EOL> else : <EOL> msg = '<STR_LIT>' % ( self . wget . url , self . fileName ) <EOL> self . wget . honeypot . logDispatch ( msg ) <EOL> print msg <EOL> self . wget . writeln ( '<STR_LIT>' % self . fakeoutfile ) <EOL> self . wget . honeypot . terminal . nextLine ( ) <EOL> return client . HTTPDownloader . gotHeaders ( self , headers ) <EOL> def pagePart ( self , data ) : <EOL> if self . status == '<STR_LIT>' : <EOL> self . currentlength += len ( data ) <EOL> if not self . nomore and self . wget . limit_size > <NUM_LIT:0> and self . currentlength > self . wget . limit_size : <EOL> print '<STR_LIT>' <EOL> self . nomore = True <EOL> self . file . close ( ) <EOL> self . fileName = os . path . devnull <EOL> self . file = self . openFile ( data ) <EOL> if ( time . time ( ) - self . lastupdate ) < <NUM_LIT:0.5> : <EOL> return client . HTTPDownloader . pagePart ( self , data ) <EOL> if self . totallength : <EOL> percent = ( self . currentlength / self . totallength ) * <NUM_LIT:100> <EOL> spercent = "<STR_LIT>" % percent <EOL> else : <EOL> spercent = '<STR_LIT>' % ( self . currentlength / <NUM_LIT:1000> ) <EOL> percent = <NUM_LIT:0> <EOL> self . speed = self . currentlength / ( time . time ( ) - self . started ) <EOL> eta = ( self . totallength - self . currentlength ) / self . speed <EOL> s = '<STR_LIT>' % ( spercent . rjust ( <NUM_LIT:3> ) , <EOL> ( '<STR_LIT>' % ( int ( <NUM_LIT> / <NUM_LIT> * percent ) * '<STR_LIT:=>' ) ) . ljust ( <NUM_LIT> ) , <EOL> splitthousands ( str ( int ( self . currentlength ) ) ) . ljust ( <NUM_LIT:12> ) , <EOL> self . speed / <NUM_LIT:1000> , <EOL> tdiff ( eta ) ) <EOL> self . wget . write ( s . ljust ( self . proglen ) ) <EOL> self . proglen = len ( s ) <EOL> self . lastupdate = time . time ( ) <EOL> return client . HTTPDownloader . pagePart ( self , data ) <EOL> def pageEnd ( self ) : <EOL> if self . totallength != <NUM_LIT:0> and self . currentlength != self . totallength : <EOL> return client . HTTPDownloader . pageEnd ( self ) <EOL> self . wget . write ( '<STR_LIT>' % ( '<STR_LIT>' % ( <NUM_LIT> * '<STR_LIT:=>' ) , <EOL> splitthousands ( str ( int ( self . totallength ) ) ) . ljust ( <NUM_LIT:12> ) , <EOL> self . speed / <NUM_LIT:1000> ) ) <EOL> self . wget . honeypot . terminal . nextLine ( ) <EOL> self . wget . honeypot . terminal . nextLine ( ) <EOL> self . wget . writeln ( <EOL> '<STR_LIT>' % ( time . strftime ( '<STR_LIT>' ) , <EOL> self . speed / <NUM_LIT:1000> , <EOL> self . fakeoutfile , self . currentlength , self . totallength ) ) <EOL> self . wget . fs . mkfile ( self . fakeoutfile , <NUM_LIT:0> , <NUM_LIT:0> , self . totallength , <NUM_LIT> ) <EOL> self . wget . fs . update_realfile ( <EOL> self . wget . fs . getfile ( self . fakeoutfile ) , <EOL> self . wget . safeoutfile ) <EOL> return client . HTTPDownloader . pageEnd ( self ) </s>
