[BinaryHack#11] 실행 파일에 데이터 삽입하기 (64bits)

‘Binary Hacks’의 hack #11에는 objcopy를 이용해서 실행파일에 binary를 추가하는 방법이 나온다. 이것을 따라 64 bits machine에서 build하면 i386:x86-64 출력물과 i386 출력물이 incompatible 해서 link할 수 없다는 오류가 발생한다.

$> objcopy -I binary -O elf32-i386 -B i386 binary.bmp binary.o
$> gcc -c main.c
$> gcc -o program main.o binary.o
/usr/bin/ld: i386 architecture of input file 'binary.o' is incompatible with i386:x86-64 output
collect2: error: ld returned 1 exit status

gcc로 만들어진 main.o는 ELF-64 format인데 objcopy로 생성한 ELF-32 format이어서 함께 link할 수 없다는 것이다. 이 문제는 동일한 ELF format이 되도록 하면 해결된다. 즉, gcc로 compile한 결과물이 objcopy의 결과물인 ELF-32와 동일하게 되도록 하거나 objcopy의 결과 물이 main.o와 동일한 ELF-64가 되도록 해야한다.

첫번째 방법 – ELF-32 format으로 object를 compile
gcc에 -m32 option을 추가하면 ELF32로 compile된 object file을 얻을 수 있다. 이것을 objcopy의 결과 물과 link한다.

$> objcopy -I binary -O elf32-i386 -B i386 binary.bmp binary.o
$> gcc -m32 -c main.c
$> gcc -m32 -o program main.o binary.o

두번째 방법 – ELF-64 format으로 binary를 생성
objcopy가 ELF64를 생성하도록 하면 gcc로 compile한 다른 object file들과 link할 수 있다.

$> objcopy -I binary -O elf64-x86-64 -B i386 binary.bmp binary.o
$> gcc -c main.c
$> gcc -o program main.o binary.o

 

* objcopy로 생성된 ELF object에서 담고 있는 symbol의 목록은 nm 명령어나 readelf -s option으로 볼 수 있다.

$> nm binary.o
00000000002fd036 D _binary_binary_bmp_end
00000000002fd036 A _binary_binary_bmp_size
0000000000000000 D _binary_binary_bmp_start

$>readelf -s binary.o
Symbol table '.symtab' contains 5 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 SECTION LOCAL  DEFAULT    1 
     2: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT    1 _binary_binary_bmp_start
     3: 00000000002fd036     0 NOTYPE  GLOBAL DEFAULT    1 _binary_binary_bmp_end
     4: 00000000002fd036     0 NOTYPE  GLOBAL DEFAULT  ABS _binary_binary_bmp_size

 

bash에서 자동완성할 때 공백 문자 들어가는 문제

언제부터 였는지는 모르겠지만, ubuntu (12.04)의 bash shell에서 자동완성을 하려고 <tab> key를 누르면 공백문자가 하나씩 추가되는 문제가 생겼다. 여러 단계로 되어 있는 file에 접근하려고 할 때 매번 <tab> key를 누르고 <backspace>로 공백문자를 지워줘야 하기 때문에 무척 불편했는데 좀 찾아보니 bash_completion file을 수정해서 이 문제를 해결 할 수 있었다.

sudo vi /etc/bash_completion
# makeinfo and texi2dvi are defined elsewhere.
for i in a2ps awk bash bc bison cat colordiff cp csplit \
    curl cut date df diff dir du enscript env expand fmt fold gperf gprof \
    grep grub head indent irb ld ldd less ln ls m4 md5sum mkdir mkfifo mknod \
    mv netstat nl nm objcopy objdump od paste patch pr ptx readelf rm rmdir \
    sed seq sha{,1,224,256,384,512}sum shar sort split strip tac tail tee \
    texindex touch tr uname unexpand uniq units vdir wc wget who; do
    have $i && complete -F _longopt -o default $i
done
unset i

1587 line의 ‘_longopt -o default‘ 부분을 다음과 같이’ _longopt -o filenames‘로 변경한 후 다시 shell을 다시 열고 사용하면 된다.

 have $i && complete -F _longopt -o filenames $i

(참조 링크)

분실한 mysql root password 재설정하기

Mysql을 처음 설치할 때 database root 계정으로 사용할 password를 설정한다. 하지만 시간이 오래 지나서 그때 설정한 password를 기억할 수 없다면 다음의 방법으로 재설정할 수 있다. (Ubuntu 12.04 기준)

Step 1. 실행중인 mysql service를 중지 시킨다.

# service mysql stop

Step 2. Password를 검사하지 않도록 mysql 환경설정 파일을 수정한다.
: /etc/mysql/my.conf file에 skip-grant-tables를 추가하면 password를 검사하지 않는다.

[mysqld]
#
# * Basic Settings
#
user         = mysql
pid-file     = /var/run/mysqld/mysqld.pid
socket       = /var/run/mysqld/mysqld.sock
port         = 3306
basedir      = /usr
datadir      = /var/lib/mysql
tmpdir       = /tmp
lc-messages-dir = /usr/share/mysql

skip-external-locking

skip-grant-tables

Step 3. 새로운 설정 값으로 mysql service를 실행한다.

# sudo service mysql start

Step 4. root 계정으로 mysql database를 연다.

$ mysql -uroot mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 37
Server version: 5.5.29-0ubuntu0.12.04.1 (Ubuntu)

Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Step 5. root password를 재설정한다.

mysql> UPDATE user SET password=PASSWORD('ROOT_비밀번호') WHERE user='root';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0

Step 6. my.conf를 복원하고 mysql service를 재실행 시킨다.