Python实现向好友发送微信消息优化篇

目录
  • 前言
  • 第二次优化
  • 第三次优化

前言

之前说了怎么写机器码到内存,然后调用。现在说说怎么优化。

用Python发送微信消息给好友

第二次优化

再看一遍c语言的代码

void SendText( wchar_t* wsTextMsg) {
    // 发送的好友,filehelper是文件传输助手
	wchar_t wsWxId[0x10] = L"filehelper";
	WxBaseStruct wxWxid(wsWxId);
	// 发送的消息内容
	WxBaseStruct wxTextMsg(wsTextMsg);
	wchar_t** pWxmsg = &wxTextMsg.buffer;
	char buffer[0x3B0] = { 0 };
	char wxNull[0x100] = { 0 };
	DWORD dllBaseAddress = (DWORD)GetModuleHandleA("WeChatWin.dll");
	// 发消息的函数call地址
	DWORD callAddress = dllBaseAddress + 0x521D30;
	__asm {
		lea eax, wxNull;
		push 0x1;
		push eax;
		mov edi, pWxmsg;
		push edi;
		lea edx, wxWxid;
		lea ecx, buffer;
		call callAddress;
		add esp, 0xC;
	}
}

上面的代码真正发消息的是asm里面的代码,之前的c代码都是在组装内存数据。那我们是不是可以用Python组装数据,只讲下面的汇编转为机器码写入内存调用,这样就少了很多无用的机器码。

改完的SendText函数如下

wchar_t wsWxId[0x10] = L"filehelper";
wchar_t wsTextMsg[0x100] = L"test";
WxBaseStruct wxWxid(wsWxId);
WxBaseStruct wxTextMsg(wsTextMsg);
wchar_t** pWxmsg = &wxTextMsg.buffer;
char buffer[0x3B0] = { 0 };
char wxNull[0x100] = { 0 };
DWORD dllBaseAddress = (DWORD)GetModuleHandleA("WeChatWin.dll");;
DWORD callAddress = dllBaseAddress + 0x521D30;
void SendText() {
    __asm {
        lea eax, wxNull;
        push 0x1;
        push eax;
        mov edi, pWxmsg;
        push edi;
        lea edx, wxWxid;
        lea ecx, buffer;
        call callAddress;
        add esp, 0xC;
    }
}

汇编代码:

[]里面包含的类型和变量名其实就是地址,只需要将地址改成用Python构造的地址就可以了

完整代码如下:

import os
import pymem
import ctypes
import time
def convert_addr(addr):
    if isinstance(addr, int):
        addr = hex(addr)
    if addr.startswith("0x") or addr.startswith("0X"):
        addr = addr[2:]
    if len(addr) < 8:
        addr = (8-len(addr))*'0' + addr
    tmp = []
    for i in range(0, 8, 2):
        tmp.append(addr[i:i+2])
    tmp.reverse()
    return ''.join(tmp)
def WxBaseStruct(process_handle, content):
    struct_address = pymem.memory.allocate_memory(process_handle, 20)
    bcontent = content.encode('utf-16le')
    content_address = pymem.memory.allocate_memory(process_handle, len(bcontent)+16)
    pymem.ressources.kernel32.WriteProcessMemory(process_handle, content_address, bcontent, len(bcontent), None)
    pymem.memory.write_int(process_handle, struct_address, content_address)
    pymem.memory.write_int(process_handle, struct_address+0x4, len(content))
    pymem.memory.write_int(process_handle, struct_address+0x8, len(content)*2)
    pymem.memory.write_int(process_handle, struct_address+0xC, 0)
    pymem.memory.write_int(process_handle, struct_address+0x10, 0)
    return struct_address, content_address
def start_thread(process_handle, address, params=None):
    params = params or 0
    NULL_SECURITY_ATTRIBUTES = ctypes.cast(0, pymem.ressources.structure.LPSECURITY_ATTRIBUTES)
    thread_h = pymem.ressources.kernel32.CreateRemoteThread(
        process_handle,
        NULL_SECURITY_ATTRIBUTES,
        0,
        address,
        params,
        0,
        ctypes.byref(ctypes.c_ulong(0))
    )
    last_error = ctypes.windll.kernel32.GetLastError()
    if last_error:
        pymem.logger.warning('Got an error in start thread, code: %s' % last_error)
    pymem.ressources.kernel32.WaitForSingleObject(thread_h, -1)
    return thread_h
