DataWiper.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<#PSScriptInfo
 
.VERSION 1.0.4
 
.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.
# $(Get-Item ".\samples\testfile.xlsx") | Wipe-Data -method 1

# SIG # Begin signature block
# MIIEUAYJKoZIhvcNAQcCoIIEQTCCBD0CAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUUgFFjnpWAqu50qJ/GMilr2m2
# icagggJQMIICTDCCAfKgAwIBAgIQZjnMMJTJlJFCtQ0CzY0AtjAKBggqhkjOPQQD
# AjBwMRIwEAYKCZImiZPyLGQBGRYCZXIxEzARBgoJkiaJk/IsZAEZFgN3aXAxFDAS
# BgoJkiaJk/IsZAEZFgRkYXRhMRUwEwYDVQQLDAxVc2VyQWNjb3VudHMxGDAWBgNV
# BAMMD1NpZ25lZCBBcHAgKG1lKTAeFw0yMTAxMDIyMzEyNThaFw0yMjAxMDIyMzMy
# NThaMHAxEjAQBgoJkiaJk/IsZAEZFgJlcjETMBEGCgmSJomT8ixkARkWA3dpcDEU
# MBIGCgmSJomT8ixkARkWBGRhdGExFTATBgNVBAsMDFVzZXJBY2NvdW50czEYMBYG
# A1UEAwwPU2lnbmVkIEFwcCAobWUpMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE
# C+Zc60XfEODeGtVLpPBm1L45uJcW3q1Aa+B7gR1eTGaE5Llg8Ou/m4zNcG3P4qbK
# y87qw0NJ6XAJItztyd3GtKNuMGwwDgYDVR0PAQH/BAQDAgeAMBMGA1UdJQQMMAoG
# CCsGAQUFBwMDMCYGA1UdEQQfMB2gGwYKKwYBBAGCNxQCA6ANDAtkYXRhQHdpcC5l
# cjAdBgNVHQ4EFgQUkKo9crqJ4uelSfaYOZjmwKZ/n88wCgYIKoZIzj0EAwIDSAAw
# RQI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