-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdayReport.py
More file actions
85 lines (66 loc) · 2.2 KB
/
dayReport.py
File metadata and controls
85 lines (66 loc) · 2.2 KB
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
import time
import smtplib
import aux
import getDates as gd
import getIPdata
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.utils import COMMASPACE, formatdate
from email import encoders
########
# PARSEO
########
f = open(aux.auth_log, 'r')
d, m, y = gd.getYesterday()
#Para pruebas
#print(d,m,y)
#d = d+1
text_sshd=""
text_user=""
for line in f:
if m + " " + str(d) + " " in line or m + " " + str(d) + " " in line :
if "sshd" in line:
text_sshd += line+"\n"
if "COMMAND=" in line and not ("dayReport" in line or "alert" in line):
text_user += line+"\n"
ip_info = getIPdata.getIPdata(text_sshd)
########
# ENVIO
########
if text_sshd=="" and text_user=="":
exit() #Today nothing happend
subj = "Day report " + str(d) + time.strftime("/%m/%Y")
end1 = "Sent using SSH-Alert:"
end2 = "https://github.com/manurs/SSH-Alert"
text_sshd = text_sshd.replace("[", "<b>[")
text_sshd = text_sshd.replace("]", "]</b>")
text_sshd = text_sshd.replace("\n", "<br>")
text_user = text_user.replace("[", "<b>[")
text_user = text_user.replace("]", "]</b>")
text_user = text_user.replace("\n", "<br>")
start = "<b><big>==================<br>"+subj+"<br>==================</b></big><br><br>"
end = "<b><big>================================<br>"+end1+"<br>"+end2+"<br>================================<b></big>"
start_sshd = "<b><u><big>SSHD Events</b></u></bigbig><br><br>"
start_user = "<b><u><big>Users Events</b></u></bigbig><br><br>"
# Email data
msg = MIMEMultipart()
msg['From'] = aux.fromaddr
msg['To'] = aux.toaddrs
msg['Date'] = formatdate(localtime = True)
msg['Subject'] = "SSH-Alert: " + subj
# Email message
msg.attach( MIMEText(start + start_sshd + text_sshd + start_user + text_user + ip_info + end, 'HTML') )
# Email File
part = MIMEBase('application', "octet-stream")
part.set_payload( open(aux.auth_log,"rb").read() )
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="auth_log.txt"')
msg.attach(part)
# Send mail
server = smtplib.SMTP(aux.smtp)
server.ehlo()
server.starttls()
server.login(aux.fromaddr,aux.pas)
server.sendmail(aux.fromaddr, aux.toaddrs, msg.as_string())
server.quit()