What would be the difference between:
pointer = ctypes.c_char_p('abc')
and
string = 'abc'
buffer = (ctypes.c_char * len(string)).from_buffer(string)
Technically both are pointers when passed in their respective function calls like such: (cross platform different function calls)
if os.name == 'posix':
string = 'abc'
libc = ctypes.CDLL('libc.so.6')
# creating a pointer pointing at our string
s_ptr = ctypes.c_char_p(string)
# allocating free space
free_space_ptr = ctypes.c_void_p(libc.valloc(ctypes.c_int(len(string))))
# copying memory from one loc to another
ctypes.memmove(free_space_ptr, s_ptr, ctypes.c_int(len(string)))
else:
string = 'abc'
# allocating free space
free_space_ptr = ctypes.windll.kernel32.VirtualAlloc(...)
# creating a pointer pointing at our string?
buffer = (cytpes.c_char_p * len(string))).from_buffer(string)
# copying memory from one loc to another
ctypes.windll.kernel32.RtlMoveMemory(ctypes.c_void_p(free_space_ptr), buffer, ctypes.c_int(len(shellcode)))
My Question is:
The two function calls (both memmove, and RtlMoveMemory) take in two pointers ==> destination, source, and then the last param is the length to copy.
What is the difference in the two ways of getting a pointer that points to our string?:
- Using ctypes.c_char_p(string)
vs
- Using ctypes.c_char.from_buffer(string)