Jump to content

MD5sfc


ripdad
 Share

Recommended Posts

#cs
    
    MD5sfc (MD5 System File Checker)
    
    Makes a MD5 List of files of your hard drive and
    then compares files against the list afterwards.
    
    Listed Filetypes: exe, dll, bat, cmd, com, sys, drv, ocx, pif
    
    UDF Credits: Melba23 and trancexx

    Release Date: June 18, 2010
    Tested only on Windows XP
    
    Not to be confused with Microsoft SFC - which works totally different
    
#ce
;
Global $cString, $uString, $sfcini = (@ScriptDir & '\MD5sfc.ini')
Local $nMsg
Local $SFCgui = GUICreate(' MD5sfc', 250, 175, -1, -1)
Local $INFlbl = GUICtrlCreateLabel('Please Close Applications Before Beginning', 20, 20, 300, 20)
Local $GENbtn = GUICtrlCreateButton('Generate MD5 System File List', 25, 60, 200, 25)
Local $CMPbtn = GUICtrlCreateButton('Compare MD5 System File List', 25, 95, 200, 25)
Local $EXTbtn = GUICtrlCreateButton('Exit', 25, 130, 200, 25)
GUISetState(@SW_SHOW, $SFCgui)
;
Do
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GENbtn
            _InitSFC(1); Generate MD5 System File List
            GUISetState(@SW_SHOW, $SFCgui)
        Case $CMPbtn
            $cString = ''
            $uString = ''
            _InitSFC(2); Compare MD5 System File List
            GUISetState(@SW_SHOW, $SFCgui)
        Case $EXTbtn
            ExitLoop
    EndSwitch
Until $nMsg = -3
;
$cString = ''
$uString = ''
$cArray = ''
$uArray = ''
GUIDelete($SFCgui)
Exit
;
Func _InitSFC($sMode = 0)
    GUISetState(@SW_HIDE, $SFCgui)
    If Not ($sMode == 1 Or $sMode == 2) Then
        MsgBox(8208, 'MD5sfc', 'Invalid Mode')
        Return
    EndIf
    If Not FileExists($sfcini) And $sMode > 1 Then
        MsgBox(8208, 'MD5sfc', 'Must Generate a List First ')
        Return
    EndIf
    Local $mRtn = '', $cArray = '', $uArray = ''
    $mRtn = _RecFileFinder(@HomeDrive, $sMode); @HomeDrive @WindowsDir @SystemDir
    If $mRtn < 1 Then
        MsgBox(8208, 'MD5sfc', 'Error - Cannot Continue ')
        Exit
    EndIf
    If $sMode = 1 Then MsgBox(8256, 'MD5sfc', 'System File List Generated ')
    If $sMode = 2 Then
        If $cString = '' Then
            MsgBox(8256, 'MD5sfc', 'System Files are Clean ')
        Else
            $cArray = StringSplit(StringTrimRight($cString, 1), '|')
            _ShowListBox("MD5 Changed File(s)", $cArray)
            If MsgBox(8228, 'MD5sfc', 'Replace Changed File(s) in System File List ? ') = 7 Then
            Else
                For $i = 1 To $cArray[0]
                    IniWrite($sfcini, 'SystemFiles', $cArray[$i], _MD5ForFile($cArray[$i]))
                Next
                MsgBox(8256, 'MD5sfc', 'Changed File(s) Replaced ')
            EndIf
        EndIf
        If $uString = '' Then
        Else
            $uArray = StringSplit(StringTrimRight($uString, 1), '|')
            _ShowListBox("Unlisted File(s)", $uArray)
            If MsgBox(8228, 'MD5sfc', 'Add Unlisted File(s) to System File List ? ') = 7 Then Return
            For $i = 1 To $uArray[0]
                IniWrite($sfcini, 'SystemFiles', $uArray[$i], _MD5ForFile($uArray[$i]))
            Next
            MsgBox(8256, 'MD5sfc', 'New File(s) Added ')
        EndIf
    EndIf
EndFunc
;
Func _ShowListBox($lbTitle, $inArray)
    Local $gc = GUICreate('MD5sfc - ' & $inArray[0] & ' ' & $lbTitle)
    $cl = GUICtrlCreateList('', 10, 10, 380, 380)
    For $a = 1 To $inArray[0]
        GUICtrlSetData($cl, $inArray[$a])
    Next
    GUISetState(@SW_SHOW, $gc)
    Do
    Until GUIGetMsg() = -3
    GUIDelete()
