-
-
Notifications
You must be signed in to change notification settings - Fork 747
New UniqueFilter for images associated with compromised accounts. #3519
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
Open
swfarnsworth
wants to merge
6
commits into
main
Choose a base branch
from
swfarnsworth/image-filter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+205
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
539ab1c
New UniqueFilter for images associated with compromised accounts.
swfarnsworth c146b3a
Use `asyncio.to_thread` to make expensive actions awaitable.
swfarnsworth 0354e66
Handle case that `attachment.content_type` is None
swfarnsworth b8e3015
Handle (and ignore) exceptions from reading file attachments.
swfarnsworth 9fb7f38
Only consider attachments less than 30mb
swfarnsworth 3b9b3d6
Add comments describing the image associated with each perceptual hash.
swfarnsworth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import asyncio | ||
| import io | ||
|
|
||
| import discord | ||
| import imagehash | ||
| from PIL import Image, UnidentifiedImageError | ||
|
|
||
| from bot.exts.filtering._filter_context import Event, FilterContext | ||
| from bot.exts.filtering._filters.filter import UniqueFilter | ||
|
|
||
| _THRESHOLD = 4 | ||
| _KNOWN_IMAGE_HASHES = [ | ||
| imagehash.hex_to_hash(s) for s in [ | ||
| # A camera-taken image of a tweet attributed to @MrBeast about the purported launch of a crypto casino; | ||
| # there is a URL in the image that varies by instance | ||
| "c0d08f2f2f60f0cf", | ||
| # An image saying "Activate Code for Bonus!" | ||
| "973c4178e79492cd", | ||
| # An image saying "Withdrawal Success!" | ||
| "817c7e9391e46c1b", | ||
| ] | ||
| ] | ||
|
|
||
|
|
||
| async def _image_is_match(image: Image.Image) -> bool: | ||
| """Return whether the one image matches any of those known to be posted by compromised accounts.""" | ||
| incoming_image_hash = await asyncio.to_thread(imagehash.phash, image) | ||
| has_match = any( | ||
| incoming_image_hash - known_image_hash < _THRESHOLD | ||
| for known_image_hash in _KNOWN_IMAGE_HASHES | ||
| ) | ||
| return has_match | ||
|
|
||
|
|
||
| class ImageFilter(UniqueFilter): | ||
| """Filter messages that contain an image attachment whose perceptual hash matches images associated with scams.""" | ||
|
|
||
| name = "image" | ||
| events = (Event.MESSAGE, ) | ||
|
|
||
| async def triggered_on(self, ctx: FilterContext) -> bool: | ||
| """Return whether the message has an attached image that is known to be posted by compromised accounts.""" | ||
| for attachment in ctx.attachments: | ||
| if ( | ||
| attachment.content_type is None | ||
| or not attachment.content_type.startswith("image") | ||
| or attachment.size > 3e7 # 30 mb | ||
| ): | ||
| continue | ||
|
|
||
| try: | ||
| image_bytes = io.BytesIO(await attachment.read()) | ||
| except (discord.Forbidden, discord.DiscordServerError, discord.NotFound): | ||
| continue | ||
|
|
||
| try: | ||
| image = await asyncio.to_thread(Image.open, image_bytes) | ||
| except UnidentifiedImageError: | ||
| continue | ||
|
|
||
| if await _image_is_match(image): | ||
| return True | ||
|
|
||
| return False | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.