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.
This example shows how to use the append
and delete
functionality of the
edit_file
action:
TTPForge/example-ttps/actions/edit-file/append-delete.yaml
Lines 1 to 35 in 7634dc6
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
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:
TTPForge/example-ttps/actions/edit-file/replace.yaml
Lines 1 to 47 in 7634dc6
Try out the above TTP by running this command:
ttpforge run examples//actions/edit-file/replace.yaml
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: true
to 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: true
to 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 todefault
in order to automatically restore the original file once the TTP completes. Note: this only works whenbackup_file
is set. You can also define a custom cleanup action.
edit_file
will 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_file
does not support editing binary files.- The
edits
list 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 youappend
and then laterdelete
that same line, the resulting final file won't contain that line.