forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdomain.py
More file actions
493 lines (442 loc) · 20.6 KB
/
domain.py
File metadata and controls
493 lines (442 loc) · 20.6 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
"""This module defines the Domain class, which models domains monitored by Tracker
and offers methods to get data about domains."""
import json
import formatting
import organization as org
import queries
class Domain:
"""Class that represents a domain in Tracker.
Instance variables provide access to scalar fields for the domain in the GraphQL schema,
while methods return JSON data for non-scalar fields. Users should not typically
instantiate this class manually, instead use methods provided by
:class:`~tracker_client.client.Client` to get Domains.
The naming irregularity between :meth:`__init__` parameters and instance variables is to match
parameter names to the keys contained in the API responses. This allows easy
use of dict unpacking when creating a Domain instance. Instance variable names instead
adhere to Python convention.
:ivar Client client: the :class:`~tracker_client.client.Client` that created
this object. Provides a way for Domain methods to execute queries.
:ivar str domain: name of the domain.
:ivar str last_ran: timestamp for last scan run on the domain.
:ivar str dmarc_phase: DMARC implementation phase. # TODO: Add ref. to relevant docs
:ivar List[str] dkim_selectors: DKIM selector strings associated with the domain. # TODO: Add ref. to relevant docs
"""
def __init__(self, client, domain, lastRan, dmarcPhase, selectors):
"""
:param Client client: As in class docstring.
:param str domain: sets domain_name.
:param str lastRan: sets last_ran.
:param str dmarcPhase: sets dmarc_phase.
:param List[str] selectors: sets dkim_selectors.
"""
self.client = client
self.domain_name = domain
self.last_ran = lastRan
self.dmarc_phase = dmarcPhase
self.dkim_selectors = selectors
def __str__(self):
return self.domain_name
def __repr__(self):
return (
"Domain(client=%r, domain=%r, lastRan=%r, dmarcPhase=%r, selectors=%r)"
% (
self.client,
self.domain_name,
self.last_ran,
self.dmarc_phase,
self.dkim_selectors,
)
)
def get_status(self):
"""Return pass/fail status information for this Domain.
:return: formatted JSON data containing the domain's status.
:rtype: str
:Example:
>>> from tracker_client.client import Client
>>> client = Client()
>>> my_domains = client.get_domains()
>>> print(my_domains[0].get_status())
{
"foo.bar": {
"lastRan": "2021-01-23 22:33:26.921529",
"status": {
"https": "FAIL",
"ssl": "FAIL",
"dmarc": "PASS",
"dkim": "PASS",
"spf": "PASS"
}
}
}
"""
params = {"domain": self.domain_name}
result = self.client.execute_query(queries.DOMAIN_STATUS, params)
if "error" not in result:
result = formatting.format_domain_status(result)
return json.dumps(result, indent=4)
def get_monthly_dmarc_summary(self, month, year):
"""Get the DMARC summary for the specified month and year.
:param str month: the full name of a month.
:param int year: positive integer representing a year.
:return: formatted JSON data containing a DMARC summary.
:rtype: str
:Example:
>>> from tracker_client.client import Client
>>> client = Client()
>>> foobar = client.get_domain("foo.bar")
>>> print(foobar.get_monthly_dmarc("september", 2020))
{
"foo.bar": {
"month": "SEPTEMBER",
"year": "2020",
"categoryPercentages": {
"fullPassPercentage": 87,
"passSpfOnlyPercentage": 0,
"passDkimOnlyPercentage": 6,
"failPercentage": 8,
"totalMessages": 10534
}
}
}
"""
params = {"domain": self.domain_name, "month": month.upper(), "year": str(year)}
result = self.client.execute_query(queries.DMARC_SUMMARY, params)
if "error" not in result:
result = formatting.format_dmarc_monthly(result)
return json.dumps(result, indent=4)
def get_yearly_dmarc_summaries(self):
"""Get yearly DMARC summaries for a domain.
:return: formatted JSON data containing yearly DMARC summaries.
:rtype: str
:Example:
Output is truncated, you should expect more than 2 reports in the list.
>>> from tracker_client.client import Client
>>> client = Client()
>>> foobar = client.get_domain("foo.bar")
>>> print(foobar.get_yearly_dmarc())
{
"foo.bar": [
{
"month": "AUGUST",
"year": "2020",
"categoryPercentages": {
"fullPassPercentage": 90,
"passSpfOnlyPercentage": 0,
"passDkimOnlyPercentage": 5,
"failPercentage": 5,
"totalMessages": 7045
}
},
{
"month": "JULY",
"year": "2020",
"categoryPercentages": {
"fullPassPercentage": 82,
"passSpfOnlyPercentage": 0,
"passDkimOnlyPercentage": 11,
"failPercentage": 8,
"totalMessages": 6647
}
},
]
}
"""
params = {"domain": self.domain_name}
result = self.client.execute_query(queries.DMARC_YEARLY_SUMMARIES, params)
if "error" not in result:
result = formatting.format_dmarc_yearly(result)
return json.dumps(result, indent=4)
def get_all_results(self, all_scans=False):
"""Get web and email scan results for this Domain.
:param bool all_scans: if True, get all scans. If False, get only most recent.
:return: formatted JSON data containing all scan results for the domain.
:rtype: str
:Example:
>>> from tracker_client.client import Client
>>> client = Client()
>>> foobar = client.get_domain("foo.bar")
>>> print(foobar.get_all_results())
{
"foo.bar": {
"lastRan": "2021-03-08 23:01:27.416532",
"web": {...}, # See Domain.get_web_results example below
"email": {...} # See Domain.get_email_results example below
}
}
"""
num_results = 100 if all_scans else 1
params = {"domain": self.domain_name, "first": num_results}
result = self.client.execute_query(queries.ALL_RESULTS, params)
if "error" not in result:
result = formatting.format_all_results(result)
return json.dumps(result, indent=4)
def get_web_results(self, all_scans=False):
"""Get web scan results for this Domain.
:param bool all_scans: if True, get all scans. If False, get only most recent.
:return: formatted JSON data containing web scan results for the domain.
:rtype: str
:Example:
>>> from tracker_client.client import Client
>>> client = Client()
>>> foobar = client.get_domain("foo.bar")
>>> print(foobar.get_web_results())
{
"foo.bar": {
"lastRan": "2021-03-08 23:01:27.416532",
"web": {
"https": [
{
"timestamp": "2021-03-09T00:28:12.880Z",
"implementation": "Valid HTTPS",
"enforced": "Strict",
"hsts": "HSTS Fully Implemented",
"hstsAge": "31536000",
"preloaded": "HSTS Preloaded",
"positiveGuidanceTags": {},
"neutralGuidanceTags": {},
"negativeGuidanceTags": {}
}
],
"ssl": [
{
"timestamp": "2021-03-08 23:01:31.545568",
"strongCiphers": [
"TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
"TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256"
],
"strongCurves": [],
"acceptableCiphers": [
"TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384",
"TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256",
"TLS_DHE_RSA_WITH_AES_256_GCM_SHA384",
"TLS_DHE_RSA_WITH_AES_256_CBC_SHA256",
"TLS_DHE_RSA_WITH_AES_128_GCM_SHA256",
"TLS_DHE_RSA_WITH_AES_128_CBC_SHA256"
],
"acceptableCurves": [],
"weakCiphers": [],
"weakCurves": [
"prime256v1"
],
"ccsInjectionVulnerable": false,
"heartbleedVulnerable": false,
"supportsEcdhKeyExchange": true,
"positiveGuidanceTags": {
"ssl5": {
"tagName": "SSL-acceptable-certificate",
"guidance": "Certificate chain signed using SHA-256/SHA-384/AEAD",
"refLinks": [
{
"description": "6.1.3 Direction",
"refLink": "https://www.canada.ca/en/government/system/digital-government/modern-emerging-technologies/policy-implementation-notices/implementing-https-secure-web-connections-itpin.html#toc6"
}
],
"refLinksTech": [
{
"description": "See ITSP.40.062 for approved cipher list",
"refLink": "https://cyber.gc.ca/en/guidance/guidance-securely-configuring-network-protocols-itsp40062"
}
]
}
},
"neutralGuidanceTags": {},
"negativeGuidanceTags": {}
}
]
}
}
}
"""
num_results = 100 if all_scans else 1
params = {"domain": self.domain_name, "first": num_results}
result = self.client.execute_query(queries.WEB_RESULTS, params)
if "error" not in result:
result = formatting.format_web_results(result)
return json.dumps(result, indent=4)
def get_email_results(self, all_scans=False):
"""Get email scan results for this Domain.
:param bool all_scans: if True, get all scans. If False, get only most recent.
:return: formatted JSON data containing email scan results for the domain.
:rtype: str
:Example:
>>> from tracker_client.client import Client
>>> client = Client()
>>> foobar = client.get_domain("foo.bar")
>>> print(foobar.get_email_results())
{
"foo.bar": {
"lastRan": "2021-03-08 23:01:27.416532",
"email": {
"dkim": [
{
"timestamp": "2021-03-09T00:28:12.880Z",
"results": {
"edges": []
}
}
],
"dmarc": [
{
"timestamp": "2021-03-08 23:01:31.545568",
"record": "v=DMARC1; p=None; pct=100; rua=mailto:dmarc@cyber.gc.ca; ruf=mailto:dmarc@cyber.gc.ca; fo=1",
"pPolicy": "None",
"spPolicy": "None",
"pct": 100,
"positiveGuidanceTags": {
"dmarc23": {
"tagName": "DMARC-valid",
"guidance": "DMARC record is properly formed",
"refLinks": [
{
"description": "Implementation Guidance: Email Domain Protection",
"refLink": null
}
],
"refLinksTech": [
{
"description": null,
"refLink": null
}
]
}
},
"neutralGuidanceTags": {
"dmarc10": {
"tagName": "RUA-CCCS",
"guidance": "CCCS added to Aggregate sender list",
"refLinks": [
{
"description": "B.3.1 DMARC Records",
"refLink": null
}
],
"refLinksTech": [
{
"description": null,
"refLink": null
}
]
},
"dmarc14": {
"tagName": "TXT-DMARC-enabled",
"guidance": "Verification TXT records for all 3rd party senders exist",
"refLinks": [
{
"description": "TBD",
"refLink": null
}
],
"refLinksTech": [
{
"description": null,
"refLink": null
}
]
},
"dmarc17": {
"tagName": "SP-none",
"guidance": "Follow implementation guide",
"refLinks": [
{
"description": "A.3.5 Monitor DMARC Reports and Correct Misconfigurations",
"refLink": null
}
],
"refLinksTech": [
{
"description": "RFC 6.3 General Record Format, SP",
"refLink": null
}
]
}
},
"negativeGuidanceTags": {
"dmarc11": {
"tagName": "RUF-CCCS",
"guidance": "CCCS added to Forensic sender list",
"refLinks": [
{
"description": "Missing from guide",
"refLink": null
}
],
"refLinksTech": [
{
"description": null,
"refLink": null
}
]
}
}
}
],
"spf": [
{
"timestamp": "2021-03-08 23:01:31.545568",
"lookups": 5,
"record": "v=spf1 a:a.foo.bar a:b.foo.bar a:c.foo.bar include:spf.protection.outlook.com -all",
"spfDefault": "-all",
"positiveGuidanceTags": {
"spf12": {
"tagName": "SPF-valid",
"guidance": "SPF record is properly formed",
"refLinks": [
{
"description": "Implementation Guidance: Email Domain Protection",
"refLink": null
}
],
"refLinksTech": [
{
"description": null,
"refLink": null
}
]
}
},
"neutralGuidanceTags": {
"spf8": {
"tagName": "ALL-hardfail",
"guidance": "Follow implementation guide",
"refLinks": [
{
"description": "B.1.1 SPF Records",
"refLink": "https://cyber.gc.ca/en/guidance/implementation-guidance-email-domain-protection#annb11"
}
],
"refLinksTech": [
{
"description": null,
"refLink": null
}
]
}
},
"negativeGuidanceTags": {}
}
]
}
}
}
"""
num_results = 100 if all_scans else 1
params = {"domain": self.domain_name, "first": num_results}
result = self.client.execute_query(queries.EMAIL_RESULTS, params)
if "error" not in result:
result = formatting.format_email_results(result)
return json.dumps(result, indent=4)
def get_owners(self):
"""Get a list of Organizations that control this domain
:return: A list of one or more :class:`organization(s) <tracker_client.organization.Organization>`
responsible for this domain.
:rtype: list[Organization]
"""
params = {"domain": self.domain_name}
result = self.client.execute_query(queries.GET_DOMAIN_OWNERS, params)
if "error" in result:
print("Server error: ", result)
raise ValueError("Unable to get owners for " + self.domain_name)
org_list = []
for edge in result["findDomainByDomain"]["organizations"]["edges"]:
org_list.append(org.Organization(self.client, **edge["node"]))
return org_list