From 617f4ef65cf0364726c76bf15f78ab062729ee3a Mon Sep 17 00:00:00 2001 From: George Allen Date: Fri, 12 May 2023 10:53:51 -0400 Subject: [PATCH] initial commit --- .ansible-lint | 130 ++++++++++ .dir-locals.el | 4 + .github/workflows/ansible-lint.yml | 20 ++ CODE_OF_CONDUCT.md | 46 ++++ LICENSE.txt | 22 ++ README.md | 111 ++++++++ defaults/main.yml | 41 +++ example-playbook.yml | 45 ++++ meta/main.yml | 38 +++ tasks/gather-art-tids.yml | 35 +++ tasks/invoke-atomictest.yml | 136 ++++++++++ tasks/main.yml | 35 +++ tasks/setup-linux.yml | 90 +++++++ tasks/setup-windows.yml | 49 ++++ vars/art-tids.yml | 395 +++++++++++++++++++++++++++++ vars/main.yml | 1 + vars/update-art-tids.sh | 20 ++ 17 files changed, 1218 insertions(+) create mode 100644 .ansible-lint create mode 100644 .dir-locals.el create mode 100644 .github/workflows/ansible-lint.yml create mode 100644 CODE_OF_CONDUCT.md create mode 100644 LICENSE.txt create mode 100644 defaults/main.yml create mode 100644 example-playbook.yml create mode 100644 meta/main.yml create mode 100644 tasks/gather-art-tids.yml create mode 100644 tasks/invoke-atomictest.yml create mode 100644 tasks/main.yml create mode 100644 tasks/setup-linux.yml create mode 100644 tasks/setup-windows.yml create mode 100644 vars/art-tids.yml create mode 100644 vars/main.yml create mode 100755 vars/update-art-tids.sh diff --git a/.ansible-lint b/.ansible-lint new file mode 100644 index 0000000..54b084c --- /dev/null +++ b/.ansible-lint @@ -0,0 +1,130 @@ +# -*-yaml-*- +--- +# .ansible-lint + +profile: shared # min, basic, moderate,safety, shared, production + +# Allows dumping of results in SARIF format +# sarif_file: result.sarif + +# exclude_paths included in this file are parsed relative to this file's location +# and not relative to the CWD of execution. CLI arguments passed to the --exclude +# option are parsed relative to the CWD of execution. +exclude_paths: + - test*.yml + # - .cache/ # implicit unless exclude_paths is defined in config + # - test/fixtures/formatting-before/ + # - test/fixtures/formatting-prettier/ +parseable: true +# quiet: true +# strict: true +# verbosity: 1 + +# Mock modules or roles in order to pass ansible-playbook --syntax-check +# mock_modules: +# - zuul_return +# # note the foo.bar is invalid as being neither a module or a collection +# - fake_namespace.fake_collection.fake_module +# - fake_namespace.fake_collection.fake_module.fake_submodule +# mock_roles: +# - mocked_role +# - author.role_name # old standalone galaxy role +# - fake_namespace.fake_collection.fake_role # role within a collection + +# Enable checking of loop variable prefixes in roles +# loop_var_prefix: "^(__|{role}_)" + +# Enforce variable names to follow pattern below, in addition to Ansible own +# requirements, like avoiding python identifiers. To disable add `var-naming` +# to skip_list. +var_naming_pattern: "^[a-z_][a-z0-9_]*$" + +use_default_rules: true +# Load custom rules from this specific folder +# rulesdir: +# - ./rule/directory/ + +# Ansible-lint is able to recognize and load skip rules stored inside +# `.ansible-lint-ignore` (or `.config/ansible-lint-ignore.txt`) files. +# To skip a rule just enter filename and tag, like "playbook.yml package-latest" +# on a new line. +# Optionally you can add comments after the tag, prefixed by "#". We discourage +# the use of skip_list below because that will hide violations from the output. +# When putting ignores inside the ignore file, they are marked as ignored, but +# still visible, making it easier to address later. +skip_list: [] +# - skip_this_tag + +# Ansible-lint does not automatically load rules that have the 'opt-in' tag. +# You must enable opt-in rules by listing each rule 'id' below. +enable_list: + - args + - empty-string-compare # opt-in + - no-log-password # opt-in + - no-same-owner # opt-in + # - name[prefix] # opt-in + # add yaml here if you want to avoid ignoring yaml checks when yamllint + # library is missing. Normally its absence just skips using that rule. + - yaml +# Report only a subset of tags and fully ignore any others +# tags: +# - jinja[spacing] + +# Ansible-lint does not fail on warnings from the rules or tags listed below +warn_list: + - no-changed-when + - no-handler + - yaml[line-length] + - yaml[truthy] + - experimental # experimental is included in the implicit list + # - role-name + # - yaml[document-start] # you can also use sub-rule matches + +# Some rules can transform files to fix (or make it easier to fix) identified +# errors. `ansible-lint --write` will reformat YAML files and run these transforms. +# By default it will run all transforms (effectively `write_list: ["all"]`). +# You can disable running transforms by setting `write_list: ["none"]`. +# Or only enable a subset of rule transforms by listing rules/tags here. +# write_list: +# - all + +# Offline mode disables installation of requirements.yml and schema refreshing +offline: true + +# Return success if number of violations compared with previous git +# commit has not increased. This feature works only in git +# repositories. +progressive: false + +# Define required Ansible's variables to satisfy syntax check +# extra_vars: +# foo: bar +# multiline_string_variable: | +# line1 +# line2 +# complex_variable: ":{;\t$()" + +# Uncomment to enforce action validation with tasks, usually is not +# needed as Ansible syntax check also covers it. +# skip_action_validation: false + +# List of additional kind:pattern to be added at the top of the default +# match list, first match determines the file kind. +kinds: [] + # - playbook: "**/examples/*.{yml,yaml}" + # - galaxy: "**/folder/galaxy.yml" + # - tasks: "**/tasks/*.yml" + # - vars: "**/vars/*.yml" + # - meta: "**/meta/main.yml" + # - yaml: "**/*.yaml-too" + +# List of additional collections to allow in only-builtins rule. +# only_builtins_allow_collections: +# - example_ns.example_collection + +# List of additions modules to allow in only-builtins rule. +# only_builtins_allow_modules: +# - example_module + +# Allow setting custom prefix for name[prefix] rule +# task_name_prefix: "{stem} | " diff --git a/.dir-locals.el b/.dir-locals.el new file mode 100644 index 0000000..245ece5 --- /dev/null +++ b/.dir-locals.el @@ -0,0 +1,4 @@ +;;; Directory Local Variables -*- no-byte-compile: t -*- +;;; For more information see (info "(emacs) Directory Variables") + +((yaml-mode . ((eval . (ansible))))) diff --git a/.github/workflows/ansible-lint.yml b/.github/workflows/ansible-lint.yml new file mode 100644 index 0000000..6bdd03b --- /dev/null +++ b/.github/workflows/ansible-lint.yml @@ -0,0 +1,20 @@ +name: ansible-lint +on: [push, pull_request] + +jobs: + build: + name: Ansible Lint # Naming the build is important to use it as a status check + runs-on: ubuntu-latest + + steps: + # Important: This sets up your GITHUB_WORKSPACE environment variable + - uses: actions/checkout@v3 + with: + fetch-depth: 0 # needed for progressive mode to work + + - name: Run ansible-lint + # replace `main` with any valid ref, or tags like `v6` + uses: ansible/ansible-lint-action@main + # optional: + # with: + # path: "playbooks/" # <-- only one value is allowed diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..4dba6cd --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,46 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. + +## Our Standards + +Examples of behavior that contributes to creating a positive environment include: + +* Using welcoming and inclusive language +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery and unwelcome sexual attention or advances +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or electronic address, without explicit permission +* Other conduct which could reasonably be considered inappropriate in a professional setting + +## Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. + +## Scope + +This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at research at redcanary.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. + +Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] + +[homepage]: http://contributor-covenant.org +[version]: http://contributor-covenant.org/version/1/4/ diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..c026b00 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,22 @@ + +The MIT License + +Copyright (c) 2023 Red Canary, Inc. + +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. diff --git a/README.md b/README.md index e69de29..b50e6a1 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,111 @@ +ansible_atomic_red_team +========= + +A role to execute atomic red team tests. + +This role facilitates executing Atomic Red Team tests via PowerShell and +Invoke-AtomicRedTeam, on Windows and Linux hosts. It runs powershell core if +necessary, installs Invoke-AtomicRedTeam and adds it to the powershell profile, +and then runs tests against target hosts in the ansible-inventory. + +This role can be included in a playbook using `include_role` along with +variables with a list of Atomic Red Team tests to execute. + +Tests are specified by Technique ID and optionally also by TestNumber or +TestGuid to pick specific ART tests. + +The default variables contain a list of "banned" TIDs, which contain behavior +that is not condusive to automated or repeated testing (eg [T1070.004-8 Delete +Filesystem +Linux](https://github.com/redcanaryco/atomic-red-team/blob/master/atomics/T1070.004/T1070.004.md#atomic-test-8---delete-filesystem---linux), +or involve extended dependencies or resources beyond a single VM (eg cloud +tests). These tests are filtered out at the TID level (eg, matching +`T[0-9]{4}(\.?[0-9]{3})?`), but can still be specified by TID+GUID if desired. + +## Why another way to execute ART Tests? + +There are several exellent execution frameworks for Atomic Red Team, but we desired easy +integreation between our test framework and other devops tools that create VMs, +configure sensors and prerequisites, and run other non-AtomicRedTeam tests. + +Ansible and Terraform allow us to meet these goals for fully automated +testing. Terraform creates VMs provisioned by Ansible. Ansible playbooks run +test scenarios so that we can repeatably generate live telemetry for testing +using different combinations of sensors or configurations. This playbook +integrates Atomic Red Team into this automation-focused testing model. + +## Notes + +Each TID should to 'evaluated' manually prior to being permanently added to the +defaults, as some tests are 'unsafe.' This is why this role doesn't execute +all the TIDs. + +`tasks/gather-art-tids.yml` runs locally from the ansible host, to directly +query the Atomic Red Team test inventory CSV files on github and create/update +`{{ playbook_dir }}/art-tids.yml`. This file is used by the playbook when running +all execpt "banned" TIDs. + +If you want to disable this fetch from github on the machine running the +playbook, set `disable_fetch_art_index: true`. This will cause +`tasks/main.yml` to fall back to `vars/art-tids.yml` which can be manually +updates with `vars/update-art-tids.sh` + + +Role Variables +-------------- + +in `defaults/main.yml`: +- `banned_tids_linux`: annotated list of TIDs to *NOT* run +- `art_tids_linux`: list of the linux TIDs available in ART +- `art_tids_mac`: list of the mac TIDs available in ART +- `art_tids_windows`: list of the windows TIDs available in ART +- `art_repository_owner: redcanaryco` - override with the github repo owner for the atomic_red_team repo to use. +- `art_branch: master` - override with the branch to use + + +Example Playbook +---------------- + +Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: + +```yaml +--- +- hosts: all + gather_facts: True + become: True + tasks: + + - include_role: + name: ansible_atomic_red_team + # you cannot use become directly on include_role, but can control elevation using apply + apply: + become: True + when: ansible_system == 'Linux' + vars: + art_tids_linux: + - T1136.001 + - T1053.003 + - T1003.008-1,2,3 + - T1003.008 f5aa6543-6cb2-4fae-b9c2-b96e14721713 + - T1070.003 47966a1d-df4f-4078-af65-db6d9aa20739,7e6721df-5f08-4370-9255-f06d8a77af4c + # separators MUST be - for TID and testnumbers + # separator MAY be ' ' or ':' for TID and GUIDs + # TID.SUBTID MUST be specified and match GUIDs, as required by Invoke-AtomicTest + + - include_role: + name: ansible_atomic_red_team + apply: + become: False + when: ansible_system == 'Win32NT' + vars: + art_tids_windows: + - T1027 + - T1053.005 + - T1547.001-1,2 + - T1547.001:eb44f842-0457-4ddc-9b92-c4caa144ac42 + - T1547.001:2cb98256-625e-4da9-9d44-f2e5f90b8bd5,dade9447-791e-4c8f-b04b-3a35855dfa06 + + # this runs all available tests against all target systems + # - include_role: + # name: ansible_atomic_red_team +``` diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..dd2c428 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,41 @@ +--- +disable_fetch_art_index: false + +art_repository_owner: redcanaryco +art_branch: master + +banned_tids_linux: + - T1018 # slow ping scan + - T1046 # nmap + - T1070.004 # delete filesystem + - T1078.004 # cloud + - T1082 # Azure + - T1098 # cloud + - T1098.001 # cloud + - T1110.003 # cloud + - T1136.003 # AWS + - T1176 # requires Firefox + - T1217 # requires Firefox + - T1484.002 # Azure + - T1526 # Azure + - T1529 # reboot/shutdown + - T1530 # cloud + - T1562.006 # auditd changes (may break some telemetry collection) + - T1562.008 # cloud logging changes + - T1574.006 # Dynamic Linker Hijacking (requires manual cleanup / testing - might break subsequent tests) + - T1611 # container-based, needs prereqs, and hangs/timeout + +banned_tids_macos: + - T1485 # impact - data destruction + - T1529 # reboot/shutdown + +banned_tids_windows: + - T1485 # impact - data destruction + - T1529 # reboot/shutdown + +# these are updated by tasks/gather-art-tids.yml which polls github to write +# playbook_dir/art-tids.yml as a fallback, the tasks/main.yml will load +# vars/art-tids.yml, which can be manually updated with vars/update-art-tids.sh +art_tids_linux: [] +art_tids_macos: [] +art_tids_windows: [] diff --git a/example-playbook.yml b/example-playbook.yml new file mode 100644 index 0000000..2ceadeb --- /dev/null +++ b/example-playbook.yml @@ -0,0 +1,45 @@ +--- +- name: Example playbook for running ansible_atomic_red_team + hosts: all + gather_facts: true + tasks: + + - name: Run all available tests + ansible.builtin.include_role: + name: ansible_atomic_red_team + when: false # change to true or remove to run all tests + + - name: Run a set of Linux ART Tests + ansible.builtin.include_role: + name: ansible_atomic_red_team + # you cannot use become directly on include_role, but can control elevation using apply + apply: + become: true + when: ansible_system == 'Linux' + vars: + art_tids_linux: + - T1136.001 + - T1053.003 + - T1003.008-1,2,3 + - T1003.008 f5aa6543-6cb2-4fae-b9c2-b96e14721713 + - T1070.003 47966a1d-df4f-4078-af65-db6d9aa20739,7e6721df-5f08-4370-9255-f06d8a77af4c + # separators MUST be - for TID and testnumbers + # separator MAY be ' ' or ':' for TID and GUIDs + # TID.SUBTID MUST be specified and match GUIDs, as required by Invoke-AtomicTest + + - name: Run a set of Windows ART Tests + ansible.builtin.include_role: + name: ansible_atomic_red_team + apply: + become: false + when: ansible_system == 'Win32NT' + vars: + art_tids_windows: + # https://thedfirreport.com/2022/08/08/bumblebee-roasts-its-way-to-domain-admin/ + - T1553.005:c2587b8d-743d-4985-aa50-c83394eaeb68 # download and mount iso, run lnk + - T1016 # System Network Configurration Discovery - 8 tests + - T1057 # Process Discovery - 5 tests + - T1219-2 # Install Anydesk + - T1087.002-5,6,7,8 # Account Discovery(domain) + - T1482-4,5 # Domain Trust Discovery + - T1003.001:2536dee2-12fb-459a-8c37-971844fa73be # OS Cred Dumping mini-dump diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..8929c06 --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,38 @@ +galaxy_info: + author: George Allen + description: An ansible role to execute Atomic Red Team tests on Windows and Linux using Invoke-AtomicRedTeam + company: Red Canary + role_name: atomic_red_team + namespace: redcanary + + license: MIT + + min_ansible_version: "2.14" + + platforms: + - name: Windows + versions: + - "2016" + - "2019" + - "2022" + - name: Ubuntu + versions: + - focal + - jammy + - name: EL + versions: + - "7" + - "8" + + + galaxy_tags: [] + # List tags for your role here, one per line. A tag is a keyword that describes + # and categorizes the role. Users find roles by searching for tags. Be sure to + # remove the '[]' above, if you add tags to this list. + # + # NOTE: A tag is limited to a single word comprised of alphanumeric characters. + # Maximum 20 tags per role. + +dependencies: [] + # List your role dependencies here, one per line. Be sure to remove the '[]' above, + # if you add dependencies to this list. diff --git a/tasks/gather-art-tids.yml b/tasks/gather-art-tids.yml new file mode 100644 index 0000000..1523f3e --- /dev/null +++ b/tasks/gather-art-tids.yml @@ -0,0 +1,35 @@ +--- +- name: Set Indexes-CSV url + ansible.builtin.set_fact: + index_csv_url: "https://github.com/{{ art_repository_owner }}/atomic-red-team/raw/{{ art_branch }}/atomics/Indexes/Indexes-CSV/" + +- name: Gather and write art-tids.yml + ansible.builtin.blockinfile: + dest: "{{ playbook_dir }}/art-tids.yml" + create: true + mode: "0644" + block: | + {%- set list = [] -%} + {%- for line in lookup('ansible.builtin.url', index_csv_url + 'windows-index.csv', wantlist=True) -%} + {%- if line.split(',')[1] | regex_search('^T[0-9.]+$') -%} + {%- if line.split(',')[1] not in list -%} + {{ list.append(line.split(',')[1]) }} + {%- endif -%} + {%- endif -%} + {%- endfor -%} + {#- build a dict with this for to_nice_yaml so it will indent correctly -#} + {%- set yamloutput = dict(art_tids_windows=list) -%} + {{ yamloutput | to_nice_yaml | indent(2) }} + {%- set list = [] -%} + {%- for line in lookup('ansible.builtin.url', index_csv_url + 'linux-index.csv', wantlist=True) -%} + {%- if line.split(',')[1] | regex_search('^T[0-9.]+$') -%} + {%- if line.split(',')[1] not in list -%} + {{ list.append(line.split(',')[1]) }} + {%- endif -%} + {%- endif -%} + {%- endfor -%} + {%- set yamloutput = dict(art_tids_linux=list) -%} + {{ yamloutput | to_nice_yaml | indent(2) }} + delegate_to: localhost + become: false + when: not disable_fetch_art_index diff --git a/tasks/invoke-atomictest.yml b/tasks/invoke-atomictest.yml new file mode 100644 index 0000000..717edda --- /dev/null +++ b/tasks/invoke-atomictest.yml @@ -0,0 +1,136 @@ +--- +# parse the TID argument list from the loop include_tasks in tasks/main.yml, eg: +# "test": [ +# { +# "input": "copy of raw item", +# "technique": "Txxxx[.xxx]", +# "testnumbers": "1,2,3" or "testguids": "" +# } +# ] +- name: Parse test set + ansible.builtin.set_fact: + test: | + {%- set test = [] -%} + {%- if item | regex_search('[ :][0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}') -%} + {%- set t = item | regex_findall('([^ :]+)') -%} + {{ test.append(dict(input=item, technique=t[0], testguids=t[1])) }} + {%- elif item | regex_search('^T.*-.*') -%} + {%- set t = item.split('-') -%} + {{ test.append(dict(input=item, technique=t[0], testnumbers=t[1])) }} + {%- else -%} + {{ test.append(dict(input=item, technique=item)) }} + {%- endif -%} + {{ test }} + +# then turn each test object into a set of command line arguments, eg: +# "testarg" : "-AtomicTechnique T1070 -TestGuid 47966a1d-df4f-4078-af65-db6d9aa20739,7e6721df-5f08-4370-9255-f06d8a77af4c " +# "testarg" : "-AtomicTechnique T1070 -TestNumbers 1,2 " +- name: Parse test set + ansible.builtin.set_fact: + testarg: |- + {{- ('-AtomicTechnique ' + test[0].technique + ' ') -}} + {%- if test[0].testguids is defined -%} + {{- ('-TestGuid ' + test[0].testguids + ' ') -}} + {%- elif test[0].testnumbers is defined -%} + {{- ('-TestNumbers ' + test[0].testnumbers + ' ') -}} + {%- endif -%} + +- name: Run Linux Tests + when: ansible_system == 'Linux' + block: + - name: "Check for and install prereqs for {{ item }}" + block: + - name: "Check prereqs for {{ item }}" + ansible.builtin.shell: + cmd: | + Invoke-AtomicTest {{ testarg }} -CheckPrereqs + args: + executable: /usr/bin/pwsh + register: check_prereqs + failed_when: '"Prerequisites not met:" in check_prereqs.stdout' + changed_when: false + rescue: + - name: "Install prereqs for {{ item }}" + ansible.builtin.shell: + cmd: | + Invoke-AtomicTest {{ testarg }} -GetPrereqs + args: + executable: /usr/bin/pwsh + register: get_prereqs + ignore_errors: true + + - name: "Debug prereqs {{ item }}" + ansible.builtin.debug: + var: get_prereqs.stdout + when: get_prereqs.changed + + - name: "Execute {{ item }}" + ansible.builtin.shell: + cmd: | + Invoke-AtomicTest {{ testarg }} -Confirm:$false -TimeoutSeconds 300 -ExecutionLogPath /root/atc_execution.csv + args: + executable: /usr/bin/pwsh + register: execute_test + ignore_errors: true + + - name: "Execute {{ item }}" + ansible.builtin.debug: + var: execute_test.stdout + + - name: "Cleanup {{ item }}" + ansible.builtin.shell: + cmd: | + Invoke-AtomicTest {{ testarg }} -Cleanup + args: + executable: /usr/bin/pwsh + register: cleanup_test + ignore_errors: true + + - name: "Cleanup {{ item }}" + ansible.builtin.debug: + var: cleanup_test.stdout + when: cleanup_test.rc != 0 + +- name: Run Windows Tests + when: ansible_system == 'Win32NT' + block: + - name: "Check for and install prereqs for {{ item }}" + block: + - name: "Check prereqs for {{ item }}" + ansible.windows.win_shell: | + Invoke-AtomicTest {{ testarg }} -CheckPrereqs + register: check_prereqs + failed_when: '"Prerequisites not met:" in check_prereqs.stdout' + changed_when: false + rescue: + - name: "Install prereqs for {{ item }}" + ansible.windows.win_shell: | + Invoke-AtomicTest {{ testarg }} -GetPrereqs + register: get_prereqs + ignore_errors: true + + - name: "Debug prereqs {{ item }}" + ansible.builtin.debug: + var: get_prereqs.stdout + when: get_prereqs.changed + + - name: "Execute {{ item }}" + ansible.windows.win_shell: | + Invoke-AtomicTest {{ testarg }} -Confirm:$false -TimeoutSeconds 300 -ExecutionLogPath /root/atc_execution.csv + register: execute_test + ignore_errors: true + + - name: "Execute {{ item }}" + ansible.builtin.debug: + var: execute_test.stdout + + - name: "Cleanup {{ item }}" + ansible.windows.win_shell: | + Invoke-AtomicTest {{ testarg }} -Cleanup + register: cleanup_test + ignore_errors: true + + - name: "Cleanup {{ item }}" + ansible.builtin.debug: + var: cleanup_test.stdout + when: cleanup_test.rc != 0 diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..74ffd35 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,35 @@ +--- +# tasks file for ansible_atomic_red_team + +- name: "Include the list of available TIDs" + block: + - name: Built list of ART TIDs + ansible.builtin.include_tasks: "gather-art-tids.yml" + when: not disable_fetch_art_index + + - name: Load the ART TIDs + ansible.builtin.include_vars: "{{ playbook_dir }}/art-tids.yml" + rescue: + # this may require manual updates via vars/update-art-tids.sh + - name: Load the ART TIDs from role/vars + ansible.builtin.include_vars: "art-tids.yml" + +- name: Setup Windows + ansible.builtin.include_tasks: "setup-windows.yml" + when: ansible_system == 'Win32NT' + +- name: Setup Linux + ansible.builtin.include_tasks: "setup-linux.yml" + when: ansible_system == 'Linux' + +- name: Run invoke-atomictest on each TID (linux) + ansible.builtin.include_tasks: invoke-atomictest.yml + loop: "{{ art_tids_linux | difference(banned_tids_linux) }}" + when: + - ansible_system == 'Linux' + +- name: Run invoke-atomictest on each TID (windows) + ansible.builtin.include_tasks: invoke-atomictest.yml + loop: "{{ art_tids_windows | difference(banned_tids_windows) }}" + when: + - ansible_system == 'Win32NT' diff --git a/tasks/setup-linux.yml b/tasks/setup-linux.yml new file mode 100644 index 0000000..3352834 --- /dev/null +++ b/tasks/setup-linux.yml @@ -0,0 +1,90 @@ +--- +- name: Powershell + block: + - name: Check for powershell + ansible.builtin.shell: + cmd: pwsh -c '$true' + changed_when: false + rescue: + + # ------- Ubuntu + + - name: Ensure powershell is installed (prereq) + ansible.builtin.package: + name: + - wget + - apt-transport-https + - software-properties-common + state: present + when: ansible_facts['distribution'] == 'Ubuntu' + + - name: Ensure powershell is installed (repo keys) + ansible.builtin.apt: + deb: "https://packages.microsoft.com/config/ubuntu/{{ ansible_distribution_version }}/packages-microsoft-prod.deb" + when: ansible_facts['distribution'] == 'Ubuntu' + + - name: Ensure powershell is installed (deb) + ansible.builtin.apt: + name: powershell + update_cache: yes + when: >- + ansible_facts['distribution'] == 'Ubuntu' + + # ------- Amazon / CentOS + + - name: Add Microsoft Repo (Amazon Linux 2) + ansible.builtin.get_url: + url: https://packages.microsoft.com/config/rhel/8/prod.repo + dest: /etc/yum.repos.d/microsoft.repo + mode: '0644' + owner: root + when: ansible_facts['distribution'] == 'Amazon' + + - name: Add Microsoft Repo (CentOS) + ansible.builtin.get_url: + url: https://packages.microsoft.com/config/rhel/{{ ansible_distribution_major_version }}/prod.repo + dest: /etc/yum.repos.d/microsoft.repo + mode: '0644' + owner: root + when: ansible_facts['distribution'] == 'CentOS' + + - name: Ensure powershell is installed (rpm) + ansible.builtin.yum: + name: powershell + update_cache: yes + when: >- + ansible_facts['distribution'] == 'CentOS' or + ansible_facts['distribution'] == 'Amazon' + +- name: Install Invoke-ART + ansible.builtin.shell: + cmd: | + IEX (IWR 'https://raw.githubusercontent.com/redcanaryco/invoke-atomicredteam/master/install-atomicredteam.ps1' -UseBasicParsing); Install-AtomicRedTeam -getAtomics -Force + args: + executable: /usr/bin/pwsh + creates: /root/AtomicRedTeam/atomics/Indexes/index.yaml + +- name: Find the path to the system powershell profile + ansible.builtin.shell: + cmd: | + $PROFILE.AllUsersAllHosts + changed_when: false + args: + executable: /usr/bin/pwsh + register: pwshprofile + +- name: Powershell Profile (debug) + ansible.builtin.debug: + var: pwshprofile.stdout + +- name: Add Invoke-AtomicRedTeam to the powershell profile + ansible.builtin.lineinfile: + path: "{{ pwshprofile.stdout }}" + state: present + regex: '.*Inovke-AtomicRedTeam.*' + line: |- + Import-Module "/root/AtomicRedTeam/invoke-atomicredteam/Invoke-AtomicRedTeam.psd1" -Force + owner: root + group: root + mode: '0644' + create: yes diff --git a/tasks/setup-windows.yml b/tasks/setup-windows.yml new file mode 100644 index 0000000..0b37dd5 --- /dev/null +++ b/tasks/setup-windows.yml @@ -0,0 +1,49 @@ +--- +# Copied and adapted from Splunk Attack Range (https://github.com/splunk/attack_range) +# https://github.com/splunk/attack_range/blob/b49e9cf288ce3d0127e710021efd3c2613a20777/packer/ansible/roles/atomic_red_team/tasks/main.yml + +- name: Enable strong dotnet crypto + ansible.windows.win_regedit: + key: "{{ item }}" + value: SchUseStrongCrypto + datatype: dword + data: 1 + with_items: + - "HKLM:\\SOFTWARE\\Microsoft\\.NetFramework\\v4.0.30319" + - "HKLM:\\SOFTWARE\\Wow6432Node\\Microsoft\\.NetFramework\\v4.0.30319" + +- name: Check installed providers + ansible.windows.win_shell: Get-PackageProvider + register: providers + changed_when: false + +- name: Install NuGet Provider + ansible.windows.win_shell: | + Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force + when: providers.stdout is not search("NuGet") + +- name: Install Atomic Red Team + # at the time of writing, this worked better for us than: community.windows.win_psmodule + ansible.windows.win_shell: | + Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted + Install-Module -Name invoke-atomicredteam,powershell-yaml -Scope CurrentUser + +- name: Add ART to powershell profile + ansible.windows.win_shell: | + $script=@' + Import-Module invoke-atomicredteam + $PSDefaultParameterValues = @{"Invoke-AtomicTest:PathToAtomicsFolder"="C:\AtomicRedTeam\atomics"} + '@ + $script | Out-File $PShome\Profile.ps1 + +- name: "Install AtomicsFolder from {{ art_repository_owner }}" + ansible.windows.win_shell: | + Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Internet Explorer\Main" -Name "DisableFirstRunCustomize" -Value 2 + IEX (IWR 'https://raw.githubusercontent.com/redcanaryco/invoke-atomicredteam/master/install-atomicsfolder.ps1' -UseBasicParsing) + Install-AtomicsFolder -Force -RepoOwner "{{ art_repository_owner }}" -Branch "{{ art_branch }}" + args: + creates: C:\AtomicRedTeam\atomics\used_guids.txt + +- name: Exclude Atomics Folder from Microsoft Defender + ansible.windows.win_shell: | + Add-MpPreference -ExclusionPath C:\AtomicRedTeam diff --git a/vars/art-tids.yml b/vars/art-tids.yml new file mode 100644 index 0000000..244a8d1 --- /dev/null +++ b/vars/art-tids.yml @@ -0,0 +1,395 @@ +--- +art_tids_linux: + - T1003.007 + - T1003.008 + - T1007 + - T1014 + - T1016 + - T1018 + - T1027 + - T1027.001 + - T1027.002 + - T1027.004 + - T1030 + - T1033 + - T1036.003 + - T1036.005 + - T1036.006 + - T1037.004 + - T1040 + - T1046 + - T1048 + - T1048.002 + - T1048.003 + - T1049 + - T1053.002 + - T1053.003 + - T1053.006 + - T1056.001 + - T1057 + - T1059.004 + - T1059.006 + - T1069.001 + - T1070.002 + - T1070.003 + - T1070.004 + - T1070.006 + - T1071.001 + - T1074.001 + - T1078.003 + - T1082 + - T1083 + - T1087.001 + - T1090.001 + - T1090.003 + - T1098.004 + - T1105 + - T1110.001 + - T1110.004 + - T1113 + - T1115 + - T1132.001 + - T1135 + - T1136.001 + - T1140 + - T1176 + - T1201 + - T1217 + - T1222.002 + - T1485 + - T1486 + - T1496 + - T1497.001 + - T1518.001 + - T1529 + - T1543.002 + - T1546.004 + - T1546.005 + - T1547.006 + - T1548.001 + - T1548.003 + - T1552 + - T1552.001 + - T1552.003 + - T1552.004 + - T1553.004 + - T1555.003 + - T1556.003 + - T1560.001 + - T1560.002 + - T1562 + - T1562.001 + - T1562.003 + - T1562.004 + - T1562.006 + - T1564.001 + - T1571 + - T1574.006 + - T1614.001 +art_tids_macos: + - T1016 + - T1018 + - T1027 + - T1027.001 + - T1027.002 + - T1027.004 + - T1030 + - T1033 + - T1036.005 + - T1036.006 + - T1037.002 + - T1037.004 + - T1037.005 + - T1040 + - T1046 + - T1048 + - T1048.002 + - T1048.003 + - T1049 + - T1053.003 + - T1056.001 + - T1056.002 + - T1057 + - T1059.002 + - T1059.004 + - T1069.001 + - T1070.002 + - T1070.003 + - T1070.004 + - T1070.006 + - T1071.001 + - T1074.001 + - T1078.001 + - T1078.003 + - T1082 + - T1083 + - T1087.001 + - T1090.001 + - T1090.003 + - T1098.004 + - T1105 + - T1110.004 + - T1113 + - T1115 + - T1123 + - T1132.001 + - T1135 + - T1136.001 + - T1140 + - T1176 + - T1201 + - T1217 + - T1222.002 + - T1485 + - T1496 + - T1497.001 + - T1518 + - T1518.001 + - T1529 + - T1543.001 + - T1543.004 + - T1546.004 + - T1546.005 + - T1546.014 + - T1547.006 + - T1547.007 + - T1547.015 + - T1548.001 + - T1548.003 + - T1552 + - T1552.001 + - T1552.003 + - T1552.004 + - T1553.001 + - T1553.004 + - T1555.001 + - T1555.003 + - T1560.001 + - T1562.001 + - T1562.003 + - T1564.001 + - T1564.002 + - T1569.001 + - T1571 + - T1574.006 + - T1647 +art_tids_windows: + - T1003 + - T1003.001 + - T1003.002 + - T1003.003 + - T1003.004 + - T1003.005 + - T1003.006 + - T1006 + - T1007 + - T1010 + - T1012 + - T1016 + - T1018 + - T1020 + - T1021.001 + - T1021.002 + - T1021.003 + - T1021.006 + - T1027 + - T1027.004 + - T1027.006 + - T1033 + - T1036 + - T1036.003 + - T1036.004 + - T1036.005 + - T1037.001 + - T1039 + - T1040 + - T1041 + - T1046 + - T1047 + - T1048 + - T1048.002 + - T1048.003 + - T1049 + - T1053.002 + - T1053.005 + - T1055 + - T1055.001 + - T1055.003 + - T1055.004 + - T1055.012 + - T1056.001 + - T1056.002 + - T1056.004 + - T1057 + - T1059.001 + - T1059.003 + - T1059.005 + - T1059.007 + - T1069.001 + - T1069.002 + - T1070 + - T1070.001 + - T1070.003 + - T1070.004 + - T1070.005 + - T1070.006 + - T1071.001 + - T1071.004 + - T1072 + - T1074.001 + - T1078.001 + - T1078.003 + - T1082 + - T1083 + - T1087.001 + - T1087.002 + - T1090.001 + - T1090.003 + - T1091 + - T1095 + - T1098 + - T1105 + - T1106 + - T1110.001 + - T1110.002 + - T1110.003 + - T1110.004 + - T1112 + - T1113 + - T1114.001 + - T1115 + - T1119 + - T1120 + - T1123 + - T1124 + - T1125 + - T1127 + - T1127.001 + - T1132.001 + - T1133 + - T1134.001 + - T1134.002 + - T1134.004 + - T1134.005 + - T1135 + - T1136.001 + - T1136.002 + - T1137 + - T1137.002 + - T1137.004 + - T1137.006 + - T1140 + - T1176 + - T1187 + - T1195 + - T1197 + - T1201 + - T1202 + - T1204.002 + - T1207 + - T1216 + - T1216.001 + - T1217 + - T1218 + - T1218.001 + - T1218.002 + - T1218.003 + - T1218.004 + - T1218.005 + - T1218.007 + - T1218.008 + - T1218.009 + - T1218.010 + - T1218.011 + - T1219 + - T1220 + - T1221 + - T1222.001 + - T1482 + - T1484.001 + - T1485 + - T1486 + - T1489 + - T1490 + - T1491.001 + - T1497.001 + - T1505.002 + - T1505.003 + - T1505.004 + - T1518 + - T1518.001 + - T1529 + - T1531 + - T1539 + - T1543.003 + - T1546 + - T1546.001 + - T1546.002 + - T1546.003 + - T1546.007 + - T1546.008 + - T1546.009 + - T1546.010 + - T1546.011 + - T1546.012 + - T1546.013 + - T1546.015 + - T1547 + - T1547.001 + - T1547.002 + - T1547.003 + - T1547.004 + - T1547.005 + - T1547.008 + - T1547.009 + - T1547.010 + - T1547.014 + - T1548.002 + - T1550.002 + - T1550.003 + - T1552.001 + - T1552.002 + - T1552.004 + - T1552.006 + - T1553.004 + - T1553.005 + - T1555 + - T1555.003 + - T1555.004 + - T1556.002 + - T1557.001 + - T1558.001 + - T1558.002 + - T1558.003 + - T1558.004 + - T1559 + - T1559.002 + - T1560 + - T1560.001 + - T1562 + - T1562.001 + - T1562.002 + - T1562.004 + - T1562.006 + - T1563.002 + - T1564 + - T1564.001 + - T1564.002 + - T1564.003 + - T1564.004 + - T1564.006 + - T1566.001 + - T1567.002 + - T1569.002 + - T1571 + - T1572 + - T1573 + - T1574.001 + - T1574.002 + - T1574.008 + - T1574.009 + - T1574.011 + - T1574.012 + - T1614.001 + - T1615 + - T1620 diff --git a/vars/main.yml b/vars/main.yml new file mode 100644 index 0000000..ed97d53 --- /dev/null +++ b/vars/main.yml @@ -0,0 +1 @@ +--- diff --git a/vars/update-art-tids.sh b/vars/update-art-tids.sh new file mode 100755 index 0000000..eb3f0b2 --- /dev/null +++ b/vars/update-art-tids.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +IFS=$'\n' +ghuser="redcanaryco" +branch="master" + +echo "---" | tee art-tids.yml + +function fetch-art-index-to-yml () { + url="https://github.com/${ghuser}/atomic-red-team/raw/${branch}/atomics/Indexes/Indexes-CSV/${1}-index.csv" + tidlist=( $(curl -sL $url | awk -F, '/T1/{print $2}' | sort -u) ) + echo "art_tids_${1}:" | tee -a art-tids.yml + for tid in ${tidlist[*]}; do + echo " - ${tid}" + done | tee -a art-tids.yml +} + +for os in linux macos windows; do + fetch-art-index-to-yml ${os} +done