Powershell : Purge Data


I always wondered how easy it would be to write a script that would essentially zero your data in files with either white space or random hexadecimal values, You seem to see this behavior a lot in movies where they wipe their own data clean and I just wondered how hard it would be to reproduce this with the a Powershell script.

Define the outline

First, we need to you get the outline of the script set and here we will have two scripts that will automatically run with the variables contained within them, and then we will have a second script that will be interactive that will ask you all the questions and then confirm what you’ve asked it to do and then give you the option to authorize the action.

Script Goal

The goal of the script in all cases is to take a base folder recursively look through that folder for certain file extensions and where the files extensions match you have two options:
  1. Zero the file which will mean any day within will be lost
  2. Replace the file with Hexadecimal values this will also invalidate the file by filling it with content that should not be there, this gibberish will be random hexadecimal values. 
Notice : While this is for educational purposes, I cannot publish I scripted that actually changes files, but the script below goes through the actions that doesn’t actually commit to changing files - call it human friendly version.

Script : FilePurge.ps1

‼️ Note : This script does not actually make any modifications to your file system, but does give you the general concept 🤩

# Function to print text with flashing effects
function Flashing-Text {
    param (
        [string]$Text,
        [int]$Duration = 500,  # Time between flashes in milliseconds
        [int]$Count = 10
    )
    for ($i = 0; $i -lt $Count; $i++) {
        Write-Host $Text -ForegroundColor Green
        Start-Sleep -Milliseconds $Duration
        Clear-Host
    }
}

# Play a beep sound
function Beep {
    [console]::beep(1000, 200)
}

# Function to print a warning message in red
function Show-Warning {
    param (
        [string]$Message
    )
    Write-Host $Message -ForegroundColor Red
    Beep
}

# Display hacker-style intro
Flashing-Text -Text "Accessing Secure Filesystem..." -Count 5

# Print a warning message in red
Show-Warning -Message "WARNING: Unauthorized access will result in data destruction!"

# Function to display the entered information
function Show-Details {
    param (
        [string]$BaseDirectory,
        [string[]]$FileExtensions,
        [string]$Action,
        [int]$GibberishLength
    )
    Write-Host "`n--- Review Your Input ---" -ForegroundColor Cyan
    Write-Host "Base Directory: $BaseDirectory" -ForegroundColor White
    Write-Host "File Extensions: $($FileExtensions -join ', ')" -ForegroundColor White
    Write-Host "Action: $Action" -ForegroundColor White
    if ($Action -eq 'gibberish') {
        Write-Host "Gibberish Length: $GibberishLength bytes" -ForegroundColor White
    }
    Beep
}

# Prompt the user for input
$BaseDirectory = Read-Host "Enter the base directory path"
$FileExtensionsInput = Read-Host "Enter the file extensions to target (comma-separated, e.g., txt,log)"
$FileExtensions = $FileExtensionsInput -split "," | ForEach-Object { $_.Trim().ToLower() }
$Action = Read-Host "Choose action - 'zero' to zero out or 'gibberish' to replace with gibberish"

if ($Action -eq 'gibberish') {
    $GibberishLength = Read-Host "Enter the length of gibberish data in bytes (e.g., 1024)" -AsInt
} else {
    $GibberishLength = 0  # Set to 0 if not used
}

# Display the entered details
Show-Details -BaseDirectory $BaseDirectory -FileExtensions $FileExtensions -Action $Action -GibberishLength $GibberishLength

# Show a warning about data loss
Show-Warning "!!! DATA PURGE INITIATED !!!"
Show-Warning "WARNING: This operation will permanently alter or delete data in the specified directory."
Show-Warning "Proceeding will affect all files with the specified extensions."

# Confirm authorization
$Confirmation = Read-Host "Do you authorize this operation? Type 'YES' to proceed or 'NO' to cancel"

if ($Confirmation -ne 'YES') {
    Write-Host "Operation aborted. Exiting..." -ForegroundColor Yellow
    exit
}

function Zero-Out-Files-Mock {
    param (
        [string]$BaseDirectory,
        [string[]]$FileExtensions
    )

    Get-ChildItem -Path $BaseDirectory -Recurse -File | Where-Object {
        $FileExtensions -contains $_.Extension.TrimStart('.').ToLower()
    } | ForEach-Object {
        try {
            # Mock operation
            Write-Host "Mock: Zeroed out: $($_.FullName)" -ForegroundColor Green
            Beep
        } catch {
            Write-Error "Error zeroing out file $($_.FullName): $_"
        }
    }
}

function Replace-With-Gibberish-Mock {
    param (
        [string]$BaseDirectory,
        [string[]]$FileExtensions,
        [int]$GibberishLength
    )

    $chars = @('A'..'Z') + ('a'..'z') + ('0'..'9') + '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '+', '='
    Get-ChildItem -Path $BaseDirectory -Recurse -File | Where-Object {
        $FileExtensions -contains $_.Extension.TrimStart('.').ToLower()
    } | ForEach-Object {
        try {
            # Mock operation
            Write-Host "Mock: Replaced with gibberish: $($_.FullName)" -ForegroundColor Green
            Beep
        } catch {
            Write-Error "Error replacing file $($_.FullName) with gibberish: $_"
        }
    }
}

# Main script execution
if ($Action -eq 'zero') {
    Zero-Out-Files-Mock -BaseDirectory $BaseDirectory -FileExtensions $FileExtensions
} elseif ($Action -eq 'gibberish') {
    if ($GibberishLength -gt 0) {
        Replace-With-Gibberish-Mock -BaseDirectory $BaseDirectory -FileExtensions $FileExtensions -GibberishLength $GibberishLength
    } else {
        Write-Error "Invalid gibberish length specified."
    }
} else {
    Write-Error "Invalid action. Please choose 'zero' or 'gibberish'."
}

# Flash success message
Flashing-Text -Text "MISSION ACCOMPLISHED" -Count 5

We have a couple of flavours of this script which include:
  1. Script to "zero byte" applicable files
  2. Script to "hexadecimal" override files 
  3. Interactive override files
  4. Hollywood style Override files.
Target directory

The Target directory current has some photos inside of it as you can see below so lets target that directory:


Running the Script

I have gone to show the "Hollywood" mode example where there are some pretty effects and jazzy wording but the target will be the Purge folder from earlier, lets get to it:


This will then once authorised zero (in this case) your files in that folder, that is the bit at the bottom in green:


If you now visit your folder of c:\Purge you will notice the thumbnails have gone and all the files are now empty with zero bytes, this means any data in those files are been removed!


This has successfully done a "Hollywood" style purge to the files specified.
Previous Post Next Post

نموذج الاتصال