我正在尝试使用SharpPcap进行TCP SYN扫描,我正在使用WireShark tosee如果数据包从我的NIC发送,它们会被发送,但我没有得到响应。
NMAP
文档说我应该得到一个SYN | ACK或一个RST TCP数据包,我得不到,另一个站点启动,如果我在它们之间进行telnet,则tcp握手在wireshark中显示正常。
代码如下:
//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;
}
答案 0 :(得分:0)
也许答案有点晚了 这是我的代码
byte[] MAC = new byte[] { 0xf8, 0x7b, 0x20, 0x6f, 0x50, 0x50 };
var etn = new EthernetPacket(device.MacAddress, new PhysicalAddress(MAC), EthernetPacketType.IPv4);
TcpPacket tcpPacket = new TcpPacket(6145,80);
IPv4Packet ipPacket = new IPv4Packet(new IPAddress(new byte[] { 192, 168, 116, 162 }), new IPAddress(new byte[] { 101, 201, 181, 92 }))
{
TimeToLive = 64,
Protocol = IPProtocolType.TCP,
Version = IPVersion.IPv4,
DestinationAddress = new IPAddress(new byte[] { 101, 201, 181, 92 }),
SourceAddress = new IPAddress(new byte[] { 192, 168, 116, 162 }),
PayloadPacket = tcpPacket,
HopLimit = 64,
Id = 15,
};
etn.PayloadPacket = ipPacket;
ipPacket.ParentPacket = etn;
tcpPacket.ParentPacket = ipPacket;
tcpPacket.SourcePort = 1545;
tcpPacket.DestinationPort = 80;
tcpPacket.Syn = true;
tcpPacket.WindowSize = 8140;
tcpPacket.AcknowledgmentNumber = 0;
tcpPacket.SequenceNumber = 1541;
tcpPacket.UpdateTCPChecksum();
ipPacket.UpdateIPChecksum();