logstash-filter-public_ip 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8ef725fbabc89b872a939bd726a4f0cb4aa0326966fc40c15132feecfe301a15
4
+ data.tar.gz: a73f1fdc599f715d1d2864140397f5fe83185a4667d9a055938466be3821af1f
5
+ SHA512:
6
+ metadata.gz: 13aac613c69dde633abadd03a6113b9f96466c49e00b7ad94856e110977aeb920ad0f6d742f54f74a40011112478c699b5536cdacfab006d1b479bf3e0a18e5e
7
+ data.tar.gz: 3367379b540aece4c03b7efbd630441ed142314fae44d643e2f8613feaad99d3aeb641506646978448a5dd91f7c735f4cbfba30a47f33446afb12992ed2d39c5
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ ## 1.0.0
2
+ - changed array lookup
3
+ ## 0.1.1
4
+ - fixed bug in cidr logic
5
+ ## 0.1.0
6
+ - Plugin created with the logstash plugin generator
data/CONTRIBUTORS ADDED
@@ -0,0 +1,10 @@
1
+ The following is a list of people who have contributed ideas, code, bug
2
+ reports, or in general have helped logstash along its way.
3
+
4
+ Contributors:
5
+ * -
6
+
7
+ Note: If you've sent us patches, bug reports, or otherwise contributed to
8
+ Logstash, and you aren't on the list above and want to be, please let us know
9
+ and we'll make sure you're here. Contributions from folks like you are what make
10
+ open source awesome.
data/DEVELOPER.md ADDED
@@ -0,0 +1,2 @@
1
+ # logstash-filter-public_ip
2
+ Example filter plugin. This should help bootstrap your effort to write your own filter plugin!
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
data/LICENSE ADDED
@@ -0,0 +1,11 @@
1
+ Licensed under the Apache License, Version 2.0 (the "License");
2
+ you may not use this file except in compliance with the License.
3
+ You may obtain a copy of the License at
4
+
5
+ http://www.apache.org/licenses/LICENSE-2.0
6
+
7
+ Unless required by applicable law or agreed to in writing, software
8
+ distributed under the License is distributed on an "AS IS" BASIS,
9
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ See the License for the specific language governing permissions and
11
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,101 @@
1
+ # Logstash Plugin
2
+
3
+ This is a plugin for [Logstash](https://github.com/elastic/logstash).
4
+
5
+ It is fully free and fully open source. The license is Apache 2.0, meaning you are pretty much free to use it however you want in whatever way.
6
+
7
+ ## Documentation
8
+
9
+ This Logstash filter plugin is used to determine ip version and if the ip address is public.
10
+
11
+ This is a plugin for [Logstash](https://github.com/elastic/logstash).
12
+
13
+ Logstash provides infrastructure to automatically generate documentation for this plugin. We use the asciidoc format to write documentation so any comments in the source code will be first converted into asciidoc and then into html. All plugin documentation are placed under one [central location](http://www.elastic.co/guide/en/logstash/current/).
14
+
15
+ - For formatting code or config example, you can use the asciidoc `[source,ruby]` directive
16
+ - For more asciidoc formatting tips, see the excellent reference here https://github.com/elastic/docs#asciidoc-guide
17
+
18
+ ## Need Help?
19
+
20
+ Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum.
21
+
22
+ ## Developing
23
+
24
+ ### 1. Plugin Developement and Testing
25
+
26
+ #### Code
27
+ - To get started, you'll need JRuby with the Bundler gem installed.
28
+
29
+ - Create a new plugin or clone and existing from the GitHub [logstash-plugins](https://github.com/logstash-plugins) organization. We also provide [example plugins](https://github.com/logstash-plugins?query=example).
30
+
31
+ - Install dependencies
32
+ ```sh
33
+ bundle install
34
+ ```
35
+
36
+ #### Test
37
+
38
+ - Update your dependencies
39
+
40
+ ```sh
41
+ bundle install
42
+ ```
43
+
44
+ - Run tests
45
+
46
+ ```sh
47
+ bundle exec rspec
48
+ ```
49
+
50
+ ### 2. Running your unpublished Plugin in Logstash
51
+
52
+ #### 2.1 Run in a local Logstash clone
53
+
54
+ - Edit Logstash `Gemfile` and add the local plugin path, for example:
55
+ ```ruby
56
+ gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome"
57
+ ```
58
+ - Install plugin
59
+ ```sh
60
+ bin/logstash-plugin install --no-verify
61
+ ```
62
+ - Run Logstash with your plugin
63
+ ```sh
64
+ bin/logstash -e 'filter {awesome {}}'
65
+ ```
66
+ At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.
67
+
68
+ #### 2.2 Run in an installed Logstash
69
+
70
+ You can use the same **2.1** method to run your plugin in an installed Logstash by editing its `Gemfile` and pointing the `:path` to your local plugin development directory or you can build the gem and install it using:
71
+
72
+ - Build your plugin gem
73
+ ```sh
74
+ gem build logstash-filter-awesome.gemspec
75
+ ```
76
+ - Install the plugin from the Logstash home
77
+ ```sh
78
+ bin/logstash-plugin install /your/local/plugin/logstash-filter-awesome.gem
79
+ ```
80
+ - Start Logstash and proceed to test the plugin
81
+
82
+ ## Contributing
83
+
84
+ All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.
85
+
86
+ Programming is not a required skill. Whatever you've seen about open source and maintainers or community members saying "send patches or die" - you will not see that here.
87
+
88
+ It is more important to the community that you are able to contribute.
89
+
90
+ For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/master/CONTRIBUTING.md) file.
91
+
92
+ #### USAGE
93
+ ```
94
+ filter {
95
+ public_ip {
96
+ source => "src_ip"
97
+ target_ipv => "src_ipv"
98
+ target_pub_ip => "src_public_ip"
99
+ }
100
+ }
101
+ ```
@@ -0,0 +1,114 @@
1
+ # encoding: utf-8
2
+ require 'ipaddr'
3
+
4
+ # NON-PUBLIC REASONS
5
+ # ---------------
6
+ # 0.0.0.0/8 = RFC1700: reserved as a source address only
7
+ # 10.0.0.0/8 = RFC1918: reserved for private networking
8
+ # 100.64.0.0/10 = RFC6598: reserved for service provider shared address space but may be used in a manner similiar to RF1918
9
+ # 127.0.0.0/8 = RFC1112: assigned for use as the Internet host loopback address
10
+ # 169.254.0.0/16 = RFC3927: used for link-local addressing in Internet Protocol Version 4
11
+ # 172.16.0.0/12 = RFC1918: reserved for private networking
12
+ # 192.0.0.0/24 = RFC6890: IETF Protocol Assignments
13
+ # 192.0.0.8/32 = RFC7600: IPv4 dummy address
14
+ # 192.0.2.0/24 = RFC5737: Assigned as TEST-NET-1, documentation and examples
15
+ # 192.31.196.0/24 = RFC7535: AS112-v4
16
+ # 192.52.193.0/24 = RFC7450: AMT
17
+ # 192.88.99.0/24 = RFC7526: Reserved. Formerly used for IPv6 to IPv4 relay (included IPv6 address block 2002::/16
18
+ # 192.168.0.0/16 = RFC1918: reserved for private networking
19
+ # 198.18.0.0/15 = RFC2544: Used for benchmark testing of inter-network communications between two separate subnets
20
+ # 198.51.100.0/24 = RFC5737: Assigned as TEST-NET-2, documentation and examples
21
+ # 203.0.113.0/24 = RFC5737: Assigned as TEST-NET-3, documentation and examples
22
+ # 224.0.0.0/4 = RFC1112: In use for IP multicast (Former Class D network)
23
+ # 240.0.0.0/4 = RFC6890: Reserved for future use. (Former Class E network)
24
+ # 255.255.255.255/32 = RFC8190: Reserved for the limited broadcast destination address
25
+ # fc00::/7 = RFC4193: Unique Local Address
26
+ # fe80::/10 = RFC4291: Link-Local Address
27
+ # ff00::/8 = RFC4291: Multicast Address
28
+ # 2001:db8::/32 = RFC3849: Addresses used in documentation and example source code
29
+ # 2001:20::/28 = RFC7343: Prefix for Overlay Routable Cryptographic Hash Identifiers Version 2
30
+ # ::1/128 = RFC8190: Loopback address to local host
31
+ # ::/128 = RFC8190: Unspecified address
32
+ # 100::/64 = RFC6666: Discard Prefix
33
+ # 64:ff9b::/96 = RFC6052: IPv4/IPv6 translation
34
+
35
+ class Ipvlookup
36
+ def initialize(ipaddress)
37
+ @ipaddress = ipaddress
38
+ @cidr = [
39
+ "0.0.0.0/8",
40
+ "10.0.0.0/8",
41
+ "100.64.0.0/10",
42
+ "127.0.0.0/8",
43
+ "169.254.0.0/16",
44
+ "172.16.0.0/12",
45
+ "192.0.0.0/24",
46
+ "192.0.0.8/32",
47
+ "192.0.2.0/24",
48
+ "192.31.196.0/24",
49
+ "192.52.193.0/24",
50
+ "192.88.99.0/24",
51
+ "192.168.0.0/16",
52
+ "198.18.0.0/15",
53
+ "198.51.100.0/24",
54
+ "203.0.113.0/24",
55
+ "224.0.0.0/4",
56
+ "240.0.0.0/4",
57
+ "255.255.255.255/32",
58
+ "fc00::/7",
59
+ "fe80::/10",
60
+ "ff00::/8",
61
+ "2001:db8::/32",
62
+ "2001:20::/28",
63
+ "::1/128",
64
+ "::/128",
65
+ "100::/64",
66
+ "64:ff9b::/96"
67
+ ]
68
+ end
69
+ def validip
70
+ begin
71
+ @src = IPAddr.new("#{@ipaddress}")
72
+ if @src.ipv4?
73
+ @valid = true
74
+ @ipv = '4'
75
+ elsif @src.ipv6?
76
+ @valid = true
77
+ @ipv = '6'
78
+ else
79
+ @valid = false
80
+ end
81
+ rescue
82
+ @valid = false
83
+ end
84
+ return @valid
85
+ end
86
+ def ipversion
87
+ if @valid == true
88
+ return "#{@ipv}"
89
+ end
90
+ end
91
+ def pubip
92
+ if @valid == true
93
+ prvip = @cidr.collect{|p|
94
+ cnet = IPAddr.new(p)
95
+ cnet.include?(@src)
96
+ }
97
+ if prvip.include? true
98
+ public_ip = false
99
+ else
100
+ public_ip = true
101
+ end
102
+ return public_ip
103
+
104
+ #public_ip = true
105
+ #for p in @cidr do
106
+ # cidr = IPAddr.new(p)
107
+ # if cidr.include?(@src) == true
108
+ # public_ip = false
109
+ # end
110
+ #end
111
+ #return public_ip
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,61 @@
1
+ # encoding: utf-8
2
+ require "logstash/filters/base"
3
+ require "logstash/filters/public_ip"
4
+ require "logstash/filters/ipvlookup"
5
+
6
+ # This filter will replace the contents of the default
7
+ # message field with whatever you specify in the configuration.
8
+ #
9
+ # It is only intended to be used as an .
10
+ class LogStash::Filters::PublicIp < LogStash::Filters::Base
11
+
12
+ # Setting the config_name here is required. This is how you
13
+ # configure this filter from your Logstash config.
14
+ #
15
+ # filter {
16
+ # public_ip {
17
+ # source => "src_ip"
18
+ # target_ipv => "src_ipv"
19
+ # target_pub_ip => "src_public_ip"
20
+ # }
21
+ # }
22
+ #
23
+ config_name "public_ip"
24
+
25
+ # Field with source ip address
26
+ config :source, :validate => :string, :required => true
27
+ # target field to put ip vesrion data
28
+ config :target_ipv, :validate => :string, :default => "ip_version"
29
+ # target field to put ip vesrion data
30
+ config :target_pub_ip, :validate => :string, :default => "public_ip"
31
+ # tag invalid ip
32
+ config :tag_on_invalid_ip, :validate => :array, :default => ["_invalid_ip"]
33
+
34
+ public
35
+ def register
36
+ end
37
+
38
+
39
+ public
40
+ def filter(event)
41
+ ip = event.get(@source)
42
+ lookup = Ipvlookup.new(ip)
43
+ validip = lookup.validip
44
+ if validip == true
45
+ ipv = lookup.ipversion
46
+ event.set("#{@target_ipv}", ipv)
47
+ pubip = lookup.pubip
48
+ event.set("#{@target_pub_ip}", pubip)
49
+ end
50
+ # Tag event if invalid ip
51
+ tag_invalid_ip(event) if validip == false
52
+ # filter_matched should go in the last line of our successful code
53
+ filter_matched(event) if validip == true
54
+ end # def filter
55
+
56
+ def tag_invalid_ip(event)
57
+ @logger.debug? && @logger.debug("Invalid IP #{event.get(@source)}", :event => event)
58
+ @tag_on_invalid_ip.each{|tag| event.tag(tag)}
59
+ end
60
+
61
+ end # class LogStash::Filters::PublicIp
@@ -0,0 +1,23 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-filter-public_ip'
3
+ s.version = '1.0.0'
4
+ s.licenses = ['Apache-2.0']
5
+ s.summary = 'IP version and Public check'
6
+ s.description = 'logstash plugin that checks ip version and if the ip is public'
7
+ s.homepage = 'https://github.com/Vigilant-LLC/logstash-filter-public_ip'
8
+ s.authors = ['Mike Pananen']
9
+ s.email = 'panaman@geekempire.com'
10
+ s.require_paths = ['lib']
11
+
12
+ # Files
13
+ s.files = Dir['lib/**/*','spec/**/*','vendor/**/*','*.gemspec','*.md','CONTRIBUTORS','Gemfile','LICENSE','NOTICE.TXT']
14
+ # Tests
15
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
16
+
17
+ # Special flag to let us know this is actually a logstash plugin
18
+ s.metadata = { "logstash_plugin" => "true", "logstash_group" => "filter" }
19
+
20
+ # Gem dependencies
21
+ s.add_runtime_dependency "logstash-core-plugin-api", "~> 2.0"
22
+ s.add_development_dependency 'logstash-devutils', '= 1.3.6'
23
+ end
@@ -0,0 +1,74 @@
1
+ # encoding: utf-8
2
+ require_relative '../spec_helper'
3
+ require "logstash/filters/public_ip"
4
+
5
+ describe LogStash::Filters::PublicIp do
6
+ describe "ipversion" do
7
+ let(:config) do <<-CONFIG
8
+ filter {
9
+ public_ip {
10
+ source => "ip"
11
+ target_ipv => "ipv"
12
+ target_pub_ip => "pubip"
13
+ }
14
+ }
15
+ CONFIG
16
+ end
17
+
18
+ sample("ip" => "10.0.0.1") do
19
+ expect(subject.get("ipv")).to eq('4')
20
+ end
21
+ end
22
+
23
+ describe "invalid ip" do
24
+ let(:config) do <<-CONFIG
25
+ filter {
26
+ public_ip {
27
+ source => "ip"
28
+ target_ipv => "ipv"
29
+ target_pub_ip => "pubip"
30
+ }
31
+ }
32
+ CONFIG
33
+ end
34
+
35
+ sample("ip" => "10000.0.0.1") do
36
+ expect(subject.get("tags")).to include("_invalid_ip")
37
+ end
38
+ end
39
+
40
+ describe "ipv4 pubip false" do
41
+ let(:config) do <<-CONFIG
42
+ filter {
43
+ public_ip {
44
+ source => "ip"
45
+ target_ipv => "ipv"
46
+ target_pub_ip => "pubip"
47
+ }
48
+ }
49
+ CONFIG
50
+ end
51
+
52
+ sample("ip" => "10.0.0.1") do
53
+ expect(subject.get("pubip")).to eq(false)
54
+ end
55
+ end
56
+
57
+ describe "ipv6 pubip true" do
58
+ let(:config) do <<-CONFIG
59
+ filter {
60
+ public_ip {
61
+ source => "src_ip"
62
+ target_ipv => "[src][ipv]"
63
+ target_pub_ip => "[src][pubip]"
64
+ }
65
+ }
66
+ CONFIG
67
+ end
68
+
69
+ sample("src_ip" => "2606:4700:4700::1001") do
70
+ expect(subject.get("[src][pubip]")).to eq(true)
71
+ end
72
+ end
73
+
74
+ end
@@ -0,0 +1,2 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-filter-public_ip
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Mike Pananen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-01-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '2.0'
19
+ name: logstash-core-plugin-api
20
+ prerelease: false
21
+ type: :runtime
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - '='
31
+ - !ruby/object:Gem::Version
32
+ version: 1.3.6
33
+ name: logstash-devutils
34
+ prerelease: false
35
+ type: :development
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.3.6
41
+ description: logstash plugin that checks ip version and if the ip is public
42
+ email: panaman@geekempire.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - CHANGELOG.md
48
+ - CONTRIBUTORS
49
+ - DEVELOPER.md
50
+ - Gemfile
51
+ - LICENSE
52
+ - README.md
53
+ - lib/logstash/filters/ipvlookup.rb
54
+ - lib/logstash/filters/public_ip.rb
55
+ - logstash-filter-public_ip.gemspec
56
+ - spec/filters/public_ip_spec.rb
57
+ - spec/spec_helper.rb
58
+ homepage: https://github.com/Vigilant-LLC/logstash-filter-public_ip
59
+ licenses:
60
+ - Apache-2.0
61
+ metadata:
62
+ logstash_plugin: 'true'
63
+ logstash_group: filter
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.7.6
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: IP version and Public check
84
+ test_files:
85
+ - spec/filters/public_ip_spec.rb
86
+ - spec/spec_helper.rb