-
Notifications
You must be signed in to change notification settings - Fork 0
/
psyposquatter.ps1
107 lines (83 loc) · 2.76 KB
/
psyposquatter.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
# Generate various strings based on input
function Get-Typos()
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true)]
[string] $Package,
[Parameter(Mandatory=$true)]
[ValidateSet("All", "CharacterSwapping","Omissions")]
[string[]] $Types = "All"
)
write-verbose "Generate Typos with types: $Types"
$Typos = @()
#Permutations - character swapping
if ($Types -contains "All" -or $Types -contains "CharacterSwapping")
{
$Typos += $Package[1]+$Package[0]+$($Package[2..$Package.Length] -join '')
for ($i=0; $i -lt $Package.Length; $i++)
{
$Typos += $($Package[0..($i)] -join '') + $Package[$i+2]+$Package[$i+1] + $($Package[($i+3)..$Package.Length] -join '')
}
$Typos += $($Package[0..($Package.Length-3)] -join '')+$Package[$Package.Length-1]+$Package[($Package.Length)-2]
}
#Omissions
if ($Types -contains "All" -or $Types -contains "Omissions")
{
$Typos += $($Package[1..$Package.Length] -join '')
for ($i=0; $i -lt $Package.Length-2; $i++)
{
$Typos += $($Package[0..($i)] -join '') + $($Package[($i+2)..$Package.Length] -join '')
}
$Typos += $Package[0..($Package.Length-2)] -join ''
}
# XXX Double characters
# XXX Similar looking
# XXX Typos, e.g. r instead of e etc -> keyboard layout dependent
$Typos = $Typos | Where-Object { $_ -ne $Package }
$Typos | sort -u
}
function Find-TypoModule()
{
[CmdletBinding(SupportsShouldProcess=$True,DefaultParameterSetName="Package")]
param (
[Parameter(ParameterSetName="File",Mandatory=$true)]
[string]$Path,
[Parameter(ParameterSetName="Package",Mandatory=$true)]
[string]$Package
)
$Result = ""
if ($Path -and !(Test-Path $Path))
{
write-error "Path $Path not found."
Return
}
elseif ($Path)
{
$Packages = gc $Path
}
elseif ($Package)
{
$Packages = $Package
}
foreach ($Package in $Packages)
{
write-output "Check Package `"$Package`""
$PackageTypos = Get-Typos $Package -Types All
foreach ($PackageTypo in $PackageTypos)
{
# Check each typo name
if ($pscmdlet.ShouldProcess($PackageTypo, "Check PowerShell gallery"))
{
write-output " Check $PackageTypo"
try {
Find-Module -Name "$PackageTypo" -erroraction stop
write-output "$PackageTypo found on PowerShell Gallery"
}
catch {}
} # whatif
} # foreach typo
write-output "Checked $($PackageTypos.count) packages"
} # foreach package
} # Find-TypoModule