Image
 
linkedin_logo.png rss_logo.jpg
twitter_logo.png youtube_logo.jpg
Latest Additions
 
EH-Net Login
Welcome Guest.






Lost Password?
No account yet? Register
Who's Online
We have 55 guests and 1 member online
 
Advertisement

You are here: Home arrow Ethical Hacking Discussions and Related Certificationsarrow Web Applicationsarrow using script for sql injection test...
EH-Net
May 18, 2013, 10:08:08 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Go back to The Ethical Hacker Network Online Magazine Home Page
 
   Home   Help Calendar Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: using script for sql injection test...  (Read 6698 times)
0 Members and 1 Guest are viewing this topic.
wlandymore
Newbie
*
Offline Offline

Posts: 34


View Profile
« on: May 08, 2012, 03:56:05 PM »

Hey guys,

I'm trying to write a script that will use the web form that is hooked into a mysql database to check if a user exists in the DB and if they have an e-mail. So I was thinking this would be possible to do through something like PHP but I've never done it before so I'm foggy on how it might be done.

I was thinking something like:

<?php
$db="testdb";
$table="users";
$host="http://someserver.com/index.php";

$host+/action=POST&usrname=user1&submit_button=submit;
if response = 'user exists' exit;
if not use another name...

Obviously that isn't what has to be there but I don't know how to automate this so that the usernames can be put in a form and then submitted one after the other to check their validity.

Anyone done something like before?
Logged
ajohnson
Recruiters
Hero Member
*
Offline Offline

Posts: 1056


aka dynamik


View Profile WWW
« Reply #1 on: May 08, 2012, 06:01:04 PM »

The following PHP script is a simplified version of the page I believe you're trying to access. It compares the POST user variable to a list of user names (users.db) and returns whether or not the POST user is a valid user. The type of back-end really isn't relevant. You ultimately just need to iterate through a list of users and identify a unique string that signifies a valid user in the page that's returned.

(Edit: You'll get a PHP Notice / blank page if you don't supply user via POST; I obviously didn't include any error checking.)

index.php:
Code:
<?php
$user 
$_POST['user'];

$f fopen('users.db''r');

$message 'Invalid User';

while (
$line trim(fgets($f))) {
    if (
$line == $user) {
        
$message 'Valid User';
        break;
    }
}

echo 
$message "\n";
?>


users.db:
Code:
steve
anthony
bob

I was originally going to write an example in Python, but I knew sil would respond with, "You can do that with bash..." so I decided to skip a step Wink

The following is the users.lst file that is iterated through and tested for validity.
Code:
bob
sally
alice
nicky
steve
bill
anthony
drew

This script iterates through the user list, acquires the page with wget, checks for the unique validity string, and writes out if a match is found.
Code:
for u in `cat users.lst`; do wget --post-data="user=$u" -q -O - http://localhost/ehtest/index.php | grep -i -q ^valid && echo $u found; done

Code:
bob found
steve found
anthony found

If you have the SQLi POST string, all you have to do is replace the username/email/whatever and perform text-matching like I did above. If you want to do this with Python and make it sexier, you can start by researching the urllib library.
« Last Edit: May 08, 2012, 06:03:45 PM by ajohnson » Logged

WIP: GCFA | www.infosiege.net | @infosiege

The day you stop learning is the day you start becoming obsolete.
ajohnson
Recruiters
Hero Member
*
Offline Offline

Posts: 1056


aka dynamik


View Profile WWW
« Reply #2 on: May 08, 2012, 11:06:20 PM »

Python example if you're curious (again, no error-checking/validation):

Code:
#!/usr/bin/python
import urllib2
import sys

if len(sys.argv) < 5:
    print 'Usage: ' + sys.argv[0] + ' <url> <post> <user list> <match text>'
    print 'Use ### as a placeholder for post variable'
    print 'i.e. ' + sys.argv[0] + ' http://localhost/ehtest/index.php user=### user.lst "Valid User"' + '\n'
    exit()

url = sys.argv[1]
post = sys.argv[2]
ulist = sys.argv[3]
match = sys.argv[4]
ph = '###'

f = open(ulist)

for user in f:
    user = user.strip()
    data = post.replace(ph, user)
    request = urllib2.Request(url, data)
    response = urllib2.urlopen(request)
    page = response.read()
    if page.find(match) > 0:
        print "Success for: " + user

Output (using same files as above):
Code:
# ./test.py http://localhost/ehtest/ user=### users.lst "Valid User"
Success for: bob
Success for: steve
Success for: anthony
Logged

WIP: GCFA | www.infosiege.net | @infosiege

The day you stop learning is the day you start becoming obsolete.
wlandymore
Newbie
*
Offline Offline

Posts: 34


View Profile
« Reply #3 on: May 11, 2012, 03:01:11 PM »

wow, thanks. That's awesome!
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines
Joomla Bridge by JoomlaHacks.com
Valid XHTML 1.0! Valid CSS!
Page created in 0.06 seconds with 23 queries.
 
Exclusive Deal

sansfire13_245x90_cw90.jpg
SANSFIRE 2013
June 15 - 22

5% Off w/ Code: EHN_5

SANS Deals 4 EH-Netters
5% OFF Any SANS Course in Any Format!
Coupon Code: EHN_5 Including SANS Rocky Mountain 2013 & SANS Boston 2013
Polls
Compared to this year, 2013 will be:
 
Recent Forum Topics
EH-Net News Feeds
Latest Additions
 
         
Free Business and Tech Magazines and eBooks

© 2013 The Ethical Hacker Network
Joomla! is Free Software released under the GNU/GPL License.