Class: Ronin::PostEx::CLI::SystemShell

Inherits:
Core::CLI::CommandShell
  • Object
show all
Defined in:
lib/ronin/post_ex/cli/system_shell.rb

Overview

A shell for System.

Constant Summary collapse

WHENCE =

Mapping of String whence values to Integer values.

{
  'SEEK_SET'  => RemoteFile::SEEK_SET,
  'SEEK_CUR'  => RemoteFile::SEEK_CUR,
  'SEEK_END'  => RemoteFile::SEEK_END,
  'SEEK_DATA' => RemoteFile::SEEK_DATA,
  'SEEK_HOLE' => RemoteFile::SEEK_HOLE
}

Instance Method Summary collapse

Constructor Details

#initialize(system, **kwargs) ⇒ SystemShell

Initializes the file-system shell.

Parameters:

  • system (System)

    The file-system resource.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.



44
45
46
47
48
49
50
51
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 44

def initialize(system, **kwargs)
  super(**kwargs)

  @system = system

  @files = {}
  @next_file_id = 1
end

Instance Method Details

#file_close(file_id) ⇒ Object

Closes an opened file.

Parameters:

  • file_id (String)

    The file ID number.

See Also:



479
480
481
482
483
484
485
486
487
488
489
490
491
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 479

def file_close(file_id)
  file_id = file_id.to_i
  length  = length.to_i

  if (file = @files[file_id])
    file.close
    @files.delete(file_id)

    puts "Closed file ##{file_id} for #{file.path}"
  else
    print_error "unknown file id: #{file_id}"
  end
end

#file_open(path, mode = "r") ⇒ Object

Opens a file.

Parameters:

  • path (String)

    The path to the file.

  • mode (String) (defaults to: "r")

    Optional open mode.

See Also:



340
341
342
343
344
345
346
347
348
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 340

def file_open(path,mode="r")
  file    = @system.fs.open(path,mode)
  file_id = @next_file_id

  @files[file_id] = file
  @next_file_id  += 1

  puts "Opened file ##{file_id} for #{file.path}"
end

#file_read(file_id, length) ⇒ Object

Reads data from an opened file.

Parameters:

  • file_id (String)

    The file ID number.

  • length (String)

    The length of data to read.

See Also:

  • RemoteFile#read


430
431
432
433
434
435
436
437
438
439
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 430

def file_read(file_id,length)
  file_id = file_id.to_i
  length  = length.to_i

  if (file = @files[file_id])
    puts(file.read(length))
  else
    print_error "unknown file id: #{file_id}"
  end
end

#file_seek(file_id, pos, whence = 'SEEK_SET') ⇒ Object

Seeks to a position within an opened file.

Parameters:

  • file_id (String)

    The file ID number.

  • pos (String)

    The position to seek to.

  • whence (String) (defaults to: 'SEEK_SET')

    Where to seek relative from. Acceptable values are:

    • "SET"
    • "CUR"
    • "END"
    • "DATA"
    • "HOLE"

See Also:



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 397

def file_seek(file_id,pos,whence='SEEK_SET')
  unless WHENCE.has_key?(whence)
    print_error "unknown file.seek whence value (#{whence}), must be #{WHENCE.keys.join(', ')}"
    return false
  end

  file_id = file_id.to_i
  pos     = pos.to_i
  whence  = WHENCE[whence]

  if (file = @files[file_id])
    file.seek(pos,whence)
    puts file.pos
  else
    print_error "unknown file id: #{file_id}"
  end
end

Removes a file.

Parameters:

  • path (String)

    The file to be removed.

See Also:



180
181
182
183
184
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 180

def file_unlink(path)
  @system.fs.unlink(path)

  puts "Removed #{@system.fs.expand_path(path)}"
end

#file_write(file_id, data) ⇒ Object

Writes data from to an opened file.

Parameters:

  • file_id (String)

    The file ID number.

  • data (String)

    The data to write.

See Also:

  • RemoteFile#write


456
457
458
459
460
461
462
463
464
465
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 456

def file_write(file_id,data)
  file_id = file_id.to_i
  length  = length.to_i

  if (file = @files[file_id])
    puts file.write(length)
  else
    print_error "unknown file id: #{file_id}"
  end
end

#filesObject

Lists opened files.



355
356
357
358
359
360
361
362
363
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 355

def files
  @files.each_with_index do |file,index|
    if file
      id = index + 1

      puts "  [#{id}] #{file.path}"
    end
  end
end

