Skip to content

Commit 1d84528

Browse files
Merge branch 'master' into master
2 parents a66f50f + 51acc58 commit 1d84528

File tree

8 files changed

+46
-22
lines changed

8 files changed

+46
-22
lines changed

plasma/lib/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ def parse_args(self):
119119
help='Disable analysis on the entry point / symbols and don\'t scan memmory. You can force it with the command push_analyze_symbols.')
120120
parser.add_argument('--debugsp', action='store_true',
121121
help="Print the stack offset on each instructions. Warning: these values will not be saved in the database.")
122+
parser.add_argument('--db_path', default=None, help="Database path ('.<exe_name>.db' by default).")
122123

123124
args = parser.parse_args()
124125

@@ -140,6 +141,7 @@ def parse_args(self):
140141
self.list_sections = args.sections
141142
self.autoanalyzer = not args.noautoanalyzer
142143
self.debugsp = args.debugsp
144+
self.db_path = args.db_path
143145

144146
if args.nbytes == 0:
145147
self.nbytes = 4
@@ -157,6 +159,13 @@ def parse_args(self):
157159
else:
158160
self.raw_base = 0
159161

162+
def _get_database_path(self, executable_filename):
163+
if self.db_path is not None:
164+
return self.db_path
165+
dirname = os.path.dirname(executable_filename)
166+
path = dirname + "/" if dirname != "" else ""
167+
path += "." + os.path.basename(executable_filename) + ".db"
168+
return path
160169

161170
def load_file(self, filename=None):
162171
if filename is None:
@@ -175,7 +184,7 @@ def load_file(self, filename=None):
175184
die()
176185

177186
self.db = Database()
178-
self.db.load(filename)
187+
self.db.load(self._get_database_path(filename))
179188

180189
if self.raw_base != 0:
181190
self.db.raw_base = self.raw_base

plasma/lib/arch/arm/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ def is_cmp(i):
4545
return i.id == ARM_INS_CMP
4646

4747
def is_jump(i):
48+
if len(i.operands) <= 0 :
49+
return False
4850
# Suppose that the written register is the first operand
4951
op = i.operands[0]
5052

@@ -67,6 +69,8 @@ def is_uncond_jump(i):
6769
return is_jump(i) and i.cc == ARM_CC_AL
6870

6971
def is_ret(i):
72+
if len(i.operands) <= 0 :
73+
return False
7074
op = i.operands[0]
7175
return i.group(CS_GRP_RET) or i.id == ARM_INS_BX and \
7276
op.type == ARM_OP_REG and op.value.reg == ARM_REG_LR

plasma/lib/arch/mips/output.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@
2626
MIPS_INS_SUBU, MIPS_INS_BGTZ, MIPS_INS_LH, MIPS_INS_LHU,
2727
MIPS_INS_SH, MIPS_INS_SD, MIPS_INS_LD, MIPS_GRP_MIPS64,
2828
MIPS_INS_BGEZ, MIPS_INS_BNEZ, MIPS_INS_BEQZ, MIPS_INS_BLEZ,
29-
MIPS_INS_BLTZ, MIPS_REG_ZERO, MIPS_REG_GP, MIPS_INS_NEG)
29+
MIPS_INS_BLTZ, MIPS_REG_ZERO, MIPS_REG_GP, MIPS_INS_NEG,
30+
MIPS_INS_BEQ, MIPS_INS_BNE)
3031

3132
from plasma.lib.output import OutputAbs
3233
from plasma.lib.arch.mips.utils import (inst_symbol, is_call, is_jump, is_ret,
3334
is_uncond_jump, cond_symbol)
3435
from capstone.mips import (MIPS_INS_SLT, MIPS_INS_SLTI, MIPS_INS_SLTIU, MIPS_INS_SLTU,
3536
MIPS_INS_ANDI, MIPS_INS_OR, MIPS_INS_ORI)
3637

37-
# ASSIGNMENT_OPS = {ARM_INS_EOR, ARM_INS_AND, ARM_INS_ORR}
38-
ASSIGNMENT_OPS = {MIPS_INS_SLT, MIPS_INS_SLTI, MIPS_INS_SLTIU, MIPS_INS_SLTU}
38+
ASSIGNMENT_OPS = {MIPS_INS_SLT, MIPS_INS_SLTI, MIPS_INS_SLTIU, MIPS_INS_SLTU, MIPS_INS_BEQ, MIPS_INS_BNE}
3939

4040
LD_TYPE = {
4141
MIPS_INS_LH: "halfword",
@@ -148,16 +148,18 @@ def _if_cond(self, cond, fused_inst):
148148
self._add(" 0")
149149
return
150150

151-
assignment = fused_inst.id in ASSIGNMENT_OPS
151+
assignment = fused_inst.id in ASSIGNMENT_OPS or fused_inst.id in COND_ADD_ZERO
152152

153153
if assignment:
154154
self._add("(")
155-
self._operand(fused_inst, 1)
156-
if cond == MIPS_INS_BNEZ:
157-
self._add(" < ")
155+
self._operand(fused_inst, 0)
156+
self._add(" ")
157+
self._add(cond_symbol(cond))
158+
if cond in COND_ADD_ZERO:
159+
self._add(" 0")
158160
else:
159-
self._add(" >= ")
160-
self._operand(fused_inst, 2)
161+
self._add(" ")
162+
self._operand(fused_inst, 1)
161163
self._add(")")
162164

163165
def _sub_asm_inst(self, i, tab=0):
@@ -265,8 +267,11 @@ def _sub_asm_inst(self, i, tab=0):
265267
if i.id == MIPS_INS_LUI:
266268
self._operand(i, 0)
267269
self._add(" = ")
268-
self._operand(i, 1)
269-
self._add(" << 16")
270+
if str(i.operands[1].value.reg).isdigit:
271+
self._add(" 0x%x" % (i.operands[1].value.reg << 16))
272+
else:
273+
self._operand(i, 1)
274+
self._add(" << 16")
270275

271276
elif i.id == MIPS_INS_MOVE:
272277
self._operand(i, 0)

plasma/lib/arch/mips/process_ast.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,16 @@
2121
MIPS_INS_LUI, MIPS_OP_REG, MIPS_REG_ZERO, MipsOpValue)
2222

