-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.py
250 lines (200 loc) · 10.2 KB
/
start.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
# Import required libraries
import json, requests, ssl, time, urllib3
import sys,os
from slack_sdk import WebClient
import argparse
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
slack_token = "Slack token here"
slack_channel = "Slack channel ID here"
client = WebClient(token=slack_token)
def cleanup():
# Delete the scan
dummy = requests.delete(MyAXURL + '/scans/' + MyScanID, headers = MyRequestHeaders, verify=False)
# Delete the target
dummy = requests.delete(MyAXURL + '/targets/' + MyTargetID, headers = MyRequestHeaders, verify=False)
parser = argparse.ArgumentParser(description="AWVS scanner script")
parser.add_argument('--api_key', required=True, help='API key')
parser.add_argument('--target', required=True, help='Target URL')
args = parser.parse_args()
# Declare variables
MyAXURLHalf="https://localhost:3443"
MyAXURL = "https://localhost:3443/api/v1"
MyAPIKEY = args.api_key
MyTargetURL = args.target
MyTargetDESC = "Test"
FullScanProfileID = "11111111-1111-1111-1111-111111111111"
MyRequestHeaders = {'X-Auth':MyAPIKEY, 'Content-Type':'application/json'}
def addTask(url,target,MyRequestHeaders):
global MyTargetDESC
try:
url = ''.join((url, '/api/v1/targets/add'))
data = {"targets":[{"address": target,"description":MyTargetDESC}],"groups":[]}
r = requests.post(url, headers=MyRequestHeaders, data=json.dumps(data), timeout=30, verify=False)
result = json.loads(r.content.decode())
return result['targets'][0]['target_id']
except Exception as e:
return e
def scan(url,target,profile_id,is_to_scan,MyRequestHeaders):
global MyTargetDESC
scanUrl = ''.join((url, '/api/v1/scans'))
target_id = addTask(url,target,MyRequestHeaders)
if target_id:
try:
configuration(url,target_id,profile_id,MyRequestHeaders)#Configure target parameters
if is_to_scan:
data = {"target_id": target_id, "profile_id": profile_id, "incremental": False,
"schedule": {"disable": False, "start_date": None, "time_sensitive": False}}
response = requests.post(scanUrl, data=json.dumps(data), headers=MyRequestHeaders, timeout=30, verify=False)
result = json.loads(response.content)
return [1,result['target_id']]
else:
return target_id
except Exception as e:
print(e)
def configuration(url,target_id,default_scanning_profile_id,MyRequestHeaders):#configure target
configuration_url = ''.join((url,'/api/v1/targets/{0}/configuration'.format(target_id)))
data = {"scan_speed": "moderate", "login": {"kind": "none"}, "ssh_credentials": {"kind": "none"},"default_scanning_profile_id":default_scanning_profile_id,
"sensor": False, "user_agent": 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0', "case_sensitive": "auto",
"limit_crawler_scope": False, "excluded_paths": [],
"authentication": {"enabled": False},
"technologies": [], "custom_headers": [], "custom_cookies": [],
"debug": False, "client_certificate_password": "", "issue_tracker_id": "", "excluded_hours_id": ""}
r = requests.patch(url=configuration_url,data=json.dumps(data), headers=MyRequestHeaders, timeout=30, verify=False)
#print(configuration_url,r.text)
# Create our intended target - target ID is in the JSON response
#MyRequestBody = {"address":MyTargetURL,"description":MyTargetDESC,"type":"default","criticality":10}
#MyTargetIDResponse = requests.post(MyAXURL + '/targets', json=MyRequestBody, headers = MyRequestHeaders, verify=False)
#MyTargetIDjson=json.loads(MyTargetIDResponse.content)
MyTargetID=scan(MyAXURLHalf,MyTargetURL,FullScanProfileID,False,MyRequestHeaders)
def delete_target(target_id,MyAXURLHalf,MyRequestHeaders):
try:
response = requests.delete(MyAXURLHalf+"/api/v1/targets/"+str(target_id),headers=MyRequestHeaders,timeout=30,verify=False)
except Exception as e:
print(str(e))
return
def delete_scan(scan_id,MyAXURLHalf,MyRequestHeaders):
try:
response = requests.delete(MyAXURLHalf+"/api/v1/scans/"+str(scan_id),headers=MyRequestHeaders,timeout=30,verify=False)
if response.status_code == "204":
return True
else:
return False
except Exception as e:
print(str(e))
return
def getreports(scan_id):
# scan_id
'''
11111111-1111-1111-1111-111111111111 Developer
21111111-1111-1111-1111-111111111111 XML
11111111-1111-1111-1111-111111111119 OWASP Top 10 2013
11111111-1111-1111-1111-111111111112 Quick
11111111-1111-1111-1111-111111111126 New
'''
data = {"template_id":"11111111-1111-1111-1111-111111111126","source":{"list_type":"scans","id_list":[scan_id]}}
try:
response = requests.post(MyAXURLHalf+"/api/v1/reports",data=json.dumps(data),headers=MyRequestHeaders,timeout=30,verify=False)
result = response.headers
report = result['Location'].replace('/api/v1/reports/','/reports/download/')
return MyAXURLHalf.rstrip('/')+report
except Exception as e:
print(str(e))
return
finally:
delete_scan(scan_id,MyAXURLHalf,MyRequestHeaders)
def generated_report(MyAXURLHalf,scan_id,target,MyRequestHeaders,MyTargetID):
try:
MyRequestBody0 = {"template_id": "11111111-1111-1111-1111-111111111126","source": {"list_type": "scan_result", "id_list":[scan_id]}}
response = requests.post(MyAXURLHalf + "/api/v1/reports", json=MyRequestBody0, headers=MyRequestHeaders, verify=False)
print(response)
report_url = MyAXURLHalf.strip('/') + response.headers['Location']
print(report_url)
requests.get(str(report_url),headers=MyRequestHeaders, verify=False)
unique_report_id = report_url.split("/")[-1]
while True:
report = get_report(MyAXURLHalf,response.headers['Location'],MyRequestHeaders)
if not report:
time.sleep(5)
elif report:
break
# Get current report download links
reports_response = requests.get(MyAXURLHalf + "/api/v1/reports?l=20", headers=MyRequestHeaders, verify=False)
data = json.loads(reports_response.text)
pdf_download_link = None
for report in data["reports"]:
if unique_report_id in report["download"][1]: # PDF should be the second link
pdf_download_link = report["download"][1]
break
if pdf_download_link is None:
print("[ERROR] No PDF found for report: %s" % unique_report_id)
return
if(not os.path.exists("reports")):
os.mkdir("reports")
filename = str(target.strip('/').split('://')[1]).replace('.','_').replace('/','-')
# Modify to PDF
file = "reports/" + filename + "%s.pdf" % time.strftime("%Y-%m-%d-%H-%M", time.localtime(time.time()))
report = requests.get(MyAXURLHalf + pdf_download_link,headers=MyRequestHeaders, verify=False,timeout=120)
with open(file, "wb") as f:
f.write(report.content)
print("[INFO] %s report have %s.pdf is generated successfully" % (target,filename))
try:
response = client.files_upload(
channels=slack_channel,
file=file,
initial_comment= filename
)
print("[INFO] Report file uploaded to Slack: %s" % response['file']['permalink'])
except Exception as e:
print("[ERROR] Failed to upload report file to Slack: %s" % str(e))
except Exception as e:
raise e
finally:
delete_scan(scan_id,MyAXURLHalf,MyRequestHeaders)
delete_target(MyTargetID,MyAXURLHalf,MyRequestHeaders)
def get_report(MyAXURLHalf,reportid,MyRequestHeaders):
res = requests.get(url=MyAXURLHalf + reportid, timeout=10, verify=False, headers=MyRequestHeaders)
try:
report_url = res.json()['download'][0]
return report_url
except Exception as e:
return False
# Trigger a scan on the target - scan ID is in the HTTP response headers
MyRequestBody = {"profile_id":FullScanProfileID,"incremental":False,"schedule":{"disable":False,"start_date":None,"time_sensitive":False},"user_authorized_to_scan":"yes","target_id":MyTargetID}
MyScanIDResponse = requests.post(MyAXURL + '/scans', json=MyRequestBody, headers = MyRequestHeaders, verify=False)
MyScanID = MyScanIDResponse.headers["Location"].replace("/api/v1/scans/","")
LoopCondition=True
while LoopCondition :
MyScanStatusResponse = requests.get(MyAXURL + '/scans/' + MyScanID, headers = MyRequestHeaders, verify=False)
MyScanStatusjson = json.loads(MyScanStatusResponse.content)
MyScanStatus = MyScanStatusjson["current_session"]["status"]
if (MyScanStatus=="processing"):
print("Scan Status: Processing - waiting 30 seconds...")
elif (MyScanStatus=="scheduled"):
print("Scan Status: Scheduled - waiting 30 seconds...")
elif (MyScanStatus=="completed"):
LoopCondition=False
elif (MyScanStatus=="failed"):
LoopCondition=False
else:
print("Invalid Scan Status: Aborting")
cleanup
exit()
MyScanStatus=""
time.sleep(30)
# Obtain the scan session ID
MyScanSessionResponse = requests.get(MyAXURL + '/scans/' + MyScanID, headers = MyRequestHeaders, verify=False)
MyScanSessionjson = json.loads(MyScanSessionResponse.content)
MyScanSessionID = MyScanSessionjson["current_session"]["scan_session_id"]
# Obtain the scan result ID
MyScanResultResponse = requests.get(MyAXURL + '/scans/' + MyScanID + "/results", headers = MyRequestHeaders, verify=False)
MyScanResultjson = json.loads(MyScanResultResponse.content)
MyScanResultID = MyScanResultjson["results"][0]["result_id"]
# Obtain scan vulnerabilities
MyScanVulnerabilitiesResponse = requests.get(MyAXURL + '/scans/' + MyScanID + '/results/' + MyScanResultID + '/vulnerabilities', headers = MyRequestHeaders, verify=False)
print (" ")
print ("Target ID: " + MyTargetID)
print ("Scan ID: " + MyScanID)
print ("Scan Session ID: " + MyScanSessionID)
print ("Scan Result ID: " + MyScanResultID)
generated_report(MyAXURLHalf,MyScanSessionID,MyTargetURL,MyRequestHeaders,MyTargetID)