ronin-support 0.4.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (224) hide show
  1. checksums.yaml +7 -0
  2. data/.editorconfig +11 -0
  3. data/.github/workflows/ruby.yml +28 -0
  4. data/.ruby-version +1 -0
  5. data/.yardopts +1 -1
  6. data/ChangeLog.md +121 -33
  7. data/Gemfile +17 -18
  8. data/README.md +57 -33
  9. data/Rakefile +10 -3
  10. data/gemspec.yml +16 -7
  11. data/lib/ronin/binary.rb +21 -0
  12. data/lib/ronin/binary/hexdump.rb +20 -0
  13. data/lib/ronin/binary/hexdump/parser.rb +403 -0
  14. data/lib/ronin/binary/struct.rb +567 -0
  15. data/lib/ronin/binary/template.rb +454 -0
  16. data/lib/ronin/extensions.rb +5 -5
  17. data/lib/ronin/extensions/enumerable.rb +5 -5
  18. data/lib/ronin/extensions/file.rb +39 -33
  19. data/lib/ronin/extensions/ip_addr.rb +29 -31
  20. data/lib/ronin/extensions/kernel.rb +5 -5
  21. data/lib/ronin/extensions/meta.rb +5 -5
  22. data/lib/ronin/extensions/regexp.rb +50 -5
  23. data/lib/ronin/extensions/resolv.rb +7 -9
  24. data/lib/ronin/extensions/string.rb +10 -10
  25. data/lib/ronin/formatting.rb +5 -5
  26. data/lib/ronin/formatting/binary.rb +5 -5
  27. data/lib/ronin/formatting/digest.rb +5 -5
  28. data/lib/ronin/formatting/extensions.rb +5 -5
  29. data/lib/ronin/formatting/extensions/binary.rb +7 -5
  30. data/lib/ronin/formatting/extensions/binary/array.rb +61 -0
  31. data/lib/ronin/formatting/extensions/binary/base64.rb +106 -0
  32. data/lib/ronin/formatting/extensions/binary/file.rb +44 -11
  33. data/lib/ronin/formatting/extensions/binary/float.rb +65 -0
  34. data/lib/ronin/formatting/extensions/binary/integer.rb +66 -50
  35. data/lib/ronin/formatting/extensions/binary/string.rb +81 -205
  36. data/lib/ronin/formatting/extensions/digest.rb +5 -5
  37. data/lib/ronin/formatting/extensions/digest/file.rb +5 -5
  38. data/lib/ronin/formatting/extensions/digest/string.rb +5 -5
  39. data/lib/ronin/formatting/extensions/html.rb +5 -5
  40. data/lib/ronin/formatting/extensions/html/integer.rb +9 -13
  41. data/lib/ronin/formatting/extensions/html/string.rb +31 -39
  42. data/lib/ronin/formatting/extensions/http.rb +5 -5
  43. data/lib/ronin/formatting/extensions/http/integer.rb +6 -6
  44. data/lib/ronin/formatting/extensions/http/string.rb +7 -7
  45. data/lib/ronin/formatting/extensions/sql.rb +5 -5
  46. data/lib/ronin/formatting/extensions/sql/string.rb +22 -24
  47. data/lib/ronin/formatting/extensions/text.rb +5 -5
  48. data/lib/ronin/formatting/extensions/text/array.rb +13 -11
  49. data/lib/ronin/formatting/extensions/text/string.rb +70 -13
  50. data/lib/ronin/formatting/html.rb +5 -5
  51. data/lib/ronin/formatting/http.rb +5 -5
  52. data/lib/ronin/formatting/sql.rb +5 -5
  53. data/lib/ronin/formatting/text.rb +5 -5
  54. data/lib/ronin/fuzzing.rb +5 -5
  55. data/lib/ronin/fuzzing/extensions.rb +5 -5
  56. data/lib/ronin/fuzzing/extensions/string.rb +42 -213
  57. data/lib/ronin/fuzzing/fuzzer.rb +110 -0
  58. data/lib/ronin/fuzzing/fuzzing.rb +33 -26
  59. data/lib/ronin/fuzzing/mutator.rb +161 -0
  60. data/lib/ronin/fuzzing/repeater.rb +81 -0
  61. data/lib/ronin/fuzzing/template.rb +133 -0
  62. data/lib/ronin/mixin.rb +2 -2
  63. data/lib/ronin/network.rb +7 -5
  64. data/lib/ronin/network/dns.rb +64 -24
  65. data/lib/ronin/network/esmtp.rb +5 -5
  66. data/lib/ronin/network/extensions.rb +5 -5
  67. data/lib/ronin/network/extensions/dns.rb +5 -5
  68. data/lib/ronin/network/extensions/dns/net.rb +5 -5
  69. data/lib/ronin/network/extensions/esmtp.rb +5 -5
  70. data/lib/ronin/network/extensions/esmtp/net.rb +5 -5
  71. data/lib/ronin/network/extensions/http.rb +5 -5
  72. data/lib/ronin/network/extensions/http/net.rb +5 -5
  73. data/lib/ronin/network/extensions/http/uri/http.rb +5 -5
  74. data/lib/ronin/network/extensions/imap.rb +5 -5
  75. data/lib/ronin/network/extensions/imap/net.rb +5 -5
  76. data/lib/ronin/network/extensions/pop3.rb +5 -5
  77. data/lib/ronin/network/extensions/pop3/net.rb +5 -5
  78. data/lib/ronin/network/extensions/smtp.rb +5 -5
  79. data/lib/ronin/network/extensions/smtp/net.rb +5 -5
  80. data/lib/ronin/network/extensions/ssl.rb +5 -5
  81. data/lib/ronin/network/extensions/ssl/net.rb +5 -5
  82. data/lib/ronin/network/extensions/tcp.rb +5 -5
  83. data/lib/ronin/network/extensions/tcp/net.rb +5 -5
  84. data/lib/ronin/network/extensions/telnet.rb +5 -5
  85. data/lib/ronin/network/extensions/telnet/net.rb +5 -5
  86. data/lib/ronin/network/extensions/udp.rb +5 -5
  87. data/lib/ronin/network/extensions/udp/net.rb +5 -5
  88. data/lib/ronin/network/ftp.rb +149 -0
  89. data/lib/ronin/network/http.rb +5 -5
  90. data/lib/ronin/network/http/exceptions.rb +5 -5
  91. data/lib/ronin/network/http/exceptions/unknown_request.rb +5 -5
  92. data/lib/ronin/network/http/http.rb +65 -70
  93. data/lib/ronin/network/http/proxy.rb +5 -5
  94. data/lib/ronin/network/imap.rb +16 -15
  95. data/lib/ronin/network/mixins.rb +6 -5
  96. data/lib/ronin/network/mixins/dns.rb +5 -5
  97. data/lib/ronin/network/mixins/esmtp.rb +5 -5
  98. data/lib/ronin/network/mixins/ftp.rb +155 -0
  99. data/lib/ronin/network/mixins/http.rb +58 -587
  100. data/lib/ronin/network/mixins/imap.rb +5 -5
  101. data/lib/ronin/network/mixins/mixin.rb +5 -5
  102. data/lib/ronin/network/mixins/pop3.rb +5 -5
  103. data/lib/ronin/network/mixins/smtp.rb +5 -5
  104. data/lib/ronin/network/mixins/ssl.rb +5 -5
  105. data/lib/ronin/network/mixins/tcp.rb +43 -10
  106. data/lib/ronin/network/mixins/telnet.rb +5 -5
  107. data/lib/ronin/network/mixins/udp.rb +126 -6
  108. data/lib/ronin/network/mixins/unix.rb +279 -0
  109. data/lib/ronin/network/network.rb +5 -5
  110. data/lib/ronin/network/pop3.rb +10 -10
  111. data/lib/ronin/network/proxy.rb +578 -0
  112. data/lib/ronin/network/smtp.rb +5 -5
  113. data/lib/ronin/network/smtp/email.rb +6 -6
  114. data/lib/ronin/network/smtp/smtp.rb +12 -13
  115. data/lib/ronin/network/ssl.rb +16 -17
  116. data/lib/ronin/network/tcp.rb +7 -310
  117. data/lib/ronin/network/tcp/proxy.rb +417 -0
  118. data/lib/ronin/network/tcp/tcp.rb +452 -0
  119. data/lib/ronin/network/telnet.rb +34 -28
  120. data/lib/ronin/network/udp.rb +7 -271
  121. data/lib/ronin/network/udp/proxy.rb +191 -0
  122. data/lib/ronin/network/udp/udp.rb +452 -0
  123. data/lib/ronin/network/unix.rb +286 -0
  124. data/lib/ronin/path.rb +35 -39
  125. data/lib/ronin/spec/ui/output.rb +6 -12
  126. data/lib/ronin/support.rb +6 -5
  127. data/lib/ronin/support/inflector.rb +8 -12
  128. data/lib/ronin/support/support.rb +7 -5
  129. data/lib/ronin/support/version.rb +6 -6
  130. data/lib/ronin/templates.rb +5 -5
  131. data/lib/ronin/templates/erb.rb +5 -5
  132. data/lib/ronin/templates/template.rb +5 -5
  133. data/lib/ronin/ui/output.rb +5 -5
  134. data/lib/ronin/ui/output/helpers.rb +42 -28
  135. data/lib/ronin/ui/output/output.rb +17 -21
  136. data/lib/ronin/ui/output/terminal.rb +5 -5
  137. data/lib/ronin/ui/output/terminal/color.rb +15 -9
  138. data/lib/ronin/ui/output/terminal/raw.rb +5 -5
  139. data/lib/ronin/ui/shell.rb +8 -11
  140. data/lib/ronin/wordlist.rb +110 -30
  141. data/ronin-support.gemspec +39 -109
  142. data/spec/binary/hexdump/helpers/hexdumps.rb +13 -0
  143. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/ascii.bin +0 -0
  144. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_decimal_shorts.txt +0 -0
  145. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_hex_bytes.txt +0 -0
  146. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_hex_shorts.txt +0 -0
  147. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_octal_bytes.txt +0 -0
  148. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_octal_shorts.txt +0 -0
  149. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_repeated.txt +0 -0
  150. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_decimal_bytes.txt +0 -0
  151. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_decimal_ints.txt +0 -0
  152. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_decimal_quads.txt +0 -0
  153. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_decimal_shorts.txt +0 -0
  154. data/spec/binary/hexdump/helpers/hexdumps/od_doubles.txt +17 -0
  155. data/spec/binary/hexdump/helpers/hexdumps/od_floats.txt +17 -0
  156. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_hex_bytes.txt +0 -0
  157. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_hex_ints.txt +0 -0
  158. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_hex_quads.txt +0 -0
  159. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_hex_shorts.txt +0 -0
  160. data/spec/binary/hexdump/helpers/hexdumps/od_named_chars.txt +17 -0
  161. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_octal_bytes.txt +0 -0
  162. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_octal_ints.txt +0 -0
  163. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_octal_quads.txt +0 -0
  164. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_octal_shorts.txt +0 -0
  165. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_repeated.txt +0 -0
  166. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/repeated.bin +0 -0
  167. data/spec/binary/hexdump/parser_spec.rb +302 -0
  168. data/spec/binary/struct_spec.rb +496 -0
  169. data/spec/binary/template_spec.rb +416 -0
  170. data/spec/extensions/enumerable_spec.rb +4 -4
  171. data/spec/extensions/file_spec.rb +12 -14
  172. data/spec/extensions/ip_addr_spec.rb +76 -50
  173. data/spec/extensions/kernel_spec.rb +7 -7
  174. data/spec/extensions/regexp_spec.rb +119 -59
  175. data/spec/extensions/resolv_spec.rb +2 -2
  176. data/spec/extensions/string_spec.rb +31 -30
  177. data/spec/formatting/binary/array_spec.rb +26 -0
  178. data/spec/formatting/binary/base64_spec.rb +50 -0
  179. data/spec/formatting/binary/float_spec.rb +36 -0
  180. data/spec/formatting/binary/integer_spec.rb +76 -50
  181. data/spec/formatting/binary/string_spec.rb +91 -198
  182. data/spec/formatting/digest/string_spec.rb +5 -5
  183. data/spec/formatting/html/integer_spec.rb +6 -6
  184. data/spec/formatting/html/string_spec.rb +10 -10
  185. data/spec/formatting/http/integer_spec.rb +3 -3
  186. data/spec/formatting/http/string_spec.rb +5 -5
  187. data/spec/formatting/sql/string_spec.rb +21 -19
  188. data/spec/formatting/text/array_spec.rb +15 -15
  189. data/spec/formatting/text/string_spec.rb +58 -28
  190. data/spec/fuzzing/extensions/string_spec.rb +87 -0
  191. data/spec/fuzzing/fuzzer_spec.rb +109 -0
  192. data/spec/fuzzing/fuzzing_spec.rb +24 -0
  193. data/spec/fuzzing/mutator_spec.rb +112 -0
  194. data/spec/fuzzing/repeater_spec.rb +57 -0
  195. data/spec/fuzzing/template_spec.rb +54 -0
  196. data/spec/mixin_spec.rb +10 -12
  197. data/spec/network/dns_spec.rb +89 -23
  198. data/spec/network/ftp_spec.rb +81 -0
  199. data/spec/network/http/http_spec.rb +237 -144
  200. data/spec/network/http/proxy_spec.rb +37 -37
  201. data/spec/network/network_spec.rb +2 -2
  202. data/spec/network/proxy_spec.rb +121 -0
  203. data/spec/network/shared/unix_server.rb +31 -0
  204. data/spec/network/smtp/email_spec.rb +14 -14
  205. data/spec/network/ssl_spec.rb +53 -3
  206. data/spec/network/tcp/proxy_spec.rb +118 -0
  207. data/spec/network/tcp/tcp_spec.rb +316 -0
  208. data/spec/network/telnet_spec.rb +67 -0
  209. data/spec/network/udp/udp_spec.rb +298 -0
  210. data/spec/network/unix_spec.rb +182 -0
  211. data/spec/path_spec.rb +43 -18
  212. data/spec/spec_helper.rb +2 -3
  213. data/spec/support/inflector_spec.rb +4 -4
  214. data/spec/support_spec.rb +1 -1
  215. data/spec/templates/erb_spec.rb +3 -3
  216. data/spec/templates/template_spec.rb +10 -10
  217. data/spec/ui/shell_spec.rb +15 -15
  218. data/spec/wordlist_spec.rb +80 -19
  219. metadata +176 -121
  220. data/.gemtest +0 -0
  221. data/spec/formatting/binary/helpers/hexdumps.rb +0 -16
  222. data/spec/fuzzing/string_spec.rb +0 -158
  223. data/spec/network/tcp_spec.rb +0 -247
  224. data/spec/network/udp_spec.rb +0 -248
