Add refactoring of file reading

This commit is contained in:
Terencio Agozzino 2018-05-03 20:28:54 +02:00
parent 906c7951df
commit 2f802e71c7
3 changed files with 11 additions and 19 deletions

View file

@ -73,29 +73,25 @@ class AppCachePlugin(Plugin):
p = self.getTemplatePrefix(section)
self.clientlog.info("Poisoning raw URL", extra=request.clientInfo)
if os.path.exists(p + '.replace'): # replace whole content
f = open(p + '.replace', 'r')
data = f.read()
f.close()
with open(p + '.replace', 'r') as f:
data = f.read()
elif os.path.exists(p + '.append'): # append file to body
f = open(p + '.append', 'r')
data += f.read()
f.close()
with open(p + '.append', 'r') as f:
data += f.read()
elif (section.get('tamper_url',False) == url) or (section.has_key('tamper_url_match') and re.search(section['tamper_url_match'], url)):
self.clientlog.info("Found URL in section '{}'!".format(name), extra=request.clientInfo)
p = self.getTemplatePrefix(section)
self.clientlog.info("Poisoning URL with tamper template: {}".format(p), extra=request.clientInfo)
if os.path.exists(p + '.replace'): # replace whole content
f = open(p + '.replace', 'r')
data = f.read()
f.close()
with open(p + '.replace', 'r') as f:
data = f.read()
elif os.path.exists(p + '.append'): # append file to body
f = open(p + '.append', 'r')
appendix = f.read()
data = re.sub(re.compile("</body>",re.IGNORECASE), appendix + "</body>", data) #append to body
f.close()
with open(p + '.append', 'r') as f:
appendix = f.read()
data = re.sub(re.compile("</body>", re.IGNORECASE), appendix + "</body>", data) #append to body
# add manifest reference
data = re.sub(re.compile("<html",re.IGNORECASE),"<html manifest=\"" + self.getManifestUrl(section)+"\"", data)
@ -155,9 +151,8 @@ class AppCachePlugin(Plugin):
if not os.path.exists(p+'.manifest'):
p = self.getDefaultTemplatePrefix()
f = open(p + '.manifest', 'r')
manifest = f.read()
f.close()
with open(p + '.manifest', 'r') as f:
manifest = f.read()
return self.decorate(manifest, section)
def decorate(self, content, section):

View file

@ -45,7 +45,6 @@ class FerretNG(Plugin):
with open(options.cookie_file, 'r') as cookie_file:
self.cookie_file = json.dumps(cookie_file.read())
URLMonitor.getInstance().cookies = self.cookie_file
cookie_file.close()
except Exception as e:
shutdown("[-] Error loading cookie log file: {}".format(e))
@ -94,4 +93,3 @@ class FerretNG(Plugin):
self.log.info("Writing cookies to log file")
with open('./logs/ferret-ng/cookies-{}.log'.format(datetime.now().strftime("%Y-%m-%d_%H:%M:%S:%s")), 'w') as cookie_file:
cookie_file.write(str(URLMonitor.getInstance().cookies))
cookie_file.close()

View file

@ -46,7 +46,6 @@ class ScreenShotter(Inject, Plugin):
try:
with open('./logs/' + img_file, 'wb') as img:
img.write(base64.b64decode(urllib.unquote(request.postData).decode('utf8').split(',')[1]))
img.close()
self.clientlog.info('Saved screenshot to {}'.format(img_file), extra=request.clientInfo)
except Exception as e: