Like Tree3Likes
  • 1 Post By dmh2000
  • 1 Post By nonoob
  • 1 Post By Salem

I dont understand this code

This is a discussion on I dont understand this code within the C Programming forums, part of the General Programming Boards category; I saw this code in a book, and I dont understand what it tries to do? Code: char code[] = ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    1

    Question I dont understand this code

    I saw this code in a book, and I dont understand what it tries to do?

    Code:
    char code[] = "\x5e\xff.....\x8d";
    
    int main() {
         int *var;
         var = (int *) &var + 2;
        (*var) = (int)code;
    }
    '*var' is a pointer and 'code' is an array. It is ok. but what is that '(int *) &code'. What he or she tries to do. What this line means?
    i) &var is an address, what is (int *)&var ?
    ii) and how can it be possible that (*var) = (int)code; I think it is like code below
    Code:
    int main() {
         int *ptr;
         *ptr = 5;
    }
    I would be grateful if you could help me.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    The code seems to be from a chapter for the introduction of pointers and typecasting.

    1) The var variable points to an integer.
    2) The value of var is its present location + 2
    3) The (int *) means that the value must be typecast to an integer pointer.
    4) *var = (int)code means the value that var points to is the value of address of first byte of code that is code[0]. As var is a pointer to an int, we cannot write *var = code.

    To make things more clear, type in the following printf statements before the closing brace

    printf("The address of var is %u\n", &var);
    printf("The value at var is %u\n", var);
    printf("The value of *var is %u\n", *var);
    printf("The value of code is %u\n", &code[0]);

    Hope that helps.
    Last edited by livin; 12-21-2011 at 05:19 AM.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    looks to me like the first example is way to hack something onto the stack by pointing at the stack location 2 past 'var' and then writing the address of 'code' into it.
    Salem likes this.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Hyberboloid View Post
    I saw this code in a book, and I dont understand what it tries to do?

    Code:
    char code[] = "\x5e\xff.....\x8d";
    
    int main() {
         int *var;
         var = (int *) &var + 2;
        (*var) = (int)code;
    }
    '*var' is a pointer and 'code' is an array. It is ok. but what is that '(int *) &code'. What he or she tries to do. What this line means?
    i) &var is an address, what is (int *)&var ?
    ii) and how can it be possible that (*var) = (int)code; I think it is like code below
    Code:
    int main() {
         int *ptr;
         *ptr = 5;
    }
    I would be grateful if you could help me.
    This is a buffer exploit... it takes a variable's address from the stack, uses that to discover the return address of the function, resets the return address to the address of the char buffer... then when the function returns, it will actually execute the code in the buffer.

    This is the stuff of virus and trojan... and it's not something you want to be messing with. If there is such a thing as illegal code... that's it!

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Given that explanation, I doubt the code was found "in a book". Unless it was in the programmer’s equivalent of Anarchist's Cookbook.
    dennis.cpp likes this.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    Rule 6
    Announcements - C Programming

    We're not interested in helping stack smashers, shell coders and other miscreants.
    rags_to_riches likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dont Understand some code
    By Davidreal in forum C Programming
    Replies: 3
    Last Post: 09-10-2011, 01:38 PM
  2. i dont understand this bug, please help me :(
    By Grey Kliche in forum C++ Programming
    Replies: 12
    Last Post: 08-09-2011, 08:03 AM
  3. I dont understand a piece of this code
    By lilbo4231 in forum C Programming
    Replies: 25
    Last Post: 06-13-2011, 04:15 AM
  4. Replies: 2
    Last Post: 05-03-2011, 12:29 AM
  5. i dont understand bit
    By joker_tony in forum C Programming
    Replies: 2
    Last Post: 03-27-2008, 12:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21