---
title: "19. Attacking applications that connect to services"
date: 2025-01-21
draft: false
---

Apps that are connected to services often include connection strings that can be leaked if not protected sufficiently. 
We can enumerate these. 

## ELF Executable Examination 
`octopus_checker` binary is found on a remote machine during testing, for example. Running this application locally reveals that it connects to database instances in order to verify they are available. 

```shell-session
stejwes@htb[/htb]$ ./octopus_checker 

Program had started..
Attempting Connection 
Connecting ... 

The driver reported the following diagnostics whilst running SQLDriverConnect

01000:1:0:[unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQL Server' : file not found
connected
```

Binary likely connects using a SQL connection string. Tools like `PEDA` can be used to examine the file. (Python Exploit Development Assistance for GDB)
- Ext of standard GNU debugger (GDB) used for debugging c/C++ progs. 
- GDB is a cli tool that lets you step through code, set breakpoints, and examine/change variables. 


```shell-session
stejwes@htb[/htb]$ gdb ./octopus_checker

GNU gdb (Debian 9.2-1) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./octopus_checker...
(No debugging symbols found in ./octopus_checker)
```

Once bin is loaded set `disassembly-flavor` to display style of code, and proceed with disassembly. 

```assembly
gdb-peda$ set disassembly-flavor intel
gdb-peda$ disas main
```

This will reveal several call instructions that point to addresses containing strings. They appear to be sections of SQL conn string but the sedctions are not in order - and the `endianness` entails the string text is reversed. `endianness` defines ORDER that the bytes are READ in different architectures. Further in the function we'll see a call to `SQLDriverConnect` 
```assembly
   0x00005555555555ff <+425>:	mov    esi,0x0
   0x0000555555555604 <+430>:	mov    rdi,rax
   0x0000555555555607 <+433>:	call   0x5555555551b0 <SQLDriverConnect@plt>
   0x000055555555560c <+438>:	add    rsp,0x10
   0x0000555555555610 <+442>:	mov    WORD PTR [rbp-0x4b4],ax
```


Adding a breakpoint at this memory address and running the program again we can reveal a SQL connection string in the RDX register address, containing creds for local db instance. 

```assembly
gdb-peda$ b *0x5555555551b0

Breakpoint 1 at 0x5555555551b0

gdb-peda$ run
```


```assembly
Starting program: /htb/rollout/octopus_checker 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Program had started..
Attempting Connection 
[----------------------------------registers-----------------------------------]
RAX: 0x55555556c4f0 --> 0x4b5a ('ZK')
RBX: 0x0 
RCX: 0xfffffffd 
RDX: 0x7fffffffda70 ("DRIVER={ODBC Driver 17 for SQL Server};SERVER=localhost, 1401;UID=username;PWD=password;")
RSI: 0x0 
RDI: 0x55555556c4f0 --> 0x4b5a ('ZK')

<SNIP>
```

---

## DLL File examination 

DLL = **Dynamically Linked Library** 
- Contain code from other progs while they are running.
- `MultimasterAPI.dll` in this example is found on a remote machine during enumeration process. Examination reveals it is a .NET assembly. 

```powershell-session
C:\> Get-FileMetaData .\MultimasterAPI.dll

<SNIP>
M .NETFramework,Version=v4.6.1 TFrameworkDisplayName.NET Framework 4.6.1    api/getColleagues        ! htt
p://localhost:8081*POST         Ò^         øJ  ø,  RSDSœ»¡ÍuqœK£"Y¿bˆ   C:\Users\Hazard\Desktop\Stuff\Multimast
<SNIP>
```

