Making Minimalist Web Server in Assembly on Linux (x64)

Sdílet
Vložit
  • čas přidán 16. 02. 2024
  • In this video I will demonstrate how you can make a simple web server using x64 Assembly and Linux system calls.
    I am planning to continue the POC from the video in the following Github project: github.com/nir9/fastws
    Documentation I used in the video: flatassembler.net/docs.php?ar...
  • Věda a technologie

Komentáře • 106

  • @jonfastofficial2460
    @jonfastofficial2460 Před 2 měsíci +55

    This is straight up sorcery. The code didn’t use a single include, extern, or struct definition. Excellent work!

    • @yestechguy-fj9tm
      @yestechguy-fj9tm Před 2 měsíci +4

      That the whole point of OS's they build all these sys calls and ABI for the programmers to use it.

    • @Bunny99s
      @Bunny99s Před 2 měsíci +5

      @@yestechguy-fj9tm Right, but the point of frameworks and standard libraries is to abstract the implementation of the ABI away from the programmer so we get a unique interface that works the same regardless of the underlying architechture. So the actual syscall number and how the arguments are passed is handled by the library. Though when you know your target, of course you can directly issue the syscall. I haven't done any low level programming on modern systems. I've done a lot during the Win95/98/XP times :)

    • @skilz8098
      @skilz8098 Před 18 dny

      @@yestechguy-fj9tm It's even more fun when you design your own ISA & ABI...

  • @gatogordo4131
    @gatogordo4131 Před 3 měsíci +63

    it's pretty hard to find hard core content like yours. I can learn a lot from your videos, keep going! Greetings from Brazil.

  • @fallout__boy1130
    @fallout__boy1130 Před 2 měsíci +4

    This looks like sorcery, but sorcery is actually all the levels of abstractions uppon which we are used to develop. Very nice content

  • @ItsCOMMANDer_
    @ItsCOMMANDer_ Před 3 měsíci +40

    next: gas compatable

    • @NOPerative
      @NOPerative Před 3 měsíci

      Flip the registers and insert the appropriate tokens. Conversion to and from GAS is pretty simple.

    • @ItsCOMMANDer_
      @ItsCOMMANDer_ Před 3 měsíci

      also mormat elf is unnecessary i think@@NOPerative

    • @NOPerative
      @NOPerative Před 3 měsíci +1

      @@ItsCOMMANDer_ Yes. Format ELF is FAsm exclusive. The format directive is unnecessary with GAS and NAsm.

  • @Crux161
    @Crux161 Před 2 měsíci +4

    I love how he shows what documentation to reference, and people still call it sorcery 😂

  • @shalinpather4198
    @shalinpather4198 Před 3 měsíci +18

    Legendary. Keep up with the videos. A few of your videos taught me far more than two year's worth of Computer Science courses.

    • @nirlichtman
      @nirlichtman  Před 3 měsíci +2

      Thanks! I appreciate the compliment :)

  • @mrsquiggles1379
    @mrsquiggles1379 Před 3 měsíci +7

    Really cool and creative how you derived addresses and call codes from C and injected that into assembly. Makes it more readable and easier to understand very impressive

  • @anthonygayflor
    @anthonygayflor Před měsícem

    Incredible work! I mainly work with JavaScript and when I used to hear people say C is high level it always left me bewildered.
    Now I understand.

  • @Mauriciohse
    @Mauriciohse Před 3 měsíci +13

    Great video man, always so cool to see how things happen under the hood. Ty!

  • @Bunny99s
    @Bunny99s Před 2 měsíci +4

    fasm is really a great and small assembler. I used it about 18 years ago to write a tiny keyboard hook dll which was just 2.5k in size. At that time Win98 / XP didn't had the low level hooks we have nowadays and keyboard / mouse hooks required a separate DLL which the OS injected into every application to process the hook in the active process space. Though afaik this dll doesn't work anymore. However I think the main reason is that i think it was an x86 (32bit) dll. Back in the day I used to write software with delphi and most applications where single file builds which is generally great for being portable. The keyboard hook dll was simply included as a resource and I unpacked it when it didn't exist yet.
    The dll was still quite large but this was mainly because of the import and export tables which require a certain memory alignment by windows.

  • @honeymak
    @honeymak Před 2 měsíci +1

    thx for all the heavy lifting of the pre-knowledge for this video to be made

  • @user-gh4lv2ub2j
    @user-gh4lv2ub2j Před 3 měsíci +6

    Thank you for your service sir.

  • @jsaenzMusic
    @jsaenzMusic Před 3 měsíci +1

    Bro......THIS is what I'm talking about! Thank you for this content!!!!

  • @twobob
    @twobob Před 3 měsíci +1

    very nice as always. thanks for the repo

  • @jamiepond
    @jamiepond Před 3 měsíci +2

    outstanding explantation.

  • @dj.yacine
    @dj.yacine Před 3 měsíci +2

    Always creative

  • @plato4ek
    @plato4ek Před 2 měsíci +1

    Hey! I really love your work! It would be great, if you put the links to the docs you use in the description box, so one doesn't need to retype them.

  • @madwilliamflint
    @madwilliamflint Před 3 měsíci +1

    I love it!

  • @dev.rahulgurjar
    @dev.rahulgurjar Před 2 měsíci

    Amazing 🤩

  • @forevervoyaging
    @forevervoyaging Před 2 měsíci

    While this is pure wizardry, your explanation is crystal clear and makes everything easy to follow. Outstanding work!

  • @AlexSmolyankin
    @AlexSmolyankin Před 3 měsíci +1

    Really nice. Especial how you combine constants from C with assembly. Could you make a video how you work with vim(as I understand you use wsl with vim). But how did you manage to run server on wsl and open page on windows? I mean does windows automatically forwards requests inside wsl?

    • @nirlichtman
      @nirlichtman  Před 3 měsíci

      Thanks! I have a playlist "Vim Tips" with videos about how I work with Vim, when I do Windows dev I work with Vim for Windows and when doing Linux dev I mostly work with Vim running on WSL.
      Yes, the request was automatically forwarded to WSL, I didn't configure anything special

    • @AlexSmolyankin
      @AlexSmolyankin Před 3 měsíci

      @@nirlichtmanThanks for full response. Will check you "Vim Tips"

  • @HP-gk7us
    @HP-gk7us Před 2 měsíci

    Love! ❤

  • @Kharzette
    @Kharzette Před 3 měsíci +2

    Amazed how you can do what I do with 2 huge monitors in a tiny little split up terminal window. 👍

    • @nirlichtman
      @nirlichtman  Před 3 měsíci

      Yah I used to have a dual monitor setup as well but decided a couple of years ago to move back to single monitor due to neck strain

    • @R.B.
      @R.B. Před 3 měsíci

      I find that even with an UWQHD monitor, it's hard to find a good way to partition the screen so that it is better than dual screens. I really need a SUWQHD I think... or just keep using dual screens. At least the footprint of a single UWQHD is an improvement.

  • @Bobby-sm3sy
    @Bobby-sm3sy Před 2 měsíci

    legend

  • @milk-it
    @milk-it Před 2 měsíci +2

    Very interesting and great job! One question: if you couldn't use the C/Python header files, where would you find the values and methodology?

    • @nirlichtman
      @nirlichtman  Před 2 měsíci +1

      Another option instead of doing the trick I did with the GCC preprocessor and header files is to manually copy over the values from the header files to the Assembly, but notice that syscall numbers change between different processor architectures so make sure you are copying the correct numbers for your architecture. Another way is to locate these values from the Linux kernel source, for example here is sockaddr_in in the kernel source elixir.bootlin.com/linux/latest/source/include/uapi/linux/in.h#L256

  • @pouf-dk3nq
    @pouf-dk3nq Před 3 měsíci

    This was so cool, btw shouldn't you have used close() to close the opened file descriptor ? and to get the syscalls values you can check a header in /usr/include/asm/unistd_64.h (as well as unistd_32.h)

    • @nirlichtman
      @nirlichtman  Před 3 měsíci

      Thanks! Yah I skipped doing cleanup/error checking to keep the video short (also have a disclaimer about this on the vid), but I added close in the code on Github. You can indeed get the syscall values directly from the header files as well (on my machine it has a different path), the idea of the preprocessor trick I did in the video is to automate this

  • @zazem4835
    @zazem4835 Před 3 měsíci

    I LOVE THIS. Also wanna know how you navigate vim

    • @nirlichtman
      @nirlichtman  Před 3 měsíci

      Thanks! I have a playlist "Vim Tips" with information about that :)

  • @savire.ergheiz
    @savire.ergheiz Před 3 měsíci +3

    Now lets create full scaleable enterprise app 😂

  • @Trippie1400
    @Trippie1400 Před 2 měsíci +1

    please make a video about creating a color theme for Vim 🙏

  • @kbsanders
    @kbsanders Před 2 měsíci +2

    Would it have been possible to create register aliases for the system call arguments?
    arg1 equ rdi
    arg2 equ rsi
    arg3 equ rdx
    arg4 equ r10
    arg5 equ r8
    arg6 equ r9

  • @Kira_x86_64
    @Kira_x86_64 Před 3 měsíci

    I am so used to using nasm that this looks foren to me... love the video though!

    • @nirlichtman
      @nirlichtman  Před 3 měsíci

      I am also used to NASM but recently I started using FASM and I like it a lot more :)

  • @durimmiziraj4815
    @durimmiziraj4815 Před 2 měsíci +1

    Great channel!

  • @llj0000
    @llj0000 Před 2 měsíci

    Love this stuff bro, but please do xor rdx, rdx instead of mov rdx, 0

  • @AMith-lv2cv
    @AMith-lv2cv Před 3 měsíci

    🔥🔥🔥🔥 wanna more and more

  • @hidoHido-vm4en
    @hidoHido-vm4en Před 2 měsíci +1

    if i want to run this Minimalist web server in raspberry pi pico w for a simple iot on/off leds and some simple controls
    is that will be more challenging ?

  • @biohazardouszxellium
    @biohazardouszxellium Před měsícem

    Based on your experience, in which scenario would be appropriate to use. NASM/FASM/MASM/etc? Subscribed by the way!

    • @nirlichtman
      @nirlichtman  Před měsícem +1

      Thanks! You mean which scenarios are appropriate to use Assembly in general? There are all kinds, for example early boot code for bootloaders, using in conjunction with C/C++ code since there are cases where using Assembly for certain parts is required to call certain specific instructions, or optimize a specific part, and of course in extreme cases you can even write full programs with Assembly - like Roller Coaster Tycoon :)

    • @biohazardouszxellium
      @biohazardouszxellium Před měsícem

      @@nirlichtman Yea the famous game Roller Coaster Tycoon! I'm starting to re-learn C and NASM. Do you also have experience with microcontrollers?

    • @nirlichtman
      @nirlichtman  Před měsícem +1

      @@biohazardouszxellium Not a lot just playing a little with Arduino, but I definitely plan on learning some Arduino AVR assembly when I have more time, interesting subject!

  • @Antek1234l
    @Antek1234l Před 2 měsíci

    17:55 Can this segmentation fault be prevented by adding a jmp instruction to a label above accept instruction? I think this might create infinite loop that waits for new incoming connections

    • @nirlichtman
      @nirlichtman  Před 2 měsíci +2

      Yah, the segfault occurs since I did not exit properly by invoking the exit system call so the execution slided after finishing. I added that jump to the accept instruction in the Github code, that would make it wait for more connections

    • @Antek1234l
      @Antek1234l Před 2 měsíci

      Okay, thank you!

  • @elyazidful
    @elyazidful Před 3 měsíci

    I like it !

  • @WiihawkPL
    @WiihawkPL Před 2 měsíci +1

    >linux assembly tutorial
    >uses windows

  • @amitmalaker5445
    @amitmalaker5445 Před 2 měsíci +1

    video request: make video on how to run a fasm macro in parallel with threading or with
    a separate process.

    • @nirlichtman
      @nirlichtman  Před 2 měsíci

      You mean a video explaining how to call assembly code from C ?

    • @amitmalaker5445
      @amitmalaker5445 Před 2 měsíci

      no like using thread and epoll using fasm
      @@nirlichtman

    • @mattmmilli8287
      @mattmmilli8287 Před 2 měsíci

      🤯

  • @jengstrm2
    @jengstrm2 Před 3 měsíci +1

    I'd love to see the benchmarks. Is it faster than NGINX?

    • @interrrp_with_three_rs
      @interrrp_with_three_rs Před 3 měsíci +1

      nope, nginx utilizes multiprocessing, this one does not

    • @TheUnkow
      @TheUnkow Před 2 měsíci

      Just use another app to route the incoming requests to different ports and have each core run a process of this server listening on each of those ports.
      Even that can be done via round robin in a fresh assembly project.
      As the code is so simple and has no security checks (which is ok when you don't serve client data), it should be blazing fast.
      You can even add a counter to save visits by IP in case you are DDoSed, you will still get most basic attacks off your back with a few more lines.

    • @interrrp_with_three_rs
      @interrrp_with_three_rs Před 2 měsíci +1

      @@TheUnkow so a reverse proxy? 😃

    • @churchers
      @churchers Před 2 měsíci

      @@interrrp_with_three_rs this is almost certainly faster than nginx for what it does - return a single file. Just the code in nginx to handle processing the request content, parse the url, map to a local file and related security checks/etc would be orders of magnitude more instructions than this entire program.

  • @PublyoRaymundo
    @PublyoRaymundo Před 2 měsíci

    not really a web server, but a starting point on how to handle sockets.

  • @vladislavknyazkov1962
    @vladislavknyazkov1962 Před 3 měsíci +2

    Next video: writing minimalist web server in x86 machine code

    • @idjles
      @idjles Před 3 měsíci

      6502

    • @user-ed1nw6vr8n
      @user-ed1nw6vr8n Před 3 měsíci

      assembly is literally a fancy machine code in english

  • @Vimposh
    @Vimposh Před 3 měsíci

    Next episode: making html templates

  • @micosair
    @micosair Před 3 měsíci

    In before RollerCoaster Tycoon 3D video in assembly....

  • @CryptAmine
    @CryptAmine Před 3 měsíci

    Why don't we have the same man page for " socket ", and there is no manual entry for "syscall" , I'm on Linux 6.7.4-arch

    • @PatrikGafvert
      @PatrikGafvert Před 3 měsíci

      If you man -k socket
      You see that it's in manual 7.
      So man 7 socket

  • @joshnjoshgaming
    @joshnjoshgaming Před 3 měsíci

    LET'S FUCKLIGN GOOO!!!!! YES YES YES YES YES YES YES YES YES YES YES YES YES YES!!! YES!!

  • @disdroid
    @disdroid Před 2 měsíci

    fasm😍

  • @ango_coding
    @ango_coding Před 3 měsíci +1

    What about for windows ?

    • @dfoxg
      @dfoxg Před 3 měsíci

      That would really be interesting to see the differences in this low level!

    • @nirlichtman
      @nirlichtman  Před 3 měsíci

      Winsock is also planned :)

  • @EndreSzasz
    @EndreSzasz Před 3 měsíci +2

    The lack of fclose is disturbing

    • @nirlichtman
      @nirlichtman  Před 3 měsíci +1

      Skipped cleanup/error handling in the video to keep it short (also added a disclaimer in the video around the middle). In the Github repo, I added calls to close

  • @diandradeeke
    @diandradeeke Před 2 měsíci

    Your videos are always so good but you are talking very fast and its difficult to keep track

    • @nirlichtman
      @nirlichtman  Před 2 měsíci

      Thanks for the feedback, will take that into consideration 👍

    • @Matt-ht8mc
      @Matt-ht8mc Před 6 dny

      you can change the speed of the vid at will

  • @rian0xFFF
    @rian0xFFF Před 3 měsíci +7

    Is this web assembly

  • @illegalsmirf
    @illegalsmirf Před 3 měsíci

    On LInux? I see a Windows task bar at the bottom.

  • @mattmmilli8287
    @mattmmilli8287 Před 2 měsíci

    someone make primeagen watch this 😂 dang

  • @azabass
    @azabass Před 3 měsíci +2

    If youre just using a C Libraries through syscalls anyway, why write it in asm at all?

    • @thisisnotok2100
      @thisisnotok2100 Před 3 měsíci +8

      I had this same thought, but the bottom line is, it's for fun. If he wasn't using the syscalls or c libs, he'd have to write his own OS to do it.

    • @brahimchaouchi
      @brahimchaouchi Před 3 měsíci +2

      syscalls are 'plugs' in the kernel. The simplest way to discover the syscall 'names' is to backtrace c 'wrappers' and voila

    • @oraz.
      @oraz. Před 3 měsíci +4

      He's not really using c libraries. The c code is a trick to get constants out of the header files.

  • @coding_pepper
    @coding_pepper Před 3 měsíci

    you are insaine

  • @mettle_x
    @mettle_x Před 3 měsíci

    That's too high-level. Consider using op-codes and binary-encoded machine instructions that work without an OS, i.e. Web Server in the BIOS.

  • @iwota
    @iwota Před 3 měsíci

    > he uses windows

  • @markmanning2921
    @markmanning2921 Před 2 měsíci +7

    My biggest critism is tht NONE of the code you are laying down is commented. There is NO such thing as "self documenting code" especially not assembler which is what I write a significant amount of my Linux code in.

    • @nirlichtman
      @nirlichtman  Před 2 měsíci +9

      "Code never lies, comments sometimes do"

    • @Crux161
      @Crux161 Před 2 měsíci +1

      Comments are fairly pointless unless they help *you* remember something for yourself. If you can’t read the code then umm…yeah. 😬

    • @markmanning2921
      @markmanning2921 Před 2 měsíci +3

      @@Crux161Comments are not only there for you, they are there for the poor shmuck that has to deep dive into your code maybe even years later.
      I have built my entire career around having to fix or maintain code written decades ago that has been hacked on repeatedly making the entire thing an unmaintainable cluster bleep (think of "a patchy web server" becoming the "apache web server" for example).
      Comments explain what you are thinking while you code, the logic behind the inner workings. I have also always said that if you have thought about your code long enough to explain it to someone else then you have thought about it long enough to code it otherwise you have not.
      NO code is self documenting, specially when you have if/and/but loops nested to the umpteenth level. Every block is an inlined, anonymous function. If people broke blocks out into sub functions it would move towards self documening but it NEVER truly gets there.