86

Like all beginners in the land of Linux, I usually look for websites that contain some useful shell commands, mark it with my mouse, copy it (CTRL + C) and paste it into a terminal.

For example, if I need to install package_name.deb

sudo apt-get install package_name.deb

I will give my root password and install the package_name.deb

When I paste this command to my text editor, it will be something like:

sudo apt-get install package_name.deb && apt-get install suspicious_file.deb 

Second example, if I want to add a new ppa (terminal)

sudo add-apt-repository ppa:some/ppa
sudo apt-get update

When I edit my sources.list, I will find something like:

deb http://ppa.launchpad.net/some-ppa/

and

deb http://ppa.launchpad.net/a_suspicious_some-ppa/

The problem is the second ppa deb http://ppa.launchpad.net/a_suspicious_some-ppa/ is added automatically and without my permission.

As you can see, there is an invisible part. It does not appear on my terminal.

What is the risk of copy and paste from an untrusted website in the terminal and how to fix my operating system?

13
  • 15
    Repair is done by wiping disk and reinstalling OS. Feb 14, 2016 at 17:38
  • 9
    Solution: use lynx. No JavaScript, no CSS oddities there. Feb 14, 2016 at 17:58
  • 5
    Here is an example: thejh.net/misc/website-terminal-copy-paste
    – Gumbo
    Feb 14, 2016 at 18:23
  • 2
    Use a browser plugin that disables access to the copy buffer. :) Feb 14, 2016 at 18:51
  • 11
    Most answers solve your immediate problem. However, if you are learning Linux command-line, you'd be well-advised to type your commands yourself. Muscle memory sure helps, and you also have to build large command pipelines from scratch, one by one. github.com/jlevy/the-art-of-command-line is a great starting point. There's also another helpful command: github.com/p-e-w/maybe Of course, if you want to shoot yourself in the foot, no preventive measures can eliminate the risk... Feb 15, 2016 at 9:28

4 Answers 4

82

Websites can append to your clipboard

The risk is exactly what you said it was. It's definitely possible to append malicious commands to the clipboard.

