Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when is reading and writing to memory legal?

Please correct me if I am wrong. I am asking this question to clarify some ideas that I have.

Today in school I learned that when a process (program) executes, then the operating systems gives it a space in memory. So take for instance this two programs:

Program1:

    static void Main(string[] args)
    {

        unsafe // in debug options on the properties enable unsafe code
        {

            int a = 2;

            int* pointer_1 = &a; // create pointer1 to point to the address of variable a

            // breakpoint in here !!!!!!!!!!!!!!!!!

            // in the debug I should be able to see the address of pointer1. Copy it and 
            // type it in the console

            string hexValue = Console.ReadLine();

            // convert the hex value to base 10
            int decValue = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);

            // create a new pointer that point to the address that I typed in the console
            IntPtr pointer_2 = new IntPtr(decValue);

            Console.WriteLine("The address of a: {0} Value {1}", ((int)&a), a);

            try
            {
                Console.WriteLine("The address of {0} points to value {1}", (int)&pointer_1, (int)pointer_2);

                // different approach of acomplishing the same
                Console.WriteLine(Marshal.ReadInt32(pointer_2));
            }
            catch
            {
                Console.WriteLine(@"you are supposed to be debuging this program.");                    
            }

        }

Program 2

    static void Main(string[] args)
    {            
        unsafe
        {
            IntPtr pointer_0 = new IntPtr(95151860); // see address of variable from program1
            int* pointer_1 = (int*)pointer_0;
            // try to access program's 1 object
            Console.WriteLine("addrees of {0} points to value {1} ", (int)&pointer_1, *pointer_1); 
        }
    }

So I understand that in program 2 I will get an error. I will be accessing restricted memory. So far what I have learned makes sense.

Ok know here is where things do not make sense.

There is a very nice program called AutoIt used to automate tasks. For example it can send mouse clicks, move the mouse, send key strokes etc.

Anyways autoit comes with a program called AutoIt Window Info where that program enables you to get the handles (pointers) of controls on windows. For example I could see the handle of the window's control by draging the finder tool to the control that I wish on getting information:

enter image description here

int this picture I am dragging the finder tool to the calculator's input control. I could also drag it to button 7 for instance.

So if you see in the picture I now have the address of that control. I could then be able to access it from my program!!


Yet another example of how you can access memory that does not belong to my program

Step 1) Get the pointer of any window with autoit window info

Step 2) In my computer that pointer is:

enter image description here

That is the window of google chrome the place where I am typing this question.

This class will send a window to the back:

    public static class SendWndToBack
    {
        [DllImport("user32.dll")]
        static extern bool SetWindowPos(
            IntPtr hWnd,
            IntPtr hWndInsertAfter,
            int X,
            int Y,
            int cx,
            int cy,
            uint uFlags);

        const UInt32 SWP_NOSIZE = 0x0001;
        const UInt32 SWP_NOMOVE = 0x0002;
        const UInt32 SWP_NOACTIVATE = 0x0010;

        static readonly IntPtr HWND_BOTTOM = new IntPtr(1);

        static readonly IntPtr k = new IntPtr(12);

        public static void WindowHandle(IntPtr windowHandle)
        {
            SetWindowPos(windowHandle, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
        }            
    }

and then if I call the method with the poniter that I just got with the help of autoit and call it as:

            SendWndToBack.WindowHandle(new IntPtr(0x00000000000510B2));

then I will send that window to the back


I post some examples in order to ilustrate my point. But my question is when are you allowed to access other parts of the memory? If I make my variable public other programs will be able to access it? Why can I access some controls of windows from my own program?

like image 564
Tono Nam Avatar asked Feb 07 '12 03:02

Tono Nam


People also ask

How many times do you have to read something before memorizing it?

According to this technique, “you've got to actively recall the memory 30 times,” Cooke says. So when you meet someone new, you might want to repeat her name 30 times. Create a mnemonic.

What is fallibility of memory?

This fallibility of memory includes not only the omission of details from the original experience, but extends to errors of commission including the creation of memory illusions.

How do you remember a case name?

Make similar cards for all the cases you want to learn. Take the cards with you wherever you go, and test yourself often. You can look at the name of the case and try to recall (without looking!) the key facts and points of law. Or you can look at the facts and points of law, and try to remember the name of the case.


1 Answers

You are confusing "Handles" and "Pointers". Just because it looks like an address, it may not be. Windows uses a lot of handles and the OS may let you do things to handles even though you didn't create them. You can represent a Handle as an IntPtr, but it you were to actually treat it directly as a pointer, you would (probably) crash.

like image 69
John3136 Avatar answered Sep 20 '22 00:09

John3136