Skip to content

Commit 49dda4a

Browse files
authored
Merge pull request #732 from ucgJhe/dev
make filter functioning again
2 parents 3415595 + 3b010fb commit 49dda4a

File tree

5 files changed

+37
-33
lines changed

5 files changed

+37
-33
lines changed

examples/hello_arm_set_filter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010

1111
if __name__ == "__main__":
1212
ql = Qiling(["rootfs/arm_linux/bin/arm_hello"], "rootfs/arm_linux")
13-
ql.filters = ["^open"]
13+
ql.filter = r"^open"
1414
ql.run()

qiling/core.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def __init__(
4444
log_plain=False,
4545
libcache = False,
4646
multithread = False,
47-
filters = None,
47+
filter = None,
4848
stop_on_stackpointer = False,
4949
stop_on_exit_trap = False,
5050
stdin=0,
@@ -79,7 +79,7 @@ def __init__(
7979
self._log_filter = None
8080
self._log_override = log_override
8181
self._log_plain = log_plain
82-
self._filters = filters
82+
self._filter = filter
8383
self._platform = ostype_convert(platform.system().lower())
8484
self._internal_exception = None
8585
self._uc = None
@@ -174,8 +174,8 @@ def __init__(
174174

175175
self._log_file_fd, self._log_filter = ql_setup_logger(self,
176176
self._log_file,
177-
self._console,
178-
self._filters,
177+
self._console,
178+
self._filter,
179179
self._multithread,
180180
self._log_override,
181181
self._log_plain)
@@ -640,23 +640,22 @@ def root(self, root):
640640
self._root = root
641641

642642
@property
643-
def filters(self) -> List[str]:
643+
def filter(self) -> str:
644644
""" Filter logs with regex.
645-
646-
Type: List[str]
647-
Example: - Qiling(filters=[r'^exit'])
648-
- ql.filters = [r'^open']
645+
Type: str
646+
Example: - Qiling(filter=r'^exit')
647+
- ql.filter = r'^open'
649648
"""
650-
return self._filters
649+
return self._filter
651650

652-
@filters.setter
653-
def filters(self, ft):
654-
self._filters = ft
651+
@filter.setter
652+
def filter(self, ft):
653+
self._filter = ft
655654
if self._log_filter is None:
656655
self._log_filter = RegexFilter(ft)
657656
self.log.addFilter(self._log_filter)
658657
else:
659-
self._log_filter.update_filters(ft)
658+
self._log_filter.update_filter(ft)
660659

661660
@property
662661
def uc(self):

qiling/utils.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,17 @@ def format(self, record: LogRecord):
9696
return super(QilingPlainFormatter, self).format(record)
9797

9898
class RegexFilter(logging.Filter):
99-
def __init__(self, filters):
99+
def __init__(self, regexp):
100100
super(RegexFilter, self).__init__()
101-
self.update_filters(filters)
101+
self.update_filter(regexp)
102102

103-
def update_filters(self, filters):
104-
self._filters = [ re.compile(ft) for ft in filters ]
103+
def update_filter(self, regexp):
104+
self._filter = re.compile(regexp)
105105

106106
def filter(self, record: LogRecord):
107107
msg = record.getMessage()
108-
for ft in self._filters:
109-
if re.match(ft, msg):
110-
return True
111-
return False
108+
109+
return re.match(self._filter, msg) is not None
112110

113111
class QlFileDes:
114112
def __init__(self, init):
@@ -542,7 +540,7 @@ def ql_setup_logger(ql, log_file, console, filters, multithread, log_override, l
542540
# If there aren't any filters, we do add the filters until users specify any.
543541
log_filter = None
544542

545-
if filters is not None and type(filters) == list and len(filters) != 0:
543+
if filters is not None and len(filters) != 0:
546544
log_filter = RegexFilter(filters)
547545
log.addFilter(log_filter)
548546

qltool

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def usage():
104104
print("\n\nWith code:")
105105
print("\t ./qltool code --os linux --arch arm --hex -f examples/codes/linarm32_tcp_reverse_shell.hex")
106106
print("\t ./qltool code --os linux --arch x86 --asm -f examples/codes/lin32_execve.asm")
107-
print("\t ./qltool code --os linux --arch arm --hex -f examples/codes/linarm32_tcp_reverse_shell.hex --strace")
107+
print("\t ./qltool code --os linux --arch arm --hex -f examples/codes/linarm32_tcp_reverse_shell.hex")
108108

109109
print("\n\nWith binary file:")
110110
print("\t ./qltool run -f examples/rootfs/x8664_linux/bin/x8664_hello --rootfs examples/rootfs/x8664_linux/")
@@ -122,8 +122,8 @@ def usage():
122122

123123
print("\n\nwith binary file and various output format:")
124124
print("\t ./qltool run -f examples/rootfs/mips32el_linux/bin/mips32el_hello --rootfs examples/rootfs/mips32el_linux --output=disasm")
125-
print("\t ./qltool run -f examples/rootfs/mips32el_linux/bin/mips32el_hello --rootfs examples/rootfs/mips32el_linux --strace -e ^open")
126-
print("\t ./qltool run -f examples/rootfs/mips32el_linux/bin/mips32el_hello --rootfs examples/rootfs/mips32el_linux --strace -e ^open")
125+
print("\t ./qltool run -f examples/rootfs/mips32el_linux/bin/mips32el_hello --rootfs examples/rootfs/mips32el_linux -e ^open")
126+
print("\t ./qltool run -f examples/rootfs/mips32el_linux/bin/mips32el_hello --rootfs examples/rootfs/mips32el_linux -e ^(open|brk)")
127127

128128
print("\n\nWith UEFI file:")
129129
print("\t ./qltool run -f examples/rootfs/x8664_efi/bin/TcgPlatformSetupPolicy --rootfs examples/rootfs/x8664_efi --env examples/rootfs/x8664_efi/rom2_nvar.pickel")
@@ -181,16 +181,15 @@ if __name__ == '__main__':
181181
comm_parser.add_argument('--qdb', action='store_true', required=False, help='attach Qdb at entry point, it\'s MIPS, ARM(THUMB) supported only for now')
182182
comm_parser.add_argument('--rr', action='store_true', required=False, help='switch on record and replay feature in qdb, only works with --qdb')
183183
comm_parser.add_argument('--profile', required=False, dest='profile', help="Define customized profile")
184-
comm_parser.add_argument('--strace', action='store_true', default=False, dest='strace', help='Run in strace mode')
185184
comm_parser.add_argument('--dump', action='store_true', default=False, dest='dump', help='Enable Debug + Diassembler mode')
186185
comm_parser.add_argument('--debug', action='store_true', default=False, dest='debug', help='Enable Debug mode')
187186
comm_parser.add_argument('--disasm', action='store_true', default=False, dest='disasm', help='Run in disassemble mode')
188187
comm_parser.add_argument('--console', required=False, default=True, dest='console', help='display in console')
189188
comm_parser.add_argument('-e', '--filter', metavar="FUNCTION NAME", required=False, dest="filter", default=None,
190-
help="Only work with strace mode, you can choose what to be printout, for multiple function calls should be separated by comma")
189+
help="Apply regexp for filtering log output.")
191190
comm_parser.add_argument('--log-file', dest="log_file", help="Write log to a file")
192191
comm_parser.add_argument('--log-plain', action="store_true", dest="log_plain", help="Don't use color in log output.")
193-
comm_parser.add_argument('--trace', action='store_true', default=False, dest='trace', help='Run in strace mode')
192+
comm_parser.add_argument('--trace', action='store_true', default=False, dest='trace', help='Run in trace mode')
194193
comm_parser.add_argument('--root', action='store_true', default=False, dest='root', help='Enable sudo required mode')
195194
comm_parser.add_argument('--debug_stop', action='store_true', default=False, dest='debug_stop',
196195
help='Stop running while encounter any error (only use it with debug mode)')
@@ -203,9 +202,7 @@ if __name__ == '__main__':
203202
options = parser.parse_args()
204203

205204
# var check
206-
if options.strace:
207-
options.output = "default"
208-
elif options.trace:
205+
if options.trace:
209206
options.output = "disasm"
210207
elif options.dump:
211208
options.output = "dump"

tests/test_qltool.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ def test_qltool_json(self):
4343
except subprocess.CalledProcessError as e:
4444
raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))
4545

46+
def test_qltool_filter(self):
47+
create = [sys.executable, '../qltool', 'run', '-f', '../examples/rootfs/arm_linux/bin/arm_hello', '--rootfs', '../examples/rootfs/arm_linux', '-e', '^(open|brk)', '--log-plain']
48+
try:
49+
output = subprocess.check_output(create, stderr=subprocess.STDOUT)
50+
except subprocess.CalledProcessError as e:
51+
raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))
52+
53+
lines = [ line.strip('[=]\t') for line in output.decode().split("\n")]
54+
self.assertTrue(all(filter(lambda x: x.startswith("open") or x.startswith("brk"), lines)))
55+
4656

4757
if __name__ == "__main__":
4858
unittest.main()

0 commit comments

Comments
 (0)