Skip to content

Conversation

@IshaanXCoder
Copy link

@IshaanXCoder IshaanXCoder commented Dec 22, 2025

Description

This PR includes the fixes discussed in #611 . This adds the ability to query the CowrieSession API by password, in addition to the existing IP address and SHA-256 hash query methods. Changes are mentioned as follows -

  • api/views/cowrie_session.py

    • Added password query support using Django ORM (removed the raw SQL added in the previous PR).
    • Simplified query validation: IP → hash → password fallback.
    • Removed character restrictions and redundant IP/hash re-checks as asked.
  • greedybear/models.py

    • Added GinIndex on CowrieSession.credentials in Meta.indexes to speed up credential lookups.
    • Removed the manual migration done earlier

I also added .DS_Store to .gitignore since it can be irritating for developers.

Related issues

SOLVES #607

Type of change

  • New feature (non-breaking change which adds functionality).

Checklist

  • I have read and understood the rules about how to contribute to this project.
  • The pull request is for the branch develop.
  • I have added documentation of the new features. (Updated docstring in cowrie_session_view)
  • Linters (Black, Flake, Isort) gave 0 errors. If you have correctly installed pre-commit, it does these checks and adjustments on your behalf.
  • I have added tests for the feature/bug I solved. All the tests (new and old ones) gave 0 errors.
  • If changes were made to an existing model/serializer/view, the docs were updated and regenerated (check CONTRIBUTE.md). (Updated view docstring)
  • If the GUI has been modified:
    • I have provided a screenshot of the result in the PR.
    • I have created new frontend tests for the new component or updated existing ones.

@IshaanXCoder
Copy link
Author

@regulartim @mlodic , kindly review this PR. It contains all the fixes discussed in #611, implemented in a cleaner format. Apologies for the delay.

@regulartim
Copy link
Collaborator

Please fix the formatting @IshaanXCoder

@regulartim
Copy link
Collaborator

Also, the PR title is not very informative. Please stick to our contribution guidelines and our PR template.

@IshaanXCoder IshaanXCoder changed the title Solved #607 Solved issue #607 - Allow to query for passwords in CowrieSession API Dec 23, 2025
@IshaanXCoder IshaanXCoder changed the title Solved issue #607 - Allow to query for passwords in CowrieSession API Allow to query for passwords in CowrieSession API - Closes #607 Dec 23, 2025
@IshaanXCoder
Copy link
Author

Please fix the formatting @IshaanXCoder

Made the required changes

@regulartim
Copy link
Collaborator

Made the required changes

No you did not. Since I asked you to fix the formatting you did not push a single commit!

@IshaanXCoder
Copy link
Author

Oh really sorry! I misunderstood it with formatting the PR description, pushed the changes, PTAL @regulartim.

@regulartim
Copy link
Collaborator

CI still fails. Please read into the contribution guidelines again, learn how to avoid such problems and make sure your code is correctly formatted before pushing.

@IshaanXCoder
Copy link
Author

Hey @regulartim PTAL. I fixed it, took time, apologies for the same.

  • Added input validation to properly distinguish between query types:
    • Invalid IPs/hashes return 400 (Bad Request) instead of being treated as password queries.
    • XSS attempts and malformed input are rejected.
    • Only well-formed, non-IP/non-hash strings are processed as password queries
  • Fixed formatting and linting issues using black, isort and flake8, all passed. Understood where I was going wrong earlier.
Screenshot 2025-12-24 at 3 22 28 PM Screenshot 2025-12-24 at 3 22 51 PM

Copy link
Collaborator

@regulartim regulartim left a comment

Choose a reason for hiding this comment

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

No problem, take your time! :) However, in my opinion, this is not ready to be merged yet. We have to discuss some things.

Comment on lines +87 to +88
if len(observable) == 64 and not is_sha256hash(observable):
return HttpResponseBadRequest("Invalid hash format: must be 64 hexadecimal characters")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok, so I is not possible to query passwords of length 64, right?

else:
return HttpResponseBadRequest("Query must be a valid IP address or SHA-256 hash")
password_pattern = f" | {observable}"
sessions = CowrieSession.objects.filter(duration__gt=0, credentials__icontains=password_pattern).prefetch_related("source", "commands")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why did you choose to use icontains instead of contains here? Passwords are usually case sensitive, right?

Copy link
Collaborator

Choose a reason for hiding this comment

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

By the way: I am very surprised that credentials__icontains=password_pattern works at all! The django docs suggest that at least __contains expects a list.
https://docs.djangoproject.com/en/6.0/ref/contrib/postgres/fields/#contains

Could you point me to some resource where the behavior of ArrayField.__icontains is described the way you use it here?

Copy link
Author

Choose a reason for hiding this comment

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

okay I checked the django and PostgreSQL documentation and this behavior is not documented there.

ArrayField.__icontains with a string operand appears to work due to implicit casting of the array to text by PostgreSQL ad not because django officially supports it.

so yeah I agree, i'll change it to the documented approach. will make the changes

Copy link
Collaborator

Choose a reason for hiding this comment

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

But I you wrote tests for that and they pass, right? Where did you get the idea to use filter like that?

class Meta:
indexes = [
models.Index(fields=["source"]),
GinIndex(fields=["credentials"], name="greedybear_credentials_gin_idx"),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are you sure that this index helps in our specific use case? I looked it up and that's really advanced database stuff! At least for me it is hard to understand what this type of index actually does.

@IshaanXCoder
Copy link
Author

@regulartim shall i implement the above conversation?

@regulartim
Copy link
Collaborator

@regulartim shall i implement the above conversation?

Sorry for my late reply. Was busy with Christmas stuff. :) Could you first reply to the rest of my comments please?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants