forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcidrmatch.go
59 lines (49 loc) · 1.3 KB
/
cidrmatch.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
package networking
import (
"context"
"net"
"github.com/Velocidex/ordereddict"
vql_subsystem "www.velocidex.com/golang/velociraptor/vql"
vfilter "www.velocidex.com/golang/vfilter"
"www.velocidex.com/golang/vfilter/arg_parser"
)
type _CIDRContainsArgs struct {
IP string `vfilter:"required,field=ip,doc=An IP address"`
Ranges []string `vfilter:"required,field=ranges,doc=A list of CIDR notation network ranges"`
}
type _CIDRContains struct{}
func (self _CIDRContains) Call(
ctx context.Context,
scope vfilter.Scope,
args *ordereddict.Dict) vfilter.Any {
arg := &_CIDRContainsArgs{}
err := arg_parser.ExtractArgsWithContext(ctx, scope, args, arg)
if err != nil {
scope.Log("cidr_contains: %s", err.Error())
return vfilter.Null{}
}
ip := net.ParseIP(arg.IP)
if ip == nil {
return false
}
for _, rng := range arg.Ranges {
_, ipNet, err := net.ParseCIDR(rng)
if err != nil {
scope.Log("cidr_contains: %v", err)
return false
}
if !ipNet.Contains(ip) {
return false
}
}
return true
}
func (self _CIDRContains) Info(scope vfilter.Scope, type_map *vfilter.TypeMap) *vfilter.FunctionInfo {
return &vfilter.FunctionInfo{
Name: "cidr_contains",
ArgType: type_map.AddType(scope, &_CIDRContainsArgs{}),
}
}
func init() {
vql_subsystem.RegisterFunction(&_CIDRContains{})
}