-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddToHosts.ps1
More file actions
84 lines (73 loc) · 3.19 KB
/
AddToHosts.ps1
File metadata and controls
84 lines (73 loc) · 3.19 KB
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
# AddTo-Hosts.ps1
# Display ASCII Art
Write-Host @"
_ _ _______ _ _ _
/\ | | | |__ __| | | | | | |
/ \ __| | __| | | | ___ | |__| | ___ ___| |_ ___
/ /\ \ / _` |/ _` | | |/ _ \| __ |/ _ \/ __| __/ __|
/ ____ \ (_| | (_| | | | (_) | | | | (_) \__ \ |_\__ \
/_/ \_\__,_|\__,_| |_|\___/|_| |_|\___/|___/\__|___/
"@ -ForegroundColor Yellow
Write-Host @"
Gɪᴛʜᴜʙ.ᴄᴏᴍ/ʏᴍᴜᴜᴜᴜ
"@ -ForegroundColor Red
# Check admin privileges
if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
exit
}
# Backup function
function Backup-Hosts {
param(
[string]$SystemType,
[string]$SourcePath,
[string]$BackupPath
)
if ($SystemType -eq "Windows") {
# Check Windows backup
if (-not (Test-Path $BackupPath -ErrorAction SilentlyContinue)) {
Copy-Item -Path $SourcePath -Destination $BackupPath -Force
Write-Host "[*] Created Windows backup: $BackupPath" -ForegroundColor Cyan
} else {
Write-Host "[*] Windows backup already exists: $BackupPath" -ForegroundColor Cyan
}
}
else {
# Check WSL backup
$checkCmd = "if [ -f '$BackupPath' ]; then exit 0; else exit 1; fi"
wsl -d kali-linux -u root bash -c $checkCmd
if ($LASTEXITCODE -eq 1) {
# Create WSL backup
$backupCmd = "sudo cp '$SourcePath' '$BackupPath'"
wsl -d kali-linux -u root bash -c $backupCmd
Write-Host "[*] Created WSL backup: $BackupPath" -ForegroundColor Cyan
} else {
Write-Host "[*] WSL backup already exists: $BackupPath" -ForegroundColor Cyan
}
}
}
# Backup Windows hosts
$winHosts = "$env:SystemRoot\System32\drivers\etc\hosts"
$winBackup = "$winHosts.bak"
Backup-Hosts -SystemType "Windows" -SourcePath $winHosts -BackupPath $winBackup
# Backup WSL hosts
$wslHosts = "/etc/hosts"
$wslBackup = "$wslHosts.bak"
Backup-Hosts -SystemType "WSL" -SourcePath $wslHosts -BackupPath $wslBackup
# Get input
$ip = Read-Host "Enter IP (e.g., 10.10.11.55)"
$hostname = Read-Host "Enter hostname (e.g., titanic.htb)"
$entry = "$ip $hostname"
# Windows hosts modification
if (-not (Select-String -Path $winHosts -Pattern "^\s*$ip\s+$hostname" -Quiet)) {
Add-Content -Path $winHosts -Value "`n$entry" -Force
Write-Host "`n[+] Windows hosts: Added '$entry'" -ForegroundColor Green
} else {
Write-Host "`n[!] Windows hosts: Entry already exists" -ForegroundColor Yellow
}
# WSL hosts modification
$wslCmd = "if ! grep -q '$hostname' $wslHosts; then echo '$entry' | sudo tee -a $wslHosts >/dev/null; echo '[+] Added to WSL Kali hosts: $entry'; else echo '[!] Entry already exists in WSL Kali hosts'; fi"
wsl -d kali-linux -u root bash -c $wslCmd
# Keep window open
Write-Host "`nPress any key to exit..."
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')