# Dosfuscated Scripts

### 1. Introduction

> Obfuscation refers to various techniques for modifying the source code of a program in such a way that it is difficult for analysts to analyze it. The effort required for reverse-engineering a malware thus increases significantly. In addition, the malicious code cannot be detected as easily by automated analysis. To obfuscate code, for example, individual letters can be replaced by others, and individual components of a program or files can be encrypted.

Attackers often use [Obfuscation framewoks](https://github.com/danielbohannon/Invoke-DOSfuscation) to hide their maliscious cmd or powershell payloads.

I've discovered a really cool IT-Security blog which shows us obfuscated examples and howto decode them:

{% embed url="https://www.kahusecurity.com/posts/deobfuscating_dosfuscated_scripts.html" %}

### 2. Deobfuscation Tips

* Replace "CMD" and "PowerShell" verbs (could be obfuscated) at the end with "echo"
* Find the final variable that holds the deobfuscated content then replace the execution verb with "echo" or "set"
* If you use "set" then get rid of the percent signs (%) before and after the variable name
* A variable name could contain spaces, or more specifically, end with a space so cleaning up the script or extracting the variable name with too little or too much spaces will likely result in failure
* Sometimes you can delete everything after the pipe (|) (but if the script has a double-quote at the end then be sure you keep it)
* Change "call" to "echo" then reference the previously assigned variable with surrounding quotes (!var!)

### 3. Examples

At the bottom is a "DO %b /c %wZ%". The "DO %b" is "cmd /c" and "%wZ%" is the variable we want to see the contents of. So you just make the change you see

![](https://3977837039-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfT0VPyK6X13Egd9pzy%2Fuploads%2FBclNGwgIjpEAqHndYS6v%2Fsample01_enc.png?alt=media\&token=b70b681c-f4a4-4195-9493-d15f2a8dee46)

If you paste this to the CMD prompt and let it run you can see that the deobfuscated content is "net user".

<div align="left"><img src="https://3977837039-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfT0VPyK6X13Egd9pzy%2Fuploads%2F00OB13X7ULkpDvA1slJd%2Fsample01_dec.png?alt=media&#x26;token=a4aae184-3aad-4af0-a9b8-b904b4f02a23" alt=""></div>

Here's one example where you can delete everything after the pipe which is near the end of the script.

![](https://3977837039-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfT0VPyK6X13Egd9pzy%2Fuploads%2FwCFYu0aavThO0GBz5ifO%2Fsample02_enc.png?alt=media\&token=216b4e3d-bb51-43ae-87e7-a8874b5b2260)

The result is shown at the very bottom.

<div align="left"><img src="https://3977837039-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfT0VPyK6X13Egd9pzy%2Fuploads%2FITuT22Th9MAkrkOWc6db%2Fsample02_dec.png?alt=media&#x26;token=bfdbfffb-b758-4875-a620-1a9c00040640" alt=""></div>

Here's a reversed script where you can replace the "%TMP:\~-8 ... -12,+1%" at the end with "echo".

![](https://3977837039-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfT0VPyK6X13Egd9pzy%2Fuploads%2FCMxwdhCrVtp61ZK9xTgD%2Fsample03_enc.png?alt=media\&token=23a72db3-5579-42db-b4ae-c9a5dc7c231d)

And "net user" is the result.

<div align="left"><img src="https://3977837039-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfT0VPyK6X13Egd9pzy%2Fuploads%2FbJK96hcOkBgNzT0ywbSH%2Fsample03_dec.png?alt=media&#x26;token=76d92bdc-3518-48be-abc3-1da6922a7bf2" alt=""></div>

In this example, you can replace the "call" at the end with "echo" and then you get the variable to the left of the equal sign. Look for the double-exclamation marks and take one of them.

![](https://3977837039-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfT0VPyK6X13Egd9pzy%2Fuploads%2FVpreIHVlodVHquJPPlmA%2Fsample04_enc.png?alt=media\&token=bf996516-e469-48e5-bc72-b2ceeec1299e)

The result is "net user" again.

<div align="left"><img src="https://3977837039-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfT0VPyK6X13Egd9pzy%2Fuploads%2FJNEAIb02zuitXlB324sJ%2Fsample04_dec.png?alt=media&#x26;token=df7ccd5a-5da2-4ba4-aebd-360b7961289f" alt=""></div>

In this last example, you can replace "echo" with "set" and remove the percent signs at the beginning and end. Do also remove the pipe up to the closest parenthesis.

![](https://3977837039-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfT0VPyK6X13Egd9pzy%2Fuploads%2FY1ui5mVLS9nLVJllZGLf%2Fsample05_enc.png?alt=media\&token=a119790b-697b-4065-956e-2e62bf0c98e6)

And get this result.

<div align="left"><img src="https://3977837039-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfT0VPyK6X13Egd9pzy%2Fuploads%2FvtkrlooGvU3Jtki0yupp%2Fsample05_dec.png?alt=media&#x26;token=e5ff2b15-7935-4776-9828-980564b6f112" alt=""></div>
