forked from CrowdStrike/falconpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprevention_policy.py
More file actions
214 lines (184 loc) · 11 KB
/
prevention_policy.py
File metadata and controls
214 lines (184 loc) · 11 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
################################################################################################################
# CROWDSTRIKE FALCON #
# OAuth2 API - Customer SDK #
# #
# prevention_policy - Falcon X Prevention Policy API Interface Class #
################################################################################################################
# This is free and unencumbered software released into the public domain.
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
# For more information, please refer to <https://unlicense.org>
import requests
import json
import urllib3
from urllib3.exceptions import InsecureRequestWarning
urllib3.disable_warnings(InsecureRequestWarning)
class Prevention_Policy:
""" The only requirement to instantiate an instance of this class
is a valid token provided by the Falcon API SDK OAuth2 class.
"""
def __init__(self, access_token, base_url='https://api.crowdstrike.com'):
""" Instantiates the base class, ingests the authorization token,
and initializes the headers and base_url global variables.
"""
self.headers = { 'Authorization': 'Bearer {}'.format(access_token) }
self.base_url = base_url
class Result:
""" Subclass to handle parsing of result client output. """
def __init__(self):
""" Instantiates the subclass and initializes the result object. """
self.result_obj = {}
def __call__(self, status_code, headers, body):
""" Formats values into a properly formatted result object. """
self.result_obj['status_code'] = status_code
self.result_obj['headers'] = dict(headers)
self.result_obj['body'] = body
return self.result_obj
def queryCombinedPreventionPolicyMembers(self, parameters={}):
""" Search for members of a Prevention Policy in your environment by providing an FQL filter
and paging details. Returns a set of host details which match the filter criteria.
"""
# [GET] https://assets.falcon.crowdstrike.com/support/api/swagger.html#/prevention-policies/queryCombinedPreventionPolicyMembers
FULL_URL = self.base_url+'/policy/combined/prevention-members/v1'
HEADERS = self.headers
PARAMS = parameters
try:
response = requests.request("GET", FULL_URL, params=PARAMS, headers=HEADERS, verify=False)
returned = self.Result()(response.status_code, response.headers, response.json())
except Exception as e:
returned = self.Result()(500, {}, str(e))
return returned
def queryCombinedPreventionPolicies(self, parameters={}):
""" Search for Prevention Policies in your environment by providing an FQL filter and
paging details. Returns a set of Prevention Policies which match the filter criteria.
"""
# [GET] https://assets.falcon.crowdstrike.com/support/api/swagger.html#/prevention-policies/queryCombinedPreventionPolicies
FULL_URL = self.base_url+'/policy/combined/prevention/v1'
HEADERS = self.headers
PARAMS = parameters
try:
response = requests.request("GET", FULL_URL, params=PARAMS, headers=HEADERS, verify=False)
returned = self.Result()(response.status_code, response.headers, response.json())
except Exception as e:
returned = self.Result()(500, {}, str(e))
return returned
def performPreventionPoliciesAction(self, parameters, body):
""" Perform the specified action on the Prevention Policies specified in the request. """
# [POST] https://assets.falcon.crowdstrike.com/support/api/swagger.html#/prevention-policies/performPreventionPoliciesAction
FULL_URL = self.base_url+'/policy/entities/prevention-actions/v1'
HEADERS = self.headers
PARAMS = parameters
BODY = body
try:
response = requests.request("POST", FULL_URL, params=PARAMS, json=BODY, headers=HEADERS, verify=False)
returned = self.Result()(response.status_code, response.headers, response.json())
except Exception as e:
returned = self.Result()(500, {}, str(e))
return returned
def setPreventionPoliciesPrecedence(self, body):
""" Sets the precedence of Prevention Policies based on the order of IDs specified in the request.
The first ID specified will have the highest precedence and the last ID specified will have the lowest.
You must specify all non-Default Policies for a platform when updating precedence.
"""
# [POST] https://assets.falcon.crowdstrike.com/support/api/swagger.html#/prevention-policies/setPreventionPoliciesPrecedence
FULL_URL = self.base_url+'/policy/entities/prevention-precedence/v1'
HEADERS = self.headers
BODY = body
try:
response = requests.request("POST", FULL_URL, json=BODY, headers=HEADERS, verify=False)
returned = self.Result()(response.status_code, response.headers, response.json())
except Exception as e:
returned = self.Result()(500, {}, str(e))
return returned
def getPreventionPolicies(self, ids):
""" Retrieve a set of Prevention Policies by specifying their IDs. """
# [GET] https://assets.falcon.crowdstrike.com/support/api/swagger.html#/prevention-policies/getPreventionPolicies
ID_LIST = str(ids).replace(",","&ids=")
FULL_URL = self.base_url+'/policy/entities/prevention/v1?ids={}'.format(ID_LIST)
HEADERS = self.headers
try:
response = requests.request("GET", FULL_URL, headers=HEADERS, verify=False)
returned = self.Result()(response.status_code, response.headers, response.json())
except Exception as e:
returned = self.Result()(500, {}, str(e))
return returned
def createPreventionPolicies(self, body):
""" Create Prevention Policies by specifying details about the policy to create. """
# [POST] https://assets.falcon.crowdstrike.com/support/api/swagger.html#/prevention-policies/createPreventionPolicies
FULL_URL = self.base_url+'/policy/entities/prevention/v1'
HEADERS = self.headers
BODY = body
try:
response = requests.request("POST", FULL_URL, json=BODY, headers=HEADERS, verify=False)
returned = self.Result()(response.status_code, response.headers, response.json())
except Exception as e:
returned = self.Result()(500, {}, str(e))
return returned
def deletePreventionPolicies(self, ids):
""" Delete a set of Prevention Policies by specifying their IDs. """
# [DELETE] https://assets.falcon.crowdstrike.com/support/api/swagger.html#/prevention-policies/deletePreventionPolicies
ID_LIST = str(ids).replace(",","&ids=")
FULL_URL = self.base_url+'/policy/entities/prevention/v1?ids={}'.format(ID_LIST)
HEADERS = self.headers
try:
response = requests.request("DELETE", FULL_URL, headers=HEADERS, verify=False)
returned = self.Result()(response.status_code, response.headers, response.json())
except Exception as e:
returned = self.Result()(500, {}, str(e))
return returned
def updatePreventionPolicies(self, body):
""" Update Prevention Policies by specifying the ID of the policy and details to update. """
# [PATCH] https://assets.falcon.crowdstrike.com/support/api/swagger.html#/prevention-policies/updatePreventionPolicies
FULL_URL = self.base_url+'/policy/entities/prevention/v1'
HEADERS = self.headers
BODY = body
try:
response = requests.request("PATCH", FULL_URL, json=BODY, headers=HEADERS, verify=False)
returned = self.Result()(response.status_code, response.headers, response.json())
except Exception as e:
returned = self.Result()(500, {}, str(e))
return returned
def queryPreventionPolicyMembers(self, parameters={}):
""" Search for members of a Prevention Policy in your environment by providing an FQL filter
and paging details. Returns a set of Agent IDs which match the filter criteria.
"""
# [GET] https://assets.falcon.crowdstrike.com/support/api/swagger.html#/prevention-policies/queryPreventionPolicyMembers
FULL_URL = self.base_url+'/policy/queries/prevention-members/v1'
HEADERS = self.headers
PARAMS = parameters
try:
response = requests.request("GET", FULL_URL, params=PARAMS, headers=HEADERS, verify=False)
returned = self.Result()(response.status_code, response.headers, response.json())
except Exception as e:
returned = self.Result()(500, {}, str(e))
return returned
def queryPreventionPolicies(self, parameters={}):
""" Search for Prevention Policies in your environment by providing an FQL filter
and paging details. Returns a set of Prevention Policy IDs which match the filter criteria.
"""
# [GET] https://assets.falcon.crowdstrike.com/support/api/swagger.html#/prevention-policies/queryPreventionPolicies
FULL_URL = self.base_url+'/policy/queries/prevention/v1'
HEADERS = self.headers
PARAMS = parameters
try:
response = requests.request("GET", FULL_URL, params=PARAMS, headers=HEADERS, verify=False)
returned = self.Result()(response.status_code, response.headers, response.json())
except Exception as e:
returned = self.Result()(500, {}, str(e))
return returned