-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAgent.py
151 lines (137 loc) · 3.78 KB
/
Agent.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
import os, sys,uuid
import hashlib
from multiprocessing import Process,Manager
import json
import time
import requests
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import datetime
import urllib.request
from email.utils import COMMASPACE, formatdate
from email import encoders
def dir_list(path):#this function parses through the directories until a file is found
global files
try:
dirs = os.listdir( path )
except:
return
for file in dirs:
if not file.startswith('.'):
abs_path = path+'//'+file
if os.path.isdir(abs_path):
dir_list(abs_path)
else:
files.append(abs_path)
def hash_cal(start,end,fl,hash):#this function calculates the hash value
#print(".")
for i in range(start,end+1):
#print(str(fl[i])+"<--"+str(i))
inputFile=fl[i]
with open( inputFile , "rb" ) as openedFile:
#print(openedFile)
while True:
readFile = openedFile.read(2**20)
if not readFile:
break;
md5Hash = hashlib.md5(readFile)
md5Hashed = md5Hash.hexdigest()
hash[inputFile]=md5Hashed
def sendmail(alert):
fromaddr = '@gmail.com'
toaddrs = '@gmail.com'
msg = MIMEMultipart()
msg['Date'] = formatdate(localtime=True)
msg = MIMEMultipart('alternative')
msg['Subject'] = "hashvalue"
msg['From'] = fromaddr #like name
msg['To'] = toaddrs
body = MIMEText(str(alert))
msg.attach(body)
username = '@gmail.com'
password = ''
server = smtplib.SMTP_SSL('smtp.googlemail.com', 465)
server.login(username, password)
server.sendmail(fromaddr, toaddrs, msg.as_string())
server.quit()
return
def diff(dcalculated,dformfile,ddiff):
if dcalculated == dformfile:
pass
else:
for key in dcalculated.keys():
if key not in dformfile.keys():
#print(key)
ddiff[key]=dcalculated[key]
else:
if dformfile[key] != dcalculated[key]:
#print(key)
ddiff[key]=dformfile[key]+'#'+dcalculated[key]
return
if __name__ == '__main__':
ddiff={}
manager = Manager()
URL = "http://localhost/fim/php/getSysId.php"
x=(':'.join(['{:02x}'.format((uuid.getnode() >> ele) & 0xff)
for ele in range(0,8*6,8)][::-1]))
PARAMS = {'mac_id':x}
r = requests.get(url = URL, params = PARAMS)
sysID = r.text
d2={}
files=manager.list()
hash=manager.dict() #C:/Users/ajayk/OneDrive/Documents/IIDT/Assignments/Forensics
dir_list(sys.argv[1])
if len(files) <5:
#hash_cal(0,len(files),files,hash)
p = Process(target=hash_cal, args=(0,len(files)-1,files,hash))
p.start()
p.join()
if os.path.isfile('hashfile.txt'):
d2 = json.load(open("hashfile.txt"))
else:
process=[]
e1=len(files)//5
#print(e1)
p1 = Process(target=hash_cal, args=(0,e1,files,hash))
p1.start()
process.append(p1)
p2 = Process(target=hash_cal, args=(e1+1,2*e1+1,files,hash))
p2.start()
process.append(p2)
s3=2*e1+2
p3 = Process(target=hash_cal, args=(s3,e1+s3,files,hash))
p3.start()
process.append(p3)
s4=e1+s3+1
p4 = Process(target=hash_cal, args=(s4,s4+e1,files,hash))
p4.start()
process.append(p4)
s5=s4+e1+1
p5 = Process(target=hash_cal, args=(s5,len(files)-1,files,hash))
p5.start()
process.append(p5)
for p in process:
p.join()
if os.path.isfile('hashfile.txt'):
d2 = json.load(open("hashfile.txt"))
while len(process) > 0:
process = [job for job in process if job.is_alive()]
print(".")
time.sleep(1)
for p in process:
p.wait()
json.dump(hash.copy(), open("hashfile.txt",'w'))
diff(hash,d2,ddiff)
print(ddiff)
print("Sending the POST to the server")
if len(ddiff.keys()) > 0:
PARAMS = json.dumps(ddiff)
URL = "http://localhost/fim/php/loadHash.php"
da = {'hashes':PARAMS,'sysid':sysID}
r1 = requests.post(url = URL, data = da)
print(r1.text);
body = "Results from the system "+x+" "
for key in ddiff.keys():
body=body+key
sendmail(body)