-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSendPacketForm.cs
147 lines (118 loc) · 5.05 KB
/
SendPacketForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//NCShark - By AlSch092 @ Github, thanks to @Diamondo25 for MapleShark
using System;
using PacketDotNet;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.Net;
using SharpPcap.LibPcap;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
namespace NCShark
{
public partial class SendPacketForm : Form
{
private LibPcapLiveDevice mDevice = null;
public string sourceIp = null;
public string destIp = null;
public int sourcePort = 0;
public int destPort = 0;
public string sourceMAC = null;
public string destinationMac = null;
public ushort lastIdentificationNum = 0;
public uint lastSequenceNumber = 0;
public uint lastAcknowledgmentNumber = 0;
public uint lastPacketSize = 0;
public bool isFirstSend = false;
public SendPacketForm()
{
InitializeComponent();
}
public void SetDevice(LibPcapLiveDevice device, string filter)
{
mDevice = device;
mDevice.Filter = filter;
}
static byte[] ConvertAsciiStringToBytes(string asciiString)
{
string[] asciiBytes = asciiString.Split(' ');
byte[] byteArray = new byte[asciiBytes.Length];
for (int i = 0; i < asciiBytes.Length; i++)
{
byteArray[i] = byte.Parse(asciiBytes[i], System.Globalization.NumberStyles.HexNumber);
}
return byteArray;
}
void sendPshAck() //packet contains TCP data -> Wrap Ethernet into TCP packet
{
if (mDevice == null)
{
MessageBox.Show("[ERROR] Device was NULL!");
return;
}
mDevice.Open();
Console.WriteLine("Sending to: {0}:{1} from {2}:{3} Device: {4}, LastId: {5}", destIp, destPort, sourceIp, sourcePort, mDevice.Addresses[0], lastIdentificationNum);
byte[] data_payload = null;
string textBytesPayload = textBox_Send.Text;
if(textBytesPayload.Length > 2) //convert ascii byte string to actual hex
{
data_payload = ConvertAsciiStringToBytes(textBytesPayload);
}
else
{
MessageBox.Show("Error parsing input packet, check spaces");
return;
}
//Cipher.XorBytes(Cipher.xor_out, data_payload, data_payload.Length, isFirstSend); //wont work, need to use ebcryption table from game itself!
isFirstSend = false;
//Create a new Ethernet packet wrapped in the TCP packet
var ethPacket = new EthernetPacket(
PhysicalAddress.Parse("00-00-00-00-00-00"), // Source MAC address, replace with your own or write a function to grab this
PhysicalAddress.Parse("00-00-00-00-00-01"), // Destination MAC address, typically your gateway device in home setups
EthernetPacketType.IpV4 // Ethernet type (IPv4)
);
// Create a new IPv4 packet
var ipPacket = new IPv4Packet(
IPAddress.Parse(sourceIp), // Source IP address
IPAddress.Parse(destIp) // Destination IP address (same as source for loopback)
);
ipPacket.Version = (IpVersion)4;
ipPacket.TimeToLive = 128;
// Set the "Don't Fragment" (DF) bit
ipPacket.FragmentFlags = (ushort)(1 << 1); // Shift 1 bit to the left to set the DF bit
ipPacket.FragmentOffset = 0;
ipPacket.Id = (ushort)(lastIdentificationNum + 1);
// Create a new TCP packet
var tcpPacket = new TcpPacket((ushort)sourcePort, (ushort)destPort)
{
Psh = true,
Ack = true,
WindowSize = 515, // TCP window size, todo: write function to auto-set this
Checksum = 0x69e0, //todo: function to auto-set checksum
SequenceNumber = lastSequenceNumber,
AcknowledgmentNumber = lastAcknowledgmentNumber,
};
//tcpPacket.SequenceNumber =
// Set the payload for the TCP packet
tcpPacket.PayloadData = data_payload;
// Add the TCP packet to the IPv4 packet
ipPacket.PayloadPacket = tcpPacket;
ethPacket.PayloadPacket = ipPacket;
lock (mDevice)
{
mDevice.SendPacket(ethPacket);
}
}
private void button_SendPacket_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(sendPshAck));
t.Start();
}
private void SendPacketForm_Load(object sender, EventArgs e)
{
}
}
}