-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 564142d
Showing
3 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
*.swp | ||
*.*~ | ||
project.lock.json | ||
.DS_Store | ||
*.pyc | ||
|
||
# Visual Studio Code | ||
.vscode | ||
|
||
# User-specific files | ||
*.suo | ||
*.user | ||
*.userosscache | ||
*.sln.docstates | ||
|
||
# Build results | ||
[Dd]ebug/ | ||
[Dd]ebugPublic/ | ||
[Rr]elease/ | ||
[Rr]eleases/ | ||
x64/ | ||
x86/ | ||
build/ | ||
bld/ | ||
[Bb]in/ | ||
[Oo]bj/ | ||
msbuild.log | ||
msbuild.err | ||
msbuild.wrn | ||
|
||
# Visual Studio 2015 | ||
.vs/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using System; | ||
using System.CommandLine; | ||
using System.CommandLine.Invocation; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.NetworkInformation; | ||
using DotNetProjects.DhcpServer; | ||
|
||
namespace dhcpshot | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
var rootCommand = new RootCommand | ||
{ | ||
new Option( | ||
new string[] { "--interface-ip", "-i" }, | ||
"IPv4 for the interface we are going to listen to") | ||
{ | ||
Argument = new Argument<string>{ Arity = ArgumentArity.ExactlyOne }, | ||
Required = true | ||
}, | ||
new Option( | ||
new string[] { "--target-ip", "-t" }, | ||
"IPv4 that will be given to the DHCP client, if none given it will be generated from interface IP") | ||
{ | ||
Argument = new Argument<string>() | ||
} | ||
}; | ||
rootCommand.Description = "DHCPshot - Assign IP address to first client asking on the specified interface"; | ||
rootCommand.Handler = CommandHandler.Create<string, string>(StartServer); | ||
rootCommand.InvokeAsync(args).Wait(); | ||
} | ||
|
||
public static void StartServer(string interfaceIp, string targetIp) | ||
{ | ||
Console.WriteLine($"Looking for interface for IP {interfaceIp}"); | ||
var intf = NetworkInterface.GetAllNetworkInterfaces() | ||
.Where( | ||
x => x.GetIPProperties().UnicastAddresses | ||
.Any(y => y.Address.Equals(IPAddress.Parse(interfaceIp))) | ||
).FirstOrDefault(); | ||
if (intf == null) | ||
{ | ||
Console.WriteLine($"Interface for IP {interfaceIp} not found!"); | ||
return; | ||
} | ||
Console.WriteLine($"Starting DHCP server on interface {intf.Name}"); | ||
var server = new DHCPServer(); | ||
|
||
if (targetIp == null) | ||
{ | ||
var rnd = new Random(); | ||
var ip_bytes = interfaceIp.Split("."); | ||
var lastByte = int.Parse(ip_bytes[3]); | ||
int newByte = rnd.Next(2, 254); | ||
while (newByte == lastByte) | ||
{ | ||
newByte = rnd.Next(2, 254); | ||
} | ||
ip_bytes[3] = newByte.ToString(); | ||
targetIp = string.Join(".", ip_bytes); | ||
} | ||
Console.WriteLine($"Client will receive IP {targetIp}"); | ||
server.OnDataReceived += delegate (DHCPRequest dhcpRequest) | ||
{ | ||
Request(dhcpRequest, IPAddress.Parse(targetIp)); | ||
}; | ||
server.BroadcastAddress = IPAddress.Broadcast; | ||
server.SendDhcpAnswerNetworkInterface = intf; | ||
server.Start(); | ||
Console.WriteLine("Running DHCP server... Press CTRL-C to exit"); | ||
} | ||
|
||
static void Request(DHCPRequest dhcpRequest, IPAddress targetIp) | ||
{ | ||
try | ||
{ | ||
var type = dhcpRequest.GetMsgType(); | ||
var replyOptions = new DHCPReplyOptions(); | ||
replyOptions.SubnetMask = IPAddress.Parse("255.255.255.0"); | ||
|
||
if (type == DHCPMsgType.DHCPDISCOVER) | ||
dhcpRequest.SendDHCPReply(DHCPMsgType.DHCPOFFER, targetIp, replyOptions); | ||
if (type == DHCPMsgType.DHCPREQUEST) | ||
dhcpRequest.SendDHCPReply(DHCPMsgType.DHCPACK, targetIp, replyOptions); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine(ex); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="DotNetProjects.DhcpServer" Version="1.0.15" /> | ||
<PackageReference Include="System.CommandLine.Experimental" Version="0.3.0-alpha.19573.2" /> | ||
</ItemGroup> | ||
|
||
</Project> |