Self-replicating Programs (Yes, like viruses)

Self-replicating Programs (Yes, like viruses)

Post by Muskelmann098 on Tue Jun 07, 2011 3:10 pm
([msg=58193]see Self-replicating Programs (Yes, like viruses)[/msg])

Hey,

I've been wanting to dive into C++ and its OOP ways lately, and I came accross something called quines. A quine is a program that is able to print its own source code onto either the console or a file. While this would be very simple in an interpreted language like Perl, in compiled languages it would be pretty hard.

So for my question: Malware wouldn't be able to use this technique, as the source code would have to be compiled (and you don't know if the victim as a compiler installed). Therefore, there must be an easier way to do it. Having the program open, read, and then print itself again is the first that comes to mind, but does anyone here know if that's the easiest or most common method?

Not condoning viruses or anything... i run Windows, so the fewer the better ;)
Muskelmann098
Experienced User
Experienced User
 
Posts: 78
Joined: Mon Feb 02, 2009 9:39 am
Blog: View Blog (0)


Re: Self-replicating Programs (Yes, like viruses)

Post by fashizzlepop on Tue Jun 07, 2011 3:33 pm
([msg=58197]see Re: Self-replicating Programs (Yes, like viruses)[/msg])

I'm pretty sure it just makes copies of itself. Just going to guess, but it doesn't sound like you're very familiar with C programming.
The glass is neither half-full nor half-empty; it's merely twice as big as it needs to be.
User avatar
fashizzlepop
Developer
Developer
 
Posts: 2303
Joined: Sat May 24, 2008 1:20 pm
Blog: View Blog (0)


Re: Self-replicating Programs (Yes, like viruses)

Post by Goatboy on Tue Jun 07, 2011 5:14 pm
([msg=58221]see Re: Self-replicating Programs (Yes, like viruses)[/msg])

fashiz is right. Quines are just interesting challenges for good programmers.
Assume that everything I say is or could be a lie.
1UHQ15HqBRZFykqx7mKHpYroxanLjJcUk
User avatar
Goatboy
Expert
Expert
 
Posts: 2823
Joined: Mon Jul 07, 2008 9:35 pm
Blog: View Blog (0)


Re: Self-replicating Programs (Yes, like viruses)

Post by Muskelmann098 on Wed Jun 08, 2011 10:18 am
([msg=58258]see Re: Self-replicating Programs (Yes, like viruses)[/msg])

Thanks for taking the time to reply guys.

@Fashizzlepop:
You're right - I'm not :) That's why I'm trying to come up with a project to work on and learn from. I'm originally a web developer, so when I started programming on local computers, I started with Perl and learned OOP through Java. I'm starting to realize that decent knowledge in a low-level language like C++ can be useful. The biggest difference is that while PHP and Java have methods for just about everything, C/++ kinda leaves you on your own.

@Goatboy:
It would only really be a challenge in a compiled language though. A Perl script could easily read its own source as a regular ASCI file. Then again, from what I've seen, maybe that's not the point :D

It's good that my logic wasn't that off though.
Muskelmann098
Experienced User
Experienced User
 
Posts: 78
Joined: Mon Feb 02, 2009 9:39 am
Blog: View Blog (0)


Re: Self-replicating Programs (Yes, like viruses)

Post by capcook on Wed Jun 08, 2011 3:22 pm
([msg=58261]see Re: Self-replicating Programs (Yes, like viruses)[/msg])

I've just done (in C).
The code is really simple. Read from the compiled program like it is an ordinary text file.

Some hints:

The variable argv[0] contains the name of the program.
Code: Select all
fp = fopen(argv[0], "r")


To make the new file executable in Linux:
Code: Select all
system(strcat("chmod 755 ", new_fname));


You can use the diff utility (under Linux) to check if the programs are equal.
I think that this is the easier way. Without error checking it takes about 10 lines of code.
capcook
New User
New User
 
Posts: 1
Joined: Thu Jun 02, 2011 5:28 am
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests