Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using System.Net.Sockets;
  7.  
  8. namespace SocketSniffConsole {
  9.     class Program {
  10.         public static void Main(string[] args) {
  11.             byte[] buffer = new byte[4096];
  12.             Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
  13.  
  14.             IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("192.168.0.3"), 0);
  15.             socket.Bind(endpoint);
  16.  
  17.             socket.SetSocketOption(SocketOptionLevel.IP,
  18.                 SocketOptionName.HeaderIncluded,
  19.                 true);
  20.  
  21.             byte[] outValues = new byte[] { 1, 0, 0, 0 };
  22.             byte[] inValues = new byte[] { 1, 0, 0, 0 };
  23.  
  24.             socket.IOControl(IOControlCode.ReceiveAll,
  25.                 inValues,
  26.                 outValues);
  27.  
  28.             while (true) {
  29.                 int read = socket.Receive(buffer);
  30.  
  31.                 if (read >= buffer.Length) {
  32.                     Console.WriteLine("The packet was too big.  I'm not going to handle it.");
  33.                     continue;
  34.                 }
  35.  
  36.                 IPPacket p = IPPacket.Parse(buffer);
  37.  
  38.                 Console.WriteLine("Version: " + p.Version);
  39.                 Console.WriteLine("Protocol: " + p.Protocol.ToString());
  40.                 Console.WriteLine("Length: " + p.TotalLength);
  41.                 Console.WriteLine("Source: " + p.Source.ToString());
  42.                 Console.WriteLine("Destination: " + p.Destination.ToString());
  43.  
  44.                 Console.WriteLine();
  45.             }
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement