Skip to content

Commit

Permalink
Reverting to the previous library, as https://github.com/projectdisco…
Browse files Browse the repository at this point in the history
…very/asnmap now demands an API Key
  • Loading branch information
j3ssie committed Mar 2, 2024
1 parent e53fe3a commit 8eed25d
Show file tree
Hide file tree
Showing 19 changed files with 839 additions and 200 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ build:
zip -j dist/$(TARGET)-linux.zip dist/$(TARGET)
rm -rf ./dist/$(TARGET)

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."

run:
$(GO) $(GOFLAGS) run *.go

Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,18 @@ Metabigor is Intelligence tool, its goal is to do OSINT tasks and more but witho

# Installation

### Pre-built Binaries

You can download pre-built binaries for your platform from the [releases page](https://github.com/j3ssie/metabigor/releases). Choose the appropriate binary for your operating system and architecture, download it, and place it in your `PATH`.

### Building from Source

To install this project from source, you'll need Go installed on your system. If you haven't installed Go yet, you can follow the official installation instructions [here](https://golang.org/doc/install).

```shell
go install github.com/j3ssie/metabigor@latest
git clone https://github.com/j3ssie/metabigor.git
cd metabigor
go build
```

# Main features
Expand Down
3 changes: 1 addition & 2 deletions cmd/cert.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"fmt"
"strings"
"sync"

Expand Down Expand Up @@ -50,7 +49,7 @@ func runCert(_ *cobra.Command, _ []string) error {

func runCertSearch(input string, options core.Options) []string {
var data []string
core.BannerF(fmt.Sprintf("Search on %v for: ", "crt.sh"), input)
core.BannerF("Searching on crt.sh for: ", input)
result := modules.CrtSHOrg(input, options)
data = append(data, result...)
return data
Expand Down
101 changes: 61 additions & 40 deletions cmd/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ package cmd

import (
"fmt"
"net"
"os"
"strings"
"sync"

"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"
asnmap "github.com/projectdiscovery/asnmap/libs"
"github.com/spf13/cast"
"github.com/spf13/cobra"
"github.com/thoas/go-funk"

"strings"
"sync"
"inet.af/netaddr"
)

func init() {
Expand Down Expand Up @@ -46,6 +47,8 @@ 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 @@ -61,6 +64,17 @@ 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 @@ -86,9 +100,43 @@ func runNet(cmd *cobra.Command, _ []string) error {

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

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

}

asnInfos = handleInput(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...)
}

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)...)
}
}
}

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

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

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 len(results) <= 0 || err != nil {
core.ErrorF("No result found for: %v", err)
return asnInfo
}

if options.Debug {
spew.Dump(results)
}
func searchByIP(input string) []modules.ASInfo {
var asnInfo []modules.ASInfo

listOfCIDR, err := asnmap.GetCIDR(results)
ip, err := netaddr.ParseIP(input)
if err != nil {
return asnInfo
}

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)
if asn := ASNMap.ASofIP(ip); asn.AS != 0 {
return ASNMap.ASInfo(asn.AS)
}
return asnInfo
}
Expand Down Expand Up @@ -284,11 +313,3 @@ 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
}
5 changes: 3 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"strings"

"github.com/fatih/color"
"github.com/j3ssie/metabigor/core"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -82,8 +83,8 @@ func initConfig() {
options.Input = core.GetFileContent(options.InputFile)
}

core.InforF("Metabigor %v by %v", core.VERSION, core.AUTHOR)
core.InforF(fmt.Sprintf("Store log file to: %v", options.LogFile))
core.InforF("Metabigor %v by %v", core.VERSION, color.HiMagentaString(core.AUTHOR))
core.DebugF(fmt.Sprintf("Store log file to: %v", options.LogFile))
}

// RootMessage print help message
Expand Down
30 changes: 24 additions & 6 deletions cmd/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package cmd

