<s> import dshell <EOL> import re <EOL> import datetime <EOL> import sys <EOL> import string <EOL> import re <EOL> import os <EOL> import hashlib <EOL> import util <EOL> from httpdecoder import HTTPDecoder <EOL> class DshellDecoder ( HTTPDecoder ) : <EOL> def __init__ ( self ) : <EOL> HTTPDecoder . __init__ ( self , <EOL> name = '<STR_LIT>' , <EOL> description = '<STR_LIT>' , <EOL> filter = '<STR_LIT>' , <EOL> author = '<STR_LIT>' , <EOL> optiondict = { '<STR_LIT>' : { '<STR_LIT:action>' : '<STR_LIT:store_true>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' : { '<STR_LIT:action>' : '<STR_LIT:store_true>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:default>' : '<STR_LIT:.>' } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } } <EOL> ) <EOL> def preModule ( self ) : <EOL> if self . content_filter : <EOL> self . content_filter = re . compile ( self . content_filter ) <EOL> if self . name_filter : <EOL> self . name_filter = re . compile ( self . name_filter ) <EOL> HTTPDecoder . preModule ( self ) <EOL> self . openfiles = { } <EOL> if not os . path . exists ( self . outdir ) : <EOL> try : <EOL> os . makedirs ( self . outdir ) <EOL> except ( IOError , OSError ) as e : <EOL> self . error ( "<STR_LIT>" % <EOL> ( self . outdir , e ) ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def splitstrip ( self , data , sep , strip = '<STR_LIT:U+0020>' ) : <EOL> return [ lpart . strip ( strip ) for lpart in data . split ( sep ) ] <EOL> def HTTPHandler ( self , conn , request , response , requesttime , responsetime ) : <EOL> payload = None <EOL> self . debug ( '<STR_LIT>' % ( repr ( request ) , repr ( response ) ) ) <EOL> if ( not self . direction or self . direction == '<STR_LIT>' ) and request and request . method == '<STR_LIT:POST>' and request . body : <EOL> payload = request <EOL> elif ( not self . direction or self . direction == '<STR_LIT>' ) and response and response . status [ <NUM_LIT:0> ] == '<STR_LIT:2>' : <EOL> payload = response <EOL> if payload : <EOL> if not ( not self . content_filter or self . content_filter . search ( payload . headers [ '<STR_LIT>' ] ) ) : <EOL> payload = None <EOL> if payload : <EOL> host = util . getHeader ( request , '<STR_LIT:host>' ) <EOL> if host == '<STR_LIT>' : <EOL> host = conn . serverip <EOL> url = host + request . uri <EOL> if url in self . openfiles : <EOL> self . debug ( "<STR_LIT>" % url ) <EOL> ( s , e ) = self . openfiles [ url ] . handleresponse ( response ) <EOL> self . write ( "<STR_LIT>" % ( s , e ) ) <EOL> else : <EOL> filename = request . uri . split ( '<STR_LIT:?>' ) [ <NUM_LIT:0> ] . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] <EOL> if '<STR_LIT>' in payload . headers : <EOL> cdparts = self . splitstrip ( payload . headers [ '<STR_LIT>' ] , '<STR_LIT:;>' ) <EOL> for cdpart in cdparts : <EOL> try : <EOL> k , v = self . splitstrip ( cdpart , '<STR_LIT:=>' ) <EOL> if k == '<STR_LIT:filename>' : <EOL> filename = v <EOL> except : <EOL> pass <EOL> self . debug ( "<STR_LIT>" % url ) <EOL> if not self . name_filter or self . name_filter . search ( filename ) : <EOL> if self . append_conn : <EOL> filename += '<STR_LIT>' % ( conn . serverip , <EOL> conn . clientip ) <EOL> if self . append_ts : <EOL> filename += '<STR_LIT>' % ( conn . ts ) <EOL> if not len ( filename ) : <EOL> filename = '<STR_LIT>' % ( <EOL> conn . serverip , conn . clientip ) <EOL> while os . path . exists ( os . path . join ( self . outdir , filename ) ) : <EOL> filename += '<STR_LIT:_>' <EOL> self . alert ( "<STR_LIT>" % <EOL> ( filename , url ) , conn . info ( ) ) <EOL> self . openfiles [ url ] = httpfile ( <EOL> os . path . join ( self . outdir , filename ) , self ) <EOL> ( s , e ) = self . openfiles [ url ] . handleresponse ( payload ) <EOL> self . write ( "<STR_LIT>" % ( s , e ) ) <EOL> if self . openfiles [ url ] . done ( ) : <EOL> self . alert ( "<STR_LIT>" % <EOL> ( self . openfiles [ url ] . filename , url ) , conn . info ( ) ) <EOL> del self . openfiles [ url ] <EOL> class httpfile : <EOL> def __init__ ( self , filename , decoder_instance ) : <EOL> self . complete = False <EOL> self . size = <NUM_LIT:0> <EOL> self . ranges = [ ] <EOL> self . decoder = decoder_instance <EOL> self . filename = filename <EOL> try : <EOL> self . fh = open ( filename , '<STR_LIT:w>' ) <EOL> except IOError as e : <EOL> self . decoder . error ( <EOL> "<STR_LIT>" % ( filename , e ) ) <EOL> self . fh = None <EOL> def __del__ ( self ) : <EOL> if self . fh is None : <EOL> return <EOL> self . fh . close ( ) <EOL> if not self . done ( ) : <EOL> print "<STR_LIT>" % self . filename <EOL> try : <EOL> os . rename ( self . filename , self . filename + "<STR_LIT>" ) <EOL> except : <EOL> pass <EOL> ls = <NUM_LIT:0> <EOL> le = <NUM_LIT:0> <EOL> for s , e in self . ranges : <EOL> if s > le + <NUM_LIT:1> : <EOL> print "<STR_LIT>" % ( le , s ) <EOL> ls , le = s , e <EOL> def handleresponse ( self , response ) : <EOL> range_start = <NUM_LIT:0> <EOL> range_end = len ( response . body ) - <NUM_LIT:1> <EOL> if '<STR_LIT>' in response . headers : <EOL> m = re . search ( <EOL> '<STR_LIT>' , response . headers [ '<STR_LIT>' ] ) <EOL> if m : <EOL> range_start = int ( m . group ( <NUM_LIT:1> ) ) <EOL> range_end = int ( m . group ( <NUM_LIT:2> ) ) <EOL> if len ( response . body ) < ( range_end - range_start + <NUM_LIT:1> ) : <EOL> range_end = range_start + len ( response . body ) - <NUM_LIT:1> <EOL> try : <EOL> if int ( m . group ( <NUM_LIT:3> ) ) > self . size : <EOL> self . size = int ( m . group ( <NUM_LIT:3> ) ) <EOL> except : <EOL> pass <EOL> elif '<STR_LIT>' in response . headers : <EOL> try : <EOL> if int ( response . headers [ '<STR_LIT>' ] ) > self . size : <EOL> self . size = int ( response . headers [ '<STR_LIT>' ] ) <EOL> except : <EOL> pass <EOL> self . ranges . append ( ( range_start , range_end ) ) <EOL> if self . fh is not None : <EOL> self . fh . seek ( range_start ) <EOL> self . fh . write ( response . body ) <EOL> return ( range_start , range_end ) <EOL> def done ( self ) : <EOL> self . checkranges ( ) <EOL> return self . complete <EOL> def checkranges ( self ) : <EOL> self . ranges . sort ( ) <EOL> current_start = <NUM_LIT:0> <EOL> current_end = <NUM_LIT:0> <EOL> foundgap = False <EOL> for s , e in self . ranges : <EOL> if s <= current_end + <NUM_LIT:1> : <EOL> current_end = e <EOL> else : <EOL> foundgap = True <EOL> current_start = s <EOL> current_end = e <EOL> if not foundgap : <EOL> if ( current_end + <NUM_LIT:1> ) >= self . size : <EOL> self . complete = True <EOL> return foundgap <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> dObj = DshellDecoder ( ) <EOL> print dObj <EOL> else : <EOL> dObj = DshellDecoder ( ) </s>
