@@ -22,6 +22,7 @@ local cache = lru.new(cache_size)
2222
2323local useragent = string.format (' GreyNoiseFluentBit/%s' , greynoise ._version )
2424local auth = requests .HTTPBasicAuth (' none' , gn_api_key )
25+
2526local headers = {[' User-Agent' ] = useragent , [' Accept' ] = ' application/json' }
2627
2728local function has_value (tab , val )
4142-- @table record
4243-- @return table
4344local function convert_record_bools (record )
44- local gn_keys = {" gn_quick " , " gn_riot" , " gn_bogon" , " gn_invalid" }
45+ local gn_keys = {" gn_noise " , " gn_riot" , " gn_bogon" , " gn_invalid" }
4546 for k , v in pairs (record ) do
4647 if has_value (gn_keys , k ) then
4748 if (v == true ) then record [k ] = " true" end
@@ -100,60 +101,38 @@ local function check_ip(record, ip)
100101 return new_record
101102end
102103
103- -- Lookup a source_ip against `/v2/riot/ ` endpoint
104+ -- Lookup a source_ip against `/v3/community ` endpoint
104105--
105106-- @string ip
106- -- @return boolean
107- local function gn_riot_check (ip )
108- local url = string.format (' https://api.greynoise.io/v2/riot /%s' , ip )
107+ -- @return boolean, boolean
108+ local function gn_community_lookup (ip )
109+ local url = string.format (' https://api.greynoise.io/v3/community /%s' , ip )
109110 local response = requests .get {url , headers = headers , auth = auth }
110111 if (not response ) then
111- log .warn (' no response from /v2/riot/ endpoint' )
112- return nil
112+ log .warn (' no response from /v3/community endpoint' )
113+ return nil , nil
113114 end
114115 if response .status_code == 200 then
115116 local body , error = response .json ()
116117 if error ~= nil then
117118 log .warn (' %v' , error )
118- return nil
119+ return nil , nil
119120 end
120- if body .riot == true then return true end
121- return false
122- elseif response .status_code == 404 then
123- -- RIOT uses a soft 404 to represent that resource is not in RIOT.
124- -- This interface differs from the `v2/noise/quick` endpoint that returns 200 for all requested IPs.
125- return false
121+ return body .noise , body .riot
126122 end
127-
128- log .warn (string.format (' Received %d status code from %s' ,
129- response .status_code , url ))
130- return nil
131- end
132-
133- -- Lookup a source_ip against `/v2/noise/quick/` endpoint
134- --
135- -- @string ip
136- -- @return boolean
137- local function gn_quick_check (ip )
138- local url = string.format (' https://api.greynoise.io/v2/noise/quick/%s' , ip )
139- local response = requests .get {url , headers = headers , auth = auth }
140- if (not response ) then
141- log .warn (' no response from /v2/noise/quick/ endpoint' )
142- return nil
143- end
144- if response .status_code == 200 then
145- local body , error = response .json ()
146- if error ~= nil then
147- log .warn (' %v' , error )
148- return nil
149- end
150- if body .noise == true then return true end
151- return false
123+ if response .status_code == 404 then
124+ local body , error = response .json ()
125+ if error ~= nil then
126+ log .warn (' %v' , error )
127+ return nil , nil
128+ end
129+ log .debug (string.format (' %s, %s' ,url , body .message ))
130+ return false , false
152131 end
153132
154133 log .warn (string.format (' Received %d status code from %s' ,
155134 response .status_code , url ))
156- return nil
135+ return nil , nil
157136end
158137
159138-- Main filter handler
@@ -163,18 +142,19 @@ end
163142-- @table record
164143-- @return number, number, table
165144function gn_filter (_ , timestamp , record )
166- local ip = record [ip_field ]
145+ -- Extract IPv4 from message
146+ local ip = record [ip_field ]:match (" (%d+%.%d+%.%d+%.%d+)" )
167147 local new_record = record
168148 new_record .gn_riot = nil
169- new_record .gn_quick = nil
149+ new_record .gn_noise = nil
170150 new_record .gn_invalid = nil
171151 new_record .gn_bogon = nil
172152 if ip then
173153 local cache_record = cache :get (ip )
174154 if cache_record then
175155 log .debug (string.format (' cache hit: %s' , ip ))
176156 new_record .gn_riot = cache_record [' r' ]
177- new_record .gn_quick = cache_record [' q' ]
157+ new_record .gn_noise = cache_record [' q' ]
178158 new_record .gn_invalid = cache_record [' i' ]
179159 new_record .gn_bogon = cache_record [' b' ]
180160 local final_record = convert_record_bools (new_record )
@@ -184,11 +164,13 @@ function gn_filter(_, timestamp, record)
184164 log .debug (string.format (' lookup: %s' , ip ))
185165 if (not validated_record .gn_invalid and not validated_record .gn_bogon ) then
186166 -- Make GN API calls for valid non-bogon IPv4 records
187- validated_record .gn_riot = gn_riot_check (ip )
188- validated_record .gn_quick = gn_quick_check (ip )
167+ validated_record .gn_noise , validated_record .gn_riot = gn_community_lookup (ip )
168+ if not validated_record .gn_noise == nil or not validated_record .gn_riot == nil then
169+ return - 1 , timestamp , record
170+ end
189171 cache :set (ip , {
190172 r = validated_record .gn_riot ,
191- q = validated_record .gn_quick ,
173+ q = validated_record .gn_noise ,
192174 i = validated_record .gn_invalid ,
193175 b = validated_record .gn_bogon
194176 })
0 commit comments