Skip to content
This repository was archived by the owner on Dec 6, 2023. It is now read-only.

Commit 9c1259b

Browse files
author
byt3bl33d3r
committed
Fixed a bunch errors in the SMB Spider (closes #117)
Additionally, regexes are now pre-compiled before starting the spider
1 parent 74f7465 commit 9c1259b

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

cme/spider/smbspider.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from cme.remotefile import RemoteFile
33
from impacket.smb3structs import FILE_READ_DATA
44
from impacket.smbconnection import SessionError
5+
from sys import exit
56
import re
67
import 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

Comments
 (0)