Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write a USB virus with Python

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces you how to use Python to write a USB virus, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Hello, everyone. I'm back.

Yesterday, when I was going to the bathroom, I wondered if you could automatically execute the program on the usb when you plugged in the usb. Checked, found that only windows can, specific we can also search (search keyword usb autorun) to. However, if I want to, for example, when a usb is plugged in, automatically copy important files in the usb to the local or upload them to a server unknowingly in the background, I need special software assistance.

So I wondered if I could write a program in python and let it run in the background. Whenever a U disk is inserted, the important files are automatically copied.

How to judge whether the USB disk is inserted or not?

First of all, we open the computer terminal, enter the / Volumes directory, and insert the USB disk at this time, we can find that it is mounted under this directory, that is, we just need to scan the directory at a fixed time, and when a new folder appears in this directory, it is likely that a USB drive has been inserted.

My design is to use the time.sleep (3) function to keep the program running and check the / Volumes/ directory every three seconds, and if there are extra folders, copy it to another folder.

As the title shows, we really completed the "virus" in only 10 lines (actually 11 lines). We can find all the directories in usb lying in the home directory half a minute after insertion.

How to selectively copy files?

We just wrote a very simple script to test the feasibility of this idea, but there is still a problem. Just now, I was able to copy all the files in the U disk very quickly, because there are only two or three files in the U disk, and the size is no more than 15m. If there are a lot of movies, music, files that we don't need on the target U disk, our program should be able to skip them and choose only important files such as .docx such as .ppt files, or just copy those files that have been recently modified, or exclude all files larger than 5m in size. Can we do that with python? That's for sure!

All files in the os.walk recursive folder

Http://www.runoob.com/python/os-walk.html

I put a tutorial for someone else here. You can have a general understanding, in short, I probably understand such a thing.

Let me give you an example.

I created a testwalk folder under a certain directory, which contains three files of file123.txt and three folders of folder123, among which there are files file4.txt and folder4 in folder1.

Now let's test it.

Root stores the current location. It will search all the folders under. / testwalk/ as the root directory.

View dirs separately

View files separately

Okay, now we need to recurse the usb folder, find all the file, check the size, if it's less than, say, 3m, copy it into home, and drop it if it's greater than that.

Shutil module

Now let's take the folder just now as an example. If you want to copy file1.txt to folder2:

There are many other tools that can be used in shutil, which are not detailed here.

Os.path.getsize () determines the size

Os.path.getsize (filename) returns a value in byte. To view the file size, we need to manually write a function to convert it into an easy-to-read form.

Here we just need to select a file size smaller than 3m, 3M = 3 * 1024kB = 3 * 1024*1024byte

Combined with shutil.copy2, files of the selected size can be copied to our target folder.

How to specify a file type

Regular expressions are needed to help us here.

There are a lot of regular expressions, and there's a whole chapter in "python Core programming," so we won't go any further. Here are the official documents, which you can take a look at if you are interested.

Https://docs.python.org/2/library/re.html

As follows, we let the specified file suffix and the specified file size be copied into our target file:

Don't forget to import re

File types can be better specified with more complex regular expressions

Filter files based on modification time

At this point, I created a file in the directory called newfile.

In summary, filtering the modification time of each file can copy only those files that have been modified or added recently, or for a specific period of time, which is useful in certain situations.

In fact, the title is just to attract people's attention, this is a Mini Program, not to mention the virus. I would like to use this example to show the powerful ability of python for file processing and arouse everyone's enthusiasm for learning. The above implementation is based on macos,linux should be the same, windows can be successful with a few modifications.

On how to use Python to write a USB virus to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report