Beispiel #1
0
        /// <summary>
        /// Convert XML string into RunScanResult object
        /// </summary>
        /// <param name="response">xml string</param>
        /// <returns></returns>
        public static RunScanResult ParseRunScanResult(string response)
        {
            RunScanResult r = new RunScanResult();

            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(response);

                foreach (XmlNode xmlNode in xmlDoc.ChildNodes[1].ChildNodes)
                {
                    switch (xmlNode.Name)
                    {
                    case "IsSuccesfull": r.IsSuccesfull = bool.Parse(xmlNode.InnerText); break;

                    case "ReturnValue": r.ScanId = xmlNode.InnerText; break;
                    }
                }

                xmlDoc = null;
            }
            catch (Exception ex)
            {
                Common.Logger.Create().Error(ex.ToString());
            }

            return(r);
        }
Beispiel #2
0
        private RunScanResult RunScan(BackgroundWorkerHelper bg, CxWebServiceClient client, ConfigurationResult configuration, byte[] zippedProject)
        {
            RunScanResult runScanResult = null;

            bg.DoWorkFunc = delegate(object obj)
            {
                ProjectSettings projectSettings = new ProjectSettings();
                projectSettings.AssociatedGroupID   = _scan.UploadSettings.Team.ToString();
                projectSettings.PresetID            = _scan.UploadSettings.Preset;
                projectSettings.ProjectName         = _scan.UploadSettings.ProjectName;
                projectSettings.ScanConfigurationID = configuration.FirstConfigurationKey;
                LocalCodeContainer localCodeContainer = new LocalCodeContainer();
                localCodeContainer.FileName   = "zipCxViewer";
                localCodeContainer.ZippedFile = zippedProject;
                try
                {
                    CxWSResponseRunID cxWSResponseRunID;
                    if (_scan.IsIncremental)
                    {
                        cxWSResponseRunID = client.ServiceClient.RunIncrementalScan(
                            _scan.LoginResult.SessionId,
                            projectSettings,
                            localCodeContainer
                            , _scan.UploadSettings.IsPublic, _scan.IsPublic
                            );
                    }
                    else
                    {
                        cxWSResponseRunID = client.ServiceClient.CreateAndRunProject(
                            _scan.LoginResult.SessionId,
                            projectSettings,
                            localCodeContainer
                            , _scan.UploadSettings.IsPublic, _scan.IsPublic
                            );
                    }


                    runScanResult = new RunScanResult();
                    runScanResult.IsSuccesfull = cxWSResponseRunID.IsSuccesfull;
                    runScanResult.ScanId       = cxWSResponseRunID.RunId;//Server actually returns the scanId which is a long number (and not the runID)
                    _scan.LoginResult.AuthenticationData.UnboundRunID = cxWSResponseRunID.RunId;
                    runScanResult.ProjectId = cxWSResponseRunID.ProjectID;
                    _scan.RunScanResult     = runScanResult;
                    if (!cxWSResponseRunID.IsSuccesfull)
                    {
                        TopMostMessageBox.Show(string.Format("Scan Error: {0}", cxWSResponseRunID.ErrorMessage), "Scanning Error", MessageBoxButtons.OK);
                    }
                    else
                    {
                        LoginHelper.Save(_scan.LoginResult.AuthenticationData);
                    }
                }
                catch (Exception err)
                {
                    Logger.Create().Error(err.ToString());

                    TopMostMessageBox.Show(string.Format("Scan Error: {0}", err.Message), "Scanning Error", MessageBoxButtons.OK);
                }
            };

            if (!bg.DoWork("Upload project zipped source for scanning..."))
            {
                return(null);
            }

            return(runScanResult);
        }
