Advertisement
vovanhik_24

Task1.2

Apr 8th, 2025
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.24 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Collections.Generic;
  7.  
  8. namespace Task1_2
  9. {
  10.     public class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             int port = 8080;
  15.             TcpListener listener = new TcpListener(IPAddress.Any, port);
  16.             listener.Start();
  17.             Console.WriteLine($"[+] Прокси-сервер запущен на порту {port}...");
  18.  
  19.             while (true)
  20.             {
  21.                 TcpClient client = listener.AcceptTcpClient();
  22.                 Thread thread = new Thread(() => HandleClient(client));
  23.                 thread.Start();
  24.             }
  25.         }
  26.  
  27.         private static readonly List<string> MaliciousPatterns = new List<string>
  28.         {
  29.             "SQLmap",
  30.             "MaliciousHeader",
  31.             "/etc/passwd",
  32.             "cmd.exe",
  33.             "union select"
  34.         };
  35.  
  36.         private static void HandleClient(TcpClient client)
  37.         {
  38.             using (client)
  39.             {
  40.                 try
  41.                 {
  42.                     NetworkStream clientStream = client.GetStream();
  43.                     byte[] buffer = new byte[8192];
  44.                     int bytesRead = clientStream.Read(buffer, 0, buffer.Length);
  45.  
  46.                     if (bytesRead == 0)
  47.                     {
  48.                         return;
  49.                     }
  50.  
  51.                     string requestText = Encoding.UTF8.GetString(buffer, 0, bytesRead);
  52.                     Console.WriteLine($"[>] Запрос от клиента:\n{requestText.Split('\n')[0]}");
  53.  
  54.                     if (IsMalicious(requestText))
  55.                     {
  56.                         Console.WriteLine("[!] Подозрительный запрос заблокирован!");
  57.                         string forbiddenResponse = "HTTP/1.1 403 Forbidden\r\nContent-Length: 0\r\n\r\n";
  58.                         byte[] responseBytes = Encoding.UTF8.GetBytes(forbiddenResponse);
  59.                         clientStream.Write(responseBytes, 0, responseBytes.Length);
  60.                         return;
  61.                     }
  62.  
  63.                     ForwardRequest(requestText, clientStream);
  64.                 }
  65.                 catch (Exception ex)
  66.                 {
  67.                     Console.WriteLine($"[x] Ошибка: {ex.Message}");
  68.                 }
  69.             }
  70.         }
  71.  
  72.         private static bool IsMalicious(string request)
  73.         {
  74.             request = request.ToLower();
  75.  
  76.             foreach (var pattern in MaliciousPatterns)
  77.             {
  78.                 if (request.Contains(pattern.ToLower()))
  79.                 {
  80.                     return true;
  81.                 }
  82.             }
  83.  
  84.             return false;
  85.         }
  86.  
  87.         private static void ForwardRequest(string requestText, NetworkStream clientStream)
  88.         {
  89.             try
  90.             {
  91.                 string[] lines = requestText.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
  92.                 string hostLine = Array.Find(lines, line => line.StartsWith("Host:", StringComparison.OrdinalIgnoreCase));
  93.  
  94.                 if (hostLine == null)
  95.                 {
  96.                     Console.WriteLine("[!] Не удалось определить хост.");
  97.                     return;
  98.                 }
  99.  
  100.                 string host = hostLine.Substring(6).Trim();
  101.                 int port = 80;
  102.  
  103.                 using (TcpClient server = new TcpClient(host, port))
  104.                 using (NetworkStream serverStream = server.GetStream())
  105.                 {
  106.                     byte[] requestBytes = Encoding.UTF8.GetBytes(requestText);
  107.                     serverStream.Write(requestBytes, 0, requestBytes.Length);
  108.  
  109.                     byte[] buffer = new byte[8192];
  110.                     int bytesRead;
  111.  
  112.                     while ((bytesRead = serverStream.Read(buffer, 0, buffer.Length)) > 0)
  113.                     {
  114.                         clientStream.Write(buffer, 0, bytesRead);
  115.                     }
  116.                 }
  117.             }
  118.             catch (Exception ex)
  119.             {
  120.                 Console.WriteLine($"[x] Ошибка при пересылке запроса: {ex.Message}");
  121.             }
  122.         }
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement