Skip to content

Commit 8ddf676

Browse files
committed
rewritings
1 parent 1b50b8e commit 8ddf676

9 files changed

+237
-240
lines changed

Makefile

+13-20
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,34 @@
11

2+
# C compiler
3+
CC = gcc
4+
25
# assembler
36
ASM = nasm
47

5-
# c compiler
6-
CC = gcc
8+
# compiler flags
9+
CFLAGS = -Wall -Wextra -std=c11
710

811
# assembler flags
9-
AFLAGS = -felf64
10-
11-
# compiler flags
12-
CFLAGS = -c -Wall -O2
12+
AFLAGS = -f elf64
1313

1414
# linker flags
1515
LFLAGS = -no-pie
1616

1717
.PHONY: clean install
1818

19-
all: makepoly example
20-
21-
makepoly: makepoly.o polyengine.o
19+
mkpoly: mkpoly.o polyeng.o
2220
$(CC) $(LFLAGS) $^ -o $@
2321

24-
example: example.o
25-
$(CC) $(LFLAGS) $< -o $@
26-
27-
makepoly.o: makepoly.c
28-
$(CC) $(CFLAGS) $< -o $@
22+
mkpoly.o: mkpoly.c
23+
$(CC) $(CFLAGS) -c $< -o $@
2924

30-
polyengine.o: polyengine.asm makepoly.inc
31-
$(ASM) $(AFLAGS) $< -o $@
32-
33-
example.o: example.asm makepoly.inc
25+
polyeng.o: polyeng.asm
3426
$(ASM) $(AFLAGS) $< -o $@
3527

3628
clean:
37-
rm -f *.o makepoly example example.poly
29+
rm -f *.o mkpoly
3830

3931
install:
40-
install makepoly /usr/local/bin/
32+
install mkpoly /usr/local/bin/
33+
4134

example.asm

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
cpu x86-64
33

4-
%include "makepoly.inc"
4+
%include "mkpoly.inc"
55

66
extern puts
77

@@ -15,6 +15,7 @@ section .text
1515
main:
1616
call decrypt
1717
call hello
18+
xor rax, rax
1819
ret
1920

2021
decrypt:

example.crypt

16.5 KB
Binary file not shown.

makepoly.c

-115
This file was deleted.

mkpoly

17.9 KB
Binary file not shown.

mkpoly.c

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
2+
#include "mkpoly.h"
3+
4+
int main(int argc, char* argv[])
5+
{
6+
if (argc < 5)
7+
{
8+
help();
9+
return 1;
10+
}
11+
12+
FILE* source = fopen(argv[1], "rb");
13+
14+
if (!source)
15+
{
16+
fprintf(stderr, "failed to open %s\n", argv[1]);
17+
return 2;
18+
}
19+
20+
size_t size;
21+
fseek(source, 0, SEEK_END);
22+
size = ftell(source);
23+
fseek(source, 0, SEEK_SET);
24+
25+
uint8_t *bin = malloc(size);
26+
27+
if (!bin)
28+
{
29+
fprintf(stderr, "failed to allocate binary data");
30+
fclose(source);
31+
return 3;
32+
}
33+
34+
if (!fread(bin, size, 1, source))
35+
{
36+
fprintf(stderr, "binary file reading failed\n");
37+
fclose(source);
38+
free(bin);
39+
return 4;
40+
}
41+
42+
fclose(source);
43+
44+
size_t coff = strtol(argv[2], NULL, 16);
45+
size_t csize = strtol(argv[3], NULL, 16);
46+
size_t eoff = strtol(argv[4], NULL, 16);
47+
48+
if (polyeng(bin, coff, csize, eoff))
49+
{
50+
fprintf(stderr, "polymorphic engine error\n");
51+
free(bin);
52+
return 5;
53+
}
54+
55+
char filename[FILENAME_MAX];
56+
snprintf(filename, FILENAME_MAX, "%s.crypt", argv[1]);
57+
58+
FILE *dest = fopen(filename, "wb");
59+
60+
if (!dest)
61+
{
62+
fprintf(stderr, "failed to open %s\n", filename);
63+
free(bin);
64+
return 6;
65+
}
66+
67+
if (!fwrite(bin, size, 1, dest))
68+
{
69+
fprintf(stderr, "failed to write polymorphic code into %s\n", filename);
70+
free(bin);
71+
return 7;
72+
}
73+
74+
free(bin);
75+
76+
return 0;
77+
}
78+
79+
void help()
80+
{
81+
printf("usage: mkpoly ");
82+
printf("<source> <crypt-off> <crypt-size> <engine-off>\n");
83+
printf("<source> the filename of the binary file\n");
84+
printf("<crypt-off> the offset of the section to crypt\n");
85+
printf("<crypt-size> the size of the section to crypt\n");
86+
printf("<engine-off> the offset where to place the decryptor\n");
87+
}
88+

