Skip to content

Commit

Permalink
Replacing the unsafe library
Browse files Browse the repository at this point in the history
  • Loading branch information
j3ssie committed Oct 6, 2023
1 parent 14929cd commit 85ddff6
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 462 deletions.
5 changes: 0 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,5 @@ build:
run:
$(GO) $(GOFLAGS) run *.go

update:
rm -rf $(GOPATH)/src/github.com/j3ssie/metabigor/modules/static/ip2asn-combined.tsv.gz
wget -q https://iptoasn.com/data/ip2asn-combined.tsv.gz -O $(GOPATH)/src/github.com/j3ssie/metabigor/modules/static/ip2asn-combined.tsv.gz
echo "Done."

test:
$(GO) $(GOFLAGS) test ./... -v%
90 changes: 35 additions & 55 deletions cmd/ipc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,94 +2,73 @@ package cmd

import (
"fmt"

"github.com/j3ssie/metabigor/core"
"github.com/j3ssie/metabigor/modules"
jsoniter "github.com/json-iterator/go"
asnmap "github.com/projectdiscovery/asnmap/libs"
"github.com/spf13/cobra"
"inet.af/netaddr"
"os"
"sort"
"strings"
)

func init() {
var netCmd = &cobra.Command{
Use: "ipc",
Short: "Summary about IP list (powered by @thebl4ckturtle)",
Short: "Summary about IP list",
Long: core.DESC,
RunE: runIPC,
}
RootCmd.AddCommand(netCmd)
}

var ASNClient *asnmap.Client

func runIPC(_ *cobra.Command, _ []string) error {
var err error
ASNMap, err = modules.GetAsnMap()
ASNClient, err := asnmap.NewClient()
if err != nil {
fmt.Fprintf(os.Stderr, "Error to generate asn info")
os.Exit(-1)
core.ErrorF("Unable to init asnmap client: %v", err)
return err
}

summary := map[string]*modules.ASInfo{}
groupByAsn := map[int][]*modules.ASInfo{}
asnCount := make(map[string]int)
asnGroupByCIDR := map[string]AsnSummaryByCIDR{}
asnSums := []AsnSummaryByCIDR{}

// do real stuff here
for _, job := range options.Inputs {
ip, err := netaddr.ParseIP(strings.TrimSpace(job))
for _, input := range options.Inputs {
item, err := ASNClient.GetData(input)
if err != nil {
continue
}
if asn := ASNMap.ASofIP(ip); asn.AS != 0 {
if result, ok := summary[asn.CIDR]; ok {
result.Amount++
continue
} else {
summary[asn.CIDR] = &modules.ASInfo{
Amount: 1,
Number: asn.AS,
CountryCode: ASNMap.ASCountry(asn.AS),
Description: ASNMap.ASName(asn.AS),
CIDR: asn.CIDR,
}
}

listOfCIDR, err := asnmap.GetCIDR(item)
if err != nil {
continue
}
}
for _, cidr := range listOfCIDR {
asnCount[cidr.String()]++

for _, result := range summary {
if _, ok := groupByAsn[result.Number]; ok {
groupByAsn[result.Number] = append(groupByAsn[result.Number], result)
} else {
groupByAsn[result.Number] = []*modules.ASInfo{}
groupByAsn[result.Number] = append(groupByAsn[result.Number], result)
asnGroupByCIDR[cidr.String()] = AsnSummaryByCIDR{
Number: item[0].ASN,
Description: item[0].Org,
CountryCode: item[0].Country,
}
}
}

// do summary here
var groupbyCIDR []AsnSummaryByCIDR
for asnNumber, asnInfos := range groupByAsn {
//fmt.Printf("AS: %d - %s\n", asnNumber, asnInfos[0].Description)
}

sort.Slice(asnInfos, func(i, j int) bool {
return asnInfos[i].Amount > asnInfos[j].Amount
for cidr, count := range asnCount {
asnInfo := asnGroupByCIDR[cidr]
asnSums = append(asnSums, AsnSummaryByCIDR{
CIDR: cidr,
Count: count,
Number: asnInfo.Number,
Description: asnInfo.Description,
CountryCode: asnInfo.CountryCode,
})

//asnSum.Count +=
for _, as := range asnInfos {
//fmt.Printf("\t%-16s\t%-4d IPs\n", as.CIDR, as.Amount)

var asnSum AsnSummaryByCIDR
asnSum.CIDR = as.CIDR
asnSum.Count = as.Amount
asnSum.Number = asnNumber
asnSum.CountryCode = as.CountryCode
asnSum.Description = asnInfos[0].Description
groupbyCIDR = append(groupbyCIDR, asnSum)
}
}

// print the output here
var contents []string
for _, asnSum := range groupbyCIDR {
for _, asnSum := range asnSums {
if options.JsonOutput {
if data, err := jsoniter.MarshalToString(asnSum); err == nil {
contents = append(contents, data)
Expand All @@ -100,6 +79,7 @@ func runIPC(_ *cobra.Command, _ []string) error {
data := fmt.Sprintf("%d - %s - %d", asnSum.Number, asnSum.CIDR, asnSum.Count)
contents = append(contents, data)
}

StoreData(contents, options)
return nil
}
Expand Down
95 changes: 36 additions & 59 deletions cmd/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package cmd

import (
"fmt"

"github.com/davecgh/go-spew/spew"
"github.com/j3ssie/metabigor/core"
"github.com/j3ssie/metabigor/modules"
jsoniter "github.com/json-iterator/go"
"github.com/panjf2000/ants"
"github.com/spf13/cast"
asnmap "github.com/projectdiscovery/asnmap/libs"
"github.com/spf13/cobra"
"github.com/thoas/go-funk"
"inet.af/netaddr"
"net"
"os"

"strings"
"sync"
)
Expand Down Expand Up @@ -46,8 +46,6 @@ func init() {
RootCmd.AddCommand(netdCmd)
}

var ASNMap modules.AsnMap

func runNet(cmd *cobra.Command, _ []string) error {
asn, _ := cmd.Flags().GetBool("asn")
org, _ := cmd.Flags().GetBool("org")
Expand All @@ -63,17 +61,6 @@ func runNet(cmd *cobra.Command, _ []string) error {
} else if domain {
options.Net.SearchType = "domain"
}
if options.Net.SearchType == "" {
fmt.Fprintf(os.Stderr, "You need to specify search type with one of these flag: --asn, --org or --ip")
os.Exit(-1)
}

var err error
ASNMap, err = modules.GetAsnMap()
if err != nil {
fmt.Fprintf(os.Stderr, "Error to generate asn info")
os.Exit(-1)
}

var wg sync.WaitGroup
p, _ := ants.NewPoolWithFunc(options.Concurrency, func(i interface{}) {
Expand All @@ -99,43 +86,9 @@ func runNet(cmd *cobra.Command, _ []string) error {

func runNetJob(input string, options core.Options) []string {
var data []string
var asnInfos []modules.ASInfo

if !options.Net.ExactMatch {
input = strings.ToLower(input)

}

switch options.Net.SearchType {
case "asn":
input = strings.ToLower(input)
if strings.Contains(input, "as") {
input = strings.ReplaceAll(input, "as", "")
}
asInfos := ASNMap.ASInfo(cast.ToInt(input))
if len(asInfos) > 0 {
asnInfos = append(asnInfos, asInfos...)
}
var asnInfos []ASInfo

case "org":
asnNums := ASNMap.ASDesc(input)
if len(asnNums) > 0 {
for _, asnNum := range asnNums {
asnInfos = append(asnInfos, ASNMap.ASInfo(asnNum)...)
}
}

case "ip":
asnInfos = append(asnInfos, searchByIP(input)...)

case "domain":
ips, err := net.LookupHost(input)
if err == nil {
for _, ip := range ips {
asnInfos = append(asnInfos, searchByIP(ip)...)
}
}
}
asnInfos = handleInput(input)

if len(asnInfos) == 0 {
core.ErrorF("No result found for: %s", input)
Expand All @@ -150,7 +103,7 @@ func runNetJob(input string, options core.Options) []string {
return data
}

func genOutput(asnInfo modules.ASInfo) string {
func genOutput(asnInfo ASInfo) string {
var line string
if options.JsonOutput {
if content, err := jsoniter.MarshalToString(asnInfo); err == nil {
Expand All @@ -166,16 +119,32 @@ func genOutput(asnInfo modules.ASInfo) string {
return line
}

func searchByIP(input string) []modules.ASInfo {
var asnInfo []modules.ASInfo
func handleInput(input string) (asnInfo []ASInfo) {
ASNClient, err := asnmap.NewClient()
if err != nil {
core.ErrorF("Unable to init asnmap client: %v", err)
return asnInfo
}
results, err := ASNClient.GetData(input)

if options.Debug {
spew.Dump(results)
}

ip, err := netaddr.ParseIP(input)
if err != nil {
core.ErrorF("No result found for: %v", err)
return asnInfo
}

if asn := ASNMap.ASofIP(ip); asn.AS != 0 {
return ASNMap.ASInfo(asn.AS)
listOfCIDR, err := asnmap.GetCIDR(results)
for _, cidr := range listOfCIDR {
info := ASInfo{
CIDR: cidr.String(),
Number: results[0].ASN,
Description: results[0].Org,
CountryCode: results[0].Country,
}
asnInfo = append(asnInfo, info)
}
return asnInfo
}
Expand Down Expand Up @@ -312,3 +281,11 @@ func StoreData(data []string, options core.Options) {
core.InforF("Write output to: %v", options.Output)
}
}

type ASInfo struct {
Amount int
Number int
CountryCode string
Description string
CIDR string
}
27 changes: 13 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,49 @@ require (
github.com/json-iterator/go v1.1.12
github.com/mitchellh/go-homedir v1.1.0
github.com/panjf2000/ants v1.3.0
github.com/projectdiscovery/asnmap v1.0.5
github.com/projectdiscovery/mapcidr v1.1.10
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cast v1.5.1
github.com/spf13/cobra v1.7.0
github.com/thoas/go-funk v0.9.3
github.com/x-cray/logrus-prefixed-formatter v0.5.2
inet.af/netaddr v0.0.0-20230525184311-b8eac61e914a
)

require (
github.com/andybalholm/cascadia v1.3.2 // indirect
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/chromedp/cdproto v0.0.0-20230914224007-a15a36ccbc2e // indirect
github.com/chromedp/cdproto v0.0.0-20230802225258-3cf4e6d46a89 // indirect
github.com/chromedp/sysutil v1.0.0 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/gobwas/httphead v0.1.0 // indirect
github.com/gobwas/pool v0.2.1 // indirect
github.com/gobwas/ws v1.3.0 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/gobwas/ws v1.2.1 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/microcosm-cc/bluemonday v1.0.25 // indirect
github.com/miekg/dns v1.1.56 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/gomega v1.28.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/projectdiscovery/blackrock v0.0.1 // indirect
github.com/projectdiscovery/utils v0.0.57 // indirect
github.com/projectdiscovery/retryabledns v1.0.36 // indirect
github.com/projectdiscovery/utils v0.0.55 // indirect
github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d // indirect
github.com/spf13/pflag v1.0.5 // indirect
go4.org/intern v0.0.0-20230525184215-6c62f75575cb // indirect
go4.org/unsafe/assume-no-moving-gc v0.0.0-20230525183740-e7c30c78aeb2 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/term v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
golang.org/x/tools v0.13.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 85ddff6

Please sign in to comment.