Kilkat

[powershell] 16진수 -> byte 문자열 본문

Script/Windows

[powershell] 16진수 -> byte 문자열

KimKwangWoon 2025. 2. 14. 00:03
function Convert-HexStringToByteArray {
     param ([string]$hexString)

     $hexString = $hexString -replace "\\x", ""
     if ($hexString.Length % 2 -ne 0) {
         throw "16진수 문자열의 길이가 홀수입니다. 유효한 16진수 문자열이어야 합니다."
     }

     $bytes = @()
     for ($i = 0; $i -lt $hexString.Length; $i += 2) {
         $hexPair = $hexString.Substring($i, 2)
         if ($hexPair -notmatch '^[0-9A-Fa-f]{2}$') {
             throw "유효하지 않은 16진수 값: $hexPair"
         }
         $bytes += [byte]::Parse($hexPair, [System.Globalization.NumberStyles]::AllowHexSpecifier)
     }
     return ,$bytes
 }

 

shell code 형태 변환을 위해 필요

 

ex

$shellcodeHex = "\x48\x83\xEC\x28\..."

function Convert-HexStringToByteArray {
     param ([string]$hexString)

     $hexString = $hexString -replace "\\x", ""
     if ($hexString.Length % 2 -ne 0) {
         throw "16진수 문자열의 길이가 홀수입니다. 유효한 16진수 문자열이어야 합니다."
     }

     $bytes = @()
     for ($i = 0; $i -lt $hexString.Length; $i += 2) {
         $hexPair = $hexString.Substring($i, 2)
         if ($hexPair -notmatch '^[0-9A-Fa-f]{2}$') {
             throw "유효하지 않은 16진수 값: $hexPair"
         }
         $bytes += [byte]::Parse($hexPair, [System.Globalization.NumberStyles]::AllowHexSpecifier)
     }
     return ,$bytes
 }
 
 $shellcodeBytes = Convert-HexStringToByteArray $shellcodeHex
 
 $shellcodeBase64 = [Convert]::ToBase64String($shellcodeBytes)
 
 Write-Output $shellcodeBase64

 

'Script > Windows' 카테고리의 다른 글

[powershell] shell_code.ps1  (0) 2025.02.14
[Windows] ms defender scheduling batch file  (0) 2024.03.15
Comments