Skip to content

Commit b59aa6b

Browse files
committed
Stripped trailing NOPs and modularized the script.
1 parent 9735b81 commit b59aa6b

1 file changed

Lines changed: 104 additions & 79 deletions

File tree

mkhex.sh

Lines changed: 104 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,22 @@
66
#
77

88

9-
if [[ $# -lt 1 || (! -f "$1") ]]
10-
then
11-
printf '%s\n' 'Invalid Arugment!'
12-
cat <<EOF
13-
Usage: $0 file -e|--escape -r|--raw -c|--c -l|--linux -w|--windows
14-
EOF
15-
exit 233
16-
fi
17-
18-
case "$2" in
19-
-e|--escape)
20-
flag=0;;
21-
-r|--raw)
22-
flag=1;;
23-
-c|--c)
24-
flag=2;;
25-
-l|--linux)
26-
flag=3;;
27-
-w|--windows)
28-
flag=4;;
29-
*)
30-
flag=0;;
31-
esac
32-
33-
34-
regex='\t([0-9a-f]{2}\s)+'
35-
36-
hex=$(x86_64-w64-mingw32-objdump -d "$1" | grep -oP $regex)
37-
38-
if [[ $flag -ge 2 ]]
39-
then
40-
cat <<EOF
9+
main()
10+
{
11+
flag=$(parse_args "$@")
12+
if [[ $? -eq 233 ]]
13+
then
14+
show_help
15+
exit 233
16+
fi
17+
18+
regex='\t([0-9a-f]{2}\s)+'
19+
hex=$(x86_64-w64-mingw32-objdump -d "$1" | grep -oP $regex)
20+
hex=$(printf '%s' $hex | sed -r 's/(90)*$//g')
21+
22+
if [[ $flag -ge 2 ]]
23+
then
24+
cat <<EOF
4125
/*
4226
* This file was automatically generated by mkhex.sh,
4327
* which, together with the complete
@@ -52,75 +36,56 @@ then
5236
5337
# include <stdlib.h>
5438
# include <stdio.h>
55-
56-
# define COUNTOF(a) (sizeof(a) / sizeof(a[0]))
57-
5839
EOF
59-
if [[ $flag -eq 3 ]]
60-
then
61-
cat <<EOF
40+
if [[ $flag -eq 3 ]]
41+
then
42+
cat <<EOF
6243
# include <stdint.h>
6344
6445
# include <sys/mman.h>
6546
EOF
66-
fi
47+
fi
48+
49+
if [[ $flag -eq 4 ]]
50+
then
51+
cat <<EOF
6752
68-
if [[ $flag -eq 4 ]]
69-
then
70-
cat <<EOF
7153
# include <windows.h>
7254
EOF
73-
fi
74-
cat <<EOF
55+
fi
56+
cat <<EOF
57+
58+
# define COUNTOF(a) (sizeof(a) / sizeof(a[0]))
7559
7660
7761
int
7862
main(void)
7963
{
8064
EOF
81-
printf ' char shellcode[] = "'
82-
fi
83-
84-
c=0
85-
for i in $hex
86-
do
87-
if [[ $flag -ge 2 ]] && [[ $c -eq 10 ]]
88-
then
89-
c=0
90-
printf '"\n "'
91-
fi
92-
93-
if [[ $flag -eq 1 ]]
94-
then
95-
fmt='%s'
96-
else
97-
fmt='\\x%s'
65+
printf ' char shellcode[] = "'
9866
fi
9967

100-
printf $fmt "$i"
68+
format_hex $hex $flag
10169

102-
c=$(($c + 1))
103-
done
104-
105-
if [[ $flag -ge 2 ]]
106-
then
107-
printf '";\n\n'
108-
if [[ $flag -eq 3 ]]
70+
if [[ $flag -ge 2 ]]
10971
then
110-
cat <<EOF
111-
int failure = mprotect((void *)((uintptr_t)shellcode & ~4095), 4096,
112-
PROT_READ | PROT_WRITE | PROT_EXEC);
72+
printf '";\n\n'
73+
if [[ $flag -eq 3 ]]
74+
then
75+
cat <<EOF
76+
int failure = mprotect((void *)((uintptr_t)shellcode & ~4095),
77+
4096, PROT_READ | PROT_WRITE | PROT_EXEC);
11378
11479
if (failure) {
11580
printf ("mprotect\n");
11681
return EXIT_FAILURE;
11782
}
11883
11984
EOF
120-
fi
121-
if [[ $flag -eq 4 ]]
122-
then
123-
cat <<EOF
85+
fi
86+
if [[ $flag -eq 4 ]]
87+
then
88+
cat <<EOF
12489
DWORD why_must_this_variable;
12590
BOOL success = VirtualProtect(shellcode, strlen(shellcode),
12691
PAGE_EXECUTE_READWRITE, &why_must_this_variable);
@@ -131,13 +96,73 @@ EOF
13196
}
13297
13398
EOF
134-
fi
135-
cat <<EOF
99+
fi
100+
cat <<EOF
136101
printf("strlen(shellcode)=%d\n", COUNTOF(shellcode));
137102
138103
((void (*)(void))shellcode)();
139104
140105
return EXIT_SUCCESS;
141106
}
142107
EOF
143-
fi
108+
fi
109+
}
110+
111+
format_hex()
112+
{
113+
local i=0
114+
local c=0
115+
if [[ $2 -eq 1 ]]
116+
then
117+
fmt='%s'
118+
else
119+
fmt='\\x%s'
120+
fi
121+
while [[ $i -lt ${#1} ]]
122+
do
123+
if [[ $2 -ge 2 && $c -eq 10 ]]
124+
then
125+
c=0
126+
printf '"\n "'
127+
fi
128+
129+
printf $fmt ${1:$i:2}
130+
i=$(($i+2))
131+
c=$(($c+1))
132+
done
133+
}
134+
135+
136+
show_help()
137+
{
138+
cat <<EOF
139+
Synopsis: $0 file -e|--escape -r|--raw -c|--c -l|--linux -w|--windows
140+
EOF
141+
}
142+
143+
144+
parse_args()
145+
{
146+
if [[ $# -lt 1 || (! -f "$1") ]]
147+
then
148+
return 233
149+
fi
150+
case "$2" in
151+
-e|--escape)
152+
flag=0;;
153+
-r|--raw)
154+
flag=1;;
155+
-c|--c)
156+
flag=2;;
157+
-l|--linux)
158+
flag=3;;
159+
-w|--windows)
160+
flag=4;;
161+
*)
162+
flag=0;;
163+
esac
164+
printf '%s' $flag
165+
}
166+
167+
168+
main "$@"

0 commit comments

Comments
 (0)