ronin-support 0.1.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (127) hide show
  1. data/.document +4 -0
  2. data/.rspec +1 -0
  3. data/.yardopts +1 -0
  4. data/COPYING.txt +504 -0
  5. data/ChangeLog.md +4 -0
  6. data/Gemfile +23 -0
  7. data/README.md +98 -0
  8. data/Rakefile +29 -0
  9. data/gemspec.yml +20 -0
  10. data/lib/ronin/extensions.rb +28 -0
  11. data/lib/ronin/extensions/file.rb +63 -0
  12. data/lib/ronin/extensions/ip_addr.rb +220 -0
  13. data/lib/ronin/extensions/kernel.rb +45 -0
  14. data/lib/ronin/extensions/meta.rb +22 -0
  15. data/lib/ronin/extensions/meta/object.rb +24 -0
  16. data/lib/ronin/extensions/string.rb +200 -0
  17. data/lib/ronin/formatting.rb +26 -0
  18. data/lib/ronin/formatting/binary.rb +22 -0
  19. data/lib/ronin/formatting/digest.rb +22 -0
  20. data/lib/ronin/formatting/extensions.rb +25 -0
  21. data/lib/ronin/formatting/extensions/binary.rb +24 -0
  22. data/lib/ronin/formatting/extensions/binary/file.rb +35 -0
  23. data/lib/ronin/formatting/extensions/binary/integer.rb +147 -0
  24. data/lib/ronin/formatting/extensions/binary/string.rb +363 -0
  25. data/lib/ronin/formatting/extensions/digest.rb +23 -0
  26. data/lib/ronin/formatting/extensions/digest/file.rb +117 -0
  27. data/lib/ronin/formatting/extensions/digest/string.rb +80 -0
  28. data/lib/ronin/formatting/extensions/http.rb +23 -0
  29. data/lib/ronin/formatting/extensions/http/integer.rb +57 -0
  30. data/lib/ronin/formatting/extensions/http/string.rb +102 -0
  31. data/lib/ronin/formatting/extensions/text.rb +23 -0
  32. data/lib/ronin/formatting/extensions/text/array.rb +125 -0
  33. data/lib/ronin/formatting/extensions/text/string.rb +206 -0
  34. data/lib/ronin/formatting/http.rb +22 -0
  35. data/lib/ronin/formatting/text.rb +22 -0
  36. data/lib/ronin/network.rb +29 -0
  37. data/lib/ronin/network/esmtp.rb +22 -0
  38. data/lib/ronin/network/extensions.rb +29 -0
  39. data/lib/ronin/network/extensions/esmtp.rb +22 -0
  40. data/lib/ronin/network/extensions/esmtp/net.rb +100 -0
  41. data/lib/ronin/network/extensions/http.rb +22 -0
  42. data/lib/ronin/network/extensions/http/net.rb +661 -0
  43. data/lib/ronin/network/extensions/imap.rb +22 -0
  44. data/lib/ronin/network/extensions/imap/net.rb +124 -0
  45. data/lib/ronin/network/extensions/pop3.rb +22 -0
  46. data/lib/ronin/network/extensions/pop3/net.rb +92 -0
  47. data/lib/ronin/network/extensions/smtp.rb +22 -0
  48. data/lib/ronin/network/extensions/smtp/net.rb +110 -0
  49. data/lib/ronin/network/extensions/ssl.rb +22 -0
  50. data/lib/ronin/network/extensions/ssl/net.rb +147 -0
  51. data/lib/ronin/network/extensions/tcp.rb +22 -0
  52. data/lib/ronin/network/extensions/tcp/net.rb +304 -0
  53. data/lib/ronin/network/extensions/telnet.rb +22 -0
  54. data/lib/ronin/network/extensions/telnet/net.rb +156 -0
  55. data/lib/ronin/network/extensions/udp.rb +22 -0
  56. data/lib/ronin/network/extensions/udp/net.rb +226 -0
  57. data/lib/ronin/network/http.rb +24 -0
  58. data/lib/ronin/network/http/exceptions.rb +22 -0
  59. data/lib/ronin/network/http/exceptions/unknown_request.rb +29 -0
  60. data/lib/ronin/network/http/http.rb +290 -0
  61. data/lib/ronin/network/http/proxy.rb +307 -0
  62. data/lib/ronin/network/imap.rb +49 -0
  63. data/lib/ronin/network/network.rb +41 -0
  64. data/lib/ronin/network/pop3.rb +49 -0
  65. data/lib/ronin/network/smtp.rb +24 -0
  66. data/lib/ronin/network/smtp/email.rb +143 -0
  67. data/lib/ronin/network/smtp/smtp.rb +68 -0
  68. data/lib/ronin/network/ssl.rb +47 -0
  69. data/lib/ronin/network/tcp.rb +22 -0
  70. data/lib/ronin/network/telnet.rb +109 -0
  71. data/lib/ronin/network/udp.rb +22 -0
  72. data/lib/ronin/path.rb +132 -0
  73. data/lib/ronin/support.rb +28 -0
  74. data/lib/ronin/support/inflector.rb +40 -0
  75. data/lib/ronin/support/version.rb +27 -0
  76. data/lib/ronin/templates.rb +23 -0
  77. data/lib/ronin/templates/erb.rb +75 -0
  78. data/lib/ronin/templates/template.rb +161 -0
  79. data/ronin-support.gemspec +10 -0
  80. data/spec/extensions/file_spec.rb +24 -0
  81. data/spec/extensions/ip_addr_spec.rb +171 -0
  82. data/spec/extensions/kernel_spec.rb +30 -0
  83. data/spec/extensions/string_spec.rb +177 -0
  84. data/spec/formatting/binary/helpers/hexdumps.rb +16 -0
  85. data/spec/formatting/binary/helpers/hexdumps/ascii.bin +0 -0
  86. data/spec/formatting/binary/helpers/hexdumps/hexdump_decimal_shorts.txt +17 -0
  87. data/spec/formatting/binary/helpers/hexdumps/hexdump_hex_bytes.txt +17 -0
  88. data/spec/formatting/binary/helpers/hexdumps/hexdump_hex_shorts.txt +17 -0
  89. data/spec/formatting/binary/helpers/hexdumps/hexdump_octal_bytes.txt +17 -0
  90. data/spec/formatting/binary/helpers/hexdumps/hexdump_octal_shorts.txt +17 -0
  91. data/spec/formatting/binary/helpers/hexdumps/hexdump_repeated.txt +6 -0
  92. data/spec/formatting/binary/helpers/hexdumps/od_decimal_bytes.txt +17 -0
  93. data/spec/formatting/binary/helpers/hexdumps/od_decimal_ints.txt +17 -0
  94. data/spec/formatting/binary/helpers/hexdumps/od_decimal_quads.txt +17 -0
  95. data/spec/formatting/binary/helpers/hexdumps/od_decimal_shorts.txt +17 -0
  96. data/spec/formatting/binary/helpers/hexdumps/od_hex_bytes.txt +17 -0
  97. data/spec/formatting/binary/helpers/hexdumps/od_hex_ints.txt +17 -0
  98. data/spec/formatting/binary/helpers/hexdumps/od_hex_quads.txt +17 -0
  99. data/spec/formatting/binary/helpers/hexdumps/od_hex_shorts.txt +17 -0
  100. data/spec/formatting/binary/helpers/hexdumps/od_octal_bytes.txt +17 -0
  101. data/spec/formatting/binary/helpers/hexdumps/od_octal_ints.txt +17 -0
  102. data/spec/formatting/binary/helpers/hexdumps/od_octal_quads.txt +17 -0
  103. data/spec/formatting/binary/helpers/hexdumps/od_octal_shorts.txt +17 -0
  104. data/spec/formatting/binary/helpers/hexdumps/od_repeated.txt +6 -0
  105. data/spec/formatting/binary/helpers/hexdumps/repeated.bin +1 -0
  106. data/spec/formatting/binary/integer_spec.rb +140 -0
  107. data/spec/formatting/binary/string_spec.rb +306 -0
  108. data/spec/formatting/digest/string_spec.rb +82 -0
  109. data/spec/formatting/http/integer_spec.rb +42 -0
  110. data/spec/formatting/http/string_spec.rb +76 -0
  111. data/spec/formatting/text/array_spec.rb +105 -0
  112. data/spec/formatting/text/string_spec.rb +180 -0
  113. data/spec/network/http/http_spec.rb +280 -0
  114. data/spec/network/http/proxy_spec.rb +150 -0
  115. data/spec/network/network_spec.rb +8 -0
  116. data/spec/network/ssl_spec.rb +14 -0
  117. data/spec/path_spec.rb +84 -0
  118. data/spec/spec_helper.rb +4 -0
  119. data/spec/support_spec.rb +8 -0
  120. data/spec/templates/classes/example_erb.rb +11 -0
  121. data/spec/templates/classes/example_template.rb +35 -0
  122. data/spec/templates/erb_spec.rb +21 -0
  123. data/spec/templates/helpers/data.rb +9 -0
  124. data/spec/templates/helpers/data/includes/_relative.erb +1 -0
  125. data/spec/templates/helpers/data/templates/example.erb +1 -0
  126. data/spec/templates/template_spec.rb +54 -0
  127. metadata +286 -0
