Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // C_hack.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
- #include <windows.h>
- #include <iostream>
- #include <string>
- #include <tlhelp32.h>
- #include <cstdlib>
- #include <inttypes.h>
- #include <list>
- using namespace std;
- int64_t S64(const char* s) {
- int64_t i;
- char c;
- int scanned = sscanf_s(s, "%" SCNd64 "%c", &i, &c);
- if (scanned == 1) return i;
- if (scanned > 1) {
- // TBD about extra data found
- return i;
- }
- // TBD failed to scan;
- return 0;
- }
- DWORD FindProcessId(const std::wstring& processName)
- {
- PROCESSENTRY32 processInfo;
- processInfo.dwSize = sizeof(processInfo);
- HANDLE processesSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
- if (processesSnapshot == INVALID_HANDLE_VALUE)
- return 0;
- Process32First(processesSnapshot, &processInfo);
- if (!processName.compare(processInfo.szExeFile))
- {
- CloseHandle(processesSnapshot);
- return processInfo.th32ProcessID;
- }
- while (Process32Next(processesSnapshot, &processInfo))
- {
- if (!processName.compare(processInfo.szExeFile))
- {
- CloseHandle(processesSnapshot);
- return processInfo.th32ProcessID;
- }
- }
- CloseHandle(processesSnapshot);
- return 0;
- }
- HANDLE pHandle;
- SYSTEM_INFO si;
- MEMORY_BASIC_INFORMATION mbi;
- LPVOID lpMem;
- DWORD ret, totalRead;
- string readText(DWORD pid, uint64_t loc) {
- lpMem = (void*)loc;
- string raw;
- list<char> text;
- uint64_t offset = loc;
- bool flag = true;
- while (flag) {
- lpMem = (void*)offset;
- char buf;
- ReadProcessMemory(pHandle, lpMem, (LPVOID)(&buf), 1, &totalRead);
- if (buf != '\0' && buf != '\r' && buf != '\n') {
- raw.push_back(buf);
- }
- else {
- flag = false;
- }
- offset++;
- }
- return raw;
- }
- int main()
- {
- std::cout << "Hello World!" <<endl;
- printf("Test\n");
- std::cout << "Please, enter process name to try and inject: ";
- wstring pname;
- std::getline(wcin, pname);
- std::cout << "Please, enter offset in integer to read some text from: ";
- string offset;
- std::getline(cin, offset);
- DWORD processID = FindProcessId(pname);
- pHandle = OpenProcess(PROCESS_ALL_ACCESS, 0, processID);
- if (pHandle == NULL) {
- exit(5);
- }
- std::cout << readText(processID, S64(offset.c_str()));
- std::getline(cin, offset);
- }
- // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
- // Debug program: F5 or Debug > Start Debugging menu
- // Tips for Getting Started:
- // 1. Use the Solution Explorer window to add/manage files
- // 2. Use the Team Explorer window to connect to source control
- // 3. Use the Output window to see build output and other messages
- // 4. Use the Error List window to view errors
- // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
- // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement