r/commandline • • Oct 21 '20

Unix general Don't Copy Paste Into A Shell

https://briantracy.xyz/writing/copy-paste-shell.html
165 Upvotes

49 comments sorted by

97

u/_0x783czar Oct 21 '20 edited Oct 21 '20

Operations Engineer here. I recommend to all my fellow engineers to take advantage of Bash's command edit mode.

Ctrl+x Ctrl+e (or Esc then v in BASH vi mode)

This will open your default text editor and execute whatever you put in there when you save and exit. You can even write entire multi-line scripts. Abort execution by exiting without saving.

I highly recommend opening this mode and then pasting in any command from a web page. That way you can inspect what you pasted before it runs. It's also very helpful if you know you need to edit the copied command before running it, because it saves you from accidentally copying a newline character and having it execute before you get a chance to change it.

A word of warning though, if you already have something written on the line when you open this editor and exit without saving it will still execute whatever was on the line before you opened the editor, even if you deleted it in the editor first.

9

u/[deleted] Oct 22 '20

A word of warning though, if you already have something written on the line when you open this editor and exit without saving it will still execute whatever was on the line before you opened the editor, even if you deleted it in the editor first.

have the editor exit (>=)1 to avoid this.

eg: :cq in vim or :q! 1 in kakoune

7

u/[deleted] Oct 22 '20

This is great non workflow breaking, only modifying advice. An actual applicable solution. Thank you.

4

u/arufu_ Oct 21 '20

Omg, i didn't knew this feature many thanks.

3

u/Atralb Oct 22 '20

Not bash's but Readline's mode.

2

u/thedeadslow Oct 22 '20 edited Jun 17 '21

With Vim, you can do "*p in command mode to insert the content of the clipboard - for an even more mouse-free experience.

39

u/pobody Oct 21 '20

For readline/bash, add this to your ~/.inputrc:

set enable-bracketed-paste on

Commands pasted into the shell will then require an explicit [Enter] before they will execute, even if they contain a trailing newline.

29

u/[deleted] Oct 21 '20

