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 IList<BasicBlock> Blocks => _blocks;
public BasicBlockCollection(MethodDef method) public BasicBlockCollection(MethodDef method, bool computeInLoop)
{ {
_method = method; _method = method;
HashSet<Instruction> splitPoints = BuildSplitPoint(method); HashSet<Instruction> splitPoints = BuildSplitPoint(method);
BuildBasicBlocks(method, splitPoints); BuildBasicBlocks(method, splitPoints);
BuildInOutGraph(method); BuildInOutGraph(method);
if (computeInLoop)
{
ComputeBlocksInLoop();
}
}
public void ComputeBlocksInLoop()
{
var loopBlocks = FindLoopBlocks(_blocks); var loopBlocks = FindLoopBlocks(_blocks);
foreach (var block in loopBlocks) foreach (var block in loopBlocks)
{ {

View File

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

View File

@ -11,11 +11,13 @@ namespace Obfuz.ObfusPasses.SymbolObfus
{ {
public class ReflectionCompatibilityDetector public class ReflectionCompatibilityDetector
{ {
private readonly HashSet<ModuleDef> _assembliesToObfuscate;
private readonly List<ModuleDef> _obfuscatedAndNotObfuscatedModules; private readonly List<ModuleDef> _obfuscatedAndNotObfuscatedModules;
private readonly IObfuscationPolicy _renamePolicy; 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; _obfuscatedAndNotObfuscatedModules = obfuscatedAndNotObfuscatedModules;
_renamePolicy = renamePolicy; _renamePolicy = renamePolicy;
} }
@ -137,7 +139,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus
private bool IsAnyEnumItemRenamed(TypeDef typeDef) 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) private void AnalyzeCallvir(IMethod calledMethod, ITypeDefOrRef constrainedType)
@ -188,20 +190,28 @@ namespace Obfuz.ObfusPasses.SymbolObfus
private void AnalyzeEnum(IMethod method, TypeDef typeDef) private void AnalyzeEnum(IMethod method, TypeDef typeDef)
{ {
const int extraSearchInstructionCount = 3; const int extraSearchInstructionCount = 3;
TypeDef parseType = GetMethodGenericParameter(method)?.ToTypeDefOrRef()?.ResolveTypeDef(); TypeSig parseTypeSig = GetMethodGenericParameter(method);
TypeDef parseType = parseTypeSig?.ToTypeDefOrRef().ResolveTypeDef();
switch (method.Name) switch (method.Name)
{ {
case "Parse": case "Parse":
{ {
if (parseType != null) if (parseTypeSig != null)
{ {
// Enum.Parse<T>(string name) or Enum.Parse<T>(string name, bool caseInsensitive) // Enum.Parse<T>(string name) or Enum.Parse<T>(string name, bool caseInsensitive)
if (parseType != null)
{
if (IsAnyEnumItemRenamed(parseType)) if (IsAnyEnumItemRenamed(parseType))
{ {
Debug.LogError($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: Enum.Parse<T> field of T:{parseType.FullName} is renamed."); Debug.LogError($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: Enum.Parse<T> field of T:{parseType.FullName} is renamed.");
} }
} }
else else
{
Debug.LogWarning($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: Enum.Parse<T> field of T should not be renamed.");
}
}
else
{ {
// Enum.Parse(Type type, string name) or Enum.Parse(Type type, string name, bool ignoreCase) // Enum.Parse(Type type, string name) or Enum.Parse(Type type, string name, bool ignoreCase)
TypeDef enumType = FindLatestTypeOf(method.GetParamCount() + extraSearchInstructionCount)?.ResolveTypeDef(); TypeDef enumType = FindLatestTypeOf(method.GetParamCount() + extraSearchInstructionCount)?.ResolveTypeDef();
@ -218,15 +228,22 @@ namespace Obfuz.ObfusPasses.SymbolObfus
} }
case "TryParse": 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) // Enum.TryParse<T>(string name, out T result) or Enum.TryParse<T>(string name, bool ignoreCase, out T result)
if (parseType != null)
{
if (IsAnyEnumItemRenamed(parseType)) if (IsAnyEnumItemRenamed(parseType))
{ {
Debug.LogError($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: Enum.TryParse<T> field of T:{parseType.FullName} is renamed."); Debug.LogError($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: Enum.TryParse<T> field of T:{parseType.FullName} is renamed.");
} }
} }
else else
{
Debug.LogWarning($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: Enum.TryParse<T> field of T should not be renamed.");
}
}
else
{ {
throw new Exception("impossible"); throw new Exception("impossible");
} }

View File

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