forked from knownsec/ksubdomain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recv.go
158 lines (154 loc) · 4.05 KB
/
recv.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
package core
import (
"bufio"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"github.com/panda843/ksubdomain/gologger"
"os"
"strconv"
"strings"
"sync/atomic"
"time"
)
func Recv(device string, options *Options, flagID uint16, retryChan chan RetryStruct, domainChan chan string) {
var (
snapshotLen int32 = 1024
promiscuous bool = false
timeout time.Duration = -1 * time.Second
)
windowWith := GetWindowWith()
if options.Silent {
windowWith = 0
}
handle, _ := pcap.OpenLive(device, snapshotLen, promiscuous, timeout)
err := handle.SetBPFFilter("udp and src port 53")
if err != nil {
gologger.Fatalf("SetBPFFilter Faild:%s\n", err.Error())
}
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
defer handle.Close()
var udp layers.UDP
var dns layers.DNS
var eth layers.Ethernet
var ipv4 layers.IPv4
var ipv6 layers.IPv6
var subNextData []string
if options.SubNameFileName == "" {
subNextData = GetDefaultSubNextData()
} else {
if !FileExists(options.SubNameFileName) {
gologger.Fatalf("三级域名文件:%s 不存在\n", options.SubNameFileName)
}
rs, err := LinesInFile(options.SubNameFileName)
if err != nil {
gologger.Fatalf("读取三级域名文件失败:%s\n", err.Error())
}
if len(rs) == 0 {
gologger.Fatalf("三级域名文件内容为空\n")
}
subNextData = rs
}
parser := gopacket.NewDecodingLayerParser(
layers.LayerTypeEthernet, ð, &ipv4, &ipv6, &udp, &dns)
var isWrite bool = false
var isttl bool = options.TTL
var isSummary bool = options.Summary
if options.Output != "" {
isWrite = true
}
var foutput *os.File
if isWrite {
foutput, err = os.OpenFile(options.Output, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0664)
if err != nil {
gologger.Errorf("写入结果文件失败:%s\n", err.Error())
}
}
for {
packet, err := packetSource.NextPacket()
if err != nil {
continue
}
var decoded []gopacket.LayerType
err = parser.DecodeLayers(packet.Data(), &decoded)
if err != nil {
continue
}
if !dns.QR {
continue
}
if dns.ID/100 == flagID {
if options.CheckOrigin {
if !IsContain(options.Resolvers, ipv4.SrcIP.String()) {
continue
}
}
atomic.AddUint64(&RecvIndex, 1)
udp, _ := packet.Layer(layers.LayerTypeUDP).(*layers.UDP)
index := GenerateMapIndex(dns.ID%100, uint16(udp.DstPort))
data, err := LocalStauts.SearchFromIndexAndDelete(uint32(index))
if err == nil {
// 处理多级域名
if dns.ANCount > 0 && data.v.DomainLevel < options.DomainLevel {
running := true
if options.SkipWildCard {
if IsWildCard(data.v.Domain) {
running = false
}
}
if running {
for _, sub := range subNextData {
subdomain := sub + "." + data.v.Domain
retryChan <- RetryStruct{Domain: subdomain, Dns: data.v.Dns, SrcPort: 0, FlagId: 0, DomainLevel: data.v.DomainLevel + 1}
}
}
}
if LocalStack.Len() <= 50000 {
LocalStack.Push(uint32(index))
}
}
if dns.ANCount > 0 {
atomic.AddUint64(&SuccessIndex, 1)
if len(dns.Questions) == 0 {
continue
}
data := RecvResult{Subdomain: string(dns.Questions[0].Name)}
data.Answers = dns.Answers
domainChan <- data.Subdomain
msg := data.Subdomain + " => "
if !options.Silent {
for _, v := range data.Answers {
msg += v.String()
if isttl {
msg += " ttl:" + strconv.Itoa(int(v.TTL))
}
msg += " => "
}
}
msg = strings.Trim(msg, " => ")
ff := windowWith - len(msg) - 1
if !options.Silent {
if windowWith > 0 && ff > 0 {
gologger.Silentf("\r%s% *s\n", msg, ff, "")
} else {
gologger.Silentf("\r%s\n", msg)
}
} else {
gologger.Silentf("%s\n", msg)
}
if isSummary {
AsnResults = append(AsnResults, data)
}
if isWrite {
w := bufio.NewWriter(foutput)
_, err = w.WriteString(msg + "\n")
if err != nil {
gologger.Errorf("写入结果文件失败.Err:%s\n", err.Error())
}
w.Flush()
}
}
PrintStatus()
}
}
}