Class: Pwnlib::Tubes::Process

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

Overview

Launch a process.

Constant Summary collapse

DEFAULT_OPTIONS =

Default options for #initialize.

{
  env: ENV,
  in: :pipe,
  out: :pipe,
  raw: true,
  aslr: true
}.freeze

Constants inherited from Tube

Tube::BUFSIZE

Instance Method Summary collapse

Methods inherited from Tube

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

Constructor Details

#initialize(argv, **opts) ⇒ Process

Instantiate a Pwnlib::Tubes::Process object.

Examples:

io = Tubes::Process.new('ls')
io.gets
#=> "Gemfile\n"

io = Tubes::Process.new('ls', out: :pty)
io.gets
#=> "Gemfile       LICENSE\t\t\t   README.md  STYLE.md\t    git-hooks  pwntools.gemspec  test\n"
io = Tubes::Process.new('cat /proc/self/maps')
io.gets
#=> "55f8b8a10000-55f8b8a18000 r-xp 00000000 fd:00 9044035                    /bin/cat\n"
io.close

io = Tubes::Process.new('cat /proc/self/maps', aslr: false)
io.gets
#=> "555555554000-55555555c000 r-xp 00000000 fd:00 9044035                    /bin/cat\n"
io.close
io = Tubes::Process.new('env', env: { 'FOO' => 'BAR' })
io.gets
#=> "FOO=BAR\n"

Parameters:

  • argv (Array<String>, String)

    List of arguments to pass to the spawned process.

  • opts (Hash)

    a customizable set of options

Options Hash (**opts):

  • env (Hash{String => String}) — default: ENV

    Environment variables. By default, inherits from Ruby's environment.

  • in (Symbol) — default: :pipe

    What kind of io should be used for stdin. Candidates are: :pipe, :pty.

  • out (Symbol) — default: :pipe

    What kind of io should be used for stdout. Candidates are: :pipe, :pty. See examples for more details.

  • raw (Boolean) — default: true

    Set the created PTY to raw mode. i.e. disable echo and control characters. If no pty is created, this has no effect.

  • aslr (Boolean) — default: true

    If false is given, the ASLR of the target process will be disabled via setarch -R.

  • timeout (Float?) — default: nil


64
65
66
67
68
69
70
71
72
# File 'lib/pwnlib/tubes/process.rb', line 64

def initialize(argv, **opts)
  opts = DEFAULT_OPTIONS.merge(opts)
  super(timeout: opts[:timeout])
  argv = normalize_argv(argv, opts)
  slave_i, slave_o = create_pipe(opts)
  @pid = ::Process.spawn(opts[:env], *argv, in: slave_i, out: slave_o, unsetenv_others: true)
  slave_i.close
  slave_o.close unless slave_i == slave_o
end

Instance Method Details

#killvoid Also known as: close

This method returns an undefined value.

Kill the process.



87
88
89
90
91
# File 'lib/pwnlib/tubes/process.rb', line 87

def kill
  shutdown
  ::Process.kill('KILL', @pid)
  ::Process.wait(@pid)
end

#shutdown(direction = :both) ⇒ void

This method returns an undefined value.

Close the IO.

Parameters:

  • direction (:both, :recv, :read, :send, :write) (defaults to: :both)

    Disallow further read/write of the process.



80
81
82
# File 'lib/pwnlib/tubes/process.rb', line 80

def shutdown(direction = :both)
  close_io(normalize_direction(direction))
end