Python Ctype Segmentation Fault

Sonia Sharma

This is the code to run shellcode using ctype. The shellcode runs "whoami" on a 64 bit linux. But this program gives me a "segmentation fault". But I m unable to figure out the error in it. The structure of the code is from: ctypes: Cast string to function?

#!/usr/bin/python

from ctypes import *

# /usr/bin/whoami
shellcode_data = ("\x6a\x3b\x58\x99\x48\xbb\x2f\x62\x69\x6e\x2f\x73\x68\x00\x53"
"\x48\x89\xe7\x68\x2d\x63\x00\x00\x48\x89\xe6\x52\xe8\x10\x00"
"\x00\x00\x2f\x75\x73\x72\x2f\x62\x69\x6e\x2f\x77\x68\x6f\x61"
"\x6d\x69\x00\x56\x57\x48\x89\xe6\x0f\x05");

shellcode = c_char_p(shellcode_data)
function = cast(shellcode, CFUNCTYPE(None))
function()

For 32bits architectures this will be the shell code:

shellcode_data = ("\x6a\x0b\x58\x99\x52\x66\x68\x2d\x63\x89\xe7\x68\x2f\x73\x68"
"\x00\x68\x2f\x62\x69\x6e\x89\xe3\x52\xe8\x10\x00\x00\x00\x2f"
"\x75\x73\x72\x2f\x62\x69\x6e\x2f\x77\x68\x6f\x61\x6d\x69\x00"
"\x57\x53\x89\xe1\xcd\x80");
phihag

The NX Bit prevents random data being executed on modern processors and OSs. To get around it, call mprotect. You should also define your shellcode as a binary instead of a character string, like this:

#!/usr/bin/python
import ctypes
shellcode_data = (b"\x6a\x3b\x58\x99\x48\xbb\x2f\x62\x69\x6e\x2f\x73\x68\x00\x53"
    b"\x48\x89\xe7\x68\x2d\x63\x00\x00\x48\x89\xe6\x52\xe8\x10\x00"
    b"\x00\x00\x2f\x75\x73\x72\x2f\x62\x69\x6e\x2f\x77\x68\x6f\x61"
    b"\x6d\x69\x00\x56\x57\x48\x89\xe6\x0f\x05")

shellcode = ctypes.create_string_buffer(shellcode_data)
function = ctypes.cast(shellcode, ctypes.CFUNCTYPE(None))

addr = ctypes.cast(function, ctypes.c_void_p).value
libc = ctypes.CDLL('libc.so.6')
pagesize = libc.getpagesize()
addr_page = (addr // pagesize) * pagesize
for page_start in range(addr_page, addr + len(shellcode_data), pagesize):
    assert libc.mprotect(page_start, pagesize, 0x7) == 0

function()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python Segmentation Fault?

From Dev

boost python threading segmentation fault

From Dev

Segmentation fault in a python with queue and mutithreading

From Dev

python - matplotlib - segmentation fault with figsize

From Dev

Segmentation fault: 11 - tkinter python

From Dev

python Segmentation fault (core dumped)

From Dev

python - matplotlib - segmentation fault with figsize

From Dev

Segmentation fault

From Dev

A segmentation fault

From Dev

Segmentation fault

From Dev

Segmentation Fault

From Dev

Segmentation fault

From Dev

Python 3 segmentation fault on OS X Mavericks

From Dev

Segmentation fault in Python after Ubuntu update

From Dev

'python' command segmentation fault on raspberry pi

From Dev

Python Pandas - Segmentation Fault after renaming columns?

From Dev

libSBML segmentation fault in python outside __init__

From Dev

Modified Ackermann Function in Python Segmentation fault

From Dev

Segmentation fault instead of traceback python nginx wsgi

From Dev

shout-python segmentation fault how can I fix this?

From Dev

Segmentation fault while calling cpp function from Python

From Dev

Python crashing when running two commands (Segmentation Fault: 11)

From Dev

fatal python error (pygame parachute) Segmentation Fault when using Tkinter

From Dev

Segmentation fault when calling C function with Python C API twice

From Dev

c++ boost python list extract causing Segmentation fault

From Dev

Reading a huge file in Python: Why am I getting a Segmentation Fault?

From Dev

Segmentation fault when importing a C++ shared object in Python

From Dev

python-openid cause segmentation fault on mod_wsgi

From Dev

segmentation fault while running cython on python3-- gdb output

Related Related

HotTag

Archive