标题: [系统相关] [讨论]一个进程如何修改其他进程的环境变量 [打印本页]
作者: plp626 时间: 2012-9-15 13:56 标题: [讨论]一个进程如何修改其他进程的环境变量
如题: 最好能有C的测试代码;
这个话题和第三方开发密切相关,大家务必重视。。
作者: wc726842270 时间: 2012-9-15 14:03
有点黑克的意思了,有点不太好吧
作者: plp626 时间: 2012-9-15 14:05
本帖最后由 plp626 于 2012-9-15 14:08 编辑
论坛的第三方区提供了一个工具:
conset.exe, 不知其中实现方法- ->set var
- 环境变量 var 没有定义
-
- ->dir/b |conset var=
-
- ->set var
- var=Debug
- x.dsp
- x.dsw
- x.ncb
- x.opt
- x.plg
复制代码
还有系统自带的外部命令doskey.exe
不知如何修改父进程空间数据的?- ->ls
- 'ls' 不是内部或外部命令,也不是可运行的程序
- 或批处理文件。
-
- ->doskey ls=dir/w
-
- ->ls
- 驱动器 D 中的卷是 SOFT
- 卷的序列号是 B89F-E38E
-
- D:\Documents_and_Settings\plp2\desktop\x 的目录
-
- [.] [..] [Debug] x.dsp x.dsw x.ncb x.opt x.plg
- 5 个文件 88,389 字节
- 3 个目录 5,512,269,824 可用字节
复制代码
作者: IUnknown 时间: 2012-9-15 17:24
远程注入法,简单写了一个:- #include <windows.h>
- #include <stdio.h>
- #include <tchar.h>
- #include <assert.h>
- #include <tlhelp32.h>
-
- DWORD GetParentProcessId(DWORD pid)
- {
- DWORD ppid = (DWORD)(-1);
- HANDLE hProcessSnap;
- PROCESSENTRY32 pe32;
-
- hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
- assert(hProcessSnap != INVALID_HANDLE_VALUE);
-
- pe32.dwSize = sizeof( PROCESSENTRY32 );
- BOOL bResult = Process32First( hProcessSnap, &pe32 );
- assert(bResult != FALSE);
-
- do
- {
- if (pid == pe32.th32ProcessID)
- {
- ppid = pe32.th32ParentProcessID;
- break;
- }
- } while( Process32Next( hProcessSnap, &pe32 ) );
-
- CloseHandle( hProcessSnap );
- return( ppid );
- }
-
- DWORD ppid(VOID)
- {
- return GetParentProcessId( GetCurrentProcessId() );
- }
-
-
- typedef struct _RemoteParam {
- DWORD funcptr;
- BYTE Param1[64];
- BYTE Param2[64];
- } RemoteParam, *PRemoteParam;
-
- typedef int (WINAPI *PFN_MessageBox)(HWND, LPCTSTR, LPCTSTR, DWORD);
- typedef BOOL (WINAPI *PFN_SetEnvironmentVariable)(LPCTSTR, LPCTSTR);
-
-
- DWORD WINAPI threadProc(LPVOID lpParam)
- {
- RemoteParam *pRP = (RemoteParam *)lpParam;
-
- PFN_SetEnvironmentVariable pfnSetEnvironmentVariable = (PFN_SetEnvironmentVariable)pRP[0].funcptr;
- pfnSetEnvironmentVariable(pRP[0].Param1, pRP[0].Param2);
-
- PFN_MessageBox pfnMessageBox = (PFN_MessageBox)pRP[1].funcptr;
- pfnMessageBox(NULL, pRP[1].Param1, pRP[1].Param2, 0);
-
- return 0;
- }
-
-
- int main(int argc, char *argv[])
- {
- DWORD dwProcessId = ppid();
- assert(dwProcessId != (DWORD)(-1));
-
- HANDLE hTargetProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessId);
- assert(hTargetProcess != NULL);
-
- DWORD dwMemSize = 4096;
- LPVOID pRemoteThread = VirtualAllocEx(hTargetProcess, 0, dwMemSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE) ;
- assert(pRemoteThread != NULL);
- BOOL bResult = WriteProcessMemory(hTargetProcess, pRemoteThread, threadProc, dwMemSize, NULL) ;
- assert(bResult != FALSE);
-
- RemoteParam RemoteParams[2] = {{0}};
- HMODULE hUser32 = LoadLibrary("User32.dll");
- HMODULE hKernel32 = LoadLibrary("Kernel32.dll");
-
- RemoteParams[0].funcptr = (DWORD)GetProcAddress(hKernel32, "SetEnvironmentVariableA");
- strcpy(RemoteParams[0].Param1, "__var");
- strcpy(RemoteParams[0].Param2, "hello");
- RemoteParams[1].funcptr = (DWORD)GetProcAddress(hUser32, "MessageBoxA");
- strcpy(RemoteParams[1].Param1, "www.bathome.net");
- strcpy(RemoteParams[1].Param2, "hello");
-
- dwMemSize = sizeof(RemoteParams);
- LPVOID pRemoteParam = VirtualAllocEx(hTargetProcess, 0, dwMemSize, MEM_COMMIT, PAGE_READWRITE);
- assert(pRemoteParam != NULL);
- bResult = WriteProcessMemory(hTargetProcess, pRemoteParam, RemoteParams, dwMemSize, NULL) ;
- assert(bResult != FALSE);
-
- HANDLE hRemoteThread = CreateRemoteThread(hTargetProcess, NULL, 0, pRemoteThread, pRemoteParam, 0, NULL);
- assert(hRemoteThread != NULL);
-
- CloseHandle(hRemoteThread);
- CloseHandle(hTargetProcess);
- return 0;
- }
复制代码
E:\Projects\Inject>set
ALLUSERSPROFILE=D:\Documents and Settings\All Users
APPDATA=D:\Documents and Settings\root\Application Data
CLIENTNAME=Console
CommonProgramFiles=D:\Program Files\Common Files
COMPUTERNAME=POWERPC
ComSpec=D:\WINDOWS\system32\cmd.exe
FP_NO_HOST_CHECK=NO
HOMEDRIVE=D:
HOMEPATH=\Documents and Settings\root
LOGONSERVER=\\POWERPC
NUMBER_OF_PROCESSORS=4
OS=Windows_NT
Path=D:\WINDOWS\system32;D:\WINDOWS;D:\WINDOWS\System32\Wbem;E:\MinGW\bin
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
PROCESSOR_ARCHITECTURE=x86
PROCESSOR_IDENTIFIER=x86 Family 6 Model 37 Stepping 5, GenuineIntel
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=2505
ProgramFiles=D:\Program Files
PROMPT=$P$G
SESSIONNAME=Console
SystemDrive=D:
SystemRoot=D:\WINDOWS
TEMP=D:\DOCUME~1\root\LOCALS~1\Temp
TMP=D:\DOCUME~1\root\LOCALS~1\Temp
USERDOMAIN=POWERPC
USERNAME=root
USERPROFILE=D:\Documents and Settings\root
windir=D:\WINDOWS
E:\Projects\Inject>gcc -o conset.exe conset.c
E:\Projects\Inject>conset
E:\Projects\Inject>set
ALLUSERSPROFILE=D:\Documents and Settings\All Users
APPDATA=D:\Documents and Settings\root\Application Data
CLIENTNAME=Console
CommonProgramFiles=D:\Program Files\Common Files
COMPUTERNAME=POWERPC
ComSpec=D:\WINDOWS\system32\cmd.exe
FP_NO_HOST_CHECK=NO
HOMEDRIVE=D:
HOMEPATH=\Documents and Settings\root
LOGONSERVER=\\POWERPC
NUMBER_OF_PROCESSORS=4
OS=Windows_NT
Path=D:\WINDOWS\system32;D:\WINDOWS;D:\WINDOWS\System32\Wbem;E:\MinGW\bin
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
PROCESSOR_ARCHITECTURE=x86
PROCESSOR_IDENTIFIER=x86 Family 6 Model 37 Stepping 5, GenuineIntel
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=2505
ProgramFiles=D:\Program Files
PROMPT=$P$G
SESSIONNAME=Console
SystemDrive=D:
SystemRoot=D:\WINDOWS
TEMP=D:\DOCUME~1\root\LOCALS~1\Temp
TMP=D:\DOCUME~1\root\LOCALS~1\Temp
USERDOMAIN=POWERPC
USERNAME=root
USERPROFILE=D:\Documents and Settings\root
windir=D:\WINDOWS
__var=hello
E:\Projects\Inject>
作者: broly 时间: 2012-9-15 21:12
conset用的远程注入的方法是对的,如图(不知为何论坛上传不了图片,用了外链)
我测试的代码是复制代码
图中就是conset赋值的关键地方,先CreateRemoteThread创建远程线程,再用SetEnvironmentVariableA进行变量赋值。
大家可以看右下角的堆栈区
作者: Demon 时间: 2012-9-17 00:16
远程注入法,简单写了一个:
IUnknown 发表于 2012-9-15 17:24
这个程序用Visual C++是无法通过编译的。
作者: Seder 时间: 2012-9-17 11:58
回复 6# Demon
用了gcc编译器。蓝色字体那里
作者: Demon 时间: 2012-9-17 12:08
回复 Demon
用了gcc编译器。蓝色字体那里
Seder 发表于 2012-9-17 11:58
我知道他用的是GCC,我说的是用VC无法编译。
作者: CrLf 时间: 2015-5-18 17:07
回复 4# IUnknown
反馈个 bug,这个长度取的是结构体数组的大小:- dwMemSize = sizeof(RemoteParams);
复制代码
实际上应该取的是数组中指向的结构体的总长度:- dwMemSize = sizeof(RemoteParams)*sizeof(RemoteParam)/sizeof(int);
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |