Created
February 20, 2024 15:28
-
-
Save Reelix/ab72d0ef57cb11c874356b6dfb6bd0bb to your computer and use it in GitHub Desktop.
A template file for console-based CTF challenges that require interaction with a server in C#.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Net; | |
using System.Net.Sockets; | |
using System.Text; | |
namespace ConsoleApp1 | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// Change these 2 | |
IPAddress ip = IPAddress.Parse("10.10.180.121"); | |
int port = 19001; | |
string responseText = ""; | |
int timeout = 2500; // ms | |
string initialText = ""; | |
byte[] buffer = new byte[15000]; | |
using (Socket socket = new(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) | |
{ | |
socket.ReceiveTimeout = timeout; | |
socket.SendTimeout = timeout; | |
try | |
{ | |
var result = socket.BeginConnect(ip, port, null, null); // Error if an invalid IP | |
bool success = result.AsyncWaitHandle.WaitOne(timeout, true); | |
if (success) | |
{ | |
if (!socket.Connected) | |
{ | |
socket.Close(); | |
} | |
if (initialText.Length != 0) | |
{ | |
byte[] cmdBytes = Encoding.ASCII.GetBytes(initialText.ToCharArray()); | |
socket.Send(cmdBytes, cmdBytes.Length, 0); | |
} | |
Thread.Sleep(2000); // Wait for all the data | |
int bytes = socket.Receive(buffer, buffer.Length, 0); | |
responseText += Encoding.ASCII.GetString(buffer, 0, bytes); | |
string oldResponse = ""; | |
List<string> responseLines = new List<string>(); | |
while (true) | |
{ | |
// Show the input | |
Console.WriteLine("Received: " + responseText); | |
// Do any required parsing | |
responseLines = responseText.Split('\n').ToList(); | |
// Remove invalid stuff | |
responseLines.Remove("Repeat the words 1000 times to get the flag"); | |
responseLines.RemoveAll(x => x == "" || x == "> "); | |
string toReturn = ""; | |
if (responseLines.Count == 1) | |
{ | |
// We only have our input - Format it and return it | |
toReturn = responseLines[0]; | |
toReturn = toReturn.Replace("> ", ""); | |
oldResponse = toReturn; | |
toReturn += "\n"; // Else its not considered a newline when sending | |
Console.WriteLine("Sending back: " + toReturn.Replace("\n", "")); | |
} | |
else | |
{ | |
// Something went wrong | |
Console.WriteLine("Woof"); | |
Console.ReadLine(); | |
} | |
// Send back the result | |
byte[] cmdBytes = Encoding.ASCII.GetBytes(toReturn.ToCharArray()); | |
socket.Send(cmdBytes, cmdBytes.Length, 0); | |
// Wait for a response | |
Thread.Sleep(50); | |
bytes = socket.Receive(buffer, buffer.Length, 0); | |
responseText = Encoding.ASCII.GetString(buffer, 0, bytes); | |
// Split response into lines | |
responseLines = responseText.Split('\n').ToList(); | |
// Remove invalid stuff | |
responseLines.RemoveAll(x => x == "" || x == "> "); | |
// If the response doesn''t contain any new lines, we didn't wait long enough | |
while (!responseLines.Any()) | |
{ | |
Thread.Sleep(50); | |
bytes = socket.Receive(buffer, buffer.Length, 0); | |
responseText = Encoding.ASCII.GetString(buffer, 0, bytes); | |
responseLines = responseText.Split('\n').ToList(); | |
responseLines.RemoveAll(x => x == "" || x == "> "); | |
} | |
} | |
} | |
else | |
{ | |
socket.Close(); | |
Console.WriteLine("Closed"); | |
Console.ReadLine(); | |
} | |
} | |
catch (SocketException ex) | |
{ | |
if (ex.SocketErrorCode == SocketError.TimedOut) | |
{ | |
// Could just mean that we''re using the wrong info to grab the banner | |
// Do nothing - A timeout response is handled later | |
} | |
else if (ex.SocketErrorCode == SocketError.ConnectionRefused) | |
{ | |
Console.WriteLine("Connection refused"); | |
Console.ReadLine(); | |
} | |
// Connection reset by peer | |
else if (ex.SocketErrorCode == SocketError.ConnectionReset) | |
{ | |
Console.WriteLine("Connection reset"); | |
Console.ReadLine(); | |
} | |
else | |
{ | |
Console.WriteLine($"Error in BannerGrab with SocketErrorCode code: {ex.SocketErrorCode}"); | |
Console.ReadLine(); | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine($"Error in General.BannerGrab ({ip}:{port} - {ex.Message})"); | |
Console.ReadLine(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment