SharpPcap を使用して TCP SYN スキャンを実行しようとしています。WireShark を使用して、パケットが NIC から送信されるかどうかを確認していますが、送信されますが、応答がありません。
NMAP
ドキュメンテーションには、SYN|ACK または RST TCP パケットを取得する必要があると書かれていますが、何も取得せず、他のステーションが起動しており、それらの間で telnet を実行すると、wireshark で tcp ハンドシェイクが正常に表示されます。
コードは次のとおりです。
//Generate a random packet
EthernetPacket packet = EthernetPacket.RandomPacket();
packet.SourceHwAddress = srcMacAddress;
packet.DestinationHwAddress = dstMacAddress;
string ss = "Message TCP";
byte[] bArray = System.Text.Encoding.ASCII.GetBytes(ss);
byte[] asd = new byte[60];
bArray.CopyTo(asd, 20);
ByteArraySegment bas = new ByteArraySegment(asd);
TcpPacket tcpPacket = new TcpPacket(bas);
IpPacket ipPacket = IpPacket.RandomPacket(IpVersion.IPv4);
ipPacket.TimeToLive = 20;
ipPacket.Protocol = IPProtocolType.TCP;
ipPacket.Version = IpVersion.IPv4;
ipPacket.DestinationAddress = dstIpAddress;
ipPacket.SourceAddress = srcIpAddress;
ipPacket.PayloadPacket = tcpPacket;
packet.PayloadPacket = ipPacket;
ipPacket.ParentPacket = packet;
tcpPacket.ParentPacket = ipPacket;
tcpPacket.SourcePort = SourcePortNumber;
tcpPacket.DestinationPort = DestinationPortNumber;
tcpPacket.Syn = true;
tcpPacket.WindowSize = 500;
tcpPacket.AcknowledgmentNumber = 1000;
tcpPacket.SequenceNumber = 1000;
tcpPacket.DataOffset = TcpFields.HeaderLength + 1;
try
{
//Send the packet out the network device
device.SendPacket(packet);
ScanManager.Instance.AddPacket(packet, IPProtocolType.TCP);
}
catch (Exception e)
{
Console.WriteLine("-- " + e.Message);
}
呼び出しコード:
public static bool InitiliazeData(string dstIp)
{
bool bResult = false;
// Print SharpPcap version
string ver = SharpPcap.Version.VersionString;
Console.WriteLine("SharpPcap Port scanner\n", ver);
// Retrieve the device list
var devices = CaptureDeviceList.Instance;
// If no devices were found print an error
if (devices.Count < 1)
{
Console.WriteLine("No devices were found on this machine");
return false;
}
Console.WriteLine("The following devices are available on this machine:");
Console.WriteLine("----------------------------------------------------");
Console.WriteLine();
int i = 0;
foreach (WinPcapDevice dev in devices)
{
Console.Out.WriteLine("{0}) {1}", i, dev.Description);
i++;
foreach (PcapAddress addr in dev.Addresses)
{
if (addr.Addr != null && addr.Addr.ipAddress != null)
{
Console.Out.Write("IP: {0}", addr.Addr.ipAddress);
}
}
}
Console.WriteLine();
Console.Write("-- Please choose a device to send a packet on: ");
i = int.Parse(Console.ReadLine());
iDeviceNumber = i;
device = (PcapDevice)devices[i];
i = 0;
Console.WriteLine("Ip addresses for the selected interface: ");
foreach (PcapAddress addr in ((WinPcapDevice)device).Addresses)
{
if (addr.Addr != null && addr.Addr.ipAddress != null)
{
Console.Out.Write("{0}) {1}", i, addr.Addr.ipAddress);
i++;
}
}
Console.WriteLine();
Console.Write("-- Please choose a source ip to send a packet from: ");
i = int.Parse(Console.ReadLine());
iIpNumber = i;
int j = 0;
foreach (PcapAddress addr in ((WinPcapDevice)device).Addresses)
{
if (addr.Addr != null && addr.Addr.ipAddress != null)
{
if (j == i)
{
srcIpAddress = addr.Addr.ipAddress;
break;
}
j++;
}
}
//do
//{
// Console.WriteLine("Please enter the IP that you want to scan: ");
// String ipAddressString = Console.ReadLine();
// dstIpAddress = IPAddress.Parse(ipAddressString);
// if (dstIpAddress != null)
// break;
// else
// Console.Write("Incorrect ip address.");
//} while (true);
IPAddress.TryParse(dstIp, out dstIpAddress);
ARP arpResolver = new ARP(((WinPcapDevice)device));
dstMacAddress = arpResolver.Resolve(dstIpAddress);
if (dstMacAddress == null)
{
Console.WriteLine("Could not get MAC Address for ip {0}", dstIpAddress);
return false;
}
else
{
Console.WriteLine("Destination MAC Address: {0}.", dstMacAddress);
}
device.Open();
srcMacAddress = device.MacAddress;
device.Close();
return bResult;
}