-
Notifications
You must be signed in to change notification settings - Fork 83
/
lookyloo.py
1343 lines (1185 loc) · 64.8 KB
/
lookyloo.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
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
from __future__ import annotations
import base64
import copy
import gzip
import json
import logging
import operator
import smtplib
import ssl
import time
from collections import defaultdict
from datetime import date, datetime, timedelta
from email.message import EmailMessage
from functools import cached_property
from io import BytesIO
from pathlib import Path
from typing import Any, Iterable, TYPE_CHECKING, overload, Literal
from urllib.parse import urlparse
from uuid import uuid4
from zipfile import ZipFile
import mmh3
from defang import defang # type: ignore[import-untyped]
from har2tree import CrawledTree, HostNode, URLNode
from lacuscore import (LacusCore,
CaptureStatus as CaptureStatusCore,
# CaptureResponse as CaptureResponseCore)
# CaptureResponseJson as CaptureResponseJsonCore,
CaptureSettings as CaptureSettingsCore)
from PIL import Image, UnidentifiedImageError
from playwrightcapture import get_devices
from puremagic import from_string, PureError # type: ignore[import-untyped]
from pylacus import (PyLacus,
CaptureStatus as CaptureStatusPy
# CaptureResponse as CaptureResponsePy,
# CaptureResponseJson as CaptureResponseJsonPy,
# CaptureSettings as CaptureSettingsPy
)
from pymisp import MISPAttribute, MISPEvent, MISPObject
from pysecuritytxt import PySecurityTXT, SecurityTXTNotAvailable
from pylookyloomonitoring import PyLookylooMonitoring # type: ignore[attr-defined]
from redis import ConnectionPool, Redis
from redis.connection import UnixDomainSocketConnection
from .capturecache import CaptureCache, CapturesIndex
from .context import Context
from .default import LookylooException, get_homedir, get_config, get_socket_path, safe_create_dir
from .exceptions import (MissingCaptureDirectory,
MissingUUID, TreeNeedsRebuild, NoValidHarFile)
from .helpers import (get_captures_dir, get_email_template,
get_resources_hashes, get_taxonomies,
uniq_domains, ParsedUserAgent, load_cookies, UserAgents,
get_useragent_for_requests, make_ts_from_dirname)
from .modules import (MISPs, PhishingInitiative, UniversalWhois,
UrlScan, VirusTotal, Phishtank, Hashlookup,
RiskIQ, RiskIQError, Pandora, URLhaus, CIRCLPDNS)
if TYPE_CHECKING:
from playwright.async_api import Cookie
class CaptureSettings(CaptureSettingsCore, total=False):
'''The capture settings that can be passed to Lookyloo'''
listing: int | None
not_queued: int | None
auto_report: bool | str | dict[str, str] | None
dnt: str | None
browser_name: str | None
os: str | None
parent: str | None
class Lookyloo():
def __init__(self, cache_max_size: int | None=None) -> None:
'''Initialize lookyloo.
:param cache_max_size: The maximum size of the cache. Alows to display captures metadata without getting it from redis
This cache is *not* useful for background indexing or pickle building, only for the front end.
So it should always be None *unless* we're running the background processes.
'''
self.logger = logging.getLogger(f'{self.__class__.__name__}')
self.logger.setLevel(get_config('generic', 'loglevel'))
self.user_agents = UserAgents()
self.is_public_instance = get_config('generic', 'public_instance')
self.public_domain = get_config('generic', 'public_domain')
self.global_proxy = {}
if global_proxy := get_config('generic', 'global_proxy'):
if global_proxy.get('enable'):
self.global_proxy = copy.copy(global_proxy)
self.global_proxy.pop('enable')
self.securitytxt = PySecurityTXT(useragent=get_useragent_for_requests())
self.taxonomies = get_taxonomies()
self.redis_pool: ConnectionPool = ConnectionPool(connection_class=UnixDomainSocketConnection,
path=get_socket_path('cache'), decode_responses=True)
self.capture_dir: Path = get_captures_dir()
self._priority = get_config('generic', 'priority')
# Initialize 3rd party components
# ## Initialize MISP(s)
try_old_config = False
# New config
self.misps = MISPs(config_name='MultipleMISPs')
if not self.misps.available:
self.logger.warning('Unable to setup the MISPs module')
try_old_config = True
if try_old_config:
# Legacy MISP config, now use MultipleMISPs key to support more than one MISP instance
try:
if misp_config := get_config('modules', 'MISP'):
misps_config = {'default': 'MISP', 'instances': {'MISP': misp_config}}
self.misps = MISPs(config=misps_config)
if self.misps.available:
self.logger.warning('Please migrate the MISP config to the "MultipleMISPs" key in the config, and remove the "MISP" key')
else:
self.logger.warning('Unable to setup the MISP module')
except Exception:
# The key was removed from the config, and the sample config
pass
# ## Done with MISP(s)
self.pi = PhishingInitiative(config_name='PhishingInitiative')
self.vt = VirusTotal(config_name='VirusTotal')
self.uwhois = UniversalWhois(config_name='UniversalWhois')
self.urlscan = UrlScan(config_name='UrlScan')
self.phishtank = Phishtank(config_name='Phishtank')
self.hashlookup = Hashlookup(config_name='Hashlookup')
self.riskiq = RiskIQ(config_name='RiskIQ')
self.pandora = Pandora(config_name='Pandora')
self.urlhaus = URLhaus(config_name='URLhaus')
self.circl_pdns = CIRCLPDNS(config_name='CIRCLPDNS')
self.monitoring_enabled = False
if monitoring_config := get_config('generic', 'monitoring'):
if monitoring_config['enable']:
self.monitoring = PyLookylooMonitoring(monitoring_config['url'], get_useragent_for_requests())
if self.monitoring.is_up:
self.monitoring_enabled = True
# NOTE: maybe move that somewhere else: we'll need to restart the webserver
# if we change the settings in the monitoring instance
self.monitoring_settings = self.monitoring.instance_settings()
self.logger.info('Initializing context...')
self.context = Context()
self.logger.info('Context initialized.')
self.logger.info('Initializing index...')
self._captures_index = CapturesIndex(self.redis, self.context, maxsize=cache_max_size)
self.logger.info('Index initialized.')
# init lacus
self.lacus
@property
def redis(self) -> Redis: # type: ignore[type-arg]
return Redis(connection_pool=self.redis_pool)
@cached_property
def lacus(self) -> PyLacus | LacusCore:
has_remote_lacus = False
self._lacus: PyLacus | LacusCore
if get_config('generic', 'remote_lacus'):
remote_lacus_config = get_config('generic', 'remote_lacus')
if remote_lacus_config.get('enable'):
self.logger.info("Remote lacus enabled, trying to set it up...")
lacus_retries = 10
while lacus_retries > 0:
remote_lacus_url = remote_lacus_config.get('url')
self._lacus = PyLacus(remote_lacus_url)
if self._lacus.is_up:
has_remote_lacus = True
self.logger.info(f"Remote lacus enabled to {remote_lacus_url}.")
break
lacus_retries -= 1
self.logger.warning(f"Unable to setup remote lacus to {remote_lacus_url}, trying again {lacus_retries} more time(s).")
time.sleep(10)
else:
raise LookylooException('Remote lacus is enabled but unreachable.')
if not has_remote_lacus:
# We need a redis connector that doesn't decode.
redis: Redis = Redis(unix_socket_path=get_socket_path('cache')) # type: ignore[type-arg]
self._lacus = LacusCore(redis, tor_proxy=get_config('generic', 'tor_proxy'),
max_capture_time=get_config('generic', 'max_capture_time'),
only_global_lookups=get_config('generic', 'only_global_lookups'),
loglevel=get_config('generic', 'loglevel'))
return self._lacus
def add_context(self, capture_uuid: str, /, urlnode_uuid: str, *, ressource_hash: str,
legitimate: bool, malicious: bool, details: dict[str, dict[str, str]]) -> None:
'''Adds context information to a capture or a URL node'''
if malicious:
self.context.add_malicious(ressource_hash, details['malicious'])
if legitimate:
self.context.add_legitimate(ressource_hash, details['legitimate'])
def add_to_legitimate(self, capture_uuid: str, /, hostnode_uuid: str | None=None, urlnode_uuid: str | None=None) -> None:
'''Mark a full capture as legitimate.
Iterates over all the nodes and mark them all as legitimate too.'''
ct = self.get_crawled_tree(capture_uuid)
self.context.mark_as_legitimate(ct, hostnode_uuid, urlnode_uuid)
def remove_pickle(self, capture_uuid: str, /) -> None:
'''Remove the pickle from a specific capture.'''
self._captures_index.remove_pickle(capture_uuid)
def rebuild_cache(self) -> None:
'''Flush and rebuild the redis cache. Doesn't remove the pickles.
The cached captures will be rebuild when loading the index.'''
self.redis.flushdb()
def rebuild_all(self) -> None:
'''Flush and rebuild the redis cache, and delete all the pickles.
The captures will be rebuilt by the background indexer'''
self._captures_index.rebuild_all()
def get_urlnode_from_tree(self, capture_uuid: str, /, node_uuid: str) -> URLNode:
'''Get a URL node from a tree, by UUID'''
ct = self.get_crawled_tree(capture_uuid)
return ct.root_hartree.get_url_node_by_uuid(node_uuid)
def get_hostnode_from_tree(self, capture_uuid: str, /, node_uuid: str) -> HostNode:
'''Get a host node from a tree, by UUID'''
ct = self.get_crawled_tree(capture_uuid)
return ct.root_hartree.get_host_node_by_uuid(node_uuid)
def get_statistics(self, capture_uuid: str, /) -> dict[str, Any]:
'''Get the statistics of a capture.'''
ct = self.get_crawled_tree(capture_uuid)
return ct.root_hartree.stats
def get_info(self, capture_uuid: str, /) -> dict[str, Any]:
'''Get basic information about the capture.'''
cache = self.capture_cache(capture_uuid)
if not cache:
return {'error': f'Unable to find UUID {capture_uuid} in the cache.'}
if not hasattr(cache, 'uuid'):
self.logger.critical(f'Cache for {capture_uuid} is broken: {cache}.')
return {'error': f'Sorry, the capture {capture_uuid} is broken, please report it to the admin.'}
to_return = {'uuid': cache.uuid,
'url': cache.url if hasattr(cache, 'url') else 'Unable to get URL for the capture'}
if hasattr(cache, 'error') and cache.error:
to_return['error'] = cache.error
if hasattr(cache, 'title'):
to_return['title'] = cache.title
if hasattr(cache, 'timestamp'):
to_return['capture_time'] = cache.timestamp.isoformat()
if hasattr(cache, 'user_agent') and cache.user_agent:
to_return['user_agent'] = cache.user_agent
if hasattr(cache, 'referer'):
to_return['referer'] = cache.referer if cache.referer else ''
return to_return
def get_meta(self, capture_uuid: str, /) -> dict[str, str]:
'''Get the meta informations from a capture (mostly, details about the User Agent used.)'''
cache = self.capture_cache(capture_uuid)
if not cache:
return {}
metafile = cache.capture_dir / 'meta'
if metafile.exists():
with metafile.open('r') as f:
return json.load(f)
if not cache.user_agent:
return {}
meta = {}
ua = ParsedUserAgent(cache.user_agent)
meta['user_agent'] = ua.string
if ua.platform:
meta['os'] = ua.platform
if ua.browser:
if ua.version:
meta['browser'] = f'{ua.browser} {ua.version}'
else:
meta['browser'] = ua.browser
if not meta:
# UA not recognized
self.logger.info(f'Unable to recognize the User agent: {ua}')
with metafile.open('w') as f:
json.dump(meta, f)
return meta
def get_capture_settings(self, capture_uuid: str, /) -> CaptureSettings:
cache = self.capture_cache(capture_uuid)
if not cache:
return {}
cs_file = cache.capture_dir / 'capture_settings.json'
if cs_file.exists():
with cs_file.open('r') as f:
return json.load(f)
return {}
def categories_capture(self, capture_uuid: str, /) -> dict[str, Any]:
'''Get all the categories related to a capture, in MISP Taxonomies format'''
categ_file = self._captures_index[capture_uuid].capture_dir / 'categories'
# get existing categories if possible
if categ_file.exists():
with categ_file.open() as f:
current_categories = [line.strip() for line in f.readlines()]
return {e: self.taxonomies.revert_machinetag(e) for e in current_categories}
return {}
def categorize_capture(self, capture_uuid: str, /, category: str) -> None:
'''Add a category (MISP Taxonomy tag) to a capture.'''
if not get_config('generic', 'enable_categorization'):
return
# Make sure the category is mappable to a taxonomy.
self.taxonomies.revert_machinetag(category)
categ_file = self._captures_index[capture_uuid].capture_dir / 'categories'
# get existing categories if possible
if categ_file.exists():
with categ_file.open() as f:
current_categories = {line.strip() for line in f.readlines()}
else:
current_categories = set()
current_categories.add(category)
with categ_file.open('w') as f:
f.writelines(f'{t}\n' for t in current_categories)
def uncategorize_capture(self, capture_uuid: str, /, category: str) -> None:
'''Remove a category (MISP Taxonomy tag) from a capture.'''
if not get_config('generic', 'enable_categorization'):
return
categ_file = self._captures_index[capture_uuid].capture_dir / 'categories'
# get existing categories if possible
if categ_file.exists():
with categ_file.open() as f:
current_categories = {line.strip() for line in f.readlines()}
else:
current_categories = set()
current_categories.remove(category)
with categ_file.open('w') as f:
f.writelines(f'{t}\n' for t in current_categories)
def trigger_modules(self, capture_uuid: str, /, force: bool=False, auto_trigger: bool=False) -> dict[str, Any]:
'''Launch the 3rd party modules on a capture.
It uses the cached result *if* the module was triggered the same day.
The `force` flag re-triggers the module regardless of the cache.'''
try:
ct = self.get_crawled_tree(capture_uuid)
except LookylooException:
self.logger.warning(f'Unable to trigger the modules unless the tree ({capture_uuid}) is cached.')
return {'error': f'UUID {capture_uuid} is either unknown or the tree is not ready yet.'}
self.uwhois.capture_default_trigger(ct, force=force, auto_trigger=auto_trigger)
self.hashlookup.capture_default_trigger(ct, auto_trigger=auto_trigger)
to_return: dict[str, dict[str, Any]] = {'PhishingInitiative': {}, 'VirusTotal': {}, 'UrlScan': {},
'URLhaus': {}}
if cache := self.capture_cache(capture_uuid):
to_return['PhishingInitiative'] = self.pi.capture_default_trigger(cache, force=force, auto_trigger=auto_trigger)
to_return['VirusTotal'] = self.vt.capture_default_trigger(cache, force=force, auto_trigger=auto_trigger)
to_return['UrlScan'] = self.urlscan.capture_default_trigger(
cache,
visibility='unlisted' if (cache and cache.no_index) else 'public',
force=force, auto_trigger=auto_trigger)
to_return['Phishtank'] = self.phishtank.capture_default_trigger(cache, auto_trigger=auto_trigger)
to_return['URLhaus'] = self.urlhaus.capture_default_trigger(cache, auto_trigger=auto_trigger)
return to_return
def get_modules_responses(self, capture_uuid: str, /) -> dict[str, Any]:
'''Get the responses of the modules from the cached responses on the disk'''
cache = self.capture_cache(capture_uuid)
# TODO: return a message when we cannot get the modules responses, update the code checking if it is falsy accordingly.
if not cache:
self.logger.warning(f'Unable to get the modules responses unless the capture {capture_uuid} is cached')
return {}
if not hasattr(cache, 'url'):
self.logger.warning(f'The capture {capture_uuid} does not have a URL in the cache, it is broken.')
return {}
to_return: dict[str, Any] = {}
if self.vt.available:
to_return['vt'] = {}
if hasattr(cache, 'redirects') and cache.redirects:
for redirect in cache.redirects:
to_return['vt'][redirect] = self.vt.get_url_lookup(redirect)
else:
to_return['vt'][cache.url] = self.vt.get_url_lookup(cache.url)
if self.pi.available:
to_return['pi'] = {}
if hasattr(cache, 'redirects') and cache.redirects:
for redirect in cache.redirects:
to_return['pi'][redirect] = self.pi.get_url_lookup(redirect)
else:
to_return['pi'][cache.url] = self.pi.get_url_lookup(cache.url)
if self.phishtank.available:
to_return['phishtank'] = {'urls': {}, 'ips_hits': {}}
if hasattr(cache, 'redirects') and cache.redirects:
for redirect in cache.redirects:
to_return['phishtank']['urls'][redirect] = self.phishtank.get_url_lookup(redirect)
else:
to_return['phishtank']['urls'][cache.url] = self.phishtank.get_url_lookup(cache.url)
ips_hits = self.phishtank.lookup_ips_capture(cache)
if ips_hits:
to_return['phishtank']['ips_hits'] = ips_hits
if self.urlhaus.available:
to_return['urlhaus'] = {'urls': {}}
if hasattr(cache, 'redirects') and cache.redirects:
for redirect in cache.redirects:
to_return['urlhaus']['urls'][redirect] = self.urlhaus.get_url_lookup(redirect)
else:
to_return['urlhaus']['urls'][cache.url] = self.urlhaus.get_url_lookup(cache.url)
if self.urlscan.available:
to_return['urlscan'] = {'submission': {}, 'result': {}}
to_return['urlscan']['submission'] = self.urlscan.get_url_submission(cache)
if to_return['urlscan']['submission'] and 'uuid' in to_return['urlscan']['submission']:
# The submission was done, try to get the results
result = self.urlscan.url_result(cache)
if 'error' not in result:
to_return['urlscan']['result'] = result
return to_return
def get_historical_lookups(self, capture_uuid: str, /, force: bool=False) -> dict[str, Any]:
# this method is only trigered when the user wants to get more details about the capture
# by looking at Passive DNS systems, check if there are hits in the current capture
# in another one and things like that. The trigger_modules method is for getting
# information about the current status of the capture in other systems.
cache = self.capture_cache(capture_uuid)
if not cache:
self.logger.warning(f'Unable to get the modules responses unless the capture {capture_uuid} is cached')
return {}
to_return: dict[str, Any] = defaultdict(dict)
if self.riskiq.available:
try:
self.riskiq.capture_default_trigger(cache)
if hasattr(cache, 'redirects') and cache.redirects:
hostname = urlparse(cache.redirects[-1]).hostname
else:
hostname = urlparse(cache.url).hostname
if hostname:
if _riskiq_entries := self.riskiq.get_passivedns(hostname):
to_return['riskiq'] = _riskiq_entries
except RiskIQError as e:
self.logger.warning(e.response.content)
if self.circl_pdns.available:
self.circl_pdns.capture_default_trigger(cache)
if hasattr(cache, 'redirects') and cache.redirects:
hostname = urlparse(cache.redirects[-1]).hostname
else:
hostname = urlparse(cache.url).hostname
if hostname:
if _circl_entries := self.circl_pdns.get_passivedns(hostname):
to_return['circl_pdns'][hostname] = _circl_entries
return to_return
def hide_capture(self, capture_uuid: str, /) -> None:
"""Add the capture in the hidden pool (not shown on the front page)
NOTE: it won't remove the correlations until they are rebuilt.
"""
capture_dir = self._captures_index[capture_uuid].capture_dir
self.redis.hset(str(capture_dir), 'no_index', 1)
(capture_dir / 'no_index').touch()
self._captures_index.reload_cache(capture_uuid)
def update_tree_cache_info(self, process_id: int, classname: str) -> None:
self.redis.hset('tree_cache', f'{process_id}|{classname}', str(self._captures_index.lru_cache_status()))
def clear_tree_cache(self) -> None:
self._captures_index.lru_cache_clear()
def sorted_capture_cache(self, capture_uuids: Iterable[str] | None=None, cached_captures_only: bool=True, index_cut_time: datetime | None=None) -> list[CaptureCache]:
'''Get all the captures in the cache, sorted by timestamp (new -> old).
By default, this method will only return the captures that are currently cached.'''
# Make sure we do not try to load archived captures that would still be in 'lookup_dirs'
cut_time = (datetime.now() - timedelta(days=get_config('generic', 'archive') - 1))
if index_cut_time:
if index_cut_time < cut_time:
index_cut_time = cut_time
else:
index_cut_time = cut_time
if capture_uuids is None:
capture_uuids = {uuid for uuid, directory in self.redis.hscan_iter('lookup_dirs')
if make_ts_from_dirname(directory.rsplit('/', 1)[-1]) > index_cut_time}
# NOTE: we absolutely have to respect the cached_captures_only setting and
# never overwrite it. This method is called to display the index
# and if we try to display everything, including the non-cached entries,
# the index can get stuck building a lot of captures
# cached_captures_only = False
if not capture_uuids:
# No captures at all on the instance
return []
if cached_captures_only:
# Do not try to build pickles
capture_uuids = set(capture_uuids) & self._captures_index.cached_captures
all_cache: list[CaptureCache] = [self._captures_index[uuid] for uuid in capture_uuids
if self.capture_cache(uuid)
and hasattr(self._captures_index[uuid], 'timestamp')]
all_cache.sort(key=operator.attrgetter('timestamp'), reverse=True)
return all_cache
def get_capture_status(self, capture_uuid: str, /) -> CaptureStatusCore | CaptureStatusPy:
'''Returns the status (queued, ongoing, done, or UUID unknown)'''
if self.redis.hexists('lookup_dirs', capture_uuid):
return CaptureStatusCore.DONE
elif self.redis.sismember('ongoing', capture_uuid):
# Post-processing on lookyloo's side
return CaptureStatusCore.ONGOING
try:
lacus_status = self.lacus.get_capture_status(capture_uuid)
except Exception as e:
self.logger.warning(f'Unable to get the status for {capture_uuid} from lacus: {e}')
if self.redis.zscore('to_capture', capture_uuid) is not None:
return CaptureStatusCore.QUEUED
else:
return CaptureStatusCore.UNKNOWN
if (lacus_status == CaptureStatusCore.UNKNOWN
and self.redis.zscore('to_capture', capture_uuid) is not None):
# If we do the query before lacus picks it up, we will tell to the user that the UUID doesn't exists.
return CaptureStatusCore.QUEUED
elif lacus_status == CaptureStatusCore.DONE:
# Done on lacus side, but not processed by Lookyloo yet (it would be in lookup_dirs)
return CaptureStatusCore.ONGOING
return lacus_status
def capture_cache(self, capture_uuid: str, /, *, force_update: bool = False) -> CaptureCache | None:
"""Get the cache from redis, rebuild the tree if the internal UUID changed => slow"""
try:
cache = self._captures_index[capture_uuid]
if cache and force_update:
needs_update = False
if not cache.user_agent and not cache.error:
# 2022-12-07: New cache format, store the user agent and referers.
needs_update = True
if not hasattr(cache, 'title') or not cache.title:
# 2023-17-27: The title should *always* be there,
# unless the HAR file is missing or broken
needs_update = True
if needs_update:
self._captures_index.reload_cache(capture_uuid)
cache = self._captures_index[capture_uuid]
return cache
except NoValidHarFile:
self.logger.debug('No HAR files, {capture_uuid} is a broken capture.')
return None
except MissingCaptureDirectory as e:
# The UUID is in the captures but the directory is not on the disk.
self.logger.warning(f'Missing Directory: {e}')
return None
except MissingUUID:
if self.get_capture_status(capture_uuid) not in [CaptureStatusCore.QUEUED, CaptureStatusCore.ONGOING]:
self.logger.info(f'Unable to find {capture_uuid} (not in the cache and/or missing capture directory).')
return None
except LookylooException as e:
self.logger.warning(f'Lookyloo Exception: {e}')
return None
except Exception as e:
self.logger.exception(e)
return None
def get_crawled_tree(self, capture_uuid: str, /) -> CrawledTree:
'''Get the generated tree in ETE Toolkit format.
Loads the pickle if it exists, creates it otherwise.'''
try:
return self._captures_index[capture_uuid].tree
except TreeNeedsRebuild:
self._captures_index.reload_cache(capture_uuid)
return self._captures_index[capture_uuid].tree
def _prepare_lacus_query(self, query: CaptureSettings) -> CaptureSettings:
# Remove the none, it makes redis unhappy
query = {k: v for k, v in query.items() if v is not None} # type: ignore[assignment]
if 'url' in query and query['url'] is not None:
# Make sure the URL does not have any space or newline
query['url'] = query['url'].strip()
# NOTE: Lookyloo' capture can pass a do not track header independently from the default headers, merging it here
headers = query.pop('headers', {})
if 'dnt' in query:
if isinstance(headers, str):
headers += f'\nDNT: {query.pop("dnt")}'
headers = headers.strip()
elif isinstance(headers, dict):
dnt_entry = query.pop("dnt")
if dnt_entry:
headers['DNT'] = dnt_entry.strip()
if headers:
query['headers'] = headers
# NOTE: Lookyloo can get the cookies in somewhat weird formats, mornalizing them
query['cookies'] = load_cookies(query.pop('cookies', None))
# NOTE: Make sure we have a useragent
user_agent = query.pop('user_agent', None)
if not user_agent:
# Catch case where the UA is broken on the UI, and the async submission.
self.user_agents.user_agents # triggers an update of the default UAs
if 'device_name' not in query:
query['user_agent'] = user_agent if user_agent else self.user_agents.default['useragent']
# NOTE: the document must be base64 encoded
document: str | bytes | None = query.pop('document', None)
if document:
if isinstance(document, bytes):
query['document'] = base64.b64encode(document).decode()
else:
query['document'] = document
return query
def enqueue_capture(self, query: CaptureSettings, source: str, user: str, authenticated: bool) -> str:
'''Enqueue a query in the capture queue (used by the UI and the API for asynchronous processing)'''
def get_priority(source: str, user: str, authenticated: bool) -> int:
src_prio: int = self._priority['sources'][source] if source in self._priority['sources'] else -1
if not authenticated:
usr_prio = self._priority['users']['_default_anon']
# reduce priority for anonymous users making lots of captures
queue_size = self.redis.zscore('queues', f'{source}|{authenticated}|{user}')
if queue_size is None:
queue_size = 0
usr_prio -= int(queue_size / 10)
else:
usr_prio = self._priority['users'][user] if self._priority['users'].get(user) else self._priority['users']['_default_auth']
return src_prio + usr_prio
for key, value in query.items():
if isinstance(value, bool):
query[key] = 1 if value else 0 # type: ignore[literal-required]
elif isinstance(value, (list, dict)):
query[key] = json.dumps(value) if value else None # type: ignore[literal-required]
query = self._prepare_lacus_query(query)
priority = get_priority(source, user, authenticated)
if priority < -100:
# Someone is probably abusing the system with useless URLs, remove them from the index
query['listing'] = 0
try:
perma_uuid = self.lacus.enqueue( # type: ignore[misc]
url=query.get('url', None),
document_name=query.get('document_name', None),
document=query.get('document', None),
# depth=query.get('depth', 0),
browser=query.get('browser', None), # type: ignore[arg-type]
device_name=query.get('device_name', None),
user_agent=query.get('user_agent', None),
proxy=self.global_proxy if self.global_proxy else query.get('proxy', None),
general_timeout_in_sec=query.get('general_timeout_in_sec', None),
cookies=query.get('cookies', None),
headers=query.get('headers', None),
http_credentials=query.get('http_credentials', None),
viewport=query.get('viewport', None),
referer=query.get('referer', None),
timezone_id=query.get('timezone_id', None),
locale=query.get('locale', None),
geolocation=query.get('geolocation', None),
color_scheme=query.get('color_scheme', None),
rendered_hostname_only=query.get('rendered_hostname_only', True),
with_favicon=query.get('with_favicon', True),
allow_tracking=query.get('allow_tracking', True),
# force=query.get('force', False),
# recapture_interval=query.get('recapture_interval', 300),
priority=priority
)
except Exception as e:
self.logger.critical(f'Unable to enqueue capture: {e}')
perma_uuid = str(uuid4())
query['not_queued'] = 1
finally:
if (not self.redis.hexists('lookup_dirs', perma_uuid) # already captured
and self.redis.zscore('to_capture', perma_uuid) is None): # capture ongoing
# Make the settings redis compatible
mapping_capture: dict[str, bytes | float | int | str] = {}
for key, value in query.items():
if isinstance(value, bool):
mapping_capture[key] = 1 if value else 0
elif isinstance(value, (list, dict)):
if value:
mapping_capture[key] = json.dumps(value)
elif value is not None:
mapping_capture[key] = value # type: ignore[assignment]
p = self.redis.pipeline()
p.zadd('to_capture', {perma_uuid: priority})
p.hset(perma_uuid, mapping=mapping_capture) # type: ignore[arg-type]
p.zincrby('queues', 1, f'{source}|{authenticated}|{user}')
p.set(f'{perma_uuid}_mgmt', f'{source}|{authenticated}|{user}')
p.execute()
return perma_uuid
def takedown_details(self, hostnode: HostNode) -> dict[str, Any]:
if not self.uwhois.available:
self.logger.warning('UWhois module not enabled, unable to use this method')
raise LookylooException('UWhois module not enabled, unable to use this method')
to_return = {'hostname': hostnode.name,
'contacts': self.uwhois.whois(hostnode.name, contact_email_only=True), # List of emails from whois
'ips': {}, # ip: [list of contacts from whois]
'asns': {}, # ASN: [list of contacts from whois]
'all_emails': set()
}
to_return['ips'] = {ip: self.uwhois.whois(ip, contact_email_only=True) for ip in set(hostnode.resolved_ips['v4']) | set(hostnode.resolved_ips['v6'])}
to_return['asns'] = {asn['asn']: self.uwhois.whois(f'AS{asn["asn"]}', contact_email_only=True) for asn in hostnode.ipasn.values()}
# try to get contact from security.txt file
try:
txtfile = self.securitytxt.get(hostnode.name)
parsed = self.securitytxt.parse(txtfile)
to_return['securitytxt'] = parsed
if 'contact' in parsed:
if isinstance(parsed['contact'], str):
to_return['all_emails'].add(parsed['contact'].lstrip('mailto:'))
else:
to_return['all_emails'] |= {contact.lstrip('mailto:') for contact in parsed['contact'] if contact.startswith('mailto:')}
except SecurityTXTNotAvailable as e:
self.logger.debug(f'Unable to get a security.txt file: {e}')
for emails in to_return['ips'].values():
to_return['all_emails'] |= set(emails)
for emails in to_return['asns'].values():
to_return['all_emails'] |= set(emails)
# URLs specific details
# # IPFS
for url in hostnode.urls:
for h in url.response['headers']:
if h['name'].lower().startswith('x-ipfs'):
# got an ipfs thing
to_return['all_emails'].add('abuse@ipfs.io')
if 'urls' not in to_return:
to_return['urls'] = {'ipfs': {}}
if url.name not in to_return['urls']['ipfs']:
to_return['urls']['ipfs'][url.name] = ['abuse@ipfs.io']
else:
to_return['urls']['ipfs'][url.name].append('abuse@ipfs.io')
break
to_return['all_emails'] = list(to_return['all_emails'])
return to_return
def contacts(self, capture_uuid: str, /) -> list[dict[str, Any]]:
capture = self.get_crawled_tree(capture_uuid)
rendered_hostnode = self.get_hostnode_from_tree(capture_uuid, capture.root_hartree.rendered_node.hostnode_uuid)
result = []
for node in reversed(rendered_hostnode.get_ancestors()):
result.append(self.takedown_details(node))
result.append(self.takedown_details(rendered_hostnode))
return result
def send_mail(self, capture_uuid: str, /, email: str='', comment: str | None=None) -> bool | dict[str, Any]:
'''Send an email notification regarding a specific capture'''
if not get_config('generic', 'enable_mail_notification'):
return {"error": "Unable to send mail: mail notification disabled"}
email_config = get_config('generic', 'email')
smtp_auth = get_config('generic', 'email_smtp_auth')
redirects = ''
initial_url = ''
if cache := self.capture_cache(capture_uuid):
if hasattr(cache, 'url'):
if email_config['defang_urls']:
initial_url = defang(cache.url, colon=True, all_dots=True)
else:
initial_url = cache.url
else:
initial_url = 'Unable to get URL from cache, this is probably a bug.'
if hasattr(cache, 'error') and cache.error:
initial_url += f' - {cache.error}'
if hasattr(cache, 'redirects') and cache.redirects:
redirects = "Redirects:\n"
if email_config['defang_urls']:
redirects += defang('\n'.join(cache.redirects), colon=True, all_dots=True)
else:
redirects += '\n'.join(cache.redirects)
else:
redirects = "No redirects."
msg = EmailMessage()
msg['From'] = email_config['from']
if email:
msg['Reply-To'] = email
msg['To'] = email_config['to']
msg['Subject'] = email_config['subject']
body = get_email_template()
body = body.format(
recipient=msg['To'].addresses[0].display_name,
domain=self.public_domain,
uuid=capture_uuid,
initial_url=initial_url,
redirects=redirects,
comment=comment if comment else '',
sender=msg['From'].addresses[0].display_name,
)
msg.set_content(body)
try:
contact_for_takedown = self.contacts(capture_uuid)
msg.add_attachment(json.dumps(contact_for_takedown, indent=2), filename='contacts.json')
except Exception as e:
self.logger.warning(f'Unable to get the contacts: {e}')
try:
with smtplib.SMTP(email_config['smtp_host'], email_config['smtp_port']) as s:
if smtp_auth['auth']:
if smtp_auth['smtp_use_starttls']:
if smtp_auth['verify_certificate'] is False:
ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
s.starttls(context=ssl_context)
else:
s.starttls()
s.login(smtp_auth['smtp_user'], smtp_auth['smtp_pass'])
s.send_message(msg)
except Exception as e:
self.logger.exception(e)
self.logger.warning(msg.as_string())
return {"error": "Unable to send mail"}
return True
def _get_raw(self, capture_uuid: str, /, extension: str='*', all_files: bool=True) -> BytesIO:
'''Get file(s) from the capture directory'''
try:
capture_dir = self._captures_index[capture_uuid].capture_dir
except NoValidHarFile:
return BytesIO(f'Capture {capture_uuid} has no HAR entries, which means it is broken.'.encode())
except MissingUUID:
return BytesIO(f'Capture {capture_uuid} not unavailable, try again later.'.encode())
except MissingCaptureDirectory:
return BytesIO(f'No capture {capture_uuid} on the system (directory missing).'.encode())
all_paths = sorted(list(capture_dir.glob(f'*.{extension}')))
if not all_files:
# Only get the first one in the list
if not all_paths:
return BytesIO()
with open(all_paths[0], 'rb') as f:
return BytesIO(f.read())
to_return = BytesIO()
# Add uuid file to the export, allows to keep the same UUID across platforms.
# NOTE: the UUID file will always be added, as long as all_files is True,
# even if we pass an extension
all_paths.append(capture_dir / 'uuid')
with ZipFile(to_return, 'w') as myzip:
for path in all_paths:
if 'pickle' in path.name:
# We do not want to export the pickle
continue
myzip.write(path, arcname=f'{capture_dir.name}/{path.name}')
to_return.seek(0)
return to_return
@overload
def get_potential_favicons(self, capture_uuid: str, /, all_favicons: Literal[False], for_datauri: Literal[True]) -> tuple[str, str]:
...
@overload
def get_potential_favicons(self, capture_uuid: str, /, all_favicons: Literal[True], for_datauri: Literal[False]) -> BytesIO:
...
def get_potential_favicons(self, capture_uuid: str, /, all_favicons: bool=False, for_datauri: bool=False) -> BytesIO | tuple[str, str]:
'''Get rendered HTML'''
fav = self._get_raw(capture_uuid, 'potential_favicons.ico', all_favicons)
if not all_favicons and for_datauri:
favicon = fav.getvalue()
if not favicon:
return '', ''
try:
mimetype = from_string(favicon, mime=True)
return mimetype, base64.b64encode(favicon).decode()
except PureError:
self.logger.warning(f'Unable to get the mimetype of the favicon for {capture_uuid}.')
return '', ''
return fav
def get_html(self, capture_uuid: str, /, all_html: bool=False) -> BytesIO:
'''Get rendered HTML'''
return self._get_raw(capture_uuid, 'html', all_html)
def get_data(self, capture_uuid: str, /) -> tuple[str, BytesIO]:
'''Get the data'''
return self._get_raw(capture_uuid, 'data.filename', False).getvalue().decode(), self._get_raw(capture_uuid, 'data', False)
def get_cookies(self, capture_uuid: str, /, all_cookies: bool=False) -> BytesIO:
'''Get the cookie(s)'''
return self._get_raw(capture_uuid, 'cookies.json', all_cookies)
def get_screenshot(self, capture_uuid: str, /) -> BytesIO:
'''Get the screenshot(s) of the rendered page'''
return self._get_raw(capture_uuid, 'png', all_files=False)
def get_screenshot_thumbnail(self, capture_uuid: str, /, for_datauri: bool=False, width: int=64) -> str | BytesIO:
'''Get the thumbnail of the rendered page. Always crop to a square.'''
to_return = BytesIO()
size = width, width
try:
s = self.get_screenshot(capture_uuid)
orig_screenshot = Image.open(s)
to_thumbnail = orig_screenshot.crop((0, 0, orig_screenshot.width, orig_screenshot.width))
except Image.DecompressionBombError as e:
# The image is most probably too big: https://pillow.readthedocs.io/en/stable/reference/Image.html
self.logger.warning(f'Unable to generate the screenshot thumbnail of {capture_uuid}: image too big ({e}).')
error_img: Path = get_homedir() / 'website' / 'web' / 'static' / 'error_screenshot.png'
to_thumbnail = Image.open(error_img)
except UnidentifiedImageError as e:
# We might have a direct download link, and no screenshot. Assign the thumbnail accordingly.
try:
filename, data = self.get_data(capture_uuid)
if filename:
self.logger.info(f'{capture_uuid} is is a download link, set thumbnail.')
error_img = get_homedir() / 'website' / 'web' / 'static' / 'download.png'
else:
# No screenshot and no data, it is probably because the capture failed.
error_img = get_homedir() / 'website' / 'web' / 'static' / 'error_screenshot.png'
except Exception:
# The capture probably doesn't have a screenshot at all, no need to log that as a warning.
self.logger.debug(f'Unable to generate the screenshot thumbnail of {capture_uuid}: {e}.')
error_img = get_homedir() / 'website' / 'web' / 'static' / 'error_screenshot.png'
to_thumbnail = Image.open(error_img)
to_thumbnail.thumbnail(size)
to_thumbnail.save(to_return, 'png')
to_return.seek(0)
if for_datauri:
return base64.b64encode(to_return.getvalue()).decode()
else:
return to_return
def get_capture(self, capture_uuid: str, /) -> BytesIO:
'''Get all the files related to this capture.'''
return self._get_raw(capture_uuid)
def get_urls_rendered_page(self, capture_uuid: str, /) -> list[str]:
ct = self.get_crawled_tree(capture_uuid)
return sorted(set(ct.root_hartree.rendered_node.urls_in_rendered_page)
- set(ct.root_hartree.all_url_requests.keys()))
def compute_mmh3_shodan(self, favicon: bytes, /) -> str:
b64 = base64.encodebytes(favicon)
return str(mmh3.hash(b64))
def get_ressource(self, tree_uuid: str, /, urlnode_uuid: str, h: str | None) -> tuple[str, BytesIO, str] | None:
'''Get a specific resource from a URL node. If a hash s also given, we want an embeded resource'''
try:
url = self.get_urlnode_from_tree(tree_uuid, urlnode_uuid)
except IndexError:
# unable to find the uuid, the cache is probably in a weird state.
return None
except NoValidHarFile as e:
# something went poorly when rebuilding the tree (probably a recursive error)
self.logger.warning(e)
return None
if url.empty_response:
return None
if not h or h == url.body_hash:
# we want the body
return url.filename if url.filename else 'file.bin', BytesIO(url.body.getvalue()), url.mimetype
# We want an embedded ressource
if h not in url.resources_hashes:
return None
for mimetype, blobs in url.embedded_ressources.items():
for ressource_h, blob in blobs:
if ressource_h == h:
return 'embedded_ressource.bin', BytesIO(blob.getvalue()), mimetype
return None
def __misp_add_vt_to_URLObject(self, obj: MISPObject) -> MISPObject | None:
urls = obj.get_attributes_by_relation('url')
if not urls:
return None
url = urls[0]
self.vt.url_lookup(url.value)
report = self.vt.get_url_lookup(url.value)
if not report:
return None
vt_obj = MISPObject('virustotal-report', standalone=False)
vt_obj.add_attribute('first-submission', value=datetime.fromtimestamp(report['attributes']['first_submission_date']), disable_correlation=True)
vt_obj.add_attribute('last-submission', value=datetime.fromtimestamp(report['attributes']['last_submission_date']), disable_correlation=True)
vt_obj.add_attribute('permalink', value=f"https://www.virustotal.com/gui/url/{report['id']}/detection", disable_correlation=True)
obj.add_reference(vt_obj, 'analysed-with')
return vt_obj
def __misp_add_urlscan_to_event(self, capture_uuid: str, visibility: str) -> MISPAttribute | None:
if cache := self.capture_cache(capture_uuid):
response = self.urlscan.url_submit(cache, visibility)
if 'result' in response: