Adam Caudill

Security Leader, Researcher, Developer, Writer, & Photographer

PageSource

This post was imported from an old blog archive, and predates the creation of AdamCaudill.com.

Here is a quick little function to retrieve the source code of a web page, it’s pure API, so no Internet Transfer control required.

API: #

Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" ( _
    ByVal sAgent As String, _
    ByVal lAccessType As Long, _
    ByVal sProxyName As String, _
    ByVal sProxyBypass As String, _
    ByVal lFlags As Long) As Long
Private Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" ( _
    ByVal hInternetSession As Long, _
    ByVal sURL As String, _
    ByVal sHeaders As String, _
    ByVal lHeadersLength As Long, _
    ByVal lFlags As Long, _
    ByVal lContext As Long) As Long
Private Declare Function InternetReadFile Lib "wininet.dll" ( _
    ByVal hFile As Long, _
    ByVal sBuffer As String, _
    ByVal lNumBytesToRead As Long, _
    lNumberOfBytesRead As Long) As Integer
Private Declare Function InternetCloseHandle Lib "wininet.dll" ( _
    ByVal hInet As Long) As Integer

Constants: #

Private Const IF_NO_CACHE_WRITE = &H4000000
Private Const BUFFER_LEN = 256

Function: #

Public Function PageSource(ByVal sURL As String, Optional ByVal strHeaders As String = "") As String
Dim sBuffer As String * BUFFER_LEN, iResult As Integer, sData As String
Dim hInternet As Long, hSession As Long, lReturn As Long
Dim lngHeaderLen As Long

    lngHeaderLen = Len(strHeaders)
    'get the handle of the current internet connection
    hSession = InternetOpen("User-Agent: Your-User-Agant-Here", 1, vbNullString, vbNullString, 0)
    'get the handle of the url
    If hSession Then hInternet = InternetOpenUrl(hSession, sURL, strHeaders, lngHeaderLen, IF_NO_CACHE_WRITE, 0)
    'if we have the handle, then start reading the web page
    If hInternet Then
        'get the first chunk & buffer it.
        iResult = InternetReadFile(hInternet, sBuffer, BUFFER_LEN, lReturn)
        sData = sBuffer
        'if there's more data then keep reading it into the buffer
        Do While lReturn <> 0
            iResult = InternetReadFile(hInternet, sBuffer, BUFFER_LEN, lReturn)
            sData = sData + Mid$(sBuffer, 1, lReturn)
            DoEvents
        Loop
    End If
    'close the URL
    iResult = InternetCloseHandle(hInternet)
    PageSource = sData
End Function

Simply pass the URL you want plus any extra headers that should be sent (one header per-line, each terminated by a vbCrLf) and it returns the full source of the page (without headers).

Adam Caudill


Related Posts

  • Conexant (formerly Rockwell) Softmodem HSF Modem

    This post was imported from an old blog archive, and predates the creation of AdamCaudill.com. I was actually rather lucky to have this brand of WinModem, as the good people over at Linuxant.com offer a very high quality driver that makes installation a breeze! But, they have recently changed their marketing methods and charge $15 for the driver, so these really lives no viable, free alternative (a rarity to say the least for linux).

  • APISettings

    This post was imported from an old blog archive, and predates the creation of AdamCaudill.com. Visual Basic provides a less than elegant method of saving data in the Windows registry, the GetSetting & SaveSetting functions. These functions store setting in HKEY_CURRENT_USER\Software\VB and VBA Program Settings\<AppName>\<Section> not very pretty is it? The APISettings module is a drop-in replacement using pure Win32 API for its processing power and increased stability. The reason for developing this and for making it drop-in compatible is to all those new to the Win32 API to add its functionality with minimal difficulty.

  • CloseApp

    This post was imported from an old blog archive, and predates the creation of AdamCaudill.com. This is a useful function to close a program based on a windows caption, this should work for any top-level window. Paste all this into a standard module, save it. Then call CloseApp("Notepad") or whatever the name of the window is, it’s nice & simple and should close the program instantly. This requires Windows 2000 plus, for older versions of Windows a different method is required, that isn’t covered here, seeing as Windows 2000 and better require special privileges to forcefully close a program.

  • Crypto, the NSA, and Broken Trust

    Even as a child I was fascinated by cryptography – and often left the local librarians with puzzled looks thanks to the books I would check out. It’s so elegantly simple, and yet massively complex. There is one very unusual property of crypto though – it’s not about math or modes, it’s about trust. Cryptography, especially as used today, has the most wonderful dichotomy of trust; on one hand crypto, by its very nature, is used in situations lacking trust.

  • Hash Storage: Make Attackers Work

    So you hash your passwords? Good. Do you salt? That’s good. Do you use a strong hashing algorithm (PBKDF2/bcrypt/scrypt)? Great! But how do you store the hashes? What happens when you get hit with a SQL injection attack? I’m a big believer in defense in-depth – not that marketing garbage about stacking layers of blinky-light boxes, but using techniques to add extra work for an attacker. You might not be able to stop every attack, but the more work they have to do, the better the odds they won’t get everything they want.