-
Notifications
You must be signed in to change notification settings - Fork 6
/
joern_filter_cfgs
executable file
·74 lines (54 loc) · 3.16 KB
/
joern_filter_cfgs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python2
import argparse
import sourceutils.pythonCFGFilter.create as pythonCFGFilterCreate
from sourceutils.pythonCFGFilter.pruning.PredicatedActionsFilter import PredicatedActionsFilter
from sourceutils.pythonCFGFilter.pruning.IdentityFilter import IdentityFilter
from sourceutils.pythonCFGFilter.pruning.CFGFilter import CFGFilter
from sourceutils.pythonCFGFilter.pruning.row2string.CFGRow2String import CFGRow2String
def stringList(value):
return value.split(',')
class CLI():
def __init__(self):
self.initializeOptParser()
self.parseCommandLine()
def initializeOptParser(self):
self.argParser = argparse.ArgumentParser(description = "Filters control flow graphs created by joern_parse.")
self.argParser.add_argument("project_dir", help="directory containing the output of joern_parse")
self.argParser.add_argument('--filter', choices=['predicatedActions', 'identity'], default = 'predicatedActions')
self.argParser.add_argument("--row2StringConverter", choices= ['default'], default='default')
self.argParser.add_argument("--customFilterNodes", type=stringList, default=None, help="Comma separated list of node types to preserve. The filter will discard all other nodes.")
self.argParser.add_argument("--customFilterDiscard", action="store_true", default=False, help="If set, all nodes specified by customFilterNodes will be discarded and all other nodes will be preserved")
self.argParser.add_argument("--customFilterNodesNotToExpand", type=stringList, default=None, help="Nodes not to expand")
self.argParser.add_argument("--customFilterMergeRows", type=stringList, default=None, help="Row types to merge")
def parseCommandLine(self):
self.args = self.argParser.parse_args()
def createFilterFromDescription(self):
print 'creating custom filter'
newFilter = CFGFilter()
newFilter.keepNodesOfInterest = not self.args.customFilterDiscard
newFilter.row2StringConverter = self.row2StringConverter
newFilter.nodeTypesOfInterest = self.args.customFilterNodes
newFilter.dontExpandNodes = self.args.customFilterNodesNotToExpand
newFilter.mergRows = self.args.customFilterMergeRows
return newFilter
def userWantsCustomFilter(self):
return self.args.customFilterNodes != None
def filterNameToFilter(self, filterName):
if filterName == 'predicatedActions':
return PredicatedActionsFilter()
elif filterName == 'identity':
return IdentityFilter()
def row2StringNameToConverter(self, name):
if name == 'default':
return CFGRow2String()
def run(self):
projectDir = self.args.project_dir
self.row2StringConverter = self.row2StringNameToConverter(self.args.row2StringConverter)
if self.userWantsCustomFilter():
f = self.createFilterFromDescription()
else:
f = self.filterNameToFilter(self.args.filter)
pythonCFGFilterCreate.main(projectDir, f, self.row2StringConverter)
if __name__ == '__main__':
cli = CLI()
cli.run()