This repository has been archived by the owner on May 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathflags-scanner.py
executable file
·278 lines (225 loc) · 7.21 KB
/
flags-scanner.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
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
#!/usr/bin/env python
from __future__ import print_function
"""Fast RPM analysis tool"""
# Installation
#
# yum install python-shove python-sqlalchemy python-pyelftools \
# python-libarchive
import sys
from shove import Shove
import re
import subprocess
try:
import libarchive
except ImportError:
print("Please install python-libarchive package.")
sys.exit(-1)
try:
import rpm
except ImportError, exc:
print(exc)
print("Please install rpm-python package")
sys.exit(-1)
import os
import stat
import multiprocessing
import threading
# global stuff
debug_packages = {}
RAMDISK_path = "RAMDISK"
dwarf_producer_binary = None
shove = Shove('sqlite:///dump.db')
data = {}
lock = threading.Lock()
def analyze(rpmfile):
"""Analyse single RPM file"""
if not os.path.exists(rpmfile):
print("%s doesn't exists!" % rpmfile)
return
# print(rpmfile)
if rpmfile.endswith(".src.rpm") or not rpmfile.endswith(".rpm"):
print("skipping %s" % os.path.basename(rpmfile))
return
try:
ts = rpm.TransactionSet()
ts.setVSFlags(rpm._RPMVSF_NOSIGNATURES)
fd = os.open(rpmfile, os.O_RDONLY)
h = ts.hdrFromFdno(fd)
os.close(fd)
except Exception, exc:
print(rpmfile, str(exc))
return
# create lookup dictionary
nvr = h[rpm.RPMTAG_NVR]
srpm = h[rpm.RPMTAG_SOURCERPM]
# print(srpm, rpmfile)
found = re.match("(.*)-.*-.*", srpm)
if not found:
print("regexp failed ;(", srpm)
return
package = found.groups()[0]
debug_package = package + "-debuginfo-"
package = h[rpm.RPMTAG_NAME]
group = h[rpm.RPMTAG_GROUP]
output = {}
output["package"] = package
output["group"] = group
output["build"] = os.path.basename(rpmfile)
output["files"] = []
output["nvr"] = nvr
try:
fd = open(rpmfile, "rb")
a = libarchive.Archive(fd)
except Exception, exc:
print(rpmfile, str(exc))
return
# process the binary RPM
ELFs = []
for entry in a:
size = entry.size
# skip 0 to 4 byte files only, size can be 0 due to compression also!
if size < 4 and not stat.S_ISDIR(entry.mode):
continue
# skip directories
if stat.S_ISDIR(entry.mode):
continue
# check if the "entry" is an ELF file
try:
if a.readstream(entry.pathname).read(4).startswith(b'\x7fELF'):
ELFs.append(entry.pathname)
except:
pass
if not ELFs:
a.close() # prevent handle leak!
fd.close()
return
# extract debuginfo RPM to "RAMDISK"
adebug_package = None
for _, v in debug_packages.items():
if re.match(re.escape(debug_package) + "\d", os.path.basename(v)):
adebug_package = v
break
if not adebug_package:
print('[-] missing "debuginfo" RPM for', output["build"])
a.close() # prevent handle leak!
fd.close()
return
try:
dfd = open(adebug_package, "rb")
da = libarchive.Archive(dfd)
dac = {}
except Exception, exc:
print(adebug_package, str(exc))
a.close() # prevent handle leak!
fd.close()
return
for entry in da:
size = entry.size
# skip 0 byte files only, size can be 0 due to compression also!
if size == 0 and not stat.S_ISDIR(entry.mode):
continue
# skip directories
if stat.S_ISDIR(entry.mode):
continue
dac[entry.pathname] = True
# close all file handles
a.close()
fd.close()
dfd.close()
da.close()
# extract debuginfo RPM into "RAMDISK"
print('[*] extracting "debuginfo" RPM %s for %s' % (adebug_package,
output["build"]))
p = subprocess.Popen("rpm2cpio %s | cpio -idmuv" % adebug_package,
shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=RAMDISK_path, close_fds=True)
p.communicate()
# locate the correct corresponding ".debug" file
for ELF in ELFs:
fileinfo = {}
found = False
for k, v in dac.items():
if k.endswith(os.path.basename(ELF + ".debug")):
found = k
break
if not found:
print("[-]", output["build"], "is missing debug file for", ELF)
continue
debug_path = os.path.join(RAMDISK_path,
os.path.dirname(found).lstrip("./"))
p = subprocess.Popen("%s %s" % (dwarf_producer_binary,
os.path.basename(found)),
cwd=debug_path, shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
close_fds=True)
producers, _ = p.communicate()
fileinfo[ELF] = producers.strip()
if "-fstack-protector-strong" not in producers and \
not "-fstack-protector-all" in producers:
print("%s -> %s is not using >= -fstack-protector-strong" %
(rpmfile, ELF))
if not producers:
print("[!]", output["build"],
"is missing producer information for", ELF)
continue
output["files"].append(fileinfo)
# print(output)
return output
def output_callback(result):
with lock:
if result:
# print(result)
shove[result["build"]] = result
# print(result)
else:
pass
def profile_main():
# Run 'main' redirecting its output to readelfout.txt
# Saves profiling information in readelf.profile
PROFFILE = 'readelf.profile'
import cProfile
cProfile.run('main()', PROFFILE)
# Dig in some profiling stats
import pstats
p = pstats.Stats(PROFFILE)
p.sort_stats('cumulative').print_stats(200)
def main():
global dwarf_producer_binary
if len(sys.argv) < 2:
sys.stderr.write(
"Usage: %s <path to RPM files>\n" % sys.argv[0])
sys.exit(-1)
# locate dwarf_producer binary
dwarf_producer_binary = os.path.realpath("./dwarf_producer")
# make "RAMDISK" folder
try:
os.mkdir("RAMDISK")
except:
pass
path = sys.argv[1]
for (path, _, files) in os.walk(path):
for fname in files:
# is this a "debuginfo" package?
if "-debuginfo-" in fname:
debug_packages[fname] = os.path.abspath(os.path.join(path,
fname))
p = multiprocessing.Pool(8)
outputmap = {}
for (path, _, files) in os.walk(sys.argv[1]):
for fname in files:
# is this a "debuginfo" package?
if "-debuginfo-" in fname:
continue
rpmfile = os.path.abspath(os.path.join(path, fname))
if not rpmfile.endswith(".rpm"):
continue
outputmap[rpmfile] = p.apply_async(
analyze,
[rpmfile],
callback=output_callback)
p.close()
p.join()
if __name__ == "__main__":
# profile_main()
main()