Skip to content

Commit

Permalink
fix: Not allow the first character of an obfuscated to be a number or…
Browse files Browse the repository at this point in the history
… a non-Ascll
  • Loading branch information
Eric-Joker committed Feb 11, 2025
1 parent 6ab7f6f commit 98b62f8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ obfuscator:
# 用于生成实体系文件键值混淆后字符串的字符
# 必须全是 Ascll 字符
obfuscate_ascll:
- 'abcdefghijklmn'
- 'abcdefg'

json_funs:
# 依据自然顺序排序 Json 键值
Expand Down
9 changes: 7 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,13 @@ def main():
!= (len(cfg.path) if isinstance(cfg.path, list) else 1)
):
raise ValueError("The namespace needs to correspond to the resource package.")
if (cfg.obfuscate_jsonui or cfg.obfuscate_entity) and len(cfg.obfuscate_strs) < 1:
raise ValueError("At least one set of obfuscated strings.")
if (cfg.obfuscate_jsonui or cfg.obfuscate_entity):
if len(cfg.obfuscate_strs) < 1:
raise ValueError("At least one set of obfuscated strings.")
if not any(any(s.isascii() for s in p) for p in cfg.obfuscate_strs):
raise ValueError("No ASCII characters present in obfuscate_strs.")
if any(any(not s.isascii() for s in p) for p in cfg.obfuscate_ascll):
raise ValueError("obfuscate_ascll must be full of ascll characters.")

try:
asyncio.run(async_start_obf(cfg))
Expand Down
13 changes: 11 additions & 2 deletions utils/obfuscator.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ def gen_obfstr(data: str, enum: OBFStrType, link=0):
enum.bi_map[data] = (rdstr := rd.choice(available_items))
return rdstr

need_ascll = enum in ENUM_LINKS[0]
ascll_pool = (
tuple(tuple(s for s in p if s.isascii() and not ("0" <= s <= "9")) for p in cfg.obfuscate_strs) if need_ascll else None
)
str_pool = cfg.obfuscate_ascll if enum in ENUM_LINKS[1] else cfg.obfuscate_strs
long = 2
times = 0
Expand All @@ -211,8 +215,13 @@ def gen_obfstr(data: str, enum: OBFStrType, link=0):
long = long if factor == 1 else next(i for i in range(long, computed) if len(enum.bi_map) < factor**i)

# gen
rdstr = "".join(rd.choices(rd.choice(tuple(str_pool)), k=long)) * rd.randint(1, 3)
if (rdstr in enum.bi_map.backward or ("0" <= rdstr[0] <= "9")) and long < computed:
pool_index = rd.randint(0, len(str_pool) - 1)
rdstr = (
(rd.choices(ascll_pool[pool_index])[0] + "".join(rd.choices(str_pool[pool_index], k=long - 1)))
if need_ascll
else ("".join(rd.choices(str_pool[pool_index], k=long)))
) * rd.randint(1, 3)
if (rdstr in enum.bi_map.backward) and long < computed:
times += 1
if all(c == data[0] for c in data):
rd.seed(data * rd.randint(1, 3))
Expand Down

0 comments on commit 98b62f8

Please sign in to comment.