This repository has been archived by the owner on Jan 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
159 lines (135 loc) · 3.15 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package stanislav
import (
"encoding/binary"
"encoding/csv"
"encoding/json"
"errors"
"fmt"
netflow9 "github.com/VerizonDigital/vflow/netflow/v9"
"log"
"net"
"os"
"path/filepath"
"strconv"
"time"
)
func WalkAllDirs(basepath string) []string {
dirs := make([]string, 0, 100)
err := filepath.Walk(basepath,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
dirs = append(dirs, path)
}
return nil
})
if err != nil {
log.Println(err)
}
return dirs
}
func ConvStringToDate(input string) (time.Time, error) {
v, err := strconv.ParseInt(input, 10, 64)
if err != nil {
return time.Time{}, err
}
return time.Unix(v, 0), nil
}
func GetAbsDateNF9(input string, ph netflow9.PacketHeader) (time.Time, error) {
vt, err := time.ParseDuration(input + "ms")
if err != nil {
return time.Time{}, err
}
sysUpTime, err := time.ParseDuration(fmt.Sprint(ph.SysUpTime) + "ms")
if err != nil {
return time.Time{}, err
}
t := time.Unix(int64(ph.UNIXSecs), 0)
t = t.Add(-sysUpTime + vt)
return t, nil
}
func IsMulticastAddress(ip string) bool {
return net.ParseIP(ip).IsMulticast()
}
func IsBroadcastAddress(ip string) bool {
addr := net.ParseIP(ip)
netIp := net.IPNet{
IP: addr,
Mask: addr.DefaultMask(),
}
lastAddr, err := lastAddr(&netIp)
if err != nil {
return false
}
return addr.Equal(lastAddr)
}
func lastAddr(n *net.IPNet) (net.IP, error) { // works when the n is a prefix, otherwise...
if n.IP.To4() == nil {
return net.IP{}, errors.New("does not support IPv6 addresses")
}
ip := make(net.IP, len(n.IP.To4()))
binary.BigEndian.PutUint32(ip, binary.BigEndian.Uint32(n.IP.To4())|^binary.BigEndian.Uint32(net.IP(n.Mask).To4()))
return ip, nil
}
func ConvFloatToDuration(v float64) time.Duration {
xTime, err := time.ParseDuration(fmt.Sprint(v, "s"))
if err != nil {
return time.Second
}
return xTime
}
func WriteObjToJSONFile(fname string, obj interface{}) {
file, err := os.OpenFile(fname, os.O_CREATE|os.O_RDWR, os.ModePerm)
defer file.Close()
if err != nil {
log.Println(err)
return
}
encoder := json.NewEncoder(file)
encoder.Encode(obj)
}
func AddPossibleThreat(ip, reason string) {
if reasons, ok := PossibleThreat[ip]; ok {
for _, v := range reasons { //avoid duplicate reasons
if v == reason {
return
}
}
reasons = append(reasons, reason)
PossibleThreat[ip] = reasons
} else {
PossibleThreat[ip] = []string{reason}
}
}
func LoadBlockListedC2() {
if Conf.IpBlackListFile == "" {
logger.Println("c2 block list not selected")
return
}
file, err := os.OpenFile(Conf.IpBlackListFile, os.O_RDONLY, 0777)
defer file.Close()
if err != nil {
logger.Println(err)
return
}
r := csv.NewReader(file)
r.Comment = '#'
for {
csvField, err := r.Read()
if err != nil {
break
}
//Parse csv fields
ip := csvField[1] //ip
name := csvField[4] //malware name
blackListIp[ip] = name
}
}
func checkAndSetPossibleThreat(blockedMap map[string]string, key, ip, reason string) {
if name, ok := blockedMap[key]; ok {
AddPossibleThreat(ip, reason+" "+name)
logger.Printf("[%s] appears in the blocked list. %s!\n", ip, name)
}
}