fidius-evasiondb 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. data/.gitignore +9 -0
  2. data/.yardopts +6 -0
  3. data/Gemfile +5 -0
  4. data/LICENSE +57 -0
  5. data/README.md +180 -0
  6. data/Rakefile +33 -0
  7. data/bin/fidius-evasiondb +111 -0
  8. data/evasion-db.gemspec +37 -0
  9. data/lib/db/db-install.rb +103 -0
  10. data/lib/db/migrations/001_create_packets.rb +18 -0
  11. data/lib/db/migrations/002_create_idmef_events.rb +23 -0
  12. data/lib/db/migrations/003_create_attack_modules.rb +14 -0
  13. data/lib/db/migrations/004_create_attack_options.rb +14 -0
  14. data/lib/db/migrations/005_create_attack_payloads.rb +13 -0
  15. data/lib/evasion-db/base.rb +80 -0
  16. data/lib/evasion-db/idmef-fetchers/fetchers.rb +67 -0
  17. data/lib/evasion-db/idmef-fetchers/prelude-db/fetcher.rb +8 -0
  18. data/lib/evasion-db/idmef-fetchers/prelude-db/lib/models/additional_data.rb +16 -0
  19. data/lib/evasion-db/idmef-fetchers/prelude-db/lib/models/address.rb +8 -0
  20. data/lib/evasion-db/idmef-fetchers/prelude-db/lib/models/alert.rb +46 -0
  21. data/lib/evasion-db/idmef-fetchers/prelude-db/lib/models/analyzer.rb +18 -0
  22. data/lib/evasion-db/idmef-fetchers/prelude-db/lib/models/classification.rb +10 -0
  23. data/lib/evasion-db/idmef-fetchers/prelude-db/lib/models/connection.rb +10 -0
  24. data/lib/evasion-db/idmef-fetchers/prelude-db/lib/models/detect_time.rb +10 -0
  25. data/lib/evasion-db/idmef-fetchers/prelude-db/lib/models/impact.rb +19 -0
  26. data/lib/evasion-db/idmef-fetchers/prelude-db/lib/models/prelude_event.rb +120 -0
  27. data/lib/evasion-db/idmef-fetchers/prelude-db/lib/models/service.rb +10 -0
  28. data/lib/evasion-db/idmef-fetchers/prelude-db/lib/patches/postgres_patch.rb +12 -0
  29. data/lib/evasion-db/idmef-fetchers/prelude-db/lib/prelude_event_fetcher.rb +67 -0
  30. data/lib/evasion-db/idmef-fetchers/test-fetcher/fetcher.rb +6 -0
  31. data/lib/evasion-db/idmef-fetchers/test-fetcher/lib/test_fetcher.rb +19 -0
  32. data/lib/evasion-db/knowledge/attack_module.rb +41 -0
  33. data/lib/evasion-db/knowledge/attack_option.rb +12 -0
  34. data/lib/evasion-db/knowledge/attack_payload.rb +13 -0
  35. data/lib/evasion-db/knowledge/connection.rb +7 -0
  36. data/lib/evasion-db/knowledge/idmef_event.rb +21 -0
  37. data/lib/evasion-db/knowledge/packet.rb +17 -0
  38. data/lib/evasion-db/knowledge.rb +128 -0
  39. data/lib/evasion-db/log_matches_helper.rb +43 -0
  40. data/lib/evasion-db/recorders/msf-recorder/lib/msf-recorder.rb +78 -0
  41. data/lib/evasion-db/recorders/msf-recorder/recorder.rb +6 -0
  42. data/lib/evasion-db/recorders/recorders.rb +61 -0
  43. data/lib/evasion-db/version.rb +5 -0
  44. data/lib/fidius-evasiondb.rb +27 -0
  45. data/lib/msf-plugins/database.yml.example +24 -0
  46. data/lib/msf-plugins/evasiondb.rb +378 -0
  47. data/test/config/database.yml +11 -0
  48. data/test/config/prelude.sql +439 -0
  49. data/test/helper.rb +50 -0
  50. data/test/preludedb_helper.rb +70 -0
  51. data/test/test_fetchers.rb +34 -0
  52. data/test/test_knowledge.rb +102 -0
  53. data/test/test_preludedb.rb +41 -0
  54. data/test/test_recorders.rb +115 -0
  55. metadata +238 -0
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ *~
6
+ *.sqlite3
7
+ coverage/
8
+ doc/
9
+ .yardoc/
data/.yardopts ADDED
@@ -0,0 +1,6 @@
1
+ --title 'EvasionDB'
2
+ --private
3
+ --protected
4
+ --readme 'README.md'
5
+ lib/**/*.rb -
6
+ LICENSE CREDITS.md
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in evasion-db.gemspec
4
+ gemspec
5
+
data/LICENSE ADDED
@@ -0,0 +1,57 @@
1
+ The Simplified BSD License
2
+
3
+ Copyright (C) 2010-2011 FIDIUS Intrusion Detection with Intelligent
4
+ User Support (FIDIUS). All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are
8
+ met:
9
+
10
+ 1. Redistributions of source code must retain the above copyright
11
+ notice, this list of conditions and the following disclaimer.
12
+
13
+ 2. Redistributions in binary form must reproduce the above copyright
14
+ notice, this list of conditions and the following disclaimer in the
15
+ documentation and/or other materials provided with the distribution.
16
+
17
+ THIS SOFTWARE IS PROVIDED BY FIDIUS ``AS IS'' AND ANY EXPRESS OR
18
+ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
+ DISCLAIMED. IN NO EVENT SHALL FIDIUS OR CONTRIBUTORS BE LIABLE FOR ANY
21
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
+ OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
+ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25
+ STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26
+ IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
+ POSSIBILITY OF SUCH DAMAGE.
28
+
29
+ The views and conclusions contained in the software and documentation
30
+ are those of the authors and should not be interpreted as representing
31
+ official policies, either expressed or implied, of FIDIUS.
32
+
33
+
34
+ *OR*
35
+
36
+ GNU GENERAL PUBLIC LICENSE
37
+ Version 2, June 1991
38
+
39
+ Copyright (C) 2010-2011 FIDIUS Intrusion Detection with Intelligent
40
+ User Suppport.
41
+
42
+ This program is free software; you can redistribute it and/or modify
43
+ it under the terms of the GNU General Public License as published by
44
+ the Free Software Foundation; either version 2 of the License, or
45
+ (at your option) any later version.
46
+
47
+ This program is distributed in the hope that it will be useful,
48
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
49
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50
+ GNU General Public License for more details.
51
+
52
+ You should have received a copy of the GNU General Public License along
53
+ with this program; if not, write to the Free Software Foundation, Inc.,
54
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
55
+
56
+ A digital copy is also available for download here:
57
+ http://www.gnu.org/licenses/gpl-2.0.txt.
data/README.md ADDED
@@ -0,0 +1,180 @@
1
+ # FIDIUS EvasionDB
2
+
3
+ The FIDIUS EvasionDB Gem provides a database which contains knowledge about metasploit exploits
4
+ and their corresponding alerts/events produced by intrusion detection systems (IDS). It includes a
5
+ Metasploit plugin which supports the recording of thrown alerts during the execution of an exploit.
6
+
7
+ ## Description
8
+
9
+ This gem is developed in the context of the students project "FIDIUS" at the
10
+ University of Bremen, for more information about FIDIUS visit [fidius.me](http://fidius.me/en).
11
+
12
+ ## Installation
13
+
14
+ Simply install this package with Rubygems:
15
+
16
+ $ gem install fidius-evasiondb
17
+
18
+ Then switch to the root directory of your Metasploit installation and run
19
+
20
+ $ fidius-evasiondb -c
21
+
22
+ Follow the instructions.
23
+
24
+ This Gem currently uses 2 databases:
25
+
26
+ * `ids_db`: A Prelude Manager database for fetching IDMEF events.
27
+ * `evasion_db`: Knowledge database for information about exploits and their IDMEF events.
28
+
29
+ Please note: The Evasion-DB Gem has only been tested with Linux systems and might not work with Windows.
30
+
31
+ ## Configuration
32
+
33
+ The database configuration can be found in
34
+
35
+ path/to/your/metasploit/root/data/database.yml
36
+
37
+ It has been tested with PostgreSQL and MySQL databases but should work for others, too.
38
+
39
+ ## Usage
40
+
41
+ There are two possibilities to use this Gem, either inside the Metasploit console (with a plugin) or
42
+ from external scripts by requiring the Gem. The first method (use in `msfconsole`) is intended to
43
+ generate knowledge about exploits. You can execute any module within metasploit and log
44
+ corresponding IDMEF events.
45
+
46
+ Please note: Currently it is only possible to fetch IDMEF events from an existing and configured
47
+ Prelude Manager database. At the beginning of a module execution, the timestamp and number of total
48
+ events in prelude are measured. After the module is finished newly generated events are identified
49
+ via timestamp and the attackers source IP address.
50
+
51
+ ### In MSF console
52
+
53
+ Example for monitoring an exploit. After loading the plugin all modules which are executed by
54
+ metasploit will be monitored. All payload which is send to the target will be stored in the
55
+ Knowledge database. After executing of the module finished generated IDMEF events will be fetched
56
+ from the Prelude database and stored to the Knowledge database, too.
57
+
58
+ $ msf > load evasiondb
59
+ $ [*] EvasionDB plugin loaded.
60
+ $ [*] Successfully loaded plugin: FIDIUS-EvasionDB
61
+ $ msf > use exploit/windows/smb/ms08_067_netapi
62
+ $ msf exploit(ms08_067_netapi) > set PAYLOAD windows/meterpreter/bind_tcp
63
+ $ PAYLOAD => windows/meterpreter/bind_tcp
64
+ $ msf exploit(ms08_067_netapi) > set RHOST 10.20.20.1
65
+ $ RHOST => 10.20.20.1
66
+ $ msf exploit(ms08_067_netapi) > exploit
67
+ $ [*] Started bind handler
68
+ $ [*] Automatically detecting the target...
69
+ $ [*] Fingerprint: Windows XP - Service Pack 2 - lang:German
70
+ $ [*] Selected Target: Windows XP SP2 German (NX)
71
+ $ [*] Attempting to trigger the vulnerability...
72
+ $ [*] Sending stage (749056 bytes) to 10.20.20.1
73
+ $ [*] Meterpreter session 1 opened (10.0.0.100:52764 -> 10.20.20.1:4444) at 2011-03-28 16:42:53 +0200
74
+ $ meterpreter > exit
75
+ $ [*] Meterpreter session 1 closed. Reason: User exit
76
+ $ msf exploit(ms08_067_netapi) > show_events
77
+ $ ------------------------------------------------------------
78
+ $ exploit/windows/smb/ms08_067_netapi with 47 options
79
+ $ ------------------------------------------------------------
80
+ $ 11 idmef-events fetched
81
+ $ ------------------------------------------------------------
82
+ $ (1)COMMUNITY SIP TCP/IP message flooding directed to SIP proxy with 0 bytes payload
83
+ $ (2)COMMUNITY SIP TCP/IP message flooding directed to SIP proxy with 1324 bytes payload
84
+ $ (3)COMMUNITY SIP TCP/IP message flooding directed to SIP proxy with 0 bytes payload
85
+ $ (4)COMMUNITY SIP TCP/IP message flooding directed to SIP proxy with 1324 bytes payload
86
+ $ (5)COMMUNITY SIP TCP/IP message flooding directed to SIP proxy with 0 bytes payload
87
+ $ (6)COMMUNITY SIP TCP/IP message flooding directed to SIP proxy with 1324 bytes payload
88
+ $ (7)ET POLICY PE EXE or DLL Windows file download with 1324 bytes payload
89
+ $ (8)ET POLICY PE EXE or DLL Windows file download with 1324 bytes payload
90
+ $ (9)ET EXPLOIT x86 JmpCallAdditive Encoder with 759 bytes payload
91
+ $ (10)ET EXPLOIT x86 JmpCallAdditive Encoder with 467 bytes payload
92
+ $ (11)NETBIOS SMB-DS IPC$ share access with 72 bytes payload
93
+ $ msf exploit(ms08_067_netapi) >
94
+
95
+ ### From external scripts or IRb
96
+
97
+ From external scripts or inside IRb, there are only queries to the Evasion DB possible.
98
+ The usage is quite simple.
99
+
100
+ Just `require 'fidius-evasion'` in your script and call
101
+
102
+ FIDIUS::EvasionDB.config 'path/to/your/database.yml'
103
+
104
+ This will connect to the database and give you the possibility to use one of the query methods below.
105
+
106
+ ### Queries
107
+
108
+ Sample how the knowledge in EvasionDB can be queried:
109
+
110
+ ruby-1.9.1-p378 > require 'fidius-evasiondb'
111
+ => true
112
+ ruby-1.9.1-p378 > FIDIUS::EvasionDB.config "data/database.yml"
113
+ ruby-1.9.1-p378 > events = FIDIUS::EvasionDB::Knowledge.find_events_for_exploit "exploit/windows/smb/ms08_067_netapi"
114
+ ruby-1.9.1-p378 > events.size
115
+ => 11
116
+ ruby-1.9.1-p378 > events.first.severity
117
+ => "medium"
118
+ ruby-1.9.1-p378 > events.first.text
119
+ => "COMMUNITY SIP TCP/IP message flooding directed to SIP proxy"
120
+
121
+ ### Find an Exploit
122
+
123
+ ruby-1.9.1-p378 > m = FIDIUS::EvasionDB::Knowledge::AttackModule.first
124
+ => #<FIDIUS::EvasionDB::Knowledge::AttackModule id: 1, name: "exploit/windows/smb/ms08_067_netapi", options_hash: "4d70ba1e95523e6d602e316a2553decf", finished: true, created_at: "2011-04-02 13:43:44", updated_at: "2011-04-02 13:45:05">
125
+
126
+ ### Find IdmefEvents
127
+
128
+ ruby-1.9.1-p378 > event = m.idmef_events.first
129
+ => #<FIDIUS::EvasionDB::Knowledge::IdmefEvent id: 1, attack_module_id: 1, attack_payload_id: nil, payload: "wrong lookup type\x00\x00\x00unsupported algorithm\x00\x00\x00unknown...", detect_time: "2011-04-02 13:44:30", dest_ip: "10.20.20.1", src_ip: "10.0.0.100", dest_port: 4444, src_port: 45944, text: "COMMUNITY SIP TCP/IP message flooding directed to S...", severity: "medium", analyzer_model: "prelude-manager", ident: 1076676, created_at: "2011-04-02 13:45:03", updated_at: "2011-04-02 13:45:05">
130
+
131
+ ### Find Packets
132
+
133
+ ruby-1.9.1-p378 > m.packets.first
134
+ => #<FIDIUS::EvasionDB::Knowledge::Packet id: 1, attack_module_id: 1, attack_payload_id: nil, src_addr: "0.0.0.0", dest_addr: "10.20.20.1", src_port: "0", dest_port: "445", payload: "\x00\x00\x00T\xFFSMBr\x00\x00\x00\x00\x18\x01(\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xCD\x11\x00\x00\xB2|\x001\x00\x02LANMAN1.0\x00\x02...", created_at: "2011-04-02 13:43:47", updated_at: "2011-04-02 13:43:47">
135
+
136
+ ### Find Payload of Packet
137
+
138
+ ruby-1.9.1-p378 > m.packets.first.payload
139
+ => "\x00\x00\x00T\xFFSMBr\x00\x00\x00\x00\x18\x01(\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xCD\x11\x00\x00\xB2|\x001\x00\x02LANMAN1.0\x00\x02LM1.2X002\x00\x02NT LANMAN 1.0\x00\x02NT LM 0.12\x00"
140
+
141
+
142
+ ### Find Options of Exploit
143
+
144
+ ruby-1.9.1-p378 > m.attack_options.first.option_key
145
+ => "EXITFUNC"
146
+ ruby-1.9.1-p378 > m.attack_options.first.option_value
147
+ => "thread"
148
+
149
+ ## Import and export the database
150
+
151
+ Run
152
+
153
+ $ fidius-evasiondb -e
154
+
155
+ in your Metasploit root to dump the database into a directory. It will create a directory named `evasion_db_yyyy-mm-dd-hhmmss` which contains a `schema.rb` file with the table structure and an `evasion_db.yml` file with the database dump.
156
+
157
+ To import an Evasion DB, simply run
158
+
159
+ $ fidius-evasiondb -i dump_dir
160
+
161
+ where `dump_dir` is a path to a directory that contains an exported Evasion DB.
162
+
163
+ ## Authors and Contact
164
+
165
+ fidius-evasiondb was written by
166
+
167
+ * FIDIUS Intrusion Detection with Intelligent User Support
168
+ <grp-fidius+evasiondb@tzi.de>, <http://fidius.me>
169
+ * in particular:
170
+ * Bernhard Katzmarski <bkatzm+evasiondb@tzi.de>
171
+ * Jens Färber <jfaerber+evasiondb@tzi.de>
172
+
173
+ If you have any questions, remarks, suggestion, improvements, etc. feel free to drop a line at the
174
+ addresses given above. You might also join `#fidius` on Freenode or use the contact form on our
175
+ [website](http://fidius.me/en/contact).
176
+
177
+
178
+ ## License
179
+
180
+ Simplified BSD License and GNU GPLv2. See also the file LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,33 @@
1
+ # fix error: cant convert nil into String
2
+ # Rakefile:1 in include?
3
+ unless ENV['GEM_HOME'] && (__FILE__.include? ENV['GEM_HOME'])
4
+ require 'bundler'
5
+ Bundler::GemHelper.install_tasks
6
+ end
7
+
8
+ require 'rake/testtask'
9
+ Rake::TestTask.new(:test) do |test|
10
+ test.libs << 'lib' << 'test'
11
+ test.pattern = 'test/**/test_*.rb'
12
+ test.verbose = true
13
+ end
14
+
15
+ begin
16
+ require 'yard'
17
+
18
+ YARD::Rake::YardocTask.new(:doc) do |t|
19
+ t.files = ['lib/**/*.rb']
20
+ exclude = 'lib/db'
21
+ static_files = 'LICENSE,CREDITS.md'
22
+ t.options += [
23
+ '--title', 'FIDIUS EvasionDB',
24
+ '--private', # include private methods
25
+ '--protected', # include protected methods
26
+ '--exclude', exclude,
27
+ '--files', static_files,
28
+ '--readme', 'README.md'
29
+ ]
30
+ end
31
+ rescue LoadError
32
+ puts 'YARD not installed (gem install yard), http://yardoc.org'
33
+ end
@@ -0,0 +1,111 @@
1
+ #!/usr/bin/env ruby
2
+ require 'optparse'
3
+ require 'fidius-evasiondb'
4
+ require 'fileutils'
5
+
6
+ GEM_BASE = File.expand_path('../../lib', __FILE__)
7
+
8
+ messages = { 'root' => "Please run this script in the root directory of " +
9
+ "your metasploit installation",
10
+ 'pre-db' => "This will migrate the Evasion DB schema into the " +
11
+ "database specified in MSF-ROOT/data/database.yml " +
12
+ "in the section 'evasion_db'.\nPlease make sure, you " +
13
+ "configured that first. Are you ready to proceed? (y/n)",
14
+ 'empty' => "Initialize FIDIUS Evasion DB",
15
+ 'db-config' => "If you do not have a database config in 'data/database.yml', " +
16
+ "you can create an example file now. (y/n)"
17
+ }
18
+
19
+ options = {}
20
+
21
+ optparse = OptionParser.new do |opts|
22
+
23
+ opts.banner = "Usage: fidius-evasiondb [options]"
24
+
25
+ opts.on_tail("-c", "--create", "create empty FIDIUS Evasion DB") do
26
+ puts messages['empty']
27
+ if in_msf_root?
28
+ puts "Copy files for MSF-Plugin..."
29
+ plugin_source = File.join(GEM_BASE, 'msf-plugins', 'evasiondb.rb')
30
+ FileUtils.cp(plugin_source, 'plugins')
31
+ puts "Copied plugin to 'plugins/evasion_tester.rb'"
32
+
33
+ puts messages['db-config']
34
+ if yes_no_dialog?
35
+ db_source = File.join(GEM_BASE, 'msf-plugins', 'database.yml.example')
36
+ FileUtils.cp(db_source, File.join('data', 'database.yml'))
37
+ puts "Copied sample database configuration file to data/database.yml"
38
+ end
39
+
40
+ puts messages['pre-db']
41
+ if yes_no_dialog?
42
+ migrate_models
43
+ end
44
+ else
45
+ puts messages['root']
46
+ end
47
+ exit
48
+ end
49
+
50
+ opts.on_tail("-e", "--export", "export FIDIUS Evasion DB to YAML") do
51
+ if in_msf_root?
52
+ require 'fidius-common'
53
+ dir = FIDIUS::Common::Db.export("data/database.yml", "evasion_db")
54
+ puts "Created dump in '#{dir}'"
55
+ else
56
+ puts messages['root']
57
+ end
58
+ end
59
+
60
+ opts.on("-i", "--import IMPORT_DIR", "import FIDIUS Evasion DB from YAML") do |dir|
61
+ if in_msf_root?
62
+ require 'fidius-common'
63
+ dir = FIDIUS::Common::Db.import("data/database.yml", "evasion_db", dir)
64
+ else
65
+ puts messages['root']
66
+ end
67
+ dir
68
+ end
69
+
70
+ opts.on_tail("-h", "--help", "Show this message") do
71
+ puts "GEM_BASE=#{GEM_BASE}"
72
+ puts opts
73
+ exit
74
+ end
75
+
76
+ opts.on_tail("-v", "--version", "Show version") do
77
+ puts "FIDIUS Evasion DB, Version #{FIDIUS::EvasionDB::VERSION}"
78
+ exit
79
+ end
80
+ end
81
+
82
+ def in_msf_root?
83
+ files = ['data', 'plugins', 'msfconsole', 'msfcli', 'msfgui', 'msfpayload']
84
+ files.each { |file| return false unless File.exists? file }
85
+
86
+ return true
87
+ end
88
+
89
+ def migrate_models
90
+ require File.join(GEM_BASE, 'db', 'db-install.rb')
91
+ include FIDIUS::EvasionDB
92
+
93
+ migrations_path = File.join(GEM_BASE, 'db', 'migrations')
94
+ db_config_path = File.join(Dir.pwd, 'data')
95
+ FIDIUS::EvasionDB.migrate(migrations_path, db_config_path)
96
+ end
97
+
98
+ def yes_no_dialog?
99
+ while answer = gets
100
+ case answer
101
+ when "y\n"
102
+ return true
103
+ when "n\n"
104
+ return false
105
+ else
106
+ puts "I don't know what you mean, enter 'y' for yes or 'n' for no."
107
+ end
108
+ end
109
+ end
110
+
111
+ optparse.parse!
@@ -0,0 +1,37 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "evasion-db/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "fidius-evasiondb"
7
+ s.version = FIDIUS::EvasionDB::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Jens Färber", "Bernhard Katzmarski"]
10
+ s.email = ["jfaerber+evasiondb@tzi.de", "bkatzm+evasiondb@tzi.de"]
11
+ s.homepage = "http://fidius.me"
12
+
13
+ s.add_dependency('fidius-common')
14
+ s.add_dependency('activerecord')
15
+
16
+ s.summary = "The FIDIUS EvasionDB Gem provides a database which contains knowledge about "+
17
+ "metasploit exploits and their corresponding alerts/events produced by intrusion "+
18
+ "detection systems (IDS)."
19
+ s.description = s.summary + "\n\nIt includes a Metasploit plugin which supports the recording of "+
20
+ "thrown alerts during the execution of an exploit."
21
+
22
+ s.rubyforge_project = ""
23
+
24
+ s.add_dependency "activerecord", ">= 3.0.0"
25
+ s.add_dependency "activesupport", ">= 3.0.0"
26
+ s.add_dependency "fidius-common", ">= 0.0.4"
27
+
28
+ s.files = `git ls-files`.split("\n")
29
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
30
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
31
+ s.require_paths = ["lib"]
32
+
33
+ s.rdoc_options << '--title' << s.name <<
34
+ '--main' << 'README.md' << '--show-hash' <<
35
+ `git ls-files -- lib/*`.split("\n") <<
36
+ 'README.md' << 'LICENSE' << 'CREDITS.md'
37
+ end
@@ -0,0 +1,103 @@
1
+ require 'rake/testtask'
2
+ require 'active_record'
3
+ require 'logger'
4
+
5
+ module FIDIUS
6
+ module EvasionDB
7
+
8
+ def self.migrate migrations_path, db_config_path
9
+ @CFG_D = db_config_path
10
+
11
+ self.connection_data
12
+
13
+ begin
14
+ self.drop_database @connection_data['evasion_db']
15
+ rescue
16
+ puts "DB drop: Could not find database #{@connection_data['database']}"
17
+ end
18
+
19
+ self.create_database @connection_data['evasion_db']
20
+
21
+ self.with_db do
22
+ ActiveRecord::Migrator.migrate(migrations_path, ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
23
+ end
24
+ end
25
+
26
+ def connection_data
27
+ @connection_data ||= YAML.load_file("#{@CFG_D}/database.yml")
28
+ end
29
+
30
+ def with_db &block
31
+ begin
32
+ ActiveRecord::Base.establish_connection(connection_data['evasion_db'])
33
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
34
+ ActiveRecord::Base.logger.level = Logger::WARN
35
+ yield connection_data
36
+ rescue
37
+ raise
38
+ ensure
39
+ ActiveRecord::Base.connection.disconnect!
40
+ end
41
+ end
42
+
43
+ # copied and modified activerecord-3.0.4/lib/active_record/railties/database.rake
44
+ def drop_database(config)
45
+ case config['adapter']
46
+ when /sqlite/
47
+ FileUtils.rm (config['database'])
48
+ when /mysql/
49
+ ActiveRecord::Base.establish_connection(config)
50
+ ActiveRecord::Base.connection.drop_database config['database']
51
+ when 'postgresql'
52
+ ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
53
+ ActiveRecord::Base.connection.drop_database config['database']
54
+ end
55
+ end
56
+
57
+ # dito
58
+ def create_database(config)
59
+ case config['adapter']
60
+
61
+ when /mysql/
62
+ require 'mysql'
63
+ @charset = ENV['CHARSET'] || 'utf8'
64
+ @collation = ENV['COLLATION'] || 'utf8_unicode_ci'
65
+ creation_options = {:charset => (config['charset'] || @charset), :collation => (config['collation'] || @collation)}
66
+ error_class = config['adapter'] =~ /mysql2/ ? Mysql2::Error : Mysql::Error
67
+ access_denied_error = 1045
68
+ begin
69
+ ActiveRecord::Base.establish_connection(config.merge('database' => nil))
70
+ ActiveRecord::Base.connection.create_database(config['database'], creation_options)
71
+ ActiveRecord::Base.establish_connection(config)
72
+ rescue error_class => sqlerr
73
+ if sqlerr.errno == access_denied_error
74
+ print "#{sqlerr.error}. \nPlease provide the root password for your mysql installation\n>"
75
+ root_password = $stdin.gets.strip
76
+ grant_statement = "GRANT ALL PRIVILEGES ON #{config['database']}.* " \
77
+ "TO '#{config['username']}'@'localhost' " \
78
+ "IDENTIFIED BY '#{config['password']}' WITH GRANT OPTION;"
79
+ ActiveRecord::Base.establish_connection(config.merge('database' => nil, 'username' => 'root', 'password' => root_password))
80
+ ActiveRecord::Base.connection.create_database(config['database'], creation_options)
81
+ ActiveRecord::Base.connection.execute grant_statement
82
+ ActiveRecord::Base.establish_connection(config)
83
+ else
84
+ $stderr.puts sqlerr.error
85
+ $stderr.puts "Couldn't create database for #{config.inspect}, charset: #{config['charset'] || @charset}, collation: #{config['collation'] || @collation}"
86
+ $stderr.puts "(if you set the charset manually, make sure you have a matching collation)" if config['charset']
87
+ end
88
+ end
89
+ when 'postgresql'
90
+ @encoding = config['encoding'] || ENV['CHARSET'] || 'utf8'
91
+ begin
92
+ ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
93
+ ActiveRecord::Base.connection.create_database(config['database'], config.merge('encoding' => @encoding))
94
+ ActiveRecord::Base.establish_connection(config)
95
+ rescue Exception => e
96
+ $stderr.puts e, *(e.backtrace)
97
+ $stderr.puts "Couldn't create database for #{config.inspect}"
98
+ end
99
+ end
100
+
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,18 @@
1
+ class CreatePackets < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :packets do |t|
4
+ t.integer :attack_module_id
5
+ t.integer :attack_payload_id
6
+ t.string :src_addr
7
+ t.string :dest_addr
8
+ t.string :src_port
9
+ t.string :dest_port
10
+ t.column :payload, :binary
11
+ t.timestamps
12
+ end
13
+ end
14
+
15
+ def self.down
16
+ drop_table :packets
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ class CreateIdmefEvents < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :idmef_events do |t|
4
+ t.integer :attack_module_id
5
+ t.integer :attack_payload_id
6
+ t.column :payload, :binary
7
+ t.datetime :detect_time
8
+ t.string :dest_ip
9
+ t.string :src_ip
10
+ t.integer :dest_port
11
+ t.integer :src_port
12
+ t.string :text
13
+ t.string :severity
14
+ t.string :analyzer_model
15
+ t.column :ident, :bigint
16
+ t.timestamps
17
+ end
18
+ end
19
+
20
+ def self.down
21
+ drop_table :idmef_events
22
+ end
23
+ end
@@ -0,0 +1,14 @@
1
+ class CreateAttackModules < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :attack_modules do |t|
4
+ t.string :name
5
+ t.string :options_hash
6
+ t.boolean :finished, :default=>false
7
+ t.timestamps
8
+ end
9
+ end
10
+
11
+ def self.down
12
+ drop_table :attack_modules
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ class CreateAttackOptions < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :attack_options do |t|
4
+ t.integer :attack_module_id
5
+ t.string :option_key
6
+ t.string :option_value
7
+ t.timestamps
8
+ end
9
+ end
10
+
11
+ def self.down
12
+ drop_table :attack_options
13
+ end
14
+ end
@@ -0,0 +1,13 @@
1
+ class CreateAttackPayloads < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :attack_payloads do |t|
4
+ t.string :name
5
+ t.integer :attack_module_id
6
+ t.timestamps
7
+ end
8
+ end
9
+
10
+ def self.down
11
+ drop_table :attack_payloads
12
+ end
13
+ end