22from cme .remotefile import RemoteFile
33from impacket .smb3structs import FILE_READ_DATA
44from impacket .smbconnection import SessionError
5+ from sys import exit
56import re
67import traceback
78
@@ -12,6 +13,15 @@ def __init__(self, logger, connection, args):
1213 self .smbconnection = connection .conn
1314 self .start_time = time ()
1415 self .args = args
16+
17+ self .regex = None
18+ if self .args .regex :
19+ try :
20+ self .regex = [re .compile (regex ) for regex in self .args .regex ]
21+ except Exception as e :
22+ self .logger .error ('Regex compilation error: {}' .format (e ))
23+ exit (1 )
24+
1525 self .logger .info ("Started spidering" )
1626
1727 def spider (self , subfolder , depth ):
@@ -29,12 +39,16 @@ def spider(self, subfolder, depth):
2939 else :
3040 subfolder = subfolder .replace ('/*/' , '/' ) + '/*'
3141
42+ filelist = None
3243 try :
3344 filelist = self .smbconnection .listPath (self .args .share , subfolder )
3445 self .dir_list (filelist , subfolder )
3546 if depth == 0 :
3647 return
3748 except SessionError as e :
49+ if not filelist :
50+ self .logger .error ("Failed to connect to share {}: {}" .format (self .args .share , e ))
51+ return
3852 pass
3953
4054 for result in filelist :
@@ -60,9 +74,9 @@ def dir_list(self, files, path):
6074 strftime ('%Y-%m-%d %H:%M' , localtime (result .get_mtime_epoch ())),
6175 result .get_filesize ()))
6276
63- elif self .args . regex :
64- for regex in self .args . regex :
65- if re .findall (regex , result .get_longname ()):
77+ elif self .regex :
78+ for regex in self .regex :
79+ if regex .findall (result .get_longname ()):
6680 if result .is_directory ():
6781 self .logger .highlight (u"//{}/{}{} [dir]" .format (self .args .share , path , result .get_longname ()))
6882 else :
@@ -90,6 +104,8 @@ def search_content(self, path, result):
90104 while True :
91105 try :
92106 contents = rfile .read (4096 )
107+ if not contents :
108+ break
93109 except SessionError as e :
94110 if 'STATUS_END_OF_FILE' in str (e ):
95111 break
@@ -109,9 +125,9 @@ def search_content(self, path, result):
109125 rfile .tell (),
110126 pattern ))
111127
112- elif self .args . regex :
113- for regex in self .args . regex :
114- if re .findall (pattern , contents ):
128+ elif self .regex :
129+ for regex in self .regex :
130+ if regex .findall (contents ):
115131 self .logger .highlight (u"//{}/{}{} [lastm:'{}' size:{} offset:{} regex:'{}']" .format (self .args .share ,
116132 path ,
117133 result .get_longname (),
0 commit comments