-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathDEV01-Part2.ps1
More file actions
113 lines (91 loc) · 4.07 KB
/
Copy pathDEV01-Part2.ps1
File metadata and controls
113 lines (91 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# Step 1: Instruct the user to complete the manual Python installation steps
Write-Host "Before proceeding, please complete the following manual steps:"
Write-Host "1. Download Python from: https://www.python.org/downloads/ by clicking 'Download Python 3.13.2'."
Write-Host "2. Double-click the installer."
Write-Host "3. Click 'Add python.exe to PATH' and click 'Install Now.'"
Write-Host "4. Once the installation is complete, close the PowerShell window, reopen a new Non-Elevated PowerShell session and run the script again."
# Immediately check if Python is properly installed by verifying its version string.
try {
$pythonVersionOutput = & python --version 2>&1
# Use a regex that matches a proper version string like "Python 3.13.2"
if ($pythonVersionOutput -notmatch "^Python\s+\d+\.\d+\.\d+") {
exit
}
} catch {
exit
}
# Step 2: Automatically install aiosmtpd via pip
Write-Host "Installing aiosmtpd via pip..."
python -m pip install aiosmtpd
# Step 3: Prompt the user for the listening IP
$ip = Read-Host "Enter your listening IP (e.g. 10.10.14.100)"
# Step 4: Define the Python SMTP server script as a multiline string
$pythonScript = @'
import asyncio
import os
import subprocess
from aiosmtpd.controller import Controller
from email.parser import BytesParser
from email.policy import default
class CustomSMTPHandler:
async def handle_DATA(self, server, session, envelope):
print(f"Connection from: {session.peer}")
print(f"Message from: {envelope.mail_from}")
print(f"Message to: {envelope.rcpt_tos}")
print("Message content:")
try:
# Parse the message
msg = BytesParser(policy=default).parsebytes(envelope.content)
# Walk through each MIME part
for part in msg.walk():
# Skip if not an attachment
if part.get_content_disposition() != 'attachment':
continue
filename = part.get_filename()
if not filename:
continue
file_data = part.get_payload(decode=True)
with open(filename, 'wb') as f:
f.write(file_data)
print(f"[+] Attachment saved to disk: {filename}")
# If it's a .lnk file, execute it automatically
if filename.lower().endswith('.lnk'):
print(f"[!] Executing shortcut: {filename}")
try:
os.startfile(filename)
except Exception as ex:
print(f"Error executing {filename}: {ex}")
print("End of message")
except Exception as e:
print(f"Error parsing email: {e}")
return '250 Message accepted for delivery'
if __name__ == "__main__":
handler = CustomSMTPHandler()
try:
controller_local = Controller(handler, hostname='127.0.0.1', port=25)
controller_local.start()
print("SMTP server is running on localhost (127.0.0.1) on port 25...")
except Exception as e:
print(f"Failed to bind to localhost: {e}")
try:
controller_external = Controller(handler, hostname='EXTERNAL_IP_PLACEHOLDER', port=25)
controller_external.start()
print("SMTP server is running on external IP (EXTERNAL_IP_PLACEHOLDER) on port 25...")
except Exception as e:
print(f"Failed to bind to external IP: {e}")
try:
asyncio.run(asyncio.Event().wait())
except KeyboardInterrupt:
print("Shutting down...")
if 'controller_local' in globals():
controller_local.stop()
if 'controller_external' in globals():
controller_external.stop()
'@
# Step 5: Replace the placeholder with the actual IP provided by the user
$pythonScript = $pythonScript -replace 'EXTERNAL_IP_PLACEHOLDER', $ip
# Step 6: Create the receiver.py file with the updated Python script
Set-Content -Path "receiver.py" -Value $pythonScript
Write-Host "receiver.py created successfully with external IP set to $ip."
# Step 7: Setup Complete
Write-Host "Script completed successfully."