DataWiper.ps1

<#PSScriptInfo
 
.VERSION 1.0.3
 
.GUID a8a7507e-35fc-499a-9b19-7c9ccc91f4fe
 
.AUTHOR c.dek.
 
.COMPANYNAME
 
.COPYRIGHT 2021
 
.TAGS
data,datawipe,datashred,data-wipe,remove-file,data-removal
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI https://findicons.com/files/icons/766/base_software/128/warning.png
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES Used for data cleanup, and data scrambling for confidential files.
 
.PRIVATEDATA
 
.DESCRIPTION Used for data cleanup, and data scrambling for confidential files.
#>


<#
.SYNOPSIS
** USE AT OWN RISK AND WITH CAUTION **
Function used for byte-level information scrambling for most files (For example: zip,documents,plain text or pdf).
 
.DESCRIPTION
** USE AT OWN RISK AND WITH CAUTION **
This function provides various methods of destroying the contents of a file, rendering it unreadable.
It can either be used on text or other types of data file formats.
Methods of removing data include XOR directly bytes in file, removing data by using source-dest. files
and by truncating contents of a file.
 
Note:
This script will keep a log of the final file output (as md5 hash) for tracking data wipe operations.
 
.PARAMETER FileName
Source file name -with path- to perform merge file operation (when using multiple files wiping option).
 
.PARAMETER method
Mode of wiping files, can select from the one or more of the following options (1, 2, 3, 4, 5).
 
.EXAMPLE
The 5 Different usage methods of wiping file data are displayed below:
 
