Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Break out from Vim insert mode when pasting

Tags:

security

vim

Can one put control characters into a text so that when I copy and paste it in Vim it exits insert mode and does something nasty in command mode?

like image 337
anselm Avatar asked Mar 22 '23 04:03

anselm


2 Answers

The short answer seems to be "yes". I was able to put the following in my clipboard:

hello<Escape>:!date<CR>

and when I pasted it into vim while in insert mode hello was typed and then the shell opened up and the date command was run.

Obviously if I can run the date command in the shell I can do much more nasty stuff.

To get that string in my paste buffer I opened vim and typed hello<C-V><Esc>:!date<C-V><Enter>. I then had to save that file, open it with Kate and copy the contents that way (copying from vim didn't preserve the control characters).

like image 180
bundacia Avatar answered Apr 02 '23 09:04

bundacia


That depends on the environment, and the Vim command used.

Graphical GVIM can differentiate pastes from typed keys, but in the terminal, this is not (generally) possible. That's why Vim has the 'paste' and 'pastetoggle' options, to tell Vim what is expected. Despite that, if the character stream contains a key like <Esc> that switches modes, Vim will do so.

Instead of pushing text into Vim, it is safer to pull with Vim's put command: "*p. There, special characters like <Esc> will be inserted literally into the buffer; Vim won't switch modes here. The only Vim command that interprets register contents as typed (and therefore is susceptible to mode switch commands) is i_CTRL-R. To avoid that, one should use any of the other command variants, e.g. i_CTRL-R_CTRL-R.

summary

Pull text into Vim instead of pushing it; if you avoid the i_CTRL-R command (or neuter it by remapping it), this is safe. Additionally, the :registers command allows you to inspect all contents before pasting.

like image 45
Ingo Karkat Avatar answered Apr 02 '23 09:04

Ingo Karkat