/* FILENAME: badFil.cpp AUTHOR: Mourad Lagdas COMPANY: Microsoft Corporation DATE: 04/01/2003 */ #define _WIN32_WINNT 0x0400 #define DEFAULT_LEN ( 256) #define MAX_S (15) #define MAX_V (3) #include #include #include #include #include #include // // This could be to a file or other output device such as a debugger // #define DEST __buff #define Write( x ) { \ char __buff[1024]; \ wsprintf x; \ OutputDebugString( __buff ); \ } // // Private prototypes to handle specified notifications // DWORD OnReadRaw( HTTP_FILTER_CONTEXT * pfc, HTTP_FILTER_RAW_DATA * pvData ); DWORD OnPreprocHeaders( HTTP_FILTER_CONTEXT * pfc, HTTP_FILTER_PREPROC_HEADERS * pvData ); DWORD OnEndOfRequest( HTTP_FILTER_CONTEXT * pfc ); DWORD OnEndOfNetSession( HTTP_FILTER_CONTEXT * pfc, HTTP_FILTER_LOG * pvData ); DWORD SendToClient( HTTP_FILTER_CONTEXT * pfc ); // // Globals // // // Pseudo context for tracking individual request threads, used in debugging // DWORD ReqNumber = 1000; CHAR szQS[256]; //Query String DWORD cbQS; //#bytes in Query String //Critical sections for deadlock demo CRITICAL_SECTION cs1; CRITICAL_SECTION cs2; //These are all the scenarios we need for now DWORD doAV1(int Variation); //AV in component code DWORD doAV2(int Variation); //AV in external code DWORD doSC1(int Variation); //Stack Corruption DWORD doSO1(int Variation); //Stock Overflow DWORD doHC1(int Variation); //Heap Corruption buffer overrun DWORD doHC2(int Variation); //Heap Corruption double free DWORD doHC3(int Variation); //Heap Corruption old pointer reuse DWORD doDL1(int Variation); //Deadlock DWORD doCPU1(int Variation); //High CPU DWORD doHandleLeak1(int Variation); // Handle Leak DWORD doSlow1(int Variation); //Slow Response DWORD doMemLeak1(int Variation); //Memory Leak DWORD doDZ1(int Variation); //Integer Divide by zero (2 variations) DWORD doRE1(int Variation); //Raise Exception continuable (handled: 2 variations) DWORD doRE2(int Variation); //Raise Exception nonecontinuable (Unhandled 2 variations) //These are functions to support implementation the scenarios. 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 DBZ(); DWORD REContinuable(); DWORD RENoncontinuable(); BOOL WINAPI TerminateFilter(DWORD dwFlags ) { Write(( DEST, "Filter TerminateExtension() called\n" )); return TRUE; } BOOL WINAPI GetFilterVersion( HTTP_FILTER_VERSION * pVer ) { Write(( DEST, "[GetFilterVersion] Server filter version is %d.%d\n", HIWORD( pVer->dwServerFilterVersion ), LOWORD( pVer->dwServerFilterVersion ) )); pVer->dwFilterVersion = MAKELONG( 0, 4 ); // Version 4.0 // // Specify the types and order of notification // pVer->dwFlags = ( //SF_NOTIFY_SECURE_PORT | //SF_NOTIFY_NONSECURE_PORT | SF_NOTIFY_READ_RAW_DATA | SF_NOTIFY_PREPROC_HEADERS | //SF_NOTIFY_AUTHENTICATION | //SF_NOTIFY_URL_MAP | //SF_NOTIFY_ACCESS_DENIED | //SF_NOTIFY_SEND_RESPONSE | //SF_NOTIFY_SEND_RAW_DATA | SF_NOTIFY_END_OF_REQUEST | //SF_NOTIFY_LOG | SF_NOTIFY_END_OF_NET_SESSION | SF_NOTIFY_ORDER_DEFAULT); strcpy( pVer->lpszFilterDesc, "IIS Debug Labs Filter version, v1.0" ); return TRUE; } DWORD WINAPI HttpFilterProc( HTTP_FILTER_CONTEXT *pfc, DWORD NotificationType, VOID *pvData ) { DWORD dwRet; // // If we don't have a context already, create one now // if ( !pfc->pFilterContext ) { CHAR buff[512]; DWORD cbText; pfc->pFilterContext = (VOID *) ReqNumber++; Write(( DEST, "[HttpFilterProc] New request, ID = %d, fIsSecurePort = %s\n", pfc->pFilterContext, (pfc->fIsSecurePort ? "TRUE" : "FALSE") )); // // Get non-existent variable // cbText = sizeof( buff ); if ( pfc->GetServerVariable( pfc, "Non-existent_variable", buff, &cbText )) { Write(( DEST, "Hey! This should have returned FALSE!!\n" )); } else if ( GetLastError() != ERROR_INVALID_INDEX ) { Write(( DEST, "Hey! This should have returned ERROR_INVALID_INDEX!!\n" )); } } // // Indicate this notification to the appropriate routine // switch ( NotificationType ) { case SF_NOTIFY_READ_RAW_DATA: dwRet = OnReadRaw( pfc, (PHTTP_FILTER_RAW_DATA) pvData ); break; case SF_NOTIFY_PREPROC_HEADERS: { //Handling this notification will get the query string dwRet = OnPreprocHeaders( pfc, (PHTTP_FILTER_PREPROC_HEADERS) pvData ); //We know now the query string, Let process each scenario char *szS; //Scenario char *szV; //Variation szS = new char[MAX_S]; szV = new char[MAX_V]; //Get Scenario and Variation getScVa(szS, szV); int Variation = atoi(szV + 1) ; //16 scenarios to go for now.... if (!strncmp(szS, "av1", 3)) { return doAV1(Variation); } else if (!strncmp(szS, "av2", 3)) { return doAV2(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 { //This was just for testing purposes //dwRet = SendToClient( pfc ); } } break; case SF_NOTIFY_END_OF_REQUEST: dwRet = OnEndOfRequest( pfc ); break; case SF_NOTIFY_END_OF_NET_SESSION: dwRet = OnEndOfNetSession( pfc, (PHTTP_FILTER_LOG) pvData ); // // We would delete any allocated memory here // pfc->pFilterContext = 0; break; default: dwRet = SF_STATUS_REQ_NEXT_NOTIFICATION; break; } return dwRet; } DWORD OnReadRaw( HTTP_FILTER_CONTEXT * pfc, HTTP_FILTER_RAW_DATA * pvData ) { DWORD cbText; CHAR buff[1024]; // // Show the first 3 bytes of raw data // Write(( DEST, "%d [OnReadRaw] %d bytes indicated, first 3 bytes: %c%c%c\n", pfc->pFilterContext, pvData->cbInData, ((BYTE *) pvData->pvInData)[0], ((BYTE *) pvData->pvInData)[1], ((BYTE *) pvData->pvInData)[2])); return SF_STATUS_REQ_NEXT_NOTIFICATION; } DWORD OnPreprocHeaders( HTTP_FILTER_CONTEXT * pfc, HTTP_FILTER_PREPROC_HEADERS * pvData ) { CHAR achUrl[512]; CHAR achUserAgent[512]; DWORD cb; DWORD cbText; CHAR buff[1024]; // // Get the url and user agent fields // cb = sizeof( achUrl )-1; if ( !pvData->GetHeader( pfc, "url", achUrl, &cb )) { return SF_STATUS_REQ_ERROR; } cb = sizeof( achUserAgent ); if ( !pvData->GetHeader( pfc, "User-Agent:", achUserAgent, &cb )) { strcpy( achUserAgent, "" ); } //Get the Query String cbQS = sizeof( szQS ); for (int i = 0; ipFilterContext, achUrl, achUserAgent, szQS)); // // Add our special "Foobar:" header for the *server* to process // if ( !pvData->AddHeader( pfc, "Foobar:", "Special foobar header for server to process" )) { return SF_STATUS_REQ_ERROR; } // // Add a header that will be sent back to the *client*. This could be // a special cookie header or something specific for the client // if ( !pfc->AddResponseHeaders( pfc, "Special-Cookie-Header: xyz\r\n", 0 )) { return SF_STATUS_REQ_ERROR; } return SF_STATUS_REQ_NEXT_NOTIFICATION; } DWORD OnEndOfRequest( HTTP_FILTER_CONTEXT * pfc ) { CHAR buff[1024]; DWORD cbText; Write(( DEST, "%d [OnEndOfRequest] Notification, queryS = %s\n", pfc->pFilterContext, szQS)); return SF_STATUS_REQ_NEXT_NOTIFICATION; } DWORD OnEndOfNetSession( HTTP_FILTER_CONTEXT * pfc, HTTP_FILTER_LOG * pvData ) { Write(( DEST, "%d [OnEndOfNetSession] End of request indicated\n", pfc->pFilterContext )); return SF_STATUS_REQ_NEXT_NOTIFICATION; } DWORD SendToClient(HTTP_FILTER_CONTEXT * pfc ) { #define MESSAGE_TEXT "

