PDA

Click to See Complete Forum and Search --> : C/C++ question


el-half
February 27th, 2004, 09:49 AM
Is there a way in C/C++ to put code explicitly at a specified memory adress (code to be executed)? Does that have to be shellcode? (Stack or heap doesn't matter)
(In windows)

pooh sun tzu
February 27th, 2004, 09:54 AM
A few tutorials for specific memory handling in C and C++, both Windows and nix based.

http://www.cpp-home.com/tutorial.php?16_4

http://atrevida.comprenica.com/atrtut04.html

http://www.juicystudio.com/tutorial/cpp/index.asp


edit: fixed links

el-half
February 27th, 2004, 10:31 AM
Thanks alot!

I tried using it in its most simple way:

#include <dos.h>

void pokeb (unsigned int segment, unsigned int offset, char value);

int main() {

pokeb (0x760F, 0x00AE, 125);

return 0;

}

(From the 2nd link)

But when linking this error occurs (Borland C++ 5.5):

Error: Unresolved external 'pokeb(unsigned int, unsigned int, char)' referenced
from C:\WINDOWS\DESKTOP\UNTITLED.OBJ

Do I have to link it with another file or something?

pooh sun tzu
February 27th, 2004, 10:49 AM
Try:

#include <iostream.h>
int main()
{
int x; //A normal integer
int *pointer; //A pointer to an integer
pointer=&x; //Read it, "pointer equals the address of x"
cin>>x; //Reads in x
cout<<*pointer; //Note the use of the * to output the actual number stored in x
return 0;
}

That's as simple as it can get, honestly. If the problem still occurs, not sure what to say, as I am a visual .net user, being unfamiliar with borland.

el-half
February 27th, 2004, 11:52 AM
Yes, but here you rely on the adress of x, I want to write explicitly to an adress I chose.
But thx anyway

pooh sun tzu
February 27th, 2004, 12:36 PM
So have x == your memory address and define a variable each time?

el-half
February 27th, 2004, 01:56 PM
A yes /me slaps self :D lol, thanks pooh sun tzu

EDIT:

I created 2 proggies:

Program 1:

#include <iostream.h>

int i = 2;

int main()
{
cout << &i;
int stuff;
cin >> stuff;

return 0;
}


Program 2:

#include <iostream.h>

int main() {

int *pointer;
pointer = (int*)0x0041C178;
cout << "Value1: " << *pointer << endl; /*<- this is supposed to print the value of i from program 1*/
*pointer = 3;
cout << "Value2: " << *pointer;
return 0;
}


Now, what I want to do is modify the value of i in program 2.
So I run program 1 which shows the adress of where i is stored.
The variable i stays in memory right? I use cin to pause the program (yes I know, stupid way but I don't know any other (yet)).
Thus in program 2 I create a pointer to that adress. But whenthe it displays the value of that location it is not the value of i from program 1 which is still running. In this case it is 0 (with me). If I declare the integer in program 1 inside main() it's some number like 570577 (something like that).
Anyone know what is wrong with this?

EDIT2:
I know this is supposed to be impossible as the kernel (should) manages memory and thus that memory adress would normally be protected as it is already in use.
But these programs run without any errors and it's clearly not some form of shared memory (Win98)

pZargs
February 27th, 2004, 08:36 PM
just out of curiousity why do you want to write to a specific address?
and why not use assembly? ;)
and I only mean that it's faster and more direct.

BlackBloodRaven
February 27th, 2004, 11:43 PM
i'm not sure why u would need to do that but it has been my exp. that c/c++ has no way to write data to a exat mem adderss because the os loads the program and then a var are offset to the start of the program

but how about this i'm not at home right now i will look though my commads book for c++ and see if there is that i didn't think of.

u could alway write it in assembly most c++ compiler will let u write a function in ASM(assmbly)

pZargs
February 28th, 2004, 01:19 AM
Well Im pretty noobyish at both assembly and c++ but one of the reasons I could think of was that person wanted to execute code affecting the memory would be @ h04 or h20 and that would be for the purposes of a .com virus replicating it self. But that would be indiginious to intel family 86 cpu's only , dont know about amd.

and thats not to say what hes thinking of, becuase im sure that there are other good reasons for accessing memory directly with a program in c++,like cleaning it same thing that reg edit does... :confused:

pooh sun tzu
February 28th, 2004, 01:30 AM
and why not use assembly? and I only mean that it's faster and more direct.

Because he is trying to learn C/C++, not assembly.

i'm not sure why u would need to do that but it has been my exp. that c/c++ has no way to write data to a exat mem adderss because the os loads the program and then a var are offset to the start of the program

We already did it. See the above posts.

becuase im sure that there are other good reasons for accessing memory directly with a program in c++,like cleaning it same thing that reg edit does..

Like learning? Like understanding C/C++ on a deeper level? Like curiosity? Like it being part of the basic tutorial of C/C++ usage?

:)

rcgreen
February 28th, 2004, 02:52 AM
Depends on what you mean by an exact memory address.
AFAIK, the OS only allocates virtual addresses to user progs.
It manages the physical locations itself. Basic multitasking
OS design. You gotta go back to DOS to be able to manage
physical mem addresses yourself.

Or learn to write device drivers. They run at a higher priviledge
level i think.
:cool:

el-half
February 28th, 2004, 09:11 AM
Yeah, VxD's, they run in ring0 (kernel priveleges) I think. I didn't know an OS allocated virtual adresses, I thought that was only the case with shared memory.

yes, pooh sun tzu, this is purely out of curiosity. I just want to manipulate another program directly.

EDIT: I found out that this is Virtual Memory at its best. It is impossible to manipulate memory that is allocated to another process from within another normal userprocess.
This is also not possible in assembly!
The only way seems to be to code a ring0 process.

cgkanchi
February 28th, 2004, 02:32 PM
It is impossible to manipulate memory that is allocated to another process from within another normal userprocess.
This is because each program has a stack, where it stores all its data. The stack of another program can only be written to by a kernel level process (under Windows, it'd probably have to run as System). Otherwise, arbitary code/data could be written to any program's stack, making it a security nightmare. This is precisely what buffer overflows do. They allow writing to the stack of another program (usually the kernel, for the exploits with shellcode).

Cheers,
cgkanchi

el-half
February 28th, 2004, 02:49 PM
Otherwise, arbitary code/data could be written to any program's stack, making it a security nightmare

That's what I was trying to do, but the easy way. I wanted to overwrite the returnadress of a program by explictly writing stuff to that place. :D

I know each program has an individual stack but I just thought you could simply write in another process's stack if you explicitly mentioned an adress.
Would have been too easy, screw virtual memory managment :D

pZargs
February 29th, 2004, 06:00 AM
very well said, I didnt mean anything by my comments just didnt understand what your main mission was,thought that assembly was the way you may have wanted togo.
cheers... :D