asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search








Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

JavaScript Protection With An ISAPI Filter
By Itay Weinberger
Rating: 3.5 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

    Introduction

    An Internet Server Application Program Interface (ISAPI) filter is a set of Windows program calls that allow you to write a Web server application that is faster than a CGI. An ISAPI filter hooks into the IIS system and monitors certain events that occur while the client tries to read a page from our server.

    The filter application sits between the network connection to the client and the HTTP server, allowing us to control the data exchange between the IIS and the client. Using it, we can enhance the server functionality and create custom features, such as advanced HTTP logging or our own encryption. The ISAPI filters are based on notifications that the IIS sends to our ISAPI filter. These notifications are actually stages that each request has to pass.

    Each notification handles a different type of data. The data is relevant to each stage of the request process.

    When the ISAPI filter gets a notification from the IIS, we then can manipulate the notification's data. After we have manipulated the data, we can choose whether we want the IIS to continue to process the request.

    Notifications

    The notifications available from the IIS server are:

    OnPreprocHeaders -- Notifies the filter that the server has preprocessed the client headers.
    OnAuthentication -- Authenticates the client.
    OnUrlMap -- Notifies a filter when a server is mapping a logical URL to a physical path.
    OnSendRawData -- Notifies the filter before raw data is sent from the server to the client.
    OnReadRawData -- Notifies the filter after raw data is sent from the client to the server, but before the server processes it.
    OnLog -- Logs information to a server file.
    OnEndOfNetSession -- Notifies the filter that the session is ending.

    For our example, we'll monitor the OnUrlMap notification. Since we want to capture every attempt to read JavaScript files, this is the most suitable notification. When the client attempts to access a JavaScript file (a file with the .js extension) the server will try to map the logical path to the physical path, and that's when the server will notify us. After the server notification, we'll check if the file is a JavaScript file. If so, we must see that it's not our server attempting to read the JavaScript file, but a client's. If it is a client request, then we must block it and display an "access denied" message.

    The Code

    The work procedure is very simple and so is the following code:

    
    1) DWORD CJSISAPIFilter::OnUrlMap(CHttpFilterContext* pCtxt,	PHTTP_FILTER_URL_MAP pMapInfo)
    2) {
    3) const char * szURL = strlwr((char *)pMapInfo->pszURL);
    4) const char * szExtension = &szURL[lenURL - 3];
    5) char szReferer[250];
    6) DWORD lenURL = strlen(pMapInfo->pszURL);
    7) DWORD dwReferer = 250;
    8) if ( strcmp(szExtension, ".js") == 0 ) {
    9) pCtxt->GetServerVariable("HTTP_REFERER", szReferer, &dwReferer);
    10) if ( szReferer[0] != 'h' ) {
    11) char szRedirect[2];
    12) char szContent[300];
    13) DWORD dwRedirect = 2;
    14) DWORD dwContent;
    15) sprintf(szRedirect,"");
    16) sprintf(szContent, "\r\n\r\n<html>\r\n<head><title>Secure 
    17) File</title></head>\r\n<body>\r\n<b><font size=+2>This file contains privileged information.</font></b>
    <br><br><br><hr>JavaScript protection ISAPI filter.<br>Written by <a href=""mailto:mrkav@mrkav.com"">mr. kav<br></a>
    <br>\r\n</body>\r\n</html>\r\n");
    18) dwContent = strlen(szContent);
    19) pCtxt->ServerSupportFunction (SF_REQ_SEND_RESPONSE_HEADER,szRedirect,&dwRedirect,NULL);
    20) pCtxt->WriteClient (szContent, &dwContent);
    21) return SF_STATUS_REQ_FINISHED;
    22) }
    23) }
    24) return SF_STATUS_REQ_NEXT_NOTIFICATION;
    25) }
    
    

    The most important step in this code is the redirect procedure. This step redirects the client to a nonexistent page and then shows him the access denied message (lines 15 to 20).

    Test the Code

    How to test the code:

    1. Compile the code using the Visual C++ ISAPI Filter wizard.
    2. Install the ISAPI filter on your IIS.
    3. Create a JavaScript file named "jsisapi.js."
    4. Create an HTML file linked to that JavaScript file.
    5. Execute the HTML file. The file should work properly.
    6. Try to access the JavaScript file directly from your browser. You should get an access denied message.

    This code has been tested on IIS4/5 and compiled with Visual C++ 5/.NET.

    About the Author

    Itay Weinberger, a young programmer from Israel, is CEO of Joola Productions, Inc. He specializes in Web development consulting. See http://www.joola.co.il, or contact him at info@joola.co.il.

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Supporting Products/Tools
    AspEncrypt
    Built around the Microsoft CryptoAPI, AspEncrypt helps you harness all major encryption and hashing algorithms such as DES, Triple-DES, RC2, RC4, RSA, MD5 and SHA1 in just a few lines of code. The component can be used in tandem with AspEmail to send encrypted and signed mail in the industry-standard S/MIME format, or with AspUpload to encrypt files as they are being uploaded. AspEncrypt can also be used to issue and manage X.509 digital certificates.
    [Top]
    AspPDF
    AspPDF is an ASP/ASP.NET component which enables generation and management of documents in PDF format. Features include advanced text formatting, font embedding, form fill-in, images, tables, content and page extraction, document stitching, encryption, digital signatures, and more.
    [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry



    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Solutions
    Whitepapers and eBooks
    Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
    Microsoft Article: 7.0, Microsoft's Lucky Version?
    Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
    Avaya Article: How to Feed Data into the Avaya Event Processor
    Microsoft Article: Install What You Need with Windows Server 2008
    HP eBook: Putting the Green into IT
    Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
    Avaya Article: Setting Up a SIP A/S Development Environment
    IBM Article: How Cool Is Your Data Center?
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    HP eBook: Storage Networking , Part 1
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Video: Are Multi-core Processors Here to Stay?
    On-Demand Webcast: Five Virtualization Trends to Watch
    HP Video: Page Cost Calculator
    Intel Video: APIs for Parallel Programming
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Sun Download: Solaris 8 Migration Assistant
    Sybase Download: SQL Anywhere Developer Edition
    Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
    Red Gate Download: SQL Compare Pro 6
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
    eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
    IBM Article: Collaborating in the High-Performance Workplace
    HP Demo: StorageWorks EVA4400
    Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES