Inline ASM jmp to new memory
So I would like to load a set of assembly instructions from a file, and execute those instructions.
OR
I would like to load already compiled machine code again from a file (non-exe)
My Thoughts:
So far I've learned enough inline asm to work with c/c++ variables easily.
asm volatile (
"mov %1, %%ecx;" // yes unnecessary, I know
"add %2, %%ecx;" // I know they're already loaded in a register
"mov %%ecx ,%0 ;"// just demonstrating what I've learned
:"=r"(address)
:"r"(address),"r"(offset)
:"%ecx"
);
I've begun to learn about op-codes, and I've gotten some x86 manuals. I understand (somewhat) how the hardware works at a basic level.
I know that I can load the file into memory with c++ with fstream, and I'm wondering if there's a way to execute memory from that space, or if it's in a non-exe开发者_高级运维cutable portion of memory or something..
Reason:
Currently there are several reasons I'd like to do this. I have a desire to create a basic encyption for my program, for a simple key to run the program. And while I can easily encrypt and decrypt the actual code, I want the program to be unencrypted every run and never stored on the harddrive. I am aware of several problems, however I'm very interested in doing so.
Final Questions:
Can I execute machine code from memory space in a program from c++ in asm?
Is asm necessary?
Are there other ways to accomplish this task more efficiently, if so, what are those methods?
I'm also reading up on some DLL injection for a mouse sharing program that I'm making (two computers next to each other, both have monitors, however you only have one mouse/keyboard. And I'm wondering where I can find some good resources on the topic? Google has been helpful, however I'm interested in perhaps some IRC channels or something similar. Anyways, thanks for reading!
This sounds like you kind of want some JIT compiling of some x86 assembly in a separate file?
I might have misunderstood, when you say 'memory space', your encapsulating a very broad term. I assume you don't mean that you want to compile it with a specific set of assembly but be able to interchange the assembly instructions at runtime?
Have a read over the following Dynamically generating and executing x86 code
You'll see
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
int main(int argc, char *argv[]) {
uint8_t *buf = mmap(NULL, 1000, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
buf[0] = 0xb8;
uint32_t u32 = 42;
memcpy(buf + 1, &u32, 4);
buf[5] = 0xc3;
if (mprotect(buf, 1000, PROT_EXEC | PROT_READ) < 0) {
fprintf(stderr, "mprotect failed: %s\n", strerror(errno));
return 1;
}
int (*ptr)(void) = (int (*)(void)) buf;
printf("return is %d\n", ptr());
return 0; }
Here you are allocating memory for a buffer. Putting the instructions into the buffer, then creating a function pointer from the allocated memory. Notice the memory is being protected also.
Read over the article for a better understanding, it also gives you some Windows equivalents.
If you want to load code as data and then execute it then you'll probably need to mark the memory containing the code as executable (this is due to a security feature in most modern operating systems to prevent various exploits). Under Linux, Mac OS X, et al you would use mprotect, but it sounds like you may be using Windows (?) so in that case you'll need to find the equivalent API.
And no you don't need to use asm for this - you can just use a C or C++ function pointer.
精彩评论