An experiment on buffer overflow

This is a c program on windows showing a example of buffer overflow.

Knowledge backgroud

When a function is called,the parameters of it and a address will be pushed in to the stack.This address(let us call it RET) points to a instruction which will be executed next if the function call didn't happen.Then internal variables of the called function will be pushed into the stack.
The later something is pushed to the stack,the lower the logic address of it in the memory will be.So the internal variable has lower address than RET.If we set the the value of the internal variable longer that it should be,we can override the value of the RET.Then the program will execute the instruction pointed by the RET,which is modified by us.

Explanations on this c program

#include <stdio.h>
#include <string.h>

void overflow(const char* input)
{
    char buf[8];
    printf("Virtual address of 'buf' = Ox%p\n", buf);
    strcpy(buf,input);
}

void fun()
{
    printf("Function 'fun' has been called without an explicitly invocation.\n");
    printf("Buffer Overflow attack succeeded!\n");
}

int main(int argc, char* argv[])
{
    printf("Virtual address of 'overflow' = Ox%p\n",overflow);
    printf("Virtual address of 'fun' = Ox%p\n",fun);
    char input[30]="01234567890123456789";
    char add[5];
    *((int*)add) = fun;
    add[4] = 0;
    strcat(input, add);
    overflow(input);
    return 0;
}

The function main() will call the function overflow(),then return to main().In normal circumstances,the function fun() will not be executed.But using the knowledge above we can let the function fun() be executed.

Paste_Image.png

function fun() is executed!

How to run this c program

On windows OS,you'd better use mingw to compile it.A [mingw-get-inst-20120426.exe] is provided here:
https://github.com/VincentSeven/buffer_overflow

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子幽崩,更是在濱河造成了極大的恐慌,老刑警劉巖坎匿,帶你破解...
    沈念sama閱讀 211,561評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件盾剩,死亡現(xiàn)場離奇詭異雷激,居然都是意外死亡替蔬,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,218評論 3 385
  • 文/潘曉璐 我一進店門屎暇,熙熙樓的掌柜王于貴愁眉苦臉地迎上來承桥,“玉大人,你說我怎么就攤上這事根悼⌒滓欤” “怎么了?”我有些...
    開封第一講書人閱讀 157,162評論 0 348
  • 文/不壞的土叔 我叫張陵挤巡,是天一觀的道長剩彬。 經(jīng)常有香客問我,道長矿卑,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,470評論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮酥夭,結果婚禮上檐嚣,老公的妹妹穿的比我還像新娘。我一直安慰自己琴昆,他們只是感情好氓鄙,可當我...
    茶點故事閱讀 65,550評論 6 385
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著业舍,像睡著了一般抖拦。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上舷暮,一...
    開封第一講書人閱讀 49,806評論 1 290
  • 那天蟋座,我揣著相機與錄音,去河邊找鬼脚牍。 笑死向臀,一個胖子當著我的面吹牛,可吹牛的內容都是我干的诸狭。 我是一名探鬼主播券膀,決...
    沈念sama閱讀 38,951評論 3 407
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼驯遇!你這毒婦竟也來了芹彬?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 37,712評論 0 266
  • 序言:老撾萬榮一對情侶失蹤叉庐,失蹤者是張志新(化名)和其女友劉穎舒帮,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,166評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡玩郊,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 36,510評論 2 327
  • 正文 我和宋清朗相戀三年肢执,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片译红。...
    茶點故事閱讀 38,643評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡预茄,死狀恐怖,靈堂內的尸體忽然破棺而出侦厚,到底是詐尸還是另有隱情耻陕,我是刑警寧澤,帶...
    沈念sama閱讀 34,306評論 4 330
  • 正文 年R本政府宣布刨沦,位于F島的核電站诗宣,受9級特大地震影響,放射性物質發(fā)生泄漏想诅。R本人自食惡果不足惜梧田,卻給世界環(huán)境...
    茶點故事閱讀 39,930評論 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望侧蘸。 院中可真熱鬧裁眯,春花似錦、人聲如沸讳癌。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,745評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽晌坤。三九已至逢艘,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間骤菠,已是汗流浹背它改。 一陣腳步聲響...
    開封第一講書人閱讀 31,983評論 1 266
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留商乎,地道東北人央拖。 一個月前我還...
    沈念sama閱讀 46,351評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像鹉戚,于是被迫代替她去往敵國和親鲜戒。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 43,509評論 2 348

推薦閱讀更多精彩內容