Advertisement
Guest User

mydll.cpp

a guest
Oct 5th, 2013
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. #include <cstdio>
  2. #include <ctime>
  3. #include <fstream>
  4. #include <iomanip>
  5. #include <string>
  6. #include <windows.h>
  7. #include <detours.h>
  8. #include <mscoree.h>
  9.  
  10. #pragma comment(lib,"detours.lib")
  11. #pragma comment(lib,"mscoree.lib")
  12. //#pragma comment(lib,"ws2_32.lib")
  13.  
  14. #pragma region hook initialization
  15. HMODULE hLib = LoadLibrary("wsock32.dll");
  16.  
  17. typedef int (WINAPI *SendPtr)(SOCKET s, const char* buf, int len, int flags);
  18. SendPtr pSend = (SendPtr)GetProcAddress(hLib, "send");
  19. int WINAPI MySend(SOCKET s, const char* buf, int len, int flags);
  20.  
  21. typedef int (WINAPI *RecvPtr)(SOCKET s, const char* buf, int len, int flags);
  22. RecvPtr pRecv = (RecvPtr)GetProcAddress(hLib, "recv");
  23. int WINAPI MyRecv(SOCKET s, char* buf, int len, int flags);
  24. #pragma endregion
  25.  
  26. #pragma region Logger
  27. std::ofstream Logger;
  28.  
  29. std::string NowToString() {
  30. time_t rawtime;
  31. tm *timeinfo = new tm();
  32. char buffer[32];
  33.  
  34. time( &rawtime );
  35. localtime_s( timeinfo, &rawtime );
  36.  
  37. strftime( buffer, 32, "%m/%d/%Y %I:%M:%S %p", timeinfo );
  38.  
  39. delete timeinfo;
  40.  
  41. return std::string( buffer );
  42. }
  43.  
  44. std::string TimeToString() {
  45. time_t rawtime;
  46. tm *timeinfo = new tm();
  47. char buffer[32];
  48.  
  49. time( &rawtime );
  50. localtime_s( timeinfo, &rawtime );
  51.  
  52. strftime( buffer, 32, "%I:%M:%S %p", timeinfo );
  53.  
  54. delete timeinfo;
  55.  
  56. return std::string( buffer );
  57. }
  58.  
  59. void LogPacket( const char *buf, int len ) {
  60. Logger << " 0 1 2 3 4 5 6 7 8 9 A B C D E F\n";
  61. Logger << " -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --\n";
  62. Logger << "0000 ";
  63.  
  64. for ( int i = 0; i < len; ++i ) {
  65. if ( i != 0 && i % 16 == 0 ) {
  66. Logger << " ";
  67.  
  68. int line = ( i / 16 ) - 1;
  69.  
  70. for ( int j = 0; j < 16; ++j ) {
  71. char c = buf[line * 16 + j];
  72.  
  73. if ( c >= 32 && c <= 126 ) {
  74. Logger << c;
  75. } else {
  76. Logger << '.';
  77. }
  78. }
  79.  
  80. Logger << "\n" << std::hex << std::setw( 4 ) << std::setfill( '0' ) << i << std::dec << std::setw( 0 ) << " ";
  81. } else if ( i % 16 == 8 ) {
  82. Logger << ' ';
  83. }
  84.  
  85. Logger << std::hex << std::setw( 2 ) << std::setfill( '0' ) << ( int( buf[i] ) & 0xFF ) << ' ';
  86. Logger << std::dec << std::setw( 0 );
  87.  
  88. if ( i == len - 1 ) {
  89. int remaining = 16 - ( len % 16 );
  90. int fill = ( remaining * 3 ) + 2;
  91.  
  92. if ( remaining >= 8 ) {
  93. ++fill;
  94. }
  95.  
  96. for ( int j = 0; j < fill; ++j ) {
  97. Logger << ' ';
  98. }
  99.  
  100. int line = ( i - ( ( len % 16 ) - 1 ) ) / 16 ;
  101.  
  102. for ( int k = 0; k < ( len % 16 ); ++k ) {
  103. char c = buf[line * 16 + k];
  104.  
  105. if ( c >= 32 && c <= 126 ) {
  106. Logger << c;
  107. } else {
  108. Logger << '.';
  109. }
  110. }
  111. }
  112. }
  113.  
  114. Logger << "\n\n";
  115. }
  116. #pragma endregion
  117.  
  118. int WINAPI MySend(SOCKET s, const char* buf, int len, int flags)
  119. {
  120. if (len < 2048)
  121. {
  122. Logger << TimeToString() << ": Client -> Server (Length: " << len << " bytes)*\n\n";
  123. LogPacket( buf, len );
  124. Logger << std::endl;
  125. }
  126.  
  127. return pSend(s, buf, len, flags);
  128. }
  129.  
  130. int WINAPI MyRecv(SOCKET s, char* buf, int len, int flags)
  131. {
  132. if (len < 2048)
  133. {
  134. Logger << TimeToString() << ": Server -> Client (Length: " << len << " bytes)*\n\n";
  135. LogPacket( buf, len );
  136. Logger << std::endl;
  137. }
  138.  
  139. return pRecv(s, buf, len, flags);
  140. }
  141.  
  142. extern "C" __declspec(dllexport) void dummy(void){
  143. return;
  144. }
  145.  
  146. BOOL WINAPI DllMain(HINSTANCE hinst, DWORD dwReason, LPVOID reserved)
  147. {
  148. if (DetourIsHelperProcess()) {
  149. return TRUE;
  150. }
  151.  
  152. if (dwReason == DLL_PROCESS_ATTACH) {
  153. MessageBoxA(0,"Attach",0,0);
  154. Logger.open( "C:\\Users\\Felipe\\Desktop\\mydll\\log\\Packets.txt", std::ios::out | std::ios::app | std::ios::ate );
  155.  
  156. if ( Logger.tellp() > 0 ) {
  157. Logger << "\n\n\n";
  158. }
  159.  
  160. Logger << "##\n## Logging Started (" << NowToString() << ")\n##\n\n\n";
  161.  
  162. DetourRestoreAfterWith();
  163. DetourTransactionBegin();
  164. DetourUpdateThread(GetCurrentThread());
  165. DetourAttach(&(PVOID&)pSend, MySend);
  166. if(DetourTransactionCommit() == NO_ERROR)
  167. {
  168. MessageBox(0,"send() detoured successfully","asd",MB_OK);
  169. }
  170.  
  171. DetourTransactionBegin();
  172. DetourUpdateThread(GetCurrentThread());
  173. DetourAttach(&(PVOID&)pRecv, MyRecv);
  174. if(DetourTransactionCommit() == NO_ERROR)
  175. {
  176. MessageBox(0,"recv() detoured successfully","asd",MB_OK);
  177. }
  178. }
  179. else if (dwReason == DLL_PROCESS_DETACH) {
  180. MessageBoxA(0,"Deattach",0,0);
  181. Logger << "##\n## Logging Stopped (" << NowToString() << ")\n##";
  182. Logger.close();
  183.  
  184. DetourTransactionBegin();
  185. DetourUpdateThread(GetCurrentThread());
  186. DetourDetach(&(PVOID&)pSend, MySend);
  187. DetourTransactionCommit();
  188.  
  189. DetourTransactionBegin();
  190. DetourUpdateThread(GetCurrentThread());
  191. DetourDetach(&(PVOID&)pRecv, MyRecv);
  192. DetourTransactionCommit();
  193. }
  194. return TRUE;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement