Skip to content

Commit

Permalink
added --update switch for ghauri to be updated from github when it is…
Browse files Browse the repository at this point in the history
… cloned and installed from github..
  • Loading branch information
r0oth3x49 committed Oct 22, 2023
1 parent dad5607 commit f4e3bfe
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 5 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![GitHub release](https://img.shields.io/badge/release-v1.2.6-brightgreen?style=flat-square)](https://github.com/r0oth3x49/ghauri/releases/tag/1.2.6)
[![GitHub release](https://img.shields.io/badge/release-v1.2.7-brightgreen?style=flat-square)](https://github.com/r0oth3x49/ghauri/releases/tag/1.2.7)
[![GitHub stars](https://img.shields.io/github/stars/r0oth3x49/ghauri?style=flat-square)](https://github.com/r0oth3x49/ghauri/stargazers)
[![GitHub forks](https://img.shields.io/github/forks/r0oth3x49/ghauri?style=flat-square)](https://github.com/r0oth3x49/ghauri/network)
[![GitHub issues](https://img.shields.io/github/issues/r0oth3x49/ghauri?style=flat-square)](https://github.com/r0oth3x49/ghauri/issues)
Expand Down Expand Up @@ -56,6 +56,8 @@ You can download the latest version of Ghauri by cloning the GitHub repository.
- added support for sql-shell switch: `--sql-shell` (experimental)
- added support for fresh queries switch: `--fresh-queries`
- added switch for hostname extraction: `--hostname`
- added switch to update ghauri from github: `--update`
- Note: ghauri has to be cloned/installed from github


## **Advanced Usage**
Expand All @@ -71,6 +73,7 @@ General:
-h, --help Shows the help.
--version Shows the version.
-v VERBOSE Verbosity level: 1-5 (default 1).
--update update ghauri
--batch Never ask for user input, use the default behavior
--flush-session Flush session files for current target
--fresh-queries Ignore query results stored in session file
Expand Down
2 changes: 1 addition & 1 deletion ghauri/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"""

__version__ = "1.2.6"
__version__ = "1.2.7"
__author__ = "Nasir Khan (r0ot h3x49)"
__license__ = "MIT"
__copyright__ = "Copyright (c) 2016-2025 Nasir Khan (r0ot h3x49)"
Expand Down
3 changes: 3 additions & 0 deletions ghauri/common/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@

NO_DEFAULT = object()

GIT_REPOSITORY = "https://www.github.com/r0oth3x49/ghauri.git"
LATEST_VERSION = "https://api.github.com/repos/r0oth3x49/ghauri/releases/latest"

INJECTABLE_HEADERS_DEFAULT = [
"X-Forwarded-For",
"User-Agent",
Expand Down
132 changes: 132 additions & 0 deletions ghauri/core/update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# pylint: disable=R,W,E,C

"""
Author : Nasir Khan (r0ot h3x49)
Github : https://github.com/r0oth3x49
License : MIT
Copyright (c) 2016-2025 Nasir Khan (r0ot h3x49)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the
Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""

import os
import re
import json
import subprocess
from ghauri.core.request import request
from ghauri import __version__ as VERSION
from ghauri.logger.colored_logger import logger
from ghauri.common.lib import GIT_REPOSITORY, LATEST_VERSION


def version_check(show=True):
is_latest = True
try:
logger.debug("checking latest version on github...")
response = request.perform(url=LATEST_VERSION)
ok = json.loads(response.text)
tag = ok.get("tag_name")
ver = [int(i) for i in tag.split(".")]
current_version = [int(i) for i in VERSION.split(".")]
if show:
if ver > current_version:
logger.info(
"You are using an old version 'v{}' of ghauri, update to latest version: 'v{}'..".format(
VERSION, tag
)
)
is_latest = False
else:
logger.info("already at the latest version 'v%s'" % (tag or VERSION))
except Exception as error:
logger.debug("update could not be completed ('%s')" % str(error))
logger.end("ending")
exit(0)
return is_latest, tag


def update_ghauri():
success = False
GHAURI_ROOT_PATH = os.path.dirname(
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
)
here = os.path.join(GHAURI_ROOT_PATH, ".git")
if not os.path.exists(here):
warnMsg = "not a git repository. It is recommended to clone the 'r0oth3x49/ghauri' repository "
warnMsg += "from GitHub (e.g. 'git clone %s ghauri')" % GIT_REPOSITORY
logger.warning(warnMsg)
else:
infoMsg = "updating ghauri to the latest development revision from the "
infoMsg += "GitHub repository"
logger.info(infoMsg)

debugMsg = "ghauri will try to update itself using 'git' command"
logger.debug(debugMsg)

logger.info("update in progress....")

output = ""
try:
process = subprocess.Popen(
"git checkout . && git pull %s HEAD" % GIT_REPOSITORY,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=GHAURI_ROOT_PATH,
)
output, _ = process.communicate()
success = not process.returncode
except Exception as ex:
success = False
output = str(ex)
finally:
output = output.decode()

_, ver = version_check(show=False)

if success:
logger.info(
"%s the latest version 'v%s'"
% (
"already at" if "Already" in output else "updated to",
ver,
)
)
else:
if "Not a git repository" in output:
errMsg = "not a valid git repository. Please checkout the 'r0oth3x49/ghauri' repository "
errMsg += "from GitHub (e.g. 'git clone %s ghauri')" % GIT_REPOSITORY
logger.error(errMsg)
else:
logger.error(
"update could not be completed ('%s')"
% re.sub(r"\W+", " ", output).strip()
)

if not success:
if os.name == "nt":
infoMsg = "for Windows platform it's recommended "
infoMsg += "to use a GitHub for Windows client for updating "
infoMsg += "purposes (https://desktop.github.com/) or just "
infoMsg += "download the latest snapshot from "
infoMsg += "https://github.com/r0oth3x49/ghauri and install"
else:
infoMsg = "for Linux platform it's recommended "
infoMsg += "to install a standard 'git' package (e.g.: 'apt install git')"

logger.info(infoMsg)
11 changes: 11 additions & 0 deletions ghauri/ghauri.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from ghauri.logger.colored_logger import logger, set_level
from ghauri.core.tests import basic_check, check_injections
from ghauri.core.extract import ghauri_extractor as ge
from ghauri.core.update import update_ghauri
from ghauri.common.lib import (
os,
re,
Expand Down Expand Up @@ -94,6 +95,7 @@ def perform_injection(
test_filter=None,
sql_shell=False,
fresh_queries=False,
update=False,
):
verbose_levels = {
1: logging.INFO,
Expand All @@ -119,6 +121,15 @@ def perform_injection(
set_level(verbose_level, "")
if threads and threads > 1:
conf.threads = threads
if update:
try:
update_ghauri()
logger.end("ending")
exit(0)
except Exception as error:
logger.error("could not update ghauri, do it manually...")
logger.end("ending")
exit(0)
GhauriResponse = collections.namedtuple(
"GhauriResponse",
[
Expand Down
12 changes: 10 additions & 2 deletions ghauri/scripts/ghauri.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ def main():
general.add_argument(
"--version", action="version", version=version, help="Shows the version."
)
general.add_argument(
"--update",
dest="update",
action="store_true",
help="update ghauri",
)
general.add_argument(
"-v",
dest="verbose",
Expand Down Expand Up @@ -434,8 +440,9 @@ def main():

raw = ""
if not args.url and not args.requestfile:
parser.print_help()
exit(0)
if not args.update:
parser.print_help()
exit(0)

if args.testparameter:
args.testparameter = [i.strip() for i in args.testparameter.split(",")]
Expand Down Expand Up @@ -477,6 +484,7 @@ def main():
test_filter=args.test_filter,
sql_shell=args.sql_shell,
fresh_queries=args.fresh_queries,
update=args.update,
)
if resp.is_injected:
target = ghauri.Ghauri(
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="ghauri",
version="1.2.6",
version="1.2.7",
description="An advanced SQL injection detection & exploitation tool.",
classifiers=["Programming Language :: Python3"],
author="Nasir Khan",
Expand Down

0 comments on commit f4e3bfe

Please sign in to comment.