|
| 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 | + sys_bineval(argv[1]); |
| 43 | + |
| 44 | + exit(0); |
| 45 | +} |
| 46 | + |
| 47 | +int sys_bineval(char *argv) |
| 48 | +{ |
| 49 | + size_t len; |
| 50 | + |
| 51 | +#if defined(_WIN32) || defined(_WIN64) || defined(__WIN32__) || defined(WIN32) |
| 52 | + int pID; |
| 53 | + char *code; |
| 54 | +#else |
| 55 | + int *addr; |
| 56 | + size_t page_size; |
| 57 | + pid_t pID; |
| 58 | +#endif |
| 59 | + |
| 60 | + len = (size_t)strlen(argv); |
| 61 | + |
| 62 | +#if defined(_WIN32) || defined(_WIN64) || defined(__WIN32__) || defined(WIN32) |
| 63 | + // allocate a +rwx memory page |
| 64 | + code = (char *) VirtualAlloc(NULL, len+1, MEM_COMMIT, PAGE_EXECUTE_READWRITE); |
| 65 | + strncpy(code, argv, len); |
| 66 | + |
| 67 | + WaitForSingleObject(CreateThread(NULL, 0, exec_payload, code, 0, &pID), INFINITE); |
| 68 | +#else |
| 69 | + pID = fork(); |
| 70 | + if(pID<0) |
| 71 | + return 1; |
| 72 | + |
| 73 | + if(pID==0) |
| 74 | + { |
| 75 | + page_size = (size_t)sysconf(_SC_PAGESIZE)-1; // get page size |
| 76 | + page_size = (len+page_size) & ~(page_size); // align to page boundary |
| 77 | + |
| 78 | + // mmap an rwx memory page |
| 79 | + addr = mmap(0, page_size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_SHARED|MAP_ANONYMOUS, 0, 0); |
| 80 | + |
| 81 | + if (addr == MAP_FAILED) |
| 82 | + return 1; |
| 83 | + |
| 84 | + strncpy((char *)addr, argv, len); |
| 85 | + |
| 86 | + ((void (*)(void))addr)(); |
| 87 | + } |
| 88 | + |
| 89 | + if(pID>0) |
| 90 | + waitpid(pID, 0, WNOHANG); |
| 91 | +#endif |
| 92 | + |
| 93 | + return 0; |
| 94 | +} |
| 95 | + |
| 96 | +#if defined(_WIN64) |
| 97 | +void __exec_payload(LPVOID); |
| 98 | + |
| 99 | +DWORD WINAPI exec_payload(LPVOID lpParameter) |
| 100 | +{ |
| 101 | + __try |
| 102 | + { |
| 103 | + __exec_payload(lpParameter); |
| 104 | + } |
| 105 | + __except(EXCEPTION_EXECUTE_HANDLER) |
| 106 | + { |
| 107 | + } |
| 108 | + |
| 109 | + return 0; |
| 110 | +} |
| 111 | +#elif defined(_WIN32) || defined(__WIN32__) || defined(WIN32) |
| 112 | +DWORD WINAPI exec_payload(LPVOID lpParameter) |
| 113 | +{ |
| 114 | + __try |
| 115 | + { |
| 116 | + __asm |
| 117 | + { |
| 118 | + mov eax, [lpParameter] |
| 119 | + call eax |
| 120 | + } |
| 121 | + } |
| 122 | + __except(EXCEPTION_EXECUTE_HANDLER) |
| 123 | + { |
| 124 | + } |
| 125 | + |
| 126 | + return 0; |
| 127 | +} |
| 128 | +#endif |
0 commit comments