BGP anomalies
BetaTo access Cloudflare Radar BGP Anomaly Detection results, you will first need to create an API token that includes a Account:Radar
permission. All the following examples should work with a free-tier Cloudflare account.
In the following example, we will query the BGP hijack events API for the most recent BGP origin hijacks originated by or affecting AS64512
(example ASN).
The result shows the most recent 10 BGP hijack events that affects AS64512
.
In the response we can learn about the following information about each event:
hijack_msg_count
: the number of potential BGP hijack messages observed from all peers.peer_asns
: the AS numbers of the route collector peers who observed the hijack messages.prefixes
: the affected prefixes.hijacker_asn
andvictim_asns
: the potential hijacker ASN and victim ASNs.confidence_score
: a quantitative score describing how confident the system is for this event being a hijack:- 1-3: low confidence.
- 4-7: medium confidence.
- 8-above: high confidence.
tags
: the evidence collected for the events. Eachtag
is also associated with a score that affects the overall confidence score:- a positive score indicates that the event is more likely to be a hijack.
- a negative score indicates that the event is less likely to be a hijack.
Users can further filter out low-confidence events by attaching a minConfidence=8
parameter, which will return only events with a confidence_score
of 8
or higher.
BGP route leak is another type of BGP anomalies that Cloudflare Radar detects. Currently, we focus on detecting specifically
the provider-customer-provider
type of route leak. You can learn more about our design and methodology in our blog post ↗.
In the following example, we will query the BGP route leak events API for the most recent BGP route leak events affecting AS64512
.
The result shows the most recent 10 BGP route leak events that affects AS64512
.
In the response we can learn about the following information about each event:
leak_asn
: the AS who potentially caused the leak.leak_seg
: the AS path segment observed and believed to be a leak.min_ts
andmax_ts
: the earliest and latest timestamps of the leak announcements.leak_count
: the total number of BGP route leak announcements observed.peer_count
: the number of route collector peers observed the leak.prefix_count
andorigin_count
: the number of prefixes and origin ASes affected by the leak.
In this example, we will show you how you can build a Cloudflare Workers app that sends out alerts for BGP hijacks relevant to a given ASN using webhooks (works for Google Hangouts, Discord, Telegram, etc) or email.
We will use Cloudflare Workers as the platform and use its Cron Triggers to periodically check for new alerts.
For the app, we would like it to do the following things:
- Fetch from Cloudflare API with a given API token.
- Check against Cloudflare KV to know what events are new.
- Construct messages for new hijacks and send out alerts via webhook triggers.
We will start with setting up a Cloudflare Worker app.
First, create a new Workers app in a local directory:
For setup, select the following options:
- For What would you like to start with?, choose
Hello World example
. - For Which template would you like to use?, choose
Hello World Worker
. - For Which language do you want to use?, choose
JavaScript
. - For Do you want to use git for version control?, choose
Yes
. - For Do you want to deploy your application?, choose
No
(we will be making some changes before deploying).
To start developing your Worker, cd
into your new project directory:
In your wrangler.toml
file, change the default checking frequency (once per hour) to what you like. Here is an example
of configuring the workers to run the script five minutes.
In this example, we will also need to use Cloudflare KV to save the latest checked event IDs which allows us to know what events are new. Once you have created a KV, you can head back to the wranglers.toml
file and add the following sections:
Start with the API fetching function.
The following apiFetch(env, paramsStr)
handles taking in a request parameters string, construct proper headers and
fetch from the Cloudflare API BGP hijacks endpoint.
The env
parameter is passed in from the caller, and we do not need to worry about construct it. The paramsStr
is a
string variable that holds the query parameters in a query URL.
Now in our main cron trigger function, we will need to construct the query parameters and call the API fetch function. The default cron trigger worker script is defined as the follows:
In our example, we use the env
variables to get the runtime variables like the TOKEN and ASN of interest, and Cloudflare
KV bindings. We do not use the controller
and ctx
variables in this example.
First, we will need to learn about what are the new events. We define new events as the events the app has not yet processed.
We use the Cloudflare KV bucket previously created and defined (HIJACKS_KV
) to save and retrieve the most recent
processed event ID.
The main loop that checks for the most recent events looks like this (some of the validation code is skipped):
Now that we have all the newly detected events saved in new_events
variable, we can then send out alerts:
The function send_alert
handles constructing alert message and sending out alerts using webhook. Here we demonstrate
an example plain-text message template using Google Hangouts webhook. Users can customize the message and the use of webhook based on their
platform of choice and needs.
Note that the webhook is considered secret and should be set to the environment via wrangler secret put WEBHOOK_URL
command.
The last step is to deploy the application with command npx wrangler deploy
and the app should be up and running on your Cloudflare account, and will be triggered to execute every five minutes.
If you have Email Routing enabled for your domain, you can also send email alerts directly from Workers. Refer to Send emails from Workers to learn more.
For this alert to work, you will need to configure the proper email bindings in the wrangler.toml
file.
Then, you can create an email-sending function to send alert emails to your configured destination address:
Refer to our API documentation for BGP route leaks and BGP hijacks for more information on these topics.