示例#1
0
        public Boolean getTask()
        {
            Console.WriteLine("Getting task");
            Task get = new Task
            {
                action = "getTask",
                token  = client.tokenID
            };

            jsonClass jsC = new jsonClass {
                debugFlag = debugFlag, connectURL = client.connectURL
            };
            string jsonString = jsC.toJson(get);
            string ret        = jsC.jsonSend(jsonString);

            if (jsC.isJsonSuccess(ret))
            {
                if (jsC.getRetVar(ret, "taskId") != null)
                {
                    taskID     = Int32.Parse(jsC.getRetVar(ret, "taskId"));
                    attackcmd  = (jsC.getRetVar(ret, "attackcmd"));
                    cmdpars    = (jsC.getRetVar(ret, "cmdpars"));
                    hashlistID = Int32.Parse(jsC.getRetVar(ret, "hashlistId"));
                    benchTime  = Int32.Parse(jsC.getRetVar(ret, "bench"));
                    crackerId  = Int32.Parse(jsC.getRetVar(ret, "crackerId"));

                    Console.WriteLine("Server has assigned client with Task:{0}, Cracker:{2} and Hashlist:{1}", taskID, hashlistID, crackerId);
                    if (jsC.getRetVar(ret, "benchType") == "run")
                    {
                        benchMethod = 1;
                    }
                    else
                    {
                        benchMethod = 2;
                    }
                    statusTimer   = Int32.Parse(jsC.getRetVar(ret, "statustimer"));
                    hashlistAlias = jsC.getRetVar(ret, "hashlistAlias");
                    files         = jsC.getRetArray(ret, "files");
                    int gotChunk = 1;

                    foreach (string fileItem in files)
                    {
                        string actualFile = Path.Combine(filepath, fileItem);
                        if (!File.Exists(actualFile))
                        {
                            getFile(fileItem);

                            if (fileItem.ToLower().EndsWith(".7z"))
                            {
                                if (sevenZip.xtract(actualFile, filepath))
                                {
                                    File.WriteAllText(actualFile, "UNPACKED");
                                }
                                else
                                {
                                    return(false);
                                }
                            }
                        }
                    }

                    //Convert implied relative paths to absolute paths only applies to Mac OSX / Linux
                    //We, break up the attack command by space and check whether the file for the full path exists, we it does we replace
                    //Could potentially cause issues if the file names are attack numbers eg 1 2 3 4 5 6 7
                    //File names cannot contain spaces
                    //Altnerative method is to perform find replace on the attackcmd based on the files array
                    if (client.osID != 1)
                    {
                        string[] explode = new string[] { };
                        explode = attackcmd.Split(' ');

                        for (int i = 0; i < files.Count; i++)
                        {
                            string absolutePath = Path.Combine(filepath, files[i].ToString());
                            string match        = " " + files[i].ToString(); //Prefix a space for better matching
                            string replace      = " \"" + absolutePath + "\"";
                            if (File.Exists(absolutePath))
                            {
                                attackcmd = attackcmd.Replace(match, replace);
                            }
                        }
                    }

                    if (getHashes(hashlistID) == false)
                    {
                        return(false);
                    }


                    if (fileSize(Path.Combine(hashpath, Path.GetFileName(hashlistID.ToString()))) == 0)
                    {
                        Console.WriteLine("Hashlist is 0 bytes");
                        return(false);
                    }

                    //Check if we have the correct cracker if not we download

                    hashcatUpdateClass hcUpdater = new hashcatUpdateClass {
                        debugFlag = debugFlag, client = client, AppPath = appPath, sevenZip = sevenZip, binaryVersionId = Int32.Parse(jsC.getRetVar(ret, "crackerId"))
                    };

                    //If the cracker did not successfully initliaze then throw error and report
                    if (!hcUpdater.updateCracker())
                    {
                        errorProps eProps = new errorProps
                        {
                            token   = client.tokenID,
                            taskId  = taskID,
                            message = "Client could not locate cracker"
                        };
                        jsonString = jsC.toJson(eProps);
                        ret        = jsC.jsonSend(jsonString);
                        return(false);
                    }

                    //The client may change per task, we need to update these after the update
                    hcClass.hcDirectory = client.crackerPath;
                    hcClass.hcBinary    = client.crackerBinary;

                    gotChunk = getChunk(taskID);

                    while (gotChunk != 0)
                    {
                        gotChunk = getChunk(taskID);
                    }

                    return(true);
                }
                else
                {
                    Console.WriteLine("No new task assigned to agent");
                }
            }


            return(false);
        }
        public int getChunk(int inTask)
        {
            Console.WriteLine("Getting chunk...");
            chunkProps cProps = new chunkProps
            {
                action = "chunk",
                token  = client.tokenID,
                taskId = inTask
            };

            jsC.debugFlag     = debugFlag;
            jsC.connectURL    = client.connectURL;
            primaryCracked    = new List <string> {
            };
            hcClass.debugFlag = debugFlag;

            string jsonString = jsC.toJson(cProps);
            string ret        = jsC.jsonSend(jsonString);


            if (jsC.isJsonSuccess(ret))
            {
                string status = jsC.getRetVar(ret, "status");


                string argBuilder   = attackcmd;
                string attackcmdMod = " " + cmdpars + " ";
                string actualHLpath = Path.Combine(hashpath, hashlistID.ToString());
                switch (status)
                {
                case "OK":
                    attackcmdMod  = " " + cmdpars + " ";                                                 //Reset the argument string
                    attackcmdMod += attackcmd.Replace(hashlistAlias, "\"" + actualHLpath + "\" ");       //Add the path to Hashlist
                    attackcmdMod += " --outfile-check-dir=\"" + zapPath + hashlistID.ToString() + "\" "; //Add the zap path to the commands

                    hcClass.setArgs(attackcmdMod);

                    chunkNo = Convert.ToInt64(jsC.getRetVar(ret, "chunk"));
                    skip    = Convert.ToInt64(jsC.getRetVar(ret, "skip"));
                    length  = Convert.ToInt64(jsC.getRetVar(ret, "length"));

                    List <Packets> uploadPackets = new List <Packets>();

                    hcClass.setDirs(appPath, client.osID);
                    hcClass.setPassthrough(ref uploadPackets, ref packetLock, separator.ToString(), debugFlag);

                    Thread thread = new Thread(() => threadPeriodicUpdate(ref uploadPackets, ref packetLock));
                    thread.Start();                                                                                   //Start our thread to monitor the upload queue

                    hcClass.startAttack(chunkNo, taskID, skip, length, separator.ToString(), statusTimer, tasksPath); //Start the hashcat binary
                    thread.Join();

                    return(1);

                case "keyspace_required":
                    hcClass.setDirs(appPath, client.osID);
                    attackcmdMod  = " " + cmdpars + " ";                  //Reset the argument string
                    attackcmdMod += attackcmd.Replace(hashlistAlias, ""); //Remove out the #HL#
                    hcClass.setArgs(attackcmdMod);
                    long calcKeyspace = 0;

                    hcClass.runKeyspace(ref calcKeyspace);


                    if (calcKeyspace == 0)
                    {
                        errorProps eProps = new errorProps
                        {
                            token   = client.tokenID,
                            task    = taskID,
                            message = "Invalid keyspace, keyspace probably too small for this hashtype"
                        };
                        jsonString = jsC.toJson(eProps);
                        ret        = jsC.jsonSend(jsonString);
                        return(0);
                    }
                    else
                    {
                        keyspaceProps kProps = new keyspaceProps
                        {
                            token    = client.tokenID,
                            taskId   = taskID,
                            keyspace = calcKeyspace
                        };
                        jsonString = jsC.toJson(kProps);
                        ret        = jsC.jsonSend(jsonString);
                    }

                    return(2);

                case "fully_dispatched":
                    return(0);

                case "benchmark":
                    hcClass.setDirs(appPath, client.osID);
                    attackcmdMod  = " " + cmdpars + " ";                                          //Reset the argument string
                    attackcmdMod += attackcmd.Replace(hashlistAlias, "\"" + actualHLpath + "\""); //Add the path to Hashlist
                    hcClass.setArgs(attackcmdMod);

                    Dictionary <string, double> collection = new Dictionary <string, double>();   //Holds all the returned benchmark values1

                    hcClass.runBenchmark(benchMethod, benchTime, ref collection, legacy);

                    benchProps bProps = new benchProps
                    {
                        token  = client.tokenID,
                        taskId = taskID,
                    };

                    if (benchMethod == 1)     //Old benchmark method using actual run
                    {
                        bProps.type = "run";
                        if (collection.ContainsKey("PROGRESS_REJ"))
                        {
                            bProps.result = collection["PROGRESS_REJ"].ToString("0." + new string('#', 100));
                        }
                        else
                        {
                            bProps.result = collection["PROGRESS_DIV"].ToString("0." + new string('#', 100));
                        }
                    }
                    else     //New benchmark method using --speed param
                    {
                        bProps.type   = "speed";
                        bProps.result = collection["LEFT_TOTAL"].ToString() + ":" + collection["RIGHT_TOTAL"].ToString();
                    }


                    jsonString = jsC.toJson(bProps);
                    ret        = jsC.jsonSend(jsonString);
                    if (!jsC.isJsonSuccess(ret))
                    {
                        Console.WriteLine("Server rejected benchmark");
                        Console.WriteLine("Check the hashlist was downloaded correctly");
                        return(0);
                    }
                    return(3);

                case "hashcat_update":
                    Console.WriteLine("A new version of hashcat was found, updating...");
                    hashcatUpdateClass hcUpdater = new hashcatUpdateClass {
                        debugFlag = debugFlag, client = client, AppPath = appPath, sevenZip = sevenZip
                    };
                    if (hcUpdater.updateHashcat())
                    {
                        hashcatClass hcClass = new hashcatClass {
                            debugFlag = debugFlag
                        };
                        hcClass.setDirs(appPath, client.osID);
                        string[] versionInts = { };
                        string   hcVersion   = hcClass.getVersion2(ref versionInts);
                        Console.WriteLine("Hashcat version {0} found", hcVersion);


                        if (hcVersion.Length != 0)
                        {
                            if (Convert.ToInt32(versionInts[0]) == 3 && Convert.ToInt32(versionInts[1]) == 6)
                            {
                                if (hcVersion.Contains("-"))
                                {
                                    legacy = false;
                                    //This is most likely a beta/custom build with commits ahead of 3.6.0 release branch
                                }
                            }
                            else if (Convert.ToInt32(versionInts[0]) == 3)
                            {
                                if (Convert.ToInt32(versionInts[1].Substring(0, 1)) >= 6)
                                {
                                    legacy = false;
                                    //This is a release build above 3.6.0
                                }
                            }
                            else if (Convert.ToInt32(versionInts[0]) >= 4)
                            {
                                legacy = false;
                                //This is a release build above 4.0.0
                            }
                        }
                        else
                        {
                            //For some reason we couldn't read the version, lets just assume we are on non legacy
                            legacy = false;
                        }
                        setOffset();
                    }
                    else
                    {
                        Console.WriteLine("Update failed");
                    }
                    return(4);
                }
            }
            return(0);
        }
