Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added vuln #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions vulns/sql_injection/sql_injection_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,71 @@ def sql_injection_login_api(request, app):
)


def sql_injection_login_apiiiii(request, app):
form = request.form

username = form.get('username')
password = form.get('password')
password_hash = _hash_password(password)

sql = f"SELECT * FROM users WHERE username='{username}' AND password='{password_hash}'"
flask.render_template_string(username)

db_result = app.db_helper.execute_read(sql)

user = list(
map(
lambda u: {
'id': u[0],
'username': u[1],
'password': u[2]
},
db_result
)
)[0] if len(db_result) > 0 else None

return render_template(
'sql_injection/login.html',
sql=sql,
logged=user is not None
)





def sql_injection_login_apis(request, app):
form = request.form

username = form.get('username')
password = form.get('password')
password_hash = _hash_password(password)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like MD5 is used as a password hash. MD5 is not considered a secure password hash because it can be cracked by an attacker in a short amount of time. Use a suitable password hashing function such as scrypt. You can use hashlib.scrypt.

View Dataflow Graph
flowchart LR
    classDef invis fill:white, stroke: none
    classDef default fill:#e7f5ff, color:#1c7fd6, stroke: none

    subgraph File0["<b>vulns/sql_injection/sql_injection_login.py</b>"]
        direction LR
        %% Source

        subgraph Source
            direction LR

            v0["<a href=https://github.com/atearjen/bad-python-app/blob/c06419049f5f898135eeba1c65309a15071249a7/vulns/sql_injection/sql_injection_login.py#L109 target=_blank style='text-decoration:none; color:#1c7fd6'>[Line: 109] hashlib.md5</a>"]
        end
        %% Intermediate

        subgraph Traces0[Traces]
            direction TB

            v2["<a href=https://github.com/atearjen/bad-python-app/blob/c06419049f5f898135eeba1c65309a15071249a7/vulns/sql_injection/sql_injection_login.py#L109 target=_blank style='text-decoration:none; color:#1c7fd6'>[Line: 109] md5_pass</a>"]

            v3["<a href=https://github.com/atearjen/bad-python-app/blob/c06419049f5f898135eeba1c65309a15071249a7/vulns/sql_injection/sql_injection_login.py#L81 target=_blank style='text-decoration:none; color:#1c7fd6'>[Line: 81] _hash_password</a>"]
        end
            v2 --> v3
        %% Sink

        subgraph Sink
            direction LR

            v1["<a href=https://github.com/atearjen/bad-python-app/blob/c06419049f5f898135eeba1c65309a15071249a7/vulns/sql_injection/sql_injection_login.py#L81 target=_blank style='text-decoration:none; color:#1c7fd6'>[Line: 81] _hash_password(password)</a>"]
        end
    end
    %% Class Assignment
    Source:::invis
    Sink:::invis

    Traces0:::invis
    File0:::invis

    %% Connections

    Source --> Traces0
    Traces0 --> Sink

Loading
Ignore this finding from md5-used-as-password.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/semgrep ignore not relevant


sql = f"SELECT * FROM users WHERE username='{username}' AND password='{password_hash}'"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Detected user input used to manually construct a SQL string. This is usually bad practice because manual construction could accidentally result in a SQL injection. An attacker could use a SQL injection to steal or modify contents of the database. Instead, use a parameterized query which is available by default in most database engines. Alternatively, consider using the Django object-relational mappers (ORM) instead of raw SQL queries.

View Dataflow Graph
flowchart LR
    classDef invis fill:white, stroke: none
    classDef default fill:#e7f5ff, color:#1c7fd6, stroke: none

    subgraph File0["<b>vulns/sql_injection/sql_injection_login.py</b>"]
        direction LR
        %% Source

        subgraph Source
            direction LR

            v0["<a href=https://github.com/atearjen/bad-python-app/blob/c06419049f5f898135eeba1c65309a15071249a7/vulns/sql_injection/sql_injection_login.py#L77 target=_blank style='text-decoration:none; color:#1c7fd6'>[Line: 77] request.form</a>"]
        end
        %% Intermediate

        subgraph Traces0[Traces]
            direction TB

            v2["<a href=https://github.com/atearjen/bad-python-app/blob/c06419049f5f898135eeba1c65309a15071249a7/vulns/sql_injection/sql_injection_login.py#L77 target=_blank style='text-decoration:none; color:#1c7fd6'>[Line: 77] form</a>"]

            v3["<a href=https://github.com/atearjen/bad-python-app/blob/c06419049f5f898135eeba1c65309a15071249a7/vulns/sql_injection/sql_injection_login.py#L79 target=_blank style='text-decoration:none; color:#1c7fd6'>[Line: 79] username</a>"]
        end
            v2 --> v3
        %% Sink

        subgraph Sink
            direction LR

            v1["<a href=https://github.com/atearjen/bad-python-app/blob/c06419049f5f898135eeba1c65309a15071249a7/vulns/sql_injection/sql_injection_login.py#L83 target=_blank style='text-decoration:none; color:#1c7fd6'>[Line: 83] f&quot;SELECT * FROM users WHERE username=&apos;{username}&apos; AND password=&apos;{password_hash}&apos;&quot;</a>"]
        end
    end
    %% Class Assignment
    Source:::invis
    Sink:::invis

    Traces0:::invis
    File0:::invis

    %% Connections

    Source --> Traces0
    Traces0 --> Sink

Loading
Ignore this finding from tainted-sql-string.

flask.render_template_string(username)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found a template created with string formatting. This is susceptible to server-side template injection and cross-site scripting attacks.

Ignore this finding from render-template-string.


db_result = app.db_helper.execute_read(sql)

user = list(
map(
lambda u: {
'id': u[0],
'username': u[1],
'password': u[2]
},
db_result
)
)[0] if len(db_result) > 0 else None

return render_template(
'sql_injection/login.html',
sql=sql,
logged=user is not None
)




def _hash_password(password):
md5_pass = hashlib.md5(password.encode('utf-8')).hexdigest()
return md5_pass
Loading