CVE-2020-8815 (published 2020-02-12T15:15Z)

Improper connection handling in the base connection handler in IKTeam BearFTP before v0.3.1 allows a remote attacker to achieve denial of service via a Slowloris approach by sending a large volume of small packets.

Severity: MEDIUM
CVSS v2 base score: 5.0 (AV:N/AC:L/Au:N/C:N/I:N/A:P)
CVSS v3 base score: 7.5, HIGH (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H)

Weakness information:
cwe_id: CWE-20
cwe_name: Improper Input Validation
description: The product receives input or data, but it does
        not validate or incorrectly validates that the input has the
        properties that are required to process the data safely and
        correctly.
extended_description: 
	   Input validation is a frequently-used technique
	   for checking potentially dangerous inputs in order to
	   ensure that the inputs are safe for processing within the
	   code, or when communicating with other components.  When
	   software does not validate input properly, an attacker is
	   able to craft the input in a form that is not expected by
	   the rest of the application. This will lead to parts of the
	   system receiving unintended input, which may result in
	   altered control flow, arbitrary control of a resource, or
	   arbitrary code execution.
	   Input validation is not the only technique for
	   processing input, however.  Other techniques attempt to
	   transform potentially-dangerous input into something safe, such
	   as filtering (CWE-790) - which attempts to remove dangerous
	   inputs - or encoding/escaping (CWE-116), which attempts to
	   ensure that the input is not misinterpreted when it is included
	   in output to another component. Other techniques exist as well
	   (see CWE-138 for more examples.)
	   Input validation can be applied to:
	   
	     raw data - strings, numbers, parameters, file contents, etc.
	     metadata - information about the raw data, such as headers or size
	   
	   Data can be simple or structured.  Structured data
	   can be composed of many nested layers, composed of
	   combinations of metadata and raw data, with other simple or
	   structured data.
	   Many properties of raw data or metadata may need
	   to be validated upon entry into the code, such
	   as:
	   
	     specified quantities such as size, length, frequency, price, rate, number of operations, time, etc.
	     implied or derived quantities, such as the actual size of a file instead of a specified size
	     indexes, offsets, or positions into more complex data structures
	     symbolic keys or other elements into hash tables, associative arrays, etc.
	     well-formedness, i.e. syntactic correctness - compliance with expected syntax 
	     lexical token correctness - compliance with rules for what is treated as a token
	     specified or derived type - the actual type of the input (or what the input appears to be)
	     consistency - between individual data elements, between raw data and metadata, between references, etc.
	     conformance to domain-specific rules, e.g. business logic 
	     equivalence - ensuring that equivalent inputs are treated the same
	     authenticity, ownership, or other attestations about the input, e.g. a cryptographic signature to prove the source of the data
	   
	   Implied or derived properties of data must often
	   be calculated or inferred by the code itself.  Errors in
	   deriving properties may be considered a contributing factor
	   to improper input validation.
	   
	   Note that "input validation" has very different
	   meanings to different people, or within different
	   classification schemes.  Caution must be used when
	   referencing this CWE entry or mapping to it.  For example,
	   some weaknesses might involve inadvertently giving control
	   to an attacker over an input when they should not be able
	   to provide an input at all, but sometimes this is referred
	   to as input validation.
	   Finally, it is important to emphasize that the
	   distinctions between input validation and output escaping
	   are often blurred, and developers must be careful to
	   understand the difference, including how input validation
	   is not always sufficient to prevent vulnerabilities,
	   especially when less stringent data types must be
	   supported, such as free-form text. Consider a SQL injection
	   scenario in which a person's last name is inserted into a
	   query. The name "O'Reilly" would likely pass the validation
	   step since it is a common last name in the English
	   language. However, this valid name cannot be directly
	   inserted into the database because it contains the "'"
	   apostrophe character, which would need to be escaped or
	   otherwise transformed. In this case, removing the
	   apostrophe might reduce the risk of SQL injection, but it
	   would produce incorrect behavior because the wrong name
	   would be recorded.
       
         

Fix information:
repo_url: https://github.com/kolya5544/BearFTP
commit_details: repo_url: https://github.com/kolya5544/BearFTP
author: kolya5544
author_date: 2020-02-10 16:04:06+03:00
msg: Implemented maxthreads, added another entry to TODO
repository: repo_url: https://github.com/kolya5544/BearFTP
repo_name: kolya5544/BearFTP
description: Honeypot FTP server written in .NET Core (C#) for both Linux and Windows.
repo_language: C#
owner: kolya5544
file_changes: filename: Config.cs
change_type: ModificationType.MODIFY
diff: @@ -24,6 +24,7 @@ namespace BearFTP
         public int BanLength = 3600;
         public int MaxErrors = 6;
         public int BufferSize = 8192;
+        public int MaxThreads = 50;
 
         
 
@@ -61,6 +62,7 @@ namespace BearFTP
                 MaxErrors = json.MaxErrors;
                 BufferSize = json.BufferSize;
                 PerIPLogs = json.PerIPLogs;
+                MaxThreads = json.MaxThreads;
                 
 
 
@@ -101,6 +103,7 @@ namespace BearFTP
         public int BanLength { get; set; }
         public int MaxErrors { get; set; }
         public int BufferSize { get; set; }
+        public int MaxThreads { get; set; }
         public List<CJSON_FILE> Files { get; set; }
     }
 }

programming_language: C#
filename: Program.cs
change_type: ModificationType.MODIFY
diff: @@ -34,10 +34,14 @@ namespace BearFTP
         public static int BanLength = 3600;
         public static int MaxErrors = 6;
         public static int BufferSize = 8192;
