30

How can normal files supported in the Windows operating system hide a virus?

Sometimes opening a .txt , .jpg or .docx files leads to running a virus. How is this possible?

Peter Mortensen
  • 885
  • 5
  • 10
  • 2
    See also on [su]: [Is it possible to store arbitrary data in an image file?](http://superuser.com/questions/357869/is-it-possible-to-store-arbitrary-data-in-an-image-file) – bwDraco Jul 24 '14 at 21:12
  • See also on [so]: [How can a Virus Exist in an image?](http://stackoverflow.com/questions/9675941/how-can-a-virus-exist-in-an-image) – Ajedi32 Jul 25 '14 at 14:05
  • See also [Does a virus need to be clicked on to function?](https://security.stackexchange.com/questions/63821/does-a-virus-need-to-be-clicked-on-to-function/63824?noredirect=1#comment103484_63824) asked a few days ago. – Steve Dodier-Lazaro Jul 25 '14 at 17:15
  • See also [How can a font be used for privilege escalation?](http://security.stackexchange.com/q/91347/32746). – WhiteWinterWolf Dec 30 '15 at 10:50

6 Answers6

20

This is possible thanks to OLE (Object Linking and Embedding) technique. It is intended to share information between applications that run on Microsoft Windows operating system. Mainly, it allows to embed objects in documents.

Official Microsoft Documentation explains the benifits of OLE. But as any other concept, it could be used for a neferaious intention.

Maybe the easiest way to explain this is the figure below: by dragging and dropping the executable icon of Notepad on an opened Wordpad file and saving, the double-click on the Wordpad file will lead to open the Notepad application. The figure below explains better the idea:

enter image description here
You can save the document in .txt format. It will look as a normal benign file. However, if you check its properties, you will find its real extension is SHS. You can imagine adding any commands to the file: such as formatting some disk partitions.

  • Wow !! Incredible !! Thank you very much for these information. –  Jul 24 '14 at 14:55
  • 5
    This has very little to do with OLE, and everything to do with super-hidden file extensions. – Ben Voigt Jul 24 '14 at 19:24
  • @BenVoigt the example illustrate a fact you can do only thanks to OLE. Please, read the first link I pointed. –  Jul 24 '14 at 19:30
  • @BenVoigt the example illustrate a fact you can do only thanks to OLE. –  Jul 24 '14 at 19:32
  • The question already accepts that unusual formats can do strange things, and asks how "normal" files can. Your answer is really all about hiding an unusual file in such a way that it appears "normal". It's social engineering, based on the `NeverShowExt` registry setting for the file type, not a technical exploit. – Ben Voigt Jul 24 '14 at 19:34
  • Also, although "shell scrap" files use an OLE container format, the execution of the embedded program is due to the scrap handler logic. Merely being an OLE container does not cause execution of embedded programs. If the shell scrap files used a TAR format, they'd act exactly the same way. – Ben Voigt Jul 24 '14 at 19:39
  • @BenVoigt Scrap files are things you can set from the windows registry keys, just to show or hide files extensions. But these scrap notions is also there **ONLY THANKS** to `OLE` notion in windows operating system. –  Jul 24 '14 at 19:48
  • 5
    But what you've done is in no way specific to OLE. Make a copy of `C:\Windows\System32\cmd.exe` and rename it to `urgent.txt.pif` Now it acts just like your scrap file (super-hidden extension, runs code when opened) with no OLE in use whatsoever. Blaming OLE for this makes no more sense as blaming WordPad. The problem is (1) super-hidden extensions and (2) a scrap handler that opens its files by running the embedded program, and a PIF file association that opens its files with `"%1" %*`. – Ben Voigt Jul 24 '14 at 19:50
  • 1
    @BenVoigt If you read the from the official link I pointed, you will guess that what you mentioned in your last comment is done thanks to OLE :) –  Jul 24 '14 at 19:52
  • 3
    Nope, PIF files do what they do because the association (on default Windows install) is `HKCR\piffile\shell\open\command = "%1" %*` No OLE whatsoever. (Of course, the registry was invented to provide a place to put OLE settings, but honestly it can't be considered a part of OLE) – Ben Voigt Jul 24 '14 at 19:53
  • 7
    I don't understand this answer. So I drag and drop notepad.exe into wordpad and I get a document with a shortcut to notepad.exe. Where is the .shs extension coming into play? And moreover, I can open notepad.exe from the wordpad document only if I explicitly double click on it, and even then Windows 7 shows a confirmation dialogue before actually executing it. Saving the wordpad document in txt format makes a 1 byte text file with all the formatting (as well as the OLE shortcut to notepad.exe) removed. Why is the answer talking about shs? – user13267 Jul 25 '14 at 06:49
  • I don't understand either. This seems terribly incomplete to me, and the technique you describe is so old that the affected products have been unsupported for years! – Steve Dodier-Lazaro Jul 25 '14 at 17:15
  • 1
    @SteveDL OLE is old. Microsoft almost abandoned it, but now it uses it again. –  Jul 25 '14 at 17:16
  • @begueradj I can't find any information on the MS site. If you have a link for a currently-used version of OLE could you please verify if the vuln you described still applies and then update your answer? – Steve Dodier-Lazaro Jul 25 '14 at 17:37
  • This answer is forgetting exploits against the opening program. A vulnerability in adobe reader can result in a valid PDF running code. – toasted_flakes Jul 25 '14 at 20:29
  • @begueradj: That's absurd. The *name* OLE has been superceded, but COM, DCOM, OLE Automation, and ActiveX are all the same technology. – Ben Voigt Jul 25 '14 at 20:53
  • @user13267 The concept (and the image) was taken from here: [Scrap Files Can Tear You Up](http://www.pc-help.org/security/scrap.htm) For what is worth, whoever took the screenshot used Windows 95 (solid color title bar; default WordPad font is Times New Roman; 16-color Notepad icon; the open button icon is missing a black pixel - comparison [here](http://i.stack.imgur.com/4YCVe.png)). The shell scrap feature was [removed](http://technet.microsoft.com/en-us/magazine/2008.02.windowsconfidential.aspx) with Windows Vista; currently supported operating system aren't affected. – and31415 Aug 02 '14 at 17:20
  • I think to add better clarity to this answer you should understand the file itself is not a virus or that this is only made possible via OLE. The file is simply containing malicious instruction that exploits the software opening it. In this example case this is taking advantage of an exploitable "feature" of WordPad to execute it's content. Together all the conditions make the virus. – Bacon Brad Mar 14 '16 at 20:23
17

Sometimes opening a .txt , .jpg or .docx files leads to running a virus. How come is this possible ?

To add to the answer to a similar question (thanks for finding it, Tcholas!):

You are correct in thinking that a virus in and on itself is harmless. A virus sitting in a file somewhere is no immediate threat to your computer. But when you open a file that contains a virus, you are actually running a program to open that file.

When you ask your operating system (say, Windows) to open a file, it basically does the following:

  1. Check what kind of file it is. This can be by reading the extension (.txt, .doc) or by reading some of the data (zip files have "PK" in the first bytes).

  2. Knowing the file's type, it finds the 'default program' to open it with. This is stored in a database somewhere on your computer.

  3. Execute the program and load the data file in it.

So when you double-click "mynotes.txt", Windows looks up the default program for .txt files, probably "notepad.exe", and then executes "notepad.exe" and makes it load "mynotes.txt". When you double-click a JPEG image, it'll load an image viewer. Even something as simple as plugging a USB pen drive into your laptop will run some code.

The devil is in any bugs/errors that this loading program may have. Viruses use these 'holes' to trick the operating system into executing different code and modify/subvert part of your computer. When the software maintainers hear about the vulnerability, they (hopefully) fix it and issue a security update.

This also means that viruses tend to target specific programs. Something that works on MS Word will probably not work on OpenOffice. Same with Acrobat Reader and Evince, Chrome and Internet Explorer, Thunderbird and Outlook, and so on.

tl;dr - When you open a file, you are really opening a program which then opens that file. It's vulnerabilities in that program that allow viruses a shot at doing their thing.

JvR
  • 505
  • 2
  • 7
  • To develop a bit further on the "holes": programs load a file directly in the computer's memory, and in the memory everything is series of 0's and 1's. The only way the program knows how to interpret those 0's and 1's is because it *assumes* that they have a certain structure (defined by the file format, so .txt in the example above). The same data could also be interpreted as instructions for the computer, which combined together do something undesirable (we call that a "malicious charge"). – Steve Dodier-Lazaro Jul 25 '14 at 17:21
  • As explained by others, the operating system is tricked into reading these 0's and 1's as if they were instructions by exploiting mistakes made by the programmers of a specific application or library (for instance the OpenSSL Heartbleed bug touched many applications because they all used buggy code from the OpenSSL library). When you give the "right" data to the buggy program, it will perform unexpected operations, and talented hackers essentially calculate what data to give for the program to end up execute the malicious charge instead of the next expected instructions. – Steve Dodier-Lazaro Jul 25 '14 at 17:25
  • @SteveDL: To be sure, Heartbleed was an information disclosure vulnerability, *not* a remote execution exploit. So it's rather useless for a virus. – Ben Voigt Jul 25 '14 at 20:54
  • @BenVoigt you're right indeed. I wanted to illustrate the exploitation of bugs with a famous exploit but should have picked a RCE exploit for that... Thanks for pointing this out! – Steve Dodier-Lazaro Jul 25 '14 at 21:15
  • So would it be safer if you were to open Notepad, then open the file from there with Ctrl+O instead? Could a virus still be executed, or would that subvert it? – Dakeyras Jul 26 '14 at 09:35
  • @Dakeyras Whole articles could be written on that. :) The brief, answer is: no, it would not neuter a virus. More accurately: it depends, in the way that it may matter where, exactly, on a landmine you step to determine whether it goes off. I'm going to let the experts with the drones figure that one out, though! – JvR Jul 26 '14 at 13:39
6

One possibility is by exploiting overflow vulnerabilities. When opening the image, the software failure will "throw" the virus into unauthorised memory sections that may be executed by the system.

Here you have a description by Symantec of a vulnerability that exploited Internet Explorer in this sense.

Also, this question was answered in Stack Overflow.

Matheus Portela
  • 281
  • 1
  • 4
4

One possibility is Unicode shenanigans.

Unicode supports displaying many languages, including those written left to right, and those written right to left. One way it does this is using special characters, including [U+202E] (left-to-right override (LRO)).

Windows supports Unicode, including in filenames.

You see a file on your desktop: evilexe.txt. It looks like a text file, but it's not. It's really named evil[U+202E]txt.exe.

It can have an icon set to make it look like a text file or JPEG file, but it's really an EXE file.

Peter Mortensen
  • 885
  • 5
  • 10
Murphy
  • 2,175
  • 1
  • 9
  • 10
3

So many ways!

What is the most common type of software flaw that is exploitable?

Buffer overflow.

A buffer overflow is where a program requests a size of memory but then writes more data in that location (overflowing) to the memory next to it.

For example: A program like OpenOffice Writer (a word processor like MS-WORD) (this is pure fiction by the way) has a limit on how large a paragraph could be, let's say it's 65535 characters. A malicious hacker creates a document with a paragraph that is 65555 characters long. The program doesn't have a check in place to see how large a paragraph is - it just loads it into memory. On the 65536th character, instead of an actual character the hacker puts in the byte code for a jump instruction to somewhere later in the hacker's document or anywhere that the hacker knows and controls. When the program accidentally reads the jump instruction that malicious document now controls the processing flow of the application. It's a virus, and you have just been infected.

There are many different types of overflow-able components in a computer so watch out!

If this sounds similar to the Heartbleed exploit it's because it is.

XKCD Heartbleed Explaination

Let's continue.

The Office

Microsoft's Office platform is extremely extensible. It allows you to add both compiled and interpreted code to the program and to documents. The interpreted code, the macro, has a lot of power for productivity purposes, but up until about a decade ago it was also used heavily for propagating malicious code. Not so much any more, but it still pops up. Often a malicious document would have a bit of code that would write a file to your hard drive and then execute it when you opened up the document. Of course most of these guys came packaged in a PowerPoint presentation containing photos of cute cats and the like so it wasn't a total loss when you got pwned.

A lot of larger software packages is generally extendible and can also suffer from similar exploits.

The Web

Oftentimes a hacker will create a file and present that file as an image or as plain text when it isn't. A website author's responsibility is to escape, encapsulate and sandbox any data that they may receive from the internet. It's a hard job.

So a hacker may put a file called "NotA.phpfile.jpg" into a PHP website's form which presents itself as a image/jpeg file, but in reality it's a application/php file. Now when you load the URL that the image was uploaded to, you may have attained control of the website.

Similarly, the website must escape text or it could suffer from SQL injection and data can be stolen.

Best Mom Ever -- drop tables

Other notable things

  • Most modern viruses will use self-replicating encryption type algorithms to try to hide their presence on a computer, mostly from antivirus' and researchers.

Let's look at some technology:

  • Alternate Data Streams (ADS).
    • On NTFS file systems an ADS is like a hidden document attached to a file. This data isn't readily visible to the end user, but it is accessible by programs and the OS. There is an old trick where you can open an ADS in Notepad by typing it into a command prompt. One prominent use of ADS is for keeping track of where files come from, I'm sure you've all been prompted about opening a file that came from The Internet before.
  • OLE (as discussed in alternative answer), and similar integrated variants of object linking and interfacing such as ActiveX, COM, COM+, and DCOM.
  • Registry.
    • A very simple database that can store a small handful of datatypes, but is used extensively. Like for storing settings about what happens when you double click on a desktop icon, or run a program.
Peter Mortensen
  • 885
  • 5
  • 10
  • 2
    As said above, until disproved OLE has been discontinued ages ago. Whatever replacement mechanisms exist do not necessarily share the issues with OLE. – Steve Dodier-Lazaro Jul 26 '14 at 00:36
  • [Where’s the Macro? Malware authors are now using OLE embedding to deliver malicious files - Windows Security Blog](https://blogs.technet.microsoft.com/mmpc/2016/06/14/wheres-the-macro-malware-author-are-now-using-ole-embedding-to-deliver-malicious-files/) – Goldfish Sandwich Aug 24 '17 at 22:16
0

The short OP is asking why clicking on a .txt runs a virus. If a virus changes the associated program for an extension, then your computer can start mad_notepad_that_also_runs_a_virus.exe instead of notepad.exe when clicking on a .txt file.

But clicking on i_love_you.txt.extension_like_that_will_obviously_make_this_computer_explode.exe, even if a very naive behaviour, may happen.

Kevin Fegan
  • 107
  • 5