📌 문제

 

찾는 비밀번호는 inhere 디렉터리 아래의 어떤 파일에 저장되어있다. 그리고 그 파일은 아래의 특징들을 전부가지고 있는 파일이다. 

  • human-readable
  • 1033 bytes in size
  • not executable

 

 

📌 풀이

 

이 문제는 특정한 조건을 가지고 있는 파일을 찾는 문제이다. 

 

 

find 명령어: find [경로] [표현식 1] [표현식 2] ...

 

먼저 find 명령어의 옵션들을 이용하여 조건에 맞는 파일들을 검색한다. 

  • '-type f'로 일반 파일을 검색한 후 grep 명령어를 이용해 파일의 확장자가 .text인 파일을 다시 찾는다. 
  • '-size 1033c'로 1033 바이트 크기의 파일을 찾는다. 
  • '! -executable'로 실행 가능하지 않은 파일을 찾는다.

 

$ find inhere -type f -size 1033c ! -executable -exec file {} + | grep text
find inhere -type f -size 1033c ! -executable -exec file {} \; | grep text

 

 

 -exec  {} \; 와 -exec  {} + 의 차이점 

 

위의 두 명령어의 차이점은 첫번째의 경우, 조건에 따라 검색된 여러개의 파일에 대하여 명령이 각각 한번씩 수행된다는 점이다. 

반대로 두번째 + 명령어를 사용하면, 찾아낸 여러개의 파일을 하나로 생각하여 명령이 한번만 수행된다. 

 

 

실행결과

bandit5@bandit:~/inhere$ find -type f -size 1033c ! -executable -exec file {} \; | grep text
./maybehere07/.file2: ASCII text, with very long lines (1000)

bandit5@bandit:~/inhere$ find -type f -size 1033c ! -executable -exec file {} + | grep text
./maybehere07/.file2: ASCII text, with very long lines (1000)

bandit5@bandit:~$ cat inhere/maybehere07/.file2

 

 

이제 파일의 내용을 읽고 정답을 제출해주면 된다. 

 

 

 

📌 정리

조건에 맞는 파일을 검색하고 싶다면 find 명령어를 사용하자

 

 

 

 

 

 

📌 참고자료 

 

find 명령어 옵션들 

https://m.blog.naver.com/tmk0429/222301447297


find 옵션으로 실행가능한 파일인지 아닌지 확인하는 방법 

https://stackoverflow.com/questions/70539901/how-can-i-find-all-non-executable-files-in-a-directory-in-linux

https://stackoverflow.com/questions/4458120/search-for-executable-files-using-find-command

'OverTheWire > bandit' 카테고리의 다른 글

Bandit Level 4 → Level 5  (0) 2025.01.10
Bandit Level 2 → Level 3  (0) 2025.01.10
Bandit Level 1 → Level 2  (0) 2025.01.10
Bandit Level 0 → Level 1  (0) 2025.01.10
Bandit Level 0  (0) 2025.01.10

📌 문제

다음 단계의 비밀번호는 inhere 디렉터리 안에서 사람이 읽을 수 있는 파일안에 담겨 있다고 한다. 

사람이 읽을 수 있는 파일이란 흔히 문자로 적혀있는 것을 말하는 것이다. 

 

이 문제에서는 파일의 타입을 검사하는 방법에 대해 정리해보자 

 

 

📌 풀이 