2323
from plasma.lib.ast import (Ast_Branch, Ast_Loop, Ast_IfGoto, Ast_Ifelse,
24-
Ast_AndIf)
24+
Ast_AndIf, Ast_If_cond)
2525
from plasma.lib.arch.mips.output import ASSIGNMENT_OPS
2626

2727

2828
FUSE_OPS = set(ASSIGNMENT_OPS)
29-
# FUSE_OPS.add(ARM_INS_CMP)
30-
# FUSE_OPS.add(ARM_INS_TST)
3129

3230

3331
def fuse_inst_with_if(ctx, ast):
3432
if isinstance(ast, Ast_Branch):
35-
types_ast = (Ast_Ifelse, Ast_IfGoto, Ast_AndIf)
33+
types_ast = (Ast_Ifelse, Ast_IfGoto, Ast_AndIf, Ast_If_cond)
3634
for i, n in enumerate(ast.nodes):
3735
if isinstance(n, list):
3836
if n[-1].id in FUSE_OPS and i + 1 < len(ast.nodes) \
@@ -43,8 +41,10 @@ def fuse_inst_with_if(ctx, ast):
4341
fuse_inst_with_if(ctx, n)
4442

4543
elif isinstance(ast, Ast_Ifelse):
44+
ast.fused_inst = ast.jump_inst
4645
fuse_inst_with_if(ctx, ast.br_next)
4746
fuse_inst_with_if(ctx, ast.br_next_jump)
4847

4948
elif isinstance(ast, Ast_Loop):
5049
fuse_inst_with_if(ctx, ast.branch)
50+

plasma/lib/arch/x86/int80.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
#
1919

20+
from collections import defaultdict
21+
2022
from capstone.x86 import (X86_REG_EBX, X86_REG_ECX, X86_REG_EDX, X86_REG_ESI,
2123
X86_INS_INT, X86_OP_IMM, X86_REG_AL, X86_REG_AX, X86_REG_EAX,
2224
X86_REG_RAX, X86_REG_BL, X86_REG_CL, X86_REG_DL, X86_REG_BX,
@@ -57,7 +59,7 @@
5759

5860
# http://docs.cs.up.ac.za/programming/asm/derick_tut/syscalls.html
5961

60-
SYSCALL = {
62+
SYSCALL_DATA = {
6163
1: {"name": "exit", "args_type": [ARG_INT]},
6264
# 2: {"name": "fork", "args_type": ['struct pt_regs']},
6365
2: {"name": "fork", "args_type": []},
@@ -241,6 +243,10 @@
241243
190: {"name": "vfork", "args_type": ['struct pt_regs']},
242244
}
243245

246+
def SYSCALL(no):
247+
if no not in SYSCALL_DATA:
248+
SYSCALL_DATA[no] = {"name": "SYS%d" % no, "args_type": []}
249+
return SYSCALL_DATA[no]
244250

245251

246252
def reg_write(inst, reg_id):
@@ -292,11 +298,11 @@ def read_block(ctx, blk):
292298
inline_comm[inst.address] = "?"
293299
continue
294300

295-
inline_comm[inst.address] = SYSCALL[sysnum]["name"] + "("
301+
inline_comm[inst.address] = SYSCALL(sysnum)["name"] + "("
296302

297303
# Search values for each args, otherwise print the register
298304

299-
args_type = SYSCALL[sysnum]["args_type"]
305+
args_type = SYSCALL(sysnum)["args_type"]
300306
for j in range(len(args_type)):
301307
idx_wr_reg = search_backward(blk, i, ARGS_ORDER[j])
302308

plasma/lib/database.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ def __init_vars(self):
9292
def load(self, filename):
9393
gc.disable()
9494

95-
dirname = os.path.dirname(filename)
96-
self.path = dirname + "/" if dirname != "" else ""
97-
self.path += "." + os.path.basename(filename) + ".db"
95+
self.path = filename
9896

9997
if os.path.exists(self.path):
10098
info("open database %s" % self.path)

plasma/lib/fileformat/elf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ def load_dyn_sym(self):
285285
"sh_size": relsz,
286286
"sh_flags":0,
287287
"sh_addralign":0
288+
"sh_flags": 2048,
288289
}
289290
reloc_sec = RelocationSection(
290291
fakerelheader, "reloc_plasma", self.elf)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pefile
22
pyelftools
33
msgpack-python>=0.4.6
4+
nose

0 commit comments

Comments
 (0)