forked from hanc00l/nemo_go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
domainscan.go
232 lines (223 loc) · 6.96 KB
/
domainscan.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
package workerapi
import (
"encoding/json"
"fmt"
"github.com/hanc00l/nemo_go/pkg/comm"
"github.com/hanc00l/nemo_go/pkg/conf"
"github.com/hanc00l/nemo_go/pkg/logging"
"github.com/hanc00l/nemo_go/pkg/task/custom"
"github.com/hanc00l/nemo_go/pkg/task/domainscan"
"github.com/hanc00l/nemo_go/pkg/task/portscan"
"github.com/hanc00l/nemo_go/pkg/utils"
"net/netip"
"strings"
)
// DomainScan 域名任务
func DomainScan(taskId, mainTaskId, configJSON string) (result string, err error) {
var ok bool
if ok, result, err = CheckTaskStatus(taskId); !ok {
return result, err
}
config := domainscan.Config{}
if err = ParseConfig(configJSON, &config); err != nil {
logging.RuntimeLog.Error(err)
return FailedTask(err.Error()), err
}
resultDomainScan := doDomainScan(config)
// 如果有端口扫描的选项
if config.IsIPPortScan || config.IsIPSubnetPortScan {
doPortScanByDomainscan(taskId, mainTaskId, config, resultDomainScan)
}
// 保存结果
resultArgs := comm.ScanResultArgs{
TaskID: taskId,
MainTaskId: mainTaskId,
DomainConfig: &config,
DomainResult: resultDomainScan.DomainResult,
}
err = comm.CallXClient("SaveScanResult", &resultArgs, &result)
if err != nil {
logging.RuntimeLog.Error(err)
return FailedTask(err.Error()), err
}
_, err = NewFingerprintTask(taskId, mainTaskId, nil, resultDomainScan, FingerprintTaskConfig{
IsHttpx: config.IsHttpx,
IsFingerprintHub: config.IsFingerprintHub,
IsIconHash: config.IsIconHash,
IsScreenshot: config.IsIconHash,
IsFingerprintx: config.IsFingerprintx,
WorkspaceId: config.WorkspaceId,
IsProxy: config.IsProxy,
})
if err != nil {
logging.RuntimeLog.Error(err)
return FailedTask(err.Error()), err
}
return SucceedTask(result), nil
}
// doDomainScan 域名收集任务
func doDomainScan(config domainscan.Config) (resultDomainScan *domainscan.Result) {
// 子域名枚举
if config.IsSubDomainFinder {
subdomain := domainscan.NewSubFinder(config)
subdomain.Do()
resultDomainScan = &subdomain.Result
}
// 子域名爆破
if config.IsSubDomainBrute {
massdns := domainscan.NewMassdns(config)
massdns.Do()
resultDomainScan = &massdns.Result
}
// Crawler
if config.IsCrawler {
crawler := domainscan.NewCrawler(config)
crawler.Do()
resultDomainScan = &crawler.Result
}
// 域名解析
resolve := domainscan.NewResolve(config)
if !config.IsSubDomainFinder && !config.IsSubDomainBrute && !config.IsCrawler {
// 对config中Target进行域名解析
resolve.Do()
resultDomainScan = &resolve.Result
} else {
// 对域名任务(子域名枚举或爆破)的结果进行域名解析
resolve.Result.DomainResult = resultDomainScan.DomainResult
resolve.Do()
}
// 去除结果中无域名解析A或CNAME记录的域名
checkDomainResolveResult(resultDomainScan)
// 对域名结果中同一个IP对应太多进行过滤
domainscan.FilterDomainHasTooMuchIP(resultDomainScan)
return resultDomainScan
}
// doPortScanByDomainscan 对IP进行端口扫描
func doPortScanByDomainscan(taskId, mainTaskId string, config domainscan.Config, resultDomainScan *domainscan.Result) {
ipResult, ipSubnetResult := getResultIPList(resultDomainScan)
if len(ipResult) == 0 {
return
}
portsConfig := conf.GlobalWorkerConfig().Portscan
ts := utils.NewTaskSlice()
ts.TaskMode = config.PortTaskMode
ts.Port = portsConfig.Port
ts.IpTarget = ipResult
if config.IsIPSubnetPortScan {
ts.IpTarget = ipSubnetResult
}
// worker任务执行,只能使用默认配置,无法读取server.yml中的配置
ts.IpSliceNumber = utils.DefaultIpSliceNumber
ts.PortSliceNumber = utils.DefaultPortSliceNumber
targets, ports := ts.DoIpSlice()
for _, t := range targets {
for _, p := range ports {
configPortScan := portscan.Config{
OrgId: config.OrgId,
Target: t,
Port: p,
Rate: portsConfig.Rate,
CmdBin: portsConfig.Cmdbin,
IsPing: portsConfig.IsPing,
Tech: portsConfig.Tech,
IsIpLocation: true,
IsHttpx: config.IsHttpx,
IsScreenshot: config.IsScreenshot,
IsFingerprintHub: config.IsFingerprintHub,
IsIconHash: config.IsIconHash,
IsFingerprintx: config.IsFingerprintx,
IsLoadOpenedPort: false, //只扫描当前结果
IsPortscan: true,
WorkspaceId: config.WorkspaceId,
IsProxy: config.IsProxy,
}
configPortScanJSON, _ := json.Marshal(configPortScan)
// 创建端口扫描任务
newTaskArgs := comm.NewTaskArgs{
TaskName: "portscan",
LastRunTaskId: taskId,
MainTaskID: mainTaskId,
ConfigJSON: string(configPortScanJSON),
}
var result string
err := comm.CallXClient("NewTask", &newTaskArgs, &result)
if err != nil {
logging.RuntimeLog.Error("Start Portscan task fail:", err)
logging.CLILog.Error("Start Portscan task fail:", err)
} else {
logging.CLILog.Info("Start Portscan task...")
}
}
}
}
// getResultIPList 提取域名收集结果的IP
func getResultIPList(resultDomainScan *domainscan.Result) (ipResult, ipSubnetResult []string) {
ips := make(map[string]struct{})
ipSubnets := make(map[string]struct{})
cdnCheck := custom.NewCDNCheck()
for domain, da := range resultDomainScan.DomainResult {
if isCdn, _, _ := cdnCheck.CheckCName(domain); isCdn {
continue
}
for _, dar := range da.DomainAttrs {
if dar.Tag == "A" {
ipArray := strings.Split(dar.Content, ".")
if len(ipArray) != 4 {
continue
}
if cdnCheck.CheckIP(dar.Content) || cdnCheck.CheckASN(dar.Content) {
continue
}
if _, ok := ips[dar.Content]; !ok {
ips[dar.Content] = struct{}{}
}
s := fmt.Sprintf("%s.%s.%s.0/24", ipArray[0], ipArray[1], ipArray[2])
if _, ok := ipSubnets[s]; !ok {
ipSubnets[s] = struct{}{}
}
} else if dar.Tag == "AAAA" {
_, err := netip.ParseAddr(dar.Tag)
if err != nil || !utils.CheckIPV6(dar.Content) {
continue
}
if _, ok := ips[dar.Content]; !ok {
ips[dar.Content] = struct{}{}
}
s := utils.GetIPV6SubnetC(dar.Content)
if _, ok := ipSubnets[s]; !ok {
ipSubnets[s] = struct{}{}
}
}
}
}
for k, _ := range ips {
ipResult = append(ipResult, k)
}
for k, _ := range ipSubnets {
ipSubnetResult = append(ipSubnetResult, k)
}
return
}
// checkDomainResolveResult 检查域名结果,去除没有解析记录的无效域名
func checkDomainResolveResult(resultDomainScan *domainscan.Result) {
var removedDomain []string
for domain, domainResult := range resultDomainScan.DomainResult {
if isHaveResolveRecord(&domainResult.DomainAttrs) == false {
removedDomain = append(removedDomain, domain)
}
}
for _, domain := range removedDomain {
delete(resultDomainScan.DomainResult, domain)
}
}
func isHaveResolveRecord(domainAttrs *[]domainscan.DomainAttrResult) bool {
if len(*domainAttrs) == 0 {
return false
}
for _, dar := range *domainAttrs {
if dar.Tag == "A" || dar.Tag == "AAAA" || dar.Tag == "CNAME" {
return true
}
}
return false
}