Search code examples
batch-filecmduac

Delete files after pressing no in batch file UAC prompt


I have a SFX zip with files and a batch inside that auto-starts after extraction. The batch starts by attempting to elevate priviliges to the batch using the get admin code below.

Works fine, have used it for a long time. But now I need it to do something extra.

The SFX, upon execution, caches the files in C:\Windows\Temp. Some of the files are sensitive in content, used during the remainder of the batch file processes to set up installations, copy files etc. At the end of the batch file, the files are deleted.

However, it dawned on me that if a user presses no at the UAC prompt, the batch doesn't run so the files remain in the cache!

I decided to try and insert the file deletion in the admin code after the user presses no. But because that part is windows shell, I cant see a way to capture that they pressed no.

"%SystemRoot%\%winSysFolder%\WScript.exe" "%vbsGetPrivileges%" %*
DEL file1.txt
DEL file2.txt
DEL file3.txt
exit /b

"%SystemRoot%\%winSysFolder%\WScript.exe" "%vbsGetPrivileges%" %*
 exit /b
 DEL file1.txt
 DEL file2.txt
 DEL file3.txt

After a long time trying having the extra lines in various places I got close to trying them before and after the exit /b. However it still deletes the files if you press YES.

I also tried removing the exit /b so it carries on running the admin code and triggering the deletion because admin wasn't gained, I expected that ELEV wouln't resolve at the end so tried this.

"%SystemRoot%\%winSysFolder%\WScript.exe" "%vbsGetPrivileges%" %*
:gotPrivileges
setlocal & cd /d %~dp0
if '%1'=='' (del "%vbsGetPrivileges%" 1>nul 2>nul  &  shift /1
    DEL file1.txt
    DEL file2.txt
    DEL file3.txt
)

I know some might comment that even if this can be done, cancelling the batch mid way through will leave the files there. Yes, but at least this is something, and I need to see if, how I can do this now.

Utlimately I need the admin code to not immediatley shutdown if the user presses no so it can delete the 3 files in the cache, THEN closing. I thought it would be easy but hours later I need to call upon the experts once again. Thanks for any advice in advance.

:getadmin
::------------------------------------------------------------------------------------------------------
:init
setlocal DisableDelayedExpansion
set cmdInvoke=1
set winSysFolder=System32
set "batchPath=%~0"
for %%k in (%0) do set batchName=%%~nk
set "vbsGetPrivileges=%temp%\OEgetPriv_%batchName%.vbs"
setlocal EnableExtensions EnableDelayedExpansion
:checkPrivileges
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )
:getPrivileges
if '%1'=='ELEV' (echo ELEV & shift /1 & goto gotPrivileges)
ECHO:
ECHO **************************************
ECHO Invoking UAC for Privilege Escalation
ECHO **************************************
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%vbsGetPrivileges%"
ECHO args = "ELEV " >> "%vbsGetPrivileges%"
ECHO For Each strArg in WScript.Arguments >> "%vbsGetPrivileges%"
ECHO args = args ^& strArg ^& " "  >> "%vbsGetPrivileges%"
ECHO Next >> "%vbsGetPrivileges%"
if '%cmdInvoke%'=='1' goto InvokeCmd 
ECHO UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%vbsGetPrivileges%"
goto ExecElevation
:InvokeCmd
ECHO args = "/c """ + "!batchPath!" + """ " + args >> "%vbsGetPrivileges%"
ECHO UAC.ShellExecute "%SystemRoot%\%winSysFolder%\cmd.exe", args, "", "runas", 1 >> "%vbsGetPrivileges%"
:ExecElevation
"%SystemRoot%\%winSysFolder%\WScript.exe" "%vbsGetPrivileges%" %*
exit /B
:gotPrivileges
setlocal & cd /d %~dp0
if '%1'=='ELEV' (del "%vbsGetPrivileges%" 1>nul 2>nul  &  shift /1)
::------------------------------------------------------------------------------------------------------

Solution

  • @echo off
    if "%1" equ "elev" goto elev
    if exist %temp%\iselevated.tmp del %temp%\iselevated.tmp >nul
    mshta vbscript:Close(CreateObject("Shell.Application").ShellExecute("cmd.exe","/c"+Space(1)+"call"+Space(1)+"""%~f0"""+Space(1)+"elev","","runas",1))
    timeout /nobreak 1 >nul
    if exist %temp%\iselevated.tmp (
        echo User clicked Yes on the UAC prompt!
        echo There is an elevated process running!
        echo Aborting Cleanup...
        pause
        exit /b
    )
    echo User clicked No on the UAC prompt!
    echo No new elevated process was spawned!
    echo Attempting to clear cached files...
    rem DEL file1.txt
    rem DEL file2.txt
    rem DEL file3.txt
    pause
    exit /b
    
    :elev
    icacls "%~f0" /setintegritylevel HIGH >nul
    cd /d "%~dp0"
    if not exist "%~dp0" exit /b
    echo. > %temp%\iselevated.tmp
    echo Running elevated
    rem change "runas",1 to "runas",0 to make the window invisible
    rem (admincode goes here)
    rem ???
    rem DEL file1.txt
    rem DEL file2.txt
    rem DEL file3.txt
    pause
    exit /b