$man dup2
NAME
dup, dup2, dup3 - duplicate a file descriptor
SYNOPSIS
#include <unistd.h>
int dup(int oldfd);
int dup2(int oldfd, int newfd);
dup2() makes newfd be the copy of oldfd, closing newfd first if necessary, but note the following:
* If oldfd is not a valid file descriptor, then the call fails, and newfd is not closed.
* If oldfd is a valid file descriptor, and newfd has the same value as oldfd, then dup2() does nothing, and returns newfd.
After a successful return from one of these system calls, the old and new file descriptors may be used interchangeably.
They refer to the same open file description (see open(2)) and thus share file offset and file status
flags; for example, if the file offset is modified by using lseek(2) on one of the descriptors, the offset is also changed for the other.
dup2( argv1, argv2);
이렇게 있으면
argv1 = argv2라고 생각하면 된다.
mov argv1, argv2다
또 이렇게 되면 file descriptor가 많아 져서 중복된게 입력들이 어디로 갈지 모른다.
그래서 사용 안하느거 닫아주자
#include <stdio.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
//new_fd[0] = stdin back
//new_fd[1] = stdout back
int new_fd[2] = {0};
char buffer[128] = {0};
dup2(stdin, new_fd[0]);
dup2(stdout,new_fd[1]);
// close(stdin);
// close(stdout);
//new_fd[0] = stdin
//read(0,buffer, sizeof(buffer));
read(new_fd[0], buffer, sizeof(buffer));
//new_fd[1] = stdout
//write(0, buffer, sizeof(buffer));
write(new_fd[1],buffer, sizeof(buffer));
printf("#######stdout test#########\n");
write(stdout, buffer, sizeof(buffer));
printf("\n###########################\n");
write(new_fd[1],buffer, sizeof(buffer));
}
위의 코드와 같이 fd를 복사하고 stdout을 닫지 않고 출력해 본다. 그러면 출력 될것 같지만.. 안된다. ㅠ
항상 헷갈릴수 있으니 닫아주고 사용하자.
'Exploit' 카테고리의 다른 글
보호기법 체크하기 (0) | 2016.04.15 |
---|---|
ropsaurox 풀이 (0) | 2016.04.12 |
jmp esp (0) | 2016.03.24 |
peda gdb 사용 url (0) | 2015.10.16 |
python 인자 값 넣기 (0) | 2015.10.15 |