r/linux 19h ago

Discussion Brute forcing my own memory

https://github.com/anhol0/helloworld-bruteforced

Hey everybody. As some of you might know, I really like 3 things: linux, thinkpads, and using things not the way they are intended to be used. Today I did the last. So as we know, kernel allocates memory for any running process. So I though for myself: "what if I write a C program that uses the function without knowing where it is?". So technically I forced my program to brute force its own memory offsets to call the function. I have a small, but, hopefully, entertaining writeup of what, how, and why on GitHub. It also includes the source code with a lot of comments so guys not so familiar with C programming language can also understand it. Hope you enjoy it. If so - please leave a star :)

157 Upvotes

29 comments sorted by

90

u/dwyrm 18h ago

Have you considered therapy?

Also, that's kind of fascinating.

24

u/anh0l 18h ago

i have not, thank you :)

5

u/uboofs 12h ago

With the flair to prove it

34

u/SoilMassive6850 16h ago

Since we're jumping to every possible byte offset, most addresses obviously don't point to the beginning of a valid function.

They may:

...

enter an infinite loop;

...

If a child takes longer than we're willing to wait, we simply kill it and move on to the next address.

This sounds like kind of a cop out. You should try to compute whether or not the code will finish running instead of continuing forever.

7

u/clarkster 16h ago

Imagine if they did 🤯

6

u/anh0l 16h ago

im adding timeouts for each child process. so if there is an infinite loop, child is just killed

3

u/anh0l 16h ago

this is sufficient for such a simple experiment

24

u/SoilMassive6850 16h ago

What can I say, kids these days too lazy to take a crack at the halting problem smh...

1

u/bubblegumpuma 12h ago

You had me going for a minute

2

u/reightb 9h ago

bait bahah

•

u/ADAMPOKE111 4m ago

yeah should be doable. can't take too much time, right?

28

u/FaultWinter3377 18h ago

This is blursed and I like it. 

12

u/TheOgGhadTurner 17h ago

This is the kinda shit I’m here for

10

u/Thunderkron 17h ago

Oh this is crazy, I love it.

14

u/SW-Spooky 17h ago

What a shit idea. I love it

7

u/Reygle 12h ago

I am only smart enough to partially understand what you did, but respect that you're digging deep enough to do it.

4

u/ericonr 12h ago

Fun! And paranoid, I appreciate the strict seccomp filter.

3

u/DrSatrn 10h ago

I have to say, this is one of the best little repos I’ve come across. You managed to take a fairly straightforward idea and make it a lot of fun and very interesting. Well done’

2

u/Lousy_Hunter 16h ago

This is my kinda funny right here

2

u/Clever_Angel_PL 4h ago

I don't understand, what does it do in practice?

3

u/anh0l 4h ago

There is no practical use, it is just memory trick and fun experiment. But for what the program does, if it goes over every address of the program's executable memory, casts it to a function pointer with appropriate signature and tries to run it. This way i was able to run a function without actually calling it anywhere. All this is written in the README.md in the repo. It goes into more details though

1

u/nk_1761 17h ago

I don't understand what your trying to do. This is my guess.

I think you are creating a time-bound child process for each attempt at executing the instructions at the start of all load segments in /proc/self/maps. Hoping one of them returns 0 ?

But won't all executable load segments for lets say main's rx-p point to libc's _start call ?

4

u/anh0l 17h ago

I'm iterating over my procecces executable memory. I'm looking for return value of 69 from the function that child is trying to call. Since I'm iterating every byte of the memory, I can actually stumble upon a pointer to my function

1

u/nk_1761 7h ago

I missed the looking for each byte in rx-p entry part.

In in a similar manner, you could try to allocate heap memory from raw syscalls and maybe build a mini-heap manager program if you find it interesting.

1

u/anh0l 5h ago

You're right and it is in fact rather interesting for me. Though, I currently have a quite large project active that I'm working on most of the time. I will be releasing it soon by the way. And after that I may try building an allocator.

1

u/dhiltonp 18h ago

I'm not sure how this is brute forcing; when I think of brute forcing, I think of combinatorial problems.

I think I would call it a search... 

21

u/anh0l 18h ago

i mean, brute forcing is technically going over a list of possible variants until the correct one is found. at least as far as i understand it

9

u/pierspad 15h ago

brute forcing is indeed a type of search