ronin-support 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. data/.gitignore +11 -0
  2. data/ChangeLog.md +42 -1
  3. data/README.md +4 -1
  4. data/gemspec.yml +2 -1
  5. data/lib/ronin/extensions.rb +2 -0
  6. data/lib/ronin/extensions/enumerable.rb +54 -0
  7. data/lib/ronin/extensions/file.rb +70 -2
  8. data/lib/ronin/extensions/ip_addr.rb +45 -45
  9. data/lib/ronin/extensions/regexp.rb +45 -0
  10. data/lib/ronin/extensions/resolv.rb +80 -0
  11. data/lib/ronin/extensions/string.rb +35 -32
  12. data/lib/ronin/formatting/extensions/binary/integer.rb +12 -5
  13. data/lib/ronin/formatting/extensions/binary/string.rb +44 -16
  14. data/lib/ronin/formatting/extensions/html/integer.rb +51 -31
  15. data/lib/ronin/formatting/extensions/html/string.rb +50 -31
  16. data/lib/ronin/formatting/extensions/http/integer.rb +10 -2
  17. data/lib/ronin/formatting/extensions/sql.rb +20 -0
  18. data/lib/ronin/formatting/extensions/sql/string.rb +98 -0
  19. data/lib/ronin/formatting/extensions/text/array.rb +11 -9
  20. data/lib/ronin/formatting/extensions/text/string.rb +213 -29
  21. data/lib/ronin/formatting/sql.rb +20 -0
  22. data/lib/ronin/network/extensions/http.rb +1 -0
  23. data/lib/ronin/network/extensions/http/net.rb +2 -2
  24. data/lib/ronin/network/extensions/http/uri/http.rb +226 -0
  25. data/lib/ronin/network/extensions/imap/net.rb +1 -1
  26. data/lib/ronin/network/extensions/ssl/net.rb +7 -1
  27. data/lib/ronin/network/http/proxy.rb +20 -21
  28. data/lib/ronin/network/mixins.rb +27 -0
  29. data/lib/ronin/network/mixins/esmtp.rb +165 -0
  30. data/lib/ronin/network/mixins/http.rb +723 -0
  31. data/lib/ronin/network/mixins/imap.rb +151 -0
  32. data/lib/ronin/network/mixins/pop3.rb +141 -0
  33. data/lib/ronin/network/mixins/smtp.rb +159 -0
  34. data/lib/ronin/network/mixins/tcp.rb +331 -0
  35. data/lib/ronin/network/mixins/telnet.rb +199 -0
  36. data/lib/ronin/network/mixins/udp.rb +227 -0
  37. data/lib/ronin/network/ssl.rb +17 -11
  38. data/lib/ronin/path.rb +3 -3
  39. data/lib/ronin/spec/ui/output.rb +28 -0
  40. data/lib/ronin/support.rb +3 -0
  41. data/lib/ronin/support/version.rb +1 -1
  42. data/lib/ronin/ui/output.rb +21 -0
  43. data/lib/ronin/ui/output/helpers.rb +248 -0
  44. data/lib/ronin/ui/output/output.rb +146 -0
  45. data/lib/ronin/ui/output/terminal.rb +21 -0
  46. data/lib/ronin/ui/output/terminal/color.rb +118 -0
  47. data/lib/ronin/ui/output/terminal/raw.rb +103 -0
  48. data/lib/ronin/ui/shell.rb +219 -0
  49. data/ronin-support.gemspec +1 -1
  50. data/spec/extensions/enumerable_spec.rb +24 -0
  51. data/spec/extensions/file_spec.rb +39 -0
  52. data/spec/extensions/ip_addr_spec.rb +6 -0
  53. data/spec/extensions/resolv_spec.rb +18 -0
  54. data/spec/formatting/html/integer_spec.rb +2 -2
  55. data/spec/formatting/html/string_spec.rb +1 -1
  56. data/spec/formatting/sql/string_spec.rb +55 -0
  57. data/spec/formatting/text/string_spec.rb +110 -0
  58. data/spec/network/ssl_spec.rb +10 -4
  59. data/spec/ui/classes/test_shell.rb +22 -0
  60. data/spec/ui/output_spec.rb +32 -0
  61. data/spec/ui/shell_spec.rb +79 -0
  62. metadata +132 -90
