# The Linux Virus Writing And Detection HOWTO

#### post link-time code modification of ELF executables under Linux/i386

Abstract
This document describes how to write parasitic file viruses infecting ELF executables on Linux/i386. Though it contains a lot of source code, no actual virus is included. Every mentioned infection method is accompanied with a practical guide to detection. Unfinished snapshot taken on 2002-04-09. Sinners can repent, but stupid is forever.

# Introduction

| You cannot have a science without measurement.
| R. W. Hamming

Writing a program that inserts code into another program file is one thing. Writing that program so that it can be injected itself is a very different art. Although this document shows a lot of code and technique, it is far from being a "Construction Kit For Dummies". You can't build a working virus just by copying whole lines from this text. Instead I'll try to show how things work. Translation of infecting code to a assembly is left as a (non-trivial) exercise to the reader.

An astonishing number of people think that viruses require secret black magic. Here you will find simple code that patches other executables. It is not hard to write a virus (once you have a good understanding of assembler, compiler, linker and operating system). It's just hard to let it make any impact.

Regular users can't overwrite system files (at least under serious operating systems). So you need root permissions. You can either trick the super user to run your virus. Or combine it with a root-exploit. But since all popular distributions come with checksum mechanisms, a single command can detect any modification. Unless you implement kernel-level stealth functionality…

I do believe that free software is superior, at least in regard to security. And I strongly oppose the argument that Linux viruses will flourish once it reaches a critical mass of popularity. On the contrary I question the credibility of people whose income relies on widespread use of ridiculously insecure operating systems.

This document is my way to fight the FUD. Use the information presented here in any way you like. I bet that Linux will only grow stronger.

## Behind the stages

All sections titled "Output" are real product of source code and shell scripts included in this document. Most numbers and calculations are processed by a Perl script parsing these output files. The document itself is written in DocBook, a SGML document type definition. Conversion to HTML is the last step of a Makefile that builds and runs all examples.

I used an installation of RedHat 7.2 for development All required tools are contained on the freely downloadable CDs.

This document is not yet officially accepted by LDP. You might want to look at the discussion on LDP-discuss. For the time being you will find complete source of this release and all previous versions here.

## Copyright Information

This document is copyrighted (c) 2002 Alexander Bartolich and is distributed under the terms of the Linux Documentation Project (LDP) license, stated below.

Unless otherwise stated, Linux HOWTO documents are copyrighted by their respective authors. Linux HOWTO documents may be reproduced and distributed in whole or in part, in any medium physical or electronic, as long as this copyright notice is retained on all copies. Commercial redistribution is allowed and encouraged; however, the author would like to be notified of any such distributions.

All translations, derivative works, or aggregate works incorporating any Linux HOWTO documents must be covered under this copyright notice. That is, you may not produce a derivative work from a HOWTO and impose additional restrictions on its distribution. Exceptions to these rules may be granted under certain conditions; please contact the Linux HOWTO coordinator at the address given below.

In short, we wish to promote dissemination of this information through as many channels as possible. However, we do wish to retain copyright on the HOWTO documents, and would like to be notified of any plans to redistribute the HOWTOs.

If you have any questions, please contact `<linux-howto@metalab.unc.edu>`

## Disclaimer

No liability for the contents of this documents can be accepted. Use the concepts, examples and other content at your own risk. As this is a new edition of this document, there may be errors and inaccuracies, that may of course be damaging to your system. Proceed with caution, the author does not take any responsibility.

Who am I kidding? This is dangerous stuff! Stop reading immediately or risk lethal pollution of your systems!

All copyrights are held by their respective owners, unless specifically noted otherwise. Use of a term in this document should not be regarded as affecting the validity of any trademark or service mark.

Naming of particular products or brands should not be seen as endorsements.

You are strongly recommended to take a backup of your system before major installation and backups at regular intervals.

## New Versions

2002-03-09. Unfinished excerpt sent to Linux Documentation Project.

2002-03-11. Unfinished excerpt sent to Linux Documentation Project.

- Section One step closer to the edge rewritten & finished.

2002-03-14. Genie escaped the bottle.

- Added epigraphs to all sections, removed one offending paragraph on the way.

- Added example for large scale scanning in The plan.

- Started section The entry point.

- Started Credits.

2002-03-15. I predict that today will be remembered until tomorrow.

- First working example in The entry point.

- Lots of small fixes about everywhere.

- Renamed from "The Linux Virus Writing HOWTO".

2002-03-17. What we do not understand we do not possess.

- Added section Before we start.

- Finished section The entry point. Fixed a silly bug in the output of the heuristic scanner.

- Started Additional code segments.

2002-03-19. The mosquito exists to keep the mighty humble.

- Tuned Freedom is security.

- Changed The plan and Patching section headers to make infected executables safe for strip. Bug resulted from sloppy reading of the holy text.

- Started section Remote shell trojan (RST).

2002-03-20. Calm down, it's *only* ones and zeroes.

- Finished Remote shell trojan (RST).

2002-03-24. Caution: Keep out of reach of children.

- Finished Additional code segments. Had to restructure One step closer to the edge to reuse framework.

2002-03-28. As a computer, I find your faith in technology amusing.

- Added In doubt use force to The magic of the Elf.

- Added Another theory to Remote shell trojan (RST).

- Simplified Additional code segments. No need to align everything to 0x1000.

2002-04-01. Be different: conform.

- Changed Target::infection and writeInfection.

- Started Doing it in C.

2002-04-06. Deliver yesterday, code today, think tomorrow.

- Reworked Introduction and Worm vs. virus.

- Progress and lots of fixes on Doing it in C. Had to change writeInfection again.

2002-04-09. Sinners can repent, but stupid is forever.

- Added alignment to One step closer to the edge and Additional code segments.

- Rewrote Dressing up binary code to emit numbers instead of a string literal. The terminating 0 would have complicated The stub.

- Added A section called .text and The stub.

## Credits

Everything in this document is either plain obvious or has been written by someone else long time ago. My meager contribution is nice formatting, reproducibility and the idea to take the subject to mainstream media. But I'm certainly not innovative.

Silvio Cesare. `<silvio@big.net.au>` Founder of the trade. Keeper of the source. Check out http://www.big.net.au/~silvio and admire the release date.

John Reiser. `<jreiser@BitWagon.com>` Found one bug and two superfluous bytes in In the language of evil. Proved that I can't code a straight 23 byte "Hello World".

paddingx. Contributed technical details and implementation for Additional code segments and Doing it in C.

A lot of people helped me shape language and ethical position of this document (sorted by Perl, blame Larry): Charles Curley, Dave Wreski, David Merrill, Gary Lawrence Murphy, Greg Ferguson, Harald Wagener, Ian Turner, Marinho Paiva Duarte, Martin Wheeler, QuickFox of kuro5hin, Steve Sanfratello.

## Feedback

Feedback is most certainly welcome for this document. Please send your additions, comments and criticisms to the following email address: `<alexander.bartolich@gmx.at>`

A few people sent me executables for that other operating system. I am very grateful for this kindness. But you should know that this document is about Linux only.

German speaking readers might find this exchange of emails between Network Associates, Inc. and the admin of my site interesting.

|  | Next >>>
|  | Before we start