-
Notifications
You must be signed in to change notification settings - Fork 18
/
dnsproxy.go
338 lines (265 loc) · 8.53 KB
/
dnsproxy.go
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"math"
"net/http"
"strings"
"sync"
"github.com/miekg/dns"
"github.com/pkg/errors"
)
type DNSProxy struct {
Cache *Cache
CorrelationId string
Repo string
ApiClient *ApiClient
EgressPolicy string
AllowedEndpoints map[string][]Endpoint
WildCardEndpoints map[string][]Endpoint
ReverseIPLookup map[string]string
ReverseIPLookupMutex sync.RWMutex
Iptables *Firewall
}
type DNSResponse struct {
Status int `json:"Status"`
Tc bool `json:"TC"`
Rd bool `json:"RD"`
Ra bool `json:"RA"`
Ad bool `json:"AD"`
Cd bool `json:"CD"`
Question []Question `json:"Question"`
Answer []Answer `json:"Answer"`
}
type Question struct {
Name string `json:"name"`
Type int `json:"type"`
}
type Answer struct {
Name string `json:"name"`
Type int `json:"type"`
TTL int `json:"TTL"`
Data string `json:"data"`
}
const StepSecuritySinkHoleIPAddress = "54.185.253.63"
func (proxy *DNSProxy) getResponse(requestMsg *dns.Msg) (*dns.Msg, error) {
responseMsg := new(dns.Msg)
if len(requestMsg.Question) > 0 {
question := requestMsg.Question[0]
switch question.Qtype {
case dns.TypeA:
answer, err := proxy.processTypeA(&question, requestMsg)
if err != nil {
return responseMsg, err
}
responseMsg.Answer = append(responseMsg.Answer, *answer)
default:
answer, err := proxy.processOtherTypes(&question, requestMsg)
if err != nil {
return responseMsg, err
}
responseMsg.Answer = append(responseMsg.Answer, *answer)
}
}
return responseMsg, nil
}
func (proxy *DNSProxy) SetReverseIPLookup(domain, ipAddress string) {
proxy.ReverseIPLookupMutex.Lock()
proxy.ReverseIPLookup[ipAddress] = domain
proxy.ReverseIPLookupMutex.Unlock()
}
func (proxy *DNSProxy) GetReverseIPLookup(ipAddress string) string {
proxy.ReverseIPLookupMutex.RLock()
domain, found := proxy.ReverseIPLookup[ipAddress]
proxy.ReverseIPLookupMutex.RUnlock()
if found {
return domain
} else {
return ""
}
}
func (proxy *DNSProxy) processOtherTypes(q *dns.Question, requestMsg *dns.Msg) (*dns.RR, error) {
queryMsg := new(dns.Msg)
requestMsg.CopyTo(queryMsg)
queryMsg.Question = []dns.Question{*q}
return nil, fmt.Errorf("not found")
}
func (proxy *DNSProxy) isAllowedDomain(domain string) bool {
for domainName := range proxy.AllowedEndpoints {
if dns.Fqdn(domainName) == dns.Fqdn(domain) {
return true
}
}
return false
}
func (proxy *DNSProxy) ResolveDomain(domain string) (*Answer, error) {
url := fmt.Sprintf("https://dns.google/resolve?name=%s&type=a", domain)
fallbackUrl := fmt.Sprintf("https://cloudflare-dns.com/dns-query?name=%s&type=A", domain)
retryCounter := 0
var httpError error
var resp *http.Response
for retryCounter < 2 {
requestUrl := url
if retryCounter == 1 {
requestUrl = fallbackUrl
}
req, _ := http.NewRequest("GET", requestUrl, nil)
req.Header.Add("accept", "application/dns-json")
resp, httpError = proxy.ApiClient.Client.Do(req)
if httpError != nil {
retryCounter++
} else {
break
}
}
if httpError != nil {
return nil, fmt.Errorf("error in response from dns.google %v", httpError)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error in response from dns.google %v", err)
}
var dnsReponse DNSResponse
err = json.Unmarshal([]byte(body), &dnsReponse)
if err != nil {
return nil, fmt.Errorf("error in response from dns.google %v", err)
}
for _, answer := range dnsReponse.Answer {
if answer.Type == 1 {
if answer.TTL < 30 {
answer.TTL = 30 // less than 30 will cause too frequent DNS requests in audit scenario
}
return &answer, nil
}
}
return nil, fmt.Errorf("unable to resolve domain %s, status %d", domain, dnsReponse.Status)
}
func getDomainFromCloudAppFormat(domain string) string {
domainParts := strings.Split(domain, ".")
totalDots := len(domainParts) - 6
finalDomain := ""
for i := 0; i < totalDots; i++ {
finalDomain += domainParts[i] + "."
}
return finalDomain
}
func (proxy *DNSProxy) getIPByDomain(domain string) (string, error) {
domain = dns.Fqdn(domain)
cacheMsg, found := proxy.Cache.Get(domain)
if found {
return cacheMsg.Value.Data, nil
}
matchesAnyWildcard := false
wildcardPort := ""
if proxy.EgressPolicy == EgressPolicyBlock {
if strings.HasSuffix(domain, ".internal.") {
go WriteLog(fmt.Sprintf("unable to resolve internal domains: %s", domain))
return "", fmt.Errorf("cannot resolve internal domains")
}
if !proxy.isAllowedDomain(domain) {
matchesAnyWildcard, wildcardPort = proxy.matchAnyWildcard(domain)
if !matchesAnyWildcard {
go WriteLog(fmt.Sprintf("domain not allowed: %s", domain))
// call to api.snapcraft.io is made by snapd in GITHUB_RUNNER
// so if it's not present in allowed-domains, traffic to it will get blocked
// since it is called by default service, we don't need to add it to annotations
// call to docker.io is made by docker. It makes a DNS resolution call, but does not
// make a connection to it. This leads to an unnecessary annotation.
if !strings.Contains(domain, "api.snapcraft.io") && !strings.Contains(domain, "docker.io") {
go WriteAnnotation(fmt.Sprintf("StepSecurity Harden Runner: DNS resolution for domain %s was blocked. This domain is not in the list of allowed-endpoints.", domain))
}
// return an ip address, so calling process calls the ip address
// the call will be blocked by the firewall
proxy.Cache.Set(domain, &Answer{Name: domain, TTL: math.MaxInt32, Data: StepSecuritySinkHoleIPAddress}, false)
go proxy.ApiClient.sendDNSRecord(proxy.CorrelationId, proxy.Repo, domain, StepSecuritySinkHoleIPAddress)
return StepSecuritySinkHoleIPAddress, nil
}
}
}
answer, err := proxy.ResolveDomain(domain)
if err != nil {
go WriteLog(fmt.Sprintf("unable to resolve domain: %s err: %v", domain, err))
return "", fmt.Errorf("error in response from dns.google %v", err)
}
if matchesAnyWildcard {
if err := InsertAllowRule(proxy.Iptables, answer.Data, wildcardPort); err != nil {
WriteLog(fmt.Sprintf("Error setting firewall for wildcard domain %s: %v", domain, err))
}
}
proxy.Cache.Set(domain, answer, matchesAnyWildcard)
go WriteLog(fmt.Sprintf("domain resolved: %s, ip address: %s, TTL: %d", domain, answer.Data, answer.TTL))
go proxy.ApiClient.sendDNSRecord(proxy.CorrelationId, proxy.Repo, domain, answer.Data)
return answer.Data, nil
}
func (proxy *DNSProxy) matchAnyWildcard(domain string) (bool, string) {
for wildcard := range proxy.WildCardEndpoints {
if matchWildcardDomain(wildcard, domain) {
return true, fmt.Sprintf("%v", proxy.WildCardEndpoints[wildcard][0].port)
}
}
return false, ""
}
func (proxy *DNSProxy) processTypeA(q *dns.Question, requestMsg *dns.Msg) (*dns.RR, error) {
queryMsg := new(dns.Msg)
requestMsg.CopyTo(queryMsg)
queryMsg.Question = []dns.Question{*q}
domains := map[string]string{
"dns.google.": "8.8.8.8",
"cloudflare-dns.com.": "1.1.1.1",
}
if ip, ok := domains[q.Name]; ok {
rr, err := dns.NewRR(fmt.Sprintf("%s IN A %s", q.Name, ip))
if err != nil {
return nil, err
}
proxy.Cache.Set(q.Name, &Answer{Name: q.Name, TTL: math.MaxInt32, Data: ip}, false)
return &rr, nil
}
// Azure VM sometimes sends domain name with suffix of internal.cloudapp.net.
if strings.HasSuffix(q.Name, ".internal.cloudapp.net.") {
q.Name = getDomainFromCloudAppFormat(q.Name)
}
ip, err := proxy.getIPByDomain(q.Name)
if err != nil {
return nil, err
}
rr, err := dns.NewRR(fmt.Sprintf("%s IN A %s", q.Name, ip))
if err != nil {
return nil, err
}
proxy.SetReverseIPLookup(q.Name, ip)
return &rr, nil
}
func startDNSServer(dnsProxy *DNSProxy, server DNSServer, errc chan error) {
dns.HandleFunc(".", func(w dns.ResponseWriter, r *dns.Msg) {
switch r.Opcode {
case dns.OpcodeQuery:
m, err := dnsProxy.getResponse(r)
if err != nil {
m.SetReply(r)
w.WriteMsg(m)
return
}
m.SetReply(r)
w.WriteMsg(m)
}
})
err := server.ListenAndServe()
if err != nil {
errc <- errors.Wrap(err, fmt.Sprintf("failed to listen on %v", server))
}
}
func matchWildcardDomain(pattern, target string) bool {
// pattern: *.github.com
// target: h0x0er.github.com
result := false
splits := strings.Split(pattern, "*")
if splits[0] != "" {
result = strings.HasPrefix(target, splits[0]) && strings.HasSuffix(target, splits[1])
} else {
result = strings.HasSuffix(target, splits[1])
}
return result
}