1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Constructs for attacker control -------------------------------------------------------------------------------------

/// Check if instruction reads single register
#[inline(always)]
pub fn is_single_reg_read(instr: &zydis::DecodedInstruction) -> bool {
    let reg_read_cnt = instr
        .operands
        .iter()
        .filter(|&o| {
            (o.action == zydis::enums::OperandAction::READ)
                && (o.ty == zydis::enums::OperandType::REGISTER)
        })
        .count();

    reg_read_cnt == 1
}

/// Check if instruction writes single register
#[inline(always)]
pub fn is_single_reg_write(instr: &zydis::DecodedInstruction) -> bool {
    let reg_write_cnt = instr
        .operands
        .iter()
        .filter(|&o| {
            (o.action == zydis::enums::OperandAction::WRITE)
                && (o.ty == zydis::enums::OperandType::REGISTER)
        })
        .count();

    reg_write_cnt == 1
}

/// Check if instruction reads single register-controlled memory location
#[inline(always)]
pub fn is_single_reg_deref_read(instr: &zydis::DecodedInstruction) -> bool {
    let reg_deref_read_cnt = instr
        .operands
        .iter()
        .filter(|&o| {
            (o.action == zydis::enums::OperandAction::READ)
                && (o.ty == zydis::enums::OperandType::MEMORY)
                && (o.mem.base != zydis::Register::NONE)
        })
        .count();

    reg_deref_read_cnt == 1
}

/// Check if instruction writes single register-controlled memory location
#[inline(always)]
pub fn is_single_reg_deref_write(instr: &zydis::DecodedInstruction) -> bool {
    let reg_deref_write_cnt = instr
        .operands
        .iter()
        .filter(|&o| {
            (o.action == zydis::enums::OperandAction::WRITE)
                && (o.ty == zydis::enums::OperandType::MEMORY)
                && (o.mem.base != zydis::Register::NONE)
        })
        .count();

    reg_deref_write_cnt == 1
}

/// Check if jump instruction with register-controlled target
#[inline(always)]
pub fn is_reg_set_call(instr: &zydis::DecodedInstruction) -> bool {
    is_call(&instr) && is_single_reg_read(&instr)
}

/// Check if jump instruction with register-controlled target
#[inline(always)]
pub fn is_reg_set_jmp(instr: &zydis::DecodedInstruction) -> bool {
    is_jmp(&instr) && is_single_reg_read(&instr)
}

/// Check if jump instruction with register-controlled memory deref target
#[inline(always)]
pub fn is_mem_ptr_set_jmp(instr: &zydis::DecodedInstruction) -> bool {
    is_jmp(&instr) && is_single_reg_deref_read(&instr)
}

/// Check if call instruction with register-controlled memory deref target
#[inline(always)]
pub fn is_mem_ptr_set_call(instr: &zydis::DecodedInstruction) -> bool {
    is_call(&instr) && is_single_reg_deref_read(&instr)
}

/// Check if instruction is a ROP/JOP/SYS gadget tail
#[inline(always)]
pub fn is_gadget_tail(instr: &zydis::DecodedInstruction) -> bool {
    is_ret(instr) || is_jop_gadget_tail(instr) || is_sys_gadget_tail(instr)
}

/// Check if instruction is a JOP gadget tail
#[inline(always)]
pub fn is_jop_gadget_tail(instr: &zydis::DecodedInstruction) -> bool {
    is_reg_set_jmp(instr)
        || is_reg_set_call(instr)
        || is_mem_ptr_set_jmp(instr)
        || is_mem_ptr_set_call(instr)
}

/// Check if instruction is a SYS gadget tail
#[inline(always)]
pub fn is_sys_gadget_tail(instr: &zydis::DecodedInstruction) -> bool {
    is_syscall(instr) || is_legacy_linux_syscall(instr)
}

/// Check if instruction updates register in predictable fashion suitable for a dispatcher
#[inline(always)]
pub fn is_reg_update_from_curr_val(
    instr: &zydis::DecodedInstruction,
    reg: zydis::Register,
) -> bool {
    let reg_read_cnt = instr
        .operands
        .iter()
        .filter(|&o| {
            (o.action.intersects(zydis::OperandAction::MASK_READ))
                && (o.ty == zydis::enums::OperandType::REGISTER)
                && (o.reg == reg)
        })
        .count();

    if reg_read_cnt != 0 {
        let reg_write_cnt = instr
            .operands
            .iter()
            .filter(|&o| {
                (o.action.intersects(zydis::OperandAction::MASK_WRITE))
                    && (o.visibility != zydis::OperandVisibility::HIDDEN)
                    && (o.ty == zydis::enums::OperandType::REGISTER)
                    && (o.reg == reg)
            })
            .count();

        if reg_write_cnt == 1 {
            return true;
        }
    }

    false
}

// Categorization ------------------------------------------------------------------------------------------------------

/// Check if return instruction
#[inline(always)]
pub fn is_ret(instr: &zydis::DecodedInstruction) -> bool {
    instr.meta.category == zydis::enums::InstructionCategory::RET
}

/// Check if call instruction
#[inline(always)]
pub fn is_call(instr: &zydis::DecodedInstruction) -> bool {
    instr.meta.category == zydis::enums::InstructionCategory::CALL
}

/// Check if jmp instruction
pub fn is_jmp(instr: &zydis::DecodedInstruction) -> bool {
    instr.mnemonic == zydis::enums::Mnemonic::JMP
}

/// Check if interrupt instruction
#[inline(always)]
pub fn is_int(instr: &zydis::DecodedInstruction) -> bool {
    instr.meta.category == zydis::enums::InstructionCategory::INTERRUPT
}

/// Check if syscall instruction
#[inline(always)]
pub fn is_syscall(instr: &zydis::DecodedInstruction) -> bool {
    instr.meta.category == zydis::enums::InstructionCategory::SYSCALL
}

/// Check if legacy Linux syscall
#[inline(always)]
pub fn is_legacy_linux_syscall(instr: &zydis::DecodedInstruction) -> bool {
    let imm_0x80_cnt = instr
        .operands
        .iter()
        .filter(|&o| {
            (o.action == zydis::enums::OperandAction::READ)
                && (o.ty == zydis::enums::OperandType::IMMEDIATE)
                && (o.imm.value == 0x80)
        })
        .count();

    (instr.meta.category == zydis::enums::InstructionCategory::INTERRUPT) && (imm_0x80_cnt == 1)
}