ronin-support 0.4.1 → 0.5.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. data/ChangeLog.md +75 -32
  2. data/Gemfile +17 -18
  3. data/README.md +9 -10
  4. data/Rakefile +10 -2
  5. data/gemspec.yml +1 -1
  6. data/lib/ronin/binary.rb +21 -0
  7. data/lib/ronin/binary/hexdump.rb +20 -0
  8. data/lib/ronin/binary/hexdump/parser.rb +411 -0
  9. data/lib/ronin/binary/struct.rb +579 -0
  10. data/lib/ronin/binary/template.rb +437 -0
  11. data/lib/ronin/extensions/ip_addr.rb +17 -13
  12. data/lib/ronin/extensions/regexp.rb +45 -0
  13. data/lib/ronin/extensions/string.rb +3 -3
  14. data/lib/ronin/formatting/extensions/binary.rb +1 -0
  15. data/lib/ronin/formatting/extensions/binary/array.rb +63 -0
  16. data/lib/ronin/formatting/extensions/binary/base64.rb +106 -0
  17. data/lib/ronin/formatting/extensions/binary/file.rb +39 -6
  18. data/lib/ronin/formatting/extensions/binary/float.rb +65 -0
  19. data/lib/ronin/formatting/extensions/binary/integer.rb +56 -43
  20. data/lib/ronin/formatting/extensions/binary/string.rb +75 -187
  21. data/lib/ronin/formatting/extensions/text/string.rb +61 -0
  22. data/lib/ronin/fuzzing/extensions/string.rb +21 -8
  23. data/lib/ronin/fuzzing/fuzzing.rb +19 -17
  24. data/lib/ronin/network.rb +2 -1
  25. data/lib/ronin/network/dns.rb +57 -15
  26. data/lib/ronin/network/extensions.rb +0 -1
  27. data/lib/ronin/network/ftp.rb +145 -0
  28. data/lib/ronin/network/http/http.rb +13 -14
  29. data/lib/ronin/network/imap.rb +11 -10
  30. data/lib/ronin/network/mixins.rb +1 -0
  31. data/lib/ronin/network/mixins/ftp.rb +155 -0
  32. data/lib/ronin/network/mixins/ssl.rb +1 -1
  33. data/lib/ronin/network/mixins/tcp.rb +39 -6
  34. data/lib/ronin/network/mixins/udp.rb +121 -1
  35. data/lib/ronin/network/mixins/unix.rb +279 -0
  36. data/lib/ronin/network/pop3.rb +5 -5
  37. data/lib/ronin/network/proxy.rb +578 -0
  38. data/lib/ronin/network/smtp/email.rb +1 -1
  39. data/lib/ronin/network/smtp/smtp.rb +7 -8
  40. data/lib/ronin/network/ssl.rb +1 -6
  41. data/lib/ronin/network/tcp.rb +2 -305
  42. data/lib/ronin/network/tcp/proxy.rb +377 -0
  43. data/lib/ronin/network/tcp/tcp.rb +435 -0
  44. data/lib/ronin/network/telnet.rb +27 -23
  45. data/lib/ronin/network/udp.rb +2 -266
  46. data/lib/ronin/network/udp/proxy.rb +169 -0
  47. data/lib/ronin/network/udp/udp.rb +442 -0
  48. data/lib/ronin/network/unix.rb +287 -0
  49. data/lib/ronin/path.rb +2 -2
  50. data/lib/ronin/spec/ui/output.rb +1 -7
  51. data/lib/ronin/support.rb +1 -0
  52. data/lib/ronin/support/inflector.rb +3 -7
  53. data/lib/ronin/support/support.rb +2 -1
  54. data/lib/ronin/support/version.rb +1 -1
  55. data/lib/ronin/ui/output/helpers.rb +13 -15
  56. data/lib/ronin/ui/output/output.rb +2 -2
  57. data/lib/ronin/ui/output/terminal/color.rb +10 -4
  58. data/lib/ronin/wordlist.rb +92 -17
  59. data/ronin-support.gemspec +38 -109
  60. data/spec/binary/hexdump/helpers/hexdumps.rb +13 -0
  61. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/ascii.bin +0 -0
  62. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_decimal_shorts.txt +0 -0
  63. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_hex_bytes.txt +0 -0
  64. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_hex_shorts.txt +0 -0
  65. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_octal_bytes.txt +0 -0
  66. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_octal_shorts.txt +0 -0
  67. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/hexdump_repeated.txt +0 -0
  68. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_decimal_bytes.txt +0 -0
  69. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_decimal_ints.txt +0 -0
  70. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_decimal_quads.txt +0 -0
  71. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_decimal_shorts.txt +0 -0
  72. data/spec/binary/hexdump/helpers/hexdumps/od_doubles.txt +17 -0
  73. data/spec/binary/hexdump/helpers/hexdumps/od_floats.txt +17 -0
  74. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_hex_bytes.txt +0 -0
  75. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_hex_ints.txt +0 -0
  76. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_hex_quads.txt +0 -0
  77. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_hex_shorts.txt +0 -0
  78. data/spec/binary/hexdump/helpers/hexdumps/od_named_chars.txt +17 -0
  79. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_octal_bytes.txt +0 -0
  80. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_octal_ints.txt +0 -0
  81. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_octal_quads.txt +0 -0
  82. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_octal_shorts.txt +0 -0
  83. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/od_repeated.txt +0 -0
  84. data/spec/{formatting/binary → binary/hexdump}/helpers/hexdumps/repeated.bin +0 -0
  85. data/spec/binary/hexdump/parser_spec.rb +302 -0
  86. data/spec/binary/struct_spec.rb +496 -0
  87. data/spec/binary/template_spec.rb +400 -0
  88. data/spec/extensions/ip_addr_spec.rb +58 -32
  89. data/spec/extensions/regexp_spec.rb +60 -0
  90. data/spec/extensions/string_spec.rb +1 -1
  91. data/spec/formatting/binary/array_spec.rb +22 -0
  92. data/spec/formatting/binary/base64_spec.rb +50 -0
  93. data/spec/formatting/binary/float_spec.rb +30 -0
  94. data/spec/formatting/binary/integer_spec.rb +54 -40
  95. data/spec/formatting/binary/string_spec.rb +69 -182
  96. data/spec/formatting/text/string_spec.rb +30 -0
  97. data/spec/network/dns_spec.rb +64 -0
  98. data/spec/network/ftp_spec.rb +65 -0
  99. data/spec/network/proxy_spec.rb +121 -0
  100. data/spec/network/shared/unix_server.rb +31 -0
  101. data/spec/network/tcp/proxy_spec.rb +116 -0
  102. data/spec/network/{tcp_spec.rb → tcp/tcp_spec.rb} +24 -1
  103. data/spec/network/telnet_spec.rb +67 -0
  104. data/spec/network/{udp_spec.rb → udp/udp_spec.rb} +24 -1
  105. data/spec/network/unix_spec.rb +183 -0
  106. data/spec/wordlist_spec.rb +74 -13
  107. metadata +129 -85
  108. data/spec/formatting/binary/helpers/hexdumps.rb +0 -16
