Advertisement
cmiN

taskman

Feb 5th, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.99 KB | None | 0 0
  1. #include <windows.h>
  2. #include <tlhelp32.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. #include <iostream>
  7. #include <map>
  8. #include <vector>
  9. #include <utility>
  10. #include <string>
  11.  
  12. using namespace std;
  13.  
  14.  
  15. const int PIDL = 16;
  16. const int EXEL = 256;
  17. const char * ROOT = "C:\\Users\\cmiN\\Desktop\\procese";
  18. const int DELAY = 1000;
  19.  
  20.  
  21. typedef unsigned int ui_t;
  22.  
  23. typedef pair<string, ui_t> psu_t;    // (dllName, addr)
  24. typedef vector<psu_t> vp_t;         // ((dllName, addr), ...)
  25. typedef pair<string, vp_t> psv_t;    // (exeName, ((dllName, addr), ...))
  26. typedef map<int, psv_t> mip_t;        // ((pid, (exeName, ((dllName, addr), ...))), ...)
  27.  
  28.  
  29. void fill(mip_t & procs)
  30. {
  31.     // fill procs with details of all processes
  32.     HANDLE hSnap;
  33.     PROCESSENTRY32 data;
  34.  
  35.     data.dwSize = sizeof(PROCESSENTRY32);
  36.     hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  37.  
  38.     if (hSnap == INVALID_HANDLE_VALUE) {
  39.         printf("Error %d: couldn't create snaphot\n", GetLastError());
  40.         return;
  41.     }
  42.  
  43.     if (!Process32First(hSnap, &data)) {
  44.         printf("Error %d: couldn't find first process\n", GetLastError());
  45.         return;
  46.     }
  47.  
  48.     // ok
  49.     do {
  50.         int pid = data.th32ProcessID;
  51.         if (pid == 0)
  52.             continue;
  53.         string name(data.szExeFile);
  54.         vp_t modules;
  55.  
  56.         // load kernel modules
  57.         HANDLE _hSnap;
  58.         MODULEENTRY32 _data;
  59.         _data.dwSize = sizeof(MODULEENTRY32);
  60.         _hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pid);
  61.         if (!Module32First(_hSnap, &_data)) {
  62.             //printf("Error %d: couldn't find module\n", GetLastError());
  63.             continue;
  64.         }
  65.         // first module found
  66.         Module32Next(_hSnap, &_data);
  67.         do {
  68.             psu_t pair;
  69.             pair.first = _data.szModule;
  70.             pair.second = (ui_t)_data.modBaseAddr;
  71.             modules.push_back(pair);
  72.         } while(Module32Next(_hSnap, &_data));
  73.         CloseHandle(_hSnap);
  74.  
  75.         psv_t pair;
  76.         pair.first = name;
  77.         pair.second.assign(modules.begin(), modules.end());
  78.         procs[pid] = pair;
  79.     } while (Process32Next(hSnap, &data));
  80.  
  81.     CloseHandle(hSnap);
  82. }
  83.  
  84.  
  85. inline void create_folder(string dir)
  86. {
  87.     string absPath(ROOT);
  88.     absPath.append("\\" + dir);
  89.     CreateDirectory(absPath.c_str(), NULL);
  90. }
  91.  
  92.  
  93. inline void create_file(string dirName, string fname, int addr)
  94. {
  95.     string absPath(ROOT);
  96.     absPath.append("\\" + dirName + "\\" + fname);
  97.     HANDLE hFile = CreateFile(absPath.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
  98.                               FILE_ATTRIBUTE_NORMAL, NULL);
  99.     int written;
  100.     char addrStr[32];
  101.     sprintf(addrStr, "%d", addr);
  102.     WriteFile(hFile, addrStr, strlen(addrStr), (LPDWORD)&written, NULL);
  103.     CloseHandle(hFile);
  104. }
  105.  
  106.  
  107. void build(const mip_t & procs)
  108. {
  109.     for (mip_t::const_iterator it = procs.begin(); it != procs.end(); ++it) {
  110.         char tmp[PIDL];
  111.         sprintf(tmp, "%d", it->first);
  112.         string dirName(tmp);
  113.         dirName.append("_" + (it->second).first);
  114.         create_folder(dirName);
  115.  
  116.         for (vp_t::const_iterator vit = (it->second).second.begin();
  117.              vit != (it->second).second.end(); ++vit) {
  118.                 string fname = vit->first;
  119.                 int addr = vit->second;
  120.                 create_file(dirName, fname, addr);
  121.         }
  122.     }
  123. }
  124.  
  125.  
  126. void remove(const mip_t& procs)
  127. {
  128.     for (mip_t::const_iterator it = procs.begin();
  129.          it != procs.end(); ++it) {
  130.         string path(ROOT);
  131.         char pid[PIDL];
  132.         sprintf(pid, "%d", it->first);
  133.         path.append("\\" + string(pid) + "_" + (it->second).first);
  134.  
  135.         WIN32_FIND_DATA findData;
  136.         HANDLE findHandle;
  137.  
  138.         findHandle = FindFirstFile((path + "\\*").c_str(), &findData);
  139.         if (findHandle != INVALID_HANDLE_VALUE) {
  140.             // there are files to delete
  141.             do {
  142.                 DeleteFile((path + "\\" + string(findData.cFileName)).c_str());
  143.             } while (FindNextFile(findHandle, &findData));
  144.         }
  145.         FindClose(findHandle);
  146.  
  147.         RemoveDirectory(path.c_str());
  148.     }
  149. }
  150.  
  151.  
  152. void diff_make(const mip_t & procs, const mip_t & _procs,
  153.                mip_t & toMake)
  154. {
  155.     for (mip_t::const_iterator it = _procs.begin();
  156.          it != _procs.end(); ++it)
  157.         if (procs.count(it->first) == 0)
  158.             toMake.insert(*it);
  159. }
  160.  
  161.  
  162. void diff_del(const mip_t & procs, const mip_t & _procs,
  163.                mip_t & toDel)
  164. {
  165.     for (mip_t::const_iterator it = procs.begin();
  166.          it != procs.end(); ++it)
  167.         if (_procs.count(it->first) == 0)
  168.             toDel.insert(*it);
  169. }
  170.  
  171.  
  172. void show(const mip_t & dict)
  173. {
  174.     /// For testing purposes.
  175.     for (mip_t::const_iterator it = dict.begin();
  176.          it != dict.end(); ++it)
  177.         cout << it->first << endl;
  178. }
  179.  
  180.  
  181. int get_pid(string path)
  182. {
  183.     HANDLE fin = CreateFile(path.c_str(), GENERIC_READ, FILE_SHARE_READ,
  184.                             NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
  185.                             NULL);
  186.     if (fin == INVALID_HANDLE_VALUE) {
  187.         fprintf(stderr, "Error %d: couldn't open start file", GetLastError());
  188.         return -1;
  189.     }
  190.  
  191.     char buffer[PIDL];
  192.     int read = -1, pid = -1;
  193.     if (ReadFile(fin, buffer, PIDL - 1, (LPDWORD)&read, NULL)) {
  194.         buffer[read] = '\0';
  195.         sscanf(buffer, "%d", &pid);
  196.     } else
  197.         fprintf(stderr, "Error %d: couldn't read start file", GetLastError());
  198.     CloseHandle(fin);
  199.     return pid;
  200. }
  201.  
  202.  
  203. string get_ename(string path)
  204. {
  205.     HANDLE fin = CreateFile(path.c_str(), GENERIC_READ, FILE_SHARE_READ,
  206.                             NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
  207.                             NULL);
  208.     if (fin == INVALID_HANDLE_VALUE) {
  209.         fprintf(stderr, "Error %d: couldn't open kill file", GetLastError());
  210.         return "";
  211.     }
  212.  
  213.     char buffer[EXEL];
  214.     int read = -1;
  215.     string exeName;
  216.     if (ReadFile(fin, buffer, EXEL - 1, (LPDWORD)&read, NULL)) {
  217.         buffer[read] = '\0';
  218.         sscanf(buffer, "%s", &buffer);
  219.         exeName.assign(buffer);
  220.     } else
  221.         fprintf(stderr, "Error %d: couldn't read kill file", GetLastError());
  222.     CloseHandle(fin);
  223.     return exeName;
  224. }
  225.  
  226.  
  227. void start(string path)
  228. {
  229.     STARTUPINFO sInfo;
  230.     PROCESS_INFORMATION pInfo;
  231.  
  232.     ZeroMemory(&sInfo, sizeof(sInfo));
  233.     ZeroMemory(&pInfo, sizeof(pInfo));
  234.     sInfo.cb = sizeof(sInfo);
  235.  
  236.     if (!CreateProcess(NULL, (LPSTR)path.c_str(), NULL, NULL, FALSE, 0,
  237.                       NULL, NULL, &sInfo, &pInfo)) {
  238.        fprintf(stderr, "Error %d: create process failed", GetLastError());
  239.        return;
  240.     }
  241.  
  242.     CloseHandle(pInfo.hProcess);
  243.     CloseHandle(pInfo.hThread);
  244. }
  245.  
  246.  
  247. void kill(int pid)
  248. {
  249.     THREADENTRY32 threadData;
  250.     HANDLE snapHandle;
  251.  
  252.     threadData.dwSize = sizeof(THREADENTRY32);
  253.     snapHandle = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
  254.     Thread32First(snapHandle, &threadData);
  255.  
  256.     do {
  257.         if (threadData.th32OwnerProcessID != pid)
  258.             continue;
  259.         // thread from process, kill it
  260.         HANDLE threadHandle = OpenThread(THREAD_TERMINATE, FALSE,
  261.                                          threadData.th32ThreadID);
  262.         TerminateThread(threadHandle, 0);
  263.         CloseHandle(threadHandle);
  264.     } while (Thread32Next(snapHandle, &threadData));
  265.     CloseHandle(snapHandle);
  266. }
  267.  
  268.  
  269. void check_sk()
  270. {
  271.     string path(ROOT);
  272.  
  273.     WIN32_FIND_DATA findData;
  274.     HANDLE findHandle;
  275.     bool sFound = false, kFound = false;
  276.  
  277.     findHandle = FindFirstFile((path + "\\*").c_str(), &findData);
  278.     do {
  279.         if (!strcmp(findData.cFileName, "start")) {
  280.             sFound = true;
  281.             string exeName = get_ename(path + "\\" + "start");
  282.             if (exeName.size())
  283.                 start(exeName);
  284.         }
  285.         if (!strcmp(findData.cFileName, "kill")) {
  286.             kFound = true;
  287.             int pid = get_pid(path + "\\" + "kill");
  288.             if (pid != -1)
  289.                 kill(pid);
  290.         }
  291.         if (sFound && kFound)
  292.             break;    // nothing to search for
  293.     } while (FindNextFile(findHandle, &findData));
  294.  
  295.     if (sFound)
  296.         DeleteFile((path + "\\start").c_str());
  297.     if (kFound)
  298.         DeleteFile((path + "\\kill").c_str());
  299. }
  300.  
  301.  
  302. void watcher()
  303. {
  304.     // initial snapshot
  305.     mip_t procs;
  306.     fill(procs);
  307.     build(procs);
  308.  
  309.     while (1) {
  310.         // check for start and kill file
  311.         check_sk();
  312.  
  313.         // view differences
  314.         mip_t _procs, toMake, toDel;
  315.         fill(_procs);
  316.         diff_make(procs, _procs, toMake);
  317.         diff_del(procs, _procs, toDel);
  318.  
  319.         /*cout << "Create..." << endl;
  320.         show(toMake);
  321.         cout << "Delete..." << endl;
  322.         show(toDel);*/
  323.         build(toMake);
  324.         remove(toDel);
  325.  
  326.         // update old procs
  327.         procs.swap(_procs);
  328.  
  329.         //cout << "Dorm..." << endl;
  330.         Sleep(DELAY);
  331.     }
  332. }
  333.  
  334.  
  335. int main(int argc, char * argv[])
  336. {
  337.     watcher();
  338.     return 0;
  339. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement