-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
238 lines (202 loc) · 10.3 KB
/
Copy pathProgram.cs
File metadata and controls
238 lines (202 loc) · 10.3 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Threading;
using DhcpClientNET.Dhcp;
namespace DhcpClientNET
{
class Program
{
static void Main(string[] args)
{
var interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (var item in interfaces.Select((i, idx) => new { nr = idx + 1, inter = i }))
{
Console.WriteLine($"-{item.nr}- {item.inter.Name}");
}
Console.Write("> Choose a interface: ");
int selection = int.Parse(Console.ReadLine());
var mainInterface = interfaces[selection - 1];
var random = new Random();
ushort packetId = (ushort) random.Next();
uint dhcpTransactionId = (uint) random.Next();
ITransportPacketFactory transportPacketFactory = new TransportPacketFactoryImpl();
IApplicationPacketFactory applicationPacketFactory = new ApplicationPacketFactoryImpl();
var dhcpDiscover = BuildDhcpDiscoverMessage(mainInterface, packetId, dhcpTransactionId);
bool running = true;
bool rcvAck = false;
ManualResetEvent rcvAckEvent = new ManualResetEvent(false);
DhcpOffer selectedOffer = null;
var socket = SetupPromiscousSocket(mainInterface);
Console.WriteLine("Send DHCP Discover...");
dhcpDiscover.Send(socket);
ISet<DhcpOffer> offers = new HashSet<DhcpOffer>();
var recieverThread = new Thread(() =>
{
while (running)
{
IPv4Packet rcvPacket = IPv4Packet.Recieve(socket, transportPacketFactory, applicationPacketFactory);
if (rcvPacket != null && rcvPacket.Payload is UdpPacket udpPacket)
{
if (udpPacket.Payload is DhcpPacket responseDhcpPacket)
{
switch (responseDhcpPacket.MessageType)
{
case DhcpMessageType.Offer:
DhcpOffer offer = DhcpOffer.FromDhcpPacket(responseDhcpPacket);
offers.Remove(offer); // remove old offer from server if necassary
offers.Add(offer);
Console.WriteLine($"New Offer from {rcvPacket.Source}");
break;
case DhcpMessageType.Ack:
if (!rcvAck)
{
Console.WriteLine("OK from server - ready to launch!");
int exitCodeInter = ExecuteNetshCommand(
$"interface ipv4 set address name=\"{mainInterface.Name}\" static" +
$" {selectedOffer.ClientAddress} {selectedOffer.SubnetMask} {selectedOffer.Gateway} 1"
);
Console.WriteLine($"netsh Interface exit code - {exitCodeInter}");
int exitCodeDns = ExecuteNetshCommand(
$"interface ipv4 set dnsservers \"{mainInterface.Name}\" static {selectedOffer.DnsServer} primary no"
);
Console.WriteLine($"netsh DNS exit code - {exitCodeDns}");
rcvAckEvent.Set();
}
rcvAck = true;
break;
}
}
}
}
});
recieverThread.Start();
Thread.Sleep(500);
if (offers.Count == 0)
{
Console.WriteLine("Resending DHCP Discover...");
dhcpDiscover.Send(socket);
}
Console.WriteLine("> Press any key to stop adding new offers\n");
Console.ReadKey();
Console.WriteLine();
Console.WriteLine("-- Select DHCP server offer --");
DhcpOffer[] finalOffers = offers.ToArray();
foreach (var item in finalOffers.Select((o, idx) => new { nr = idx + 1, offer = o }))
{
Console.WriteLine($"-{item.nr}- Offer from {item.offer.DhcpServer}");
Console.WriteLine($" Client address: {item.offer.ClientAddress}");
Console.WriteLine($" Gateway address: {item.offer.Gateway}");
Console.WriteLine($" DNS server: {item.offer.DnsServer}");
Console.WriteLine($" Domain: {item.offer.Domain}");
}
Console.Write("> Choose a offer: ");
selection = int.Parse(Console.ReadLine());
selectedOffer = finalOffers[selection - 1];
Console.WriteLine("Send DHCP Request");
IPv4Packet dhcpRequest = BuildDhcpRequestMessage(selectedOffer, mainInterface, ++packetId, dhcpTransactionId);
dhcpRequest.Send(socket);
Console.WriteLine("Waiting for DHCP Ack and netsh...");
rcvAckEvent.WaitOne();
running = false;
socket.Close();
Console.WriteLine("> Press any key to close");
Console.ReadKey();
}
private static int ExecuteNetshCommand(string command)
{
Process proc = Process.Start(new ProcessStartInfo()
{
FileName = "netsh",
Arguments = command,
UseShellExecute = false
});
proc.WaitForExit();
return proc.ExitCode;
}
private static IPv4Packet BuildDhcpDiscoverMessage(NetworkInterface inter, ushort packetId, uint dhcpTransactionId)
{
var hardwareAddress = new HardwareAddress(HardwareAddressType.Ethernet,
inter.GetPhysicalAddress().GetAddressBytes());
var dhcpPacket = new DhcpPacket();
dhcpPacket.Operation = DhcpOperation.Request;
dhcpPacket.MessageType = DhcpMessageType.Discover;
dhcpPacket.TransactionId = dhcpTransactionId;
dhcpPacket.HardwareAddress = hardwareAddress;
var dhcpUdpPacket = new UdpPacket();
dhcpUdpPacket.SourcePort = 68;
dhcpUdpPacket.DestinationPort = 67;
dhcpUdpPacket.Payload = dhcpPacket;
var dhcpIpPacket = new IPv4Packet();
dhcpIpPacket.Source = IPAddress.Any;
dhcpIpPacket.Destination = IPAddress.Broadcast;
dhcpIpPacket.Identifiation = packetId;
dhcpIpPacket.Payload = dhcpUdpPacket;
return dhcpIpPacket;
}
private static IPv4Packet BuildDhcpRequestMessage(DhcpOffer offer, NetworkInterface inter, ushort packetId, uint dhcpTransactionId)
{
var hardwareAddress = new HardwareAddress(HardwareAddressType.Ethernet,
inter.GetPhysicalAddress().GetAddressBytes());
var dhcpPacket = new DhcpPacket();
dhcpPacket.Operation = DhcpOperation.Request;
dhcpPacket.MessageType = DhcpMessageType.Request;
dhcpPacket.TransactionId = dhcpTransactionId;
dhcpPacket.HardwareAddress = hardwareAddress;
dhcpPacket.Options.Add(new DhcpServerIdentifierOption(offer.DhcpServer));
dhcpPacket.Options.Add(new DhcpClientIdentifier(hardwareAddress));
dhcpPacket.Options.Add(new DhcpRequestedIpAddressOption(offer.ClientAddress));
var dhcpUdpPacket = new UdpPacket();
dhcpUdpPacket.SourcePort = 68;
dhcpUdpPacket.DestinationPort = 67;
dhcpUdpPacket.Payload = dhcpPacket;
var dhcpIpPacket = new IPv4Packet();
dhcpIpPacket.Source = IPAddress.Any;
dhcpIpPacket.Destination = IPAddress.Broadcast;
dhcpIpPacket.Identifiation = packetId;
dhcpIpPacket.Payload = dhcpUdpPacket;
return dhcpIpPacket;
}
private static IPv4Packet BuildDhcpRebindMessage(DhcpOffer offer, NetworkInterface inter, ushort packetId, uint dhcpTransactionId)
{
var hardwareAddress = new HardwareAddress(HardwareAddressType.Ethernet,
inter.GetPhysicalAddress().GetAddressBytes());
var dhcpPacket = new DhcpPacket();
dhcpPacket.Operation = DhcpOperation.Request;
dhcpPacket.MessageType = DhcpMessageType.Request;
dhcpPacket.TransactionId = dhcpTransactionId;
dhcpPacket.HardwareAddress = hardwareAddress;
dhcpPacket.ClientAddress = offer.ClientAddress;
dhcpPacket.Options.Add(new DhcpClientIdentifier(hardwareAddress));
var dhcpUdpPacket = new UdpPacket();
dhcpUdpPacket.SourcePort = 68;
dhcpUdpPacket.DestinationPort = 67;
dhcpUdpPacket.Payload = dhcpPacket;
var dhcpIpPacket = new IPv4Packet();
dhcpIpPacket.Source = offer.ClientAddress;
dhcpIpPacket.Destination = offer.DhcpServer;
dhcpIpPacket.Identifiation = packetId;
dhcpIpPacket.Payload = dhcpUdpPacket;
return dhcpIpPacket;
}
private static Socket SetupPromiscousSocket(NetworkInterface inter)
{
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Udp);
IPAddress listeningAddress = inter.GetIPProperties().UnicastAddresses
.Where(info => info.Address.AddressFamily == AddressFamily.InterNetwork)
.Select(info => info.Address)
.First();
Console.WriteLine($" Binding to address {listeningAddress}");
socket.Bind(new IPEndPoint(listeningAddress, 0));
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, 1);
byte[] byOn = BitConverter.GetBytes(1);
socket.IOControl(IOControlCode.ReceiveAll, byOn, BitConverter.GetBytes(0));
socket.EnableBroadcast = true;
return socket;
}
}
}