def main(wxpid, wxid, msg):
    process_handle = pymem.process.open(wxpid)
    wxNull_address = pymem.memory.allocate_memory(process_handle, 0x100)
    buffer_address = pymem.memory.allocate_memory(process_handle, 0x3B0)
    wxid_struct_address, wxid_address = WxBaseStruct(process_handle, wxid)
    msg_struct_address, msg_address = WxBaseStruct(process_handle, msg)
    process_WeChatWin_handle = pymem.process.module_from_name(process_handle, "WeChatWin.dll")
    call_address = process_WeChatWin_handle.lpBaseOfDll + 0x521D30
    call_p_address = pymem.memory.allocate_memory(process_handle, 4)
    pymem.memory.write_int(process_handle, call_p_address, call_address)
    format_code = '''
    57
    8D05 {wxNull}
    6A 01
    50
    8D3D {wxTextMsg}
    57
    8D15 {wxWxid}
    8D0D {buffer}
    FF15 {callAddress}
    83C4 0C
    5F
    C3
    '''
    shellcode = format_code.format(wxNull=convert_addr(wxNull_address),
                wxTextMsg=convert_addr(msg_struct_address),
                wxWxid=convert_addr(wxid_struct_address),
                buffer=convert_addr(buffer_address),
                callAddress=convert_addr(call_p_address))
    shellcode = bytes.fromhex(shellcode.replace(' ', '').replace('\n', ''))
    shellcode_address = pymem.memory.allocate_memory(process_handle, len(shellcode)+5)
    pymem.ressources.kernel32.WriteProcessMemory(process_handle, shellcode_address, shellcode, len(shellcode), None)
    thread_h = start_thread(process_handle, shellcode_address)
    time.sleep(0.5)
    pymem.memory.free_memory(process_handle, wxNull_address)
    pymem.memory.free_memory(process_handle, buffer_address)
    pymem.memory.free_memory(process_handle, wxid_struct_address)
    pymem.memory.free_memory(process_handle, wxid_address)
    pymem.memory.free_memory(process_handle, msg_struct_address)
    pymem.memory.free_memory(process_handle, msg_address)
    pymem.memory.free_memory(process_handle, call_p_address)
    pymem.memory.free_memory(process_handle, shellcode_address)
    pymem.process.close_handle(process_handle)
if __name__ == "__main__":
    wxpid = 16892
    wxid = "filehelper"
    msg = "python test"
    main(wxpid, wxid, msg)

第三次优化

直接在Python里写汇编,然后自动转机器码写入内存。使用的是Python的keystone库

# -*- coding: utf-8 -*-
import os
import pymem
import ctypes
import time
from keystone import Ks, KS_ARCH_X86, KS_MODE_32
def asm2code(asm_code, syntax=0):
    ks = Ks(KS_ARCH_X86, KS_MODE_32)
    bytes_code, _ = ks.asm(asm_code, as_bytes=True)
    return bytes_code
def WxBaseStruct(process_handle, content):
    struct_address = pymem.memory.allocate_memory(process_handle, 20)
    bcontent = content.encode('utf-16le')
    content_address = pymem.memory.allocate_memory(process_handle, len(bcontent)+16)
    pymem.ressources.kernel32.WriteProcessMemory(process_handle, content_address, bcontent, len(bcontent), None)
    pymem.memory.write_int(process_handle, struct_address, content_address)
    pymem.memory.write_int(process_handle, struct_address+0x4, len(content))
    pymem.memory.write_int(process_handle, struct_address+0x8, len(content)*2)
    pymem.memory.write_int(process_handle, struct_address+0xC, 0)
    pymem.memory.write_int(process_handle, struct_address+0x10, 0)
    return struct_address, content_address
