@daharveyjr

Architect, Developer, Web Geek!

February 22, 2016
by daharveyjr
3 Comments

cat_linux

Blocking Abusive IP Addresses with Iptables on Linux

I’ve recently had some questions come up from some of my clients who want to know how they can block specific IP addresses that are abusing their web sites and server. In those instances where you want to block a particular IP from your server and are running a distrubtion of Linux, you can block the traffic from that IP address by using iptables. This tutorial will teach you how to set up, view, and configure a rule to block a specific IP address.

What Is Iptables?

Iptables is a firewall, installed by default on most Linux distributions. By default iptables allows all traffic to pass through to your server, but you can configure it to restrict or drop traffic through a variety of rules that can be setup to limit who and what can actually connect to your server.

To access and use iptables, you’ll need root access, either by using sudo or having direct access to the root user of the server you’re working with. The iptables commands shown below assume you have direct access to the root user on the server, if not you’ll need to prefix them with the sudo command.

How to Set up a Rule to Block a Specific IP Address

The specific case being discussed in this article to the desire to simply stop any all all traffic originating from a specific IP address by simply dropping the inbound connection and not allowing it to proceed further.

The syntax to add a simple rule to iptables to block a specific IP address and drop its connection to the server is:

iptables -A INPUT -s {IP_ADDRESS} -j DROP

Looking at this command, there are a few flags that I think are important for you to know.

The -A flag followed by the INPUT chain indicates that the rule to is to be appended to the INPUT rule chain. We will only deal with the INPUT chain in this tutorial, as it is designed to affect only incoming traffic.

The -s flag followed by the IP address specifies the source of the traffic to be acted upon. It will apply any rules you define to any traffic coming from this source.

The -j flag followed by the DROP rule will cause the firewall to silently ignore the packet, and stop processing further rules in the specified chain.

So if we look at the command above, we’ve indicated that we want to append a new rule to the INPUT chain, and all traffic originating from the specified IP address will be silently ignored and dropped without further processing.

How Do I Unblock an IP Address?

In the event that you make a mistake and entered the wrong IP address, how do you remove that IP address so that traffic may flow freely from that IP address once again?

iptables -D INPUT -s {IP_ADDRESS} -j DROP

The new flag shown in this command is -D followed by the INPUT chain to indicate that we want the rule defined to be removed from the iptables entries.

How Do I View Currently Setup Iptables Rules?

To view currently setup rules, and confirm the rules you have entered you can execute the iptables command with the -L flag. This will list the current filter rules.

iptables -L

The output from this command will be similar to the following output shown below. You’ll see all possible chains, and your new rules will appear under the INPUT chain.

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
DROP       all  --  {IP_ADDRESS}         anywhere            

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination  

Your New Iptables Rules Setup and Configured

The rules you’ve setup will take effect immediately, and in the case of the rules we’ve defined above you’ll start seeing that traffic from the IP address specified in the rule will instantly stop and will not be allowed to interact with your server.

December 3, 2015
by daharveyjr
0 comments

cat_db2

DB2 REPLACE Function

The DB2 REPLACE function replaces all occurrences of a search string within a source string. If the search string is not found within the source string, the original string is returned unchanged.

The syntax for the REPLACE function is shown here:

SELECT REPLACE({source_string}, {search_string}, {replace_string});

-- After DB2 v9.7 Fix Pack 5 {replace_string} is optional
SELECT REPLACE({source_string}, {search_string});

To show an example statement in use:

-- Leveraging a table column
SELECT REPLACE(LONGDESCRIPTION, 'A', 'B') FROM CATENTDESC;

The above query will take the source string, defined as a field, or explicit string, in our case the LONGDESCRIPTION value from CATENTDESC and search it for the search string ‘A’. All instances of ‘A’ will then be replaced ‘B’.

NOTE: There are few things to consider regarding the REPLACE function:

  1. If the source, search, or replace string is not a CHAR, VARCHAR, GRAPHIC, or VARGRAPHIC data type it will be it will be cast to a VARCHAR data type before evaluation.
  2. Staring in DB2 Version 9.7 Fix Pack 5 and later, the replace string is optional as noted in the above examples. When the expression is an empty string or the replace string has not been specified, nothing will replace the string that is removed from the source string.

July 21, 2015
by daharveyjr
0 comments

Portfolio Project: Fit Uniforms

fituniforms_1280

About the Project

Fit Uniforms was running an e-commerce website based on WordPress and Jigoshop that was in need of some help. They came to me wanting to optimize and redesign their website. They wanted a website that was professional, easy to navigate, and mobile friendly. A website that allowed mobile devices to easily complete a purchase. In addition, they also wanted to move to a new hosting provider, one that provided better features and more stability.

Keeping them based on their existing WordPress and Jigoshop platform, a new professional looking and mobile responsive website theme was built and customized for them, allowing their customers to interact and shop with them regardless of the device they were using. Mobile checkout was a high priority during the redesign, and a lot of testing was done throughout the project to ensure the flow worked, on both a desktop and mobile device. Their web site content was enhanced, and additional content, galleries, and product portfolios were added allowing them to showcase their uniform work to prospective customers. In addition, there were moved to a new hosting provider and setup with a plan accurately sized to their traffic and business needs.

