github.com/zartbot/goflow@v0.0.0-20221001080520-456f7dfad288/service/identity/reputation.go (about)

     1  package identity
     2  
     3  import (
     4  	"encoding/csv"
     5  	"log"
     6  	"net"
     7  	"os"
     8  	"sync"
     9  
    10  	"github.com/zartbot/go_utils/net/netradix"
    11  	"github.com/zartbot/goflow/lib/iputil"
    12  )
    13  
    14  var ReputationInfoDatabase sync.Map
    15  
    16  //CSV Format
    17  //based on https://reputation.alienvault.com/reputation.snort
    18  //142.93.136.62,Spamming
    19  
    20  func LoadReputationInfo(filename string) {
    21  	file, err := os.Open(filename)
    22  	if err != nil {
    23  		log.Println("[Reputation Database Parser] Error:", err)
    24  		return
    25  	}
    26  	defer file.Close()
    27  	reader := csv.NewReader(file)
    28  	records, err := reader.ReadAll()
    29  	if err != nil {
    30  		log.Fatal(err)
    31  	}
    32  
    33  	for idx := 1; idx < len(records); idx++ {
    34  		r := records[idx]
    35  		var address net.IP
    36  		var hostcategory string
    37  		address = net.ParseIP(r[0])
    38  		hostcategory = r[1]
    39  
    40  		/* sync.Map hash key need string type..
    41  		   convert Raw input string to Net.IP format as
    42  		   a validation method to avoid human input error
    43  		   and also filter private address by iputil.IsPrivate
    44  		   function
    45  		*/
    46  
    47  		if address != nil && !iputil.IsPrivateIP(address) {
    48  			ReputationInfoDatabase.Store(address.String(), hostcategory)
    49  		}
    50  	}
    51  }
    52  
    53  func LookupIPReputation(ipaddr string) (string, int) {
    54  	var result string
    55  	if _repinfo, ok := ReputationInfoDatabase.Load(ipaddr); ok {
    56  		result = _repinfo.(string)
    57  		return result, 8
    58  	}
    59  	return result, 0
    60  }
    61  
    62  var BotNet *netradix.NetRadixTree
    63  
    64  func LoadBotnetInfo(filename string) error {
    65  	rtree, err := netradix.NewNetRadixTree()
    66  	if err != nil {
    67  		panic(err)
    68  	}
    69  
    70  	file, err := os.Open(filename)
    71  	if err != nil {
    72  		log.Println("[BotNet Database Parser] Error:", err)
    73  		return err
    74  	}
    75  	defer file.Close()
    76  	reader := csv.NewReader(file)
    77  	records, err := reader.ReadAll()
    78  	if err != nil {
    79  		log.Fatal(err)
    80  	}
    81  
    82  	for idx := 1; idx < len(records); idx++ {
    83  		r := records[idx]
    84  		var prefix string
    85  		var hostcategory string
    86  		prefix = r[0]
    87  		hostcategory = r[1]
    88  
    89  		if err = rtree.Add(prefix, hostcategory); err != nil {
    90  			log.Fatal(err)
    91  		}
    92  	}
    93  	BotNet = rtree
    94  	return nil
    95  }