-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathstart_search.py
executable file
·190 lines (146 loc) · 7.31 KB
/
start_search.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
#!/usr/bin/env python3
# written by sqall
# twitter: https://twitter.com/sqall01
# blog: https://h4des.org
# github: https://github.com/sqall01
#
# Licensed under the MIT License.
import os
import subprocess
import socket
import sys
import time
from scripts.config.config import START_PROCESS_TIMEOUT, TO_ADDR, FROM_ADDR, ALERTR_FIFO
from scripts.lib.alerts import raise_alert_alertr, raise_alert_mail
if __name__ == '__main__':
print_output = False
if ALERTR_FIFO is None and FROM_ADDR is None and TO_ADDR is None:
print_output = True
script_dir = os.path.dirname(os.path.abspath(__file__)) + "/scripts/"
for script in os.listdir(script_dir):
# Execute all python scripts.
if script[-3:] == ".py" and script != "__init__.py":
if print_output:
print("Executing %s" % script)
to_execute = [script_dir + script]
# Pass arguments to scripts.
if len(sys.argv) > 1:
to_execute.extend(sys.argv[1:])
process = None
try:
process = subprocess.Popen(to_execute,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
process.wait(START_PROCESS_TIMEOUT)
# Catch timeout.
except subprocess.TimeoutExpired:
if print_output:
print("Script '%s' timed out." % script)
else:
if ALERTR_FIFO is not None:
hostname = socket.gethostname()
optional_data = dict()
optional_data["script"] = script
optional_data["hostname"] = hostname
message = "Script '%s' on host '%s' timed out." % (script, hostname)
optional_data["message"] = message
raise_alert_alertr(ALERTR_FIFO,
optional_data)
if FROM_ADDR is not None and TO_ADDR is not None:
hostname = socket.gethostname()
subject = "[Security] Script '%s' on '%s' timed out" % (script, hostname)
message = "Script '%s' on host '%s' timed out." % (script, hostname)
raise_alert_mail(FROM_ADDR,
TO_ADDR,
subject,
message)
# Catch any execution error.
except Exception as e:
if print_output:
print("Executing script '%s' raised error: %s" % (script, str(e)))
else:
if ALERTR_FIFO is not None:
hostname = socket.gethostname()
optional_data = dict()
optional_data["script"] = script
optional_data["hostname"] = hostname
message = "Executing script '%s' on host '%s' raised error: %s" % (script, hostname, str(e))
optional_data["message"] = message
raise_alert_alertr(ALERTR_FIFO,
optional_data)
if FROM_ADDR is not None and TO_ADDR is not None:
hostname = socket.gethostname()
subject = "[Security] Executing script '%s' on '%s' raised error" % (script, hostname)
message = "Executing script '%s' on host '%s' raised error: %s" % (script, hostname, str(e))
raise_alert_mail(FROM_ADDR,
TO_ADDR,
subject,
message)
continue
exit_code = process.poll()
# Process did not terminate yet.
if exit_code is None:
process.terminate()
time.sleep(5)
exit_code = process.poll()
# Kill process if not exited.
if exit_code != -15:
if print_output:
print("Script '%s' did not terminate. Killing it." % script)
else:
if ALERTR_FIFO is not None:
hostname = socket.gethostname()
optional_data = dict()
optional_data["script"] = script
optional_data["hostname"] = hostname
message = "Script '%s' on host '%s' did not terminate. Killing it." % (script, hostname)
optional_data["message"] = message
raise_alert_alertr(ALERTR_FIFO,
optional_data)
if FROM_ADDR is not None and TO_ADDR is not None:
hostname = socket.gethostname()
subject = "[Security] Script '%s' on '%s' did not terminate" % (script, hostname)
message = "Script '%s' on host '%s' did not terminate. Killing it." % (script, hostname)
raise_alert_mail(FROM_ADDR,
TO_ADDR,
subject,
message)
# noinspection PyBroadException
try:
process.kill()
except:
pass
# Process executed successfully.
elif exit_code == 0:
if print_output:
stdout, stderr = process.communicate()
print(stdout.decode("ascii"))
print("")
continue
# Process encountered error.
else:
if print_output:
print("Script '%s' exited with exit code: %d" % (script, exit_code))
else:
if ALERTR_FIFO is not None:
hostname = socket.gethostname()
optional_data = dict()
optional_data["script"] = script
optional_data["hostname"] = hostname
message = "Script '%s' on host '%s' exited with exit code '%d'." % (script, hostname, exit_code)
optional_data["message"] = message
raise_alert_alertr(ALERTR_FIFO,
optional_data)
if FROM_ADDR is not None and TO_ADDR is not None:
hostname = socket.gethostname()
subject = "[Security] Script '%s' on '%s' unsuccessful" % (script, hostname)
message = "Script '%s' on host '%s' exited with exit code '%d'." % (script, hostname, exit_code)
raise_alert_mail(FROM_ADDR,
TO_ADDR,
subject,
message)
# noinspection PyBroadException
try:
process.kill()
except:
pass