Hack Forums
TAKEOWN every file subfolder drive and usb port PowerShell script - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Hacks, Exploits, and Various Discussions (https://hackforums.net/forumdisplay.php?fid=45)
+--- Forum: Whitehat • Blue Team • Defensive Security (https://hackforums.net/forumdisplay.php?fid=448)
+---- Forum: Pentesting and Forensics (https://hackforums.net/forumdisplay.php?fid=231)
+---- Thread: TAKEOWN every file subfolder drive and usb port PowerShell script (/showthread.php?tid=6272275)



TAKEOWN every file subfolder drive and usb port PowerShell script - Clumsy - May 30, 2024

Enjoy This only works when ran as admin in PowerShell.

Code
# Run this script with elevated privileges (Run as Administrator)

# Specify the drives you want to change ownership for
$targetDrives = @("C:", "E:", "D:", "F:", "I:", "H:")

# Add connected USB drives if any
$usbDrives = Get-WmiObject Win32_DiskDrive | Where-Object { $_.InterfaceType -eq 'USB' } | ForEach-Object {
    $_.Associators("Win32_DiskDriveToDiskPartition") | ForEach-Object {
        $_.Associators("Win32_LogicalDiskToPartition") | ForEach-Object {
            $_.DeviceID
        }
    }
}

$targetDrives += $usbDrives

# Loop through each specified drive and set the owner using takeown command
foreach ($driveLetter in $targetDrives) {
    $drivePath = "$driveLetter\"

    # Set the owner using takeown command
    takeown /F $drivePath /R /A /D Y
}

Write-Host "Ownership changed for specified drives: $($targetDrives -join ', ')"

# Add the code to block all incoming TCP handshakes
New-NetFirewallRule -DisplayName "Block All Incoming TCP" -Direction Inbound -Protocol TCP -Action Block



RE: TAKEOWN every file subfolder drive and usb port PowerShell script - I ɴ ᴄ ʀ ᴇ ᴅ ɪ ʙ ʟ E - May 31, 2024

Thanks for sharing this. it surely is useful.


RE: TAKEOWN every file subfolder drive and usb port PowerShell script - Clumsy - May 31, 2024

(May 31, 2024 07:45 AM)I ɴ ᴄ ʀ ᴇ ᴅ ɪ ʙ ʟ E Wrote:  Thanks for sharing this. it surely is useful.

Thank you sir glad my scripts are of use to someone :)


RE: TAKEOWN every file subfolder drive and usb port PowerShell script - obtainhigh - Jul 4, 2024

it was fun testing it. Here is an improved version.

PHP Code

<?php 
# Run this script with elevated privileges (Run as Administrator)

# Function to get all connected USB drives
function Get-UsbDrives {
    
$usbDrives = Get-WmiObject Win32_DiskDrive | Where-Object { $_.InterfaceType -eq 'USB' } | ForEach-Object {
        
$_.Associators("Win32_DiskDriveToDiskPartition") | ForEach-Object {
            
$_.Associators("Win32_LogicalDiskToPartition") | ForEach-Object {
                
$_.DeviceID
            
}
        }
    }
    return 
$usbDrives
}

# Function to change ownership of the specified drives
function Change-Ownership {
    
param (
        [
string[]]$drives
    
)

    foreach (
$drive in $drives) {
        
$drivePath = "$drive\"
        Write-Host "
Changing ownership for $drivePath"
        takeown /F 
$drivePath /R /A /D Y
    }
}

# Function to block all incoming TCP handshakes
function Block-IncomingTCP {
    Write-Host "
Blocking all incoming TCP handshakes"
    New-NetFirewallRule -DisplayName "
Block All Incoming TCP" -Direction Inbound -Protocol TCP -Action Block
}

# Main script execution
$targetDrives = @("C:", "E:", "D:", "F:", "I:", "H:")
$targetDrives += Get-UsbDrives

Change-Ownership -drives 
$targetDrives
Write-Host "
Ownership changed for specified drives: $($targetDrives -join ', ')"

Block-IncomingTCP
Write-Host "
Firewall rule added to block all incoming TCP handshakes" 



RE: TAKEOWN every file subfolder drive and usb port PowerShell script - DramaKing - Jul 4, 2024

(Jul 4, 2024 03:07 AM)obtainhigh Wrote:  it was fun testing it. Here is an improved version.

Code
# Run this script with elevated privileges (Run as Administrator)

# Function to get all connected USB drives
function Get-UsbDrives {
    $usbDrives = Get-WmiObject Win32_DiskDrive | Where-Object { $_.InterfaceType -eq 'USB' } | ForEach-Object {
        $_.Associators("Win32_DiskDriveToDiskPartition") | ForEach-Object {
            $_.Associators("Win32_LogicalDiskToPartition") | ForEach-Object {
                $_.DeviceID
            }
        }
    }
    return $usbDrives
}

# Function to change ownership of the specified drives
function Change-Ownership {
    param (
        [string[]]$drives
    )

    foreach ($drive in $drives) {
        $drivePath = "$drive\"
        Write-Host "Changing ownership for $drivePath"
        takeown /F $drivePath /R /A /D Y
    }
}

# Function to block all incoming TCP handshakes
function Block-IncomingTCP {
    Write-Host "Blocking all incoming TCP handshakes"
    New-NetFirewallRule -DisplayName "Block All Incoming TCP" -Direction Inbound -Protocol TCP -Action Block
}

# Main script execution
$targetDrives = @("C:", "E:", "D:", "F:", "I:", "H:")
$targetDrives += Get-UsbDrives

Change-Ownership -drives $targetDrives
Write-Host "Ownership changed for specified drives: $($targetDrives -join ', ')"

Block-IncomingTCP
Write-Host "Firewall rule added to block all incoming TCP handshakes"

You just rewrote the code blocks as functions.


RE: TAKEOWN every file subfolder drive and usb port PowerShell script - obtainhigh - Jul 4, 2024

(Jul 4, 2024 06:56 AM)DramaKing Wrote:  You just rewrote the code blocks as functions.

DramaKing clearly posts trash and doesnt know anything about computers. Loser.


RE: TAKEOWN every file subfolder drive and usb port PowerShell script - mrshlash - Aug 17, 2024

Do you have any doubt about that