Virtualallocex cross-process read/write data code injection

Source: Internet
Author: User
The virtualallocex function is used to reserve or submit the memory area in the virtual space of the specified process. Unless the mem_reset parameter is specified, the memory area is set to 0.

Lpvoid virtualallocex (
Handle hprocess, // Process Handle where the applied memory is located
Lpvoid lpaddress, // reserved page memory address; Generally, it is automatically allocated using null.
Size_t dwsize, // size of the memory to be allocated, in bytes. Note that the actual memory size in the Shard is an integer multiple of the page memory size.
DWORD flallocationtype,
DWORD flprotect
);

Flallocationtype and flprotect are too many. The parameter values and meanings are available on msdn.
Problem description: I want to select an item in a listview window of another process. In fact, I only need to send a message:
Sendmessage (hwnd, lvm_setitemstate, (wparam) nitemindex, (lparam) plvitem );
However, the lparam must point to the structure of an lvitem. We know that a message can be transferred across processes, but it needs to address this structure when the target window receives a message, this structure is actually stored in the sender's address space. Therefore, we must write lvitem content into the address space of the window process, so we need virtualallocex and virtualfreeex.
The Code is as follows. Listviewoperator: listviewoperator (hwnd) // initialization Function
...{
M_hwnd = NULL; // target window
M_hprocess = NULL; // target process
M_plvitem = NULL; // write the lvitem structure of the target process
M_ptextbuffer = NULL; // other usage

M_hwnd = hwnd;

DWORD dwprocessid;
Getwindowthreadprocessid (hwnd, & dwprocessid); // obtain the process ID

M_hprocess = OpenProcess (process_all_access, false, dwprocessid); // open the process
Throw_exception (m_hprocess = NULL), "open process failed ");

M_plvitem = (lvitema *) virtualallocex (m_hprocess, null, sizeof (lvitema), mem_commit, page_readwrite); obtain the memory in the target process space, and allow reading and writing.
Throw_exception (m_plvitem = NULL), "memory alloc failed ");
M_ptextbuffer = (char *) virtualallocex (m_hprocess, null, max_text_buffer_length, mem_commit, page_readwrite );
Throw_exception (m_ptextbuffer = NULL), "memory alloc failed ");

}

Void listviewoperator: setitemselected (INT nitemindex) // select an item
...{
Lvitema lvitem; // static structure

Lvitem. Mask = lvif_state;
Lvitem. isubitem = 0;
Lvitem. State = lvis_selected | lvis_focused;
Lvitem. statemask = lvis_selected | lvis_focused;

Int nret = writeprocessmemory (m_hprocess, m_plvitem, & lvitem, sizeof (lvitem), null); // write the structure of the local process to the target process
Throw_exception (nret = 0), "write process memory failed ");

Hresult HRET = sendmessage (m_hwnd, lvm_setitemstate, (wparam) nitemindex, (lparam) m_plvitem); // send a message
Throw_exception (failed (HRET), "Send message failed ");

Return;
}
 

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.