Advertisement
N3ll4

IP/Network Tool

Jun 22nd, 2024
2,440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.42 KB | Source Code | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Net.Http;
  4. using System.Runtime.InteropServices;
  5. using System.Threading.Tasks;
  6.  
  7. class Program
  8. {
  9.     const int AF_INET = 2;
  10.     const int TCP_TABLE_OWNER_PID_ALL = 5;
  11.     const string BASE_URL = "https://www.virustotal.com/vtapi/v2/ip-address/report";
  12.     const string API_KEY = " ";
  13.  
  14.     [StructLayout(LayoutKind.Sequential)]
  15.     public struct MIB_TCPROW_OWNER_PID
  16.     {
  17.         public uint state;
  18.         public uint localAddr;
  19.         public uint localPort;
  20.         public uint remoteAddr;
  21.         public uint remotePort;
  22.         public uint owningPid;
  23.     }
  24.  
  25.     [StructLayout(LayoutKind.Sequential)]
  26.     public struct MIB_TCPTABLE_OWNER_PID
  27.     {
  28.         public uint dwNumEntries;
  29.         [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
  30.         public MIB_TCPROW_OWNER_PID[] table;
  31.     }
  32.  
  33.     [DllImport("iphlpapi.dll", SetLastError = true)]
  34.     static extern int GetExtendedTcpTable(
  35.         IntPtr pTcpTable,
  36.         ref int pdwSize,
  37.         bool bOrder,
  38.         int ulAf,
  39.         int TableClass,
  40.         int Reserved
  41.     );
  42.  
  43.     static async Task Main(string[] args)
  44.     {
  45.         DisplayBanner();
  46.         DisplayMenu();
  47.  
  48.         string choice = Console.ReadLine();
  49.  
  50.         switch (choice)
  51.         {
  52.             case "1":
  53.                 ListActiveConnections();
  54.                 break;
  55.             case "2":
  56.                 await CheckIp();
  57.                 break;
  58.             default:
  59.                 Console.WriteLine("Ungültige Auswahl.");
  60.                 break;
  61.         }
  62.     }
  63.  
  64.     static void DisplayBanner()
  65.     {
  66.         Console.WriteLine(" ____  ____      ____  _____  _____  __   ");
  67.         Console.WriteLine("(_  _)(  _ \\ ___(_  _)(  _  )(  _  )(  )  ");
  68.         Console.WriteLine(" _)(_  )___/(___) )(   )(_)(  )(_)(  )(__ ");
  69.         Console.WriteLine("(____)(__)       (__) (_____)(_____)(____)");
  70.         Console.WriteLine("            N3LL4 v.1");
  71.         Console.WriteLine();
  72.     }
  73.  
  74.     static void DisplayMenu()
  75.     {
  76.         Console.WriteLine("Willkommen! Was willst du tun?");
  77.         Console.WriteLine("[1] Auflisten aktiver Netzwerkverbindungen");
  78.         Console.WriteLine("[2] IP überprüfen");
  79.         Console.Write("Gebe die Nummer ein: ");
  80.     }
  81.  
  82.     static void ListActiveConnections()
  83.     {
  84.         try
  85.         {
  86.             int bufferSize = 0;
  87.             IntPtr tcpTable = IntPtr.Zero;
  88.  
  89.             GetExtendedTcpTable(tcpTable, ref bufferSize, true, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0);
  90.             tcpTable = Marshal.AllocHGlobal(bufferSize);
  91.  
  92.             if (GetExtendedTcpTable(tcpTable, ref bufferSize, true, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0) == 0)
  93.             {
  94.                 IntPtr currentRowPtr = tcpTable;
  95.                 int numEntries = Marshal.ReadInt32(currentRowPtr);
  96.                 currentRowPtr = (IntPtr)((long)currentRowPtr + Marshal.SizeOf(typeof(uint)));
  97.  
  98.                 for (int i = 0; i < numEntries; i++)
  99.                 {
  100.                     MIB_TCPROW_OWNER_PID tcpRow = Marshal.PtrToStructure<MIB_TCPROW_OWNER_PID>(currentRowPtr);
  101.  
  102.                     string localAddress = new IPAddress(tcpRow.localAddr).ToString();
  103.                     string remoteAddress = new IPAddress(tcpRow.remoteAddr).ToString();
  104.                     int localPort = ntohs((ushort)tcpRow.localPort);
  105.                     int remotePort = ntohs((ushort)tcpRow.remotePort);
  106.  
  107.                     Console.WriteLine($"Local Address: {localAddress}:{localPort} - Remote Address: {remoteAddress}:{remotePort} - PID: {tcpRow.owningPid}");
  108.  
  109.                     currentRowPtr = (IntPtr)((long)currentRowPtr + Marshal.SizeOf(typeof(MIB_TCPROW_OWNER_PID)));
  110.                 }
  111.             }
  112.  
  113.             Marshal.FreeHGlobal(tcpTable);
  114.         }
  115.         catch (Exception ex)
  116.         {
  117.             Console.WriteLine($"Ein Fehler ist aufgetreten: {ex.Message}");
  118.         }
  119.     }
  120.  
  121.     static async Task CheckIp()
  122.     {
  123.         try
  124.         {
  125.             Console.Write("Bitte geben Sie die zu überprüfende IP-Adresse ein: ");
  126.             string ip = Console.ReadLine();
  127.  
  128.             if (IPAddress.TryParse(ip, out IPAddress address))
  129.             {
  130.                 bool isMalicious = await CheckIpWithVirusTotal(ip);
  131.                 Console.WriteLine($"Is Malicious: {isMalicious}");
  132.             }
  133.             else
  134.             {
  135.                 Console.WriteLine("Ungültige IP-Adresse eingegeben.");
  136.             }
  137.         }
  138.         catch (Exception ex)
  139.         {
  140.             Console.WriteLine($"Ein Fehler ist aufgetreten: {ex.Message}");
  141.         }
  142.     }
  143.  
  144.     static async Task<bool> CheckIpWithVirusTotal(string ip)
  145.     {
  146.         using (HttpClient client = new HttpClient())
  147.         {
  148.             try
  149.             {
  150.                 string url = $"{BASE_URL}?apikey={API_KEY}&ip={ip}";
  151.  
  152.                 HttpResponseMessage response = await client.GetAsync(url);
  153.                 response.EnsureSuccessStatusCode();
  154.  
  155.                 string responseBody = await response.Content.ReadAsStringAsync();
  156.                 return !responseBody.Contains("\"positives\":0");
  157.             }
  158.             catch (HttpRequestException e)
  159.             {
  160.                 Console.WriteLine($"Anfragefehler: {e.Message}");
  161.                 return false;
  162.             }
  163.         }
  164.     }
  165.  
  166.     static ushort ntohs(ushort netshort)
  167.     {
  168.         return (ushort)(((netshort & 0xFF) << 8) | ((netshort & 0xFF00) >> 8));
  169.     }
  170. }
  171.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement