| 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.debug;
|
| 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 Test_read extends TestUnit implements Serializable {
|
| 48 |
|
|
public static void main(String args[]) throws Exception {
|
| 49 |
|
|
run_test(Test_read.class);
|
| 50 |
|
|
}
|
| 51 |
|
|
|
| 52 |
|
|
//--------------------------------------------------------------------------
|
| 53 |
|
|
@Override
|
| 54 |
|
|
public int get_test_count() throws Exception {
|
| 55 |
|
|
return 1;
|
| 56 |
|
|
}
|
| 57 |
|
|
|
| 58 |
|
|
int type;
|
| 59 |
|
|
|
| 60 |
|
|
@Override
|
| 61 |
|
|
public void init() throws Exception {
|
| 62 |
|
|
|
| 63 |
|
|
random = new Random(0 + index);
|
| 64 |
|
|
|
| 65 |
|
|
String instruction;
|
| 66 |
|
|
while(true) {
|
| 67 |
|
|
layers.clear();
|
| 68 |
|
|
|
| 69 |
|
|
/* 0 - REP LODS read breakpoint
|
| 70 |
|
|
*/
|
| 71 |
|
|
|
| 72 |
|
|
type = 0;
|
| 73 |
|
|
|
| 74 |
|
|
LinkedList<Pair<Long, Long>> prohibited_list = new LinkedList<>();
|
| 75 |
|
|
|
| 76 |
|
|
// if false: v8086 mode
|
| 77 |
|
|
boolean is_real = true;
|
| 78 |
|
|
|
| 79 |
|
|
InstructionLayer instr = new InstructionLayer(random, prohibited_list, true);
|
| 80 |
|
|
layers.add(instr);
|
| 81 |
|
|
StackLayer stack = new StackLayer(random, prohibited_list);
|
| 82 |
|
|
layers.add(stack);
|
| 83 |
|
|
layers.add(new OtherLayer(is_real ? OtherLayer.Type.REAL : OtherLayer.Type.PROTECTED_OR_V8086, random));
|
| 84 |
|
|
layers.add(new FlagsLayer(FlagsLayer.Type.RANDOM, random));
|
| 85 |
|
|
layers.add(new GeneralRegisterLayer(random));
|
| 86 |
|
|
layers.add(new SegmentLayer(random));
|
| 87 |
|
|
layers.add(new MemoryLayer(random));
|
| 88 |
|
|
layers.add(new IOLayer(random));
|
| 89 |
|
|
layers.addFirst(new HandleModeChangeLayer(
|
| 90 |
|
|
getInput("cr0_pe"),
|
| 91 |
|
|
getInput("vmflag"),
|
| 92 |
|
|
getInput("cs_rpl"),
|
| 93 |
|
|
getInput("cs_p"),
|
| 94 |
|
|
getInput("cs_s"),
|
| 95 |
|
|
getInput("cs_type")
|
| 96 |
|
|
));
|
| 97 |
|
|
|
| 98 |
|
|
// instruction size
|
| 99 |
|
|
boolean cs_d_b = getInput("cs_d_b") == 1;
|
| 100 |
|
|
boolean vmflag = getInput("vmflag") == 1;
|
| 101 |
|
|
|
| 102 |
|
|
long cs_limit = getInput("cs_limit");
|
| 103 |
|
|
long cs_base = getInput("cs_base");
|
| 104 |
|
|
|
| 105 |
|
|
boolean a32 = random.nextBoolean();
|
| 106 |
|
|
boolean o32 = true;
|
| 107 |
|
|
|
| 108 |
|
|
final boolean iflag = (type == -1)? true : random.nextBoolean();
|
| 109 |
|
|
final int ecx = (type == 0)? 4 : random.nextInt();
|
| 110 |
|
|
Layer debug_layer = new Layer() {
|
| 111 |
|
|
|
| 112 |
|
|
long rflag() { return 0; }
|
| 113 |
|
|
long tflag() { return 0; }
|
| 114 |
|
|
long iflag() { return iflag? 1 : 0; }
|
| 115 |
|
|
long ecx() { return ecx; }
|
| 116 |
|
|
long edx() { return 0x30000040; } // LEN3 = 1 byte(00); RW3 = read/write(10)
|
| 117 |
|
|
|
| 118 |
|
|
long dflag() { return 0; }
|
| 119 |
|
|
long ds_base() { return 0; }
|
| 120 |
|
|
long esi() { return 0; }
|
| 121 |
|
|
long dr3() { return 3; }
|
| 122 |
|
|
|
| 123 |
|
|
long check_interrupt(long counter) { return (type == -1 && (counter == 6))? 0xAB : 0x100; }
|
| 124 |
|
|
|
| 125 |
|
|
long get_test_type() { return 18; }
|
| 126 |
|
|
};
|
| 127 |
|
|
layers.addFirst(debug_layer);
|
| 128 |
|
|
|
| 129 |
|
|
long handler = cs_limit;
|
| 130 |
|
|
|
| 131 |
|
|
//debug vector = 0x01*4 = 0x04 linear
|
| 132 |
|
|
MemoryPatchLayer int_patch = new MemoryPatchLayer(random, prohibited_list, (int)(0x01*4), (byte)(handler&0xFF),(byte)((handler>>8)&0xFF), 0x00,0x00);
|
| 133 |
|
|
layers.addFirst(int_patch);
|
| 134 |
|
|
|
| 135 |
|
|
//interrupt handler: simply IRET
|
| 136 |
|
|
MemoryPatchLayer handler_patch = new MemoryPatchLayer(random, prohibited_list, (int)handler, 0xCF);
|
| 137 |
|
|
layers.addFirst(handler_patch);
|
| 138 |
|
|
|
| 139 |
|
|
|
| 140 |
|
|
String prefix = "";
|
| 141 |
|
|
if(cs_d_b != o32) { prefix = "66" + prefix; }
|
| 142 |
|
|
|
| 143 |
|
|
int eflags_start = 0x00000300; //TF and IF set
|
| 144 |
|
|
int eflags_end = 0x00000200; //IF set
|
| 145 |
|
|
stack.push_dword(eflags_start);
|
| 146 |
|
|
stack.push_dword(eflags_end);
|
| 147 |
|
|
|
| 148 |
|
|
instruction = "";
|
| 149 |
|
|
if(type == 0) {
|
| 150 |
|
|
// MOV to DR from edx; REP LODS
|
| 151 |
|
|
instruction = prefix + "0F23FA" + "F3" + "AC" + "90909090909090909090900F0F";
|
| 152 |
|
|
}
|
| 153 |
|
|
|
| 154 |
|
|
// add instruction
|
| 155 |
|
|
instr.add_instruction(instruction);
|
| 156 |
|
|
|
| 157 |
|
|
// end condition
|
| 158 |
|
|
break;
|
| 159 |
|
|
}
|
| 160 |
|
|
|
| 161 |
|
|
System.out.println("Instruction: [" + instruction + "]");
|
| 162 |
|
|
}
|
| 163 |
|
|
|
| 164 |
|
|
}
|