# XSS (Cross Site Scripting)
Ikiwa unavutiwa na **kazi ya uhalifu wa mtandao** na kujaribu kuvunja yasiyoweza kuvunjwa - **tunatafuta wafanyakazi!** (_kuandika na kuzungumza kwa Kiswahili vizuri kunahitajika_).
{% embed url="https://www.stmcyber.com/careers" %}
## Methodology
1. Angalia ikiwa **thamani yoyote unayodhibiti** (_parameta_, _njia_, _vichwa_?, _cookies_?) inarudi **katika** HTML au **inatumiwa** na **JS** code.
2. **Pata muktadha** ambapo inarudi/inatumika.
3. Ikiwa **inarudi**
1. Angalia **ni alama zipi unaweza kutumia** na kulingana na hiyo, andaa payload:
1. Katika **HTML safi**:
1. Je, unaweza kuunda vitambulisho vipya vya HTML?
2. Je, unaweza kutumia matukio au sifa zinazounga mkono `javascript:` itifaki?
3. Je, unaweza kupita kinga?
4. Je, maudhui ya HTML yanatafsiriwa na injini yoyote ya JS upande wa mteja (_AngularJS_, _VueJS_, _Mavo_...), unaweza kutumia [**Client Side Template Injection**](../client-side-template-injection-csti.md).
5. Ikiwa huwezi kuunda vitambulisho vya HTML vinavyotekeleza JS code, unaweza kutumia [**Dangling Markup - HTML scriptless injection**](../dangling-markup-html-scriptless-injection/)?
2. Ndani ya **HTML tag**:
1. Je, unaweza kutoka kwenye muktadha wa HTML safi?
2. Je, unaweza kuunda matukio/mapitio mapya ili kutekeleza JS code?
3. Je, sifa ambapo umekwama inasaidia utekelezaji wa JS?
4. Je, unaweza kupita kinga?
3. Ndani ya **JavaScript code**:
1. Je, unaweza kukwepa ``** lebo za ukurasa wa HTML, ndani ya faili ya `.js` au ndani ya sifa ikitumia **`javascript:`** itifaki:
* Ikiwa linajitokeza kati ya **``** lebo, hata kama ingizo lako liko ndani ya aina yoyote ya nukuu, unaweza kujaribu kuingiza `` na kutoroka kutoka kwenye muktadha huu. Hii inafanya kazi kwa sababu ** kivinjari kitaanza kuchambua lebo za HTML** na kisha yaliyomo, kwa hivyo, hakitagundua kwamba lebo yako ya kuingiza `` iko ndani ya msimbo wa HTML.
* Ikiwa linajitokeza **ndani ya mfuatano wa JS** na hila ya mwisho haifanyi kazi, unahitaji **kutoka** kwenye mfuatano, **kutekeleza** msimbo wako na **kurekebisha** msimbo wa JS (ikiwa kuna kosa lolote, halitatekelezwa):
* `'-alert(1)-'`
* `';-alert(1)//`
* `\';alert(1)//`
* Ikiwa linajitokeza ndani ya maandiko ya kiolezo unaweza **kuingiza maelekezo ya JS** ukitumia sintaksia ya `${ ... }`: `` var greetings = `Hello, ${alert(1)}` ``
* **Unicode encode** inafanya kazi kuandika **msimbo sahihi wa javascript**:
```javascript
\u{61}lert(1)
\u0061lert(1)
\u{0061}lert(1)
```
#### Javascript Hoisting
Javascript Hoisting inahusisha fursa ya **kutangaza kazi, mabadiliko au madarasa baada ya kutumika ili uweze kutumia hali ambapo XSS inatumia mabadiliko au kazi zisizotangazwa.**\
**Angalia ukurasa ufuatao kwa maelezo zaidi:**
{% content-ref url="js-hoisting.md" %}
[js-hoisting.md](js-hoisting.md)
{% endcontent-ref %}
### Javascript Function
Kurasa kadhaa za wavuti zina mwisho ambao **zinakubali kama parameter jina la kazi ya kutekeleza**. Mfano wa kawaida wa kuona katika mazingira halisi ni kitu kama: `?callback=callbackFunc`.
Njia nzuri ya kugundua ikiwa kitu kilichotolewa moja kwa moja na mtumiaji kinajaribu kutekelezwa ni **kubadilisha thamani ya param** (kwa mfano kuwa 'Vulnerable') na kutazama kwenye console kwa makosa kama:
![](<../../.gitbook/assets/image (711).png>)
Iwapo ni hatari, unaweza kuwa na uwezo wa **kuanzisha tahadhari** kwa kutuma tu thamani: **`?callback=alert(1)`**. Hata hivyo, ni kawaida sana kwamba mwisho huu uta **thibitisha maudhui** ili kuruhusu herufi, nambari, nukta na viwango vya chini (**`[\w\._]`**).
Hata hivyo, hata na kikomo hicho bado inawezekana kufanya baadhi ya vitendo. Hii ni kwa sababu unaweza kutumia herufi hizo halali ili **kufikia kipengele chochote katika DOM**:
![](<../../.gitbook/assets/image (747).png>)
Baadhi ya kazi muhimu kwa hili:
```
firstElementChild
lastElementChild
nextElementSibiling
lastElementSibiling
parentElement
```
You can also try to **trigger Javascript functions** directly: `obj.sales.delOrders`.
However, usually the endpoints executing the indicated function are endpoints without much interesting DOM, **kurasa nyingine katika asili hiyo hiyo** will have a **DOM yenye kuvutia zaidi** to perform more actions.
Therefore, in order to **abuse this vulnerability in a different DOM** the **Same Origin Method Execution (SOME)** exploitation was developed:
{% content-ref url="some-same-origin-method-execution.md" %}
[some-same-origin-method-execution.md](some-same-origin-method-execution.md)
{% endcontent-ref %}
### DOM
There is **JS code** that is using **unsafely** some **data controlled by an attacker** like `location.href` . An attacker, could abuse this to execute arbitrary JS code.
{% content-ref url="dom-xss.md" %}
[dom-xss.md](dom-xss.md)
{% endcontent-ref %}
### **Universal XSS**
These kind of XSS can be found **popote**. They not depend just on the client exploitation of a web application but on **muktadha** **wowote**. These kind of **arbitrary JavaScript execution** can even be abuse to obtain **RCE**, **kusoma** **faili** **za kawaida** in clients and servers, and more.\
Some **mfano**:
{% content-ref url="server-side-xss-dynamic-pdf.md" %}
[server-side-xss-dynamic-pdf.md](server-side-xss-dynamic-pdf.md)
{% endcontent-ref %}
{% content-ref url="../../network-services-pentesting/pentesting-web/electron-desktop-apps/" %}
[electron-desktop-apps](../../network-services-pentesting/pentesting-web/electron-desktop-apps/)
{% endcontent-ref %}
## WAF bypass encoding image
![from https://twitter.com/hackerscrolls/status/1273254212546281473?s=21](<../../.gitbook/assets/EauBb2EX0AERaNK (1).jpg>)
## Injecting inside raw HTML
When your input is reflected **ndani ya ukurasa wa HTML** or you can escape and inject HTML code in this context the **kitu** cha kwanza you need to do if check if you can abuse `<` to create new tags: Just try to **reflect** that **char** and check if it's being **HTML encoded** or **deleted** of if it is **reflected without changes**. **Only in the last case you will be able to exploit this case**.\
For this cases also **keep in mind** [**Client Side Template Injection**](../client-side-template-injection-csti.md)**.**\
_**Note: A HTML comment can be closed using**** ****`-->`**** ****or**** ****`--!>`**_
In this case and if no black/whitelisting is used, you could use payloads like:
```html