Information Security Stack Exchange is a question and answer site for information security professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

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?

share|improve this question

marked as duplicate by Ohnana, Deer Hunter, Xander, woliveirajr, Dmitry Grigoryev Feb 16 at 14:40

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

14  
Repair is done by wiping disk and reinstalling OS. – Neil Smithline Feb 14 at 17:38
9  
Solution: use lynx. No JavaScript, no CSS oddities there. – Deer Hunter Feb 14 at 17:58
5  
Here is an example: thejh.net/misc/website-terminal-copy-paste – Gumbo Feb 14 at 18:23
2  
Use a browser plugin that disables access to the copy buffer. :) – Mark Buffalo Feb 14 at 18:51
9  
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... – Deer Hunter Feb 15 at 9:28
up vote 76 down vote accepted

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 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.

share|improve this answer
7  
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 at 20:20
30  
@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. – Neil McGuigan Feb 14 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. – Steve DL Feb 14 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. – Mark Buffalo Feb 14 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". – Steve DL Feb 14 at 23:17

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.

share|improve this answer
4  
That's hideous. – Mark Buffalo Feb 14 at 19:53
2  
Isn't it though? – Micheal Feb 14 at 23:08
1  
-1 for not visiting websies. – Eugene Ryabtsev Feb 15 at 5:25
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... – Eugene Ryabtsev Feb 15 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. – Chris Johnson Feb 15 at 19:01

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.

share|improve this answer
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. – Rui F Ribeiro Feb 15 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 at 8:55
    
(modded you both up) – Rui F Ribeiro Feb 15 at 8:55
    
Stig, I disagree with the notion that "the text you paste will always contain the text you copy". – Deer Hunter Feb 15 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. – Peter Cordes Feb 15 at 20:53

Besides the excellent answers above, I would add that besides malicious purposes, there are also problems 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▒~@~]); 
share|improve this answer
8  
Of note: github.com/reinderien/mimic - the horrific things that can be done to prevent mindless copy-pasta. – Deer Hunter Feb 14 at 21:02
    
I find this problem with PDFs but never with websites. – aitchnyu Feb 15 at 13:26
    
Thanks for the heads up @aitchnyu, it is well remembered. I added it to the answer. – Rui F Ribeiro Feb 16 at 11:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.