Site Monitoring¶
The functions of site_monitoring.core, taken from their docstrings.
site_monitoring.core
¶
DesktopUserAgents
¶
Picks a desktop User-Agent without any network call (same random_nomobile as the library it replaces).
PageError
¶
Bases: Exception
The page could not be downloaded; the message says why in a few words.
start_scheduler()
¶
Launch multiple planning tasks in background:
- Fire `monitoring_check` every 15 minutes from Monday to Sunday
- Fire `update_site_monitoring_rdap_data` every 15 minute for the RDAP lookup
sites_initializing()
¶
Sites whose first monitoring check (monitoring_init) is not finished yet.
Returns:
| Type | Description |
|---|---|
|
Queryset of Site. |
monitoring_init(site)
¶
Init the monitoring for a specific website.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
site
|
Site Object. |
required |
Returns:
| Type | Description |
|---|---|
|
|
create_rdap_alert(site, alert_code, registrar_data=None, expiry_data=None, notify=True)
¶
Create RDAP alerts using the same system as monitoring alerts, and notify the change of registrar or of expiry date.
create_banner_alert(site, old_server, new_server, old_xpb, new_xpb)
¶
Create a banner-change alert and notify subscribers. Returns the Alert instance, or None if a duplicate was found within the last hour.
perform_site_rdap_lookup(site)
¶
Perform the RDAP lookup of a single site and keep its registrar, registration date and expiry date up to date.
The expiry date follows the rules of the legitimate domains (common.utils.expiry_check): the registry lowers it only with the same earlier date on two passes less than a day apart, and in between the stored date stays. Returns True when something about the site was updated.
update_site_monitoring_rdap_data()
¶
Update the registry data (registrar, registration and expiry dates) of all sites in Site Monitoring.
monitoring_check()
¶
Main monitoring function.
banner_key(banner)
¶
What counts in a Server / X-Powered-By header: the products and their major version. A patch of the same major version, a distribution in brackets ("nginx/1.28.3 (Ubuntu)") or the order of the products is not a change.
check_content(site, alert, ua)
¶
Monitor Website Content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
site
|
Site Object. |
required | |
alert
|
Alert Integer. |
required | |
ua
|
User Agent. |
required |
Returns:
| Type | Description |
|---|---|
int, int
|
alert, score |
content_fingerprint(response)
¶
TLSH hash of a page.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
Http response. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
The hash, or None when the page is too short or too uniform to fingerprint (tlsh.hash returns the string 'TNULL' for those, which is not a hash). |
tlsh_score(response, site, alert)
¶
Calculate TLSH Score.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
Http response. |
required | |
site
|
Site Object. |
required | |
alert
|
Alert Integer. |
required |
Returns:
| Type | Description |
|---|---|
int, int
|
alert, score |
known_networks(site, stored_ips)
¶
/16 networks a site was seen in during the last KNOWN_NETWORKS_MEMORY, plus those of the addresses stored on it. A rotating pool of addresses is a change of hosting only when it brings a network the site was never seen in.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
site
|
Site Object. |
required | |
stored_ips
|
IP addresses stored on the site (Iterable of Str or None). |
required |
remember_networks(site, networks)
¶
Record in Site.network_history the networks the site was seen in (dates refreshed once a day at most) and forget the ones not seen for KNOWN_NETWORKS_MEMORY.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
site
|
Site Object. |
required | |
networks
|
Networks the site was seen in (Iterable of ip_network). |
required |
check_ip(site, alert)
¶
Monitor IP Address.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
site
|
Site Object. |
required | |
alert
|
Alert Integer. |
required |
Returns:
| Type | Description |
|---|---|
int, str, str
|
alert, new_ip, new_ip_second |
check_mail(site, alert)
¶
Monitor Mail (MX Records + mail.example.com).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
site
|
Site Object. |
required | |
alert
|
Alert Integer. |
required |
Returns:
| Type | Description |
|---|---|
int
|
alert |
previous_alert(site, alert_type, alert_pk)
¶
Check if there was a previous Alert created for the same website in the last hour.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alert_pk
|
Bypass Alert ID. |
required | |
site
|
Site Object. |
required | |
alert_type
|
Alert type. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if there is a previous Alert. |
create_alert(alert, site, new_ip, new_ip_second, score)
¶
Create Alerts & Emails.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alert
|
Alert Integer. |
required | |
site
|
Site Object. |
required | |
new_ip
|
New IP. |
required | |
new_ip_second
|
New Second IP. |
required | |
score
|
TLSH Score. |
required |
Returns:
| Type | Description |
|---|---|
|
|
send_website_monitoring_notifications(site, alert_data)
¶
Sends notifications to Slack, Citadel, TheHive or Email based on Site Monitoring. Slack, Citadel and TheHive follow the instance settings (Connectors > Notifications); the email goes to the subscribers who ticked E-mail.
Args: site (Site): The object representing the site to monitor. alert_data (dict): The alert data associated with the site.
capture_for_version(page, version)
¶
Take the screenshot of a page and keep it with a version, replacing the one it had.
Returns:
| Type | Description |
|---|---|
|
True when a screenshot was kept. |
is_public(host)
¶
True when every address the host resolves to is a public one (Watcher must not be used to reach its network).
fetch_page(url)
¶
Download a page, following at most PAGE_MAX_REDIRECTS redirects, each one checked.
Returns:
| Type | Description |
|---|---|
|
(status code, body bytes or None when the answer is not text) |
Raises:
| Type | Description |
|---|---|
PageError
|
not http(s), private address, too many redirects, unreachable |
compile_ignore_patterns(source)
¶
The regular expressions of a page, one per line. ValueError (with the line) when one is not valid.
extract_page_text(body, css_selector='', ignore_patterns='')
¶
The text of a page, one line per block, without what changes by itself.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
body
|
HTML (bytes: the parser reads the charset) or text. |
required | |
css_selector
|
only this part of the page; the whole page when empty or when nothing matches. |
''
|
|
ignore_patterns
|
regular expressions, one per line, removed from the text. |
''
|
compile_patterns_quietly(source)
¶
The valid patterns only: an invalid one (edited in the admin) must not stop the checks.
compare_texts(old_text, new_text)
¶
How much the text changed.
Returns:
| Type | Description |
|---|---|
|
(ratio in %, lines added, lines removed). The ratio is the number of characters of the lines that were removed or added, over the characters of both texts: 0 for the same text, 100 for a completely different one. |
diff_rows(old_text, new_text, context=3)
¶
The difference between two versions for display: rows {'kind': 'same'|'add'|'remove'|'skip', 'text'}. Runs of
unchanged lines longer than twice context are cut to context lines on each side, with a 'skip' row that says how
many lines were left out.
summarize_change(added, removed)
¶
A few lines of a change, shortened: what the notification shows (the page text is never sent whole).
trim_versions(page)
¶
Keep the newest retention versions (at least two: a change needs a version to be compared with).
check_page(page)
¶
Check one page.
Returns:
| Type | Description |
|---|---|
|
the new PageVersion when the text changed (or the first one), None otherwise. |
due_pages(now=None)
¶
The active pages whose last check is older than their interval.
check_monitored_pages()
¶
Scheduled job: check the pages that are due.
notify_page_change(page, version, summary)
¶
Tell the channels that a page changed. A channel that fails does not stop the next ones; how each went is logged in one line (a warning when one failed).