Python實(shí)現(xiàn)向好友發(fā)送微信消息優(yōu)化篇_第1頁(yè)
Python實(shí)現(xiàn)向好友發(fā)送微信消息優(yōu)化篇_第2頁(yè)
Python實(shí)現(xiàn)向好友發(fā)送微信消息優(yōu)化篇_第3頁(yè)
Python實(shí)現(xiàn)向好友發(fā)送微信消息優(yōu)化篇_第4頁(yè)
Python實(shí)現(xiàn)向好友發(fā)送微信消息優(yōu)化篇_第5頁(yè)
已閱讀5頁(yè),還剩5頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第Python實(shí)現(xiàn)向好友發(fā)送微信消息優(yōu)化篇目錄前言第二次優(yōu)化第三次優(yōu)化

前言

之前說(shuō)了怎么寫(xiě)機(jī)器碼到內(nèi)存,然后調(diào)用?,F(xiàn)在說(shuō)說(shuō)怎么優(yōu)化。

用Python發(fā)送微信消息給好友

第二次優(yōu)化

再看一遍c語(yǔ)言的代碼

voidSendText(wchar_t*wsTextMsg){

//發(fā)送的好友,filehelper是文件傳輸助手

wchar_twsWxId[0x10]=L"filehelper";

WxBaseStructwxWxid(wsWxId);

//發(fā)送的消息內(nèi)容

WxBaseStructwxTextMsg(wsTextMsg);

wchar_t**pWxmsg=wxTextMsg.buffer;

charbuffer[0x3B0]={0};

charwxNull[0x100]={0};

DWORDdllBaseAddress=(DWORD)GetModuleHandleA("WeChatWin.dll");

//發(fā)消息的函數(shù)call地址

DWORDcallAddress=dllBaseAddress+0x521D30;

__asm{

leaeax,wxNull;

push0x1;

pusheax;

movedi,pWxmsg;

pushedi;

leaedx,wxWxid;

leaecx,buffer;

callcallAddress;

addesp,0xC;

}

上面的代碼真正發(fā)消息的是asm里面的代碼,之前的c代碼都是在組裝內(nèi)存數(shù)據(jù)。那我們是不是可以用Python組裝數(shù)據(jù),只講下面的匯編轉(zhuǎn)為機(jī)器碼寫(xiě)入內(nèi)存調(diào)用,這樣就少了很多無(wú)用的機(jī)器碼。

改完的SendText函數(shù)如下

wchar_twsWxId[0x10]=Lfilehelper

wchar_twsTextMsg[0x100]=Ltest

WxBaseStructwxWxid(wsWxId);

WxBaseStructwxTextMsg(wsTextMsg);

wchar_t**pWxmsg=wxTextMsg.buffer;

charbuffer[0x3B0]={0};

charwxNull[0x100]={0};

DWORDdllBaseAddress=(DWORD)GetModuleHandleA(WeChatWin.dll;

DWORDcallAddress=dllBaseAddress+0x521D30;

voidSendText(){

__asm{

leaeax,wxNull;

push0x1;

pusheax;

movedi,pWxmsg;

pushedi;

leaedx,wxWxid;

leaecx,buffer;

callcallAddress;

addesp,0xC;

}

}

匯編代碼:

[]里面包含的類(lèi)型和變量名其實(shí)就是地址,只需要將地址改成用Python構(gòu)造的地址就可以了

完整代碼如下:

importos

importpymem

importctypes

importtime

defconvert_addr(addr):

ifisinstance(addr,int):

addr=hex(addr)

ifaddr.startswith("0x")oraddr.startswith("0X"):

addr=addr[2:]

iflen(addr)8:

addr=(8-len(addr))*'0'+addr

tmp=[]

foriinrange(0,8,2):

tmp.append(addr[i:i+2])

tmp.reverse()

return''.join(tmp)

defWxBaseStruct(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)

returnstruct_address,content_address

defstart_thread(process_handle,address,params=None):

params=paramsor0

NULL_SECURITY_ATTRIBUTES=ctypes.cast(0,pymem.ressources.structure.LPSECURITY_ATTRIBUTES)

thread_h=pymem.ressources.kernel32.CreateRemoteThread(

process_handle,

NULL_SECURITY_ATTRIBUTES,

address,

params,

ctypes.byref(ctypes.c_ulong(0))

last_error=ctypes.windll.kernel32.GetLastError()

iflast_error:

pymem.logger.warning('Gotanerrorinstartthread,code:%s'%last_error)

pymem.ressources.kernel32.WaitForSingleObject(thread_h,-1)

returnthread_h

defmain(wxpid,wxid,msg):

process_handle=cess.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=cess.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='''

8D05{wxNull}

6A01

8D3D{wxTextMsg}

8D15{wxWxid}

8D0D{buffer}

FF15{callAddress}

83C40C

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)

cess.close_handle(process_handle)

if__name__=="__main__":

wxpid=16892

wxid="filehelper"

msg="pythontest"

main(wxpid,wxid,msg)

第三次優(yōu)化

直接在Python里寫(xiě)匯編,然后自動(dòng)轉(zhuǎn)機(jī)器碼寫(xiě)入內(nèi)存。使用的是Python的keystone庫(kù)

#-*-coding:utf-8-*-

importos

importpymem

importctypes

importtime

fromkeystoneimportKs,KS_ARCH_X86,KS_MODE_32

defasm2code(asm_code,syntax=0):

ks=Ks(KS_ARCH_X86,KS_MODE_32)

bytes_code,_=ks.asm(asm_code,as_bytes=True)

returnbytes_code

defWxBaseStruct(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)

returnstruct_address,content_address

defstart_thread(process_handle,address,params=None):

params=paramsor0

NULL_SECURITY_ATTRIBUTES=ctypes.cast(0,pymem.ressources.structure.LPSECURITY_ATTRIBUTES)

thread_h=pymem.ressources.kernel32.CreateRemoteThread(

process_handle,

NULL_SECURITY_ATTRIBUTES,

address,

params,

ctypes.byref(ctypes.c_ulong(0))

last_error=ctypes.windll.kernel32.GetLastError()

iflast_error:

pymem.logger.warning('Gotanerrorinstartthread,code:%s'%last_error)

pymem.ressources.kernel32.WaitForSingleObject(thread_h,-1)

returnthread_h

defmain(wxpid,wxid,msg):

process_handle=cess.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=cess.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='''

pushedi;

leaeax,dwordptrds:[{wxNull:#02x}];

push0x1;

pusheax;

leaedi,dwordptrds:[{wxTextMsg:#02x}];

pushedi;

leaedx,dwordptrds:[{wxWxid:#02x}];

leaecx,dwordptrds:[{buffer:#02x}];

calldwordptrds:[{callAddress:#02x}];

addesp,0xC;

popedi;

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.

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論