forked from lcvvvv/kscan
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
204 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package fofa | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"io/ioutil" | ||
"kscan/lib/misc" | ||
"log" | ||
"net/http" | ||
"reflect" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
var logger = log.New(io.Discard, "", log.Ldate|log.Ltime) | ||
|
||
type Fofa struct { | ||
email, key string | ||
baseUrl, loginPath, searchPath string | ||
fieldList []string | ||
|
||
size int | ||
|
||
results []Result | ||
} | ||
|
||
type ResponseJson struct { | ||
Error bool `json:"error"` | ||
Mode string `json:"mode"` | ||
Page int `json:"page"` | ||
Query string `json:"query"` | ||
Results [][]string `json:"results"` | ||
Size int `json:"size"` | ||
} | ||
|
||
func New(email, key string) *Fofa { | ||
f := &Fofa{ | ||
email: email, | ||
key: key, | ||
baseUrl: "https://fofa.info", | ||
searchPath: "/api/v1/search/all", | ||
loginPath: "/api/v1/info/my", | ||
fieldList: []string{ | ||
"host", "title", "ip", "domain", "port", "country", "province", | ||
"city", "country_name", "header", "server", "protocol", "banner", | ||
"cert", "isp", "as_organization", | ||
}, | ||
} | ||
return f | ||
} | ||
|
||
func (f *Fofa) SetSize(i int) { | ||
f.size = i | ||
} | ||
|
||
func (f *Fofa) Search(keyword string) (int, []Result) { | ||
url := f.baseUrl + f.searchPath | ||
req, _ := http.NewRequest(http.MethodGet, url, nil) | ||
q := req.URL.Query() | ||
q.Add("qbase64", misc.Base64Encode(keyword)) | ||
q.Add("email", f.email) | ||
q.Add("key", f.key) | ||
q.Add("page", "1") | ||
q.Add("fields", strings.Join(f.fieldList, ",")) | ||
q.Add("size", strconv.Itoa(f.size)) | ||
q.Add("full", "false") | ||
req.URL.RawQuery = q.Encode() | ||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
logger.Println(err) | ||
} | ||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
logger.Println(err) | ||
} | ||
var responseJson ResponseJson | ||
if err = json.Unmarshal(body, &responseJson); err != nil { | ||
logger.Println(body, err) | ||
} | ||
r := f.makeResult(responseJson) | ||
f.results = append(f.results, r...) | ||
return responseJson.Size, r | ||
} | ||
|
||
func (f *Fofa) makeResult(responseJson ResponseJson) []Result { | ||
var results []Result | ||
var result Result | ||
|
||
for _, row := range responseJson.Results { | ||
m := reflect.ValueOf(&result).Elem() | ||
for index, f := range f.fieldList { | ||
f = misc.First2Upper(f) | ||
m.FieldByName(f).SetString(row[index]) | ||
} | ||
result.Fix() | ||
results = append(results, result) | ||
} | ||
return results | ||
} | ||
|
||
func (f *Fofa) Results() []Result { | ||
return f.results | ||
} | ||
|
||
func SetLogger(log *log.Logger) { | ||
logger = log | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package fofa | ||
|
||
import ( | ||
"fmt" | ||
"kscan/lib/misc" | ||
"reflect" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
type Result struct { | ||
Host, Title, Ip, Domain, Port, Country string | ||
Province, City, Country_name, Protocol string | ||
Server, Banner, Isp, As_organization string | ||
Header, Cert string | ||
} | ||
|
||
func (r *Result) Fix() { | ||
if r.Protocol != "" { | ||
r.Host = fmt.Sprintf("%s://%s:%s", r.Protocol, r.Ip, r.Port) | ||
} | ||
if regexp.MustCompile("http([s]?)://.*").MatchString(r.Host) == false && r.Protocol == "" { | ||
r.Host = "http://" + r.Host | ||
} | ||
if r.Title == "" && r.Protocol != "" { | ||
r.Title = strings.ToUpper(r.Protocol) | ||
} | ||
|
||
r.Title = misc.FixLine(r.Title) | ||
|
||
} | ||
|
||
func (r Result) Map() map[string]string { | ||
t := reflect.TypeOf(r) | ||
v := reflect.ValueOf(r) | ||
m := make(map[string]string) | ||
for k := 0; k < t.NumField(); k++ { | ||
key := t.Field(k).Name | ||
value := v.Field(k).String() | ||
m[key] = value | ||
} | ||
return m | ||
} |