2009-03-29 07:30:54 +00:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
# This file is part of Metasm, the Ruby assembly manipulation suite
|
2010-09-09 18:19:35 +00:00
|
|
|
# Copyright (C) 2006-2009 Yoann GUILLOT
|
2009-03-29 07:30:54 +00:00
|
|
|
#
|
|
|
|
# Licence is LGPL, see LICENCE in the top-level directory
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# usage: test.rb < source.asm
|
|
|
|
|
|
|
|
require 'metasm'
|
|
|
|
|
|
|
|
|
|
|
|
dump = ARGV.delete '--dump'
|
|
|
|
|
|
|
|
source = ARGF.read
|
|
|
|
|
|
|
|
cpu = Metasm::Ia32.new
|
|
|
|
shellcode = Metasm::Shellcode.assemble(cpu, source).encode_string
|
|
|
|
shellstring = shellcode.unpack('C*').map { |b| '\\x%02x' % b }.join
|
|
|
|
|
|
|
|
if dump
|
2013-08-30 21:28:33 +00:00
|
|
|
puts shellstring
|
|
|
|
exit
|
2009-03-29 07:30:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
File.open('test-testraw.c', 'w') { |fd|
|
2013-08-30 21:28:33 +00:00
|
|
|
fd.puts <<EOS
|
2009-03-29 07:30:54 +00:00
|
|
|
unsigned char sc[] = "#{shellstring}";
|
|
|
|
int main(void)
|
|
|
|
{
|
2013-08-30 21:28:33 +00:00
|
|
|
((void (*)())sc)();
|
|
|
|
return 42;
|
2009-03-29 07:30:54 +00:00
|
|
|
}
|
|
|
|
EOS
|
|
|
|
}
|
|
|
|
|
|
|
|
system 'gcc -W -Wall -o test-testraw test-testraw.c'
|
|
|
|
system 'chpax -psm test-testraw'
|
|
|
|
|
|
|
|
puts "running"
|
|
|
|
system './test-testraw'
|
|
|
|
puts "done"
|
|
|
|
#File.unlink 'test-testraw'
|
|
|
|
File.unlink 'test-testraw.c'
|