Skip to content

Commit

Permalink
Recreated repo to fix LFS issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ip2location committed Nov 24, 2023
0 parents commit 71f5c6a
Show file tree
Hide file tree
Showing 11 changed files with 338 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

11 changes: 11 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
source 'https://rubygems.org'

gemspec

logstash_path = ENV["LOGSTASH_PATH"] || "../../logstash"
use_logstash_source = ENV["LOGSTASH_SOURCE"] && ENV["LOGSTASH_SOURCE"].to_s == "1"

if Dir.exist?(logstash_path) && use_logstash_source
gem 'logstash-core', :path => "#{logstash_path}/logstash-core"
gem 'logstash-core-plugin-api', :path => "#{logstash_path}/logstash-core-plugin-api"
end
13 changes: 13 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright (c) 2022 IP2Location.com

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
85 changes: 85 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# IP2Proxy Filter Plugin
This is IP2Proxy filter plugin for Logstash that enables Logstash's users to reverse search of IP address to detect VPN servers, open proxies, web proxies, Tor exit nodes, search engine robots, data center ranges and residential proxies using IP2Proxy BIN database. Other information available includes proxy type, country, state, city, ISP, domain name, usage type, AS number, AS name, threats, last seen date and provider names. The library took the proxy IP address from **IP2Proxy BIN Data** file.

