From e8de8442cecce54fc4f372dc2dacecc7abca23ae Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Thu, 27 May 2021 08:04:17 +0100 Subject: Moved archived notes from html to md. 5 Articles --- .../asciitex_ascii_text_formating_utility.md | 25 +++ md/writeup/create_elf_file_from_scratch.md | 187 +++++++++++++++++ md/writeup/gdb_helper_functions.md | 224 +++++++++++++++++++++ md/writeup/microbbs_minimalistic_bbs_system.md | 76 +++++++ md/writeup/serial_gps_data_reading_utility.md | 88 ++++++++ 5 files changed, 600 insertions(+) create mode 100644 md/writeup/asciitex_ascii_text_formating_utility.md create mode 100644 md/writeup/create_elf_file_from_scratch.md create mode 100644 md/writeup/microbbs_minimalistic_bbs_system.md create mode 100644 md/writeup/serial_gps_data_reading_utility.md (limited to 'md/writeup') diff --git a/md/writeup/asciitex_ascii_text_formating_utility.md b/md/writeup/asciitex_ascii_text_formating_utility.md new file mode 100644 index 0000000..5b89eac --- /dev/null +++ b/md/writeup/asciitex_ascii_text_formating_utility.md @@ -0,0 +1,25 @@ +title:ASCIITex ascii text formatting utility +keywords:ascii,utilities + +# ASCIITex ascii text formatting utility + +ASCIITex is attempt to write tool that formats text to ascii style. +Format width to 80 char width add some title generation and support of RFC +is planned in future. With this tool should be possible to generate +e-zines ;]. + +Use: +Minimal features and its usable for real world ascii text. + +```bash +./asciitex file.at file.txt +``` + + +## Links +https://github.com/FreeArtMan/asciitex + +## Downloads +http://archive.main.lv/files/writeup/asciitex_ascii_text_formating_utility/asciitex-0.0.2.tar.gz + + diff --git a/md/writeup/create_elf_file_from_scratch.md b/md/writeup/create_elf_file_from_scratch.md new file mode 100644 index 0000000..2bed7bc --- /dev/null +++ b/md/writeup/create_elf_file_from_scratch.md @@ -0,0 +1,187 @@ +title:Create ELF file from scratch +keywords:elf,linux + +# Create ELF file from scratch +## Creating smallest possible elf file. + +### Structure of ELF file: +Elf header +Program header +Code Part +Data Part + +C structure of ELF header /usr/include/elf.h: + +```c +typedef struct +{ + unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */ + Elf64_Half e_type; /* Object file type */ + Elf64_Half e_machine; /* Architecture */ + Elf64_Word e_version; /* Object file version */ + Elf64_Addr e_entry; /* Entry point virtual address */ + Elf64_Off e_phoff; /* Program header table file offset */ + Elf64_Off e_shoff; /* Section header table file offset */ + Elf64_Word e_flags; /* Processor-specific flags */ + Elf64_Half e_ehsize; /* ELF header size in bytes */ + Elf64_Half e_phentsize; /* Program header table entry size */ + Elf64_Half e_phnum; /* Program header table entry count */ + Elf64_Half e_shentsize; /* Section header table entry size */ + Elf64_Half e_shnum; /* Section header table entry count */ + Elf64_Half e_shstrndx; /* Section header string table index */ +} Elf64_Ehdr; +``` + +Structure of Program header file /usr/include/elf.h: + +```c +typedef struct +{ + Elf64_Word p_type; /* Segment type */ + Elf64_Word p_flags; /* Segment flags */ + Elf64_Off p_offset; /* Segment file offset */ + Elf64_Addr p_vaddr; /* Segment virtual address */ + Elf64_Addr p_paddr; /* Segment physical address */ + Elf64_Xword p_filesz; /* Segment size in file */ + Elf64_Xword p_memsz; /* Segment size in memory */ + Elf64_Xword p_align; /* Segment alignment */ +} Elf64_Phdr; +``` + +This structures is all what we need to make our ELF file. +Now we will look inside kernel source and see that +we need only one program header for our program. All big programs +using usually two program headers one for code and one for data. + +/linux-3.3.1/fs/binfmt_elf.c:605 + +```c +if (loc->elf_ex.e_phnum < 1 || + loc->elf_ex.e_phnum > 65536U / sizeof(struct elf_phdr)) + goto out; +``` + +Step by step there should be filled all +fields of the ELF header structure. + +```c +typedef struct +{ + unsigned char e_ident[EI_NIDENT]; /* default values of ELFMAG,ELFCLASS64,ELFDATA2LSB */ + Elf64_Half e_type; /* we making executable then it would be ET_EXEC */ + Elf64_Half e_machine; /* Architecture is 0x3e(EM_X86_64) + (not from elf header + from /binutils/include/elf/common.h) */ + Elf64_Word e_version; /* Object file version EV_CURRENT */ + Elf64_Addr e_entry; /* Entry point virtual address points to + main function it is with label entrypoint */ + Elf64_Off e_phoff; /* Program header table file offset */ + offset of program header sizeof(Elf64_Ehdr) + Elf64_Off e_shoff; /* Section header table file offset + there is no section header */ + Elf64_Word e_flags; /* No processor-specific flags + */ + Elf64_Half e_ehsize; /* ELF header size in bytes + 0x40 sizeof(Elf64_Ehdr) + Elf64_Half e_phentsize; /* Program header table entry size + 0x38 sizeof(Elf64_Phdr) */ + Elf64_Half e_phnum; /* Program header table entry count + 0x01 */ + Elf64_Half e_shentsize; /* Section header table entry size + I put 0x40 */ + Elf64_Half e_shnum; /* Section header table entry count + 0x00 */ + Elf64_Half e_shstrndx; /* There is no section header and + string table index is 0x0 then */ +} Elf64_Ehdr; +``` + +With program header we will tell kernel how to load our file in memory +and with part of file will be mmaped to needed address. As our data +and code is placed in one address space and kernel ELF source says +that there is enough with 1 program header then we will use only 1. + +```c +typedef struct +{ + Elf64_Word p_type; /* Segment type PT_LOAD */ + Elf64_Word p_flags; /* Segment flags PF_X,PF_R,PF_W + as our memory should be readable, writable and + executable as it contains code and data */ + Elf64_Off p_offset; /* Segment file offset + point to offset of entry point label offset + in file */ + Elf64_Addr p_vaddr; /* Segment virtual address + 64bits programs is usually at 0x400000+code_file_offset*/ + Elf64_Addr p_paddr; /* Segment physical address + same as above*/ + Elf64_Xword p_filesz; /* Segment size in file + size of code and data if file */ + Elf64_Xword p_memsz; /* Segment size in memory + same as above */ + Elf64_Xword p_align; /* Segment alignment + same as all programs have on my CPU*/ +} Elf64_Phdr; +``` + +Now everything is ready. Only thing that is left is code some small code +that uses data. And it would be hello world + +```asm +mov eax, 1 +mov edx, 12 +mov rsi, qword 0x040009c ;address of string +mov edi, 1 +syscall + +xor edi, edi +mov eax, 60 +syscall + +msg db 'Hello World',0xA +``` + +To calculate offsets of code and data labels is used macro: + +```asm +macro doffset +{ + bits = 16 + display ' 0x' + repeat bits/4 + d = '0' + $ shr (bits-%*4) and 0Fh + if d > '9' + d = d + 'A'-'9'-1 + end if + display d + end repeat + display 13,10 +} +``` + + +Total size of executable on 64bit system: +ELF header size 0x40 +Program header 0x38 +Code size 0x24 +Data size 0xc +Total: 168 bytes + +If 32 bit system is used then need to find definitions of data structures +and retype some bytes. Also architecture variable need to be changed. + +## Future plans: +Add some shared libs and compile smallest possible program using +SDL graphics lib. + +## Code +Code is written and tested on x86_64. + + + +## Links +http://refspecs.freestandards.org/elf/elf.pdf + +## Source + +http://archive.main.lv/files/writeup/create_elf_file_from_scratch/small_elf_file.zip \ No newline at end of file diff --git a/md/writeup/gdb_helper_functions.md b/md/writeup/gdb_helper_functions.md index e69de29..5fc45af 100644 --- a/md/writeup/gdb_helper_functions.md +++ b/md/writeup/gdb_helper_functions.md @@ -0,0 +1,224 @@ +title:Notes on videos +keywords:math,statistics + +# GDB helper functions + +```text +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++ GDB helper functions + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++ INDEX + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +1. Intro +2. Source +3. First run +4. Breakpoints +5. Registers +6. Helper commands +7. ToDo +8. Links + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++ 1.Intro + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + +GDB is GNU debugger. It comes as standard tool in gcc toolchain and all distros +have it as package. It work on all arch'es that gcc supports and it also can be +used as remote debugger. To debug it uses Linux kernel debugging functionality +of ptrace. For first moment its quite confusing tool too many command to type +and it doesn't have GUI or TUI. + +What here will be added is command to show XMM registers, general purpose +registers and eflags with one command. This command make gdb more fun tool +to use. + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++ 2.Source + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + +You can find GDB source here http://ftp.gnu.org/gnu/gdb/ + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++ 3.First run + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + +For first run we have example of program that just prints some string and +that's all. If you just run program with debugger and you haven't given commands +to debugger it will run program as expected if everything is OK with program. + +SOURCE: main1.c + +#include + +{ + printf("Works fine\n"); +} + + +Firs run is + + gdb ./main1 + +then in gdb command line type + + (gdb) run + +And it will show + + Starting program: main1 + Works fine + [Inferior 1 (process XXX) exit normally] + +String "Works fine" comes from printf's. And as everything was alright with +program its terminated "normally". Lets start to go deeper in debugging things. + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++ 4.Breakpoints + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + +Now lets use break points first breakpoint when to see whats happens in +program is set at main/_start function of C program as its start point +of program. If program is written in assembler then there could be no +main function like in C but still there entry point to program. And +possible why how to get address of entry point(main/_start) to program is +with readelf utility + + >readelf -h ./main1 | grep Entry + + Entry point address: 0x400410 + +Setting breakpoint to main function first way is just + + >gdb ./main1 + (gdb)break main + Breakpoint 1 at 0x40050a + (gdb)run + Start program: ./main1 + Breakpoint 1, 0x040050a in main() + +Now we called C main function and stopped at needed location. +Entry point is different in C it could be settuped directly with address or + + (gdb) break _start + +breakpoint on address + + (gdb) break *0x400410 + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++ 5.Registers + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + +Moment when you need debugger is when something "broken" here is example +situation of broken code. + +SOURCE: main2.c + +int main() +{ + int i; + + i = i/0; +} + +Run until it break and see what happened + + gdb ./main2 + (gdb) run + Program received signal SIGFPE + (gdb) display/i $pc + => 0x4004c3 : idiv %ecx + + +when number is divided on zero CPU usually generates exception and stops +program showing that something bad is happened. + + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++ 6.Helper commands + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + +To improve your experience there could be written macroses that can improve +your experience with gdb. There is example of gdb macroses that could be useful +and if you want to use them you can put gdbalias file at same directory as +debugged file and load from gdb with + + (gdb)source gdbalias + +shr32 - show 32 bit general purpose registers +shr32a - show 32 bit registers with 16, 8 bit registers and system registers +shr64 - show 64 bit general purpose registers +shr64a - show 64 bit registers and 32/16/8 bit equivalents +shsse_float - show xmm registers and its 4 32 bit float values +shsse_double - show xmm registers and its 2 64 bit double values +shsse_i8 - show xmm registers and its 16 8 bit integer values +shsse_i16 - show xmm registers and its 8 16 bit integer values +shsse_i32 - show xmm registers and its 4 32 bit integer values +shsse_i64 - show xmm registers and its 2 64 bit integer values +s - one step in debugger +flags - show eflags +showrchanges - on every step show changes from previous step +sc - one step and show changed registers + +All register could be printed with gdb command + + printf + +registers name that are used is $rax, $eax, $ax, $ah, $al and all others + +shsee commands are showing xmm0 registers and what is inside depends +on you interpretation that why there is 8 registers + + $xmm0,$xmm1,$xmm2,$xmm3,$xmm4,$xmm5,$xmm6,$xmm7 + +and values depended on interpretation can be accessed as + +X - register index, Y - array index + + $xmmX.v4_float[0] + $xmmX.v2_double[0] + $xmmX.v16_int8[0] + $xmmX.v8_int16[0] + $xmmX.v4_int32[0] + $xmmX.v2_int64[0]h + +Changes on each step are made just by saving registers: + + set $oldrax = $rax + +and when changes happens if/else: + + if ($rax != $oldrax) + printf "RAX:0x016lX ", $rax + end + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++ 7.ToDo + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +Add more descriptions and more basic topics how to use gdb + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++ 8.Links + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +[1] http://ftp.gnu.org/gnu/gdb/ +[2] https://en.wikipedia.org/wiki/GNU_Debugger +[3] https://github.com/gdbinit/Gdbinit/blob/master/gdbinit +[4] https://sourceware.org/gdb/onlinedocs/gdb/Define.html +[5] https://github.com/FreeArtMan/gdbalias +[6] http://www.delorie.com/gnu/docs/gdb/gdb_28.html +[7] http://www.delorie.com/gnu/docs/gdb/gdb_29.html +``` +## Downloads + +http://archive.main.lv/files/writeup/gdb_helper_functions/gdbalias + diff --git a/md/writeup/microbbs_minimalistic_bbs_system.md b/md/writeup/microbbs_minimalistic_bbs_system.md new file mode 100644 index 0000000..4f7b8a1 --- /dev/null +++ b/md/writeup/microbbs_minimalistic_bbs_system.md @@ -0,0 +1,76 @@ +title:Notes on videos +keywords:math,statistics + +# MicroBBS minimalistic BBS system + + +```text +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++ MicroBBS + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++ INDEX + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + 1. Intro + 2. Goal + 3. Usage + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++ 1.Intro + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + +MicroBBS is attempt to write basic plain BBS that could be run on Linux/BSD +systems on any kind of hardware. Also minimal dependencies should be used. No +scripting, no databases, no external libraries as dependencies. Everything +should be self-sufficient and easily compiled and lunched. + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++ 2.Goal + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +basic chat +article publishing +file sharing +message board +maybe door-games + +No extra stuff and extra super mega features. Also i have downloaded +old BBS sources and try to write everything in old school style =). Now +its is only 14kb and 50 more kb to go. And yes i will try not to go over +64kb size of executable. + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++ 3.Usage + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +Not alot works now just some picture shows and one article could be readed. +Its still in alfa stage. + +Type in shell: + +microbbs + +And bbs should run in your terminal. You can put it as default shell for ssh + + + + + + +Links +https://github.com/FreeArtMan/microbbs + +``` + +## Downloads + + + +microbbs-0.1.7.tar.gz - +6KiB - +http://archive.main.lv/files/writeup/microbbs_minimalistic_bbs_system/microbbs-0.1.7.tar.gz +microbbs-0.2.2.tar.gz - +49KiB - +http://archive.main.lv/files/writeup/microbbs_minimalistic_bbs_system/microbbs-0.2.2.tar.gz diff --git a/md/writeup/serial_gps_data_reading_utility.md b/md/writeup/serial_gps_data_reading_utility.md new file mode 100644 index 0000000..b3d233f --- /dev/null +++ b/md/writeup/serial_gps_data_reading_utility.md @@ -0,0 +1,88 @@ +title:Serial GPS data reading utility +keywords:gps,serial,uart,tty + +# Serial GPS data reading utility +Serial usb gps deviceses can be used trought serial consoles +or some other libraries. From shell need some basic configuaration +at begining to use serial device from shell. This command +provide simple way how to do it. +This code opens /dev/tty* device, setup baud rate to 4800 +and outputs line by line recieved data. +When device connected ther could be that is not in NMEA mode +it could be switched with: + +``` +gpsctl -n /dev/ttyUSB0 +``` + +Then you can connect with it with some serial terminal(dont forget baudrate +could be 4800 or 9600): + +``` +minicom -D /dev/ttyUSB0 +``` + +Now we can use our gpsr utility + +``` +./gpsr -d /dev/ttyUSB0 -c 0 +./gpsr -d /dev/ttyACM0 -c 1 +``` + +NMEA format is csv like and it easyly can be used from shell. Here is +exmple how it looks: + +``` +$GPRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A +``` + +Where: +RMC Recommended Minimum sentence C +123519 Fix taken at 12:35:19 UTC +A Status A=active or V=Void. +4807.038,N Latitude 48 deg 07.038' N +01131.000,E Longitude 11 deg 31.000' E +022.4 Speed over the ground in knots +084.4 Track angle in degrees True +230394 Date - 23rd of March 1994 +003.1,W Magnetic Variation +*6A The checksum data, always begins with * + +Get time from GPS + +``` +./gpsr -d /dev/ttyUSB0 -c 100 | stdbuf -o0 grep -w "GPRMC" | cut -d',' -f2 +``` + +## REQUIREMENTS +GCC C, minicom, shell, GPS device + +## TESTED +I have tested everything with GPS devices BU-353 and with +some device that havenot any visual marks but have chip +from u-blox manufacturer. + +## COMPILE: + +``` +gcc buf.c -c +gcc serial_tty.c -c +gcc serial_tty.o buf.o gpsr.c -o gpsr +``` + +## TODO: +there could be added baudrate set as params +loging in csv,xml,json files +make longterm test for stability + + +## Links +http://en.wikipedia.org/wiki/NMEA_0183 +http://www.gpsinformation.org/dale/nmea.htm +http://home.mira.net/~gnb/gps/nmea.html +http://linux.die.net/man/1/minicom + +## Downloads +gpsr.tar.gz - 3KiB - +http://archive.main.lv/files/writeup/serial_gps_data_reading_utility/gpsr.tar.gz + -- cgit v1.2.3