-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathShellFlow.py
222 lines (194 loc) · 6.68 KB
/
ShellFlow.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/python
import sys
import re
import collections
from graphviz import Digraph
# Parsing based on Bash Grammar provided in link : http://wiki.bash-hackers.org/syntax/basicgrammar
class BashParser:
shellbuiltInWords="alias,bg,bind,break,builtin,caller,cd,command,compgen,complete,compopt,continue,declare,dirs,disown,echo,enable,eval,exec,exit,export,false,fc,fg,getopts,hash,help,history,jobs,kill,let,local,logout,mapfile,popd,printf,pushd,pwd,read,readarray,readonly,return,set,shift,shopt,source,suspend,test,times,trap,true,type,typeset,ulimit,umask,unalias,unset,wait"
usualCommands="mkdir,rmdir,rm -rf,ls,ps,tree,find,grep,egrep,sed,awk,ifconfig,ping,rm,du,df,less,more,test"
complexWords="for,if,else,elif,fi,do,done,while,{,},((,)),[[,]],case,esac,until,select"
compound="(),{},(()),[[ ]],(( ))"
pipes=">,>>,2>&1,|,&,||,;"
def __init__(self):
self.line=""
def parse(self, cmdString):
cmdString=cmdString.strip()
cmd=None
runCommand=cmdString.split(" ")[0].lower()
if ( ("||" not in cmdString) and ("|" in cmdString)):
cmd=PipelineCommand(cmdString)
return cmd
elif ( "()" in cmdString or "function " in cmdString):
cmd=BashFunction(cmdString.replace("function","").replace("(","").replace(")","").strip())
return cmd
elif runCommand in self.complexWords:
cmd=CompoundCommand(cmdString)
return cmd
elif runCommand in self.usualCommands:
cmd=UsualCommand(cmdString)
return cmd
elif runCommand in self.shellbuiltInWords:
cmd=BuiltinCommand(cmdString)
return cmd
elif (("=" in cmdString) and ("==" not in cmdString)):
cmd=AssignmentCommand(cmdString)
return cmd
else:
cmd=BashCommand(runCommand)
return cmd
class BashCommand:
def __init__(self, cmdString):
self.cmd=cmdString.split(" ")[0]
self.shape="box"
self.cmdType="Other"
def printGraph(self, dot):
#if ("FUNCTION" in self.cmdType):
return dot.node(self.cmdType,self.cmd.upper(),shape=self.shape)
#else:
# return None
class BlockCommand:
def __init__(self, cmdString):
self.cmds=[]
self.shape="box3d"
self.cmdType="block"
def printGraph(self, dot):
#if ("FUNCTION" in self.cmdType):
return dot.node(self.cmdType,self.cmd,shape=self.shape)
#else:
# return None
class AssignmentCommand(BashCommand):
#builtInWords=self.shellbuiltInWords.split(",")
def __init__(self, cmdString):
super(AssignmentCommand, self).__init__(cmdString.split("=")[0])
self.shape="box"
self.cmdType="SET"
def isBuiltin():
return True;
class BuiltinCommand (BashCommand):
#builtInWords=self.shellbuiltInWords.split(",")
def __init__(self, cmdString):
super(BuiltinCommand, self).__init__(cmdString)
self.shape="box"
self.cmdType="BUILTIN"
def isBuiltin():
return True;
class UsualCommand (BashCommand):
#builtInWords=self.usualCommands.split(",")
def __init__(self, cmdString):
super(UsualCommand, self).__init__(cmdString)
self.cmdType="USUAL"
self.shape="box"
def isBuiltin():
return false
class PipelineCommand (BashCommand):
def __init__(self, cmdString):
super(PipelineCommand, self).__init__(cmdString)
self.cmdType="PIPELINE"
self.shape="cds"
self.leftCmd=cmdString
self.rightCmd=cmdString
class ListCommand (BashCommand):
def __init__(self, cmdString):
super(ListCommand, self).__init__(cmdString)
self.cmd=cmdString.split(" ")[0]
#self.cmdType="LIST"
#self.cmdList.add(cmdString.split("&&,&,;,||,"))
self.shape="hexagon"
#if ( ("&&" in cmdString) or ("&" in cmdString)):
# self.cmd="AND"
#elif ("||" in cmdString):
# self.cmd="OR"
class CompoundCommand (BlockCommand):
def __init__(self, cmdString):
super(CompoundCommand, self).__init__(cmdString)
#self.cmdType="COMPOUND"
if ("if" in cmdString or "then" in cmdString or "fi" in cmdString or "else" in cmdString ):
self.cmdType="IF"
self.cmd=cmdString.split(" ")[0].upper()
self.shape="diamond"
else:
self.cmdType="LOOP"
self.cmd=cmdString.split(" ")[0].upper()
self.shape="box3d"
#self.cmds=[cmdString.split(" ")[0]]
def findCmdType(self):
'''
for command
while, do while loop commands
if then elif command
do done command
sub-shell or execute commands
{} - run a s group command
(()) and [[]] expressions
'''
class BashFunction (BlockCommand):
def __init__(self, cmdString):
super(BashFunction, self).__init__(cmdString)
self.cmd=cmdString
self.cmdType="FUNCTION"
self.shape="ellipse"
#if "}" in cmdString :
#self.cmdType="FUNCTION - END"
#else:
#self.cmdType="FUNCTION - START"
self.commandsInBlock=[]
def Grammar(bashCommand):
SingleQuoteRegEx='(\\\'.*?\\\')'
DoubleQuoteRegEx='(\\\".*?\\\")'
VariableRegEx='\$[\{].*?[\}]'
BackQuoteRegEx='(`).*?(`)'
SubShellRegEx='($\().*?(\))'
TestCmdRegEx='($\[\[).*?(\]\])'
Test2CmdRegEx='($\[).*?(\])'
Others='.*?'
line= re.sub(SingleQuoteRegEx,'CMD_CONSTANTVAR', bashCommand)
line= re.sub(BackQuoteRegEx,'CMD_SUBSHELL', line)
line= re.sub(SubShellRegEx,'CMD_SUBSHELL2', line)
line=re.sub(TestCmdRegEx,"TESTINPUT",line)
line=re.sub(Test2CmdRegEx,"TEST2INPUT", line)
return line
# Strip comments, empty lines from the bash script and load the file
def readScriptFile(fileName):
with open(fileName,"r") as fileObj:
content = fileObj.readlines()
content = [line.strip() for line in content if (re.search("^[ ]*#",line)==None) and (re.match(r'^\s*$', line)==None)]
return content
if __name__ == "__main__":
lines = readScriptFile(sys.argv[1])
dot=Digraph(comment="Shell script analysis")
precmd=None
dq=collections.deque()
for line in lines:
bparser = BashParser()
grammarLine=Grammar(line.strip())
print(grammarLine)
currentCmd=bparser.parse(grammarLine)
try:
prevcmd=dq.pop()
print(prevcmd.cmdType + " vs "+ currentCmd.cmdType)
#print(currentCmd.cmdType)
if (prevcmd.cmdType != currentCmd.cmdType):
#or prevcmd.cmd!=currentCmd.cmd):
if (prevcmd.cmd != currentCmd.cmd):
dq.append(prevcmd)
except IndexError:
print("ER")
pass
dq.append(currentCmd)
while True:
try:
dotNode=dq.popleft().printGraph(dot)
except IndexError:
break
#if ((precmd is not None) and (dotNode is not None)):
#if (isinstance(cmd,BashCommand)):
#dot.edge(precmd.cmd,cmd.cmd,"Next")
#precmd=cmd
# print("[*] " + line.strip() + "=>" + cmd.cmd)
#else:
# dot.edge(precmd.cmd,cmd.cmds[0],"Next")
# precmd=cmd
# print("[*] " + line.strip() + "=>" + cmds.cmds[0])
dot.render("siva.gv", view=True)
#if not line.trim().startswith(pattern) for pattern in builtInWords