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.
' //
' // ****************************************************************************

Option Explicit

On Error Resume Next

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 & """"

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

ts.WriteLine String(120, "_")
ts.WriteLine String(120, "¯")
ts.WriteLine Now() & " - Java Runtime(s) uninstallation started..."
ts.WriteLine String(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)

If Not fso.FolderExists(strTempISS) Then fso.CreateFolder(strTempISS)

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

    On Error Resume Next

    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")

    On Error Goto 0

    '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 > 0 Then
    CurVerFound = True

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

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

        If search4 > 0 Then
            '# 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)
        End If

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

    End If

Loop

tsIn.Close

Dim I
If CurVerFound AND strArrayCount > 0 Then
    ts.Writeline Now() & " - Current Version: " & strCurrentVersion & " found, continuing with uninstalls..."
    For I = LBOUND(arrayJREDisplayName) to UBOUND(arrayJREDisplayName)
        ts.WriteLine Now() & arrayJREDisplayName(I)
        ts.WriteLine Now() & " - Uninstall String sent: " & arrayJREUninstallString(I)
        ret = wshShell.Run(arrayJREUninstallString(I) , 0, True)
        ts.WriteLine Now() & " - Return: " & ret
        If ret <> 0 And ret <> 3010 Then qVal = 1
    Next

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

ElseIf Not CurVerFound Then

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

ts.WriteLine String(120, "_")
ts.WriteLine String(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
    If Not fso.FileExists(sTemp & "\uninstall.tmp") Then
        ts.WriteLine Now() & " - No input - %temp%\uninstall.tmp Reg extract not created."
        ts.WriteLine String(120, "_")
        ts.WriteLine String(120, "¯")
        ts.Close
        WScript.Quit(1)
    End If
End Sub

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
End Sub 

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