-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.py
336 lines (266 loc) · 10.1 KB
/
main.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
from bottle import route, run, request, error
import requests
import os.path
import configparser
import base64
import datetime
import ipaddress
import urllib.request
from urllib.parse import quote
import json
##########################
# Config section
##########################
configfile = "elasticpot.cfg" # point to elasticpot.cfg or an ews.cfg if you use ewsposter
hostport = 9200 # port to run elasticpot on
##########################
# FUNCTIONS
##########################
# read config from eventually existing T-Pot installation (see dtag-dev-sec.github.io)
def getConfig():
config2 = configparser.ConfigParser()
config2.read(configfile)
username = config2.get("EWS", "username")
token = config2.get("EWS", "token")
server = config2.get("EWS", "rhost_first")
nodeid = config2.get("ELASTICPOT", "nodeid")
ewssender = config2.get("ELASTICPOT", "elasticpot")
jsonpath = config2.get("ELASTICPOT", "logfile")
ignorecert = config2.get("EWS", "ignorecert")
hostip = config2.get("MAIN", "ip")
return (username, token, server, nodeid, ignorecert, ewssender, jsonpath, hostip)
# re-assemble raw http request from request headers, return base64 encoded
def createRaw(request):
# Generate querystring
if request.query_string=="":
querystring=""
else:
querystring= "?"+ request.query_string
httpreq = request.method + " " +request.path + querystring
# Get post content
if request.method == "POST":
postContent = ""
for l in request.body:
postContent += l.decode("utf-8")
# Generate raw http-request manually
requestheaders=httpreq + " " + request.environ.get('SERVER_PROTOCOL') + "\n"
requestheaders+="Host: "+ request.get_header('Host') + "\n"
requestheaders+="User-Agent: "+ request.get_header('User-Agent') + "\n"
requestheaders+="Accept: "+ request.get_header('Accept') + "\n"
requestheaders+="Content-Length: "+ request.get_header('Content-Length') + "\n"
requestheaders+="Content-Type: "+ request.get_header('Content-Type') + "\n" + "\n"
if request.method == "POST":
requestheaders+=postContent+"\n"
# base64 encode
requestheaders64=base64.b64encode(requestheaders.encode('UTF-8')).decode('ascii')
return requestheaders64
# Send data to either logfile (for ewsposter, location from ews.cfg) or directly to ews backend
def logData(querystring, postdata, ip,raw):
global username, token, server, nodeid, ignorecert, ewssender, jsonpath, hostip
curDate = datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%dT%H:%M:%S')
data = {}
data['timestamp'] = curDate
data['event_type'] = "alert"
data['src_ip'] = ip
data['src_port'] = srcport
data['dest_ip'] = hostip
data['dest_port'] = hostport
data2 = {}
data2['name'] = "Elasticpot"
data2['nodeid'] = nodeid
data2['name'] = "Elasticpot"
data2['query'] = querystring
data2['postdata'] = postdata
data2['raw'] = raw
data['honeypot'] = data2
# Send to json logfile
if os.path.isfile(configfile) and ewssender.upper() == "TRUE":
with open(jsonpath, 'a') as outfile:
json.dump(data, outfile)
outfile.write('\n')
# send via own posting mechanism to defined server
else:
if (username == None or token == None):
print("No credentials found in config file.")
return
txt = open("./templates/ews.txt")
xml = txt.read()
xml = xml.replace("_IP_", ip)
xml = xml.replace("_TARGET_", hostip)
xml = xml.replace("_SRCPORT_", str(srcport))
xml = xml.replace("_DSTPORT_", str(hostport))
xml = xml.replace("_USERNAME_", username)
xml = xml.replace("_TOKEN_", token)
xml = xml.replace("_URL_", quote(str(querystring)))
xml = xml.replace("_RAW_", raw)
xml = xml.replace("_DATA_", quote(str(postdata)))
xml = xml.replace("_NODEID_", nodeid)
curDate = datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')
xml = xml.replace("_TIME_", curDate)
headers = {'Content-Type': 'application/xml'}
# fix ignorecert to verifycert logic
if (ignorecert == None):
ignorecert = True
elif (ignorecert == "true"):
ignorecert = False
elif (ignorecert == "false"):
ignorecert = True
try:
requests.post(server, data=xml, headers=headers, verify=ignorecert, timeout=5)
except requests.exceptions.Timeout:
print("Elasticpot: Error trying to submit attack: Connection timeout.")
except requests.exceptions.RequestException as e:
print(e)
##########################
####### SITE HANDLER
##########################
# Handle index site
@route('/', method='GET')
def index():
txt = open("./templates/index.txt")
indexData = txt.read()
# Not an attack
# Return data, do nothing
return indexData
# handle irrelevant / error requests
@error(404)
def error404(error):
txt = open("./templates/404.txt")
indexData = txt.read()
# DO WE WANT TO LOG THESE???
# Log request to console
postContent = ""
for l in request.body:
postContent += l.decode("utf-8")
print("Elasticpot: Access to non existing ressource: " + request.url + " " + postContent)
ip = request.environ.get('REMOTE_ADDR')
# Generate querystring
if request.query_string=="":
querystring=""
else:
querystring= "?"+ request.query_string
httpreq = request.method + " " +request.path + querystring
# Create request headers for raw request
requestheaders64=createRaw(request)
# Log the data
logData(httpreq, postContent, ip, requestheaders64)
# Return data
return indexData
# handle favicon
@route('/favicon.ico', method='GET')
def getindeces():
txt = open("./templates/favicon.ico.txt")
indexData = txt.read()
# Not an attack
# Return default data, do nothing
return indexData
# handle route to indices
@route('/_cat/indices', method='GET')
def getindeces():
txt = open("./templates/getindeces.txt")
indexData = txt.read()
# Log request to console
postContent = ""
print ("Elasticpot: Found possible attack (/_cat/indices): " + request.url)
ip = request.environ.get('REMOTE_ADDR')
# Generate querystring
if request.query_string=="":
querystring=""
else:
querystring= "?"+ request.query_string
httpreq = request.method + " " +request.path + querystring
# Create request headers for raw request
requestheaders64=createRaw(request)
# Log the data
logData(httpreq, postContent, ip, requestheaders64)
# Return data
return indexData
# handle search route (GET)
@route('/_search', method='GET')
def handleSearchExploitGet():
# Log request to console
postContent = ""
print ("Elasticpot: Found possible attack (_search): " + request.url)
ip = request.environ.get('REMOTE_ADDR')
# Generate querystring
if request.query_string=="":
querystring=""
else:
querystring= "?"+ request.query_string
httpreq = request.method + " " +request.path + querystring
# Create request headers for raw request
requestheaders64=createRaw(request)
# Log the data
logData(httpreq, postContent, ip, requestheaders64)
return ""
# handle search route (POST)
@route('/_search', method='POST')
def handleSearchExploit():
# Log request to console
postContent = ""
for l in request.body:
postContent += l.decode("utf-8")
print("Elasticpot: Found possible attack (_search): " + request.url + postContent)
ip = request.environ.get('REMOTE_ADDR')
# Generate querystring
if request.query_string=="":
querystring=""
else:
querystring= "?"+ request.query_string
httpreq = request.method + " " +request.path + querystring
# Create request headers for raw request
requestheaders64=createRaw(request)
# Log the data
logData(httpreq, postContent, ip, requestheaders64)
return ""
# handle head plugin
@route('/_plugin/head')
def pluginhead():
txt = open("./templates/pluginhead.txt")
indexData = txt.read()
# Log request to console
postContent = ""
for l in request.body:
postContent += l.decode("utf-8")
print("Elasticpot: Access to ElasticSearch head plugin: " + request.url + " " + postContent)
ip = request.environ.get('REMOTE_ADDR')
# Generate querystring
if request.query_string=="":
querystring=""
else:
querystring= "?"+ request.query_string
httpreq = request.method + " " +request.path + querystring
# Create request headers for raw request
requestheaders64=createRaw(request)
# Log the data
logData(httpreq, postContent, ip, requestheaders64)
# Return data
return indexData
### More routes to add...
#@route('/<index:path>?pretty', method='PUT')
#def createindex(index):
##########################
##### MAIN START
##########################
# initialize relevant data from the config file
if (not os.path.isfile(configfile)):
print("Elasticpot: Failed to read configfile. Elasticpot will exit.")
exit(1)
else:
username, token, server, nodeid, ignorecert, ewssender, jsonpath, hostip = getConfig()
try:
if ((ipaddress.ip_address(hostip).is_private) and not (ipaddress.ip_address(hostip).is_global)):
if (ipaddress.ip_address(hostip).is_private):
try:
# if IP is private, determine external ip via lookup
hostip = requests.get('https://api.ipify.org', timeout=5).text
print("Elasticpot: IP in config file is private. Determined the public IP %s" % hostip)
except:
print("could not determine external IP address")
except:
print("IP is invalid in config file, please make sure to put a valid IP address in config file: " + hostip)
exit(1)
srcport = 44927 # Cannot be retrieved via bottles request api, this is just a dummy port
# done Initialization
# run server
run(host='0.0.0.0', port=hostport)