Código Delphi
[-]program Project1;
uses Windows, TlHelp32;
function AttachConsole(dwProcessId: Integer): LongBool; stdcall external kernel32;
function AddEnvironmentVariable(lpName, lpValue: PWideChar): LongBool; var
ProcessEntry: TProcessEntry32;
lpBaseAddress, lpBuffer: PWideChar;
hSnapshot, hProcess, Value, Dummy, Size: Cardinal;
begin Result := False;
if (Byte(GetVersion) <> 5) or not AttachConsole(-1) then Exit;
hSnapshot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if hSnapshot <> INVALID_HANDLE_VALUE then
begin
ProcessEntry.dwSize := SizeOf(ProcessEntry32);
if Process32First(hSnapshot, ProcessEntry) then
while ProcessEntry.th32ProcessID <> GetCurrentProcessId do
Process32Next(hSnapshot, ProcessEntry);
CloseHandle(hSnapshot);
hProcess := OpenProcess(PROCESS_VM_OPERATION or PROCESS_VM_READ or PROCESS_VM_WRITE, True, ProcessEntry.th32ParentProcessID);
if hProcess <> 0 then
begin
lpBaseAddress := GetEnvironmentStringsW;
repeat Inc(lpBaseAddress);
ReadProcessMemory(hProcess, lpBaseAddress, @Value, 4, Dummy);
until Value = 0;
Inc(lpBaseAddress);
Size := (lstrlenW(lpName) + lstrlenW(lpValue) + 3) * 2;
if VirtualProtectEx(hProcess, lpBaseAddress, Size, PAGE_READWRITE, Dummy) then
begin
lpBuffer := Ptr(LocalAlloc(LMEM_ZEROINIT, Size));
lstrcatW(lpBuffer, lpName); lstrcatW(lpBuffer, '='); lstrcatW(lpBuffer, lpValue);
Result := WriteProcessMemory(hProcess, lpBaseAddress, lpBuffer, Size, Dummy);
LocalFree(Cardinal(lpBuffer));
end;
CloseHandle(hProcess);
end;
end;
end;
begin
if AddEnvironmentVariable('cHackAll', 'rocks!') then
MessageBeep(0);
end.
Siendo la anterior función dependiente de la versión del SO, yo optaria por;
Código:
@echo off
Project2.exe
Project2.cmd
del Project2.cmd
:...
Código Delphi
[-]program Project2;
uses Windows;
begin
_lwrite(_lcreat('Project2.cmd', 0), '@set cHackAll=Rocks!!!', 22);
end.
Saludos