Using the debugger and `.NET` assembly editor `dnSpy` can view source code directly. dnspy allows reading/editing/debugging source  of a .net assembly (C# and vis basic) 

MultimasterAPI.Controllers -> ColleagueController reveals a database connection string containing a password. 

```bash
htb-student@htb:~$ gdb ./octopus_checker        
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2                                                                
Copyright (C) 2020 Free Software Foundation, Inc.                                                                                                                                                                  
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>                            
This is free software: you are free to change and redistribute it.                              
There is NO WARRANTY, to the extent permitted by law.                                                                                                                                                              
Type "show copying" and "show warranty" for details.                                                     
This GDB was configured as "x86_64-linux-gnu".                                                           
Type "show configuration" for configuration details.                                            
For bug reporting instructions, please see:                                                                                                                                                                        
<http://www.gnu.org/software/gdb/bugs/>.                                                                                                                                                                           
Find the GDB manual and other documentation resources online at:                                         
    <http://www.gnu.org/software/gdb/documentation/>.                                                    
                                                                                                         
For help, type "help".                                                                                   
Type "apropos word" to search for commands related to "word"...     
Reading symbols from ./octopus_checker...                                                                
(No debugging symbols found in ./octopus_checker)                                                        
gdb-peda$ set disassembly-flavor intel                                                                   
gdb-peda$ disas main                                                                                     
Dump of assembler code for function main:                                                                                                                                                                          
   0x0000000000001456 <+0>:     endbr64                                                                  
   0x000000000000145a <+4>:     push   rbp                                                               
   0x000000000000145b <+5>:     mov    rbp,rsp                                                                                                                                                                     
   0x000000000000145e <+8>:     push   rbx                                                               
   0x000000000000145f <+9>:     sub    rsp,0x4b8                                                         
   0x0000000000001466 <+16>:    mov    rax,QWORD PTR fs:0x28                                             
   0x000000000000146f <+25>:    mov    QWORD PTR [rbp-0x18],rax                                          
   0x0000000000001473 <+29>:    xor    eax,eax                                                                                                                                                                     
   0x0000000000001475 <+31>:    lea    rsi,[rip+0xbe5]        # 0x2061
   0x000000000000147c <+38>:    lea    rdi,[rip+0x2bbd]        # 0x4040 <_ZSt4cout@@GLIBCXX_3.4>         
   0x0000000000001483 <+45>:    call   0x11a0 <_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@plt>                                                                                                        
   0x0000000000001488 <+50>:    mov    rdx,rax                                                           
   0x000000000000148b <+53>:    mov    rax,QWORD PTR [rip+0x2b3e]        # 0x3fd0                        
   0x0000000000001492 <+60>:    mov    rsi,rax                                                                                                                                                                     
   0x0000000000001495 <+63>:    mov    rdi,rdx                                                           
   0x0000000000001498 <+66>:    call   0x11c0 <_ZNSolsEPFRSoS_E@plt>   
   0x000000000000149d <+71>:    lea    rax,[rbp-0x4b0]         
   0x00000000000014a4 <+78>:    mov    rdx,rax                                                                                                                                                                     
   0x00000000000014a7 <+81>:    mov    esi,0x0                                                                                                                                                                     
   0x00000000000014ac <+86>:    mov    edi,0x1                                                           
   0x00000000000014b1 <+91>:    call   0x1170 <SQLAllocHandle@plt>                   
   0x00000000000014ac <+86>:    mov    edi,0x1                                                                                                                                                  16:52:42 [135/1824]
   0x00000000000014b1 <+91>:    call   0x1170 <SQLAllocHandle@plt>                                                                                                                                                 
   0x00000000000014b6 <+96>:    mov    rax,QWORD PTR [rbp-0x4b0]                                         
   0x00000000000014bd <+103>:   mov    ecx,0x0                                                           
   0x00000000000014c2 <+108>:   mov    edx,0x3  
   0x00000000000014c7 <+113>:   mov    esi,0xc8 
   0x00000000000014cc <+118>:   mov    rdi,rax                                                           
   0x00000000000014cf <+121>:   call   0x1230 <SQLSetEnvAttr@plt>                                                                                                                                                  
   0x00000000000014d4 <+126>:   mov    rax,QWORD PTR [rbp-0x4b0]                                         
   0x00000000000014db <+133>:   lea    rdx,[rbp-0x4a8]                                          
   0x00000000000014e2 <+140>:   mov    rsi,rax                                                                                                                                                                     
   0x00000000000014e5 <+143>:   mov    edi,0x2                                                           
   0x00000000000014ea <+148>:   call   0x1170 <SQLAllocHandle@plt>                                       
   0x00000000000014ef <+153>:   lea    rsi,[rip+0xb81]        # 0x2077                          
   0x00000000000014f6 <+160>:   lea    rdi,[rip+0x2b43]        # 0x4040 <_ZSt4cout@@GLIBCXX_3.4>                                                                                                                   
   0x00000000000014fd <+167>:   call   0x11a0 <_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@plt>                                                                                                        
   0x0000000000001502 <+172>:   mov    rdx,rax                                                           
   0x0000000000001505 <+175>:   mov    rax,QWORD PTR [rip+0x2ac4]        # 0x3fd0                        
   0x000000000000150c <+182>:   mov    rsi,rax                                                           
   0x000000000000150f <+185>:   mov    rdi,rdx                                                           
   0x0000000000001512 <+188>:   call   0x11c0 <_ZNSolsEPFRSoS_E@plt>
   0x0000000000001517 <+193>:   movabs rax,0x7b3d524556495244                                            
   0x0000000000001521 <+203>:   movabs rdx,0x697244204342444f                                            
   0x000000000000152b <+213>:   mov    QWORD PTR [rbp-0x480],rax                                         
   0x0000000000001532 <+220>:   mov    QWORD PTR [rbp-0x478],rdx                                         
   0x0000000000001539 <+227>:   movabs rax,0x6620373120726576                                                                                                                                                      
   0x0000000000001543 <+237>:   movabs rdx,0x53204c515320726f                                            
   0x000000000000154d <+247>:   mov    QWORD PTR [rbp-0x470],rax                                         
   0x0000000000001554 <+254>:   mov    QWORD PTR [rbp-0x468],rdx                                                                                                                                                   
   0x000000000000155b <+261>:   movabs rax,0x533b7d7265767265                                            
   0x0000000000001565 <+271>:   movabs rdx,0x6f6c3d5245565245                                            
   0x000000000000156f <+281>:   mov    QWORD PTR [rbp-0x460],rax                                         
   0x0000000000001576 <+288>:   mov    QWORD PTR [rbp-0x458],rdx                                         
   0x000000000000157d <+295>:   movabs rax,0x2c74736f686c6163                                                                                                                                                      
   0x0000000000001587 <+305>:   movabs rdx,0x49553b3130343120         
   0x0000000000001591 <+315>:   mov    QWORD PTR [rbp-0x450],rax                                         
   0x0000000000001598 <+322>:   mov    QWORD PTR [rbp-0x448],rdx                                                                                                                                                   
   0x000000000000159f <+329>:   movabs rax,0x4457503b41533d44                                            
   0x00000000000015a9 <+339>:   movabs rdx,0x7263335374304e3d                                            
   0x00000000000015b3 <+349>:   mov    QWORD PTR [rbp-0x440],rax                                                                                                                                                   
   0x00000000000015ba <+356>:   mov    QWORD PTR [rbp-0x438],rdx                                         
   0x00000000000015c1 <+363>:   mov    DWORD PTR [rbp-0x430],0x3b217433
   0x00000000000015cb <+373>:   mov    BYTE PTR [rbp-0x42c],0x0
   0x00000000000015d2 <+380>:   mov    rax,QWORD PTR [rbp-0x4a8]                                                                                                                                                   
   0x00000000000015d9 <+387>:   lea    rsi,[rbp-0x420]                                                                                                                                                             
   0x00000000000015e0 <+394>:   lea    rdx,[rbp-0x480]                                                   
   0x00000000000015e7 <+401>:   push   0x0                                            
     0x00000000000016c2 <+620>:   lea    rdi,[rip+0x2977]        # 0x4040 <_ZSt4cout@@GLIBCXX_3.4>                                                                                                 16:52:49 [45/1824]
   0x00000000000016c9 <+627>:   call   0x11a0 <_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@plt>                                                                                                        
   0x00000000000016ce <+632>:   mov    rdx,rax                                                           
   0x00000000000016d1 <+635>:   mov    rax,QWORD PTR [rip+0x28f8]        # 0x3fd0                        
   0x00000000000016d8 <+642>:   mov    rsi,rax  
   0x00000000000016db <+645>:   mov    rdi,rdx  
   0x00000000000016de <+648>:   call   0x11c0 <_ZNSolsEPFRSoS_E@plt>
   0x00000000000016e3 <+653>:   jmp    0x170d <main+695>                                                                                                                                                           
   0x00000000000016e5 <+655>:   lea    rsi,[rip+0x9d3]        # 0x20bf                                   
   0x00000000000016ec <+662>:   lea    rdi,[rip+0x294d]        # 0x4040 <_ZSt4cout@@GLIBCXX_3.4>
   0x00000000000016f3 <+669>:   call   0x11a0 <_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@plt>                                                                                                        
   0x00000000000016f8 <+674>:   mov    rdx,rax                                                           
   0x00000000000016fb <+677>:   mov    rax,QWORD PTR [rip+0x28ce]        # 0x3fd0
   0x0000000000001702 <+684>:   mov    rsi,rax                                                           
   0x0000000000001705 <+687>:   mov    rdi,rdx                                                                                                                                                                     
   0x0000000000001708 <+690>:   call   0x11c0 <_ZNSolsEPFRSoS_E@plt>                                     
   0x000000000000170d <+695>:   mov    eax,0x0                                                           
   0x0000000000001712 <+700>:   mov    rcx,QWORD PTR [rbp-0x18]                                          
   0x0000000000001716 <+704>:   xor    rcx,QWORD PTR fs:0x28                                             
   0x000000000000171f <+713>:   je     0x1761 <main+779>            
   0x0000000000001721 <+715>:   jmp    0x175c <main+774>        
   0x0000000000001723 <+717>:   endbr64                                                                  
   0x0000000000001727 <+721>:   mov    rbx,rax                                                           
   0x000000000000172a <+724>:   lea    rax,[rbp-0x4a0]            
   0x0000000000001731 <+731>:   mov    rdi,rax                                                           
   0x0000000000001734 <+734>:   call   0x1160 <_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev@plt>                                                                                                      
   0x0000000000001739 <+739>:   jmp    0x1742 <main+748>              
   0x000000000000173b <+741>:   endbr64           
   0x000000000000173f <+745>:   mov    rbx,rax                                                                                                                                                                     
   0x0000000000001742 <+748>:   lea    rax,[rbp-0x4b7]
   0x0000000000001749 <+755>:   mov    rdi,rax                                                           
   0x000000000000174c <+758>:   call   0x11d0 <_ZNSaIcED1Ev@plt>                                         
   0x0000000000001751 <+763>:   mov    rax,rbx                                                           
   0x0000000000001754 <+766>:   mov    rdi,rax                                                                                                                                                                     
   0x0000000000001757 <+769>:   call   0x1210 <_Unwind_Resume@plt>
   0x000000000000175c <+774>:   call   0x11e0 <__stack_chk_fail@plt>                                     
   0x0000000000001761 <+779>:   mov    rbx,QWORD PTR [rbp-0x8]                                                                                                                                                     
   0x0000000000001765 <+783>:   leave                                                                    
   0x0000000000001766 <+784>:   ret                                                                      
End of assembler dump.                                                                                                                                                                                             
gdb-peda$ b SQLDriverConnect                                                                             
Breakpoint 1 at 0x11b0                                                                                   
gdb-peda$ run                                                                                            
Starting program: /home/htb-student/octopus_checker                                                                                                                                                                
[Thread debugging using libthread_db enabled]                                                                                                                                                                      
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".                      
Program had started..                                                                                 
Attempting Connection 
[----------------------------------registers-----------------------------------]
RAX: 0x55555556c4f0 --> 0x4b5a ('ZK')
RBX: 0x5555555557d0 (<__libc_csu_init>: endbr64)
RCX: 0xfffffffd 
RDX: 0x7fffffffdf30 ("DRIVER={ODBC Driver 17 for SQL Server};SERVER=localhost, 1401;UID=SA;PWD=N0tS3cr3t!;")
RSI: 0x0 
RDI: 0x55555556c4f0 --> 0x4b5a ('ZK')
RBP: 0x7fffffffe3b0 --> 0x0 
RSP: 0x7fffffffded8 --> 0x55555555560c (<main+438>:     add    rsp,0x10)
RIP: 0x7ffff7d61c20 (<SQLDriverConnect>:        push   r15)
R8 : 0x7fffffffdf90 --> 0x0 
R9 : 0x400 
R10: 0xfffffffffffff8ff 
R11: 0x246 
R12: 0x555555555240 (<_start>:  endbr64)
R13: 0x7fffffffe4a0 --> 0x1 
R14: 0x0 
R15: 0x0
EFLAGS: 0x213 (CARRY parity ADJUST zero sign trap INTERRUPT direction overflow)
[-------------------------------------code-------------------------------------]
   0x7ffff7d61c15 <__handle_attr_extensions_cs+149>:    pop    rbp
   0x7ffff7d61c16 <__handle_attr_extensions_cs+150>:    ret    
   0x7ffff7d61c17:      nop    WORD PTR [rax+rax*1+0x0]
=> 0x7ffff7d61c20 <SQLDriverConnect>:   push   r15
   0x7ffff7d61c22 <SQLDriverConnect+2>: push   r14
   0x7ffff7d61c24 <SQLDriverConnect+4>: mov    r14d,ecx
   0x7ffff7d61c27 <SQLDriverConnect+7>: push   r13
   0x7ffff7d61c29 <SQLDriverConnect+9>: mov    r13,r8
[------------------------------------stack-------------------------------------]
0000| 0x7fffffffded8 --> 0x55555555560c (<main+438>:    add    rsp,0x10)
0008| 0x7fffffffdee0 --> 0x7fffffffdefa --> 0xb3b000007ffff7fe 
0016| 0x7fffffffdee8 --> 0x0 
0024| 0x7fffffffdef0 --> 0x7fffffffe2b0 --> 0x7ffff7d4cf74 --> 0x2 
0032| 0x7fffffffdef8 --> 0x7ffff7fe7c3e (<_dl_runtime_resolve_xsavec+126>:      mov    r11,rax)
0040| 0x7fffffffdf00 --> 0x55555556b3b0 --> 0x4b59 ('YK')
0048| 0x7fffffffdf08 --> 0x55555556c4f0 --> 0x4b5a ('ZK')
0056| 0x7fffffffdf10 --> 0x7ffff7d4c000 --> 0x7ffff7d46280 --> 0x7ffff7cb2cb0 (<_ZNSt7num_getIwSt19istreambuf_iteratorIwSt11char_traitsIwEEED2Ev>:      endbr64)
[------------------------------------------------------------------------------]
Legend: code, data, rodata, value

Breakpoint 1, SQLDriverConnect (hdbc=0x55555556c4f0, hwnd=0x0, conn_str_in=0x7fffffffdf30 "DRIVER={ODBC Driver 17 for SQL Server};SERVER=localhost, 1401;UID=SA;PWD=N0tS3cr3t!;", len_conn_str_in=0xfffd, 
    conn_str_out=0x7fffffffdf90 "", conn_str_out_max=0x400, ptr_conn_str_out=0x7fffffffdefa, driver_completion=0x0) at SQLDriverConnect.c:686
686     SQLDriverConnect.c: No such file or directory.

```

Specifying the memory address as seen in the module didnt work so I asked chatgpt and it said setting the breakpoint to `b SQLDriverConnect` would work fine. 

(the line `   0x0000555555555607 <+433>:	call   0x5555555551b0 <SQLDriverConnect@plt>` isnt in my output above so its mostly useless but assume its there)