IIS Debug Labs Filter

" DWORD cbText; cbText = sizeof(MESSAGE_TEXT) - sizeof(CHAR); pfc->AddResponseHeaders(pfc, "Content-Type:text/html\r\nContent-Length:257\r\n\r\n", NULL); pfc->WriteClient( pfc, MESSAGE_TEXT, &cbText, 0 ); return SF_STATUS_REQ_FINISHED; } DWORD doAV1(int Variation) { if (1 == Variation) { SYSTEMTIME* pSysTime = NULL; pSysTime->wYear = 2003; // av right here. } return 1; } DWORD doAV2(int Variation) { if (1 == Variation) { SYSTEMTIME* pSysTime = NULL; GetSystemTime( pSysTime ); // would av in GetSystemTime } return 1; } DWORD doSC1(int Variation) { if (1 == Variation) { char _buffer[] = "01234567890123456789"; // more than 10 chars. corrupt( _buffer ); } return 1; } 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 1; } 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 1; } 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 1; } 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 1; } 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 1; } DWORD doCPU1(int Variation) { if (1 == Variation) { DWORD dwStartTime = GetTickCount(); while( GetTickCount() - dwStartTime <= 30000 ) { } } return 1; } 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 1; } 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 ); // DWORD dw = NoScenario(lpEcb); } return 1; } 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 1; } DWORD doDZ1(int Variation) { DWORD dwThreadID; HANDLE hThread; if (1 == Variation) { 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 1; } 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 1; } 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 1; } 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; } // 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 'sf=" if (szQS[0] != 's' || szQS[1] != 'f' || szQS[2] != '=') { szS[0] = '\0'; szV[0] = '\0'; return; } //find the position of' _' for (i = 3; 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_ - 3 >= MAX_S )//(2 bytes to deduct, 2 for (sf=) { 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 + 3 < pos_; i++) szS[i] = szQS[i + 3]; 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 (i < strlen(szQS)&& szQS[i] != '&') { i++; } if (i >= strlen(szQS)) { szVar[0] = '\0'; szVal[0] = '\0'; return; } int j = 0; i++; while (i < strlen(szQS) && szQS[i] != '=' ) { 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 ); }