Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
alex27riva committed Oct 18, 2024
1 parent ba159a8 commit cdad95c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
5 changes: 2 additions & 3 deletions cmd/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ func analyzeIP(ip string) {

ipInfoApiKey := viper.GetString("api_keys.ipinfo.api_key")
if ipInfoApiKey == "" {
log.Println("API key is missing! Please set the ipinfo_api_key in config.yaml file")
log.Println("API key is missing! Please set the ipinfo api_key in config.yaml file")
}

abuseIPDBApiKey := viper.GetString("api_keys.abuseipdb.api_key")
if abuseIPDBApiKey == "" {
log.Println("API key is missing! Please set the ipinfo_api_key in config.yaml file")
log.Println("API key is missing! Please set the abuseipdb api_key in config.yaml file")
}

// Fetch IpInfo api
Expand Down Expand Up @@ -77,7 +77,6 @@ func analyzeIP(ip string) {
fmt.Println(Blue + "\nAbuseIPDB report" + Reset)

// Print AbuseIPDB info
fmt.Printf("AbuseIPDB Data for IP: %s\n", ip)
fmt.Printf("Abuse Confidence Score: %d\n", abuseIPDBData.Data.AbuseConfidenceScore)
fmt.Printf("Total Reports: %d\n", abuseIPDBData.Data.TotalReports)
fmt.Printf("Last Reported At: %s\n", abuseIPDBData.Data.LastReportedAt)
Expand Down
43 changes: 27 additions & 16 deletions cmd/urlscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,19 @@ import (
"time"
)

// urlScanResult represents a simplified result from URLscan.io
var defautVisibility = "private" // public, unlisted or private

const (
urlscanScanApi = "https://urlscan.io/api/v1/scan/"
urlscanResultApi = "https://urlscan.io/api/v1/result/%s/"
)

type urlScanResult struct {
Page struct {
URL string `json:"url"`
Domain string `json:"domain"`
Country string `json:"country"`
IP string `json:"ip"`
} `json:"page"`
Verdict struct {
Malicious bool `json:"malicious"`
Expand All @@ -33,16 +40,15 @@ type urlScanResult struct {
func submitURLScan(url string) (string, error) {
apiKey := viper.GetString("api_keys.urlscan.api_key")
if apiKey == "" {
return "", fmt.Errorf("API key is missing! Please set the urlscan_api_key in config.yaml file")
return "", fmt.Errorf("API key is missing! Please set the urlscan api_key in config.yaml file")
}

apiUrl := "https://urlscan.io/api/v1/scan/"
requestBody, err := json.Marshal(map[string]string{"url": url})
requestBody, err := json.Marshal(map[string]string{"url": url, "visibility": defautVisibility})
if err != nil {
return "", fmt.Errorf("failed to create request body: %v", err)
}

req, err := http.NewRequest("POST", apiUrl, bytes.NewBuffer(requestBody))
req, err := http.NewRequest("POST", urlscanScanApi, bytes.NewBuffer(requestBody))
if err != nil {
return "", fmt.Errorf("failed to create request: %v", err)
}
Expand Down Expand Up @@ -72,7 +78,7 @@ func submitURLScan(url string) (string, error) {

// fetchURLScanResult fetches the results of a URL scan
func fetchURLScanResult(scanID string) (*urlScanResult, error) {
apiUrl := fmt.Sprintf("https://urlscan.io/api/v1/result/%s/", scanID)
apiUrl := fmt.Sprintf(urlscanResultApi, scanID)

// Polling for scan results
for i := 0; i < 10; i++ {
Expand Down Expand Up @@ -104,7 +110,19 @@ func fetchURLScanResult(scanID string) (*urlScanResult, error) {
return nil, fmt.Errorf("scan result not available after multiple attempts")
}

// urlScanCmd represents the URL scanning command
func displayResults(scanResult urlScanResult) {
fmt.Printf("Scan Results for URL: %s\n", scanResult.Page.URL)
fmt.Printf("Domain: %s\n", scanResult.Page.Domain)
fmt.Printf("Country: %s\n", scanResult.Page.Country)
fmt.Printf("IP: %s\n", scanResult.Page.IP)
if scanResult.Verdict.Malicious {
fmt.Println("Verdict: " + Red + "MALICIOUS")
} else {
fmt.Println("Verdict: " + Green + "SAFE")
}

}

var urlScanCmd = &cobra.Command{
Use: "urlscan [url]",
Short: "Submit a URL for malware scanning and fetch the results",
Expand All @@ -118,23 +136,16 @@ var urlScanCmd = &cobra.Command{
log.Fatalf("Error submitting URL for scan: %v", err)
}

fmt.Printf("Scan ID: %s\n", scanID)
fmt.Println("URL submitted successfully. Awaiting results...")

// Fetch the scan results
scanResult, err := fetchURLScanResult(scanID)
if err != nil {
log.Fatalf("Error retrieving scan results: %v", err)
}
displayResults(*scanResult)

// Print the scan results
fmt.Printf("Scan Results for URL: %s\n", scanResult.Page.URL)
fmt.Printf("Domain: %s\n", scanResult.Page.Domain)
fmt.Printf("Country: %s\n", scanResult.Page.Country)
if scanResult.Verdict.Malicious {
fmt.Println("Verdict: MALICIOUS")
} else {
fmt.Println("Verdict: SAFE")
}
},
}

Expand Down

0 comments on commit cdad95c

Please sign in to comment.