2005-12-17 06:46:23 +00:00
|
|
|
#!/usr/bin/env ruby
|
2005-12-08 15:37:10 +00:00
|
|
|
|
|
|
|
require 'tempfile'
|
|
|
|
require 'rex/file'
|
|
|
|
require 'rex/text'
|
|
|
|
|
|
|
|
module Rex
|
|
|
|
module Assembly
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# This class uses nasm to assemble and disassemble stuff.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class Nasm
|
|
|
|
|
|
|
|
#
|
|
|
|
# Ensures that the nasm environment is sane.
|
|
|
|
#
|
|
|
|
def self.check
|
|
|
|
if (Rex::FileUtils.find_full_path('nasm') == nil)
|
|
|
|
raise RuntimeError, "No nasm installation was found."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Assembles the supplied assembly and returns the raw opcodes.
|
|
|
|
#
|
|
|
|
def self.assemble(assembly)
|
|
|
|
check
|
|
|
|
|
|
|
|
# Open the temporary file
|
|
|
|
tmp = Tempfile.new('nasm').path
|
|
|
|
file = File.new(tmp, "w")
|
|
|
|
|
|
|
|
# Write the assembly data to a file
|
|
|
|
begin
|
|
|
|
file.write("BITS 32\n" + assembly)
|
|
|
|
file.close
|
|
|
|
file = nil
|
|
|
|
ensure
|
|
|
|
file.close if (file)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Run nasm
|
|
|
|
if (system("nasm -f bin -o #{tmp}.out #{tmp}") == false)
|
|
|
|
raise RuntimeError, "Assembler did not complete successfully: #{$?.exitstatus}"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Read the assembled text
|
|
|
|
rv = ::IO.readlines(tmp + ".out").join('')
|
|
|
|
|
|
|
|
# Remove temporary files
|
|
|
|
File.unlink(tmp)
|
|
|
|
File.unlink(tmp + ".out")
|
|
|
|
|
|
|
|
rv
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Disassembles the supplied raw opcodes
|
|
|
|
#
|
|
|
|
def self.disassemble(raw)
|
|
|
|
check
|
|
|
|
|
2005-12-30 21:15:32 +00:00
|
|
|
# Race condition?! You bet!
|
2005-12-08 15:37:10 +00:00
|
|
|
tmp = Tempfile.new('nasmout').path
|
2005-12-30 21:15:32 +00:00
|
|
|
File.open(tmp, "wb") { |f| f.write(raw) }
|
2005-12-08 15:37:10 +00:00
|
|
|
|
2005-12-30 21:15:32 +00:00
|
|
|
p = ::IO.popen("ndisasm -u #{tmp}")
|
2005-12-08 15:37:10 +00:00
|
|
|
o = ''
|
|
|
|
|
|
|
|
begin
|
|
|
|
until p.eof?
|
|
|
|
o += p.read
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
p.close
|
|
|
|
end
|
|
|
|
|
|
|
|
File.unlink(tmp)
|
|
|
|
|
|
|
|
o
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|