Guest User

ghost recon phantoms d3d hack

a guest
Mar 2nd, 2015
3,702
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.82 KB | None | 0 0
  1. //ghost recon phantoms hack by Nseven
  2. //credits: scrapdizle, ntKid, Citadell, s0beit, google
  3.  
  4. #include <Windows.h>
  5. #include <fstream>
  6. #include <vector>
  7. #include <d3d9.h>
  8. #include <d3dx9.h>
  9. #pragma comment (lib, "d3dx9.lib")
  10. #pragma comment (lib, "d3d9.lib")
  11. #include <detours.h>
  12. using namespace std;
  13.  
  14. //=====================================================================================
  15.  
  16. // settings
  17. DWORD aimkey = VK_SHIFT;            //aimkey
  18. int aimfov = 25;                //aim fov in %
  19. float aimheight = 1.2f;             //adjust aim height, 0 = feet
  20. int aimsens = 3;                //aimsensitivity (high aim sensitivity is for high mouse sensitivity)
  21.  
  22. //device
  23. LPDIRECT3DDEVICE9 Device;
  24.  
  25. //init once
  26. bool FirstInit = false;
  27.  
  28. //vertexshader
  29. //IDirect3DVertexShader9* vShader;
  30. //UINT vSize;
  31.  
  32. //models
  33. bool BODY;
  34. bool HEAD;
  35.  
  36. //viewport
  37. D3DVIEWPORT9 Viewport;
  38.  
  39. //
  40. UINT mStartRegister;
  41. UINT mVector4fCount;
  42.  
  43. //=====================================================================================
  44.  
  45. typedef HRESULT (WINAPI* tDrawIndexedPrimitive)(LPDIRECT3DDEVICE9 Device, D3DPRIMITIVETYPE PrimType,INT BaseVertexIndex,UINT MinVertexIndex,UINT NumVertices,UINT startIndex,UINT primCount);
  46. tDrawIndexedPrimitive oDrawIndexedPrimitive;
  47.  
  48. typedef HRESULT (WINAPI* tEndScene)(LPDIRECT3DDEVICE9 Device);
  49. tEndScene oEndScene;
  50.  
  51. typedef HRESULT (WINAPI* tReset) (LPDIRECT3DDEVICE9 Device, D3DPRESENT_PARAMETERS *pPresentationParameters);
  52. tReset oReset;
  53.  
  54. typedef HRESULT(__stdcall* tCreateQuery)(LPDIRECT3DDEVICE9 Device, D3DQUERYTYPE Type, IDirect3DQuery9** ppQuery);
  55. tCreateQuery oCreateQuery;
  56.  
  57. typedef HRESULT(__stdcall* tSetVertexShaderConstantF)(LPDIRECT3DDEVICE9 Device, UINT StartRegister, const float *pConstantData, UINT Vector4fCount);
  58. tSetVertexShaderConstantF oSetVertexShaderConstantF;
  59.  
  60. //=====================================================================================
  61.  
  62. LPDIRECT3DTEXTURE9    texRed, texYellow, texBlue;
  63. HRESULT GenerateTexture(IDirect3DDevice9 *pD3Ddev, IDirect3DTexture9 **ppD3Dtex, DWORD colour32)
  64. {
  65.     if (FAILED(pD3Ddev->CreateTexture(8, 8, 1, 0, D3DFMT_A4R4G4B4, D3DPOOL_MANAGED, ppD3Dtex, NULL)))
  66.         return E_FAIL;
  67.  
  68.     WORD colour16 = ((WORD)((colour32 >> 28) & 0xF) << 12)
  69.         | (WORD)(((colour32 >> 20) & 0xF) << 8)
  70.         | (WORD)(((colour32 >> 12) & 0xF) << 4)
  71.         | (WORD)(((colour32 >> 4) & 0xF) << 0);
  72.  
  73.     D3DLOCKED_RECT d3dlr;
  74.     (*ppD3Dtex)->LockRect(0, &d3dlr, 0, 0);
  75.     WORD *pDst16 = (WORD*)d3dlr.pBits;
  76.  
  77.     for (int xy = 0; xy < 8 * 8; xy++)
  78.         *pDst16++ = colour16;
  79.  
  80.     (*ppD3Dtex)->UnlockRect(0);
  81.  
  82.     return S_OK;
  83. }
  84.  
  85. //=====================================================================================
  86.  
  87. void DrawPoint(LPDIRECT3DDEVICE9 Device, int baseX, int baseY, int baseW, int baseH, D3DCOLOR Cor)
  88. {
  89.     D3DRECT BarRect = { baseX, baseY, baseX + baseW, baseY + baseH };
  90.     Device->Clear(1, &BarRect, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, Cor, 0, 0);
  91. }
  92.  
  93. void DrawCornerBox(LPDIRECT3DDEVICE9 Device, int x, int y, int w, int h, int borderPx, DWORD borderColor) //with x,y being the center of the box
  94. {
  95.     DrawPoint(Device, x - (w / 2), (y - h + borderPx), w / 3, borderPx, borderColor); //bottom
  96.     DrawPoint(Device, x - (w / 2) + w - w / 3, (y - h + borderPx), w / 3, borderPx, borderColor); //bottom
  97.     DrawPoint(Device, x - (w / 2), (y - h + borderPx), borderPx, w / 3, borderColor); //left
  98.     DrawPoint(Device, x - (w / 2), (y - h + borderPx) + h - w / 3, borderPx, w / 3, borderColor); //left
  99.     DrawPoint(Device, x - (w / 2), y, w / 3, borderPx, borderColor); //top
  100.     DrawPoint(Device, x - (w / 2) + w - w / 3, y, w / 3, borderPx, borderColor); //top
  101.     DrawPoint(Device, (x + w - borderPx) - (w / 2), (y - h + borderPx), borderPx, w / 3, borderColor);//right
  102.     DrawPoint(Device, (x + w - borderPx) - (w / 2), (y - h + borderPx) + h - w / 3, borderPx, w / 3, borderColor);//right
  103. }
  104.  
  105. //=====================================================================================
  106.  
  107. //   Name               Reg   Size
  108. //   ------------------ ----- ----
  109. //   g_mObjectToView    c0       3
  110. //   g_mProjection      c4       4
  111. //   gaf_UnpackPosInfos c118     1
  112.  
  113. struct ModelInfo_t
  114. {
  115.     D3DXVECTOR3 Position2D;
  116.     D3DXVECTOR3 Position3D;
  117.     float CrosshairDistance;
  118. };
  119. std::vector<ModelInfo_t*>ModelInfo;
  120.  
  121. float GetDistance(float Xx, float Yy, float xX, float yY)
  122. {
  123.     return sqrt((yY - Yy) * (yY - Yy) + (xX - Xx) * (xX - Xx));
  124. }
  125.  
  126. void Worldtoscreen(LPDIRECT3DDEVICE9 Device)
  127. {
  128.     ModelInfo_t* pModel = new ModelInfo_t;
  129.  
  130.     D3DXMATRIX g_mProjection, g_mObjectToView, BSMatrix;
  131.     D3DXVECTOR3 VectorMiddle(0, 0, 0), ScreenMiddle(0, 0, (float)aimheight);
  132.     pModel->Position3D = ScreenMiddle;
  133.  
  134.     Device->GetVertexShaderConstantF(4, g_mProjection, 4);
  135.     Device->GetVertexShaderConstantF(0, g_mObjectToView, 3);
  136.  
  137.     D3DXMatrixIdentity(&BSMatrix);
  138.     D3DXMatrixTranspose(&g_mObjectToView, &g_mObjectToView);
  139.     D3DXMatrixTranspose(&g_mProjection, &g_mProjection);
  140.  
  141.     //D3DXVec3Unproject(&VectorMiddle, &ScreenMiddle, &Viewport, &g_mProjection, &g_mObjectToView, &BSMatrix);
  142.     D3DXVec3Project(&VectorMiddle, &pModel->Position3D, &Viewport, &g_mProjection, &g_mObjectToView, &BSMatrix);
  143.  
  144.         pModel->Position2D.x = VectorMiddle.x;
  145.         pModel->Position2D.y = VectorMiddle.y;
  146.  
  147.     ModelInfo.push_back(pModel);
  148.     //delete[] pModel;
  149. }
  150.  
  151. //=====================================================================================
  152.  
  153. HRESULT __stdcall hkSetVertexShaderConstantF(LPDIRECT3DDEVICE9 Device, UINT StartRegister, const float *pConstantData, UINT Vector4fCount)
  154. {
  155.     HRESULT hRet;
  156.  
  157.     if (Device == nullptr)
  158.     return oSetVertexShaderConstantF(Device, StartRegister, pConstantData, Vector4fCount);
  159.  
  160.     if (pConstantData == NULL)
  161.         return oSetVertexShaderConstantF(Device, StartRegister, pConstantData, Vector4fCount);
  162.  
  163.     hRet = (HRESULT)oSetVertexShaderConstantF(Device, StartRegister, pConstantData, Vector4fCount);
  164.  
  165.     if (SUCCEEDED(hRet))
  166.     {
  167.         mStartRegister = StartRegister;
  168.         mVector4fCount = Vector4fCount;
  169.     }
  170.  
  171.     return hRet;
  172. }
  173.  
  174. //=====================================================================================
  175.  
  176. HRESULT WINAPI hkDrawIndexedPrimitive(LPDIRECT3DDEVICE9 Device, D3DPRIMITIVETYPE PrimType,INT BaseVertexIndex,UINT MinVertexIndex,UINT NumVertices,UINT startIndex,UINT primCount)
  177. {
  178.     //HRESULT hRet;
  179.  
  180.     if (FirstInit == FALSE)
  181.     {
  182.         GenerateTexture(Device, &texRed, D3DCOLOR_ARGB(255, 255, 0, 0));
  183.         GenerateTexture(Device, &texYellow, D3DCOLOR_ARGB(255, 255, 255, 0));
  184.         GenerateTexture(Device, &texBlue, D3DCOLOR_ARGB(255, 0, 0, 255));
  185.  
  186.         FirstInit = TRUE;
  187.     }
  188.  
  189.     /*
  190.     //get vSize
  191.     hRet = Device->GetVertexShader(&vShader);
  192.     if (SUCCEEDED(hRet) && vShader != NULL)
  193.     {
  194.         hRet = vShader->GetFunction(NULL, &vSize);
  195.         if (SUCCEEDED(hRet) && vShader != NULL)
  196.         {
  197.             vShader->Release();
  198.             vShader = NULL;
  199.         }
  200.     }
  201.  
  202.     //model rec
  203.     //body near         //video settings
  204.     if ((vSize == 3712) ||      //high
  205.     (vSize == 3404)         //med, low & very low
  206.     ||
  207.     //body far
  208.     (vSize == 2524) ||      //med & high
  209.     (vSize == 2488))        //low & very low
  210.     BODY = true;
  211.     else BODY = false;
  212.  
  213.     //head near         //video settings
  214.     if ((vSize == 3592) ||      //high
  215.     (vSize == 3308)         //med, low & very low
  216.     ||
  217.     //head far
  218.     (vSize == 3308) ||      //med & high
  219.     (vSize == 3272) ||      //low & very low
  220.     (vSize == 3672))        //main menu chara
  221.     HEAD = true;
  222.     else HEAD = false;
  223.     */
  224.  
  225.     //head
  226.     if (mStartRegister == 128)
  227.         HEAD = true;
  228.     else HEAD = false;
  229.  
  230.     //body
  231.     if (mStartRegister == 192 || mStartRegister == 131)
  232.         BODY = true;
  233.     else BODY = false;
  234.  
  235.     //worldtoscreen (head chams can f.up aim)
  236.     if (HEAD)
  237.     {
  238.         Worldtoscreen(Device);
  239.     }
  240.  
  241.     //chams
  242.     if (BODY)
  243.     {
  244.         Device->SetTexture(0, texRed);
  245.         //Device->SetTexture(1, texRed);
  246.         Device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
  247.         oDrawIndexedPrimitive(Device, PrimType, BaseVertexIndex, MinVertexIndex, NumVertices, startIndex, primCount);
  248.         Device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
  249.         Device->SetTexture(0, texBlue);
  250.     }
  251.  
  252.     return oDrawIndexedPrimitive(Device, PrimType, BaseVertexIndex, MinVertexIndex, NumVertices, startIndex, primCount);
  253. }
  254.  
  255. //=====================================================================================
  256.  
  257. HRESULT WINAPI hkEndScene(LPDIRECT3DDEVICE9 Device)
  258. {
  259.     //aimbot & esp
  260.     Device->GetViewport(&Viewport);
  261.     if (ModelInfo.size() != NULL)
  262.     {
  263.         UINT BestTarget = -1;
  264.         DOUBLE fClosestPos = 99999;
  265.  
  266.         for (size_t i = 0; i < ModelInfo.size(); i += 1)
  267.         {
  268.             //drawbox on targets (Esp)
  269.             if (ModelInfo[i]->Position2D.x > 1 || ModelInfo[i]->Position2D.y > 1)
  270.             {
  271.                 DrawCornerBox(Device, (int)ModelInfo[i]->Position2D.x, (int)ModelInfo[i]->Position2D.y+20, 20, 30, 1, D3DCOLOR_ARGB(255, 255, 255, 0));
  272.             }
  273.            
  274.             //get screen center
  275.             float ScreenCenterX = Viewport.Width / 2.0f;
  276.             float ScreenCenterY = Viewport.Height / 2.0f;
  277.  
  278.             //aimfov
  279.             float radiusx = aimfov * (ScreenCenterX / 100);
  280.             float radiusy = aimfov * (ScreenCenterY / 100);
  281.  
  282.             //get crosshairdistance
  283.             ModelInfo[i]->CrosshairDistance = GetDistance(ModelInfo[i]->Position2D.x, ModelInfo[i]->Position2D.y, ScreenCenterX, ScreenCenterY);
  284.  
  285.             //if in fov
  286.             if (ModelInfo[i]->Position2D.x >= ScreenCenterX - radiusx && ModelInfo[i]->Position2D.x <= ScreenCenterX + radiusx && ModelInfo[i]->Position2D.y >= ScreenCenterY - radiusy && ModelInfo[i]->Position2D.y <= ScreenCenterY + radiusy)
  287.  
  288.                 //get closest/nearest target to crosshair
  289.                 if (ModelInfo[i]->CrosshairDistance < fClosestPos)
  290.                 {
  291.                 fClosestPos = ModelInfo[i]->CrosshairDistance;
  292.                 BestTarget = i;
  293.                 }
  294.         }
  295.  
  296.         //if nearest target to crosshair
  297.         if (BestTarget != -1)
  298.         {
  299.             double DistX = ModelInfo[BestTarget]->Position2D.x - Viewport.Width / 2.0f;
  300.             double DistY = ModelInfo[BestTarget]->Position2D.y - Viewport.Height / 2.0f;
  301.  
  302.             //aimsmooth
  303.             DistX /= aimsens;
  304.             DistY /= aimsens;
  305.  
  306.             //if aimkey is pressed
  307.             if ((GetAsyncKeyState(aimkey) & 0x8000))
  308.                 mouse_event(MOUSEEVENTF_MOVE, (DWORD)DistX, (DWORD)DistY, NULL, NULL); //doaim, move mouse to x & y
  309.         }
  310.  
  311.         ModelInfo.clear();
  312.     }
  313.  
  314.     return oEndScene(Device);
  315. }
  316.  
  317. //=====================================================================================
  318.  
  319. HRESULT WINAPI hkReset(LPDIRECT3DDEVICE9 Device, D3DPRESENT_PARAMETERS *pPresentationParameters)
  320. {
  321.     HRESULT hRet = oReset(Device, pPresentationParameters);
  322.  
  323.     if (hRet == D3D_OK)
  324.     {
  325.        
  326.     }
  327.  
  328.     if (SUCCEEDED(hRet))
  329.     {
  330.  
  331.     }
  332.  
  333.     return hRet;
  334. }
  335.  
  336. //=====================================================================================
  337.  
  338. class CFakeQuery : public IDirect3DQuery9
  339. {
  340. public:
  341.     HRESULT WINAPI QueryInterface(REFIID riid, void** ppvObj)
  342.     {
  343.         return D3D_OK;
  344.     }
  345.  
  346.     ULONG WINAPI AddRef()
  347.     {
  348.         m_ref++;
  349.  
  350.         return m_ref;
  351.     }
  352.  
  353.     ULONG WINAPI Release()
  354.     {
  355.         return 1;
  356.     }
  357.  
  358.     HRESULT WINAPI GetDevice(IDirect3DDevice9 **ppDevice)
  359.     {
  360.         return D3D_OK;
  361.     }
  362.  
  363.     D3DQUERYTYPE WINAPI GetType()
  364.     {
  365.         return m_type;
  366.     }
  367.  
  368.     DWORD WINAPI GetDataSize()
  369.     {
  370.         return sizeof(DWORD);
  371.     }
  372.  
  373.     HRESULT    WINAPI Issue(DWORD dwIssueFlags)
  374.     {
  375.         return D3D_OK;
  376.     }
  377.  
  378.     HRESULT WINAPI GetData(void* pData, DWORD dwSize, DWORD dwGetDataFlags)
  379.     {
  380.         DWORD* pdwData = (DWORD*)pData;
  381.  
  382.         *pdwData = 500; //500 pixels visible
  383.  
  384.         return D3D_OK;
  385.     }
  386.  
  387.     int                m_ref;
  388.     D3DQUERYTYPE    m_type;
  389. };
  390.  
  391. //=====================================================================================
  392.  
  393. HRESULT __stdcall hkCreateQuery(LPDIRECT3DDEVICE9 Device, D3DQUERYTYPE Type, IDirect3DQuery9** ppQuery)
  394. {
  395.     if (Type == D3DQUERYTYPE_OCCLUSION)
  396.     {
  397.         *ppQuery = new CFakeQuery;
  398.  
  399.         ((CFakeQuery*)*ppQuery)->m_type = Type;
  400.  
  401.         return D3D_OK;
  402.     }
  403.  
  404.     return oCreateQuery(Device, Type, ppQuery);
  405. }
  406.  
  407. //=====================================================================================
  408.  
  409. LRESULT CALLBACK MsgProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
  410. {
  411.     return DefWindowProc(hwnd, uMsg, wParam, lParam);
  412. }
  413.  
  414. //=====================================================================================
  415.  
  416. DWORD* pVTable;
  417. void DX_Init(DWORD* table)
  418. {
  419.     WNDCLASSEX wc = { sizeof(WNDCLASSEX),CS_CLASSDC,MsgProc,0L,0L,GetModuleHandle(NULL),NULL,NULL,NULL,NULL,"DX",NULL};
  420.     RegisterClassEx(&wc);
  421.     HWND hWnd = CreateWindow("DX",NULL,WS_OVERLAPPEDWINDOW,100,100,300,300,GetDesktopWindow(),NULL,wc.hInstance,NULL);
  422.     LPDIRECT3D9 pD3D = Direct3DCreate9( D3D_SDK_VERSION );
  423.     D3DPRESENT_PARAMETERS d3dpp;
  424.     ZeroMemory( &d3dpp, sizeof(d3dpp) );
  425.     d3dpp.Windowed = TRUE;
  426.     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  427.     d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
  428.    
  429.     pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&Device);
  430.     pVTable = (DWORD*)Device;
  431.     pVTable = (DWORD*)pVTable[0];
  432.  
  433.     DestroyWindow(hWnd);
  434. }
  435.  
  436. //=====================================================================================
  437.  
  438. HMODULE hmRendDx9Base = NULL;
  439. DWORD WINAPI DxHook(LPVOID)
  440. {
  441.     DWORD VTable[3] = {0};
  442.  
  443.     while(hmRendDx9Base == NULL)
  444.     {
  445.         hmRendDx9Base = GetModuleHandleA("d3d9.dll");
  446.         Sleep(250);
  447.     }
  448.     CloseHandle(hmRendDx9Base);
  449.  
  450.     DX_Init(VTable);
  451.     oDrawIndexedPrimitive = (tDrawIndexedPrimitive)DetourFunction((BYTE*)pVTable[82], (BYTE*)hkDrawIndexedPrimitive);
  452.     oEndScene = (tEndScene)DetourFunction((BYTE*)pVTable[42], (BYTE*)hkEndScene);
  453.     oSetVertexShaderConstantF = (tSetVertexShaderConstantF)DetourFunction((BYTE*)pVTable[94], (BYTE*)hkSetVertexShaderConstantF);
  454.     oCreateQuery = (tCreateQuery)DetourFunction((BYTE*)pVTable[118], (BYTE*)hkCreateQuery);
  455.     //oReset = (tReset)DetourFunction((BYTE*)pVTable[16], (BYTE*)hkReset);
  456.     return 0;
  457. }
  458.  
  459. //=====================================================================================
  460.  
  461. BOOL APIENTRY DllMain( HMODULE hModule, DWORD  ulReason, LPVOID lpReserved )
  462. {
  463.     switch (ulReason)
  464.     {
  465.         case DLL_PROCESS_ATTACH:
  466.         {
  467.             DisableThreadLibraryCalls(hModule);
  468.  
  469.             CreateThread(NULL, NULL, DxHook, NULL, NULL, NULL);
  470.         }
  471.         break;
  472.         case DLL_PROCESS_DETACH:
  473.         case DLL_THREAD_ATTACH:
  474.         case DLL_THREAD_DETACH:
  475.         break;
  476.         }
  477.     return TRUE;
  478. }
  479.  
  480. //http://i60.tinypic.com/2q3w0gh.jpg
Advertisement
Add Comment
Please, Sign In to add comment