示例#3
0
        public int getChunk(int inTask)
        {
            Console.WriteLine("Getting chunk...");
            chunkProps cProps = new chunkProps
            {
                action = "getChunk",
                token  = client.tokenID,
                taskId = inTask
            };

            jsC.debugFlag     = debugFlag;
            jsC.connectURL    = client.connectURL;
            primaryCracked    = new List <string> {
            };
            hcClass.debugFlag = debugFlag;

            string jsonString = jsC.toJson(cProps);
            string ret        = jsC.jsonSend(jsonString);


            if (jsC.isJsonSuccess(ret))
            {
                string status = jsC.getRetVar(ret, "status");


                string argBuilder   = attackcmd;
                string attackcmdMod = " " + cmdpars + " ";
                string actualHLpath = Path.Combine(hashpath, hashlistID.ToString());
                switch (status)
                {
                case "OK":
                    attackcmdMod  = " " + cmdpars + " ";                                           //Reset the argument string
                    attackcmdMod += attackcmd.Replace(hashlistAlias, "\"" + actualHLpath + "\" "); //Add the path to Hashlist

                    attackcmdMod = convertToRelative(attackcmdMod);

                    attackcmdMod += " --outfile-check-dir=\"" + zapPath + hashlistID.ToString() + "\" ";     //Add the zap path to the commands

                    hcClass.setArgs(attackcmdMod);

                    chunkNo = Convert.ToInt64(jsC.getRetVar(ret, "chunkId"));
                    skip    = Convert.ToInt64(jsC.getRetVar(ret, "skip"));
                    length  = Convert.ToInt64(jsC.getRetVar(ret, "length"));

                    List <Packets> uploadPackets = new List <Packets>();

                    hcClass.setDirs(appPath);
                    hcClass.setPassthrough(ref uploadPackets, ref packetLock, debugFlag);

                    Thread thread = new Thread(() => threadPeriodicUpdate(ref uploadPackets, ref packetLock));
                    thread.Start();                                                             //Start our thread to monitor the upload queue

                    hcClass.startAttack(chunkNo, taskID, skip, length, statusTimer, tasksPath); //Start the hashcat binary
                    thread.Join();

                    return(1);

                case "keyspace_required":
                    hcClass.setDirs(appPath);
                    attackcmdMod  = " " + cmdpars + " ";                  //Reset the argument string
                    attackcmdMod += attackcmd.Replace(hashlistAlias, ""); //Remove out the #HL#

                    attackcmdMod = convertToRelative(attackcmdMod);

                    hcClass.setArgs(attackcmdMod);
                    long calcKeyspace = 0;

                    if (!hcClass.runKeyspace(ref calcKeyspace))
                    {
                        Console.WriteLine("Keyspace measuring was unsuccessful, check all files are present");
                        return(0);
                    }


                    if (calcKeyspace == 0)
                    {
                        errorProps eProps = new errorProps
                        {
                            token   = client.tokenID,
                            taskId  = taskID,
                            message = "Invalid keyspace, keyspace probably too small for this hashtype"
                        };
                        jsonString = jsC.toJson(eProps);
                        ret        = jsC.jsonSend(jsonString);
                        return(0);
                    }
                    else
                    {
                        keyspaceProps kProps = new keyspaceProps
                        {
                            token    = client.tokenID,
                            taskId   = taskID,
                            keyspace = calcKeyspace
                        };
                        jsonString = jsC.toJson(kProps);
                        ret        = jsC.jsonSend(jsonString);
                    }

                    return(2);

                case "fully_dispatched":
                    return(0);

                case "benchmark":
                    hcClass.setDirs(appPath);
                    attackcmdMod  = " " + cmdpars + " ";                                          //Reset the argument string
                    attackcmdMod += attackcmd.Replace(hashlistAlias, "\"" + actualHLpath + "\""); //Add the path to Hashlist

                    attackcmdMod = convertToRelative(attackcmdMod);

                    hcClass.setArgs(attackcmdMod);

                    Dictionary <string, double> collection = new Dictionary <string, double>();   //Holds all the returned benchmark values1

                    if (!hcClass.runBenchmark(benchMethod, benchTime, ref collection, legacy))
                    {
                        Console.WriteLine("Benchmark error, perhaps hashlist is empty");
                        errorProps eProps = new errorProps
                        {
                            token   = client.tokenID,
                            taskId  = taskID,
                            message = "Client received an invalid hashlist for benchmark"
                        };
                        jsonString = jsC.toJson(eProps);
                        ret        = jsC.jsonSend(jsonString);

                        return(0);
                    }

                    benchProps bProps = new benchProps
                    {
                        token  = client.tokenID,
                        taskId = taskID,
                    };

                    try
                    {
                        if (benchMethod == 1)     //Old benchmark method using actual run
                        {
                            bProps.type   = "run";
                            bProps.result = collection["PROGRESS_REJ"].ToString("0." + new string('#', 100));
                        }
                        else     //New benchmark method using --speed param
                        {
                            bProps.type   = "speed";
                            bProps.result = collection["LEFT_TOTAL"].ToString() + ":" + collection["RIGHT_TOTAL"].ToString();
                        }
                    }
                    catch
                    {
                        Console.WriteLine("Benchmark was unsuccessful, check all files are present");
                        return(0);
                    }



                    jsonString = jsC.toJson(bProps);
                    ret        = jsC.jsonSend(jsonString);
                    if (!jsC.isJsonSuccess(ret))
                    {
                        Console.WriteLine("Server rejected benchmark");
                        Console.WriteLine("Check the hashlist was downloaded correctly");
                        return(0);
                    }
                    return(3);
                }
            }
            return(0);
        }