Skip to content

Instantly share code, notes, and snippets.

@warabanshi
warabanshi / bf_compiler.py
Created January 3, 2013 15:03
easy brainf*ck compiler by python
import sys, struct
from ctypes import *
libc = cdll.LoadLibrary("libc.so.6")
free = libc.free
mmap = libc.mmap
mmap.restype = c_void_p
munmap = libc.munmap
munmap.argtype = [c_void_p, c_size_t]
@warabanshi
warabanshi / Attributes.py
Last active December 14, 2015 01:19
make ELF part4
pType = {
'NULL' : 0,
'LOAD' : 1,
'DYNAMIC' : 2,
'INTERP' : 3,
'NOTE' : 4,
'SHLIB' : 5,
'PHDR' : 6,
'TLS' : 7,
'NUM' : 8,
@warabanshi
warabanshi / elf.c
Last active December 14, 2015 10:59
binutils/bfd/elf.c _bfd_elf_map_sections_to_segments()
/* Set up a mapping from BFD sections to program segments. */
bfd_boolean
_bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
{
unsigned int count;
struct elf_segment_map *m;
asection **sections = NULL;
const struct elf_backend_data *bed = get_elf_backend_data (abfd);
bfd_boolean no_user_phdrs;
BITS 64
EXTERN putchar
EXTERN exit
GLOBAL _start
SECTION .text
_start:
mov edi, 'T'
call putchar
mov edi, 42
@warabanshi
warabanshi / Eh.py
Last active December 14, 2015 15:38
from Header import Header
from elf.Utils import *
# ELF header
class Eh(Header):
org = 0x400000
def getOrg(self):
return self.org
from elf.Utils import *
from elf.components.headers.Eh import Eh
from elf.components.headers.Sh import Sh
from elf.components.Section import Section
from elf.components.SectionAggregator import SectionAggregator
# teardown ELF file
f = open('test.out')
byteList = map(lambda x: int(ord(x)), f.read())
@warabanshi
warabanshi / readelf
Created April 8, 2013 16:51
readelf -a test.out
ELF Header:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: Advanced Micro Devices X86-64
Version: 0x1
segment .data
switch: dq main.case0
dq main.case1
dq main.case2
i: dq 2
segment .text
global main ; tell linker about main
main:
mov rax, [i] ; move i to rax
jmp [switch+rax*8] ; switch(i)
sum = 0;
i = 0;
while ( i < 64 ) {
sum += data & 1;
data = data >> 1;
i++;
}
segment .data
data dq 0xfedcba9876543210
sum dq 0
segment .text
global main
main:
push rbp
mov rbp, rsp
sub rsp, 16