View Portfolio Page | Visit Fit Uniforms

February 17, 2015
by daharveyjr
0 comments

Portfolio Project: MRP Design Engineering

MRP Design Engineering

About the Project

I recently completed work on a new project for MRP Design Engineering. We took their existing website, built using the SnippetMaster web editor, and migrated it to the WordPress platform with a new look and feel. Doing this allows them to easily control and manage their own content, which is an option they really didn’t have before. In addition, the website features an updated and improved user interface, enriched content across across the services offered to clients, a new navigation layout that is well-defined and structured, and resolves a few outstanding bugs that limited their client interactions on the old website.

View Portfolio Page | Visit MRP Design Engineering

December 22, 2014
by daharveyjr
0 comments

cat_mysql

MySQL REGEXP (Regular Expression) Operators

MySQL offers the ability to use regular expressions to perform complex searches against your data. A regular expression is a tool that provides for a concise and flexible way to identify strings of text based on user-defined patterns.

This article will discuss the MySQL regular expression operators, review their use and syntax, and identify the constructs and special characters that can be used in a MySQL regular expression, as well as provide a few examples of their use.

MySQL Regular Expression Operators

The following operators are used in MySQL to perform regular expression operations. These are used in a WHERE clause similar to the well-known and often used LIKE operator.

  • REGEXP: The pattern matching operator for using regular expressions.
  • NOT REGEXP: The negation of the REGEXP operator.
  • RLIKE: A synonym for the REGEXP operator.

MySQL Regular Expression Syntax

The basic syntax used for MySQL regular expression operations is:

-- For the REGEXP Operator
SELECT {COLUMN_NAME} FROM {TABLE_NAME} WHERE {COLUMN_NAME} REGEXP '{REGEXP_PATTERN}';

-- For the NOT REGEXP Operator
SELECT {COLUMN_NAME} FROM {TABLE_NAME} WHERE {COLUMN_NAME} NOT REGEXP '{REGEXP_PATTERN}';

-- For the RLIKE Alias Operator
SELECT {COLUMN_NAME} FROM {TABLE_NAME} WHERE {COLUMN_NAME} RLIKE '{REGEXP_PATTERN}';

To provide more detailed, yet simple, example of a MySQL regular expression operation, take the following statement. It will retrieve all the columns of each record in the table PRICE where the PRICELIST_ID matches the pattern specified (starts with the numeric range 0-9 occurring one or more times, followed by an ‘_’ (underscore), and then the character sequence ‘USD’.

SELECT * FROM PRICE WHERE PRICELIST_ID REGEXP '^[0-9]+_USD';

Another example of a MySQL regular expression operation, can be shown in the following statement. It will retrieve all columns of each record from the PRICE table where the PRICE_ID matches the pattern specified (starts with an O, followed by and ‘_’ (underscore), then the numeric range 0-9 occurring one or more times, followed by and ‘_’ (underscore), then ending with either the character sequence USD, or BRA.

SELECT * FROM PRICE WHERE PRICE_ID REGEXP '^O_[0-9]+_[USD|BRA]';

MySQL REGEXP Constructs and Special Characters

A MySQL regular expression may use any of the following constructs and special characters to construct a pattern for use with the REGEXP operators. The construct or special character is shown, followed by a description of each and what operations in performs within the pattern for the regular expression.

  • ^ : Match the beginning of a string.
  • $ : Match the end of a string.
  • . : Match any character (including carriage return and newline characters).
  • a* : Match any sequence of zero or more a characters.
  • a+ : Match any sequence of one or more a characters.
  • a? : Match either zero or one a characters.
  • de|abc : Match either of the character sequences, de or abc.
  • (abc)* : Match zero or more instances of the character sequence abc.
  • {1},{2,3} : Provides a more general way of writing regular expressions that match many occurences of the previous atom (or “piece”) of the pattern. i.e. a? can be written as a{0,1}.
  • [a-dX],[^a-dX] : Matches any character that is (or is not, if ^ is used) either a, b, c, d, or X. A “-” character between two other characters forms a range that maches all characters from the first character to the second.
  • [.characters.] : Within a bracket expression (using “[” and “]”), matches the sequence of characters of that collating element. i.e. the pattern [[.period.]] would match the ‘.’ (period) character.
  • [=character_class=] : Within a bracket expression, represents an equivalence class. It matches all characters with the same collation value, including itself.
  • [:character_class:] : Within a bracket expression, represents a character class that matches all characters belonging to that class. i.e. the pattern [[:alpha:]] would match against a string that is all aphabetic characters.
  • [[:<:]],[[:>:]] : These markers stand for word boundaries, and as such they match the beginning and ending of words, respectively.

* NOTE: MySQL interprets the “\” (backslash) character as an escape character. If you choose to use the “\” character as part of your pattern in a regular expression it will need to escaped with another backslash “\\”.

For further documentation on the MySQL regular expression operator, please visit Regular Expressions in the MySQL Reference Manual (v5.1 currently linked).