| 1 | + | namespace ES.Taipan.Inspector.AddOns.WebFormBruteforcer |
| 2 | + | |
| 3 | + | open System |
| 4 | + | open System.Collections.Generic |
| 5 | + | open ES.Taipan.Inspector |
| 6 | + | open ES.Taipan.Inspector.AddOns |
| 7 | + | open ES.Taipan.Infrastructure.Service |
| 8 | + | open ES.Taipan.Infrastructure.Text |
| 9 | + | open ES.Taipan.Infrastructure.Network |
| 10 | + | open ES.Taipan.Infrastructure.Messaging |
| 11 | + | open ES.Fslog |
| 12 | + | open System.Collections.Concurrent |
| 13 | + | open ES.Taipan.Infrastructure.Threading |
| 14 | + | open ES.Taipan.Infrastructure.Text |
| 15 | + | open ES.Taipan.Crawler |
| 16 | + | open DiffLib |
| 17 | + | open System.Threading.Tasks |
| 18 | + | open System.Linq |
| 19 | + | open System.Threading |
| 20 | + | |
| 21 | + | type WebFormBruteforcerAddOn() as this = |
| 22 | + | inherit BaseStatelessAddOn("Web Form Bruteforcer AddOn", string WebFormBruteforcerAddOn.Id, 1) |
| 23 | + | let _progressIndexes = new Dictionary<String, String * Int32 * Int32 * Int32>() |
| 24 | + | |
| 25 | + | let _numOfConcurrentTasks = 5 |
| 26 | + | let _analyzedPages = new HashSet<String>() |
| 27 | + | let _scanLock = new Object() |
| 28 | + | let _testRequests = new BlockingCollection<TestRequest>() |
| 29 | + | |
| 30 | + | let mutable _taskManager: TaskManager option = None |
| 31 | + | let mutable _usernames = List.empty<String> |
| 32 | + | let mutable _passwords = List.empty<String> |
| 33 | + | let mutable _combinations = List.empty<String * String> |
| 34 | + | |
| 35 | + | let _logger = |
| 36 | + | log "WebFormBruteforcerAddOn" |
| 37 | + | |> info "BruteforceUsername" "Start to identify password for username: {0}" |
| 38 | + | |> info "BruteforceOnlyPasswords" "No suitable username input field found, bruteforce only password fields" |
| 39 | + | |> info "UpdateStatus" "Bruteforce of {0}, username {1} at {2}% [{3}/{4}]" |
| 40 | + | |> info "TestForCombination" "Test for username/password combination on directory: {0}" |
| 41 | + | |> build |
| 42 | + | |
| 43 | + | let reportSecurityIssue(username: String, password: String, webRequest: WebRequest, webResponse: WebResponse) = |
| 44 | + | let securityIssue = |
| 45 | + | new SecurityIssue( |
| 46 | + | WebFormBruteforcerAddOn.Id, |
| 47 | + | Name = "Weak Web Form Credentials", |
| 48 | + | Uri = webRequest.HttpRequest.Uri, |
| 49 | + | EntryPoint = EntryPoint.Header, |
| 50 | + | Note = String.Format("Account {0}:{1}", username, password) |
| 51 | + | ) |
| 52 | + | |
| 53 | + | securityIssue.Transactions.Add(webRequest, webResponse) |
| 54 | + | securityIssue.Details.Properties.Add("Username", username) |
| 55 | + | securityIssue.Details.Properties.Add("Password", password) |
| 56 | + | this.Context.Value.AddSecurityIssue(securityIssue) |
| 57 | + | |
| 58 | + | let initLogStatus(index: String, username: String, totalReq: Int32) = |
| 59 | + | lock _progressIndexes (fun _ -> _progressIndexes.[index] <- (username, 0, totalReq, 0)) |
| 60 | + | |
| 61 | + | let logStatus(index: String, forcePrint: Boolean) = |
| 62 | + | lock _progressIndexes (fun _ -> |
| 63 | + | if _progressIndexes.ContainsKey(index) then |
| 64 | + | let (username, currentIndex, totalCount, lastPercentage) = _progressIndexes.[index] |
| 65 | + | let percentage = (float currentIndex / float totalCount) * 100. |> int32 |
| 66 | + | _progressIndexes.[index] <- (username, currentIndex+1, totalCount, lastPercentage) |
| 67 | + | |
| 68 | + | if forcePrint || lastPercentage < percentage && percentage % 5 = 0 then |
| 69 | + | _progressIndexes.[index] <- (username, currentIndex, totalCount, percentage) |
| 70 | + | _logger?UpdateStatus(index, username, percentage, currentIndex, totalCount) |
| 71 | + | ) |
| 72 | + | |
| 73 | + | let testUsernameAndPassword(testRequest: TestRequest, usernameInputs: String list, passwordInputs: String list, username: String, password: String, resultVerifier: WebResponse -> Boolean, serviceStateController: ServiceStateController)= |
| 74 | + | let mutable isVulnerable = false |
| 75 | + | if not serviceStateController.IsStopped then |
| 76 | + | serviceStateController.WaitIfPauseRequested() |
| 77 | + | let (sentWebRequest, receivedWebResponse) = |
| 78 | + | BruteforceHelper.sendProbe( |
| 79 | + | testRequest, |
| 80 | + | usernameInputs, |
| 81 | + | passwordInputs, |
| 82 | + | username, |
| 83 | + | password, |
| 84 | + | this.WebRequestor.Value |
| 85 | + | ) |
| 86 | + | |
| 87 | + | // verify test |
| 88 | + | if resultVerifier(receivedWebResponse) then |
| 89 | + | reportSecurityIssue(username, password, sentWebRequest, receivedWebResponse) |
| 90 | + | isVulnerable <- true |
| 91 | + | isVulnerable |
| 92 | + | |
| 93 | + | let bruteforceUriWithCombinations(testRequest: TestRequest, usernameInputs: String list, passwordInputs: String list, resultVerifier: WebResponse -> Boolean, serviceStateController: ServiceStateController) = |
| 94 | + | if not usernameInputs.IsEmpty then |
| 95 | + | _logger?TestForCombination(testRequest.WebRequest.HttpRequest.Uri.AbsolutePath) |
| 96 | + | _combinations |
| 97 | + | |> List.exists(fun (username, password) -> |
| 98 | + | testUsernameAndPassword( |
| 99 | + | testRequest, |
| 100 | + | usernameInputs, |
| 101 | + | passwordInputs, |
| 102 | + | username, |
| 103 | + | password, |
| 104 | + | resultVerifier, |
| 105 | + | serviceStateController |
| 106 | + | ) |
| 107 | + | ) |
| 108 | + | else |
| 109 | + | false |
| 110 | + | |
| 111 | + | let getTaskManager(serviceStateController: ServiceStateController) = |
| 112 | + | match _taskManager with |
| 113 | + | | None -> |
| 114 | + | _taskManager <- Some <| new TaskManager(serviceStateController, true, false, _numOfConcurrentTasks) |
| 115 | + | _taskManager.Value |
| 116 | + | | Some tm -> tm |
| 117 | + | |
| 118 | + | let bruteforcePasswordList(testRequest: TestRequest, username: String, usernameInputs: String list, passwordInputs: String list, resultVerifier: WebResponse -> Boolean, serviceStateController: ServiceStateController) = |
| 119 | + | // run in parallels all the instantiated workers |
| 120 | + | let queue = new BlockingCollection<String>() |
| 121 | + | let tasks = new List<Task>() |
| 122 | + | let taskManager = getTaskManager(serviceStateController) |
| 123 | + | let passwordFound = ref 0 |
| 124 | + | for _ in Enumerable.Range(0, 10) do |
| 125 | + | if not serviceStateController.IsStopped then |
| 126 | + | serviceStateController.WaitIfPauseRequested() |
| 127 | + | |
| 128 | + | // run the parallel task |
| 129 | + | taskManager.RunTask(fun serviceStateController -> |
| 130 | + | for password in queue.GetConsumingEnumerable() do |
| 131 | + | if not serviceStateController.IsStopped then |
| 132 | + | serviceStateController.WaitIfPauseRequested() |
| 133 | + | |
| 134 | + | // check if the password was already found for this username |
| 135 | + | if Interlocked.CompareExchange(passwordFound, 1, 1) = 0 then |
| 136 | + | |
| 137 | + | // bruteforce password |
| 138 | + | logStatus(testRequest.WebRequest.HttpRequest.Uri.AbsolutePath, false) |
| 139 | + | let pwdFoundTmp = |
| 140 | + | testUsernameAndPassword( |
| 141 | + | testRequest, |
| 142 | + | usernameInputs, |
| 143 | + | passwordInputs, |
| 144 | + | username, |
| 145 | + | password, |
| 146 | + | resultVerifier, |
| 147 | + | serviceStateController |
| 148 | + | ) |
| 149 | + | |
| 150 | + | if pwdFoundTmp then |
| 151 | + | // password found set exit flag |
| 152 | + | Interlocked.Increment(passwordFound) |> ignore |
| 153 | + | , true) |> tasks.Add |
| 154 | + | |
| 155 | + | // add all password and set the queue to completed |
| 156 | + | _passwords |> List.iter(queue.Add) |
| 157 | + | queue.CompleteAdding() |
| 158 | + | |
| 159 | + | // wait for all task completed |
| 160 | + | let counter = ref 0 |
| 161 | + | while not(Task.WaitAll(tasks |> Seq.toArray, 1000)) do |
| 162 | + | incr counter |
| 163 | + | |
| 164 | + | let bruteforceUriWithUsernameAndPassword(testRequest: TestRequest, usernameInputs: String list, passwordInputs: String list, resultVerifier: WebResponse -> Boolean, serviceStateController: ServiceStateController) = |
| 165 | + | if not usernameInputs.IsEmpty then |
| 166 | + | // if the username list to use is empty just add an |
| 167 | + | // empty username to check for password only auth |
| 168 | + | if _usernames |> List.isEmpty |
| 169 | + | then [String.Empty] |
| 170 | + | else _usernames |
| 171 | + | |> List.iter(fun username -> |
| 172 | + | _logger?BruteforceUsername(username) |
| 173 | + | initLogStatus(testRequest.WebRequest.HttpRequest.Uri.AbsolutePath, username, _passwords.Length) |
| 174 | + | bruteforcePasswordList( |
| 175 | + | testRequest, |
| 176 | + | username, |
| 177 | + | usernameInputs, |
| 178 | + | passwordInputs, |
| 179 | + | resultVerifier, |
| 180 | + | serviceStateController |
| 181 | + | ) |
| 182 | + | ) |
| 183 | + | logStatus(testRequest.WebRequest.HttpRequest.Uri.AbsolutePath, true) |
| 184 | + | else |
| 185 | + | // No username in form so I'll just bruteforce password input. |
| 186 | + | // I'm sure there are password inputs otherwise I'll not be at this code point |
| 187 | + | _logger?BruteforceOnlyPasswords() |
| 188 | + | initLogStatus(testRequest.WebRequest.HttpRequest.Uri.AbsolutePath, "N/A", _passwords.Length) |
| 189 | + | bruteforcePasswordList( |
| 190 | + | testRequest, |
| 191 | + | "N/A", |
| 192 | + | usernameInputs, |
| 193 | + | passwordInputs, |
| 194 | + | resultVerifier, |
| 195 | + | serviceStateController |
| 196 | + | ) |
| 197 | + | logStatus(testRequest.WebRequest.HttpRequest.Uri.AbsolutePath, true) |
| 198 | + | |
| 199 | + | let resultVerifier(falseResponse: WebResponse) (testResponse: WebResponse) = |
| 200 | + | if falseResponse.HttpResponse.StatusCode = testResponse.HttpResponse.StatusCode then |
| 201 | + | if HttpUtility.isRedirect(falseResponse.HttpResponse.StatusCode) then |
| 202 | + | // if it is a relocation, check if the destinations are different |
| 203 | + | let templateLocation = HttpUtility.tryGetHeader("Location", falseResponse.HttpResponse.Headers) |
| 204 | + | let testLocation = HttpUtility.tryGetHeader("Location", testResponse.HttpResponse.Headers) |
| 205 | + | |
| 206 | + | match (templateLocation, testLocation) with |
| 207 | + | | (Some templateLocation, Some testLocation) -> |
| 208 | + | not <| templateLocation.Value.Equals(testLocation.Value, StringComparison.OrdinalIgnoreCase) |
| 209 | + | | _ -> |
| 210 | + | // one of the two response doesn't have Location header, pass found (may cause FPs) |
| 211 | + | true |
| 212 | + | else |
| 213 | + | // check change on HTML page |
| 214 | + | let ratio = TextUtility.computeDifferenceRatio(falseResponse.HttpResponse.Html, testResponse.HttpResponse.Html) |
| 215 | + | ratio < 0.80 |
| 216 | + | else |
| 217 | + | true |
| 218 | + | |
| 219 | + | let bruteforceWoker(serviceStateController: ServiceStateController) = |
| 220 | + | for testRequest in _testRequests.GetConsumingEnumerable() do |
| 221 | + | let (usernameInputs, passwordInputs) = BruteforceHelper.getUsernameandPasswordInputs(testRequest) |
| 222 | + | |
| 223 | + | // send a not valid username and password to identify form behaviour |
| 224 | + | let (_, webResponse) = |
| 225 | + | BruteforceHelper.sendProbe( |
| 226 | + | testRequest, |
| 227 | + | usernameInputs, |
| 228 | + | passwordInputs, |
| 229 | + | Guid.NewGuid().ToString("N"), |
| 230 | + | Guid.NewGuid().ToString("N"), |
| 231 | + | this.WebRequestor.Value |
| 232 | + | ) |
| 233 | + | |
| 234 | + | // run the bruteforce |
| 235 | + | let bruteforceWithCombinationResult = |
| 236 | + | bruteforceUriWithCombinations( |
| 237 | + | testRequest, |
| 238 | + | usernameInputs, |
| 239 | + | passwordInputs, |
| 240 | + | resultVerifier webResponse, |
| 241 | + | serviceStateController |
| 242 | + | ) |
| 243 | + | |
| 244 | + | if not bruteforceWithCombinationResult && not _passwords.IsEmpty then |
| 245 | + | // combination didn't give any result, try an exhaustive approach |
| 246 | + | bruteforceUriWithUsernameAndPassword( |
| 247 | + | testRequest, |
| 248 | + | usernameInputs, |
| 249 | + | passwordInputs, |
| 250 | + | resultVerifier webResponse, |
| 251 | + | serviceStateController |
| 252 | + | ) |
| 253 | + | |
| 254 | + | let runAllWorkers(taskManager: TaskManager) = |
| 255 | + | for i=0 to _numOfConcurrentTasks-1 do |
| 256 | + | taskManager.RunTask(fun serviceStateController -> |
| 257 | + | bruteforceWoker(serviceStateController) |
| 258 | + | , true) |> ignore |
| 259 | + | |
| 260 | + | let bruteforcePage(testRequest: TestRequest, taskManager: TaskManager) = |
| 261 | + | if taskManager.Count() = 0 then |
| 262 | + | // no worker running, instantiace all workers |
| 263 | + | runAllWorkers(taskManager) |
| 264 | + | _testRequests.Add(testRequest) |
| 265 | + | |
| 266 | + | let completePasswordList() = |
| 267 | + | // all the usernames also as passowrd and the empty string |
| 268 | + | _passwords <- String.Empty::_usernames@_passwords |> List.distinct |
| 269 | + | |
| 270 | + | let hasWebFormAuthentication(testRequest: TestRequest) = |
| 271 | + | if testRequest.RequestType = TestRequestType.CrawledPage then |
| 272 | + | let webLink = testRequest.GetData<WebLink>() |
| 273 | + | let inputs = RegexUtility.getAllHtmlTagsWithName(webLink.ParsedHtmlCode, "input") |
| 274 | + | inputs |
| 275 | + | |> Seq.exists(fun input -> |
| 276 | + | RegexUtility |
| 277 | + | .getHtmlInputValue(input, "type") |
| 278 | + | .Equals("password", StringComparison.OrdinalIgnoreCase) |
| 279 | + | ) |
| 280 | + | else |
| 281 | + | false |
| 282 | + | |
| 283 | + | static member Id = Guid.Parse("65B5E32A-D952-4A51-93FC-B1A97B590886") |
| 284 | + | override this.IsBackgroundService with get() = true |
| 285 | + | |
| 286 | + | default this.Initialize(context: Context, webRequestor: IWebPageRequestor, messageBroker: IMessageBroker, logProvider: ILogProvider) = |
| 287 | + | let initResult = base.Initialize(context, webRequestor, messageBroker, logProvider) |
| 288 | + | logProvider.AddLogSourceToLoggers(_logger) |
| 289 | + | |
| 290 | + | webRequestor.HttpRequestor.Settings.AllowAutoRedirect <- false |
| 291 | + | _usernames <- defaultArg (this.Context.Value.AddOnStorage.ReadProperty<List<String>>("Usernames")) (new List<String>()) |> Seq.distinct |> Seq.toList |
| 292 | + | _passwords <- defaultArg (this.Context.Value.AddOnStorage.ReadProperty<List<String>>("Passwords")) (new List<String>()) |> Seq.distinct |> Seq.toList |
| 293 | + | _combinations <- defaultArg (this.Context.Value.AddOnStorage.ReadProperty<List<String * String>>("Combinations")) (new List<String * String>()) |> Seq.distinct |> Seq.toList |
| 294 | + | completePasswordList() |
| 295 | + | initResult |
| 296 | + | |
| 297 | + | override this.RunToCompletation(stateController: ServiceStateController) = |
| 298 | + | _testRequests.CompleteAdding() |
| 299 | + | let taskManager = getTaskManager(stateController) |
| 300 | + | while not <| taskManager.AreAllTaskCompleted() do |
| 301 | + | Async.Sleep(1000) |> Async.RunSynchronously |
| 302 | + | |
| 303 | + | default this.Scan(testRequest: TestRequest, stateController: ServiceStateController) = |
| 304 | + | if hasWebFormAuthentication(testRequest) then |
| 305 | + | lock _scanLock (fun _ -> |
| 306 | + | if _analyzedPages.Add(testRequest.WebRequest.HttpRequest.Uri.AbsolutePath) then |
| 307 | + | let taskManager = getTaskManager(stateController) |
| 308 | + | bruteforcePage(testRequest, taskManager) |
| 309 | + | ) |
| 310 | + | |