-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplasticcable_cli.py
190 lines (175 loc) · 6.83 KB
/
plasticcable_cli.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
#!/usr/bin/python
import psutil, sqlite3, os, time, base64, socket, signal, datetime, ctypes, hashlib
from inspect import getmembers
from pprint import pprint
#file hashing
#https://stackoverflow.com/a/44873382
signal.signal(signal.SIGINT, signal.SIG_DFL) #So we can Ctrl+C out
DEBUG=True
def debugPrint(str):
if(DEBUG):
print(str)
def file_hash(filename):
h = hashlib.sha256()
with open(filename, 'rb', buffering=0) as f:
for b in iter(lambda : f.read(128*1024), b''):
h.update(b)
return h.hexdigest()
def var_dump(var):
pprint(getmembers(var))
#https://stackoverflow.com/questions/1026431/cross-platform-way-to-check-admin-rights-in-a-python-script-under-windows
def isAdmin():
try:
is_admin = os.geteuid() == 0
except AttributeError:
is_admin = ctypes.windll.shell32.IsUserAnAdmin() != 0
return is_admin
def printConnections():
print("psutil.net_connections()=")
for connection in psutil.net_connections():
status = str(connection[5])
if status == 'ESTABLISHED':
localaddress = ""
localport = ""
destinationaddress = ""
destinationport = ""
try:
connectionstring = str(connection)
print("connectingstring="+connectiongstring)
localaddress = str(connection[3][0])
localhostname = str(socket.gethostbyaddr(localaddress)[0])
localport = str(connection[3][1])
destinationaddress = str(connection[4][0])
destinationport = str(connection[4][1])
destinationhostname = "<Could not resolve>"
destinationhostname = str(socket.gethostbyaddr(destinationaddress)[0])
except Exception as e:
pass
if localaddress != '127.0.0.1' and destinationaddress != '127.0.0.1':
print("Local Address="+localaddress+ " hostname="+localhostname+ " port="+localport)
print("Destination Address="+destinationaddress+ " hostname="+destinationhostname+ " port="+destinationport)
print("Status="+status)
index = connectionstring.find("pid=")
pid = int(connectionstring[index+4:-1])
process = psutil.Process(pid)
print("program="+str(process.name()))
try:
print("path="+str(process.exe()))
except Exception as e:
print("path=access denied")
#new line
print("datetime="+str(datetime.datetime.now()))
print("")
def buildDatabase():
debugPrint ("DEBUG-buildDatabase")
for connection in psutil.net_connections():
status = str(connection[5])
destinationaddress = "<?>"
if status == 'ESTABLISHED':
localaddress = ""
localport = ""
destinationaddress = ""
destinationport = ""
pid = "None"
try:
localaddress = str(connection[3][0])
destinationaddress = str(connection[4][0])
except Exception as e:
pass
if localaddress != '127.0.0.1' and destinationaddress != '127.0.0.1':
connectionstring = str(connection)
index = int(connectionstring.find("pid="))
try: #if pid is still None this will throw an error
pid = int(connectionstring[index+4:-1])
except Exception as e:
break
#pass
process = psutil.Process(pid)
path = "<Access denied>"
name = str(process.name())
debugPrint ("DEBUG-name="+name)
try:
path = str(process.exe())
hash = file_hash(path)
except Exception as e:
hash = "<Access denied>"
pass
debugPrint ("DEBUG-path="+path)
debugPrint ("DEBUG-name+path="+name+path)
debugPrint ("DEBUG-hash="+hash)
conn = sqlite3.connect('plasticcable.db')
sqlreturned = conn.execute('''SELECT HASH FROM APPS WHERE HASH = "'''+hash+'''"''')
rowcount = len(sqlreturned.fetchall())
conn.close()
debugPrint ("DEBUG-buildDatabase()sqlreturned.rowcount="+str(rowcount))
if rowcount == 0:
date = str(datetime.datetime.now())
print(date+" - New application found:"+name+" at "+path+", adding to database")
destinationport = "<?>"
try:
destinationport = str(connection[4][1])
except Exception as e:
pass
try: #do this here as hostname lookup is slow
destinationhostname = "<Could not resolve>"
destinationhostname = str(socket.gethostbyaddr(destinationaddress)[0])
except Exception as e:
pass
debugPrint ("DEBUG-name="+path)
conn = sqlite3.connect('plasticcable.db')
conn.execute ('''INSERT INTO APPS (NAME,PATH,HASH,DESTIP,DESTPORT,DESTHOST,DATE) VALUES (?,?,?,?,?,?,?);''', (name, path, hash,destinationaddress,destinationport,destinationhostname,date))
conn.commit()
conn.close()
else:
debugPrint("DEBUG-Already in database\n")
def printDatabase():
debugPrint ("DEBUG-printDatabase()")
conn = sqlite3.connect('plasticcable.db')
print("Currently in database:")
sqlreturned = conn.execute('''SELECT NAME, PATH, HASH, DESTIP, DESTPORT, DESTHOST, DATE FROM APPS''')
queryresult = sqlreturned.fetchall()
rowcount = len(queryresult)
debugPrint ("DEBUG-sqlreturned.rowcount="+str(rowcount))
if rowcount > 0:
for row in queryresult:
print(str(row[0]))
print(str(row[1]))
print(str(row[2]))
print(str(row[3]))
print(str(row[4]))
print(str(row[5]))
print(str(row[6]))
print("")
else:
print("Database is empty.")
conn.close()
if __name__ == "__main__":
debugPrint("DEBUG - start")
if not isAdmin():
exit("Please Run as Administrator, use an elevated cmd.")
firstStart = False
if not(os.path.isfile('plasticcable.db')):
debugPrint ("DEBUG-plasticcable.db does not exist.")
firstStart = True
else:
debugPrint ("DEBUG-plasticcable.db exists.")
if(firstStart):
print("First start of PlasticCable, creating app database...")
conn = sqlite3.connect('plasticcable.db')
conn.execute('''CREATE TABLE APPS
(NAME TEXT NOT NULL,
PATH TEXT NOT NULL,
HASH TEXT NOT NULL,
DESTIP TEXT NOT NULL,
DESTPORT TEXT NOT NULL,
DESTHOST TEXT NOT NULL,
DATE DATE NOT NULL);''')
print("Database created successfully.");
conn.close()
#print str(datetime.datetime.now())
print("Monitoring socket connections.")
while(True):
#printConnections()
buildDatabase()
#printDatabase()
time.sleep(2)