Showing posts with label Server. Show all posts
Showing posts with label Server. Show all posts

Monday, June 29, 2009

SQL Injection: Tutorial Part 1

SQL Injection
What? What is SQL ? SQL stands for Structured Query Language, It is the most widely used database querying language. Before reading on I must say that this is an old technique and I dint want this to be posted because most sites have patched up this hole. I am posting this for the few websites I came across that had this vulnerability. If you have a website or planning to build one or administrates one please read on and fix the security holes.

Warning: The following methods may be illegal, given for illustration purpose only.

Most dynamic websites use a database server such as SQL Server from Microsoft, MySQL, etc. To demonstrate what SQL injection is, let us see a piece of code that is used as a login script. Most ASP websites use a code like this:

<%
user = Request.Form("user")
pass = Request.Form("pass")
SQL = "SELECT * FROM users WHERE username='" & user & _
"' AND password='" & pass & "'"
' Execute the SQL ...
%>


This is an example of a poor code which is vulnerable to SQL injection. In this example we can see that the user input of user and pass is directly included in the SQL query.

A legitimate user enters his details, say
user=digitalpbk
pass=password

The SQL query would be constructed as based on the code as
SELECT * FROM users WHERE user='digitalpbk' 
AND pass='password'


Now in order to demonstrate or find if there is an SQL injection hole, all an user has to do is add a ' (single quote) to the username and/or password fields, say
user=digitalpbk
pass=password'

Now the SQL query would be constructed as
SELECT * FROM users WHERE user='digitalpbk'
AND pass='password''


This will throw up an invalid SQL and if the errors are not supressed you will get a screen as shown below:



So if we modify the user input field pass such that
user=digitalpbk
pass=password' OR '1'='1,

the constructed SQL query would be
SELECT * FROM users WHERE user='digitalpbk'
AND pass='password' OR '1'='1'


Which is a valid query and it would always return true.

This validates the user and logs him in without even checking the username or password.

Prevention/Security Measures
Always escape or filter user input fields. It takes lesser time to do user input filtering (than posting about cyberlaws ). This type of vulnerability is because of the poor codes on the server which practically hands over you the control.

The method demonstrated here is just the teaser, you can do much more sinister things with SQL injection like dropping tables (which is not a good thing)

to be continued...

In case you find a website with such a vulnerability, please inform the webmaster of the condition and refer this page (in case they are ignorant).

Be ethical
Happy Browsing...

Thursday, May 28, 2009

Timezone: PHP, Shell and Crontab

Programming in different timezones can be a headache if not taken care off initially. Codes written to work in one time zone can go wrong when ported to a server that runs on another Timezone. There a few things to take care off when you change from server to server.

PHP

By default PHP uses the Timezone that is set in the server or php.ini configuration files. Inorder to make a robust php code that can work in any server environment, it is always a good practice to set the Timezone in the code written.

date_default_timezone_set(Timezone)


is the PHP function to set the Timezone in PHP. This can take care of most of the timezone anomalies that can occur in your script when using PHP's Date Function.
Timezone is a string, for example for Indian Standard Time it is "Asia/Calcutta", for LA it is "America/Los_Angeles" etc. Checkout PHP Timezone list for a complete list of timezones.

Shell
In a shell the date command gives you the localtime at the SERVER. In order to set the Timezone to your area, we have to set the appropriate environment variable.

export TZ="/usr/share/zoneinfo/{Continent}/{Place}"
Eg:
export TZ="/usr/share/zoneinfo/Asia/Calcutta"


Add this snippet to your .bashrc to set your timezone to your localtime.

Crontab
Crontab runs in the same timezone as the server, so for now the best thing to do is calculate the offset between the server timezone and your localtime zone, and plan your crontab accordingly.

For example if you are in IST and the server is in GMT, which has an offset of +05:30 from IST, add your cron such that it runs localtime - 05:30 hrs on the server. ie
localtime - (offset time).

It would be easy to write a script to do this for you.

Tuesday, May 5, 2009

cURL PHP Error on Windows

Fatal error: Call to undefined function curl_init() in D:/webs/php.php on line 284

This problem arises because of not including the lib_curl.dll to PHP. To solve this uncomment the line as shown below

;extension=php_bz2.dll
extension=php_curl.dll
;extension=php_dba.dll


Now restart apache server.
If you get an error "Unable to Load X:/path/php/ext/php_curl.dll", copy
  • SSLEAY32.PHP
  • libEAY32.dll
(OpenSSL) Libraries to the System32 folder.

Now restart apache server and you are good to go.

Monday, July 7, 2008

Hosting a Site at your Home using Dataone

Intro

