Attribute VB_Name = "modImportAnalysis"
Option Explicit

'Enhanced Import Analysis for PE Differ
'Shows exactly what changed when imphash differs
'Roger's Malware Analysis Toolkit

Public Function AnalyzeImportChanges(pe1 As CPEEditor, pe2 As CPEEditor) As CImportChangeReport
    'Detailed analysis of import table changes
    On Error GoTo hell
    
    Dim report As New CImportChangeReport
    report.imphash1 = pe1.impHash
    report.imphash2 = pe2.impHash
    report.imphashMatch = (report.imphash1 = report.imphash2)
    
    'Get import tables
    Dim imp1 As CLoadImports, imp2 As CLoadImports
    On Error Resume Next
    Set imp1 = pe1.Imports
    Set imp2 = pe2.Imports
    On Error GoTo hell
    
    If imp1 Is Nothing And imp2 Is Nothing Then
        report.summary = "No imports in either file"
        Set AnalyzeImportChanges = report
        Exit Function
    End If
    
    If imp1 Is Nothing Then
        report.summary = "Imports added (none in original)"
        Dim i As Long
        For i = 1 To imp2.Modules.Count
            Dim impMod As CImport
            Set impMod = imp2.Modules(i)
            report.dllsAdded.Add impMod.DllName
        Next
        report.totalChanges = imp2.Modules.Count
        Set AnalyzeImportChanges = report
        Exit Function
    End If
    
    If imp2 Is Nothing Then
        report.summary = "All imports removed"
        For i = 1 To imp1.Modules.Count
            Set impMod = imp1.Modules(i)
            report.dllsRemoved.Add impMod.DllName
        Next
        report.totalChanges = imp1.Modules.Count
        Set AnalyzeImportChanges = report
        Exit Function
    End If
    
    'Build DLL lookup tables
    Dim dlls1 As Object, dlls2 As Object
    Set dlls1 = CreateObject("Scripting.Dictionary")
    Set dlls2 = CreateObject("Scripting.Dictionary")
    
    For i = 1 To imp1.Modules.Count
        Set impMod = imp1.Modules(i)
        dlls1(LCase(impMod.DllName)) = impMod
    Next
    
    For i = 1 To imp2.Modules.Count
        Set impMod = imp2.Modules(i)
        dlls2(LCase(impMod.DllName)) = impMod
    Next
    
    'Find added DLLs
    Dim key
    For Each key In dlls2.Keys
        If Not dlls1.exists(key) Then
            report.dllsAdded.Add dlls2(key).DllName
            report.totalChanges = report.totalChanges + 1
            
            'All functions in this DLL are "added"
            Dim j As Long
            For j = 1 To dlls2(key).functions.Count
                Dim fc As New CImportFunctionChange
                fc.dllName = dlls2(key).DllName
                fc.functionName = CStr(dlls2(key).functions(j))
                report.functionsAdded.Add fc
            Next
        End If
    Next
    
    'Find removed DLLs
    For Each key In dlls1.Keys
        If Not dlls2.exists(key) Then
            report.dllsRemoved.Add dlls1(key).DllName
            report.totalChanges = report.totalChanges + 1
            
            'All functions in this DLL are "removed"
            For j = 1 To dlls1(key).functions.Count
                Set fc = New CImportFunctionChange
                fc.dllName = dlls1(key).DllName
                fc.functionName = CStr(dlls1(key).functions(j))
                report.functionsRemoved.Add fc
            Next
        End If
    Next
    
    'Compare common DLLs for function changes
    For Each key In dlls1.Keys
        If dlls2.exists(key) Then
            Dim imp1Dll As CImport, imp2Dll As CImport
            Set imp1Dll = dlls1(key)
            Set imp2Dll = dlls2(key)
            
            'Build function lookup tables
            Dim funcs1 As Object, funcs2 As Object
            Set funcs1 = CreateObject("Scripting.Dictionary")
            Set funcs2 = CreateObject("Scripting.Dictionary")
            
            For j = 1 To imp1Dll.functions.Count
                Dim fname As String
                fname = CStr(imp1Dll.functions(j))
                funcs1(LCase(fname)) = fname
            Next
            
            For j = 1 To imp2Dll.functions.Count
                fname = CStr(imp2Dll.functions(j))
                funcs2(LCase(fname)) = fname
            Next
            
            'Check for function changes
            Dim hasChanges As Boolean
            hasChanges = False
            
            'Functions added to this DLL
            Dim funcKey
            For Each funcKey In funcs2.Keys
                If Not funcs1.exists(funcKey) Then
                    Set fc = New CImportFunctionChange
                    fc.dllName = imp2Dll.DllName
                    fc.functionName = funcs2(funcKey)
                    report.functionsAdded.Add fc
                    hasChanges = True
                    report.totalChanges = report.totalChanges + 1
                End If
            Next
            
            'Functions removed from this DLL
            For Each funcKey In funcs1.Keys
                If Not funcs2.exists(funcKey) Then
                    Set fc = New CImportFunctionChange
                    fc.dllName = imp1Dll.DllName
                    fc.functionName = funcs1(funcKey)
                    report.functionsRemoved.Add fc
                    hasChanges = True
                    report.totalChanges = report.totalChanges + 1
                End If
            Next
            
            If hasChanges Then
                report.dllsModified.Add imp1Dll.DllName
            End If
        End If
    Next
    
    'Build summary
    report.summary = BuildImportChangeSummary(report)
    
    Set AnalyzeImportChanges = report
    Exit Function
    