You could even append && rm -rf /* (only executes if the first command was successful), or ; rm -rf /* (executes even if the first command was unsuccessful) and brick certain UEFI devices.

You should also check out Michael's post in this thread for another example.

In the end, it really depends on how creative and malicious a particular evil "hacker" is.


But how can you make the commands "invisible" in the terminal?"

  1. Method one

    echo test;echo insert evil here;clear;echo installing package
    

    Execution order:

    1. Echo "test" happens
    2. Echo "insert evil here" happens
    3. Actions are "cleared"
    4. Intended action happens here, but you don't see the rest.

    ...

    You can try to scroll up in the terminal window to find the rest of it.

  2. Method two

    stty -echo 
    tput smcup
    

    This will disable the terminal from showing what you're typing, so it doesn't appear in the terminal window at all.

    You can try it like this:

    stty -echo;tput smcup;echo evil commands
    expected command
    

Those are just two really rough examples, but show the potential of what can be done to obfuscate commands. Note that it likely doesn't hide from ~/.bash_history unless the hidden commands specifically delete/modify it's contents.

You should assume that there are other ways to do this.


Mitigation

I recommend using an addon to disable clipboard manipulation. There are unfortunately ways to get around that, so I'd recommend pasting everything into a GUI text editor before it goes into your terminal, or anywhere.

You need to verify what you're doing. If you don't understand each individual command, you should google it. This is proper tinfoil hattery because copy and pasting can force the commands to auto-execute on many Linux flavors.


Repairing your Linux installation

You might not have any idea how deep the rabbit hole goes. Unless you have the time and effort to put into it, I'd suggest you just nuke from orbit, unless you have important files. If you have important files, just back up the non-executable stuff (no pdfs, documents, etc), and then nuke from orbit.

If you have PDFs, you can convert the PDF to post-script, or copy and paste the contents into a text file. With documents, copy and paste the text and format it later.

20
  • 8
    But in all these cases, the website is putting actual plain text in the clipboard. So upon pasting, you'll see the real command. The OP implies that the added/altered command was somehow invisible in the pasted terminal window.
    – JDługosz
    Feb 14, 2016 at 20:20
  • 34
    @JDługosz if it's got a line break it will run immediately in your terminal. Who cares if you can see it or not. The damage is already done. Feb 14, 2016 at 20:40
  • 4
    Nice coverage on the terminal part, though I should point out that there is no way to really disable pasting crap into the clipboard in general. Even assuming the app (here the browser) is trusted and provides no API for untrusted principals to programmatically tamper with the clipboard, sites could use clever CSS tricks to make some invisible text be selectable over a background image containing text rendered with the user's assumed font on the fly. Feb 14, 2016 at 23:07
  • 4
    @SteveDL /sadface. At least now I know my tinfoil clipboard practice really paid off after all these years. I actually verify everything in my clipboard before even putting it anywhere. Feb 14, 2016 at 23:15
  • 7
    @MarkBuffalo :-) I would recommend techies to use a "buffer", aka a text editor, and to verify they're happy with what they'll type line by line. Beginners.. well given the low volume of attacks out there and the level of confusion that this kind of details can cause I'd say "just copy paste whatever you like, but from trusted sites". Feb 14, 2016 at 23:17
38

There is a risk. Websites can use CSS and JavaScript to hide things and then when you copy from that website, you actually copy what they want. @Gumbo provided the example: https://thejh.net/misc/website-terminal-copy-paste.

The fix: Don't copy and paste from websites you don't trust. Or visit them.

8
  • 4
    That's hideous. Feb 14, 2016 at 19:53
  • 2
    Isn't it though?
    – h4ckNinja
    Feb 14, 2016 at 23:08
  • 2
    The good idea is to trust no one. We should discourage people not from exploring the 'net, but from having "I hereby trust you" attitude towards everything they read (and people's browsers from having the like attitude towards everything they open). Did I trust SE first time google pointed me to it? Hell no! Do I now? Well... Feb 15, 2016 at 6:08
  • 3
    The less horrible alternative is, don't copy and paste directly from the web site to your terminal - paste it into a text editor first and review it. I would think that's a pretty obvious remedy. Feb 15, 2016 at 19:01
  • 1
    @EugeneRyabtsev The answer should at most be "sometimes". (Pay special attention to the leave upvoted answer.)
    – jpmc26
    Feb 16, 2016 at 7:27
20

Yes, cutting and pasting commands from untrusted web sites can be dangerous. The text you paste will always contain the text you copy, but can have more text before, in between or after that.

On the web page this is done by CSS. Just make the extra text invisible. It will still be copied.

When you paste it in a terminal, the extra text will be shown. However, if it contains a newline it will be immediately executed and the damage will be done. Also, it can contain commands to clear away the extra text.

To avoid the danger paste the text into a text editor. Look it over. And then copy it from the editor to the terminal.

Just... make sure you understand the command well enough that you know it is safe. The other danger of pasting unknown commands is that you can simply don't understand what you are doing.

5
  • 1
    Excellent pratical comment. One of my mantra to my trainees is: more important than finding web pages, is understanding what is in there. Often there are mistakes, a critical step is missing, or it does not really applies to what you need. Feb 15, 2016 at 8:55
  • 4
    Pasting into a text editor is a good general rule as even a well-intentioned site in a well-intentioned browser can copy a linefeed without it being obvious, and you may well want to edit the code. It may or may not protect against malice or bugs related to character encoding, depending on how clever your editor tries to be.
    – Chris H
    Feb 15, 2016 at 8:55
  • (modded you both up) Feb 15, 2016 at 8:55
  • Stig, I disagree with the notion that "the text you paste will always contain the text you copy". Feb 15, 2016 at 9:29
  • You don't need an actual text editor. If you don't trust what you're pasting, just run cat, so your paste goes into its stdin. Or cat > /dev/null if it's something that will cause too much clutter when cat writes it out again. This could possibly still be dangerous if the paste contains an embedded ctrl-D or ctrl-C. It's unlikely that anything that would exit cat can do something useful if pasted directly into a shell. Hmm, maybe start out with cat\n, which does nothing if already in cat... So this isn't totally safe, but better than something that's too much work to do. Feb 15, 2016 at 20:53
17

Besides the excellent answers above, I would add that besides malicious purposes, there are also issues copying&pasting from websites or PDFs due to character encoding.

You may be thinking you are pasting a group of characters, and due to character encoding, be pasting a different things, and having issues difficult to track down.

syslog fails to start

From the thread, the OP thought he had pasted:

file("/var/log/cisco/cisco.log");

But in reality he pasted something like this:

file(▒~@~\/var/log/cisco/cisco.log▒~@~]); 
3
  • 8
    Of note: github.com/reinderien/mimic - the horrific things that can be done to prevent mindless copy-pasta. Feb 14, 2016 at 21:02
  • I find this problem with PDFs but never with websites. Feb 15, 2016 at 13:26
  • Thanks for the heads up @aitchnyu, it is well remembered. I added it to the answer. Feb 16, 2016 at 11:45

Not the answer you're looking for? Browse other questions tagged .