-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from emiliacebrat/master
Update tia_example.py Thanks @emiliacebrat !
- Loading branch information
Showing
4 changed files
with
147 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,23 +6,16 @@ This repository contains sample code for threat intelligence providers who provi | |
|
||
threat-intel-api is an HTTP/Websocket service that allows threat intelligence (TI) providers to retrieve telemetry data generated from the malicious domain names they provide to Quad9 via their threat intelligence feeds. | ||
|
||
Contact Quad9 at [email protected] if you are a threat intelligence provider and need a key. | ||
Contact Quad9 at [email protected] if you are a threat intelligence provider and need a key. | ||
|
||
Clients will access the api via a websocket. | ||
Clients need the url of the websocket (https://tiapi.quad9.net) and an authorization token. | ||
Clients will access the api via a websocket. If a client has multiple threat lists they will be given a separate token for each list. | ||
|
||
If a client has multiple threat lists they will be given a separate token for each list. | ||
|
||
# Starting a Session | ||
|
||
Clients initiate their session by making an HTTP GET request to the service. There is only one endpoint: "/". In this initial request, the client is expected to provide a "bearer" token in the `Authorization` header, like so: | ||
## Requirements | ||
Clients need: | ||
- the url of the websocket (https://tiapi.quad9.net) | ||
- an authorization token which can be requested at [email protected] | ||
|
||
```http | ||
HTTP/``1.1 GET / | ||
Authorization: Token <YOUR_TOKEN> | ||
``` | ||
|
||
When the client supplies a valid, active token, their connection will be "upgraded" to a Websocket. | ||
|
||
## API Business Rules | ||
|
||
|
@@ -31,12 +24,23 @@ threat-intel-api was written with the following business rules in place: | |
- Clients must acknowledge each message they receive; | ||
- Clients must acknowledge messages in the order they are received within 5 seconds. (This can be adjusted with a config setting) | ||
- Multiple clients connecting with the same authorization token is allowed and increases throughput; | ||
- If a client fails to acknowledge the messages they receive, in-order, the API will terminate the connection. | ||
- If a client acknowledges your messages and needs a reset, contact [email protected] and we can reset it to 0. | ||
- There is currently no way to retrieve data by date. We are working on it. | ||
|
||
|
||
# Starting a Session | ||
|
||
Clients initiate their session by making an HTTP GET request to the service. There is only one endpoint: "/". In this initial request, the client is expected to provide a "bearer" token in the `Authorization` header, like so: | ||
|
||
```http | ||
HTTP/``1.1 GET / | ||
Authorization: Token <YOUR_TOKEN> | ||
``` | ||
|
||
If a client fails to acknowledge the messages they receive, in-order, the API will terminate the connection. | ||
When the client supplies a valid, active token, their connection will be "upgraded" to a Websocket. | ||
|
||
If you ack your messages and need a reset please contact [email protected] and we can reset you to 0. | ||
|
||
There is currently no way for you to retrieve by date. We are working on it. | ||
|
||
# Simple Curl Example | ||
|
||
|
@@ -57,7 +61,7 @@ Dload Upload Total Spent Left Speed | |
|
||
Sends to a file called output.txt | ||
|
||
Exmple single entry: | ||
Example single entry: | ||
|
||
```json | ||
{ | ||
|
@@ -79,38 +83,68 @@ Exmple single entry: | |
- `region` = region that the query originated from, can be blank | ||
- `country` = two character country code (https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) that the query originated from, can be blank | ||
|
||
Note: We use MaxMind for geographic lookups but we snap locations to the center of the closest city that is above the minimum population - it never moves out of a region (typically a country or nation) but you will not get granular geographic information for small cities. This is to ensure end user privacy. | ||
*_Note: We use MaxMind for geographic lookups but we snap locations to the center of the closest city that is above the minimum population - it never moves out of a region (typically a country or nation) but you will not get granular geographic information for small cities. This is to ensure end user privacy._* | ||
|
||
# Python Example | ||
|
||
The example script `tia_example.py` was written against Python version 3.6. | ||
Convenient way to setup a 3.6 environment on Linux. https://linuxize.com/post/how-to-install-python-3-on-centos-7/ | ||
The example script `tia_example.py` was written against Python version 3.8. | ||
Convenient way to set up a Python and virtual environment on Linux: https://linuxize.com/post/how-to-install-python-3-on-centos-7/ | ||
|
||
|
||
## Overview | ||
|
||
This measures download speed: | ||
The repository contains three files: | ||
1. `requirements.txt` containing all required Python libraries | ||
|
||
``` | ||
aiofile==3.8.1 | ||
environs==9.5.0 | ||
pyyaml==6.0 | ||
websockets==10.3 | ||
``` | ||
|
||
2. `config.yaml.example` - config file | ||
``` | ||
ti_url: "wss://tiapi.quad9.net" | ||
auth_token: "" | ||
data_file: /output/stream.json | ||
log_file: /output/tiapi.log | ||
verbose: true | ||
nolog: false | ||
noack: false | ||
``` | ||
- Replace `auth_token` with token received from [email protected] | ||
- `data_file` - file to write the telemetry data | ||
- `log_file` - file to write logs generated by script | ||
- `verbose` - if set to true, print retrieved data on command line, default: true | ||
- `nolog` - no logging enabled, default: false | ||
- `noack` - do not acknowledge retrieved data, default: false | ||
|
||
3. `tiapi.py` | ||
|
||
|
||
## Usage | ||
|
||
```shell | ||
(my_project_venv) [exampleuser@commandline]\$ ./tia_example.py --auth_token <YOUR_TOKEN> | ||
10000 8140.593486210115/sec | ||
20000 9139.962665341003/sec | ||
30000 9979.32015085066/sec | ||
40000 10866.962743213264/sec | ||
50000 10731.540519453485/sec | ||
(my_project_venv) [exampleuser@commandline]\$ python3 ./tiapi.py --config path-to-config-file | ||
``` | ||
|
||
To see the data being retrieved: | ||
*_Replace `path-to-config-file`._* | ||
|
||
### If verbose==true: | ||
With `verbose` set to true, data will be printed to command line: | ||
|
||
```shell | ||
(my_project_venv) [exampleuser@commandline]\$ ./tia_example.py --auth_token <YOUR_TOKEN> --verbose | ||
(my_project_venv) [exampleuser@commandline]\$ ./tia_example.py --config path-to-config-file | ||
{'id': '191960005', 'qname': 'blockeddomain.example.com', 'qtype': 'A', 'timestamp': '2018-12-11T03:15:47.038932839Z', 'city': 'San Jose', 'region': 'CA', 'country': 'US'} | ||
{'id': '191961005', 'qname': 'blockeddomain.example.com', 'qtype': 'A', 'timestamp': '2018-12-11T03:15:47.051392978Z', 'city': 'San Jose', 'region': 'CA', 'country': 'US'} | ||
{'id': '191962005', 'qname': 'blockeddomain.example.com', 'qtype': 'A', 'timestamp': '2018-12-11T03:15:47.0605273Z', 'city': 'San Jose', 'region': 'CA', 'country': 'US'} | ||
{'id': '191963005', 'qname': 'blockeddomain.example.com', 'qtype': 'A', 'timestamp': '2018-12-11T03:15:47.102118471Z', 'city': 'San Jose', 'region': 'CA', 'country': 'US'} | ||
``` | ||
|
||
# Usage | ||
|
||
Clients must acknowledge that messages have been received. | ||
|
||
In the example above the script will send back to the server JSON objects of `{"id":'191960005'}` to indicate that is has successfully received and processed the JSON structure. | ||
In the example above the script will send back to the server JSON objects of `{"id":'191960005'}` to indicate that it has successfully received and processed the JSON structure. | ||
|
||
If the websocket is terminated before the ack is received by the server the message will be resent on the next connection. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
ti_url: "wss://tiapi.quad9.net" | ||
auth_token: "" | ||
data_file: /output/stream.json | ||
log_file: /output/tiapi.log | ||
verbose: true | ||
nolog: false | ||
noack: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
websockets==10.3 | ||
pyyaml==6.0 | ||
aiofile==3.8.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters