orgasm 0.0.1a2 → 0.0.1a3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. data/bin/{disorgasm → ejaculate} +0 -0
  2. data/bin/swallow +0 -0
  3. data/lib/orgasm.rb +8 -2
  4. data/lib/orgasm/arch/i386.rb +27 -0
  5. data/lib/orgasm/{style.rb → arch/i386/base.rb} +4 -0
  6. data/lib/orgasm/{common → arch/i386/base}/address.rb +12 -19
  7. data/lib/orgasm/{common/unknown.rb → arch/i386/base/immediate.rb} +6 -10
  8. data/lib/orgasm/arch/i386/base/instruction.rb +41 -0
  9. data/lib/orgasm/arch/i386/base/register.rb +40 -0
  10. data/lib/orgasm/arch/i386/disassembler.rb +26 -154
  11. data/lib/orgasm/arch/i386/generator.rb +44 -0
  12. data/lib/orgasm/arch/i386/instructions.rb +150 -0
  13. data/lib/orgasm/arch/i386/instructions/dsl.rb +159 -0
  14. data/lib/orgasm/arch/i386/instructions/dsl/special.rb +75 -0
  15. data/lib/orgasm/arch/i386/instructions/instructions.rb +50 -0
  16. data/lib/orgasm/arch/i386/styles.rb +70 -0
  17. data/lib/orgasm/architecture.rb +103 -0
  18. data/lib/orgasm/assembler.rb +5 -16
  19. data/lib/orgasm/base.rb +50 -0
  20. data/lib/orgasm/{common/constant.rb → base/address.rb} +7 -6
  21. data/lib/orgasm/{common/register.rb → base/constant.rb} +11 -8
  22. data/lib/orgasm/base/instruction.rb +41 -0
  23. data/lib/orgasm/{common/instruction.rb → base/register.rb} +8 -8
  24. data/lib/orgasm/base/unknown.rb +36 -0
  25. data/lib/orgasm/disassembler.rb +25 -22
  26. data/lib/orgasm/disassembler/decoder.rb +26 -20
  27. data/lib/orgasm/{common/extensions.rb → extensions.rb} +12 -0
  28. data/lib/orgasm/generator.rb +46 -0
  29. data/lib/orgasm/generator/dsl.rb +60 -0
  30. data/lib/orgasm/piece.rb +49 -0
  31. data/lib/orgasm/styles.rb +64 -0
  32. data/lib/orgasm/styles/style.rb +55 -0
  33. data/lib/orgasm/version.rb +1 -1
  34. metadata +54 -14
  35. data/lib/orgasm/common.rb +0 -36
File without changes
File without changes
@@ -17,6 +17,12 @@
17
17
  # along with orgasm. If not, see <http://www.gnu.org/licenses/>.
18
18
  #++
19
19
 
20
- module Orgasm
20
+ require 'orgasm/base'
21
21
 
22
- end
22
+ require 'orgasm/piece'
23
+ require 'orgasm/styles'
24
+ require 'orgasm/disassembler'
25
+ require 'orgasm/generator'
26
+ require 'orgasm/assembler'
27
+
28
+ require 'orgasm/architecture'
@@ -0,0 +1,27 @@
1
+ #--
2
+ # Copyleft meh. [http://meh.paranoid.pk | meh@paranoici.org]
3
+ #
4
+ # This file is part of orgasm.
5
+ #
6
+ # orgasm is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # orgasm is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with orgasm. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ require 'orgasm/arch/i386/base'
21
+
22
+ Orgasm::Architecture.for 'i386' do
23
+ instructions 'orgasm/arch/i386/instructions'
24
+ disassembler 'orgasm/arch/i386/disassembler'
25
+ generator 'orgasm/arch/i386/generator'
26
+ styles 'orgasm/arch/i386/styles'
27
+ end
@@ -17,3 +17,7 @@
17
17
  # along with orgasm. If not, see <http://www.gnu.org/licenses/>.
18
18
  #++
19
19
 
20
+ require 'orgasm/arch/i386/base/instruction'
21
+ require 'orgasm/arch/i386/base/address'
22
+ require 'orgasm/arch/i386/base/register'
23
+ require 'orgasm/arch/i386/base/immediate'
@@ -17,33 +17,26 @@
17
17
  # along with orgasm. If not, see <http://www.gnu.org/licenses/>.
