| 1 |
2 |
alfik |
/*
|
| 2 |
|
|
* Copyright (c) 2014, Aleksander Osman
|
| 3 |
|
|
* All rights reserved.
|
| 4 |
|
|
*
|
| 5 |
|
|
* Redistribution and use in source and binary forms, with or without
|
| 6 |
|
|
* modification, are permitted provided that the following conditions are met:
|
| 7 |
|
|
*
|
| 8 |
|
|
* * Redistributions of source code must retain the above copyright notice, this
|
| 9 |
|
|
* list of conditions and the following disclaimer.
|
| 10 |
|
|
*
|
| 11 |
|
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
| 12 |
|
|
* this list of conditions and the following disclaimer in the documentation
|
| 13 |
|
|
* and/or other materials provided with the distribution.
|
| 14 |
|
|
*
|
| 15 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
| 16 |
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
| 17 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
| 18 |
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
| 19 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
| 20 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
| 21 |
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
| 22 |
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
| 23 |
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
| 24 |
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| 25 |
|
|
*/
|
| 26 |
|
|
|
| 27 |
|
|
package ao486.test.branch;
|
| 28 |
|
|
|
| 29 |
|
|
import ao486.test.TestUnit;
|
| 30 |
|
|
import ao486.test.layers.FlagsLayer;
|
| 31 |
|
|
import ao486.test.layers.GeneralRegisterLayer;
|
| 32 |
|
|
import ao486.test.layers.HandleModeChangeLayer;
|
| 33 |
|
|
import ao486.test.layers.IOLayer;
|
| 34 |
|
|
import ao486.test.layers.InstructionLayer;
|
| 35 |
|
|
import ao486.test.layers.Layer;
|
| 36 |
|
|
import ao486.test.layers.MemoryLayer;
|
| 37 |
|
|
import ao486.test.layers.MemoryPatchLayer;
|
| 38 |
|
|
import ao486.test.layers.OtherLayer;
|
| 39 |
|
|
import ao486.test.layers.Pair;
|
| 40 |
|
|
import ao486.test.layers.SegmentLayer;
|
| 41 |
|
|
import ao486.test.layers.StackLayer;
|
| 42 |
|
|
import java.io.*;
|
| 43 |
|
|
import java.util.LinkedList;
|
| 44 |
|
|
import java.util.Random;
|
| 45 |
|
|
|
| 46 |
|
|
|
| 47 |
|
|
public class TestIRET_protected_to_v8086 extends TestUnit implements Serializable {
|
| 48 |
|
|
public static void main(String args[]) throws Exception {
|
| 49 |
|
|
run_test(TestIRET_protected_to_v8086.class);
|
| 50 |
|
|
}
|
| 51 |
|
|
|
| 52 |
|
|
//--------------------------------------------------------------------------
|
| 53 |
|
|
@Override
|
| 54 |
|
|
public int get_test_count() throws Exception {
|
| 55 |
|
|
return 100;
|
| 56 |
|
|
}
|
| 57 |
|
|
|
| 58 |
|
|
@Override
|
| 59 |
|
|
public void init() throws Exception {
|
| 60 |
|
|
|
| 61 |
|
|
random = new Random(1 + index);
|
| 62 |
|
|
|
| 63 |
|
|
String instruction;
|
| 64 |
|
|
while(true) {
|
| 65 |
|
|
layers.clear();
|
| 66 |
|
|
|
| 67 |
|
|
LinkedList<Pair<Long, Long>> prohibited_list = new LinkedList<>();
|
| 68 |
|
|
|
| 69 |
|
|
InstructionLayer instr = new InstructionLayer(random, prohibited_list);
|
| 70 |
|
|
layers.add(instr);
|
| 71 |
|
|
StackLayer stack = new StackLayer(random, prohibited_list);
|
| 72 |
|
|
layers.add(stack);
|
| 73 |
|
|
layers.add(new OtherLayer(OtherLayer.Type.PROTECTED_OR_V8086, random));
|
| 74 |
|
|
layers.add(new FlagsLayer(FlagsLayer.Type.NOT_V8086_NOT_NT, random));
|
| 75 |
|
|
layers.add(new GeneralRegisterLayer(random));
|
| 76 |
|
|
layers.add(new SegmentLayer(random));
|
| 77 |
|
|
layers.add(new MemoryLayer(random));
|
| 78 |
|
|
layers.add(new IOLayer(random));
|
| 79 |
|
|
layers.addFirst(new HandleModeChangeLayer(
|
| 80 |
|
|
getInput("cr0_pe"),
|
| 81 |
|
|
getInput("vmflag"),
|
| 82 |
|
|
0, //getInput("cs_rpl"),
|
| 83 |
|
|
getInput("cs_p"),
|
| 84 |
|
|
getInput("cs_s"),
|
| 85 |
|
|
getInput("cs_type")
|
| 86 |
|
|
));
|
| 87 |
|
|
|
| 88 |
|
|
// instruction size
|
| 89 |
|
|
boolean cs_d_b = getInput("cs_d_b") == 1;
|
| 90 |
|
|
|
| 91 |
|
|
boolean a32 = random.nextBoolean();
|
| 92 |
|
|
boolean o32 = true;
|
| 93 |
|
|
|
| 94 |
|
|
// destination
|
| 95 |
|
|
long cs = random.nextInt(0xFFFF+1);
|
| 96 |
|
|
long eip = random.nextInt(0x1FFFF);
|
| 97 |
|
|
long eflags = random.nextInt();
|
| 98 |
|
|
|
| 99 |
|
|
long eip_final = eip & 0xFFFF;
|
| 100 |
|
|
|
| 101 |
|
|
// eflags have set vmflag
|
| 102 |
|
|
eflags |= 0x20000;
|
| 103 |
|
|
|
| 104 |
|
|
long dest = (cs << 4) + eip_final;
|
| 105 |
|
|
|
| 106 |
|
|
boolean can_add = Layer.collides(prohibited_list, (int)dest, (int)(dest+1));
|
| 107 |
|
|
|
| 108 |
|
|
if(can_add == false) continue;
|
| 109 |
|
|
|
| 110 |
|
|
MemoryPatchLayer patch = new MemoryPatchLayer(random, prohibited_list, (int)dest, 0x0F,0x0F);
|
| 111 |
|
|
layers.addFirst(patch);
|
| 112 |
|
|
|
| 113 |
|
|
// stack
|
| 114 |
|
|
stack.push_dword((int)eip); //eip
|
| 115 |
|
|
stack.push_dword((int)cs | (random.nextInt() & 0xFFFF0000)); //cs
|
| 116 |
|
|
stack.push_dword((int)eflags); //eflags
|
| 117 |
|
|
|
| 118 |
|
|
// add instruction
|
| 119 |
|
|
instruction = prepare_instr(cs_d_b, a32, o32);
|
| 120 |
|
|
instr.add_instruction(instruction);
|
| 121 |
|
|
|
| 122 |
|
|
// end condition
|
| 123 |
|
|
System.out.printf("cs: %x\neip: %x\neflags: %x\ndst: %x\n", (int)cs,(int)eip,(int)eflags,(int)dest);
|
| 124 |
|
|
break;
|
| 125 |
|
|
}
|
| 126 |
|
|
|
| 127 |
|
|
System.out.println("Instruction: [" + instruction + "]");
|
| 128 |
|
|
}
|
| 129 |
|
|
|
| 130 |
|
|
int imm_len(boolean a32, boolean o32, int opcode) {
|
| 131 |
|
|
return 0;
|
| 132 |
|
|
}
|
| 133 |
|
|
String prepare_instr(boolean cs_d_b, boolean a32, boolean o32) throws Exception {
|
| 134 |
|
|
int opcodes[] = {
|
| 135 |
|
|
0xCF
|
| 136 |
|
|
};
|
| 137 |
|
|
|
| 138 |
|
|
String prefix = "";
|
| 139 |
|
|
if(cs_d_b != o32) { prefix = "66" + prefix; }
|
| 140 |
|
|
if(cs_d_b != a32) { prefix = "67" + prefix; }
|
| 141 |
|
|
|
| 142 |
|
|
int opcode = opcodes[random.nextInt(opcodes.length)];
|
| 143 |
|
|
boolean is_modregrm = false;
|
| 144 |
|
|
|
| 145 |
|
|
byte possible_modregrm = (byte)random.nextInt();
|
| 146 |
|
|
byte possible_sib = (byte)random.nextInt();
|
| 147 |
|
|
|
| 148 |
|
|
int len = (is_modregrm == false)? 1 : 1 + modregrm_len(!cs_d_b, unsigned(possible_modregrm), unsigned(possible_sib));
|
| 149 |
|
|
len += imm_len(a32, o32, opcode);
|
| 150 |
|
|
|
| 151 |
|
|
|
| 152 |
|
|
byte instr[] = new byte[len];
|
| 153 |
|
|
instr[0] = (byte)opcode;
|
| 154 |
|
|
for(int i=1; i<len; i++) {
|
| 155 |
|
|
if(i==1) instr[1] = possible_modregrm;
|
| 156 |
|
|
else if(i==2) instr[2] = possible_sib;
|
| 157 |
|
|
else instr[i] = (byte)random.nextInt();
|
| 158 |
|
|
}
|
| 159 |
|
|
|
| 160 |
|
|
return prefix + bytesToHex(instr);
|
| 161 |
|
|
}
|
| 162 |
|
|
|
| 163 |
|
|
}
|