// Scenarios.cpp : Implementation of CScenarios #include "stdafx.h" #include "BadCOM.h" #include "Scenarios.h" ///////////////////////////////////////////////////////////////////////////// // CScenarios #include #include #include #include #include #define DEFAULT_LEN ( 1024) #define MAX_S (15) #define MAX_V (3) void getScVa(char *szS, char *szV); void getQSVars (char *szVar, char *szVal); void FillChar1( char* sz, char c, int len ); void FillChar2( char* sz, char c, int len ); void corrupt( const char *str ); char* FindChar(char* sz, char c); void LockTables( ); void LockAll( ); DWORD CheckFile(); DWORD DBZ(); DWORD REContinuable(); DWORD RENoncontinuable(); //still not sure if I really need this 1st param, i may take it out DWORD doAV1(int Variation); DWORD doAV2(int Variation); DWORD do2CE1(int Variation); DWORD doSC1(int Variation); DWORD doSO1(int Variation); DWORD doHC1(int Variation); DWORD doHC2(int Variation); DWORD doHC3(int Variation); DWORD doDL1(int Variation); DWORD doCPU1(int Variation); DWORD doHandleLeak1(int Variation); DWORD doSlow1(int Variation); DWORD doMemLeak1(int Variation); //Added 04/01/2003 by mouradl to implement new exceptions DWORD doDZ1(int Variation); //Integer Divide by zero (2 variations) DWORD doRE1(int Variation); //Raise Exception (handled: 2 variations) DWORD doRE2(int Variation); //Raise Exception (Unhandled 2 variations) CRITICAL_SECTION cs1; CRITICAL_SECTION cs2; DWORD dwThreadID; HANDLE hThread; char* szQS; // Query String BSTR bstrQS; //Query String stored in a BSTR for conversion purposes STDMETHODIMP CScenarios::getSc(VARIANT *pVar) { // TODO: Add your implementation code here return S_OK; } STDMETHODIMP CScenarios::sendSc(VARIANT *pVar) { // TODO: Add your implementation code here return S_OK; } STDMETHODIMP CScenarios::processSc(VARIANT *pVar) { // TODO: Add your implementation code here // Cahnge 06/09/2003 Less overhead to gather // all functionality in "processSc // We will leave "getSc" and "sendSc" for further enhancements szQS = new char[DEFAULT_LEN]; pVar->vt = VT_BSTR; USES_CONVERSION; if (pVar->bstrVal) { bstrQS = pVar->bstrVal; szQS = W2A(bstrQS); } char *szS; //Scenario char *szV; //Variation szS = new char[10]; szV = new char[3]; //Get Scenario and Variation getScVa(szS, szV); int Variation = atoi(szV + 1) ; //13 scenarios to go for now.... if (!strncmp(szS, "av1", 3)) { return doAV1(1); } else if (!strncmp(szS, "av2", 3)) { return doAV2(1); } else if (!strncmp(szS, "2ce1", 4)) { return do2CE1(1); } else if (!strncmp(szS, "sc1", 3)) { return doSC1(1); } else if (!strncmp(szS, "so1", 3)) { return doSO1(1); } else if (!strncmp(szS, "hc1", 3)) { return doHC1(Variation); } else if (!strncmp(szS, "hc2", 3)) { return doHC2(Variation); } else if (!strncmp(szS, "hc3", 3)) { return doHC3(Variation); } else if (!strncmp(szS, "dl1", 3)) { return doDL1(1); } else if (!strncmp(szS, "cpu1", 4)) { return doCPU1(1); } else if (!strncmp(szS, "handleleak1", 11)) { return doHandleLeak1(1); } else if (!strncmp(szS, "slow1", 5)) { return doSlow1(1); } else if (!strncmp(szS, "memleak1", 8)) { return doMemLeak1(1); } else if (!strncmp(szS, "dz1", 3)) { return doDZ1(Variation); } else if (!strncmp(szS, "re1", 3)) { return doRE1(Variation); } else if (!strncmp(szS, "re2", 3)) { return doRE2(Variation); } else { char* szResponse = new char [DEFAULT_LEN]; BSTR bstrResponse; strcpy(szResponse, "\r\n"); strcat (szResponse, "IIS Debug Training Labs \n"); strcat (szResponse, "

