By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,169 Members | 1,484 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,169 IT Pros & Developers. It's quick & easy.

Constructing and sending a SYN header

P: n/a
The following code is intended to construct a SYN header and send it
to a host (just the localhost at the moment) to form part of a port
scanner tool for network security. However, the packet that the
socket receives in response is a copy of the SYN packet sent with some
data inserted at the start. Can someone please shed some light on
what is happening?

Code:

using System;
using System.Net;
using System.Net.Sockets;

public class SynTest
{
public static void Main ()
{
IPAddress localIP = IPAddress.Parse ("127.0.0.1");
int localPort = 1025;
IPEndPoint localEndPoint = new IPEndPoint (localIP, localPort);

IPAddress remoteIP = IPAddress.Parse ("127.0.0.1"); // Use
localhost for now int remotePort = 22;
IPEndPoint remoteEndPoint = new IPEndPoint (remoteIP, remotePort);

Socket ipSocket = new Socket (AddressFamily.InterNetwork,
SocketType.Raw, ProtocolType.IP);

// Bind the socket to the local end point

ipSocket.Bind (localEndPoint);

// Code to construct the datagram
byte[] datagram = new byte[32];

// Source address

datagram[0] = 127;
datagram[1] = 0;
datagram[2] = 0;
datagram[3] = 1;

// Destimation address

datagram [4] = 127;
datagram [5] = 0;
datagram [6] = 0;
datagram [7] = 1;

// Zero bits

datagram [8] = 0;

// Protocol - TCP = 6

datagram [9] = 6;

// TCP length

datagram [10] = 0;
datagram [11] = 32;

// Source port

datagram [12] = 4; // 1025
datagram [13] = 1;
// Destination port

datagram [14] = 0;
datagram [15] = 22; // 22

// Sequence number

datagram [16] = 0;
datagram [17] = 0;
datagram [18] = 0;
datagram [19] = 0;

// Acknowledgement number

datagram [20] = 0;
datagram [21] = 0;
datagram [22] = 0;
datagram [23] = 0;

// Data Offset + reserved - the left four bytes = 5 for header
length (* four-byte words), the right four bits = 0. The whole byte =
80 in base 10.

datagram [24] = 80;

// Flags

datagram [25] = 2; // Bit 2 set - SYN flag only

// Window

datagram [26] = 0;
datagram [27] = 255;

// Checksum - initialised to 0

datagram [28] = 0;
datagram [29] = 0;

// Urgent field

datagram [30] = 0;
datagram [31] = 0;

byte[] receivedPacket = new byte[256];
Console.WriteLine ("Sending packet...");
ipSocket.SendTo (datagram, remoteEndPoint);
Console.WriteLine ("Packet sent");
ipSocket.Receive (receivedPacket);
Console.WriteLine ("Received packet");
for (int i = 0; i < 256; i++)
{
Console.WriteLine (i + " " + receivedPacket[i]);
}
}
}

Jan 27 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.