Try except added to re.sub to prevent plugin from crashing if incorrect regexes where provided. Removed default values for search_str and replace_str

This commit is contained in:
Ruben 2014-07-18 14:29:55 +10:00
parent d6f3405f71
commit 5c1dbb3596

View file

@ -21,7 +21,7 @@ class Replace(CacheKill,Plugin):
self.implements.remove("handleHeader") self.implements.remove("handleHeader")
self.implements.remove("connectionMade") self.implements.remove("connectionMade")
if self.search_str==self.replace_str=="" and self.regex_file is None: if (self.search_str==self.replace_str==None or self.search_str==self.replace_str=="") and self.regex_file is None:
print "[*] Please provide a search and replace string or a regex file" print "[*] Please provide a search and replace string or a regex file"
quit() quit()
@ -36,15 +36,21 @@ class Replace(CacheKill,Plugin):
if self._should_replace(ip,hn,mime): if self._should_replace(ip,hn,mime):
if self.search_str==self.replace_str!="": # Did the user provide us a search and replace str?
if self.search_str==self.replace_str!=None and self.search_str==self.replace_str!="":
data = data.replace(self.search_str, self.replace_str) data = data.replace(self.search_str, self.replace_str)
logging.info("%s [%s] Replaced '%s' with '%s'" % (request.client.getClientIP(), request.headers['host'], self.search_str, self.replace_str)) logging.info("%s [%s] Replaced '%s' with '%s'" % (request.client.getClientIP(), request.headers['host'], self.search_str, self.replace_str))
# DI the user provide us with a regex file?
if self.regex_file is not None: if self.regex_file is not None:
for line in self.regex_file: for line in self.regex_file:
replaceRegex = line.split("\t") replaceRegex = line.split("\t")
try:
data = re.sub(replaceRegex[0], replaceRegex[1], data) data = re.sub(replaceRegex[0], replaceRegex[1], data)
logging.info("%s [%s] Replaced '%s' with '%s'" % (request.client.getClientIP(), request.headers['host'], replaceRegex[0], replaceRegex[1])) logging.info("%s [%s] Replaced '%s' with '%s'" % (request.client.getClientIP(), request.headers['host'], replaceRegex[0], replaceRegex[1]))
except Exception, e:
logging.error("%s [%s] Your provided regex (%s) or replace value (%s) is empyt or invalid. Please debug your provided regex(es)" % (request.client.getClientIP(), request.headers['host'], replaceRegex[0], replaceRegex[1]))
self.ctable[ip] = time.time() self.ctable[ip] = time.time()
self.dtable[ip+hn] = True self.dtable[ip+hn] = True
@ -54,9 +60,9 @@ class Replace(CacheKill,Plugin):
return return
def add_options(self,options): def add_options(self,options):
options.add_argument("--regex-file",type=file,help="Load file with regexes") options.add_argument("--replace-str",type=str,help="String you would like to replace.")
options.add_argument("--replace-str",type=str,default="",help="String you would like to replace.") options.add_argument("--search-str",type=str,help="String you would like to replace --replace-str with. Default: '' (empty string)")
options.add_argument("--search-str",type=str,default="",help="String you would like to replace --replace-str with. Default: '' (empty string)") options.add_argument("--regex-file",type=file,help="Load file with regexes. File format: <regex1>[tab]<regex2>[new-line]")
options.add_argument("--keep-cache",action="store_true",help="Don't kill the server/client caching.") options.add_argument("--keep-cache",action="store_true",help="Don't kill the server/client caching.")
def _should_replace(self,ip,hn,mime): def _should_replace(self,ip,hn,mime):