43

Problems of Automated Generation of Exploits on the Basis of Source Code

Embed Size (px)

Citation preview

Page 1: Problems of Automated Generation of Exploits on the Basis of Source Code
Page 2: Problems of Automated Generation of Exploits on the Basis of Source Code

Проблемы автоматической генерации эксплойтов по

исходному коду

Алексей Москвин

Сергей Плехов

Page 3: Problems of Automated Generation of Exploits on the Basis of Source Code

Automatic Exploit Generation.

Page 4: Problems of Automated Generation of Exploits on the Basis of Source Code

Техники обнаружения уязвимостей

Динамический анализ кода (DAST)

Фаззинг входящих параметров (Fuzz testing/Black Box)

Инструментация кода

Статический анализ кода (SAST)

Pattern matching

Анализ не самой программы, а какого-либо её представления (AST/CFG/PDG)

Гибридный анализ

Совмещение результатов полученных статическим и динамическим анализом.

Page 5: Problems of Automated Generation of Exploits on the Basis of Source Code

Статический анализ кода

Необходимо иметь доступ к исходным кодам приложения

Позволяет не только искать уязвимости, но и проверять различные свойства программ

Преимущество: не нужно развернутого приложения

Большинство академических методов статического анализа, не позволяют анализировать большие приложения

Тяжело проверить полученные результаты

Page 6: Problems of Automated Generation of Exploits on the Basis of Source Code

Динамический анализ кода.

Настройки конфигурации

Не нужно иметь доступ к исходным кодам приложения, но необходимо установленное приложение

Не зависит от языка, на котором написано приложение

Легко реализуется и легко проверить результаты анализа

Сложно оценить покрытие кода при проведении анализа

Можно уничтожить веб приложение

Page 7: Problems of Automated Generation of Exploits on the Basis of Source Code

Гибридный анализ.

Необходимо иметь доступ как к исходным кодам приложения, так и развернутое приложение.

Есть возможность проверить полученные результаты, но необходимо скомбинировать результаты статического анализа и динамического.

При “плохой” реализации, возникают в полном объеме все минусы динамического анализа

Отчеты содержат большое количество результатов, не относящихся к уязвимостям.

Page 8: Problems of Automated Generation of Exploits on the Basis of Source Code

Реализованные цели.

Производить анализ при отсутствии развернутого приложения.

Небольшое количество ложных срабатывании (false positive)

Автоматическая генерация эксплойтов!

Page 9: Problems of Automated Generation of Exploits on the Basis of Source Code

Описание алгоритма.

По исходному коду строится промежуточное представление программы.

По промежуточному представлению создается абстрактная интерпретация программы.

По абстрактной интерпретации производятся символические вычисления.

Page 10: Problems of Automated Generation of Exploits on the Basis of Source Code

Представления кода.

При анализе используем промежуточные представления исходного кода.

Page 11: Problems of Automated Generation of Exploits on the Basis of Source Code

Генерация формулы.

По исходному коду собираем логическую формулу.

<?php // test.php if ($_GET['p'] == 'P@ssw0rd') { print base64_decode($_GET['text']); }

Входящие данные являются неизвестными значениями.

($_GET[‘p’] == ‘P@ssw0rd’) && (base64_decode($_GET['text']) == <script>alert('test');</script>)

Page 12: Problems of Automated Generation of Exploits on the Basis of Source Code

Символические вычисления.

Exploit:

test.php?p=P@ssw0rd&text=PHNjcmlwdD5hbGVydCgxKTwvc2NyaXB0Pg%3D%3D

Page 13: Problems of Automated Generation of Exploits on the Basis of Source Code

Компоненты реализации.

Page 14: Problems of Automated Generation of Exploits on the Basis of Source Code

Важные моменты анализа.

Анализ хода выполнения программы.

Частичное выполнение кода.

Специфика языков программирования.

Page 15: Problems of Automated Generation of Exploits on the Basis of Source Code

Ход выполнения: тривиальный пример

<?phpprint $_GET['x'];

Page 16: Problems of Automated Generation of Exploits on the Basis of Source Code

Ход выполнения: анализ Data Flow

<?php$x = $_GET[‘x’];// …print $x; 

Page 17: Problems of Automated Generation of Exploits on the Basis of Source Code

