-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
223 lines (193 loc) · 9.49 KB
/
test.cpp
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
#define BOOST_TEST_DYN_LINK
//g++ test.cpp dns.cpp -o test -lboost_unit_test_framework -std=c++11 -lpcap
#include <cstdlib>
#include <cstdio>
#include "dns.hpp"
#define BOOST_TEST_MODULE DNSTests
#include <boost/test/unit_test.hpp>
#include <boost/test/included/unit_test.hpp>
#include <netinet/ip.h>
BOOST_AUTO_TEST_CASE( single_dns_query )
{
/*
* Dumped from wireshark:
* 0000 c4 01 01 00 00 01 00 00 00 00 00 00 02 77 70 02 .............wp.
* 0010 70 6c 00 00 01 00 01 pl.....
*/
const char * wp_pl_query = "\xc4\x01\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x02\x77\x70\x02\x70\x6c\x00\x00\x01\x00\x01";
auto section = parse_dns_section(wp_pl_query, 23);
BOOST_CHECK_EQUAL( section.transaction_ID , 0xc401 );
BOOST_CHECK_EQUAL( section.flags , 0x0100 );
BOOST_CHECK_EQUAL( section.questions , 0x1 );
BOOST_CHECK_EQUAL( section.answer_PRs , 0x0 );
BOOST_CHECK_EQUAL( section.authority_PRs , 0x0 );
BOOST_CHECK_EQUAL( section.additional_PRs , 0x0 );
BOOST_CHECK(strcmp(section.queries[0].name, "\x02wp\x02pl")==0);
BOOST_CHECK_EQUAL(section.queries[0].get_name(), std::string("wp.pl"));
BOOST_CHECK_EQUAL(section.queries[0].type, 0x1);
BOOST_CHECK_EQUAL(section.queries[0].class_, 0x1);
BOOST_CHECK(section.answers.empty());
}
BOOST_AUTO_TEST_CASE( dns_query_multiple_questions )
{
/*
* 3 questions
* wp.pl
* play.google.com
* notifications.google.com
*/
const char * dns_query = "\xc4\x01\x01\x00\x00\03\x00\x00\x00\x00\x00\x00\x02\x77\x70\x02\x70\x6c\x00\x00\x01\x00\x01"
"\x04\x70\x6c\x61\x79\x06\x67\x6f\x6f\x67\x6c\x65\03\x63\x6f\x6d\x00\x00\x01\x00\x01"//play.google.com
"\u000d\x6e\x6f\x74\x69\x66\x69\x63\x61\x74\x69\x6f\x6e\x73\06\x67\x6f\x6f\x67\x6c\x65\03\x63\x6f\x6d\x00\x00\x1c\x00\x01"; //notifications.google.com
auto section = parse_dns_section(dns_query, 23);
BOOST_CHECK_EQUAL( section.transaction_ID , 0xc401 );
BOOST_CHECK_EQUAL( section.flags , 0x0100 );
BOOST_CHECK_EQUAL( section.questions , 0x3 );
BOOST_CHECK_EQUAL( section.answer_PRs , 0x0 );
BOOST_CHECK_EQUAL( section.authority_PRs , 0x0 );
BOOST_CHECK_EQUAL( section.additional_PRs , 0x0 );
BOOST_CHECK(strcmp(section.queries[0].name, "\x02wp\x02pl")==0);
BOOST_CHECK_EQUAL(section.queries[0].get_name(), std::string("wp.pl"));
BOOST_CHECK_EQUAL(section.queries[0].type, 0x1);
BOOST_CHECK_EQUAL(section.queries[0].class_, 0x1);
BOOST_CHECK(strcmp(section.queries[1].name, "\04play\06google\03com")==0);
BOOST_CHECK_EQUAL(section.queries[1].get_name(), std::string("play.google.com"));
BOOST_CHECK_EQUAL(section.queries[1].type, 0x1);
BOOST_CHECK_EQUAL(section.queries[1].class_, 0x1);
BOOST_CHECK_EQUAL(strlen(section.queries[2].name), 25);
BOOST_CHECK(strcmp(section.queries[2].name, "\u000dnotifications\06google\03com")==0);
BOOST_CHECK_EQUAL(section.queries[2].get_name().substr(0,14), std::string("notifications.google.com").substr(0,14));
BOOST_CHECK_EQUAL(section.queries[2].type, 0x1c); //AAAA
BOOST_CHECK_EQUAL(section.queries[2].class_, 0x1);
BOOST_CHECK(section.answers.empty());
}
BOOST_AUTO_TEST_CASE( dns_query_to_bytes )
{
const char* wp_pl_query = "\x02\x77\x70\x02\x70\x6c\x00";
auto query = DNSQuery();
query.name=wp_pl_query;
auto bytes = query.get_name();
std::string real_url = "wp.pl";
BOOST_CHECK_EQUAL( bytes.size() , real_url.size() );
BOOST_CHECK_EQUAL( bytes , real_url );
const char* facebook_com="\x08\x66\x61\x63\x65\x62\x6f\x6f\x6b\03\x63\x6f\x6d\x00";
query.name=facebook_com;
bytes=query.get_name();
real_url = "facebook.com";
BOOST_CHECK_EQUAL( bytes.size() , real_url.size() );
BOOST_CHECK_EQUAL( bytes , real_url );
}
BOOST_AUTO_TEST_CASE( dns_query_response )
{
/*
* Dumped from wireshark:
* 0000 c4 01 01 00 00 01 00 00 00 00 00 00 02 77 70 02 .............wp.
* 0010 70 6c 00 00 01 00 01 pl.....
*/
const char * wp_pl_query = "\xc4\x01\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x02\x77\x70\x02\x70\x6c\x00\x00\x01\x00\x01";
auto dns_section_question = parse_dns_section(wp_pl_query, 23);
std::string spoofed_ip="192.168.1.111";
auto dns_section_response = construct_dns_section_response(dns_section_question,spoofed_ip);
BOOST_CHECK_EQUAL( dns_section_response.transaction_ID , 0xc401 );
BOOST_CHECK_EQUAL( dns_section_response.flags , 0x8180 );
BOOST_CHECK_EQUAL( dns_section_response.questions , 0x1 );
BOOST_CHECK_EQUAL( dns_section_response.answer_PRs , 0x1 );
BOOST_CHECK_EQUAL( dns_section_response.authority_PRs , 0x0 );
BOOST_CHECK_EQUAL( dns_section_response.additional_PRs , 0x0 );
BOOST_CHECK_EQUAL( dns_section_response.queries.size() , 1 );
BOOST_CHECK(strcmp(dns_section_response.queries[0].name, "\x02wp\02pl")==0);
BOOST_CHECK_EQUAL(dns_section_response.queries[0].type, 0x1);
BOOST_CHECK_EQUAL(dns_section_response.queries[0].class_, 0x1);
BOOST_CHECK_EQUAL(dns_section_response.answers.size(), 1);
BOOST_CHECK_EQUAL(dns_section_response.answers[0].name, 0xc00c);
BOOST_CHECK_EQUAL(dns_section_response.answers[0].type, 0x1);
BOOST_CHECK_EQUAL(dns_section_response.answers[0].class_, 0x1);
BOOST_CHECK_GT(dns_section_response.answers[0].time_to_live,50);
BOOST_CHECK_LT(dns_section_response.answers[0].time_to_live,100);
int expected_address;
inet_pton(AF_INET, spoofed_ip.c_str(), &expected_address);
BOOST_CHECK_EQUAL(dns_section_response.answers[0].address, expected_address);
}
BOOST_AUTO_TEST_CASE( dns_query_response_to_bytes )
{
/*
* Dumped from wireshark:
* 0000 c4 01 01 00 00 01 00 00 00 00 00 00 02 77 70 02 .............wp.
* 0010 70 6c 00 00 01 00 01 pl.....
*/
std::string wp_pl_query("\xc4\x01\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x02\x77\x70\x02\x70\x6c\x00\x00\x01\x00\x01" ,23);
auto dns_section_question = parse_dns_section(wp_pl_query.c_str(), wp_pl_query.size());
std::string spoofed_ip="192.168.1.111";
auto dns_section_response = construct_dns_section_response(dns_section_question,spoofed_ip);
/*
* 0000 c4 01 81 80 00 01 00 01 00 00 00 00 02 77 70 02 .............wp.
* 0010 70 6c 00 00 01 00 01 c0 0c 00 01 00 01 00 00 00 pl..............
* 0020 41 00 04 d4 4d 62 09 A...Mb.
*
* s="c4018180000100010000000002777002706c0000010001c00c00010001000000410004d44d6209"
* print(len(s)/2, "".join(["\\x"+s[i:i+2] for i in range(0,len(s),2)]))
*/
ssize_t expected_len=39;
std::string expected("\xc4\x01\x81\x80\x00\x01\x00\x01\x00\x00\x00\x00\x02\x77\x70\x02\x70\x6c\x00\x00\x01\x00\x01\xc0\x0c\x00\x01\x00\x01\x00\x00\x00\x41\x00\x04\xd4\x4d\x62\x09",
expected_len);
expected[expected_len-4]=(char) 192;
expected[expected_len-3]=(char) 168;
expected[expected_len-2]=(char) 1;
expected[expected_len-1]=(char) 111;
auto calcualted_bytes=dns_section_response.to_bytes();
BOOST_CHECK_EQUAL(calcualted_bytes.size(), expected.size());
BOOST_CHECK_EQUAL(calcualted_bytes, expected);
}
BOOST_AUTO_TEST_CASE( DNSQuery_to_bytes )
{
const char * name = "\x02wp\x02pl";
auto dns_query = DNSQuery();
dns_query.name=name;
dns_query.type=0x325;
dns_query.class_=0x0102;
auto raw_bytes = dns_query.to_bytes();
BOOST_CHECK_EQUAL(raw_bytes.size(), 7+2+2);
BOOST_CHECK(strncmp(raw_bytes.substr(0,7).c_str(),name,strlen(name)+1)==0);
BOOST_CHECK_EQUAL(raw_bytes[7], 0x03);
BOOST_CHECK_EQUAL(raw_bytes[8], 0x25);
BOOST_CHECK_EQUAL(raw_bytes[9], 0x01);
BOOST_CHECK_EQUAL(raw_bytes[10], 0x02);
}
BOOST_AUTO_TEST_CASE( DNSResponse_to_bytes )
{
auto dns_response = DNSResponse();
dns_response.name=0xc00c;
dns_response.type=0x325;
dns_response.class_=0x0102;
dns_response.time_to_live=0x41;
dns_response.data_length=4;
uint32_t expected_address;
inet_pton(AF_INET, "200.201.202.203", &expected_address);
dns_response.address=expected_address;
auto raw_bytes = dns_response.to_bytes();
BOOST_CHECK_EQUAL(raw_bytes.size(), 2+2+2+4+2+4);
BOOST_CHECK_EQUAL((uint8_t ) raw_bytes[0], 0xc0);
BOOST_CHECK_EQUAL((uint8_t ) raw_bytes[1], 0x0c);
BOOST_CHECK_EQUAL((uint8_t ) raw_bytes[2], 0x03);
BOOST_CHECK_EQUAL((uint8_t ) raw_bytes[3], 0x25);
BOOST_CHECK_EQUAL((uint8_t ) raw_bytes[4], 0x01);
BOOST_CHECK_EQUAL((uint8_t ) raw_bytes[5], 0x02);
BOOST_CHECK_EQUAL((uint8_t ) raw_bytes[6], 0x00);
BOOST_CHECK_EQUAL((uint8_t ) raw_bytes[7], 0x00);
BOOST_CHECK_EQUAL((uint8_t ) raw_bytes[8], 0x00);
BOOST_CHECK_EQUAL((uint8_t ) raw_bytes[9], 0x41);
BOOST_CHECK_EQUAL((uint8_t ) raw_bytes[10], 0x00);
BOOST_CHECK_EQUAL((uint8_t ) raw_bytes[11], 0x04);
BOOST_CHECK_EQUAL((uint8_t ) raw_bytes[12], 200);
BOOST_CHECK_EQUAL((uint8_t ) raw_bytes[13], 201);
BOOST_CHECK_EQUAL((uint8_t ) raw_bytes[14], 202);
BOOST_CHECK_EQUAL((uint8_t ) raw_bytes[15], 203);
}
BOOST_AUTO_TEST_CASE( test_checksum_calculation )
{
// taken from the wikipedia example https://en.wikipedia.org/wiki/IPv4_header_checksum
const char * ip_hdr="\x45\x00\x00\x73\x00\x00\x40\x00\x40\x11\xb8\x61\xc0\xa8\x00\x01\xc0\xa8\x00\xc7";
BOOST_CHECK_EQUAL(calculate_ipv4_checksum((iphdr*) ip_hdr), 0xB861);
const char * ip_hdr2="\x45\x00\x00\x73\x00\x00\x40\x00\x40\x11\x00\x00\xc0\xa8\x00\x01\xc0\xa8\x00\xc7";
BOOST_CHECK_EQUAL(calculate_ipv4_checksum((iphdr*) ip_hdr2), 0xB861);
}