Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. Imports System.Threading
  2. Imports System.Security.Policy
  3. Imports System.Security.Cryptography
  4. Imports System.Reflection
  5. Imports System.IO
  6. Imports System.Collections.Generic
  7. Imports System
  8.  
  9. Namespace SecStarter
  10.  
  11. <Serializable> _
  12. Friend Class SecureLoader
  13. Private dllList As New Dictionary(Of String, Byte())()
  14.  
  15. Private executableBinary() As Byte
  16.  
  17. Friend Sub LoadAssembies(ByVal password As String)
  18. Dim fileList() As String = Directory.GetFiles(Environment.CurrentDirectory)
  19.  
  20. For Each fileName As String In fileList
  21. If fileName.EndsWith(".exee") Then
  22. Me.executableBinary = LoadAndDecrypt(fileName, password)
  23. End If
  24.  
  25. If fileName.EndsWith(".dlle") Then
  26. Dim startIndex As Integer = fileName.LastIndexOf("\") + 1
  27. Dim nameOnly As String = fileName.Substring(startIndex, fileName.Length - (startIndex + 5))
  28. Me.dllList.Add(nameOnly, LoadAndDecrypt(fileName, password))
  29. End If
  30. Next fileName
  31. End Sub
  32.  
  33. Friend Sub StartTheApp()
  34. Dim theThread As New Thread(AddressOf MainWorkerThread)
  35. theThread.Start()
  36. End Sub
  37.  
  38. Public Sub MainWorkerThread()
  39. Dim adevidence As Evidence = AppDomain.CurrentDomain.Evidence
  40. Dim domain As AppDomain = AppDomain.CreateDomain("Debug", adevidence)
  41.  
  42. AddHandler domain.AssemblyResolve, AddressOf Domain_AssemblyResolve
  43.  
  44. domain.ExecuteAssemblyByName("TestingDebug.exe")
  45. End Sub
  46.  
  47. Private Function Domain_AssemblyResolve(ByVal sender As Object, ByVal args As ResolveEventArgs) As System.Reflection.Assembly
  48. If args.Name.EndsWith(".exe") Then
  49. Dim assembly As System.Reflection.Assembly = System.Reflection.Assembly.Load(Me.executableBinary)
  50. Return assembly
  51. Else
  52. Dim assemblyData() As String = args.Name.Split(","c)
  53. Dim assembly As System.Reflection.Assembly = System.Reflection.Assembly.Load(dllList(assemblyData(0)))
  54. Return assembly
  55. End If
  56. End Function
  57.  
  58. Private Function LoadAndDecrypt(ByVal filename As String, ByVal password As String) As Byte()
  59. Dim theAssemblyBytes() As Byte = Nothing
  60.  
  61. If File.Exists(filename) Then
  62. Dim fileBlob() As Byte = File.ReadAllBytes(filename)
  63. Dim saltData(7) As Byte
  64. Dim fileData((fileBlob.Length - 8) - 1) As Byte
  65.  
  66. ' extract the salt that we need for proper key derivation from pwd (added as 8 byte header by the cryptor)
  67. For i As Integer = 0 To 7
  68. saltData(i) = fileBlob(i)
  69. Next i
  70.  
  71. ' binary dll data follows after the salt
  72. For i As Integer = 8 To fileBlob.Length - 1
  73. fileData(i - 8) = fileBlob(i)
  74. Next i
  75.  
  76. ' password + salt -> key
  77. Dim deriveBytes As New Rfc2898DeriveBytes(password, saltData)
  78. Dim memoryStream As New MemoryStream()
  79.  
  80. Dim aesCipher As Aes = New AesManaged()
  81. aesCipher.Key = deriveBytes.GetBytes(aesCipher.KeySize \ 8)
  82. aesCipher.IV = deriveBytes.GetBytes(aesCipher.BlockSize \ 8)
  83. aesCipher.Mode = CipherMode.CBC
  84. aesCipher.Padding = PaddingMode.None
  85.  
  86. ' decrypt the assembly
  87. Dim cryptoStream As New CryptoStream(memoryStream, aesCipher.CreateDecryptor(), CryptoStreamMode.Write)
  88. cryptoStream.Write(fileData, 0, fileData.Length)
  89. cryptoStream.Close()
  90.  
  91. theAssemblyBytes = memoryStream.ToArray()
  92. End If
  93.  
  94. Return theAssemblyBytes
  95. End Function
  96. End Class
  97. End Namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement