-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindpriv.py
executable file
·77 lines (71 loc) · 2.37 KB
/
findpriv.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
#!/usr/bin/env python3
import os
import stat
import sys
from subprocess import Popen, PIPE
import argparse
# initialize the variables
path = "/"
noofExecutables = 0
noOfFiles = 0
setuidexecs = []
capaware = []
searchSetUidFiles = False
isearchCapableFiles = False
# set requirements based on command line arguments
parser = argparse.ArgumentParser(description='flags for findpriv')
parser.add_argument('-p')
parser.add_argument('-c',action='store_true')
parser.add_argument('-s',action='store_true')
args = parser.parse_args()
if args.p != None:
path = args.p
searchSetUidFiles = args.s
searchCapableFiles = args.c
if len(sys.argv)==1:
path = "/"
searchSetUidFiles = True
searchCapableFiles = True
# search the file system
for root, directories, files in os.walk(path):
for name in files:
noOfFiles+=1
filename = os.path.join(root, name)
if not os.path.isfile(filename):
continue
# noOfFiles+=1
status = os.stat(filename)
if (status.st_mode & stat.S_IXUSR) or (status.st_mode & stat.S_IXGRP) or (status.st_mode & stat.S_IXOTH) :
noofExecutables+=1
# check for setuid
if searchSetUidFiles:
if (status.st_mode & stat.S_ISUID) or (status.st_mode & stat.S_ISGID):
setuidexecs.append(filename)
# check for capabilites
if searchCapableFiles:
process = Popen(['getcap', filename], stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()
details = stdout.decode('ascii')
if details != "":
details = details.split(' ')
result = [details[0]]
details = details[1].split(',')
for x in details:
result.append(x.split('=')[0])
capaware.append(result)
# print the data
print("Scanned ",noOfFiles," files, found ",noofExecutables," executables")
if searchSetUidFiles:
print("setuid executables: ",len(setuidexecs))
for x in setuidexecs:
print(x)
if searchCapableFiles:
print("capability-aware executables: ",len(capaware))
for x in capaware:
print(x[0],end=' ')
for i in range(1,len(x)):
print(x[i],end='')
if i < len(x)-1:
print(',',end='')
print("")
# The End - author: Sarath