1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
|
/*
* This filter is a flavor of the filter IIS uses for compression.
* (Original filter written by David Treadwell on July 1997.)
*
* ISA and IIS let you accumulate Requests chunks into a complete
* Request.
* The following filter is an example to a filter that collects the
* response chunks and then allows you to change them depending on the
* complete response.
* This filter works with 2 notifications.
* In Send Raw Data it collects the response's chunks, sends 0 bytes
* instead of them (i.e. sends nothing).
* Then, when all the chunks of this response passed Send Raw Data
* notification, ISA thinks the complete response was sent. So
* it calls End Of Request Notification. End Of Request Notification
* will be the place where we will send the complete response.
*
* Note. Since the filter allocates memory for accumulating a response from a
* Web server, it is vulnerable to a DoS attack. In this sample we show how
* to handle that issue using a threshold. When the memory needed for the
* response exceeds that threshold, the filter will behave as if it received
* an end-of-request notification and will call OnEndOfRequest. The
* OnEndOfRequest function will try do to change the response based on the
* partial data accumulated. After doing this, the filter will notify the
* ISA Server Web proxy to stop sending notifications regarding the current
* request. After this, the filter will transparent and pass the response
* without altering it.
*/
#include <windows.h>
#include <httpfilt.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <assert.h>
#include <time.h>
/*************** Declaration ***************/
const char *fileName = "C:\\LogWRM.txt"; // i made the C drive accessible to network service so this work fine
FILE *p = NULL;
const char LINE_TO_INSERT[] = "<h3>Brought to you by an ISA Web Filter</h3>\r\n";
const char CONTENT_LENGTH[] = "Content-Length:";
const int ACCUMULATION_SIZE_LIMIT = 10000;
/*************** Prototype ***************/
void logLn(char buffer[] ); // to log into a file to see the trace of the filter
static DWORD OnSendRawData(PHTTP_FILTER_CONTEXT pfc, PHTTP_FILTER_RAW_DATA pRawData);
static DWORD OnEndOfRequest(PHTTP_FILTER_CONTEXT pfc);
static void DisableNotifications(PHTTP_FILTER_CONTEXT pfc, DWORD flags);
static int strnstr(const char *string, const char *strCharSet , int n);
void OutputDebugStringF (const char* pchFormat, ...);
static bool InsertOurHTMLHeader(PHTTP_FILTER_CONTEXT pfc,LPBYTE *ppBuffer,LPDWORD lpdwLen);
static DWORD dwfnContentLen(LPSTR ContentLenLine,int iLineLen);
static DWORD IsFirstInCARP(PHTTP_FILTER_CONTEXT pfc, BOOL *IsFirst);
/*************** Implementation ***************/
void logLn(char buffer[] ){
time_t t;
time(&t);
if (p != NULL)
fprintf(p,"%s\t\t%s\n", ctime(&t), buffer);
};
BOOL WINAPI TerminateFilter ( DWORD dwFlags )
{
//logLn("" );
logLn("TerminateFilter" );
if (p != NULL)
fclose(p);
UNREFERENCED_PARAMETER(dwFlags);
return TRUE;
}
BOOL WINAPI GetFilterVersion ( PHTTP_FILTER_VERSION pVer )
{
p = fopen(fileName, "a+" );
logLn("GetFilterVersion" );
if (pVer == NULL)
{
SetLastError( ERROR_INVALID_PARAMETER);
return FALSE;
}
pVer->dwFilterVersion = HTTP_FILTER_REVISION;
pVer->dwFlags = SF_NOTIFY_END_OF_REQUEST | SF_NOTIFY_SECURE_PORT | SF_NOTIFY_NONSECURE_PORT | SF_NOTIFY_SEND_RAW_DATA //| SF_NOTIFY_ORDER_HIGH
;
return TRUE;
}
DWORD WINAPI HttpFilterProc (
PHTTP_FILTER_CONTEXT pfc,
DWORD NotificationType,
LPVOID pvNotification
)
{
logLn("HttpFilterProc: begin" );
OutputDebugStringA("WEBRESPONSEMODIFIER: entring HttpFilterProc.\n" );
DWORD dwRet = SF_STATUS_REQ_NEXT_NOTIFICATION;
switch (NotificationType)
{
case SF_NOTIFY_SEND_RAW_DATA:
dwRet = OnSendRawData(pfc, (PHTTP_FILTER_RAW_DATA)pvNotification);
break;
case SF_NOTIFY_END_OF_REQUEST:
dwRet = OnEndOfRequest(pfc);
break;
default:
// We cannot reach here, unless Web Filter support has a BAD ERROR.
SetLastError( ERROR_INVALID_PARAMETER);
dwRet = SF_STATUS_REQ_ERROR;
break;
}
logLn("HttpFilterProc: end" );
return dwRet;
}
/*
* OnSendRawData():
* During Send Raw Data Notification we do the following:
* 1) Append each chunk to an accumulation buffer (pRawData->cvInData).
* 2) Resize the Current chunk to 0 (don't send anything.)
*/
static DWORD OnSendRawData ( PHTTP_FILTER_CONTEXT pfc, PHTTP_FILTER_RAW_DATA pInRawData )
{
logLn("OnSendRawData" );
if ( NULL == pfc || NULL == pInRawData)
{
SetLastError( ERROR_INVALID_PARAMETER);
return SF_STATUS_REQ_ERROR;
}
/*
* Making the filter CARP-aware.
* The filter is active only if we are in the last array server.
*/
BOOL IsFirst;
DWORD dwErrorCode = IsFirstInCARP(pfc,&IsFirst) ;
if (dwErrorCode != ERROR_SUCCESS)
{
SetLastError(dwErrorCode);
return SF_STATUS_REQ_ERROR;
}
if (!IsFirst)
{
OutputDebugStringA("WEBRESPONSEMODIFIER: OnSendRawData, intra array server, do nothing.\n" );
DisableNotifications(pfc, SF_NOTIFY_SEND_RAW_DATA);
return SF_STATUS_REQ_NEXT_NOTIFICATION;
}
OutputDebugStringA("WEBRESPONSEMODIFIER: First array server, accumulate data.\n" );
/*
* Called first time for this request - then allocate pRawData.
*/
DWORD dwReserved = 0;
if ( NULL == pfc->pFilterContext )
{
pfc->pFilterContext = (LPVOID)pfc->AllocMem( pfc, sizeof(HTTP_FILTER_RAW_DATA), dwReserved);
if ( NULL == pfc->pFilterContext )
{
DisableNotifications( pfc, SF_NOTIFY_END_OF_REQUEST | SF_NOTIFY_SEND_RAW_DATA);
SetLastError( ERROR_NOT_ENOUGH_MEMORY );
return SF_STATUS_REQ_ERROR;
}
PHTTP_FILTER_RAW_DATA pRawData = (PHTTP_FILTER_RAW_DATA)pfc->pFilterContext;
pRawData->cbInBuffer = pInRawData->cbInBuffer;
pRawData->pvInData = (LPVOID)pfc->AllocMem( pfc, pRawData->cbInBuffer, dwReserved);
if ( NULL == pRawData->pvInData )
{
DisableNotifications(pfc,SF_NOTIFY_END_OF_REQUEST | SF_NOTIFY_SEND_RAW_DATA);
SetLastError( ERROR_NOT_ENOUGH_MEMORY );
return SF_STATUS_REQ_ERROR;
}
pRawData->cbInData = 0;
pRawData->dwReserved = pInRawData->dwReserved ;
}
/*
* Get the pRawData from the Request Context.
*/
PHTTP_FILTER_RAW_DATA pRawData = (PHTTP_FILTER_RAW_DATA)pfc->pFilterContext;
/*
* Check for integer overflow
*/
DWORD NewSize = pInRawData->cbInData + pRawData->cbInData ;
if (NewSize < pInRawData->cbInData || NewSize < pRawData->cbInData )
{
DisableNotifications(pfc,SF_NOTIFY_END_OF_REQUEST | SF_NOTIFY_SEND_RAW_DATA);
SetLastError( ERROR_NOT_ENOUGH_MEMORY );
return SF_STATUS_REQ_ERROR;
}
OutputDebugStringF("WEBRESPONSEMODIFIER: new data size: %d.\n",NewSize);
/*
* Check if the memory needed exceeds the limit allowed (the threshold).
* Note. In this sample, when the filter identifies such a case, it will try to
* change the response based on the partial data accumulated. After that,
* the filter will cease to receive notifications for the current request and
* will pass the rest of the response without change. This sample can be
* modified so that the filter will block the response if the threshold is
* exceeded.
*/
if (NewSize > ACCUMULATION_SIZE_LIMIT)
{
OutputDebugStringF("WEBRESPONSEMODIFIER: OnSendRawData, required data size (%d) exceeds accumulation limit (%d) - trying to insert header.\n",NewSize,ACCUMULATION_SIZE_LIMIT);
DWORD dwRetVal = OnEndOfRequest(pfc);
DisableNotifications( pfc, SF_NOTIFY_END_OF_REQUEST | SF_NOTIFY_SEND_RAW_DATA);
return dwRetVal;
}
/*
* If the buffer in pRawData is insufficient, increase the buffer size.
* Note. To achieve better performance, when additional memory allocation
* is needed, the filter allocates twice the amount of memory that is needed.
* This way, we reduce the number of allocation operations significantly and
* achieve better performance for the filter.
*/
if ( NewSize > pRawData->cbInBuffer)
{
/*
* Check for integer overflow
*/
pRawData->cbInBuffer = 2*NewSize;
if (pRawData->cbInBuffer < NewSize)
{
DisableNotifications(pfc,SF_NOTIFY_END_OF_REQUEST | SF_NOTIFY_SEND_RAW_DATA);
SetLastError( ERROR_ARITHMETIC_OVERFLOW );
return SF_STATUS_REQ_ERROR;
}
LPBYTE lpBuffer = (LPBYTE)pfc->AllocMem(pfc,pRawData->cbInBuffer, dwReserved);
if ( NULL == lpBuffer )
{
DisableNotifications(pfc,SF_NOTIFY_END_OF_REQUEST | SF_NOTIFY_SEND_RAW_DATA);
SetLastError( ERROR_NOT_ENOUGH_MEMORY );
return SF_STATUS_REQ_ERROR;
}
memcpy((LPVOID)lpBuffer, pRawData->pvInData, pRawData->cbInData);
pRawData->pvInData = (void *)lpBuffer;
}
/*
* Append InRawData ( new chunk ) to accumulation buffer.
*/
LPBYTE lpBuffer = (LPBYTE)pRawData->pvInData;
memcpy((LPVOID)(&lpBuffer[pRawData->cbInData]), pInRawData->pvInData, pInRawData->cbInData);
pRawData->cbInData = pRawData->cbInData + pInRawData->cbInData;
/*
* Mark current chunk as size 0 ( i.e. Don't send enything. )
*/
pInRawData->cbInData = 0;
return SF_STATUS_REQ_NEXT_NOTIFICATION;
}
/*
* OnEndOfRequest():
* During End Of Request Notification we:
* 1) Get the complete response from the filter Request Specific Data.
* 2) May manipulate that response.
* 3) Send that response.
*/
static DWORD OnEndOfRequest ( PHTTP_FILTER_CONTEXT pfc )
{
logLn("OnEndOfRequest" );
if ( NULL == pfc)
{
SetLastError( ERROR_INVALID_PARAMETER);
return SF_STATUS_REQ_ERROR;
}
/*
* Making the filter CARP-aware. We change the HTTP reply (adding the header) only
* if we are the first server. Note that if this is not the first server, all the data already transferred
* and there is nothing left to do.
*
*/
BOOL IsFirst;
DWORD dwErrorCode = IsFirstInCARP(pfc,&IsFirst) ;
if (dwErrorCode != ERROR_SUCCESS)
{
SetLastError(dwErrorCode);
return SF_STATUS_REQ_ERROR;
}
if (!IsFirst)
{
OutputDebugStringA("WEBRESPONSEMODIFIER: OnEndOfRequest, intra array server, do nothing.\n" );
return SF_STATUS_REQ_NEXT_NOTIFICATION;
}
OutputDebugStringA("WEBRESPONSEMODIFIER: First array server, insert header and send.\n" );
/*
* Block the following SEND_RESPONSE and END_OF_REQUEST notifications for
* this request. ( WriteClient() bellow will generate them.)
*
*/
DisableNotifications( pfc, SF_NOTIFY_END_OF_REQUEST | SF_NOTIFY_SEND_RAW_DATA);
/*
* OK all data was accumulated. Now extract from Request Data
* the response buffer. (which now contains the complete response).
*/
PHTTP_FILTER_RAW_DATA pRawData = (PHTTP_FILTER_RAW_DATA)pfc->pFilterContext;
if ( NULL == pRawData)
{
return SF_STATUS_REQ_NEXT_NOTIFICATION;
}
LPBYTE lpBuffer = (LPBYTE)pRawData->pvInData;
DWORD bytesToSend = pRawData->cbInData;
/*
* Empty Request data to make it ready for next response
* in case the connection is kept alive
*/
pfc->pFilterContext = NULL;
/*
* Add here code to modify the response. It is now the complete response.
* any decisions and changes that demand the complete Response in hand
* should be done here.
*/
if (!InsertOurHTMLHeader( pfc, &lpBuffer, &bytesToSend))
{
SetLastError( ERROR_NOT_ENOUGH_MEMORY );
return SF_STATUS_REQ_ERROR;
}
/*
* Send the complete response in one chunk.
*/
DWORD dwReserved = 0 ;
if ( pfc->WriteClient(pfc,(LPVOID)lpBuffer,&bytesToSend,dwReserved) )
{
return SF_STATUS_REQ_NEXT_NOTIFICATION;
}
else
{
return SF_STATUS_REQ_ERROR;
}
}
#define STRING_SIZE(str) (sizeof(str) - 1)
/*
*
* InsertOurHTMLHeader()
* Given the full response chunk, test if the response is in HTML format (has
* <HTML> in it). If so, insert an HTML header line into the response
* HTML. To do that we look at the input HTML as made of 3 chunks:
* Chunk1 from start till "Content-Length: ..." header ( if exist.)
* Chunk2 from "\r\n" after "Content-Length:" header until last character of
* "<HTML>".
* Chunk3 from first character after "<HTML>" till the end of the response.
* We then calculate the new Content-Length value by adding the length of the
* line we will insert to the original content length, and finally allocate a new
* buffer and paste into it the chunks in the following order:
* Chunk1, Updateded-Content-Length-Header, Chunk2, Line-we-wish-to-insert, chunk3.
*
*/
static bool InsertOurHTMLHeader ( PHTTP_FILTER_CONTEXT pfc, LPBYTE *ppBuffer, LPDWORD lpdwLen )
{
logLn("InsertOurHTMLHeader" );
LPSTR lpBuffer = (LPSTR)*ppBuffer;
DWORD dwLen = *lpdwLen;
bool bStatus = true;
if (lpBuffer == NULL || dwLen > INT_MAX)
{
bStatus = false;
goto ReturnBuffer;
}
if ( 0 < strnstr( lpBuffer, "<HTML>", dwLen) )
{
/*
*
* Find first chunk. If no Content-Length header then first chunk will be
* zero sized.
*
*/
int iStartChunk1 = 0;
int iEndChunk1 = strnstr( lpBuffer, CONTENT_LENGTH, dwLen);
if ( 0 >= iEndChunk1 )
iEndChunk1 = 0;
int iTmp = 0;
if ( 0 < iEndChunk1 )
{
/*
*
* If we are here then there is a Content-Length header, so we
* find the first "\r\n" after the the Content-Length header.
*
*/
iTmp = strnstr( lpBuffer + iEndChunk1, "\r\n" , dwLen - iEndChunk1);
if ( 0 >= iTmp )
goto ReturnBuffer;
}
/*
*
* Find the second chunk. It starts on the "\r\n" after the Content-Length
* Header, and ends after the first "<HTML>".
*
*/
int iStartChunk2 = iEndChunk1 + iTmp;
iTmp = strnstr( lpBuffer + iStartChunk2, "<HTML>", dwLen - iStartChunk2);
if ( 0 >= iTmp )
goto ReturnBuffer;
int iEndChunk2 = iStartChunk2 + iTmp + STRING_SIZE("<HTML>" );
/*
*
* The third and last chunk starts after the first "<HTML>" and goes till
* the end of the input response.
*
*/
int iStartChunk3 = iEndChunk2;
int iEndChunk3 = (int)dwLen;
char szContentLen[100];
if ( 0 < iEndChunk1 )
{
/*
*
* If there is Content-Length Header then find the Input Content Length,
* and add to it the length of the inserted line.
*
*/
DWORD dwContentLen =
dwfnContentLen( lpBuffer + iEndChunk1, iStartChunk2 - iEndChunk1) +
STRING_SIZE(LINE_TO_INSERT);
char *p = (char *) memcpy( szContentLen, CONTENT_LENGTH, STRING_SIZE(CONTENT_LENGTH)) +
STRING_SIZE(CONTENT_LENGTH);
*p++ = ' ';
_ultoa( dwContentLen, p, 10);
}
else
{
szContentLen[0] = 0;
}
/*
*
* Allocate a buffer for the new response.
*
*/
iTmp = iEndChunk1 - iStartChunk1 + strlen(szContentLen) + iEndChunk2 - iStartChunk2 +
STRING_SIZE(LINE_TO_INSERT) + iEndChunk3 - iStartChunk3 + 1;
DWORD dwReserved = 0;
LPSTR lpNewBuffer = (LPSTR)pfc->AllocMem(pfc,iTmp, dwReserved);
if ( !lpNewBuffer )
{
bStatus = false;
goto ReturnBuffer;
}
#define MEMCAT(lpTarget,lpSource,SourceLen,TargetLen) \
memcpy(lpTarget,lpSource,SourceLen); TargetLen += SourceLen;
DWORD dwNewLen = 0;
MEMCAT( lpNewBuffer + dwNewLen, lpBuffer + iStartChunk1, iEndChunk1 - iStartChunk1, dwNewLen);
MEMCAT( lpNewBuffer + dwNewLen, szContentLen, strlen(szContentLen), dwNewLen);
MEMCAT( lpNewBuffer + dwNewLen, lpBuffer + iStartChunk2, iEndChunk2 - iStartChunk2, dwNewLen);
MEMCAT( lpNewBuffer + dwNewLen, LINE_TO_INSERT, strlen(LINE_TO_INSERT), dwNewLen);
MEMCAT( lpNewBuffer + dwNewLen, lpBuffer + iStartChunk3, iEndChunk3 - iStartChunk3, dwNewLen);
/*
* We don't have to do that, but making it a string is good for debugging
*/
lpNewBuffer[dwNewLen] = 0;
lpBuffer = lpNewBuffer;
dwLen = dwNewLen;
}
ReturnBuffer:
*ppBuffer = (LPBYTE)lpBuffer;
*lpdwLen = dwLen;
return bStatus;
}
/*
*
* Extract the input Content-Length value.
*
* Note: This function receive a pointer to a string begins with "Content-Length:".
* It searches for the first occurrence of ":" and tries to parse the number followed by
* the ":" character. If the parsing fails, the function return 0.
*
*/
static ULONG dwfnContentLen(LPSTR ContentLenLine, int iLineLen)
{
.....
}
/*
*
* Utility function to notify that a filter is not to be called
* to a notification throughout the lifetime of the current Request.
*
*/
static void DisableNotifications ( PHTTP_FILTER_CONTEXT pfc, DWORD flags )
{
.....
}
/*
* strnstr()
* finds first appearance of strCharSet in string ignoring
* letters case.
*/
static int strnstr ( const char *string, const char *strCharSet , int n)
{
....
}
void OutputDebugStringF (const char* pchFormat, ...)
{
.....
}
/*
* IsFirstInCARP()
* Returns in IsFirst variable true if the current server is the first server and false otherwise.
*/
static DWORD IsFirstInCARP(PHTTP_FILTER_CONTEXT pfc, BOOL *pfIsFirst)
{
.....
} |
Partager