Class: Ronin::PostEx::System::Shell
- 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
-
#env ⇒ Hash{String => String}
readonly
Persistent environment variables for the shell.
Attributes inherited from Resource
Instance Method Summary collapse
-
#cat(*arguments) ⇒ String
Reads the contents of one or more files.
-
#cc(*arguments) ⇒ String
Compiles some C source-code with
cc
. -
#cd(path) ⇒ String
Changes the current working directory in the shell.
-
#cp(*arguments) ⇒ String
Copies one or more files or directories.
-
#cp_a(*arguments) ⇒ String
Runs
cp -a
. -
#cp_r(*arguments) ⇒ String
Runs
cp -r
. -
#curl(*arguments) ⇒ String
Runs the
curl
. -
#curl_out(path, *arguments) ⇒ String
Runs
curl -O
. -
#date ⇒ Date
Gets the current time and date from the shell.
-
#egrep(*arguments, &block) ⇒ Array<String>?
Runs
grep -E
. -
#faillog(*arguments) {|line| ... } ⇒ String
Shows login failures.
-
#fgrep(*arguments, &block) ⇒ Array<String>?
Runs
grep -F
. -
#file(*arguments) ⇒ String
Determines the format of a file.
-
#find(*arguments) {|path| ... } ⇒ Array<String>?
Searches for files or directories.
-
#gcc(*arguments) ⇒ String
Compiles some C source-code with
gcc
. -
#gid ⇒ Integer
The GID of the current user.
-
#grep(*arguments) {|path, line| ... } ⇒ Array<String>?
Searches one or more files for a given pattern.
-
#head(*arguments) ⇒ String
Reads the first
n
lines of one or more files. -
#head_n(lines, *arguments) ⇒ String
Reads the first
n
lines of one or more files. -
#id ⇒ Hash{Symbol => String}
The ID information of the current user.
-
#ifconfig(*arguments) ⇒ String
Shows information about network interfaces.
-
#initialize(session) ⇒ Shell
constructor
Initializes the Shell resource.
-
#interact ⇒ Object
Starts an interactive Shell console.
-
#kill(*arguments) ⇒ String
Kills a current running process.
-
#lastlog(*arguments) ⇒ String
Shows when users last logged in.
-
#ls(*arguments) ⇒ String
Lists the files or directories.
-
#ls_a(*arguments) ⇒ String
Lists all files or directories.
-
#ls_l(*arguments) ⇒ String
Lists information about files or directories.
-
#ls_la(*arguments) ⇒ String
(also: #ls_al)
Lists information about all files or directories.
-
#mkdir(*arguments) ⇒ String
Creates a new directory.
-
#mktemp(*arguments) ⇒ String
Creates a tempfile.
-
#mktempdir(*arguments) ⇒ String
Creates a tempdir.
-
#netstat(*arguments) ⇒ String
Shows network connections.
-
#netstat_anp(*arguments) ⇒ String
Runs
netstat -anp
. -
#perl(*arguments) ⇒ String
Runs a PERL script.
-
#ping(*arguments) {|line| ... } ⇒ String
Pings an IP address.
-
#ps(*arguments) ⇒ String
Shows the current running processes.
-
#ps_aux(*arguments) ⇒ String
Runs
ps aux
. -
#pwd ⇒ String
Gets the current working directory.
-
#python(*arguments) ⇒ String
Runs a Python script.
-
#rm(*arguments) {|line| ... } ⇒ String
Removes one or more files or directories.
-
#rm_r(*arguments) ⇒ String
Runs
rm -r
. -
#rm_rf(*arguments) ⇒ String
Runs
rm -rf
. -
#rmdir(*arguments) ⇒ String
Removes a directory.
-
#rsync(*arguments) ⇒ String
Runs
rsync
. -
#rsync_a(*arguments) ⇒ String
Runs
rsync -a
. -
#ruby(*arguments) ⇒ String
Runs a Ruby script.
-
#run(command, *arguments) ⇒ String
Executes a shell command and returns it's output.
-
#system(command, *arguments) ⇒ nil
Executes a command and prints the resulting output.
-
#tail(*arguments) ⇒ String
Reads the last
n
lines of one or more files. -
#tail_n(lines, *arguments) ⇒ String
Reads the last
n
lines of one or more files. -
#time ⇒ Time
Gets the current time from the shell.
-
#touch(*arguments) ⇒ String
Touches a file.
-
#uid ⇒ Integer
The UID of the current user.
-
#w(*arguments) ⇒ String
Similar to #who but runs the
w
command. -
#wget(*arguments) ⇒ String
Runs
wget
. -
#wget_out(path, *arguments) ⇒ String
Runs
wget -O
. -
#which(*arguments) ⇒ String
Finds a program available to the shell.
-
#who(*arguments) ⇒ String
Shows who is currently logged in.
-
#whoami(*arguments) ⇒ String
The name of the current user.
Methods inherited from Resource
Constructor Details
#initialize(session) ⇒ Shell
Initializes the Shell resource.
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
#env ⇒ Hash{String => String} (readonly)
Persistent environment variables for the shell.
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.
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
.
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.
120 121 122 |
# File 'lib/ronin/post_ex/system/shell.rb', line 120 def cd(path) @pwd = File.(path,pwd) end |
#cp(*arguments) ⇒ String
Copies one or more files or directories.
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
.
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
.
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
.
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
.
608 609 610 |
# File 'lib/ronin/post_ex/system/shell.rb', line 608 def curl_out(path,*arguments) curl('-O',path,*arguments) end |
#date ⇒ Date
Gets the current time and date from 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
.
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.
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
.
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.
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.
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
.
958 959 960 |
# File 'lib/ronin/post_ex/system/shell.rb', line 958 def gcc(*arguments) run('gcc',*arguments) end |
#gid ⇒ 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.
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.
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.
304 305 306 |
# File 'lib/ronin/post_ex/system/shell.rb', line 304 def head_n(lines,*arguments) head('-n',lines,*arguments) end |
#id ⇒ Hash{Symbol => String}
The ID information of the current user.
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.
887 888 889 |
# File 'lib/ronin/post_ex/system/shell.rb', line 887 def ifconfig(*arguments) run('ifconfig',*arguments) end |
#interact ⇒ Object
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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
.
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.
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.
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.
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
.
857 858 859 |
# File 'lib/ronin/post_ex/system/shell.rb', line 857 def ps_aux(*arguments) ps('aux',*arguments) end |
#pwd ⇒ String
Gets 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.
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.
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
.
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
.
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.
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
.
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
.
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.
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.
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.
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.
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.
339 340 341 |
# File 'lib/ronin/post_ex/system/shell.rb', line 339 def tail_n(lines,*arguments) tail('-n',lines,*arguments) end |
#time ⇒ Time
Gets the current time from 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.
418 419 420 |
# File 'lib/ronin/post_ex/system/shell.rb', line 418 def touch(*arguments) run('touch',*arguments) end |
#uid ⇒ 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.
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
.
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
.
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.
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.
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.
754 755 756 |
# File 'lib/ronin/post_ex/system/shell.rb', line 754 def whoami(*arguments) run('whoami',*arguments).chomp end |