@@ -0,0 +1,22 @@
1
+ #
2
+ # Ronin - A Ruby platform for exploit development and security research.
3
+ #
4
+ # Copyright (c) 2006-2010 Hal Brodigan (postmodern.mod3 at gmail.com)
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library 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 GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
+ # Boston, MA 02110-1301 USA
20
+ #
21
+
22
+ require 'ronin/network/extensions/udp/net'
@@ -0,0 +1,226 @@
1
+ #
2
+ # Ronin - A Ruby platform for exploit development and security research.
3
+ #
4
+ # Copyright (c) 2006-2010 Hal Brodigan (postmodern.mod3 at gmail.com)
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library 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 GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
+ # Boston, MA 02110-1301 USA
20
+ #
21
+
22
+ require 'socket'
23
+
24
+ module Net
25
+ #
26
+ # Creates a new UDPSocket object connected to a given host and port.
27
+ #
28
+ # @param [String] host
29
+ # The host to connect to.
30
+ #
31
+ # @param [Integer] port
32
+ # The port to connect to.
33
+ #
34
+ # @param [String] local_host (nil)
35
+ # The local host to bind to.
36
+ #
37
+ # @param [Integer] local_port (nil)
38
+ # The local port to bind to.
39
+ #
40
+ # @yield [socket]
41
+ # If a block is given, it will be passed the newly created socket.
42
+ #
43
+ # @yieldparam [UDPsocket] socket
44
+ # The newly created UDPSocket object.
45
+ #
46
+ # @return [UDPSocket]
47
+ # The newly created UDPSocket object.
48
+ #
49
+ # @example
50
+ # Net.udp_connect('www.hackety.org',80)
51
+ # # => UDPSocket
52
+ #
53
+ # @example
54
+ # Net.udp_connect('www.wired.com',80) do |sock|
55
+ # puts sock.readlines
56
+ # end
57
+ #
58
+ def Net.udp_connect(host,port,local_host=nil,local_port=nil)
59
+ host = host.to_s
60
+ local_host = if local_host
61
+ local_host.to_s
62
+ end
63
+
64
+ sock = UDPSocket.new(host,port,local_host,local_port)
65
+
66
+ yield sock if block_given?
67
+ return sock
68
+ end
69
+
70
+ #
71
+ # Creates a new UDPSocket object, connected to a given host and port.
72
+ # The given data will then be written to the newly created UDPSocket.
73
+ #
74
+ # @param [String] data
75
+ # The data to send through the connection.
76
+ #
77
+ # @param [String] host
78
+ # The host to connect to.
79
+ #
80
+ # @param [Integer] port
81
+ # The port to connect to.
82
+ #
83
+ # @param [String] local_host (nil)
84
+ # The local host to bind to.
85
+ #
86
+ # @param [Integer] local_port (nil)
87
+ # The local port to bind to.
88
+ #
89
+ # @yield [socket]
90
+ # If a block is given, it will be passed the newly created socket.
91
+ #
92
+ # @yieldparam [UDPsocket] socket
93
+ # The newly created UDPSocket object.
94
+ #
95
+ # @return [UDPSocket]
96
+ # The newly created UDPSocket object.
97
+ #
98
+ def Net.udp_connect_and_send(data,host,port,local_host=nil,local_port=nil)
99
+ sock = Net.udp_connect(host,port,local_host,local_port)
100
+ sock.write(data)
101
+
102
+ yield sock if block_given?
103
+ return sock
104
+ end
105
+
106
+ #
107
+ # Creates a new temporary UDPSocket object, connected to the given host
108
+ # and port.
109
+ #
110
+ # @param [String] host
111
+ # The host to connect to.
112
+ #
113
+ # @param [Integer] port
114
+ # The port to connect to.
115
+ #
116
+ # @param [String] local_host (nil)
117
+ # The local host to bind to.
118
+ #
119
+ # @param [Integer] local_port (nil)
120
+ # The local port to bind to.
121
+ #
122
+ # @yield [socket]
123
+ # If a block is given, it will be passed the newly created socket.
124
+ # After the block has returned, the socket will then be closed.
125
+ #
126
+ # @yieldparam [UDPsocket] socket
127
+ # The newly created UDPSocket object.
128
+ #
129
+ # @return [nil]
130
+ #
131
+ def Net.udp_session(host,port,local_host=nil,local_port=nil)
132
+ sock = Net.udp_connect(host,port,local_host,local_port)
133
+
134
+ yield sock if block_given?
135
+
136
+ sock.close
137
+ return nil
138
+ end
139
+
140
+ #
141
+ # Reads the banner from the service running on the given host and port.
142
+ #
143
+ # @param [String] host
144
+ # The host to connect to.
145
+ #
146
+ # @param [Integer] port
147
+ # The port to connect to.
148
+ #
149
+ # @param [String] local_host (nil)
150
+ # The local host to bind to.
151
+ #
152
+ # @param [Integer] local_port (nil)
153
+ # The local port to bind to.
154
+ #
155
+ # @yield [banner]
156
+ # If a block is given, it will be passed the grabbed banner.
157
+ #
158
+ # @yieldparam [String] banner
159
+ # The grabbed banner.
160
+ #
161
+ # @return [String]
162
+ # The grabbed banner.
163
+ #
164
+ def Net.udp_banner(host,port,local_host=nil,local_port=nil)
165
+ banner = nil
166
+
167
+ Net.udp_session(host,port,local_host,local_port) do |sock|
168
+ banner = sock.readline
169
+ end
170
+
171
+ yield banner if block_given?
172
+ return banner
173
+ end
174
+
175
+ #
176
+ # Creates a new UDPServer listening on a given host and port.
177
+ #
178
+ # @param [Integer] port
179
+ # The local port to listen on.
180
+ #
181
+ # @param [String] host ('0.0.0.0')
182
+ # The host to bind to.
183
+ #
184
+ # @return [UDPServer]
185
+ # The new UDP server.
186
+ #
187
+ # @example
188
+ # Net.udp_server(1337)
189
+ #
190
+ def Net.udp_server(port,host='0.0.0.0')
191
+ host = host.to_s
192
+ server = UDPServer.new(host,port)
193
+
194
+ yield server if block_given?
195
+ return server
196
+ end
197
+
198
+ #
199
+ # Creates a new temporary UDPServer listening on a given host and port.
200
+ #
201
+ # @param [Integer] port
202
+ # The local port to bind to.
203
+ #
204
+ # @param [String] host ('0.0.0.0')
205
+ # The host to bind to.
206
+ #
207
+ # @yield [server]
208
+ # The block which will be called after the _server_ has been created.
209
+ # After the block has finished, the _server_ will be closed.
210
+ #
211
+ # @yieldparam [UDPServer] server
212
+ # The newly created UDP server.
213
+ #
214
+ # @return [nil]
215
+ #
216
+ # @example
217
+ # Net.udp_server_session(1337) do |server|
218
+ # data, sender = server.recvfrom(1024)
219
+ # end
220
+ #
221
+ def Net.udp_server_session(port,host='0.0.0.0',&block)
222
+ server = Net.udp_server(port,host,&block)
223
+ server.close()
224
+ return nil
225
+ end
226
+ end
@@ -0,0 +1,24 @@
1
+ #
2
+ # Ronin - A Ruby platform for exploit development and security research.
3
+ #
4
+ # Copyright (c) 2006-2010 Hal Brodigan (postmodern.mod3 at gmail.com)
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library 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 GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
+ # Boston, MA 02110-1301 USA
20
+ #
21
+
22
+ require 'ronin/network/http/exceptions'
23
+ require 'ronin/network/http/http'
24
+ require 'ronin/network/extensions/http'
@@ -0,0 +1,22 @@
1
+ #
2
+ # Ronin - A Ruby platform for exploit development and security research.
3
+ #
4
+ # Copyright (c) 2006-2010 Hal Brodigan (postmodern.mod3 at gmail.com)
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library 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 GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
+ # Boston, MA 02110-1301 USA
20
+ #
21
+
22
+ require 'ronin/network/http/exceptions/unknown_request'
@@ -0,0 +1,29 @@
1
+ #
2
+ # Ronin - A Ruby platform for exploit development and security research.
3
+ #
4
+ # Copyright (c) 2006-2010 Hal Brodigan (postmodern.mod3 at gmail.com)
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library 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 GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
+ # Boston, MA 02110-1301 USA
20
+ #
21
+
22
+ module Ronin
23
+ module Network
24
+ module HTTP
25
+ class UnknownRequest < StandardError
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,290 @@
1
+ #
2
+ # Ronin - A Ruby platform for exploit development and security research.
3
+ #
4
+ # Copyright (c) 2006-2010 Hal Brodigan (postmodern.mod3 at gmail.com)
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library 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 GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
+ # Boston, MA 02110-1301 USA
20
+ #
21
+
22
+ require 'ronin/network/http/exceptions/unknown_request'
23
+ require 'ronin/network/http/proxy'
24
+ require 'ronin/network/extensions/http'
25
+
26
+ module Ronin
27
+ module Network
28
+ module HTTP
29
+ #
30
+ # The Ronin HTTP proxy to use. Parses the value of the `HTTP_PROXY`
31
+ # environment variable if set.
32
+ #
33
+ # @return [Proxy]
34
+ # The Ronin HTTP proxy.
35
+ #
36
+ # @see Proxy.new
37
+ # @see Proxy.parse
38
+ #
39
+ def HTTP.proxy
40
+ @proxy ||= if ENV['HTTP_PROXY']
41
+ Proxy.parse(ENV['HTTP_PROXY'])
42
+ else
43
+ Proxy.new
44
+ end
45
+ end
46
+
47
+ #
48
+ # Sets the Ronin HTTP proxy to use.
49
+ #
50
+ # @param [Proxy, URI::HTTP, Hash, String] new_proxy
51
+ # The new proxy information to use.
52
+ #
53
+ # @return [Proxy]
54
+ # The new proxy.
55
+ #
56
+ # @raise [ArgumentError]
57
+ # The given proxy information was not a {Proxy}, `URI::HTTP`,
58
+ # `Hash` or {String}.
59
+ #
60
+ def HTTP.proxy=(new_proxy)
61
+ @proxy = Proxy.create(new_proxy)
62
+ end
63
+
64
+ #
65
+ # The default Ronin HTTP User-Agent string.
66
+ #
67
+ # @return [String, nil]
68
+ # The default Ronin HTTP User-Agent.
69
+ #
70
+ def HTTP.user_agent
71
+ @user_agent ||= nil
72
+ end
73
+
74
+ #
75
+ # Sets the default Ronin HTTP User-Agent string.
76
+ #
77
+ # @param [String] agent
78
+ # The new User-Agent string to use.
79
+ #
80
+ def HTTP.user_agent=(agent)
81
+ @user_agent = agent
82
+ end
83
+
84
+ #
85
+ # Expands the URL into options.
86
+ #
87
+ # @param [URI::HTTP, String, nil] url
88
+ # The URL to expand.
89
+ #
90
+ # @return [Hash{Symbol => Object}]
91
+ # The options for the URL.
92
+ #
93
+ def HTTP.expand_url(url)
94
+ new_options = {
95
+ :port => Net::HTTP.default_port,
96
+ :path => '/'
97
+ }
98
+
99
+ if url
100
+ url = case url
101
+ when URI
102
+ url
103
+ when Hash
104
+ URI::HTTP.build(url)
105
+ else
106
+ URI(url.to_s)
107
+ end
108
+
109
+ new_options[:ssl] = {} if url.scheme == 'https'
110
+
111
+ new_options[:host] = url.host
112
+ new_options[:port] = url.port
113
+
114
+ new_options[:user] = url.user if url.user
115
+ new_options[:password] = url.password if url.password
116
+
117
+ new_options[:path] = url.path unless url.path.empty?
118
+ new_options[:path] += "?#{url.query}" if url.query
119
+ end
120
+
121
+ return new_options
122
+ end
123
+
124
+ #
125
+ # Expands the given HTTP options.
126
+ #
127
+ # @param [Hash] options
128
+ # HTTP options.
129
+ #
130
+ # @option options [String, URI::HTTP, URI::HTTPS] :url
131
+ # The URL to request.
132
+ #
133
+ # @option options [String] :host
134
+ # The host to connect to.
135
+ #
136
+ # @option options [String] :port (::Net::HTTP.default_port)
137
+ # The port to connect to.
138
+ #
139
+ # @option options [String] :user
140
+ # The user to authenticate as.
141
+ #
142
+ # @option options [String] :password
143
+ # The password to authenticate with.
144
+ #
145
+ # @option options [String] :path ('/')
146
+ # The path to request.
147
+ #
148
+ # @option options [String, Hash] :proxy (Ronin::Network::HTTP.proxy)
149
+ # The Proxy information.
150
+ #
151
+ # @return [Hash]
152
+ # The expanded version of options.
153
+ #
154
+ def HTTP.expand_options(options={})
155
+ new_options = options.dup
156
+
157
+ if new_options[:ssl] == true
158
+ new_options[:ssl] = {}
159
+ end
160
+
161
+ url = new_options.delete(:url)
162
+ new_options.merge!(HTTP.expand_url(url))
163
+
164
+ new_options[:proxy] = if new_options.has_key?(:proxy)
165
+ HTTP::Proxy.create(new_options[:proxy])
166
+ else
167
+ HTTP.proxy
168
+ end
169
+
170
+ return new_options
171
+ end
172
+
173
+ #
174
+ # Converts an underscored, dashed, lowercase or uppercase HTTP header
175
+ # name to the standard camel-case HTTP header name.
176
+ #
177
+ # @param [Symbol, String] name
178
+ # The unformatted HTTP header name.
179
+ #
180
+ # @return [String]
181
+ # The camel-case HTTP header name.
182
+ #
183
+ def HTTP.header_name(name)
184
+ name.to_s.split(/[\s+_-]/).map { |word| word.capitalize }.join('-')
185
+ end
186
+
187
+ #
188
+ # Converts underscored, dashed, lowercase and uppercase HTTP headers
189
+ # to standard camel-cased HTTP headers.
190
+ #
191
+ # @param [Hash{Symbol,String => String}] options
192
+ # Ronin HTTP headers.
193
+ #
194
+ # @return [Hash]
195
+ # The camel-cased HTTP headers created from the given options.
196
+ #
197
+ def HTTP.headers(options={})
198
+ headers = {}
199
+
200
+ if HTTP.user_agent
201
+ headers['User-Agent'] = HTTP.user_agent
202
+ end
203
+
204
+ if options
205
+ options.each do |name,value|
206
+ headers[HTTP.header_name(name)] = value.to_s
207
+ end
208
+ end
209
+
210
+ return headers
211
+ end
212
+
213
+ #
214
+ # Creates a specific type of HTTP request object.
215
+ #
216
+ # @param [Hash] options
217
+ # The HTTP options for the request.
218
+ #
219
+ # @option options [Symbol, String] :method
220
+ # The HTTP method to use for the request.
221
+ #
222
+ # @option options [String] :path ('/')
223
+ # The path to request.
224
+ #
225
+ # @option options [String] :body
226
+ # The body of the request.
227
+ #
228
+ # @option options [Hash, String] :form_data
229
+ # The form data that may be sent in the body of the request.
230
+ #
231
+ # @option options [String] :user
232
+ # The user to authenticate as.
233
+ #
234
+ # @option options [String] :password
235
+ # The password to authenticate with.
236
+ #
237
+ # @option options [Hash{Symbol,String => String}] :headers
238
+ # Additional HTTP headers to use for the request.
239
+ #
240
+ # @return [HTTP::Request]
241
+ # The new HTTP Request object.
242
+ #
243
+ # @raise [ArgumentError]
244
+ # The `:method` option must be specified.
245
+ #
246
+ # @raise [UnknownRequest]
247
+ # The `:method` option did not match a known Net::HTTP request
248
+ # class.
249
+ #
250
+ # @see HTTP.expand_options
251
+ #
252
+ def HTTP.request(options={})
253
+ unless options[:method]
254
+ raise(ArgumentError,"the :method option must be specified")
255
+ end
256
+
257
+ name = options[:method].to_s.capitalize
258
+
259
+ unless Net::HTTP.const_defined?(name)
260
+ raise(UnknownRequest,"unknown HTTP request type #{name.dump}")
261
+ end
262
+
263
+ headers = HTTP.headers(options[:headers])
264
+ path = (options[:path] || '/').to_s
265
+
266
+ request = Net::HTTP.const_get(name).new(path,headers)
267
+
268
+ if request.request_body_permitted?
269
+ if options[:form_data]
270
+ request.set_form_data(options[:form_data])
271
+ elsif options[:body]
272
+ request.body = options[:body]
273
+ end
274
+ end
275
+
276
+ if (user = options.delete(:user))
277
+ user = user.to_s
278
+
279
+ if (password = options.delete(:password))
280
+ password = password.to_s
281
+ end
282
+
283
+ request.basic_auth(user,password)
284
+ end
285
+
286
+ return request
287
+ end
288
+ end
289
+ end
290
+ end