18
18
  #++
19
19
 
20
- module Orgasm
20
+ module Orgasm; module I386
21
21
 
22
- class Address
23
- attr_reader :start
24
-
25
- def initialize (value, offset=nil)
26
- if offset
27
- @start = value
28
- @value = offset.to_i
22
+ class Address < Orgasm::Address
23
+ def initialize (value=nil, options={})
24
+ if value.respond_to? :to_i
25
+ super(value)
29
26
  else
30
- @value = value.to_i
27
+ super()
31
28
  end
32
29
 
33
- yield self if block_given?
34
- end
35
-
36
- def offset?
37
- !!start
30
+ @options = options
38
31
  end
39
32
 
40
- def to_i
41
- @value
33
+ def relative?
34
+ !!@options[:relative]
42
35
  end
43
36
 
44
- def to_s
45
- offset? ? "[#{start}+#{to_i}]" : "0x%x" % to_i
37
+ def offset?
38
+ !!@options[:offset]
46
39
  end
47
40
  end
48
41
 
49
- end
42
+ end; end
@@ -17,18 +17,14 @@
17
17
  # along with orgasm. If not, see <http://www.gnu.org/licenses/>.
18
18
  #++
19
19
 
20
- module Orgasm
20
+ module Orgasm; module I386
21
21
 
22
- class Unknown
23
- def initialize (size)
24
- @size = size.to_i
22
+ class Immediate < Orgasm::Constant
23
+ attr_accessor :size
25
24
 
26
- yield self if block_given?
27
- end
28
-
29
- def to_i
30
- @size
25
+ def initialize (value=nil, size=nil)
26
+ super(value)
31
27
  end
32
28
  end
33
29
 
34
- end
30
+ end; end
@@ -0,0 +1,41 @@
1
+ #--
2
+ # Copyleft meh. [http://meh.paranoid.pk | meh@paranoici.org]
3
+ #
4
+ # This file is part of orgasm.
5
+ #
6
+ # orgasm is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # orgasm is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with orgasm. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ module Orgasm; module I386
21
+
22
+ class Instruction < Orgasm::Instruction
23
+ extend Forwardable
24
+
25
+ def_delegator :@parameters, :first, :destination
26
+ def_delegator :@parameters, :last, :source
27
+
28
+ def initialize (name=nil, destination=nil, source=nil)
29
+ super(name, destination, source)
30
+ end
31
+
32
+ def destination= (value)
33
+ parameters[0] = value
34
+ end
35
+
36
+ def source= (value)
37
+ parameters[1] = value
38
+ end
39
+ end
40
+
41
+ end; end
@@ -0,0 +1,40 @@
1
+ #--
2
+ # Copyleft meh. [http://meh.paranoid.pk | meh@paranoici.org]
3
+ #
4
+ # This file is part of orgasm.
5
+ #
6
+ # orgasm is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # orgasm is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with orgasm. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ module Orgasm; module I386
21
+
22
+ class Register < Orgasm::Register
23
+ attr_accessor :size
24
+
25
+ def initialize (name=nil)
26
+ super(name, Architecture.i386.instructions.register?(name))
27
+ end
28
+
29
+ def name= (value)
30
+ value = value.to_s.downcase.to_sym
31
+
32
+ unless Architecture.i386.instructions.register?(value)
33
+ raise ArgumentError, "#{value} isn't a valid i386 register"
34
+ end
35
+
36
+ @name = value
37
+ end
38
+ end
39
+
40
+ end; end
@@ -17,162 +17,34 @@
17
17
  # along with orgasm. If not, see <http://www.gnu.org/licenses/>.
18
18
  #++
19
19
 
