Skip to content

Commit

Permalink
More structure
Browse files Browse the repository at this point in the history
  • Loading branch information
alex27riva committed Oct 18, 2024
1 parent 225d67a commit 6e17608
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 99 deletions.
102 changes: 4 additions & 98 deletions cmd/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,106 +10,12 @@ import (
"fmt"
"log"
"os"
"soc-cli/internal/apis"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

const (
greyNoiseAPIURL = "https://api.greynoise.io/v3/community/%s"
ipInfoAPIURL = "https://ipinfo.io/%s?token=%s"
abuseAPIURL = "https://api.abuseipdb.com/api/v2/check?ipAddress=%s&maxAgeInDays=90"
)

type ipInfo struct {
IP string `json:"ip"`
Country string `json:"country"`
Hostname string `json:"hostname"`
Org string `json:"org"`
}

type greyNoiseInfo struct {
IP string `json:"ip"`
Noise bool `json:"noise"`
Riot bool `json:"riot"`
Classification string `json:"classification"`
Name string `json:"name"`
Link string `json:"link"`
}

type abuseIPDBResponse struct {
Data struct {
IPAddress string `json:"ipAddress"`
IsPublic bool `json:"isPublic"`
IPVersion int `json:"ipVersion"`
IsWhitelisted bool `json:"isWhitelisted"`
AbuseConfidenceScore int `json:"abuseConfidenceScore"`
CountryCode string `json:"countryCode"`
UsageType string `json:"usageType"`
ISP string `json:"isp"`
Domain string `json:"domain"`
Hostnames []string `json:"hostnames"`
TotalReports int `json:"totalReports"`
LastReportedAt string `json:"lastReportedAt"`
Reports []struct {
ReporterID int `json:"reporterId"`
ReporterCountry string `json:"reporterCountry"`
ReportedAt string `json:"reportedAt"`
Comment string `json:"comment"`
} `json:"reports"`
} `json:"data"`
}

// Get threat intelligence from GreyNoise API
func getGreyNoiseData(ip string, apiKey string) *greyNoiseInfo {
apiUrl := fmt.Sprintf(greyNoiseAPIURL, ip)

headers := map[string]string{
"key": apiKey,
}

var greyNoiseData greyNoiseInfo

err := MakeAPIRequest(apiUrl, headers, &greyNoiseData)
if err != nil {
log.Fatalf("Error fetching AbuseIPDB info: %v", err)
}

return &greyNoiseData
}

func getIPInfo(ip string, apiKey string) *ipInfo {
apiUrl := fmt.Sprintf(ipInfoAPIURL, ip, apiKey)

var info ipInfo

err := MakeAPIRequest(apiUrl, nil, &info)
if err != nil {
log.Fatalf("Error fetching IP info: %v", err)
}

return &info
}

// getAbuseIPDBInfo fetches data from AbuseIPDB for a specific IP address
func getAbuseIPDBInfo(ip string, apiKey string) *abuseIPDBResponse {
apiUrl := fmt.Sprintf(abuseAPIURL, ip)

headers := map[string]string{
"Key": apiKey,
"Accept": "application/json",
}

var data abuseIPDBResponse

err := MakeAPIRequest(apiUrl, headers, &data)
if err != nil {
log.Fatalf("Error fetching AbuseIPDB info: %v", err)
}

return &data
}

func analyzeIP(ip string) {

// Validate provided IP address
Expand Down Expand Up @@ -141,12 +47,12 @@ func analyzeIP(ip string) {
}

// Fetch IpInfo api
ipInfoData := getIPInfo(ip, ipInfoApiKey)
ipInfoData := apis.GetIPInfo(ip, ipInfoApiKey)

// Fetch GreyNoise threat intelligence
greyNoiseData := getGreyNoiseData(ip, greyNoiseApiKey)
greyNoiseData := apis.GetGreyNoiseData(ip, greyNoiseApiKey)

abuseIPDBData := getAbuseIPDBInfo(ip, abuseIPDBApiKey)
abuseIPDBData := apis.GetAbuseIPDBInfo(ip, abuseIPDBApiKey)

// Print the IP information
fmt.Println(Blue + "IP information from IPInfo" + Reset)
Expand Down
51 changes: 51 additions & 0 deletions internal/apis/abuseipdb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package apis

import (
"fmt"
"log"
"soc-cli/internal/util"
)

const abuseAPIURL = "https://api.abuseipdb.com/api/v2/check?ipAddress=%s&maxAgeInDays=90"

type abuseIPDBResponse struct {
Data struct {
IPAddress string `json:"ipAddress"`
IsPublic bool `json:"isPublic"`
IPVersion int `json:"ipVersion"`
IsWhitelisted bool `json:"isWhitelisted"`
AbuseConfidenceScore int `json:"abuseConfidenceScore"`
CountryCode string `json:"countryCode"`
UsageType string `json:"usageType"`
ISP string `json:"isp"`
Domain string `json:"domain"`
Hostnames []string `json:"hostnames"`
TotalReports int `json:"totalReports"`
LastReportedAt string `json:"lastReportedAt"`
Reports []struct {
ReporterID int `json:"reporterId"`
ReporterCountry string `json:"reporterCountry"`
ReportedAt string `json:"reportedAt"`
Comment string `json:"comment"`
} `json:"reports"`
} `json:"data"`
}

// getAbuseIPDBInfo fetches data from AbuseIPDB for a specific IP address
func GetAbuseIPDBInfo(ip string, apiKey string) *abuseIPDBResponse {
apiUrl := fmt.Sprintf(abuseAPIURL, ip)

headers := map[string]string{
"Key": apiKey,
"Accept": "application/json",
}

var data abuseIPDBResponse

err := util.MakeAPIRequest(apiUrl, headers, &data)
if err != nil {
log.Fatalf("Error fetching AbuseIPDB info: %v", err)
}

return &data
}
36 changes: 36 additions & 0 deletions internal/apis/greynoise.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package apis

import (
"fmt"
"log"
"soc-cli/internal/util"
)

const greyNoiseAPIURL = "https://api.greynoise.io/v3/community/%s"

type greyNoiseInfo struct {
IP string `json:"ip"`
Noise bool `json:"noise"`
Riot bool `json:"riot"`
Classification string `json:"classification"`
Name string `json:"name"`
Link string `json:"link"`
}

// Get threat intelligence from GreyNoise API
func GetGreyNoiseData(ip string, apiKey string) *greyNoiseInfo {
apiUrl := fmt.Sprintf(greyNoiseAPIURL, ip)

headers := map[string]string{
"key": apiKey,
}

var greyNoiseData greyNoiseInfo

err := util.MakeAPIRequest(apiUrl, headers, &greyNoiseData)
if err != nil {
log.Fatalf("Error fetching AbuseIPDB info: %v", err)
}

return &greyNoiseData
}
29 changes: 29 additions & 0 deletions internal/apis/ipinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package apis

import (
"fmt"
"log"
"soc-cli/internal/util"
)

const ipInfoAPIURL = "https://ipinfo.io/%s?token=%s"

type ipInfo struct {
IP string `json:"ip"`
Country string `json:"country"`
Hostname string `json:"hostname"`
Org string `json:"org"`
}

func GetIPInfo(ip string, apiKey string) *ipInfo {
apiUrl := fmt.Sprintf(ipInfoAPIURL, ip, apiKey)

var info ipInfo

err := util.MakeAPIRequest(apiUrl, nil, &info)
if err != nil {
log.Fatalf("Error fetching IP info: %v", err)
}

return &info
}
2 changes: 1 addition & 1 deletion cmd/api.go → internal/util/api.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package util

import (
"encoding/json"
Expand Down

0 comments on commit 6e17608

Please sign in to comment.