解析CSV并解码base64

时间:2018-11-05 20:24:43

标签: regex powershell csv parsing base64

该脚本的目标是要找到CSV特定行中的所有base64编码的字符串,仅抓取base64,为其添加正确数量的填充字符,对其进行解码,然后将其写入输出

function Get-FileName($initialDirectory) {
    [System.Reflection.Assembly]::LoadWithPartialName("System.Sindows.Forms") | Out-Null

    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.InitialDirectory = $initialDirectory
    $OpenFileDialog.Filter = "CSV (*.csv)| *.csv"
    $OpenFileDialog.ShowDialog() | Out-Null
    $OpenFileDialog.Filename
}

$inputfile = Get-FileName "C:\My Documents\"
$inputdata = Import-Csv $inputfile
$output = "C:\My Documents\output.csv"

$regex = [regex]::New('(?<=-[Ee]ncoded[Cc]ommand\s"??)[\w\d=\/!]{0,792}')

$csv1 = $inputdata | select -ExpandProperty 'NameOfRow' | ? {$_ -ne ""} 
| Select-String -Pattern $regex -Allmatches | % {$_.Matches} 
| ? {$_.Value -ne ""} | % {$_.Value}  

foreach ($line in $csv1) {
    $csvL = $line.Length
    $csvM = $csvL %= 4
    if ($csvM | % {$_ -eq 1}) {
        $line | % {$_ + "==="}
    } elseif ($csvM | % {$_ -eq 2}) {
        $line | % {$_ + "=="}
    } elseif ($csvM | % {$_ -eq 3}) {
        $line | % {$_ + "="}
    }
    $decode = [System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String($line))
    $decode > $output
}

0 个答案:

没有答案