# Attribute-Based Obfuscation

## :coffee:Motivation

Sometimes you might feel the urge to apply specific protections to special parts of your code, attribute based obfuscation (also known as declarative obfuscation) allows you to choose which features to apply on a given type or method.

For example, your application might be formed by two main components, one linked with the authentication and copy protection of your software, and another one which is the core of your application itself. In those scenarios, it makes perfect sense to use a lightweight obfuscation in the whole assembly and more aggressive protection on the anti-piracy part.

Simultaneously, you might find yourself looking to add exceptions or special rules to certain methods or types, all that and more is possible with our SDK.

## :hammer\_pick:Obfuscation SDK

Legacy tools tended to rely on the [**Obfuscation Attribute**](https://docs.microsoft.com/en-us/dotnet/api/system.reflection.obfuscationattribute?view=net-5.0) shipped within .NET itself. This attribute works well until more complex scenarios and sub-settings of obfuscation technologies appear.\
\
For this reason, ever since our first public release, we recommend using our SDK instead of the classical Obfuscation Attribute.

Our SDK allows you to place rules in the most human-friendly manner, we achieve this by making a separated attribute for each one of our protections.

## :brain:Enabling a Protection using the SDK

### :inbox\_tray: Methods

Suppose that there is a **method** that you want to protect more than others. You can achieve this by using the proper protection attributes.

{% tabs %}
{% tab title="C#" %}

```csharp
[Virtualization()]
public static void MyImportantMethod()
{
// Your code goes in here
}
```

{% endtab %}

{% tab title="VB.NET" %}

```php
<Virtualization()>
Public Shared Sub MyImportantMethod()
// Your code goes in here
End Sub
```

{% endtab %}
{% endtabs %}

Simultaneously, you can stack multiple protections on the same method.

{% tabs %}
{% tab title="C#" %}

```csharp
[ControlFlow()]
[Virtualization()]
public static void MyImportantMethod()
{
// Your code goes in here
}
```

{% endtab %}

{% tab title="VB.NET" %}

```php
<ControlFlow()>
<Virtualization()>
Public Shared Sub MyImportantMethod()
// Your code goes in here
End Sub
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
:exclamation: **The order** in which you place the attributes **does not affect** the obfuscation result.
{% endhint %}

### :outbox\_tray: Types

Suppose there is a **class** that you want to protect more than others. You can do this by using the proper protection attributes.

{% tabs %}
{% tab title="C#" %}

```csharp
[Virtualization()]
public class MyClass {
// Class code
}
```

{% endtab %}

{% tab title="VB.NET" %}

```php
<Virtualization()>
Public Shared Sub MyImportantMethod()
// Your code goes in here
End Sub
```

{% endtab %}
{% endtabs %}

Simultaneously, you can stack multiple protections on the class.

{% tabs %}
{% tab title="C#" %}

```csharp
[ControlFlow()]
[Virtualization()]
public class MyClass {
// Class code
}
```

{% endtab %}
{% endtabs %}

###


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.cyphor.net/.net-code-obfuscation/advanced-obfuscation/attribute-based-obfuscation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
