2016-10-18 2 views
1

powershell -encodedcommand xxx을 통해 실행하려면 base64에 powershell 명령 (문자열 (get-date).date)을 인코딩하고 싶습니다.VBScript로 Base64로 유니 코드 문자열 인코딩

표준 VBS 방법 (또는 심지어 https://www.base64encode.org/)을 사용하면 실행되지 않는 KGdldC1kYXRlKS5kYXRl이됩니다.

다음과 같은 파워 쉘 스크립트를 사용 :

$bytes = [System.Text.Encoding]::Unicode.GetBytes($command) 
$encodedCommand = [Convert]::ToBase64String($bytes) 

내가 작동 KABnAGUAdAAtAGQAYQB0AGUAKQAuAGQAYQB0AGUA를 얻을 수 있습니다. 차이점은 명령이 처음 유니 코드 바이트로 인코딩된다는 것입니다.

이 코드 또는 VBS에 해당하는 Unicode.GetBytes()를 제공하는 VBS 함수를 제공하면 누구든지 올바른 문자열을 인코딩 할 수 있습니까?

답변

3

파워 쉘은 그
-EncodedCommand 파라미터 UTF-16 LE 부호화 문자열을 Base64 인코딩을 받아 들인다.
UTF-16 LE는 [System.Text.Encoding]::Unicode을 나타내고 두 개의 바이트로 유니 코드 문자 (코드 포인트)의 대부분을 인코딩합니다. VBScript와 PowerShell 모두 내부적으로 문자열 인코딩입니다. 거기

반면

, 대부분의 VBScript 솔루션은 단일 -byte ASCII 인코딩을 사용, 심지어 다른 laudably 유니 코드를 인식 https://www.base64encode.org/ 만 주로 단일 바이트가 UTF-8 기반 인코딩을 (제공 -for-Western-languages ​​인코딩은 다른 언어의 문자로, 2-4 바이트로 표시됨).

여기에 강력한 UTF-16 LE 기반 Base64 인코딩 솔루션이 있습니다.

선택적으로 지원하는 모듈 식 변형을 게시했습니다. UTF-8here; 두 위치의 코드는 this great answer에 작성됩니다.

전화 :

Base64EncodeUtf16Le("(get-date).date") ' -> "KABnAGUAdAAtAGQAYQB0AGUAKQAuAGQAYQB0AGUA" 

소스 코드 : 솔루션을 단순화하는 데 도움을위한 MC ND에 모자 팁.

' Base64-encodes the specified string using UTF-16 LE as the underlying text 
' encoding. 
Function Base64EncodeUtf16Le(ByVal sText) 

    Dim bytesUtf16Le 

    ' Create an aux. stream from which we can get a binary (byte array) 
    ' representation of the input string in UTF-16 LE encoding. 
    With CreateObject("ADODB.Stream") 
     ' Create a UTF 16-LE encoded text stream... 
     .Type = 2 ' adTypeText 
     .Charset = "utf-16le" 
     .Open 
     .WriteText sText 
     ' ... and convert it to a binary stream, 
     ' so we can get the string as a byte array 
     .Position = 0 
     .Type = 1 ' adTypeBinary 
     .Position = 2 ' Skip BOM 
     bytesUtf16Le = .Read 
     .Close 
    End With 

    ' Use an aux. XML document with a Base64-encoded element. 
    ' Assigning a byte stream (array) to .NodeTypedValue 
    ' automatically performs Base64-encoding, whose result can then be accessed 
    ' as the element's text. 
    With CreateObject("Msxml2.DOMDocument").CreateElement("aux") 
     .DataType = "bin.base64" 
     .NodeTypedValue = bytesUtf16Le 
     Base64EncodeUtf16Le = .Text 
    End With 

End Function 
+1

그레이트!나는 시험하고 있었지만'utf-16le' 옵션을 놓쳤습니다. 내 코드를 정보에 적용했지만 의심 스럽습니다 : 왜 두 개의 'adodb.stream' 객체입니까? 하나만있는 솔루션이 실패 할 수있는 경우가 있습니까? 두 개의 인스턴스를 사용하여 개선이 있습니까? –

+1

@MCND D' oh! 고마워, 내 대답을 업데이 트했습니다 - 1 스트림으로 충분합니다; 하나의 스트림을 실험하면서 오래된 코드와 실수를 조합하면 2 개의 스트림이 필요하다는 잘못된 생각이 들게되었습니다. – mklement0

2

나는이 모든 요구를 처리 모르겠지만, 적어도 그것은 귀하의 질문에 표시된 출력과 일치하는

Function ToBase64(ByVal text) 
    Const adTypeText = 2 
    Const adTypeBinary = 1 

    ' Right pad each character with a null 
    With New RegExp 
     .Pattern = "(.)" 
     .Global = True 
     text = .Replace(text, "$1" & Chr(0)) 
    End With 

    ' Convert String to binary 
    With WScript.CreateObject("ADODB.Stream") 
     .Type = adTypeText 
     .CharSet = "us-ascii" 
     .Open 
     .WriteText text 
     .Position = 0 
     .Type = adTypeBinary 
     text = .Read 
    End With 

    ' Encode binary as Base64 
    With WScript.CreateObject("Msxml2.DOMDocument.6.0").CreateElement("base64") 
     .dataType = "bin.base64" 
     .nodeTypedValue = text 
     ToBase64 = Replace(.text, vbLf, "") 
    End With 
End Function 

WScript.Echo ToBase64("(get-date).date") 

mklement0 answer 어디를의 정보에 내 앞의 코드를 적응 편집 powershell 요구 사항과 코드 작동 방식에 대한 세부 정보를 찾을 수 있습니다.

Function ToBase64(ByVal text) 
    Const adTypeText = 2 
    Const adTypeBinary = 1 

    ' Change string encoding 
    With WScript.CreateObject("ADODB.Stream") 
     ' Convert input string to UTF-16 LE 
     .Type = adTypeText 
     .CharSet = "utf-16le" 
     .Open 
     .WriteText text 
     ' Get binary representation of the string 
     .Position = 0 
     .Type = adTypeBinary 
     .Position = 2 ' Skip BOM 
     text = .Read 
    End With 

    ' Encode binary as Base64 
    With WScript.CreateObject("Msxml2.DOMDocument.6.0").CreateElement("base64") 
     .dataType = "bin.base64" 
     .nodeTypedValue = text 
     ToBase64 = Replace(.text, vbLf, "") 
    End With 
End Function 

WScript.Echo ToBase64("(get-date).date")