Here is a module with code for encrypting and decrypting strings.
Code:
Imports System.Security.Cryptography
Module Encryption
Public Function AESEncrypt(ByVal Key As String, ByVal Data As String) As String
Try
Dim AES As AesCryptoServiceProvider
With AES
.Mode = CipherMode.ECB
.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(Key)
.IV = .Key
End With
Dim AESEncryptor As ICryptoTransform = AES.CreateEncryptor
Dim DataBuffer As Byte()
Dim EncryptedData As Byte()
DataBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(Data)
EncryptedData = AESEncryptor.TransformFinalBlock(DataBuffer, 0, DataBuffer.Length)
Return ByteToString(EncryptedData)
Catch ex As Exception
MessageBox.Show("The Following Error(s) occured: " & ex.Message, "Error", MessageBoxButtons.OK)
End Try
End Function
Public Function AESEncrypt(ByVal Key() As Byte, ByVal Data() As Byte) As Byte()
Try
Dim AES As AesCryptoServiceProvider
With AES
.Mode = CipherMode.ECB
.Key = Key
.IV = .Key
End With
Dim AESEncryptor As ICryptoTransform = AES.CreateEncryptor
Dim DataBuffer As Byte()
Dim EncryptedData As Byte()
DataBuffer = Data
EncryptedData = AESEncryptor.TransformFinalBlock(DataBuffer, 0, DataBuffer.Length)
Return EncryptedData
Catch ex As Exception
MessageBox.Show("The Following Error(s) occured: " & ex.Message, "Error", MessageBoxButtons.OK)
End Try
End Function
Public Function AESDecrypt(ByVal Key() As Byte, ByVal Data() As Byte) As Byte()
Try
Dim AES As AesCryptoServiceProvider
With AES
.Mode = CipherMode.ECB
.Key = Key
.IV = .Key
End With
Dim AESDecryptor As ICryptoTransform = AES.CreateDecryptor
Dim DataBuffer As Byte()
Dim DecryptedData As Byte()
DataBuffer = Data
DecryptedData = AESDecryptor.TransformFinalBlock(DataBuffer, 0, DataBuffer.Length)
Return DecryptedData
Catch ex As Exception
MessageBox.Show("The Following Error(s) occured: " & ex.Message, "Error", MessageBoxButtons.OK)
End Try
End Function
Public Function AESDecrypt(ByVal Key As String, ByVal Data As String) As String
Try
Dim AES As AesCryptoServiceProvider
With AES
.Mode = CipherMode.ECB
.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(Key)
.IV = .Key
End With
Dim AESDecryptor As ICryptoTransform = AES.CreateDecryptor
Dim DataBuffer As Byte()
Dim DecryptedData As Byte()
DataBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(Data)
DecryptedData = AESDecryptor.TransformFinalBlock(DataBuffer, 0, DataBuffer.Length)
Return ByteToString(DecryptedData)
Catch ex As Exception
MessageBox.Show("The Following Error(s) occured: " & ex.Message, "Error", MessageBoxButtons.OK)
End Try
End Function
Public Function RC2Encrypt(ByVal Key As String, ByVal Data As String) As Byte()
Try
Dim RC2 As RC2CryptoServiceProvider
With RC2
.Mode = CipherMode.ECB
.UseSalt = True
.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(Key)
.IV = .Key
End With
Dim RC2Encryptor As ICryptoTransform = RC2.CreateEncryptor
Dim databuffer As Byte()
Dim EncryptedData As Byte()
databuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(Data)
EncryptedData = RC2Encryptor.TransformFinalBlock(databuffer, 0, databuffer.Length)
Return EncryptedData
Catch ex As Exception
MessageBox.Show("The Following Error(s) occured: " & ex.Message, "Error", MessageBoxButtons.OK)
End Try
End Function
Public Function RC2Encrypt(ByVal Key() As Byte, ByVal Data() As Byte) As Byte()
Try
Dim RC2 As RC2CryptoServiceProvider
With RC2
.Mode = CipherMode.ECB
.UseSalt = True
.Key = Key
.IV = .Key
End With
Dim RC2Encryptor As ICryptoTransform = RC2.CreateEncryptor
Dim databuffer As Byte()
Dim EncryptedData As Byte()
databuffer = Data
EncryptedData = RC2Encryptor.TransformFinalBlock(databuffer, 0, databuffer.Length)
Return EncryptedData
Catch ex As Exception
MessageBox.Show("The Following Error(s) occured: " & ex.Message, "Error", MessageBoxButtons.OK)
End Try
End Function
Public Function RC2Decrypt(ByVal key() As Byte, ByVal data() As Byte) As Byte()
Try
Dim RC2 As RC2CryptoServiceProvider
With RC2
.Mode = CipherMode.ECB
.UseSalt = True
.Key = key
.IV = .Key
End With
Dim RC2Decryptor As ICryptoTransform = RC2.CreateDecryptor
Dim databuffer As Byte()
Dim Decrypteddata As Byte()
databuffer = data
Decrypteddata = RC2Decryptor.TransformFinalBlock(databuffer, 0, databuffer.Length)
Return Decrypteddata
Catch ex As Exception
MessageBox.Show("The Following Error(s) occured: " & ex.Message, "Error", MessageBoxButtons.OK)
End Try
End Function
Public Function RC2Decrypt(ByVal Key As String, ByVal Data As String) As String
Try
Dim RC2 As RC2CryptoServiceProvider
With RC2
.Mode = CipherMode.ECB
.UseSalt = True
.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(Key)
.IV = .Key
End With
Dim RC2Decryptor As ICryptoTransform = RC2.CreateDecryptor
Dim DataBuffer As Byte()
Dim DecryptedData As Byte()
DataBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(Data)
DecryptedData = RC2Decryptor.TransformFinalBlock(DataBuffer, 0, DataBuffer.Length)
Return ByteToString(DecryptedData)
Catch ex As Exception
MessageBox.Show("The Following Error(s) occured: " & ex.Message, "Error", MessageBoxButtons.OK)
End Try
End Function
Public Function DESEncrypt(ByVal Key As String, ByVal Data As String) As String
Try
Dim DES As DESCryptoServiceProvider
With DES
.Mode = CipherMode.ECB
.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(Key)
.IV = .Key
End With
Dim DESEncryptor As ICryptoTransform = DES.CreateEncryptor
Dim DataBuffer As Byte()
Dim EncryptedData As Byte()
DataBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(Data)
EncryptedData = DESEncryptor.TransformFinalBlock(DataBuffer, 0, DataBuffer.Length)
Return ByteToString(EncryptedData)
Catch ex As Exception
MessageBox.Show("The Following Error(s) occured: " & ex.Message, "Error", MessageBoxButtons.OK)
End Try
End Function
Public Function DESEncrypt(ByVal key() As Byte, ByVal data() As Byte) As Byte()
Try
Dim DES As DESCryptoServiceProvider
With DES
.Mode = CipherMode.ECB
.Key = key
.IV = .Key
End With
Dim DESEncryptor As ICryptoTransform = DES.CreateEncryptor
Dim DataBuffer As Byte()
Dim Encrypteddata As Byte()
DataBuffer = data
Encrypteddata = DESEncryptor.TransformFinalBlock(DataBuffer, 0, DataBuffer.Length)
Return Encrypteddata
Catch ex As Exception
MessageBox.Show("The Following Error(s) occured: " & ex.Message, "Error", MessageBoxButtons.OK)
End Try
End Function
Public Function DESDecrypt(ByVal Key As String, ByVal Data As String) As String
Try
Dim DES As DESCryptoServiceProvider
With DES
.Mode = CipherMode.ECB
.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(Key)
.IV = .Key
End With
Dim DESDecryptor As ICryptoTransform = DES.CreateDecryptor
Dim DataBuffer As Byte()
Dim DecryptedData As Byte()
DataBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(Data)
DecryptedData = DESDecryptor.TransformFinalBlock(DataBuffer, 0, DataBuffer.Length)
Return ByteToString(DecryptedData)
Catch ex As Exception
MessageBox.Show("The Following Error(s) occured: " & ex.Message, "Error", MessageBoxButtons.OK)
End Try
End Function
Public Function DESDecrypt(ByVal Key() As Byte, ByVal data() As Byte) As Byte()
Try
Dim DES As DESCryptoServiceProvider
With DES
.Mode = CipherMode.ECB
.Key = Key
.IV = .Key
End With
Dim DESDecryptor As ICryptoTransform = DES.CreateDecryptor
Dim DataBuffer As Byte()
Dim DecryptedData As Byte()
DataBuffer = data
DecryptedData = DESDecryptor.TransformFinalBlock(DataBuffer, 0, DataBuffer.Length)
Return DecryptedData
Catch ex As Exception
MessageBox.Show("The Following Error(s) occured: " & ex.Message, "Error", MessageBoxButtons.OK)
End Try
End Function
Public Function TripleDESEncrypt(ByVal Key As String, ByVal Data As String) As String
Try
Dim TripleDes As TripleDESCryptoServiceProvider
With TripleDes
.Mode = CipherMode.ECB
.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(Key)
.IV = .Key
End With
Dim TripleDESEncryptor As ICryptoTransform = TripleDes.CreateEncryptor
Dim DataBuffer As Byte()
Dim EncryptedData As Byte()
DataBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(Data)
EncryptedData = TripleDESEncryptor.TransformFinalBlock(DataBuffer, 0, DataBuffer.Length)
Return ByteToString(EncryptedData)
Catch ex As Exception
MessageBox.Show("The Following Error(s) occured: " & ex.Message, "Error", MessageBoxButtons.OK)
End Try
End Function
Public Function TripleDESEncrypt(ByVal key As Byte(), ByVal data As Byte()) As Byte()
Try
Dim TripleDes As TripleDESCryptoServiceProvider
With TripleDes
.Mode = CipherMode.ECB
.Key = key
.IV = .Key
End With
Dim TripleDESEncryptor As ICryptoTransform = TripleDes.CreateEncryptor
Dim DataBuffer As Byte()
Dim EncryptedData As Byte()
DataBuffer = data
EncryptedData = TripleDESEncryptor.TransformFinalBlock(DataBuffer, 0, DataBuffer.Length)
Return EncryptedData
Catch ex As Exception
MessageBox.Show("The Following Error(s) occured: " & ex.Message, "Error", MessageBoxButtons.OK)
End Try
End Function
Public Function TriplesDESDecrypt(ByVal key As Byte(), ByVal Data As Byte()) As Byte()
Try
Dim TripleDes As TripleDESCryptoServiceProvider
With TripleDes
.Mode = CipherMode.ECB
.Key = key
.IV = .Key
End With
Dim TripleDESDecryptor As ICryptoTransform = TripleDes.CreateDecryptor
Dim DataBuffer As Byte()
Dim DecryptedData As Byte()
DataBuffer = Data
DecryptedData = TripleDESDecryptor.TransformFinalBlock(DataBuffer, 0, DataBuffer.Length)
Return DecryptedData
Catch ex As Exception
MessageBox.Show("The Following Error(s) occured: " & ex.Message, "Error", MessageBoxButtons.OK)
End Try
End Function
Public Function TripleDESDecrypt(ByVal Key As String, ByVal Data As String) As String
Try
Dim TripleDes As TripleDESCryptoServiceProvider
With TripleDes
.Mode = CipherMode.ECB
.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(Key)
.IV = .Key
End With
Dim TripleDESDecryptor As ICryptoTransform = TripleDes.CreateDecryptor
Dim DataBuffer As Byte()
Dim DecryptedData As Byte()
DataBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(Data)
DecryptedData = TripleDESDecryptor.TransformFinalBlock(DataBuffer, 0, DataBuffer.Length)
Return ByteToString(DecryptedData)
Catch ex As Exception
MessageBox.Show("The Following Error(s) occured: " & ex.Message, "Error", MessageBoxButtons.OK)
End Try
End Function
Public Function ByteToString(ByVal Bytes As Byte()) As String
Return Convert.ToString(Bytes)
End Function
End Module
Here is another module. This time its for setting debug privileges in the case that you dont have them.
Code:
Module SetDebugPrivileges
Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, ByRef lpLuid As LUID) As Int32
Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (ByVal TokenHandle As Int32, ByVal DisableAllPrivileges As Int32, ByRef NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Int32, ByRef PreviousState As TOKEN_PRIVILEGES, ByRef ReturnLength As Int32) As Int32
Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Int32, ByVal DesiredAccess As Int32, ByRef TokenHandle As Int32) As Int32
Private Declare Function GetCurrentProcess Lib "kernel32.dll" () As Int32
Private Const SE_DEBUG_NAME As String = "SeDebugPrivilege"
Private Const TOKEN_ADJUST_PRIVILEGES As Int32 = &H20
Private Const TOKEN_QUERY As Int32 = &H8
Private Const SE_PRIVILEGE_ENABLED As Int32 = &H2
Private Structure LUID
Dim LowPart As Int32
Dim HighPart As Int32
End Structure
Private Structure LUID_AND_ATTRIBUTES
Dim pLuid As LUID
Dim Attributes As Int32
End Structure
Private Structure TOKEN_PRIVILEGES
Dim PrivilegeCount As Int32
Dim TheLuid As LUID
Dim Attributes As Int32
End Structure
Public Function LoadPrivilege(ByVal Privilege As String) As Boolean
On Error GoTo ErrHandler
Dim ProcHandle As Int32
Dim htoken As Int32
Dim tokenPrivileges As TOKEN_PRIVILEGES
Dim SEDebugNameValue As LUID
Dim tkpNewButIgnored As TOKEN_PRIVILEGES
ProcHandle = GetCurrentProcess()
OpenProcessToken(ProcHandle, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, htoken)
LookupPrivilegeValue("", Privilege, SEDebugNameValue)
With tokenPrivileges
.PrivilegeCount = 1
.TheLuid = SEDebugNameValue
.Attributes = SE_PRIVILEGE_ENABLED
End With
AdjustTokenPrivileges(htoken, False, tokenPrivileges, Len(tokenPrivileges), tkpNewButIgnored, Nothing)
Return True
Exit Function
ErrHandler:
Return False
End Function
End Module
And here is a module for DLL injection on 32-Bit OS's
Code:
Module InjLib
'CreateRemoteThread for calling loadlibrary in the target process address space to load our Dll
Private Declare Function CreateRemoteThread Lib "kernel32.dll" (ByVal hProcess As Int32, ByVal lpThreadAttributes As Int32, ByVal dwStackSize As Int32, ByVal lpStartAddress As Int32, ByVal lpParameter As Int32, ByVal dwCreationFlags As Int32, ByRef lpThreadId As Int32) As Int32
'VirtualAllocEx to allocate space in our target process so that we can write the path to our Dll
Private Declare Function VirtualAllocEx Lib "kernel32.dll" (ByVal hProcess As Int32, ByVal lpAddress As Int32, ByVal dwSize As Int32, ByVal flAllocationType As Int32, ByVal flProtect As Int32) As Int32
'WriteProcessMemory to write the path to our Dll in the target process address space
Private Declare Function WriteProcessMemory Lib "kernel32.dll" (ByVal hProcess As Int32, ByVal lpBaseAddress As Int32, ByVal lpBuffer As String, ByVal nSize As Int32, ByRef lpNumberOfBytesWritten As Int32) As Int32
'VirtualFreeEx to clean up when done
Private Declare Function VirtualFreeEx Lib "kernel32.dll" (ByVal hProcess As Int32, ByVal lpAddress As Int32, ByRef dwSize As Int32, ByVal dwFreeType As Int32) As Int32
'Get ModuleHandle to get a handle to LoadLibrary so we can use the Handle to get its Address in the target Process' space
Private Declare Function GetModuleHandle Lib "kernel32.dll" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Int32
'GetProcAddress to get the address that LoadLibraryA resides at
Private Declare Function GetProcAddress Lib "kernel32.dll" (ByVal hModule As Int32, ByVal lpProcName As String) As Int32
'OpenProcess to get a handle to our target process and open it with the rights we require
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Int32, ByVal bInheritHandle As Int32, ByVal dwProcessId As Int32) As Int32
'CloseHandle to Close all open handles we needed
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Int32) As Int32
Private Const Create_Suspended As Int32 = &H4 'To freeze process when we call createremotethread
Private Const process_vm_operation As Int32 = &H8 ' Access Rights
Private Const process_create_thread As Int32 = &H2
Private Const process_suspend_resume As Int32 = &H800
Private Const process_vm_write As Int32 = &H20 ' Access Rights
Private Const process_vm_read As Int32 = &H10 ' Access Rights
Private Const mem_commit As Int32 = &H1000 ' What to do with memory from VirtualallocEx
Private Const mem_release As Int32 = &H8000 ' Tells the Computer to free memory when called with VirtualFree
Private Const page_readwrite As Int32 = &H4
Private Access As Int32 = process_vm_read Or process_vm_write Or process_vm_operation Or process_create_thread
Public Function InjectDlls(ByVal ProcessName As String, ByVal DllPaths() As String) As int32
Dim ProcHandle As Int32 ' Handle to Target Process
Dim DllVirtLoc As Int32 ' Address of Dll Path
Dim Inject As Int32 ' Error Checking
Dim CreateThread As Int32 ' Error Checking
Dim ThreadID As Int32 ' Handle to our Created Thread
Dim MHandle As Int32 ' Handle to Kernel32.dll
Dim i As Int32 ' Counter
Dim TargetProc As Process() = Process.GetProcessesByName(ProcessName) ' Gets Process info
MHandle = GetModuleHandle("Kernel32.dll") ' Gets Handle to Kernel32.dll
If MHandle = 0 Then
MessageBox.Show("Could not get a handle to Kernel32.dll", "Error", MessageBoxButtons.OK)
Return 0
Exit Function
Else
ProcHandle = OpenProcess(Access, 0, TargetProc(0).Id) ' Gets Handle to Process and opens with our desired rights
If ProcHandle = 0 Then
MessageBox.Show("Could not get a handle to Target process", "Error", MessageBoxButtons.OK)
CloseHandle(MHandle) ' Closes handle to kernel32.dll because we could not open our target process
Return 0
Exit Function
Else
For i = 0 To UBound(DllPaths) - 1
System.Threading.Thread.Sleep(100) ' Our Delay for initial Injection and subsequent injection
DllVirtLoc = VirtualAllocEx(ProcHandle, 0, DllPaths(i), mem_commit, page_readwrite) ' Allocates Space in Target Address Space
If DllVirtLoc = 0 Then
MessageBox.Show("Could not allocate space in target process", "Error", MessageBoxButtons.OK)
CloseHandle(MHandle) ' Closes Handle to Kernel32.dll because we could not allocate space
CloseHandle(ProcHandle) ' Closes Handle to Process becausewe could not allocate the space
Else
Inject = WriteProcessMemory(ProcHandle, DllVirtLoc, DllPaths(i), DllPaths(i).Length + 1, Nothing) ' Writes our Dll's path to Targets Address Space
If Inject = 0 Then
MessageBox.Show("Could not write to process' address space", "Error", MessageBoxButtons.OK)
VirtualFreeEx(ProcHandle, DllVirtLoc, 0, mem_release) ' Free Allocated Space because writing failed
CloseHandle(MHandle) ' Close handle to kernel32.dll because writing failed
CloseHandle(ProcHandle) ' Close Handle to Process because writing failed
Else
CreateThread = CreateRemoteThread(ProcHandle, 0, 0, GetProcAddress(MHandle, "LoadLibraryA"), DllVirtLoc, 0, ThreadID)
If CreateThread = 0 Then
MessageBox.Show("Could not create remote thread", "Error", MessageBoxButtons.OK)
VirtualFreeEx(ProcHandle, DllVirtLoc, 0, mem_release) ' Frees Allocated space because we could not create our remote thread
CloseHandle(MHandle) ' Closes Handle to Kernel32.dll because we could not create our remote thread
CloseHandle(ProcHandle) ' Closes Handle to Target Process because we could not create our remote thread
Return 0
Exit Function
Else
VirtualFreeEx(ProcHandle, DllVirtLoc, 0, mem_release) ' Frees Allocated Space because we are done
End If
End If
End If
Return 1 ' Returns 1 for Success 0 for failure declare recieving variable as array
Next i
CloseHandle(MHandle) ' Close Handle to Kernel32.dll because we are done
CloseHandle(ProcHandle) ' Close Handle to Target Process because we are done
End If
End If
End Function
End Module