10 lines of Python code to write an USB virus! The roommate of small movies are black out

Yesterday in the toilet when the whim, when you insert the usb when the program can automatically execute on usb. Checked, can be found only windows, you can also search for specific (keyword search usb autorun) to. But if I want to, for example, when a usb inserted automatically in the background in usb important files mysteriously copied to a local or uploaded to a server, you need special software to assist.

So I thought to myself, can not write a program in python, let it run in the background. Whenever there u disk inserted, which automatically copy important files.

How to determine the U disk inserted or not?

Learning Python Python Exchange Group 631 441 315 daily share knowledge, information!

 

10 lines of Python code to write an USB virus!  The roommate of small movies are black out

 

 

First, we turn on the computer terminal, enter the / Volumes directory, this time insert U disk, may find it to be mounted in the under this directory, that is, as long as we scan the directory at a fixed time, when there is a new folder in this directory when appears, it is likely to have U disk is inserted.

My design is such that, with time.sleep (3) function, so the program is left running, and look at / Volumes / directory every three seconds, if the extra folder, it will be copied to another folder.

 

10 lines of Python code to write an USB virus!  The roommate of small movies are black out

 

 

Like the title suggests, we really only took 10 lines (11 lines in fact, collected a whole :) completed the "virus." We can find usb directories, all lying half a minute after the insertion of the home directory.

How to copy files selectively?

We just wrote a very simple script to test the feasibility of this idea a bit, but still a problem. All files just the reason why the U disk can quickly copy into it, because only two or three U disk file size is less than 15M. If the target U disk has a lot of movies, music, documents which we do not need, our program should be able to skip them, just select some important documents such as .docx such as .ppt, or just copy those recently modified files, or exclude files larger than 5M of all sizes. We can do it in python? Of course!

All files os.walk recursive folder

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

Here I put a tutorial others. We can probably look, in short, I probably understand that such a thing.

Or give you an example.

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

 

10 lines of Python code to write an USB virus!  The roommate of small movies are black out

 

 

Now we have to test

 

10 lines of Python code to write an USB virus!  The roommate of small movies are black out

 

 

root is stored in the current location, it will ./testwalk/ all folder as the root directory, search down

 

10 lines of Python code to write an USB virus!  The roommate of small movies are black out

 

 

View a separate dirs

 

10 lines of Python code to write an USB virus!  The roommate of small movies are black out

 

 

View files alone

 

10 lines of Python code to write an USB virus!  The roommate of small movies are black out

 

 

Well, we now need a recursive usb folder, find all the file, view the size, if smaller than, for example, 3M, copy it into the home, it is greater than rounding.

shutil module

 

10 lines of Python code to write an USB virus!  The roommate of small movies are black out

 

 

Now we just take for example sub-folders, if you want to copy file1.txt folder2:

 

10 lines of Python code to write an USB virus!  The roommate of small movies are black out

 

 

There are many tools in shutil which is not detailed here.

os.path.getsize () to determine the size

os.path.getsize (filename) returns a unit of byte values, if used to view the file size, we will need to manually write a function that will convert it into easily readable form.

 

10 lines of Python code to write an USB virus!  The roommate of small movies are black out

 

 

Here we can just select the file size is less than 3M, 3M = 3 * 1024kB = 3 * 1024 * 1024byte

 

10 lines of Python code to write an USB virus!  The roommate of small movies are black out

 

 

Combined shutil.copy2 can put files in a selected size of copy into our destination folder for the

How to specify file types

Here it is necessary regular expression to help us.

Regular expressions lot of content, "python core programming" is concerned with the entire chapter, so we do not deeply. Here is the official document, interested can look at.

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

Follows, we let specify a file extension and specify the file size can be copied into our target file:

Do not forget to import re

10 lines of Python code to write an USB virus!  The roommate of small movies are black out

 

Expressions can better specify the file type with a more complex regular

According to the screening file modification time

 

10 lines of Python code to write an USB virus!  The roommate of small movies are black out

 

 

This time I created a file in the directory named newfile

 

10 lines of Python code to write an USB virus!  The roommate of small movies are black out

 

 

In short, the screening modified time for each file can be copied only modify those recently or over a specific period or add a file, this function is useful in specific situations.

to sum up

In fact, from the title so just to attract attention, this is a small program, there can be no virus. I think through this example, the ability to demonstrate strong python file handle, causing everyone's enthusiasm for learning. Above implementations are based on macos, linux should be the same, windows slightly modified successfully.

Guess you like

Origin www.cnblogs.com/qingdeng123/p/11298728.html
Recommended