Compare commits

...

3 Commits
main ... 1.x

4 changed files with 38 additions and 12 deletions

View File

@ -38,13 +38,20 @@ namespace Obfuz.Emit
public IList<BasicBlock> Blocks => _blocks;
public BasicBlockCollection(MethodDef method)
public BasicBlockCollection(MethodDef method, bool computeInLoop)
{
_method = method;
HashSet<Instruction> splitPoints = BuildSplitPoint(method);
BuildBasicBlocks(method, splitPoints);
BuildInOutGraph(method);
if (computeInLoop)
{
ComputeBlocksInLoop();
}
}
public void ComputeBlocksInLoop()
{
var loopBlocks = FindLoopBlocks(_blocks);
foreach (var block in loopBlocks)
{

View File

@ -10,6 +10,8 @@ namespace Obfuz.ObfusPasses
{
protected abstract bool NeedObfuscateMethod(MethodDef method);
protected virtual bool ComputeBlockInLoop => true;
public override void Process()
{
var ctx = ObfuscationPassContext.Current;
@ -48,7 +50,7 @@ namespace Obfuz.ObfusPasses
private void ObfuscateData(MethodDef method)
{
BasicBlockCollection bbc = new BasicBlockCollection(method);
BasicBlockCollection bbc = new BasicBlockCollection(method, ComputeBlockInLoop);
IList<Instruction> instructions = method.Body.Instructions;

View File

@ -11,11 +11,13 @@ namespace Obfuz.ObfusPasses.SymbolObfus
{
public class ReflectionCompatibilityDetector
{
private readonly HashSet<ModuleDef> _assembliesToObfuscate;
private readonly List<ModuleDef> _obfuscatedAndNotObfuscatedModules;
private readonly IObfuscationPolicy _renamePolicy;
public ReflectionCompatibilityDetector(List<ModuleDef> obfuscatedAndNotObfuscatedModules, IObfuscationPolicy renamePolicy)
public ReflectionCompatibilityDetector(List<ModuleDef> assembliesToObfuscate, List<ModuleDef> obfuscatedAndNotObfuscatedModules, IObfuscationPolicy renamePolicy)
{
_assembliesToObfuscate = new HashSet<ModuleDef>(assembliesToObfuscate);
_obfuscatedAndNotObfuscatedModules = obfuscatedAndNotObfuscatedModules;
_renamePolicy = renamePolicy;
}
@ -137,7 +139,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus
private bool IsAnyEnumItemRenamed(TypeDef typeDef)
{
return typeDef.Fields.Any(f => _renamePolicy.NeedRename(f));
return _assembliesToObfuscate.Contains(typeDef.Module) && typeDef.Fields.Any(f => _renamePolicy.NeedRename(f));
}
private void AnalyzeCallvir(IMethod calledMethod, ITypeDefOrRef constrainedType)
@ -188,17 +190,25 @@ namespace Obfuz.ObfusPasses.SymbolObfus
private void AnalyzeEnum(IMethod method, TypeDef typeDef)
{
const int extraSearchInstructionCount = 3;
TypeDef parseType = GetMethodGenericParameter(method)?.ToTypeDefOrRef()?.ResolveTypeDef();
TypeSig parseTypeSig = GetMethodGenericParameter(method);
TypeDef parseType = parseTypeSig?.ToTypeDefOrRef().ResolveTypeDef();
switch (method.Name)
{
case "Parse":
{
if (parseType != null)
if (parseTypeSig != null)
{
// Enum.Parse<T>(string name) or Enum.Parse<T>(string name, bool caseInsensitive)
if (IsAnyEnumItemRenamed(parseType))
if (parseType != null)
{
Debug.LogError($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: Enum.Parse<T> field of T:{parseType.FullName} is renamed.");
if (IsAnyEnumItemRenamed(parseType))
{
Debug.LogError($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: Enum.Parse<T> field of T:{parseType.FullName} is renamed.");
}
}
else
{
Debug.LogWarning($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: Enum.Parse<T> field of T should not be renamed.");
}
}
else
@ -218,12 +228,19 @@ namespace Obfuz.ObfusPasses.SymbolObfus
}
case "TryParse":
{
if (parseType != null)
if (parseTypeSig != null)
{
// Enum.TryParse<T>(string name, out T result) or Enum.TryParse<T>(string name, bool ignoreCase, out T result)
if (IsAnyEnumItemRenamed(parseType))
if (parseType != null)
{
Debug.LogError($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: Enum.TryParse<T> field of T:{parseType.FullName} is renamed.");
if (IsAnyEnumItemRenamed(parseType))
{
Debug.LogError($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: Enum.TryParse<T> field of T:{parseType.FullName} is renamed.");
}
}
else
{
Debug.LogWarning($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: Enum.TryParse<T> field of T should not be renamed.");
}
}
else

View File

@ -182,7 +182,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus
PrecomputeNeedRename();
if (_detectReflectionCompatibility)
{
var reflectionCompatibilityDetector = new ReflectionCompatibilityDetector(_obfuscatedAndNotObfuscatedModules, _renamePolicy);
var reflectionCompatibilityDetector = new ReflectionCompatibilityDetector(_toObfuscatedModules, _obfuscatedAndNotObfuscatedModules, _renamePolicy);
reflectionCompatibilityDetector.Analyze();
}
RenameTypes();