Advertisement
Guest User

dboot.cpp

a guest
Jan 6th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // dboot.cpp : Defines the entry point for the application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "dboot.h"
  6. #include "hook.h"
  7.  
  8. static _SetWindowsHookExW   SetWindowsHookEx;
  9. static _UnhookWindowsHookEx UnhookWindowsHookEx;
  10. static _CallNextHookEx      CallNextHookEx;
  11.  
  12. #define MAX_LOADSTRING 100
  13. #define ID_TIMER_BOOTEND 101
  14.  
  15. // Global Variables:
  16. HINSTANCE           g_hInst;            // current instance
  17. BOOL                g_bootEnd;
  18. HINSTANCE           g_hCore;
  19. HHOOK               g_hHook;
  20.  
  21. int                 g_mPos;
  22. BOOL                g_pressed;
  23.  
  24. // Forward declarations of functions included in this code module:
  25. ATOM            MyRegisterClass(HINSTANCE, LPTSTR);
  26. BOOL            InitInstance(HINSTANCE, int);
  27. LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
  28.  
  29. LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam);
  30. BOOL ActivateMHook();
  31. void DeactivateMHook();
  32.  
  33. BOOL FileExist(TCHAR *file);
  34. BOOL ShellExe(TCHAR *file);
  35. BOOL StartWinCE();
  36.  
  37. LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam)
  38. {
  39.     PMSLLHOOKSTRUCT pMouseHookStruct = (PMSLLHOOKSTRUCT)lParam;
  40.  
  41.     if (nCode < HC_ACTION)
  42.         return CallNextHookEx(g_hHook, nCode, wParam, lParam);
  43.  
  44.     TCHAR name[MAX_PATH];
  45.     HWND hfore = GetForegroundWindow();
  46.     if (hfore != NULL) {
  47.         if (GetClassName(hfore, name, MAX_PATH) > 0) {
  48.             if (wcsncmp(name, _T("CGUIEmptyDlg"), 12) != 0)
  49.                 return CallNextHookEx(g_hHook, nCode, wParam, lParam);
  50.         }
  51.     } else {
  52.         return CallNextHookEx(g_hHook, nCode, wParam, lParam);
  53.     }
  54.  
  55.     switch (wParam)
  56.     {
  57.         case WM_LBUTTONDOWN:
  58.             g_pressed = TRUE;
  59.             g_mPos = pMouseHookStruct->pt.x;
  60.             break;
  61.         case WM_LBUTTONUP:
  62.             g_pressed = FALSE;
  63.             break;
  64.         case WM_MOUSEMOVE:
  65.             if (g_pressed) {
  66.                 if (abs(g_mPos - pMouseHookStruct->pt.x) > 300)
  67.                 {
  68.                     g_pressed = FALSE;
  69.                     ShellExe(_T("\\Storage Card\\System\\dmenu.exe"));
  70.  
  71.                     NKDbgPrintfW(L"Start dmenu\n");
  72.                 }
  73.             }
  74.             break;
  75.     }
  76.  
  77.     return CallNextHookEx(g_hHook, nCode, wParam, lParam);
  78. }
  79.  
  80. void DeactivateMHook()
  81. {
  82.     if (g_hHook != NULL) {
  83.         UnhookWindowsHookEx(g_hHook);
  84.         g_hHook = NULL;
  85.     }
  86.  
  87.     if (g_hCore != NULL) {
  88.         FreeLibrary(g_hCore);
  89.         g_hCore = NULL;
  90.     }
  91. }
  92.  
  93. BOOL ActivateMHook()
  94. {
  95.     if (g_hHook != NULL)
  96.         return TRUE;
  97.  
  98.     if (g_hCore == NULL) {
  99.         g_hCore = LoadLibrary(_T("coredll.dll"));
  100.         if (g_hCore == NULL) {
  101.             NKDbgPrintfW(L"[Mouse hook] LoadLibrary core.dll failed\n");
  102.             return FALSE;
  103.         }
  104.     }
  105.  
  106.     SetWindowsHookEx = (_SetWindowsHookExW)GetProcAddress(g_hCore, _T("SetWindowsHookExW"));
  107.     if (SetWindowsHookEx == NULL) {
  108.         NKDbgPrintfW(L"[Mouse hook] SetWindowsHookEx not found\n");
  109.         return FALSE;
  110.     }
  111.  
  112.     CallNextHookEx = (_CallNextHookEx)GetProcAddress(g_hCore, _T("CallNextHookEx"));
  113.     if (CallNextHookEx == NULL) {
  114.         NKDbgPrintfW(L"[Mouse hook] CallNextHookEx not found\n");
  115.         return FALSE;
  116.     }
  117.  
  118.     UnhookWindowsHookEx = (_UnhookWindowsHookEx)GetProcAddress(g_hCore, _T("UnhookWindowsHookEx"));
  119.     if (UnhookWindowsHookEx == NULL) {
  120.         NKDbgPrintfW(L"[Mouse hook] UnhookWindowsHookEx not found\n");
  121.         return FALSE;
  122.     }
  123.  
  124.     g_hHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProc, NULL, 0);
  125.     if (g_hHook == NULL) {
  126.         NKDbgPrintfW(L"[Mouse hook] SetWindowsHookEx WH_MOUSE_LL failed\n");
  127.         return FALSE;
  128.     }
  129.  
  130.     return TRUE;
  131. }
  132.  
  133. int WINAPI WinMain(HINSTANCE hInstance,
  134.                    HINSTANCE hPrevInstance,
  135.                    LPTSTR    lpCmdLine,
  136.                    int       nCmdShow)
  137. {
  138.     HKEY regKey = NULL;
  139.     DWORD Disposition;
  140.     if (RegCreateKeyEx(HKEY_CURRENT_USER, _T("ControlPanel\\Comm"), 0, 0, REG_OPTION_NON_VOLATILE, 0,
  141.         NULL, &regKey, &Disposition) == ERROR_SUCCESS)
  142.     {
  143.         TCHAR* cnct = _T("`Default USB`");
  144.         RegSetValueEx(regKey, TEXT("Cnct"), 0, REG_SZ, (const BYTE*)cnct, (wcslen(cnct) + 1) * sizeof( TCHAR));
  145.         DWORD autocnct = 1;
  146.         RegSetValueEx(regKey, TEXT("AutoCnct"), 0, REG_DWORD, (const BYTE*)&autocnct, sizeof(autocnct));
  147.         RegCloseKey(regKey);
  148.     }
  149.  
  150.     if (FileExist(_T("\\Storage Card3\\StartWinCE")) ||
  151.         !ShellExe(_T("\\Storage Card\\System\\UpgradeManager.exe")))
  152.     {
  153.         StartWinCE();
  154.         return 0;
  155.     }
  156.    
  157.     MSG msg;
  158.  
  159.     // Perform application initialization:
  160.     if (!InitInstance(hInstance, nCmdShow))
  161.     {
  162.         return FALSE;
  163.     }
  164.  
  165.     // Main message loop:
  166.     while (GetMessage(&msg, NULL, 0, 0))
  167.     {
  168.         TranslateMessage(&msg);
  169.         DispatchMessage(&msg);
  170.     }
  171.  
  172.     return (int) msg.wParam;
  173. }
  174.  
  175. //
  176. //  FUNCTION: MyRegisterClass()
  177. //
  178. //  PURPOSE: Registers the window class.
  179. //
  180. //  COMMENTS:
  181. //
  182. ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
  183. {
  184.     WNDCLASS wc;
  185.  
  186.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  187.     wc.lpfnWndProc   = WndProc;
  188.     wc.cbClsExtra    = 0;
  189.     wc.cbWndExtra    = 0;
  190.     wc.hInstance     = hInstance;
  191.     wc.hIcon         = 0;
  192.     wc.hCursor       = 0;
  193.     wc.hbrBackground = 0;
  194.     wc.lpszMenuName  = 0;
  195.     wc.lpszClassName = szWindowClass;
  196.  
  197.     return RegisterClass(&wc);
  198. }
  199.  
  200. //
  201. //   FUNCTION: InitInstance(HINSTANCE, int)
  202. //
  203. //   PURPOSE: Saves instance handle and creates main window
  204. //
  205. //   COMMENTS:
  206. //
  207. //        In this function, we save the instance handle in a global variable and
  208. //        create and display the main program window.
  209. //
  210. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  211. {
  212.     HWND hWnd;
  213.  
  214.     g_hInst = hInstance; // Store instance handle in our global variable
  215.  
  216.     if (!MyRegisterClass(hInstance, _T("dboot")))
  217.     {
  218.         return FALSE;
  219.     }
  220.  
  221.     hWnd = CreateWindow(_T("dboot"), _T("dboot"), WS_VISIBLE,
  222.         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
  223.  
  224.     if (!hWnd)
  225.     {
  226.         return FALSE;
  227.     }
  228.  
  229.     RECT rc;
  230.     GetWindowRect(hWnd, &rc);
  231.  
  232.     SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 800, 480, 0);
  233.     SetForegroundWindow((HWND)(((ULONG) hWnd) | 0x01));
  234.     ShowWindow(hWnd, nCmdShow);
  235.     UpdateWindow(hWnd);
  236.  
  237.     g_pressed               = FALSE;
  238.     g_bootEnd               = FALSE;
  239.     g_hCore                 = NULL;
  240.     g_hHook                 = NULL;
  241.     SetWindowsHookEx        = NULL;
  242.     CallNextHookEx          = NULL;
  243.     UnhookWindowsHookEx     = NULL;
  244.  
  245.     SetTimer(hWnd, ID_TIMER_BOOTEND, 5000, NULL);
  246.  
  247.     return TRUE;
  248. }
  249.  
  250. LRESULT CALLBACK LowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam)
  251. {
  252.  
  253.     return 0;
  254. }
  255.  
  256. //
  257. //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  258. //
  259. //  PURPOSE:  Processes messages for the main window.
  260. //
  261. //  WM_COMMAND  - process the application menu
  262. //  WM_PAINT    - Paint the main window
  263. //  WM_DESTROY  - post a quit message and return
  264. //
  265. //
  266.  
  267. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  268. {
  269.     PAINTSTRUCT ps;
  270.     HDC hdc;
  271.     RECT rec;
  272.    
  273.     switch (message)
  274.     {
  275.         case WM_LBUTTONDOWN:
  276.             g_pressed = TRUE;
  277.             g_mPos = LOWORD(lParam);
  278.             break;
  279.         case WM_LBUTTONUP:
  280.             g_pressed = FALSE;
  281.             break;
  282.         case WM_MOUSEMOVE:
  283.             if (!g_bootEnd && g_pressed) {
  284.                 if (abs(g_mPos - LOWORD(lParam)) > 300)
  285.                 {
  286.                     KillTimer(hWnd, ID_TIMER_BOOTEND);
  287.                     g_pressed = FALSE;
  288.                     ShellExe(_T("\\Storage Card\\System\\dmenu.exe"));
  289.  
  290.                     NKDbgPrintfW(L"Start dmenu\n");
  291.                 }
  292.             }
  293.             break;
  294.         case WM_PAINT:
  295.             if (!g_bootEnd) {
  296.                 hdc = BeginPaint(hWnd, &ps);
  297.  
  298.                 SetTextColor(hdc, RGB(245,245,245));
  299.                 SetBkMode(hdc, TRANSPARENT);
  300.  
  301.                 SetRect(&rec,20,450,200,470);
  302.                 DrawText(hdc, TEXT("DBOOT 2.0 - 2017"),strlen("DBOOT 2.0 - 2017"), &rec, DT_TOP|DT_LEFT);
  303.                
  304.                 EndPaint(hWnd, &ps);
  305.             }
  306.             break;
  307.         case WM_DESTROY:
  308.             DeactivateMHook();
  309.             PostQuitMessage(0);
  310.             break;
  311.         case WM_TIMER:
  312.             if (wParam == ID_TIMER_BOOTEND)
  313.             {
  314.                 g_bootEnd = TRUE;
  315.                 SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, 0, 0, 0);
  316.                 ShowWindow(hWnd, SW_HIDE);
  317.                 KillTimer(hWnd, ID_TIMER_BOOTEND);
  318.                
  319.                 if (!ActivateMHook()) {
  320.                     DeactivateMHook();
  321.                     PostQuitMessage(0);
  322.                 }
  323.             }
  324.             break;
  325.         default:
  326.             return DefWindowProc(hWnd, message, wParam, lParam);
  327.     }
  328.     return 0;
  329. }
  330.  
  331. BOOL FileExist(TCHAR *file)
  332. {
  333.     BOOL rc = 0;
  334.  
  335.     DWORD attribs = GetFileAttributes(file);
  336.     if (attribs != -1) {
  337.         if ( (attribs & FILE_ATTRIBUTE_DIRECTORY) == 0)
  338.             rc = 1;
  339.     }
  340.  
  341.     return rc;
  342. }
  343.  
  344. BOOL ShellExe(TCHAR* file)
  345. {
  346.     if (file == NULL || !FileExist(file))
  347.         return FALSE;
  348.  
  349.     SHELLEXECUTEINFO sei;
  350.     memset(&sei, 0, sizeof(sei));
  351.     sei.cbSize = sizeof(sei);
  352.     sei.nShow = SW_SHOWNORMAL;
  353.     sei.hwnd = NULL;
  354.     sei.lpParameters = NULL;
  355.     sei.lpFile = file;
  356.  
  357.     return ShellExecuteEx(&sei);
  358. }
  359.  
  360. BOOL StartWinCE()
  361. {
  362.     DeleteFile(_T("\\Storage Card3\\StartWinCE"));
  363.  
  364.     HANDLE hFile;
  365.     hFile = CreateFile(_T("\\Windows\\Desktop\\Redemarrer.lnk"),
  366.                         GENERIC_WRITE,          // Open for writing
  367.                         0,                      // Do not share
  368.                         NULL,                   // No security
  369.                         OPEN_ALWAYS,            // Open or create
  370.                         FILE_ATTRIBUTE_NORMAL,  // Normal file
  371.                         NULL);                  // No template file
  372.  
  373.     if (hFile != INVALID_HANDLE_VALUE)
  374.     {
  375.         CHAR* lnk = "35#\"\\Storage Card\\System\\cereboot.exe\"";
  376.         DWORD nbrWr;
  377.         WriteFile(hFile, lnk, strlen(lnk), &nbrWr, 0);
  378.         CloseHandle(hFile);
  379.     }
  380.  
  381.     hFile = CreateFile(_T("\\Windows\\Desktop\\Redemarrer WinCE.lnk"),
  382.                         GENERIC_WRITE,          // Open for writing
  383.                         0,                      // Do not share
  384.                         NULL,                   // No security
  385.                         OPEN_ALWAYS,            // Open or create
  386.                         FILE_ATTRIBUTE_NORMAL,  // Normal file
  387.                         NULL);                  // No template file
  388.  
  389.     if (hFile != INVALID_HANDLE_VALUE)
  390.     {
  391.         CHAR* lnk = "32#\"\\Storage Card\\System\\dmenu.exe\"";
  392.         DWORD nbrWr;
  393.         WriteFile(hFile, lnk, strlen(lnk), &nbrWr, 0);
  394.         CloseHandle(hFile);
  395.     }
  396.  
  397.     ShellExe(_T("\\Windows\\explorer.exe"));
  398.  
  399.     return TRUE;
  400. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement