Search: Home Bugtraq Vulnerabilities Mailing Lists Jobs Tools Vista
      Digg this story   Add to del.icio.us   (page 2 of 4 ) previous  next 
Blocking Traffic by Country on Production Networks
Timothy M. Mullen 2008-07-16

Article continued from Page 1

Converting dotted notation to a long integer is easy enough as well. You just break the dot notation octets into their base decimal values multiplied by the decimal value of each octet itself, and add each octet's base * value product together. For example, let's take the next to the last entry from above - 203.83.16.0 through 203.83.23.255. The base for each octet is 256 to the power of the octet column's position, or 2563.2562.2561.2560 or further extrapolated to 16777216.65536.256.0 respectively. So in this case, for the beginning IP, you'll take (16,777,216 * 203) + (65,536 * 83) + (256 * 16) + (0 ) or 3,405,774,848 + 5,439,488 + 4,096 + 0, which equals 3,411,218,432 as indicated above in the BegIPLong field. Using the same process, we see that the EndIPLong field would be 3405774848 + 5439488 + 5888 + 255, or 3400220479 as again indicated above. (Note that for the last octet of 2560 you just use the decimal value of the IP address, not 0 * the value which would always be 0.)

Converting the beginning and ending IP address to a long integer like this within the IPAddresses table allows us to easily retrieve the Country value from the associated IP range record where the long integer IP address in the Firewall log is between the beginning and ending long integer IP referenced in the table. The ISA Firewall Log table structure is well documented, so I won't eat web space here detailing it; however, I will reference some important, basic fields therein: ID (bigint), logtime (datetime), protocol (varchar 32), SourceIP (bigint), SourcePort (int), DestinationIP (bigint), DestinationPort (int) works for now. Let's call this the FirewallLog table.

Consider this single record matching the above columns that might be found in the FirewallLog:

"1044432","2007-11-08 12:26:35.157","TCP","3684096418","45887","3515032734","443"
If we wanted to find out what country this connection came from via the IPAddresses table and the FirewallLog table, we would execute a SQL statement similar to this:
select FirewallLog.*, IPAddresses.FullCntry from FirewallLog,IPAddresses where FirewallLog.SourceIP between IPAddresses.BegIPLong and IPAddresses.EndIPLong and FirewallLog.ID = 1044432
Yielding the results:
"1044432","2007-11-08 12:26:35.157","TCP","3684096418","45887","3515032734","443","China"
So, here we see how we can link the IPAddresses tabular data to our ISA (or IIS, etc for that matter) log data to report on traffic by source (or destination) country.

Functions and Procedures


Before we move on, here's a helpful function that will come in handy. As already stated, the ISA SourceIP and DestinationIP fields are stored as long (big) integers. This is great for a WHERE or JOIN statement, but not so great when it comes to reporting and verification. In the same way that we convert dotted IP notation to a long integer, we need to be able to convert the long integer back to a dotted notation. When logging ISA to SQL, you can't just change the table structure to whatever you want as the insert job will fail -- ISA expects the table format to be what it is "supposed to be." So you just can't add a SourceIPDot field to the FirewallLog table and update it when you want to. Therefore, I decided to create a scalar function in SQL to do this for me at query runtime.

The following SQL will create a function for you called dbo.ConvertLongIP:

set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go -- ============================================= -- Author: Timothy "Thor" Mullen -- Create date: 03/01/08 -- Description: Scalar function to converts long/bigint formatted IP addresses to dot notation -- ============================================= ALTER FUNCTION [dbo].[ConvertLongIP] ( -- Input parameter for long (bigint)IP here @LongIP bigint ) RETURNS varchar(15) AS BEGIN DECLARE @DotIP varchar(15), @bin varbinary(4) select @bin = cast(@LongIP as varbinary(4)) select @DotIP = cast(convert(int,substring(@bin,1,1)) as varchar(3)) + '.' + cast(convert(int,substring(@bin,2,1)) as varchar(3)) + '.' + cast(convert(int,substring(@bin,3,1)) as varchar(3)) + '.' + cast(convert(int,substring(@bin,4,1)) as varchar(3)) RETURN @DotIP END

Once the function is created, one can now easily determine the source IP address by including the following function call in the original SQL statement:

select FirewallLog.*, dbo.ConvertLongIP(SourceIP),IPAddresses.FullCntry from FirewallLog,IPAddresses where FirewallLog.SourceIP between IPAddresses.BegIPLong and IPAddresses.EndIPLong and FirewallLog.ID = 1044432
Which now yields : "1044432","2007-11-08 12:26:35.157","TCP","3684096418","45887","3515032734","443",
" 219.150.217.162","China" Notice the inclusion of the 219.150.217.162 address. Now we have the full reporting capabilities that we want in order to begin our due diligence in research before choosing to block (or allow) traffic to or from any particular country.

Reporting Examples


Here is some sample data from a compilation of several network capture point set up at different parts of the world over a few days - let's start with SMTP:

Country Prot Port Connections
United States TCP 25 290964
Russian Federation TCP 25 171858
China TCP 25 133314
Turkey TCP 25 105648
Poland TCP 25 97635
Brazil TCP 25 96870
Korea, Republic of TCP 25 87570
Spain TCP 25 82584
Germany TCP 25 72645
Mexico TCP 25 66672
United Kingdom TCP 25 53868

For my environments, I have verified that there is no valid traffic coming out of Russia, China, Turkey, Poland, and Korea. And very, very little valid SMTP traffic comes from Brazil and Mexico to my production networks. While a large portion of the US-based SMTP traffic is indeed spam, I at least now have the opportunity to analyze traffic and make decisions based upon my research.

Now let's look at HTTP:

Country Prot Port Connections
Brazil TCP 80 597351
United Kingdom TCP 80 97704
United States TCP 80 69210
Japan TCP 80 29331
Russian Federation TCP 80 10158
Germany TCP 80 7236
Turkey TCP 80 6972
China TCP 80 6945
India TCP 80 4332
Italy TCP 80 3861
France TCP 80 3744
Article continued on Page 3 



SecurityFocus accepts Infocus article submissions from members of the security community. Articles are published based on outstanding merit and level of technical detail. Full submission guidelines can be found at http://www.securityfocus.com/static/submissions.html.
    Digg this story   Add to del.icio.us   (page 2 of 4 ) previous  next 
Comments Mode:







 

Privacy Statement
Copyright 2008, SecurityFocus