frida icon indicating copy to clipboard operation
frida copied to clipboard

How to create a NativePointer which points to an int value(not memory address)?

Open ghd8239820 opened this issue 5 years ago • 3 comments

ghd8239820 avatar Jul 29 '21 04:07 ghd8239820

Allocate memory for int and write to it var temp = Memory.alloc(Process.pointerSize); temp.writeInt(1337);

using NativePointer for similar task var temp = new NativePointer('1337'); // you can use ptr('1337') as a shorthand

Make sure to always go through the docs before opening issues

0xXA avatar Aug 03 '21 04:08 0xXA

@0xXA are you suggesting that ptr('1337') allocates an integer with the value '1337' and then returns a native pointer pointing to that value? I've 'read the docs' and they seem to suggest that code will simply create a pointer pointing to the address 0x539 (1337)...

@oleavr I would really like some clarification on this as it's boggling my mind a bit. If I have a libc function that takes a 'pointer to an int' as its argument (eg. wait()) - what is the best practice for creating an empty int allocation and passing a pointer to it as an argument to my native function?

Cheers

felipewarrener avatar Sep 28 '22 10:09 felipewarrener

ptr('1337') is just a number, it doesn't allocate. It's up to the native function on how to interpret it.

what is the best practice for creating an empty int allocation and passing a pointer to it as an argument to my native function?

const intPtr = Memory.alloc(Process.pointerSize);
wait(intPtr);
console.log(intPtr.readInt());

Also careful with scoping: as soon as intPtr is garbage collected in js, it will also free that memory region. You can store it in some global array for example if that happens.

j4k0xb avatar Sep 04 '24 15:09 j4k0xb