(don't copy that)

14

u/SibLiant Oct 21 '20

FFUUUCK!

4

u/[deleted] Oct 22 '20

or you can just paste this quick one-liner into your shell (:

echo "set enable-bracketed-paste on" >> ~/.inputrc

wait

1

u/o11c Oct 22 '20

I'm not sure if Reddit allows you to specify the nasty bits enough to be vulnerable - I don't know how much subreddit customization allows.

The particular exploit linked here relies on JavaScript, which you really should be disabling except on whitelisted sites.

But there are also nasty vulnerabilities that rely only on CSS. I don't know if the necessary CSS passes Reddit's whitelist.

5

u/[deleted] Oct 22 '20

Has anyone ever found a real-world attack using this?

5

u/sytanoc Oct 22 '20

Another reason to disable dom.event.clipboardevents.enabled in Firefox. Although there are other ways to do this with CSS or weird unicode fuckery

3

u/Willy-the-kid Oct 21 '20

Can't you read the text from your clipboard or a test document before you run it?

1

u/thisbenzenering Oct 22 '20

my normal process is to always put the text into a plain text file format and save it before putting it in the console. I mostly do this because I forget what I was doing when I look at what my previous commands were for

3

u/mrcruz Oct 22 '20
Ctrl-t,
Ctrl-v

Copies contents, and pastes contents within the Address Bar of your browser. Once you verify that things are good,

Ctrl-a,
Ctrl-c,
Ctrl-w

to close the tab.

Also helps with removing HTML stylization.

And rememver, NoScript is your friend.

6

u/tonebacas Oct 21 '20

That's really shady.

Now that I've seen this example, I wonder how being able to set the contents of your clipboard could be used for a non-nefarious purpose, since the functionality is there for use.

IMO, being able to do this should not be made possible in javascript.

10

u/[deleted] Oct 21 '20

Presumably it is there for all those buttons at e.g. Github that allow you to copy something without selecting it. People used to use Flash or Java applets for that purpose in the distant past.

5

u/metiulekm Oct 21 '20

Honestly, quick one-click copying of longish strings for convenience isn't that uncommon nor unreasonable. For example, GitHub and Gerrit both use it for the "clone" command copying. Also, most sites carrying Steam keys (Humble, for example) use it for quick copying them. Linux/X11 primary selection almost completely subsumes this usecase, but there exist some people who don't use Linux lol

The real vulnerability here is that the trailing newline is interpreted as a newline, but this shouldn't (and in my experience doesn't) happen in any reasonable terminal environment in 2020.

4

u/zebediah49 Oct 21 '20

IMO, being able to do this should not be made possible in javascript.

I agree.

dom.event.clipboardevents.enabled = false

2

u/gandalfx Oct 21 '20

When you're building interactive web applications that feature can be extremely useful.

2

u/chisquared Oct 21 '20 edited Oct 21 '20

Here's a zsh plugin that protects you from some exploits of this type: https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/safe-paste

It's not bullet-proof, but it helps.

See also https://cirw.in/blog/bracketed-paste

1

u/OneTurnMore Oct 21 '20

This isn't needed; bracketed paste is enabled by default in Zsh. It adds bracketed-paste-magic, which works around any ZLE keybinding changes, such as if jj is bound to vicmd.

1

u/sliddis Oct 21 '20

remindme! 12 hours

2

u/RemindMeBot Oct 21 '20

There is a 1 hour delay fetching comments.

I will be messaging you in 12 hours on 2020-10-22 07:23:08 UTC to remind you of this link

CLICK THIS LINK to send a PM to also be reminded and to reduce spam.

Parent commenter can delete this message to hide from others.


InfoCustomYour RemindersFeedback

2

u/LIKE-AN-ANIMAL Oct 22 '20

Is Fish Shell immune to this by default?

From the docs:
"for which fish enables "Bracketed Paste Mode", so it can tell a paste from manually entered text"
http://fishshell.com/docs/current/index.html#copy-and-paste-kill-ring

2

u/npsimons Oct 21 '20 edited Oct 21 '20
  1. I browse with NoScript set to default deny and only turn it on when needed.

  2. Paste into a temporary buffer in Emacs first as I usually need to clean up and edit the command anyway.

  3. For that matter, I'll often browse in emacs-w3m, which has no JavaScript, and I copykill-region from there, edit, then paste.

  4. But I'm glad to be made aware of this attack, now I can add a filter to my transparent proxy to cut that sort of thing out before it even gets to the clients.

8

u/Traches Oct 21 '20

nerd

4

u/npsimons Oct 21 '20

Why thank you!

1

u/scriptkiddy315 Oct 21 '20 edited Oct 21 '20

I used "plain text copy" extension in firefox, hoping it would prevent copying malicious hidden text. Could that help?

The bracketed text thing is cool, although I wonder if that might mess up some scripts. I suppose it's not a big deal.

1

u/tuxbass Oct 22 '20

Another error-prone way to protect yourself - use a clipboard manager (eg copyq) and inspect and/or edit the entry prior to pasting.

1

u/xyzndsgn Oct 22 '20

Why the fuck was clipboard manipulation methods are openly implemented in the first place in the browser? I really can't justify a reason.

1

u/[deleted] Oct 22 '20

I've said it before, and I'll say it again: Javascript is a failure, and if you keep it activated, you have noone to blame but yourself.

"But the websites I use demand I have it activated!" - Go and use better websites.

1

u/jmdugan Oct 22 '20

IMO this as a critical model failure of modern browsers

1

u/thisbenzenering Oct 22 '20

jokes on you, my noscript blocked the attempt to change my copy/paste so I only copied the highlighted text

1

u/t-rkr Oct 22 '20

xfce-terminal shows a warning when pasting: "Pasting this text to the terminal may be dangerous as it looks like some commands may be executed, ..." and shows the command.

1

u/[deleted] Oct 21 '20

...or just select and paste from the selection clipboard. Nothing can get there without you actually selecting it.

0

u/[deleted] Oct 21 '20

[deleted]

14

u/phil_g Oct 21 '20 edited Oct 22 '20

Welcome to the historical vagaries of Unix. Basically, there are two different common ways to copy text from one program to another.0 The first is to highlight text in one window and then use a middle mouse click (or, often, Shift-Insert) in a second window. This transfer uses the X Window System's "primary selection". The second way is to issue a "copy" or "cut" command in one window (or, often, Ctrl-C or Ctrl-X, respectively) and then issue a "paste" command (or, often, Ctrl-V or Shift-Ctrl-Insert) in a second window. This uses X's "clipboard selection".1

What's probably happening is that asserting the primary selection (by highlighting the text) doesn't trigger the JavaScript, but explicitly copying the text (into the clipboard selection) does.

0Technically, there's more than two ways, but the two mentioned above are the only ones that really matter nowadays.

1For more information, see X Selections, Cut Buffers, and Kill Rings by Jamie Zawinski.

2

u/FinancialAdvicePleas Oct 21 '20

Control c and control x are backwards in your parenthetical. Minor point but worth calling out.

1

u/phil_g Oct 22 '20

Oh, thanks for pointing that out. Fixed!

11

u/zebediah49 Oct 21 '20

6

u/conairh Oct 22 '20

In case anyone wants a quick summary, a <span> containing the payload is placed inside a <p> which has the innocent command you're trying to copy. The span is positioned -100, -100 absolute, outside the visible rendered page. When you drag select the innocent command, you're also selecting the span containing the evil.

6

u/antoo98 Oct 21 '20

When using the middle mouse button (often the wheel click) you paste the currently selected text (not to be confused with copied text, this really is just the text you selected). I assume when you copy (and only then is this javascript hook run), this overwrites this "selected text clipboard" with the text you copied or in this case the replaced text from the javascript hook.

5

u/zebediah49 Oct 21 '20

More precisely, it's a separate clipboard. Any time you select text, that copies the selected text into the X Primary selection. Middle click then pastes it. Meanwhile, ctl-c and ctl-v manipulate the Clipboard selection.

But yeah, it's JS that activates on copy. It doesn't do it for me, because (1) I have noscript on, and (2) I have clipboard events disabled on top of that.

2

u/npsimons Oct 21 '20

what is happening here, and am I protected when using the first method?

Sort of. It's a remnant from ye olden days when X (née X11) would have it's own copy and paste functionality that was rather minimal, hence why most desktops implemented their own clipboards. So in a sense you are protected by this method, but any serious attacker is going to take that into account and add code to make sure it happens in all cases; it's the sort of thing JavaScript programmers have to work around (platform differences) all the time.