Skip to content

Commit fd5b50f

Browse files
committed
v0.1.14 search add uniqByIP
1 parent b802242 commit fd5b50f

File tree

8 files changed

+94
-8
lines changed

8 files changed

+94
-8
lines changed

HISTORY.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## v0.1.14 search add uniqByIP
2+
3+
- search add uniqByIP argument, which can be used to filter data as group by ip. ```./fofa --fixUrl --size 1000 --fields host --uniqByIP 'host="edu.cn"'```
4+
15
## v0.1.13 dump add inFile
26

37
- dump add inFile/json argument, which can be used to dump data from queries file. ```./fofa dump -inFile a.txt -outFile out.json -j```

README.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@ The official library doesn't has unittests, 之前官方的库功能不全,
2020

2121
```
2222
~ go install github.com/LubyRuffy/gofofa/cmd/fofa@latest
23-
~ $GOPATH/bin/fofa
24-
~ $GOPATH/bin/fofa search port=80
25-
WARN[0000] auth failed
26-
2022/08/07 14:38:29 auth failed: '[-700] Account Invalid', make sure key is valid
27-
~ FOFA_CLIENT_URL='https://fofa.info/[email protected]&key=xxx' $GOPATH/bin/fofa search port=80
23+
~ fofa
24+
~ FOFA_CLIENT_URL='https://fofa.info/[email protected]&key=xxx' fofa search port=80
2825
```
2926

