Skip to content

Commit

Permalink
enable rating of threat indicators by OpenAI.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonhadfield committed Oct 6, 2024
1 parent 5e7a470 commit 167a0a2
Show file tree
Hide file tree
Showing 24 changed files with 474 additions and 4 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ require (
github.com/rivo/uniseg v0.4.7 // indirect
github.com/sagikazarmark/locafero v0.6.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sashabaranov/go-openai v1.31.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ github.com/sagikazarmark/locafero v0.6.0 h1:ON7AQg37yzcRPU69mt7gwhFEBwxI6P9T4Qu3
github.com/sagikazarmark/locafero v0.6.0/go.mod h1:77OmuIc6VTraTXKXIs/uvUxKGUXjE1GbemJYHqdNjX0=
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
github.com/sashabaranov/go-openai v1.31.0 h1:rGe77x7zUeCjtS2IS7NCY6Tp4bQviXNMhkQM6hz/UC4=
github.com/sashabaranov/go-openai v1.31.0/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
github.com/scylladb/termtables v0.0.0-20191203121021-c4c0b6d42ff4/go.mod h1:C1a7PQSMz9NShzorzCiG2fk9+xuCgLkPeCvMHYR2OWg=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/shurcooL/component v0.0.0-20170202220835-f88ec8f54cc4/go.mod h1:XhFIlyj5a1fBNx5aJTbKoIq0mNaPvOagO+HjB3EtxrY=
Expand Down
25 changes: 25 additions & 0 deletions providers/abuseipdb/abuseipdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/netip"
"net/url"
"os"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -121,6 +122,30 @@ func (c *Client) RateHostData(findRes []byte, ratingConfigJSON []byte) (provider
return rateResult, nil
}

func (c *Client) ExtractThreatIndicators(findRes []byte) (*providers.ThreatIndicators, error) {
var doc HostSearchResult

if err := json.Unmarshal(findRes, &doc); err != nil {
return nil, fmt.Errorf("error unmarshalling find result: %w", err)
}

threatIndicators := providers.ThreatIndicators{
Provider: ProviderName,
}

indicators := make(map[string]string)

if doc.Data.IsTor {
indicators["TOR"] = "true"
}

indicators["AbuseConfidencePercentage"] = strconv.Itoa(int(doc.Data.AbuseConfidenceScore))

threatIndicators.Indicators = indicators

return &threatIndicators, nil
}

type Client struct {
session.Session
}
Expand Down
38 changes: 38 additions & 0 deletions providers/annotated/annotated.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"path/filepath"
"slices"
"sort"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -87,6 +88,43 @@ func annotationNotesContain(notes []string, term string) bool {
return false
}

func extractThreatAnnotations(ae []annotation) (threats []string) {
for y := range ae {
for z := range ae[y].Notes {
if strings.HasPrefix(ae[y].Notes[z], "threat:") {
threats = append(threats, ae[y].Notes[z])
}
}
}

return
}

func (c *ProviderClient) ExtractThreatIndicators(findRes []byte) (*providers.ThreatIndicators, error) {
var doc HostSearchResult

if err := json.Unmarshal(findRes, &doc); err != nil {
return nil, fmt.Errorf("error unmarshalling find result: %w", err)
}

threatIndicators := providers.ThreatIndicators{
Provider: ProviderName,
}

indicators := make(map[string]string)

for _, v := range doc {
threatAnnotations := extractThreatAnnotations(v)
for x, ta := range threatAnnotations {
indicators["userSpecified"+strconv.Itoa(x)] = ta
}
}

threatIndicators.Indicators = indicators

return &threatIndicators, nil
}

func (c *ProviderClient) RateHostData(findRes []byte, ratingConfigJSON []byte) (providers.RateResult, error) {
var doc HostSearchResult

Expand Down
22 changes: 22 additions & 0 deletions providers/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,28 @@ func (c *ProviderClient) GetConfig() *session.Session {
return &c.Session
}

func (c *ProviderClient) ExtractThreatIndicators(findRes []byte) (*providers.ThreatIndicators, error) {
var doc HostSearchResult

if err := json.Unmarshal(findRes, &doc); err != nil {
return nil, fmt.Errorf("error unmarshalling find result: %w", err)
}

threatIndicators := providers.ThreatIndicators{
Provider: ProviderName,
}

indicators := make(map[string]string)

if doc.IPPrefix.IsValid() || doc.IPv6Prefix.IPv6Prefix.IsValid() {
indicators["HostedInAWS"] = "true"
}

threatIndicators.Indicators = indicators

return &threatIndicators, nil
}

func (c *ProviderClient) RateHostData(findRes []byte, ratingConfigJSON []byte) (providers.RateResult, error) {
var ratingConfig providers.RatingConfig
if err := json.Unmarshal(ratingConfigJSON, &ratingConfig); err != nil {
Expand Down
22 changes: 22 additions & 0 deletions providers/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,28 @@ func (c *ProviderClient) GetConfig() *session.Session {
return &c.Session
}

func (c *ProviderClient) ExtractThreatIndicators(findRes []byte) (*providers.ThreatIndicators, error) {
var doc HostSearchResult

if err := json.Unmarshal(findRes, &doc); err != nil {
return nil, fmt.Errorf("error unmarshalling find result: %w", err)
}

threatIndicators := providers.ThreatIndicators{
Provider: ProviderName,
}

indicators := make(map[string]string)

if doc.Prefix.IsValid() {
indicators["HostedInAzure"] = "true"
}

threatIndicators.Indicators = indicators

return &threatIndicators, nil
}

func (c *ProviderClient) RateHostData(findRes []byte, ratingConfigJSON []byte) (providers.RateResult, error) {
var ratingConfig providers.RatingConfig
if err := json.Unmarshal(ratingConfigJSON, &ratingConfig); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions providers/azurewaf/azurewaf.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func (c *ProviderClient) GetConfig() *session.Session {
return &c.Session
}

func (c *ProviderClient) ExtractThreatIndicators(findRes []byte) (*providers.ThreatIndicators, error) {
return nil, nil
}

func (c *ProviderClient) RateHostData(findRes []byte, ratingConfigJSON []byte) (providers.RateResult, error) {
return providers.RateResult{}, nil
}
Expand Down
22 changes: 22 additions & 0 deletions providers/bingbot/bingbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,28 @@ func (c *ProviderClient) GetConfig() *session.Session {
return &c.Session
}

func (c *ProviderClient) ExtractThreatIndicators(findRes []byte) (*providers.ThreatIndicators, error) {
var doc HostSearchResult

if err := json.Unmarshal(findRes, &doc); err != nil {
return nil, fmt.Errorf("error unmarshalling find result: %w", err)
}

threatIndicators := providers.ThreatIndicators{
Provider: ProviderName,
}

indicators := make(map[string]string)

if doc.Prefix.IsValid() {
indicators["ReputableBot"] = "true"
}

threatIndicators.Indicators = indicators

return &threatIndicators, nil
}

func (c *ProviderClient) RateHostData(findRes []byte, ratingConfigJSON []byte) (providers.RateResult, error) {
var ratingConfig providers.RatingConfig
if err := json.Unmarshal(ratingConfigJSON, &ratingConfig); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions providers/criminalip/criminalip.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func (c *Client) GetConfig() *session.Session {
return &c.Session
}

func (c *Client) ExtractThreatIndicators(findRes []byte) (*providers.ThreatIndicators, error) {
return nil, nil
}

func (c *Client) RateHostData(findRes []byte, ratingConfigJSON []byte) (providers.RateResult, error) {
var doc HostSearchResult

Expand Down
22 changes: 22 additions & 0 deletions providers/digitalocean/digitalocean.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,28 @@ func (c *ProviderClient) GetConfig() *session.Session {
return &c.Session
}

func (c *ProviderClient) ExtractThreatIndicators(findRes []byte) (*providers.ThreatIndicators, error) {
var doc HostSearchResult

if err := json.Unmarshal(findRes, &doc); err != nil {
return nil, fmt.Errorf("error unmarshalling find result: %w", err)
}

threatIndicators := providers.ThreatIndicators{
Provider: ProviderName,
}

indicators := make(map[string]string)

if doc.Record.Network.IsValid() {
indicators["HostedInDigitalOcean"] = "true"
}

threatIndicators.Indicators = indicators

return &threatIndicators, nil
}

func (c *ProviderClient) RateHostData(findRes []byte, ratingConfigJSON []byte) (providers.RateResult, error) {
var ratingConfig providers.RatingConfig
if err := json.Unmarshal(ratingConfigJSON, &ratingConfig); err != nil {
Expand Down
22 changes: 22 additions & 0 deletions providers/gcp/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,28 @@ func (c *ProviderClient) GetConfig() *session.Session {
return &c.Session
}

func (c *ProviderClient) ExtractThreatIndicators(findRes []byte) (*providers.ThreatIndicators, error) {
var doc HostSearchResult

if err := json.Unmarshal(findRes, &doc); err != nil {
return nil, fmt.Errorf("error unmarshalling find result: %w", err)
}

threatIndicators := providers.ThreatIndicators{
Provider: ProviderName,
}

indicators := make(map[string]string)

if doc.Prefix.IsValid() {
indicators["HostedInGCP"] = "true"
}

threatIndicators.Indicators = indicators

return &threatIndicators, nil
}

func (c *ProviderClient) RateHostData(findRes []byte, ratingConfigJSON []byte) (providers.RateResult, error) {
var ratingConfig providers.RatingConfig
if err := json.Unmarshal(ratingConfigJSON, &ratingConfig); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions providers/google/google.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func (c *ProviderClient) GetConfig() *session.Session {
return &c.Session
}

func (c *ProviderClient) ExtractThreatIndicators(findRes []byte) (*providers.ThreatIndicators, error) {
return nil, nil
}

func (c *ProviderClient) RateHostData(findRes []byte, ratingConfigJSON []byte) (providers.RateResult, error) {
var ratingConfig providers.RatingConfig
if err := json.Unmarshal(ratingConfigJSON, &ratingConfig); err != nil {
Expand Down
22 changes: 22 additions & 0 deletions providers/googlebot/googlebot.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,28 @@ func (c *ProviderClient) GetConfig() *session.Session {
return &c.Session
}

func (c *ProviderClient) ExtractThreatIndicators(findRes []byte) (*providers.ThreatIndicators, error) {
var doc HostSearchResult

if err := json.Unmarshal(findRes, &doc); err != nil {
return nil, fmt.Errorf("error unmarshalling find result: %w", err)
}

threatIndicators := providers.ThreatIndicators{
Provider: ProviderName,
}

indicators := make(map[string]string)

if doc.Prefix.IsValid() {
indicators["ReputableBot"] = "true"
}

threatIndicators.Indicators = indicators

return &threatIndicators, nil
}

func (c *ProviderClient) RateHostData(findRes []byte, ratingConfigJSON []byte) (providers.RateResult, error) {
var ratingConfig providers.RatingConfig
if err := json.Unmarshal(ratingConfigJSON, &ratingConfig); err != nil {
Expand Down
22 changes: 22 additions & 0 deletions providers/googlesc/googlesc.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,28 @@ func (c *ProviderClient) GetConfig() *session.Session {
return &c.Session
}

func (c *ProviderClient) ExtractThreatIndicators(findRes []byte) (*providers.ThreatIndicators, error) {
var doc HostSearchResult

if err := json.Unmarshal(findRes, &doc); err != nil {
return nil, fmt.Errorf("error unmarshalling find result: %w", err)
}

threatIndicators := providers.ThreatIndicators{
Provider: ProviderName,
}

indicators := make(map[string]string)

if doc.Prefix.IsValid() {
indicators["ReputableBot"] = "true"
}

threatIndicators.Indicators = indicators

return &threatIndicators, nil
}

func (c *ProviderClient) RateHostData(findRes []byte, ratingConfigJSON []byte) (providers.RateResult, error) {
var ratingConfig providers.RatingConfig
if err := json.Unmarshal(ratingConfigJSON, &ratingConfig); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions providers/icloudpr/icloudpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ func (c *ProviderClient) GetConfig() *session.Session {
return &c.Session
}

func (c *ProviderClient) ExtractThreatIndicators(findRes []byte) (*providers.ThreatIndicators, error) {
return nil, nil
}

func (c *ProviderClient) RateHostData(findRes []byte, ratingConfigJSON []byte) (providers.RateResult, error) {
var ratingConfig providers.RatingConfig
if err := json.Unmarshal(ratingConfigJSON, &ratingConfig); err != nil {
Expand Down
20 changes: 20 additions & 0 deletions providers/ipapi/ipapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,26 @@ func (c *Client) GetConfig() *session.Session {
return &c.Session
}

func (c *Client) ExtractThreatIndicators(findRes []byte) (*providers.ThreatIndicators, error) {
var doc HostSearchResult

if err := json.Unmarshal(findRes, &doc); err != nil {
return nil, fmt.Errorf("error unmarshalling find result: %w", err)
}

threatIndicators := providers.ThreatIndicators{
Provider: ProviderName,
}

indicators := make(map[string]string)

indicators["CountryCodeISO3"] = doc.CountryCodeIso3

threatIndicators.Indicators = indicators

return &threatIndicators, nil
}

func (c *Client) RateHostData(findRes []byte, ratingConfigJSON []byte) (providers.RateResult, error) {
var ratingConfig providers.RatingConfig
if err := json.Unmarshal(ratingConfigJSON, &ratingConfig); err != nil {
Expand Down
Loading

0 comments on commit 167a0a2

Please sign in to comment.