/
TorExitNodeLookupFunction.java
67 lines (53 loc) · 2.77 KB
/
TorExitNodeLookupFunction.java
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
package org.graylog.plugins.threatintel.functions.tor;
import org.graylog.plugins.pipelineprocessor.EvaluationContext;
import org.graylog.plugins.pipelineprocessor.ast.functions.FunctionArgs;
import org.graylog.plugins.pipelineprocessor.ast.functions.FunctionDescriptor;
import org.graylog.plugins.pipelineprocessor.ast.functions.ParameterDescriptor;
import org.graylog.plugins.threatintel.functions.misc.LookupTableFunction;
import org.graylog.plugins.threatintel.functions.GenericLookupResult;
import org.graylog2.lookup.LookupTableService;
import org.graylog2.plugin.lookup.LookupResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Inject;
public class TorExitNodeLookupFunction extends LookupTableFunction<GenericLookupResult> {
private static final Logger LOG = LoggerFactory.getLogger(TorExitNodeLookupFunction.class);
public static final String NAME = "tor_lookup";
private static final String VALUE = "ip_address";
private static final String LOOKUP_TABLE_NAME = "tor-exit-node-list";
private final ParameterDescriptor<String, String> valueParam = ParameterDescriptor.string(VALUE).description("The IP to look up.").build();
private final LookupTableService.Function lookupFunction;
@Inject
public TorExitNodeLookupFunction(final LookupTableService lookupTableService) {
this.lookupFunction = lookupTableService.newBuilder().lookupTable(LOOKUP_TABLE_NAME).build();
}
@Override
public GenericLookupResult evaluate(FunctionArgs args, EvaluationContext context) {
String ip = valueParam.required(args, context);
if (ip == null) {
LOG.error("NULL parameter passed to Tor exit node lookup.");
return null;
}
LOG.debug("Running Tor exit node lookup for IP [{}].", ip);
final LookupResult lookupResult = this.lookupFunction.lookup(ip.trim());
if (lookupResult != null && !lookupResult.isEmpty()) {
final Object value = lookupResult.singleValue();
if (value instanceof Boolean) {
return (Boolean) value ? GenericLookupResult.TRUE : GenericLookupResult.FALSE;
}
if (value instanceof String) {
return Boolean.valueOf((String) value) ? GenericLookupResult.TRUE : GenericLookupResult.FALSE;
}
}
return GenericLookupResult.FALSE;
}
@Override
public FunctionDescriptor<GenericLookupResult> descriptor() {
return FunctionDescriptor.<GenericLookupResult>builder()
.name(NAME)
.description("Match an IP address against known Tor exit nodes to identify connections from the Tor network.")
.params(valueParam)
.returnType(GenericLookupResult.class)
.build();
}
}