{"id":13843838,"url":"https://github.com/threatexpress/cs2modrewrite","last_synced_at":"2025-04-04T14:05:50.701Z","repository":{"id":37743143,"uuid":"93530412","full_name":"threatexpress/cs2modrewrite","owner":"threatexpress","description":"Convert Cobalt Strike profiles to modrewrite scripts","archived":false,"fork":false,"pushed_at":"2023-01-30T17:47:51.000Z","size":62,"stargazers_count":594,"open_issues_count":3,"forks_count":116,"subscribers_count":19,"default_branch":"master","last_synced_at":"2025-03-28T13:07:16.805Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/threatexpress.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2017-06-06T14:53:57.000Z","updated_at":"2025-03-25T09:17:40.000Z","dependencies_parsed_at":"2023-02-11T08:45:55.805Z","dependency_job_id":null,"html_url":"https://github.com/threatexpress/cs2modrewrite","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/threatexpress%2Fcs2modrewrite","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/threatexpress%2Fcs2modrewrite/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/threatexpress%2Fcs2modrewrite/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/threatexpress%2Fcs2modrewrite/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/threatexpress","download_url":"https://codeload.github.com/threatexpress/cs2modrewrite/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247190231,"owners_count":20898700,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-04T17:02:29.005Z","updated_at":"2025-04-04T14:05:50.682Z","avatar_url":"https://github.com/threatexpress.png","language":"Python","funding_links":[],"categories":["\u003ca id=\"e8d16b3301a2ab9109b503b89ec6eece\"\u003e\u003c/a\u003eCobalt Strike","Python","Python (1887)"],"sub_categories":["\u003ca id=\"f587c1f0baaf7aa1d9e9c5fe533c7a6d\"\u003e\u003c/a\u003e工具"],"readme":"# Automatically Generate Rulesets for Apache mod_rewrite or Nginx for Intelligent HTTP C2 Redirection\n\n![Python application](https://github.com/threatexpress/cs2modrewrite/workflows/Python%20application/badge.svg)\nThis project converts a Cobalt Strike profile to a functional mod_rewrite `.htaccess` or Nginx config file to support HTTP reverse proxy redirection to a Cobalt Strike teamserver. The use of reverse proxies provides protection to backend C2 servers from profiling, investigation, and general internet background radiation.\n\n***Note***: You should test and tune the output as needed before deploying, but these scripts should handle the heavy lifting.\n\n## Features\n\n- Now requires Python 3.0+\n- Supports the Cobalt Strike custom URI features as of CS 4.0\n- Rewrite Rules based on valid C2 URIs (HTTP GET, POST, and Stager) and specified User-Agent string.\n  - Result: Only requests to valid C2 endpoints with a specified UA string will be proxied to the teamserver by default.\n- Uses a custom Malleable C2 profile to build a .htaccess file with corresponding mod_rewrite rules\n- Uses a custom Malleable C2 profile to build a Nginx config with corresponding proxy_pass rules\n- HTTP or HTTPS proxying to the Cobalt Strike teamserver\n- HTTP 302 Redirection to a Legitimate Site for Non-Matching Requests\n\n## Quick start\n\nThe havex.profile example is included for a quick test.\n\n1) Run the script against a profile\n2) Save the output to `.htaccess` or `/etc/nginx/nginx.conf` on your redirector\n3) Modify as needed\n4) Reload\\restart the web server\n\n## Apache mod_rewrite Example Usage using a remote include file\n\n```\npython3 cs2modrewrite.py -i havex.profile -c https://TEAMSERVER -r https://GOHERE -o /etc/apache2/redirect.rules\n```\n\nExample Apache Config\n\n```\n\u003cVirtualHost *:80\u003e\n    ServerAdmin webmaster@localhost\n    DocumentRoot /var/www/html\n    RemoteIPHeader X-Forwarded-For\n\n    ErrorLog /var/log/apache2/redirector_error.log\n    CustomLog /var/log/apache2/redirector_access.log combined\n    ErrorDocument 401 \" \"\n    ErrorDocument 403 \" \"\n    ErrorDocument 404 \" \"\n    ErrorDocument 500 \" \"\n    ErrorDocument 503 \" \"\n\n    # Include redirect.rules\n    Include /etc/apache2/redirect.rules\n\u003c/VirtualHost\u003e\n```\n\nConsider Updating Apache Server Header, ServerTokens, and logging with something like the following.\n\n```\n## Update Apached Server Header, ServerTokens, and logging\necho \"Update Update Apached Server Header, ServerTokens, and logging\"\nsed -i -e 's/\\(ServerTokens\\s\\+\\)OS/\\1Prod/g' /etc/apache2/conf-enabled/security.conf\nsed -i -e 's/\\(ServerSignature\\s\\+\\)On/\\1Off/g' /etc/apache2/conf-enabled/security.conf\necho \"SecServerSignature Server\" \u003e\u003e /etc/apache2/conf-enabled/security.conf\necho \"LogLevel alert rewrite:trace2\" \u003e\u003e /etc/apache2/conf-enabled/security.conf\n\n## Update Apached remoteip.conf\necho \"Update Apached remoteip.conf\"\necho \"RemoteIPHeader X-Forwarded-For\" \u003e\u003e /etc/apache2/conf-enabled/remoteip.conf\n\n## Restart apache server\necho \"Restart apache server\"\nsystemctl restart apache2\n```\n\n## Apache mod_rewrite Example Usage using a .htaccess file\n\n```\npython3 cs2modrewrite.py -i havex.profile -c https://TEAMSERVER -r https://GOHERE -o /var/www/html/.htaccess\n```\n\n## Apache Rewrite Setup and Tips\n\n### Enable Rewrite and Proxy\n\n    apt-get install apache2\n    a2enmod rewrite headers proxy proxy_http ssl cache\n    a2dismod -f deflate\n    service apache2 reload\n\n**Note:** https://bluescreenofjeff.com/2016-06-28-cobalt-strike-http-c2-redirectors-with-apache-mod_rewrite/\n\"e0x70i pointed out in the comments below that if your Cobalt Strike Malleable C2 profile contains an Accept-Encoding header for gzip, your Apache install may compress that traffic by default and cause your Beacon to be unresponsive or function incorrectly. To overcome this, disable mod_deflate (via a2dismod deflate and add the No Encode ([NE]) flag to your rewrite rules. (Thank you, e0x70i!)\"\n\n### Enable SSL support\n\nEnsure the following entries are in the site's config (i.e. `/etc/apache2/available-sites/*.conf`)\n\n    # Enable SSL\n    SSLEngine On\n    # Enable SSL Proxy\n    SSLProxyEngine On\n    # Trust Self-Signed Certificates generated by CobaltStrike\n    SSLProxyVerify none\n    SSLProxyCheckPeerCN off\n    SSLProxyCheckPeerName off\n    SSLProxyCheckPeerExpire off\n### .HTACCESS\n\nIf you plan on using mod_rewrite in .htaccess files (instead of the site's config file), you also need to enable the use of `.htaccess` files by changing `AllowOverride None` to `AllowOverride All`. For all websites, edit `/etc/apache2/apache.conf`\n\n    \u003cDirectory /var/www/\u003e\n        Options FollowSymLinks MultiViews\n        AllowOverride All\n        Order allow,deny\n        allow from all\n    \u003c/Directory\u003e\n\nFinally, restart apache once more for good measure.\n\n`service apache2 restart`\n\n### Troubleshooting\n\nIf you need to troubleshoot redirection rule behavior, enable detailed error tracing in your site's configuration file by adding the following line.\n\n`LogLevel alert rewrite:trace5`\n\nNext, reload apache, and monitor `/var/log/access.log` `/var/log/error.log` to see which rules are matching.\n\n----------------------------------------------\n\n## Nginx Example Usage\n\n### Install Nginx\n\n    apt-get install nginx nginx-extras\n\n**Note:** `nginx-extras` is needed for custom server headers. If you can't get this package, then comment out the server header line in the resulting configuration file.\n\n### Create Redirection Rules\n\nSave the cs2nginx.py output to `/etc/nginx/nginx.conf` and modify as needed (SSL parameters).\n\n`python3 ./cs2nginx.py -i havex.profile -c https://127.0.0.1 -r https://www.google.com -H mydomain.local \u003e/etc/nginx/nginx.conf`\n\nFinally, restart nginx after modifying the server configuration file.\n\n`service nginx restart`\n\n## Final Thoughts\n\nOnce redirection is configured and functioning, ensure your C2 servers only allow ingress from the redirector and your trusted IPs (VPN, office ranges, etc).\n\nConsider adding additional redirector protections using GeoIP restrictions (mod_maxmind) and blacklists of bad user agents and IP ranges. Thanks to [@curi0usJack](https://twitter.com/curi0usJack) for the ideas.\n\n## References\n\n- [Joe Vest and Andrew Chiles - cs2modrewrite.py blog post](https://posts.specterops.io/automating-apache-mod-rewrite-and-cobalt-strike-malleable-c2-profiles-d45266ca642)\n\n- [@bluescreenofjeff - Cobalt Strike HTTP C2 Redirectors with Apache mod_rewrite](https://bluescreenofjeff.com/2016-06-28-cobalt-strike-http-c2-redirectors-with-apache-mod_rewrite/)\n\n- [Adam Brown - Resilient Red Team HTTPS Redirection Using Nginx](https://coffeegist.com/security/resilient-red-team-https-redirection-using-nginx/)\n\n- [Apache - Apache mod_rewrite Documentation](http://httpd.apache.org/docs/current/mod/mod_rewrite.html)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthreatexpress%2Fcs2modrewrite","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthreatexpress%2Fcs2modrewrite","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthreatexpress%2Fcs2modrewrite/lists"}