Skip to main content
Corrected spelling; prettify the C code.
Source Link
Toby Speight
  • 6.5k
  • 1
  • 24
  • 41

Or the same thing in cC (with inline assembly):

#include <stdio.h>

extern char src;
asm("src: .incbin \"" __FILE__ "\"\n.byte 0");

int main(int argc, char *argv[]) {
    unlink(argv[0]);
    FILE *file = fopen(argv[0], "w");
    fprintf(file, "%s",&src);
    fclose(file);
    return 0;
}
#include <stdio.h>

extern char src;
asm("src: .incbin \"" __FILE__ "\"\n.byte 0");

int main(int argc, char *argv[]) {
    unlink(argv[0]);
    FILE *file = fopen(argv[0], "w");
    fprintf(file, "%s",&src);
    fclose(file);
    return 0;
}

There must be a better way to do this in cC (or maybe not), but I don't know any.

Or the same thing in c (with inline assembly):

#include <stdio.h>

extern char src;
asm("src: .incbin \"" __FILE__ "\"\n.byte 0");

int main(int argc, char *argv[]) {
    unlink(argv[0]);
    FILE *file = fopen(argv[0], "w");
    fprintf(file, "%s",&src);
    fclose(file);
    return 0;
}

There must be a better way to do this in c (or maybe not), but I don't know any.

Or the same thing in C (with inline assembly):

#include <stdio.h>

extern char src;
asm("src: .incbin \"" __FILE__ "\"\n.byte 0");

int main(int argc, char *argv[]) {
    unlink(argv[0]);
    FILE *file = fopen(argv[0], "w");
    fprintf(file, "%s",&src);
    fclose(file);
    return 0;
}

There must be a better way to do this in C (or maybe not), but I don't know any.

add c
Source Link
Ian D. Scott
  • 2.1k
  • 1
  • 13
  • 15

x86_64 NASM Assembly for Linux

This assembly program replaces itself with its source code. It essentially "decompiles" itself, replacing the binary.

SECTION .data
    source incbin __FILE__
    len equ $ - source

SECTION .text
global _start

_start:

    pop rdi           ;number of parameters
    pop rdi           ;path to executable, parameter of unlink and open

    mov    rax, 87    ;unlink
    syscall
    cmp    rax, 0
    jl     error

    mov    rax, 2     ;open
    mov    rsi, 0101o ;O_WRONLY O_CREAT
    mov    rdx, 0600o ;permissions on created file
    syscall
    cmp    rax, 0
    jl     error
    mov    rdi, rax   ;file (return value of open)

    mov    rax, 1     ;write
    mov    rsi, source
    mov    rdx, len
    syscall
    cmp    rax, 0
    jl     error

    mov    rax, 60    ;exit
    mov    rdi, 0     ;return code
    syscall

    error:
        mov    rax, 60
        mov    rdi, 1
        syscall

Compile with:

nasm -f elf64 FILENAME
ld -m elf_x86_64 FILENAME.o -o FILENAME

Or the same thing in c (with inline assembly):

#include <stdio.h>

extern char src;
asm("src: .incbin \"" __FILE__ "\"\n.byte 0");

int main(int argc, char *argv[]) {
    unlink(argv[0]);
    FILE *file = fopen(argv[0], "w");
    fprintf(file, "%s",&src);
    fclose(file);
    return 0;
}

When run, the program deletes itself, and then writes its source code to the same path as the executable was at. That way, the deleted file can always be retrieved by recompiling, even if you lost the original source code.

There must be a better way to do this in c (or maybe not), but I don't know any.

Isn't that much better than just having it delete itself!

x86_64 NASM Assembly for Linux

This assembly program replaces itself with its source code. It essentially "decompiles" itself, replacing the binary.

SECTION .data
    source incbin __FILE__
    len equ $ - source

SECTION .text
global _start

_start:

    pop rdi           ;number of parameters
    pop rdi           ;path to executable, parameter of unlink and open

    mov    rax, 87    ;unlink
    syscall
    cmp    rax, 0
    jl     error

    mov    rax, 2     ;open
    mov    rsi, 0101o ;O_WRONLY O_CREAT
    mov    rdx, 0600o ;permissions on created file
    syscall
    cmp    rax, 0
    jl     error
    mov    rdi, rax   ;file (return value of open)

    mov    rax, 1     ;write
    mov    rsi, source
    mov    rdx, len
    syscall
    cmp    rax, 0
    jl     error

    mov    rax, 60    ;exit
    mov    rdi, 0     ;return code
    syscall

    error:
        mov    rax, 60
        mov    rdi, 1
        syscall

Compile with:

nasm -f elf64 FILENAME
ld -m elf_x86_64 FILENAME.o -o FILENAME

When run, the program deletes itself, and then writes its source code to the same path as the executable was at. That way, the deleted file can always be retrieved by recompiling, even if you lost the original source code.

Isn't that much better than just having it delete itself!

x86_64 NASM Assembly for Linux

This assembly program replaces itself with its source code. It essentially "decompiles" itself, replacing the binary.

SECTION .data
    source incbin __FILE__
    len equ $ - source

SECTION .text
global _start