def start_thread(process_handle, address, params=None):
    params = params or 0
    NULL_SECURITY_ATTRIBUTES = ctypes.cast(0, pymem.ressources.structure.LPSECURITY_ATTRIBUTES)
    thread_h = pymem.ressources.kernel32.CreateRemoteThread(
        process_handle,
        NULL_SECURITY_ATTRIBUTES,
        0,
        address,
        params,
        0,
        ctypes.byref(ctypes.c_ulong(0))
    )
    last_error = ctypes.windll.kernel32.GetLastError()
    if last_error:
        pymem.logger.warning('Got an error in start thread, code: %s' % last_error)
    pymem.ressources.kernel32.WaitForSingleObject(thread_h, -1)
    return thread_h
def main(wxpid, wxid, msg):
    process_handle = pymem.process.open(wxpid)
    wxNull_address = pymem.memory.allocate_memory(process_handle, 0x100)
    buffer_address = pymem.memory.allocate_memory(process_handle, 0x3B0)
    wxid_struct_address, wxid_address = WxBaseStruct(process_handle, wxid)
    msg_struct_address, msg_address = WxBaseStruct(process_handle, msg)
    process_WeChatWin_handle = pymem.process.module_from_name(process_handle, "WeChatWin.dll")
    call_address = process_WeChatWin_handle.lpBaseOfDll + 0x521D30
    call_p_address = pymem.memory.allocate_memory(process_handle, 4)
    pymem.memory.write_int(process_handle, call_p_address, call_address)
    format_asm_code = '''
    push edi;
    lea eax,dword ptr ds:[{wxNull:#02x}];
    push 0x1;
    push eax;
    lea edi,dword ptr ds:[{wxTextMsg:#02x}];
    push edi;
    lea edx,dword ptr ds:[{wxWxid:#02x}];
    lea ecx,dword ptr ds:[{buffer:#02x}];
    call dword ptr ds:[{callAddress:#02x}];
    add esp, 0xC;
    pop edi;
    ret;
    '''
    asm_code = format_asm_code.format(wxNull=wxNull_address,
                wxTextMsg=msg_struct_address,
                wxWxid=wxid_struct_address,
                buffer=buffer_address,
                callAddress=call_p_address)
    shellcode = asm2code(asm_code.encode())
    shellcode_address = pymem.memory.allocate_memory(process_handle, len(shellcode)+5)
    pymem.ressources.kernel32.WriteProcessMemory(process_handle, shellcode_address, shellcode, len(shellcode), None)
    thread_h = start_thread(process_handle, shellcode_address)
    time.sleep(0.5)
    pymem.memory.free_memory(process_handle, wxNull_address)
    pymem.memory.free_memory(process_handle, buffer_address)
    pymem.memory.free_memory(process_handle, wxid_struct_address)
    pymem.memory.free_memory(process_handle, wxid_address)
    pymem.memory.free_memory(process_handle, msg_struct_address)
    pymem.memory.free_memory(process_handle, msg_address)
    pymem.memory.free_memory(process_handle, call_p_address)
    pymem.memory.free_memory(process_handle, shellcode_address)
    pymem.process.close_handle(process_handle)
if __name__ == "__main__":
    wxpid = 18604
    wxid = "filehelper"
    msg = "python test msg"
    main(wxpid, wxid, msg)

