---
title: "18. Web Mass Assignment Vulnerabilities"
date: 2025-01-21
draft: false
---

Several frameworks offer mass-assignment features to lessen workload. 
- Programmers directly insert a whole set of user-entered data frmo a form into an obj or db
- Feature often used without a whitelist for protecting the fields from the user's input. 
- vuln could be used by an attacker to steal sensitive information or destroy data 

Vuln where attackers can modify model attributes of an application through the parameters sent to server. 
Reversing code, attackers can see these parameters + by assigning values to critical unprotected parameters during HTTP request they can edit the data of a database and change the intended functionality of an application 

RubyOnRails is a webapp framework vulnerable of this kind of attack. 

Assuming we have a `user` model with these attributes: 

```ruby
class User < ActiveRecord::Base
  attr_accessible :username, :email
end
```

Above model specifies only `username` and `email` attributes are allowed to be mass-assigned. However - attackers can modify other attributes by tampering with the params sent to server. Assume server receives below 

```javascript
{ "user" => { "username" => "hacker", "email" => "hacker@example.com", "admin" => true } }
```

Although `user` model doesn't explicitly state `admin` attribute is accessible - attacker can change it because it is present in the arguments. 
Bypassing any access controls that may be in place, attacker can send this as a post request to establish a user with admin privs. 

---

**example of exploiting mass assignment vulnerabilities**

Basic idea: 

Code is written in some way that evaluates session params. 
```python
for i,j,k in cur.execute('select * from users where username=? and password=?',(username,password)):
  if k:
    session['user']=i
    return redirect("/home",code=302)
  else:
    return render_template('login.html',value='Account is pending for approval')
```

Above checks value of `k` - If yes, user can login. In below code, `confirmed` param set during registration and sets `cond` as `true` and allows bypassing registration checking 
```python
try:
  if request.form['confirmed']:
    cond=True
except:
      cond=False
with sqlite3.connect("database.db") as con:
  cur = con.cursor()
  cur.execute('select * from users where username=?',(username,))
  if cur.fetchone():
    return render_template('index.html',value='User exists!!')
  else:
    cur.execute('insert into users values(?,?,?)',(username,password,cond))
    con.commit()
    return render_template('index.html',value='Success!!')
```

If we send a request to /register with `confirmed` set to some value, 
```bash
username=new&password=test&confirmed=test
```

Assignment vuln is exploited and we can login without pending approval. 
Example of prevention:
```ruby
class UsersController < ApplicationController
  def create
    @user = User.new(user_params)
    if @user.save
      redirect_to @user
    else
      render 'new'
    end
  end

  private

  def user_params
    params.require(:user).permit(:username, :email)
  end
end
```

`user_params` method returns hash that includes only `username` and `email` attributes, ignoring any more input client sent. 

In the given exercise, we have this `app.py` code: 
```python
┌──(root㉿kali)-[~/cpts/common-webapps/iis/IIS-ShortName-Scanner]
  Processes:               173
  IPv4 address for ens160: 10.129.205.15

   just raised the bar for easy, resilient and secure K8s cluster deployment.
   https://ubuntu.com/engage/secure-kubernetes-at-the-edge
                return render_template('home.html')
                return render_template('home.html')
        else:
                return redirect('/',code=302)

@app.route('/logout')
def logout():
        session.clear
        return redirect('/',code=302)

@app.route('/register',methods=['GET','POST'])
def register():
        if request.method=='GET':
                return render_template('index.html')
        else:
                username=request.form['username']
                password=request.form['password']
                try:
                        if request.form['active']:
                                cond=True
                except:
                                cond=False
                with sqlite3.connect("database.db") as con:
                        cur = con.cursor()
                        cur.execute('select * from users where username=?',(username,))
                        if cur.fetchone():
                                return render_template('index.html',value='User exists!!')
                        else:
                                cur.execute('insert into users values(?,?,?)',(username,password,cond))
                                con.commit()
                                return render_template('index.html',value='Success!!')

@app.route('/profit',methods=['GET','POST'])
def profit():
        if session:
                if request.method=='GET':
                        return render_template('profit.html')
                else:
                        expr=request.form['sp']
                        result=eval(expr)
                        return render_template('profit.html',value=result)

        else:
                return redirect('/',code=302)

if __name__=="__main__":
        app.run('0.0.0.0',3000)
```

In the above example the parameter **active** can be manipulated to 'trick' the webapplication into thinking the registration has already been done for our request. This is similar to the `request.form['confirmed']` parameter seen in the above example. 

