Yet Another Way to Defeat URL Filters

by ThermoFish (JW)

In 17:3, the article entitled "Another Way to Defeat URL Filters" by ASM_dood put it up to readers to come up with a script to turn IP addresses into their decimal equivalent.  At the end of the article a script by CSS was put in which did just that.  While that script works great, most people know the hostname (URL) of the site they want to go to.  Who wants to have to go get the IP address of the hostname they want to go to?  Instead of the two-step process of getting the IP address of the hostname and then turning that IP into a decimal, I would rather just type in a hostname and get its decimal equivalent in one step.  Therefore, I wrote some code to accomplish that.

This code was written in VC++ and you need to include the "WSOCK32.LIB" library in the workspace for it to link properly.  I left the IP-to-Decimal function separate to show how that is done more clearly.  The retrieval of the IP from the hostname is done with the 'hostent' structure and 'gethostbyname()'.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream.h>
#include <winsock.h>
#include <conio.h>

int IPtoDec(char *ip);

int main()
{
  using namespace std;
  WSAData wData;

  if (WSAStartup(MAKEWORD(2, 2), &wData) == SOCKET_ERROR) {
    cout << "Winsock init error\n";
    cout << "\n\nPress any key to exit.\n";
  getch((); return 1;}

        hostent * h = NULL;
        char hostname[80];
        cout << "\n\n"
        << "########################################\n"
        << "# Host Name to Decimal Equivalent v1.0 #\n"
        << "#         by: ThermoFish (JW)          #\n"
        << "########################################\n";
        cout << "Enter hostname: ";
        cin >> hostname; h = gethostbyname(hostname); if (h == NULL) {
        cout << "Could not resolve " << hostname << endl;
        cout << "\n\nPress any key to exit.\n"; getch(); return 1;}

        char *ip = inet_ntoa(*(reinterpret_cast < in_addr * >(h->h_addr)));
        cout << "\nIP address :" << ip << endl;
        IPtoDec(ip); cout << "\nPress any key to exit.\n"; getch(); return 0;}

// Function to convert from IP to decimal

        int IPtoDec(char *ip) {

        {
        using namespace std;
        char *cptr = strtok(ip, ".");
        int shift = 24; unsigned long acc = 0L; while (cptr != NULL) {
        acc += atol(cptr) << shift; shift -= 8; cptr = strtok(NULL, ".");}

        cout << "\nIP as Decimal: " << acc << endl;}
        return 0;}

Code: host2dec.cpp

	An Easier Way

	An IP address can be considered a polynomial in 256.

        Given an IP address A.B.C.D, the resulting number is:

             ((A * (256^3)) + (B * (256^2)) + (C * 256) + D 

	Example 

        207.99.30.230 would be:

        (207 * 16,777,216) + (99 * 65,536) + (30 * 256) + 230 = 3479379686
Return to $2600 Index