Discord Scanner Plugin
Relevant source files
The following files were used as context for generating this wiki page:
The Discord Scanner Plugin is a specialized module within the 2ms (Too Many Secrets) tool designed to scan Discord chat application history for sensitive information, such as secrets, authentication keys, and passwords stored in unencrypted text. It leverages the Discord API to traverse guilds (servers), channels, and message threads to identify potential security risks.
The plugin functions by establishing a session with Discord using a provided authentication token, fetching message content based on user-defined constraints (such as message count or time duration), and converting these messages into standardized ISourceItem objects for the scanning engine to process.
Sources: README.md:12-16, plugins/discord.go:42-46, plugins/discord.go:276-285
Architecture and Data Flow
The Discord plugin follows a hierarchical retrieval pattern: Guilds -> Channels -> Messages -> Threads. It uses Go's concurrency primitives (sync.WaitGroup and goroutines) to scan multiple guilds and channels in parallel.
Data Flow Overview
The diagram above illustrates the high-level flow from command execution to secret detection in the engine.
Sources: plugins/discord.go:81-91, plugins/discord.go:107-116, engine/engine.go:103-110
Command Configuration
The plugin is integrated into the 2ms CLI and accepts several flags to define the scope of the scan.
Configuration Flags
| Flag | Argument | Default | Description |
|---|---|---|---|
--token | string | - | Required. Discord authentication token. |
--server | strings | - | Required. Discord server (Guild) IDs or names to scan. |
--channel | strings | All channels | Specific Discord channel IDs/names to scan. |
--duration | duration | 336h (14 days) | The time interval to scan backwards from now. |
--messages-count | int | 0 (Unlimited) | Maximum number of messages to scan per channel. |
Sources: plugins/discord.go:17-25, plugins/discord.go:50-65, README.md:189-200
Core Components
1. Initialization and Session Management
The plugin uses the discordgo library to manage sessions. During initialization, it verifies that either a message count or a duration limit is set to avoid infinite or excessively large scans.
func (p *DiscordPlugin) getDiscordReady() (err error) {
p.Session, err = discordgo.New(p.Token)
if err != nil {
return err
}
// ... handles state and opening session
}
Sources: plugins/discord.go:121-135, plugins/discord.go:94-105
2. Message and Thread Retrieval
The plugin iterates through channels and fetches messages in batches of 100. It includes logic to handle Discord threads by recursively calling the message retrieval function for any message that contains a Thread object.
The sequence diagram shows the iterative process of fetching messages and handling nested threads.
Sources: plugins/discord.go:214-230, plugins/discord.go:234-274
3. Permission Handling
Before scanning a channel, the plugin checks the bot/user permissions. It specifically verifies PermissionViewChannel and ensures the channel type is ChannelTypeGuildText. If the plugin encounters a 50001 (Missing Access) error, it skips the channel gracefully.
Sources: plugins/discord.go:193-210
Data Transformation
Messages retrieved from Discord are converted into a standardized item struct that implements the ISourceItem interface required by the 2ms engine.
Item Structure Mapping
| Field | Source / Logic | Example |
|---|---|---|
| Content | message.Content | "My secret key is..." |
| ID | discord-<guildId>-<channelId>-<messageId> | discord-123-456-789 |
| Source | URL format for deep linking | https://discord.com/channels/<guild>/<channel>/<msg> |
Sources: plugins/discord.go:276-285, plugins/plugins.go:19-24
Implementation Details
Message Filtering Logic
The message retrieval loop in getMessages terminates based on three conditions:
- Time Limit: When
time.Since(message.Timestamp)exceeds theBackwardDuration. - Count Limit: When the number of collected messages reaches the
Countdefined by the user. - End of History: When the Discord API returns an empty batch.
Sources: plugins/discord.go:247-260
Error Handling
The plugin utilizes a dedicated error channel (errChan) to report issues back to the main engine without halting the entire scanning process. Errors include session timeouts, API request failures, and permission denials.
Sources: plugins/discord.go:81-88, plugins/discord.go:137-145
Conclusion
The Discord Scanner Plugin provides a robust mechanism for auditing internal communications within Discord servers. By integrating with the standard 2ms engine, it allows organizations to apply the same secret-detection rules used for code repositories and file systems to their chat history, ensuring a comprehensive security posture across different platforms.
Sources: cmd/main.go:60-67, README.md:12-18