|
| 1 | +# POF, a free and open source Python obfuscation framework. |
| 2 | +# Copyright (C) 2022 - 2026 Deoktr |
| 3 | +# |
| 4 | +# This program is free software: you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU General Public License as published by |
| 6 | +# the Free Software Foundation, either version 3 of the License, or |
| 7 | +# (at your option) any later version. |
| 8 | +# |
| 9 | +# This program is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +import random |
| 18 | +from tokenize import LPAR, LSQB, NAME, NUMBER, RPAR, RSQB, STRING |
| 19 | + |
| 20 | + |
| 21 | +class BooleanObfuscator: |
| 22 | + """Obfuscate booleans with multiple methods.""" |
| 23 | + |
| 24 | + @staticmethod |
| 25 | + def obf_true(): |
| 26 | + match random.randint(1, 6): |
| 27 | + case 1: |
| 28 | + # all([]) |
| 29 | + return [ |
| 30 | + (NAME, "all"), |
| 31 | + (LPAR, "("), |
| 32 | + (LSQB, "["), |
| 33 | + (RSQB, "]"), |
| 34 | + (RPAR, ")"), |
| 35 | + ] |
| 36 | + case 2: |
| 37 | + # any([True]) |
| 38 | + return [ |
| 39 | + (NAME, "any"), |
| 40 | + (LPAR, "("), |
| 41 | + (LSQB, "["), |
| 42 | + (NAME, "True"), |
| 43 | + (RSQB, "]"), |
| 44 | + (RPAR, ")"), |
| 45 | + ] |
| 46 | + case 3: |
| 47 | + # not False |
| 48 | + return [ |
| 49 | + (NAME, "not"), |
| 50 | + (NAME, "False"), |
| 51 | + ] |
| 52 | + case 4: |
| 53 | + # not not True |
| 54 | + return [ |
| 55 | + (NAME, "not"), |
| 56 | + (NAME, "not"), |
| 57 | + (NAME, "True"), |
| 58 | + ] |
| 59 | + case 5: |
| 60 | + # "" in "" |
| 61 | + return [ |
| 62 | + (STRING, "''"), |
| 63 | + (NAME, "in"), |
| 64 | + (STRING, "''"), |
| 65 | + ] |
| 66 | + case 6: |
| 67 | + # bool(1) |
| 68 | + return [ |
| 69 | + (NAME, "bool"), |
| 70 | + (LPAR, "("), |
| 71 | + (NUMBER, "1"), |
| 72 | + (RPAR, ")"), |
| 73 | + ] |
| 74 | + |
| 75 | + @staticmethod |
| 76 | + def obf_false(): |
| 77 | + match random.randint(1, 6): |
| 78 | + case 1: |
| 79 | + # False = all([[]]) |
| 80 | + return [ |
| 81 | + (NAME, "all"), |
| 82 | + (LPAR, "("), |
| 83 | + (LSQB, "["), |
| 84 | + (LSQB, "["), |
| 85 | + (RSQB, "]"), |
| 86 | + (RSQB, "]"), |
| 87 | + (RPAR, ")"), |
| 88 | + ] |
| 89 | + case 2: |
| 90 | + # all([False]) |
| 91 | + return [ |
| 92 | + (NAME, "all"), |
| 93 | + (LPAR, "("), |
| 94 | + (LSQB, "["), |
| 95 | + (NAME, "False"), |
| 96 | + (RSQB, "]"), |
| 97 | + (RPAR, ")"), |
| 98 | + ] |
| 99 | + case 3: |
| 100 | + # not True |
| 101 | + return [ |
| 102 | + (NAME, "not"), |
| 103 | + (NAME, "True"), |
| 104 | + ] |
| 105 | + case 4: |
| 106 | + # not not False |
| 107 | + return [ |
| 108 | + (NAME, "not"), |
| 109 | + (NAME, "not"), |
| 110 | + (NAME, "False"), |
| 111 | + ] |
| 112 | + case 5: |
| 113 | + # "" not in "" |
| 114 | + return [ |
| 115 | + (STRING, "''"), |
| 116 | + (NAME, "not"), |
| 117 | + (NAME, "in"), |
| 118 | + (STRING, "''"), |
| 119 | + ] |
| 120 | + case 6: |
| 121 | + # bool(0) |
| 122 | + return [ |
| 123 | + (NAME, "bool"), |
| 124 | + (LPAR, "("), |
| 125 | + (NUMBER, "0"), |
| 126 | + (RPAR, ")"), |
| 127 | + ] |
| 128 | + |
| 129 | + def obfuscate_boolean(self, tokval): |
| 130 | + if tokval == "True": |
| 131 | + return self.obf_true() |
| 132 | + return self.obf_false() |
| 133 | + |
| 134 | + def obfuscate_tokens(self, tokens): |
| 135 | + result = [] |
| 136 | + for toknum, tokval, *_ in tokens: |
| 137 | + new_tokens = [(toknum, tokval)] |
| 138 | + |
| 139 | + if toknum == NAME and tokval in ["True", "False"]: |
| 140 | + new_tokens = self.obfuscate_boolean(tokval) |
| 141 | + |
| 142 | + if new_tokens: |
| 143 | + result.extend(new_tokens) |
| 144 | + return result |
0 commit comments