Compare commits
5 Commits
labs/08
...
4c423c7acc
| Author | SHA1 | Date | |
|---|---|---|---|
| 4c423c7acc | |||
| a338ac968e | |||
| 386c7be1a7 | |||
| 34d0c33e87 | |||
| 65d5c3f5c3 |
109
01-asm-basics/main.c
Normal file
109
01-asm-basics/main.c
Normal file
@ -0,0 +1,109 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "substitutions.h"
|
||||
|
||||
#define PortCan0 0x40
|
||||
|
||||
void beep(unsigned iTone, unsigned iDlit);
|
||||
|
||||
void delay(unsigned int ms)
|
||||
{
|
||||
usleep(ms * 1000);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
long int lCnt = 0;
|
||||
int iA = 0x1234;
|
||||
|
||||
char *pT = (char *)0x46C;
|
||||
printf("\nПечатаем 10 раз значение байта с известным адресом\n");
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
printf(" \n %d ", *pT);
|
||||
}
|
||||
printf("\n Для продолжения нажмите любую клавишу \n");
|
||||
system("pause"); // Ждем нажатия клавиши
|
||||
|
||||
printf("\n Читаем содержимое порта с адресом 40 с помощью функции Си \n");
|
||||
printf("\n Для выхода из цикла - нажмите любую клавишу \n");
|
||||
|
||||
set_input_mode();
|
||||
while (isKeyPressed() == 0) {
|
||||
printf("\n Порт40 = %d", inp(PortCan0));
|
||||
delay(500);
|
||||
}
|
||||
reset_input_mode();
|
||||
|
||||
system("pause");
|
||||
printf("\n Читаем содержимое порта с адресом 40 ассемблером \n");
|
||||
|
||||
set_input_mode();
|
||||
while (isKeyPressed() == 0) {
|
||||
asm {
|
||||
push ax
|
||||
in al,0x40
|
||||
}
|
||||
unsigned char Tmm = _AL;
|
||||
asm pop ax
|
||||
delay(500);
|
||||
printf("\n Порт40 = %d", Tmm);
|
||||
}
|
||||
reset_input_mode();
|
||||
system("pause");
|
||||
printf("\n Для продолжения - нажмите любую клавишу \n");
|
||||
system("pause");
|
||||
|
||||
long *pTime = (long *)0x46C;
|
||||
set_input_mode();
|
||||
while (isKeyPressed() == 0) {
|
||||
printf("\n %ld", *pTime);
|
||||
delay(1000);
|
||||
}
|
||||
reset_input_mode();
|
||||
system("pause");
|
||||
|
||||
int Time;
|
||||
set_input_mode();
|
||||
while (isKeyPressed() == 0) {
|
||||
asm push ds
|
||||
asm push si
|
||||
asm mov ax, 40h
|
||||
asm mov ds, ax
|
||||
asm mov si, 0x6C
|
||||
asm mov ax, [ds : si]
|
||||
asm mov Time, ax
|
||||
asm pop si
|
||||
asm pop ds
|
||||
|
||||
printf("\n %d", Time);
|
||||
delay(300);
|
||||
}
|
||||
reset_input_mode();
|
||||
|
||||
beep(400, 200);
|
||||
for (lCnt = 0; lCnt < 1000000; lCnt++) {
|
||||
a1:
|
||||
asm {
|
||||
mov ax,iA
|
||||
mov ax,iA
|
||||
mov ax,iA
|
||||
mov ax,iA
|
||||
mov ax,iA
|
||||
mov ax,iA
|
||||
mov ax,iA
|
||||
mov ax,iA
|
||||
mov ax,iA
|
||||
a2:
|
||||
mov ax,iA
|
||||
}
|
||||
}
|
||||
beep(400, 200);
|
||||
}
|
||||
|
||||
void beep(unsigned iTone, unsigned iDlit) {
|
||||
sound(iTone);
|
||||
delay(iDlit);
|
||||
nosound();
|
||||
}
|
||||
61
01-asm-basics/substitutions.c
Normal file
61
01-asm-basics/substitutions.c
Normal file
@ -0,0 +1,61 @@
|
||||
#include "substitutions.h"
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <termios.h>
|
||||
|
||||
/* Use this variable to remember original terminal attributes. */
|
||||
|
||||
struct termios saved_attributes;
|
||||
|
||||
void reset_input_mode()
|
||||
{
|
||||
tcsetattr (STDIN_FILENO, TCSANOW, &saved_attributes);
|
||||
}
|
||||
|
||||
void set_input_mode()
|
||||
{
|
||||
struct termios tattr;
|
||||
char *name;
|
||||
|
||||
/* Make sure stdin is a terminal. */
|
||||
if (!isatty (STDIN_FILENO))
|
||||
{
|
||||
fprintf (stderr, "Not a terminal.\n");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* Save the terminal attributes so we can restore them later. */
|
||||
tcgetattr (STDIN_FILENO, &saved_attributes);
|
||||
atexit (reset_input_mode);
|
||||
|
||||
/* Set the funny terminal modes. */
|
||||
tcgetattr (STDIN_FILENO, &tattr);
|
||||
tattr.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO. */
|
||||
tattr.c_cc[VMIN] = 1;
|
||||
tcsetattr (STDIN_FILENO, TCSAFLUSH, &tattr);
|
||||
}
|
||||
|
||||
void delay(unsigned int ms)
|
||||
{
|
||||
usleep(ms * 1000);
|
||||
}
|
||||
|
||||
char isKeyPressed()
|
||||
{
|
||||
char key_handler = 0;
|
||||
read(STDIN_FILENO, &key_handler, 1);
|
||||
if (key_handler > 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
//int main()
|
||||
//{
|
||||
// set_input_mode();
|
||||
// while (isKeyPressed() == 0) {}
|
||||
// printf("ok");
|
||||
// reset_input_mode();
|
||||
//}
|
||||
9
01-asm-basics/substitutions.h
Normal file
9
01-asm-basics/substitutions.h
Normal file
@ -0,0 +1,9 @@
|
||||
#ifndef SUBSTITUTIONS_H
|
||||
#define SUBSTITUTIONS_H
|
||||
|
||||
void reset_input_mode();
|
||||
void set_input_mode();
|
||||
void delay(unsigned int ms);
|
||||
char isKeyPressed();
|
||||
|
||||
#endif
|
||||
@ -1,17 +0,0 @@
|
||||
|
||||
all: test testee
|
||||
|
||||
test: test.o
|
||||
gcc $^ -o $@ -g
|
||||
|
||||
test.o: test.c
|
||||
gcc -c $^ -g
|
||||
|
||||
testee: testee.o
|
||||
ld $^ -o $@
|
||||
|
||||
testee.o: testee.asm
|
||||
nasm -felf64 $^ -o $@ -g
|
||||
|
||||
clean:
|
||||
rm *.o test *.gch testee
|
||||
@ -2,13 +2,3 @@
|
||||
|
||||
## Отладчик
|
||||
|
||||
Всю основу написания отладчиков на Linux держит на своих плечах один единственный систенмый вызов - ptrace ("process trace"). Он позволяет выполнять все то, что мы привыкли делать в дебаггере - шаг с обходом, шаг с заходом, просмотр регистров, мучения оперативы и прочие радости отладки. Как тут помочь не знаю, потому что подробный гайд займет целую статью. Предложение простое - почитать великую статью по написанию (первые 2 части покроют весь материал этой лабы): https://blog.tartanllama.xyz/writing-a-linux-debugger-setup/
|
||||
|
||||
В основном пробелмы будут сопряжены только с чтением того, что вообще может делать ptrace, а может он почти все, что нас инетерсует, тольк вот интерфейс у него мягко скажем не по solid.
|
||||
|
||||
Внутри своего кода я ввел специальный макрос `DONT_CARE`, который призван сигнализировать о том, что при определенном флаге некоторые поля будут игнорироваться.
|
||||
|
||||
В остальном удачи и учтите, что программа останавливается как-то по своей логике, а не по логике обычного человека. Она остановится не в момент ptrace (PTRACE_TRACEME, ...), а в момент execv. Почему так? да фиг его знает если честно. Скорее всего просто в этот момент программа посылает сигнал о том, что образ потока был переписан. Других идей у меня нет.
|
||||
|
||||
Собственно именно поэтому continue прожималось мной 2 раза, а не один, как хотелось бы верить по логике
|
||||
|
||||
|
||||
@ -1,67 +0,0 @@
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#include<unistd.h>
|
||||
#include<sys/types.h>
|
||||
#include<sys/ptrace.h>
|
||||
#include<sys/user.h>
|
||||
#include<sys/wait.h>
|
||||
|
||||
#define DONT_CARE 0
|
||||
|
||||
int stats;
|
||||
struct user_regs_struct regs;
|
||||
|
||||
void continue_execution(pid_t pid)
|
||||
{
|
||||
ptrace(PTRACE_CONT, pid, DONT_CARE, DONT_CARE);
|
||||
waitpid(pid, &stats, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
void print_rax(pid_t pid)
|
||||
{
|
||||
ptrace(PTRACE_GETREGS, pid, DONT_CARE, ®s);
|
||||
printf("rax = %llu\n", regs.rax);
|
||||
return;
|
||||
}
|
||||
|
||||
void step(pid_t pid)
|
||||
{
|
||||
ptrace(PTRACE_SINGLESTEP, pid, DONT_CARE, DONT_CARE);
|
||||
waitpid(pid, &stats, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("procces is run ");
|
||||
pid_t pid;
|
||||
pid = fork();
|
||||
if (pid==0)
|
||||
{
|
||||
printf(" -- child\n");
|
||||
ptrace(PTRACE_TRACEME, 0, 0, 0);
|
||||
execl("./testee", "testee", NULL);
|
||||
printf("____AFTER_TRACE_CHILD____\n");
|
||||
}
|
||||
else if(pid > 0)
|
||||
{
|
||||
char buff;
|
||||
printf(" -- parrent\n");
|
||||
continue_execution(pid); // to start app
|
||||
continue_execution(pid);
|
||||
if (stats & SIGTRAP)
|
||||
{
|
||||
printf("--BREAKPOINT--\n");
|
||||
while (stats != 0)
|
||||
{
|
||||
read(0, &buff, 1);
|
||||
print_rax(pid);
|
||||
step(pid);
|
||||
}
|
||||
}
|
||||
|
||||
printf("____PROCESS_WAS_TERMINATED____\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -1,26 +0,0 @@
|
||||
global _start
|
||||
|
||||
section .data
|
||||
msg: db "I'm alive", `\n`, 0
|
||||
msg_len equ $-msg
|
||||
|
||||
section .text
|
||||
_start:
|
||||
xor rax, rax,
|
||||
add rax, 1
|
||||
;int3
|
||||
add rax, 12
|
||||
;int3
|
||||
mov rax, 33
|
||||
;int3
|
||||
|
||||
mov rax, 1
|
||||
mov rdi, 1
|
||||
mov rsi, msg
|
||||
mov rdx, msg_len
|
||||
syscall
|
||||
int3
|
||||
|
||||
mov rax, 60
|
||||
mov rdi, 0
|
||||
syscall
|
||||
Reference in New Issue
Block a user