Search code examples
pythonrubyperlwinapireadprocessmemory

How to write a Perl, Python, or Ruby program to change the memory of another process on Windows?


I wonder if Perl, Python, or Ruby can be used to write a program so that it will look for 0x12345678 in the memory of another process (probably the heap, for both data and code data) and then if it is found, change it to 0x00000000? It is something similar to Cheat Engine, which can do something like that on Windows.


Solution

  • I initially thought this was not possible but after seeing Brian's comment, I searched CPAN and lo and behold, there is Win32::Process::Memory:

    C:\> ppm install Win32::Process::Info
    C:\> ppm install Win32::Process::Memory
    

    The module apparently uses the ReadProcessMemory function: Here is one of my attempts:

    #!/usr/bin/perl
    use strict; use warnings;
    
    use Win32;
    use Win32::Process;
    use Win32::Process::Memory;
    
    my $process;
    
    Win32::Process::Create(
        $process,
        'C:/opt/vim/vim72/gvim.exe',
        q{},
        0,
        NORMAL_PRIORITY_CLASS,
        q{.}
    ) or die ErrorReport();
    
    my $mem = Win32::Process::Memory->new({
        pid => $process->GetProcessID(),
        access => 'read/query',
    });
    
    $mem->search_sub( 'VIM', sub {
        print $mem->hexdump($_[0], 0x20), "\n";
    });
    
    sub ErrorReport{
        Win32::FormatMessage( Win32::GetLastError() );
    }
    
    END { $process->Kill(0) if $process }
    

    Output:

    C:\Temp> proc
    0052A580 : 56 49 4D 20 2D 20 56 69 20 49 4D 70 72 6F 76 65 : VIM - Vi IMprove
    0052A590 : 64 20 37 2E 32 20 28 32 30 30 38 20 41 75 67 20 : d 7.2 (2008 Aug
    
    0052A5F0 :       56 49 4D 52 55 4E 54 49 4D 45 3A 20 22 00 :   VIMRUNTIME: ".
    0052A600 : 20 20 66 61 6C 6C 2D 62 61 63 6B 20 66 6F 72 20 :   fall-back for
    0052A610 : 24 56                                           : $V