Skip to content

Commit 91700f1

Browse files
author
Brent Cook
committed
tidy up the ruby style while we're in here testing
1 parent ac8b483 commit 91700f1

File tree

1 file changed

+54
-58
lines changed

1 file changed

+54
-58
lines changed

lib/msf/core/handler/reverse_tcp.rb

Lines changed: 54 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
module Msf
66
module Handler
7-
87
###
98
#
109
# This module implements the reverse TCP handler. This means
@@ -16,7 +15,6 @@ module Handler
1615
#
1716
###
1817
module ReverseTcp
19-
2018
include Msf::Handler
2119
include Msf::Handler::Reverse
2220
include Msf::Handler::Reverse::Comm
@@ -26,7 +24,7 @@ module ReverseTcp
2624
# 'reverse_tcp'.
2725
#
2826
def self.handler_type
29-
return "reverse_tcp"
27+
"reverse_tcp"
3028
end
3129

3230
#
@@ -55,7 +53,6 @@ def initialize(info = {})
5553
self.conn_threads = []
5654
end
5755

58-
5956
#
6057
# Closes the listener socket if one was created.
6158
#
@@ -64,9 +61,13 @@ def cleanup_handler
6461

6562
# Kill any remaining handle_connection threads that might
6663
# be hanging around
67-
conn_threads.each { |thr|
68-
thr.kill rescue nil
69-
}
64+
conn_threads.each do |thr|
65+
begin
66+
thr.kill
67+
rescue
68+
nil
69+
end
70+
end
7071
end
7172

7273
# A string suitable for displaying to the user
@@ -84,137 +85,132 @@ def start_handler
8485

8586
local_port = bind_port
8687

