File size: 2,553 Bytes
55401d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
$bad_vars2 = @('$_', '$ignore', '$PSScriptRoot', '$global', '$MyInvocation', '$local', '`$', '$args', '$ErrorActionPreference', '$ProgressPreference', '$PROFILE')
$good_chars = "cdghijklmopqsuwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

function ObfuscateVariables($variable_good, $parameter) {
    $lower_var = $variable_good.ToLower()
    switch ($lower_var) {
        '$true' { return ObfuscateTrue }
        '$false' { return ObfuscateFalse }
        '$null' { return ObfuscateNull }
    }

    foreach ($bad_var in $bad_vars2) {
        if ($lower_var -contains $bad_var) {
            return $variable_good
        }
    }
    $var = MakeRandomVariableName 10
    $new_var_final = RandomChangeVar $var $parameter
    Write-Host "Obfuscating variable: $variable_good to $new_var_final"
    return $new_var_final
}

function MakeRandomVariableName($length) {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    $name = "`$KDOT"
    for ($i = 0; $i -lt $length; $i++) {
        $name += $chars[(Get-Random -Minimum 0 -Maximum $chars.Length)]
    }
    return $name
}

function RandomChangeVar($variable, $parameter) {
    # if the variable is in good_chars, do random capitalization and randomly add a ` in front.
    $ticks = Get-Random -Minimum 0 -Maximum 2
    if ($parameter -eq $true) {
        $ticks = 999
    }
    $variable = $variable -split ""
    for ($i = 0; $i -lt $variable.Length; $i++) {
        if ($i -gt 0 -and $variable[$i - 1] -eq '`' -and $good_chars.Contains($variable[$i])) {
            continue
        }

        if ($good_chars.Contains($variable[$i]) -and ($variable[$i] -ne "")) {
            $random = Get-Random -Minimum 0 -Maximum 2
            $random2 = Get-Random -Minimum 0 -Maximum 2
            if ($random -eq 0) {
                $variable[$i] = $variable[$i].ToUpper()
            } else {
                $variable[$i] = $variable[$i].ToLower()
            }

            if (($random2 -eq 0) -and ($ticks -eq 0)) {
                $variable[$i] = "``" + $variable[$i]
            }
        }
    }
    $variable = $variable -join ''
    if ($ticks -eq 0) {
        #insert a { at the beginning after the first character and a } at the end
        $variable = $variable.Insert(1, "{")
        $variable += "}"
    }
    return $variable
}

function ObfuscateTrue {
    return '$true'
}

function ObfuscateFalse {
    return '$false'
}

function ObfuscateNull {
    return '$null'
}

#ObfuscateVariables '$this_is_a_test'