Algoritma serta Teknik Menangkal SQL Injection

Tidak dipungkiri lagi, SQL injection adalah salah satu teknik hacking yg sangat berbahaya. Jika diamati, SQL injection berawal dari satu hal yg sebenarnya sangat sederhana sekali. Kali ini kita akan membahas teknik dan algoritma menangkal SQL injection yg dilakukan melalui methode $_GET.

Tulisan ini diawali kepada kesadaran bahwa atas banyaknya kasus SQL injection. Disamping sedikitnya kesadaran dari programmer dan kebingungan programmer untuk membenahi skripnya. Dikarenakan sedikitnya resource yg membahas bagaimana mencegah SQL injection. Sementara tips dan trik untuk melakukannya sangat banyak.

Mari kita lihat bersama: http://target.com/berita.php?task=detail&id=9

Pada kebanyakan aplikasi yg dibuat oleh programmer pemula, kode program pada berita.php adalah sebagai berikut

<?php

require_once(”connection.php”);

$sql = “SELECT * FROM berita WHERE id=”.$_GET['id'];

$result = mysql_query($sql);

while($data = mysql_fetch_array()){

echo “<h2>”.$data['judul'].”</h2>”;

echo “Posting pada tanggal: “.$data['tanggal_posting'];

echo $data['isi'];

}

?>

Coba kita lihat lagi, betapa programmer, entah karena ketidak tahuan atau malas membenahi(nah ini yg susah) tidak memberikan filter pada input $_GET['id']. Padahal ini sangat berbahaya jika attacker mencoba melakukan teknik ini.

Disana ada 2 kesalahan fatal.

1. Tidak memberikan filter atas variable $_GET['id']. Sehingga MySQL Server akan memberikan signal kesalahan ke browser(Jika pada PHP error_message=ON).

2. Tidak melakukan checking terhadap hasil dari Query.

Lantas, bagaimana cara penanggulangannya. Cara mudahnya kita modifikasi skrip diatas seperti berikut ini:

<?php

require_once(”connection.php”);

// –> validasi $_GET['id']

if(!ctype_digit($_GET['id'])){

die(”<script>alert(’Jangan coba-coba ya.’);window.history.go(-1);</script>”);

}

$sql = “SELECT * FROM berita WHERE id=”.$_GET['id'];

$result = mysql_query($sql);

// –> validasi hasil query

if(mysql_num_rows($result)<0){

while($data = mysql_fetch_array()){

echo “<h2>”.$data['judul'].”</h2>”;

echo “Posting pada tanggal: “.$data['tanggal_posting'];

echo $data['isi'];

}

} else { echo “Berita tidak ditemukan.”; }

?>

Nah, skrip diatas kira-kira mempunyai arti algoritma seperti ini:

1. Pertama-tama, validasi $_GET['id'], jika ia bernilai Integer(angka), maka lanjutkan. Jika tidak, tampilkan peringatan “Jangan coba-coba ya.” dan kembali kehalaman selanjutnya.

2. Lakukan Query

3. Cek hasil query. Jika hasil lebih besar dari 0(1,2,dst), ambil hasil query dan tampilkan ke layar browser. Jika tidak tampilkan pesan “Berita tidak ditemukan.”.

Nah, dengan begitu aplikasi kita sudah lumayan aman.

Algoritma ini bisa dipakai tidak hanya pada PHP saja, tetapi bisa untuk bahasa pemrograman web yg lain. Tentu saja dengan syntax masing-masing. Untuk database selain MySQL tinggal mencari fungsi yg sama atau setara dengan fungsi-fungsi MySQL.

Sharing Article Ini Bookmark and Share

Top Articles


Leave a Reply

Early years as a fierce competition for the company's graphics card developers. The reason is, between the months of January or February, AMD plans to spawn cards Graphics Processing Unit (GPU) is the new, while Nvidia has prepared Fermi, GPU that supports DirectX-11 in March 2010. Rumors, Nvidia will actually poured Fermi in November 2009. However, the plan berubaha because they want to wait for technology exhibition 2010 Consumer Electronics Show held in January completed. But, one of the hardware developers fulfill his promise to make the shipping Fermi in the first quarter of next year. As reported by Digitimes, Thursday (31/12/2009), Nvidia wants to make the beginning of 2010 as their basis for poured some of its newest product. The plan, Nvidia will bring GDDR5 memory Fermi 40nm GPU-based GF-100 in March, the next version of the GF104 in the second quarter. GF104 with GeForce GTX295/285/275/260 be targeting upper middle segment. While AMD will face attacks by presenting Nvidia Radeon HD 5670/5570 (Redwood) and HD 5450 (Cedar) in late January or early February 2010. Of course, both companies are waiting for the opening of the year by fans, because up to now not known in detail what kind of graphics card capability made. An increase in malware attacks through P2P networks. In 2009, many who saw the epidemic of malware malicious files supported. This attack method has been used several viruses, such as TDSS and Virut.Cyber criminals as now beginning to make efforts to legalize themselves. Also they started making money by creating a huge traffic using a botnet. Fake antivirus programs will increasingly mengganas in the year 2010. However, many antivirus sure, with the market monitoring by IT security firm and government legal agencies, antiviral distribution will be increasingly difficult. Wave attack on Google. The attack is believed will continue to be a headline in 2010. The attack on Google's service will begin with a pattern of sending spam, and phishing attacks, take advantage of the gaps on the website and spread of malware. Increased attacks on the iPhone and Android phones as a platform. The attack on the platform that emerged in the year 2009, indicating interest in the cyber criminals. Many criminals take advantage of special days to spread malware. Valentine's Day, the issue of H1N1, the crash of Air France Flight 447, Serena Williams, to the death of Michael Jackson. The makers of malware and spammers trying to lure Internet users unsuspecting to download malware, buy products and trapped in the fraud. Data theft continues. According to the Identity Theft Resource Center, until October 13, 2009, 403 cases of data breach have been reported throughout the year and exposing more than 220 million documents. According to the Ponemon Institute, in which people mean no harm continue to represent the bulk of the data loss incidents by 88 per cent of all incidents of data loss caused by such person in the employee and partner.