Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple assembly code involving syscalls does not get decompiled correctly #24

Closed
mbalous opened this issue Dec 14, 2017 · 5 comments
Closed
Assignees

Comments

@mbalous
Copy link

mbalous commented Dec 14, 2017

Code

BITS 32
global _start

section .text
_start:
    	mov eax, 5 ; sys_open
    	mov ebx, fileName
	mov ecx, 66 ; creation flags: O_RDWR | O_CREAT
	mov edx, 292 ; file mode: S_IRUSR | S_IRGRP | S_IROTH
	int 0x80 ; syscall, eax now contains file descriptor

	mov ebx, eax
	mov eax, 4 ; sys_write
	mov ecx, payload
	mov edx, payloadSize
	int 0x80

	mov eax, 1
	int 0x80
fileName:
    	db  "/tmp/myfile",0
payload:
	db "malicious payload here", 0ah, 0
payloadSize: equ $-payload

Build and run

nasm -f elf test.asm && ld -m elf_i386 test.o && ./a.out

Retdec output

//
// This file was generated by the Retargetable Decompiler
// Website: https://retdec.com
// Copyright (c) 2017 Retargetable Decompiler <[email protected]>
//

#include <stdint.h>

// ------------------- Function Prototypes --------------------

int32_t _start(int32_t a1, int32_t a2, int32_t a3, int32_t a4, int32_t a5, uint32_t a6, int32_t a7, int32_t a8);
int32_t unknown_80480c4(void);
int32_t unknown_80480fe(void);
int32_t unknown_8048100(void);
int32_t unknown_8048116(void);
int32_t unknown_8048117(void);

// ------------------------ Functions -------------------------

// Address range: 0x8048060 - 0x80480b3
int32_t _start(int32_t a1, int32_t a2, int32_t a3, int32_t a4, int32_t a5, uint32_t a6, int32_t a7, int32_t a8) {
    // 0x8048060
    unknown_8048100();
    unknown_80480c4();
    unknown_80480fe();
    unknown_8048116();
    char * v1 = (char *)(a7 + 101); // 0x80480ad_0
    *v1 = (char)((int32_t)*v1 & a6 / 256);
    int32_t v2 = unknown_8048117(); // 0x80480b0
    return (int32_t)*(char *)v2 | v2;
}

// --------------------- Meta-Information ---------------------

// Detected compiler/packer: elfcrypt (1.0)
// Detected functions: 1
// Decompiler release: v2.2.1 (2016-09-07)
// Decompilation date: 2017-12-14 10:28:45

I'm not experienced in low level programming, but it seems like, that the C code does not reflect what is going on...

Maybe it is confused by storing data in .text section?

@PeterMatula
Copy link
Collaborator

Thank you for your report. After we finish work related to open-sourcing, we will focus on improving the decompilation quality. I will definitely look into this pretty soon.

@PeterMatula PeterMatula self-assigned this Dec 14, 2017
@mbalous
Copy link
Author

mbalous commented Dec 14, 2017

@mendax47
Copy link

mendax47 commented Dec 17, 2017

Yah... Same Here.. Wrong Output....
2nd image Upper Part RetDec Lower Part HexRay

Using Windows 7 32 bit

https://imgur.com/a/To4eI

@MerovingianByte
Copy link

When retdec does manage to decompile though, I find it better than hex-ray's.

PeterMatula added a commit to avast/retdec-regression-tests that referenced this issue Feb 21, 2019
@PeterMatula
Copy link
Collaborator

Output from the current master:

// Address range: 0x8049000 - 0x8049030
int32_t _start(void) {
    int32_t fd = open("/tmp/myfile", 66); // 0x8049014
    write(fd, (int32_t *)"malicious payload here\n", 24);
    exit(fd);
    return 1;
}

Regression tests added in avast/retdec-regression-tests@69a2f3f.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants