Skip to content

Commit f04b801

Browse files
committed
v1.5: Add WinEXE support & remove BAT limitations with telnet
1 parent 4dd97d5 commit f04b801

File tree

2 files changed

+291
-90
lines changed

2 files changed

+291
-90
lines changed

README.md

+178-41
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ The result then can be transferred to the target machine (It is much easier to e
1616

1717
Upon executing exe2hex's output file, the original program is restored by using `DEBUG.exe` or PowerShell (which are pre-installed by default on Windows).
1818

19+
Can be automated by using either the in-built Telnet or WinEXE options in exe2hex to transfer the file over to the target machine, else the output can manually be inserted.
20+
1921
```Binary EXE -> ASCII Text -> *Transfer* -> Binary EXE```
2022

21-
![](https://i.imgur.com/6ZWU9OP.png)
23+
![](https://i.imgur.com/vAmiyj9.png)
2224

2325
- - -
2426

@@ -32,7 +34,7 @@ Upon executing exe2hex's output file, the original program is restored by using
3234
**Create BATch & PowerShell files**:
3335
```bash
3436
$ python3 exe2hex.py -x /usr/share/windows-binaries/sbd.exe
35-
[*] exe2hex v1.4
37+
[*] exe2hex v1.5
3638
[i] Outputting to /root/sbd.bat (BATch) and /root/sbd.cmd (PoSh)
3739
[+] Successfully wrote (BATch) /root/sbd.bat
3840
[+] Successfully wrote (PoSh) /root/sbd.cmd
@@ -42,9 +44,9 @@ $
4244
**Compress the file before creating a BATch file**:
4345
```bash
4446
$ ./exe2hex.py -x /usr/share/windows-binaries/nc.exe -b /var/www/html/nc.txt -cc
45-
[*] exe2hex v1.4
47+
[*] exe2hex v1.5
4648
[i] Attempting to clone and compress
47-
[i] Creating temporary file /tmp/tmpll55q5u9
49+
[i] Creating temporary file /tmp/tmpft9tmm_i
4850
[+] Compression (strip) was successful! (0.0% saved)
4951
[+] Compression (UPX) was successful! (50.9% saved)
5052
[+] Successfully wrote (BATch) /var/www/html/nc.txt
@@ -54,7 +56,7 @@ $
5456
**Use STDIN to create BATch & PowerShell files**:
5557
```bash
5658
$ cat /usr/share/windows-binaries/whoami.exe | python3 exe2hex.py -s -b debug.bat -p ps.cmd
57-
[*] exe2hex v1.4
59+
[*] exe2hex v1.5
5860
[i] Reading from STDIN
5961
[+] Successfully wrote (BATch) /root/debug.bat
6062
[+] Successfully wrote (PoSh) /root/ps.cmd
@@ -65,7 +67,7 @@ $
6567

6668
```bash
6769
$ python3 exe2hex.py
68-
[*] exe2hex v1.4
70+
[*] exe2hex v1.5
6971

7072
Encodes an executable binary file into ASCII text format
7173
Restore using DEBUG.exe (BATch - x86) or PowerShell (PoSh - x86/x64)
@@ -94,7 +96,8 @@ Options:
9496
-l INT Maximum HEX values per line
9597
-c Clones and compress the file before converting (-cc for higher
9698
compression)
97-
-t Create a Expect file, to automate to Telnet session.
99+
-t Create a Expect file, to automate to a Telnet session.
100+
-w Create a Expect file, to automate to a WinEXE session.
98101
-v Enable verbose mode
99102
$
100103
```
@@ -113,7 +116,7 @@ $
113116
+ Aimed at more "recent" versions of Windows.
114117
+ PowerShell was first integrated into core OS with Windows 7/Windows Server 2008 R2.
115118
+ Windows XP SP2, Windows Server 2003 & Windows Vista requires PowerShell to be pre-installed.
116-
+ This is **not** a `.ps1` file (pure PowerShell). It only calls PowerShell at the end to convert.
119+
+ This is **not** a `.ps1` file (pure PowerShell). It only calls PowerShell at the end.
117120

118121
- - -
119122

@@ -129,7 +132,8 @@ $
129132
+ The option to add prefix and suffix text to each line.
130133
+ Able to set a maximum HEX length per line.
131134
+ Can use a binary file or pipe from standard input (`STDIN`).
132-
+ Automate transfers over Telnet.
135+
+ Automate transfers over Telnet and/or WinEXE.
136+
133137

134138
Note: This is nothing new. [The core idea (using DEBUG.exe for inline file transfer) has been around since 2003](https://www.blackhat.com/presentations/bh-asia-03/bh-asia-03-chong.pdf) _(if not earlier!)_.
135139

@@ -139,23 +143,25 @@ Note: This is nothing new. [The core idea (using DEBUG.exe for inline file trans
139143

140144
When pasting a large amount of data (100+ lines) directly into a Telnet session, the results can be "unpredictable". Behaviours include lines being executed in a incorrect order or characters are just completely skipped.
141145

142-
A solution is to use "[Expect](http://expect.sourceforge.net/)" (which is an extension of [TCL](https://sourceforge.net/projects/tcl/)). Expect can be found in a most major Linux OSs repositories (`apt-get -y install expect` / `yum -y install expect` / `pacman -S expect`). Upon executing exe2hex's Telnet script, Expect will automate the Telnet login (based on the arguments used), look for a writeable folder (e.g. defaults to the system variable, `%TEMP%`) and then start inputting commands from exe2hex's .bat file, line by line one at a time. If required, the variables at the top of the Expect script can be manually edited (to use a different Telnet port, path, or command prompt).
146+
A solution is to use "[Expect](http://expect.sourceforge.net/)" (which is an extension of [TCL](https://sourceforge.net/projects/tcl/)). Expect can be found in a most major Linux OSs repositories (`apt-get -y install expect` / `yum -y install expect` / `pacman -S expect`). Upon executing exe2hex's Telnet script, Expect will automate the Telnet login (based on the arguments used), look for a writeable folder (e.g. defaults to the system variable, `%TEMP%`) and then start inputting commands from exe2hex's output file, line by line one at a time. If required, the variables at the top of the Expect script can be manually edited (to use a different Telnet port, path, or command prompt).
143147

144148
An example of exe2hex's Telnet mode can be seen below:
145149

146150
```bash
147-
root@kali:~# exe2hex -x /usr/share/windows-binaries/nc.exe -b nc.bat -t
148-
[*] exe2hex v1.4
149-
[+] Successfully wrote (BATch) /root/nc.bat
150-
[+] Successfully wrote (Expect) /root/nc-telnet
151-
root@kali:~#
152-
root@kali:~# expect /root/nc-telnet
153-
Usage: ./nc-telnet <ip> <username> <password>
154-
root@kali:~#
155-
root@kali:~# expect /root/nc-telnet 192.168.103.204 winxp 123456
156-
spawn telnet 192.168.103.204
157-
Trying 192.168.103.204...
158-
Connected to 192.168.103.204.
151+
$ python3 exe2hex.py -x /usr/share/windows-binaries/klogger.exe -b klogger.bat -t
152+
[*] exe2hex v1.5
153+
[+] Successfully wrote (BATch) /root/klogger.bat
154+
[+] Successfully wrote (Expect) /root/klogger-bat-telnet
155+
$
156+
$ expect /root/klogger-bat-telnet
157+
Usage: ./klogger-bat-telnet <ip> <username> <password>
158+
$
159+
$ /root/klogger-bat-telnet 192.168.103.148 winxp pass123
160+
161+
spawn telnet 192.168.103.148
162+
163+
Trying 192.168.103.148...
164+
Connected to 192.168.103.148.
159165
Escape character is '^]'.
160166
Welcome to Microsoft Telnet Service
161167

@@ -165,30 +171,161 @@ password:
165171
*===============================================================
166172
Welcome to Microsoft Telnet Server.
167173
*===============================================================
168-
C:\Documents and Settings\WinXP>cd %TEMP%
169-
C:\DOCUME~1\WinXP\LOCALS~1\Temp>echo 86484.0 > nc.bat
170-
86484.0 E~1\WinXP\LOCALS~1\Temp>type nc.bat
174+
C:\Documents and Settings\winxp>cd %TEMP%
175+
C:\DOCUME~1\winxp\LOCALS~1\Temp>echo 418671.0>klogger.bat
176+
418671.0E~1\winxp\LOCALS~1\Temp>type klogger.bat
177+
178+
C:\DOCUME~1\winxp\LOCALS~1\Temp>
179+
180+
[i] Writeable folder!
181+
182+
C:\DOCUME~1\winxp\LOCALS~1\Temp>del /F klogger.bat
183+
Runs Debug, a program testing and editing tool.
171184

172-
C:\DOCUME~1\WinXP\LOCALS~1\Temp>
185+
DEBUG [[drive:][path]filename [testfile-parameters]]
173186

174-
[i] Writable folder!
187+
[drive:][path]filename Specifies the file you want to test.
188+
testfile-parameters Specifies command-line information required by
189+
the file you want to test.
190+
191+
After Debug starts, type ? to display a list of debugging commands.
192+
193+
C:\DOCUME~1\winxp\LOCALS~1\Temp>C:\DOCUME~1\winxp\LOCALS~1\Temp> (Progress: 1/382)
194+
if NOT %ERRORLEVEL% == 0 echo &echo &echo &echo **** **** **** **** ****&echo *** Missing DEBUG.exe ***&echo **** **** **** **** ****&exit /b
195+
C:\DOCUME~1\winxp\LOCALS~1\Temp>C:\DOCUME~1\winxp\LOCALS~1\Temp> (Progress: 2/382)
196+
echo n klogger.0>klogger.hex
197+
C:\DOCUME~1\winxp\LOCALS~1\Temp>C:\DOCUME~1\winxp\LOCALS~1\Temp> (Progress: 3/382)
198+
echo e 0100>>klogger.hex
199+
C:\DOCUME~1\winxp\LOCALS~1\Temp>C:\DOCUME~1\winxp\LOCALS~1\Temp> (Progress: 4/382)
200+
echo 4d 5a 90 00 03 00 00 00 04 00 00 00 ff ff 00 00 b8 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 d0 00 00 00 0e 1f ba 0e 00 b4 09 cd 21 b8 01 4c cd 21 54 68 69 73 20 70 72 6f 67 72 61 6d 20 63 61 6e 6e 6f 74 20 62 65 20 72 75 6e 20 69 6e 20 44 4f 53 20 6d 6f 64 65 2e 0d 0d 0a 24 00 00 00 00 00 00 00>>klogger.hex
201+
C:\DOCUME~1\winxp\LOCALS~1\Temp>C:\DOCUME~1\winxp\LOCALS~1\Temp> (Progress: 5/382)
202+
echo e 0180>>klogger.hex
175203

176-
C:\DOCUME~1\WinXP\LOCALS~1\Temp>del /F nc.bat
177-
C:\DOCUME~1\WinXP\LOCALS~1\Temp>echo n nc.0>nc.hex
178-
C:\DOCUME~1\WinXP\LOCALS~1\Temp>C:\DOCUME~1\WinXP\LOCALS~1\Temp> (Progress: 1/938)
179-
echo e 0100>>nc.hex
180204

181205
...SNIP...
182206

183-
C:\DOCUME~1\WinXP\LOCALS~1\Temp>C:\DOCUME~1\WinXP\LOCALS~1\Temp> (Progress: 934/938)
184-
move /Y nc.0 nc.exe
185-
C:\DOCUME~1\WinXP\LOCALS~1\Temp>C:\DOCUME~1\WinXP\LOCALS~1\Temp> (Progress: 935/938)
186-
echo. >nc.hex
187-
C:\DOCUME~1\WinXP\LOCALS~1\Temp>C:\DOCUME~1\WinXP\LOCALS~1\Temp> (Progress: 936/938)
188-
C:\DOCUME~1\WinXP\LOCALS~1\Temp\nc.hex
189-
The process cannot access the file because it is being used by another process.
207+
C:\DOCUME~1\winxp\LOCALS~1\Temp>C:\DOCUME~1\winxp\LOCALS~1\Temp> (Progress: 376/382)
208+
move /Y klogger.0 klogger.exe
209+
C:\DOCUME~1\winxp\LOCALS~1\Temp>C:\DOCUME~1\winxp\LOCALS~1\Temp> (Progress: 377/382)
210+
echo. >klogger.hex
211+
C:\DOCUME~1\winxp\LOCALS~1\Temp>C:\DOCUME~1\winxp\LOCALS~1\Temp> (Progress: 378/382)
212+
del /F /Q klogger.hex klogger.0
213+
C:\DOCUME~1\winxp\LOCALS~1\Temp>C:\DOCUME~1\winxp\LOCALS~1\Temp> (Progress: 379/382)
214+
Volume in drive C has no label.
215+
Volume Serial Number is 002C-A3B2
216+
217+
Directory of C:\DOCUME~1\winxp\LOCALS~1\Temp
218+
219+
06/09/2017 10:19 AM 23,552 klogger.exe
220+
1 File(s) 23,552 bytes
221+
0 Dir(s) 40,501,571,584 bytes free
222+
223+
C:\DOCUME~1\winxp\LOCALS~1\Temp>C:\DOCUME~1\winxp\LOCALS~1\Temp> (Progress: 380/382)
224+
225+
226+
[i] Done
227+
228+
C:\DOCUME~1\winxp\LOCALS~1\Temp>
229+
```
230+
231+
232+
### WinEXE
233+
234+
Like the Telnet mode (`-t`), exe2hex can automate using winexe to transfer files across, inline, using expect:
235+
236+
```bash
237+
$ python3 exe2hex.py -x /usr/share/windows-binaries/mbenum/mbenum.exe -p mbenum.cmd -w
238+
[*] exe2hex v1.5
239+
[+] Successfully wrote (PoSh) /root/mbenum.cmd
240+
[+] Successfully wrote (Expect) /root/mbenum-posh-winexe
241+
$
242+
$ expect /root/mbenum-posh-winexe
243+
Usage: ./mbenum-posh-winexe <ip> <username> <password>
244+
$
245+
$ ./mbenum-posh-winexe 192.168.103.147 win7 123456789
246+
247+
spawn winexe -U win7%123456789 //192.168.103.147 cmd.exe
248+
249+
Microsoft Windows [Version 6.1.7601]
250+
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
251+
252+
C:\Windows\system32>cd %TEMP%
253+
cd %TEMP%
254+
255+
C:\Windows\Temp>echo 656082.0>mbenum.cmd
256+
echo 656082.0>mbenum.cmd
257+
258+
C:\Windows\Temp>type mbenum.cmd
259+
type mbenum.cmd
260+
656082.0
261+
262+
[i] Writeable folder!
263+
264+
265+
C:\Windows\Temp>del /F mbenum.cmd
266+
del /F mbenum.cmd
267+
268+
C:\Windows\Temp>echo|set /p="">mbenum.hex
269+
270+
echo|set /p="">mbenum.hex
190271

191-
C:\DOCUME~1\WinXP\LOCALS~1\Temp>C:\DOCUME~1\WinXP\LOCALS~1\Temp> (Progress: 937/938)
272+
C:\Windows\Temp> (Progress: 1/388)
192273

193-
C:\DOCUME~1\WinXP\LOCALS~1\Temp>start /wait /b nc.exe
194-
```
274+
C:\Windows\Temp>echo|set /p="4d5a90000300000004000000ffff0000b800000000000000400000000000000000000000000000000000000000000000000000000000000000000000e80000000e1fba0e00b409cd21b8014ccd21546869732070726f6772616d2063616e6e6f742062652072756e20696e20444f53206d6f64652e0d0d0a2400000000000000">>mbenum.hex
275+
276+
echo|set /p="4d5a90000300000004000000ffff0000b800000000000000400000000000000000000000000000000000000000000000000000000000000000000000e80000000e1fba0e00b409cd21b8014ccd21546869732070726f6772616d2063616e6e6f742062652072756e20696e20444f53206d6f64652e0d0d0a2400000000000000">>mbenum.hex
277+
278+
C:\Windows\Temp> (Progress: 2/388)
279+
280+
C:\Windows\Temp>echo|set /p="fa28c48dbe49aadebe49aadebe49aadec555a6debf49aaded156a1debf49aade3d55a4deae49aaded156a0de8b49aade3d41f7debb49aadebe49abde9049aadeb86aa0debc49aade52696368be49aade000000000000000000000000000000000000000000000000504500004c01030001ea7f3f0000000000000000e0000f01">>mbenum.hex
281+
282+
...SNIP...
283+
284+
C:\Windows\Temp> (Progress: 385/388)
285+
286+
C:\Windows\Temp>powershell -Command "$h=Get-Content -readcount 0 -path './mbenum.hex';$l=$h[0].length;$b=New-Object byte[] ($l/2);$x=0;for ($i=0;$i -le $l-1;$i+=2){$b[$x]=[byte]::Parse($h[0].Substring($i,2),[System.Globalization.NumberStyles]::HexNumber);$x+=1};set-content -encoding byte 'mbenum.exe' -value $b;Remove-Item -force mbenum.hex;Get-ChildItem mbenum.exe;"
287+
288+
powershell -Command "$h=Get-Content -readcount 0 -path './mbenum.hex';$l=$h[0].length;$b=New-Object byte[] ($l/2);$x=0;for ($i=0;$i -le $l-1;$i+=2){$b[$x]=[byte]::Parse($h[0].Substring($i,2),[System.Globalization.NumberStyles]::HexNumber);$x+=1};set-content -encoding byte 'mbenum.exe' -value $b;Remove-Item -force mbenum.hex;Get-ChildItem mbenum.exe;"
289+
290+
291+
(Progress: 386/388)ows\Temp
292+
293+
294+
[i] Done
295+
296+
297+
298+
Mode LastWriteTime Length Name
299+
---- ------------- ------ ----
300+
-a--- 09/06/2017 10:21 49152 mbenum.exe
301+
302+
303+
304+
305+
C:\Windows\Temp>
306+
```
307+
308+
_NOTE: May need to press enter to get a prompt back at the end._
309+
310+
- - -
311+
312+
## Install
313+
314+
Just exe2hex just requires [Python 3](https://www.python.org/) to function ([Expect](http://expect.sourceforge.net/) is optional for Telnet and WinEXE functions).
315+
316+
Simply add exe2hex a folder in your `$PATH` variable:
317+
318+
```bash
319+
$ echo $PATH
320+
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
321+
$ curl https://raw.githubusercontent.com/g0tmi1k/exe2hex/master/exe2hex.py > /usr/local/bin/exe2hex
322+
$ chmod 0755 /usr/local/bin/exe2hex
323+
```
324+
325+
### Kali-Linux
326+
327+
exe2hex is already [packaged](https://pkg.kali.org/pkg/exe2hexbat) in [Kali Rolling](https://www.kali.org/), so all you have to-do is:
328+
329+
```bash
330+
root@kali:~# apt install -y exe2hexbat
331+
```

0 commit comments

Comments
 (0)