Module: PWN::Plugins::Sock

Defined in:
lib/pwn/plugins/sock.rb

Overview

This plugin was created to support fuzzing various networking protocols

Constant Summary collapse

@@logger =
PWN::Plugins::PWNLogger.create

Class Method Summary collapse

Class Method Details

.authorsObject

Author(s)

0day Inc. <[email protected]>



312
313
314
315
316
# File 'lib/pwn/plugins/sock.rb', line 312

public_class_method def self.authors
  "AUTHOR(S):
    0day Inc. <[email protected]>
  "
end

.check_port_in_use(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Plugins::Sock.check_port_in_use(

port: 'required - target port',
server_ip: 'optional - target host or ip to check (Defaults to 127.0.0.1)',
protocol: 'optional - :tcp || :udp (defaults to tcp)'

)



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/pwn/plugins/sock.rb', line 121

public_class_method def self.check_port_in_use(opts = {})
  server_ip = opts[:server_ip] ||= '127.0.0.1'
  port      = opts[:port]
  protocol  = (opts[:protocol] ||= :tcp).to_s.downcase.to_sym

  ct = 0.5 # connect timeout in seconds

  case protocol
  when :tcp
    # Use &:close intead of block
    Socket.tcp(server_ip, port, connect_timeout: ct, &:close)
    # Port is already in use (or no permission)
    true

  when :udp
    socket = UDPSocket.new
    socket.bind(server_ip, port)
    # Port is NOT in use (or at least we can use it)
    false
  else
    raise "Unsupported protocol: #{protocol}"
  end
rescue Errno::EADDRINUSE, # address already in use
       Errno::EACCES # permission denied

  state = true if protocol == :udp
  state = false if protocol == :tcp

  state
rescue Errno::ECONNREFUSED, # connection refused (usually means port exists but no listener)
       Errno::EHOSTUNREACH, # host unreachable
       Errno::ETIMEDOUT # connection timed out

  # Port is NOT in use (or at least we can use it)
  false
rescue SocketError => e
  # Usually means invalid address / interface
  raise "Socket error while checking port #{port}: #{e.message}"
rescue StandardError => e
  warn "[!] Unexpected error while checking port: #{e.class}#{e.message}"
  false
ensure
  socket.close if protocol == :udp && !socket.nil?
end

.connect(opts = {}) ⇒ Object

Supported Method Parameters

sock_obj = PWN::Plugins::Sock.connect(

target: 'required - target host or ip',
port: 'required - target port',
protocol: 'optional - :tcp || :udp (defaults to :tcp)',
tls: 'optional - boolean connect to target socket using TLS (defaults to false)'

)



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/pwn/plugins/sock.rb', line 20

public_class_method def self.connect(opts = {})
  target = opts[:target].to_s.scrub
  port = opts[:port].to_i

  protocol = opts[:protocol]
  protocol ||= :tcp

  # TODO: Add proxy support

  tls = true if opts[:tls]
  tls ||= false

  tls_min_version = OpenSSL::SSL::TLS1_VERSION if tls_min_version.nil?

  case protocol.to_s.to_sym
  when :tcp
    if tls
      sock = TCPSocket.open(target, port)
      tls_context = OpenSSL::SSL::SSLContext.new
      tls_context.set_params(verify_mode: OpenSSL::SSL::VERIFY_NONE)
      tls_context.min_version = tls_min_version
      # tls_context.ciphers = tls_context.ciphers.select do |cipher|
      #   cipher[1] == cipher_tls
      # end
      tls_sock = OpenSSL::SSL::SSLSocket.new(sock, tls_context)
      tls_sock.hostname = target
      sock_obj = tls_sock.connect
      sock_obj.sync_close = true
    else
      sock_obj = TCPSocket.open(target, port)
    end
  when :udp
    sock_obj = UDPSocket.new
    sock_obj.connect(target, port)
  else
    raise "Unsupported protocol: #{protocol}"
  end

  sock_obj
rescue OpenSSL::SSL::SSLError => e
  case tls_min_version
  when OpenSSL::SSL::TLS1_VERSION
    puts 'Attempting OpenSSL::SSL::TLS1_1_VERSION...'
    # cipher_tls = 'TLSv1.0'
    tls_min_version = OpenSSL::SSL::TLS1_1_VERSION
  when OpenSSL::SSL::TLS1_1_VERSION
    puts 'Attempting OpenSSL::SSL::TLS1_2_VERSION...'
    # cipher_tls = 'TLSv1.2'
    tls_min_version = OpenSSL::SSL::TLS1_2_VERSION
  when OpenSSL::SSL::TLS1_2_VERSION
    puts 'Attempting OpenSSL::SSL::TLS1_3_VERSION...'
    # cipher_tls = 'TLSv1.3'
    tls_min_version = OpenSSL::SSL::TLS1_3_VERSION
  else
    tls_min_version = :abort
  end

  retry unless tls_min_version == :abort
  raise "\n#{e.inspect}" if tls_min_version == :abort
rescue StandardError => e
  sock_obj = disconnect(sock_obj: sock_obj) unless sock_obj.nil?
  raise e
end

.disconnect(opts = {}) ⇒ Object

Supported Method Parameters

sock_obj = PWN::Plugins::Sock.disconnect(

sock_obj: 'required - sock_obj returned from #connect method'

)



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/pwn/plugins/sock.rb', line 292

public_class_method def self.disconnect(opts = {})
  sock_obj = opts[:sock_obj]
  return unless sock_obj.respond_to?(:close)

  # Shutdown both directions to terminate flows immediately
  # sock_obj.shutdown(Socket::SHUT_RDWR)

  # Set SO_LINGER=0 to force RST (skips TIME_WAIT; ideal for fuzzing)
  # linger = [1, 0].pack('ii')
  # sock_obj.setsockopt(Socket::SOL_SOCKET, Socket::SO_LINGER, linger)

  sock_obj.close
rescue StandardError => e
  raise e
ensure
  sock_obj = nil
end

.get_random_unused_port(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Plugins::Sock.get_random_unused_port(

server_ip: 'optional - target host or ip to check (Defaults to 127.0.0.1)',
protocol: 'optional - :tcp || :udp (defaults to tcp)'

)



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/pwn/plugins/sock.rb', line 90

public_class_method def self.get_random_unused_port(opts = {})
  server_ip = opts[:server_ip]
  server_ip ||= '127.0.0.1'
  port = -1
  protocol = opts[:protocol]
  protocol ||= :tcp

  port_in_use = true
  while port_in_use
    port = Random.rand(1024..65_535)
    port_in_use = check_port_in_use(
      server_ip: server_ip,
      port: port,
      protocol: protocol
    )
  end

  port
rescue Errno::ECONNREFUSED,
       Errno::EHOSTUNREACH,
       Errno::ETIMEDOUT
  false
end

.get_tls_cert(opts = {}) ⇒ Object

Supported Method Parameters

cert_obj = PWN::Plugins::Sock.get_tls_cert(

target: 'required - target host or ip',
port: 'optional - target port (defaults to 443)'

)



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/pwn/plugins/sock.rb', line 269

public_class_method def self.get_tls_cert(opts = {})
  target = opts[:target].to_s.scrub
  port = opts[:port]
  port ||= 443

  tls_sock_obj = connect(
    target: target,
    port: port,
    protocol: :tcp,
    tls: true
  )
  tls_sock_obj.peer_cert
rescue StandardError => e
  raise e
ensure
  tls_sock_obj = disconnect(sock_obj: tls_sock_obj) unless tls_sock_obj.nil?
end

.helpObject

Display Usage for this Module



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/pwn/plugins/sock.rb', line 320

public_class_method def self.help
  puts "USAGE:
    sock_obj = #{self}.connect(
      target: 'required - target host or ip',
      port: 'required - target port',
      protocol: 'optional - :tcp || :udp (defaults to tcp)',
      tls: 'optional - boolean connect to target socket using TLS (defaults to false)'
    )

    port = #{self}.get_random_unused_port(
      server_ip: 'optional - target host or ip to check (Defaults to 127.0.0.1)',
      protocol: 'optional - :tcp || :udp (defaults to tcp)'
    )

    #{self}.check_port_in_use(
      server_ip: 'optional - target host or ip to check (Defaults to 127.0.0.1)',
      port: 'required - target port',
      protocol: 'optional - :tcp || :udp (defaults to tcp)'
    )

    listen_obj = #{self}.listen(
      port: 'required - target port',
      server_ip: 'optional - target host or ip to listen (Defaults to 127.0.0.1')',
      protocol: 'optional - :tcp || :udp (defaults to tcp)',
      tls: 'optional - boolean listen on TLS-enabled socket (defaults to false)',
      detach: 'optional - boolean to detach listener to background (defaults to false)'
    )

    cert_obj = #{self}.get_tls_cert(
      target: 'required - target host or ip',
      port: 'optional - target port (defaults to 443)'
    )

    sock_obj = #{self}.disconnect(
      sock_obj: 'required - sock_obj returned from #connect method'
    )

    #{self}.authors
  "
end

.listen(opts = {}) ⇒ Object

Supported Method Parameters

listen_obj = PWN::Plugins::Sock.listen(

port: 'required - target port',
server_ip: 'optional - target host or ip to listen (Defaults to 127.0.0.1')',
protocol: 'optional - :tcp || :udp (defaults to tcp)',
tls: 'optional - boolean listen on TLS-enabled socket (defaults to false)',
detach: 'optional - boolean to detach listener to background (defaults to false)'

)



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/pwn/plugins/sock.rb', line 175

public_class_method def self.listen(opts = {})
  port = opts[:port]
  raise 'ERROR: Missing required parameter: port' if port.nil?

  server_ip = opts[:server_ip] ||= '127.0.0.1'
  protocol = (opts[:protocol] ||= :tcp).to_s.downcase.to_sym
  tls = opts[:tls] || false
  detach = opts[:detach] || false

  listen_obj = nil

  case protocol
  when :tcp
    server = TCPServer.open(server_ip, port)

    if tls
      tls_context = OpenSSL::SSL::SSLContext.new
      tls_context.set_params(verify_mode: OpenSSL::SSL::VERIFY_NONE)
      # TODO: min_version, ciphers, etc.
      listen_obj = OpenSSL::SSL::SSLServer.new(server, tls_context)
    else
      listen_obj = server
    end

    unless detach
      # Default blocking mode - simple accept-and-handle loop
      # puts "[*] Listening on #{server_ip}:#{port} (#{tls ? 'TLS' : 'plain'} TCP)..."

      loop do
        client = listen_obj.accept
        Thread.new(client) do |c|
          peer = c.peeraddr
          puts "[+] Connection from #{peer}"

          while (data = c.gets)
            puts "[#{peer}] #{data.strip}"
            # Optional: echo back
            # c.puts "ECHO: #{data}"
          end
        rescue Errno::ECONNRESET, Errno::EPIPE, IOError
          # Client disconnected
        ensure
          client.close unless client.nil?
        end
      end
    end

  when :udp
    listen_obj = UDPSocket.new
    listen_obj.bind(server_ip, port)

    # puts "[*] Listening on #{server_ip}:#{port} (UDP)..."

    unless detach
      # Simple single-threaded UDP receive loop
      loop do
        msg, addrinfo = listen_obj.recvmsg
        next unless msg && !msg.empty?

        peer = "#{addrinfo.ip_address}:#{addrinfo.ip_port}"
        puts "[#{peer}] #{msg.inspect}"
        # Optional: echo back
        # listen_obj.send("ECHO: #{msg}", 0, addrinfo.ip_address, addrinfo.ip_port)
      end
    end

  else
    raise "Unsupported protocol: #{protocol}"
  end

  loop do
    listening = check_port_in_use(
      server_ip: server_ip,
      port: port,
      protocol: protocol
    )
    break if listening
  end

  listen_obj
rescue Interrupt
  puts "\n[!] Caught interrupt, shutting down listener..."
rescue StandardError => e
  raise
ensure
  listen_obj.close unless listen_obj.nil? || detach
end