'*********************************************************** ' File: GetP7s.vbs (WSH sample in VBScript) ' Author: (c) M. Gallant ' Date: 11/10/2001 ' ' Based partly on "ReplaceTxt.vbs " Listing 12-23 ' by G. Born ' "Windows Script Host 2.0 Developer's Guide" 2000 ' ' Reads a vbs, js or wsf signed script and attempts ' to extract out the appended base64-encoded pkcs#7 block '************************************************************* Option Explicit ' We must define ForWriting because the ' iomode constants are unknown to VBScript. Const ForWriting = 2 ' iomode: write access Const replacement = "" Dim Text Dim fso ' Object variable Dim oFileIn, oFileOut ' Text stream Dim path, fileIn, fileOut, fileName Dim fileargs Dim pattern, patternreg, beginblock, endblock path = GetPath() ' Retrieve current path to script. If WScript.Version < 5.6 Then WScript.Echo "*** Requires WSH Version 5.6+ ***" WScript.Quit End If Set fileargs = WScript.Arguments If fileargs.Count<1 Then WScript.Echo "-------- GetP7s.vbs usage: --------" & vbCrLf & "cscript GetPkcs7blk.vbs " & _ vbCrLf & "where is .vbs, .js or .wfs type" & _ vbCrLf & vbCrLf & "Or drag a file of these types onto the GetP7s.vbs icon." & _ vbCrLf & vbCrLf & "Output file: .p7s" WScript.Quit End If ' Create a FileSystemObject object to access the file system. Set fso = CreateObject("Scripting.FileSystemObject") fileIn = fileargs(0) If NOT fso.FileExists(fileIn) Then WScript.Echo "File " & fileIn & " not found!" WScript.Quit 1 End If fileName = fso.GetFile(fileIn).Name '------- Only allow .vbs, .js or .wsf file types for signature extraction ----- If InStr(LCase(fileName), ".vbs") Then pattern = "'' SIG '' " patternreg = pattern beginblock = "'' SIG '' Begin signature block" endblock = "'' SIG '' End signature block" ElseIf InStr(LCase(fileName), ".js") Then pattern = "// SIG // " patternreg = pattern beginblock = "// SIG // Begin signature block" endblock = "// SIG // End signature block" ElseIf InStr(LCase(fileName), ".wsf") Then pattern = "** SIG ** " patternreg = "\*\* SIG \*\* " ' since pattern uses * chars, need to excape for RegExp.Pattern beginblock = "** SIG ** Begin signature block" ' not uses currently for wsf file types. endblock = "** SIG ** End signature block" Else WScript.Echo "File type must be .vbs, .js or .wsf" WScript.Quit 1 End If fileIn = path & fileName ' build full path fileOut = path & fileName & ".p7s" ' output .p7s type; provides OS-level cert. snapin extension functionality. ' Input file present; open file and create output file. Set oFileIn = fso.OpenTextFile(fileIn) ' Open input file. Set oFileOut = fso.OpenTextFile(fileOut, _ ForWriting, True) ' Open output file. Do While Not (oFileIn.atEndOfStream) Text = oFileIn.ReadLine ' Read a line. IF InStr(Text,pattern) AND NOT InStr(Text,beginblock) AND NOT InStr(Text,endblock) THEN Text = Filter(Text, patternreg, replacement) oFileOut.WriteLine Text ' Write text. END IF Loop WScript.Echo "Input Text file: " & fileIn & vbCrLf & _ "Output PKCS7 File: " & fileOut Function GetPath ' Retrieve the script path. Dim path path = WScript.ScriptFullName ' Script name GetPath = Left(path, InStrRev(path, "\")) End Function Function Filter(txt, expr1, expr2) ' Replace expr1 with expr2 in text. Dim oReg Set oReg = New RegExp ' Create regular expression. oReg.Global = True ' All matches oReg.IgnoreCase = True ' Make case-insensitive. ' Replace all expr1 with expr2. oReg.Pattern = expr1 ' Set pattern. Filter = oReg.Replace(txt, expr2) End Function