Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdetectify-python3.py
More file actions
Latest commit
98 lines (72 loc) · 2.59 KB
/
Copy pathdetectify-python3.py
File metadata and controls
98 lines (72 loc) · 2.59 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
importrequests
importhmac
importhashlib
importjson
importos
frombase64importb64encode, b64decode
fromdatetimeimportdatetime
ENDPOINT="https://api.detectify.com/rest"
api_key=os.getenv("API_KEY")
api_secret_key=os.getenv("API_SECRET_KEY")
defsend_post_request(data: dictorNone, path: str, url: str) ->dictorNone:
timestamp=str(int(datetime.now().timestamp()))
ifdata:
headers=make_headers("POST", path, timestamp, json.dumps(data))
req=requests.post(url, headers=headers, data=json.dumps(data))
else:
headers=make_headers("POST", path, timestamp, None)
req=requests.post(url, headers=headers, data=None)
try:
req.raise_for_status()
exceptExceptionase:
print(e)
returnNone
print(f"Status code: {req.status_code} | Raw response: {req.json()}")
returnreq.json()
defmake_headers(method: str, path: str, timestamp: str, body: str=None):
method=method.upper()
signature=make_signature(method, path, timestamp, body)
return {
"X-Detectify-Key": api_key,
"X-Detectify-Signature": signature,
"X-Detectify-Timestamp": timestamp,
}
defmake_signature(method: str, path: str, timestamp: str, body: str=None):
msg=f"{method};{path};{api_key};{timestamp};"
ifbody:
msg+=f"{body}"
secret=b64decode(api_secret_key)
signature=hmac.new(
secret, msg=bytes(msg, "utf=8"), digestmod=hashlib.sha256
).digest()
b64_sig=b64encode(signature)
returnb64_sig.decode("utf-8")
def_create_add_domain_payload(domain: str) ->dict:
data= {"name": f"{domain}"}
returndata
defadd_domain(domain: str):
path="/v2/domains/"
url=f"{ENDPOINT}{path}"
data=_create_add_domain_payload(domain)
send_post_request(data, path, url)
def_add_scan_profile_payload(scan_profile: str) ->dict:
return {"endpoint": scan_profile, "unique": True}
defadd_scan_profile(scan_profile: str) ->strorNone:
path="/v2/profiles/"
url=f"{ENDPOINT}{path}"
data=_add_scan_profile_payload(scan_profile)
resp=send_post_request(data, path, url)
ifrespisnotNone:
scan_profile_token=resp.get("token", None)
returnscan_profile_token
returnNone
defstart_scan(scan_profile_token: str):
path=f"/v2/scans/{scan_profile_token}/"
url=f"{ENDPOINT}{path}"
resp=send_post_request(None, path, url)
ifrespisnotNone:
print(resp)
if__name__=="__main__":
add_domain("abcd.com")
sp_token=add_scan_profile("my.abcd.com")
start_scan(sp_token)