-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_report.py
269 lines (215 loc) · 10.1 KB
/
generate_report.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
#!/bin/env python3
from pprint import pprint
import yaml
from pathlib import Path
TABLE = """
| First Header | Second Header |
| ------------- | ------------- |
| Content Cell | Content Cell |
| Content Cell | Content Cell |
"""
def readme(file_transfer, c2):
README=f"""
# DNS Tunneling Dataset
[![License MIT](https://img.shields.io/badge/license-MIT-blue)](https://en.wikipedia.org/wiki/MIT_License)
[![GitHub release (latest by date)](https://img.shields.io/github/v/release/Korving-F/dns-tunnel-dataset)](https://github.com/Korving-F/dns-tunnel-dataset)
## Table of Contents
* [Introduction](#introduction)
* [MITRE ATT&CK](#mitre-attck)
* [Scenario](#scenario)
* [Consume Datasets](#consume-datasets)
* [Architecture](#architecture)
* [Detection Rules](#detection-rules)
* [Datasets](#datasets)
* [File transfer over DNS Tunnel](#file-transfer-over-dns-tunnel)
* [C2 over DNS Tunnel](#c2-over-dns-tunnel)
* [License](#license)
## Introduction
This repository documents a DNS tunneling scenario written in [DACA](https://github.com/Korving-F/DACA/) configuration language and the generated datasets it creates.
Samples can be used for detection tuning or for educational purposes.
This dataset was created as part of Master thesis work at [TalTech](https://taltech.ee/).
To reproduce the generated datasets, follow these instructions:
```bash
# 1. Install Vagrant and VirtualBox
# 2. Install Vagrant modules
vagrant plugin install vagrant-vbguest
vagrant plugin install vagrant-scp
# 3A Start and stop the VMs of a single dataset, data collection will follow automatically.
cd directory/with/Vagrantfile
vagrant up
vagrant halt
# 3B Alternatively generate and run and recreate all datasets in the scenario.
pip3 install pipenv
git clone git@github.com:Korving-F/DACA.git
cd DACA
pipenv install
python3 daca.py run -d data/ --path /path/to/scenario_file.yaml
```
## MITRE ATT&CK
* [C2 Application Layer Protocol: DNS - T1071.004](https://attack.mitre.org/techniques/T1071/004/)
* [C2 Protocol Tunneling - T1572](https://attack.mitre.org/techniques/T1572/)
* [Exfiltration Over Alternative Protocol - T1048](https://attack.mitre.org/techniques/T1048/)
## Scenario
Used DNS Tunneling software: [IODINE](https://github.com/yarrick/iodine) / [DNS2TCP](https://github.com/alex-sector/dns2tcp) / [DNSCAT](https://github.com/iagox86/dnscat2)
Used DNS Servers: [BIND 9](https://www.isc.org/bind/) / [CoreDNS](https://coredns.io/) / [Dnsmasq](https://thekelleys.org.uk/dnsmasq/doc.html) / [PowerDNS](https://www.powerdns.com/)
## Consume Datasets
Collected data within this repository comes in a variety of formats:
* .log - Flatfiles containing query logs as produced by the DNS Server.
* .json - Same flatfiles but then relayed by Filebeat. This allows for post-hoc ingestion into an elasticsearch cluster.
* .cast - [asciinema](https://asciinema.org/) recordings of attacker's perspective. Replay by issuing: `asciinema play *.cast`.
* .pcap - Standard packet capture looking at traffic on port 53.
## Architecture
![](images/dns_tunnel.drawio.png)
> **Fig 1:** DNS Tunnel high-level overview. Encoded/encrypted DNS queries establish a communications channel.
</br>
</br>
![](images/dns_tunnel_simulated.drawio.png)
> **Fig 2:** Overview on how the DNS Tunnels are simulated and allow for C2 / data transfers.
</br>
</br>
![](images/dns_tunnel_devops.drawio.png)
> **Fig 3:** Runthrough of the VM Creation, Provisioning, Data Generation and Acquisition process using IaC / DevOps tooling.
</br>
## Datasets
### File transfer over DNS Tunnel
{file_transfer}
### C2 over DNS Tunnel
{c2}
## License
> DACA is licensed under the [MIT](#) license.
> Copyright © 2022, Frank Korving
"""
return README
def gen_table(title, columns, rows):
HEADER = f"#### {title}\n"
COLUMNS = "| "
for c in columns:
COLUMNS += f"{c} | "
COLUMNS += "\n"
for c in columns:
COLUMNS += f"| ------------- "
COLUMNS += "|\n"
ROWS = ""
for row in rows:
ROWS += "| "
for c in columns:
ROWS += f"{row[c]} | "
ROWS += "\n"
return HEADER + COLUMNS + ROWS + "\n"
def gen_c2(path):
files = Path(path).glob('*')
data_dirs = [i for i in files if i.is_dir() and i.name != 'scenario']
dnscat_cols = ['DNS SERVER', 'AUTOMATION LEVEL', 'DNS RECORD TYPE', 'LINK', 'DATA LINK']
dnscat_rows = []
dns2tcp_cols = ['DNS SERVER', 'AUTOMATION LEVEL', 'DNS RECORD TYPE', 'COMPRESSION', 'PASSPHRASE', 'LINK', 'DATA LINK']
dns2tcp_rows = []
for d in data_dirs:
row = {}
with open(f"{d}/.metadata", 'r') as f:
meta = f.read()
meta_loaded = yaml.safe_load(meta)
# Add link to directory
row['LINK'] = f"[Scenario files]({d})"
# Add link to data file
row['DATA LINK'] = f"[Data files]({d}/{d.name}_full_dataset.tar.gz)"
# Specify which DNS Server was used
for component in meta_loaded['scenario']['components']:
if "bind9" in component["name"].lower():
row['DNS SERVER'] = "BIND9"
break
elif "dnsmasq" in component["name"].lower():
row['DNS SERVER'] = "DNSMASQ"
break
elif "coredns" in component["name"].lower():
row['DNS SERVER'] = "COREDNS"
break
elif "powerdns" in component["name"].lower():
row['DNS SERVER'] = "POWERDNS"
break
for component in meta_loaded['scenario']['components']:
if "dns2tcp" in component["name"]:
row['AUTOMATION LEVEL'] = "Partly Manual"
row['DNS RECORD TYPE'] = meta_loaded["variables"]["record_type"].upper()
row['COMPRESSION'] = "YES" if meta_loaded["variables"]["compression"] == "-c" else "NO"
row['PASSPHRASE'] = meta_loaded["variables"]["passphrase"]
dns2tcp_rows.append(row)
break
if "dnscat" in component["name"]:
row['AUTOMATION LEVEL'] = "Partly Manual"
row['DNS RECORD TYPE'] = "TXT,CNAME,MX"
dnscat_rows.append(row)
break
dns2tcp_rows_sorted = sorted(dns2tcp_rows, key=lambda d: (d['DNS SERVER'], d['DNS RECORD TYPE']))
dnscat_rows_sorted = sorted(dnscat_rows, key=lambda d: (d['DNS SERVER'], d['DNS RECORD TYPE']))
tables = []
tables.append(gen_table("DNS2TCP", dns2tcp_cols, dns2tcp_rows_sorted))
tables.append(gen_table("DNSCAT", dnscat_cols, dnscat_rows_sorted))
return "\n".join(tables)
def gen_file_transfer(path):
files = Path(path).glob('*')
data_dirs = [i for i in files if i.is_dir() and i.name != 'scenario']
iodine_cols = ['DNS SERVER', 'AUTOMATION LEVEL','DNS RECORD TYPE', 'ENCODING', 'PASSPHRASE', 'LINK', 'DATA LINK']
iodine_rows = []
dnscat_cols = ['DNS SERVER', 'AUTOMATION LEVEL', 'DNS RECORD TYPE', 'LINK', 'DATA LINK']
dnscat_rows = []
dns2tcp_cols = ['DNS SERVER', 'AUTOMATION LEVEL', 'DNS RECORD TYPE', 'COMPRESSION', 'PASSPHRASE', 'LINK', 'DATA LINK']
dns2tcp_rows = []
for d in data_dirs:
row = {}
with open(f"{d}/.metadata", 'r') as f:
meta = f.read()
meta_loaded = yaml.safe_load(meta)
# Add link to directory
row['LINK'] = f"[Scenario files]({d})"
# Add link to data file
row['DATA LINK'] = f"[Data files]({d}/{d.name}_full_dataset.tar.gz)"
# Specify which DNS Server was used
for component in meta_loaded['scenario']['components']:
if "bind9" in component["name"].lower():
row['DNS SERVER'] = "BIND9"
break
elif "dnsmasq" in component["name"].lower():
row['DNS SERVER'] = "DNSMASQ"
break
elif "coredns" in component["name"].lower():
row['DNS SERVER'] = "COREDNS"
break
elif "powerdns" in component["name"].lower():
row['DNS SERVER'] = "POWERDNS"
break
for component in meta_loaded['scenario']['components']:
if "iodine" in component["name"]:
row['AUTOMATION LEVEL'] = "Fully Automated"
row['DNS RECORD TYPE'] = meta_loaded["variables"]["record_type"].upper()
row['ENCODING'] = meta_loaded["variables"]["encoding"].upper()
row['PASSPHRASE'] = meta_loaded["variables"]["passphrase"]
iodine_rows.append(row)
break
if "dns2tcp" in component["name"]:
row['AUTOMATION LEVEL'] = "Fully Automated"
row['DNS RECORD TYPE'] = meta_loaded["variables"]["record_type"].upper()
row['COMPRESSION'] = "YES" if meta_loaded["variables"]["compression"] == "-c" else "NO"
row['PASSPHRASE'] = meta_loaded["variables"]["passphrase"]
dns2tcp_rows.append(row)
break
if "dnscat" in component["name"]:
row['AUTOMATION LEVEL'] = "Partly Manual"
row['DNS RECORD TYPE'] = "TXT,CNAME,MX"
dnscat_rows.append(row)
break
iodine_rows_sorted = sorted(iodine_rows, key=lambda d: (d['DNS SERVER'], d['DNS RECORD TYPE']))
dns2tcp_rows_sorted = sorted(dns2tcp_rows, key=lambda d: (d['DNS SERVER'], d['DNS RECORD TYPE']))
dnscat_rows_sorted = sorted(dnscat_rows, key=lambda d: (d['DNS SERVER'], d['DNS RECORD TYPE']))
tables = []
tables.append(gen_table("IODINE", iodine_cols, iodine_rows_sorted))
tables.append(gen_table("DNS2TCP", dns2tcp_cols, dns2tcp_rows_sorted))
tables.append(gen_table("DNSCAT", dnscat_cols, dnscat_rows_sorted))
return "\n".join(tables)
if __name__ == '__main__':
# FIRST GENERATE REPORT ON FILE TRANSFER DATASETS
ft = gen_file_transfer('dns_tunnel_file_transfer')
# SECONDLY GENERATE REPORT ON C2 DATASET
c2 = gen_c2('dns_tunnel_c2')
# RENDER README AND WRITE TO DISK
with open("README.md",'w') as f:
f.write(readme(ft, c2))