@@ -0,0 +1,146 @@
1
+ #
2
+ # Copyright (c) 2006-2011 Hal Brodigan (postmodern.mod3 at gmail.com)
3
+ #
4
+ # This file is part of Ronin Support.
5
+ #
6
+ # Ronin Support is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Lesser General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Ronin Support is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public License
17
+ # along with Ronin Support. If not, see <http://www.gnu.org/licenses/>.
18
+ #
19
+
20
+ require 'ronin/ui/output/terminal/raw'
21
+ require 'ronin/ui/output/terminal/color'
22
+
23
+ module Ronin
24
+ module UI
25
+ #
26
+ # Controls {Output} from Ronin.
27
+ #
28
+ module Output
29
+ @mode = if ($VERBOSE || $DEBUG || ENV['VERBOSE'])
30
+ :verbose
31
+ else
32
+ :quiet
33
+ end
34
+
35
+ @handler = if STDOUT.tty?
36
+ Terminal::Color
37
+ else
38
+ Terminal::Raw
39
+ end
40
+
41
+ #
42
+ # @return [Boolean]
43
+ # Specifies whether verbose output is enabled.
44
+ #
45
+ # @since 0.3.0
46
+ #
47
+ # @api semipublic
48
+ #
49
+ def Output.verbose?
50
+ @mode == :verbose
51
+ end
52
+
53
+ #
54
+ # @return [Boolean]
55
+ # Specifies whether quiet output is enabled.
56
+ #
57
+ # @since 0.3.0
58
+ #
59
+ # @api semipublic
60
+ #
61
+ def Output.quiet?
62
+ @mode == :quiet
63
+ end
64
+
65
+ #
66
+ # @return [Boolean]
67
+ # Specifies whether silent output is enabled.
68
+ #
69
+ # @since 0.3.0
70
+ #
71
+ # @api semipublic
72
+ #
73
+ def Output.silent?
74
+ @mode == :silent
75
+ end
76
+
77
+ #
78
+ # Enables verbose output.
79
+ #
80
+ # @return [Output]
81
+ #
82
+ # @since 1.0.0
83
+ #
84
+ # @api semipublic
85
+ #
86
+ def Output.verbose!
87
+ @mode = :verbose
88
+ return self
89
+ end
90
+
91
+ #
92
+ # Disables verbose output.
93
+ #
94
+ # @return [Output]
95
+ #
96
+ # @since 1.0.0
97
+ #
98
+ # @api semipublic
99
+ #
100
+ def Output.quiet!
101
+ @mode = :quiet
102
+ return self
103
+ end
104
+
105
+ #
106
+ # Disables all output.
107
+ #
108
+ # @return [Output]
109
+ #
110
+ # @since 1.0.0
111
+ #
112
+ def Output.silent!
113
+ @mode = :silent
114
+ return self
115
+ end
116
+
117
+ #
118
+ # @return [Ronin::UI::Output::Handler]
119
+ # The current Output handler.
120
+ #
121
+ # @since 0.3.0
122
+ #
123
+ # @api semipublic
124
+ #
125
+ def Output.handler
126
+ @handler
127
+ end
128
+
129
+ #
130
+ # Sets the current Output handler.
131
+ #
132
+ # @param [Handler] new_handler
133
+ # The new output handler to use. Must provide the `puts`,
134
+ # `print_info`, `print_debug`, `print_warning` and `print_error`
135
+ # class methods.
136
+ #
137
+ # @since 0.3.0
138
+ #
139
+ # @api semipublic
140
+ #
141
+ def Output.handler=(new_handler)
142
+ @handler = new_handler
143
+ end
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,21 @@
1
+ #
2
+ # Copyright (c) 2006-2011 Hal Brodigan (postmodern.mod3 at gmail.com)
3
+ #
4
+ # This file is part of Ronin Support.
5
+ #
6
+ # Ronin Support is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Lesser General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Ronin Support is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public License
17
+ # along with Ronin Support. If not, see <http://www.gnu.org/licenses/>.
18
+ #
19
+
20
+ require 'ronin/ui/output/terminal/raw'
21
+ require 'ronin/ui/output/terminal/color'
@@ -0,0 +1,118 @@
1
+ #
2
+ # Copyright (c) 2006-2011 Hal Brodigan (postmodern.mod3 at gmail.com)
3
+ #
4
+ # This file is part of Ronin Support.
5
+ #
6
+ # Ronin Support is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Lesser General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Ronin Support is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public License
17
+ # along with Ronin Support. If not, see <http://www.gnu.org/licenses/>.
18
+ #
19
+
20
+ module Ronin
21
+ module UI
22
+ module Output
23
+ module Terminal
24
+ #
25
+ # The handler for color output to the terminal.
26
+ #
27
+ class Color
28
+
29
+ # ANSI Green code
30
+ GREEN = "\e[32m"
31
+
32
+ # ANSI Cyan code
33
+ CYAN = "\e[36m"
34
+
35
+ # ANSI Yellow code
36
+ YELLOW = "\e[33m"
37
+
38
+ # ANSI Red code
39
+ RED = "\e[31m"
40
+
41
+ # ANSI Clear code
42
+ CLEAR = "\e[0m"
43
+
44
+ #
45
+ # Writes data to `STDOUT`.
46
+ #
47
+ # @param [String] data
48
+ # The data to write.
49
+ #
50
+ # @since 1.0.0
51
+ #
52
+ # @api private
53
+ #
54
+ def self.write(data)
55
+ STDOUT.write(data)
56
+ end
57
+
58
+ #
59
+ # Prints an `info` message.
60
+ #
61
+ # @param [String] message
62
+ # The message to print.
63
+ #
64
+ # @since 1.0.0
65
+ #
66
+ # @api private
67
+ #
68
+ def self.print_info(message)
69
+ puts "#{GREEN}[-] #{message}#{CLEAR}"
70
+ end
71
+
72
+ #
73
+ # Prints a `debug` message.
74
+ #
75
+ # @param [String] message
76
+ # The message to print.
77
+ #
78
+ # @since 1.0.0
79
+ #
80
+ # @api private
81
+ #
82
+ def self.print_debug(message)
83
+ puts "#{CYAN}[=] #{message}#{CLEAR}"
84
+ end
85
+
86
+ #
87
+ # Prints a `warning` message.
88
+ #
89
+ # @param [String] message
90
+ # The message to print.
91
+ #
92
+ # @since 1.0.0
93
+ #
94
+ # @api private
95
+ #
96
+ def self.print_warning(message)
97
+ puts "#{YELLOW}[*] #{message}#{CLEAR}"
98
+ end
99
+
100
+ #
101
+ # Prints an `error` message.
102
+ #
103
+ # @param [String] message
104
+ # The message to print.
105
+ #
106
+ # @since 1.0.0
107
+ #
108
+ # @api private
109
+ #
110
+ def self.print_error(message)
111
+ puts "#{RED}[!] #{message}#{CLEAR}"
112
+ end
113
+
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,103 @@
1
+ #
2
+ # Copyright (c) 2006-2011 Hal Brodigan (postmodern.mod3 at gmail.com)
3
+ #
4
+ # This file is part of Ronin Support.
5
+ #
6
+ # Ronin Support is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Lesser General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Ronin Support is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public License
17
+ # along with Ronin Support. If not, see <http://www.gnu.org/licenses/>.
18
+ #
19
+
20
+ module Ronin
21
+ module UI
22
+ module Output
23
+ module Terminal
24
+ #
25
+ # The handler for raw output to the terminal.
26
+ #
27
+ class Raw
28
+
29
+ #
30
+ # Writes data to `STDOUT`.
31
+ #
32
+ # @param [String] data
33
+ # The data to write.
34
+ #
35
+ # @since 1.0.0
36
+ #
37
+ # @api private
38
+ #
39
+ def self.write(data)
40
+ STDOUT.write(data)
41
+ end
42
+
43
+ #
44
+ # Prints an `info` message.
45
+ #
46
+ # @param [String] message
47
+ # The message to print.
48
+ #
49
+ # @since 1.0.0
50
+ #
51
+ # @api private
52
+ #
53
+ def self.print_info(message)
54
+ puts "[-] #{message}"
55
+ end
56
+
57
+ #
58
+ # Prints a `debug` message.
59
+ #
60
+ # @param [String] message
61
+ # The message to print.
62
+ #
63
+ # @since 1.0.0
64
+ #
65
+ # @api private
66
+ #
67
+ def self.print_debug(message)
68
+ puts "[=] #{message}"
69
+ end
70
+
71
+ #
72
+ # Prints a `warning` message.
73
+ #
74
+ # @param [String] message
75
+ # The message to print.
76
+ #
77
+ # @since 1.0.0
78
+ #
79
+ # @api private
80
+ #
81
+ def self.print_warning(message)
82
+ puts "[*] #{message}"
83
+ end
84
+
85
+ #
86
+ # Prints an `error` messages.
87
+ #
88
+ # @param [String] message
89
+ # The message to print.
90
+ #
91
+ # @since 1.0.0
92
+ #
93
+ # @api private
94
+ #
95
+ def self.print_error(message)
96
+ puts "[!] #{message}"
97
+ end
98
+
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,219 @@
1
+ #
2
+ # Copyright (c) 2006-2011 Hal Brodigan (postmodern.mod3 at gmail.com)
3
+ #
4
+ # This file is part of Ronin Support.
5
+ #
6
+ # Ronin Support is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Lesser General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Ronin Support is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public License
17
+ # along with Ronin Support. If not, see <http://www.gnu.org/licenses/>.
18
+ #
19
+
20
+ require 'ronin/ui/output/helpers'
21
+
22
+ require 'set'
23
+
24
+ module Ronin
25
+ module UI
26
+ #
27
+ # Spawns a ReadLine powered interactive Shell.
28
+ #
29
+ # @api semipublic
30
+ #
31
+ class Shell
32
+
33
+ include Output::Helpers
34
+
35
+ # Default shell prompt
36
+ DEFAULT_PROMPT = '>'
37
+
38
+ # The shell name
39
+ attr_accessor :name
40
+
41
+ # The shell prompt
42
+ attr_accessor :prompt
43
+
44
+ # The commands available for the shell
45
+ attr_reader :commands
46
+
47
+ #
48
+ # Creates a new shell.
49
+ #
50
+ # @param [Hash] options
51
+ # Additional options.
52
+ #
53
+ # @option options [String] :name ('')
54
+ # The shell-name to use before the prompt.
55
+ #
56
+ # @option options [String] :prompt (DEFAULT_PROMPT)
57
+ # The prompt to use for the shell.
58
+ #
59
+ # @yield [shell, line]
60
+ # The block that will be passed every command entered.
61
+ #
62
+ # @yieldparam [Shell] shell
63
+ # The shell to use for output.
64
+ #
65
+ # @yieldparam [String] line
66
+ # The command entered into the shell.
67
+ #
68
+ # @api semipublic
69
+ #
70
+ # @since 0.3.0
71
+ #
72
+ def initialize(options={},&block)
73
+ @name = options.fetch(:name,'')
74
+ @prompt = options.fetch(:prompt,DEFAULT_PROMPT)
75
+
76
+ @commands = Set[:help, :exit]
77
+ @commands += protected_methods.map { |name| name.to_sym }
78
+
79
+ @input_handler = block
80
+ end
81
+
82
+ #
83
+ # Creates a new Shell object and starts it.
84
+ #
85
+ # @param [Array] arguments
86
+ # Arguments for {#initialize}.
87
+ #
88
+ # @yield [shell, line]
89
+ # The block that will be passed every command entered.
90
+ #
91
+ # @yieldparam [Shell] shell
92
+ # The shell to use for output.
93
+ #
94
+ # @yieldparam [String] line
95
+ # The command entered into the shell.
96
+ #
97
+ # @return [nil]
98
+ #
99
+ # @example
100
+ # Shell.start(:prompt => '$') { |shell,line| system(line) }
101
+ #
102
+ def self.start(*arguments,&block)
103
+ new(*arguments,&block).start
104
+ end
105
+
106
+ #
107
+ # Starts the shell.
108
+ #
109
+ # @since 0.3.0
110
+ #
111
+ def start
112
+ history_rollback = 0
113
+
114
+ loop do
115
+ unless (raw_line = Readline.readline("#{name}#{prompt} "))
116
+ break # user exited the shell
117
+ end
118
+
119
+ line = raw_line.strip
120
+
121
+ if (line == 'exit' || line == 'quit')
122
+ exit
123
+ break
124
+ elsif !(line.empty?)
125
+ Readline::HISTORY << raw_line
126
+ history_rollback += 1
127
+
128
+ begin
129
+ handler(line)
130
+ rescue => e
131
+ print_error "#{e.class.name}: #{e.message}"
132
+ end
133
+ end
134
+ end
135
+
136
+ history_rollback.times { Readline::HISTORY.pop }
137
+ return nil
138
+ end
139
+
140
+ #
141
+ # Handles input for the shell.
142
+ #
143
+ # @param [String] line
144
+ # A line of input received by the shell.
145
+ #
146
+ # @since 0.3.0
147
+ #
148
+ def call(line)
149
+ if @input_handler
150
+ @input_handler.call(self,line)
151
+ else
152
+ arguments = line.split(/\s+/)
153
+ command = arguments.shift
154
+
155
+ # ignore empty lines
156
+ return false unless command
157
+
158
+ command = command.to_sym
159
+
160
+ # no explicitly calling handler
161
+ return false if command == :handler
162
+
163
+ unless @commands.include?(command)
164
+ print_error "Invalid command: #{command}"
165
+ return false
166
+ end
167
+
168
+ return send(command,*arguments)
169
+ end
170
+ end
171
+
172
+ protected
173
+
174
+ #
175
+ # Method which is called before exiting the shell.
176
+ #
177
+ # @since 0.3.0
178
+ #
179
+ def exit
180
+ end
181
+
182
+ #
183
+ # @see #exit
184
+ #
185
+ # @since 0.3.0
186
+ #
187
+ def quit
188
+ exit
189
+ end
190
+
191
+ #
192
+ # Prints the available commands and their arguments.
193
+ #
194
+ # @since 0.3.0
195
+ #
196
+ def help
197
+ puts "Available commands:"
198
+ puts
199
+
200
+ @commands.sort.each do |name|
201
+ command_method = method(name)
202
+ arguments = command_method.parameters.map do |param|
203
+ case param[0]
204
+ when :opt
205
+ "[#{param[1]}]"
206
+ when :rest
207
+ "[#{param[1]} ...]"
208
+ else
209
+ param[1]
210
+ end
211
+ end
212
+
213
+ puts " #{name} #{arguments.join(' ')}"
214
+ end
215
+ end
216
+
217
+ end
218
+ end
219
+ end