Skip to content

Commit 862acbd

Browse files
committed
Improve screensaver management
- Add modern ways to unlock Linux machines remotely - Use proper `register_options` - Clarify the actions: lock/unlock, start/stop - Add more platforms - Add a couple of checks before running the commands
1 parent 062a1e7 commit 862acbd

File tree

1 file changed

+82
-20
lines changed

1 file changed

+82
-20
lines changed

modules/post/multi/manage/screensaver.rb

Lines changed: 82 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
##
55

66
class MetasploitModule < Msf::Post
7+
Rank = ExcellentRanking
8+
79
def initialize(info = {})
810
super(
911
update_info(
@@ -14,28 +16,39 @@ def initialize(info = {})
1416
lock the current session.
1517
},
1618
'License' => MSF_LICENSE,
17-
'Author' => [ 'Eliott Teissonniere'],
18-
'Platform' => [ 'linux', 'osx', 'win' ],
19+
'Author' => [
20+
'Eliott Teissonniere', # Metasploit module
21+
'Julien Voisin' # Linux improvements
22+
],
23+
'Platform' => [ 'linux', 'osx', 'win', 'unix', 'solaris' ],
1924
'SessionTypes' => [ 'shell', 'meterpreter' ],
2025
'Actions' => [
2126
[ 'LOCK', { 'Description' => 'Lock the current session' } ],
27+
[ 'UNLOCK', { 'Description' => 'Unlock the current session' } ],
2228
[ 'START', { 'Description' => 'Start the screensaver, may lock the current session' } ],
23-
[ 'STOP', { 'Description' => 'Stop the screensaver, user may be prompted for its password' }]
24-
]
29+
[ 'STOP', { 'Description' => 'Stop the screensaver, user may be prompted for its password' }],
30+
],
31+
'References' => [
32+
['URL', 'https://gitlab.gnome.org/GNOME/gnome-shell/-/issues/7530']
33+
],
34+
'Notes' => {
35+
'Reliability' => [ ],
36+
'Stability' => [ ],
37+
'SideEffects' => [ ]
38+
}
2539
)
2640
)
2741
end
2842

2943
#
30-
# cmd_exec but with some controls and verbosity
44+
# cmd_exec but returning a boolean
3145
#
3246
def cmd_vexec(cmd)
33-
print_status("Executing '#{cmd}'")
47+
vprint_status("Executing '#{cmd}'")
3448

3549
begin
3650
cmd_exec(cmd)
37-
rescue EOFError
38-
print_error('Command failed')
51+
rescue StandardError
3952
return false
4053
end
4154

@@ -44,8 +57,27 @@ def cmd_vexec(cmd)
4457

4558
def lock_session
4659
case session.platform
47-
when 'linux'
48-
cmd_vexec('xdg-screensaver lock')
60+
when 'linux', 'solaris'
61+
ret = false
62+
if command_exists?('xdg-screensaver-lock')
63+
ret |= cmd_vexec('xdg-screensaver lock')
64+
end
65+
if command_exists?('qdbus')
66+
ret |= cmd_vexec('qdbus org.freedesktop.ScreenSaver /ScreenSaver Lock')
67+
end
68+
if command_exists?('dbus-send')
69+
ret |= cmd_exec('dbus-send --type=method_call --print-reply --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.SetActive boolean:true')
70+
end
71+
if command_exists?('loginctl')
72+
self.class.include Msf::Post::Linux::Priv
73+
if is_root?
74+
ret |= cmd_vexec('loginctl lock-sessions')
75+
else
76+
ret |= cmd_vexec('loginctl lock-session')
77+
end
78+
end
79+
print_error('Unable to lock session.') unless ret
80+
return ret
4981
when 'osx'
5082
cmd_vexec('pmset displaysleepnow')
5183
when 'windows'
@@ -55,9 +87,41 @@ def lock_session
5587
true
5688
end
5789

90+
def unlock_session
91+
case session.platform
92+
when 'linux', 'solaris'
93+
ret = false
94+
if command_exists?('xdg-screensaver')
95+
ret |= cmd_vexec('xdg-screensaver reset')
96+
end
97+
if command_exists?('qdbus')
98+
ret |= cmd_vexec('qdbus org.freedesktop.ScreenSaver /ScreenSaver Unlock')
99+
end
100+
if command_exists?('dbus-send')
101+
ret |= cmd_exec('dbus-send --type=method_call --print-reply --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.SetActive boolean:false')
102+
end
103+
if command_exists?('loginctl')
104+
self.class.include Msf::Post::Linux::Priv
105+
if is_root?
106+
ret |= cmd_vexec('loginctl unlock-sessions')
107+
else
108+
ret |= cmd_vexec('loginctl unlock-session')
109+
end
110+
end
111+
print_error('Unable to unlock session.') unless ret
112+
return ret
113+
when 'osx'
114+
fail_with(Msf::Exploit::Failure::NoTarget, 'Not supported on Mac OSX, you can still lock the screen or start the screensaver')
115+
when 'windows'
116+
fail_with(Msf::Exploit::Failure::NoTarget, 'Not supported on Windows, you can still lock the screen or start the screensaver')
117+
end
118+
119+
true
120+
end
121+
58122
def start_screensaver
59123
case session.platform
60-
when 'linux'
124+
when 'linux', 'solaris'
61125
cmd_vexec('xdg-screensaver activate')
62126
when 'osx'
63127
cmd_vexec('open -a ScreenSaverEngine')
@@ -70,27 +134,25 @@ def start_screensaver
70134

71135
def stop_screensaver
72136
case session.platform
73-
when 'linux'
74-
cmd_vexec('xdg-screensaver reset')
137+
when 'linux', 'solaris'
138+
cmd_vexec('xdg-screensaver reset') if command_exists?('xdg-screensaver')
75139
when 'osx'
76-
print_error('Not supported on Mac OSX, you can still lock the screen or start the screensaver')
77-
return false
140+
fail_with(Msf::Exploit::Failure::NoTarget, 'Not supported on Mac OSX, you can still lock the screen or start the screensaver')
78141
when 'windows'
79-
print_error('Not supported on Windows, you can still lock the screen or start the screensaver')
80-
return false
142+
fail_with(Msf::Exploit::Failure::NoTarget, 'Not supported on Windows, you can still lock the screen or start the screensaver')
81143
end
82144

83145
true
84146
end
85147

86148
def run
87-
if action.nil?
88-
print_error('Please specify an action')
89-
end
149+
print_error('Please specify an action') if action.nil?
90150

91151
case action.name
92152
when 'LOCK'
93153
return lock_session
154+
when 'UNLOCK'
155+
return unlock_session
94156
when 'START'
95157
return start_screensaver
96158
when 'STOP'

0 commit comments

Comments
 (0)