到此这篇关于Python实现向好友发送微信消息优化篇的文章就介绍到这了,更多相关Python微信消息内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • 使用Python实现给企业微信发送消息功能

    目录 一.概述 二.python脚本 三.企业微信设置 1. 注册企业微信 2. 点击进入管理后台 3. 创建应用完成后 4. 查看企业id 5. 查看部门id 四.测试脚本 一.概述 本文将介绍如何使用python3给企业微信发送消息.我的环境是linux + python3.6.10. 二.python脚本 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2020/8/20 上午12:42 # @Author : wangyin

  • python向企业微信发送文字和图片消息的示例

    吐槽一下企业微信的api文档真的不好读······ 企业微信本来是有功能,可以直接把图片显示到正文的,但是那个api我调用一直出错,各种折腾也没解决.后来感觉就算了,没必要,用url拼接在文字中也行,这样的好处就是以后可以换图床,不局限在企业微信内部(好像免费版一天之内可以上传200张图片,所以如果需求比较大的话可以采用第三方图床) 我把上传部分封装了一个使用新线程的方法,以防使用的时候因为上传卡住主线程(亲测如果不这样的话确实会卡住) import requests import json f

  • python实现企业微信定时发送文本消息的示例代码

    企业微信定时发送文本消息 使用工具:企业微信机器人+python可执行文件+计算机管理中的任务计划程序 第一步:创建群机器人 选择群聊,单击鼠标右键,添加群机器人. 建立群机器人后,右键查看机器人,如下 复制机器人的链接. 第二步:编辑python程序 import requests from datetime import datetime url = 'https://qyapi.we......' #机器人的webhook地址 headers = {'Content-type':'appl

  • 用python发送微信消息

    条件 1.能够上网 2.必须是你的好友 3.必须能二维码登录网页微信 发送示例 # 使用微信接口给微信好友发送消息, import itchat   # 自动登录方法,hotReload=True可以缓存,不用每次都登录,但是第一次执行时会出现一个二维码,需要手机微信扫码登录 itchat.auto_login(hotReload=False)   # 搜索好友,search_friends("xxx"),其中"xxx"为好友昵称,备注或微信号不行 userfinf

  • python实现企业微信定时发送文本消息的实例代码

    企业微信定时发送文本消息 使用工具:企业微信机器人+python可执行文件+计算机管理中的任务计划程序 第一步:创建群机器人 选择群聊,单击鼠标右键,添加群机器人. 建立群机器人后,右键查看机器人,如下 复制机器人的链接. 第二步:编辑python程序 import requests from datetime import datetime url = 'https://qyapi.we......' #机器人的webhook地址 headers = {'Content-type':'appl

  • python实现给微信指定好友定时发送消息

    python有很多有趣的库,其中wxpy是连接微信的接口,具体可以查看官方文档.可以实现自动操作,wxpy 支持 Python 3.4-3.6,以及 2.7 版本. 一.安装 win10环境,直接在cmd中,输入 pip install wxpy 有时网络不稳定,可能出现错误,重新执行操作尝试一下. 二.简单介绍 # 导入模块 from wxpy import * # 初始化机器人,扫码登陆 bot = Bot() # 搜索名称含有 "游否" 的男性深圳好友 my_friend = b

  • python实现微信定时每天和女友发送消息

    但凡有些事情重复时,我就在想怎么可以用程序来自动化.这里想分享如何每天给女友定时微信发送"晚安",如果只是晚安,就略显单调,于是爬取金山词霸每日一句,英文和翻译,借此设定定时器进行发送. 准备: pip install wxpy pip install requests 实现代码: from __future__ import unicode_literals from threading import Timer from wxpy import * import requests

  • 教你利用python实现企业微信发送消息

    一.需要的参数 1.通讯用户:touser 或 通讯组:toparty 2.企业ID:corpid 3.应用ID/密钥:agentId,secret 二.获取通讯用户/组 通讯录 用户的账号或创建组的部门ID 三.获取企业ID 我的企业最下方 四.获取应用ID/密钥 企业微信管理员登录企业微信, 应用管理创建应用 可见范围:发给谁 五.脚本代码 #! /usr/bin/env python # -*- coding: UTF-8 -*- import requests, sys class Se

  • Python实现向好友发送微信消息优化篇

    目录 前言 第二次优化 第三次优化 前言 之前说了怎么写机器码到内存,然后调用.现在说说怎么优化. 用Python发送微信消息给好友 第二次优化 再看一遍c语言的代码 void SendText( wchar_t* wsTextMsg) { // 发送的好友,filehelper是文件传输助手 wchar_t wsWxId[0x10] = L"filehelper"; WxBaseStruct wxWxid(wsWxId); // 发送的消息内容 WxBaseStruct wxText

  • Python实现向好友发送微信消息

    目录 前言 c语言发微信消息 Python调用 不用c编写dll如何发消息 调用我们写入的机器码 第一次优化 第二次优化 x86/x64 Call Jmp指令区别 前言 原理:Windows逆向,通过内联汇编的形式调用发消息的函数 下面的代码PC微信版本是:3.7.0.26 , python使用的32位的3.8.13版本 如果只是需要最后的Python代码请直接翻到最后看 优化篇 c语言发微信消息 因为c语言本身支持内联汇编,所以写发消息的代码很简单,需要找到发消息的call,构造一下参数即可

  • 使用Python制作自动推送微信消息提醒的备忘录功能

    日常工作生活中,事情一多,就会忘记一些该做未做的事情.即使有时候把事情记录在了小本本上或者手机.电脑端备忘录上,也总会有查看不及时,导致错过的尴尬.如果有一款小工具,可以及时提醒,而不用再主动去查备忘录,化被动为主动,那就再合适不过了.因此,在这里我们就利用Python,实现这样的一款"小工具". 初步设想 毫无疑问,手机是当前使用最频繁的工具,没有之一.饭可以不吃,手机不可以不带.如果能在某些特定的时点,将备忘记录事项通过某种形式发送到手机端,通过查看手机端消息实现事项提醒,那将是再

  • python自动化UI工具发送QQ消息的实例

    概述 个人遇到过小的需求,windows自动水群发送垃圾消息,使用一些特别简单易上手的小工具,快速实现功能需求(而不是使用一些重量级的还需要额外花时间去熟悉功能语法的大工具,如UI自动化工具sikulix).在一番摸索下,得出一番结论: 对于多平台的UI自动复杂操作,还是去学sikulix吧,这不是啃一点win32 api获取窗口句柄就能轻松解决的,毕竟sikulix是MIT大佬折腾出来的.而且,原理也更复杂(通过使用opencv对窗体控件进行识别,进而实现控制操作). 对于简单的操作(控制剪切

  • Python实现钉钉发送报警消息的方法

    钉钉开放平台传送门:https://open.dingtalk.com 我司使用钉钉作为内部通讯工具,基本上大家在电脑和手机上都开着,消息可以第一时间查看,报警消息的即时性要求比较高,所以适合用钉钉通知. 下面介绍如何用Python实现钉钉发送报警消息. 获取access token 要使用钉钉发送消息,首先需要获取access token,代码如下: def get_access_token(): url = 'https://oapi.dingtalk.com/gettoken?corpid

  • python实现半自动化发送微信信息

    本文实例为大家分享了python半自动化发送微信信息的具体代码,供大家参考,具体内容如下 相关第三方库 1.pyautogui 自动操作鼠标.键盘的第三方库 2.pyperclip 用于将文本复制和粘贴到剪贴板 3.requests HTTP第三方库 4.psutil 可以查看系统信息,进程.CPU等 5.腾讯地图API 因为我想实现发送定位,所以需要用 总体思路 1.先手动登录微信 2.使用os模块调用微信进程 3.使用pyautogui模块来自动操作微信的快捷键,实现搜索好友.发送信息,py

  • python实现定时发送qq消息

    因为生活中老是忘记各种事情,刚好又在学python,便突发奇想通过python实现提醒任务的功能(尽管TIM有定时功能),也可定时给好友.群.讨论组发送qq消息.其工作流程是:访问数据库提取最近计划-->根据数据内容(提醒时间.提醒对象.提醒内容)设置定时任务-->给特定qq好友发送消息. 1. 软件版本: 2.安装依赖环境 pymysql安装:pip install pymysql qqbot安装:pip install qqbot 3.数据库操作 数据库操作非常简单,跟Java类似,自己去

  • Python使用itcaht库实现微信自动收发消息功能

    itchat库 模拟微信网页登录 通过python code接受/发送微信消息 实现微信聊天机器人:调用聊天机器人api,将接收到的微信消息传给api,再将api返回的消息传给微信 展示如何使用itchat发送微信消息 # !pip install itchat import itchat 在当前文件夹下生成二维码图片,微信扫码即可登录网页版微信 itchat.auto_login() Getting uuid of QR code. Downloading QR code. Please sc

  • 一步步教你用python给女朋友写个微信自动提醒的程序

    目录 前言 第一步:文本内容的确定 第二步:微信端发送消息的实现 第三步:定时任务的设置 总结 前言 事件背景是经常有很多琐碎的事情需要在某个时间点去做,光靠人力去记,容易出现偏差,尤其是对容易迷糊的选手. 所以动手写了一套代码,可以按需要通过微信发送消息,不论是给自己充当自动提醒的备忘录还是给其他人发送定时消息,都可以在这套代码的基础上实现. 首先放上最终成果示例: 图中的文字都是可以根据自身需要而进行修改的,所以文章中附上的代码也只是抛砖引玉,读者可以根据自身需要而进行调整.本篇文章会分三个

随机推荐