https://devblogs.microsoft.com/oldnewthing/20230911-00/?p=108749 Skip to main content [RE1Mu3b] Microsoft The Old New Thing The Old New Thing The Old New Thing * Home * DevBlogs * Developer + Visual Studio + Visual Studio Code + Visual Studio for Mac + DevOps + Windows Developer + Developer support + CSE Developer + Engineering@Microsoft + Azure SDK + IoT + Command Line + Perf and Diagnostics + Dr. International + Notification Hubs + Math in Office + React Native * Technology + DirectX + PIX + Semantic Kernel + SurfaceDuo + Startups + Sustainable Engineering + Windows AI Platform * Languages + C++ + C# + F# + Visual Basic + TypeScript + PowerShell Community + PowerShell Team + Python + Q# + JavaScript + Java + Java Blog in Chinese * .NET + All .NET posts + .NET MAUI + ASP.NET Core + Blazor + Entity Framework + ML.NET + NuGet + Servicing + Xamarin + .NET Blog in Chinese * Platform Development + #ifdef Windows + Azure Depth Platform + Azure Government + Azure VM Runtime Team + Bing Dev Center + Microsoft Edge Dev + Microsoft Azure + Microsoft 365 Developer + Microsoft Entra Identity Developer Blog + Old New Thing + Power Platform + Windows MIDI and Music dev + Windows Search Platform * Data Development + Azure Cosmos DB + Azure Data Studio + Azure SQL Database + OData + Revolutions R + SQL Server Data Tools * More [ ] Search Search * No results Cancel Email Subscriptions are here! Get notified in your email when a new post is published to this blog Subscribe Close Any sufficiently advanced uninstaller is indistinguishable from malware [png] Raymond Chen September 11th, 202318 5 There was a spike in Explorer crashes that resulted in the instruction pointer out in the middle of nowhere. 0:000> r eax=00000001 ebx=008bf8aa ecx=77231cf3 edx=00000000 esi=008bf680 edi=008bf8a8 eip=7077c100 esp=008bf664 ebp=008bf678 iopl=0 nv up ei pl zr na pe nc cs=0023 ss=002b ds=002b es=002b fs=0053 gs=002b efl=00010246 7077c100 ?? ??? Maybe the return address tells us something. 0:000> u poi esp 008bf6d4 test eax,eax 008bf6d6 je 008bf6b9 008bf6d8 xor edi,edi 008bf6da cmp dword ptr [esi+430h],edi It's strange that we're executing from someplace that has no name. If you look closely, you'll see that we are executing code from the stack: esp is 008bf664, so the code that went haywire is on the stack. Who executes code from the stack? Malware, that's who. Let's see what this malware is trying to do. Disassembling around the last known good code address gives us this: 008bf6c4 call dword ptr [esi+214h] 008bf6ca inc dword ptr [ebp+8] 008bf6cd push edi 008bf6ce call dword ptr [esi+210h] ; this called into space 008bf6d4 test eax,eax 008bf6d6 je 008bf6b9 008bf6d8 xor edi,edi 008bf6da cmp dword ptr [esi+430h],edi 008bf6e0 je 008bf70d It looks like the payload stored function pointers at esi+210 and esi+214. Let's see what's there. This is probably where the payload stashed all its call targets. 0:000> dps @esi+200 008bf880 1475ff71 008bf884 00000004 008bf888 76daecf0 kernel32!WaitForSingleObject 008bf88c 76daeb00 kernel32!CloseHandle 008bf890 7077c100 008bf894 76dada90 kernel32!SleepStub 008bf898 76db6a40 kernel32!ExitProcessImplementation 008bf89c 76daf140 kernel32!RemoveDirectoryW 008bf8a0 76da6e30 kernel32!GetLastErrorStub 008bf8a4 770d53f0 user32!ExitWindowsEx 008bf8a8 003a0043 008bf8ac 0050005c 008bf8b0 006f0072 008bf8b4 00720067 008bf8b8 006d0061 Yup, there's a payload of function pointers here. It looks like this malware is going to wait for something, and then exit the process, or remove a directory, or exit Windows. Those bytes after user32! ExitWindowsEx look like a Unicode string, so let's dump them as a string: 0:000> du 008bf8a8 008bf8a8 "C:\Program Files\Contoso\contoso_update.exe" Wait, what? It is trying to mess around with Contoso's auto-updater? Let's take a look at more of the malware payload. Maybe we can figure out what it's doing. It looks like it's using esi as its base of operations, so let's disassemble from esi. 008bf684 push ebp ; build stack frame 008bf685 mov ebp,esp 008bf687 push ebx ; save ebx 008bf688 push esi ; save esi 008bf689 mov esi,dword ptr [ebp+8] ; parameter 008bf68c push edi ; save edi 008bf68d push 0FFFFFFFFh ; INFINITE 008bf68f push dword ptr [esi+204h] ; data->hProcess 008bf695 lea ebx,[esi+22Ah] ; address of path + 2 008bf69b call dword ptr [esi+208h] ; WaitForSingleObject 008bf6a1 push dword ptr [esi+204h] ; data->hProcess 008bf6a7 call dword ptr [esi+20Ch] ; CloseHandle 008bf6ad and dword ptr [ebp+8],0 ; count = 0 008bf6b1 lea edi,[esi+228h] ; address of path 008bf6b7 jmp 008bf6cd ; enter loop 008bf6b9 cmp dword ptr [ebp+8],28h ; waited too long? 008bf6bd jge 008bf6d8 ; then stop 008bf6bf push 1F4h ; 500 008bf6c4 call dword ptr [esi+214h] ; Sleep 008bf6ca inc dword ptr [ebp+8] ; count++ 008bf6cd push edi ; path 008bf6ce call dword ptr [esi+210h] ; DeleteFile 008bf6d4 test eax,eax ; Q: Did it delete? 008bf6d6 je 008bf6b9 ; N: Loop and try again 008bf6d8 xor edi,edi 008bf6da cmp dword ptr [esi+430h],edi ; data->fRemoveDirectory? 008bf6e0 je 008bf70d ; N: Skip 008bf6e2 jmp 008bf6f0 ; Enter loop for trimming file name 008bf6e4 cmp ax,5Ch ; Q: Backslash? 008bf6e8 jne 008bf6ed ; N: Ignore 008bf6ea mov dword ptr [ebp+8],ebx ; Remember location of last backslash 008bf6ed add ebx,2 ; Move to character 008bf6f0 movzx eax,word ptr [ebx] ; Fetch next character 008bf6f3 cmp ax,di ; Q: End of string? 008bf6f6 jne 008bf6e4 ; N: Keep looking 008bf6f8 mov ecx,dword ptr [ebp+8] ; Get location of last backslash 008bf6fb xor eax,eax ; eax = 0 008bf6fd mov word ptr [ecx],ax ; Terminate string at last backslash 008bf700 lea eax,[esi+228h] ; Get path (now without file name) 008bf706 push eax ; Push address 008bf707 call dword ptr [esi+21Ch] ; RemoveDirectory 008bf70d cmp dword ptr [esi+434h],edi ; data->fExitWindows? 008bf713 je 008bf71e ; N: Skip 008bf715 push edi ; dwReason = 0 008bf716 push 12h ; EWX_REBOOT | EWX_FORCEIFHUNG 008bf718 call dword ptr [esi+224h] ; ExitWindowsEx 008bf71e push edi ; dwExitCode = 0 008bf71f call dword ptr [esi+218h] ; ExitProcess 008bf725 pop edi 008bf726 pop esi 008bf727 pop ebx 008bf728 pop ebp 008bf729 ret ; This code appears to be unused 008bf72a push ebp 008bf72b mov ebp,esp 008bf72d push esi 008bf72e mov esi,dword ptr [ebp+10h] 008bf731 test esi,esi 008bf733 jle 008bf746 ... Reverse-compiling back to C, we have struct Data { char code[0x0204]; HANDLE hProcess; DWORD (CALLBACK* WaitForSingleObject)(HANDLE, DWORD); BOOL (CALLBACK* CloseHandle)(HANDLE); DWORD (CALLBACK* MysteryFunction)(PCWSTR); void (CALLBACK* Sleep)(DWORD); void (CALLBACK* ExitProcess)(UINT); BOOL (CALLBACK* RemoveDirectoryW)(PCWSTR); DWORD (CALLBACK* GetLastError)(); BOOL (CALLBACK* ExitWindowsEx)(UINT, DWORD); wchar_t path[MAX_PATH]; BOOL fRemoveDirectory; BOOL fExitWindows; }; void Payload(Data* data) { // Wait for the process to exit data->WaitForSingleObject(data->hProcess, INFINITE); data->CloseHandle(data->hProcess); // Try up to 20 seconds to do something with the file for (int count = 0; !data->MysteryFunction(data->path) && count < 40; count++) { Sleep(500); } if (data->fRemoveDirectory) { PWSTR p = &data->path[1]; PWSTR lastBackslash = p; while (*p != L'\0') { if (*p == L'\\') lastBackslash = p; p++; } *lastBackslash = L'\0'; RemoveDirectoryW(data->path); } if (data->fExitWindows) { ExitWindowsEx(EWX_REBOOT | EWX_FORCEIFHUNG, 0); } } Aha, this isn't malware. This is an uninstaller! The mystery function is almost certainly DeleteFileW. It's waiting for the main uninstaller to exit, so it can delete the binary. There is a page on CodeProject that shows how to write a self-deleting file, and it seems that multiple companies have decided to use that code to implement their own uninstallers. (Whether they follow the licensing terms for that code I do not know.) Okay, so why did we crash? What went wrong with DeleteFileW? According to the dump file, the spot where DeleteFileW was supposed to be instead holds 7077c100. This is a function pointer into some mystery DLL that isn't loaded. How did that happen? My guess is that the DeleteFileW function was detoured in the Contoso uninstaller. When the uninstaller tried to built its table of useful functions, it ended up getting not the address of DeleteFileW but the address of a detour. It then tried to call that detour from its payload, but since the detour is not installed in Explorer (or if it is, the detour is in some other location), it ended up calling into space. Neither code injection nor detouring is officially supported. I can't tell who did the detouring. Maybe somebody added a detour to the uninstaller, unaware that the uninstaller is going to inject a call to the detour into Explorer. Or maybe the detour was injected by anti-malware software. Or maybe the detour was injected by Windows' own application compatibility layer. Whatever the reason, the result was a crash in Explorer. Which means that people like me spend a lot of time studying these crashes to figure out what is going on, only to conclude that they were caused by other people abusing the system. If you want to create a self-deleting binary, please don't use code injection into somebody else's process. Here's a way to delete a binary and leave no trace: Create a temporary file called cleanup.js that goes like this: var fso = new ActiveXObject("Scripting.FileSystemObject"); fso.DeleteFile("C:\\Users\\Name\\AppData\\Local\\Temp\\cleanup.js"); var path = "C:\\Program Files\\Contoso\\contoso_update.exe"; for (var count = 0; fso.FileExists(path) && count < 40; count++) { try { fso.DeleteFile(path); break; } catch (e) { } WSH.Sleep(500); } This script deletes itself and then tries to delete contoso_update.exe for 20 seconds. Run it with wscript cleanup.js and let it do its thing. No code injection, no detours, all documented. [png] Raymond Chen Follow Tagged Code Read next How do I perform a case-insensitive comparison of two strings in the Deseret script? It sort of depends on why you're comparing them. [png]Raymond Chen September 13, 2023 0 comment 18 comments Leave a commentCancel reply Log in to join the discussion. * [png] skSdnW September 11, 2023 8:40 am 1 collapse this comment copy link to this comment Vbscript is on its way to becoming a not-installed-by-default optional component, does this apply to Jscript as well? FILE_DISPOSITION_POSIX_SEMANTICS almost helps. Why not just give people what they want/need? BOOL WINAPI DeleteSelf(BOOL TryDeleteDirectory); Log in to Vote or Reply + [png] Jan Ringos September 11, 2023 2:40 pm 0 collapse this comment copy link to this comment I usually move myself (as in the uninstaller.exe) to Temp, with randomized filename, and schedule deletion on Restart. Log in to Vote or Reply * [png] Alex Guo September 11, 2023 8:51 am 1 collapse this comment copy link to this comment Ah uninstallers... Had some fun with those. When our product's uninstaller sees an undeletable file (possibly a DLL loaded in another process), it uses the MOVEFILE_DELAY_UNTIL_REBOOT flag to mark it for deletion, and warns the user "Please reboot as soon as possible to remove the remaining files." However, some user uninstall our product, just to be able to reinstall it later, to the same location. And of course they ignored the warning. Once they reinstalled it, everything works, until a reboot. Imagine figuring that out without knowing the uninstaller trick. The install log says everything is OK, and the software log initially shows nothing wrong, but all of a sudden, some files just disappear into thin air. We finally managed to fix it by first renaming that undeletable file to "something.del", and then marking that for deletion. We also added a registry check in our installer. If it sees a PendingFileRenameOperations entry located in the install destination, but without the "del" extension, it will abort. (We are also experimenting with MSIX! However it changes everything a bit more than we currently can handle.) Log in to Vote or Reply * [png] Michael Taylor September 11, 2023 8:55 am 0 collapse this comment copy link to this comment In my experience most IT groups block VBS on machines because of security issues. Personally I always found it easier to just start a new batch script (or perhaps PS these days) that polled the desired file into it could be deleted. But that is only for cases where I needed the file deleted ASAP. I think the preferred approach is to use `PendingFileRenameOperations` to schedule the file for removal on restart. If the user opts to remove the file before a reboot then that is up to them but it will eventually get cleaned up and works as the system was intending. It is also not reliant on script settings. Log in to Vote or Reply + [png] skSdnW September 11, 2023 9:05 am 1 collapse this comment copy link to this comment PendingFileRenameOperations (MoveFileEx) only works for administrators since it needs to write to HKLM, not usable for things installed in FOLDERID_UserProgramFiles. Log in to Vote or Reply o [png] Michael Taylor September 11, 2023 2:54 pm 0 collapse this comment copy link to this comment As does most other approaches that need to clean up files. Since there was no contextual information about user privileges the solution I used to use (pre per-user installs) is sufficient. Note that in modern IT systems you also cannot run scripts of any sort without being an admin (and even then) and JS doesn't work outside the sandbox unless you have Java installed (which, again most computers won't for security reasons). So the solution you are recommending doesn't work for a normal user either. Log in to Vote or Reply # [png] Raymond ChenMicrosoft employee September 11, 2023 3:00 pm 2 collapse this comment copy link to this comment Not sure what you mean by "JS doesn't work outside the sandbox unless you have Java installed." The wscript program happily runs JS outside any sandbox. Go ahead and create a FileSystemObject like the one in the article. (And Java is completely unrelated to Javascript.) Log in to Vote or Reply * [png] Brian Friesen September 11, 2023 11:01 am 0 collapse this comment copy link to this comment Could a process mark itself for deletion using SetFileInformationByHandle() with FileDispositionInfo? I have not tried this, but if it worked when the last handle for the process was closed the system would delete the file. The same as if the file were created/opened with FILE_DELETE_ON_CLOSE. I don't think this requires special permissions either, as long as you could delete the file if it wasn't busy then you should be able to mark it for deletion on close when it is busy. Log in to Vote or Reply + [png] skSdnW September 11, 2023 11:38 am 0 collapse this comment copy link to this comment If FileDispositionInfo or FILE_DELETE_ON_CLOSE worked on a running exe file then neither this blog post nor the injection code would exist in the first place. CreateProcess does not open the file with FILE_SHARE_DELETE to stop you from doing this in the name of compatibility (with DOS?, Windows 3.x?). Log in to Vote or Reply o [png] LB September 11, 2023 8:37 pm 0 collapse this comment copy link to this comment You can create an executable file with the flag set to delete it on close, and then before closing the last handle to it, you can execute it. Then you can exit your own process, and that executable you created will be deleted once it exits too since that will close the last handle. Log in to Vote or Reply * [png] Georg Rottensteiner September 11, 2023 10:35 pm 0 collapse this comment copy link to this comment Noone? I've always used the age old method of the self deleting batch file to finally remove the uninstaller and then itself. Write a batch file to the temp folder, do your work and as last line a del on itself, without(!) line break. Voila, everything gone, as intended. Works a treat since Win 3.x. Log in to Vote or Reply * [png] Richard Deeming September 12, 2023 1:05 am 0 collapse this comment copy link to this comment Who needs JavaScript when you can have a self-deleting batch file? https://stackoverflow.com/a/20333575/124386 (goto) 2>nul & del "%~f0" Log in to Vote or Reply * [png] Marcus Andre September 12, 2023 1:42 am 0 collapse this comment copy link to this comment Insane way for simple tasks, lol. Usually in my case I just call the cmd with an safe timeout to delete the remaining files and never got trouble. Log in to Vote or Reply * [png] Shawn Van Ness September 12, 2023 8:16 am 0 collapse this comment copy link to this comment Is there really no registry key or API to call, to have Windows cleanup the last bits, after custom uninstall process completes? I suppose I always assumed there was.. but I've never implemented an uninstaller, obvs. Log in to Vote or Reply + [png] Dan Bugglin September 13, 2023 12:25 am 0 collapse this comment copy link to this comment MoveFileEx can, but it defers to next boot though. This is mostly useful for removing or replacing files that are in use at the time you're trying to remove/replace them . Many installers or uninstallers use it for that purpose (any installer that asks you to reboot usually does so because it has done this, especially if you find the app in question does not work until you actually reboot). Log in to Vote or Reply * [png] Dan Bugglin September 13, 2023 12:23 am 0 collapse this comment copy link to this comment If a jscript dependency is somehow a concern, you can fairly easily rewrite the script in batch. Then all you need is cmd.exe which everyone has. A bit of IF EXISTS, a bit of DEL, a GOTO loop and label, and finally run cmd.exe with no window to keep things nice and tidy. The only bit that could be trouble is there's no built in delay function. But it's fairly easy to find a command that implements a delay itself and abuse the hell out of it for our own purposes. ping.exe will work just fine. You just need to craft a command you know will always fail via timeout, and do a single ping with your desired delay as the timeout. Turns out localhost subnet's multicast IP address works fine for this. Multicast addresses are reserved and can't actually point to a single device. And apparently ICMP doesn't multicast, I guess. ping -n 1 -w [delay in milliseconds] 127.255.255.255 Another completely unrelated approach is to use MoveFileEx with no new file name and MOVEFILE_DELAY_UNTIL_REBOOT flag, to indicate the file should be deleted next boot (If you're patient.) Log in to Vote or Reply + [png] Chris Iverson September 13, 2023 10:53 am 0 collapse this comment copy link to this comment "The only bit that could be trouble is there's no built in delay function." No built-in delay function? Does the TIMEOUT command not work, for some reason? Log in to Vote or Reply * [png] Youfu Zhang September 13, 2023 2:59 am 0 collapse this comment copy link to this comment I don't like the idea of invoking wscript from uninstaller. Windows Script Host may be disabled on some hardened PC. Under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows Script Host\ Settings, create a new DWORD value named "Enabled" and set the value data to "0". A pop up will show that "Windows Script Host access is disabled on this computer. Contact your administrator for details." Log in to Vote or Reply Archive September 2023 August 2023 July 2023 June 2023 May 2023 April 2023 March 2023 February 2023 January 2023 December 2022 November 2022 October 2022 September 2022 August 2022 July 2022 June 2022 May 2022 April 2022 March 2022 February 2022 January 2022 December 2021 November 2021 October 2021 September 2021 August 2021 July 2021 June 2021 May 2021 April 2021 March 2021 February 2021 January 2021 December 2020 November 2020 October 2020 September 2020 August 2020 July 2020 June 2020 May 2020 April 2020 March 2020 February 2020 January 2020 December 2019 November 2019 October 2019 September 2019 August 2019 July 2019 June 2019 May 2019 April 2019 March 2019 February 2019 January 2019 December 2018 November 2018 October 2018 September 2018 August 2018 July 2018 June 2018 May 2018 April 2018 March 2018 February 2018 January 2018 December 2017 November 2017 October 2017 September 2017 August 2017 July 2017 June 2017 May 2017 April 2017 March 2017 February 2017 January 2017 December 2016 November 2016 October 2016 September 2016 August 2016 July 2016 June 2016 May 2016 April 2016 March 2016 February 2016 January 2016 December 2015 November 2015 October 2015 September 2015 August 2015 July 2015 June 2015 May 2015 April 2015 March 2015 February 2015 January 2015 December 2014 November 2014 October 2014 September 2014 August 2014 July 2014 June 2014 May 2014 April 2014 March 2014 February 2014 January 2014 December 2013 November 2013 October 2013 September 2013 August 2013 July 2013 June 2013 May 2013 April 2013 March 2013 February 2013 January 2013 December 2012 November 2012 October 2012 September 2012 August 2012 July 2012 June 2012 May 2012 April 2012 March 2012 February 2012 January 2012 December 2011 November 2011 October 2011 September 2011 August 2011 July 2011 June 2011 May 2011 April 2011 March 2011 February 2011 January 2011 December 2010 November 2010 October 2010 September 2010 August 2010 July 2010 June 2010 May 2010 April 2010 March 2010 February 2010 January 2010 December 2009 November 2009 October 2009 September 2009 August 2009 July 2009 June 2009 May 2009 April 2009 March 2009 February 2009 January 2009 December 2008 November 2008 October 2008 September 2008 August 2008 July 2008 June 2008 May 2008 April 2008 March 2008 February 2008 January 2008 December 2007 November 2007 October 2007 September 2007 August 2007 July 2007 June 2007 May 2007 April 2007 March 2007 February 2007 January 2007 December 2006 November 2006 October 2006 September 2006 August 2006 July 2006 June 2006 May 2006 April 2006 March 2006 February 2006 January 2006 December 2005 November 2005 October 2005 September 2005 August 2005 July 2005 June 2005 May 2005 April 2005 March 2005 February 2005 January 2005 December 2004 November 2004 October 2004 September 2004 August 2004 July 2004 June 2004 May 2004 April 2004 March 2004 February 2004 January 2004 December 2003 November 2003 October 2003 September 2003 August 2003 July 2003 Relevant Links I wrote a book Ground rules Disclaimers and such My necktie's Twitter Categories Code History Tips/Support Other Non-Computer Stay informed [ ] [Subscribe] By subscribing you agree to our Terms of Use and Privacy Policy Share on Social media * * * Login Theme * light-theme-iconLight * dark-theme-iconDark Insert/edit link Close Enter the destination URL URL [ ] Link Text [ ] [ ] Open link in a new tab Or link to existing content Search [ ] No search term specified. Showing recent items. Search or use up and down arrow keys to select an item. Cancel [Add Link] Code Block x Paste your code snippet [ ] Cancel Ok Feedback usabilla icon What's new * Surface Pro 9 * Surface Laptop 5 * Surface Studio 2+ * Surface Laptop Go 2 * Surface Laptop Studio * Surface Go 3 * Microsoft 365 * Windows 11 apps Microsoft Store * Account profile * Download Center * Microsoft Store support * Returns * Order tracking * Trade-in for Cash * Microsoft Store Promise * Flexible Payments Education * Microsoft in education * Devices for education * Microsoft Teams for Education * Microsoft 365 Education * How to buy for your school * Educator training and development * Deals for students and parents * Azure for students Business * Microsoft Cloud * Microsoft Security * Dynamics 365 * Microsoft 365 * Microsoft Power Platform * Microsoft Teams * Microsoft Industry * Small Business Developer & IT * Azure * Developer Center * Documentation * Microsoft Learn * Microsoft Tech Community * Azure Marketplace * AppSource * Visual Studio Company * Careers * About Microsoft * Company news * Privacy at Microsoft * Investors * Diversity and inclusion * Accessibility * Sustainability Your Privacy Choices Your Privacy Choices * Sitemap * Contact Microsoft * Privacy * Manage cookies * Terms of use * Trademarks * Safety & eco * Recycling * About our ads * (c) Microsoft 2023