Goal Reached Thanks to every supporter — we hit 100%!

Goal: 1000 CNY · Raised: 1000 CNY

100.0%

CVE-2017-17917 PoC — Ruby on Rails SQL注入漏洞

Source
Associated Vulnerability
Title:Ruby on Rails SQL注入漏洞 (CVE-2017-17917)
Description:Ruby on Rails(Rails)是Rails核心团队开发维护的一套基于Ruby语言的开源Web应用框架,它是由大卫-海纳梅尔-韩森从美国37signals公司的项目管理工具Basecamp里分离出来的。 Ruby on Rails 5.1.4及之前的版本中的where方法存在SQL注入漏洞。远程攻击者可借助‘id’参数利用该漏洞执行任意的SQL命令。
Readme
# rails-cve-2017-17917
The project demonstrates the replication of a SQL injection vulnerability in the <b>id</b> parameter, and subsequently provides insights into mitigating and resolving this security issue.
https://www.cvedetails.com/cve/CVE-2017-17917/?q=CVE-2017-17917

<b>Stack</b>:

```
  Ruby: 3.2.2
  Rails: 7.0.8
  Docker 24.0.5
  Docker-Compose 1.29.2
  PostgreSQL
```

## [CVE-2017-17917](https://www.cvedetails.com/cve/CVE-2017-17917/?q=CVE-2017-17917)
![image](https://github.com/matiasarenhard/rails-cve-2017-17917/assets/14844393/d601e033-5dd6-410f-b9bf-be32c51b255c)

We selected this CVE to highlight the persisting occurrence of this issue in the latest versions of Rails, reaffirming its relevance of the development best practices.


## Schema from the environment to replicate the vulnerability

![Diagrama sem nome drawio(1)](https://github.com/matiasarenhard/rails-cve-2017-17917/assets/48795586/e6566323-7dae-45b3-bb2e-4849b06cfc43)

## Instructions to replicate this vulnerability.
<b>Requirements: </b>
```
  docker
  docker-compose
```

<b>Steps to build a project</b>:

```
  sudo docker-compose build
```
![image](https://github.com/matiasarenhard/rails-cve-2017-17917/assets/14844393/c1a23f3c-4cee-42e1-b030-1d33a9a7454d)

```
  sudo docker-compose run web bundle install
```

![image](https://github.com/matiasarenhard/rails-cve-2017-17917/assets/14844393/11aa8eb1-37a3-4f42-88e5-a152c04ad4b4)

```
  sudo docker-compose run web rails db:create db:migrate db:seed
```

![image](https://github.com/matiasarenhard/rails-cve-2017-17917/assets/14844393/2757fcc4-a05b-4c6f-a2d2-65f075824343)

```
  sudo docker-compose up
```
![image](https://github.com/matiasarenhard/rails-cve-2017-17917/assets/14844393/3a4d0277-0e9a-40bf-b6b1-d7425051ac50)


Go to http://localhost:3000/

![image](https://github.com/matiasarenhard/rails-cve-2017-17917/assets/14844393/a0e43e0c-b54a-4399-a702-dd28dcbded8d)

![image](https://github.com/matiasarenhard/rails-cve-2017-17917/assets/14844393/f8e4e76e-f7e3-4757-a59c-2052c1c30e3f)

SQL Injection param `1 OR id > 1`

![image](https://github.com/matiasarenhard/rails-cve-2017-17917/assets/14844393/df1c162f-0ce9-4038-bc26-bc56996f2281)

## Assessing the risks of the vulnerability in a production environment.
With this SQL injection, an attacker can retrieve user data they wouldn't normally have access to view. <br/>
degree difficulty to execution: <b>easy</b>

## Conclusion
The issue lies in the controller method, specifically when invoking a where clause as follows: <br/>
 ``` @clients = Client.where("id = #{params[:id_search]}") ``` <br/>
 [Link to Code](https://github.com/matiasarenhard/rails-cve-2017-17917/blob/main/app/controllers/clients_controller.rb#L7)
 <br/>

 
The problem is resolved by using the following approach: <br/>
  ```@clients = Client.where(id: "#{params[:id_search]}")``` <br/>
  [Link to Code](https://github.com/matiasarenhard/rails-cve-2017-17917/blob/main/app/controllers/clients_controller.rb#L10)
  
## Tests 
```
  sudo docker-compose run web rspec
```
![image](https://github.com/matiasarenhard/rails-cve-2017-17917/assets/14844393/841d0afc-b84b-46ee-9e1f-b6cdbc876a21)




File Snapshot

[4.0K] /data/pocs/6f96a810dc0f07afac6bbeec70929671a1fc9d20 ├── [4.0K] app │   ├── [4.0K] assets │   │   ├── [4.0K] config │   │   │   └── [ 143] manifest.js │   │   ├── [4.0K] images │   │   └── [4.0K] stylesheets │   │   └── [ 112] application.scss │   ├── [4.0K] channels │   │   └── [4.0K] application_cable │   │   ├── [ 79] channel.rb │   │   └── [ 85] connection.rb │   ├── [4.0K] controllers │   │   ├── [ 57] application_controller.rb │   │   ├── [1.7K] clients_controller.rb │   │   └── [4.0K] concerns │   ├── [4.0K] helpers │   │   ├── [ 29] application_helper.rb │   │   └── [ 25] clients_helper.rb │   ├── [4.0K] javascript │   │   ├── [ 202] application.js │   │   └── [4.0K] controllers │   │   ├── [ 218] application.js │   │   ├── [ 157] hello_controller.js │   │   └── [ 584] index.js │   ├── [4.0K] jobs │   │   └── [ 269] application_job.rb │   ├── [4.0K] mailers │   │   └── [ 102] application_mailer.rb │   ├── [4.0K] models │   │   ├── [ 74] application_record.rb │   │   ├── [ 228] client.rb │   │   └── [4.0K] concerns │   └── [4.0K] views │   ├── [4.0K] clients │   │   ├── [ 383] _client.html.erb │   │   ├── [ 142] _client.json.jbuilder │   │   ├── [ 149] edit.html.erb │   │   ├── [1.5K] _form.html.erb │   │   ├── [1.8K] index.html.erb │   │   ├── [ 61] index.json.jbuilder │   │   ├── [ 118] new.html.erb │   │   ├── [1.2K] show.html.erb │   │   └── [ 48] show.json.jbuilder │   ├── [4.0K] layouts │   │   ├── [ 592] application.html.erb │   │   ├── [ 229] mailer.html.erb │   │   └── [ 13] mailer.text.erb │   └── [4.0K] shared │   └── [ 451] _alerts.html.erb ├── [4.0K] bin │   ├── [2.7K] bundle │   ├── [ 91] importmap │   ├── [ 141] rails │   ├── [ 90] rake │   └── [1010] setup ├── [4.0K] config │   ├── [ 782] application.rb │   ├── [ 207] boot.rb │   ├── [ 233] cable.yml │   ├── [ 464] credentials.yml.enc │   ├── [ 332] database.yml │   ├── [ 128] environment.rb │   ├── [4.0K] environments │   │   ├── [2.4K] development.rb │   │   ├── [3.8K] production.rb │   │   └── [2.3K] test.rb │   ├── [ 409] importmap.rb │   ├── [4.0K] initializers │   │   ├── [ 580] assets.rb │   │   ├── [1.0K] content_security_policy.rb │   │   ├── [ 396] filter_parameter_logging.rb │   │   ├── [ 649] inflections.rb │   │   └── [ 384] permissions_policy.rb │   ├── [4.0K] locales │   │   └── [ 849] en.yml │   ├── [1.8K] puma.rb │   ├── [ 214] routes.rb │   └── [1.1K] storage.yml ├── [ 160] config.ru ├── [4.0K] db │   ├── [4.0K] migrate │   │   └── [ 252] 20240119002301_create_clients.rb │   ├── [1.1K] schema.rb │   └── [ 403] seeds.rb ├── [ 449] docker-compose.yml ├── [ 372] Dockerfile ├── [ 216] entrypoint.sh ├── [2.7K] Gemfile ├── [6.8K] Gemfile.lock ├── [4.0K] lib │   ├── [4.0K] assets │   └── [4.0K] tasks ├── [4.0K] log ├── [4.0K] public │   ├── [1.7K] 404.html │   ├── [1.7K] 422.html │   ├── [1.6K] 500.html │   ├── [ 0] apple-touch-icon.png │   ├── [ 0] apple-touch-icon-precomposed.png │   ├── [ 0] favicon.ico │   └── [ 99] robots.txt ├── [ 227] Rakefile ├── [3.1K] README.md ├── [4.0K] spec │   ├── [4.0K] controllers │   │   └── [1003] clients_controller_spec.rb │   ├── [4.0K] factories │   │   └── [ 284] clients.rb │   ├── [4.0K] models │   │   └── [1.1K] client_spec.rb │   ├── [2.8K] rails_helper.rb │   └── [4.5K] spec_helper.rb ├── [4.0K] storage ├── [4.0K] tmp │   ├── [4.0K] pids │   └── [4.0K] storage └── [4.0K] vendor └── [4.0K] javascript 42 directories, 76 files
Shenlong Bot has cached this for you
Remarks
    1. It is advised to access via the original source first.
    2. If the original source is unavailable, please email f.jinxu#gmail.com for a local snapshot (replace # with @).
    3. Shenlong has snapshotted the POC code for you. To support long-term maintenance, please consider donating. Thank you for your support.