How To: Write a Java Based Backdoor(RAT)

Write a Java Based Backdoor(RAT)

Welcome to my tutorial where Voidx and I will be expanding on my previous article to provide more clarity into the logic of the code.

My code can be found here -> http://pastebin.com/2nYicFPM
*****************************
Index:
$->Keyword
!->Start of code
^->End of code

*****************************
Keywords:
continue-> This key work is used to indicate that if the program does catch any errors to ignore them.

static->This key word is used to indicate that the variable is related to a type not a particular instance of the variable.

private->This indicates that the particular variable can only be accessed by class or method which created it.
************DISCLAIMER************
Voidx nor me is responsible for anything done with the backdoor.*
******************************

Public Class RAT

Okay now we will start on the actual code.

As you see we have declared a lot of variables here. We have done this as these variables are imperative for the RAT, the inclusion of the variables in the class RAT means that we have access to these variables from every method the class RAT holds. I have done this as the code can be highly customized now. Let's go over the variables.

Socket

!private static Socket s^ creates a private type Socket named s. This is the most important variable in the code as it is necessary to receive the input.

Scanner(Getting the input)

!private static Scanner sc^ is used to create a variable named sc of the type Scanner. In Java Scanner is used to read input into a buffer. This is imperative as later we will ask our sc to get the input stream of the Socket s we created just before.

PrintWriter

Surely you are getting what private static are doing by now, so from now on I will only talk about what these do. PrintWriter lets us write information to the Socket.

!int length^
This is used to calculate the amount of arguments passed on to the backdoor later.
ProcessBuilder
This type lets us execute commands.
BufferedReader
This type is used to get the output of the command run by ProcessBuilder ps.
!String line^
Declares a String called line.
We will see about this later
!String stdout^
Declares a String called stdout.
We will also see this later

Public Static Void Main(String Args[])

This method is the method that will be run upon execution, this is indicated by the method name being main. It also takes in an array of String with the array name being args. The Strings can be accessed by args0, args1..., Okay let's start.

Now we declare on a host name as this backdoor is DNS based. Meaning that it resolves a Domain name like www.example.com and turns that to an IP before connecting. 'localhost' means your own computer.

Then we declare what port we want our backdoor to connect to.

Now we need an String of the ip of the hostname. In this case we want "localhost" ip address as a String. Therefore we call the resolve method and pass in the DNS name we want to resolve into an ip.

******************************
Now head over to Voidx's tutorial to figure what the method resolve does.
https://null-byte.wonderhowto.com/how-to/create-rat-java-part-1-method-resolve-0167859/
******************************
Now we declare a variable of type int and name 'stat'.

The do-while loop is used try to connect to the ip address. The method returns an integer(1 if successful) that is why we had to declare an int called stat. The next line !while(stat!=1) says that if the method is successful(i.e. returns 1) to stop.

******************************
Head over to this article to find out what the connect method does->
Sorry not yet finished
******************************

Done now we are conneted to the server. We then use System.gc() to do a garbage collection. If you don't know what this is don't worry.

Remember the PrintWriter and Scanner we declared before as pr and sc,well we are now assigning the values of pr and sc into the ouput of the Socket named s and the input of the Socket named s.

Next we specify the Delimiter. Th Delimiter is used in order to know the end of the message received. Then received and stdout are Declared as Strings.

*******************************

The while(true) loop is used to continuously get input from the Socket by using the Scanner. This code is sc.next() which is written into the variable received. Next we use received.trim() to get rid of any leading of trailing whitespace. Next we remove the Delimiter from the old received value and write it into received. Then we act upon the String contained in received. The if statement is used to see if a quit was sent then we act on this by closing the Socket and it's streams and finally quiting the program.

*******************************

You may have noticed we had try/catch in the while loop. Well we will go over that later, but lets have a look on what to do if received did not contain "quit".The else is used to say that if there was no "quit" in received then do.Now we did declare a variable called stdout. Well now we are assigning the value of the execution of received into stdout. Then we will send the stdout through the Socket's Output Stream using the PrintWriter called pr followed by the \0 to indicate that it is the end of the string. pr.flush is used to empty the buffer.

******************************Head over here to find out how the execute method works ->
Sorry not yet done*

*******************************

Now to why that try/catch is in place. Now the try/catch is in place due to the following reason. What if the backdoor connects and then the server disconnects. What will happen in this scenario is that the backdoor will not know what to do and be rendered useless. We use the catch to connect back into the server.

******************************
Good luck
Mendax v2

7 Comments

Hello, i'd like to share a bit of constructive criticism so that the author can improve on his/hers next post, this may be a bit harsh, so i apologize in advance.

So here it goes...