Beispiel #3
0
        private RunScanResult RunBoundedProjectScan(Scan scan, BackgroundWorkerHelper bg, CxWebServiceClient client, byte[] zippedProject)
        {
            RunScanResult runScanResult = null;

            bg.DoWorkFunc = delegate(object obj)
            {
                ProjectSettings projectSettings = new ProjectSettings();
                projectSettings.projectID = CommonData.ProjectId;
                LocalCodeContainer localCodeContainer = new LocalCodeContainer();
                localCodeContainer.FileName   = "zipCxViewer";
                localCodeContainer.ZippedFile = zippedProject;
                try
                {
                    CxWSResponseRunID cxWSResponseRunID;

                    if (_scan.IsIncremental)
                    {
                        cxWSResponseRunID = client.ServiceClient.RunIncrementalScan(
                            scan.LoginResult.SessionId,
                            projectSettings,
                            localCodeContainer
                            , CommonData.IsProjectPublic,
                            scan.IsPublic
                            );
                    }
                    else
                    {
                        cxWSResponseRunID = client.ServiceClient.RunScanAndAddToProject(
                            scan.LoginResult.SessionId,
                            projectSettings,
                            localCodeContainer
                            , CommonData.IsProjectPublic,
                            scan.IsPublic
                            );
                    }

                    runScanResult = new RunScanResult();                  // RunScanResult.FromXml(scanZipedSource);
                    runScanResult.IsSuccesfull = cxWSResponseRunID.IsSuccesfull;
                    runScanResult.ScanId       = cxWSResponseRunID.RunId; //Server actually returns the scanId which is a long number (and not the runID)
                    scan.LoginResult.AuthenticationData.UnboundRunID = cxWSResponseRunID.RunId;
                    CommonData.ProjectId = cxWSResponseRunID.ProjectID;
                    _scan.RunScanResult  = runScanResult;
                    if (!cxWSResponseRunID.IsSuccesfull)
                    {
                        TopMostMessageBox.Show(string.Format("Scan Error: {0}", cxWSResponseRunID.ErrorMessage), "Scanning Error", MessageBoxButtons.OK);
                    }
                    else
                    {
                        LoginHelper.Save(scan.LoginResult.AuthenticationData);
                    }
                }
                catch (Exception err)
                {
                    Logger.Create().Error(err.ToString());
                    TopMostMessageBox.Show(string.Format("Scan Error: {0}", err.Message), "Scanning Error", MessageBoxButtons.OK);
                }
            };
            if (!bg.DoWork("Upload project zipped source for scanning..."))
            {
                return(null);
            }

            return(runScanResult);
        }
