vovanhik_24

Task 2.2

Apr 8th, 2025
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.15 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Collections.Generic;
  8.  
  9. namespace HttpProxy
  10. {
  11.     class Program
  12.     {
  13.         static readonly List<string> maliciousPatterns = new List<string>
  14.         {
  15.             "SQLmap",
  16.             "MaliciousHeader",
  17.             "cmd=",
  18.             "exec",
  19.             "/etc/passwd",
  20.             "base64",
  21.             "select+from"
  22.         };
  23.  
  24.         static void Main(string[] args)
  25.         {
  26.             int port = 8888;
  27.             TcpListener listener = new TcpListener(IPAddress.Any, port);
  28.  
  29.             listener.Start();
  30.             Console.WriteLine($"[+] Прокси-сервер запущен на порту {port}");
  31.  
  32.             while (true)
  33.             {
  34.                 TcpClient client = listener.AcceptTcpClient();
  35.                 ThreadPool.QueueUserWorkItem(HandleClient, client);
  36.             }
  37.         }
  38.  
  39.         static void HandleClient(object obj)
  40.         {
  41.             TcpClient client = obj as TcpClient;
  42.  
  43.             try
  44.             {
  45.                 NetworkStream clientStream = client.GetStream();
  46.                 StreamReader reader = new StreamReader(clientStream);
  47.                 StreamWriter writer = new StreamWriter(clientStream) { AutoFlush = true };
  48.  
  49.                 string requestLine = reader.ReadLine();
  50.                 if (string.IsNullOrEmpty(requestLine)) return;
  51.  
  52.                 Console.WriteLine($"\n[>] Запрос: {requestLine}");
  53.  
  54.                 StringBuilder headers = new StringBuilder();
  55.                 string line;
  56.  
  57.                 while (!string.IsNullOrEmpty(line = reader.ReadLine()))
  58.                 {
  59.                     headers.AppendLine(line);
  60.                 }
  61.  
  62.                 string fullRequest = requestLine + "\n" + headers.ToString();
  63.  
  64.                 foreach (string pattern in maliciousPatterns)
  65.                 {
  66.                     if (fullRequest.IndexOf(pattern, StringComparison.OrdinalIgnoreCase) >= 0)
  67.                     {
  68.                         Console.ForegroundColor = ConsoleColor.Red;
  69.                         Console.WriteLine($"[!] Обнаружена угроза: {pattern}");
  70.                         Console.ResetColor();
  71.  
  72.                         string forbiddenResponse = "HTTP/1.1 403 Forbidden\r\nContent-Length: 0\r\n\r\n";
  73.                         byte[] forbiddenBytes = Encoding.ASCII.GetBytes(forbiddenResponse);
  74.                         clientStream.Write(forbiddenBytes, 0, forbiddenBytes.Length);
  75.                        
  76.                         client.Close();
  77.                         return;
  78.                     }
  79.                 }
  80.  
  81.                 string[] tokens = requestLine.Split(' ');
  82.  
  83.                 if (tokens.Length < 2)
  84.                 {
  85.                     client.Close();
  86.                     return;
  87.                 }
  88.  
  89.                 string method = tokens[0];
  90.                 string url = tokens[1];
  91.  
  92.                 string hostLine = headers.ToString().Split(new[] { "Host: " }, StringSplitOptions.None)[1];
  93.                 string host = hostLine.Split('\n')[0].Trim();
  94.  
  95.                 Uri uri = new Uri(url.StartsWith("http") ? url : "http://" + host + url);
  96.                 int port = uri.Port == -1 ? 80 : uri.Port;
  97.  
  98.                 TcpClient server = new TcpClient(uri.Host, port);
  99.                 NetworkStream serverStream = server.GetStream();
  100.  
  101.                 StreamWriter serverWriter = new StreamWriter(serverStream) { AutoFlush = true };
  102.                 serverWriter.WriteLine(requestLine);
  103.                 serverWriter.Write(headers.ToString() + "\r\n");
  104.  
  105.                 byte[] buffer = new byte[8192];
  106.                 int bytesRead;
  107.  
  108.                 while ((bytesRead = serverStream.Read(buffer, 0, buffer.Length)) > 0)
  109.                 {
  110.                     clientStream.Write(buffer, 0, bytesRead);
  111.                 }
  112.  
  113.                 client.Close();
  114.                 server.Close();
  115.             }
  116.             catch (Exception ex)
  117.             {
  118.                 Console.WriteLine($"[!] Ошибка: {ex.Message}");
  119.                 client?.Close();
  120.             }
  121.         }
  122.     }
  123. }
Add Comment
Please, Sign In to add comment