Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Threading
- Imports System.Security.Policy
- Imports System.Security.Cryptography
- Imports System.Reflection
- Imports System.IO
- Imports System.Collections.Generic
- Imports System
- Namespace SecStarter
- <Serializable> _
- Friend Class SecureLoader
- Private dllList As New Dictionary(Of String, Byte())()
- Private executableBinary() As Byte
- Friend Sub LoadAssembies(ByVal password As String)
- Dim fileList() As String = Directory.GetFiles(Environment.CurrentDirectory)
- For Each fileName As String In fileList
- If fileName.EndsWith(".exee") Then
- Me.executableBinary = LoadAndDecrypt(fileName, password)
- End If
- If fileName.EndsWith(".dlle") Then
- Dim startIndex As Integer = fileName.LastIndexOf("\") + 1
- Dim nameOnly As String = fileName.Substring(startIndex, fileName.Length - (startIndex + 5))
- Me.dllList.Add(nameOnly, LoadAndDecrypt(fileName, password))
- End If
- Next fileName
- End Sub
- Friend Sub StartTheApp()
- Dim theThread As New Thread(AddressOf MainWorkerThread)
- theThread.Start()
- End Sub
- Public Sub MainWorkerThread()
- Dim adevidence As Evidence = AppDomain.CurrentDomain.Evidence
- Dim domain As AppDomain = AppDomain.CreateDomain("Debug", adevidence)
- AddHandler domain.AssemblyResolve, AddressOf Domain_AssemblyResolve
- domain.ExecuteAssemblyByName("TestingDebug.exe")
- End Sub
- Private Function Domain_AssemblyResolve(ByVal sender As Object, ByVal args As ResolveEventArgs) As System.Reflection.Assembly
- If args.Name.EndsWith(".exe") Then
- Dim assembly As System.Reflection.Assembly = System.Reflection.Assembly.Load(Me.executableBinary)
- Return assembly
- Else
- Dim assemblyData() As String = args.Name.Split(","c)
- Dim assembly As System.Reflection.Assembly = System.Reflection.Assembly.Load(dllList(assemblyData(0)))
- Return assembly
- End If
- End Function
- Private Function LoadAndDecrypt(ByVal filename As String, ByVal password As String) As Byte()
- Dim theAssemblyBytes() As Byte = Nothing
- If File.Exists(filename) Then
- Dim fileBlob() As Byte = File.ReadAllBytes(filename)
- Dim saltData(7) As Byte
- Dim fileData((fileBlob.Length - 8) - 1) As Byte
- ' extract the salt that we need for proper key derivation from pwd (added as 8 byte header by the cryptor)
- For i As Integer = 0 To 7
- saltData(i) = fileBlob(i)
- Next i
- ' binary dll data follows after the salt
- For i As Integer = 8 To fileBlob.Length - 1
- fileData(i - 8) = fileBlob(i)
- Next i
- ' password + salt -> key
- Dim deriveBytes As New Rfc2898DeriveBytes(password, saltData)
- Dim memoryStream As New MemoryStream()
- Dim aesCipher As Aes = New AesManaged()
- aesCipher.Key = deriveBytes.GetBytes(aesCipher.KeySize \ 8)
- aesCipher.IV = deriveBytes.GetBytes(aesCipher.BlockSize \ 8)
- aesCipher.Mode = CipherMode.CBC
- aesCipher.Padding = PaddingMode.None
- ' decrypt the assembly
- Dim cryptoStream As New CryptoStream(memoryStream, aesCipher.CreateDecryptor(), CryptoStreamMode.Write)
- cryptoStream.Write(fileData, 0, fileData.Length)
- cryptoStream.Close()
- theAssemblyBytes = memoryStream.ToArray()
- End If
- Return theAssemblyBytes
- End Function
- End Class
- End Namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement