10 lines of code--write a USB virus in Python (deepweaver)

Source: Internet
Author: User
Tags local time

Yesterday in the toilet when the whim, when you put the USB plug in, can not automatically execute the program on the USB. Check a bit, found only windows can, specific people can also search (search keywords USB autorun) to. However, if I want to, for example, when a USB plug-in, in the background automatically the important files in the USB to be copied locally or uploaded to a server, you need special software assistance.

So I thought, could you write a program in Python and let it run in the background. Each time a U-disk is inserted, it automatically copies the important files.

How to determine the USB stick insert or not?

First we open the computer terminal, into the/volumes directory, this time to insert a USB flash drive, you can find it is mounted under this directory, that is, we just have to scan the directory at a fixed time, when the directory has a new folder appears, it is likely that a USB flash drive was inserted.

My design is this, with the Time.sleep (3) function, let the program stay running state, and every three seconds to view the/volumes/directory, if the more out of the folder, it will be copied to another folder.

# encoding=utf-8from time import sleepimport os, shutilusb_path = "/Volumes/"content = os.listdir(usb_path) # os.listdir(路径)返回路径下所有文件以及文件夹的名称while True:    new_content = os.listdir(usb_path) #每隔三秒扫描一次/Volumes/    if new_content != content: # 如果发现异常,即多出一个文件夹,则退出        break;    sleep(3)x = [item for item in new_content if item not in content]# 找到那个新文件夹,返回包括新文件夹string类型名称的列表,这个表达方法很pythonicshutil.copytree(os.path.join(usb_path, x[0]), ‘/Users/home/usb_copy‘)# shutil.copytree 把目录下所有东西一股脑复制进/Users/home/usb_copy, # 放进了自己的home目录下

As the headline shows, we really only used 10 lines (actually 11 lines, rounding up a whole:) to complete the "virus". We can find the contents of the USB and lie in the home directory after half a minute of insertion.

How do I select a copy of a file?

We just wrote a very simple script to test the feasibility of the idea, but there are still problems. Just now can be all the files in the USB flash drive in the copy, because there are only two or three files in the USB flash drive, size does not exceed 15M. If there are many movies, music, and other files that we don't need, our program should be able to skip them, just choose some important ones like. docx such as. ppt files, or simply copy those files that have recently been modified, or exclude all files larger than 5M in size. Can we do it in Python? Of course!

Os.walk all files in a recursive folder

How to use Python os.walk () consult me to get

Let me give you an example.

I created the Testwalk folder in a directory, there are file123.txt three files, folder123 three folders, where Folder1 file File4.txt and Folder4

?  testwalk touch file1.txt file2.txt file3.txt?  testwalk mkdir folder1 folder2 folder3?  testwalk cd folder1?  folder1 touch file4.txt && mkdir folder4?  folder1 cd ..# 上面创建了这些文件以及文件夹,你也可以在图形界面上创建# tree 是个很好玩的命令,可以直观地显示文件路径?  testwalk tree ./testwalk/├── file1.txt├── file2.txt├── file3.txt├── folder1│   ├── file4.txt│   └── folder4├── folder2└── folder34 directories, 4 files

Now let's test it.

import osfor root, dirs, files in os.walk("./testwalk/"):    for name in files:        print(os.path.join(root, name))    for name in dirs:        print(os.path.join(root, name))-----------------------------------------------------------------------------运行结果:./testwalk/folder1/file4.txt./testwalk/folder1/folder4./testwalk/file2.txt./testwalk/file3.txt./testwalk/file1.txt./testwalk/folder2./testwalk/folder3./testwalk/folder1

Root is the current location, and it will take all the folders under the./testwalk/as the root directory and search down

for root, dirs, files in os.walk("./testwalk/", topdown=False):    print(root)./testwalk/folder2./testwalk/folder3./testwalk/folder1/folder4./testwalk/folder1./testwalk/

View Dirs Separately

for root, dirs, files in os.walk("./testwalk/"):    for name in dirs:        print(os.path.join(root, name))./testwalk/folder2./testwalk/folder3./testwalk/folder1./testwalk/folder1/folder4

View files separately

for root, dirs, files in os.walk("./testwalk/", topdown=False):    for name in files:        print(os.path.join(root, name))./testwalk/file2.txt./testwalk/file3.txt./testwalk/file1.txt./testwalk/folder1/file4.txt

OK, we now need to recursive USB folder, find all the file, look at the size, if less than, such as 3M, copy into the home, greater than to go.

?? Here is still to recommend my own built
Python Development Learning Group: 725479218, the group is learning Python development, if you are learning Python, small series welcome you to join, everyone is the software Development Party, Do not regularly share dry goods (only Python software development related), including my own 2018 of the latest Python advanced information and high-level development tutorials, welcome to advanced and into the small partners to deep python

Shutil Module
import shutil>>> help(shutil)>>> dir(shutil)[‘Error‘, ‘ExecError‘, ‘SpecialFileError‘, ‘WindowsError‘, ‘_ARCHIVE_FORMATS‘, ‘_BZ2_SUPPORTED‘, ‘_ZLIB_SUPPORTED‘, ‘__all__‘, ‘__builtins__‘, ‘__doc__‘, ‘__file__‘, ‘__name__‘, ‘__package__‘, ‘_basename‘, ‘_call_external_zip‘, ‘_destinsrc‘, ‘_get_gid‘, ‘_get_uid‘, ‘_make_tarball‘, ‘_make_zipfile‘, ‘_samefile‘, ‘abspath‘, ‘collections‘, ‘copy‘, ‘copy2‘, ‘copyfile‘, ‘copyfileobj‘, ‘copymode‘, ‘copystat‘, ‘copytree‘, ‘errno‘, ‘fnmatch‘, ‘get_archive_formats‘, ‘getgrnam‘, ‘getpwnam‘, ‘ignore_patterns‘, ‘make_archive‘, ‘move‘, ‘os‘, ‘register_archive_format‘, ‘rmtree‘, ‘stat‘, ‘sys‘, ‘unregister_archive_format‘]

Well, if you don't understand, you still have to read the official documents.

Now let's take an example of the folder we just had, if you want to copy File1.txt to Folder2:

>>> shutil.copy2(‘./file1.txt‘, ‘./folder2‘)------------------------------------------------我是分割线-----------?  folder2 lsfile1.txt

There are also many use tools in the shutil inside this is not detailed here.

Os.path.getsize () Judging size
Os.path.getsize (file name) returns a number in bytes, and if used to view the file size, we need to manually write a function to convert it into an easy-to-read form.

movie = /Users/home/somemovie.rmvbdef convert_bytes(num):#   this function will convert bytes to MB.... GB... etc    for x in [‘bytes‘, ‘KB‘, ‘MB‘, ‘GB‘, ‘TB‘]:        if num < 1024.0:            return "%3.1f %s" % (num, x)        num /= 1024.0def getDocSize(path):    try:        size = os.path.getsize(path)        return size    except Exception as err:        print(err)print(convert_bytes(getDocSize(movie)))

Results:
1.3 GB
[Finished in 0.1s]

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

for root, dirs, files in os.walk(os.path.join(usb_path, x[0])): #MyUSB location    for name in files:        file = os.path.join(root, name)        if os.path.getsize(file) < 3*1024*1024:            shutil.copy2(file, target_folder)

Combined with shutil.copy2, you can copy files of selected size into our target folder.

How to specify file types

We need regular expressions to help us out here.

There are a lot of regular expressions, and "python core programming" uses a whole chapter, so we don't go into it. Here are the official documents, which are interesting to look at.

7.2. Re-regular expression Operations-python 2.7.14 documentation

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

Don't forget to import the RE

import re...regex_filename = re.compile(‘(.*zip$)|(.*rar$)|(.*docx$)|(.*ppt$)|(.*xls$)‘)for root, dirs, files in os.walk(os.path.join(usb_path, x[0])): #MyUSB location    for name in files:        file = os.path.join(root, name)        if regex_filename.match(file) and os.path.getsize(file) < 1024*1024:            shutil.copy2(file, target_folder)

More complex regular expressions can be used to better specify file types

Filter files based on modified time
>>> from os.path import *>>> help(getmtime)getmtime(filename)    Return the last modification time of a file, reported by os.stat().>>> help(getctime)getctime(filename)    Return the metadata change time of a file, reported by os.stat().

At this time I created a file under the directory called NewFile

>>> getctime("newfile")1522746383.716875# 我们可以看到返回的time是从某个时间到现在的秒数,如需阅读,我们需要time.ctime来转换>>> import time>>> time.ctime(1522746383.716875)‘Tue Apr  3 17:06:23 2018‘ # 这就是刚才创建的时间啦>>> help(time.ctime)ctime(...) # 查看文档    ctime(seconds) -> string    Convert a time in seconds since the Epoch to a string in local time.    This is equivalent to asctime(localtime(seconds)). When the time tuple is    not present, current time as returned by localtime() is used.

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

Summarize

In fact, the title is just to attract attention, this is a small program, not to mention the virus. I would like to use this example to demonstrate Python's strong ability to file processing and to spark enthusiasm for learning. The above implementations are based on the macos,linux should be the same, Windows a little modification can also succeed.

10 lines of code--write a USB virus in Python (deepweaver)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.