@@ -17,269 +17,5 @@
17
17
  # along with Ronin Support. If not, see <http://www.gnu.org/licenses/>.
18
18
  #
19
19
 
20
- require 'socket'
21
-
22
- module Ronin
23
- module Network
24
- #
25
- # Provides helper methods for using the UDP protocol.
26
- #
27
- module UDP
28
- #
29
- # Creates a new UDPSocket object connected to a given host and port.
30
- #
31
- # @param [String] host
32
- # The host to connect to.
33
- #
34
- # @param [Integer] port
35
- # The port to connect to.
36
- #
37
- # @param [String] local_host (nil)
38
- # The local host to bind to.
39
- #
40
- # @param [Integer] local_port (nil)
41
- # The local port to bind to.
42
- #
43
- # @yield [socket]
44
- # If a block is given, it will be passed the newly created socket.
45
- #
46
- # @yieldparam [UDPsocket] socket
47
- # The newly created UDPSocket object.
48
- #
49
- # @return [UDPSocket]
50
- # The newly created UDPSocket object.
51
- #
52
- # @example
53
- # udp_connect('www.hackety.org',80)
54
- # # => UDPSocket
55
- #
56
- # @example
57
- # udp_connect('www.wired.com',80) do |socket|
58
- # puts socket.readlines
59
- # end
60
- #
61
- # @api public
62
- #
63
- def udp_connect(host,port,local_host=nil,local_port=0)
64
- host = host.to_s
65
- local_host = (local_host || '0.0.0.0').to_s
66
-
67
- socket = UDPSocket.new
68
- socket.bind(local_host,local_port) if (local_host && local_port)
69
- socket.connect(host,port)
70
-
71
- yield socket if block_given?
72
- return socket
73
- end
74
-
75
- #
76
- # Creates a new UDPSocket object, connected to a given host and port.
77
- # The given data will then be written to the newly created UDPSocket.
78
- #
79
- # @param [String] data
80
- # The data to send through the connection.
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 [UDPsocket] socket
98
- # The newly created UDPSocket object.
99
- #
100
- # @return [UDPSocket]
101
- # The newly created UDPSocket object.
102
- #
103
- # @api public
104
- #
105
- def udp_connect_and_send(data,host,port,local_host=nil,local_port=0)
106
- socket = udp_connect(host,port,local_host,local_port)
107
- socket.write(data)
108
-
109
- yield socket if block_given?
110
- return socket
111
- end
112
-
113
- #
114
- # Creates a new temporary UDPSocket object, connected to the given host
115
- # and port.
116
- #
117
- # @param [String] host
118
- # The host to connect to.
119
- #
120
- # @param [Integer] port
121
- # The port to connect to.
122
- #
123
- # @param [String] local_host (nil)
124
- # The local host to bind to.
125
- #
126
- # @param [Integer] local_port (nil)
127
- # The local port to bind to.
128
- #
129
- # @yield [socket]
130
- # If a block is given, it will be passed the newly created socket.
131
- # After the block has returned, the socket will then be closed.
132
- #
133
- # @yieldparam [UDPsocket] socket
134
- # The newly created UDPSocket object.
135
- #
136
- # @return [nil]
137
- #
138
- # @api public
139
- #
140
- def udp_session(host,port,local_host=nil,local_port=0)
141
- socket = udp_connect(host,port,local_host,local_port)
142
-
143
- yield socket if block_given?
144
-
145
- socket.close
146
- return nil
147
- end
148
-
149
- #
150
- # Connects to a specified host and port, sends the given data and then
151
- # closes the connection.
152
- #
153
- # @param [String] data
154
- # The data to send through the connection.
155
- #
156
- # @param [String] host
157
- # The host to connect to.
158
- #
159
- # @param [Integer] port
160
- # The port to connect to.
161
- #
162
- # @param [String] local_host (nil)
163
- # The local host to bind to.
164
- #
165
- # @param [Integer] local_port (nil)
166
- # The local port to bind to.
167
- #
168
- # @return [true]
169
- # The data was successfully sent.
170
- #
171
- # @example
172
- # buffer = "GET /" + ('A' * 4096) + "\n\r"
173
- # udp_send(buffer,'victim.com',80)
174
- # # => true
175
- #
176
- # @api public
177
- #
178
- # @since 0.4.0
179
- #
180
- def udp_send(data,host,port,local_host=nil,local_port=0)
181
- udp_session(host,port,local_host,local_port) do |socket|
182
- socket.write(data)
183
- end
184
-
185
- return true
186
- end
187
-
188
- #
189
- # Reads the banner from the service running on the given host and port.
190
- #
191
- # @param [String] host
192
- # The host to connect to.
193
- #
194
- # @param [Integer] port
195
- # The port to connect to.
196
- #
197
- # @param [String] local_host (nil)
198
- # The local host to bind to.
199
- #
200
- # @param [Integer] local_port (nil)
201
- # The local port to bind to.
202
- #
203
- # @yield [banner]
204
- # If a block is given, it will be passed the grabbed banner.
205
- #
206
- # @yieldparam [String] banner
207
- # The grabbed banner.
208
- #
209
- # @return [String]
210
- # The grabbed banner.
211
- #
212
- # @api public
213
- #
214
- def udp_banner(host,port,local_host=nil,local_port=0)
215
- banner = nil
216
-
217
- udp_session(host,port,local_host,local_port) do |socket|
218
- banner = socket.readline
219
- end
220
-
221
- yield banner if block_given?
222
- return banner
223
- end
224
-
225
- #
226
- # Creates a new UDPServer listening on a given host and port.
227
- #
228
- # @param [Integer] port
229
- # The local port to listen on.
230
- #
231
- # @param [String] host ('0.0.0.0')
232
- # The host to bind to.
233
- #
234
- # @return [UDPServer]
235
- # The new UDP server.
236
- #
237
- # @example
238
- # udp_server(1337)
239
- #
240
- # @api public
241
- #
242
- def udp_server(port=0,host=nil)
243
- host = (host || '0.0.0.0').to_s
244
- server = UDPSocket.new
245
-
246
- server.bind(host,port)
247
-
248
- yield server if block_given?
249
- return server
250
- end
251
-
252
- #
253
- # Creates a new temporary UDPServer listening on a given host and port.
254
- #
255
- # @param [Integer] port
256
- # The local port to bind to.
257
- #
258
- # @param [String] host ('0.0.0.0')
259
- # The host to bind to.
260
- #
261
- # @yield [server]
262
- # The block which will be called after the server has been created.
263
- # After the block has finished, the server will be closed.
264
- #
265
- # @yieldparam [UDPServer] server
266
- # The newly created UDP server.
267
- #
268
- # @return [nil]
269
- #
270
- # @example
271
- # udp_server_session(1337) do |server|
272
- # data, sender = server.recvfrom(1024)
273
- # end
274
- #
275
- # @api public
276
- #
277
- def udp_server_session(port=0,host=nil,&block)
278
- server = udp_server(port,host,&block)
279
-
280
- server.close()
281
- return nil
282
- end
283
- end
284
- end
285
- end
20
+ require 'ronin/network/udp/udp'
21
+ require 'ronin/network/udp/proxy'
@@ -0,0 +1,169 @@
1
+ #
2
+ # Copyright (c) 2006-2012 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/network/proxy'
21
+
22
+ require 'socket'
23
+
24
+ module Ronin
25
+ module Network
26
+ module UDP
27
+ #
28
+ # @since 0.5.0
29
+ #
30
+ class Proxy < Network::Proxy
31
+
32
+ #
33
+ # Opens the UDP Proxy.
34
+ #
35
+ # @api public
36
+ #
37
+ def open
38
+ @socket = UDPSocket.new
39
+ @socket.bind(@host,@port)
40
+ end
41
+
42
+ #
43
+ # Polls the connections for data/errors and the proxy socket for
44
+ # new client connections.
45
+ #
46
+ # @api public
47
+ #
48
+ def poll
49
+ sockets = [@socket] + server_connections
50
+
51
+ readable, writtable, errors = IO.select(sockets,nil,sockets)
52
+
53
+ (errors & server_connections).each do |server_socket|
54
+ client_socket = client_for(server_socket)
55
+
56
+ close_connection(client_socket,server_socket)
57
+ end
58
+
59
+ (readable & server_connections).each do |server_socket|
60
+ client_socket = client_for(server_socket)
61
+ data, addrinfo = recv(server_socket)
62
+
63
+ server_data(client_socket,server_socket,data)
64
+ end
65
+
66
+ if readable.include?(@socket)
67
+ data, addrinfo = recv(@socket)
68
+
69
+ client_socket = [@socket, [addrinfo[3], addrinfo[1]]]
70
+ server_socket = (@connections[client_socket] ||= open_server_connection)
71
+
72
+ client_data(client_socket,server_socket,data)
73
+ end
74
+ end
75
+
76
+ #
77
+ # Sends data to a connection.
78
+ #
79
+ # @param [UDPSocket, (UDPSocket, (String, Integer))] connection
80
+ # The connection from the proxy to the server, or the proxy socket
81
+ # and host/port of the client.
82
+ #
83
+ # @param [String] data
84
+ # The data to be sent.
85
+ #
86
+ # @api public
87
+ #
88
+ def send(connection,data)
89
+ case connection
90
+ when Array
91
+ socket, (host, port) = connection
92
+
93
+ socket.send(data,0,host,port)
94
+ when UDPSocket
95
+ connection.send(data,0)
96
+ end
97
+ end
98
+
99
+ #
100
+ # Receives data from a connection.
101
+ #
102
+ # @param [UDPSocket, (UDPSocket, (String, Integer))] connection
103
+ # The connection from the proxy to the server, or the proxy socket
104
+ # and the address of a client.
105
+ #
106
+ # @return [String, (String, Array)]
107
+ # The data received.
108
+ #
109
+ # @api public
110
+ #
111
+ def recv(connection)
112
+ case connection
113
+ when Array
114
+ socket, (host, port) = connection
115
+
116
+ socket.recvfrom(@buffer_size)
117
+ when UDPSocket
118
+ socket.recv(@buffer_size)
119
+ end
120
+ end
121
+
122
+ protected
123
+
124
+ #
125
+ # Creates a new connection from the proxy to the server.
126
+ #
127
+ # @return [UDPSocket]
128
+ # The new UDPSocket to the server.
129
+ #
130
+ def open_server_connection
131
+ socket = UDPSocket.new
132
+ socket.connect(@server_host,@server_port)
133
+
134
+ return socket
135
+ end
136
+
137
+ #
138
+ # Closes a connection from the client to the proxy.
139
+ #
140
+ # @param [(UDPSocket, (String, Integer))] connection
141
+ # The UDP Proxy socket and the host/port of the client.
142
+ #
143
+ # @note no-op
144
+ #
145
+ def close_client_connection(connection)
146
+ # no-op
147
+ end
148
+
149
+ #
150
+ # Closes the connection from the proxy to the server.
151
+ #
152
+ # @param [UDPSocket] connection
153
+ # The UDPSocket from the proxy to the server.
154
+ #
155
+ def close_server_connection(connection)
156
+ connection.close
157
+ end
158
+
159
+ #
160
+ # Closes the UDP proxy socket.
161
+ #
162
+ def close_proxy
163
+ @socket.close
164
+ end
165
+
166
+ end
167
+ end
168
+ end
169
+ end
@@ -0,0 +1,442 @@
1
+ #
2
+ # Copyright (c) 2006-2012 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 'socket'
21
+ require 'timeout'
22
+
23
+ module Ronin
24
+ module Network
25
+ #
26
+ # Provides helper methods for using the UDP protocol.
27
+ #
28
+ module UDP
29
+ #
30
+ # Tests whether a remote UDP 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 UDP port is open.
49
+ # If no data or ICMP error were received, `nil` will be returned.
50
+ #
51
+ # @api public
52
+ #
53
+ # @since 0.5.0
54
+ #
55
+ def udp_open?(host,port,local_host=nil,local_port=nil,timeout=nil)
56
+ timeout ||= 5
57
+
58
+ begin
59
+ Timeout.timeout(timeout) do
60
+ udp_session(host,port,local_host,local_port) do |socket|
61
+ # send an empty UDP packet, just like nmap
62
+ socket.syswrite('')
63
+
64
+ # send junk data, to elicit an error message
65
+ socket.syswrite("\0" * 64)
66
+
67
+ # test if we've received any data
68
+ socket.sysread(1)
69
+ end
70
+ end
71
+
72
+ return true
73
+ rescue Timeout::Error
74
+ return nil
75
+ rescue SocketError, SystemCallError
76
+ return false
77
+ end
78
+ end
79
+
80
+ #
81
+ # Creates a new UDPSocket object connected to a given host and port.
82
+ #
83
+ # @param [String] host
84
+ # The host to connect to.
85
+ #
86
+ # @param [Integer] port
87
+ # The port to connect to.
88
+ #
89
+ # @param [String] local_host (nil)
90
+ # The local host to bind to.
91
+ #
92
+ # @param [Integer] local_port (nil)
93
+ # The local port to bind to.
94
+ #
95
+ # @yield [socket]
96
+ # If a block is given, it will be passed the newly created socket.
97
+ #
98
+ # @yieldparam [UDPsocket] socket
99
+ # The newly created UDPSocket object.
100
+ #
101
+ # @return [UDPSocket]
102
+ # The newly created UDPSocket object.
103
+ #
104
+ # @example
105
+ # udp_connect('www.hackety.org',80)
106
+ # # => UDPSocket
107
+ #
108
+ # @example
109
+ # udp_connect('www.wired.com',80) do |socket|
110
+ # puts socket.readlines
111
+ # end
112
+ #
113
+ # @see http://rubydoc.info/stdlib/socket/UDPSocket
114
+ #
115
+ # @api public
116
+ #
117
+ def udp_connect(host,port,local_host=nil,local_port=nil)
118
+ host = host.to_s
119
+ port = port.to_i
120
+ local_host = (local_host || '0.0.0.0').to_s
121
+ local_port = local_port.to_i
122
+
123
+ socket = UDPSocket.new
124
+ socket.bind(local_host,local_port) if (local_host && local_port)
125
+ socket.connect(host,port)
126
+
127
+ yield socket if block_given?
128
+ return socket
129
+ end
130
+
131
+ #
132
+ # Creates a new UDPSocket object, connected to a given host and port.
133
+ # The given data will then be written to the newly created UDPSocket.
134
+ #
135
+ # @param [String] data
136
+ # The data to send through the connection.
137
+ #
138
+ # @param [String] host
139
+ # The host to connect to.
140
+ #
141
+ # @param [Integer] port
142
+ # The port to connect to.
143
+ #
144
+ # @param [String] local_host (nil)
145
+ # The local host to bind to.
146
+ #
147
+ # @param [Integer] local_port (nil)
148
+ # The local port to bind to.
149
+ #
150
+ # @yield [socket]
151
+ # If a block is given, it will be passed the newly created socket.
152
+ #
153
+ # @yieldparam [UDPsocket] socket
154
+ # The newly created UDPSocket object.
155
+ #
156
+ # @return [UDPSocket]
157
+ # The newly created UDPSocket object.
158
+ #
159
+ # @api public
160
+ #
161
+ def udp_connect_and_send(data,host,port,local_host=nil,local_port=nil)
162
+ socket = udp_connect(host,port,local_host,local_port)
163
+ socket.write(data)
164
+
165
+ yield socket if block_given?
166
+ return socket
167
+ end
168
+
169
+ #
170
+ # Creates a new temporary UDPSocket object, connected to the given host
171
+ # and port.
172
+ #
173
+ # @param [String] host
174
+ # The host to connect to.
175
+ #
176
+ # @param [Integer] port
177
+ # The port to connect to.
178
+ #
179
+ # @param [String] local_host (nil)
180
+ # The local host to bind to.
181
+ #
182
+ # @param [Integer] local_port (nil)
183
+ # The local port to bind to.
184
+ #
185
+ # @yield [socket]
186
+ # If a block is given, it will be passed the newly created socket.
187
+ # After the block has returned, the socket will then be closed.
188
+ #
189
+ # @yieldparam [UDPsocket] socket
190
+ # The newly created UDPSocket object.
191
+ #
192
+ # @return [nil]
193
+ #
194
+ # @api public
195
+ #
196
+ def udp_session(host,port,local_host=nil,local_port=nil)
197
+ socket = udp_connect(host,port,local_host,local_port)
198
+
199
+ yield socket if block_given?
200
+ socket.close
201
+ return nil
202
+ end
203
+
204
+ #
205
+ # Connects to a specified host and port, sends the given data and then
206
+ # closes the connection.
207
+ #
208
+ # @param [String] data
209
+ # The data to send through the connection.
210
+ #
211
+ # @param [String] host
212
+ # The host to connect to.
213
+ #
214
+ # @param [Integer] port
215
+ # The port to connect to.
216
+ #
217
+ # @param [String] local_host (nil)
218
+ # The local host to bind to.
219
+ #
220
+ # @param [Integer] local_port (nil)
221
+ # The local port to bind to.
222
+ #
223
+ # @return [true]
224
+ # The data was successfully sent.
225
+ #
226
+ # @example
227
+ # buffer = "GET /" + ('A' * 4096) + "\n\r"
228
+ # udp_send(buffer,'victim.com',80)
229
+ # # => true
230
+ #
231
+ # @api public
232
+ #
233
+ # @since 0.4.0
234
+ #
235
+ def udp_send(data,host,port,local_host=nil,local_port=nil)
236
+ udp_session(host,port,local_host,local_port) do |socket|
237
+ socket.write(data)
238
+ end
239
+
240
+ return true
241
+ end
242
+
243
+ #
244
+ # Reads the banner from the service running on the given host and port.
245
+ #
246
+ # @param [String] host
247
+ # The host to connect to.
248
+ #
249
+ # @param [Integer] port
250
+ # The port to connect to.
251
+ #
252
+ # @param [String] local_host (nil)
253
+ # The local host to bind to.
254
+ #
255
+ # @param [Integer] local_port (nil)
256
+ # The local port to bind to.
257
+ #
258
+ # @yield [banner]
259
+ # If a block is given, it will be passed the grabbed banner.
260
+ #
261
+ # @yieldparam [String] banner
262
+ # The grabbed banner.
263
+ #
264
+ # @return [String]
265
+ # The grabbed banner.
266
+ #
267
+ # @api public
268
+ #
269
+ def udp_banner(host,port,local_host=nil,local_port=nil)
270
+ banner = nil
271
+
272
+ udp_session(host,port,local_host,local_port) do |socket|
273
+ banner = socket.readline
274
+ end
275
+
276
+ yield banner if block_given?
277
+ return banner
278
+ end
279
+
280
+ #
281
+ # Creates a new UDPServer listening on a given host and port.
282
+ #
283
+ # @param [Integer] port
284
+ # The local port to listen on.
285
+ #
286
+ # @param [String] host ('0.0.0.0')
287
+ # The host to bind to.
288
+ #
289
+ # @return [UDPServer]
290
+ # The new UDP server.
291
+ #
292
+ # @example
293
+ # udp_server(1337)
294
+ #
295
+ # @see http://rubydoc.info/stdlib/socket/UDPSocket
296
+ #
297
+ # @api public
298
+ #
299
+ def udp_server(port=nil,host=nil)
300
+ port = port.to_i
301
+ host = (host || '0.0.0.0').to_s
302
+
303
+ server = UDPSocket.new
304
+ server.bind(host,port)
305
+
306
+ yield server if block_given?
307
+ return server
308
+ end
309
+
310
+ #
311
+ # Creates a new temporary UDPServer listening on a given host and port.
312
+ #
313
+ # @param [Integer] port
314
+ # The local port to bind to.
315
+ #
316
+ # @param [String] host ('0.0.0.0')
317
+ # The host to bind to.
318
+ #
319
+ # @yield [server]
320
+ # The block which will be called after the server has been created.
321
+ # After the block has finished, the server will be closed.
322
+ #
323
+ # @yieldparam [UDPServer] server
324
+ # The newly created UDP server.
325
+ #
326
+ # @return [nil]
327
+ #
328
+ # @example
329
+ # udp_server_session(1337) do |server|
330
+ # data, sender = server.recvfrom(1024)
331
+ # end
332
+ #
333
+ # @api public
334
+ #
335
+ def udp_server_session(port=nil,host=nil,&block)
336
+ server = udp_server(port,host,&block)
337
+ server.close()
338
+ return nil
339
+ end
340
+
341
+ #
342
+ # Creates a new UDPServer listening on a given host and port,
343
+ # accepting messages from clients in a loop.
344
+ #
345
+ # @param [Integer] port
346
+ # The port the UDPServer will listen on.
347
+ #
348
+ # @param [String] host
349
+ # The optional host the UDPServer will bind to.
350
+ #
351
+ # @yield [server, (client_host, client_port), mesg]
352
+ # The given block will be passed the client host/port and the received
353
+ # message.
354
+ #
355
+ # @yieldparam [UDPServer] server
356
+ # The UDPServer.
357
+ #
358
+ # @yieldparam [String] client_host
359
+ # The source host of the message.
360
+ #
361
+ # @yieldparam [Integer] client_port
362
+ # The source port of the message.
363
+ #
364
+ # @yieldparam [String] mesg
365
+ # The received message.
366
+ #
367
+ # @return [nil]
368
+ #
369
+ # @example
370
+ # udp_server_loop(1337) do |server,(host,port),mesg|
371
+ # server.send('hello',host,port)
372
+ # end
373
+ #
374
+ # @api public
375
+ #
376
+ # @since 0.5.0
377
+ #
378
+ def udp_server_loop(port=nil,host=nil)
379
+ udp_server_session(port,host) do |server|
380
+ loop do
381
+ mesg, addrinfo = server.recvfrom(4096)
382
+
383
+ yield server, [addrinfo[3], addrinfo[1]], mesg if block_given?
384
+ end
385
+ end
386
+ end
387
+
388
+ #
389
+ # Creates a new UDPServer listening on a given host and port,
390
+ # accepts only one message from a client.
391
+ #
392
+ # @param [Integer] port
393
+ # The port the UDPServer will listen on.
394
+ #
395
+ # @param [String] host
396
+ # The optional host the UDPServer will bind to.
397
+ #
398
+ # @yield [server, (client_host, client_port), mesg]
399
+ # The given block will be passed the client host/port and the received
400
+ # message.
401
+ #
402
+ # @yieldparam [UDPServer] server
403
+ # The UDPServer.
404
+ #
405
+ # @yieldparam [String] client_host
406
+ # The source host of the message.
407
+ #
408
+ # @yieldparam [Integer] client_port
409
+ # The source port of the message.
410
+ #
411
+ # @yieldparam [String] mesg
412
+ # The received message.
413
+ #
414
+ # @return [nil]
415
+ #
416
+ # @example
417
+ # udp_recv(1337) do |server,(host,port),mesg|
418
+ # server.send('hello',host,port)
419
+ # end
420
+ #
421
+ # @api public
422
+ #
423
+ # @since 0.5.0
424
+ #
425
+ def udp_recv(port=nil,host=nil)
426
+ udp_server_session(port,host) do |server|
427
+ mesg, addrinfo = server.recvfrom(4096)
428
+
429
+ yield server, [addrinfo[3], addrinfo[1]], mesg if block_given?
430
+ end
431
+ end
432
+
433
+ #
434
+ # @deprecated
435
+ # Deprecated as of 0.5.0. Use {#udp_recv} instead.
436
+ #
437
+ def udp_single_server(port=nil,host=nil)
438
+ udp_recv(port,host)
439
+ end
440
+ end
441
+ end
442
+ end