Black Hat Rust Applied offensive security with the Rust programming language Sylvain Kerkour 2024 scribd download
Black Hat Rust Applied offensive security with the Rust programming language Sylvain Kerkour 2024 scribd download
com
https://ebookmeta.com/product/black-hat-rust-applied-
offensive-security-with-the-rust-programming-language-
sylvain-kerkour/
OR CLICK HERE
DOWLOAD NOW
https://ebookmeta.com/product/black-hat-rust-applied-offensive-
security-with-the-rust-programming-language-sylvain-kerkour-3/
ebookmeta.com
https://ebookmeta.com/product/black-hat-rust-applied-offensive-
security-with-the-rust-programming-language-sylvain-kerkour/
ebookmeta.com
Black Hat Rust Deep dive into offensive security with the
Rust programming language Sylvain Kerkour
https://ebookmeta.com/product/black-hat-rust-deep-dive-into-offensive-
security-with-the-rust-programming-language-sylvain-kerkour/
ebookmeta.com
https://ebookmeta.com/product/agriscience-fundamentals-and-
applications-sixth-edition-l-devere-burton/
ebookmeta.com
Letting Data Lead How to Design Analyze and Respond to
Classroom Assessment Gain Actionable Insights Through
Effective Assessment Methods and Data Interpretation 1st
Edition Eileen Depka
https://ebookmeta.com/product/letting-data-lead-how-to-design-analyze-
and-respond-to-classroom-assessment-gain-actionable-insights-through-
effective-assessment-methods-and-data-interpretation-1st-edition-
eileen-depka/
ebookmeta.com
The Bear the Bat and the Dove Three Stories from Aesop Rob
Cleveland
https://ebookmeta.com/product/the-bear-the-bat-and-the-dove-three-
stories-from-aesop-rob-cleveland/
ebookmeta.com
https://ebookmeta.com/product/software-defined-radio-theory-and-
practice-artech-house-mobile-communications-library-1st-edition-
reyland/
ebookmeta.com
https://ebookmeta.com/product/no-truth-without-beauty-god-the-qur-an-
and-women-s-rights-sustainable-development-goals-series-el-ali/
ebookmeta.com
https://ebookmeta.com/product/perfect-phrases-for-esl-conversation-
skills-diane-engelhardt/
ebookmeta.com
Redemption Dr Rebecca Sharp
https://ebookmeta.com/product/redemption-dr-rebecca-sharp/
ebookmeta.com
Black Hat Rust
Applied offensive security with the Rust
programming language
Sylvain Kerkour
Black Hat Rust
Applied offensive security with the Rust programming language
Sylvain Kerkour
v2021.46
Contents
Copyright 7
Contact 9
Preface 10
1 Introduction 13
1.1 Types of attacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.2 Phases of an attack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.3 Profiles of attackers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.4 Attribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.5 The Rust programming language . . . . . . . . . . . . . . . . . . . . . 19
1.6 History of Rust . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.7 Rust is awesome . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.8 Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.9 Our first Rust program: A SHA-1 hash cracker . . . . . . . . . . . . . 25
1.10 Mental models for approaching Rust . . . . . . . . . . . . . . . . . . . 31
1.11 A few things I’ve learned along the way . . . . . . . . . . . . . . . . . . 33
1.12 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
1
2.15 Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
2.16 Going further . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
2.17 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
2
5.18 Crawling a JSON API . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
5.19 Crawling a JavaScript web application . . . . . . . . . . . . . . . . . . 133
5.20 How to defend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
5.21 Going further . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
5.22 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
3
8 Writing shellcodes in Rust 182
8.1 What is a shellcode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
8.2 Sections of an executable . . . . . . . . . . . . . . . . . . . . . . . . . . 183
8.3 Rust compilation process . . . . . . . . . . . . . . . . . . . . . . . . . . 184
8.4 no_std . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
8.5 Using assembly from Rust . . . . . . . . . . . . . . . . . . . . . . . . . 187
8.6 The never type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
8.7 Executing shellcodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
8.8 Our linker script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
8.9 Hello world shellcode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
8.10 An actual shellcode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
8.11 Reverse TCP shellcode . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
8.12 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
4
11.3 Cryptography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
11.4 Hash functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
11.5 Message Authentication Codes . . . . . . . . . . . . . . . . . . . . . . . 278
11.6 Key derivation functions . . . . . . . . . . . . . . . . . . . . . . . . . . 280
11.7 Block ciphers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
11.8 Authenticated encryption (AEAD) . . . . . . . . . . . . . . . . . . . . 281
11.9 Asymmetric encryption . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
11.10Diffie–Hellman key exchange . . . . . . . . . . . . . . . . . . . . . . . . 284
11.11Signatures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
11.12End-to-end encryption . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
11.13Who uses cryptography . . . . . . . . . . . . . . . . . . . . . . . . . . 294
11.14Common problems and pitfalls with cryptography . . . . . . . . . . . . 295
11.15A little bit of TOFU? . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
11.16The Rust cryptography ecosystem . . . . . . . . . . . . . . . . . . . . . 296
11.17Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
11.18Our threat model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
11.19Designing our protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
11.20Implementing end-to-end encryption in Rust . . . . . . . . . . . . . . . 303
11.21Some limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
11.22To learn more . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
11.23Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
5
13.10Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
14 Conclusion 348
14.1 What we didn’t cover . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
14.2 The future of Rust . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
14.3 Leaked repositories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
14.4 How bad guys get caught . . . . . . . . . . . . . . . . . . . . . . . . . 350
14.5 Your turn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
14.6 Build your own RAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
14.7 Other interesting blogs . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
14.8 Contact . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
6
Copyright
All rights reserved. No portion of this book may be reproduced in any form without
permission from the publisher, except as permitted by law. For permissions contact:
[email protected]
7
Your early access bonuses
Dear reader, in order to thank you for buying the Black Hat Rust early access edition
and helping to make this book a reality, I prepared you a special bonus: I curated a
list of the best detailed analyses of the most advanced malware of the past two decades.
You may find inside great inspiration when developing your own offensive tools. You
can find the list at this address: https://github.com/black-hat-rust-bonuses/black-
hat-rust-bonuses
If you notice a mistake (it happens), something that could be improved, or want to
share your ideas about offensive security, feel free to join the discussion on Github:
https://github.com/skerkour/black-hat-rust
8
Contact
Every week I share updates about my projects and everything I learn about how to
(ab)use technology for fun & profit: Programming, Hacking & Entrepreneurship. You
can subscribe by Email or RSS: https://kerkour.com/follow.
You bought the book and are annoyed by something? Please tell me, and I will do my
best to improve it!
Or, you greatly enjoyed the read and want to say thank you?
9
Preface
After high school, my plan for life was to become a private detective, maybe because
I read too many Sherlock Holmes books. In France, the easiest way to become one is
(was?) to go to law university and then to attend a specialized school.
I quickly realized that studying law was not for me: reality is travestied to fit whatever
narrative politics or professor wanted us to believe. No deep knowledge is taught here,
only numbers, dates, how to look nice and sound smart. It was deeply frustrating for
the young man I was, with an insatiable curiosity. I wanted to understand how the
world works, not human conventions. For example, how do these machines we call
computers that we are frantically typing on all day long work under the hood?
So I started by installing Linux (no, I won’t enter the GNU/Linux war) on my Asus
EeePC, a small netbook with only 1GB of RAM, because Windows was too slow, and
started to learn to develop C++ programs with Qt, thanks to online tutorials. I coded
my own text and my own chat systems. But my curiosity was not fulfilled.
One day, I inadvertently fell on the book that changed my life: “Hacking: The Art of
Exploitation, 2nd Edition”, by Jon Erickson.
This book not only made me curious about how to make things, but, more importantly,
how to break things. It made me realize that you can’t build reliable things without
understanding how to break them, and by extension, where their weaknesses are.
While the book remains great to learn low-level programming and how to exploit simple
memory safety bugs, today, hacking requires new skills: web exploitation, network and
system programming, and, above all, how to code in a modern programming language.
While the Rust Book does an excellent job teaching What is Rust, I felt that a book
about Why and How to Rust was missing. That means that some concepts will not
10
be covered in-depth in this book. Instead, we are going to see how to effectively use
them in practice.
In this book, we will shake the preconceived ideas (Rust is too complex for the real world,
Rust is not productive…) and see how to architect and create real-world Rust projects
applied to offensive security. We will see how polyvalent Rust is, which enables its users
to replace the plethora of programming languages (Python, Ruby, C, C++…) plaguing
the offensive security world with a unique language that offers high-level abstractions,
high performance, and low-level control when needed.
We will always start with some theory, deep knowledge that pass through ages, tech-
nologies and trends. This knowledge is independent of any programming language and
will help you to get the right mindset required for offensive security.
I designed this book for people who either want to understand how attackers think in
order to better defend themselves or for people who want to enter the world of offensive
security and eventually make a living off it.
The goal of this book is to save you time in your path to action, by distilling knowledge
and presenting it in applied code projects.
It’s important to understand that Black Hat Rust is not meant to be a big encyclopedia
containing all the knowledge of the world. Instead, it was designed as a guide to help
you getting started and pave the way to action. Knowledge is often a prerequisite,
but it’s action that is shaping the world, and sometimes knowledge is a blocker for
action (see analysis paralysis). As we will see, some of the most primitive offensive
techniques are still the most effective. Thus some very specific topics, such as how to
bypass modern OSes protection mechanisms won’t be covered because there already is
extensive literature on these topics, and they have little value in a book about Rust.
That being said, I did my best to list the best resources to further your learning journey.
It took me approximately 1 year to become efficient in Rust, but it’s only when I started
to write (and rewrite) a lot of code that I made real progress.
Rust is an extremely vast language, but in reality, you will (and should) use only a
subset of its features: you don’t need to learn them all ahead of time. Some, that we
will study in this book, are fundamentals. Others are not and may have an adversarial
effect on the quality of your code by making it harder to read and maintain.
My intention with this book is not only to make you discover the fabulous world of
offensive security, to convince you that Rust is the long-awaited one-size-fits-all pro-
gramming language meeting all the needs of offensive security, but also to save you
11
a lot of time by guiding you to what really matters when learning Rust and offensive
security. But remember, knowledge is not enough. Knowledge doesn’t move mountains.
Actions do.
Thus, the book is only one half of the story. The other half is the accompanying code
repository: https://github.com/skerkour/black-hat-rust. It’s impossible to learn
without practice, so I invite you to read the code, modify it and make it
yours!
If at any time you feel lost or don’t understand a chunk of Rust code, don’t hesitate
to refer to the Rust Language Cheat Sheet, The Rust Book, and the Rust Language
Reference.
Also, the book is code-heavy. I recommend reading it with a web browser aside, in order
to explore and play with the code on GitHub: https://github.com/skerkour/black-hat-
rust/.
12
Chapter 1
Introduction
In reality, the spectrum of the profile of the attackers is extremely large, from the bored
teenager exploring the internet to sovereign State’s armies as well as the unhappy former
employee. As we will see, cyberattacks are not that hard. Knowledge is simply unevenly
distributed and jealously kept secret by the existing actors. The principal ingredients
are a good dose of curiosity and the courage to follow your instinct.
As digital is taking an always more important place in our lives, the impact and scale
of cyberattacks will increase in the same way: we are helplessly witnessing during
the current COVID-19 pandemic attacks against our hospitals which have real-life and
dramatic consequences.
It’s time to fight back and to prepare ourselves for the wars and battles of today (not
tomorrow) and to understand that, in order to defend, there is no other way than
to put ourselves in the shoes of attackers and think how they think. What are their
motivations? How can they break seemingly so easily into any system? What do they
do to their victims? From theory to practice, we will explore the arcanes of offensive
security and build our own offensive tools with the Rust programming language.
Why Rust?
The world of security (and, more generally, software) is plagued by too many program-
ming languages with too many footguns. You have to choose between fast and unsafe
(C, C++…) or slow but mostly safe (Python, Java…).
13
Another Random Document on
Scribd Without Any Related Topics
liked it.” W. A. Barrett
[2]
GUITERMAN, ARTHUR. Chips of Jade. il *$2
Dutton 895
20–19184
[2]
GUITRY, SACHA. Deburau; a comedy; in an
English version by Harley Granville Barker. $2
Putnam 842
20–10284
20–4701
Joseph Lee, in his foreword to this posthumous volume, calls it Dr
Gulick’s legacy to his fellow citizens. In making the study of play his
life work the author has come to the conclusion that it affords the
best and most profitable way of studying humankind itself; that the
individual reveals himself more completely in play than in any other
way; that play has a greater shaping power over the character and
nature of man than any other activity; and that a people also most
truly reveals itself in the character of its pleasures. Contents: The
extent of the play interest: Separation vs. concentration; Hunting
and fighting plays; Playing house; Fire play; Toys—construction and
ownership; Masculine and feminine differences; The play of animals;
The play of adults; The play of subnormal children; Play progression;
Play and physical growth; Play and education; Play and moral
growth; Instinct and tradition in play; Play and our changing
civilization; Play and the modern city; Direction and control in play—
playgrounds; Play and democracy; Play, the pursuit of the ideal;
Index.
“With this book Dr Gulick has made a real contribution which will
enrich all who read it. It should be in the hands not only of all who
are interested in recreational activities, but of fathers, mothers and
educators as well.” S. L. Jean
20–26883
The time setting of this story is about ten years in the future, when
travel and commerce by air have become thoroughly established, and
cross-Atlantic air trips are an everyday occurrence. The story is told
by Sir John Custance, young and popular commissioner of air police
for the British government. On one of its regular trips, one of the
aerial liners is held up by a pirate airship, and even while this affair is
being investigated, a second holdup is made. And it so happens that
on this ship, Connie Shepherd, Sir John’s fiancée, is a passenger, and
is captured and carried away by the pirates. His motive is therefore
doubly strong for discovering the criminals. He has the help of Mr
Danjuro, a unique Japanese personality with apparently infinite
resources and capabilities. Altho they are in the end successful in
capturing the whole pirate band and releasing Connie, it is by no
means an easy task, and Sir John finds himself in close proximity to
death more than once.
20–6660
“Indispensable aid.”
+ Booklist 16:296 Je 20
19–15645
A20–768
“These essays are for the most part revived from the years 1897–
1907, representing the views, during the changing moods of the
decade, of this capable and cultured Irish essayist, who, it will be
remembered, severed his connexion with the Gaelic league when it
decided to make the learning of Irish compulsory and who believes
that, as Yeats and Synge have shown, it is possible to be completely
Irish while using the English language. His subjects are Nineteenth
century novels of Irish life; A century of Irish humour (written 1901);
Literature among the illiterates, from a volume called ‘To-day and to-
morrow in Ireland’ (1902), now out of print (in two parts, The
Shanachy, and The life of a song, a traditional song which Mr Gwynn
took down from the lips of an Irish peasant); Irish education and
Irish character. There are two later essays on Irish gentry (1913), and
Yesterday in Ireland (1918).”—The Times [London] Lit Sup
20–5238
“Mr Gwynn has given far the clearest account of the procession of
events, and especially a fascinating narrative of the labors and
personalities of the convention. His book is almost indispensable to
anyone who would wish to understand the relation of opinion to the
controversy which is about to open concerning the new Home rule
bill.”
“Mr Gwynn writes in a sanely liberal vein and can take a detached
view of all sides of the struggle of Ireland for home rule....
Nevertheless, the summing-up is an indictment of a government that
had an excellent chance to show, by firmness and justice, that it was
determined to give Ireland the promised measure of home rule.”
+ Springf’d Republican p11a Mr 21 ’20
1200w
Reviewed by N. J. O’Conor
20–8515
“The little book will have no political influence at this time, but it
should have a personal influence to inspire better citizenship and
continual preparedness.” J. S. B.
20–5230
“The tale is told swiftly and simply, as all good Rider Haggard tales
are told. It moves so naturally that one overlooks the unreality. ‘The
ancient Allan’ is by no means to be named in the same breath with
‘King Solomon’s mines’ and other earlier creations of its
indefatigable author. But it will not disappoint the reader who wants
thrills without analyzing too closely the methods employed to
provide them for him.”
“It is a very good example of the author at his second best—we can
never hope to recover the first thrill of ‘She.’”
“The story is told in Sir Rider’s customary colorful style and with
his gift for creating illusion. Ancient Egypt becomes a vivid reality.”
“From the time Field Marshal (now Earl) Haig assumed the chief
command of the British armies in France on December 19, 1915, until
the close of fighting at the end of 1918, he forwarded to the war office
at London in May and December of each year a summary of the
operations for the six months preceding. These were intended
frankly for the information of the people at home and were quite
apart from the detailed, confidential information sent daily from
great headquarters in France to the general staff at home. These
statements have been collected and edited by Lieut.-Col. J. H.
Boraston, private secretary to Earl Haig and published under the title
‘Sir Douglas Haig’s despatches.’ The despatches, which number eight
and fill 357 pages of the heavy volume, are preceded by an
introduction written by Marshal Foch, and a preface by the field
marshal himself. The volume is accompanied by a number of
carefully prepared, highly detailed maps in large scale.”—Springf’d
Republican
20–3879
Reviewed by Sganarelle
“It goes without saying that Viscount Haldane makes out a good
case for Great Britain: but he does so in anything but a blindly
chauvinistic temper. Without anger or irritation, imputing sinister
motives to none, he deals honestly with the facts as he sees them and
presents his case with a patient and persuasive reasonableness that
lends an air of finality to his conclusions. Nevertheless, what strikes
one on reflection is that the discussion never goes below the surface
of things.” Carl Becker
“Great injustice has been done by the press and the public to Mr
Haldane’s work before the war as secretary of state.... The war being
over, Lord Haldane publishes his defence, which we hope everybody
will read, and having read, will admit to be a refutation of charges
hatched in the fever of fear.”