import (
"fmt"
"github.com/thoas/go-funk"
"io/ioutil"
"os"
"strings"
"sync"

"github.com/pkg/errors"
"github.com/thoas/go-funk"

"github.com/j3ssie/metabigor/core"
"github.com/j3ssie/metabigor/modules"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -228,9 +229,16 @@ func parseResult(resultFolder string, options core.Options) {
core.ErrorF("Result Folder not found: ", resultFolder)
return
}
core.BannerF("Reading result from: ", fmt.Sprintf("%v", resultFolder))
Files, err := ioutil.ReadDir(resultFolder)
dir, err := os.Open(resultFolder)
if err != nil {
fmt.Println(errors.Wrap(err, "error opening directory"))
return
}
defer dir.Close()

Files, err := dir.Readdir(0)
if err != nil {
fmt.Println(errors.Wrap(err, "error reading directory contents"))
return
}

Expand Down Expand Up @@ -271,10 +279,20 @@ func parseResult(resultFolder string, options core.Options) {
// StoreTmpInput store list of string to tmp file
func StoreTmpInput(raw []string, options core.Options) string {
tmpDest := options.Scan.TmpOutput
tmpFile, _ := ioutil.TempFile(options.Scan.TmpOutput, "joined-*.txt")
tmpFile, err := os.CreateTemp(tmpDest, "joined-*.txt")
if err != nil {
return ""
}
defer tmpFile.Close()

if tmpDest != "" {
tmpFile, _ = ioutil.TempFile(tmpDest, "joined-input-*.txt")
tmpFile, err = os.CreateTemp(tmpDest, "joined-input-*.txt")
if err != nil {
return ""
}
defer tmpFile.Close()
}

tmpDest = tmpFile.Name()
core.WriteToFile(tmpDest, strings.Join(raw, "\n"))
return tmpDest
Expand Down
18 changes: 13 additions & 5 deletions core/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package core
import (
"fmt"
"io"
"io/ioutil"
"os"
"path"

Expand All @@ -13,6 +12,16 @@ import (
prefixed "github.com/x-cray/logrus-prefixed-formatter"
)

// NullWriter implements the io.Writer interface
// and discards all data written to it.
type NullWriter struct{}

// Write implements the Write method of the io.Writer interface.
// It discards all data written to it.
func (nw *NullWriter) Write(p []byte) (n int, err error) {
return len(p), nil
}

var logger = logrus.New()

// InitLog init log
Expand Down Expand Up @@ -48,7 +57,7 @@ func InitLog(options *Options) {
logger.SetLevel(logrus.InfoLevel)
}
if options.Quiet {
logger.SetOutput(ioutil.Discard)
logger.SetOutput(&NullWriter{})
}
}

Expand All @@ -59,9 +68,8 @@ func GoodF(format string, args ...interface{}) {
}

// BannerF print info message
func BannerF(format string, data string) {
banner := color.BlueString("[*] %v", format)
logger.Info(fmt.Sprintf("%v%v", banner, color.HiGreenString(data)))
func BannerF(prefix string, data string) {
logger.Info(fmt.Sprintf("%v %v%v", color.HiBlueString("==>"), prefix, color.HiGreenString(data)))
}

// InforF print info message
Expand Down
35 changes: 26 additions & 9 deletions core/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"path/filepath"
Expand All @@ -20,22 +19,40 @@ import (
"github.com/mitchellh/go-homedir"
)

// GetFileContent Reading file and return content of it
// GetFileContent reads file and returns its content.
func GetFileContent(filename string) string {
var result string
var result strings.Builder

if strings.Contains(filename, "~") {
filename, _ = homedir.Expand(filename)
var err error
filename, err = homedir.Expand(filename)
if err != nil {
return ""
}
}

file, err := os.Open(filename)
if err != nil {
return result
return ""
}
defer file.Close()
b, err := ioutil.ReadAll(file)
if err != nil {
return result

// Create a buffer to store file content
buf := make([]byte, 1024)

// Read file content into the buffer
for {
n, err := file.Read(buf)
if err != nil && err != io.EOF {
return ""
}
if n == 0 {
break
}
result.Write(buf[:n])
}
return string(b)

return result.String()
}

// ReadingFile Reading file and return content as []string
Expand Down
Loading

0 comments on commit 8eed25d

Please sign in to comment.