{"id":3549,"date":"2018-04-25T10:07:35","date_gmt":"2018-04-25T17:07:35","guid":{"rendered":"http:\/\/mattfife.com\/?p=3549"},"modified":"2018-04-25T10:09:09","modified_gmt":"2018-04-25T17:09:09","slug":"ld_preload-and-stealing-function-calls","status":"publish","type":"post","link":"https:\/\/mattfife.com\/?p=3549","title":{"rendered":"LD_PRELOAD and stealing function calls"},"content":{"rendered":"<p>There is a clever little trick that one can use for a variety of purposes on Linux. It involves overriding or hijacking function calls. It&#8217;s called LD_PRELOAD.<\/p>\n<p>Lets say you create a file called unrandom.c that includes an implementation of the rand() function. It matches the function rand() in standard C.<\/p>\n<div>\n<div id=\"highlighter_445866\" class=\"syntaxhighlighter cpp\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<caption>unrandom.c:<\/caption>\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"cpp color1 bold\">int<\/code> <code class=\"cpp functions bold\">rand<\/code><code class=\"cpp plain\">(){<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"cpp spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"cpp keyword bold\">return<\/code> <code class=\"cpp plain\">42; <\/code><code class=\"cpp comments\">\/\/the most random number in the universe<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"cpp plain\">}<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>We\u2019ll compile it into a shared library.<\/p>\n<blockquote>\n<pre>gcc -shared -fPIC unrandom.c -o unrandom.so<\/pre>\n<\/blockquote>\n<p>Now\u2026 just run a program (my_program) that uses random numbers like this, and you&#8217;ll find that the rand function only generates 42.<\/p>\n<blockquote>\n<pre>LD_PRELOAD=$PWD\/unrandom.so .\/my_program<\/pre>\n<\/blockquote>\n<p>This trick can be used in a variety of ways. A good write-up can be found here, and is worth a read:<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"svgjLVzitu\"><p><a href=\"https:\/\/rafalcieslak.wordpress.com\/2013\/04\/02\/dynamic-linker-tricks-using-ld_preload-to-cheat-inject-features-and-investigate-programs\/\">Dynamic linker tricks: Using LD_PRELOAD to cheat, inject features and investigate&nbsp;programs<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Dynamic linker tricks: Using LD_PRELOAD to cheat, inject features and investigate&nbsp;programs&#8221; &#8212; Rafa\u0142 Cie\u015blak&#039;s blog\" src=\"https:\/\/rafalcieslak.wordpress.com\/2013\/04\/02\/dynamic-linker-tricks-using-ld_preload-to-cheat-inject-features-and-investigate-programs\/embed\/#?secret=WJkjL2sWQ7#?secret=svgjLVzitu\" data-secret=\"svgjLVzitu\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There is a clever little trick that one can use for a variety of purposes on Linux. It involves overriding or hijacking function calls. It&#8217;s called LD_PRELOAD. Lets say you create a file called unrandom.c that includes an implementation of the rand() function. It matches the function rand() in standard C. unrandom.c: 1 2 3 int rand(){ \u00a0\u00a0\u00a0\u00a0return 42; \/\/the most random number in the universe } We\u2019ll compile it into a shared library. gcc -shared -fPIC unrandom.c -o unrandom.so&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/mattfife.com\/?p=3549\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[7,5],"tags":[],"class_list":["post-3549","post","type-post","status-publish","format-standard","hentry","category-technicalprogramming","category-technical"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4WECr-Vf","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/posts\/3549","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mattfife.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3549"}],"version-history":[{"count":3,"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/posts\/3549\/revisions"}],"predecessor-version":[{"id":3552,"href":"https:\/\/mattfife.com\/index.php?rest_route=\/wp\/v2\/posts\/3549\/revisions\/3552"}],"wp:attachment":[{"href":"https:\/\/mattfife.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mattfife.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mattfife.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}