|
| 1 | +/* |
| 2 | + * ZAnnotate Copyright 2026 Regents of the University of Michigan |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy |
| 6 | + * of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 11 | + * implied. See the License for the specific language governing |
| 12 | + * permissions and limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package zannotate |
| 16 | + |
| 17 | +import ( |
| 18 | + "errors" |
| 19 | + "flag" |
| 20 | + "fmt" |
| 21 | + "net" |
| 22 | + "net/netip" |
| 23 | + "os" |
| 24 | + |
| 25 | + "github.com/oschwald/maxminddb-golang/v2" |
| 26 | + log "github.com/sirupsen/logrus" |
| 27 | +) |
| 28 | + |
| 29 | +type GreyNoiseAnnotatorFactory struct { |
| 30 | + BasePluginConf |
| 31 | + DBPath string // path to the .mmdb path |
| 32 | + greynoiseDB *maxminddb.Reader |
| 33 | +} |
| 34 | + |
| 35 | +// GreyNoise Annotator Factory (Global) |
| 36 | + |
| 37 | +func (a *GreyNoiseAnnotatorFactory) MakeAnnotator(i int) Annotator { |
| 38 | + var v GreyNoiseAnnotator |
| 39 | + v.Factory = a |
| 40 | + v.Id = i |
| 41 | + return &v |
| 42 | +} |
| 43 | + |
| 44 | +func (a *GreyNoiseAnnotatorFactory) Initialize(_ *GlobalConf) error { |
| 45 | + if len(a.DBPath) == 0 { |
| 46 | + return errors.New("greynoise database path is required when greynoise annotator is enabled, use --greynoise-database") |
| 47 | + } |
| 48 | + data, err := os.ReadFile(a.DBPath) // ensure DB is read in-memory |
| 49 | + if err != nil { |
| 50 | + return fmt.Errorf("unable to read greynoise database at %s: %w", a.DBPath, err) |
| 51 | + } |
| 52 | + a.greynoiseDB, err = maxminddb.OpenBytes(data) |
| 53 | + if err != nil { |
| 54 | + return fmt.Errorf("unable to open greynoise database at %s: %w", a.DBPath, err) |
| 55 | + } |
| 56 | + return nil |
| 57 | +} |
| 58 | + |
| 59 | +func (a *GreyNoiseAnnotatorFactory) GetWorkers() int { |
| 60 | + return a.Threads |
| 61 | +} |
| 62 | + |
| 63 | +func (a *GreyNoiseAnnotatorFactory) Close() error { |
| 64 | + if err := a.greynoiseDB.Close(); err != nil { |
| 65 | + return fmt.Errorf("unable to close greynoise database at %s: %w", a.DBPath, err) |
| 66 | + } |
| 67 | + return nil |
| 68 | +} |
| 69 | + |
| 70 | +func (a *GreyNoiseAnnotatorFactory) IsEnabled() bool { |
| 71 | + return a.Enabled |
| 72 | +} |
| 73 | + |
| 74 | +func (a *GreyNoiseAnnotatorFactory) AddFlags(flags *flag.FlagSet) { |
| 75 | + // Reverse DNS Lookup |
| 76 | + flags.BoolVar(&a.Enabled, "greynoise", false, "greynoise psychic data intelligence") |
| 77 | + flags.StringVar(&a.DBPath, "greynoise-database", "", "path to greynoise psychic .mmdb file") |
| 78 | + flags.IntVar(&a.Threads, "greynoise-threads", 2, "how many enrichment threads to use") |
| 79 | +} |
| 80 | + |
| 81 | +// GreyNoiseAnnotator (Per-Worker) |
| 82 | +type GreyNoiseAnnotator struct { |
| 83 | + Factory *GreyNoiseAnnotatorFactory |
| 84 | + Id int |
| 85 | +} |
| 86 | + |
| 87 | +func (a *GreyNoiseAnnotator) Initialize() (err error) { |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +func (a *GreyNoiseAnnotator) GetFieldName() string { |
| 92 | + return "greynoise" |
| 93 | +} |
| 94 | + |
| 95 | +// Annotate performs a reverse DNS lookup for the given IP address and returns the results. |
| 96 | +// If an error occurs or a lookup fails, it returns nil |
| 97 | +func (a *GreyNoiseAnnotator) Annotate(ip net.IP) interface{} { |
| 98 | + addr, ok := netip.AddrFromSlice(ip) |
| 99 | + if !ok { |
| 100 | + log.Debugf("unable to convert IP %s to address", ip) |
| 101 | + return nil |
| 102 | + } |
| 103 | + addr = addr.Unmap() |
| 104 | + var result any |
| 105 | + err := a.Factory.greynoiseDB.Lookup(addr).Decode(&result) |
| 106 | + if err != nil { |
| 107 | + log.Debugf("unable to annotate IP (%s): %v", addr, err) |
| 108 | + return nil |
| 109 | + } |
| 110 | + return result |
| 111 | +} |
| 112 | + |
| 113 | +func (a *GreyNoiseAnnotator) Close() error { |
| 114 | + return nil |
| 115 | +} |
| 116 | + |
| 117 | +func init() { |
| 118 | + s := new(GreyNoiseAnnotatorFactory) |
| 119 | + RegisterAnnotator(s) |
| 120 | +} |
0 commit comments