#fs_chdir(path) ⇒ Object

Changes the working directory.

Parameters:

  • path (String)

    The new working directory.

See Also:



65
66
67
68
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 65

def fs_chdir(path)
  @system.fs.chdir(path)
  puts "Current working directory is now: #{@system.fs.pwd}"
end

#fs_chgrp(group, path) ⇒ Object

Changes group ownership of a file or directory.

Parameters:

  • group (String)

    The desired new group.

  • path (String)

    The path of the file or directory.

See Also:



282
283
284
285
286
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 282

def fs_chgrp(group,path)
  @system.fs.chgrp(group,path)

  puts "Changed group ownership of #{@system.fs.expand_path(path)} to #{group}"
end

#fs_chmod(mode, path) ⇒ Object

Changes the permissions of a file or directory.

Parameters:

  • mode (String)

    The desired new octal permission mode.

  • path (String)

    The path of the file or directory.

See Also:



303
304
305
306
307
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 303

def fs_chmod(mode,path)
  @system.fs.chmod(mode.to_i(8),path)

  puts "Changed permissions on #{@system.fs.expand_path(path)} to #{mode}"
end

#fs_chown(user, path) ⇒ Object

Changes ownership of a file or directory.

Parameters:

  • user (String)

    The desired new user.

  • path (String)

    The path of the file or directory.

See Also:



261
262
263
264
265
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 261

def fs_chown(user,path)
  @system.fs.chown(user,path)

  puts "Changed ownership of #{@system.fs.expand_path(path)} to #{user}"
end

#fs_copy(src, dest) ⇒ Object

Copies a file to a destination.

Parameters:

  • src (String)

    The file to copy.

  • dest (String)

    The destination to copy the file to.

See Also:



162
163
164
165
166
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 162

def fs_copy(src,dest)
  @system.fs.copy(src,dest)

  puts "Copied #{@system.fs.expand_path(src)} -> #{@fs.expand_path(dest)}"
end

Creates a link to a file or directory.

Parameters:

  • src (String)

    The file or directory to link to.

  • dest (String)

    The path of the new link.

See Also:



240
241
242
243
244
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 240

def fs_link(src,dest)
  @system.fs.link(src,dest)

  puts "Linked #{@system.fs.expand_path(src)} -> #{@fs.expand_path(dest)}"
end

#fs_mv(src, dest) ⇒ Object

Moves a file or directory.

Parameters:

  • src (String)

    The file or directory to move.

  • dest (String)

    The destination to move the file or directory to.

See Also:



219
220
221
222
223
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 219

def fs_mv(src,dest)
  @system.fs.move(src,dest)

  puts "Moved #{@system.fs.expand_path(src)} -> #{@fs.expand_path(dest)}"
end

#fs_pwdObject

Prints the current working directory.

See Also:



78
79
80
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 78

def fs_pwd
  puts "Current working directory: #{@system.fs.getcwd}"
end

#fs_readdir(path) ⇒ Object

Reads the entries of a directory.

Parameters:

  • path (String)

    The path to the directory.

See Also:



126
127
128
129
130
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 126

def fs_readdir(path)
  @system.fs.readdir(path).each do |entry|
    puts entry
  end
end

#fs_readfile(path) ⇒ Object

Reads data from a file.

Parameters:

  • path (String)

    The file to read from.

See Also:

  • System::FS#read


94
95
96
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 94

def fs_readfile(path)
  write(@system.fs.readfile(path))
end

Reads the destination of a link.

Parameters:

  • path (String)

    The path to the link.

See Also:



110
111
112
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 110

def fs_readlink(path)
  puts @system.fs.readlink(path)
end

#fs_rmdir(path) ⇒ Object

Removes an empty directory.

Parameters:

  • path (String)

    The file to be removed.

See Also:



198
199
200
201
202
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 198

def fs_rmdir(path)
  @system.fs.rmdir(path)

  puts "Removed directory #{@system.fs.expand_path(path)}"
end

#fs_stat(path) ⇒ Object

Stats a file or directory.

Parameters:

  • path (String)

    The file or directory to stat.

See Also:



321
322
323
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 321

def fs_stat(path)
  stat = @system.fs.stat(path)
end

#hexdump(path) ⇒ Object

Hexdumps a file.

Parameters:

  • path (String)

    The file to hexdump.

See Also:



143
144
145
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 143

def hexdump(path)
  @system.fs.hexdump(path,self)
end

#process_egidObject

Prints the process'es EGID.



