TTPForge Actions: edit_file
December 5, 2023 ยท View on GitHub
The edit_file action is useful for automating malicious modifications to files
(for example, adding yourself to /etc/sudoers or commenting out important
logging code). edit_file can append, delete, or replace lines in the target
file - check out the examples below to learn more.
Appending and Deleting Lines
This example shows how to use the append and delete functionality of the
edit_file action:
You can experiment with the above TTP by installing the examples TTP
repository (skip this if ttpforge list repos shows that the examples repo is
already installed):
ttpforge install repo https://github.com/facebookincubator/TTPForge --name examples
and then running the below command:
ttpforge run examples//actions/edit-file/append-delete.yaml
Replacing Lines
You can also use edit_file to replace lines in a file and optionally use
powerful regular expressions to perform complex transformations. The next
example shows this functionality in action:
Try out the above TTP by running this command:
ttpforge run examples//actions/edit-file/replace.yaml
Fields
You can specify the following YAML fields for the edit_file action:
edit_file:(type:string) the path to the file you want to edit (must exist).backup_file:(type:string) the backup path to which the original file should be copied.edits:(type:list) a list of edits to make. Each entry can contain the following fields:delete:(type:string) string/pattern to delete - pair withregexp: trueto treat as a Golang regular expression and delete all matches thereof.append:(typestring) line(s) to append to the end of the file.old:(type:string) string/pattern to replace - pair withregexp: trueto treat as a Golang regular expression and replace all matches thereof. Must always be paired withnew:new:(type:string) string with which to replace the string/pattern specified byold:- must always be paired withold:
cleanup:you can set this todefaultin order to automatically restore the original file once the TTP completes. Note: this only works whenbackup_fileis set. You can also define a custom cleanup action.
Notes
edit_filewill read the entire file into memory, perform all specified edits, and then write out the results. Be careful when using it against very large files.edit_filedoes not support editing binary files.- The
editslist is looped through from top to bottom and all edits are applied sequentially to the copy of the file contents residing in memory. This means, for example, that if youappendand then laterdeletethat same line, the resulting final file won't contain that line.