Archive

Posts Tagged ‘Redgate’

Use Red Gate SmartAssembly in Automated Team Build to Obfuscate Assemblies


This post is in response to a question asked in the TFS Automated Build Forum: http://social.msdn.microsoft.com/Forums/en-US/tfsgeneral/thread/44de4e0a-fcc0-4874-a4af-804dc458f43f/#b0c4c0a3-758a-4204-ac4b-d4e1e93e2d75.

Smart Assembly is a product to obfuscate .NET assemblies and automated error reporting. For more information about the tool, you can visit http://www.red-gate.com/products/dotnet-development/smartassembly/

To answer the question in the MSDN forum, I downloaded and installed the trial version Smart Assembly from Red Gate’s website. There wasn’t anything tricky while installing the product and it was straight forward.

I had to create a smart assembly project (.saproj) file which is an xml file that contains all the features that need to be enabled and their configuration. Smart Assembly comes with its configurator which makes it really easy to configure the features to be used. The following is a screen shot of the configurator

Follow these steps to create a project

  • Click New Project
  • Click Browse Assembly button
  • Select the file to be obfuscated, you want to select the dll or exe file located in the bin\release folder

  • Click Set Destination button, the configurator doesn’t allow you to select the same destination file as the input one using the same folder (i.e. over write the input one)

  • I have set the destination to be bin\SmartAssembly\SmartAssemblyPOC.exe
  • Notice the toolbar, clicking on toolbar buttons auto scrolls to the respective section

  • Check the assembly that you want to obfuscate
  • Click Save project on the left list
  • Save the saproj at the same directory where the project is saved. You can choose other location if you want to
  • Test your project by clicking Build on the left

Usually we don’t have one project in a solution. Therefore it is not practical to open the smart assembly tool for every project and build it manually. Luckily, we can integrate building the smart assembly project while building the project using MSBuild.

I basically followed the instruction in this link;

  • Right click on your project
  • Unload the project
  • Find the following line if you have a vb project

<Import Project=$(MSBuildToolsPath)\Microsoft.VisualBasic.targets />

  • Or this line if you are using C#

<Import Project=$(MSBuildToolsPath)\Microsoft.CSharp.targets />

  • Below the above line add the Smart Assembly targets

<UsingTask TaskName=SmartAssembly.MSBuild.Tasks.Build
AssemblyName=SmartAssembly.MSBuild.Tasks, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7f465a1c156d4d57 />

<Target Name=AfterBuild
Condition= ‘$(Configuration)’ == ‘Release’ >
 <SmartAssembly.MSBuild.Tasks.Build ProjectFile=SmartAssemblyPOC.saproj
input=$(OutDir)SmartAssemblyPOC.exe

output=$(OutDir)SmartAssemblyPOC.exe

OverwriteAssembly=true

MarkAsReleased=true/>
</Target> 

  • Save the project
  • Load the project from solution explorer
  • I will quickly explain what the above lines mean. The UsingTask element maps the AssemblyName to the TaskName. Version is the version number of the SmartAssembly. If you are using version 5, change the version number to 5.0.0.0. AfterBuild target is executed after the build is complete to obfuscate the smartassemblyPOC.exe in the release folder and then override it on the same folder. The $(OutDir) will be replaced by MSBuild with output folder of your assembly. The input and output in the MSBuild task overrides the input and output in the saproj
  • Build the project in Visual studio to make sure it compiles and the assembly gets obfuscated
  • It is not necessary to include the saproj file in the project as long as it is checked in. To include the saproj in the project:

  • Check in your changes
  • You need the SmartAssembly executable to be installed on your build machine otherwise Team Build won’t be able to find the SmartAssembly Tasks
  • After the Team build is done, view the log file
  • Notice the execution of the SmartAssembly task

Advertisements