disasm 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,69 @@
1
+ #include "ruby.h"
2
+ #include <libdis.h>
3
+
4
+ #define LINE_SIZE 1024
5
+
6
+ static VALUE t_init(VALUE self)
7
+ {
8
+ return INT2FIX(x86_init(opt_none, NULL, NULL));
9
+ }
10
+
11
+ static VALUE t_disassemble2yield(VALUE self, VALUE _data, VALUE _rva, VALUE _offset, VALUE _syntax)
12
+ {
13
+ x86_insn_t insn;
14
+ int size, line_len;
15
+ char line[LINE_SIZE];
16
+
17
+ if( !_data || _data == Qnil ) return Qnil;
18
+
19
+ char*buf = RSTRING_PTR(_data);
20
+ unsigned int bufsize = RSTRING_LEN(_data);
21
+ uint32_t rva = FIX2INT(_rva);
22
+ unsigned int offset = FIX2INT(_offset);
23
+ int syntax = FIX2INT(_syntax);
24
+ int n_ok = 0; // number of successfully disassembled instructions
25
+
26
+ if(!buf || !bufsize) return INT2FIX(0);
27
+
28
+ switch(syntax){
29
+ case native_syntax:
30
+ case intel_syntax:
31
+ case att_syntax:
32
+ case xml_syntax:
33
+ case raw_syntax:
34
+ break;
35
+ default:
36
+ // TODO: raise exception
37
+ syntax = native_syntax;
38
+ break;
39
+ }
40
+
41
+ while( offset < bufsize ){
42
+ size = x86_disasm(buf, bufsize, rva, offset, &insn);
43
+ if( size ){
44
+ // success
45
+ line_len = x86_format_insn(&insn, line, LINE_SIZE, syntax);
46
+ rb_yield_values(2, rb_str_new(line, line_len), INT2FIX(offset+rva));
47
+ offset += size;
48
+ n_ok++;
49
+ } else {
50
+ // invalid instruction
51
+ char err_buf[1024];
52
+ sprintf(err_buf, "raise InvalidInstruction.new(0x%x, 0x%x)", offset, offset+rva);
53
+ rb_eval_string(err_buf);
54
+ //rb_raise(ex,"invalid instruction at offset 0x%x (VA 0x%x)", offset, offset+rva);
55
+ break;
56
+ }
57
+ }
58
+
59
+ return INT2FIX(n_ok);
60
+ }
61
+
62
+ VALUE mDisasm;
63
+
64
+ void Init_disasm_ext() {
65
+ x86_init(opt_none, NULL, NULL);
66
+ mDisasm = rb_define_module("Disasm");
67
+ rb_define_singleton_method(mDisasm, "init", t_init, 0);
68
+ rb_define_singleton_method(mDisasm, "disassemble2yield", t_disassemble2yield, 4);
69
+ }
@@ -0,0 +1,3 @@
1
+ require 'mkmf'
2
+ have_library 'disasm'
3
+ create_makefile 'disasm_ext'
@@ -0,0 +1,3 @@
1
+ module Disasm
2
+ VERSION = "0.0.1"
3
+ end
data/lib/disasm.rb ADDED
@@ -0,0 +1,49 @@
1
+ require 'disasm_ext'
2
+
3
+ module Disasm
4
+
5
+ class Exception < ::Exception; end
6
+ class InvalidInstruction < Exception
7
+ attr_accessor :offset, :va
8
+
9
+ def initialize offset, va
10
+ @offset = offset
11
+ @va = va
12
+ end
13
+
14
+ def to_s
15
+ "Invalid instruction at offset 0x%x (VA 0x%x)" % [@offset, @va]
16
+ end
17
+ end
18
+
19
+ class << self
20
+ def disasm data, params = {}
21
+ rva = params[:rva] || params[:va] || 0
22
+ offset = params[:offset] || 0
23
+
24
+ syntax =
25
+ case params[:syntax]
26
+ when :native; 1
27
+ when :intel; 2
28
+ when :att; 3
29
+ when :xml; 4
30
+ when :raw; 5
31
+ else 1 # default to native syntax
32
+ end
33
+
34
+ if block_given?
35
+ disassemble2yield(data, rva, offset, syntax) do |x,va|
36
+ yield x,va
37
+ end
38
+ else
39
+ r = []
40
+ disassemble2yield(data, rva, offset, syntax) do |x|
41
+ r << x
42
+ end
43
+ r
44
+ end
45
+ end
46
+
47
+ alias :disassemble :disasm
48
+ end
49
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: disasm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andrey "Zed" Zaikin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-19 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: x86 disassembler
15
+ email:
16
+ - zed.0xff@gmail.com
17
+ executables: []
18
+ extensions:
19
+ - ext/disasm_ext/extconf.rb
20
+ extra_rdoc_files: []
21
+ files:
22
+ - ext/disasm_ext/disasm_ext.c
23
+ - ext/disasm_ext/extconf.rb
24
+ - lib/disasm.rb
25
+ - lib/disasm/version.rb
26
+ homepage: https://github.com/zed-0xff/disasm
27
+ licenses: []
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project: ! '[none]'
46
+ rubygems_version: 1.8.24
47
+ signing_key:
48
+ specification_version: 3
49
+ summary: x86 disassembler
50
+ test_files: []