-
Notifications
You must be signed in to change notification settings - Fork 0
/
irule1
359 lines (207 loc) · 8.73 KB
/
irule1
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
when RULE_INIT priority 500 {
# The table entry timeout for the DNS Protection by looking at the cookie and if matches the corect source IP
set static::timeout 200
# For a domain to be captured otherwise the DNS traffic will be send to the server!
set static::matchdomain "linuxsys.net"
# Testing adding server cookie(not done yet)
set static::add_srv_cookie false
array set static::db {0 ""}
set static::db(linuxsys.net,soa) {dns2.sr-tech.com}
set static::db(linuxsys.net,email) {hostmaster.sr-tech.com}
set static::db(linuxsys.net,serial) 2002032103
set static::db(linuxsys.net,refresh) 1800
set static::db(linuxsys.net,retry) 3600
set static::db(linuxsys.net,expire) 2400
set static::db(linuxsys.net,ttl) 3600
set static::db(linuxsys.net,NS) {dns.sr-tech.com dns3.sr-tech.com}
set static::db(linuxsys.net,MX) {{5 mail.sr-tech.com} {10 mail2.sr-tech.com}}
set static::db(linuxsys.net,A,) {69.3.36.102}
set static::db(linuxsys.net,A,www) {69.3.36.98}
set static::db(linuxsys.net,A,mail) {69.3.36.99}
set static::db(linuxsys.net,A,test) {69.3.36.100}
set static::db(linuxsys.net,CNAME,dns) {ns.sr-tech.com}
set static::db(linuxsys.net,CNAME,dns2) {dns2.sr-tech.com}
set static::db(linuxsys.net,PTR) ""
set static::db(linuxsys.net,HINFO,www) {{1.1ghz Intel P4} {SR Tech Linux}}
}
proc cvtname {dnsname} {
#convert from binary dns hostname format to readable string
set result ""
set chr 0
while {$chr < [string length $dnsname]} {
binary scan [string index $dnsname $chr] c* groupcnt
if {$chr > 0} {set dnsname [string replace $dnsname $chr $chr "."]}
incr chr +$groupcnt
incr chr
}
return [string range $dnsname 1 end]
}
proc cvthostnamedns {hostname} {
#convert from text hostname format to dns binary format
set result ""
set hostname [split $hostname "."]
foreach part $hostname {
append result "[format %c [string length $part]]$part"
}
return $result
}
proc cvtaddressdns {address} {
#convert from text dotted address format to dns binary format
set result ""
set address [split $address "."]
foreach octet $address {
append result "[format %c $octet]"
}
return $result
}
proc process_dns {host port pkt} {
#[binary format H* $reply]
# binary scan $pkt ScH* id1 flags1 cvt_data
binary scan $pkt H* cvt_data
set transid [string range $cvt_data 0 3]
set params [string range $cvt_data 4 7]
set quests [string range $cvt_data 8 11]
set answers [string range $cvt_data 12 15]
set authors [string range $cvt_data 16 19]
set addits [string range $cvt_data 20 23]
# The logic reply to DNS query with additional or no records
if {$addits equals "0000"}{
set queryname [string range $cvt_data 24 end-8]
set querytype [string range $cvt_data end-7 end-4]
set queryclass [string range $cvt_data end-3 end]
set additional ""
} else {
set queryname [string range $cvt_data 24 end-54]
set querytype [string range $cvt_data end-53 end-50]
set queryclass [string range $cvt_data end-49 end-46]
set additional [string range $cvt_data end-45 end]
set cookie_length [string range $cvt_data end-19 end-16]
set client_cookie [string range $cvt_data end-15 end]
}
# set cvt_data $transid$params$quests$answers$authors$addits$queryname$querytype$queryclass
switch $querytype {
0001 {set qtype A}
0002 {set qtype NS}
0005 {set qtype CNAME}
000c {set qtype PTR}
000d {set qtype HINFO}
000f {set qtype MX}
default {set qtype error}
}
if {$addits equals "0000"}{
set qname [split [call cvtname [string range $pkt 12 end-4]] "."]
} else {
# DNS Protection by looking at the cookie and if matches the corect source IP
if { [info exists client_cookie] } {
set cookie_ip [table lookup -subtable dns -notouch $client_cookie]
if { $cookie_ip != "" && $cookie_ip != [IP::client_addr] } {
drop
event disable all
} else {
table set -subtable dns $client_cookie [IP::client_addr] $static::timeout $static::timeout
}
}
set qname [split [call cvtname [string range $pkt 12 end-27]] "."]
}
set llen [llength $qname]
set qhost [string map {" " .} [lrange $qname 0 end-3]]
set qdomain [string map {" " .} [lrange $qname end-2 end-1]]
log local0. "Rec query from $host on port $port for host $qhost at $qdomain type $qtype"
if {$qdomain contains $static::matchdomain}{
set answer "0001"
set author "0000"
set params "8180" ; #this indicates a successful query
set class $queryclass
set type $querytype
#heres all the brains of the lookup
set error 0
if {! [info exists static::db($qdomain,soa)]} {
# domain doesnt exist, so resolve elsewhere if
set params "8181"; #this indicates an error
set error 1
set answer "0000"
log local0. "error: no domain entry found"
}
if {! [info exists static::db($qdomain,$qtype,$qhost)] && $qtype != "NS" && $qtype != "MX"} {
# host doesnt exist, so return an error reply in bits 13-16=0001
set params "8181"; #this indicates an error
set error 1
set answer "0000"
log local0. "error: no host entry found"
}
set resp_answer ""
set recindex 0
while {! $error} {
#return a response for each record found
switch $qtype {
A {set lookup [call cvtaddressdns [lindex $static::db($qdomain,$qtype,$qhost) $recindex]]}
MX {set mxprefix [lindex [lindex $static::db($qdomain,$qtype) $recindex] 0]
set lookup [call cvthostnamedns [lindex [lindex $static::db($qdomain,$qtype) $recindex] 1]]
}
NS {set lookup [call cvthostnamedns [lindex $static::db($qdomain,$qtype) $recindex]]}
CNAME {set lookup [call cvthostnamedns [lindex $static::db($qdomain,$qtype,$qhost) $recindex]]}
HINFO {set lookup [lindex $static::db($qdomain,$qtype,$qhost) $recindex]}
}
#test for no more records, then break to sent accumulated response
if {$lookup == ""} {break}
set len [string length $lookup]
binary scan $lookup H* hexlookup
switch $qtype {
A {set data $hexlookup}
NS {set data "${hexlookup}00"
incr len
}
CNAME {set data "${hexlookup}00"
incr len
}
MX {set data "[format %04x $mxprefix]${hexlookup}00"
incr len +3
}
HINFO {incr recindex
set data "[format %02x $len]${hexlookup}"
set lookup [lindex $static::db($qdomain,$qtype,$qhost) $recindex]
set len [string length $lookup]
binary scan $lookup H* hexlookup
append data "[format %02x $len]${hexlookup}00"
set data_length [string length $data]
set len [expr {$data_length} / 2 -1]
}
}
set len [format %04x $len]
set ttl [format %08x $static::db($qdomain,ttl)]
#puts "${type} ${class} ${ttl} $len $data"
append resp_answer "C00C${type}${class}${ttl}${len}${data}"
incr recindex
set answer [format %04x $recindex]
}
set resp_author ""
# set resp_addit ""
if {$addits equals "0000"}{
set response "[string range $cvt_data 0 3]$params[string range $cvt_data 8 11]$answer$author[string range $cvt_data 20 end]"
} else {
set response "[string range $cvt_data 0 3]$params[string range $cvt_data 8 11]$answer$author[string range $cvt_data 20 end-46]"
}
if {$static::add_srv_cookie} {
set server_cookie "01000000662fc06afb612bf88aefe7c4"
} else {
set resp_addit $additional
return [binary format H* ${response}${resp_answer}${resp_author}${resp_addit}]
}
} else {
return "bypass"
}
}
when CLIENT_ACCEPTED {
}
when CLIENT_DATA {
set ip_addr [IP::client_addr]
set port [UDP::server_port]
set pkt [UDP::payload]
# set qname [call cvtname [string range $pkt 12 end-4]]
set dns_response [call process_dns $ip_addr $port $pkt]
# UDP::drop
if {$dns_response equals "bypass"}{
}else {
UDP::respond $dns_response
}
}