Browse Source

getpunk windows SetConsoleMode take2

master
Julian Noble 4 days ago
parent
commit
56d77c8b8f
  1. 56
      getpunk.cmd
  2. 55
      src/scriptapps/getpunk.ps1

56
getpunk.cmd

@ -1029,48 +1029,54 @@ git branch --set-upstream-to=origin/master master
Set-Location $launchdir #restore original CWD
# Define the necessary Win32 API functions and constants
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public class NativeMethods
public class WinAPI
{
[DllImport("kernel32.dll", SetLastError=true)]
public static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool GetConsoleMode(IntPtr hConsoleHandle, out int lpMode);
// Console Input/Output Handles
public const int STD_OUTPUT_HANDLE = -11;
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool SetConsoleMode(IntPtr hConsoleHandle, int dwMode);
// Console Modes
public const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;
public const uint DISABLE_NEWLINE_AUTO_RETURN = 0x0008;
// Constants for standard handles
public const int STD_INPUT_HANDLE = -10;
public const int STD_OUTPUT_HANDLE = -11;
public const int STD_ERROR_HANDLE = -12;
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr GetStdHandle(int nStdHandle);
// Console Input Modes
public const int ENABLE_VIRTUAL_TERMINAL_INPUT = 0x0200;
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);
// Console Output Modes
public const int ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
}
"@
# Get the output handle
$hConsoleOutput = [NativeMethods]::GetStdHandle([NativeMethods]::STD_OUTPUT_HANDLE)
# Get the handle to the console output buffer
$stdoutHandle = [WinAPI]::GetStdHandle([WinAPI]::STD_OUTPUT_HANDLE)
# Get the current output mode
$currentMode = 0
[NativeMethods]::GetConsoleMode($hConsoleOutput, [ref]$currentMode) | Out-Null
# Get the current console mode
[uint]$currentMode = 0
if (![WinAPI]::GetConsoleMode($stdoutHandle, [ref]$currentMode)) {
Write-Error "Failed to get console mode. Error code: $($LAST_ERROR)"
return
}
# Enable Virtual Terminal Processing
$newMode = $currentMode -bor [NativeMethods]::ENABLE_VIRTUAL_TERMINAL_PROCESSING
# Enable virtual terminal processing
$newMode = $currentMode -bor [WinAPI]::ENABLE_VIRTUAL_TERMINAL_PROCESSING
# Set the new console mode
[NativeMethods]::SetConsoleMode($hConsoleOutput, $newMode) | Out-Null
if (![WinAPI]::SetConsoleMode($stdoutHandle, $newMode)) {
Write-Error "Failed to set console mode. Error code: $($LAST_ERROR)"
return
}
write-host "`e[92m getpunk done `e[m"
Write-Host "Virtual terminal processing enabled successfully."
write-host "`e[92m getpunk done `e[m"
#</powershell-payload>
#<powershell-pre-launch-subprocess>

55
src/scriptapps/getpunk.ps1

@ -120,45 +120,50 @@ git branch --set-upstream-to=origin/master master
Set-Location $launchdir #restore original CWD
# Define the necessary Win32 API functions and constants
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public class NativeMethods
public class WinAPI
{
[DllImport("kernel32.dll", SetLastError=true)]
public static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool GetConsoleMode(IntPtr hConsoleHandle, out int lpMode);
// Console Input/Output Handles
public const int STD_OUTPUT_HANDLE = -11;
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool SetConsoleMode(IntPtr hConsoleHandle, int dwMode);
// Console Modes
public const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;
public const uint DISABLE_NEWLINE_AUTO_RETURN = 0x0008;
// Constants for standard handles
public const int STD_INPUT_HANDLE = -10;
public const int STD_OUTPUT_HANDLE = -11;
public const int STD_ERROR_HANDLE = -12;
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr GetStdHandle(int nStdHandle);
// Console Input Modes
public const int ENABLE_VIRTUAL_TERMINAL_INPUT = 0x0200;
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);
// Console Output Modes
public const int ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
}
"@
# Get the output handle
$hConsoleOutput = [NativeMethods]::GetStdHandle([NativeMethods]::STD_OUTPUT_HANDLE)
# Get the handle to the console output buffer
$stdoutHandle = [WinAPI]::GetStdHandle([WinAPI]::STD_OUTPUT_HANDLE)
# Get the current output mode
$currentMode = 0
[NativeMethods]::GetConsoleMode($hConsoleOutput, [ref]$currentMode) | Out-Null
# Get the current console mode
[uint]$currentMode = 0
if (![WinAPI]::GetConsoleMode($stdoutHandle, [ref]$currentMode)) {
Write-Error "Failed to get console mode. Error code: $($LAST_ERROR)"
return
}
# Enable Virtual Terminal Processing
$newMode = $currentMode -bor [NativeMethods]::ENABLE_VIRTUAL_TERMINAL_PROCESSING
# Enable virtual terminal processing
$newMode = $currentMode -bor [WinAPI]::ENABLE_VIRTUAL_TERMINAL_PROCESSING
# Set the new console mode
[NativeMethods]::SetConsoleMode($hConsoleOutput, $newMode) | Out-Null
if (![WinAPI]::SetConsoleMode($stdoutHandle, $newMode)) {
Write-Error "Failed to set console mode. Error code: $($LAST_ERROR)"
return
}
Write-Host "Virtual terminal processing enabled successfully."
write-host "`e[92m getpunk done `e[m"
write-host "`e[92m getpunk done `e[m"

Loading…
Cancel
Save