-
-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathapp.py
More file actions
276 lines (246 loc) · 9.51 KB
/
app.py
File metadata and controls
276 lines (246 loc) · 9.51 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
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
import socket
import asyncio
import time
import random
import json
import boto3
import botocore
from botocore.config import Config
from walkoff_app_sdk.app_base import AppBase
class AWSGuardduty(AppBase):
__version__ = "1.0.0"
app_name = "AWS Guardduty"
def __init__(self, redis, logger, console_logger=None):
"""
Each app should have this __init__ to set up Redis and logging.
:param redis:
:param logger:
:param console_logger:
"""
super().__init__(redis, logger, console_logger)
def auth_guardduty(self, access_key, secret_key, region):
my_config = Config(
region_name = region,
signature_version = "v4",
retries = {
'max_attempts': 10,
'mode': 'standard'
},
)
if access_key!="":
return boto3.client(
'guardduty',
config=my_config,
aws_access_key_id=access_key,
aws_secret_access_key=secret_key,
)
else:
return boto3.client(
'guardduty',
config=my_config,
)
def create_detector(self, access_key, secret_key, region, enable):
client = self.auth_guardduty(access_key, secret_key, region)
try:
return client.create_detector(bool(enable))
except Exception as e:
return f"Error: {e}"
def delete_detector(self, access_key, secret_key, region, detectorId):
client = self.auth_guardduty(access_key, secret_key, region)
try:
return client.delete_detector(
DetectorId = detectorId
)
except Exception as e:
return f"Error: {e}"
def get_detector(self, access_key, secret_key, region, detectorId):
client = self.auth_guardduty(access_key, secret_key, region)
try:
return client.get_detector(
DetectorId = detectorId
)
except Exception as e:
return f"Error: {e}"
def update_detector(self, access_key, secret_key, region, detectorId, enable):
client = self.auth_guardduty(access_key, secret_key, region)
try:
return client.update_detector(
DetectorId = detectorId,
Enable = bool(enable)
)
except Exception as e:
return f"Error: {e}"
def create_ip_set(self, access_key, secret_key, region, detectorId, name, fileformat, location, activate):
client = self.auth_guardduty(access_key, secret_key, region)
try:
return client.create_ip_set(
DetectorId = detectorId,
Name = name,
Format = fileformat,
Location = location,
Activate = bool(activate)
)
except Exception as e:
return f"Error: {e}"
def delete_ip_set(self, access_key, secret_key, region, detectorId, ipSetId):
client = self.auth_guardduty(access_key, secret_key, region)
try:
return client.delete_ip_set(
DetectorId = detectorId,
IpSetId = ipSetId
)
except Exception as e:
return f"Error: {e}"
def list_detectors(self, access_key, secret_key, region):
client = self.auth_guardduty(access_key, secret_key, region)
try:
return client.list_detectors()
except Exception as e:
return f"Error: {e}"
def update_ip_set(self, access_key, secret_key, region, detectorId, ipSetId, name, location, activate):
client = self.auth_guardduty(access_key, secret_key, region)
try:
return client.update_ip_set(
DetectorId = detectorId,
IpSetId = ipSetId,
Name = name,
Location = location,
Activate = bool(activate)
)
except Exception as e:
return f"Error: {e}"
def get_ip_set(self, access_key, secret_key, region, detectorId, ipSetId):
client = self.auth_guardduty(access_key, secret_key, region)
try:
return client.get_ip_set(
DetectorId = detectorId,
IpSetId = ipSetId,
)
except Exception as e:
return f"Error: {e}"
def list_ip_sets(self, access_key, secret_key, region, detectorId):
client = self.auth_guardduty(access_key, secret_key, region)
try:
return client.list_ip_sets(
DetectorId = detectorId
)
except Exception as e:
return f"Error: {e}"
def create_threat_intel_set(self, access_key, secret_key, region, detectorId, name, fileformat, location, activate):
client = self.auth_guardduty(access_key, secret_key, region)
try:
return client.create_threat_intel_set(
DetectorId = detectorId,
Name = name,
Format = fileformat,
Location = location,
Activate = bool(activate)
)
except Exception as e:
return f"Error: {e}"
def delete_threat_intel_set(self, access_key, secret_key, region, detectorId, threatIntelSetId):
client = self.auth_guardduty(access_key, secret_key, region)
try:
return client.delete_threat_intel_set(
DetectorId = detectorId,
ThreatIntelSetId = threatIntelSetId
)
except Exception as e:
return f"Error: {e}"
def get_threat_intel_set(self, access_key, secret_key, region, detectorId, threatIntelSetId):
client = self.auth_guardduty(access_key, secret_key, region)
try:
return client.get_threat_intel_set(
DetectorId = detectorId,
ThreatIntelSetId = threatIntelSetId
)
except Exception as e:
return f"Error: {e}"
def list_threat_intel_sets(self, access_key, secret_key, region, detectorId):
client = self.auth_guardduty(access_key, secret_key, region)
try:
return client.list_threat_intel_sets(
DetectorId = detectorId
)
except Exception as e:
return f"Error: {e}"
def update_threat_intel_set(self, access_key, secret_key, region, detectorId, threatIntelSetId, name, location, activate):
client = self.auth_guardduty(access_key, secret_key, region)
try:
return client.update_threat_intel_set(
DetectorId = detectorId,
ThreatIntelSetId = threatIntelSetId,
Name = name,
Location = location,
Activate = bool(activate)
)
except Exception as e:
return f"Error: {e}"
def list_findings(self, access_key, secret_key, region, detectorId):
client = self.auth_guardduty(access_key, secret_key, region)
try:
return client.list_findings(
DetectorId = detectorId
)
except Exception as e:
return f"Error: {e}"
def get_findings(self, access_key, secret_key, region, detectorId, findingIds):
client = self.auth_guardduty(access_key, secret_key, region)
try:
findingIds = findingIds.split(',')
return client.get_findings(
DetectorId = detectorId,
FindingIds = findingIds
)
except Exception as e:
return f"Error: {e}"
def create_sample_findings(self, access_key, secret_key, region, detectorId, findingIds):
client = self.auth_guardduty(access_key, secret_key, region)
try:
findingIds = findingIds.split(',')
return client.create_sample_findings(
DetectorId = detectorId,
FindingIds = findingIds
)
except Exception as e:
return f"Error: {e}"
def archive_findings(self,access_key, secret_key, region, detectorId, findingIds):
client = self.auth_guardduty(access_key, secret_key, region)
try:
findingIds = findingIds.split(',')
return client.archive_findings(
DetectorId = detectorId,
FindingIds = findingIds
)
except Exception as e:
return f"Error: {e}"
def unarchive_findings(self,access_key, secret_key, region, detectorId, findingIds):
client = self.auth_guardduty(access_key, secret_key, region)
try:
findingIds = findingIds.split(',')
return client.unarchive_findings(
DetectorId = detectorId,
FindingIds = findingIds
)
except Exception as e:
return f"Error: {e}"
def list_members(self,access_key, secret_key, region, detectorId):
client = self.auth_guardduty(access_key, secret_key, region)
try:
return client.list_members(
DetectorId = detectorId,
)
except Exception as e:
return f"Error: {e}"
def get_members(self,access_key, secret_key, region, detectorId, accountIds):
client = self.auth_guardduty(access_key, secret_key, region)
try:
accountIds = accountIds.split(',')
return client.get_members(
DetectorId = detectorId,
AccountIds = accountIds
)
except Exception as e:
return f"Error: {e}"
if __name__ == "__main__":
AWSGuardduty.run()