-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_readme.py
282 lines (225 loc) · 11.2 KB
/
generate_readme.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
#!/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"""
# DoH 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/doh-tunnel-dataset)](https://github.com/Korving-F/doh-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 DoH Tunnel](#file-transfer-over-doh-tunnel)
* [C2 over DoH Tunnel](#c2-over-doh-tunnel)
* [License](#license)
## Introduction
This repository documents a DNS tunneling over DoH 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 [email protected]: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) / [DNSTT](https://www.bamsoftware.com/software/dnstt/)
Used DoH Server/Client: [DNS-over-HTTPS](https://github.com/m13253/dns-over-https) with [NGINX](http://hg.nginx.org/nginx/) as a TLS termination proxy.
## Consume Datasets
Collected data within this repository comes in a variety of formats:
* .log - Flatfiles containing query logs as produced by the DoH Server and Webserver.
* .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 ports:
* UDP 53 - DNS traffic from DoH proxy to tunnel server
* TCP 443 - DoH HTTPS traffic from client to DoH server
* TCP 8053 - HTTP after TLS termination
Two additional directories can be found:
* suricata - Contains [Suricata](https://suricata.io/) logs produced from the pcap.</br> See also `suricata-logging.bash` and `suricata.yaml`.
* ja3 - Exported JA3 signatures using a [Wireshark JA3 plugin](https://github.com/fullylegit/ja3).</br> See also the following commands and/or the two `ja3` scripts placed in the root of this repository.
```bash
tshark -r filename.pcap -Y tls.handshake.type==1 -T fields -e ja3.hash
tshark -r filename.pcap -Y tls.handshake.type==2 -T fields -e ja3s.hash
```
## Architecture
![](images/doh_tunnel.drawio.png)
> **Fig 1:** DNS Tunnel over DoH high-level overview. Encoded/encrypted DNS queries establish a communications channel over HTTPS.
</br>
</br>
![](images/doh_tunnel_simulated.drawio.png)
> **Fig 2:** Overview on how the DNS Tunnels over DoH 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 DoH Tunnel
{file_transfer}
### C2 over DoH 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 = ['DOH SERVER', 'AUTOMATION LEVEL', 'DNS RECORD TYPE', 'LINK', 'DATA LINK']
#dnscat_rows = []
dns2tcp_cols = ['DOH SERVER', 'AUTOMATION LEVEL', 'DNS RECORD TYPE', 'COMPRESSION', 'PASSPHRASE', 'LINK', 'DATA LINK']
dns2tcp_rows = []
dnstt_cols = ['DOH SERVER', 'AUTOMATION LEVEL', 'TLS FINGERPRINT', 'LINK', 'DATA LINK']
dnstt_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 DOH Server was used
for component in meta_loaded['scenario']['components']:
if "dnsoverhttps" in component["name"].lower():
row['DOH SERVER'] = "DNS-over-HTTPS"
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 "dnstt" in component["name"]:
row['AUTOMATION LEVEL'] = "Partly Manual"
row['TLS FINGERPRINT'] = meta_loaded["variables"]["utls"]
dnstt_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['DOH SERVER'], d['DNS RECORD TYPE']))
#dnscat_rows_sorted = sorted(dnscat_rows, key=lambda d: (d['DOH SERVER'], d['DNS RECORD TYPE']))
dns2tt_rows_sorted = sorted(dnstt_rows, key=lambda d: (d['DOH SERVER'], d['TLS FINGERPRINT']))
tables = []
tables.append(gen_table("DNS2TCP", dns2tcp_cols, dns2tcp_rows_sorted))
#tables.append(gen_table("DNSCAT", dnscat_cols, dnscat_rows_sorted))
tables.append(gen_table("DNSTT", dnstt_cols, dns2tt_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 = ['DOH SERVER', 'AUTOMATION LEVEL','DNS RECORD TYPE', 'ENCODING', 'PASSPHRASE', 'LINK', 'DATA LINK']
iodine_rows = []
#dnscat_cols = ['DOH SERVER', 'AUTOMATION LEVEL', 'DNS RECORD TYPE', 'LINK', 'DATA LINK']
#dnscat_rows = []
dns2tcp_cols = ['DOH SERVER', 'AUTOMATION LEVEL', 'DNS RECORD TYPE', 'COMPRESSION', 'PASSPHRASE', 'LINK', 'DATA LINK']
dns2tcp_rows = []
dnstt_cols = ['DOH SERVER', 'AUTOMATION LEVEL', 'TLS FINGERPRINT', 'LINK', 'DATA LINK']
dnstt_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 DOH Server was used
for component in meta_loaded['scenario']['components']:
if "dnsoverhttps" in component["name"].lower():
row['DOH SERVER'] = "DNS-over-HTTPS"
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
if "dnstt" in component["name"]:
row['AUTOMATION LEVEL'] = "Fully Automated"
row['TLS FINGERPRINT'] = meta_loaded["variables"]["utls"]
dnstt_rows.append(row)
break
iodine_rows_sorted = sorted(iodine_rows, key=lambda d: (d['DOH SERVER'], d['DNS RECORD TYPE']))
dns2tcp_rows_sorted = sorted(dns2tcp_rows, key=lambda d: (d['DOH SERVER'], d['DNS RECORD TYPE']))
#dnscat_rows_sorted = sorted(dnscat_rows, key=lambda d: (d['DOH SERVER'], d['DNS RECORD TYPE']))
dns2tt_rows_sorted = sorted(dnstt_rows, key=lambda d: (d['DOH SERVER'], d['TLS FINGERPRINT']))
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))
tables.append(gen_table("DNSTT", dnstt_cols, dns2tt_rows_sorted))
return "\n".join(tables)
if __name__ == '__main__':
# FIRST GENERATE REPORT ON FILE TRANSFER DATASETS
ft = gen_file_transfer('doh_tunnel_file_transfer')
# SECONDLY GENERATE REPORT ON C2 DATASET
c2 = gen_c2('doh_tunnel_c2')
# RENDER README AND WRITE TO DISK
with open("README.md",'w') as f:
f.write(readme(ft, c2))