EndFunc
;
; UDF: _RecFileFinder
; Author: Melba23 - Modified: June 18, 2010 by ripdad
Func _RecFileFinder($sPath, $tMode)
    If Not FileExists($sPath) Then Return -1
    If StringInStr($sPath, '/') Then Return -1
    If Not ($tMode == 1 Or $tMode == 2) Then Return -1
    If StringRight($sPath, 1) <> "\" Then $sPath = $sPath & "\"
    Local $asFolderList[3] = [1], $cPath, $ttDisplay, $ttName, $tExt
    Local $hSearch, $sName, $fFolder, $tFile, $ttPath, $cct = 0, $uct = 0
    Local $svi = @HomeDrive & '\System Volume Information\', $rec = @HomeDrive & '\Recycler\', $spf = 'pagefile.sys'
    If $tMode = 1 Then $ttDisplay = ' Generating MD5 System File List '
    If $tMode = 2 Then $ttDisplay = ' Comparing MD5 System File List '
    $asFolderList[1] = $sPath
    While $asFolderList[0] > 0
        $cPath = $asFolderList[$asFolderList[0]]
        $asFolderList[0] -= 1
        $hSearch = FileFindFirstFile($cPath & "*")
        If $hSearch = -1 Then ContinueLoop
        If $cPath = $svi Or $cPath = $rec Then ContinueLoop
        While 1
            $sName = FileFindNextFile($hSearch)
            If @error Then ExitLoop
            If $sName = $spf Then ContinueLoop
            $fFolder = @extended
            If $fFolder Then
                $asFolderList[0] += 1
                If UBound($asFolderList) <= $asFolderList[0] + 1 Then ReDim $asFolderList[UBound($asFolderList) * 2]
                $asFolderList[$asFolderList[0]] = $cPath & $sName & "\"
            EndIf
            If $fFolder Then ContinueLoop
            $tExt = StringTrimLeft($sName, StringInStr($sName, '.', 0, -1))
            Switch $tExt
                Case 'exe', 'dll', 'bat', 'cmd', 'com', 'sys', 'drv', 'ocx', 'pif'
                Case Else
                    ContinueLoop
            EndSwitch
            $ttPath = $cPath
            $ttName = $sName
            If StringLen($ttPath) > 52 Then $ttPath = StringTrimRight($ttPath, StringLen($ttPath) - 52)
            If StringLen($ttName) > 52 Then $ttName = StringTrimRight($ttName, StringLen($ttName) - 52)
            ToolTip($ttPath & @CRLF & $ttName, 0, 0, 'MD5sfc -' & $ttDisplay, 1)
            If $tMode = 1 Then IniWrite($sfcini, 'SystemFiles', $cPath & $sName, _MD5ForFile($cPath & $sName))
            If @error Then Return -1
            If $tMode = 2 Then
                $tFile = IniRead($sfcini, 'SystemFiles', $cPath & $sName, 'NotFound')
                If $tFile = _MD5ForFile($cPath & $sName) Then
                    ContinueLoop
                ElseIf $tFile = 'NotFound' Then
                    $uString &= $cPath & $sName & '|'
                Else
                    $cString &= $cPath & $sName & '|'
                EndIf
            EndIf
        WEnd
        FileClose($hSearch)
    WEnd
    ToolTip('')
    Return 1
EndFunc
;
; UDF: _MD5ForFile
; Author: trancexx
Func _MD5ForFile($sFile)
    Local $a_hCall = DllCall("kernel32.dll", "hwnd", "CreateFileW", _
            "wstr", $sFile, _
            "dword", 0x80000000, _
            "dword", 3, _
            "ptr", 0, _
            "dword", 3, _
            "dword", 0, _
            "ptr", 0)
    If @error Or $a_hCall[0] = -1 Then
        Return SetError(1, 0, "")
    EndIf
    Local $hFile = $a_hCall[0]
    $a_hCall = DllCall("kernel32.dll", "ptr", "CreateFileMappingW", _
            "hwnd", $hFile, _
            "dword", 0, _
            "dword", 2, _
            "dword", 0, _
            "dword", 0, _
            "ptr", 0)
    If @error Or Not $a_hCall[0] Then
        DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFile)
        Return SetError(2, 0, "")
    EndIf
    DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFile)
    Local $hFileMappingObject = $a_hCall[0]
    $a_hCall = DllCall("kernel32.dll", "ptr", "MapViewOfFile", _
            "hwnd", $hFileMappingObject, _
            "dword", 4, _
            "dword", 0, _
            "dword", 0, _
            "dword", 0)
    If @error Or Not $a_hCall[0] Then
        DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject)
        Return SetError(3, 0, "")
    EndIf
    Local $pFile = $a_hCall[0]
    Local $iBufferSize = FileGetSize($sFile)
    Local $tMD5_CTX = DllStructCreate("dword i[2];" & _
            "dword buf[4];" & _
            "ubyte in[64];" & _
            "ubyte digest[16]")
    DllCall("advapi32.dll", "none", "MD5Init", "ptr", DllStructGetPtr($tMD5_CTX))
    If @error Then
        DllCall("kernel32.dll", "int", "UnmapViewOfFile", "ptr", $pFile)
        DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject)
        Return SetError(4, 0, "")
    EndIf
    DllCall("advapi32.dll", "none", "MD5Update", _
            "ptr", DllStructGetPtr($tMD5_CTX), _
            "ptr", $pFile, _
            "dword", $iBufferSize)
    If @error Then
        DllCall("kernel32.dll", "int", "UnmapViewOfFile", "ptr", $pFile)
        DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject)
        Return SetError(5, 0, "")
    EndIf
    DllCall("advapi32.dll", "none", "MD5Final", "ptr", DllStructGetPtr($tMD5_CTX))
    If @error Then
        DllCall("kernel32.dll", "int", "UnmapViewOfFile", "ptr", $pFile)
        DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject)
        Return SetError(6, 0, "")
    EndIf
    DllCall("kernel32.dll", "int", "UnmapViewOfFile", "ptr", $pFile)
    DllCall("kernel32.dll", "int", "CloseHandle", "hwnd", $hFileMappingObject)
    Local $sMD5 = Hex(DllStructGetData($tMD5_CTX, "digest"))
    Return SetError(0, 0, $sMD5)
EndFunc
;

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

Hi Shafayat,

Thanks. I wish you had posted yours - so I wouldn't have to make this one!

What else do you have in hiding? lol

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...