For the methods to use IP2Proxy filter plugin with Elastic Stack (Elasticsearch, Filebeat, Logstash, and Kibana), please take a look on this [tutorial](https://blog.ip2location.com/knowledge-base/how-to-use-ip2proxy-filter-plugin-with-elastic-stack).

*Note: This plugin works in Logstash 7 and Logstash 8.*


## Dependencies (IP2PROXY BIN DATA FILE)
This plugin requires IP2Proxy BIN data file to function. You may download the BIN data file at
* IP2Proxy LITE BIN Data (Free): https://lite.ip2location.com
* IP2Proxy Commercial BIN Data (Commercial): https://www.ip2location.com


## Installation
Install this plugin by the following code:
```
bin/logstash-plugin install logstash-filter-ip2proxy
```


## Config File Example
```
input {
beats {
port => "5043"
}
}
filter {
grok {
match => { "message" => "%{COMBINEDAPACHELOG}"}
}
ip2proxy {
source => "clientip"
}
}
output {
elasticsearch {
hosts => [ "localhost:9200" ]
}
}
```


## IP2Proxy Filter Configuration
|Setting|Input type|Required|
|---|---|---|
|source|string|Yes|
|database|a valid filesystem path|No|
|use_memory_mapped|boolean|No|
|use_cache|boolean|No|
|hide_unsupported_fields|boolean|No|

* **source** field is a required setting that containing the IP address or hostname to get the ip information.
* **database** field is an optional setting that containing the path to the IP2Proxy BIN database file.
* **use_memory_mapped** field is an optional setting that used to allow user to enable the use of memory mapped file. Default value is false.
* **use_cache** field is an optional setting that used to allow user to enable the use of cache. Default value is true.
* **hide_unsupported_fields** field is an optional setting that used to allow user to hide unsupported fields. Default value is false.


## Sample Output
|Field|Description|
|---|---|
|ip2proxy.as|the autonomous system (AS) name of proxy's IP address or domain name|
|ip2proxy.asn|the autonomous system number (ASN) of proxy's IP address or domain name|
|ip2proxy.city|the city name of the proxy|
|ip2proxy.country_long|the ISO3166-1 country name of the proxy|
|ip2proxy.country_short|the ISO3166-1 country code (two-characters) of the proxy|
|ip2proxy.domain|the domain name of proxy's IP address or domain name|
|ip2proxy.is_proxy|Check whether if an IP address was a proxy. Returned value:<ul><li>-1 : errors</li><li>0 : not a proxy</li><li>1 : a proxy</li><li>2 : a data center IP address</li></ul>|
|ip2proxy.isp|the ISP name of the proxy|
|ip2proxy.last_seen|the last seen days ago value of proxy's IP address or domain name|
|ip2proxy.provider|the VPN service provider name if available|
|ip2proxy.proxy_type|the proxy type. Please visit <a href="https://www.ip2location.com/database/px11-ip-proxytype-country-region-city-isp-domain-usagetype-asn-lastseen-threat-residential-provider" target="_blank">IP2Location</a> for the list of proxy types supported|
|ip2proxy.region|the ISO3166-2 region name of the proxy. Please visit <a href="https://www.ip2location.com/free/iso3166-2" target="_blank">ISO3166-2 Subdivision Code</a> for the information of ISO3166-2 supported|
|ip2proxy.thread|the threat type of the proxy|
|ip2proxy.usage_type|the usage type classification of the proxy. Please visit <a href="https://www.ip2location.com/database/px11-ip-proxytype-country-region-city-isp-domain-usagetype-asn-lastseen-threat-residential-provider" target="_blank">IP2Location</a> for the list of usage types supported|


## Support
Email: support@ip2location.com

URL: [https://www.ip2location.com](https://www.ip2location.com)
3 changes: 3 additions & 0 deletions lib/logstash-filter-ip2proxy_jars.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require 'jar_dependencies'
require_jar('com.ip2proxy.ip2proxy', 'ip2proxy', '3.4.0')
require_jar('org.logstash.filters', 'logstash-filter-ip2proxy', '2.3.2')
171 changes: 171 additions & 0 deletions lib/logstash/filters/ip2proxy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
# encoding: utf-8
require "logstash/filters/base"
require "logstash/namespace"

require "logstash-filter-ip2proxy_jars"

class LogStash::Filters::IP2Proxy < LogStash::Filters::Base
config_name "ip2proxy"

# The path to the IP2Proxy.BIN database file which Logstash should use.
# If not specified, this will default to the IP2PROXY-LITE-PX1.BIN database that embedded in the plugin.
config :database, :validate => :path

# The field containing the IP address.
# If this field is an array, only the first value will be used.
config :source, :validate => :string, :required => true

# The field used to define iplocation as target.
config :target, :validate => :string, :default => 'ip2proxy'

# The field used to allow user to enable the use of cache.
config :use_cache, :validate => :boolean, :default => true

# The field used to allow user to enable the use of memory mapped file.
config :use_memory_mapped, :validate => :boolean, :default => false

# The field used to allow user to hide unsupported fields.
config :hide_unsupported_fields, :validate => :boolean, :default => false

# The field used to define the size of the cache. It is not required and the default value is 10 000
config :cache_size, :validate => :number, :required => false, :default => 10_000

public
def register
if @database.nil?
@database = ::Dir.glob(::File.join(::File.expand_path("../../../vendor/", ::File.dirname(__FILE__)),"IP2PROXY-LITE-PX1.BIN")).first

if @database.nil? || !File.exists?(@database)
raise "You must specify 'database => ...' in your ip2proxy filter (I looked for '#{@database}')"
end
end

@logger.info("Using ip2proxy database", :path => @database)

@ip2proxyfilter = org.logstash.filters.IP2ProxyFilter.new(@source, @target, @database, @use_memory_mapped, @hide_unsupported_fields)
end

public
def filter(event)
ip = event.get(@source)

return unless filter?(event)
if @use_cache
if value = IP2ProxyCache.find(event, ip, @ip2proxyfilter, @cache_size).get('ip2proxy')
event.set('ip2proxy', value)
filter_matched(event)
else
tag_iplookup_unsuccessful(event)
end
else
if @ip2proxyfilter.handleEvent(event)
filter_matched(event)
else
tag_iplookup_unsuccessful(event)
end
end
end

def tag_iplookup_unsuccessful(event)
@logger.debug? && @logger.debug("IP #{event.get(@source)} was not found in the database", :event => event)
end

end # class LogStash::Filters::IP2Proxy

class IP2ProxyOrderedHash
ONE = 1

attr_reader :times_queried # ip -> times queried
attr_reader :hash

def initialize
@times_queried = Hash.new(0) # ip -> times queried
@hash = {} # number of hits -> array of ips
end

def add(key)
hash[ONE] ||= []
hash[ONE] << key
times_queried[key] = ONE
end

def reorder(key)
number_of_queries = times_queried[key]

hash[number_of_queries].delete(key)
hash.delete(number_of_queries) if hash[number_of_queries].empty?

hash[number_of_queries + 1] ||= []
hash[number_of_queries + 1] << key
end

def increment(key)
add(key) unless times_queried.has_key?(key)
reorder(key)
times_queried[key] += 1
end

def delete_least_used
first_pile_with_something.shift.tap { |key| times_queried.delete(key) }
end

def first_pile_with_something
hash[hash.keys.min]
end
end

class IP2ProxyCache
ONE_DAY_IN_SECONDS = 86_400

@cache = {} # ip -> event
@timestamps = {} # ip -> time of caching
@times_queried = IP2ProxyOrderedHash.new # ip -> times queried
@mutex = Mutex.new

class << self
attr_reader :cache
attr_reader :timestamps
attr_reader :times_queried

def find(event, ip, filter, cache_size)
synchronize do
if cache.has_key?(ip)
refresh_event(event, ip, filter) if too_old?(ip)
else
if cache_full?(cache_size)
make_room
end
cache_event(event, ip, filter)
end
times_queried.increment(ip)
cache[ip]
end
end

def too_old?(ip)
timestamps[ip] < Time.now - ONE_DAY_IN_SECONDS
end

def make_room
key = times_queried.delete_least_used
cache.delete(key)
timestamps.delete(key)
end

def cache_full?(cache_size)
cache.size >= cache_size
end

def cache_event(event, ip, filter)
filter.handleEvent(event)
cache[ip] = event
timestamps[ip] = Time.now
end

def synchronize(&block)
@mutex.synchronize(&block)
end

alias_method :refresh_event, :cache_event
end
end
25 changes: 25 additions & 0 deletions logstash-filter-ip2proxy.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Gem::Specification.new do |s|

s.name = 'logstash-filter-ip2proxy'
s.version = '2.3.2'
s.licenses = ['Apache-2.0']
s.summary = "Logstash filter IP2Proxy"
s.description = "IP2Proxy filter plugin for Logstash enables Logstash's users to reverse search of IP address to detect VPN servers, open proxies, web proxies, Tor exit nodes, search engine robots, data center ranges and residential proxies using IP2Proxy BIN database."
s.authors = ["IP2Location"]
s.email = 'support@ip2location.com'
s.homepage = "https://www.ip2location.com"
s.require_paths = ["lib", "vendor/jar-dependencies"]

# Files
s.files = Dir["lib/**/*",'spec/**/*',"vendor/**/*","vendor/jar-dependencies/**/*.jar","*.gemspec","*.md","Gemfile","LICENSE"]

# Tests
s.test_files = s.files.grep(%r{^(test|spec|features)/})

# Special flag to let us know this is actually a logstash plugin
s.metadata = { "logstash_plugin" => "true", "logstash_group" => "filter" }

# Gem dependencies
s.add_runtime_dependency "logstash-core-plugin-api", "~> 2.0"
s.add_development_dependency "logstash-devutils"
end
27 changes: 27 additions & 0 deletions spec/filters/ip2proxy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# encoding: utf-8
require_relative '../spec_helper'
require "logstash/filters/ip2proxy"

IP2PROXYDB = ::Dir.glob(::File.expand_path("../../vendor/", ::File.dirname(__FILE__))+"/IP2PROXY-LITE-PX1.BIN").first

describe LogStash::Filters::IP2Proxy do

describe "normal test" do
config <<-CONFIG
filter {
ip2proxy {
source => "ip"
#database => "#{IP2PROXYDB}"
}
}
CONFIG

sample("ip" => "8.8.8.8") do
expect(subject.get("ip2proxy")).not_to be_empty
expect(subject.get("ip2proxy")["country_short"]).to eq("US")
end
end

end

end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# encoding: utf-8
require "logstash/devutils/rspec/spec_helper"
Binary file not shown.
Binary file not shown.

0 comments on commit 71f5c6a

Please sign in to comment.