├── .gitignore ├── .travis.yml ├── Rakefile ├── NOTICE.TXT ├── .github ├── PULL_REQUEST_TEMPLATE.md ├── ISSUE_TEMPLATE.md └── CONTRIBUTING.md ├── examples ├── ping-to-stdout │ └── logstash.conf └── elasticsearch │ ├── logstash_nmap.conf │ └── elasticsearch_nmap_template.json ├── Gemfile ├── spec ├── fixtures │ ├── ip_down.xml │ ├── nothingup.xml │ ├── pingsweep.xml │ ├── scanme.nmap.org │ ├── traceroutes.xml │ ├── scanme_traceroute.xml │ ├── ipv6_all.xml │ ├── scanme_A.xml │ ├── full_scan.xml │ └── localscan.xml └── codecs │ └── nmap_spec.rb ├── CHANGELOG.md ├── logstash-codec-nmap.gemspec ├── docs └── index.asciidoc ├── README.md ├── lib └── logstash │ └── codecs │ └── nmap.rb └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | Gemfile.lock 3 | .bundle 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | import: 2 | - logstash-plugins/.ci:travis/travis.yml@1.x -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | @files=[] 2 | 3 | task :default do 4 | system("rake -T") 5 | end 6 | 7 | require "logstash/devutils/rake" 8 | -------------------------------------------------------------------------------- /NOTICE.TXT: -------------------------------------------------------------------------------- 1 | Elasticsearch 2 | Copyright 2012-2015 Elasticsearch 3 | 4 | This product includes software developed by The Apache Software 5 | Foundation (http://www.apache.org/). -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thanks for contributing to Logstash! If you haven't already signed our CLA, here's a handy link: https://www.elastic.co/contributor-agreement/ 2 | -------------------------------------------------------------------------------- /examples/ping-to-stdout/logstash.conf: -------------------------------------------------------------------------------- 1 | input { 2 | http { 3 | host => "127.0.0.1" 4 | port => 8000 5 | codec => nmap 6 | } 7 | } 8 | 9 | output { 10 | stdout { 11 | codec => rubydebug 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gemspec 4 | 5 | logstash_path = ENV["LOGSTASH_PATH"] || "../../logstash" 6 | use_logstash_source = ENV["LOGSTASH_SOURCE"] && ENV["LOGSTASH_SOURCE"].to_s == "1" 7 | 8 | if Dir.exist?(logstash_path) && use_logstash_source 9 | gem 'logstash-core', :path => "#{logstash_path}/logstash-core" 10 | gem 'logstash-core-plugin-api', :path => "#{logstash_path}/logstash-core-plugin-api" 11 | end 12 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Please post all product and debugging questions on our [forum](https://discuss.elastic.co/c/logstash). Your questions will reach our wider community members there, and if we confirm that there is a bug, then we can open a new issue here. 2 | 3 | For all general issues, please provide the following details for fast resolution: 4 | 5 | - Version: 6 | - Operating System: 7 | - Config File (if you have sensitive info, please remove it): 8 | - Sample Data: 9 | - Steps to Reproduce: 10 | -------------------------------------------------------------------------------- /spec/fixtures/ip_down.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /spec/fixtures/nothingup.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 0.0.22 2 | - [DOC] Fixed typo: changed `namp` to `nmap` [#32](https://github.com/logstash-plugins/logstash-codec-nmap/pull/32) 3 | 4 | ## 0.0.21 5 | - Update gemspec summary 6 | 7 | ## 0.0.20 8 | - Fix some documentation issues 9 | 10 | # 0.0.18 11 | - Fix bug in 0.0.17 that prevented this from working with LS 5.x 12 | # 0.0.17 13 | - Expand logstash-core-api constraints to allow for 5.2 functionality 14 | # 0.0.16 15 | - Pin ruby-nmap version to 0.8.0 to avoid needing ruby 2.0+ 16 | # 0.0.15 17 | - Depend on logstash-core-plugin-api instead of logstash-core, removing the need to mass update plugins on major releases of logstash 18 | # 0.0.14 19 | - New dependency requirements for logstash-core for the 5.0 release 20 | ## 0.0.13 21 | - Actually include 'times' element 22 | ## 0.0.12 23 | - Improve mapping examples 24 | - Fix IDs for nmap_scan_metadata 25 | ## 0.0.11 26 | - Add start/end times for nmap_scan_metadata documents 27 | ## 0.0.10 28 | - Add top level metadata object 29 | - Improve examples 30 | -------------------------------------------------------------------------------- /examples/elasticsearch/logstash_nmap.conf: -------------------------------------------------------------------------------- 1 | input { 2 | http { 3 | port => 8000 4 | codec => nmap 5 | tags => [nmap] 6 | } 7 | } 8 | 9 | filter { 10 | if "nmap" in [tags] { 11 | # Don't emit documents for 'down' hosts 12 | if [status][state] == "down" { 13 | drop {} 14 | } 15 | 16 | mutate { 17 | # Drop HTTP headers and logstash server hostname 18 | remove_field => ["headers", "hostname"] 19 | } 20 | 21 | if "nmap_traceroute_link" == [type] { 22 | geoip { 23 | source => "[to][address]" 24 | target => "[to][geoip]" 25 | } 26 | 27 | geoip { 28 | source => "[from][address]" 29 | target => "[from][geoip]" 30 | } 31 | } 32 | 33 | if [ipv4] { 34 | geoip { 35 | source => ipv4 36 | target => geoip 37 | } 38 | } 39 | 40 | } 41 | } 42 | 43 | output { 44 | if "nmap" in [tags] { 45 | elasticsearch { 46 | document_type => "%{[type]}" 47 | document_id => "%{[id]}" 48 | # Nmap data usually isn't too bad, so monthly rotation should be fine 49 | index => "nmap-logstash-%{+YYYY.MM}" 50 | template => "./elasticsearch_nmap_template.json" 51 | template_name => "logstash_nmap" 52 | } 53 | 54 | stdout { 55 | codec => json_lines 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /logstash-codec-nmap.gemspec: -------------------------------------------------------------------------------- 1 | Gem::Specification.new do |s| 2 | 3 | s.name = 'logstash-codec-nmap' 4 | s.version = '0.0.22' 5 | s.licenses = ['Apache License (2.0)'] 6 | s.summary = "Reads Nmap data in XML format" 7 | s.description = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program" 8 | s.authors = ["Elastic"] 9 | s.email = 'info@elastic.co' 10 | s.homepage = "http://www.elastic.co/guide/en/logstash/current/index.html" 11 | s.require_paths = ["lib"] 12 | 13 | # Files 14 | s.files = Dir["lib/**/*","spec/**/*","*.gemspec","*.md","CONTRIBUTORS","Gemfile","LICENSE","NOTICE.TXT", "vendor/jar-dependencies/**/*.jar", "vendor/jar-dependencies/**/*.rb", "VERSION", "docs/**/*"] 15 | 16 | # Tests 17 | s.test_files = s.files.grep(%r{^(test|spec|features)/}) 18 | 19 | # Special flag to let us know this is actually a logstash plugin 20 | s.metadata = { "logstash_plugin" => "true", "logstash_group" => "codec" } 21 | 22 | # Gem dependencies 23 | s.add_runtime_dependency "logstash-core-plugin-api", ">= 1.60", "<= 2.99" 24 | s.add_runtime_dependency 'ruby-nmap', "~> 0.8.0" 25 | 26 | s.add_development_dependency 'logstash-devutils' 27 | s.add_development_dependency 'insist' 28 | end 29 | -------------------------------------------------------------------------------- /spec/fixtures/pingsweep.xml: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /spec/codecs/nmap_spec.rb: -------------------------------------------------------------------------------- 1 | require "logstash/devutils/rspec/spec_helper" 2 | require "logstash/codecs/nmap" 3 | require "logstash/event" 4 | require "insist" 5 | 6 | describe LogStash::Codecs::Nmap do 7 | context "#decode" do 8 | subject do 9 | events = [] 10 | LogStash::Codecs::Nmap.new.decode(xml_string) do |event| 11 | events << event 12 | end 13 | events 14 | end 15 | 16 | shared_examples_for "a valid parse" do 17 | it "should decode without error" do 18 | expect(subject).to be_a(Array) 19 | end 20 | 21 | it "should encode at least one thing" do 22 | expect(subject.length > 0).to eql(true) 23 | end 24 | 25 | it "should encode the output as LogStash::Event objects" do 26 | subject.each do |event| 27 | expect(event).to be_a(LogStash::Event) 28 | end 29 | end 30 | 31 | let(:ids) { subject.map {|e| e.get("id") } } 32 | it "should add a unique id field to all events" do 33 | expect(ids).to eql(ids.uniq) 34 | end 35 | 36 | it "should not have any null id fields" do 37 | expect(ids.include?(nil)).to be_falsey 38 | end 39 | end 40 | 41 | describe "parsing traceroutes" do 42 | let(:xml_string) { File.open("spec/fixtures/traceroutes.xml").read } 43 | it_should_behave_like "a valid parse" 44 | end 45 | 46 | # This is broken until https://github.com/sophsec/ruby-nmap/pull/40 is accepted 47 | # describe "parsing ipv6" do 48 | # let(:xml_string) { File.open("spec/fixtures/ipv6_all.xml").read } 49 | 50 | # it_should_behave_like "a valid parse" 51 | # end 52 | 53 | describe "parsing pingsweeps" do 54 | let(:xml_string) { File.open("spec/fixtures/pingsweep.xml").read } 55 | it_should_behave_like "a valid parse" 56 | end 57 | 58 | describe "localscan.xml" do 59 | let(:xml_string) { File.open("spec/fixtures/localscan.xml").read } 60 | it_should_behave_like "a valid parse" 61 | end 62 | 63 | describe "scanme_A.xml" do 64 | let(:xml_string) { File.open("spec/fixtures/scanme_A.xml").read } 65 | it_should_behave_like "a valid parse" 66 | end 67 | 68 | describe "full_scan.xml" do 69 | let(:xml_string) { File.open("spec/fixtures/full_scan.xml").read } 70 | it_should_behave_like "a valid parse" 71 | end 72 | 73 | describe "nothingup.xml" do 74 | let(:xml_string) { File.open("spec/fixtures/nothingup.xml").read } 75 | it_should_behave_like "a valid parse" 76 | end 77 | 78 | describe "ip_down.xml" do 79 | let(:xml_string) { File.open("spec/fixtures/ip_down.xml").read } 80 | it_should_behave_like "a valid parse" 81 | end 82 | 83 | end 84 | 85 | end 86 | -------------------------------------------------------------------------------- /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to Logstash 2 | 3 | All contributions are welcome: ideas, patches, documentation, bug reports, 4 | complaints, etc! 5 | 6 | Programming is not a required skill, and there are many ways to help out! 7 | It is more important to us that you are able to contribute. 8 | 9 | That said, some basic guidelines, which you are free to ignore :) 10 | 11 | ## Want to learn? 12 | 13 | Want to lurk about and see what others are doing with Logstash? 14 | 15 | * The irc channel (#logstash on irc.freenode.org) is a good place for this 16 | * The [forum](https://discuss.elastic.co/c/logstash) is also 17 | great for learning from others. 18 | 19 | ## Got Questions? 20 | 21 | Have a problem you want Logstash to solve for you? 22 | 23 | * You can ask a question in the [forum](https://discuss.elastic.co/c/logstash) 24 | * Alternately, you are welcome to join the IRC channel #logstash on 25 | irc.freenode.org and ask for help there! 26 | 27 | ## Have an Idea or Feature Request? 28 | 29 | * File a ticket on [GitHub](https://github.com/elastic/logstash/issues). Please remember that GitHub is used only for issues and feature requests. If you have a general question, the [forum](https://discuss.elastic.co/c/logstash) or IRC would be the best place to ask. 30 | 31 | ## Something Not Working? Found a Bug? 32 | 33 | If you think you found a bug, it probably is a bug. 34 | 35 | * If it is a general Logstash or a pipeline issue, file it in [Logstash GitHub](https://github.com/elasticsearch/logstash/issues) 36 | * If it is specific to a plugin, please file it in the respective repository under [logstash-plugins](https://github.com/logstash-plugins) 37 | * or ask the [forum](https://discuss.elastic.co/c/logstash). 38 | 39 | # Contributing Documentation and Code Changes 40 | 41 | If you have a bugfix or new feature that you would like to contribute to 42 | logstash, and you think it will take more than a few minutes to produce the fix 43 | (ie; write code), it is worth discussing the change with the Logstash users and developers first! You can reach us via [GitHub](https://github.com/elastic/logstash/issues), the [forum](https://discuss.elastic.co/c/logstash), or via IRC (#logstash on freenode irc) 44 | Please note that Pull Requests without tests will not be merged. If you would like to contribute but do not have experience with writing tests, please ping us on IRC/forum or create a PR and ask our help. 45 | 46 | ## Contributing to plugins 47 | 48 | Check our [documentation](https://www.elastic.co/guide/en/logstash/current/contributing-to-logstash.html) on how to contribute to plugins or write your own! It is super easy! 49 | 50 | ## Contribution Steps 51 | 52 | 1. Test your changes! [Run](https://github.com/elastic/logstash#testing) the test suite 53 | 2. Please make sure you have signed our [Contributor License 54 | Agreement](https://www.elastic.co/contributor-agreement/). We are not 55 | asking you to assign copyright to us, but to give us the right to distribute 56 | your code without restriction. We ask this of all contributors in order to 57 | assure our users of the origin and continuing existence of the code. You 58 | only need to sign the CLA once. 59 | 3. Send a pull request! Push your changes to your fork of the repository and 60 | [submit a pull 61 | request](https://help.github.com/articles/using-pull-requests). In the pull 62 | request, describe what your changes do and mention any bugs/issues related 63 | to the pull request. 64 | 65 | 66 | -------------------------------------------------------------------------------- /docs/index.asciidoc: -------------------------------------------------------------------------------- 1 | :plugin: nmap 2 | :type: codec 3 | 4 | /////////////////////////////////////////// 5 | START - GENERATED VARIABLES, DO NOT EDIT! 6 | /////////////////////////////////////////// 7 | :version: %VERSION% 8 | :release_date: %RELEASE_DATE% 9 | :changelog_url: %CHANGELOG_URL% 10 | :include_path: ../../../../logstash/docs/include 11 | /////////////////////////////////////////// 12 | END - GENERATED VARIABLES, DO NOT EDIT! 13 | /////////////////////////////////////////// 14 | 15 | [id="plugins-{type}s-{plugin}"] 16 | 17 | === Nmap codec plugin 18 | 19 | include::{include_path}/plugin_header.asciidoc[] 20 | 21 | ==== Description 22 | 23 | This codec is used to parse https://nmap.org/[nmap] output data which is serialized in XML format. Nmap ("Network Mapper") is a free and open source utility for network discovery and security auditing. 24 | For more information on nmap, see https://nmap.org/. 25 | 26 | This codec can only be used for decoding data. 27 | 28 | Event types are listed below 29 | 30 | `nmap_scan_metadata`: An object containing top level information about the scan, including how many hosts were up, and how many were down. Useful for the case where you need to check if a DNS based hostname does not resolve, where both those numbers will be zero. 31 | `nmap_host`: One event is created per host. The full data covering an individual host, including open ports and traceroute information as a nested structure. 32 | `nmap_port`: One event is created per host/port. This duplicates data already in `nmap_host`: This was put in for the case where you want to model ports as separate documents in Elasticsearch (which Kibana prefers). 33 | `nmap_traceroute_link`: One of these is output per traceroute 'connection', with a `from` and a `to` object describing each hop. Note that traceroute hop data is not always correct due to the fact that each tracing ICMP packet may take a different route. Also very useful for Kibana visualizations. 34 | 35 | [id="plugins-{type}s-{plugin}-options"] 36 | ==== Nmap Codec Configuration Options 37 | 38 | [cols="<,<,<",options="header",] 39 | |======================================================================= 40 | |Setting |Input type|Required 41 | | <> |<>|No 42 | | <> |<>|No 43 | | <> |<>|No 44 | | <> |<>|No 45 | |======================================================================= 46 | 47 |   48 | 49 | [id="plugins-{type}s-{plugin}-emit_hosts"] 50 | ===== `emit_hosts` 51 | 52 | * Value type is <> 53 | * Default value is `true` 54 | 55 | Emit all host data as a nested document (including ports + traceroutes) with the type 'nmap_fullscan' 56 | 57 | [id="plugins-{type}s-{plugin}-emit_ports"] 58 | ===== `emit_ports` 59 | 60 | * Value type is <> 61 | * Default value is `true` 62 | 63 | Emit each port as a separate document with type 'nmap_port' 64 | 65 | [id="plugins-{type}s-{plugin}-emit_scan_metadata"] 66 | ===== `emit_scan_metadata` 67 | 68 | * Value type is <> 69 | * Default value is `true` 70 | 71 | Emit scan metadata 72 | 73 | [id="plugins-{type}s-{plugin}-emit_traceroute_links"] 74 | ===== `emit_traceroute_links` 75 | 76 | * Value type is <> 77 | * Default value is `true` 78 | 79 | Emit each hop_tuple of the traceroute with type 'nmap_traceroute_link' 80 | 81 | 82 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Logstash Plugin 2 | 3 | [![Travis Build Status](https://travis-ci.com/logstash-plugins/logstash-codec-nmap.svg)](https://travis-ci.com/logstash-plugins/logstash-codec-nmap) 4 | 5 | This is a plugin for [Logstash](https://github.com/elastic/logstash). 6 | 7 | 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. 8 | 9 | ## Documentation 10 | 11 | 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/). 12 | 13 | - For formatting code or config example, you can use the asciidoc `[source,ruby]` directive 14 | - For more asciidoc formatting tips, see the excellent reference here https://github.com/elastic/docs#asciidoc-guide 15 | 16 | ## Need Help? 17 | 18 | Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum. 19 | 20 | ## Developing 21 | 22 | ### 1. Plugin Developement and Testing 23 | 24 | #### Code 25 | - To get started, you'll need JRuby with the Bundler gem installed. 26 | 27 | - 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). 28 | 29 | - Install dependencies 30 | ```sh 31 | bundle install 32 | ``` 33 | 34 | #### Test 35 | 36 | - Update your dependencies 37 | 38 | ```sh 39 | bundle install 40 | ``` 41 | 42 | - Run tests 43 | 44 | ```sh 45 | bundle exec rspec 46 | ``` 47 | 48 | ### 2. Running your unpublished Plugin in Logstash 49 | 50 | #### 2.1 Run in a local Logstash clone 51 | 52 | - Edit Logstash `Gemfile` and add the local plugin path, for example: 53 | ```ruby 54 | gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome" 55 | ``` 56 | - Install plugin 57 | ```sh 58 | # Logstash 2.3 and higher 59 | bin/logstash-plugin install --no-verify 60 | 61 | # Prior to Logstash 2.3 62 | bin/plugin install --no-verify 63 | 64 | ``` 65 | - Run Logstash with your plugin 66 | ```sh 67 | bin/logstash -e 'filter {awesome {}}' 68 | ``` 69 | At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash. 70 | 71 | #### 2.2 Run in an installed Logstash 72 | 73 | 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: 74 | 75 | - Build your plugin gem 76 | ```sh 77 | gem build logstash-filter-awesome.gemspec 78 | ``` 79 | - Install the plugin from the Logstash home 80 | ```sh 81 | # Logstash 2.3 and higher 82 | bin/logstash-plugin install --no-verify 83 | 84 | # Prior to Logstash 2.3 85 | bin/plugin install --no-verify 86 | 87 | ``` 88 | - Start Logstash and proceed to test the plugin 89 | 90 | ## Contributing 91 | 92 | All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin. 93 | 94 | 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. 95 | 96 | It is more important to the community that you are able to contribute. 97 | 98 | For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/master/CONTRIBUTING.md) file. 99 | -------------------------------------------------------------------------------- /spec/fixtures/scanme.nmap.org: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /spec/fixtures/traceroutes.xml: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /spec/fixtures/scanme_traceroute.xml: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /spec/fixtures/ipv6_all.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 120 | 121 | 122 | cpe:/a:apple:itunes:12.3.2.35 123 | 124 | 131 | 132 | 133 | 134 | 135 | -------------------------------------------------------------------------------- /spec/fixtures/scanme_A.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | cpe:/a:openbsd:openssh:6.6.1p1cpe:/o:linux:linux_kernel 37 | 38 | cpe:/a:apache:http_server:2.4.7 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | cpe:/o:linux:linux_kernel:3 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /lib/logstash/codecs/nmap.rb: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | require "logstash/codecs/base" 3 | require "nmap/xml" 4 | require 'securerandom' 5 | 6 | # This codec is used to parse https://nmap.org/[namp] output data which is serialized in XML format. Nmap ("Network Mapper") is a free and open source utility for network discovery and security auditing. 7 | # For more information on nmap, see https://nmap.org/. 8 | # 9 | # This codec can only be used for decoding data. 10 | # 11 | # Event types are listed below 12 | # 13 | # `nmap_scan_metadata`: An object containing top level information about the scan, including how many hosts were up, and how many were down. Useful for the case where you need to check if a DNS based hostname does not resolve, where both those numbers will be zero. 14 | # `nmap_host`: One event is created per host. The full data covering an individual host, including open ports and traceroute information as a nested structure. 15 | # `nmap_port`: One event is created per host/port. This duplicates data already in `nmap_host`: This was put in for the case where you want to model ports as separate documents in Elasticsearch (which Kibana prefers). 16 | # `nmap_traceroute_link`: One of these is output per traceroute 'connection', with a `from` and a `to` object describing each hop. Note that traceroute hop data is not always correct due to the fact that each tracing ICMP packet may take a different route. Also very useful for Kibana visualizations. 17 | 18 | class LogStash::Codecs::Nmap < LogStash::Codecs::Base 19 | config_name "nmap" 20 | 21 | # Emit scan metadata 22 | config :emit_scan_metadata, :validate => :boolean, :default => true 23 | # Emit all host data as a nested document (including ports + traceroutes) with the type 'nmap_fullscan' 24 | config :emit_hosts, :validate => :boolean, :default => true 25 | # Emit each port as a separate document with type 'nmap_port' 26 | config :emit_ports, :validate => :boolean, :default => true 27 | # Emit each hop_tuple of the traceroute with type 'nmap_traceroute_link' 28 | config :emit_traceroute_links, :validate => :boolean, :default => true 29 | 30 | public 31 | def register 32 | end 33 | 34 | public 35 | def decode(data) 36 | xml = Nmap::XML.parse(data) 37 | scan_id = SecureRandom.uuid 38 | 39 | base = {} 40 | base['arguments'] = xml.scanner.arguments 41 | base['version'] = xml.scanner.version 42 | base['scan_id'] = scan_id 43 | 44 | # This really needs to be put into ruby-nmap 45 | scan_host_stats = Hash[xml.instance_variable_get(:@doc).xpath('/nmaprun[@scanner="nmap"]/runstats/hosts').first.attributes.map {|k,v| [k,v.value.to_i]}] 46 | 47 | finished_info = Hash[xml.instance_variable_get(:@doc).xpath('/nmaprun[@scanner="nmap"]/runstats/finished').first.attributes.map {|k,v| [k,v.value] }] 48 | finished_info["elapsed"] = finished_info["elapsed"].to_f 49 | finished_info["time"] = timeify(Time.at(finished_info["time"].to_i)) 50 | 51 | run_stats = hashify_struct(xml.run_stats.first) 52 | run_stats["finished"] = finished_info 53 | 54 | if @emit_scan_metadata 55 | yield LogStash::Event.new(base.merge({ 56 | 'id' => scan_id, 57 | 'type' => 'nmap_scan_metadata', 58 | 'host_stats' => scan_host_stats, 59 | 'start_time' => timeify(xml.scanner.start_time), 60 | 'end_time' => run_stats["finished"]["time"], 61 | 'run_stats' => hashify_run_stats(xml.run_stats.first) 62 | })) 63 | end 64 | 65 | xml.hosts.each_with_index do |host,idx| 66 | # Convert the host to a 'host_base' host event 67 | # This will be used for the later port/hop types 68 | host_base = hashify_host(host, xml).merge(base) 69 | 70 | 71 | # Pull out the detail 72 | ports = host.ports.map {|p| hashify_port(p)} 73 | traceroute = hashify_traceroute(host.traceroute) 74 | scan_host_id = scan_id + "-h#{idx}" 75 | 76 | if @emit_ports && ports 77 | ports.each.with_index do |port,idx| 78 | yield LogStash::Event.new(host_base.merge( 79 | 'type' => 'nmap_port', 80 | 'port' => port, 81 | 'scan_host_id' => scan_host_id, 82 | 'id' => scan_host_id+"-p#{idx}" 83 | )) 84 | end 85 | end 86 | 87 | if @emit_traceroute_links && traceroute && (hops = traceroute['hops']) 88 | hops.each_with_index do |hop,idx| 89 | next_hop = hops[idx+1] 90 | yield LogStash::Event.new(host_base.merge( 91 | 'type' =>'nmap_traceroute_link', 92 | 'from' => hop, 93 | 'to' => next_hop, 94 | 'rtt_diff' => (next_hop ? next_hop['rtt'] - hop['rtt'] : nil), 95 | 'scan_host_id' => scan_host_id, 96 | 'id' => scan_host_id+"-tl#{idx}" 97 | )) 98 | end 99 | end 100 | 101 | if @emit_hosts 102 | yield LogStash::Event.new(host_base.merge( 103 | 'type' => 'nmap_host', 104 | 'ports' => ports, 105 | 'traceroute' => traceroute, 106 | 'id' => scan_host_id 107 | )) 108 | end 109 | end 110 | rescue StandardError => e 111 | raise e 112 | @logger.warn("An unexpected error occurred parsing nmap XML", 113 | :input => data, 114 | :message => e.message, 115 | :class => e.class.name, 116 | :backtrace => e.backtrace) 117 | end 118 | 119 | def hashify_host(host, xml) 120 | scan_start = timeify(xml.scanner.start_time) 121 | 122 | h = {} 123 | h['start_time'] = timeify(host.start_time, scan_start) 124 | h['end_time'] = timeify(host.end_time, scan_start) 125 | 126 | # Needs to be pached in ruby-nmap 127 | times = host.instance_variable_get(:@node).xpath("times").first 128 | h['times'] = Hash[times.first.map {|k,v| [k,v.to_i]}] if times 129 | 130 | # These two are actually different. 131 | # Address may contain a MAC, addresses will not AFAICT 132 | h['addresses'] = hashify_structs(host.addresses) 133 | h['address'] = host.address # str 134 | 135 | h['ip'] = host.ip # str 136 | h['ipv4'] = host.ipv4 # str 137 | h['ipv6'] = host.ipv6 # str 138 | h['mac'] = host.mac # str 139 | h['status'] = hashify_status(host.status) 140 | h['hostname'] = hashify_hostname(host.hostname) 141 | h['uptime'] = hashify_uptime(host.uptime) 142 | h['os'] = hashify_os(host.os) 143 | 144 | h 145 | end 146 | 147 | def hashify_run_stats(run_stats) 148 | h = hashify_struct(run_stats) 149 | h["elapsed"] = h["elapsed"].to_f 150 | h 151 | end 152 | 153 | def hashify_status(status) 154 | return unless status 155 | 156 | { 157 | 'state' => status.state.to_s, # str 158 | 'reason' => status.reason # str 159 | } 160 | end 161 | 162 | def hashify_hostname(hostname) 163 | return unless hostname 164 | 165 | { 166 | 'name' => hostname.name, # str 167 | 'type' => hostname.type, # str 168 | } 169 | end 170 | 171 | def hashify_os(os) 172 | return unless os 173 | 174 | # we need this nil guard here till https://github.com/sophsec/ruby-nmap/pull/41 is accepted 175 | fingerprint = os.fingerprint rescue nil 176 | { 177 | 'ports_used' => os.ports_used, 178 | 'fingerprint' => fingerprint, 179 | 'classes' => hashify_os_classes(os.classes), 180 | 'matches' => hashify_structs(os.matches) 181 | } 182 | end 183 | 184 | def hashify_os_classes(classes) 185 | return if !classes || classes.empty? 186 | 187 | classes.map do |klass| 188 | { 189 | 'type' => klass.type.to_s, # returned as sym originally 190 | 'vendor' => klass.vendor.to_s, 191 | 'family' => klass.family.to_s, 192 | 'gen' => klass.gen.to_s, 193 | 'accuracy' => klass.accuracy # int 194 | } 195 | end 196 | end 197 | 198 | def hashify_uptime(uptime) 199 | return unless uptime 200 | 201 | { 202 | 'seconds' => uptime.seconds, 203 | 'last_boot' => timeify(uptime.last_boot) 204 | } 205 | end 206 | 207 | def hashify_service(service) 208 | return unless service 209 | 210 | protocol = service.protocol rescue nil 211 | { 212 | 'name' => service.name, 213 | 'ssl' => service.ssl?, 214 | 'protocol' => protocol, 215 | 'product' => service.product, 216 | 'version' => service.version, 217 | 'hostname' => service.hostname, # This is just a string 218 | 'device_type' => service.device_type, 219 | 'fingerprint_method' => service.fingerprint_method.to_s, 220 | 'fingerprint' => service.fingerprint, 221 | 'confidence' => service.confidence 222 | } 223 | end 224 | 225 | def hashify_port(port) 226 | return unless port 227 | 228 | { 229 | 'number' => port.number, 230 | 'reason' => port.reason, 231 | 'protocol' => port.protocol.to_s, 232 | 'service' => hashify_service(port.service), 233 | 'state' => port.state.to_s 234 | } 235 | end 236 | 237 | def hashify_traceroute(traceroute) 238 | return unless traceroute 239 | 240 | protocol = traceroute.protocol rescue nil 241 | { 242 | 'port' => traceroute.port, # int 243 | 'protocol' => protocol, 244 | 'hops' => traceroute.map.with_index do |hop, idx| 245 | { 246 | 'address' => hop.addr, # str 247 | 'hostname' => hop.host, # str 248 | 'ttl' => hop.ttl.to_i, # int 249 | 'rtt' => hop.rtt.to_i, # int 250 | 'index' => idx # int (for searching by distance) 251 | } 252 | end 253 | } 254 | end 255 | 256 | def hashify_structs(structs) 257 | structs.map {|s| hashify_struct(s)} 258 | end 259 | 260 | def hashify_struct(struct) 261 | Hash[struct.each_pair.map {|k,v| [de_keyword(k), de_keyword(v)]}] 262 | end 263 | 264 | def de_keyword(value) 265 | value.is_a?(Symbol) ? value.to_s : value 266 | end 267 | 268 | EPOCH = LogStash::Timestamp.new(Time.at(0)) 269 | def timeify(time, default=nil) 270 | timestamp = time ? LogStash::Timestamp.new(time) : nil 271 | # Sometimes the nmap parser returns the epoch when there's no time... 272 | if (!timestamp || timestamp <= EPOCH) 273 | default 274 | else 275 | timestamp 276 | end 277 | end 278 | 279 | # Some strings have quoted values, we may want to remove leading/trailing quotes 280 | def dequote(str) 281 | return nil unless str 282 | str.gsub(/\A"|"\Z/, '') 283 | end 284 | 285 | end 286 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright 2020 Elastic and contributors 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /spec/fixtures/full_scan.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 | cpe:/a:thekelleys:dnsmasq:2.72test3