Skip to content

Commit

Permalink
added Dns server
Browse files Browse the repository at this point in the history
  • Loading branch information
vanshaj committed Dec 19, 2021
1 parent fc53970 commit d676c9a
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 0 deletions.
5 changes: 5 additions & 0 deletions blackhatgo/chapter4/fileServer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

func main(){

}
70 changes: 70 additions & 0 deletions blackhatgo/chapter5/dnsServer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"bufio"
"fmt"
"log"
"os"
"strings"

"github.com/miekg/dns"
)

var records map[string]string

func parse(filename string) (map[string]string, error) {
records := make(map[string]string)
fh, err := os.Open(filename)
if err != nil {
return records, err
}
defer fh.Close()
scanner := bufio.NewScanner(fh)
for scanner.Scan() {
line := scanner.Text()
parts := strings.SplitN(line, ",", 2)
if len(parts) < 2 {
return records, fmt.Errorf("%s is not a valid line", line)
}
records[parts[0]] = parts[1]
}
return records, scanner.Err()

}

func dnsRequestHandler(w dns.ResponseWriter, req *dns.Msg) {
msg := dns.Msg{}
msg.SetReply(req)
if len(req.Question) < 1 {
dns.HandleFailed(w, req)
return
}
name := strings.TrimSuffix(req.Question[0].Name, ".")
parts := strings.Split(name, ".")

if len(parts) > 1 {
name = strings.Join(parts[len(parts)-2:], ".")
}
match, ok := records[name]
fmt.Println(match)
if !ok {
dns.HandleFailed(w, req)
return
}
resp
if err != nil {
dns.HandleFailed(w, req)
return
}
if err := w.WriteMsg(resp); err != nil {
dns.HandleFailed(w, req)
return
}
}

func main() {
records, _ = parse("proxy.config")

dns.HandleFunc(".", dnsRequestHandler)
log.Fatal(dns.ListenAndServe(":53", "udp", nil))
}
9 changes: 9 additions & 0 deletions blackhatgo/chapter5/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/vanshaj/golang/blackhatgo/chapter5

go 1.17

require (
github.com/miekg/dns v1.1.43 // indirect
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect
golang.org/x/sys v0.0.0-20210303074136-134d130e1a04 // indirect
)
11 changes: 11 additions & 0 deletions blackhatgo/chapter5/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
github.com/miekg/dns v1.1.43 h1:JKfpVSCB84vrAmHzyrsxB5NAr5kLoMXZArPSw7Qlgyg=
github.com/miekg/dns v1.1.43/go.mod h1:+evo5L0630/F6ca/Z9+GAqzhjGyn8/c+TBaOyfEl0V4=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210303074136-134d130e1a04 h1:cEhElsAv9LUt9ZUUocxzWe05oFLVd+AA2nstydTeI8g=
golang.org/x/sys v0.0.0-20210303074136-134d130e1a04/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
2 changes: 2 additions & 0 deletions blackhatgo/chapter5/proxy.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
attacker1.com,127.0.0.1:2021
attacker2.com,127.0.0.1:2022
11 changes: 11 additions & 0 deletions blackhatgo/chapter5/wordlist
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
qa1
qa3
qa7
qa8
qa9
qa10
abc
def
xyz
hhh
mnh

0 comments on commit d676c9a

Please sign in to comment.