Created
January 17, 2026 01:53
-
-
Save ChrisPritchard/10574c839ece873a33d75e0d452bc76b to your computer and use it in GitHub Desktop.
C# SYN packet network scanner
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; | |
| using System.Net; | |
| using System.Net.Sockets; | |
| using System.Text; | |
| class SynPacketSender | |
| { | |
| static void Main(string[] args) | |
| { | |
| string sourceIp = "192.168.68.68"; // Your IP address | |
| string destIp = "192.168.68.66"; // Target IP address | |
| for (var port = 1; port <= 10000; port++) { | |
| SendRawPacket(sourceIp, destIp, port); | |
| } | |
| } | |
| static byte[] CraftSynPacket(string sourceIp, string destIp, int sourcePort, int destPort) | |
| { | |
| // IP Header (20 bytes) | |
| byte[] ipHeader = new byte[20]; | |
| // TCP Header (20 bytes) | |
| byte[] tcpHeader = new byte[20]; | |
| // Pseudo-header for TCP checksum calculation | |
| byte[] pseudoHeader = new byte[12]; | |
| // Fill IP header fields | |
| ipHeader[0] = 0x45; // Version (4) + IHL (5, meaning 20 bytes) | |
| ipHeader[2] = 0x00; // Total length (set later) | |
| ipHeader[3] = 0x3C; // Total length (60 bytes) | |
| ipHeader[8] = 0x40; // TTL (64) | |
| ipHeader[9] = 0x06; // Protocol (TCP) | |
| // Set source and destination IP addresses | |
| byte[] sourceIpBytes = IPAddress.Parse(sourceIp).GetAddressBytes(); | |
| byte[] destIpBytes = IPAddress.Parse(destIp).GetAddressBytes(); | |
| Array.Copy(sourceIpBytes, 0, ipHeader, 12, 4); | |
| Array.Copy(destIpBytes, 0, ipHeader, 16, 4); | |
| // Fill TCP header fields | |
| tcpHeader[0] = (byte)(sourcePort >> 8); // Source port (high byte) | |
| tcpHeader[1] = (byte)(sourcePort & 0xFF); // Source port (low byte) | |
| tcpHeader[2] = (byte)(destPort >> 8); // Destination port (high byte) | |
| tcpHeader[3] = (byte)(destPort & 0xFF); // Destination port (low byte) | |
| tcpHeader[4] = 0x00; // Sequence number (high byte) | |
| tcpHeader[5] = 0x00; // Sequence number (low byte) | |
| tcpHeader[6] = 0x00; // Sequence number (low byte) | |
| tcpHeader[7] = 0x00; // Sequence number (low byte) | |
| tcpHeader[12] = 0x50; // Data offset (5, meaning 20 bytes) | |
| tcpHeader[13] = 0x02; // SYN flag | |
| // Calculate TCP checksum | |
| // Pseudo-header: Source IP + Destination IP + Protocol + TCP length | |
| Array.Copy(sourceIpBytes, 0, pseudoHeader, 0, 4); | |
| Array.Copy(destIpBytes, 0, pseudoHeader, 4, 4); | |
| pseudoHeader[9] = 0x06; // Protocol (TCP) | |
| pseudoHeader[10] = 0x00; // TCP length (high byte) | |
| pseudoHeader[11] = 0x14; // TCP length (low byte, 20 bytes) | |
| // Combine pseudo-header, TCP header, and data (empty for SYN packet) | |
| byte[] checksumData = new byte[pseudoHeader.Length + tcpHeader.Length]; | |
| Array.Copy(pseudoHeader, 0, checksumData, 0, pseudoHeader.Length); | |
| Array.Copy(tcpHeader, 0, checksumData, pseudoHeader.Length, tcpHeader.Length); | |
| // Compute checksum | |
| ushort tcpChecksum = ComputeChecksum(checksumData); | |
| tcpHeader[16] = (byte)(tcpChecksum >> 8); // Checksum (high byte) | |
| tcpHeader[17] = (byte)(tcpChecksum & 0xFF); // Checksum (low byte) | |
| // Combine IP and TCP headers into a single packet | |
| byte[] packet = new byte[ipHeader.Length + tcpHeader.Length]; | |
| Array.Copy(ipHeader, 0, packet, 0, ipHeader.Length); | |
| Array.Copy(tcpHeader, 0, packet, ipHeader.Length, tcpHeader.Length); | |
| return packet; | |
| } | |
| static void SendRawPacket(string sourceIp, string destIp, int port) | |
| { | |
| var syn_packet = CraftSynPacket(sourceIp, destIp, 51432, port); | |
| try | |
| { | |
| // Create a raw socket | |
| Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Tcp); | |
| socket.Bind(new IPEndPoint(IPAddress.Any, 0)); | |
| socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, true); | |
| // Send the packet | |
| socket.SendTo(syn_packet, new IPEndPoint(IPAddress.Parse(destIp), 0)); | |
| var buffer = new byte[1024]; | |
| var received = socket.Receive(buffer); | |
| if (received > 13 && buffer[13] == 0x12) | |
| Console.WriteLine($"Port {port} is open on {destIp}"); | |
| } | |
| catch (Exception ex) | |
| { | |
| Console.WriteLine($"Error: {ex.Message}"); | |
| } | |
| } | |
| static ushort ComputeChecksum(byte[] data) | |
| { | |
| int sum = 0; | |
| for (int i = 0; i < data.Length; i += 2) | |
| { | |
| if (i + 1 < data.Length) | |
| { | |
| sum += (data[i] << 8) + data[i + 1]; | |
| } | |
| else | |
| { | |
| sum += data[i] << 8; | |
| } | |
| } | |
| while ((sum >> 16) != 0) | |
| { | |
| sum = (sum & 0xFFFF) + (sum >> 16); | |
| } | |
| return (ushort)~sum; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment