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

Python: Add LDAP Improper Authentication query #5444

Merged
merged 32 commits into from
Jul 22, 2021

Conversation

jorgectf
Copy link
Contributor

@jorgectf jorgectf commented Mar 18, 2021

Any suggestions/tricks to improve the query or workarounds (to learn), no matter how minimal they are, are massively appreciated.

@jorgectf jorgectf force-pushed the jorgectf/python/ldapimproperauth branch from 503e512 to 2f874c5 Compare April 6, 2021 13:48
@RasmusWL RasmusWL self-assigned this May 5, 2021
@RasmusWL
Copy link
Member

RasmusWL commented May 5, 2021

After reading up on LDAP, I don't quite agree about this query. As pointed out by your own #5443, all user-input should be escaped before being used to construct DNs/filters, so the examples auth_good_2.py and auth_good_3.py doesn't look good to me, since they still contain vulnerabilities.

I think there is value in finding places where the bind operation is being used in an insecure way, for example by allowing the password to be empty/non-existent. However, in it's current form, I don't think I would accept this query. If I've misunderstood something, please do let know though 👍

@jorgectf
Copy link
Contributor Author

jorgectf commented May 7, 2021

all user-input should be escaped before being used to construct DNs/filters, so the examples auth_good_2.py and auth_good_3.py doesn't look good to me, since they still contain vulnerabilities.

You are right, the tests are focused on the actual vulnerability/bad practice (using None, an empty string or not specifying a password in a bind process)

I think there is value in finding places where the bind operation is being used in an insecure way, for example by allowing the password to be empty/non-existent. However, in it's current form, I don't think I would accept this query. If I've misunderstood something, please do let know though 👍

I had to copy most of the structure from #5443, but this query can be very much simplified using that PR's .qll and just checking the arguments regarding the password set in the bind function.

How would you see this query fine for a PR? I'm open to additions 😀

@RasmusWL
Copy link
Member

I see that this query basically does what SonarSource does in https://rules.sonarsource.com/python/type/Vulnerability/RSPEC-4433.

I agree that authenticating with any service (internal/external) with only a username and no password is bad practice.

However, I think such a query does fall on the simple side, where most cases could be found by just using grep.

If you do go ahead with this PR, I do think you need to only focus on the calls to bind, and no so much on what kind of LDAP operations are made after the bind 😊

@jorgectf jorgectf force-pushed the jorgectf/python/ldapimproperauth branch from 4bbfc30 to 1d7ddce Compare June 17, 2021 16:10
@RasmusWL RasmusWL self-requested a review June 22, 2021 09:58
@RasmusWL
Copy link
Member

Thanks for letting me know 👍 I've started tests, and will do a proper review in the coming days 👍

RasmusWL added 4 commits June 28, 2021 10:46
Since having it inlined makes the query a bit easier to read. We
obviously need to share it if we want to share this predicate, but for
now that does not seem to be the case.
RasmusWL
RasmusWL previously approved these changes Jun 28, 2021
Copy link
Member

@RasmusWL RasmusWL left a comment

Choose a reason for hiding this comment

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

I think this PR looks ok. I did a few minor changes, which I pushed directly to this PR. They were so small that I felt it was easier just to fix them up myself 😊

This PR is going through a bit of internal process, but if everything goes according to plan, it should be merged within the next few days +1

I'm not 100% convinced about the qhelp since it talks about strong password for executing a query the user controls. I don't see how taking a password from an environment variable helps in this case. I think the original sonarsource query that you link to simply wants all LDAP connections to use passwords.

I'm also not 100% happy that the "secure" examples are vulnerable to LDAP injection 😅

I'm not necessarily expecting you to fix these up, just noting it down if we want to promote this query to our standard query set 👍

@jorgectf
Copy link
Contributor Author

I think this PR looks ok. I did a few minor changes, which I pushed directly to this PR. They were so small that I felt it was easier just to fix them up myself 😊

Thanks for that! 😊

I'm not 100% convinced about the qhelp since it talks about strong password for executing a query the user controls. I don't see how taking a password from an environment variable helps in this case. I think the original sonarsource query that you link to simply wants all LDAP connections to use passwords.

You are right, the main point of improper authentication is the fact of not using a password (or an empty one), so I'm going to change the qhelp to strictly focus on this point.

I'm also not 100% happy that the "secure" examples are vulnerable to LDAP injection 😅

Haha, I wanted to focus on showing the binding process, but since the code is an example of secure code, I'm going to escape user input and change the comments accordingly.

I'm not necessarily expecting you to fix these up, just noting it down if we want to promote this query to our standard query set 👍

I want the query to be the most accurate possible (and so learning through the process), so I have no problem on making any changes needed. Thanks a lot for your review and suggestions 😄

RasmusWL
RasmusWL previously approved these changes Jun 29, 2021
Copy link
Member

@RasmusWL RasmusWL left a comment

Choose a reason for hiding this comment

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

Awesome, thanks 💪

@kevinbackhouse
Copy link
Contributor

@jorgectf: Please could you take a look at these results on Bladefidz/mongodb-university: https://lgtm.com/query/9086417468810526559/
They look like false positives to me because I see username/passwords in all of them. Other projects that I have looked at have very similar results.

@jorgectf
Copy link
Contributor Author

@jorgectf: Please could you take a look at these results on Bladefidz/mongodb-university: https://lgtm.com/query/9086417468810526559/
They look like false positives to me because I see username/passwords in all of them. Other projects that I have looked at have very similar results.

Thanks @kevinbackhouse for the FP catch! The problem was that I didn't realize .matches("%bind%") would also take unbinding operations. It should be fixed in 2f9e645. Apologies for that!

Sorry @RasmusWL for another review cancel 😅.

RasmusWL
RasmusWL previously approved these changes Jun 29, 2021
Copy link
Member

@RasmusWL RasmusWL left a comment

Choose a reason for hiding this comment

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

Just fixed up merge conflict

@jorgectf
Copy link
Contributor Author

Just fixed up merge conflict

Thanks!

@RasmusWL RasmusWL merged commit f71c99a into github:main Jul 22, 2021
@jorgectf jorgectf deleted the jorgectf/python/ldapimproperauth branch July 23, 2021 00:09
@w2n1ck
Copy link

w2n1ck commented Sep 24, 2021

Nice, We've already encountered similar bug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants