/* FILENAME: badEXT.cpp AUTHOR: Mourad Lagdas COMPANY: Microsoft Corporation DATE: 01/27/2003 */ #define _WIN32_WINNT 0x0400 #define DEFAULT_LEN ( 1024) #define MAX_S (15) #define MAX_V (3) #include #include #include #include #include CRITICAL_SECTION cs1; CRITICAL_SECTION cs2; DWORD NoScenario(EXTENSION_CONTROL_BLOCK *lpEcb); void getScVa(EXTENSION_CONTROL_BLOCK *lpEcb, char *szS, char *szV); void getQSVars (EXTENSION_CONTROL_BLOCK *lpEcb, 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(EXTENSION_CONTROL_BLOCK *lpEcb, int Variation); DWORD doAV2(EXTENSION_CONTROL_BLOCK *lpEcb, int Variation); DWORD doIH1(EXTENSION_CONTROL_BLOCK *lpEcb, int Variation); DWORD doSC1(EXTENSION_CONTROL_BLOCK *lpEcb, int Variation); DWORD doSO1(EXTENSION_CONTROL_BLOCK *lpEcb, int Variation); DWORD doHC1(EXTENSION_CONTROL_BLOCK *lpEcb, int Variation); DWORD doHC2(EXTENSION_CONTROL_BLOCK *lpEcb, int Variation); DWORD doHC3(EXTENSION_CONTROL_BLOCK *lpEcb, int Variation); DWORD doDL1(EXTENSION_CONTROL_BLOCK *lpEcb, int Variation); DWORD doCPU1(EXTENSION_CONTROL_BLOCK *lpEcb, int Variation); DWORD doHandleLeak1(EXTENSION_CONTROL_BLOCK *lpEcb, int Variation); DWORD doSlow1(EXTENSION_CONTROL_BLOCK *lpEcb, int Variation); DWORD doMemLeak1(EXTENSION_CONTROL_BLOCK *lpEcb, int Variation); //Added 04/01/2003 by mouradl to implement new exceptions DWORD doDZ1(EXTENSION_CONTROL_BLOCK *lpEcb, int Variation); //Integer Divide by zero (2 variations) DWORD doRE1(EXTENSION_CONTROL_BLOCK *lpEcb, int Variation); //Raise Exception (handled: 2 variations) DWORD doRE2(EXTENSION_CONTROL_BLOCK *lpEcb, int Variation); //Raise Exception (Unhandled 2 variations) BOOL WINAPI GetExtensionVersion(HSE_VERSION_INFO *pVer) { pVer->dwExtensionVersion = MAKELONG(HSE_VERSION_MINOR, HSE_VERSION_MAJOR); lstrcpyn(pVer->lpszExtensionDesc, "IIS Debug Training Labs", HSE_MAX_EXT_DLL_NAME_LEN); return TRUE; } DWORD WINAPI HttpExtensionProc(EXTENSION_CONTROL_BLOCK *lpEcb) { // If no query string was sent, return the No Scenario page if (!strlen(lpEcb->lpszQueryString)) return NoScenario(lpEcb); char *szS; //Scenario char *szV; //Variation szS = new char[10]; szV = new char[3]; //Get Scenario and Variation getScVa(lpEcb, szS, szV); int Variation = atoi(szV + 1) ; //13 scenarios to go for now.... if (!strncmp(szS, "av1", 3)) { return doAV1(lpEcb, Variation); } else if (!strncmp(szS, "av2", 3)) { return doAV2(lpEcb, 1); } else if (!strncmp(szS, "ih1", 3)) { return doIH1(lpEcb, 1); } else if (!strncmp(szS, "sc1", 3)) { return doSC1(lpEcb, 1); } else if (!strncmp(szS, "so1", 3)) { return doSO1(lpEcb, 1); } else if (!strncmp(szS, "hc1", 3)) { return doHC1(lpEcb, Variation); } else if (!strncmp(szS, "hc2", 3)) { return doHC2(lpEcb, Variation); } else if (!strncmp(szS, "hc3", 3)) { return doHC3(lpEcb, Variation); } else if (!strncmp(szS, "dl1", 3)) { return doDL1(lpEcb, 1); } else if (!strncmp(szS, "cpu1", 4)) { return doCPU1(lpEcb, 1); } else if (!strncmp(szS, "handleleak1", 11)) { return doHandleLeak1(lpEcb, 1); } else if (!strncmp(szS, "slow1", 5)) { return doSlow1(lpEcb, 1); } else if (!strncmp(szS, "memleak1", 8)) { return doMemLeak1(lpEcb, 1); } else if (!strncmp(szS, "dz1", 3)) { return doDZ1(lpEcb, Variation); } else if (!strncmp(szS, "re1", 3)) { return doRE1(lpEcb, Variation); } else if (!strncmp(szS, "re2", 3)) { return doRE2(lpEcb, Variation); } else { //if no valid scenario... return NoScenario(lpEcb); } return HSE_STATUS_SUCCESS; } DWORD doAV1(EXTENSION_CONTROL_BLOCK *lpEcb, int Variation) { if (1 == Variation) { SYSTEMTIME* pSysTime = NULL; pSysTime->wYear = 2003; // av right here. } return HSE_STATUS_SUCCESS; } DWORD doAV2(EXTENSION_CONTROL_BLOCK *lpEcb, int Variation) { if (1 == Variation) { SYSTEMTIME* pSysTime = NULL; GetSystemTime( pSysTime ); // would av in GetSystemTime } return HSE_STATUS_SUCCESS; } DWORD doIH1(EXTENSION_CONTROL_BLOCK *lpEcb, int Variation) { DWORD dwThreadID; HANDLE hThread; HANDLE h = NULL; if (1 == Variation) { // CheckFile(); hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CheckFile, NULL, 0, &dwThreadID); } return HSE_STATUS_SUCCESS; } DWORD doSC1(EXTENSION_CONTROL_BLOCK *lpEcb, int Variation) { if (1 == Variation) { char _buffer[] = "01234567890123456789"; // more than 10 chars. corrupt( _buffer ); } return HSE_STATUS_SUCCESS; } DWORD doSO1(EXTENSION_CONTROL_BLOCK *lpEcb, 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 HSE_STATUS_SUCCESS; } DWORD doHC1(EXTENSION_CONTROL_BLOCK *lpEcb, 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 HSE_STATUS_SUCCESS; } DWORD doHC2(EXTENSION_CONTROL_BLOCK *lpEcb, 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 HSE_STATUS_SUCCESS; } DWORD doHC3(EXTENSION_CONTROL_BLOCK *lpEcb, 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 HSE_STATUS_SUCCESS; } DWORD doDL1(EXTENSION_CONTROL_BLOCK *lpEcb, 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 HSE_STATUS_SUCCESS; } DWORD doCPU1(EXTENSION_CONTROL_BLOCK *lpEcb, int Variation) { if (1 == Variation) { DWORD dwStartTime = GetTickCount(); while( GetTickCount() - dwStartTime <= 30000 ) { } } return HSE_STATUS_SUCCESS; } DWORD doHandleLeak1(EXTENSION_CONTROL_BLOCK *lpEcb, int Variation) { if (1 == Variation) { HKEY hKey; LONG lRet; lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\ProductOptions", 0, KEY_QUERY_VALUE, &hKey ); } return HSE_STATUS_SUCCESS; } DWORD doSlow1(EXTENSION_CONTROL_BLOCK *lpEcb, int Variation) { char *szVar = new char[10]; char *szVal = new char[10]; getQSVars (lpEcb, 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 ); DWORD dw = NoScenario(lpEcb); } return HSE_STATUS_SUCCESS; } DWORD doMemLeak1(EXTENSION_CONTROL_BLOCK *lpEcb, int Variation) { char *szVar = new char[10]; char *szVal = new char[10]; getQSVars (lpEcb, 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 HSE_STATUS_SUCCESS; } DWORD doDZ1(EXTENSION_CONTROL_BLOCK *lpEcb, 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 HSE_STATUS_SUCCESS; } DWORD doRE1(EXTENSION_CONTROL_BLOCK *lpEcb, 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 HSE_STATUS_SUCCESS; } DWORD doRE2(EXTENSION_CONTROL_BLOCK *lpEcb, 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 HSE_STATUS_SUCCESS; } 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; } 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 ); HANDLE h = INVALID_HANDLE_VALUE; CloseHandle( h ); CloseHandle( NULL ); // LPWIN32_FIND_DATA lpFindFileData; // HANDLE hFile2 = FindFirstFile("c:\nofileIn", lpFindFileData); // CloseHandle (hFile2); return 1; } // Get scenario and variation void getScVa(EXTENSION_CONTROL_BLOCK *lpEcb, char *szS, char *szV) { char szQS[DEFAULT_LEN]; strcpy(szQS, lpEcb->lpszQueryString); 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 (EXTENSION_CONTROL_BLOCK *lpEcb, char *szVar, char *szVal) { char szQS[DEFAULT_LEN]; strcpy(szQS, lpEcb->lpszQueryString); 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 NoScenario(EXTENSION_CONTROL_BLOCK *lpEcb) { char szOutput[1024]; DWORD dwBuffSize; // Send headers lpEcb->ServerSupportFunction(lpEcb->ConnID, HSE_REQ_SEND_RESPONSE_HEADER, NULL, NULL, (LPDWORD)"Content-type: text/html\r\n\r\n"); // Build the response page strcpy(szOutput, "\r\n"); strcat(szOutput, "IIS Debug Labs\r\n"); strcat(szOutput, "

IIS Debug Training Labs (ISAPI Extension)

\r\n"); strcat(szOutput, "

No Valid Scenario Specified

"); strcat(szOutput, ""); strcat(szOutput, "\r\n"); // Return the page to the browser dwBuffSize = strlen(szOutput); lpEcb->WriteClient(lpEcb->ConnID, szOutput, &dwBuffSize, 0); return HSE_STATUS_SUCCESS; }