_start:

    pop rdi           ;number of parameters
    pop rdi           ;path to executable, parameter of unlink and open

    mov    rax, 87    ;unlink
    syscall
    cmp    rax, 0
    jl     error

    mov    rax, 2     ;open
    mov    rsi, 0101o ;O_WRONLY O_CREAT
    mov    rdx, 0600o ;permissions on created file
    syscall
    cmp    rax, 0
    jl     error
    mov    rdi, rax   ;file (return value of open)

    mov    rax, 1     ;write
    mov    rsi, source
    mov    rdx, len
    syscall
    cmp    rax, 0
    jl     error

    mov    rax, 60    ;exit
    mov    rdi, 0     ;return code
    syscall

    error:
        mov    rax, 60
        mov    rdi, 1
        syscall

Compile with:

nasm -f elf64 FILENAME
ld -m elf_x86_64 FILENAME.o -o FILENAME

Or the same thing in c (with inline assembly):

#include <stdio.h>

extern char src;
asm("src: .incbin \"" __FILE__ "\"\n.byte 0");

int main(int argc, char *argv[]) {
    unlink(argv[0]);
    FILE *file = fopen(argv[0], "w");
    fprintf(file, "%s",&src);
    fclose(file);
    return 0;
}

When run, the program deletes itself, and then writes its source code to the same path as the executable was at. That way, the deleted file can always be retrieved by recompiling, even if you lost the original source code.

There must be a better way to do this in c (or maybe not), but I don't know any.

Isn't that much better than just having it delete itself!

do not require hard coded path
Source Link
Ian D. Scott
  • 2.1k
  • 1
  • 13
  • 15

x86_64 NASM Assembly for Linux

This assembly program replaces itself with its source code. It essentially "decompiles" itself, replacing the binary.

SECTION .data
    source incbin "disassemble-self" __FILE__
    len equ $ - source
    name db "disassemble-self",0

SECTION .text
global _start

_start: 

    movpop rdi   rax        ;number of parameters
    pop rdi           ;path to executable, 87parameter of unlink and ;unlinkopen

    mov    rdirax, name87    ;unlink
    syscall
    cmp    rax, 0
    jl     error

    mov    rax, 2     ;open
    mov    rdi, name
    mov    rsi, 0101o ;O_WRONLY O_CREAT
    mov    rdx, 0600o ;permissions on created file
    syscall
    cmp    rax, 0
    jl     error
    mov    rdi, rax   ;file (return value of open)

    mov    rax, 1     ;write
    mov    rsi, source
    mov    rdx, len
    syscall
    cmp    rax, 0
    jl     error

    mov    rax, 60    ;exit
    mov    rdi, 0     ;return code
    syscall

    error:
        mov    rax, 60
        mov    rdi, 1
        syscall

Save to a file named "disassemble-self" and compileCompile with:

nasm -f elf64 disassemble-selfFILENAME
ld -m elf_x86_64 disassemble-selfFILENAME.o -o disassemble-selfFILENAME

MuchWhen run, the program deletes itself, and then writes its source code to the same path as the executable was at. That way, the deleted file can always be retrieved by recompiling, even if you lost the original source code.

Isn't that much better than just deletinghaving it delete itself!

x86_64 NASM Assembly for Linux

This assembly program replaces itself with its source code. It essentially "decompiles" itself, replacing the binary.

SECTION .data
    source incbin "disassemble-self" 
    len equ $ - source
    name db "disassemble-self",0

SECTION .text
global _start

_start:
    mov    rax, 87    ;unlink
    mov    rdi, name
    syscall
    cmp    rax, 0
    jl     error

    mov    rax, 2     ;open
    mov    rdi, name
    mov    rsi, 0101o ;O_WRONLY O_CREAT
    mov    rdx, 0600o ;permissions on created file
    syscall
    cmp    rax, 0
    jl     error
    mov    rdi, rax   ;file (return value of open)

    mov    rax, 1     ;write
    mov    rsi, source
    mov    rdx, len
    syscall
    cmp    rax, 0
    jl     error

    mov    rax, 60    ;exit
    mov    rdi, 0     ;return code
    syscall

    error:
        mov    rax, 60
        mov    rdi, 1
        syscall

Save to a file named "disassemble-self" and compile with:

nasm -f elf64 disassemble-self
ld -m elf_x86_64 disassemble-self.o -o disassemble-self

Much better than just deleting itself!

x86_64 NASM Assembly for Linux

This assembly program replaces itself with its source code. It essentially "decompiles" itself, replacing the binary.

SECTION .data
    source incbin __FILE__
    len equ $ - source

SECTION .text
global _start

_start: 

    pop rdi           ;number of parameters
    pop rdi           ;path to executable, parameter of unlink and open

    mov    rax, 87    ;unlink
    syscall
    cmp    rax, 0
    jl     error

    mov    rax, 2     ;open
    mov    rsi, 0101o ;O_WRONLY O_CREAT
    mov    rdx, 0600o ;permissions on created file
    syscall
    cmp    rax, 0
    jl     error
    mov    rdi, rax   ;file (return value of open)

    mov    rax, 1     ;write
    mov    rsi, source
    mov    rdx, len
    syscall
    cmp    rax, 0
    jl     error

    mov    rax, 60    ;exit
    mov    rdi, 0     ;return code
    syscall

    error:
        mov    rax, 60
        mov    rdi, 1
        syscall

Compile with:

nasm -f elf64 FILENAME
ld -m elf_x86_64 FILENAME.o -o FILENAME

When run, the program deletes itself, and then writes its source code to the same path as the executable was at. That way, the deleted file can always be retrieved by recompiling, even if you lost the original source code.

Isn't that much better than just having it delete itself!

Source Link
Ian D. Scott
  • 2.1k
  • 1
  • 13
  • 15
Loading