執行時生成其它EXE檔案(VB6)

evancss發表於2006-01-25

大家經常能看到一些程式,如木馬程式能夠生成一個完整的可執行程式出來,是不是覺得很有意思?其實VB也能做到這一點。

首先我們需要把一個EXE檔案轉為容易在程式中儲存的文字形式,這裡我轉成16進製表示,大家也可以轉為Base64或其它相容的格式。下面這個函式能將二進位制檔案轉為VB可識別的語句。

Function ConvBinToHex(byval strVarName as string,byval strInFile as string,byval strOutFile as string)
    Open strInFile For Binary Access Read As #1
        ReDim bBytes(LOF(1))
        Get 1, , bBytes
    Close #1
   
    Dim strHex As String, strHexTmp As String
    Dim i As Long, n As Long
    strHex = strVarName & " = """
   
    For i = 0 To UBound(bBytes)
        strHexTmp = Hex(bBytes(i))
        If Len(strHexTmp) = 1 Then
            strHexTmp = "0" & strHexTmp
        End If
       
        n = n + 1
        If n = 50 Then      '設定一行顯示50個位元組
            strHex = strHex & """" & vbCrLf & strVarName & " = " & strVarName & " & """ & strHexTmp & " "
            n = 0
        Else               
            strHex = strHex & strHexTmp & " "
        End If
    Next
   
    strHex = Left(strHex, Len(strHex) - 1) & """"
   
    Open strOutFile For Output As #1
        Print #1, strHex
    Close #1

End Function

執行上面的函式後,我們會得到這樣的程式碼:

trBin = "4D 5A 90 00 03 00 00 00 04 00 00 00 FF FF 00 00 B8 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
strBin = strBin & "00 00 00 00 00 00 00 00 00 00 00 C0 00 00 00 0E 1F BA 0E 00 B4 09 CD 21 B8 01 4C CD 21 54 68 69 73 20 70 72 6F 67 72 61 6D 20 63 61 6E 6E 6F 74 20 62 "
.............

將這些程式碼放入VB的工程原始碼中,再使用下面的函式即可完成將文字恢復為二進位制的功能:

Public Function ConvHexToBin(ByRef strHex As String, ByRef OutputFile As String)
    Dim vHex As Variant, bBytes() As Byte, i As Long
    vHex = Split(strHex, " ", -1)
    ReDim bBytes(UBound(vHex))
    For i = 0 To UBound(vHex)
        bBytes(i) = CDec("&H" & vHex(i))
    Next
   
    Open OutputFile For Binary Access Write As #1
        Put #1, , bBytes
    Close #1
End Function

大家有什麼好的想法歡迎留言:)

相關文章