+        public static int MaxThreads = 50;
 
         //IP TempBan list (hostname:seconds)
         public static List<Ban> bans = new List<Ban>();
 
+        //Count of dynamic threads currently used by a server
+        public static int thread_amount = 0;
+
         //An instance of config to extract values
         public static Config config;
 
@@ -51,7 +55,7 @@ namespace BearFTP
         public static Directory root = new Directory();
 
         //Current version
-        public static string _VERSION = "v0.3.1 BETA";
+        public static string _VERSION = "v0.4.0 BETA";
 
         //Default log.
         public static StreamWriter logfile = new StreamWriter("log.txt", true);
@@ -382,10 +386,11 @@ namespace BearFTP
                             if (!perips.Any(logs => ((FileStream)(logs.BaseStream)).Name.Contains(hostname)))
                             {
                                 perip = new StreamWriter("iplogs/" + hostname + ".txt", true);
-                            perip.AutoFlush = true;
-                            
+                                perip.AutoFlush = true;
+
                                 perips.Add(perip);
-                            } else
+                            }
+                            else
                             {
                                 foreach (StreamWriter ip in perips)
                                 {
@@ -445,7 +450,7 @@ namespace BearFTP
                     else
                     {
                         per_second.Add(new Active(hostname, 1));
-                       
+
                     }
                     try
                     {
@@ -459,409 +464,421 @@ namespace BearFTP
 
                     }
 
-                    new Thread(new ThreadStart(() =>
+                    if (thread_amount <= MaxThreads)
                     {
-                        Thread.CurrentThread.IsBackground = true;
+                        thread_amount++;
+                        new Thread(new ThreadStart(() =>
+                        {
+                            Thread.CurrentThread.IsBackground = true;
 
-                        bool triggered = false;
-                        bool trigger2 = false;
-                        Client c = new Client("null", "null", "null");
-                        string username = "";
-                        string password = "";
-                        string directory = "/";
-                        bool Authed = false;
-                        bool passive = false;
-                        int error = MaxErrors;
-                        
+                            bool triggered = false;
+                            bool trigger2 = false;
+                            Client c = new Client("null", "null", "null");
+                            string username = "";
+                            string password = "";
+                            string directory = "/";
+                            bool Authed = false;
+                            bool passive = false;
+                            int error = MaxErrors;
 
-                        //AbuseDBIP.com API
-                        bool hacking = false;
-                        bool bruteforce = false;
-                        bool webapp = false;
-                        string comment = "";
 
+                            //AbuseDBIP.com API
+                            bool hacking = false;
+                            bool bruteforce = false;
+                            bool webapp = false;
+                            string comment = "";
 
-                        bool banned = false;
-                        
 
-                        
+                            bool banned = false;
 
-                        try
-                        {
-                            Thread.Sleep(100);
-                            Log("Connected - " + hostname, "in", true, hostname, perip);
-                            LogWrite("220 " + Banner.Replace("%host%", Hostname) + "\r\n", sw, hostname, perip);
 
-                            while (client.Connected)
+
+
+                            try
                             {
                                 Thread.Sleep(100);
-                                
-                                string answ = sr.ReadLine(); //Who'd think this ACTUALLY works. BUT: It's doesnt work on Linux? (Needs testing)
-                                //Tested on Ubuntu 16.04 client and 18.04 server. Seems to work!
-                         
-                                string upperfix = answ.Split(' ')[0].ToUpper();
-                                answ.Replace(answ.Split(' ')[0], upperfix); //Fixing the lowercase commands an easy way
-
-                                //Command processing.
-                                if (answ.Length >= 3) //We dont want dummies to spam/DDoS.
-                                {
-                                    Log(answ, "in", true, hostname, perip);
-                                }
-                                if (answ.StartsWith("CONNECT") || answ.StartsWith("GET http"))
+                                Log("Connected - " + hostname, "in", true, hostname, perip);
+                                LogWrite("220 " + Banner.Replace("%host%", Hostname) + "\r\n", sw, hostname, perip);
+
+                                while (client.Connected)
                                 {
-                                    if (Ban)
-                                    {
-                                        var aaa = new Ban();
-                                        aaa.hostname = hostname;
-                                        aaa.time = BanLength;
-                                        bans.Add(aaa);
-                                        client.Close();
-                                    }
-                                    var a = ReportAsync(hostname, "[" + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + "] " + "System scanning (Proxy judging) using CONNECT or GET requests", false, false, true, true, false);
-                                    a.Start();
+                                    Thread.Sleep(100);
 
+                                    string answ = sr.ReadLine(); //Who'd think this ACTUALLY works. BUT: It's doesnt work on Linux? (Needs testing)
+                                                                 //Tested on Ubuntu 16.04 client and 18.04 server. Seems to work!
 
-                                }
-                                if (answ.Length > 128)
-                                {
-                                    client.Close();
-                                }
-                                if (answ.StartsWith("OPTS"))
-                                {
-                                    LogWrite("200 Encoding successfully changed!\r\n", sw, hostname, perip);
-                                }
-                                else if (answ.StartsWith("USER") && username.Length < 3 && !Authed)
-                                {
-                                    string temp = answ.Substring(5).Trim();
-                                    Regex r = new Regex("^[a-zA-Z0-9]*$");
-                                    if (r.IsMatch(temp) && temp.Length < 32 && temp.Length > 1 && (temp != "anonymous" || AllowAnonymous))
-                                    {
-                                        username = temp;
-                                        LogWrite("331 This user is protected with password\r\n", sw, hostname, perip);
+                                    string upperfix = answ.Split(' ')[0].ToUpper();
+                                    answ.Replace(answ.Split(' ')[0], upperfix); //Fixing the lowercase commands an easy way
 
+                                    //Command processing.
+                                    if (answ.Length >= 3) //We dont want dummies to spam/DDoS.
+                                    {
+                                        Log(answ, "in", true, hostname, perip);
                                     }
-                                    else
+                                    if (answ.StartsWith("CONNECT") || answ.StartsWith("GET http"))
                                     {
-                                        LogWrite("530 Wrong username or/and password.\r\n", sw, hostname, perip);
-                                        if (temp.Length > 128)
+                                        if (Ban)
                                         {
+                                            var aaa = new Ban();
+                                            aaa.hostname = hostname;
+                                            aaa.time = BanLength;
+                                            bans.Add(aaa);
                                             client.Close();
                                         }
+                                        var a = ReportAsync(hostname, "[" + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + "] " + "System scanning (Proxy judging) using CONNECT or GET requests", false, false, true, true, false);
+                                        a.Start();
+
+
                                     }
-                                }
-                                else if (answ.StartsWith("PASS") && password.Length < 3 && !Authed)
-                                {
-                                    string temp = answ.Substring(5).Trim();
-                                    if (temp.Length < 32 && temp.Length > 1)
+                                    if (answ.Length > 128)
+                                    {
+                                        client.Close();
+                                    }
+                                    if (answ.StartsWith("OPTS"))
+                                    {
+                                        LogWrite("200 Encoding successfully changed!\r\n", sw, hostname, perip);
+                                    }
+                                    else if (answ.StartsWith("USER") && username.Length < 3 && !Authed)
                                     {
-                                        password = temp;
-                                        if (password == "IEUser@" && PunishScans)
+                                        string temp = answ.Substring(5).Trim();
+                                        Regex r = new Regex("^[a-zA-Z0-9]*$");
+                                        if (r.IsMatch(temp) && temp.Length < 32 && temp.Length > 1 && (temp != "anonymous" || AllowAnonymous))
                                         {
-                                            if (Ban)
-                                            {
-                                                var aaa = new Ban();
-                                                aaa.hostname = hostname;
-                                                aaa.time = BanLength;
-                                                bans.Add(aaa);
-                                                client.Close();
-                                            }
-                                            var a = ReportAsync(hostname, "[" + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + "] " + "System scanning (port scanning) using NMAP", false, false, false, true, false);
-                                            a.Start();
-
-
+                                            username = temp;
+                                            LogWrite("331 This user is protected with password\r\n", sw, hostname, perip);
 
                                         }
-                                        LogWrite("230 Successful login.\r\n", sw, hostname, perip);
-                                        ns.ReadTimeout = 60000;
-                                        Authed = true;
-                                        c = new Client(username, password, hostname);
-
-                                        connected.Add(c);
-                                    }
-                                    else
-                                    {
-                                        LogWrite("530 Wrong username or/and password.\r\n", sw, hostname, perip);
-                                        if (temp.Length > 128)
+                                        else
                                         {
-                                            client.Close();
+                                            LogWrite("530 Wrong username or/and password.\r\n", sw, hostname, perip);
+                                            if (temp.Length > 128)
+                                            {
+                                                client.Close();
+                                            }
                                         }
                                     }
-                                }
-                                else if (answ.Trim() == "SYST")
-                                {
-                                    LogWrite("215 UNIX Type: L8\r\n", sw, hostname, perip);
-                                }
-                                else if (answ.Trim() == "FEAT")
-                                {
-                                    LogWrite("502 Command unavailable.\r\n", sw, hostname, perip);
-                                }
-                                else if (answ.Trim() == "PWD")
-                                {
-                                    LogWrite("257 \"" + directory + "\" is the current working directory\r\n", sw, hostname, perip);
-                                }
-                                else if (answ.Trim() == "PORT")
-                                {
-                                    LogWrite("502 Command unavailable.\r\n", sw, hostname, perip);
-                                }
-                                else if (answ.Trim().StartsWith("TYPE"))
-                                {
-                                    LogWrite("200 OK!\r\n", sw, hostname, perip);
-                                }
-                                else if (answ.Trim().StartsWith("STOR") && Authed)
-                                {
-                                    Thread.Sleep(2000);
-                                    if (passives.ContainsKey(c))
+                                    else if (answ.StartsWith("PASS") && password.Length < 3 && !Authed)
                                     {
-                                        Connectivity connn;
-                                        passives.TryGetValue(c, out connn);
-                                        if (connn.tcp.Connected)
+                                        string temp = answ.Substring(5).Trim();
+                                        if (temp.Length < 32 && temp.Length > 1)
                                         {
-                                            Thread.Sleep(1000);
-                                            LogWrite("150 Ok to send data.\r\n", sw, hostname, perip);
-                                            Thread.Sleep(100);
-                                            List<byte> filess = new List<byte>();
-                                            var bytess = default(byte[]);
-                                            using (var memstream = new MemoryStream())
+                                            password = temp;
+                                            if (password == "IEUser@" && PunishScans)
                                             {
-                                                var buffer = new byte[512];
-                                                var bytesRead = default(int);
-                                                while ((bytesRead = connn.sr.BaseStream.Read(buffer, 0, buffer.Length)) > 0)
-                                                    memstream.Write(buffer, 0, bytesRead);
-                                                bytess = memstream.ToArray();
-                                            }
-                                            System.IO.File.WriteAllBytes("dumps/dump_i" + rnd.Next(1, 2000000000).ToString() + ".txt", bytess);
-                                            Thread.Sleep(200);
-                                            LogWrite("226 Transfer complete!\r\n", sw, hostname, perip);
+                                                if (Ban)
+                                                {
+                                                    var aaa = new Ban();
+                                                    aaa.hostname = hostname;
+                                                    aaa.time = BanLength;
+                                                    bans.Add(aaa);
+                                                    client.Close();
+                                                }
+                                                var a = ReportAsync(hostname, "[" + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + "] " + "System scanning (port scanning) using NMAP", false, false, false, true, false);
+                                                a.Start();
 
-                                            if (Ban)
-                                            {
-                                                var aaa = new Ban();
-                                                aaa.hostname = hostname;
-                                                aaa.time = BanLength;
-                                                bans.Add(aaa);
-                                                client.Close();
-                                            }
-                                            var a = ReportAsync(hostname, "[" + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + "] " + "Unauthorized system access using FTP", true, false, false, false, false);
-                                            a.Start();
 
 
+                                            }
+                                            LogWrite("230 Successful login.\r\n", sw, hostname, perip);
+                                            ns.ReadTimeout = 60000;
+                                            Authed = true;
+                                            c = new Client(username, password, hostname);
+
+                                            connected.Add(c);
                                         }
                                         else
                                         {
-                                            client.Close();
-                                            c.Connected = false;
+                                            LogWrite("530 Wrong username or/and password.\r\n", sw, hostname, perip);
+                                            if (temp.Length > 128)
+                                            {
+                                                client.Close();
+                                            }
                                         }
                                     }
-                                }
-                                else if (answ.StartsWith("RETR") && Authed)
-                                {
-                                    Thread.Sleep(2000);
-                                    string filename = answ.Substring(5).Trim().Replace("/", "");
-                                    File aaaa = null;
-                                    foreach (File aa in files)
+                                    else if (answ.Trim() == "SYST")
+                                    {
+                                        LogWrite("215 UNIX Type: L8\r\n", sw, hostname, perip);
+                                    }
+                                    else if (answ.Trim() == "FEAT")
+                                    {
+                                        LogWrite("502 Command unavailable.\r\n", sw, hostname, perip);
+                                    }
+                                    else if (answ.Trim() == "PWD")
                                     {
-                                        if (aa.name == filename)
+                                        LogWrite("257 \"" + directory + "\" is the current working directory\r\n", sw, hostname, perip);
+                                    }
+                                    else if (answ.Trim() == "PORT")
+                                    {
+                                        LogWrite("502 Command unavailable.\r\n", sw, hostname, perip);
+                                    }
+                                    else if (answ.Trim().StartsWith("TYPE"))
+                                    {
+                                        LogWrite("200 OK!\r\n", sw, hostname, perip);
+                                    }
+                                    else if (answ.Trim().StartsWith("STOR") && Authed)
+                                    {
+                                        Thread.Sleep(2000);
+                                        if (passives.ContainsKey(c))
                                         {
-                                            aaaa = aa;
+                                            Connectivity connn;
+                                            passives.TryGetValue(c, out connn);
+                                            if (connn.tcp.Connected)
+                                            {
+                                                Thread.Sleep(1000);
+                                                LogWrite("150 Ok to send data.\r\n", sw, hostname, perip);
+                                                Thread.Sleep(100);
+                                                List<byte> filess = new List<byte>();
+                                                var bytess = default(byte[]);
+                                                using (var memstream = new MemoryStream())
+                                                {
+                                                    var buffer = new byte[512];
+                                                    var bytesRead = default(int);
+                                                    while ((bytesRead = connn.sr.BaseStream.Read(buffer, 0, buffer.Length)) > 0)
+                                                        memstream.Write(buffer, 0, bytesRead);
+                                                    bytess = memstream.ToArray();
+                                                }
+                                                System.IO.File.WriteAllBytes("dumps/dump_i" + rnd.Next(1, 2000000000).ToString() + ".txt", bytess);
+                                                Thread.Sleep(200);
+                                                LogWrite("226 Transfer complete!\r\n", sw, hostname, perip);
+
+                                                if (Ban)
+                                                {
+                                                    var aaa = new Ban();
+                                                    aaa.hostname = hostname;
+                                                    aaa.time = BanLength;
+                                                    bans.Add(aaa);
+                                                    client.Close();
+                                                }
+                                                var a = ReportAsync(hostname, "[" + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + "] " + "Unauthorized system access using FTP", true, false, false, false, false);
+                                                a.Start();
+
+
+                                            }
+                                            else
+                                            {
+                                                client.Close();
+                                                c.Connected = false;
+                                            }
                                         }
                                     }
-                                    if (passives.ContainsKey(c) && aaaa != null)
+                                    else if (answ.StartsWith("RETR") && Authed)
                                     {
-                                        Connectivity connn;
-                                        passives.TryGetValue(c, out connn);
-                                        if (connn.tcp.Connected)
+                                        Thread.Sleep(2000);
+                                        string filename = answ.Substring(5).Trim().Replace("/", "");
+                                        File aaaa = null;
+                                        foreach (File aa in files)
                                         {
-                                            Thread.Sleep(1000);
-                                            LogWrite("150 Ok to send data.\r\n", sw, hostname, perip);
-                                            Thread.Sleep(100);
-                                            //       byte[] file = aaaa.content;
-                                            //Encoding.ASCII.GetChars(file);
-                                            //      connn.sw.Write(chars, 0, file.Length);
-                                            //      connn.tcp.Close();
-                                            SendFile(aaaa, connn.sw);
-                                            connn.tcp.Close();
-                                            Thread.Sleep(200);
-                                            LogWrite("226 Transfer complete!\r\n", sw, hostname, perip);
-
-                                            if (Ban)
+                                            if (aa.name == filename)
                                             {
-                                                var aaa = new Ban();
-                                                aaa.hostname = hostname;
-                                                aaa.time = BanLength;
-                                                bans.Add(aaa);
-                                                client.Close();
+                                                aaaa = aa;
                                             }
-                                            var a = ReportAsync(hostname, "[" + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + "] " + "Unauthorized system access using FTP", true, false, false, false, false);
-                                            a.Start();
                                         }
-                                        else
+                                        if (passives.ContainsKey(c) && aaaa != null)
                                         {
-                                            client.Close();
-                                            c.Connected = false;
+                                            Connectivity connn;
+                                            passives.TryGetValue(c, out connn);
+                                            if (connn.tcp.Connected)
+                                            {
+                                                Thread.Sleep(1000);
+                                                LogWrite("150 Ok to send data.\r\n", sw, hostname, perip);
+                                                Thread.Sleep(100);
+                                                //       byte[] file = aaaa.content;
+                                                //Encoding.ASCII.GetChars(file);
+                                                //      connn.sw.Write(chars, 0, file.Length);
+                                                //      connn.tcp.Close();
+                                                SendFile(aaaa, connn.sw);
+                                                connn.tcp.Close();
+                                                Thread.Sleep(200);
+                                                LogWrite("226 Transfer complete!\r\n", sw, hostname, perip);
+
+                                                if (Ban)
+                                                {
+                                                    var aaa = new Ban();
+                                                    aaa.hostname = hostname;
+                                                    aaa.time = BanLength;
+                                                    bans.Add(aaa);
+                                                    client.Close();
+                                                }
+                                                var a = ReportAsync(hostname, "[" + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + "] " + "Unauthorized system access using FTP", true, false, false, false, false);
+                                                a.Start();
+                                            }
+                                            else
+                                            {
+                                                client.Close();
+                                                c.Connected = false;
+                                            }
                                         }
-                                    }
 
-                                }
-                                else if (answ.Trim() == "PASV" && Authed)
-                                {
-                                    if (Authed && !passive)
-                                    {
-                                        LogWrite("227 Entering Passive Mode " + PasvInit(PortPasv, Hostname) + "\r\n", sw, hostname, perip);
-                                        c.passive = true;
                                     }
-                                }
-                                else if (answ.Trim().StartsWith("SIZE") && Authed)
-                                {
-                                    string filename = answ.Substring(5).Trim().Replace("/", "");
-                                    File aaaa = null;
-                                    foreach (File aa in files)
+                                    else if (answ.Trim() == "PASV" && Authed)
                                     {
-                                        if (aa.name == filename)
+                                        if (Authed && !passive)
                                         {
-                                            aaaa = aa;
+                                            LogWrite("227 Entering Passive Mode " + PasvInit(PortPasv, Hostname) + "\r\n", sw, hostname, perip);
+                                            c.passive = true;
                                         }
                                     }
-                                    if (aaaa != null)
+                                    else if (answ.Trim().StartsWith("SIZE") && Authed)
                                     {
-                                        LogWrite("213 " + aaaa.size.ToString(), sw, hostname, perip);
+                                        string filename = answ.Substring(5).Trim().Replace("/", "");
+                                        File aaaa = null;
+                                        foreach (File aa in files)
+                                        {
+                                            if (aa.name == filename)
+                                            {
+                                                aaaa = aa;
+                                            }
+                                        }
+                                        if (aaaa != null)
+                                        {
+                                            LogWrite("213 " + aaaa.size.ToString(), sw, hostname, perip);
+                                        }
                                     }
-                                }
 
-                                else if (answ.Trim().StartsWith("LIST") && Authed)
-                                {
-                                    Thread.Sleep(1500);
-                                    if (passives.ContainsKey(c))
+                                    else if (answ.Trim().StartsWith("LIST") && Authed)
                                     {
-                                        Connectivity connn;
-                                        passives.TryGetValue(c, out connn);
-                                        if (connn.tcp.Connected)
+                                        Thread.Sleep(1500);
+                                        if (passives.ContainsKey(c))
                                         {
-                                            string Builder = "";
-                                            Builder += "drwxrwxrwx 5 root root 12288 Dec  1 16:51 .\r\n";
-                                            Builder += "drwxrwxrwx 5 root root 12288 Dec  1 16:51 ..\r\n";
-                                            int length = 5;
-                                            foreach (File file in files)
+                                            Connectivity connn;
+                                            passives.TryGetValue(c, out connn);
+                                            if (connn.tcp.Connected)
                                             {
-                                                if (file.size.ToString().Length > length)
+                                                string Builder = "";
+                                                Builder += "drwxrwxrwx 5 root root 12288 Dec  1 16:51 .\r\n";
+                                                Builder += "drwxrwxrwx 5 root root 12288 Dec  1 16:51 ..\r\n";
+                                                int length = 5;
+                                                foreach (File file in files)
+                                                {
+                                                    if (file.size.ToString().Length > length)
+                                                    {
+                                                        length = file.size.ToString().Length;
+                                                    }
+                                                }
+                                                foreach (File file in files)
                                                 {
-                                                    length = file.size.ToString().Length;
+                                                    Builder += file.chmod;
+                                                    Builder += " " + rnd.Next(1, 9) + " ";
+                                                    Builder += "root root ";
+                                                    Builder += new string(' ', length - file.size.ToString().Length) + file.size.ToString();
+                                                    Builder += " " + file.creation;
+                                                    Builder += " " + file.name + "\r\n";
                                                 }
+                                                LogWrite("150 Here comes the directory listing.\r\n", sw, hostname, perip);
+                                                Thread.Sleep(100);
+                                                connn.sw.Write(Builder);
+                                                connn.tcp.Close();
+                                                Thread.Sleep(100);
+                                                LogWrite("226 Directory send OK\r\n", sw, hostname, perip);
+
                                             }
-                                            foreach (File file in files)
+                                            else
                                             {
-                                                Builder += file.chmod;
-                                                Builder += " " + rnd.Next(1, 9) + " ";
-                                                Builder += "root root ";
-                                                Builder += new string(' ', length - file.size.ToString().Length) + file.size.ToString();
-                                                Builder += " " + file.creation;
-                                                Builder += " " + file.name + "\r\n";
+                                                client.Close();
+                                                c.Connected = false;
                                             }
-                                            LogWrite("150 Here comes the directory listing.\r\n", sw, hostname, perip);
-                                            Thread.Sleep(100);
-                                            connn.sw.Write(Builder);
-                                            connn.tcp.Close();
-                                            Thread.Sleep(100);
-                                            LogWrite("226 Directory send OK\r\n", sw, hostname, perip);
-
-                                        }
-                                        else
-                                        {
-                                            client.Close();
-                                            c.Connected = false;
                                         }
                                     }
-                                }
-                                else if (answ.StartsWith("CWD"))
-                                {
-                                    LogWrite("200 OK!\r\n", sw, hostname, perip);
-                                }
-                                else if (answ.StartsWith("CPFR"))
-                                {
-                                    //Fun part: tricking random exploiters. Very "hackers"
-                                    triggered = true; //First level trigger
-                                    LogWrite("350 Need more information.\r\n", sw, hostname, perip);
-                                }
-                                else if (answ.Trim().StartsWith("CPTO") && triggered)
-                                {
-                                    LogWrite("250 Need more information.\r\n", sw, hostname, perip);
+                                    else if (answ.StartsWith("CWD"))
+                                    {
+                                        LogWrite("200 OK!\r\n", sw, hostname, perip);
+                                    }
+                                    else if (answ.StartsWith("CPFR"))
+                                    {
+                                        //Fun part: tricking random exploiters. Very "hackers"
+                                        triggered = true; //First level trigger
+                                        LogWrite("350 Need more information.\r\n", sw, hostname, perip);
+                                    }
+                                    else if (answ.Trim().StartsWith("CPTO") && triggered)
+                                    {
+                                        LogWrite("250 Need more information.\r\n", sw, hostname, perip);
 
-                                }
-                                else if (answ.Trim().StartsWith("AUTH"))
-                                {
-                                    LogWrite("502 Please use plain FTP.\r\n", sw, hostname, perip); // We dont want them to use security.
-                                }
-                                else if (Authed && username == "admin" && md5(password) == "")
-                                {
-                                    //Todo: admin cmds
-                                }
-                                else if (answ.Trim().StartsWith("CLNT"))
-                                {
-                                    LogWrite("200 OK!\r\n", sw, hostname, perip);
-                                }
-                                else if (Authed && answ.Trim().StartsWith("NOOP"))
-                                {
-                                    LogWrite("200 OK!\r\n", sw, hostname, perip);
-                                } else if (Authed && answ.Trim().StartsWith("REST"))
-                                {
-                                    LogWrite("502 There is no such command.\r\n", sw, hostname, perip);
-                                }
-                                else
-                                {
-                                    if (answ.Length >= 3)
+                                    }
+                                    else if (answ.Trim().StartsWith("AUTH"))
+                                    {
+                                        LogWrite("502 Please use plain FTP.\r\n", sw, hostname, perip); // We dont want them to use security.
+                                    }
+                                    else if (Authed && username == "admin" && md5(password) == "")
                                     {
-                                        error--;
-                                        if (error <= 0)
+                                        //Todo: admin cmds
+                                    }
+                                    else if (answ.Trim().StartsWith("CLNT"))
+                                    {
+                                        LogWrite("200 OK!\r\n", sw, hostname, perip);
+                                    }
+                                    else if (Authed && answ.Trim().StartsWith("NOOP"))
+                                    {
+                                        LogWrite("200 OK!\r\n", sw, hostname, perip);
+                                    }
+                                    else if (Authed && answ.Trim().StartsWith("REST"))
+                                    {
+                                        LogWrite("502 There is no such command.\r\n", sw, hostname, perip);
+                                    }
+                                    else
+                                    {
+                                        if (answ.Length >= 3)
                                         {
-                                            client.Close();
-                                        }
+                                            error--;
+                                            if (error <= 0)
+                                            {
+                                                client.Close();
+                                            }
 
+                                        }
                                     }
-                                }
-                                if (answ.Contains("php") && triggered)
-                                {
-                                    trigger2 = true; //Second level trigger
-                                }
-                                if (trigger2)
-                                {
-                                    LogWrite("110 Illegal activity was detected.\r\n", sw, hostname, perip);
-                                    LogWrite("110 Please, log off now.\r\n", sw, hostname, perip);
-                                    if (Ban)
+                                    if (answ.Contains("php") && triggered)
                                     {
-                                        var aaa = new Ban();
-                                        aaa.hostname = hostname;
-                                        aaa.time = BanLength;
-                                        bans.Add(aaa);
-                                        client.Close();
+                                        trigger2 = true; //Second level trigger
                                     }
-                                    var a = ReportAsync(hostname, "[" + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + "] " + "RCE Attempt at 21 port using ProFTPd exploit", true, false, false, false, false);
-                                    a.Start();
+                                    if (trigger2)
+                                    {
+                                        LogWrite("110 Illegal activity was detected.\r\n", sw, hostname, perip);
+                                        LogWrite("110 Please, log off now.\r\n", sw, hostname, perip);
+                                        if (Ban)
+                                        {
+                                            var aaa = new Ban();
+                                            aaa.hostname = hostname;
+                                            aaa.time = BanLength;
+                                            bans.Add(aaa);
+                                            client.Close();
+                                        }
+                                        var a = ReportAsync(hostname, "[" + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + "] " + "RCE Attempt at 21 port using ProFTPd exploit", true, false, false, false, false);
+                                        a.Start();
 
 
 
+                                    }
+
                                 }
 
                             }
-
-                        }
-                        catch (Exception e)
-                        {
-                            client.Close();
-                            c.Connected = false;
-                        }
-                        Active.SetConnections(hostname, actives, Active.GetConnections(hostname, actives) - 1);
-                        if (PerIPLogs)
-                        {
-                            try
+                            catch (Exception e)
                             {
-                                perips.Remove(perip);
-                                perip.Close();
-                            } catch
+                                client.Close();
+                                c.Connected = false;
+                            }
+                            Active.SetConnections(hostname, actives, Active.GetConnections(hostname, actives) - 1);
+                            if (PerIPLogs)
                             {
+                                try
+                                {
+                                    perips.Remove(perip);
+                                    perip.Close();
+                                }
+                                catch
+                                {
 
+                                }
                             }
+                            thread_amount--;
                         }
+                        )).Start();
+                    }
+                    else
+                    {
+                        Console.WriteLine("-> ALL FREE THREADS ARE CURRENTLY BUSY! <-");
+                        client.Close();
                     }
-                    )).Start();
                 }
             }).Start();
             new Thread(() =>
@@ -924,67 +941,72 @@ namespace BearFTP
                     {
                         pasv_actives.Add(new Active(hostname, 1));
                     }
-                    Thread user = new Thread(new ThreadStart(() =>
+                    if (thread_amount <= MaxThreads)
                     {
-                        Thread.CurrentThread.IsBackground = true;
+                        thread_amount++;
+                        Thread user = new Thread(new ThreadStart(() =>
+                        {
+                            Thread.CurrentThread.IsBackground = true;
 
-                        Client c = new Client("1", "2", "3");
+                            Client c = new Client("1", "2", "3");
 
-                        
 
-                        try
-                        {
-                            bool ispresent = false;
-                            foreach (Client cl in connected)
+
+                            try
                             {
-                                if (cl.hostname == hostname && cl.Connected)
+                                bool ispresent = false;
+                                foreach (Client cl in connected)
                                 {
-                                    c = cl;
-                                    ispresent = true;
+                                    if (cl.hostname == hostname && cl.Connected)
+                                    {
+                                        c = cl;
+                                        ispresent = true;
+                                    }
                                 }
-                            }
-                            if (!ispresent)
-                            {
-                                client.Close();
+                                if (!ispresent)
+                                {
+                                    client.Close();
 
-                            }
-                            else
-                            {
-                                Connectivity ca = new Connectivity();
-                                ca.sr = sr;
-                                ca.sw = sw;
-                                ca.tcp = client;
-                                passives.Add(c, ca);
+                                }
+                                else
+                                {
+                                    Connectivity ca = new Connectivity();
+                                    ca.sr = sr;
+                                    ca.sw = sw;
+                                    ca.tcp = client;
+                                    passives.Add(c, ca);
                                 /*  while (client.Connected)
                                   {
                                       Thread.Sleep(3000);
                                   }*/
-                                for (int i = 0; client.Connected; i++)
-                                {
-                                    Thread.Sleep(1000);
-                                    if (i >= 120)
+                                    for (int i = 0; client.Connected; i++)
                                     {
-                                        client.Close();
-                                        passives.Remove(c);
+                                        Thread.Sleep(1000);
+                                        if (i >= 120)
+                                        {
+                                            client.Close();
+                                            passives.Remove(c);
+                                        }
                                     }
+                                    client.Close();
+                                    passives.Remove(c);
                                 }
-                                client.Close();
-                                passives.Remove(c);
                             }
-                        }
-                        catch (Exception e)
-                        {
-                            if (!e.Message.StartsWith("An item"))
+                            catch (Exception e)
                             {
-                                client.Close();
-                                passives.Remove(c);
+                                if (!e.Message.StartsWith("An item"))
+                                {
+                                    client.Close();
+                                    passives.Remove(c);
+                                }
                             }
+                            Active.SetConnections(hostname, pasv_actives, Active.GetConnections(hostname, pasv_actives) - 1);
+                            thread_amount--;
                         }
-                        Active.SetConnections(hostname, pasv_actives, Active.GetConnections(hostname, pasv_actives) - 1);
-                    }
 
-                    ));
-                    user.Start();
+                        ));
+                        user.Start();
+                    }
 
 
                 }

programming_language: C#
filename: ConfigFile.txt
change_type: ModificationType.MODIFY
diff: @@ -14,6 +14,7 @@
   "BanLength": 3600,
   "MaxErrors": 6,
   "BufferSize": 8192,
+  "MaxThreads": 50,
   "Files": [
   {
     "Name": "readme.txt",

programming_language: TypeScript
filename: config.json
change_type: ModificationType.MODIFY
diff: @@ -14,6 +14,7 @@
   "BanLength": 7200,
   "MaxErrors": 5,
   "BufferSize": 8192,
+  "MaxThreads": 30,
   "Files": [
   {
     "Name": "sqlite.db",

programming_language: CoffeeScript
filename: CHANGELOG.txt
change_type: ModificationType.MODIFY
diff: @@ -4,4 +4,5 @@ v0.4.0:
 - Proper mitigation of slow loris base attack by implementing configurable amount of maximal allowed threads to be spawned (def. 50 a.k.a somewhere about 20 clients or 10 bots) (CVE-2020-8815)
 - Anonymous private version usage statistics implemented (toggleable)
 - Active mode implement
-- Local command handler to edit config on-the-fly (see COMMANDS.md)
\ No newline at end of file
+- Local command handler to edit config on-the-fly (see COMMANDS.md)
+- Config option for disabling outputting to console
\ No newline at end of file

programming_language: Markdown
repo_url: https://github.com/kolya5544/BearFTP
commit_details: repo_url: https://github.com/kolya5544/BearFTP
author: kolya5544
author_date: 2020-02-09 21:20:17+03:00
msg: Anonymous configuration fix, base thread vulnerability fix. v0.3.1
repository: repo_url: https://github.com/kolya5544/BearFTP
repo_name: kolya5544/BearFTP
description: Honeypot FTP server written in .NET Core (C#) for both Linux and Windows.
repo_language: C#
owner: kolya5544
file_changes: filename: Program.cs
change_type: ModificationType.MODIFY
diff: @@ -495,36 +495,12 @@ namespace BearFTP
                             while (client.Connected)
                             {
                                 Thread.Sleep(100);
-                                //Receiving handler START
-                                string answ = "";
-                                bool flag = true;
-                                bool upper = true;
-
-                                while (flag)
-                                {
-                                    int a = sr.Read();
-                                    if (upper)
-                                    {
-                                        answ += char.ToUpper((char)a);
-                                    } else
-                                    {
-                                        answ += (char)a;
-                                    }
-                                    if (a == 13)
-                                    {
-                                        flag = false;
-                                    }
-                                    if (a == 0x20)
-                                    {
-                                        upper = false;
-                                    }
-                                    if (answ.Length > 128)
-                                    {
-                                        client.Close();
-                                    }
-                                }
-                                answ = answ.Trim();
-                                //Receiving handler END
+                                
+                                string answ = sr.ReadLine(); //Who'd think this ACTUALLY works. BUT: It's doesnt work on Linux? (Needs testing)
+                                //Tested on Ubuntu 16.04 client and 18.04 server. Seems to work!
+                         
+                                string upperfix = answ.Split(' ')[0].ToUpper();
+                                answ.Replace(answ.Split(' ')[0], upperfix); //Fixing the lowercase commands an easy way
 
                                 //Command processing.
                                 if (answ.Length >= 3) //We dont want dummies to spam/DDoS.
@@ -558,7 +534,7 @@ namespace BearFTP
                                 {
                                     string temp = answ.Substring(5).Trim();
                                     Regex r = new Regex("^[a-zA-Z0-9]*$");
-                                    if (r.IsMatch(temp) && temp.Length < 32 && temp.Length > 1 && (temp != "anonymous" && !AllowAnonymous))
+                                    if (r.IsMatch(temp) && temp.Length < 32 && temp.Length > 1 && (temp != "anonymous" || AllowAnonymous))
                                     {
                                         username = temp;
                                         LogWrite("331 This user is protected with password\r\n", sw, hostname, perip);
@@ -644,16 +620,16 @@ namespace BearFTP
                                             LogWrite("150 Ok to send data.\r\n", sw, hostname, perip);
                                             Thread.Sleep(100);
                                             List<byte> filess = new List<byte>();
-                                            var bytes = default(byte[]);
+                                            var bytess = default(byte[]);
                                             using (var memstream = new MemoryStream())
                                             {
                                                 var buffer = new byte[512];
                                                 var bytesRead = default(int);
                                                 while ((bytesRead = connn.sr.BaseStream.Read(buffer, 0, buffer.Length)) > 0)
                                                     memstream.Write(buffer, 0, bytesRead);
-                                                bytes = memstream.ToArray();
+                                                bytess = memstream.ToArray();
                                             }
-                                            System.IO.File.WriteAllBytes("dumps/dump_i" + rnd.Next(1, 2000000000).ToString() + ".txt", bytes);
+                                            System.IO.File.WriteAllBytes("dumps/dump_i" + rnd.Next(1, 2000000000).ToString() + ".txt", bytess);
                                             Thread.Sleep(200);
                                             LogWrite("226 Transfer complete!\r\n", sw, hostname, perip);
 

programming_language: C#
filename: CHANGELOG.txt
change_type: ModificationType.MODIFY
diff: @@ -1,12 +1,6 @@
 --> IT ONLY CONTAINS THE MOST RECENT CHANGES!
 
-v0.3.0:
-- Toggleable anonymous logins
-- Per-IP logs (create "iplogs" folder)
-- Max connections per second and max active connections are now changeable
-- Buffer size can now be changed (def.8192) 
-- Fixed lower-case command handling
-- Final attempt to fix an encoding bug related to outputting control characters
-- Changeable bantime
-- Toggleable ban on big amount of errors (a.k.a some sort of service probe)
-- Moved default config to Resources
\ No newline at end of file
+v0.3.1:
+- Fixed AllowAnonymous handling (oops)
+- One more attempt to fix encoding break on some characters (only affects console)
+- Fixed improper handling of incoming data on base socket (a.k.a base socket thread overuse fix)
\ No newline at end of file

programming_language: Markdown