Damn people, have you ever heard about Object Oriented Design? about SOLID principles? I don't even want to talk about design patterns, i understand that you probably want this to be more beginner friendly, but you could at least remove repetitive code from the Execute method, that's a time bomb waiting to explode. You also make a call to Garbage Collection, really? REALLY? How long have you been programming? a month? And also the try/catch blocks, i know that Java likes to make those mandatory, but those blocks use a lot of resources, so best avoid them, especially when you want to make something as lightweight as possible!

This code should not be considered a RAT, more like the basics of something that some day could become a RAT.

That is it, hope that this comment will help the author on his/hers next post.
Cheers!

I understand that Garbage Collection has an overhead of about 100ms but does that really matter in this case. I had heard of a developer who used System.gc() in a server(multi-client), obviously it's cases like this that it matters

Why is it bad to make a call to garbage collection. Any ideas how to fix the execute method? The try/catch blocks were kinda necessary though. I've only been programming in Java for about two months though. Programming in c/c++ and python for 3 years. And my design was to make this as easy to read and code as possible otherwise it would have been impossible to do a tutorial on. I am not being defensive, I did encounter problems especially in that execute method I just kinda patched it up and went with it but please show how i could avoid the repetitiveness, I hate it as well.

Here are my "improvements" on your one class app, this also should NOT be considered a RAT, just some basics in a client/server app..

Here, read this about why it is bad to explicitly call GC.

About the rest, i guess i'll return later with an adequate answer.

We're all at different levels of different languages, some of us write code one way, because we've learned up to that point, or simply because we thought of it that way and wrote it. Then when we came back to the said code, we think "Hey! we can also write it this way"

but thanks for the criticism

Hello i want to buy Java RAT .I am ready to pay for that .

Here are some features what i need in that RAT:

Goal:
We need Rename Ver RAT as WRAT Like Jrat, Jnet, ratty , Pupy RAT as below features for Mac and Linux

Linux
Basic Features: Device info
Builder to build server
Shut down, restart, uninstall server

  1. Key Logger
  1. Webcam Access
  1. Solution to listen to live streaming calls on the client pc not the server.
  1. File System (Device storage browsing, and download and upload)
  1. Browser History
  1. Remote Desktop –with photo taken
  1. Process –See all process
  1. Command Shell –to give commands

MAC
Basic Features: Device info
Builder to build server
Shut down, restart, uninstall server

  1. Key Logger
  1. Webcam Access
  1. solution to listen to live streaming calls on the client pc not the server.
  1. File System (Devise storage browsing, and download and upload)

5.Browser History

6..Remote Desktop –with photo taken

7.Process –See all process

8.Command Shell –to give commands

And signature code:

-----BEGIN PGP SIGNATURE-----
Comment: GPGTools - https://gpgtools.org

iQIcBAEBCgAGBQJZBjHnAAoJEGKztgNXV7XzW/gQAMrLSzRujWPh9zS2/1xnxJ6G
sL906pinphX+pGny0/r0cYOmZSFyp2l8S6Flx8g+8EMHReWU7ktCQNmyScDoli/Q
yoATIb4uHoxO8sWx2uqOW9jOOednITdox6L9cdLzKoFtsJyp0bMNRbUnbfsjTQVH
bUArnZ2DTskCLquFKgK+jBvs18ysqOl6sTlzN/cyfEPS5Ir6Yusd2UkkTXyCA0Ah
0laHp48XOCpe2M1ae6YvaqFoKkNgPhwQ7rXMWUNHjO6wwNhnk4xJc3/F9xsRxJUm
cuKlp7gyTPmbIpo834rWTr88Vfl6YojR6CLzokKMFOnX1wSiativfm8tuKXZEKtP
G3a/HDo3y5cAeFsgDMd9XONHIgqJ8uIx8oWoJ/KxDwvDDSYa15U5eTVwggdAGmh8
tL3rISvteMQKOzfZnPqLVTg75BqpsTCP9HUUeMo+7/TYBStHx4mKwBMpGBdSGUSP
c6Va/Go8GgZ+vK6nmXRpdscXRXOd9Asbg9Qa8eu1JobA38kC1SV418ankdkd7v+X
hThEoCeSQ7oSqs1SszZgL2fRfF37ERF6Jigx2HbElydmMmMgzMuAK31lrC4MQ5tE
Pk7P8iPOFapEbDTD+MQRRJydCmsHMVNd4C5zxkR4cToUL7/SkAkBzW9GP4Qvopps
GoDEndwP5Ln8s+yjD0w0
=omNb
-----END PGP SIGNATURE-----

If you can create one for me I'll pay u just DM me.I need this RAT as soon as possible.

Share Your Thoughts

  • Hot
  • Latest