1 - Multiple pass 4-bit binary sequences XORing - example:
PS\> $(Get-ChildItem ".\samples\" -File -Filter "*.pdf") | Wipe-Data -method 1
 
Produces a file fragment with xored bits of the initial one, logs operation to "Wipe.log"
 
NOTE:This algorithm is particularly slow, can be adjusted according to volume and
quantity of the files through limiting the number of lines via -TotalCount (initial limit set to 5KB).
 
 
2 - Single pass 4-bit binary sequence XORing - example:
PS\> $(Get-ChildItem ".\samples\" -File -Filter "*.pdf") | Wipe-Data -method 2
 
Produces a full xored file of the initial one, logs operation to "Wipe.log"
 
3 - Simple file bytes truncation - example:
PS\> $(Get-ChildItem ".\samples\" -File -Filter "*.pdf") | Wipe-Data -method 3
 
Truncates a file, produces a file of 0KB size, logs operation to "Wipe.log"
 
4 - Merge-wipe scrambled data file - example:
PS\> $(Get-ChildItem ".\samples\" -File -Filter "*.pdf") | Wipe-Data -method 4
 
Produces an "out.bin" file with xored bits of the initial one and replaces every other file content from "out.bin", logs operation to "Wipe.log"
 
5 - Using .NET rngcryptoservices file info replacement - example:
PS\> $(Get-ChildItem ".\samples\" -File -Filter "*.pdf") | Wipe-Data -method 5
 
Produces an "out.bin" file of random data, replaces every other file content from "out.bin", logs operation to "Wipe.log"
#>

Function Wipe-Data {
[CmdLetBinding()]
param(
    [Parameter(ValueFromPipeLine)]
    [PSObject[]]
    $FileName,             # source file path - this parameter is set by default as pipelined input
    [Parameter(Mandatory=$false)]
    [int]
    $method = 1           # wiping operation method (1, 2, 3, 4, 5), default is set to method 1
    )
Begin {
$bin = $([Convert]::ToString((Get-Random),2)).Substring(0,4)
# $randName = ".$((Get-Random).ToString().Substring(5))_$( (([Guid]::NewGuid()).Guid -replace '-','').Substring(32-5) )__."
}
Process {
if ($method) {
 if (($method -is [int]) -and $method -ne $null) {
Try {
switch ($method) {

    # Generate random binary sequence sets (6 passes bin bit xoring using 4-digit default - method 1)
    1 {
    $digits=@();$i=0;[String]$res=""
    [String]::new([Convert]::ToString((Get-Random),2)) -split "" | %{
    if ($i % 5 -eq 0) {
    $digits+=$res -as [int];$res="";
     } else {
    $res+=$_ -replace " ","";
     };$i++
    }
    $digitsBin = $digits | Select -Last 6;
    [Char[]]$outContent=@()

    $c = Get-Content -Path "$($FileName.FullName)" -Encoding Byte -TotalCount 5KB; [Char[]]$prev = @();
    for ($k=0; $k -lt $digitsBin.Length; $k++) {
    if ($k -gt 0) {$outContent = @(); $c = $prev;}
    $c | %{ $outContent+=$($_ -bxor $digitsBin[$k]) -as [char]; }
    $prev = $outContent;
    $prev | Set-Content $($FileName.FullName) -Encoding Unicode -NoNewline;
     }
     "Data wipe operation complete, MD5 on wiped file is: $((Get-FileHash $($FileName.FullName) -Algorithm MD5 | select Hash).Hash)`r`n `r`n " | Out-File "$($FileName.Directory[0].Name)\Wipe.log" -Append -Encoding utf8
     $($FileName.FullName) | Remove-Item
    } # END - METHOD 1

    # Export random binary integer stream. (1 pass bin bit xoring using 4-digit default - method 2)
    2 {
    $outbytes = Get-Content -Path "$($FileName.FullName)" -Encoding Byte | %{$($_-bxor $bin) -as [char]};
    Set-Content "$($FileName.FullName)" -Value $outbytes -Encoding UTF8 -NoNewline
    "Data wipe operation complete, MD5 on wiped file is: $((Get-FileHash $($FileName.FullName) -Algorithm MD5 | select Hash).Hash)`r`n `r`n " | Out-File "$($FileName.Directory[0].Name)\Wipe.log" -Append -Encoding utf8
    Remove-Item "$($FileName.FullName)";
    } # END - METHOD 2

    # Simple truncation of file using same output result. (txt,other - method 3)
    3 {
    $($FileName.FullName) 2> $($FileName.FullName) | Out-Null
    "Data wipe operation complete: MD5 on wiped file is: $((Get-FileHash $($FileName.FullName) -Algorithm MD5 | select Hash).Hash)`r`n `r`n " | Out-File "$($FileName.Directory[0].Name)\Wipe.log" -Append -Encoding utf8
    $($FileName.FullName) | Remove-Item
    } # END - METHOD 3

    # Merge/truncate with a scrambled data file using bytes replacement on file. (txt,pdf,other - method 4)
    4 {
    $(Get-Content -Path "$($FileName[0].FullName)" -Encoding Byte | %{$($_ -bxor $bin) -as [char]}) >>"$(($FileName).Directory)\out.bin"
    (Get-Content "$(($FileName).Directory)\out.bin") | Set-content -Path "$($FileName.FullName)" -Encoding UTF8 -NoNewLine -Force;
    "Data wipe operation complete, MD5 on wiped file is: $((Get-FileHash $($FileName.FullName) -Algorithm MD5 | select Hash).Hash)`r`n `r`n " | Out-File "$($FileName.Directory[0].Name)\Wipe.log" -Append -Encoding utf8
    Remove-Item "$($FileName.FullName)";
    } # END - METHOD 4

    # RNG Crypto Provider data overwrite in source file. (txt.pdf,other - method 5)
    5 {
    $prng = [System.Security.Cryptography.RNGCryptoServiceProvider]::new();
    $bytes = New-Object byte[] 2MB; $prng.GetBytes($bytes);
    [System.IO.File]::WriteAllBytes("$(Resolve-Path .)\$($FileName.Directory[0].Name)\out.bin",$bytes);
    $(Get-Content -Path ".\$($FileName.Directory[0].Name)\out.bin" -TotalCount 3KB) | Set-Content -Path "$($FileName.FullName)" -Encoding Unicode -NoNewline
    "Data wipe operation complete, MD5 on wiped file is: $((Get-FileHash $($FileName.FullName) -Algorithm MD5 | select Hash).Hash)`r`n `r`n " | Out-File "$($FileName.Directory[0].Name)\Wipe.log" -Append -Encoding utf8
    Remove-Item "$($FileName.FullName)";
    } # END - METHOD 5
    default { throw [System.InvalidOperationException] }
 }
}
Catch [System.Exception]{ return ($null | Out-Null); } # When selection is out of range, script exits on exception.
   }
  }
  else { continue; $(Write-Host "No Method specified, exiting.."); }
 }
 # Ending process - remove unecessary data files.
 End {
 if (Test-Path -Path "$(Resolve-Path .)\$($FileName.Directory[0].Name)\out.bin") {
 Remove-Item "$(($FileName).Directory)\out.bin";
  }
 }
}

$(Get-ChildItem ".\samples\" -File -Filter "*.pdf") | Wipe-Data -method 1


# Additional Samples of usage:
# ----------------------------
# Example with bulk data wipe operation via pipelined command.
# $(Get-ChildItem ".\samples\" -File -Filter "*.pdf") | Wipe-Data -method 1

# Example with single file wipe/delete operation.
# ".\excel2.xlsx" | Wipe-Data -method 1

# Example with Bulk data wipe/delete operation.
# @(".\file1.xlsx",'.\file2.txt','.\file3.txt',".\data1.csv",".\data3.csv",".\word1.docx",".\test2.pdf") | Wipe-Data -method 1
# SIG # Begin signature block
# MIIEUAYJKoZIhvcNAQcCoIIEQTCCBD0CAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUUgFFjnpWAqu50qJ/GMilr2m2
# icagggJQMIICTDCCAfKgAwIBAgIQZjnMMJTJl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