Thursday, September 27, 2012

Java Security Remediation script

Just sharing some code. This encompasses the uninstall logic for all versions of Java previous to JRE 1.7.0.7, installs JRE 1.7.0.9 if it's not present, and handles logging. Let me know if you have any comments or suggestions



***************************************************************************
' //
' // File:      JRE1.7.0_09.vbs
' // 
' // Author: Winston Hinton
' // 
' // Version:   1.3
' // 
' // Purpose:   Uninstall all Java versions previous to and including JRE 1.7.0_7 
' //            while installing JRE 1.7.0_9 with logging logic.
' //
' // ****************************************************************************

OptionExplicit

OnErrorResumeNext

Dim wshShell, fso, strLogFile, ts, strTempDir, strTempISS, strUnString, tsIn
Dim strUninstLine, CLSID, search5, search6, search7, strJRE1, strDisplayName, strDisplayVersion
Dim strPublisher, strUninstallString, strJREUninstallString, strJREDisplayName
Dim search1, search2, search3, search4, strJREUninstallStringNEW, ret, strUninstCMD
Dim tsISS, strSetupexe, qVal, strComputername, strCurrentVersion, strInstallMSI
Dim searchCurVer, CurVerFound, strArrayCount, strLogPath, strInstallCMD, strInstallLog

Dim arrayJREDisplayName()
Dim arrayJREUninstallString()

'# This is the version that you don't want to have it uninstall
strCurrentVersion = "Java 7 Update 9"

'# Set these to the desired log path and current version installer location
strLogPath = "c:\logs\"
strInstallMSI = "\\your\unc\path\here\jre1.7.0_09\jre1.7.0_09.msi"

qVal = 0
strArrayCount = 0
ReDim arrayJREDisplayName(strArrayCount)
ReDim arrayJREUninstallString(strArrayCount)

Set wshShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

strComputername = wshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")