mkpoly.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
#ifndef __MKPOLY_H__
3+
#define __MKPOLY_H__
4+
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
#include <stdint.h>
8+
9+
/**
10+
* Print an helpful message.
11+
*/
12+
void help();
13+
14+
/**
15+
* Execute the polymorphic engine.
16+
* @param coff The offset of the section to crypt.
17+
* @param csize The size of the section to crypt.
18+
* @param eoff The offset where to place the decryptor.
19+
* @return non-zero if an error occurred.
20+
*/
21+
extern int polyeng(uint8_t *bin, size_t coff, size_t csize, size_t eoff);
22+
23+
#endif // __MKPOLY_H__
24+

makepoly.inc mkpoly.inc

+34-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,37 @@ extern mprotect
88
%define PROT_WRITE 0x2
99
%define PROT_EXEC 0x4
1010

11+
; prefixes and opcodes of 8 invertible instructions:
12+
; instructions | inverted instructions
13+
; - add reg, reg | - sub reg, reg
14+
; - sub reg, reg | - add reg, reg
15+
; - xor reg, reg | - xor reg, reg
16+
; - add reg, i32 | - sub reg, i32
17+
; - sub reg, i32 | - add reg, i32
18+
; - xor reg, i32 | - xor reg, i32
19+
; - rol reg, i8 | - ror reg, i8
20+
; - ror reg, i8 | - rol reg, i8
21+
; - inc reg
22+
; - dec reg
23+
; - not reg
24+
; - neg reg
25+
%define OPCODE_ADD_RM 0x01
26+
%define OPCODE_SUB_RM 0x29
27+
%define OPCODE_XOR_RM 0x31
28+
%define PREFIX_ASX_IMM 0x81
29+
%define OPCODE_ADD_RI 0xC0
30+
%define OPCODE_SUB_RI 0xE8
31+
%define OPCODE_XOR_RI 0xF0
32+
%define PREFIX_ROT_IMM 0xC1
33+
%define OPCODE_ROL_RI 0xC0
34+
%define OPCODE_ROR_RI 0xC8
35+
%define PREFIX_INC_DEC 0xFF
36+
%define OPCODE_INC_R 0xC0
37+
%define OPCODE_DEC_R 0xC8
38+
%define PREFIX_NOT_NEG 0xF7
39+
%define OPCODE_NOT_R 0xD0
40+
%define OPCODE_NEG_R 0xD8
41+
1142
; no operation x86 opcode
1243
%define OPCODE_NOP 0x90
1344

@@ -37,20 +68,20 @@ extern mprotect
3768
call mprotect
3869
mov rdi, %1
3970
lea rsi, [rdi+%2]
40-
.makepoly_loop:
71+
.mkpoly_loop:
4172
mov eax, [rdi ]
4273
mov ecx, [rdi+0x4]
4374
mov edx, [rdi+0x8]
4475
mov ebx, [rdi+0xC]
45-
.makepoly_func:
76+
.mkpoly_func:
4677
times POLY_FUNC_SIZE db OPCODE_NOP
4778
mov [rdi ], eax
4879
mov [rdi+0x4], ecx
4980
mov [rdi+0x8], edx
5081
mov [rdi+0xC], ebx
5182
add rdi, 0x10
5283
cmp rdi, rsi
53-
jne .makepoly_loop
84+
jne .mkpoly_loop
5485
mov rdi, r12
5586
mov rsi, r13
5687
mov edx, (PROT_READ | PROT_EXEC)

0 commit comments

Comments
 (0)