20
- module Orgasm
21
-
22
- Disassembler.for('i386') {
23
- reg = registers = Class.new(Hash) {
24
- def initialize
25
- merge!(
26
- 32 => {
27
- EAX: 0x0,
28
- ECX: 0x1,
29
- EDX: 0x2,
30
- EBX: 0x3,
31
- ESP: 0x4,
32
- EBP: 0x5,
33
- ESI: 0x6,
34
- EDI: 0x7
35
- },
36
-
37
- 16 => {
38
- AX: 0x0,
39
- CX: 0x1,
40
- DX: 0x2,
41
- BX: 0x3,
42
- SP: 0x4,
43
- BP: 0x5,
44
- SI: 0x6,
45
- DI: 0x7
46
- },
47
-
48
- 8 => {
49
- AL: 0x0,
50
- CL: 0x1,
51
- DL: 0x2,
52
- BL: 0x3,
53
- AH: 0x4,
54
- CH: 0x5,
55
- DH: 0x6,
56
- BH: 0x7
57
- }
58
- )
59
- end
60
-
61
- def source (byte, bits=32)
62
- self[bits].key((byte & 0x38) >> 3)
63
- end
64
-
65
- def destination (byte, bits=32)
66
- self[bits].key(byte & 0x07)
67
- end; alias dest destination
68
- }.new
69
-
70
- on ?\x01, ?\x09, ?\x11, ?\x19, ?\x21, ?\x25, ?\x29, ?\x31, ?\x39, ?\x85, ?\x86, ?\x87, ?\x89, ?\xA1, ?\xA3 do
71
- increment = 1
72
-
73
- seek 1 do
74
- read 1 do |data|
75
- increment += 1 if data.to_byte & 0x07 == reg[32][:ESP]
76
- increment += 1 if (data.to_byte & 0xC0) >> 6 == 0x01
77
-
78
- if (data.to_byte & 0xC0) >> 6 == 0x10
79
- Unknown.new(1)
80
- end
81
- end
82
- end
83
-
84
- on ?\x01 do
85
- Instruction.new(:add) {|i|
86
- seek +1
87
-
88
- read 1 do |data|
89
- i.parameters << Register.new(reg.source(data.to_byte), 32)
90
- i.parameters << Register.new(reg.destination(data.to_byte), 32)
91
- end
92
-
93
- seek increment
94
- }
95
- end
96
-
97
- on ?\x09 do
98
- Instruction.new(:or) {
99
- seek +1
100
- }
101
- end
102
-
103
- on ?\x11 do
104
- Instruction.new(:adc) {
105
- seek +1
106
- }
107
- end
108
-
109
- on ?\x19 do
110
- Instruction.new(:sbb) {
111
- seek +1
112
- }
113
- end
114
-
115
- on ?\x21, ?\x25 do
116
- Instruction.new(:ad) {
117
- seek +1
118
- }
119
- end
120
-
121
- on ?\x29 do
122
- Instruction.new(:sub) {
123
- seek +1
124
- }
125
- end
126
-
127
- on ?\x31 do
128
- Instruction.new(:xor) {
129
- seek +1
130
- }
131
- end
132
-
133
- on ?\x19 do
134
- Instruction.new(:cmp) {
135
- seek +1
136
- }
137
- end
138
-
139
- on ?\x85 do
140
- Instruction.new(:test) {
141
- seek +1
142
- }
143
- end
144
-
145
- on ?\x86 do
146
- Instruction.new(:xchg) {
147
- seek +1
148
-
149
- # 8bit
150
- }
151
- end
152
-
153
- on ?\x87 do
154
- Instruction.new(:xchg) {
155
- seek +1
156
- }
157
- end
158
-
159
- on ?\x89 do
160
- Instruction.new(:mov) {
161
- seek +1
162
-
163
- read 1 do |data|
164
- increment = 5 if data.to_byte & 0x07 == 0x05 && data.to_byte < 0x40
20
+ instructions.to_hash.each {|name, description|
21
+ description.each {|description|
22
+ if description.is_a?(Hash)
23
+ description.each {|params, opcodes|
24
+ opcodes = opcodes.clone
25
+ known = opcodes.reverse.drop_while {|x| !x.is_a?(Integer)}.reverse.map {|x| x.chr}.join
26
+ opcodes.slice! known.length
27
+
28
+ on known do |whole, which|
29
+ seek which.length do
30
+ if opcodes.first.is_a?(String)
31
+ check = opcodes.shift.to_i
32
+
33
+ read 1 do |data|
34
+ skip unless ((data.to_byte & '00111000'.to_i(2)) >> 3) == check
35
+ end
36
+
37
+ opcodes.shift
38
+ end
39
+ end
165
40
  end
166
41
  }
167
- end
42
+ else
43
+ on description.map {|b| b.chr}.join do |whole, which|
44
+ seek which.length
168
45
 
169
- on ?\xA1, ?\xA3 do
170
- # increment = 4
171
- Instruction.new(:mov) {
172
- seek +1
173
- }
46
+ I386::Instruction.new(name)
47
+ end
174
48
  end
175
- end
49
+ }
176
50
  }
177
-
178
- end
@@ -0,0 +1,44 @@
1
+ #--
2
+ # Copyleft meh. [http://meh.paranoid.pk | meh@paranoici.org]
3
+ #
4
+ # This file is part of orgasm.
5
+ #
6
+ # orgasm is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # orgasm is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with orgasm. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ instructions.registers.each {|register|
21
+ define_singleton_method register do
22
+ register
23
+ end
24
+ }
25
+
26
+ generator.for I386::Instruction do |name, &block|
27
+ I386::Instruction.new(name, &block)
28
+ end
29
+
30
+ generator.for I386::Register do |name|
31
+ I386::Register.new(name)
32
+ end
33
+
34
+ generator.for I386::Address do |data|
35
+ if data.is_a?(Array)
36
+ Address.new(data)
37
+ else
38
+ Address.new(data)
39
+ end
40
+ end
41
+
42
+ generator.for I386::Immediate do |data|
43
+ I386::Immediate.new(data, 32)
44
+ end
@@ -0,0 +1,150 @@
1
+ #--
2
+ # Copyleft meh. [http://meh.paranoid.pk | meh@paranoici.org]
3
+ #
4
+ # This file is part of orgasm.
5
+ #
6
+ # orgasm is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU Affero General Public License as published
8
+ # by the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # orgasm is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU Affero General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Affero General Public License
17
+ # along with orgasm. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+
20
+ require 'orgasm/arch/i386/instructions/dsl'
21
+ require 'orgasm/arch/i386/instructions/instructions'
22
+
23
+ I386::Instructions[I386::DSL.new {
24
+ # ASCII Adjust After Addition
25
+ AAA [0x37]
26
+
27
+ # ASCII Adjust AX Before Division
28
+ AAD [0xD5, 0x0A],
29
+ [imm8] => [0xD5, ib]
30
+
31
+ # ASCII Adjust AX After Multiply
32
+ AAM [0xD4, 0x0A],
33
+ [imm8] => [0xD4, ib]
34
+
35
+ # ASCII Adjust AL After Substraction
36
+ AAS [0x3F]
37
+
38
+ # Add with Carry
39
+ ADC [al, imm8] => [0x14, ib],
40
+ [ax, imm16] => [0x15, iw],
41
+ [eax, imm32] => [0x15, id],
42
+ [r8|m8, imm8] => [0x80, ?2, ib],
43
+ [r16|m16, imm16] => [0x81, ?2, iw],
44
+ [r32|m32, imm32] => [0x81, ?2, id],
45
+ [r16|m16, imm8] => [0x83, ?2, ib],
46
+ [r32|m32, imm8] => [0x83, ?2, ib],
47
+ [r8|m8, r8] => [0x10, r],
48
+ [r16|m16, r16] => [0x11, r],
49
+ [r32|m32, r32] => [0x11, r],
50
+ [r8, r8|m8] => [0x12, r],
51
+ [r16, r16|m16] => [0x13, r],
52
+ [r32, r32|m32] => [0x13, r]
53
+
54
+ # Add
55
+ ADD [al, imm8] => [0x04, ib],
56
+ [ax, imm16] => [0x05, iw],
57
+ [eax, imm32] => [0x05, id],
58
+ [r8|m8, imm8] => [0x80, ?0, ib],
59
+ [r16|m16, imm16] => [0x81, ?0, iw],
60
+ [r32|m32, imm32] => [0x81, ?0, id],
61
+ [r16|m16, imm8] => [0x83, ?0, ib],
62
+ [r32|m32, imm8] => [0x83, ?0, ib],
63
+ [r8|m8, r8] => [0x00, r],
64
+ [r16|m16, r16] => [0x01, r],
65
+ [r32|m32, r32] => [0x01, r],
66
+ [r8, r8|m8] => [0x02, r],
67
+ [r16, r16|m16] => [0x03, r],
68
+ [r32, r32|m32] => [0x03, r]
69
+
70
+ # Logical AND
71
+ AND [al, imm8] => [0x24, ib],
72
+ [ax, imm16] => [0x25, iw],
73
+ [eax, imm32] => [0x25, id],
74
+ [r8|m8, imm8] => [0x80, ?4, ib],
75
+ [r16|m16, imm16] => [0x81, ?4, iw],
76
+ [r32|m32, imm32] => [0x81, ?4, id],
77
+ [r16|m16, imm8] => [0x83, ?4, ib],
78
+ [r32|m32, imm8] => [0x83, ?4, ib],
79
+ [r8|m8, r8] => [0x20, r],
80
+ [r16|m16, r16] => [0x21, r],
81
+ [r32|m32, r32] => [0x21, r],
82
+ [r8, r8|m8] => [0x22, r],
83
+ [r16, r16|m16] => [0x23, r],
84
+ [r32, r32|m32] => [0x23, r]
85
+
86
+ # Adjust RPL Field of Segment Selector
87
+ ARPL [r16|m16, r16] => [0x63, r]
88
+
89
+ # Check Array Index Against Bounds
90
+ BOUND [r16, m16&16] => [0x62, r],
91
+ [r32, m32&32] => [0x62, r]
92
+
93
+ # Bit Scan Forward
94
+ # BFS [r16, r16|m16] => [0x0F, 0xBC],
95
+ # [r32, r32|m32] => [0x0F, 0xBC]
96
+ # TODO: find out what the fuck is this
97
+
98
+ # Bit Scan Reverse
99
+ # BSR [r16, r16|m16] => [0x0F, 0xBD],
100
+ # [r32, r32|m32] => [0x0F, 0xBD]
101
+ # TODO: find out what the fuck is this
102
+
103
+ # Byte Swap
104
+ BSWAP [r32] => [0x0F, 0xC8, rd]
105
+ # FIXME: not available on i386, only i486+
106
+
107
+ # Bit Test
108
+ BT [r16|m16, r16] => [0x0F, 0xA3],
109
+ [r32|m32, r32] => [0x0F, 0xA3],
110
+ [r16|m16, imm8] => [0x0F, 0xBA, ?4, ib],
111
+ [r32|m32, imm8] => [0x0F, 0xBA, ?4, ib]
112
+
113
+ # Bit Test and Complement
114
+ BTC [r16|m16, r16] => [0x0F, 0xBB],
115
+ [r32|m32, r32] => [0x0F, 0xBB],
116
+ [r16|m16, imm8] => [0x0F, 0xBA, ?7, ib],
117
+ [r32|m32, imm8] => [0x0F, 0xBA, ?7, ib]
118
+
119
+ # Bit Test and Reset
120
+ BTR [r16|m16, r16] => [0x0F, 0xB3],
121
+ [r32|m32, r32] => [0x0F, 0xB3],
122
+ [r16|m16, imm8] => [0x0F, 0xBA, ?6, ib],
123
+ [r32|m32, imm8] => [0x0F, 0xBA, ?6, ib]
124
+
125
+ # Call Procedure
126
+ CALL [rel16] => [0xE8, cw],
127
+ [rel32] => [0xE8, cd],
128
+ [r16|m16] => [0xFF, ?2],
129
+ [r32|m32] => [0xFF, ?2],
130
+ [ptr16^16] => [0x9A, cd],
131
+ [ptr16^32] => [0x9A, cp],
132
+ [m16^16] => [0xFF, ?3],
133
+ [m16^32] => [0xFF, ?3]
134
+
135
+
136
+ # -- x87 FPU --
137
+
138
+ # Packed Single-FP Add
139
+ ADDPS [xmm1, xmm2|m128] => [0x0F, 0x58, r]
140
+
141
+ # Scalar Single-FP Add
142
+ ADDSS [xmm1, xmm2|m32] => [0xF3, 0x0F, 0x58, r]
143
+
144
+ # Bit-wise Logical And ot For Single-FP
145
+ ANDNPS [xmm1, xmm2|m128] => [0x0F, 0x55, r]
146
+
147
+ # Bit-wise Logical And For Single FP
148
+ ANDPS [xmm1, xmm2|m128] => [0x0F, 0x54, r]
149
+
150
+ }.to_hash]