Skip to content

Instantly share code, notes, and snippets.

@ihnorton
Last active January 31, 2020 21:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ihnorton/9903135 to your computer and use it in GitHub Desktop.
Save ihnorton/9903135 to your computer and use it in GitHub Desktop.
Curiosity and the cat
mem_size = 64
mem_prot = 0x40 # PAGE_EXECUTE_READWRITE
mem_type = 0x00001000 # MEM_COMMIT
ex_mem = ccall(:VirtualAlloc,
Ptr{Uint8},
(Ptr{Void}, Csize_t, Uint64, Uint64),
C_NULL, mem_size, mem_type, mem_prot) # call VirtualAlloc and get executable page
exec_arr = pointer_to_array(ex_mem, (32,2)); # consider as an array so we can write to it
# note that we make it (32,2) so that we won't
# accidentally try to resize it. Can still
# access directly up to index 64.
exec_arr[1] = 0x55 # push ebp
exec_arr[2] = 0x5d # pop ebp
exec_arr[3] = 0xc3 # ret
ccall(pointer(exec_arr), Void, ())
@StefanKarpinski
Copy link

I love that you can do this, even though it's a rather bad idea.

@porterjamesj
Copy link

This is really cool. I love that I can basically write like Python and jump to raw bytes and start executing them in the same language :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment