11#!/usr/bin/env python
22## ElasticSearch Reindexer by Daniel Eden
3- ## 28/07 /2016 Update
3+ ## 29/08 /2016 Update
44## - Fixed SSL based communications
55## daniel.eden@gmail.com
66
77import requests
88from requests import ConnectionError
9+ from requests .packages .urllib3 .exceptions import InsecureRequestWarning
10+ requests .packages .urllib3 .disable_warnings (InsecureRequestWarning )
911import json
1012import re
1113import sys
12-
13- class CommonAttributes ():
14- def __init__ (self ):
15- with open ('/opt/nighthawk/etc/nighthawk.json' , 'r' ) as config :
16- self .conf_data = json .load (config )
17-
18- with open ('/opt/nighthawk/lib/elastic/ElasticMapping.json' , 'r' ) as mapping :
19- self .mapping_file = json .load (mapping )
20-
21- if self .conf_data ['elastic' ]['elastic_ssl' ]:
22- self .es_host = "https://{0}" .format (self .conf_data ['elastic' ]['elastic_server' ])
23- self .es_port = str (self .conf_data ['elastic' ]['elastic_port' ])
24- else :
25- self .es_host = "http://{0}" .format (self .conf_data ['elastic' ]['elastic_server' ])
26- self .es_port = str (self .conf_data ['elastic' ]['elastic_port' ])
27-
28- self .elastic_user = self .conf_data ['elastic' ]['elastic_user' ]
29- self .elastic_pass = self .conf_data ['elastic' ]['elastic_pass' ]
30- self .index = '/investigations'
14+ sys .path .append ('/opt/nighthawk/web' )
15+ from nighthawk .triageapi .dataendpoint .common import CommonAttributes
3116
3217class SearchQuery (CommonAttributes ):
3318 def __init__ (self ):
3419 CommonAttributes .__init__ (self )
3520
36-
3721 def CheckAliases (self ):
38- print "\n [+] Obtaining latest index alias to determine index number"
22+ print "[+] Obtaining latest index alias to determine index number"
3923 try :
40- r = requests .get (self .es_host + self .es_port + '/_aliases' , auth = (self .elastic_user , self .elastic_pass ), verify = False )
24+ r = requests .get (self .es_host + ":" + self .es_port + '/_aliases' , auth = (self .elastic_user , self .elastic_pass ), verify = False )
4125 except ConnectionError as e :
4226 print '[!] Error connecting to {0}{1}' .format (self .es_host , self .es_port )
4327
@@ -63,7 +47,7 @@ def GetMappingAndCreateIndex(self, current_index):
6347
6448 try :
6549 print '[-] Sending mapping to new index'
66- r = requests .put ("{0}{1}{2}{3}" .format (self .es_host , self .es_port , self .index , index_num ), data = json .dumps (self .mapping_file ), auth = (self .elastic_user , self .elastic_pass ), verify = False )
50+ r = requests .put ("{0}: {1}{2}{3}" .format (self .es_host , self .es_port , self .index , index_num ), data = json .dumps (self .mapping_file ), auth = (self .elastic_user , self .elastic_pass ), verify = False )
6751 try :
6852 if r .json ()['acknowledged' ]:
6953 print '[+] Returned successfully, index created.'
@@ -90,7 +74,7 @@ def RemoveOldAlias(self, op_code, index_num):
9074 ]
9175 }
9276
93- r = requests .post (self .es_host + self .es_port + '/_aliases' , data = json .dumps (remove_alias ), auth = (self .elastic_user , self .elastic_pass ), verify = False )
77+ r = requests .post (self .es_host + ":" + self .es_port + '/_aliases' , data = json .dumps (remove_alias ), auth = (self .elastic_user , self .elastic_pass ), verify = False )
9478 try :
9579 if r .json ()['acknowledged' ]:
9680 print '[+] Returned successfully, alias removed.'
@@ -119,7 +103,7 @@ def ReindexData(self, op_code, index, index_num):
119103 }
120104
121105 print '[-] Large datasets will take a while, sit back and grab a coke....'
122- r = requests .post (self .es_host + self .es_port + '/_reindex' , data = json .dumps (reindex ), auth = (self .elastic_user , self .elastic_pass ), verify = False )
106+ r = requests .post (self .es_host + ":" + self .es_port + '/_reindex' , data = json .dumps (reindex ), auth = (self .elastic_user , self .elastic_pass ), verify = False )
123107
124108 try :
125109 if r .json ()['created' ]:
@@ -134,9 +118,14 @@ def ReindexData(self, op_code, index, index_num):
134118 print '[!] Returned op_code 1, error in index creation and mapping. Exiting now'
135119 sys .exit (1 )
136120
121+ def Version (self ):
122+ print "-- Reindexing automation by Daniel Eden (nightHawk Response team)."
123+ print "-- Version 1.0.3. 29/08/2016\n "
124+
137125def main ():
138126
139127 s = SearchQuery ()
128+ s .Version ()
140129 index = s .CheckAliases ()
141130 op_code , index_num = s .GetMappingAndCreateIndex (index )
142131 op_code = s .RemoveOldAlias (op_code , index_num )
0 commit comments