title: Executing Shellcode in Python
body: <p>The following code works in C, but is it possible to do something similar in Python? It can be 2.7.x or 3.x.</p>
  <pre><code>char bytes[] = &quot;\x90\x90\x90\x90\x90\x90\x90\x90\xeb\x1a\x5e\x31\xc0&quot;
                 &quot;\x88\x46\x07\x8d\x1e\x89\x5e\x08\x89\x46\x0c\xb0\x0b&quot;
                 &quot;\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\xe8\xe1\xff&quot;
                 &quot;\xff\xff\x2f\x62\x69\x6e\x2f\x73\x68\x20&quot;;
  
  int main() {
     ((int (*)())bytes)();
  }
  </code></pre>
  <p>I tried the following:</p>
  <pre><code>#!/usr/bin/python
  import ctypes
  from subprocess import call
  
  lib = ctypes.cdll.LoadLibrary(None)
  
  shellcode = (b&quot;\x90\x90\x90\x90\x90\x90\x90\x90\xeb\x1a\x5e\x31\xc0&quot;
                &quot;\x88\x46\x07\x8d\x1e\x89\x5e\x08\x89\x46\x0c\xb0\x0b&quot;
                &quot;\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\xe8\xe1\xff&quot;
                &quot;\xff\xff\x2f\x62\x69\x6e\x2f\x73\x68\x20&quot;)
  
  code = ctypes.create_string_buffer(shellcode)
  addr = id(shellcode)
  
  # Test Shellcode
  functype = ctypes.CFUNCTYPE(ctypes.c_int)
  func = functype(addr)
  func()
  </code></pre>
  <p>I keep getting <code>Segmentation fault (core dumped)</code>.</p>