Class: Pwnlib::Tubes::Sock

Inherits:
Tube
  • Object
show all
Defined in:
lib/pwnlib/tubes/sock.rb

Overview

Socket!

Constant Summary

Constants inherited from Tube

Tube::BUFSIZE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Tube

#gets, #interact, #puts, #recv, #recvall, #recvline, #recvn, #recvpred, #recvregex, #recvuntil, #send, #sendline, #unrecv

Constructor Details

#initialize(host, port, timeout: nil) ⇒ Sock

Instantiate a Pwnlib::Tubes::Sock object.

Parameters:

  • host (String)

    The host to connect.

  • port (Integer)

    The port to connect.

  • timeout (Float?) (defaults to: nil)


23
24
25
26
27
28
29
# File 'lib/pwnlib/tubes/sock.rb', line 23

def initialize(host, port, timeout: nil)
  super(timeout: timeout)
  @sock = TCPSocket.new(host, port)
  @sock.binmode
  @timeout = nil
  @closed = { read: false, write: false }
end

Instance Attribute Details

#sockTCPSocket (readonly) Also known as: io_out

Returns The socket object.

Returns:

  • (TCPSocket)

    The socket object.



13
14
15
# File 'lib/pwnlib/tubes/sock.rb', line 13

def sock
  @sock
end

Instance Method Details

#close(direction = :both) ⇒ void

This method returns an undefined value.

Close the TCPSocket if no arguments passed. Or close the direction in sock.

Parameters:

  • direction (:both, :recv, :read, :send, :write) (defaults to: :both)
    • Close the TCPSocket if :both or no arguments passed.

    • Disallow further read in sock if :recv or :read passed.

    • Disallow further write in sock if :send or :write passed.

Difference with Python pwntools:

  • In pwntools-python, method shutdown(direction) is for closing socket one side, close() is for closing both side. We merge these two methods into one here.



43
44
45
46
47
48
49
50
51
52
# File 'lib/pwnlib/tubes/sock.rb', line 43

def close(direction = :both)
  if direction == :both
    return if @sock.closed?

    @closed[:read] = @closed[:write] = true
    @sock.close
  else
    shutdown(*normalize_direction(direction))
  end
end