Skip to content

Instantly share code, notes, and snippets.

@rekkusu
Last active August 28, 2018 18:05
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 rekkusu/a4203888d96b80937001 to your computer and use it in GitHub Desktop.
Save rekkusu/a4203888d96b80937001 to your computer and use it in GitHub Desktop.
Run shellcode from Ruby
require 'mkmf'
create_makefile('shellcode')
require 'shellcode'
"\x48\x31\xd2\x48\xbb\x2f\x2f\x62\x69\x6e\x2f\x73\x68\x48\xc1\xeb\x08\x53\x48\x89\xe7\x50\x57\x48\x89\xe6\xb0\x3b\x0f\x05".exec
# http://shell-storm.org/shellcode/files/shellcode-603.php
#include "ruby/ruby.h"
#ifdef unix
#include <sys/mman.h>
#endif
VALUE exec_s_m(VALUE self) ;
void Init_shellcode(void) {
VALUE cString = rb_define_class("String", rb_cObject);
rb_define_method(cString, "exec", exec_s_m, 0);
}
VALUE exec_s_m(VALUE self) {
#ifdef unix
char *sc = RSTRING_PTR(self);
intptr_t p = sc;
p = p ^ (p & 0xfff);
mprotect(p, 0x1000, PROT_READ | PROT_WRITE | PROT_EXEC);
(*(void (*)()) sc)();
return Qtrue;
#else
return Qfalse;
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment