Changing method parameters to "p1", "p2", etc.

I'm getting close to running out of things to anonymize / obfuscate, but haven't quite reached that point yet. In this article I'll show how I modifyed Roslyn to (optionally) change method parameter names to "p1", "p2", etc. This is fairly simple, the advantage being that method parameter names are not visible from other modules so there is no need to persist the modified parameter names or numbers. (And even if the parameter names were visible externally the renaming scheme is so simple that the new names could be deduced externally anyway.)

Here's the code that has been changed and added to Roslyn in this step.

Note: The code shown below is somewhat obsolete. A newer version is available for download - see this article.


This source file is part of the CodeAnalysis project in Roslyn. In the Visual Studio Solution Explorer it can be found under CodeAnalysis - PEWriter.

Compared with the previous steps one more method has been modified.

private void PopulateParamTableRows() { var parameterDefs = this.GetParameterDefs(); metadata.SetCapacity(TableIndex.Param, parameterDefs.Count); foreach (IParameterDefinition parDef in parameterDefs) { //Yacks11: Change all parameter names to "p1", "p2", etc. if creating an anonymized // output module StringHandle parameterNameHandle = AnonymizeMethodParameter(parDef); metadata.AddParameter( attributes: GetParameterAttributes(parDef), sequenceNumber: (parDef is ReturnValueParameter) ? 0 : parDef.Index + 1, //name: GetStringHandleForNameAndCheckLength(parDef.Name, parDef)); name: parameterNameHandle); } }


This is a source file which has been added to the CodeAnalysis project. One additional method has been added.

/// <summary> /// Method to either "anonymize" a C# method parameter name if applicable, or else to do /// standard processing to emit the parameter name. (Some of the code in this method is copied /// from original code in the MetadataWriter.PopulateParamTableRows() method.) /// </summary> private StringHandle AnonymizeMethodParameter(IParameterDefinition parameterDefinition) { if (EmittingAnonymized()) return metadata.GetOrAddString("p" + (parameterDefinition.Index + 1)); return GetStringHandleForNameAndCheckLength(parameterDefinition.Name, parameterDefinition); }


As usual, I used a (slightly modified) library assembly Merlinia.CommonClasses.MArrays to test the modified Roslyn compiler. These screen shots (with highlighting added) show the Parm metadata for the non-anonymized and the anonymized versions of the MArrays assembly as displayed by JetBrains dotPeek.

ModRos 11 Snap1

ModRos 11 Snap2

Finally I'll just mention that calling this code from another program did not result in any problems.

You must login to post a comment.
Loading comment... The comment will be refreshed after 00:00.

Be the first to comment.