hell:
    If report Is Nothing Then Set report = New CImportChangeReport
    report.summary = "Error analyzing imports: " & Err.Description
    Set AnalyzeImportChanges = report
End Function

Private Function BuildImportChangeSummary(report As CImportChangeReport) As String
    Dim s As String
    
    s = "Import Hash: " & IIf(report.imphashMatch, "MATCH", "CHANGED") & vbCrLf
    s = s & "  Original: " & report.imphash1 & vbCrLf
    s = s & "  Modified: " & report.imphash2 & vbCrLf
    s = s & vbCrLf
    
    s = s & "DLL Changes:" & vbCrLf
    s = s & "  Added:    " & report.dllsAdded.Count & vbCrLf
    s = s & "  Removed:  " & report.dllsRemoved.Count & vbCrLf
    s = s & "  Modified: " & report.dllsModified.Count & vbCrLf
    s = s & vbCrLf
    
    s = s & "Function Changes:" & vbCrLf
    s = s & "  Added:   " & report.functionsAdded.Count & vbCrLf
    s = s & "  Removed: " & report.functionsRemoved.Count & vbCrLf
    s = s & vbCrLf
    
    s = s & "Total Changes: " & report.totalChanges
    
    BuildImportChangeSummary = s
End Function

Public Function GetDetailedImportReport(pe1 As CPEEditor, pe2 As CPEEditor) As String
    'Generate comprehensive human-readable report
    Dim report As CImportChangeReport
    Set report = AnalyzeImportChanges(pe1, pe2)
    
    Dim s As String
    s = String(80, "=") & vbCrLf
    s = s & "IMPORT TABLE ANALYSIS" & vbCrLf
    s = s & String(80, "=") & vbCrLf
    s = s & vbCrLf
    
    s = s & report.summary & vbCrLf
    s = s & vbCrLf
    
    'DLLs Added
    If report.dllsAdded.Count > 0 Then
        s = s & String(80, "-") & vbCrLf
        s = s & "DLLs ADDED (" & report.dllsAdded.Count & "):" & vbCrLf
        s = s & String(80, "-") & vbCrLf
        
        Dim i As Long
        For i = 1 To report.dllsAdded.Count
            s = s & "  [+] " & report.dllsAdded(i) & vbCrLf
        Next
        s = s & vbCrLf
    End If
    
    'DLLs Removed
    If report.dllsRemoved.Count > 0 Then
        s = s & String(80, "-") & vbCrLf
        s = s & "DLLs REMOVED (" & report.dllsRemoved.Count & "):" & vbCrLf
        s = s & String(80, "-") & vbCrLf
        
        For i = 1 To report.dllsRemoved.Count
            s = s & "  [-] " & report.dllsRemoved(i) & vbCrLf
        Next
        s = s & vbCrLf
    End If
    
    'DLLs Modified
    If report.dllsModified.Count > 0 Then
        s = s & String(80, "-") & vbCrLf
        s = s & "DLLs MODIFIED (" & report.dllsModified.Count & "):" & vbCrLf
        s = s & String(80, "-") & vbCrLf
        
        For i = 1 To report.dllsModified.Count
            s = s & "  [~] " & report.dllsModified(i) & vbCrLf
        Next
        s = s & vbCrLf
    End If
    
    'Functions Added
    If report.functionsAdded.Count > 0 Then
        s = s & String(80, "-") & vbCrLf
        s = s & "FUNCTIONS ADDED (" & report.functionsAdded.Count & "):" & vbCrLf
        s = s & String(80, "-") & vbCrLf
        
        'Group by DLL
        Dim byDll As Object
        Set byDll = CreateObject("Scripting.Dictionary")
        
        For i = 1 To report.functionsAdded.Count
            Dim fc As CImportFunctionChange
            Set fc = report.functionsAdded(i)
            
            If Not byDll.exists(LCase(fc.dllName)) Then
                byDll(LCase(fc.dllName)) = New Collection
            End If
            byDll(LCase(fc.dllName)).Add fc.functionName
        Next
        
        Dim key
        For Each key In byDll.Keys
            s = s & "  " & key & ":" & vbCrLf
            Dim j As Long
            For j = 1 To byDll(key).Count
                s = s & "    [+] " & byDll(key)(j) & vbCrLf
            Next
        Next
        s = s & vbCrLf
    End If
    
    'Functions Removed
    If report.functionsRemoved.Count > 0 Then
        s = s & String(80, "-") & vbCrLf
        s = s & "FUNCTIONS REMOVED (" & report.functionsRemoved.Count & "):" & vbCrLf
        s = s & String(80, "-") & vbCrLf
        
        'Group by DLL
        Set byDll = CreateObject("Scripting.Dictionary")
        
        For i = 1 To report.functionsRemoved.Count
            Set fc = report.functionsRemoved(i)
            
            If Not byDll.exists(LCase(fc.dllName)) Then
                byDll(LCase(fc.dllName)) = New Collection
            End If
            byDll(LCase(fc.dllName)).Add fc.functionName
        Next
        
        For Each key In byDll.Keys
            s = s & "  " & key & ":" & vbCrLf
            For j = 1 To byDll(key).Count
                s = s & "    [-] " & byDll(key)(j) & vbCrLf
            Next
        Next
        s = s & vbCrLf
    End If
    
    s = s & String(80, "=") & vbCrLf
    
    GetDetailedImportReport = s
