-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolicy-parser
executable file
·322 lines (300 loc) · 13.1 KB
/
policy-parser
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/usr/bin/env python
import sys, getopt, re, os
"""
================================================================================
=
= This file is part of seorigin
= Copyright (C) 2012 Devan Franchini, Anthony G. Basile, Sven Vermeulen
=
= seorigin is free software: you can redistribute it and/or modify
= it under the terms of the GNU General Public License as published by
= the Free Software Foundation, either version 3 of the License, or
=
= seorigin is distributed in the hope that it will be useful,
= but WITHOUT ANY WARRANTY; without even the implied warranty of
= MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
= GNU General Public License for more details.
=
= You should have received a copy of the GNU General Public License
= along with seorigin. If not, see <http://www.gnu.org/licenses/>.
=
= ----
= policy-parser.py is the parsing component of seorigin, the SELinux Policy originator.
=
= Purpose:
= The parser takes the m4 marco expansion of SELinux policies and parses it into a suitable and
= useful manner for the workflow component of the policy originator to database.
=
= See README for more information.
=
================================================================================
"""
"""
usage() is printed out in 2 cases:
1.) The -h flag is set in the execution of the script.
2.) There is an exception caught.
"""
def usage():
print("Proper usage:\npolicy-parser -i(--input) [file to parse] -o(--output) [parsed file location]")
sys.exit(0)
"""
parse_cmd_agrs() sets up the -i -o and -h flags for the policy-parser script. See usage for what each flag is.
"""
def parse_cmd_args():
shortOpts = 'i:o:h'
longOpts = ['input=','output=','help']
opts, extraparams = getopt.getopt(sys.argv[1:], shortOpts, longOpts)
inputCheck = False # Boolean check to see if input location has been set.
outputCheck = False # Boolean check to see if output location has been set.
defOutFile = os.path.join(os.environ["PWD"],'parsed_output.txt') # Default output location.
#Set up arguement flags for script execution.
for o, p in opts:
if o in ['-i', '--input']:
if os.path.exists(p):
inputFile=p
if not os.access(p, os.R_OK):
print("\nFile %s does not have read permissions!\n" % p)
sys.exit()
else:
print("\nFile %s does not exist!\nPlease specify new input file.\n" % p)
inputCheck = True
elif o in ['-o', '--output']:
outputFile=p
outputCheck= True
elif o in ['-h', '--help']:
usage()
# Sanity check to make sure the parsed information is getting written to some location.
if not inputCheck and outputCheck:
print ("\nInput location not specified, please specify input file or\ndefault to debug.log located in:")
print ("refpolicy/debug.log")
sys.exit()
if not outputCheck and inputCheck:
print ("\nOutput location not specified, please specify output file.")
sys.exit()
if not outputCheck and not inputCheck:
print("\nInput file and output file both not specified. Please specify files.")
sys.exit()
return( inputFile, outputFile )
"""
readInput( inputFile ) reads in the specified file. If a file does not exist it will shoot an exception and print out
usage().
"""
def readInput( inputFile ):
try:
f=open(inputFile, 'r') # This creates the function for opening the file, and assigns it to f.
except Exception as err:
print("\n\n Error: {0}".format(err),"\n\n")
usage()
fileLines = f.readlines()
f.close()
return fileLines
"""
parseFile( lines ) the lines specified of raw SELinux m4 macro expansions and puts them into Source, & Definition records.
For our intents and purposes we'll be parsing entire input files.
"""
def parseFile( lines ):
output = ''
sourceCount = 0
definitionCount = 0
oldLine = 1208845
multiLineCheck = False
definitionLine = False
gen_requireCheck = False
sptMultiLineCheck = False
for line in lines:
# Beginning of source record parsing.
# Parse only if we have a .te line
if re.search('\.te:', line):
multiLineCheck = False
line = re.sub('dnl.*$', '', line) # Remove macro comments
line = re.sub('\m4trace:','', line)
line = re.sub(': \-\d+\-', '', line)
# Take line and break the parts after the file location into the source expression
sourceExpression = re.sub('^.*\d+ ', '',line)
sourceExpression = re.sub('^policy.*$', '', sourceExpression)
sourceExpression = re.sub('[ififn]def.*$', '', sourceExpression)
sourceExpression = re.sub('ifelse.*$', '', sourceExpression)
sourceExpression = re.sub('pushdef.*$', '', sourceExpression)
sourceExpression = re.sub('popdef.*$', '', sourceExpression)
sourceExpression = re.sub('^optional_policy.*', '', sourceExpression)
sourceExpression = re.sub('^incr\(.*$', '', sourceExpression)
sourceExpression = re.sub('gen_require.*$', '', sourceExpression)
sourceExpression = re.sub('gen_tunable.*$', '', sourceExpression) # gen_tunable omitted from source records
sourceExpression = re.sub('incr\(\d+\)->\d+', '', sourceExpression)
sourceExpression = re.sub('\->.*$', '', sourceExpression)
sourceExpression = re.sub('_perms.*$', '', sourceExpression)
sourceExpression = re.sub('^.*regexp.*$', '', sourceExpression)
sourceExpression = re.sub('^i$', '', sourceExpression)
sourceExpression = re.sub('^if$', '', sourceExpression)
sourceExpression = re.sub('\n', '', sourceExpression)
sourceCall = re.sub('\(.*$', '', sourceExpression)
sourceCallArgs = re.sub('^.*\(', '(', sourceExpression)
# Greedily remove everything after line number and turn it into our source location
sourceLocation = re.sub('\s+.*$', '', line)
sourceLineNum = re.sub('\s+.*$', '', line)
sourceLineNum = re.sub('\w+/', '', sourceLineNum)
sourceLineNum = re.sub('\w+\.te:', '', sourceLineNum)
if re.search('^\w*$', sourceExpression): # Skip lines that are all white space
continue
if re.search('^\w*$', line):
continue
if re.search('^\w*$', sourceLocation):
continue
if re.search(sourceLineNum, sourceLocation):
currentLine = sourceLineNum
if currentLine == oldLine:
continue
else:
oldLine = currentLine
if not re.search('tunable_policy', line):
sourceLocation = re.sub(':\d+', '', sourceLocation)
sourceCount += 1
output += "\n"
output += "## source record "
output += str(sourceCount)
output += "\n"
output += "# " + sourceLocation
output += "\n"
output += "# line: " + sourceLineNum
output += "\n"
output += sourceCall + sourceCallArgs
output += "\n"
# End of the source record parsing. Beginning of the definition record parsing.
# Parse out only if we have an "all_interfaces.conf:" line
if re.search('\m4trace:tmp/all_interfaces.conf:', line):
multiLineCheck = True
definitionCall = re.sub('^.*\- ', '', line)
definitionCall = re.sub('dnl.*$', '', definitionCall)
definitionCall = re.sub('divert\(-1\)','', definitionCall)
definitionCall = re.sub('define\(','', definitionCall)
definitionCall = re.sub(',', '', definitionCall)
if re.search('^\w*$', definitionCall):
continue
# Skips any popdef() lines
if re.search('popdef', line):
multiLineCheck = False
if multiLineCheck:
# Bypasses any .fc files.
if re.search('\.fc:.*$', line):
continue
definition = re.sub('\#.*$', '', line)
definition = re.sub('^.*\d+\- ', '', definition)
definition = re.sub('divert\(-1\)','', definition)
definition = re.sub('[ififn]def.*$', '', definition)
definition = re.sub('^.dnl*.$', '', definition)
definition = re.sub('pushdef.*$', '', definition)
definition = re.sub('popdef.*$', '', definition)
definition = re.sub('tunable_policy.*', '', definition)
definition = re.sub('optional_policy.*', '', definition)
definition = re.sub('gen_tunable.*$', '', definition) # gen_tunable is omitted from all_interfaces.conf
definition = re.sub('refpolicywarn.*$', '', definition)
definition = re.sub('policy_m4_comment.*$', '', definition)
if re.search('gen_require\(\`.*$', definition):
gen_requireCheck = True
elif re.search('^\t\w', definition):
gen_requireCheck = False
if gen_requireCheck:
definition = re.sub('.*$', '', definition)
definition = re.sub('\n', '', definition)
definition = re.sub('\t', '', definition)
definition = re.sub('^\)', '', definition)
definition = re.sub('^\'\).*$', '', definition)
if re.search('^\w*$', definition):
continue
if re.search('^define', definition):
definitionLine = True
else:
definitionLine = False
if definitionLine:
definitionCount += 1
output += "\n"
output += "## definition record "
output += str(definitionCount)
output += "\n"
output += "# " + definitionCall
definition = re.sub('^define\(.*$', '', definition)
if re.search('^\w*$', definition):
continue
output += definition
output += '\n'
if re.search('\.spt:', line):
line = re.sub('^.*\- ', '', line)
# SDC = .spt definition call
SDC = re.sub('define\(', '', line)
SDC = re.sub(',.*$', '', SDC)
SDC = re.sub('\n', '', SDC)
if re.search('.*_pattern', SDC):
sptMultiLineCheck = True
elif re.search('.*_perms', SDC):
sptMultiLineCheck = True
else:
sptMultiLineCheck = False
if sptMultiLineCheck:
if re.search('\.fc:.*$', line):
line = re.sub('\n', '', line)
continue
if re.search('\.m4.*$', line):
continue
if re.search('^define', line):
# SDL = spt Definition Line
SDL = True
else:
SDL = False
sptDefinition = re.sub('\}\)', '}', line)
sptDefinition = re.sub(' \)$', '', sptDefinition)
sptDefinition = re.sub('^\)', '', sptDefinition)
sptDefinition = re.sub(SDC + '\, ', '', sptDefinition)
if re.search('gen_require\(\`.*$', sptDefinition):
gen_requireCheck = True
elif re.search('^\t\w', sptDefinition):
gen_requireCheck = False
if gen_requireCheck:
sptDefinition = re.sub('.*$', '', sptDefinition)
sptDefinition = re.sub('^\)', '', sptDefinition)
sptDefinition = re.sub('/.*$', '', sptDefinition)
if re.search('^\w*$', line):
continue
if SDL:
definitionCount += 1
output += '\n'
output += "## definition record "
output += str(definitionCount)
output += "\n"
output += "# " + SDC
sptDefinition = re.sub('^\s+', '', sptDefinition)
sptDefinition = re.sub('\n', '', sptDefinition)
sptDefinition = re.sub('^define\(', '', sptDefinition)
if re.search('^\{.*$', sptDefinition ):
output += '\n'
output += sptDefinition
output += '\n\n'
else:
output += sptDefinition
output += '\n'
return output
"""
writeOut( outputFile, output) writes output to the file we want to have it outputted to.
"""
def writeOut( outputFile, output ):
try:
parsedOut = open(outputFile, 'w') # This assigns a new file to parsedOut
except Exception as err:
print("\nwriteOut() Error: {0}".format(err),"\n\n")
usage()
parsedOut.write(output)
parsedOut.close()
"""
main(), this is where the magic happens.
"""
def main():
print("Policy-parser v1.4.3: ")
(inputFile, outputFile) = parse_cmd_args()
lines = readInput( inputFile )
output = parseFile( lines )
writeOut( outputFile, output )
print("Policy parsing complete! Enjoy :)\n")
"""
The main function is run below.
"""
if __name__ == "__main__":
main()