Jump to content
Tuts 4 You
Sign in to follow this  
CodeExplorer

Execute an exe in same process Java

Recommended Posts

CodeExplorer

Hi guys and gals. I wanna execute an exe inside same process with Java code.
Hopefully someone will give some advices , I am not familiarly not even with executing exes from native code.
So first I have to use LoadLibrary of the exe, then what?
From what I've read on https://stackoverflow.com/questions/44912/java-delegates
I'm fornicated since there are no delegates on Java!
 

  • Haha 1

Share this post


Link to post
CodeExplorer

Solved a part of problems, I've used com.sun.jna.win32-x86 and the fallowing code:
 

   Path path = Paths.get(jTextField1.getText());
   byte[] data = Files.readAllBytes(path);
   int EPrva = GetEntryPointRva(data);
   
   HANDLE handle = ClassReaderWriter.mainprogram.Kernel32.INSTANCE.LoadLibraryA(jTextField1.getText());

    Pointer pointer = new Pointer(handle.getPointer().peer+EPrva);
    Function func = new Function(pointer, 0);
    func.invokeVoid(new Object[]{});

It throws and exception but only because imports are not fixed and dwords are not re-based.
A lot of work to do.
Does anyone know a good import table parser/fixer on C++ so I would convert it to Java?
 

Share this post


Link to post
CodeExplorer

Finded a good solution for preserving default ImageBase:
 

    public static boolean WasPersevedSpace = false;
    
    public static void ExecuteItself()
    {
ClassReaderWriter.mainprogram.Kernel32.MemoryBasicInformation mb = new 
ClassReaderWriter.mainprogram.Kernel32.MemoryBasicInformation();

Pointer ba_pointer = new Pointer(0x0400000);  // standard image base
ClassReaderWriter.mainprogram.Kernel32.INSTANCE.VirtualQuery(
ba_pointer, mb, mb.size());

if (mb.RegionSize.longValue()>10*1024*1024)  // if we have at last 10 MB
{
WasPersevedSpace = true;
return;
}

        ProcessInformation processInformation = new ProcessInformation();
        
        StartupInfoA startupInfo = new StartupInfoA();
        // startupInfo.dwFlags = CREATE_SUSPENDED;
        //startupInfo.wShowWindow = SW_HIDE;

        boolean ok = Kernel32.INSTANCE.CreateProcessA("D:\\HonorableNetPatcher.exe", null
                , null
                , null
                , true
                , CREATE_SUSPENDED
                , null
                , "C:\\Windows\\System32\\"
                , startupInfo
                , processInformation);

        Pointer global_alloc = ClassReaderWriter.mainprogram.Kernel32.INSTANCE.VirtualAllocEx(
        processInformation.hProcess,
        ba_pointer,
        15*1024*1024,  // 15 MB reserved
        ClassReaderWriter.mainprogram.Kernel32.MEM_RESERVE,
        ClassReaderWriter.mainprogram.Kernel32.PAGE_EXECUTE_READWRITE);

ClassReaderWriter.mainprogram.Kernel32.INSTANCE.ResumeThread(processInformation.hThread);

    }

So in this way we reserve 15 MB of space at default ImageBase: 0400000
I've found an article: https://www.joachim-bauch.de/tutorials/loading-a-dll-from-memory/comment-page-1/
But how do we get current process name and current process parameters???
A proper import table fixer in C++ still to be found, the one from article I've posted is silly!
 

Edited by CodeCracker (see edit history)

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
×
×
  • Create New...