bandit4@bandit:~/inhere$ ls
-file00  -file02  -file04  -file06  -file08
-file01  -file03  -file05  -file07  -file09
bandit4@bandit:~/inhere$ cat ./-file00
�p��&�y�,�(jo�.at�:uf�^���@

 

inhere안에 여러가지 파일들이 보인다.  가장 앞에 있는 파일 하나를 읽어보니 우리는 읽을 수 없는 형태가 출력된다. 

아마 이 파일들을 하나씩 출력하다보면 우리가 찾는 비밀번호가 들어있는 파일을 찾을 수 있을 것이다. 

 

하지만 그렇게 찾기에는 너무 번거롭다. 

파일안의 내용을 우리가 읽을 수 있다는 것은 "문자나 숫자"로 이루어져있기 때문이다.  그렇다면 파일의 타입중에서 .txt 또는 .text 형태의 확장자를 가진 파일이 있는지 확인해보면 될 것 같다. 

 

file [파일 이름] 
bandit4@bandit:~/inhere$ file ./*
./-file00: data
./-file01: data
./-file02: data
./-file03: data
./-file04: data
./-file05: data
./-file06: data
./-file07: ASCII text
./-file08: data
./-file09: data

 

file 명령어를 이용하여 파일의 타입을 검사할 수 있다.  특정 파일 확장자만 찾고 싶다면 grep 명령어를 이용하자. 

 

 

문자열 검색 grep , 명령어를 연결해주는 | (파이프) 
$ file ./* | grep *.text

 

 

 

📌 정리 

파일의 타입을 검사하는 명령어는 file [파일이름] 이다. 

'OverTheWire > bandit' 카테고리의 다른 글

Bandit Level 5 → Level 6  (0) 2025.01.11
Bandit Level 2 → Level 3  (0) 2025.01.10
Bandit Level 1 → Level 2  (0) 2025.01.10
Bandit Level 0 → Level 1  (0) 2025.01.10
Bandit Level 0  (0) 2025.01.10

📌 문제

홈 디렉터리 아래에 spaces in this filename 이라는 파일안에 bandit3의 비밀번호가 들어있다고 한다. 

해당 파일을 읽어서 비밀번호를 찾으면 된다. 

이 문제를 통해서 파일 이름에 공백이 있는 경우 파일을 읽는 방법에 대해 알아보자 

 

 

📌 풀이 

 

cat 명령어는 동시에 여러개의 파일을 읽을 수 있고, 각각의 파일 이름을 공백으로 구분한다.

cat [파일이름1] [파일이름2] .....

 

따라서 spaces in this filename 이라는 파일의 이름을 그대로 입력하게 되면, cat은 하나의 파일이 아닌 4개의 파일을 각각 읽으라는 의미로 인식하게 된다. 

bandit2@bandit:~$ ls
spaces in this filename
bandit2@bandit:~$ cat ./spaces in this filename
cat: ./spaces: No such file or directory
cat: in: No such file or directory
cat: this: No such file or directory
cat: filename: No such file or directory

 

그렇게 명령을 입력해보니 역시 space, in , this, filename이라는 파일들은 해당 디렉터리안에 존재하지 않는다고 나온다. 

 

공백이 있는 파일을 읽는 방법은 " " 나 ' ' 안에 파일 이름을 적어주면 된다.

또는 \ 를 사용하여 공백 문자를 무시하도록 해도 된다. 

bandit2@bandit:~$ cat "spaces in this filename"

bandit2@bandit:~$ cat 'spaces in this filename'

bandit2@bandit:~$ cat spaces\ in\ this\ filename

bandit2@bandit:~$ cat ./*

 

 

 

📌 정리

공백이 있는 파일의 이름은 "" , '' 안에 파일이름을 입력하거나 \를 이용하여 특수문자가 무시되도록한다. 

 

 

 

참고자료

https://appuals.com/how-to-handle-passing-filenames-with-spaces-in-bash/

'OverTheWire > bandit' 카테고리의 다른 글

Bandit Level 5 → Level 6  (0) 2025.01.11
Bandit Level 4 → Level 5  (0) 2025.01.10
Bandit Level 1 → Level 2  (0) 2025.01.10
Bandit Level 0 → Level 1  (0) 2025.01.10
Bandit Level 0  (0) 2025.01.10

📌 문제

앞선 문제들과 비슷하게 홈 디렉터리 아래의 - 파일안에 bandit2의 비밀번호가 들어있다고 한다. 

- 파일 안에 있는 비밀번호를 읽으면 된다. 

 

이 문제를 통해 -로 시작하는 파일 읽는 방법에 대해알아보자 

 

 

📌 풀이 

 

파일을 읽는 명령어는 cat이므로 cat [ 파일 이름] 을 이용하여 내용을 읽으려고 했다. 

그런데 그렇게 입력해도 계속 대기만 할 뿐 파일의 내용을 읽지 않는다. 그 이유는 파일의 이름과 관련이 있었다. 

 

파일의 이름이 - 이다. 보통 리눅스의 명령어에서 - 다음에 옵션들을 붙여서 여러가지 기능들을 추가적으로 수행 할 수 있도록 해준다. 

그래서 쉘은 cat 다음에 들어온 - 를 파일의 이름으로 읽는것이 아닌, - 옵션을 위한 특수문자로 인식하여 사용자로부터의 입력을 기다리고 있고 있던 것이다. 

bandit1@bandit:~$ ls
-
bandit1@bandit:~$ cat -
^C

 

 

이러한 파일을 읽기 위한 방법은 파일의 경로를 명시적으로 지정해주거나, 절대경로를 이용하면 된다. 

$ cat ./-

$ cat home/bandit1/-

 

아니면 cat 명령의 입력으로 - 파일을 사용한다.  (입력 리다이렉션) 

$ cat < -

 

 

 

🍪 cat [파일 이름] 과 cat < [파일 이름] 의 차이점

https://askubuntu.com/questions/1190700/what-is-the-difference-between-cat-filename-and-cat-filename

 

What is the difference between "cat < filename" and "cat filename"?

The simplest way to display file contents is using the cat command: cat file.txt I can get the same result using input redirection: cat < file.txt Then, what is the difference between them?

askubuntu.com

 

 

  1. 첫번째의 경우 cat 명령어가 직접 파일을 읽는 것이고, 두번째 명령어는 쉘이 파일을 읽고 cat에게 전달하는 것이다. 
  2. 첫번째 명령의 경우 여러개의 파일을 읽을 수 있지만, 두번째는 한개의 파일만을 읽을 수 있다. 
  3. cat이 파일을 읽는 경우, 파일을 읽을때 마다 새로운 파일 디스크립터를 열기 때문에 파일 디스크립터 한계에 도달하면  "Too many open files" 오류가 발생할 수 있다. 하지만 쉘이 파일을 읽는 경우에는 이미 열려있는 표준 입력을 이용하기 때문에 추가적으로 파일 디스크립터를 사용하지 않는다고 한다. 

 

따라서 대량이 파일을 읽을때는 두번째 명령어가 파일 디스크립터의 소비가 적을 수 있다.

※ 파일 디스크립터?  열린 파일이나 입출력 자원들을 고유하게 식별하고 접근하기 위한 참조값 

 

그리고 이러한 파일 디스크립터는 시스템 혹은 운영체제 별로 최대값이 설정되어있지만, 사용자에 따라 그 값을 바꿀 수 있다. 

https://hunnie.co.kr/61

 

리눅스 오픈 가능한 파일 - Too many open files

"Too many open files" 라는 에러가 발생하였을 때의 조치 방법입니다. 리눅스는 모든 것(파일, 세션 등)을 파일로 취급합니다. 파일 디스크립터는 시스템 에서 파일이나 소켓과 같은 I/O 리소스를 참조

hunnie.co.kr

 

 

 

📌 정리

파일이름에 특수문자가 포함되어있어 읽혀지지 않는다면, 명시적으로 파일의 경로를 작성해주거나 절대경로를 이용하여 파일을 읽자. 또는 입력리다이렉션을 이용해보자 

'OverTheWire > bandit' 카테고리의 다른 글

Bandit Level 5 → Level 6  (0) 2025.01.11
Bandit Level 4 → Level 5  (0) 2025.01.10
Bandit Level 2 → Level 3  (0) 2025.01.10
Bandit Level 0 → Level 1  (0) 2025.01.10
Bandit Level 0  (0) 2025.01.10

📌 문제 설명

bandit0의 홈 디렉터리 아래에 있는 readme 파일 안에 bandit1의 비밀번호가 들어있다고 한다. 

해당 비밀번호를 이용하여 bandit1에 ssh 접속을 하면 된다.

이 문제를 통해 파일을 읽는 방법에 대해 정리해보자

 

 

 

📌 풀이

파일을 읽을때는 cat 명령어를 이용해서 읽으면 된다. 

이렇게 비밀번호를 확인했다면 서버에 bandit1 이라는 유저 아이디로 로그인을 해야하기 때문에, 현재 로그인 되어있는 bandit0에서 로그아웃 해줘야한다. 

bandit0@bandit:~$ ls
readme
bandit0@bandit:~$ cat readme
Congratulations on your first steps into the bandit game!!
Please make sure you have read the rules at https://overthewire.org/rules/
If you are following a course, workshop, walkthrough or other educational activity,
please inform the instructor about the rules as well and encourage them to
contribute to the OverTheWire community so we can keep these games free!

The password you are looking for is: 비밀번호

bandit0@bandit:~$ logout
$ su -  [유저네임]

위 명령어을 입력하면 로그아웃 하지 않아도 바로 사용자를 바꿀순 있지만, 워게임에서는 안될것이기 때문에 재접속을 하자. 

 

터미널을 새로 열어서 bandit1으로 재접속 한다. 

$ ssh bandit1@bandit.labs.overthewire.org -p 2220

                        
                        | |__   __ _ _ __   __| (_) |_
                        | '_ \ / _` | '_ \ / _` | | __|
                        | |_) | (_| | | | | (_| | | |_
                        |_.__/ \__,_|_| |_|\__,_|_|\__|


                      This is an OverTheWire game server.
            More information on http://www.overthewire.org/wargames

bandit1@bandit.labs.overthewire.org's password : 아까 봤던 비밀번호 입력

 

 

 

📌 정리

파일은 cat 명령어를 이용해서 읽는다. 

cat 명령어의 옵션들은 한번씩 읽어보기

https://recipes4dev.tistory.com/177#google_vignette

'OverTheWire > bandit' 카테고리의 다른 글

Bandit Level 5 → Level 6  (0) 2025.01.11
Bandit Level 4 → Level 5  (0) 2025.01.10
Bandit Level 2 → Level 3  (0) 2025.01.10
Bandit Level 1 → Level 2  (0) 2025.01.10
Bandit Level 0  (0) 2025.01.10

📌 문제

이 단계에서는 ssh를 이용하여 원격 컴퓨터에 로그인을 하는 것이다. 

원격 컴퓨터의 hostName은 bandit.labs.overthewire.org , 포트번호는2220 이다. 

접속 아이디는 bandit0, 비밀번호도 bandit0 이다. 

 

이 문제를 통해 ssh란 무엇인지, 그리고 접속을 어떻게 하는지 정리해보자

 

 

📌 풀이

 

내가 해당 서버에 접속해야하는 입장이므로 SSH Client가 되고, bandit.labs.overthewire.org가 SSH Server가 된다. 

윈도우의 경우 원래 ssh기능을 지원하지 않았기 때문에 PuTTy라는 프로그램을 이용하여 ssh 통신을 해야했다. 

 

하지만 마이크로소프트가 OpenSSH를 윈도우에 통합하여 이제는 별도의 프로그램 없이도 ssh접속이 가능해졌다. 

버전차이는 있지만, Window10 이상인 경우 윈도우에서도 ssh 접속이 별도의 프로그램 없이 가능하다. 

 

 

PuTTY

Putty에서 hostName과 Port번호를 설정해준뒤 connet 를 하면 아래와 같은 화면이 나온다.

아래의 화면에서 아이디와 비밀번호인 bandit0를 입력하면 된다. 그럼 접속 성공 

 

 

 

CMD에서 ssh 명령어 

$ ssh bandit0@bandit.labs.overthewire.org -p 2220

 

Putty말고 일반 터미널로 접속하려면 ssh 명령어를 사용해서 접속하면 된다. 

일반 터미널에서 해당 명령어를 작성하면 비밀번호를 물어본다. 이때 동일하게 비밀번호를 입력하면 접속에 성공한다. 

 

 

📌 참고자료

https://chun-chun-123.tistory.com/102

'OverTheWire > bandit' 카테고리의 다른 글

Bandit Level 5 → Level 6  (0) 2025.01.11
Bandit Level 4 → Level 5  (0) 2025.01.10
Bandit Level 2 → Level 3  (0) 2025.01.10
Bandit Level 1 → Level 2  (0) 2025.01.10
Bandit Level 0 → Level 1  (0) 2025.01.10

+ Recent posts