Skip to content

Commit bd94a46

Browse files
committed
Land rapid7#8884, Fix logic for on_session callbacks
2 parents 602a29a + d3775c3 commit bd94a46

File tree

1 file changed

+25
-31
lines changed

1 file changed

+25
-31
lines changed

lib/msf/base/sessions/meterpreter_options.rb

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,41 +31,42 @@ def initialize(info = {})
3131
# advanced option is set to true.
3232
#
3333
def on_session(session)
34-
super
35-
36-
# Defer the session initialization to the Session Manager scheduler
37-
framework.sessions.schedule Proc.new {
38-
39-
# Configure unicode encoding before loading stdapi
40-
session.encode_unicode = datastore['EnableUnicodeEncoding']
41-
42-
session.init_ui(self.user_input, self.user_output)
43-
44-
valid = true
45-
46-
session.tlv_enc_key = session.core.negotiate_tlv_encryption
47-
48-
if datastore['AutoVerifySession']
49-
if not session.is_valid_session?(datastore['AutoVerifySessionTimeout'].to_i)
50-
print_error("Meterpreter session #{session.sid} is not valid and will be closed")
51-
valid = false
34+
init_session = Proc.new do
35+
# Configure unicode encoding before loading stdapi
36+
session.encode_unicode = datastore['EnableUnicodeEncoding']
37+
38+
session.init_ui(self.user_input, self.user_output)
39+
40+
print_good("negotiating tlv encryption")
41+
session.tlv_enc_key = session.core.negotiate_tlv_encryption
42+
print_good("negotiated tlv encryption")
43+
44+
if datastore['AutoVerifySession']
45+
if !session.is_valid_session?(datastore['AutoVerifySessionTimeout'].to_i)
46+
print_error("Meterpreter session #{session.sid} is not valid and will be closed")
47+
# Terminate the session without cleanup if it did not validate
48+
session.skip_cleanup = true
49+
session.kill
50+
return nil
51+
end
5252
end
53-
end
53+
print_good("negotiated tlv encryption")
5454

55-
if valid
5655
# always make sure that the new session has a new guid if it's not already known
5756
guid = session.session_guid
5857
if guid == '00000000-0000-0000-0000-000000000000'
5958
guid = SecureRandom.uuid
6059
session.core.set_session_guid(guid)
6160
session.session_guid = guid
62-
# TODO: New statgeless session, do some account in the DB so we can track it later.
61+
# TODO: New stageless session, do some account in the DB so we can track it later.
6362
else
6463
# TODO: This session was either staged or previously known, and so we shold do some accounting here!
6564
end
6665

67-
if datastore['AutoLoadStdapi']
66+
# Call registered on_session callbacks
67+
super
6868

69+
if datastore['AutoLoadStdapi']
6970
session.load_stdapi
7071

7172
if datastore['AutoSystemInfo']
@@ -91,16 +92,9 @@ def on_session(session)
9192
end
9293
end
9394

94-
# Terminate the session without cleanup if it did not validate
95-
if not valid
96-
session.skip_cleanup = true
97-
session.kill
98-
end
99-
100-
}
101-
95+
# Defer the session initialization to the Session Manager scheduler
96+
framework.sessions.schedule init_session
10297
end
103-
10498
end
10599
end
106100
end

0 commit comments

Comments
 (0)