This is just a timepass for anyone who would want to try making their home PC to a server. The details are for my BSNL Dataone Router WA300XXX, But you can do it on any router provided your ISP supports, it works for BSNL (Bharath Sanchar Nigam Limited, INDIA) (Hurray!) .

So the first step is to setup apache, HTTP Server. I have covered this topic in my earlier post and doesnt want to repeat

Quick install for Linux/Debian way :

sudo apt-get install apache2


On the router..
Once having done that next step is to open a PORT in the router and forward the port to our PORT 80 on your computer. Port 80 is the default HTTP port. You can open up any port in the router, say 2345.

I did it using telnet and the way to open up a port differs from router to router.
Most modem hosts a site and runs a server internally at port 80, so you can try http://192.168.1.1
or the IP address of the router.

It will ask for a username and password.
The default username and password for the router is admin

Find out NAT
Here is my screen shot of the page


Add an entry similar to that

Name : http # Whats in a name ?
External Port : 2345 #You choose, 80 may not work
Protocol : TCP
Internal Port : 80
Server IP : 192.168.1.232 #thts me


Ok I guess thats it!

Restart your modem
Find your IP
and suppose your ip is AAA.BBB.CCC.DDD

People outside can access your site http://AAA.BBB.CCC.DDD:2345/
(as long as your ip is valid and your computer is on)

What abt something like www.yourname.com?

For that you gotta pay, Since remebering numbers is not so good when compared to memorizing a name a DNS system is used. And to get into a DNS you will need a static IP and pay to point a www.yourname.com to your static IP. There are workarounds for everything, if anyone is generous to comment on this, please help. :)

One more thing
You cant access the site from http://AAA.BBB.CCC.DDD:2345/, for that http://localhost is better.

Try it out...

Saturday, December 8, 2007

Virtual Hosts on Apache 2.2.6 [Windows]

What ?
Virtual Hosts allow you to simulate multiple sites on a single server.
i.e, One Server many Websites.
Virtual Hosts is enabled by uncommenting the line :


# Virtual hosts
#Include conf/extra/httpd-vhosts.conf #Uncomment this line

on httpd.conf

The virtual host conf file ./conf/extra/httpd-vhosts.conf

Eg:

#
# Virtual Hosts
#
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
# <URL:http://httpd.apache.org/docs/2.2/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.

#
# Use name-based virtual hosting.
#
NameVirtualHost *:80

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#

<VirtualHost *:80>
ServerName localhost
DocumentRoot "F:/webs/localhost"
DirectoryIndex index.php index.html index.htm index.shtml
</VirtualHost>

<VirtualHost *:80>
DocumentRoot "F:/webs/kitiyo.com"
ServerName pp.com
DirectoryIndex index.php index.html index.htm index.shtml
</VirtualHost>


In the above example shows localhost and kitiyo.com host on same server.
Remember to add the sites in the hosts file.

Apache 2.2.6 Getting 403 Forbidden on all pages

Intro
This is the issue about getting 403 Forbidden Error on all pages when you are working on localhost. I have used Virtual Host to simulate multiple sites on my computer. So I get 403 Forbidden on all pages. I tried for many solutions couldn't find anything much relevant. After sometime tweaking here is the solution or rather the problem I found.

...
The error forbidden arises due to


<Directory Directory Path, eg C:/www/>
# many things here ... :)
#
# Controls who can get stuff from this server.
#
Order allow,deny
Allow from all

</Directory>


If you have virtual hosts, you will have to allow permission to access those directories too which contain the sites. For eg. if you have another site on
C:/anotherwww/, you should include

<Directory Directory Path, eg C:/anotherwww/>
Options Indexes FollowSymLinks

#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Order allow,deny
Allow from all

</Directory>


Thats all
Restart Apache Server.
Your Problem is solved.

:)

Saturday, May 26, 2007

Custom extenstion for a Server Script

Intro
PHP files have a ".PHP" extension and is by default the extension used to run php scripts. Thats ordinary. Now it would be cool if we could name another extension (say .do or .html) for a PHP script and get the same output. This is precisely what the following is about.

In order to map a particular extension to a particular application, apache uses handlers. Basically you can define an extension and add the corresponding handler. This can be done in 2 ways :

CPanel / Apache Handlers
Add extension html and handler application/x-httpd-php to make a new handler :

html application/x-httpd-php

This makes any ordinary html file to be handled and processed by the PHP engine before it is output.

.htaccess
For those who dont have cPanel access, the .htaccess file can be edited.
Add
AddHandler application/x-httpd-php html

this line to your .htaccess file. This makes the same effect as done in the previous method.

Tip
  • Adding html extension as php can be cool, because a statically appearing page is actually dynamic. But the disadvantage of using this is that, all HTML files will be passed through the PHP engine and parsed for the PHP codes. This puts unnecessary pressure on the servers and makes it slow.
  • It is better to choose someother extension, if there is a lot of static content on your website. If most of your pages are in PHP, this effect is cool.
  • If for some reason your .htaccess gets deleted or restored to default, All the PHP codes will be echoed to the users browser and all the source would be revealed!. So beware when using this.

Thursday, January 4, 2007

Making subdomains on localhost

Intro
Developing websites on localhost may sometimes need making of sub domains. Installing and running another server for this purpose is useless, consumes more resources etc. So here is the technique to make sub domains on localhost.

You can also use this to host more than one site on localhost. i.e, you can host multiple sites situated on different folders.

PS: This is for Apache-Windows configuration. (If you have info about other Server-OS configs please let me know.)

Steps

First decide on the subdomain names...
for eg:
sub.localhost, sub2.localhost, alt.localhost , images.localhost etc..
You can also name www.name.com. (Please note that if there is a site with that name on the net, you wont be able to access that site on the internet.)

Second making these sites to point to 127.0.0.1, for this you can edit the HOSTS file on windows. See this about the HOSTS file.

Now add to HOSTS line
127.0.0.1 hostname
Egs:


127.0.0.1 sub.localhost
127.0.0.1 sub1.localhost
127.0.0.1 sub2.localhost
127.0.0.1 images.localhost
127.0.0.1 www.mysiteonmycomp.com


Next we have to assign each of these different URL's to different folders.
This is done on the apache conf file. (httpd.conf file situated in apache\conf\ directory)

Scroll all the way down the httpd.conf file until you reach about Virtual Hosts.
Add

NameVirtualHost *:80 #(This line was commented before)

<VirtualHost *:80>
ServerName subdomain name.localhost OR yoursite.com
DocumentRoot Path to the server root, See examples.
DirectoryIndex index.php index.html index.html index.htm index.shtml
</VirtualHost>

Egs:

<VirtualHost *:80>
ServerName sub.localhost
DocumentRoot "C:/public_html/sub"
DirectoryIndex index.php index.html index.html index.htm index.shtml
</VirtualHost>

<VirtualHost *:80>
ServerName images.localhost
DocumentRoot "C:/public_html/images"
DirectoryIndex index.php index.html index.html index.htm index.shtml
</VirtualHost>

<VirtualHost *:80>
ServerName site.com
DocumentRoot "C:/public_html/site"
DirectoryIndex index.php index.html index.html index.htm index.shtml
</VirtualHost>


Test the configurations (using apache -t)
All done!
Hope it worked for you ...
Feel free to leave a comment....

Friday, December 29, 2006

Setting up Apache-PHP on windows.

Step 1 : Downloading required files.

apache server binaries : httpd.apache.org
php binaries : www.php.net

Step 2 : Install apache server.
Installing apache server is pretty much simple, and same as any other installation.
Just enter a network domain name and server name and some email id (someone@microsoft.com).
Then choose a installation directory : for reference i am using "C:\".
Note that a directory called apache2 will be created under C:\ .
So "C:\apache2\" is our directory.
Verify your installation by taking your browser and visiting http://127.0.0.1 or http://localhost.
You will see a page that confirms your apache installation.

Step 3 : Configuring apache
Now goto "C:\Apache2\conf\".
Open the file "httpd.conf" in any text editor.
Lines beginning with # are comments .. just read them to know how to tweak apache.
Change the DocumentRoot property to the folder which is the root of the site.

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "C:/public_html/"

and after that a few lines under ..

#
# This should be changed to whatever you set DocumentRoot to.
#
<Directory "C:/public_html/">

#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs-2.0/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks

#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None

#
# Controls who can get stuff from this server.
#
Order allow,deny
Allow from all

</Directory>

So C:\public_html\ is the server root. ie http://localhost/ = C:\public_html.
After this is done test the configuration by executing (command line):

C:\Apache2\bin>apache -t

If the syntax is right you will get a "Syntax OK" message.

Step 4 : Installing php
To install php, just extract or copy the "PHP" folder to C:\Apache2\ so that C:\Apache2\PHP is created.

Step 5 : configuring PHP
Copy the "php.ini-recommended" file from C:\apache2\php\ to C:\apache2\
Rename it to "php.ini".

Add 2 lines to the end of the file httpd.conf:


LoadModule php5_module C:/apache2/php/php5apache2.dll
AddType application/x-httpd-php .php


Test the php installation by making a php file.

test.php

<?php
phpinfo();
?>


Access the test.php (http://localhost/test.php) from your browser. If you see the source code, installation has failed.
If you see the php info file . PHP installation was successful.
Test the apache server configuration (mentioned above) to find out the error.

Stuck ? leave a comment...