# Leaky Pipes \[Pwn]



<figure><img src="https://3959829653-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKsbQ7f9jhHpgfkJbVM1s%2Fuploads%2FjUwhrLCvJrWGk0Y8uy4y%2Fimage.png?alt=media&#x26;token=f2ad5511-7fca-4cd6-9e06-b60473a5fceb" alt="" width="368"><figcaption></figcaption></figure>



The challenge provided us with a `nc` connection to the server hosting the binary and the binary itself. So, without launching Ghidra or IDA, we can use the following tool to disassemble the binary.

{% embed url="https://dogbolt.org/" %}

The disassembled file is as follows:

```c
int __cdecl readflag(char *s, int n)
{
  FILE *stream; // [esp+Ch] [ebp-Ch]

  stream = fopen("./flag.txt", "r");
  if ( !stream )
  {
    printf("%s %s\n", "Please create 'flag.txt' in this directory with your", "own debugging flag.");
    exit(0);
  }
  fgets(s, n, stream);
  return fclose(stream);
}

//----- (08049361) --------------------------------------------------------
int vuln()
{
  char format[128]; // [esp+0h] [ebp-C8h] BYREF
  char s[68]; // [esp+80h] [ebp-48h] BYREF

  readflag(s, 64);
  printf("Tell me your secret so I can reveal mine ;) >> ");
  __isoc99_scanf("%127s", format);
  puts("Here's your secret.. I ain't telling mine :p");
  printf(format);
  return putchar(10);
}
// 80491A0: using guessed type int __cdecl __isoc99_scanf(_DWORD, _DWORD);
// 8049361: using guessed type char s[68];

//----- (080493ED) --------------------------------------------------------
int __cdecl main(int argc, const char **argv, const char **envp)
{
  __gid_t v4; // [esp+0h] [ebp-Ch]

  setvbuf(stdout, 0, 2, 0);
  v4 = getegid();
  setresgid(v4, v4, v4);
  vuln();
  return 0;
}
```

The vulnerability present in the binary is within the `printf` function, the complete detail on this vulnerability can be found on my old blog here:

{% embed url="https://medium.com/@j1gs4w/picoctf-2021-stonks-beaeef7447ae" %}

<figure><img src="https://3959829653-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKsbQ7f9jhHpgfkJbVM1s%2Fuploads%2FeaGRqm7GUC0qiVyl5Tvg%2Fimage.png?alt=media&#x26;token=5403bc95-7aae-4b4e-adc2-f5463711ad04" alt=""><figcaption></figcaption></figure>

With that explanation, no script is needed here. By connecting to the instance given to us, we can just insert a 100 `%x`'s and expect the binary to leak out information from the stack.



<figure><img src="https://3959829653-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKsbQ7f9jhHpgfkJbVM1s%2Fuploads%2FzdEwTWGNWgu62vOLKSEg%2Fimage.png?alt=media&#x26;token=0a038146-cc59-477f-a7ff-cd2d23e70009" alt=""><figcaption></figcaption></figure>

Now, all we need to do is to swap the endianness of the output and convert the output from hex. However, no flag appeared. So, referring to my old blog post, there may be some "bad bytes".



<figure><img src="https://3959829653-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKsbQ7f9jhHpgfkJbVM1s%2Fuploads%2FbhuVYi9QJApni3jXun3J%2Fimage.png?alt=media&#x26;token=38bba9cd-9759-4658-845e-4beb7bcd4879" alt=""><figcaption></figcaption></figure>

By removing some of the characters from the start of the hex output, we finally got the flag!

```
flag: OSCTF{F0rm4t_5tr1ngs_l3ak4g3_l0l}
```
