Skip to content

Commit c2fec5d

Browse files
committed
Land rapid7#7770, Improve TCP channel handling
2 parents 2856fac + fdca963 commit c2fec5d

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed

lib/rex/post/meterpreter/channel.rb

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,22 @@ def request_handler(client, packet)
5757
cid = packet.get_tlv_value(TLV_TYPE_CHANNEL_ID)
5858

5959
# No channel identifier, then drop it
60-
if (cid == nil)
60+
if cid.nil?
6161
return false
6262
end
6363

6464
channel = client.find_channel(cid)
6565

6666
# No valid channel context? The channel may not be registered yet
67-
if (channel == nil)
67+
if channel.nil?
6868
return false
6969
end
7070

7171

7272
dio = channel.dio_map(packet.method)
7373

7474
# Supported DIO request? Dump it.
75-
if (dio == nil)
75+
if dio.nil?
7676
return true
7777
end
7878

@@ -98,12 +98,12 @@ def Channel.create(client, type = nil, klass = nil,
9898
request = Packet.create_request('core_channel_open')
9999

100100
# Set the type of channel that we're allocating
101-
if (type != nil)
101+
if !type.nil?
102102
request.add_tlv(TLV_TYPE_CHANNEL_TYPE, type)
103103
end
104104

105105
# If no factory class was provided, use the default native class
106-
if (klass == nil)
106+
if klass.nil?
107107
klass = self
108108
end
109109

@@ -112,15 +112,20 @@ def Channel.create(client, type = nil, klass = nil,
112112
request.add_tlvs(addends);
113113

114114
# Transmit the request and wait for the response
115-
response = client.send_request(request)
116-
cid = response.get_tlv_value(TLV_TYPE_CHANNEL_ID)
117-
118-
return nil unless cid
119-
120-
# Create the channel instance
121-
channel = klass.new(client, cid, type, flags)
115+
cid = nil
116+
begin
117+
response = client.send_request(request)
118+
cid = response.get_tlv_value(TLV_TYPE_CHANNEL_ID)
119+
rescue RequestError
120+
# Handle channel open failure exceptions
121+
end
122122

123-
return channel
123+
if cid
124+
# Create the channel instance
125+
klass.new(client, cid, type, flags)
126+
else
127+
raise Rex::ConnectionRefused
128+
end
124129
end
125130

126131
##
@@ -169,13 +174,13 @@ def read(length = nil, addends = nil)
169174
# Reads data from the remote half of the channel.
170175
#
171176
def _read(length = nil, addends = nil)
172-
if (self.cid == nil)
177+
if self.cid.nil?
173178
raise IOError, "Channel has been closed.", caller
174179
end
175180

176181
request = Packet.create_request('core_channel_read')
177182

178-
if (length == nil)
183+
if length.nil?
179184
# Default block size to a higher amount for passive dispatcher
180185
length = self.client.passive_service ? (1024*1024) : 65536
181186
end
@@ -217,7 +222,7 @@ def write(buf, length = nil, addends = nil)
217222
#
218223
def _write(buf, length = nil, addends = nil)
219224

220-
if (self.cid == nil)
225+
if self.cid.nil?
221226
raise IOError, "Channel has been closed.", caller
222227
end
223228

@@ -245,7 +250,7 @@ def _write(buf, length = nil, addends = nil)
245250
response = self.client.send_request(request)
246251
written = response.get_tlv(TLV_TYPE_LENGTH)
247252

248-
return (written == nil) ? 0 : written.value
253+
written.nil? ? 0 : written.value
249254
end
250255

251256
#
@@ -273,7 +278,7 @@ def close_read
273278
# Closes the channel.
274279
#
275280
def self._close(client, cid, addends=nil)
276-
if (cid == nil)
281+
if cid.nil?
277282
raise IOError, "Channel has been closed.", caller
278283
end
279284

@@ -302,7 +307,7 @@ def _close(addends = nil)
302307
# Enables or disables interactive mode.
303308
#
304309
def interactive(tf = true, addends = nil)
305-
if (self.cid == nil)
310+
if self.cid.nil?
306311
raise IOError, "Channel has been closed.", caller
307312
end
308313

lib/rex/post/meterpreter/extensions/stdapi/net/socket_subsystem/tcp_client_channel.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ def TcpClientChannel.open(client, params)
5656
'value' => params.retries
5757
}
5858
])
59-
c.params = params
59+
if c
60+
c.params = params
61+
end
6062
c
6163
end
6264

modules/auxiliary/scanner/portscan/tcp.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@ def run_host(ip)
8080
'ConnectTimeout' => (timeout / 1000.0)
8181
}
8282
)
83-
print_status("#{ip}:#{port} - TCP OPEN")
84-
r << [ip,port,"open"]
83+
if s
84+
print_status("#{ip}:#{port} - TCP OPEN")
85+
r << [ip,port,"open"]
86+
end
8587
rescue ::Rex::ConnectionRefused
8688
vprint_status("#{ip}:#{port} - TCP closed")
8789
r << [ip,port,"closed"]
@@ -92,7 +94,9 @@ def run_host(ip)
9294
rescue ::Exception => e
9395
print_error("#{ip}:#{port} exception #{e.class} #{e} #{e.backtrace}")
9496
ensure
95-
disconnect(s) rescue nil
97+
if s
98+
disconnect(s) rescue nil
99+
end
96100
end
97101
end
98102
end

0 commit comments

Comments
 (0)