Beispiel #4
0
        /// <summary>
        /// Execute scan
        /// </summary>
        /// <param name="project">Upload project folder</param>
        /// <param name="scanData"></param>
        /// <param name="scanId"></param>
        /// <returns></returns>
        private ProjectScanStatuses ExecuteScan(Project project, ref CxWSQueryVulnerabilityData[] scanData, ref long scanId)
        {
            Logger.Create().Debug("DoScan in");
            bool bCancel        = false;
            bool backgroundMode = _scan.LoginResult.AuthenticationData.IsRunScanInBackground == SimpleDecision.Yes;

            if (_dispatcher == null)
            {
                _dispatcher = ServiceLocators.ServiceLocator.GetDispatcher();
            }

            if (_dispatcher != null)
            {
                IScanView view    = null;
                var       waitEnd = new ManualResetEvent(false);

                //if was selected "always run in background" checkbox - hide dialog
                if (!backgroundMode)
                {
                    ICommandResult commandResult = _dispatcher.Dispatch(_scan);
                    view = ((ScanPresenter)commandResult).View;
                }

                _scan.ScanView = view;

                BackgroundWorkerHelper bg = new BackgroundWorkerHelper(_scan.LoginResult.AuthenticationData.ReconnectInterval * 1000, _scan.LoginResult.AuthenticationData.ReconnectCount);

                CxWebServiceClient client = new CxWebServiceClient(_scan.LoginResult.AuthenticationData);
                client.ServiceClient.Timeout = 1800000;

                bool isIISStoped    = false;
                bool isScanningEror = false;

                //User click cancel while info dialog was showed
                if (!bCancel)
                {
                    ShowScanProgressBar();

                    ConfigurationResult configuration = _configurationHelper.GetConfigurationList(_scan.LoginResult.SessionId, bg, client);

                    if (configuration == null)
                    {
                        _cancelPressed = true;
                    }

                    if (!configuration.IsSuccesfull)
                    {
                        LoginHelper.DoLogout();
                        if (client != null)
                        {
                            client.Close();
                        }
                        if (view != null)
                        {
                            view.CloseView();
                        }

                        _scan.InProcess = false;
                        return(ProjectScanStatuses.CanceledByUser);
                    }

                    //User click cancel while info dialog was showed
                    if (!bCancel)
                    {
                        byte[] zippedProject = ZipProject(_scan, project, bg);

                        if (!_scan.IsCancelPressed && zippedProject != null)
                        {
                            if (configuration.Configurations.Count > 0)
                            {
                                RunScanResult runScanResult = null;

                                if (!CommonData.IsProjectBound)
                                {
                                    if (_uploadSettings.IsPublic)
                                    {
                                        _scan.IsPublic = SetScanPrivacy();
                                    }

                                    runScanResult = RunScan(bg, client, configuration, zippedProject);
                                }
                                else
                                {
                                    if (_scan.UploadSettings.IsPublic)
                                    {
                                        _scan.IsPublic = SetScanPrivacy();
                                    }

                                    runScanResult = RunBoundedProjectScan(_scan, bg, client, zippedProject);
                                }

                                if (runScanResult == null || !runScanResult.IsSuccesfull)
                                {
                                    bCancel        = true;
                                    isIISStoped    = true;
                                    isScanningEror = true;
                                }

                                // Continue if project uploaded succesfull and cancel button while process wasn't pressed
                                if (runScanResult != null && runScanResult.IsSuccesfull)
                                {
                                    _scan.RunScanResult = runScanResult;

                                    //perform scan work in separated thread to improve UI responsibility
                                    System.Threading.ThreadPool.QueueUserWorkItem(delegate(object stateInfo)
                                    {
                                        try
                                        {
                                            // Wait while scan operation complete
                                            while (true)
                                            {
                                                StatusScanResult statusScan = UpdateScanStatus(ref bCancel, backgroundMode, view, bg, client, ref isIISStoped);

                                                // if scan complete with sucess or failure or cancel button was pressed
                                                // operation complete
                                                bCancel = bCancel ? bCancel : _scan.WaitForCancel();

                                                if (isIISStoped || bCancel ||
                                                    (statusScan != null && statusScan.RunStatus == CurrentStatusEnum.Finished) ||
                                                    (statusScan != null && statusScan.RunStatus == CurrentStatusEnum.Failed))
                                                {
                                                    break;
                                                }
                                            }

                                            waitEnd.Set();
                                        }
                                        catch (Exception err)
                                        {
                                            Logger.Create().Error(err.ToString());
                                            // show error
                                            waitEnd.Set();
                                            isIISStoped = true;
                                            Logger.Create().Debug(err);
                                        }

                                        if (_scan.ScanView == null || _scan.ScanView.Visibility == false)
                                        {
                                            var scanStatusBar = new ScanStatusBar(false, "", 0, 0, true);

                                            CommonActionsInstance.getInstance().UpdateScanProgress(scanStatusBar);

                                            //ObserversManager.Instance.Publish(typeof (ScanStatusBar), scanStatusBar);
                                        }
                                    });

                                    while (!waitEnd.WaitOne(0, false))
                                    {
                                        Application.DoEvents();
                                        Thread.Sleep(10);
                                    }
                                }
                            }

                            #region [Scan completed. Open perspective]

                            if (!bCancel && !isIISStoped)
                            {
                                ShowScanData(ref scanData, ref scanId, client);
                            }
                            else
                            {
                                #region [Stop scan in cancel pressed]
                                if (_scan.RunScanResult != null && !isIISStoped)
                                {
                                    bg.DoWorkFunc = delegate
                                    {
                                        if (!isIISStoped)
                                        {
                                            client.ServiceClient.CancelScan(_scan.LoginResult.SessionId, _scan.RunScanResult.ScanId);
                                        }
                                    };
                                    bg.DoWork("Stop scan...");
                                }
                                #endregion
                            }

                            #endregion

                            client.Close();
                        }
                        else
                        {
                            client.Close();
                            bCancel = true;
                        }
                    }
                    else
                    {
                    }
                }
                else
                {
                }
                if (!backgroundMode && view != null)
                {
                    view.CloseView();
                }

                if (isIISStoped)
                {
                    if (isScanningEror)
                    {
                        return(ProjectScanStatuses.Error);
                    }
                    else
                    {
                        return(ProjectScanStatuses.CanceledByUser);
                    }
                }

                if (!bCancel)
                {
                    return(ProjectScanStatuses.Success);
                }
                else
                {
                    if (isScanningEror)
                    {
                        return(ProjectScanStatuses.Error);
                    }
                    else
                    {
                        return(ProjectScanStatuses.CanceledByUser);
                    }
                }
            }

            return(ProjectScanStatuses.CanceledByUser);
        }