Search code examples
vimctf

Is there a way to execute vim commands that are within txt file along with text?


I'm at my CTF final stage and there I have this:

vim flag
iCourse{v3ry_g00d_fl4g}[ESC]13hs1[ESC]am_[ESC]9l2xli_[ESC]2li3[ESC]vypaks[ESC]:s/ry/15
:wq 

I'm not really familiar with Vim so I can't figure out how to run this script. I'm guessing it must be doing something bigger than replacing "ry" with "15", because I tried that manually and flag is incorrect. Thanks in advance!


Solution

  • Yes it is possible. When launched with the -s command line flag, vim will fetch commands from an input file:

    From the man page:

    -s {scriptin}
          The  script  file {scriptin} is read. The characters in the
          file are interpreted as if you had typed them. The same can
          be done with the command ":source! {scriptin}". If the end
          of the file is reached before the editor exits, further
          characters are read from the keyboard.
    

    So all you need to do is use a tool like sed to convert every occurrence of [ESC] into an escape character, store the result in a temporary file, and then feed it to vim:

    s='iCourse{v3ry_g00d_fl4g}[ESC]13hs1[ESC]am_[ESC]9l2xli_[ESC]2li3[ESC]vypaks[ESC]:s/ry/15'
    cmdfile=`mktemp`
    echo -e `sed 's/\[ESC\]/\\\x1B/g' <<<"$s"` >$cmdfile
    vim -s $cmdfile