-
Notifications
You must be signed in to change notification settings - Fork 923
/
Copy pathdnswasher.cc
278 lines (230 loc) · 7.1 KB
/
dnswasher.cc
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
/*
* This file is part of PowerDNS or dnsdist.
* Copyright -- PowerDNS.COM B.V. and its contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* In addition, for the avoidance of any doubt, permission is granted to
* link this program with OpenSSL and to (re)distribute the binaries
* produced as the result of such linking.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/** two modes:
anonymizing and stripping tcpdumps of irrelevant traffic, so operators can send non-privacy violating dumps
for analysis.
algorithm:
read a packet, check if it has the QR bit set.
If the question has the response bit set, obfuscate the destination IP address
otherwise, obfuscate the response IP address
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "namespaces.hh"
#include "statbag.hh"
StatBag S;
#ifdef HAVE_IPCIPHER
#include "dnspcap.hh"
#include "iputils.hh"
#include "ipcipher.hh"
#include <boost/program_options.hpp>
#include "base64.hh"
namespace po = boost::program_options;
po::variables_map g_vm;
class IPObfuscator
{
public:
virtual ~IPObfuscator() = default;
virtual uint32_t obf4(uint32_t orig)=0;
virtual struct in6_addr obf6(const struct in6_addr& orig)=0;
};
class IPSeqObfuscator : public IPObfuscator
{
public:
IPSeqObfuscator() : d_romap(d_ipmap), d_ro6map(d_ip6map), d_counter(0)
{
}
static std::unique_ptr<IPObfuscator> make()
{
return std::make_unique<IPSeqObfuscator>();
}
uint32_t obf4(uint32_t orig) override
{
if(d_romap.count(orig))
return d_ipmap[orig];
else {
return d_ipmap[orig]=d_counter++;
}
}
struct in6_addr obf6(const struct in6_addr& orig) override
{
uint32_t val;
if(d_ro6map.count(orig))
val=d_ip6map[orig];
else {
val=d_ip6map[orig]=d_counter++;
}
struct in6_addr ret;
val=htonl(val);
memset(&ret, 0, sizeof(ret));
memcpy(((char*)&ret)+12, &val, 4);
return ret;
}
private:
map<uint32_t, uint32_t> d_ipmap;
const decltype(d_ipmap)& d_romap;
struct cmp {
bool operator()(const struct in6_addr&a , const struct in6_addr&b) const
{
return memcmp(&a, &b, sizeof(a)) < 0;
}
};
// For IPv6 addresses
map<struct in6_addr, uint32_t, cmp> d_ip6map;
const decltype(d_ip6map)& d_ro6map;
// The counter that we'll convert to an IP address
uint32_t d_counter;
};
class IPCipherObfuscator : public IPObfuscator
{
public:
IPCipherObfuscator(const std::string& key, bool decrypt) : d_key(key), d_decrypt(decrypt)
{
if(d_key.size()!=16) {
throw std::runtime_error("IPCipher requires a 128 bit key");
}
}
static std::unique_ptr<IPObfuscator> make(const std::string& key, bool decrypt)
{
return std::make_unique<IPCipherObfuscator>(key, decrypt);
}
uint32_t obf4(uint32_t orig) override
{
ComboAddress ca;
ca.sin4.sin_family = AF_INET;
ca.sin4.sin_addr.s_addr = orig;
ca = d_decrypt ? decryptCA(ca, d_key) : encryptCA(ca, d_key);
return ca.sin4.sin_addr.s_addr;
}
struct in6_addr obf6(const struct in6_addr& orig) override
{
ComboAddress ca;
ca.sin4.sin_family = AF_INET6;
ca.sin6.sin6_addr = orig;
ca = d_decrypt ? decryptCA(ca, d_key) : encryptCA(ca, d_key);
return ca.sin6.sin6_addr;
}
private:
std::string d_key;
bool d_decrypt;
};
static void usage() {
cerr<<"Syntax: dnswasher INFILE1 [INFILE2..] OUTFILE"<<endl;
}
int main(int argc, char** argv)
try
{
po::options_description desc("Allowed options");
desc.add_options()
("help,h", "produce help message")
("version", "show version number")
("key,k", po::value<string>(), "base64 encoded 128 bit key for ipcipher")
("passphrase,p", po::value<string>(), "passphrase for ipcipher (will be used to derive key)")
("decrypt,d", "decrypt IP addresses with ipcipher");
po::options_description alloptions;
po::options_description hidden("hidden options");
hidden.add_options()
("infiles", po::value<vector<string>>(), "PCAP source file(s)")
("outfile", po::value<string>(), "outfile");
alloptions.add(desc).add(hidden);
po::positional_options_description p;
p.add("infiles", 1);
p.add("outfile", 1);
po::store(po::command_line_parser(argc, argv).options(alloptions).positional(p).run(), g_vm);
po::notify(g_vm);
if(g_vm.count("help")) {
usage();
cout<<desc<<endl;
exit(EXIT_SUCCESS);
}
if(g_vm.count("version")) {
cout<<"dnswasher "<<VERSION<<endl;
exit(EXIT_SUCCESS);
}
if(!g_vm.count("outfile")) {
cout<<"Missing outfile"<<endl;
usage();
exit(EXIT_FAILURE);
}
bool doDecrypt = g_vm.count("decrypt");
PcapPacketWriter pw(g_vm["outfile"].as<string>());
std::unique_ptr<IPObfuscator> ipo;
if(!g_vm.count("key") && !g_vm.count("passphrase"))
ipo = IPSeqObfuscator::make();
else if(g_vm.count("key") && !g_vm.count("passphrase")) {
string key;
if(B64Decode(g_vm["key"].as<string>(), key) < 0) {
cerr<<"Invalidly encoded base64 key provided"<<endl;
exit(EXIT_FAILURE);
}
ipo = IPCipherObfuscator::make(std::move(key), doDecrypt);
}
else if(!g_vm.count("key") && g_vm.count("passphrase")) {
string key = makeIPCipherKey(g_vm["passphrase"].as<string>());
ipo = IPCipherObfuscator::make(std::move(key), doDecrypt);
}
else {
cerr<<"Can't specify both 'key' and 'passphrase'"<<endl;
exit(EXIT_FAILURE);
}
for(const auto& inf : g_vm["infiles"].as<vector<string>>()) {
PcapPacketReader pr(inf);
pw.setPPR(pr);
while(pr.getUDPPacket()) {
if(ntohs(pr.d_udp->uh_dport)==53 || (ntohs(pr.d_udp->uh_sport)==53 && pr.d_len > sizeof(dnsheader))) {
dnsheader* dh=(dnsheader*)pr.d_payload;
if (pr.d_ip->ip_v == 4){
uint32_t *src=(uint32_t*)&pr.d_ip->ip_src;
uint32_t *dst=(uint32_t*)&pr.d_ip->ip_dst;
if(dh->qr)
*dst=ipo->obf4(*dst);
else
*src=ipo->obf4(*src);
pr.d_ip->ip_sum=0;
} else if (pr.d_ip->ip_v == 6) {
auto src=&pr.d_ip6->ip6_src;
auto dst=&pr.d_ip6->ip6_dst;
if(dh->qr)
*dst=ipo->obf6(*dst);
else
*src=ipo->obf6(*src);
// IPv6 checksum does not cover source/destination addresses
}
pw.write();
}
}
cerr<<"Saw "<<pr.d_correctpackets<<" correct packets, "<<pr.d_runts<<" runts, "<< pr.d_oversized<<" oversize, "<<
pr.d_nonetheripudp<<" unknown encaps"<<endl;
}
}
catch(std::exception& e)
{
cerr<<"Fatal: "<<e.what()<<endl;
}
#else
int main()
{
cerr<<"dnswasher requires ipcipher support, which is not available"<<endl;
exit(1);
}
#endif /* HAVE_IPCIPHER */