Ejemplo n.º 1
0
 public Target(IConfig cfg, String hostname, NetworkImage img)
 {
     config        = cfg;
     this.img      = img;
     this.hostname = hostname;
     resolveHostname();
     firewallLevel = img.getFirewallLevel();
     isWatched     = img.checkForAnonymity();
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Process an image extracting host info and attacks it if possible
        /// </summary>
        /// <param name="imgstring">host image represented by its bytes string</param>
        /// <param name="hostname"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        public async Task <int> ProcessImgAndAttack(string imgstring, string hostname, ScanMode mode)
        {
            var    image = new NetworkImage(imgstring);
            string text  = "";
            Page   page  = null;

            try
            {
                // the image is made of three lines:
                // line 1: "Hostname:"
                // line 2: hostname
                // line 3: firewall level
                page = engine.Process(new Bitmap(image.image));
                text = page.GetText();

                // get subimage for the second line: if orange the host is already been hacked
                var subimgHostname = image.GetSubImage(0, image.image.Height / 3, image.image.Width, image.image.Height / 3);
                // get subimage for the third line: if red the host is watched by FBI
                var subimgFwall = image.GetSubImage(0, image.image.Height / 3 * 2, image.image.Width, image.image.Height / 3);



                var hackedColor       = Color.FromArgb(255, 250, 152, 25);
                var watchedbyFBIColor = Color.FromArgb(255, 136, 0, 0);
                if (hasColor(subimgHostname, hackedColor))
                {
                    //var filename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), Path.GetRandomFileName() + ".png");
                    //subimgHostname.Save(filename, System.Drawing.Imaging.ImageFormat.Png);



                    config.logger.Log("Host {0} already hacked, skip", hostname);
                    // only updates hostname
                    var scan = await ScanHost(hostname, 10);

                    if (scan != null)
                    {
                        var ip  = (string)scan["ipaddress"];
                        var ips = config.persistanceMgr.GetIp(ip);
                        if (ips != null && ips.Hostname == "unknown")
                        {
                            ips.Hostname = hostname;
                            if (config.persistanceMgr.UpdateIp(ips))
                            {
                                config.logger.Log("Updated hostname {0} for ip {1}", ips.Hostname, ips.IP);
                            }
                        }
                    }
                    return(1);
                }
                if (hasColor(subimgFwall, watchedbyFBIColor))
                {
                    //if (vhUtils.IsContestRunning())
                    //{
                    //    var filename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
                    //        , "FBI_WATCHED_" + hostname + "_" + Path.GetRandomFileName() + ".png");
                    //    image.image.Save(filename, System.Drawing.Imaging.ImageFormat.Png);
                    //}

                    //var filename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), Path.GetRandomFileName() + "_FBI.png");
                    //subimgFwall.Save(filename, System.Drawing.Imaging.ImageFormat.Png);
                    config.logger.Log("Host {0} is watched by FBI!! skipping", hostname);
                    Log.ContestLogger.Log("Skip watched by FBI: {0}", hostname);
                    return(1);
                }
                else
                {
                }
                //for (int i = 0; i < subimgHostname.Width; i++)
                //{
                //    int j = 0;
                //    for (; j < subimgHostname.Height; j++)
                //    {
                //        var pix = subimgHostname.GetPixel(i, j);
                //        if (pix == hackedColor)
                //        {
                //            config.logger.Log("Host {0} already hacked, skip", hostname);
                //            // only updates hostname
                //            var scan = await ScanHost(hostname, 10);
                //            if (scan != null)
                //            {
                //                var ip = (string)scan["ipaddress"];
                //                var ips = config.persistanceMgr.GetIp(ip);
                //                if (ips != null && ips.Hostname == "unknown")
                //                {
                //                    ips.Hostname = hostname;
                //                    if (config.persistanceMgr.UpdateIp(ips))
                //                        config.logger.Log("Updated hostname {0} for ip {1}", ips.Hostname, ips.IP);
                //                }
                //            }
                //            return 1;
                //        }
                //        //if (pix.R != 0)
                //        //    break;
                //    }
                //    if (j < subimgHostname.Height)
                //        break;
                //}
            }
            catch (Exception e)
            {
                Debug.Print(e.ToString());
                return(-1);
            }
            finally
            {
                if (page != null)
                {
                    page.Dispose();
                }
            }

            if (text.Contains("Matched by the FBI") || text.Contains("Watched by the FBI"))
            {
                //if (vhUtils.IsContestRunning())
                //{
                //    var filename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
                //        , "FBI_WATCHED_" + hostname + "_" + Path.GetRandomFileName() + ".png");
                //    image.image.Save(filename, System.Drawing.Imaging.ImageFormat.Png);
                //}

                config.logger.Log("Host {0} is watched by FBI!! skipping", hostname);
                Log.ContestLogger.Log("Skip watched by FBI: {0}", hostname);
                return(1);
            }
            else
            {
                //if (vhUtils.IsContestRunning())
                //{
                //    // FOR TESTING PURPOSE
                //    var root = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "VHACK");
                //    if (!Directory.Exists(root))
                //        Directory.CreateDirectory(root);
                //    var filename = Path.Combine(root
                //        , "FBI_WATCHED_" + hostname + "_" + Path.GetRandomFileName() + ".png");
                //    image.image.Save(filename, System.Drawing.Imaging.ImageFormat.Png);

                //    config.logger.Log($"******\n\tATTACKING {hostname};\n\t{text}\n\t{filename}\n*******");
                //}

                var firewall = text.Split(':');
                if (firewall.Length > 2)
                {
                    var val = Convert.ToInt32(firewall[2].Trim());
                    if (val > config.maxFirewall)
                    {
                        config.logger.Log("Firewall is too high: {0}", val);
                    }
                    else
                    {
                        var scan = await ScanHost(hostname, 10);

                        if (scan == null)
                        {
                            config.logger.Log("Unable to scan host {0}", hostname);
                            config.logger.Log("********* BLOCKED BY FBI!!! **********");
                            Log.ContestLogger.Log("ScanHost {0} error BLOCKED BY FBI!!!", hostname);
                            return(-1);
                        }

                        var ip   = (string)scan["ipaddress"];
                        var vuln = (int)scan["vuln"];
                        if (!ip.Contains(".vHack.cc") && vuln == 1)
                        {
                            if (mode == ScanMode.Secure)
                            {
                                Thread.Sleep(r.Next(1, 3) * vhConsole.WaitStep);
                            }
                            else if (mode == ScanMode.Potator)
                            {
                                Thread.Sleep(r.Next(0, 1) * vhConsole.WaitStep);
                            }

                            try
                            {
                                // look if already in list and in case update hostname
                                var ips = config.persistanceMgr.GetIp(ip);
                                if (ips != null && ips.Hostname == "unknown")
                                {
                                    ips.Hostname = hostname;
                                    if (config.persistanceMgr.UpdateIp(ips))
                                    {
                                        config.logger.Log("Updated hostname {0} for ip {1}", ips.Hostname, ips.IP);
                                    }
                                }

                                //if (vhUtils.IsContestRunning())
                                //{
                                //    var filename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
                                //        , "FBI_FREE_" + hostname + "_" + Path.GetRandomFileName() + ".png");
                                //    image.image.Save(filename, System.Drawing.Imaging.ImageFormat.Png);
                                //}

                                var res = await AttackIp(ip);

                                // remove spyware
                                // TODO
                                //var upd = new Update(config);
                                //var spy = await upd.SpywareInfo();
                                //var splt = (string)(spy[0]);
                                //if (!string.IsNullOrEmpty(splt)) //TODO
                                //{
                                //    var removed = await upd.removeSpyware();
                                //}

                                if (res == -1)
                                {
                                    config.logger.Log("Attack to IP {0} failed\n", ip);
                                }

                                return(res);
                            }
                            catch (Exception exc)
                            {
                                config.logger.Log("Error: {0}", exc.ToString());
                                return(-1);
                            }
                        }
                    }
                }
                else
                {
                    config.logger.Log("Text not recognized:\n\n{0}\n\n", text);
                    return(-1);
                }
            }

            return(-1);
        }