Skip to content

Commit 70526ed

Browse files
authored
Create Send-ProtocolHandlerEmailLinks.psm1
1 parent cbbee5e commit 70526ed

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed

Send-ProtocolHandlerEmailLinks.psm1

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# Author: Scott Sutherland, @_nullbind, NetSPI
2+
Function Send-ProtocolHandlerEmailLinks
3+
{
4+
<#
5+
.SYNOPSIS
6+
The script can be used to enumerate local protocol handlers and create sample emails
7+
contain links to the handlers. It is intended to be used for testing email controls
8+
that help prevent phishing.
9+
.PARAMETER TargetEmail
10+
Email address to send generated emails to.
11+
.PARAMETER OutPutFile
12+
File path where the list of protocol handlers with be written to.
13+
.PARAMETER Display only.
14+
Enumerate the protocol handlers and display them, but do not generate emails.
15+
.EXAMPLE
16+
PS C:\> Send-ProtocolHandlerEmailLinks -Verbose -TargetEmail [email protected]
17+
.EXAMPLE
18+
PS C:\> Send-ProtocolHandlerEmailLinks -Verbose -DisplayOnly
19+
.REFERENCES
20+
https://support.microsoft.com/en-us/help/310262/how-to-use-the-microsoft-outlook-object-library-to-send-an-html-format
21+
https://msrc-blog.microsoft.com/2008/12/09/ms08-075-reducing-attack-surface-by-turning-off-protocol-handlers/
22+
https://docs.microsoft.com/en-us/office/vba/api/outlook.application
23+
https://blogs.msdn.microsoft.com/noahc/2006/10/19/register-a-custom-url-protocol-handler/
24+
https://docs.microsoft.com/en-us/windows/win32/shell/app-registration
25+
https://docs.microsoft.com/en-us/windows/win32/shell/fa-intro
26+
https://www.vdoo.com/blog/exploiting-custom-protocol-handlers-in-windows
27+
https://zero.lol/2019-05-22-fun-with-uri-handlers/
28+
#>
29+
[CmdletBinding()]
30+
Param(
31+
[Parameter(Mandatory = $false,
32+
HelpMessage = 'Set the target email address.')]
33+
[string]$TargetEmail,
34+
35+
[Parameter(Mandatory = $false,
36+
HelpMessage = 'Output file path.')]
37+
[string]$OutPutFile = ".\protocolhandlers.csv",
38+
39+
[Parameter(Mandatory = $false,
40+
HelpMessage = 'Only display the protocol handlers')]
41+
[switch]$DisplayOnly
42+
)
43+
44+
Begin
45+
{
46+
# Create datatable for output
47+
$null = $DataTable = New-Object System.Data.DataTable;
48+
$null = $DataTable.Columns.Add("key");
49+
$null = $DataTable.Columns.Add("path");
50+
}
51+
52+
Process
53+
{
54+
Write-Verbose "Enumerating protocol handlers"
55+
56+
# Get protocol handlers
57+
foreach ($Key in Get-ChildItem Microsoft.PowerShell.Core\Registry::HKEY_CLASSES_ROOT)
58+
{
59+
$Path = $Key.PSPath + '\shell\open\command';
60+
$HasURLProtocol = $Key.Property -contains 'URL Protocol';
61+
62+
if(($HasURLProtocol) -and (Test-Path $Path)){
63+
$CommandKey = Get-Item $Path;
64+
$ProtBin = $CommandKey.GetValue("")
65+
$ProtKey = $Key.Name.SubString($Key.Name.IndexOf('\') + 1)
66+
$null = $DataTable.Rows.Add($ProtKey,$ProtBin)
67+
}
68+
}
69+
70+
# Display protocol handler count
71+
$PCount = $DataTable.Rows.Count
72+
Write-Verbose "$PCount protocol handlers found"
73+
74+
# Write list of handlers to a file
75+
$DataTable | Export-Csv -NoTypeInformation "$OutputFile"
76+
Write-Verbose "List of protocol handlers saved to $OutputFile"
77+
78+
# Display list
79+
if($DisplayOnly){
80+
81+
$DataTable
82+
}
83+
84+
# Check if emails should / can be sent
85+
if((!$DisplayOnly) -and ($TargetEmail))
86+
{
87+
88+
# Send emails
89+
Write-Output "$PCount emails are being sent to $TargetEmail"
90+
$DataTable |
91+
Foreach {
92+
93+
# Parse handler and associated executable.
94+
$Thekey = $_.Key
95+
$ThePath = $_.Path
96+
Write-Verbose "Sending $Thekey"
97+
98+
# Sending emails with protocol handler links to target email
99+
$outlook = new-object -com outlook.application -Verbose:$False
100+
$ns = $outlook.GetNameSpace("MAPI");
101+
$mail = $outlook.CreateItem(0)
102+
$mail.subject = "Protocol Handler Test: $Thekey"
103+
$Html = "<HTML>" +
104+
"<HEAD>" +
105+
"<TITLE>$Thekey Test</TITLE>" +
106+
"</HEAD>" +
107+
"<BODY>" +
108+
"Key: $Thekey <br>" +
109+
"Executable: $ThePath <br>" +
110+
"<a href='$Thekey`://testin123'>Click Here Please</a><br>" +
111+
"</BODY>" +
112+
"</HTML>";
113+
$mail.HTMLbody = "$Html"
114+
#$mail.body = "This is text only."
115+
$mail.To = "$TargetEmail"
116+
$mail.Send()
117+
}
118+
}
119+
}
120+
121+
End
122+
{
123+
# Nothing
124+
}
125+
}
126+

0 commit comments

Comments
 (0)