87-
self.listener_thread = framework.threads.spawn("ReverseTcpHandlerListener-#{local_port}", false, queue) { |lqueue|
88+
handler_name = "ReverseTcpHandlerListener-#{local_port}"
89+
self.listener_thread = framework.threads.spawn(handler_name, false, queue) { |lqueue|
8890
loop do
8991
# Accept a client connection
9092
begin
91-
client = self.listener_sock.accept
92-
if ! client
93-
wlog("ReverseTcpHandlerListener-#{local_port}: No client received in call to accept, exiting...")
93+
client = listener_sock.accept
94+
if !client
95+
wlog("#{handler_name}: No client received in call to accept, exiting...")
9496
else
9597
self.pending_connections += 1
9698
lqueue.push(client)
9799
end
98-
rescue ::Exception
99-
wlog("ReverseTcpHandlerListener-#{local_port}: Exception raised during listener accept: #{$!}\n\n#{$@.join("\n")}")
100+
rescue StandardError
101+
wlog("#{handler_name}: Exception raised during listener accept: #{$ERROR_INFO}\n\n#{$ERROR_POSITION.join("\n")}")
100102
break
101103
end
102104
end
103105
}
104106

105-
self.handler_thread = framework.threads.spawn("ReverseTcpHandlerWorker-#{local_port}", false, queue) { |cqueue|
107+
worker_name = "ReverseTcpHandlerWorker-#{local_port}"
108+
self.handler_thread = framework.threads.spawn(worker_name, false, queue) { |cqueue|
106109
loop do
107110
begin
108111
client = cqueue.pop
109112

110-
if ! client
111-
elog("ReverseTcpHandlerWorker-#{local_port}: Queue returned an empty result, exiting...")
112-
break
113+
unless client
114+
elog("#{worker_name}: Queue returned an empty result, exiting...")
113115
end
114116

115117
# Timeout and datastore options need to be passed through to the client
116118
opts = {
117-
:datastore => datastore,
118-
:expiration => datastore['SessionExpirationTimeout'].to_i,
119-
:comm_timeout => datastore['SessionCommunicationTimeout'].to_i,
120-
:retry_total => datastore['SessionRetryTotal'].to_i,
121-
:retry_wait => datastore['SessionRetryWait'].to_i
119+
datastore: datastore,
120+
expiration: datastore['SessionExpirationTimeout'].to_i,
121+
comm_timeout: datastore['SessionCommunicationTimeout'].to_i,
122+
retry_total: datastore['SessionRetryTotal'].to_i,
123+
retry_wait: datastore['SessionRetryWait'].to_i
122124
}
123125

124126
if datastore['ReverseListenerThreaded']
125-
self.conn_threads << framework.threads.spawn("ReverseTcpHandlerSession-#{local_port}-#{client.peerhost}", false, client) { |client_copy|
127+
thread_name = "#{worker_name}-#{client.peerhost}"
128+
conn_threads << framework.threads.spawn(thread_name, false, client) do |client_copy|
126129
handle_connection(wrap_aes_socket(client_copy), opts)
127-
}
130+
end
128131
else
129132
handle_connection(wrap_aes_socket(client), opts)
130133
end
131-
rescue ::Exception
132-
elog("Exception raised from handle_connection: #{$!.class}: #{$!}\n\n#{$@.join("\n")}")
134+
rescue StandardError
135+
elog("Exception raised from handle_connection: #{$ERROR_INFO.class}: #{$ERROR_INFO}\n\n#{$ERROR_POSITION.join("\n")}")
133136
end
134137
end
135138
}
136-
137139
end
138140

139141
def wrap_aes_socket(sock)
140-
if datastore["PAYLOAD"] !~ /java\// or (datastore["AESPassword"] || "") == ""
142+
if datastore["PAYLOAD"] !~ %r{java/} || (datastore["AESPassword"] || "") == ""
141143
return sock
142144
end
143145

144-
socks = Rex::Socket::tcp_socket_pair()
146+
socks = Rex::Socket.tcp_socket_pair
145147
socks[0].extend(Rex::Socket::Tcp)
146148
socks[1].extend(Rex::Socket::Tcp)
147149

148150
m = OpenSSL::Digest.new('md5')
149151
m.reset
150152
key = m.digest(datastore["AESPassword"] || "")
151153

152-
Rex::ThreadFactory.spawn('Session-AESEncrypt', false) {
154+
Rex::ThreadFactory.spawn('Session-AESEncrypt', false) do
153155
c1 = OpenSSL::Cipher.new('aes-128-cfb8')
154156
c1.encrypt
155-
c1.key=key
157+
c1.key = key
156158
sock.put([0].pack('N'))
157-
sock.put(c1.iv=c1.random_iv)
159+
sock.put((c1.iv = c1.random_iv))
158160
buf1 = socks[0].read(4096)
159-
while buf1 and buf1 != ""
161+
while buf1 && buf1 != ""
160162
sock.put(c1.update(buf1))
161163
buf1 = socks[0].read(4096)
162164
end
163-
sock.close()
164-
}
165-
Rex::ThreadFactory.spawn('Session-AESDecrypt', false) {
165+
sock.close
166+
end
167+
168+
Rex::ThreadFactory.spawn('Session-AESDecrypt', false) do
166169
c2 = OpenSSL::Cipher.new('aes-128-cfb8')
167170
c2.decrypt
168-
c2.key=key
169-
iv=""
170-
while iv.length < 16
171-
iv << sock.read(16-iv.length)
172-
end
171+
c2.key = key
172+
173+
iv = ""
174+
iv << sock.read(16 - iv.length) while iv.length < 16
175+
173176
c2.iv = iv
174177
buf2 = sock.read(4096)
175-
while buf2 and buf2 != ""
178+
while buf2 && buf2 != ""
176179
socks[0].put(c2.update(buf2))
177180
buf2 = sock.read(4096)
178181
end
179-
socks[0].close()
180-
}
181-
return socks[1]
182+
socks[0].close
183+
end
184+
185+
socks[1]
182186
end
183187

184188
#
185189
# Stops monitoring for an inbound connection.
186190
#
187191
def stop_handler
188192
# Terminate the listener thread
189-
if (self.listener_thread and self.listener_thread.alive? == true)
190-
self.listener_thread.kill
191-
self.listener_thread = nil
192-
end
193+
listener_thread.kill if listener_thread && listener_thread.alive? == true
193194

194195
# Terminate the handler thread
195-
if (self.handler_thread and self.handler_thread.alive? == true)
196-
self.handler_thread.kill
197-
self.handler_thread = nil
198-
end
196+
handler_thread.kill if handler_thread && handler_thread.alive? == true
199197

200-
if (self.listener_sock)
198+
if listener_sock
201199
begin
202-
self.listener_sock.close
200+
listener_sock.close
203201
rescue IOError
204202
# Ignore if it's listening on a dead session
205203
dlog("IOError closing listener sock; listening on dead session?", LEV_1)
206204
end
207-
self.listener_sock = nil
208205
end
209206
end
210207

211-
protected
208+
protected
212209

213210
attr_accessor :listener_sock # :nodoc:
214211
attr_accessor :listener_thread # :nodoc:
215212
attr_accessor :handler_thread # :nodoc:
216213
attr_accessor :conn_threads # :nodoc:
217214
end
218-
219215
end
220216
end

0 commit comments

Comments
 (0)