608
609
610
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 608

def process_egid
  puts @system.process.getegid
end

#process_envObject

Prints the process'es environment variables.



662
663
664
665
666
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 662

def process_env
  @system.process.env.each do |name,value|
    puts "#{name}=#{value}"
  end
end

#process_euidObject

Prints the process'es EUID.



553
554
555
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 553

def process_euid
  puts @system.process.geteuid
end

#process_getenv(name) ⇒ Object

Prints a specific environment variable from the process.

Parameters:

  • name (String)

    The environment variable name.

See Also:



680
681
682
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 680

def process_getenv(name)
  puts @system.process.getenv(name)
end

#process_gidObject

Prints the process'es GID.



581
582
583
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 581

def process_gid
  puts @system.process.getgid
end

#process_kill(pid, signal = 'KILL') ⇒ Object

Kills a process.

Parameters:

  • pid (String)

    The process PID to kill.

  • signal (String) (defaults to: 'KILL')

    The signal to send the process.



731
732
733
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 731

def process_kill(pid,signal='KILL')
  @system.process.kill(pid.to_i,signal)
end

#process_pidObject

Prints the process'es PID.



501
502
503
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 501

def process_pid
  puts @system.process.getpid
end

#process_ppidObject

Prints the process'es PPID.



513
514
515
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 513

def process_ppid
  puts @system.process.getppid
end

#process_setegid(new_egid) ⇒ Object

Sets the process'es EGID.

Parameters:

  • new_egid (String)

See Also:



623
624
625
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 623

def process_setegid(new_egid)
  @system.process.setegid(new_egid.to_i)
end

#process_setenv(name_and_value) ⇒ Object

Sets a specific environment variable from the process.

Parameters:

  • name_and_value (String)

    The environment variable name.

See Also:



696
697
698
699
700
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 696

def process_setenv(name_and_value)
  name, value = name_and_value.split('=',2)

  @system.process.setenv(name,value)
end

#process_seteuid(new_euid) ⇒ Object

Sets the process'es EUID.

Parameters:

  • new_euid (String)

See Also:



568
569
570
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 568

def process_seteuid(new_euid)
  @system.process.seteuid(new_euid.to_i)
end

#process_setgid(new_gid) ⇒ Object

Sets the process'es GID.

Parameters:

  • new_gid (String)

See Also:



596
597
598
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 596

def process_setgid(new_gid)
  @system.process.setgid(new_gid.to_i)
end

#process_setsid(new_sid) ⇒ Object

Sets the process'es SID.

Parameters:

  • new_sid (String)

See Also:



650
651
652
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 650

def process_setsid(new_sid)
  @system.process.setsid(new_sid.to_i)
end

#process_setuid(new_uid) ⇒ Object

Sets the process'es UID.

Parameters:

  • new_uid (String)

See Also:



540
541
542
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 540

def process_setuid(new_uid)
  @system.process.setuid(new_uid.to_i)
end

#process_sidObject

Prints the process'es SID.



635
636
637
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 635

def process_sid
  puts @system.process.getsid
end

#process_spawn(program, *arguments) ⇒ Object

Spawns a new process.

Parameters:

  • program (String)

    The program name.

  • arguments (Array<String>)

    Additional command arguments.

See Also:



750
751
752
753
754
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 750

def process_spawn(program,*arguments)
  pid = @system.process.spawn(program,*arguments)

  puts "PID: #{pid}"
end

#process_uidObject

Prints the process'es UID.



525
526
527
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 525

def process_uid
  puts @system.process.getuid
end

#process_unsetenv(name) ⇒ Object

Unsets a process environment variable.

Parameters:

  • name (String)

    The environment variable to unset.

See Also:



714
715
716
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 714

def process_unsetenv(name)
  @system.process.unsetenv(name)
end

#shellObject

Spawns an interactive command sub-shell.



780
781
782
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 780

def shell
  ShellShell.start(@system.shell)
end

#shell_exec(command) ⇒ Object

Executes a shell command.

Parameters:

  • command (String)

    The command to execute.

See Also:



768
769
770
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 768

def shell_exec(command)
  puts @system.shell.run(command)
end

#sys_hostnameObject

Prints the system's hostname.

See Also:



804
805
806
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 804

def sys_hostname
  puts @system.hostname
end

#sys_timeObject

Prints the system's current time.

See Also:



792
793
794
# File 'lib/ronin/post_ex/cli/system_shell.rb', line 792

def sys_time
  puts @system.time
end