From 7c7f9f57f9457d44a2fc5c171da62f3ffa010f4f Mon Sep 17 00:00:00 2001 From: Captain ALM Date: Sat, 11 Jan 2020 15:52:14 +0000 Subject: [PATCH] Add whitelist support. --- .../OCDaemonHoster/OCDaemonHoster.sln | 18 +++++ .../OCDaemonHoster/OCNetworkClient.cs | 2 +- .../OCDaemonHoster/OCNetworkListener.cs | 65 +++++++++++++++---- .../OCDaemonHoster/Program.cs | 10 ++- 4 files changed, 79 insertions(+), 16 deletions(-) create mode 100644 OCUploadDownloadServer/OCDaemonHoster/OCDaemonHoster.sln diff --git a/OCUploadDownloadServer/OCDaemonHoster/OCDaemonHoster.sln b/OCUploadDownloadServer/OCDaemonHoster/OCDaemonHoster.sln new file mode 100644 index 0000000..68bb712 --- /dev/null +++ b/OCUploadDownloadServer/OCDaemonHoster/OCDaemonHoster.sln @@ -0,0 +1,18 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +# SharpDevelop 4.4 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OCDaemonHoster", "OCDaemonHoster.csproj", "{08F6D48F-9EAB-4861-9D50-F9F1BC10C074}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {08F6D48F-9EAB-4861-9D50-F9F1BC10C074}.Debug|Any CPU.Build.0 = Debug|Any CPU + {08F6D48F-9EAB-4861-9D50-F9F1BC10C074}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {08F6D48F-9EAB-4861-9D50-F9F1BC10C074}.Release|Any CPU.Build.0 = Release|Any CPU + {08F6D48F-9EAB-4861-9D50-F9F1BC10C074}.Release|Any CPU.ActiveCfg = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/OCUploadDownloadServer/OCDaemonHoster/OCNetworkClient.cs b/OCUploadDownloadServer/OCDaemonHoster/OCNetworkClient.cs index ab00246..89be527 100644 --- a/OCUploadDownloadServer/OCDaemonHoster/OCNetworkClient.cs +++ b/OCUploadDownloadServer/OCDaemonHoster/OCNetworkClient.cs @@ -13,7 +13,7 @@ using System.Threading; namespace captainalm.network.oc { - public class OCNetworkClient { + public sealed class OCNetworkClient { private Socket sock; private IPEndPoint remoteAddress; private IPEndPoint localAddress; diff --git a/OCUploadDownloadServer/OCDaemonHoster/OCNetworkListener.cs b/OCUploadDownloadServer/OCDaemonHoster/OCNetworkListener.cs index ba876c6..ba89424 100644 --- a/OCUploadDownloadServer/OCDaemonHoster/OCNetworkListener.cs +++ b/OCUploadDownloadServer/OCDaemonHoster/OCNetworkListener.cs @@ -7,6 +7,7 @@ * To change this template use Tools | Options | Coding | Edit Standard Headers. */ using System; +using System.Collections.Generic; using System.Net; using System.Net.NetworkInformation; using System.Net.Sockets; @@ -14,7 +15,7 @@ using System.Threading; namespace captainalm.network.oc { - public class OCNetworkListener { + public sealed class OCNetworkListener { private Socket sSock; private Thread lThread; private Boolean listening; @@ -23,6 +24,7 @@ namespace captainalm.network.oc private Boolean cExists; private Object slockcl = new Object(); private IPEndPoint listeningAddress; + private List whitelist; public OCNetworkListener(IPEndPoint addressIn) { lThread = new Thread(new ThreadStart(this.run)); @@ -55,6 +57,11 @@ namespace captainalm.network.oc if (listening) { lThread.Start(); } + whitelist = new List(); + } + + public OCNetworkListener(IPEndPoint addressIn, List whitelistIn) : this(addressIn) { + whitelist.AddRange(whitelistIn); } public OCNetworkClient getAcceptedClient() { @@ -79,6 +86,10 @@ namespace captainalm.network.oc } } } + + public List getWhiteList() { + return whitelist; + } public IPEndPoint getListeningAddress() { return listeningAddress; @@ -117,7 +128,7 @@ namespace captainalm.network.oc sSock = null; } - protected void run() { + private void run() { while (listening) { while (cExists) { try { @@ -128,22 +139,50 @@ namespace captainalm.network.oc } try { Socket sa = sSock.Accept(); - sa.ReceiveBufferSize = Int16.MaxValue; - sa.SendBufferSize = Int16.MaxValue; - sa.ReceiveTimeout = 5000; - sa.SendTimeout = 5000; - acceptedClient = new OCNetworkClient(sa); - cWaiting = true; - while (cWaiting) { - try { - Thread.Sleep(100); - } catch (ThreadInterruptedException e) { - break; + if (shouldAccept(sa)) { + sa.ReceiveBufferSize = Int16.MaxValue; + sa.SendBufferSize = Int16.MaxValue; + sa.ReceiveTimeout = 5000; + sa.SendTimeout = 5000; + acceptedClient = new OCNetworkClient(sa); + cWaiting = true; + while (cWaiting) { + try { + Thread.Sleep(100); + } catch (ThreadInterruptedException e) { + break; + } } + } else { + try { + sa.Shutdown(SocketShutdown.Both); + } catch (SocketException e) { + } + try { + sa.Close(); + } catch (SocketException e) { + } + sa = null; } } catch (SocketException e) { } } } + + private bool shouldAccept(Socket si) { + if (whitelist.Count > 0) { + String addr = ((IPEndPoint) si.RemoteEndPoint).Address.ToString(); + bool toret = false; + for (int i = 0; i < whitelist.Count; i++) { + if (whitelist[i].Equals(addr)) { + toret = true; + break; + } + } + return toret; + } else { + return true; + } + } } } diff --git a/OCUploadDownloadServer/OCDaemonHoster/Program.cs b/OCUploadDownloadServer/OCDaemonHoster/Program.cs index c95397e..91ef282 100644 --- a/OCUploadDownloadServer/OCDaemonHoster/Program.cs +++ b/OCUploadDownloadServer/OCDaemonHoster/Program.cs @@ -69,7 +69,12 @@ namespace OCDaemonHoster } catch (IOException e) { } } - OCNetworkListener server = new OCNetworkListener(address); + List wl = new List(); + if (settings.ContainsKey("whitelist")) { + + wl.AddRange(settings["whitelist"].Split(",".ToCharArray())); + } + OCNetworkListener server = new OCNetworkListener(address, wl); writeLine("[INFO] : Listener Started!"); writeLine("[INFO] : Listener 'Address:Port' : " + server.getListeningAddress().Address.ToString() + ":" + server.getListeningAddress().Port); @@ -286,9 +291,10 @@ namespace OCDaemonHoster writeLine(""); writeLine("Usage:"); writeLine( - "OCDH.exe [-mode=] [-target=] [-cache] [-enumeration] [-creation] [-deletion]"); + "OCDH.exe [-mode=] [-whitelist=] [-target=] [-cache] [-enumeration] [-creation] [-deletion]"); writeLine(""); writeLine("-mode= : allows to select a Hosting Mode."); + writeLine("-whitelist= : allows IP Address to connect, if there is no whitelist switch then any IP Address can connect."); writeLine("-target= : allows to select a file for hosting (File Host Mode Only)."); writeLine("-cache : caches the target file once (File Host Mode Only)."); writeLine("-enumeration : allows for file/directory enumeration (File Access Mode Only).");