Files
ai-llm-red-team-handbook/scripts/evasion/homoglyphs_unicode.py
T
shiva108 b3d3bac51f Add practical scripts directory with 400+ tools
- Extracted all code examples from handbook chapters
- Organized into 15 attack categories
- Created shared utilities (api_client, validators, logging, constants)
- Added workflow orchestration scripts
- Implemented install.sh for easy setup
- Renamed all scripts to descriptive functional names
- Added comprehensive README and documentation
- Included pytest test suite and configuration
2026-01-07 11:39:46 +01:00

85 lines
2.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""
Homoglyphs and Unicode Substitution
Source: Chapter_18_Evasion_Obfuscation_and_Adversarial_Inputs
Category: evasion
"""
import random
import random
import argparse
import sys
class HomoglyphObfuscator:
"""Replace characters with visually similar Unicode alternatives"""
def __init__(self):
# Common homoglyph mappings
self.homoglyphs = {
'a': ['а', 'ɑ', 'α', ''], # Cyrillic, Latin, Greek, Fullwidth
'e': ['е', '', 'ε', ''],
'o': ['о', 'ο', 'ο', ''],
'i': ['і', 'ı', 'ι', ''],
'c': ['с', 'ϲ', '', ''],
'p': ['р', 'ρ', '𝗉', ''],
's': ['ѕ', '', ''],
'h': ['һ', ''],
'n': ['п', 'ո'],
'x': ['х', '', ''],
'y': ['у', '', ''],
}
def obfuscate(self, text, percentage=0.5):
"""Replace percentage of characters with homoglyphs"""
result = []
for char in text:
lower_char = char.lower()
if random.random() < percentage and lower_char in self.homoglyphs:
replacement = random.choice(self.homoglyphs[lower_char])
result.append(replacement if char.islower() else replacement.upper())
else:
result.append(char)
return ''.join(result)
def strategic_obfuscate(self, text, target_words):
"""Obfuscate specific target words only"""
result = text
for target in target_words:
if target.lower() in result.lower():
obfuscated = self.obfuscate(target, percentage=1.0)
result = result.replace(target, obfuscated)
return result
# Example usage
obfuscator = HomoglyphObfuscator()
# General obfuscation
original = "How to hack a system"
obfuscated = obfuscator.obfuscate(original, percentage=0.3)
print(f"Original: {original}")
print(f"Obfuscated: {obfuscated}")
# Strategic targeting
sensitive_text = "This tool can be used to hack and exploit systems"
target_words = ['hack', 'exploit']
strategic = obfuscator.strategic_obfuscate(sensitive_text, target_words)
print(f"\nStrategic: {strategic}")
def main():
"""Command-line interface."""
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--verbose", "-v", action="store_true", help="Verbose output")
args = parser.parse_args()
# TODO: Add main execution logic
pass
if __name__ == "__main__":
main()