Skip to content

Commit 670055d

Browse files
committed
Prevent leaked sockets in edge cases
1 parent c76fa2c commit 670055d

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

lib/msf/core/modules/external/python/metasploit/probe_scanner.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,17 @@ async def __anext__(self):
6666
async def probe_host(host, port, payload, connect_timeout, read_timeout):
6767
buf = bytearray()
6868

69-
async with timeout(connect_timeout):
70-
r, w = await asyncio.open_connection(host, port)
71-
remote = w.get_extra_info('peername')
72-
if remote[0] == host:
73-
module.log('{}:{} - Connected'.format(host, port), level='debug')
74-
else:
75-
module.log('{}({}):{} - Connected'.format(host, *remote), level='debug')
76-
w.write(payload)
77-
await w.drain()
78-
7969
try:
70+
async with timeout(connect_timeout):
71+
r, w = await asyncio.open_connection(host, port)
72+
remote = w.get_extra_info('peername')
73+
if remote[0] == host:
74+
module.log('{}:{} - Connected'.format(host, port), level='debug')
75+
else:
76+
module.log('{}({}):{} - Connected'.format(host, *remote), level='debug')
77+
w.write(payload)
78+
await w.drain()
79+
8080
async with timeout(read_timeout):
8181
while len(buf) < 4096:
8282
data = await r.read(4096)
@@ -90,6 +90,12 @@ async def probe_host(host, port, payload, connect_timeout, read_timeout):
9090
pass
9191
else:
9292
raise
93+
finally:
94+
try:
95+
w.close()
96+
except Exception:
97+
# Either we got something and the socket got in a bad state, or the
98+
# original error will point to the root cause
99+
pass
93100

94-
w.close()
95101
return buf

0 commit comments

Comments
 (0)