Ход выполнения: фильтрации

// фильтрация основного параметра<?php$x = $_GET['x'];if(preg_match('/[a-z]/', $x)) print $x; // фильтрация дополнительного параметра<?php$x = $_GET['x'];if(preg_match('/[a-z]/', $_GET['y'])) print $x;

Page 18: Problems of Automated Generation of Exploits on the Basis of Source Code

Контекст вывода данных.

<?php// <script>alert('test');</script>print $_GET['name']; // "><script>alert('test');</script>print '<input type="button" id="'.$_GET['id'].'">';

Page 19: Problems of Automated Generation of Exploits on the Basis of Source Code

Ход выполнения: ООП

<?php class ParamGetter { public function getParam($param) { return $_GET[$param]; }} $x = new ParamGetter();print $x->getParam('name'); 

Page 20: Problems of Automated Generation of Exploits on the Basis of Source Code

Ход выполнения: пространство имен

<?php…use Modules/Params;$x = new ParamGetter();print $x->getParam('name'); 

Page 21: Problems of Automated Generation of Exploits on the Basis of Source Code

Ход выполнения: глобальные переменные

<?php

$flag = False; function f() { global $flag; if ($flag) // меняется в функции g() print $_GET['x']; // false positive g();} function g() { // вызывается в трех местах global $flag; $flag = !$flag;} f(); g(); f(); g();

$flag

g()

print $_GET['x']

f() Main:Line 17

Main:Line 18

… … … комбинаторный взрыв

Page 22: Problems of Automated Generation of Exploits on the Basis of Source Code

Ход выполнения: присваивание по ссылке

<?php function f($x, &$y) { $y = $x;} $x = $_GET['x'];$y = NULL; f($x, $y);print $y;

Page 23: Problems of Automated Generation of Exploits on the Basis of Source Code

Ход выполнения: зависимости модулей

Файл не является точкой входа

<?php // module1.php$x = $_GET['x'];require('module2.php');?> <?php // module2.phpprint $x;?>

 

Page 24: Problems of Automated Generation of Exploits on the Basis of Source Code

При поиске уязвимостей необходимо учитывать “естественный” ход

выполнения программы.

Page 25: Problems of Automated Generation of Exploits on the Basis of Source Code

Специфика PHP (I)

Перезапись переменных

<?php

$name = 'x';${$name} = $_GET['x'];print $x; 

Page 26: Problems of Automated Generation of Exploits on the Basis of Source Code

Частичное выполнение: динамические пути

<?php $path = $_SERVER['DOCUMENT_ROOT'].'/my_lib'; require $path.'/my_funcs.php'; my_print($_GET['x']); 

Page 27: Problems of Automated Generation of Exploits on the Basis of Source Code

Частичное выполнение: сокращение перебора

<?php//...$allowed_tags = array('history'); $f = fread('footer.template'); $tags = array('name', 'title', 'job', 'address', 'phone' 'id'); foreach ($tags as $tag) if (in_array($tag, $allowed_tags)) $f = str_replace('<'.$tag.'>', $_POST[$tag], $f)

print $f;

Page 28: Problems of Automated Generation of Exploits on the Basis of Source Code

Частичное выполнение: обфускация<?php $_F=__FILE__;$_X='Pz48IS0tIGI1ZzRuIGYyMnQ1ciAtLT4NCg0KPGQ0diBzdHlsNT0iY2w1MXI6YjJ0aDsiPjwvZDR2Pg0KPGQ0diBzdHlsNT0iY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g0KPC9odG1sPg==';eval(base64_decode('JF9YPWJhc2U2NF9kZWNvZGUoJF9YKTskX1g9c3RydHIoJF9YLCcxMjM0NTZhb3VpZScsJ2FvdWllMTIzNDU2Jyk7JF9SPWVyZWdfcmVwbGFjZSgnX19GSUxFX18nLCInIi4kX0YuIiciLCRfWCk7ZXZhbCgkX1IpOyRfUj0wOyRfWD0wOw=='));?>

Page 29: Problems of Automated Generation of Exploits on the Basis of Source Code

Частичное выполнение: деобфускация

Page 30: Problems of Automated Generation of Exploits on the Basis of Source Code

Промежуточные вычисления сильно упрощают дальнейший анализ

Page 31: Problems of Automated Generation of Exploits on the Basis of Source Code

Состояния системы и переменных.

<?php $id = isset($_GET['id']) ? $_GET['id'] : 'Your id';$name = isset($_GET['name']) ? $_GET['name'] : 'Your name';$title = isset($_GET['title']) ? $_GET['title'] : 'Your title';$job = isset($_GET['job']) ? $_GET['job'] : 'Your job';$phone = isset($_GET['phone']) ? $_GET['phone'] : 'Your phone'; // 2^5 = 32 возможных состояний системы// на самом деле надо проанализировать всего 2*5 = 10 возможных состояний переменныхprint $id;print $name; print $title;print $job;print $phone;

Page 32: Problems of Automated Generation of Exploits on the Basis of Source Code

Все состояния системы<?php $id = isset($_GET['id']) ? $_GET['id'] : 'Your id';$name = isset($_GET['name']) ? $_GET['name'] : 'Your name';$title = isset($_GET['title']) ? $_GET['title'] : 'Your title';

print $id;print $name; print $title;

Page 33: Problems of Automated Generation of Exploits on the Basis of Source Code

Компрессированное состояние

<?php $id = isset($_GET['id']) ? $_GET['id'] : 'Your id';$name = isset($_GET['name']) ? $_GET['name'] : 'Your name';$title = isset($_GET['title']) ? $_GET['title'] : 'Your title';

print $id;print $name; print $title;

Page 34: Problems of Automated Generation of Exploits on the Basis of Source Code

Дополнительные сложности

• Циклы• Рекурсия• Файловая система

Page 35: Problems of Automated Generation of Exploits on the Basis of Source Code

Циклы

// вычисляемые:• foreach (array('one', 'two', 'three') as $v) {  ...}

// предполагаемые:• foreach($_GET['x'] as $v) { ... }

Page 36: Problems of Automated Generation of Exploits on the Basis of Source Code

Рекурсия

function dump($x) {  if (is_array($x)) { foreach($x as $v) dump($v); } else dump_const($x);}

// вычисляемая:dump(array('one', 'two', ['three']));

// предполагаемая:dump($_GET['x']);

Page 37: Problems of Automated Generation of Exploits on the Basis of Source Code

Файловая система

// анализируемая• require 'langs/' . $_GET['lang'] . '.php';

// виртуальная• file_put_contents('cache.dat', $data); ... $data = file_get_contents('cache.dat');

// предполагаемая• if (file_exists('/tmp/tmp.dat'); ...

Page 38: Problems of Automated Generation of Exploits on the Basis of Source Code

Дополнительные сложности 2

• Определение подключаемых модулей по результату запроса к базе данных

• Для компилируемых языков отсутствие части зависимостей не дает собрать проект

• Символьное решение методами SAT может работать очень долго: необходимы эвристики и ограничение по времени

• Можно решать уравнения не на множестве строк а на множестве регулярных выражений для них

• Всегда есть место для оптимизации

Page 39: Problems of Automated Generation of Exploits on the Basis of Source Code

Другие виды уязвимостей

• Не все типы уязвимостей стоит ловить подобным методом (Pattern Matching, Fingerprinting, ошибки конфигурации)

• При расширении подхода становится возможным искать и более сложные инъекции (хранимые XSS, SQLi)

Page 40: Problems of Automated Generation of Exploits on the Basis of Source Code

Second Chance

ApplicationServerBD

Inject Exploit

Trigger Exploit

Page 41: Problems of Automated Generation of Exploits on the Basis of Source Code

Second Chance

if ($_GET['action'] == 'insert') { $user = $_GET['user']; $id = $_GET['id']; $sql = "UPDATE table1 SET user = '".

mysql_real_escape_string($user) . "' WHERE id = “ . intval($id) . ";"; mysql_query($sql);}else if ($_GET['action'] == 'select') { $sql = "SELECT * FROM table1 WHERE id = ". intval($id) . ";"; $res = mysql_query($sql); while ($row = mysql_fetch_array($res)) { echo $row['id'] + $row['user']; }}

Page 43: Problems of Automated Generation of Exploits on the Basis of Source Code