forked from xujun10110/es_email_intel
/
es_query_ipv4_outhtml_outmail.py
204 lines (163 loc) · 5.52 KB
/
es_query_ipv4_outhtml_outmail.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
from elasticsearch import Elasticsearch
import requests
import os, sys
import time
import re
import hashlib
import ipaddress
import common_functions
import external_lookups
import gen_wordcloud
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEImage import MIMEImage
from email import Encoders
import time
EMAIL_SERVER = '192.168.3.2'
EMAIL_FROM = 'pierre@userid.org'
SUBJECT = "ES Intel Report (IPv4) for "+time.ctime()
EMAIL_TO = {'pierre@userid.org'}
report_contents = ''
gen_wordcloud.gen_wordcloud()
def send_mail(report_contents):
msg = MIMEMultipart()
msg['Subject'] = SUBJECT
msg['From'] = EMAIL_FROM
msg['To'] = ', '.join(EMAIL_TO)
fp = open('/home/pierre/es_email_intel/wordcloud.png', 'rb')
try:
msgImage = MIMEImage(fp.read())
except:
fp = open('/home/pierre/es_email_intel/1x1.png', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-ID', '<wordcloud>')
msg.attach(msgImage)
part = MIMEBase('application', "octet-stream")
part.set_payload(report_contents)
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="report.html"')
msg.attach(part)
server = smtplib.SMTP(EMAIL_SERVER)
server.sendmail(EMAIL_FROM, EMAIL_TO, msg.as_string())
es_server = '192.168.3.208'
es = Elasticsearch([{'host': es_server, 'port': 9200}])
es_collection_name = 'mail2json'
then = common_functions.queryrange(1)
body = '''{
"size" : 10000,
"query": {
"constant_score": {
"filter": {
"range": {
"epoch": {
"from": '''+then+'''
}
}
}
}
}
}'''
preskel = '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!-- The above 2 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="">
<meta name="author" content="">
<!-- Note there is no responsive meta tag here -->
<link rel="icon" href="../../favicon.ico">
<title>Newly observed IPv4 indicator report</title>
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<!-- Custom styles for this template -->
<link href="non-responsive.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<br/>
<div class="container">
<div class="page-header">
<p class="lead">Here is a list of newly observed IPv4 indicators from our monitored email lists.</p>
</div>
<h3>IPv4 addresses</h3>
<table class="table table-striped">
<thead>
<tr>
<th>Address</th>
<th>Subject(s)</th>
<th>List-Id(s)</th>
</tr>
</thead>
<tbody>
'''
postskel = '''
</tbody>
</table>
</div> <!-- /container -->
<br><br><br>
<p> Raw data supplement</p>
'''
postskel2 = '''
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="../../dist/js/bootstrap.min.js"></script>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="../../assets/js/ie10-viewport-bug-workaround.js"></script>
</body>
</html>
'''
ipv4s = common_functions.pull_ipv4_addresses(es, es_collection_name, body)
# Remove recently-seen items from the result set
for i in xrange(len(ipv4s) - 1, -1, -1):
if external_lookups.check_newobserved(ipv4s[i]):
del ipv4s[i]
# Exclude previously seen addresses from the past 30 days
### Everything after this is to write the HTML page
report_contents += preskel
for ipv4 in ipv4s:
meta = common_functions.describesources(es, es_collection_name, 'ipv4:'+ipv4)
report_contents += '<tr>'
v4hash = hashlib.sha224(ipv4).hexdigest()
report_contents += '<th scope="row"><a href="#'+str(v4hash)+'">'+ipv4+'</a></th>'
report_contents += '<td>'
seensubjects = []
for subject in meta[0]:
subject = subject.strip()
if subject in seensubjects: continue
seensubjects.append(subject)
if re.search('^RE$', subject): continue
subject = re.sub('\[cw warroom\] ', '', subject)
subject = re.sub('\[cw general\] ', '', subject)
report_contents += subject+'</br/>'
report_contents += '</td>'
report_contents += '<th>'
seenlists = []
for list in meta[1]:
if list in seenlists: continue
seenlists.append(list)
report_contents += list+'</br/>'
report_contents += '</th>'
report_contents += '</tr>'
report_contents += postskel
for ipv4 in ipv4s:
v4hash = hashlib.sha224(ipv4).hexdigest()
report_contents += '<span id="'+v4hash+'">'
report_contents += '<p>============================================================================</p>'
report_contents += '<h3>'+str(ipv4)+'</h3>\n'
report_contents += '<b>FutureCrimes lookup:</b><br>\n<pre>'
report_contents += external_lookups.ipv4_futurecrimes(ipv4)
report_contents += '</pre><br>\n'
report_contents += '<b>Packetmail IPRep lookup:</b><br>\n<pre>'
report_contents += external_lookups.packetmail_lookup(ipv4)
report_contents += postskel2
send_mail(report_contents)