'# Set this to the appropriate command line settings to do a silent MSI install
strInstallLog = strLogPath & "Java_Install_" & strComputername & ".log"
strInstallCMD = "msiexec /I """ & strInstallMSI & """ /QN /Lime """ & strInstallLog & """"

IfNot fso.FolderExists(strLogPath) Then fso.CreateFolder(strLogPath)
strLogFile = strLogPath & "Java_Uninstall_" & strComputername & ".log"
Set ts = fso.OpenTextFile(strLogFile, 8, True)

ts.WriteLineString(120, "_")
ts.WriteLineString(120, "¯")
ts.WriteLineNow() & " - Java Runtime(s) uninstallation started..."
ts.WriteLineString(120, "_") & vbCrlf

'# Generate Registry extracts from 'Uninstall' keys.
PreFlight()

'# Kill Java Processes
KillProc()

'# Uninstall JRE 1.7.0_7
'# Deprecated process. Leaving code intact and commented for troubleshooting and testing.
'UninstallJREu7()


strTempDir = wshShell.ExpandEnvironmentStrings("%temp%")
strTempISS = strTempDir & "\iss"
strUnString = " -s -a /s /f1"
Set tsIn = fso.OpenTextFile(strTempDir & "\uninstall.tmp", 1)

IfNot fso.FolderExists(strTempISS) Then fso.CreateFolder(strTempISS)

DoWhileNot tsIn.AtEndOfStream
    strUninstLine = tsIn.ReadLine
    CLSID = Mid(strUninstLine, 73, 38)
    search5 = Instr(strUninstLine, "JRE 1")
    search6 = Instr(strUninstLine, "]")
    If search5 > 0AND search6 > 0Then
        strJRE1 = Replace(Mid(strUninstLine, search5, search6),"]","") 
    EndIf

    OnErrorResumeNext

    strDisplayName = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & CLSID & "\DisplayName")
    strDisplayVersion = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & CLSID & "\DisplayVersion")
    strPublisher = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & CLSID & "\Publisher")
    strUninstallString = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & CLSID & "\UninstallString")
    strJREUninstallString = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & strJRE1 & "\UninstallString")
    strJREDisplayName = wshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" & strJRE1 & "\DisplayName")

    OnErrorGoto0

    'Search for presence of Java and Sun in DisplayName and Publisher
    search1 = Instr(1, strDisplayName, "Java", 1)
    search2 = Instr(1, strPublisher, "Sun", 1)
    search3 = Instr(1, strDisplayName, "J2SE", 1)
    search4 = Instr(1, strUninstallString, "setup.exe", 1)
    search7 = InStr(1, strDisplayName, "Development", 1) + InStr(1, strDisplayName, "Java DB", 1)

    'See if it is the current version
    searchCurVer = InStr(1, strDisplayName, strCurrentVersion, 1)

    'If it is, Show that the current version is found
    If searchCurVer > 0Then
    CurVerFound = True

    ElseIf strJREUninstallString <> ""Then
        '# JRE 1 found
        strJREUninstallStringNEW = Replace(strJREUninstallString," -f"," -s -a /s /f")
    redimPreserve arrayJREDisplayName(strArrayCount)
    redimPreserve arrayJREUninstallString(strArrayCount)
        arrayJREDisplayName(strArrayCount) = " - Found Old JRE: " & strDisplayName & "  - Version: " & strDisplayVersion & ", Uninstalling..."
    arrayJREUninstallString(strArrayCount) = strJREUninstallStringNEW
    strArrayCount = strArrayCount + 1

    ElseIf search7 = 0And search1 > 0Or search3 > 0And search2 > 0Then
        strUninstCMD = "msiexec.exe /x " & CLSID & " /norestart /qn"

        If search4 > 0Then
            '# Old InstallShield setup found
            Set tsISS = fso.OpenTextFile(strTempISS & "\" & CLSID & ".iss", 2, True)

            'Create Response file for any Java Version
            tsISS.WriteLine"[InstallShield Silent]"
            tsISS.WriteLine"Version=v6.00.000"
            tsISS.WriteLine"File=Response File"
            tsISS.WriteLine"[File Transfer]"
            tsISS.WriteLine"OverwrittenReadOnly=NoToAll"
            tsISS.WriteLine"[" & CLSID & "-DlgOrder]"
            tsISS.WriteLine"Dlg0=" & CLSID & "-SprintfBox-0"
            tsISS.WriteLine"Count=2"
            tsISS.WriteLine"Dlg1=" & CLSID & "-File Transfer"
            tsISS.WriteLine"[" & CLSID & "-SprintfBox-0]"
            tsISS.WriteLine"Result=1"
            tsISS.WriteLine"[Application]"
            tsISS.WriteLine"Name=Java 2 Runtime Environment, SE v1.4.0_01"
            tsISS.WriteLine"Version=1.4.0_01"
            tsISS.WriteLine"Company=JavaSoft"
            tsISS.WriteLine"Lang=0009"
            tsISS.WriteLine"[" & CLSID & "-File Transfer]"
            tsISS.WriteLine"SharedFile=YesToAll"
            tsISS.Close

            strSetupexe = Left(strUninstallString, search4 + 9)
            strUninstCMD =  strSetupexe & strUnString & Chr(34) & strTempISS & "\" & CLSID & ".iss" & Chr(34)
        EndIf

    redimPreserve arrayJREDisplayName(strArrayCount)
    redimPreserve arrayJREUninstallString(strArrayCount)
    arrayJREDisplayName(strArrayCount) = " - Found Old JRE: " & strDisplayName & "    - Version: " & strDisplayVersion & ", Uninstalling..."
    arrayJREUninstallString(strArrayCount) = strUninstCMD
    strArrayCount = strArrayCount + 1

    EndIf

Loop

tsIn.Close

Dim I
If CurVerFoundAND strArrayCount > 0Then
    ts.WritelineNow() & " - Current Version: " & strCurrentVersion & " found, continuing with uninstalls..."
    For I = LBOUND(arrayJREDisplayName) toUBOUND(arrayJREDisplayName)
        ts.WriteLineNow() & arrayJREDisplayName(I)
        ts.WriteLineNow() & " - Uninstall String sent: " & arrayJREUninstallString(I)
        ret = wshShell.Run(arrayJREUninstallString(I) , 0, True)
        ts.WriteLineNow() & " - Return: " & ret
        If ret <> 0And ret <> 3010Then qVal = 1
    Next

ElseIf CurVerFoundAND strArrayCount = 0Then
    ts.WriteLineNow() & " - Current version, " & strCurrentVersion & ", found."
    ts.WriteLineNow() & " - No Old Java Runtime versions are installed."
    qVal = 99

ElseIfNot CurVerFoundThen

    ts.WriteLineNow() & " - Current Java version, " & strCurrentVersion & ", not found, installing it."
    ts.WriteLineNow() & " - Running Command: " & strInstallCMD
    ret = wshShell.Run(strInstallCMD , 0, True)
    If ret <> 0AND ret<> 3010Then
        ts.WriteLineNow() & " - Failed to Install Java, see " & strInstallLog & " for more details.  Exiting Script."
        qVal = 1
    ElseIf strArrayCount > 0Then
        ts.WriteLineNow() & " - Successfully installed " & strCurrentVersion & ", and logged to " & strInstallLog & "."
        For I = LBOUND(arrayJREDisplayName) toUBOUND(arrayJREDisplayName)
        ts.WriteLineNow() & arrayJREDisplayName(I)
        ts.WriteLineNow() & " - Uninstall String sent: " & arrayJREUninstallString(I)
        ret = wshShell.Run(arrayJREUninstallString(I) , 0, True)
        ts.WriteLineNow() & " - Return: " & ret
        If ret <> 0And ret <> 3010Then qVal = 1
    Next
    ElseIf strArrayCount = 0Then
      ts.WriteLineNow() & " - Successfully installed " & strCurrentVersion & ", and logged to " & strInstallLog & "."
    ts.WriteLineNow() & " - No Old Java Runtime versions are installed."
        qVal = 99
    EndIf
EndIf

ts.WriteLineString(120, "_")
ts.WriteLineString(120, "¯")
ts.Close
fso.DeleteFolder(strTempISS)
fso.DeleteFile(strTempDir & "\uninstall.tmp")

WScript.Quit(qVal)

Sub PreFlight()
    '# Creates temp files containing extracts from registry 'Uninstall' keys.
    Dim wshShell, fso, sTemp
    Set wshShell = CreateObject("WScript.Shell")
    Set fso = CreateObject("Scripting.FileSystemObject")
    sTemp = wshShell.ExpandEnvironmentStrings("%temp%")
    wshShell.Run"REGEDIT /E %temp%\registry.tmp HKEY_LOCAL_MACHINE\SOFTWARE\microsoft\windows\currentversion\uninstall", 0, True
    wshShell.Run"cmd /c type %temp%\registry.tmp | find /i ""{"" | find /i ""}]"" > %temp%\uninstall.tmp ", 0, True
    wshShell.Run"cmd /c type %temp%\registry.tmp | find /i ""JRE 1"" >> %temp%\uninstall.tmp ", 0, True
    IfNot fso.FileExists(sTemp & "\uninstall.tmp") Then
        ts.WriteLineNow() & " - No input - %temp%\uninstall.tmp Reg extract not created."
        ts.WriteLineString(120, "_")
        ts.WriteLineString(120, "¯")
        ts.Close
        WScript.Quit(1)
    EndIf
EndSub

Sub KillProc()
    '# kills jusched.exe and jqs.exe if they are running.  These processes will cause the installer to fail.
    Dim wshShell
    Set wshShell = CreateObject("WScript.Shell")
    wshShell.Run"Taskkill /F /IM jusched.exe /T", 0, True
    wshShell.Run"Taskkill /F /IM jqs.exe /T", 0, True
EndSub

'Sub UninstallJREu7()
'# Deprecated process. Leaving code intact and commented for troubleshooting and testing.
'    Dim wshShell
'    Set wshShell = CreateObject("WScript.Shell")
'    wshShell.Run "msiexec /X{26A24AE4-039D-4CA4-87B4-2F83217007FF} /qn", 0, True
'End Sub

Wednesday, September 5, 2012

Hello world!

This is just a short introductory post. The focus of this blog will be around Windows operating system deployment using MDT, WDS and WAIK. I will be touching on customizing the MDT  LiteTouch scripts, customizing USMT  and large scale deployments. Welcome!