[:it]
Accantonate wget e curl, i client SOAP e WSDL, i server WebDAV, le API REST e i callback JSON; quando si tratta di trasferire dati dai siti web al vostro computer, quella roba appiccicosa che unge gli ingranaggi è copia e incolla .
Questo lato dei guanti aptici, Ctrl+C e Ctrl+V è quanto di più vicino possiamo immaginare per raggiungere e afferrare qualcosa dal web. È la cyber-presa che cyber-impari nella tua cyber-infanzia e mai cyber-dimentichi perché la cyber-ripeti all’infinito.
La ripetizione c’insegna che ciò che entra nella nostra mano quando Ctrl+C (afferriamo qualcosa), esce dalla nostra mano quando Ctrl+V (rilasciamo).
Ma cosa succede se non accade?
E se allunghi la mano per afferrare una mela e quando la apri trovi una pera? O un piranha?
Pastejacking con JavaScript
JavaScript è un linguaggio di programmazione che può essere integrato nelle pagine web HTML e, forse più di ogni altra tecnologia, è quello che ha trasformato la rete da una raccolta di documenti da poter leggere in un insieme di applicazioni da poter utilizzare.
Non può uscire dal browser e mettere cose nel computer, ma entro i confini della modalità sandbox di una pagina web, può accedere a tutte le potenti funzionalità che rendono possibile qualsiasi cosa: da Nyan Cat a Gmail (e, quando siete completamente nyanizzati Chrome Experiments.)
Con il vostro permesso può attivare notifiche push e geolocalizzazione e senza il vostro permesso JavaScript è in grado di memorizzare megabyte di dati nella cache del browser, aprire finestre, spostare oggetti all’interno della pagina, disegnare oggetti sui canvas virtuali, memorizzare i tasti premuti e tracciare il mouse.
E, grazie a una funzione chiamata execCommand('copy')
, è anche possibile incollare i cyber-pirhana negli appunti.
Un’ottima dimostrazione di come farlo e perché sia una cattiva idea è stata messa insieme dall’hacker Dylan Ayrey su Github e sul suo sito personale security.love .
Nella demo gli utenti sono invitati a copiare il testo echo "not evil"
e ad assistere con orrore che ciò che realmente incollano è il crudelmente diverso echo "evil"\n
.
Il comando execCommand('copy')
che esegue questa magia deve avere un trigger, noto come ‘evento’ per essere eseguito, in modo che il codice di Ayrey utilizzi l’evento keydown
che viene attivato quando si utilizza la scorciatoia da tastiera Ctrl+C. Il codice attende quindi 0,8 secondi e passa il testo alla clipboard.
I frammenti di testo nell’esempio non sono solo parole ma comandi veri e propri che possono essere eseguiti all’interno di una finestra di terminale (quella misteriosa, monotona finestra nera con testo bianco che i ‘power user’ non vedono mai e i veri geek utilizzano per completare il lavoro).
L’\n
alla fine di echo "evil"\n
è un ritorno a capo e se si digita un ritorno a capo in una finestra di terminale verrà eseguito immediatamente il comando precedente.
In altre parole Ayrey vi ha offerto qualcosa che non verrà eseguito fino a quando non gli si dice di farlo, per poi sostituirlo, alle vostre spalle, con qualcos’altro che verrà eseguito non appena s’incolla.
Fortunatamente per coloro che utilizzano l’esempio di Ayrey si tratta di un comando benigno che vien semplicemente eseguito ma ovviamente non si deve farlo: l’autore di un attacco potrebbe altrettanto facilmente farvi pensare che stiate copiando qualcosa di sicuro e sostituirlo con un comando che cancella l’home directory e ruba il file delle password.
Pastejacking con CSS
Se si disattiva completamente Javascript, o si utilizza un componente aggiuntivo del browser come NoScript che consente di scegliere quando eseguire Javascript, si sarà immuni dal pastejacking di Ayrey; ma c’è un altro modo per trafugare comandi negli appunti che non si affida a JavaScript.
HTML è il linguaggio con il quale sono scritte le pagine web ma è CSS (Cascading Style Sheets) che determina come si presentano.
È CSS che riorganizza le pagine affinché si adattino a tutto: dai cellulari agli schermi cinematografici, ridimensiona il testo, aggiunge colonne, aggiunge il colore, arrotonda i bordi, dispone loghi e fornisce la spaziatura che i designer amano aggiungere e che i clienti amano chiedere ai designer di rimuovere.
Può anche essere usato per disporre oggetti sulla pagina o, meglio ancora per i molesti pastejacker, al di fuori della pagina dove non si possa vedere.
L’hacker Jann Horn ha un demo che mostra proprio questa tecnica sul suo sito thejh.net.
Nell’esempio di Horn, ciò che sembra essere un comando per copiare un repository git source:
git clone git://git.kernel.org/pub/scm/utils/kup/kup.git
…in realtà è un comando molto più lungo che sì copia un repository git source, ma non prima di aver scritto un avvertimento personalizzato nella prima riga del file delle password.
git clone /dev/null; clear; echo -n "Hello ";whoami|tr -d '\n';echo -e '!\nThat was a bad idea. Don'"'"'t copy code from websites you don'"'"'t trust! Here'"'"'s the first line of your /etc/passwd: ';head -n1 /etc/passwd git clone git://git.kernel.org/pub/scm/utils/kup/kup.git
In sordina, nel codice della pagina, tutto il testo è là, come lo si vede sopra; ma Horn ha utilizzato CSS per mostrare la robaccia a metà dei 100 pixel in alto a sinistra della pagina, dove non è visibile.
Non fidatevi del taglia e incolla dalle pagine web
Per programmatori, sviluppatori, amministratori, hacker e geek di tutti i gusti il web è lo strumento di apprendimento più utile che si possa immaginare. Gli esempi di codice sono ovunque sul web, pronti per essere decifrati, discussi, messi in discussione, vagliati, esaminati e, soprattutto, copiati, incollati ed eseguiti.
Purtroppo, grazie a CSS, non si sa, necessariamente, cosa si stia copiando e, grazie a Javascript, necessariamente, non si sa cosa ci sia negli appunti.
Ci sono terminali che vi avviseranno se state incollando qualcosa che termina con un ritorno a capo e ci sono browser, come Lynx o Mosaic, che possono isolarvi dalla modernità del CSS e Javascript. La miglior difesa, però, è quella che chiunque con la conoscenza e l’inclinazione a copiare e incollare comandi in un terminale dovrebbe già avere impresso nella memoria: non ci si può fidare dell’input dell’utente
Suggerisco di non affidarvi a strumenti di terze parti per salvarvi: date per scontato che tutto sia ostile fino a quando non lo abbiate controllato, o dimostrato che sia OK
Il modo più semplice per farlo è quello d’incollare qualsiasi cosa copiate da una pagina web in qualcosa che non possa eseguire comandi, come Blocco note o TextEdit ed esaminarlo.
Poi copiatelo di nuovo e incollatelo dove lo volete veramente.
(Il post è stato tradotto in italiano grazie all’esplicita autorizzazione di Naked Security )
Fonte: Why you can’t trust things you copy and paste from web pages[:en]
Put away your wget and curl, your SOAP clients and WSDLs, WebDAV servers, REST APIs and JSON callbacks; when it comes to moving data off websites and on to your computer the sticky stuff that greases the wheels is copy and paste.
This side of haptic gloves, Ctrl+C and Ctrl+V is as close as we can get to reaching out and grabbing something off the web. It’s the cyber-grab you cyber-learn in your cyber-infancy and never cyber-forget because you endlessly cyber-repeat it.
Repetition teaches us that what goes in to our hand when we Ctrl+C (grab something) comes out of our hand when we Ctrl+V (let it go).
But what if it didn’t?
What if you reached out to grab one apple but when you opened your hand you had a pear? Or a piranha?
Pastejacking with Javascript
Javascript is a programming language that can be embedded into HTML web pages and, perhaps more than any other technology, it’s what turned the web from a collection of documents you could read into a collection of applications you can use.
It can’t break out from your browser and put things on your computer, but within the sandboxed confines of a web page it can access all sorts of powerful functionality that makes possible everything from Nyan Cat to Gmail (and, when you’re all nyaned-out, Chrome Experiments.)
With your permission it can trigger push notifications and geolocation, and without your permission Javascript can store megabytes of data in your browser’s cache, open windows, move things around the page, draw things on virtual canvases, log your keystrokes and track your mouse.
And, thanks to a function called execCommand('copy')
it can paste cyber-pirhanas to your clipboard too.
An excellent demonstration of how to do this and why it’s a bad idea has been put together by hacker Dylan Ayrey on Github and his personal site security.love.
In the demo, users are invited to copy the text echo "not evil"
and witness with horror as what they actually paste is the cruelly different echo "evil"\n
.
The execCommand('copy')
command that performs this magic has to have a trigger, known as an ‘event’ to run, so Ayrey’s code uses the keydown
event which happens to be triggered when you use the keyboard shortcut for Ctrl+C. The code then waits 0.8 seconds and switches out the text from your clipboard.
The snippets of text in the example aren’t just words, they’re valid computer commands that can be run inside a terminal window (that mysterious, featureless black window with white text that ‘power users’ never see and real geeks use to get work done).
The \n
on the end of echo "evil"\n
is a newline and if you type a newline into a terminal window it will run the preceding command immediately.
In other words Ayrey has offered you something that won’t run until you tell it to and then replaced it behind your back with something else that will run as soon as you paste it.
Luckily for anyone using Ayrey’s example it’s a benign command that ends up getting run, but of course it doesn’t have to be; an attacker could just as easily make you think you’re copying something safe and replace it with a command that deletes your home directory and steals your password file.
Pastejacking with CSS
If you switch off Javascript altogether or use a browser add-on like NoScript that allows you to choose when to run javascript you can render yourself invulnerable to Ayrey’s pastejacking technique, but there’s another way to smuggle commands in to your clipboard that doesn’t rely on Javascript.
HTML is the language the web pages are written in but it’s CSS (Cascading Style Sheets) that determines how they look.
It’s CSS that rearranges pages to work in everything from phones to cinema screens, sizes text, adds columns, adds colour, rounds edges, positions logos, and supplies the white space that designers love to add and clients love to ask designers to remove.
It can also be used to position things on the page or, more usefully for malicious pastejackers, off the page where you can’t see it.
Hacker Jann Horn has a demo that shows just this technique on his website thejh.net.
In Horn’s example, what appears to be a command to copy a git source repository:
git clone git://git.kernel.org/pub/scm/utils/kup/kup.git
…is in fact a much longer command that still copies a git source repository but not before it’s written out a personalised warning alongside the first line of your password file.
git clone /dev/null; clear; echo -n "Hello ";whoami|tr -d '\n';echo -e '!\nThat was a bad idea. Don'"'"'t copy code from websites you don'"'"'t trust! Here'"'"'s the first line of your /etc/passwd: ';head -n1 /etc/passwd git clone git://git.kernel.org/pub/scm/utils/kup/kup.git
Under the hood, in the page’s code, all the text is there as you see it above but Horn has used CSS to display the nasty bit in the middle 100 pixels above and to the left of the page where you can’t see it.
Don’t trust cut and paste from web pages
For programmers, developers, admins, hackers and geeks of all flavours the web is the most useful learning tool imaginable. Examples of code are everywhere on the web ready to be deciphered, discussed, questioned, picked over, picked apart and, above all copied, pasted and run.
Unfortunately, thanks to CSS, you don’t necessarily know what you’re copying and, thanks to Javascript, you don’t necessarily know what’s in your clipboard.
There are terminals that will warn you if you’re pasting something that ends in a newline character, and there are browsers, say Lynx or Mosaic, that can insulate you from the modernity of CSS and Javascript. The best defence, however, is one that anyone with the knowledge and inclination to copy and paste commands into a terminal should know by heart already: you can’t trust user input.
I suggest that you don’t rely on third party tools to save you – just assume that everything is hostile until you’ve sanitised it or proved it’s OK.
The simplest way to do that is to paste anything you copy from a web page into something that can’t run commands, like NOTEPAD or TextEdit and examine it first.
Then copy it again and paste it where you really want it.
(The post has been translated into Italian with explicit permission of Naked Security )
Source: Why you can’t trust things you copy and paste from web pages[:]