Skip to content

Commit

Permalink
Phishing: Custom domains, reword to "unsafe domain"
Browse files Browse the repository at this point in the history
  • Loading branch information
gdude2002 committed Mar 18, 2024
1 parent a484ff4 commit 487370b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,29 @@ class ExtPhishingBuilder {
val checks: MutableList<CheckWithCache<Event>> = mutableListOf()

/**
* If you want to require a permission for the phishing check commands, supply it here. Alternatively, supply
* `null` and everyone will be given access to them.
* Set of bad domains, used as well as the usual unsafe domains.
*
* Contains a small list of known unsafe domains.
*/
val badDomains: MutableSet<String> = mutableSetOf(
// Data broker for scraped Discord user and message data.
"spy.pet",
)

/**
* If you want to require a permission for the check commands, supply it here.
* Alternatively, supply `null` and everyone will be given access to them.
*/
var requiredCommandPermission: Permission? = Permission.ManageMessages

/**
* What to do when a message creation/edit contains a phishing domain.
* What to do when a message creation/edit contains an unsafe domain.
*
* @see DetectionAction
*/
var detectionAction: DetectionAction = DetectionAction.Delete

/** Whether to DM users when their messages contain phishing domains, with the action taken. **/
/** Whether to DM users when their messages contain unsafe domains, with the action taken. **/
var notifyUser = true

/**
Expand All @@ -63,12 +73,21 @@ class ExtPhishingBuilder {
*/
var logChannelName = "logs"

/** Register a check that must pass in order for an event handler to run, and for messages to be processed. **/
/**
* Register a bad domain.
*
* The extension will treat them like other unsafe domains, removing messages linking to them.
*/
fun badDomain(domain: String) {
badDomains.add(domain)
}

/** Register a check, which must pass in order for an event handler to run, and for messages to be processed. **/
fun check(check: CheckWithCache<Event>) {
checks.add(check)
}

/** Register checks that must pass in order for an event handler to run, and for messages to be processed. **/
/** Register checks, which must pass in order for an event handler to run, and for messages to be processed. **/
fun check(vararg checkList: CheckWithCache<Event>) {
checks.addAll(checkList)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class PhishingExtension(private val settings: ExtPhishingBuilder) : Extension()
override val name = "phishing"

private val api = PhishingApi(settings.appName)
private val domainCache: MutableSet<String> = mutableSetOf()
private val domainCache: MutableSet<String> = settings.badDomains.toMutableSet()
private val logger = KotlinLogging.logger { }

private var websocket: PhishingWebsocketWrapper = api.websocket(::handleChange)
Expand Down Expand Up @@ -94,7 +94,7 @@ class PhishingExtension(private val settings: ExtPhishingBuilder) : Extension()
}

ephemeralMessageCommand {
name = "Phishing Check"
name = "URL Safety Check"

if (this@PhishingExtension.settings.requiredCommandPermission != null) {
check { hasPermission(this@PhishingExtension.settings.requiredCommandPermission!!) }
Expand All @@ -107,19 +107,19 @@ class PhishingExtension(private val settings: ExtPhishingBuilder) : Extension()
respond {
content = if (matches.isNotEmpty()) {
"⚠️ [Message ${message.id.value}](${message.getJumpUrl()}) " +
"**contains ${matches.size} phishing link/s**."
"**contains ${matches.size} known unsafe link/s**."
} else {
"✅ [Message ${message.id.value}](${message.getJumpUrl()}) " +
"**does not contain any phishing links**."
"**does not contain any known unsafe links**."
}
}
}
}
}

ephemeralSlashCommand(::DomainArgs) {
name = "phishing-check"
description = "Check whether a given domain is a known phishing domain."
name = "url-safety-check"
description = "Check whether a given domain is a known unsafe domain."

if (this@PhishingExtension.settings.requiredCommandPermission != null) {
check { hasPermission(this@PhishingExtension.settings.requiredCommandPermission!!) }
Expand All @@ -128,9 +128,9 @@ class PhishingExtension(private val settings: ExtPhishingBuilder) : Extension()
action {
respond {
content = if (domainCache.contains(arguments.domain.lowercase())) {
"⚠️ `${arguments.domain}` is a known phishing domain."
"⚠️ `${arguments.domain}` is a known unsafe domain."
} else {
"✅ `${arguments.domain}` is not a known phishing domain."
"✅ `${arguments.domain}` is not a known unsafe domain."
}
}
}
Expand All @@ -145,16 +145,16 @@ class PhishingExtension(private val settings: ExtPhishingBuilder) : Extension()
val matches = parseDomains(message.content)

if (matches.isNotEmpty()) {
logger.debug { "Found a message with ${matches.size} phishing domains." }
logger.debug { "Found a message with ${matches.size} unsafe domains." }

if (settings.notifyUser) {
message.kord.launch {
message.author!!.dm {
content = "We've detected that the following message contains a phishing domain. For this " +
content = "We've detected that the following message contains an unsafe domain. For this " +
"reason, **${settings.detectionAction.message}**."

embed {
title = "Phishing domain detected"
title = "Unsafe domain detected"
description = message.content
color = DISCORD_RED

Expand Down Expand Up @@ -186,17 +186,17 @@ class PhishingExtension(private val settings: ExtPhishingBuilder) : Extension()
when (settings.detectionAction) {
DetectionAction.Ban -> {
message.getAuthorAsMemberOrNull()!!.ban {
reason = "Message contained a phishing domain"
reason = "Message linked to an unsafe domain"
}

message.delete("Message contained a phishing domain")
message.delete("Message linked to an unsafe domain")
}

DetectionAction.Delete -> message.delete("Message contained a phishing domain")
DetectionAction.Delete -> message.delete("Message linked to an unsafe domain")

DetectionAction.Kick -> {
message.getAuthorAsMemberOrNull()!!.kick("Message contained a phishing domain")
message.delete("Message contained a phishing domain")
message.getAuthorAsMemberOrNull()!!.kick("Message linked to an unsafe domain")
message.delete("Message linked to an unsafe domain")
}

DetectionAction.LogOnly -> {
Expand Down Expand Up @@ -226,7 +226,7 @@ class PhishingExtension(private val settings: ExtPhishingBuilder) : Extension()
return
}

val matchList = "# Phishing Domain Matches\n\n" +
val matchList = "# Unsafe Domain Matches\n\n" +
"**Total:** ${matches.size}\n\n" +
matches.joinToString("\n") { "* `$it`" }

Expand All @@ -237,7 +237,7 @@ class PhishingExtension(private val settings: ExtPhishingBuilder) : Extension()
)

embed {
title = "Phishing domain detected"
title = "Unsafe domain detected"
description = message.content
color = DISCORD_RED

Expand Down

0 comments on commit 487370b

Please sign in to comment.