@@ -0,0 +1,417 @@
1
+ #
2
+ # Copyright (c) 2006-2021 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 <https://www.gnu.org/licenses/>.
18
+ #
19
+
20
+ require 'ronin/network/proxy'
21
+
22
+ require 'socket'
23
+
24
+ module Ronin
25
+ module Network
26
+ module TCP
27
+ #
28
+ # The TCP Proxy allows for inspecting and manipulating TCP protocols.
29
+ #
30
+ # ## Example
31
+ #
32
+ # require 'ronin/network/tcp/proxy'
33
+ # require 'hexdump'
34
+ #
35
+ # Ronin::Network::TCP::Proxy.start(:port => 1337, :server => ['www.wired.com', 80]) do |proxy|
36
+ # address = lambda { |socket|
37
+ # addrinfo = socket.peeraddr
38
+ #
39
+ # "#{addrinfo[3]}:#{addrinfo[1]}"
40
+ # }
41
+ # hex = Hexdump::Dumper.new
42
+ #
43
+ # proxy.on_client_data do |client,server,data|
44
+ # puts "#{address[client]} -> #{proxy}"
45
+ # hex.dump(data)
46
+ # end
47
+ #
48
+ # proxy.on_client_connect do |client|
49
+ # puts "#{address[client]} -> #{proxy} [connected]"
50
+ # end
51
+ #
52
+ # proxy.on_client_disconnect do |client,server|
53
+ # puts "#{address[client]} <- #{proxy} [disconnected]"
54
+ # end
55
+ #
56
+ # proxy.on_server_data do |client,server,data|
57
+ # puts "#{address[client]} <- #{proxy}"
58
+ # hex.dump(data)
59
+ # end
60
+ #
61
+ # proxy.on_server_connect do |client,server|
62
+ # puts "#{address[client]} <- #{proxy} [connected]"
63
+ # end
64
+ #
65
+ # proxy.on_server_disconnect do |client,server|
66
+ # puts "#{address[client]} <- #{proxy} [disconnected]"
67
+ # end
68
+ # end
69
+ #
70
+ # ## Callbacks
71
+ #
72
+ # In addition to the events supported by the {Network::Proxy Proxy}
73
+ # base class, the TCP Proxy also supports the following callbacks.
74
+ #
75
+ # ### client_connect
76
+ #
77
+ # When a client connects to the proxy:
78
+ #
79
+ # on_client_connect do |client|
80
+ # puts "[connected] #{client.remote_address.ip_address}:#{client.remote_addre
81
+ # end
82
+ #
83
+ # ### client_disconnect
84
+ #
85
+ # When a client disconnects from the proxy:
86
+ #
87
+ # on_client_disconnect do |client,server|
88
+ # puts "[disconnected] #{client.remote_address.ip_address}:#{client.remote_ad
89
+ # end
90
+ #
91
+ # ### server_connect
92
+ #
93
+ # When the server accepts a connection from the proxy:
94
+ #
95
+ # on_server_connect do |client,server|
96
+ # puts "[connected] #{proxy}"
97
+ # end
98
+ #
99
+ # ### server_disconnect
100
+ #
101
+ # When the server closes a connection from the proxy.
102
+ #
103
+ # on_server_disconnect do |client,server|
104
+ # puts "[disconnected] #{proxy}"
105
+ # end
106
+ #
107
+ # ### connect
108
+ #
109
+ # Alias for {#on_server_connect}.
110
+ #
111
+ # ### disconnect
112
+ #
113
+ # Alias for {#on_client_disconnect}.
114
+ #
115
+ # @since 0.5.0
116
+ #
117
+ class Proxy < Network::Proxy
118
+
119
+ #
120
+ # Creates a new TCP Proxy.
121
+ #
122
+ # @see Network::Proxy#initialize
123
+ #
124
+ def initialize(options={})
125
+ super(options) do |proxy|
126
+ @callbacks[:client_connect] = []
127
+ @callbacks[:client_disconnect] = []
128
+ @callbacks[:server_connect] = []
129
+ @callbacks[:server_disconnect] = []
130
+
131
+ yield proxy if block_given?
132
+ end
133
+ end
134
+
135
+ #
136
+ # Opens the proxy.
137
+ #
138
+ # @api public
139
+ #
140
+ def open
141
+ @socket = TCPServer.new(@host,@port)
142
+ end
143
+
144
+ #
145
+ # Polls the connections for data.
146
+ #
147
+ # @api public
148
+ #
149
+ def poll
150
+ sockets = [@socket] + client_connections + server_connections
151
+
152
+ readable, writtable, errors = IO.select(sockets,nil,sockets)
153
+
154
+ (errors & client_connections).each do |client_socket|
155
+ server_socket = server_connection_for(client_socket)
156
+
157
+ client_disconnect(client_socket,server_socket)
158
+ end
159
+
160
+ (errors & server_connections).each do |server_socket|
161
+ client_socket = client_connection_for(server_socket)
162
+
163
+ server_disconnect(client_socket,server_socket)
164
+ end
165
+
166
+ (readable & client_connections).each do |client_socket|
167
+ server_socket = server_connection_for(client_socket)
168
+ data = recv(client_socket)
169
+
170
+ unless data.empty?
171
+ client_data(client_socket,server_socket,data)
172
+ else
173
+ client_disconnect(client_socket,server_socket)
174
+ end
175
+ end
176
+
177
+ (readable & server_connections).each do |server_socket|
178
+ client_socket = client_connection_for(server_socket)
179
+ data = recv(server_socket)
180
+
181
+ unless data.empty?
182
+ server_data(client_socket,server_socket,data)
183
+ else
184
+ server_disconnect(client_socket,server_socket)
185
+ end
186
+ end
187
+
188
+ if readable.include?(@socket)
189
+ client_connect(@socket.accept)
190
+ end
191
+ end
192
+
193
+ #
194
+ # Sends data to a connection.
195
+ #
196
+ # @param [TCPSocket] connection
197
+ # A TCP connection to write data to.
198
+ #
199
+ # @param [String] data
200
+ # The data to write.
201
+ #
202
+ # @api public
203
+ #
204
+ def send(connection,data)
205
+ connection.send(data,0)
206
+ end
207
+
208
+ #
209
+ # Receives data from a connection.
210
+ #
211
+ # @param [TCPSocket] connection
212
+ # The TCP connection to receive data from.
213
+ #
214
+ # @return [String, nil]
215
+ # The received data.
216
+ #
217
+ # @api public
218
+ #
219
+ def recv(connection)
220
+ connection.recv(@buffer_size)
221
+ end
222
+
223
+ #
224
+ # Registers a callback for when a client connects.
225
+ #
226
+ # @yield [client]
227
+ # The block will be passed each newly connected client.
228
+ #
229
+ # @yieldparam [TCPSocket] client
230
+ # The connection from the client to the proxy.
231
+ #
232
+ # @example
233
+ # on_client_connect do |client|
234
+ # puts "[connected] #{client.remote_address.ip_address}:#{client.remote_address.ip_port}"
235
+ # end
236
+ #
237
+ # @api public
238
+ #
239
+ def on_client_connect(&block)
240
+ @callbacks[:client_connect] << block
241
+ end
242
+
243
+ #
244
+ # Registers a callback for when a client disconnects.
245
+ #
246
+ # @yield [client, server]
247
+ # The block will be passed each disconnected client and their
248
+ # connection to the server.
249
+ #
250
+ # @yieldparam [TCPSocket] client
251
+ # The connection from the client to the proxy.
252
+ #
253
+ # @yieldparam [TCPSocket] server
254
+ # The connection from the proxy to the server.
255
+ #
256
+ # @example
257
+ # on_client_disconnect do |client,server|
258
+ # puts "[disconnected] #{client.remote_address.ip_address}:#{client.remote_address.ip_port}"
259
+ # end
260
+ #
261
+ # @api public
262
+ #
263
+ def on_client_disconnect(&block)
264
+ @callbacks[:client_disconnect] << block
265
+ end
266
+
267
+ alias on_disconnect on_client_disconnect
268
+
269
+ #
270
+ # Registers a callback for when the server accepts a connection.
271
+ #
272
+ # @yield [client, server]
273
+ # The block will be passed each connected client and their newly
274
+ # formed connection to the server.
275
+ #
276
+ # @yieldparam [TCPSocket] client
277
+ # The connection from the client to the proxy.
278
+ #
279
+ # @yieldparam [TCPSocket] server
280
+ # The connection from the proxy to the server.
281
+ #
282
+ # @example
283
+ # on_server_connect do |client,server|
284
+ # puts "[connected] #{proxy}"
285
+ # end
286
+ #
287
+ # @api public
288
+ #
289
+ def on_server_connect(&block)
290
+ @callbacks[:server_connect] << block
291
+ end
292
+
293
+ alias on_connect on_server_connect
294
+
295
+ #
296
+ # Registers a callback for when the server closes a connection.
297
+ #
298
+ # @yield [client, server]
299
+ # The block will be passed the each client connection and the
300
+ # recently disconnected server connection.
301
+ #
302
+ # @yieldparam [TCPSocket] client
303
+ # The connection from the client to the proxy.
304
+ #
305
+ # @yieldparam [TCPSocket] server
306
+ # The connection from the proxy to the server.
307
+ #
308
+ # @example
309
+ # on_server_disconnect do |client,server|
310
+ # puts "[disconnected] #{proxy}"
311
+ # end
312
+ #
313
+ # @api public
314
+ #
315
+ def on_server_disconnect(&block)
316
+ @callbacks[:server_disconnect] << block
317
+ end
318
+
319
+ protected
320
+
321
+ #
322
+ # Creates a new connection to the server.
323
+ #
324
+ # @return [TCPSocket]
325
+ # A new connection.
326
+ #
327
+ def open_server_connection
328
+ TCPSocket.new(@server_host,@server_port)
329
+ end
330
+
331
+ #
332
+ # Closes a connection from the client.
333
+ #
334
+ # @param [TCPSocket] socket
335
+ # The connection from the client.
336
+ #
337
+ def close_client_connection(socket)
338
+ socket.close
339
+ end
340
+
341
+ #
342
+ # Closes a connection to the server.
343
+ #
344
+ # @param [TCPSocket] socket
345
+ # The connection to the server.
346
+ #
347
+ def close_server_connection(socket)
348
+ socket.close
349
+ end
350
+
351
+ #
352
+ # Closes the TCP proxy.
353
+ #
354
+ def close_proxy
355
+ @socket.close
356
+ end
357
+
358
+ #
359
+ # Triggers the `client_connect` event.
360
+ #
361
+ # @param [connection] client_connection
362
+ # The new connection from a client to the proxy.
363
+ #
364
+ def client_connect(client_connection)
365
+ callback(:client_connect,client_connection) do
366
+ server_connect(client_connection)
367
+ end
368
+ end
369
+
370
+ #
371
+ # Triggers the `client_disconnect` event.
372
+ #
373
+ # @param [connection] client_connection
374
+ # The connection from a client to the proxy.
375
+ #
376
+ # @param [connection] server_connection
377
+ # The connection from the proxy to the server.
378
+ #
379
+ def client_disconnect(client_connection,server_connection)
380
+ callback(:client_disconnect,client_connection,server_connection) do
381
+ close_connection(client_connection,server_connection)
382
+ end
383
+ end
384
+
385
+ #
386
+ # Triggers the `server_connect` event.
387
+ #
388
+ # @param [connection] client_connection
389
+ # The connection from a client to the proxy.
390
+ #
391
+ def server_connect(client_connection)
392
+ server_connection = open_server_connection
393
+
394
+ callback(:server_connect,client_connection,server_connection) do
395
+ @connections[client_connection] = server_connection
396
+ end
397
+ end
398
+
399
+ #
400
+ # Triggers the `server_disconnect` event.
401
+ #
402
+ # @param [connection] client_connection
403
+ # The connection from a client to the proxy.
404
+ #
405
+ # @param [connection] server_connection
406
+ # The connection from the proxy to the server.
407
+ #
408
+ def server_disconnect(client_connection,server_connection)
409
+ callback(:server_disconnect,client_connection,server_connection) do
410
+ close_connection(client_connection)
411
+ end
412
+ end
413
+
414
+ end
415
+ end
416
+ end
417
+ end
@@ -0,0 +1,452 @@
1
+ #
2
+ # Copyright (c) 2006-2021 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 <https://www.gnu.org/licenses/>.
18
+ #
19
+
20
+ require 'socket'
21
+ require 'timeout'
22
+
23
+ module Ronin
24
+ module Network
25
+ #
26
+ # Provides helper methods for using the TCP protocol.
27
+ #
28
+ module TCP
29
+ #
30
+ # Tests whether a remote TCP port is open.
31
+ #
32
+ # @param [String] host
33
+ # The host to connect to.
34
+ #
35
+ # @param [Integer] port
36
+ # The port to connect to.
37
+ #
38
+ # @param [String] local_host (nil)
39
+ # The local host to bind to.
40
+ #
41
+ # @param [Integer] local_port (nil)
42
+ # The local port to bind to.
43
+ #
44
+ # @param [Integer] timeout (5)
45
+ # The maximum time to attempt connecting.
46
+ #
47
+ # @return [Boolean, nil]
48
+ # Specifies whether the remote TCP port is open.
49
+ # If the connection was not accepted, `nil` will be returned.
50
+ #
51
+ # @example
52
+ # tcp_open?('example.com',80)
53
+ # # => true
54
+ #
55
+ # @example Using a timeout:
56
+ # tcp_open?('example.com',1111,nil,nil,5)
57
+ # # => nil
58
+ #
59
+ # @api public
60
+ #
61
+ # @since 0.5.0
62
+ #
63
+ def tcp_open?(host,port,local_host=nil,local_port=nil,timeout=nil)
64
+ timeout ||= 5
65
+
66
+ begin
67
+ Timeout.timeout(timeout) do
68
+ tcp_session(host,port,local_host,local_port)
69
+ end
70
+
71
+ return true
72
+ rescue Timeout::Error
73
+ return nil
74
+ rescue SocketError, SystemCallError
75
+ return false
76
+ end
77
+ end
78
+
79
+ #
80
+ # Creates a new TCPSocket object connected to a given host and port.
81
+ #
82
+ # @param [String] host
83
+ # The host to connect to.
84
+ #
85
+ # @param [Integer] port
86
+ # The port to connect to.
87
+ #
88
+ # @param [String] local_host (nil)
89
+ # The local host to bind to.
90
+ #
91
+ # @param [Integer] local_port (nil)
92
+ # The local port to bind to.
93
+ #
94
+ # @yield [socket]
95
+ # If a block is given, it will be passed the newly created socket.
96
+ #
97
+ # @yieldparam [TCPsocket] socket
98
+ # The newly created TCPSocket object.
99
+ #
100
+ # @return [TCPSocket]
101
+ # The newly created TCPSocket object.
102
+ #
103
+ # @example
104
+ # tcp_connect('www.hackety.org',80) # => TCPSocket
105
+ #
106
+ # @example
107
+ # tcp_connect('www.wired.com',80) do |socket|
108
+ # socket.write("GET /\n\n")
109
+ #
110
+ # puts socket.readlines
111
+ # socket.close
112
+ # end
113
+ #
114
+ # @see https://rubydoc.info/stdlib/socket/TCPSocket
115
+ #
116
+ # @api public
117
+ #
118
+ def tcp_connect(host,port,local_host=nil,local_port=nil)
119
+ host = host.to_s
120
+ port = port.to_i
121
+
122
+ socket = if local_host || local_port
123
+ local_host = local_host.to_s
124
+ local_port = local_port.to_i
125
+
126
+ TCPSocket.new(host,port,local_host,local_port)
127
+ else
128
+ TCPSocket.new(host,port)
129
+ end
130
+
131
+ yield socket if block_given?
132
+ return socket
133
+ end
134
+
135
+ #
136
+ # Creates a new TCPSocket object, connected to a given host and port.
137
+ # The given data will then be written to the newly created TCPSocket.
138
+ #
139
+ # @param [String] data
140
+ # The data to send through the connection.
141
+ #
142
+ # @param [String] host
143
+ # The host to connect to.
144
+ #
145
+ # @param [Integer] port
146
+ # The port to connect to.
147
+ #
148
+ # @param [String] local_host (nil)
149
+ # The local host to bind to.
150
+ #
151
+ # @param [Integer] local_port (nil)
152
+ # The local port to bind to.
153
+ #
154
+ # @yield [socket]
155
+ # If a block is given, it will be passed the newly created socket.
156
+ #
157
+ # @yieldparam [TCPSocket] socket
158
+ # The newly created TCPSocket object.
159
+ #
160
+ # @api public
161
+ #
162
+ def tcp_connect_and_send(data,host,port,local_host=nil,local_port=nil)
163
+ socket = tcp_connect(host,port,local_host,local_port)
164
+ socket.write(data)
165
+
166
+ yield socket if block_given?
167
+ return socket
168
+ end
169
+
170
+ #
171
+ # Creates a new temporary TCPSocket object, connected to the given host
172
+ # and port.
173
+ #
174
+ # @param [String] host
175
+ # The host to connect to.
176
+ #
177
+ # @param [Integer] port
178
+ # The port to connect to.
179
+ #
180
+ # @param [String] local_host (nil)
181
+ # The local host to bind to.
182
+ #
183
+ # @param [Integer] local_port (nil)
184
+ # The local port to bind to.
185
+ #
186
+ # @yield [socket]
187
+ # If a block is given, it will be passed the newly created socket.
188
+ # After the block has returned, the socket will then be closed.
189
+ #
190
+ # @yieldparam [TCPsocket] socket
191
+ # The newly created TCPSocket object.
192
+ #
193
+ # @return [nil]
194
+ #
195
+ # @api public
196
+ #
197
+ def tcp_session(host,port,local_host=nil,local_port=nil)
198
+ socket = tcp_connect(host,port,local_host,local_port)
199
+
200
+ yield socket if block_given?
201
+ socket.close
202
+ return nil
203
+ end
204
+
205
+ #
206
+ # Reads the banner from the service running on the given host and port.
207
+ #
208
+ # @param [String] host
209
+ # The host to connect to.
210
+ #
211
+ # @param [Integer] port
212
+ # The port to connect to.
213
+ #
214
+ # @param [String] local_host (nil)
215
+ # The local host to bind to.
216
+ #
217
+ # @param [Integer] local_port (nil)
218
+ # The local port to bind to.
219
+ #
220
+ # @yield [banner]
221
+ # If a block is given, it will be passed the grabbed banner.
222
+ #
223
+ # @yieldparam [String] banner
224
+ # The grabbed banner.
225
+ #
226
+ # @return [String]
227
+ # The grabbed banner.
228
+ #
229
+ # @example
230
+ # tcp_banner('pop.gmail.com',25)
231
+ # # => "220 mx.google.com ESMTP c20sm3096959rvf.1"
232
+ #
233
+ # @api public
234
+ #
235
+ def tcp_banner(host,port,local_host=nil,local_port=nil)
236
+ banner = nil
237
+
238
+ tcp_session(host,port,local_host,local_port) do |socket|
239
+ banner = socket.readline.strip
240
+ end
241
+
242
+ yield banner if block_given?
243
+ return banner
244
+ end
245
+
246
+ #
247
+ # Connects to a specified host and port, sends the given data and then
248
+ # closes the connection.
249
+ #
250
+ # @param [String] data
251
+ # The data to send through the connection.
252
+ #
253
+ # @param [String] host
254
+ # The host to connect to.
255
+ #
256
+ # @param [Integer] port
257
+ # The port to connect to.
258
+ #
259
+ # @param [String] local_host (nil)
260
+ # The local host to bind to.
261
+ #
262
+ # @param [Integer] local_port (nil)
263
+ # The local port to bind to.
264
+ #
265
+ # @return [true]
266
+ # The data was successfully sent.
267
+ #
268
+ # @example
269
+ # buffer = "GET /" + ('A' * 4096) + "\n\r"
270
+ # tcp_send(buffer,'victim.com',80)
271
+ # # => true
272
+ #
273
+ # @api public
274
+ #
275
+ def tcp_send(data,host,port,local_host=nil,local_port=nil)
276
+ tcp_session(host,port,local_host,local_port) do |socket|
277
+ socket.write(data)
278
+ end
279
+
280
+ return true
281
+ end
282
+
283
+ #
284
+ # Creates a new TCPServer listening on a given host and port.
285
+ #
286
+ # @param [Integer] port (nil)
287
+ # The local port to listen on.
288
+ #
289
+ # @param [String] host (nil)
290
+ # The host to bind to.
291
+ #
292
+ # @param [Integer] backlog (5)
293
+ # The maximum backlog of pending connections.
294
+ #
295
+ # @yield [server]
296
+ # The block which will be called after the server has been created.
297
+ #
298
+ # @yieldparam [TCPServer] server
299
+ # The newly created TCP server.
300
+ #
301
+ # @return [TCPServer]
302
+ # The new TCP server.
303
+ #
304
+ # @example
305
+ # tcp_server(1337)
306
+ #
307
+ # @see https://rubydoc.info/stdlib/socket/TCPServer
308
+ #
309
+ # @api public
310
+ #
311
+ def tcp_server(port=nil,host=nil,backlog=5)
312
+ port = port.to_i
313
+
314
+ server = if host
315
+ host = host.to_s
316
+ TCPServer.new(host,port)
317
+ else
318
+ TCPServer.new(port)
319
+ end
320
+ server.listen(backlog)
321
+
322
+ yield server if block_given?
323
+ return server
324
+ end
325
+
326
+ #
327
+ # Creates a new temporary TCPServer listening on a host and port.
328
+ #
329
+ # @param [Integer] port (nil)
330
+ # The local port to bind to.
331
+ #
332
+ # @param [String] host (nil)
333
+ # The host to bind to.
334
+ #
335
+ # @param [Integer] backlog (5)
336
+ # The maximum backlog of pending connections.
337
+ #
338
+ # @yield [server]
339
+ # The block which will be called after the server has been created.
340
+ # After the block has finished, the server will be closed.
341
+ #
342
+ # @yieldparam [TCPServer] server
343
+ # The newly created TCP server.
344
+ #
345
+ # @return [nil]
346
+ #
347
+ # @example
348
+ # tcp_server_session(1337) do |server|
349
+ # client1 = server.accept
350
+ # client2 = server.accept
351
+ #
352
+ # client2.write(server.read_line)
353
+ #
354
+ # client1.close
355
+ # client2.close
356
+ # end
357
+ #
358
+ # @api public
359
+ #
360
+ def tcp_server_session(port=nil,host=nil,backlog=5,&block)
361
+ server = tcp_server(port,host,backlog,&block)
362
+ server.close()
363
+ return nil
364
+ end
365
+
366
+ #
367
+ # Creates a new TCPServer listening on a given host and port,
368
+ # accepting clients in a loop.
369
+ #
370
+ # @param [Integer] port (nil)
371
+ # The local port to bind to.
372
+ #
373
+ # @param [String] host (nil)
374
+ # The host to bind to.
375
+ #
376
+ # @yield [client]
377
+ # The given block will be passed the newly connected client.
378
+ # After the block has finished, the client will be closed.
379
+ #
380
+ # @yieldparam [TCPSocket] client
381
+ # A newly connected client.
382
+ #
383
+ # @return [nil]
384
+ #
385
+ # @example
386
+ # tcp_server_loop(1337) do |client|
387
+ # client.puts 'lol'
388
+ # end
389
+ #
390
+ # @api public
391
+ #
392
+ # @since 0.5.0
393
+ #
394
+ def tcp_server_loop(port=nil,host=nil)
395
+ tcp_server_session(port,host) do |server|
396
+ loop do
397
+ client = server.accept
398
+
399
+ yield client if block_given?
400
+ client.close
401
+ end
402
+ end
403
+ end
404
+
405
+ #
406
+ # Creates a new TCPServer listening on a given host and port,
407
+ # accepts only one client and then stops listening.
408
+ #
409
+ # @param [Integer] port (nil)
410
+ # The local port to bind to.
411
+ #
412
+ # @param [String] host (nil
413
+ # The host to bind to.
414
+ #
415
+ # @yield [client]
416
+ # The given block will be passed the newly connected client.
417
+ # After the block has finished, both the client and the server will be
418
+ # closed.
419
+ #
420
+ # @yieldparam [TCPSocket] client
421
+ # The newly connected client.
422
+ #
423
+ # @return [nil]
424
+ #
425
+ # @example
426
+ # tcp_accept(1337) do |client|
427
+ # client.puts 'lol'
428
+ # end
429
+ #
430
+ # @api public
431
+ #
432
+ # @since 0.5.0
433
+ #
434
+ def tcp_accept(port=nil,host=nil)
435
+ tcp_server_session(port,host,1) do |server|
436
+ client = server.accept
437
+
438
+ yield client if block_given?
439
+ client.close
440
+ end
441
+ end
442
+
443
+ #
444
+ # @deprecated
445
+ # Deprecated as of 0.5.0. Use {#tcp_accept} instead.
446
+ #
447
+ def tcp_single_server(port=nil,host=nil)
448
+ tcp_accept(port,host)
449
+ end
450
+ end
451
+ end
452
+ end