{"id":18350239,"url":"https://github.com/mebeim/bggp","last_synced_at":"2025-04-06T10:32:10.512Z","repository":{"id":247136666,"uuid":"825104348","full_name":"mebeim/bggp","owner":"mebeim","description":"My attempts and submissions for Binary Golf Grand Prix (https://binary.golf/)","archived":false,"fork":false,"pushed_at":"2025-01-30T21:13:07.000Z","size":95,"stargazers_count":2,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-21T21:51:11.362Z","etag":null,"topics":["bggp","bggp5","code-golf"],"latest_commit_sha":null,"homepage":"","language":"Assembly","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mebeim.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-06T19:34:59.000Z","updated_at":"2025-01-30T21:13:11.000Z","dependencies_parsed_at":"2024-09-05T16:15:06.182Z","dependency_job_id":"9f582ca3-aca5-48e0-a275-05b8dce64c6e","html_url":"https://github.com/mebeim/bggp","commit_stats":null,"previous_names":["mebeim/bggp5","mebeim/bggp"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mebeim%2Fbggp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mebeim%2Fbggp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mebeim%2Fbggp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mebeim%2Fbggp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mebeim","download_url":"https://codeload.github.com/mebeim/bggp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247470221,"owners_count":20944143,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bggp","bggp5","code-golf"],"created_at":"2024-11-05T21:25:45.398Z","updated_at":"2025-04-06T10:32:09.985Z","avatar_url":"https://github.com/mebeim.png","language":"Assembly","funding_links":[],"categories":[],"sub_categories":[],"readme":"# BGGP5 - @mebeim\n\nMy attempts and submissions for the [Binary Golf Grand Prix 5][bggp5]\ncompetition, which took place from June 21 2024 to September 6 2024. This year's\ngoal: download and display the contents of the file at\n`https://binary.golf/5/5`.\n\n## Summary of my entries\n\n| Kind                                   | Source                                                                                 | Entry size | Issue link                                    |\n|:---------------------------------------|:---------------------------------------------------------------------------------------|:-----------|:----------------------------------------------|\n| PHP script                             | [`php/BGGP5.php`](php/BGGP5.php)                                                       | 26 bytes   | https://github.com/binarygolf/BGGP/issues/69  |\n| Linux ELF i386 exec                    | [`elf/exec_i386/exec_curl_needs_arg.asm`](elf/exec_i386/exec_curl_needs_arg.asm)       | 76 bytes   | https://github.com/binarygolf/BGGP/issues/117 |\n| Linux ELF i386 exec                    | [`elf/exec_i386/exec_curl_short_url_v2.asm`](elf/exec_i386/exec_curl_needs_arg.asm)    | 82 bytes   | https://github.com/binarygolf/BGGP/issues/118 |\n| Linux ELF x86-64 exec                  | [`elf/exec_x86_64/exec_curl_needs_arg.asm`](elf/exec_x86_64/exec_curl_needs_arg.asm)   | 112 bytes  | https://github.com/binarygolf/BGGP/issues/123 |\n| Go program                             | [`go/cgo_system_curl_env_trick.go`](go/cgo_system_curl_env_trick.go)                   | 136 bytes  | https://github.com/binarygolf/BGGP/issues/116 |\n| UEFI application                       | [`uefi/asm/BGGP5.asm`](uefi/asm/BGGP5.asm)                                             | 316 bytes  | https://github.com/binarygolf/BGGP/issues/130 |\n| Linux ELF x86-64 dyn\u003csup\u003e**(1)**\u003c/sup\u003e | [`elf/dyn_x86_64/system_curl_pwd_trick.asm`](elf/dyn_x86_64/system_curl_pwd_trick.asm) | 456 bytes  | N/A\u003csup\u003e**(1)**\u003c/sup\u003e                         |\n\n\u003csup\u003e***(1)**: Not submitted as it would fall in the same category as the 64-bit exec version, which is smaller.*\u003c/sup\u003e\n\n\n## UEFI applications\n\n**See writeup and source code in the [`uefi/`](uefi/) directory.**\n\nI wrote various UEFI applications and tested them on QEMU with [EDK II][edk2]\nOVMF firmware. You can see the different iterations, which are all commented.\nThese apps use the UEFI HTTP(S) protocol to download the file at\n`https://binary.golf/5/5` to memory and display it.\n\nI configured and built the OVMF firmware from EDK II source code to hav HTTPS\nsupport. It was a bit of a pain to get the network stack and TLS working\nproperly because for some reason some domains were not passing the TLS handshake\nverification (is that a bug in the implementation?), so in the end I opted to\ndisable TLS handshake verification at build time (like you would do with\n`curl --insecure`). The connection is still HTTPS, but the server certificate is\nnot actually verified.\n\nSome of the programs are written in C using the EDK II C APIs, while others are\ncarefully hand-crafted using NASM assembler syntax and x86 assembly operations.\nOf course, the latter are quite smaller. The base I used for the assembly\nprograms was [the BGGP4 UEFI entry by @netspooky][netspooky-bggp4]. It was a\ngood starting point since I was not very familiar with the PE before attempting\nthis.\n\nThis was definitely the most interesting and fun entry to write. Took me quite a\nwhile to get it down to the final size, including writing\n[a Python script](uefi/minimize.py) to optimize the packing and scheduling of\nx86 instructions in the various PE/COFF header holes.\n\nI also had fun writing automated QEMU runner scripts to test things and a\nDocker-based build environment to make things easily reproducible (mainly to\nmake the life of whoever would be validating my entry easier).\n\n\n## ELF programs\n\n**See writeup and source code in the [`elf/`](elf/) directory.**\n\nI wrote a bunch of different ELF programs for Linux x86 and x86-64, both static\n`ET_EXEC` and dynamic `ET_DYN` using `libcurl`. The dynamic ones are quite\nlarger for obvious reasons so in the end I did not submit those, as it seems\nlike this year the ELF category does not distinguisy between `ET_EXEC` and\n`ET_DYN`.\n\nAll these ELFs are hand crafted using NASM. They abuse the fact that the Linux\nkernel ELF loader does not care about most of the ELF header and is very lenient\nabout its content. Code is stuffed in unused parts of ELF headers.\n\nAll the `ET_EXEC` ELFs end up executing `curl`. Some of them also download the\nBGGP5 file from the `7f.uk` shorter URL, which redirects to the actual URL and\nalso redirects HTTP to HTTPS (not sure who created it, but thanks!).\n\nSome of the `ET_DYN` ELFs link `libcurl.so` instead of executing the `curl`\nbinary. They make the dynamic linker load and resolve the necessary library\nfunctions through appropriately crafted dynamic section, string table and\nPLT/GOT tables. Writing dynamic ELFs that link and call library functions by\nhand was pretty interesting. I had to dig into Glibc `ld.so` source code a few\ntimes to understand how some of the things work at a low level.\n\nSince this year's BGGP entries are differentiated based on the need of command\nline args, which is why I have submitted two entries for x86 32-bit `ET_EXEC`.\nAt the end of the day, the smallest one simply performs `execve(\"/bin/curl\",\nargv, NULL)` and needs a command line argument.\n\n\n## Go programs\n\n**See writeup and source code in the [`go/`](go/) directory.**\n\nThis was a short experiment. These programs use [cgo][cgo] to call\n[`system(3)`][man3system] from the C library directly.\n\n\n## PHP script\n\n**See writeup and source code in the [`php/`](php/) directory.**\n\nThis was also a very short experiment, just for fun. I almost put negative\neffort into it. LOL.\n\n---\n\n*Copyright \u0026copy; 2024 Marco Bonelli (@mebeim). Licensed under the MIT License.*\n\n\n[bggp5]: https://binary.golf/5/\n[edk2]: https://github.com/tianocore/edk2\n[netspooky-bggp4]: https://github.com/netspooky/golfclub/tree/master/uefi/bggp4\n[cgo]: https://go.dev/wiki/cgo\n[man3system]: https://manned.org/man/system\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmebeim%2Fbggp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmebeim%2Fbggp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmebeim%2Fbggp/lists"}