Skip to content

Post Mixins

Mixins for post modules live under lib/msf/core/post/. Inheriting from Msf::Post provides the foundation for Common and File functionality; include OS-specific features separately. The bundled wiki page Post-Mixins.md is another reference.

include Msf::Post::Common
hostname = cmd_exec('hostname')
out = create_process('grep', args: ['-r', 'password', '/var/www'], time_out: 30)
home = get_env('HOME')
envs = get_envs('HOME', 'USER', 'PATH')
print_status('python3 ready') if command_exists?('python3')
MethodPurpose
cmd_execRun a command string (simple)
create_processRun an executable with an argument array (often preferable)
cmd_exec_with_resultOutput and success status
get_env / get_envsEnvironment variables
command_exists?Check whether a command exists
rhost / rport / peerDisplay target information
include Msf::Post::File
cd('/tmp')
print_status(pwd)
entries = dir('/etc')
if file?('/etc/passwd') && readable?('/etc/passwd')
data = read_file('/etc/passwd')
store_loot('linux.passwd', 'text/plain', session, data, 'passwd')
end
write_file('/tmp/out.txt', "hello\n")
append_file('/tmp/out.txt', "world\n")
upload_file('/tmp/tool', '/local/path/tool')
upload_and_chmodx('/tmp/run.bin', File.binread('/local/run.bin'))
rm_f('/tmp/out.txt')
MethodPurpose
pwd / cd / dir (ls)Navigate and list
exist? / file? / directory?File type
readable? / writable? / executable?Permissions
read_file / write_file / append_fileRead and write files
upload_file / upload_and_chmodxUpload files
rm_f / rm_rf / mkdir / rename_file / copy_fileManage files and directories
expand_pathExpand $HOME or %APPDATA%
file_remote_digestmd5 and othersHash files
include Msf::Post::Process
get_processes.each { |p| print_line("#{p['pid']} #{p['name']}") }
kill_process(pid) if has_pid?(pid)
include Msf::Post::Unix
print_good('root') if is_root?
get_users.each { |u| print_line(u['name']) }
include Msf::Post::Windows::Registry
val = registry_getvaldata('HKLM\\SOFTWARE\\...', 'ProductName')

Windows-specific functionality is split into Registry, Accounts, Powershell, Services, LDAP, and others under lib/msf/core/post/windows/. Include only what you need.

TaskCombination
Run and display one commandCommon: cmd_exec
Run with separate argumentsCommon: create_process
Collect a configuration fileFile: readable? → read_file → store_loot
Place a temporary binaryFile: write_file / upload_and_chmodx + FileDropper
List users on LinuxUnix: get_users / is_root?
Read a Windows registry valueWindows::Registry
Stop a processProcess: pidof / kill_process
  • Use cmd_exec for a static one-line command; use create_process when arguments should be separate.
  • Handle OS path differences with expand_path or absolute paths.
  • For Meterpreter-only features such as Railgun, limit SessionTypes to meterpreter and check session.type.
  • For the full structure of a post module, see Post.