0

For dynamic memory allocation in C/C++

  int main(){
  MessageBox(0,"crackname","",0);

  int * ptr = new int[50];
  int * ptr2 = new int[50];
  ptr[0] = 0x75;
  ptr[1] = 0x65;
  (*ptr) +1 = 0x75;

  MessageBox(0,"crackname","",0);
  return 0;
  }

the assembly for this code is the following:

   MOV DWORD PTR SS:[EBP-10],EAX
   MOV ECX,DWORD PTR SS:[EBP-10]
   MOV DWORD PTR SS:[EBP-8],ECX
   MOV EDX,DWORD PTR SS:[EBP-4]
   MOV DWORD PTR DS:[EDX],75
   MOV EAX,DWORD PTR SS:[EBP-4]
   MOV DWORD PTR DS:[EAX+4],65

Each dynamic memory allocation is 4 bytes. But is it possible to make it like this?

   MOV DWORD PTR DS:[EAX+1],65   // +1 not +4 

enter image description here

And if it's possible, what is the theory behind it with dynamic allocated memory?

2 Answers 2

1

int is 4 bytes so when you declare

int *ptr =new int [50]

you have a memory layout like this

TYPE   int      int      int      int      int
SIZE    4         4        4       4        4
CONT 00000000 11111111 44444444 88888888 ffffffff
ADDR|00400000|00400004|00400008|0040000C|00400010|
    p        p        p        p        p        p 
    t        t        t        t        t        t
    r        r        r        r        r        r
    [0]      [1]      [2]      [3]      [4]      [5]

so you should allocate properly sized memory

char is 1 BYTE so char * ptr = new char[50] should work the memory layout will be

TYPE   char     char     char     char     char
SIZE    1         1        1       1        1
CONT    00        11       44      88       ff
ADDR|00400000|00400001|00400002|00400003|00400004|
    p        p        p        p        p        p 
    t        t        t        t        t        t
    r        r        r        r        r        r
    [0]      [1]      [2]      [3]      [4]      [5]

if you want to use int only and not char then you may need to employ other techniques like ptr[0] = 'sseM'

see code below

#include <stdio.h>
#include <string.h>
#define BUFFSIZE 0x50
void main (void) {

  int *blah = new int[BUFFSIZE];
  memset(blah,0,BUFFSIZE);
  blah[0] = 'sseM';
  blah[1] = ' ega';
  blah[2] = 'S si';
  blah[3] = 'erce';
  blah[4] = 'oy\0t';

  printf ("%s\n" , (char *)blah);

  char *ptr = new char[BUFFSIZE];
  memset(ptr,0,BUFFSIZE);
  char *foo = "MySecretMessage";
  for(char i =0; i< (char)strlen(foo) ; i++)
  {
    ptr[i] = *foo+i;
    ptr[i+BUFFSIZE/2] = *(foo+i);
  }
  printf ("contents of ptr is %s\nptr[25] is %s\n ", ptr , &ptr[BUFFSIZE/2]);
}

executed

intptr.exe
Message is Secret
contents of ptr is MNOPQRSTUVWXYZ[
ptr[25] is MySecretMessage
0

you defined ptr as int pointer. int type size is 4 byte in 32-bit intel architecture. thus (*ptr) +1 = 0x75; will add 1 to a 32bit mem/reg. maybe you can test type casting like: (char *)(ptr) +1 = 0x75;; additionally memory alignment is 4 just for performance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.