<s> """<STR_LIT>""" <EOL> import threading <EOL> import sys <EOL> import re <EOL> import os <EOL> import optparse <EOL> import base64 <EOL> import stat <EOL> import fnmatch <EOL> import time <EOL> from hashlib import md5 <EOL> import urllib2 <EOL> import cgi <EOL> import datetime <EOL> class PhpSerializer ( threading . Thread ) : <EOL> def __init__ ( self ) : <EOL> threading . Thread . __init__ ( self ) <EOL> def unserialize ( self , s ) : <EOL> return PhpSerializer . _unserialize_var ( self , s ) [ <NUM_LIT:0> ] <EOL> def _unserialize_var ( self , s ) : <EOL> return ( <EOL> { '<STR_LIT:i>' : PhpSerializer . _unserialize_int <EOL> , '<STR_LIT:b>' : PhpSerializer . _unserialize_bool <EOL> , '<STR_LIT:d>' : PhpSerializer . _unserialize_double <EOL> , '<STR_LIT:n>' : PhpSerializer . _unserialize_null <EOL> , '<STR_LIT:s>' : PhpSerializer . _unserialize_string <EOL> , '<STR_LIT:a>' : PhpSerializer . _unserialize_array <EOL> } [ s [ <NUM_LIT:0> ] . lower ( ) ] ( self , s [ <NUM_LIT:2> : ] ) ) <EOL> def _unserialize_int ( self , s ) : <EOL> x = s . partition ( '<STR_LIT:;>' ) <EOL> return ( int ( x [ <NUM_LIT:0> ] ) , x [ <NUM_LIT:2> ] ) <EOL> def _unserialize_bool ( self , s ) : <EOL> x = s . partition ( '<STR_LIT:;>' ) <EOL> return ( x [ <NUM_LIT:0> ] == '<STR_LIT:1>' , x [ <NUM_LIT:2> ] ) <EOL> def _unserialize_double ( self , s ) : <EOL> x = s . partition ( '<STR_LIT:;>' ) <EOL> return ( float ( x [ <NUM_LIT:0> ] ) , x [ <NUM_LIT:2> ] ) <EOL> def _unserialize_null ( self , s ) : <EOL> return ( None , s ) <EOL> def _unserialize_string ( self , s ) : <EOL> ( l , _ , s ) = s . partition ( '<STR_LIT::>' ) <EOL> return ( s [ <NUM_LIT:1> : int ( l ) + <NUM_LIT:1> ] , s [ int ( l ) + <NUM_LIT:3> : ] ) <EOL> def _unserialize_array ( self , s ) : <EOL> ( l , _ , s ) = s . partition ( '<STR_LIT::>' ) <EOL> a , k , s = { } , None , s [ <NUM_LIT:1> : ] <EOL> for i in range ( <NUM_LIT:0> , int ( l ) * <NUM_LIT:2> ) : <EOL> ( v , s ) = PhpSerializer . _unserialize_var ( self , s ) <EOL> if k : <EOL> a [ k ] = v <EOL> k = None <EOL> else : <EOL> k = v <EOL> return ( a , s [ <NUM_LIT:1> : ] ) <EOL> class ShellDetector ( threading . Thread ) : <EOL> _extension = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> _command_line = True <EOL> _showlinenumbers = True <EOL> _dateformat = "<STR_LIT>" <EOL> _directory = '<STR_LIT:.>' <EOL> _scan_hidden = True <EOL> _report_format = '<STR_LIT>' <EOL> _remotefingerprint = False <EOL> _output = "<STR_LIT>" <EOL> _files = [ ] <EOL> _badfiles = [ ] <EOL> _fingerprints = [ ] <EOL> _title = '<STR_LIT>' <EOL> _version = '<STR_LIT>' <EOL> _regex = r"<STR_LIT>" <EOL> def __init__ ( self , options ) : <EOL> threading . Thread . __init__ ( self ) <EOL> if options . extension is not None : <EOL> self . _extension = options . extension . split ( '<STR_LIT:U+002C>' ) <EOL> self . _showlinenumbers = options . linenumbers <EOL> if options . directory is not None : <EOL> self . _directory = options . directory <EOL> """<STR_LIT>""" <EOL> self . _remotefingerprint = options . remote . lower ( ) in ( "<STR_LIT:yes>" , "<STR_LIT:true>" , "<STR_LIT:t>" , "<STR_LIT:1>" ) <EOL> def remote ( self ) : <EOL> if self . _remotefingerprint is True : <EOL> self . alert ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> url = '<STR_LIT>' <EOL> self . _fingerprints = urllib2 . urlopen ( url ) . read ( ) <EOL> try : <EOL> self . _fingerprints = base64 . decodestring ( bytes ( self . _fingerprints ) ) <EOL> serial = PhpSerializer ( ) <EOL> self . _fingerprints = serial . unserialize ( str ( self . _fingerprints ) ) <EOL> except IOError as e : <EOL> print ( "<STR_LIT>" . format ( e ) ) <EOL> else : <EOL> if os . path . isfile ( "<STR_LIT>" ) : <EOL> try : <EOL> self . _fingerprints = base64 . decodestring ( str ( open ( '<STR_LIT>' , '<STR_LIT:r>' ) . read ( ) ) ) <EOL> serial = PhpSerializer ( ) <EOL> self . _fingerprints = serial . unserialize ( str ( self . _fingerprints ) ) <EOL> except IOError as e : <EOL> print ( "<STR_LIT>" . format ( e ) ) <EOL> def start ( self ) : <EOL> self . header ( ) <EOL> self . remote ( ) <EOL> self . version ( ) <EOL> self . filescan ( ) <EOL> self . anaylize ( ) <EOL> self . footer ( ) <EOL> return None <EOL> def anaylize ( self ) : <EOL> _counter = <NUM_LIT:0> <EOL> _regex = re . compile ( self . _regex ) <EOL> for _filename in self . _files : <EOL> _content = open ( _filename , '<STR_LIT>' , - <NUM_LIT:1> ) . read ( ) <EOL> _filename = re . sub ( '<STR_LIT>' , '<STR_LIT>' , _filename ) <EOL> _match = _regex . findall ( _content ) <EOL> if _match : <EOL> self . getfileinfo ( _filename ) <EOL> if self . _showlinenumbers is True : <EOL> _lines = _content . split ( "<STR_LIT:\n>" ) <EOL> _linecounter = <NUM_LIT:1> <EOL> for _line in _lines : <EOL> _match_line = _regex . findall ( _line ) <EOL> if _match_line : <EOL> self . alert ( '<STR_LIT>' + _match_line . __str__ ( ) + '<STR_LIT>' + str ( _linecounter ) + '<STR_LIT:)>' ) <EOL> _linecounter += <NUM_LIT:1> <EOL> else : <EOL> self . alert ( '<STR_LIT>' + _match . __str__ ( ) ) <EOL> _counter += <NUM_LIT:1> <EOL> self . fingerprint ( _filename , _content ) <EOL> self . alert ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . alert ( '<STR_LIT>' + str ( _counter ) + '<STR_LIT>' + str ( len ( self . _badfiles ) ) + '<STR_LIT>' , '<STR_LIT>' ) <EOL> def _get_precomputed_fingerprints ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _precomputed_fingerprints = [ ] <EOL> for fingerprint , shellname in self . _fingerprints . iteritems ( ) : <EOL> if fingerprint == "<STR_LIT:version>" : <EOL> continue <EOL> if '<STR_LIT>' in fingerprint : <EOL> fingerprint = base64 . decodestring ( bytes ( fingerprint . replace ( '<STR_LIT>' , '<STR_LIT>' ) ) ) <EOL> self . _precomputed_fingerprints . append ( ( re . compile ( re . escape ( fingerprint ) ) , shellname ) ) <EOL> return self . _precomputed_fingerprints <EOL> def fingerprint ( self , _filename , _content ) : <EOL> for _regex , shellname in self . _get_precomputed_fingerprints ( ) : <EOL> _match = _regex . findall ( base64 . b64encode ( _content ) ) <EOL> if _match : <EOL> self . _badfiles . append ( [ _filename ] ) <EOL> _regex_shell = re . compile ( '<STR_LIT>' ) <EOL> _match_shell = list ( _regex_shell . findall ( shellname ) [ <NUM_LIT:0> ] ) <EOL> _shell_note = '<STR_LIT>' <EOL> if _match_shell [ <NUM_LIT:2> ] == <NUM_LIT:1> : <EOL> _shell_note = '<STR_LIT>' <EOL> elif _match_shell [ <NUM_LIT:2> ] == <NUM_LIT:2> : <EOL> _shell_note = '<STR_LIT>' <EOL> _shellflag = _match_shell [ <NUM_LIT:0> ] + '<STR_LIT:(>' + _match_shell [ <NUM_LIT:3> ] + '<STR_LIT:)>' <EOL> self . alert ( '<STR_LIT>' + str ( _shellflag ) + '<STR_LIT:U+0020>' + _shell_note , '<STR_LIT>' ) <EOL> def unpack ( self ) : <EOL> """<STR_LIT>""" <EOL> def getfileinfo ( self , _file ) : <EOL> ( mode , ino , dev , nlink , uid , gid , size , atime , mtime , ctime ) = os . stat ( _file ) <EOL> self . alert ( '<STR_LIT>' ) <EOL> self . alert ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . alert ( '<STR_LIT>' ) <EOL> self . alert ( '<STR_LIT>' + _file ) <EOL> self . alert ( '<STR_LIT>' + os . path . abspath ( _file ) ) <EOL> self . alert ( '<STR_LIT>' + str ( uid ) + '<STR_LIT::>' + str ( gid ) ) <EOL> self . alert ( '<STR_LIT>' + oct ( mode ) [ - <NUM_LIT:3> : ] ) <EOL> self . alert ( '<STR_LIT>' + time . ctime ( atime ) ) <EOL> self . alert ( '<STR_LIT>' + time . ctime ( mtime ) ) <EOL> self . alert ( '<STR_LIT>' + self . sizeof_fmt ( size ) ) <EOL> self . alert ( '<STR_LIT>' ) <EOL> def sizeof_fmt ( self , num ) : <EOL> for x in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if num < <NUM_LIT> : <EOL> return "<STR_LIT>" % ( num , x ) <EOL> num /= <NUM_LIT> <EOL> def version ( self ) : <EOL> try : <EOL> _version = self . _fingerprints [ '<STR_LIT:version>' ] <EOL> except ValueError : <EOL> _version = <NUM_LIT:0> <EOL> try : <EOL> _server_version = urllib2 . urlopen ( '<STR_LIT>' ) . read ( ) <EOL> except ValueError : <EOL> _server_version = <NUM_LIT:0> <EOL> if _server_version == <NUM_LIT:0> : <EOL> self . alert ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> if _server_version < _version : <EOL> self . alert ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> try : <EOL> _app_server_version = urllib2 . urlopen ( '<STR_LIT>' ) . read ( ) <EOL> except urllib2 . HTTPError : <EOL> _app_server_version = <NUM_LIT:0> <EOL> if _app_server_version == <NUM_LIT:0> : <EOL> self . alert ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> if _server_version < _version : <EOL> self . alert ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def filescan ( self ) : <EOL> self . alert ( '<STR_LIT>' ) <EOL> self . alert ( '<STR_LIT>' + str ( len ( self . _fingerprints ) ) ) <EOL> self . listdir ( ) <EOL> self . alert ( '<STR_LIT>' + str ( len ( self . _files ) ) + '<STR_LIT>' ) <EOL> def listdir ( self ) : <EOL> for root , dirnames , filenames in os . walk ( self . _directory ) : <EOL> for extension in self . _extension : <EOL> for filename in fnmatch . filter ( filenames , '<STR_LIT>' + extension ) : <EOL> self . _files . append ( os . path . join ( root , filename ) ) <EOL> return None <EOL> def header ( self ) : <EOL> self . alert ( '<STR_LIT>' ) <EOL> self . alert ( '<STR_LIT>' ) <EOL> self . alert ( '<STR_LIT>' ) <EOL> self . alert ( '<STR_LIT>' ) <EOL> self . alert ( '<STR_LIT>' ) <EOL> self . alert ( '<STR_LIT>' ) <EOL> self . alert ( '<STR_LIT>' ) <EOL> self . alert ( '<STR_LIT>' ) <EOL> def footer ( self ) : <EOL> self . alert ( '<STR_LIT>' ) <EOL> self . alert ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . alert ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . alert ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . alert ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . alert ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . alert ( '<STR_LIT>' ) <EOL> def alert ( self , _content , _color = '<STR_LIT>' , _class = '<STR_LIT:info>' , _html = False , _flag = False ) : <EOL> _color_result = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } [ _color ] <EOL> if self . supports_color ( ) is True : <EOL> print _color_result + _content + '<STR_LIT>' <EOL> else : <EOL> print _content <EOL> if _flag is True : <EOL> self . output ( _content , _class , _html ) <EOL> def supports_color ( self ) : <EOL> """<STR_LIT>""" <EOL> plat = sys . platform <EOL> supported_platform = plat != '<STR_LIT>' and ( plat != '<STR_LIT:win32>' or <EOL> '<STR_LIT>' in os . environ ) <EOL> is_a_tty = hasattr ( sys . stdout , '<STR_LIT>' ) and sys . stdout . isatty ( ) <EOL> if not supported_platform or not is_a_tty : <EOL> return False <EOL> return True <EOL> def output ( self , _content , _class = '<STR_LIT:info>' , _html = True ) : <EOL> if _html is True : <EOL> self . _output += '<STR_LIT>' + _class + '<STR_LIT>' + _content + '<STR_LIT>' <EOL> else : <EOL> self . _output += _content <EOL> def flush ( self ) : <EOL> if self . _command_line is False : <EOL> print ( "<STR_LIT>" ) <EOL> filename = datetime . now ( ) . strftime ( self . _report_format ) <EOL> file = open ( filename , "<STR_LIT:w>" , - <NUM_LIT:1> , '<STR_LIT:utf-8>' ) <EOL> file . write ( self . _output ) <EOL> parser = optparse . OptionParser ( ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , type = "<STR_LIT:string>" , default = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , default = True , help = "<STR_LIT>" ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , type = "<STR_LIT:string>" , help = "<STR_LIT>" ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , default = "<STR_LIT:False>" , help = "<STR_LIT>" ) <EOL> ( options , args ) = parser . parse_args ( ) <EOL> if len ( sys . argv ) == <NUM_LIT:1> : <EOL> parser . print_usage ( ) <EOL> parser . print_help ( ) <EOL> else : <EOL> shell = ShellDetector ( options ) <EOL> shell . start ( ) </s>