3027
### Search
@@ -102,6 +99,12 @@ redis://152.136.145.87:6379
10299
./fofa -fields "host" -fixUrl 'app="Aspera-Faspex"' | nuclei -t http/cves/2022/CVE-2022-47986.yaml
103100
```
104101

102+
- uniq by ip
103+
104+
```shell
105+
./fofa --fixUrl --size 1000 --fields host --uniqByIP 'host="edu.cn"'
106+
```
107+
105108
### Stats
106109

107110
- stats subcommand

client_test.go

+8
Large diffs are not rendered by default.

cmd/fofa/cmd/random.go

+24-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"errors"
55
"fmt"
6+
"github.com/LubyRuffy/gofofa"
67
"github.com/LubyRuffy/gofofa/pkg/outformats"
78
"github.com/sirupsen/logrus"
89
"github.com/urfave/cli/v2"
@@ -48,6 +49,24 @@ var randomCmd = &cli.Command{
4849
Usage: "ms",
4950
Destination: &sleepMS,
5051
},
52+
&cli.BoolFlag{
53+
Name: "fixUrl",
54+
Value: false,
55+
Usage: "each host fix as url, like 1.1.1.1,80 will change to http://1.1.1.1",
56+
Destination: &fixUrl,
57+
},
58+
&cli.StringFlag{
59+
Name: "urlPrefix",
60+
Value: "http://",
61+
Usage: "prefix of url, default is http://, can be redis:// and so on ",
62+
Destination: &urlPrefix,
63+
},
64+
&cli.BoolFlag{
65+
Name: "full",
66+
Value: false,
67+
Usage: "search result for over a year",
68+
Destination: &full,
69+
},
5170
},
5271
Action: randomAction,
5372
}
@@ -104,7 +123,11 @@ func randomAction(ctx *cli.Context) error {
104123
newQuery = newQuery + ` && before="` + ts + `"`
105124
}
106125

107-
res, err := fofaCli.HostSearch(newQuery, 1, fields)
126+
res, err := fofaCli.HostSearch(newQuery, 1, fields, gofofa.SearchOptions{
127+
FixUrl: fixUrl,
128+
UrlPrefix: urlPrefix,
129+
Full: full,
130+
})
108131
if err != nil {
109132
return err
110133
}

cmd/fofa/cmd/search.go

+8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var (
2424
full bool // search result for over a year
2525
batchSize int // amount of data contained in each batch, only for dump
2626
json bool // out format as json for short
27+
uniqByIP bool // group by ip
2728
)
2829

2930
// search subcommand
@@ -82,6 +83,12 @@ var searchCmd = &cli.Command{
8283
Usage: "search result for over a year",
8384
Destination: &full,
8485
},
86+
&cli.BoolFlag{
87+
Name: "uniqByIP",
88+
Value: false,
89+
Usage: "uniq by ip",
90+
Destination: &uniqByIP,
91+
},
8592
},
8693
Action: SearchAction,
8794
}
@@ -157,6 +164,7 @@ func SearchAction(ctx *cli.Context) error {
157164
FixUrl: fixUrl,
158165
UrlPrefix: urlPrefix,
159166
Full: full,
167+
UniqByIP: uniqByIP,
160168
})
161169
if err != nil {
162170
return err

cmd/fofa/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
var (
12-
version = "v0.1.13"
12+
version = "v0.1.15"
1313
commit = "none"
1414
date = "unknown"
1515
builtBy = "unknown" // goreleaser fill

host.go

+28-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type SearchOptions struct {
4343
FixUrl bool // each host fix as url, like 1.1.1.1,80 will change to http://1.1.1.1, https://1.1.1.1:8443 will no change
4444
UrlPrefix string // default is http://
4545
Full bool // search result for over a year
46+
UniqByIP bool // uniq by ip
4647
}
4748

4849
// fixHostToUrl 替换host为url
@@ -70,8 +71,10 @@ func fixHostToUrl(res [][]string, fields []string, hostIndex int, urlPrefix stri
7071
// options for search
7172
func (c *Client) HostSearch(query string, size int, fields []string, options ...SearchOptions) (res [][]string, err error) {
7273
var full bool
74+
var uniqByIP bool
7375
if len(options) > 0 {
7476
full = options[0].Full
77+
uniqByIP = options[0].UniqByIP
7578
}
7679

7780
freeSize := c.freeSize()
@@ -113,6 +116,24 @@ func (c *Client) HostSearch(query string, size int, fields []string, options ...
113116
fields = []string{"ip", "port"}
114117
}
115118

119+
// 确认fields包含ip
120+
ipIndex := -1
121+
uniqIPMap := make(map[string]bool)
122+
if uniqByIP {
123+
ipExists := false
124+
for index, f := range fields {
125+
if f == "ip" {
126+
ipIndex = index
127+
ipExists = true
128+
break
129+
}
130+
}
131+
if !ipExists {
132+
fields = append(fields, "ip")
133+
ipIndex = len(fields) - 1
134+
}
135+
}
136+
116137
// 分页取数据
117138
for {
118139
if ctx := c.GetContext(); ctx != nil {
@@ -157,6 +178,12 @@ func (c *Client) HostSearch(query string, size int, fields []string, options ...
157178
for _, vStr := range vStrSlice {
158179
newSlice = append(newSlice, vStr.(string))
159180
}
181+
if uniqByIP {
182+
if _, ok := uniqIPMap[newSlice[ipIndex]]; ok {
183+
continue
184+
}
185+
uniqIPMap[newSlice[ipIndex]] = true
186+
}
160187
results = append(results, newSlice)
161188
} else if vStr, ok := result.(string); ok {
162189
results = append(results, []string{vStr})
@@ -178,7 +205,7 @@ func (c *Client) HostSearch(query string, size int, fields []string, options ...
178205
}
179206

180207
// 数据已经没有了
181-
if len(results) < perPage {
208+
if len(hr.Results.([]interface{})) < perPage {
182209
break
183210
}
184211

host_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,19 @@ func TestClient_HostSearch(t *testing.T) {
141141
})
142142
assert.Nil(t, err)
143143
assert.Equal(t, 10, len(res))
144+
145+
// 过滤UniqByIP
146+
res, err = cli.HostSearch("port=80", 500, []string{"ip", "port"}, SearchOptions{
147+
FixUrl: true,
148+
})
149+
assert.Nil(t, err)
150+
assert.Equal(t, 1000, len(res))
151+
res, err = cli.HostSearch("port=80", 500, []string{"ip", "port"}, SearchOptions{
152+
FixUrl: true,
153+
UniqByIP: true,
154+
})
155+
assert.Nil(t, err)
156+
assert.Equal(t, 986, len(res))
144157
}
145158

146159
func TestClient_HostSize(t *testing.T) {

0 commit comments

Comments
 (0)