Class: Ronin::PostEx::System::Shell

Inherits:
Resource
  • Object
show all
Defined in:
lib/ronin/post_ex/system/shell.rb

Overview

Provides access to a system's shell and executing shell commands.

Supported API Methods

  • shell_exec(command : String) -> String

Instance Attribute Summary collapse

Attributes inherited from Resource

#session

Instance Method Summary collapse

Methods inherited from Resource

#supports, #supports?

Constructor Details

#initialize(session) ⇒ Shell

Initializes the Shell resource.

Parameters:

  • session (#shell_exec)

    The Resource#session object that defines the shell_exec method.



49
50
51
52
53
54
# File 'lib/ronin/post_ex/system/shell.rb', line 49

def initialize(session)
  super(session)

  @cwd = nil
  @env = {}
end

Instance Attribute Details

#envHash{String => String} (readonly)

Persistent environment variables for the shell.

Returns:

  • (Hash{String => String})


41
42
43
# File 'lib/ronin/post_ex/system/shell.rb', line 41

def env
  @env
end

Instance Method Details

#cat(*arguments) ⇒ String

Reads the contents of one or more files.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the cat command.

Returns:

  • (String)

    The full output of the command.

See Also:

  • #exec


269
270
271
# File 'lib/ronin/post_ex/system/shell.rb', line 269

def cat(*arguments)
  run('cat',*arguments)
end

#cc(*arguments) ⇒ String

Compiles some C source-code with cc.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the cc command.

Returns:

  • (String)

    The full output of the command.

See Also:

  • #exec


974
975
976
# File 'lib/ronin/post_ex/system/shell.rb', line 974

def cc(*arguments)
  run('cc',*arguments)
end

#cd(path) ⇒ String

Changes the current working directory in the shell.

Parameters:

  • path (String)

    The path for the new current working directory.

Returns:

  • (String)

    Any error messages.



120
121
122
# File 'lib/ronin/post_ex/system/shell.rb', line 120

def cd(path)
  @pwd = File.expand_path(path,pwd)
end

#cp(*arguments) ⇒ String

Copies one or more files or directories.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the cp command.

Returns:

  • (String)

    Any error messages returned by the cp command.



474
475
476
# File 'lib/ronin/post_ex/system/shell.rb', line 474

def cp(*arguments)
  run('cp',*arguments)
end

#cp_a(*arguments) ⇒ String

Runs cp -a.

Parameters:

  • arguments (Array<String>)

    Additional arguments for the cp command.

Returns:

  • (String)

    Any error messages returned by the cp command.

See Also:



506
507
508
# File 'lib/ronin/post_ex/system/shell.rb', line 506

def cp_a(*arguments)
  cp('-a',*arguments)
end

#cp_r(*arguments) ⇒ String

Runs cp -r.

Parameters:

  • arguments (Array<String>)

    Additional arguments for the cp command.

Returns:

  • (String)

    Any error messages returned by the cp command.

See Also:



490
491
492
# File 'lib/ronin/post_ex/system/shell.rb', line 490

def cp_r(*arguments)
  cp('-r',*arguments)
end

#curl(*arguments) ⇒ String

Runs the curl.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the curl command.

Returns:

  • (String)

    The full output of the command.

See Also:

  • #exec


589
590
591
# File 'lib/ronin/post_ex/system/shell.rb', line 589

def curl(*arguments)
  run('curl','-s',*arguments)
end

#curl_out(path, *arguments) ⇒ String

Runs curl -O.

Parameters:

  • path (String)

    The path that curl will write to.

  • arguments (Array<String>)

    Additional arguments for the curl command.

Returns:

  • (String)

    The full output of the command.

See Also:



608
609
610
# File 'lib/ronin/post_ex/system/shell.rb', line 608

def curl_out(path,*arguments)
  curl('-O',path,*arguments)
end

#dateDate

Gets the current time and date from the shell.

Returns:

  • (Date)

    The current data returned by the shell.



699
700
701
# File 'lib/ronin/post_ex/system/shell.rb', line 699

def date
  time.to_date
end

#egrep(*arguments, &block) ⇒ Array<String>?

Runs grep -E.

Parameters:

  • arguments (Array<String>)

    Additional arguments for the grep command.

Returns:

  • (Array<String>, nil)

    If no block is given, all matching paths and lines will be returned.

See Also:



387
388
389
# File 'lib/ronin/post_ex/system/shell.rb', line 387

def egrep(*arguments,&block)
  grep('-E',*arguments,&block)
end

#faillog(*arguments) {|line| ... } ⇒ String

Shows login failures.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the faillog command.

Yields:

  • (line)

    If a block is given, it will be passed each line of output from the command.

Yield Parameters:

  • line (String)

    A line of output from the command.

Returns:

  • (String)

    The full output of the command.

See Also:

  • #exec


825
826
827
# File 'lib/ronin/post_ex/system/shell.rb', line 825

def faillog(*arguments)
  run('faillog',*arguments)
end

#fgrep(*arguments, &block) ⇒ Array<String>?

Runs grep -F.

Parameters:

  • arguments (Array<String>)

    Additional arguments for the grep command.

Returns:

  • (Array<String>, nil)

    If no block is given, all matching paths and lines will be returned.

See Also:



404
405
406
# File 'lib/ronin/post_ex/system/shell.rb', line 404

def fgrep(*arguments,&block)
  grep('-F',*arguments,&block)
end

#file(*arguments) ⇒ String

Determines the format of a file.

Examples:

system.shell.file('data.db')
# => "data.db: SQLite 3.x database"

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the file command.

Returns:

  • (String)

    The output of the file command.



239
240
241
# File 'lib/ronin/post_ex/system/shell.rb', line 239

def file(*arguments)
  run('file',*arguments)
end

#find(*arguments) {|path| ... } ⇒ Array<String>?

Searches for files or directories.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the find command.

Yields:

  • (path)

    If a block is given, it will be passed each path found.

Yield Parameters:

  • path (String)

    A path found by the find command.

Returns:

  • (Array<String>, nil)

    If no block is given, all found paths will be returned.



217
218
219
220
221
222
223
# File 'lib/ronin/post_ex/system/shell.rb', line 217

def find(*arguments,&block)
  if block
    run('find',*arguments).each_line(chomp: true,&block)
  else
    enum_for(__method__,*arguments).to_a
  end
end

#gcc(*arguments) ⇒ String

Compiles some C source-code with gcc.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the gcc command.

Returns:

  • (String)

    The full output of the command.

See Also:

  • #exec


958
959
960
# File 'lib/ronin/post_ex/system/shell.rb', line 958

def gcc(*arguments)
  run('gcc',*arguments)
end

#gidInteger

The GID of the current user.

Returns:

  • (Integer)

    The GID of the current user.



740
741
742
# File 'lib/ronin/post_ex/system/shell.rb', line 740

def gid
  run('id','-g').to_i
end

#grep(*arguments) {|path, line| ... } ⇒ Array<String>?

Searches one or more files for a given pattern.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the grep command.

Yields:

  • (path, line)

    If a block is given, it will be passed the paths and lines within files that matched the given pattern.

Yield Parameters:

  • path (String)

    The path of a file that contains matching lines.

  • line (String)

    A line that matches the given pattern.

Returns:

  • (Array<String>, nil)

    If no block is given, all matching paths and lines will be returned.



364
365
366
367
368
369
370
371
372
# File 'lib/ronin/post_ex/system/shell.rb', line 364

def grep(*arguments)
  if block_given?
    run('grep',*arguments).each_line(chomp: true) do |line|
      yield(*line.split(':',2))
    end
  else
    enum_for(__method__,*arguments).to_a
  end
end

#head(*arguments) ⇒ String

Reads the first n lines of one or more files.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the head command.

Returns:

  • (String)

    The full output of the command.

See Also:

  • #exec


285
286
287
# File 'lib/ronin/post_ex/system/shell.rb', line 285

def head(*arguments)
  run('head',*arguments)
end

#head_n(lines, *arguments) ⇒ String

Reads the first n lines of one or more files.

Parameters:

  • lines (Integer)

    The number of lines to read from one or more files.

  • arguments (Array<String>)

    Additional arguments to pass to the head command.

Returns:

  • (String)

    The full output of the command.

See Also:

  • #exec


304
305
306
# File 'lib/ronin/post_ex/system/shell.rb', line 304

def head_n(lines,*arguments)
  head('-n',lines,*arguments)
end

#idHash{Symbol => String}

The ID information of the current user.

Returns:

  • (Hash{Symbol => String})

    The ID information returned by the id command.



710
711
712
713
714
715
716
717
718
719
720
# File 'lib/ronin/post_ex/system/shell.rb', line 710

def id
  hash = {}

  run('id').split.each do |name_value|
    name, value = name_value.split('=',2)

    hash[name.to_sym] = value
  end

  return hash
end

#ifconfig(*arguments) ⇒ String

Shows information about network interfaces.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the ifconfig command.

Returns:

  • (String)

    The full output of the command.

See Also:

  • #exec


887
888
889
# File 'lib/ronin/post_ex/system/shell.rb', line 887

def ifconfig(*arguments)
  run('ifconfig',*arguments)
end

#interactObject

Starts an interactive Shell console.



1030
1031
1032
# File 'lib/ronin/post_ex/system/shell.rb', line 1030

def interact
  CLI::ShellShell.start(self)
end

#kill(*arguments) ⇒ String

Kills a current running process.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the kill command.

Returns:

  • (String)

    Output from the kill command.



871
872
873
# File 'lib/ronin/post_ex/system/shell.rb', line 871

def kill(*arguments)
  run('kill',*arguments)
end

#lastlog(*arguments) ⇒ String

Shows when users last logged in.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the lastlog command.

Returns:

  • (String)

    The full output of the command.

See Also:

  • #exec


802
803
804
# File 'lib/ronin/post_ex/system/shell.rb', line 802

def lastlog(*arguments)
  run('lastlog',*arguments)
end

#ls(*arguments) ⇒ String

Lists the files or directories.

Parameters:

  • arguments (Array<String>)

    Arguments to pass to the ls command.

Returns:

  • (String)

    The full output of the command.

See Also:

  • #exec


147
148
149
# File 'lib/ronin/post_ex/system/shell.rb', line 147

def ls(*arguments)
  run('ls',*arguments)
end

#ls_a(*arguments) ⇒ String

Lists all files or directories.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the ls -a command.

Returns:

  • (String)

    The full output of the command.

See Also:

  • #exec


163
164
165
# File 'lib/ronin/post_ex/system/shell.rb', line 163

def ls_a(*arguments)
  run('ls','-a',*arguments)
end

#ls_l(*arguments) ⇒ String

Lists information about files or directories.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the ls -l command.

Returns:

  • (String)

    The full output of the command.

See Also:

  • #exec


179
180
181
# File 'lib/ronin/post_ex/system/shell.rb', line 179

def ls_l(*arguments)
  run('ls','-l',*arguments)
end

#ls_la(*arguments) ⇒ String Also known as: ls_al

Lists information about all files or directories.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the ls -la command.

Returns:

  • (String)

    The full output of the command.

See Also:

  • #exec


195
196
197
# File 'lib/ronin/post_ex/system/shell.rb', line 195

def ls_la(*arguments)
  run('ls','-la',*arguments)
end

#mkdir(*arguments) ⇒ String

Creates a new directory.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the mkdir command.

Returns:

  • (String)

    Any error messages returned by the mkdir command.



460
461
462
# File 'lib/ronin/post_ex/system/shell.rb', line 460

def mkdir(*arguments)
  run('mkdir',*arguments)
end

#mktemp(*arguments) ⇒ String

Creates a tempfile.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to mktemp.

Returns:

  • (String)

    The path of the new tempfile.



432
433
434
# File 'lib/ronin/post_ex/system/shell.rb', line 432

def mktemp(*arguments)
  run('mktemp',*arguments).chomp
end

#mktempdir(*arguments) ⇒ String

Creates a tempdir.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to mktemp.

Returns:

  • (String)

    The path of the new tempdir.



446
447
448
# File 'lib/ronin/post_ex/system/shell.rb', line 446

def mktempdir(*arguments)
  mktemp('-d',*arguments)
end

#netstat(*arguments) ⇒ String

Shows network connections.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the netstat command.

Returns:

  • (String)

    The full output of the command.

See Also:

  • #exec


903
904
905
# File 'lib/ronin/post_ex/system/shell.rb', line 903

def netstat(*arguments)
  run('netstat',*arguments)
end

#netstat_anp(*arguments) ⇒ String

Runs netstat -anp.

Parameters:

  • arguments (Array<String>)

    Additional arguments for the netstat command.

Returns:

  • (String)

    The full output of the command.

See Also:



919
920
921
# File 'lib/ronin/post_ex/system/shell.rb', line 919

def netstat_anp(*arguments)
  netstat('-anp',*arguments)
end

#perl(*arguments) ⇒ String

Runs a PERL script.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the perl command.

Returns:

  • (String)

    The full output of the command.

See Also:

  • #exec


990
991
992
# File 'lib/ronin/post_ex/system/shell.rb', line 990

def perl(*arguments)
  run('perl',*arguments)
end

#ping(*arguments) {|line| ... } ⇒ String

Pings an IP address.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the ping command.

Yields:

  • (line)

    If a block is given, it will be passed each line of output from the command.

Yield Parameters:

  • line (String)

    A line of output from the command.

Returns:

  • (String)

    The full output of the command.

See Also:

  • #exec


942
943
944
# File 'lib/ronin/post_ex/system/shell.rb', line 942

def ping(*arguments)
  run('ping',*arguments)
end

#ps(*arguments) ⇒ String

Shows the current running processes.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the ps command.

Returns:

  • (String)

    The full output of the command.

See Also:

  • #exec


841
842
843
# File 'lib/ronin/post_ex/system/shell.rb', line 841

def ps(*arguments)
  run('ps',*arguments)
end

#ps_aux(*arguments) ⇒ String

Runs ps aux.

Parameters:

  • arguments (Array<String>)

    Additional arguments for the ps command.

Returns:

  • (String)

    The full output of the command.

See Also:



857
858
859
# File 'lib/ronin/post_ex/system/shell.rb', line 857

def ps_aux(*arguments)
  ps('aux',*arguments)
end

#pwdString

Gets the current working directory.

Returns:

  • (String)

    The path of the current working directory.



131
132
133
# File 'lib/ronin/post_ex/system/shell.rb', line 131

def pwd
  @pwd ||= run('pwd').chomp
end

#python(*arguments) ⇒ String

Runs a Python script.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the python command.

Returns:

  • (String)

    The full output of the command.

See Also:

  • #exec


1006
1007
1008
# File 'lib/ronin/post_ex/system/shell.rb', line 1006

def python(*arguments)
  run('python',*arguments)
end

#rm(*arguments) {|line| ... } ⇒ String

Removes one or more files or directories.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the rm command.

Yields:

  • (line)

    If a block is given, it will be passed each line of output from the command.

Yield Parameters:

  • line (String)

    A line of output from the command.

Returns:

  • (String)

    The full output of the command.

See Also:

  • #exec


645
646
647
# File 'lib/ronin/post_ex/system/shell.rb', line 645

def rm(*arguments)
  run('rm',*arguments)
end

#rm_r(*arguments) ⇒ String

Runs rm -r.

Parameters:

  • arguments (Array<String>)

    Additional arguments for the rm command.

Returns:

  • (String)

    The full output of the command.

See Also:



661
662
663
# File 'lib/ronin/post_ex/system/shell.rb', line 661

def rm_r(*arguments)
  rm('-r',*arguments)
end

#rm_rf(*arguments) ⇒ String

Runs rm -rf.

Parameters:

  • arguments (Array<String>)

    Additional arguments for the rm command.

Returns:

  • (String)

    The full output of the command.

See Also:



677
678
679
# File 'lib/ronin/post_ex/system/shell.rb', line 677

def rm_rf(*arguments)
  rm('-rf',*arguments)
end

#rmdir(*arguments) ⇒ String

Removes a directory.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the rmdir command.

Returns:

  • (String)

    Any error messages returned by the rmdir command.



622
623
624
# File 'lib/ronin/post_ex/system/shell.rb', line 622

def rmdir(*arguments)
  run('rmdir',*arguments)
end

#rsync(*arguments) ⇒ String

Runs rsync.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the rsync command.

Returns:

  • (String)

    The full output of the command.

See Also:

  • #exec


522
523
524
# File 'lib/ronin/post_ex/system/shell.rb', line 522

def rsync(*arguments)
  run('rsync',*arguments)
end

#rsync_a(*arguments) ⇒ String

Runs rsync -a.

Parameters:

  • arguments (Array<String>)

    Additional arguments for the rsync command.

Returns:

  • (String)

    The full output of the command.

See Also:



538
539
540
# File 'lib/ronin/post_ex/system/shell.rb', line 538

def rsync_a(*arguments)
  rsync('-a',*arguments)
end

#ruby(*arguments) ⇒ String

Runs a Ruby script.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the ruby command.

Returns:

  • (String)

    The full output of the command.

See Also:

  • #exec


1022
1023
1024
# File 'lib/ronin/post_ex/system/shell.rb', line 1022

def ruby(*arguments)
  run('ruby',*arguments)
end

#run(command, *arguments) ⇒ String

Executes a shell command and returns it's output.

Examples:

system.shell.run 'ls'

with additional arguments:

system.shell.run 'ls', '-l'

Parameters:

  • command (String)

    The shell command to execute.

  • arguments (Array<String>)

    Additional arguments for the shell command.

Returns:

  • (String)

    The output of the shell command.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ronin/post_ex/system/shell.rb', line 74

def run(command,*arguments)
  unless arguments.empty?
    command = "#{command} #{Shellwords.shelljoin(arguments)}"
  end

  unless @env.empty?
    env_vars = @env.map { |key,value|
      "#{key}=#{Shellwords.shellescape(value)}"
    }.join(' ')

    command = "env #{env_vars} #{command}"
  end

  if @cwd
    command = "cd #{@cwd} && #{command}"
  end

  return @session.shell_exec(command)
end

#system(command, *arguments) ⇒ nil

Executes a command and prints the resulting output.

Parameters:

  • command (String)

    The program name or path to execute.

  • arguments (Array<String>)

    Additional arguments for the shell command.

Returns:

  • (nil)


106
107
108
# File 'lib/ronin/post_ex/system/shell.rb', line 106

def system(command,*arguments)
  puts(run(command,*arguments))
end

#tail(*arguments) ⇒ String

Reads the last n lines of one or more files.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the tail command.

Returns:

  • (String)

    The full output of the command.

See Also:

  • #exec


320
321
322
# File 'lib/ronin/post_ex/system/shell.rb', line 320

def tail(*arguments)
  run('tail',*arguments)
end

#tail_n(lines, *arguments) ⇒ String

Reads the last n lines of one or more files.

Parameters:

  • lines (Integer)

    The number of lines to read from one or more files.

  • arguments (Array<String>)

    Additional arguments to pass to the tail command.

Returns:

  • (String)

    The full output of the command.

See Also:

  • #exec


339
340
341
# File 'lib/ronin/post_ex/system/shell.rb', line 339

def tail_n(lines,*arguments)
  tail('-n',lines,*arguments)
end

#timeTime

Gets the current time from the shell.

Returns:

  • (Time)

    The current time returned by the shell.



688
689
690
# File 'lib/ronin/post_ex/system/shell.rb', line 688

def time
  Time.parse(run('date').chomp)
end

#touch(*arguments) ⇒ String

Touches a file.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the touch command.

Returns:

  • (String)

    Any error messages returned by the touch command.



418
419
420
# File 'lib/ronin/post_ex/system/shell.rb', line 418

def touch(*arguments)
  run('touch',*arguments)
end

#uidInteger

The UID of the current user.

Returns:

  • (Integer)

    The UID of the current user.



729
730
731
# File 'lib/ronin/post_ex/system/shell.rb', line 729

def uid
  run('id','-u').to_i
end

#w(*arguments) ⇒ String

Similar to #who but runs the w command.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the w command.

Returns:

  • (String)

    The full output of the command.

See Also:

  • #exec


786
787
788
# File 'lib/ronin/post_ex/system/shell.rb', line 786

def w(*arguments)
  run('w',*arguments)
end

#wget(*arguments) ⇒ String

Runs wget.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the rsync command.

Returns:

  • (String)

    The full output of the command.

See Also:

  • #exec


554
555
556
# File 'lib/ronin/post_ex/system/shell.rb', line 554

def wget(*arguments)
  run('wget','-q',*arguments)
end

#wget_out(path, *arguments) ⇒ String

Runs wget -O.

Parameters:

  • path (String)

    The path that wget will write to.

  • arguments (Array<String>)

    Additional arguments for the wget command.

Returns:

  • (String)

    The full output of the command.

See Also:



573
574
575
# File 'lib/ronin/post_ex/system/shell.rb', line 573

def wget_out(path,*arguments)
  wget('-O',path,*arguments)
end

#which(*arguments) ⇒ String

Finds a program available to the shell.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the which command.

Returns:

  • (String)

    The output from the which command.



253
254
255
# File 'lib/ronin/post_ex/system/shell.rb', line 253

def which(*arguments)
  run('which',*arguments)
end

#who(*arguments) ⇒ String

Shows who is currently logged in.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the who command.

Returns:

  • (String)

    The full output of the command.

See Also:

  • #exec


770
771
772
# File 'lib/ronin/post_ex/system/shell.rb', line 770

def who(*arguments)
  run('who',*arguments)
end

#whoami(*arguments) ⇒ String

The name of the current user.

Parameters:

  • arguments (Array<String>)

    Additional arguments to pass to the whoami command.

Returns:

  • (String)

    The name of the current user returned by the whoami command.



754
755
756
# File 'lib/ronin/post_ex/system/shell.rb', line 754

def whoami(*arguments)
  run('whoami',*arguments).chomp
end