End Function

Public Sub PrintImportAnalysis(pe1 As CPEEditor, pe2 As CPEEditor)
    'Quick debug output
    Debug.Print GetDetailedImportReport(pe1, pe2)
End Sub

Public Function QuickImportCheck(pe1 As CPEEditor, pe2 As CPEEditor) As String
    'One-liner summary for quick checks
    Dim report As CImportChangeReport
    Set report = AnalyzeImportChanges(pe1, pe2)
    
    If report.imphashMatch Then
        QuickImportCheck = "Imphash MATCH - imports identical"
    Else
        QuickImportCheck = "Imphash CHANGED - " & _
                          report.dllsAdded.Count & " DLLs added, " & _
                          report.dllsRemoved.Count & " DLLs removed, " & _
                          report.dllsModified.Count & " DLLs modified, " & _
                          report.functionsAdded.Count & " functions added, " & _
                          report.functionsRemoved.Count & " functions removed"
    End If
End Function

'============================================================================
' USAGE EXAMPLES
'============================================================================

'Quick check:
'  Debug.Print QuickImportCheck(pe1, pe2)
'
'Detailed analysis:
'  Call PrintImportAnalysis(pe1, pe2)
'
'Programmatic access:
'  Dim report As CImportChangeReport
'  Set report = AnalyzeImportChanges(pe1, pe2)
'  If Not report.imphashMatch Then
'      Debug.Print "DLLs added: " & report.dllsAdded.Count
'      Dim i As Long
'      For i = 1 To report.functionsAdded.Count
'          Dim fc As CImportFunctionChange
'          Set fc = report.functionsAdded(i)
'          Debug.Print fc.dllName & "!" & fc.functionName
'      Next
'  End If