IIS Debug Training Labs (COM)

\n"); strcat (szResponse, "

No Valid Scenario Specified

"); strcat (szResponse, ""); strcat(szResponse, "\r\n"); bstrResponse = A2W(szResponse); pVar -> bstrVal = SysAllocString(bstrResponse); } return S_OK; } DWORD doAV1(int Variation) { if (1 == Variation) { SYSTEMTIME* pSysTime = NULL; pSysTime->wYear = 2003; // av right here. } return S_OK; } DWORD doAV2( int Variation) { if (1 == Variation) { SYSTEMTIME* pSysTime = NULL; GetSystemTime( pSysTime ); // would av in GetSystemTime } return S_OK; } DWORD do2CE1(int Variation) { if (1 == Variation) { HANDLE hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CheckFile, NULL, 0, &dwThreadID); } return S_OK; } DWORD doSC1(int Variation) { if (1 == Variation) { char _buffer[] = "01234567890123456789"; // more than 10 chars. corrupt( _buffer ); } return S_OK; } DWORD doSO1(int Variation) { if (1 == Variation) { char x = 'x'; HANDLE hHeap = GetProcessHeap(); char* pszBuffer = (char*)HeapAlloc( hHeap, 0, 16 ); char* sztmp = FindChar( pszBuffer, x ); HeapFree( hHeap, 0, pszBuffer ); } return S_OK; } DWORD doHC1(int Variation) //Buffer Overwrite { if (1 == Variation) // Straight forward to corrupt the heap { char *ptr, *tmp; int i; ptr = (char*)GlobalAlloc(GMEM_FIXED, 16); tmp = ptr; for (i = 0; i < 32; ++i) *(tmp++) = 'a'; free(ptr); } if (2 == Variation)// calling other functions to corrupt the heap { char ch1 = 'x'; char ch2 = 'y'; HANDLE hHeap = HeapCreate( 0, 0, 0 ); char* pszBuffer = (char*)HeapAlloc( hHeap, 0, 16 ); char* pszBuffer2 = (char*)HeapAlloc( hHeap, 0, 16 ); FillChar1( pszBuffer, ch1, 0x16 ); FillChar1( pszBuffer2, ch2, 16 ); HeapFree( hHeap, 0, pszBuffer2 ); HeapFree( hHeap, 0, pszBuffer ); HeapDestroy( hHeap ); } return S_OK; } DWORD doHC2(int Variation)//Double Free { if (1 == Variation) { char *ptr, *tmp; int i; ptr = (char*)GlobalAlloc(GMEM_FIXED, 16); tmp = ptr; for (i = 0; i < 16; ++i) *(tmp++) = 'b'; free(ptr); free(tmp); free (ptr); return (DWORD) ptr; } if (2 == Variation)// calling other functions to corrupt the heap { char* pszBuffer; char ch = 'x'; HANDLE hHeap = GetProcessHeap(); pszBuffer = (char*)HeapAlloc( hHeap, 0, 16 ); FillChar2( pszBuffer, ch, 16 ); HeapFree( hHeap, 0, pszBuffer ); return (DWORD) pszBuffer; } return S_OK; } DWORD doHC3(int Variation) //Old pointer reuse { if (1 == Variation) { char *ptr, *tmp; int i; ptr = (char*)GlobalAlloc(GMEM_FIXED, 16); tmp = ptr; for (i = 0; i < 16; ++i) *(tmp++) = 'a'; free(ptr); *ptr = 'x'; return (DWORD)ptr; } if (2 == Variation)// calling other functions to corrupt the heap { char* pszBuffer = (char*)HeapAlloc( GetProcessHeap(), 0, 16 ); lstrcpy( pszBuffer, "xxxxxxxxxxxxxxx" ); HeapFree( GetProcessHeap(), 0, pszBuffer ); char* pszBuffer1 = (char*)HeapAlloc( GetProcessHeap(), 0, 16 ); //int i = strcmp(pszBuffer, pszBuffer1); lstrcpy( pszBuffer, "yyyyyyyyyyyyyyy" ); return (DWORD) pszBuffer; } return S_OK; } DWORD doDL1(int Variation) { DWORD dw; DWORD dwThreadID; HANDLE lpHandles[2]; if (1 == Variation) { InitializeCriticalSection( &cs1 ); InitializeCriticalSection( &cs2 ); EnterCriticalSection( &cs1 ); EnterCriticalSection( &cs2 ); lpHandles[0] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)LockTables, NULL, 0, &dwThreadID); lpHandles[1] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)LockAll, NULL, 0, &dwThreadID); LeaveCriticalSection( &cs1 ); LeaveCriticalSection( &cs2 ); dw = WaitForMultipleObjects(2, lpHandles, TRUE, INFINITE); DeleteCriticalSection( &cs1 ); DeleteCriticalSection( &cs2 ); } return S_OK; } DWORD doCPU1(int Variation) { if (1 == Variation) { DWORD dwStartTime = GetTickCount(); while( GetTickCount() - dwStartTime <= 30000 ) { } } return S_OK; } DWORD doHandleLeak1(int Variation) { if (1 == Variation) { HKEY hKey; LONG lRet; lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\ProductOptions", 0, KEY_QUERY_VALUE, &hKey ); } return S_OK; } DWORD doSlow1(int Variation) { char *szVar = new char[10]; char *szVal = new char[10]; getQSVars (szVar, szVal); int iTime; iTime = atoi( szVal ); iTime *=1000; //convert to ms since we get the time in sec if (1 == Variation) { //Cause iTime delay DWORD sl = SleepEx( iTime, FALSE ); } return S_OK; } DWORD doMemLeak1(int Variation) { char *szVar = new char[10]; char *szVal = new char[10]; getQSVars (szVar, szVal); int iType; iType = atoi( szVal ); if (1 == Variation) { void* lp = NULL; switch ( iType ) { case 1: lp = HeapAlloc( GetProcessHeap(), 0, 1024 ); //Tracked by "Windows Heap Allocator" break; case 2: lp = malloc( 1024 );//Tracked by "C Runtime Allocator" break; case 3: lp = VirtualAlloc( NULL, 1024, MEM_COMMIT, PAGE_READWRITE ); //Tracked by "Virtual Memory Allocator" break; default: lp = HeapAlloc( GetProcessHeap(), 0, 1024 ); break; } } return S_OK; } DWORD CheckFile() { HANDLE hFile = CreateFile("MYFILE.TXT", // create MYFILE.TXT GENERIC_WRITE, // open for writing 0, // do not share NULL, // no security OPEN_EXISTING, // overwrite existing FILE_ATTRIBUTE_NORMAL | // normal file FILE_FLAG_OVERLAPPED, // asynchronous I/O NULL); // no attr. template DWORD retVal = CloseHandle( hFile ); retVal = CloseHandle( hFile ); // CloseHandle( (HANDLE) NULL ); // LPWIN32_FIND_DATA lpFindFileData; // HANDLE hFile2 = FindFirstFile("c:\nofileIn", lpFindFileData); // CloseHandle (hFile2); return S_OK; } // Get scenario and variation void getScVa(char *szS, char *szV) { int i, pos_; //Reject if QS is null if (*szQS == '\0') { szS[0] = '\0'; szV[0] = '\0'; return; } //Reject if QS does not contain 's=" if (szQS[0] != 's' || szQS[1] != '=') { szS[0] = '\0'; szV[0] = '\0'; return; } //find the position of' _' for (i = 2; i < strlen(szQS) && szQS[i] != '_'; i++); //Reject if no varition pos_ = i; if (pos_ >= strlen(szQS) - 1) { szS[0] = '\0'; szV[0] = '\0'; return; } //Reject if it's a long Scenario if (pos_ - 2 >= MAX_S )//(2 bytes to deduct, 2 for (s=) { szS[0] = '\0'; szV[0] = '\0'; return; } //Reject if variation does not start with 'v' if (szQS[pos_ + 1] != 'v' || szQS[pos_ + 2] == '\0') { szS[0] = '\0'; szV[0] = '\0'; return; } //at this point QS is well formatted! for (i = 0; i + 2 < pos_; i++) szS[i] = szQS[i + 2]; szS[i] = '\0'; //getting Variation szV[0] = szQS[pos_ + 1]; szV[1] = szQS[pos_ + 2]; szV[2] = '\0'; return; } void getQSVars (char *szVar, char *szVal) { int i = 0; while (szQS[i] != '&' && i < strlen(szQS)) { i++; } int j = 0; i++; while (szQS[i] != '=' && i < strlen(szQS)) { szVar[j] = szQS[i]; i++; j++; } szVar[j] = '\0'; j = 0; while (i < strlen(szQS)) { szVal[j] = szQS[i + 1]; i++; j++; } szVal[j] = '\0'; return; } void corrupt( const char *str ) { char buffer[10]; // stack var. strcpy( buffer, str ); // overrun buffer. } char* FindChar(char* sz, char c) { char* ch = NULL; { ch = sz++; sz = FindChar( ch, c ); } return sz; } void FillChar1( char* sz, char c, int len ) { while( len ) { sz[(--len)] = c; } } void FillChar2( char* sz, char c, int len ) { while( (--len) ) { sz[len] = c; } if ( !(*sz) ) { // if we didn’t write any chars, free the buffer. HeapFree( GetProcessHeap(), 0, sz ); } } void LockTables( ) { EnterCriticalSection( &cs1 ); DWORD sl = SleepEx( 250, FALSE ); EnterCriticalSection( &cs2 ); LeaveCriticalSection( &cs2 ); LeaveCriticalSection( &cs1 ); } void LockAll( ) { EnterCriticalSection( &cs2 ); DWORD sl = SleepEx( 150, FALSE ); EnterCriticalSection( &cs1 ); LeaveCriticalSection( &cs1 ); LeaveCriticalSection( &cs2 ); } DWORD doDZ1(int Variation) { DWORD dwThreadID; HANDLE hThread; if (Variation == 1) { int i = 0; int j = 1; j = j / i; return j; } if (2 == Variation) { hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)DBZ, NULL, 0, &dwThreadID); } return S_OK; } DWORD doRE1(int Variation) { DWORD dwThreadID; HANDLE hThread; if (1 == Variation) { RaiseException(ERROR_INVALID_PARAMETER, 0, 0, NULL); } if (2 == Variation) { hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)REContinuable, NULL, 0, &dwThreadID); } return S_OK; } DWORD doRE2(int Variation) { DWORD dwThreadID; HANDLE hThread; if (1 == Variation) { RaiseException(ERROR_INVALID_PARAMETER, EXCEPTION_NONCONTINUABLE, 0, NULL); } if (2 == Variation) { hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)RENoncontinuable, NULL, 0, &dwThreadID); } return S_OK; } DWORD REContinuable() { RaiseException(ERROR_INVALID_PARAMETER, 0, 0, NULL); return 1; } DWORD RENoncontinuable() { RaiseException(ERROR_INVALID_PARAMETER, EXCEPTION_NONCONTINUABLE, 0, NULL); return 1; } DWORD DBZ() { int i = 0; int j = 1; j = j / i; return j; }