Archive for the 'Programming' Category

How to install PHP-FPDF in Ubuntu

March 28, 2010

What is FPDF?

In this article, Apache2 and PHP5 assumed has been installed and running well.

The following lines are the installation steps.

  1. Please login as “root” or equivalent for installing.

    ~# apt-get install php-fpdf

  2. If the installation is successful, there will be a file “fpdf.php” in the following folder.

    ~# ls -l /usr/share/php/fpdf

  3. Please create a symbolic link is directed at each site are available on your web server with the following way.

    e.g.

    ~# ln -s /usr/share/php/fpdf /var/www/fpdf

  4. Reload your web server.

    ~# /etc/init.d/apache2 force-reload

  5. Make sure the PHP commands on the web page associated with the conversion to PDF function like this.
    <?php
    require('fpdf/fpdf.php');
    ?>
    
  6. Finish.
Advertisements

How to check a Table is empty or not in MySql

December 7, 2009

Here, I summarize some of the fastest way to check a Table in MySQL is empty or not.

  1. By querying on Database [information_schema]
    This is the fastest and the most I recommended to be applied, because in the database [information_schema] stored name, number of recordings and others are always up-to-date each time the operation occurred on both the MySQL Database system, Table, Trigger, etc.

    » Syntax
    set @rows = (select TABLE_ROWS
    from information_schema.TABLES
    where TABLE_SCHEMA = ‘database_name’
    and TABLE_NAME = ‘table_name’
    and TABLE_TYPE = ‘BASE TABLE’);

    select @rows;

    » Parameters
    ‘database_name’, please change in the quote with the name of the database from the Table which will be checked!
    ‘table_name’, please change in the quote with the name of Table which will be checked!
    ‘BASE TABLE’, as standard type of MySQL Database and Table.

    » Returns
    if @rows = 0 then table is empty.
    if @rows = null then database or table not found.
    if @rows > 0 then total existing row aka table is not empty.

  2. By counting records of a Table
    By counting the amount of data from a Table is about to be checked and for me, this is silly but effective way, but not recommended if adjusted to the title of this article.

    » Syntax
    set @rows = (select count(*) from ‘database_name’.’table_name’);

    select @rows;

    » Parameters
    ‘database_name’, please change in the quote with the name of the database from the Table which will be checked!
    ‘table_name’, please change in the quote with the name of Table which will be checked!

    » Returns
    if @rows = 0 then table is empty.
    if @rows > 0 then total existing row aka table is not empty.

SSH brute-force combat script

November 28, 2009
#!/bin/sh
# rc.turkey
# http://twitter.com/kokikode

# File "/etc/hosts.rogues" will be applied to "/etc/hosts.allow" as an exception, like this sample.
# ALL: 127.0.0.1/32
# ALL: 192.168.0.0/24
# sshd: ALL EXCEPT /etc/hosts.rogues

# Then don't forget to configure the file "/etc/hosts.deny" like this.
# ALL: ALL

hosts_rogues() {
grep $rawline /etc/hosts.rogues > /dev/null 2>&1
if [ "$?" -ne "0" ]; then
   echo "$rawline/32" >> /etc/hosts.rogues
fi
}

# Additional commands if needed.
# /sbin/iptables -A FORWARD -j DROP -s $rawline
# /sbin/iptables -A FORWARD -j DROP -d $rawline
# /sbin/iptables -A OUTPUT -j DROP -d $rawline

resist_attack() {
/sbin/iptables -L -v -n | grep $rawline > /dev/null 2>&1
if [ "$?" -ne "0" ]; then
   /sbin/iptables -A INPUT -j DROP -s $rawline
   /sbin/iptables -A INPUT -j DROP -d $rawline
fi
}

route_to_blackhole() {
/bin/ip route list type blackhole | grep $rawline > /dev/null 2>&1 
if [ "$?" -ne "0" ]; then
   /bin/ip route add blackhole $rawline
fi
}

echo `date` &gt; /var/log/lastlog.turkey

# Please add the rules in IPTables-based firewall as one of the requirements to run this script, such as the following example.
# iptables -A INPUT -j ACCEPT -p tcp --dport 22 --syn -m state --state NEW -m limit --limit 1/m --limit-burst 1
# iptables -A INPUT -j LOG -p tcp --dport 22 --syn -m --state NEW --log-level debug
# iptables -A INPUT -j DROP -p tcp --dport 22 --syn -m state --state NEW

cat /var/log/debug | grep "IN=eth1" | grep "DPT=22" | cut -f5 -d= | sed 's/DST//g' | sort -u | while read rawline; do
   if [ -n "$rawline" ]; then
      cat /var/log/auth.log | grep "sshd" | grep "Invalid user" | grep $rawline > /tmp/$rawline
      if [ -s /tmp/$rawline ]; then
         hosts_rogues
         resist_attack
         route_to_blackhole
      fi
      rm /tmp/$rawline
      cat /var/log/auth.log | grep "sshd" | grep "Failed password" | grep $rawline > /tmp/$rawline
      if [ -s /tmp/$rawline ]; then
         hitcount=$(grep -c $rawline /tmp/$rawline)
         if [ $hitcount -gt 2 ]; then
            hosts_rogues
            resist_attack
            route_to_blackhole
         fi
      fi
      rm /tmp/$rawline
      cat /var/log/auth.log | grep "sshd" | grep "POSSIBLE BREAK-IN ATTEMPT" | grep $rawline > /tmp/$rawline
      if [ -s /tmp/$rawline ]; then
         hosts_rogues
         resist_attack
         route_to_blackhole
      fi
      rm /tmp/$rawline
   fi
done

exit 0

I put this script in crontab and run every 5 minutes.