-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
get info registers #1
base: master
Are you sure you want to change the base?
Conversation
Signed-off-by: Tiago Natel de Moura <tiago.natel@neoway.com.br>
Signed-off-by: Tiago Natel de Moura <tiago.natel@neoway.com.br>
Signed-off-by: Tiago Natel de Moura <tiago.natel@neoway.com.br>
@tiago4orion, hi there! 😄
For this kind of software, C, undoubtedly. We can try to use C over assembly but assembly still will be necessary.
Smaller C is an interesting choice. For now, it's ok.
Perhaps a mix of linux kernel, pike, ritchie...
The way it's we're destroying If using functions, I think they should be Another way could be: #include <stdio.h>
typedef struct Regs {
int eax;
int ebx;
} Regs;
void read_regs(Regs *regs)
{
register int eax asm ("eax");
register int ebx asm ("ebx");
}
int main(void)
{
Regs regs;
read_regs(®s);
printf("eax=%04x\n", regs.eax);
printf("ebx=%04x\n", regs.ebx);
return 0;
} I think that this line is misguided: https://github.com/c0defellas/exp/blob/xxx/xxx/regs.c#L68 Won't it return the address of the code that calls eip()? https://github.com/c0defellas/exp/blob/xxx/xxx/regs.c#L16 That's why I told you about inline functions. #defines perhaps are better in this case. |
Yeah, mixing C and assembly in the same file isn't a good idea. It was only the most fast way to get things done. Worst problem with this approach is that C files will have architecture dependent code (32bit x86 in this case). Making the whole project non-portable. Maybe the best option is just glue C and assembly files at link time. But then, the next question: What would be a good project layout? What do you think of: λ> find .
./src
./src/port
./src/port/main.c
./src/386
./src/386/register.asm
./src/amd64
./src/amd64/register.asm
./Makefile
λ>
SmallerC is good for start, but not good for long term support. We can support gcc or other compiler from day zero also. We can adjust CI to build both.
We're not destrying eax, but just setting the return value of the function to what we want. But maybe I did not understand what you said. We can set the data structure directly also, but this is not much more complex and error prone? Changing order of fields in the
Sorry, but I don't know this The use of Regarding using
Yeah, thanks for catching this up. This is my default way of getting
Maybe this should be addressed in another way. I think inlining functions nor using macros won't work. Separating C and assembly will require a compiler independent solution. Thank you! |
👍
Really nice. I liked it.
We're relying on the calling convention, so nothing granted here for other archs and conventions. i32 eax() {
/* by calling convention */
} Getting
Could you point me some references?
It's in C99. Is C89 a bit oldish, perhaps? Let's discuss about standards versions, pointing out their benefits and drawbacks.
Inline is complicated too. From wikipedia
Now I'm only seeing macros as a solution. What do you think about inline vs macro?
👍 I realized that that code is shellcode pimp style. 😄 |
By ANSI C, as the statement above says, the void read_regs(Regs *regs)
{
register int eax asm ("eax");
register int ebx asm ("ebx");
}
Ok, to align expectations I'll re-post here the initial proposal of the software to help we define what would be the next PoC.
You are right about pusha/popa, we'll definitely need them, but I was thinking in use it to save/restore registers before/after every call into debugger code. But maybe the correct approach should be getting register data directly from memory. The debugger will have a parser of assembly syntax and maintain the plain list of opcodes to execute in memory. The entire state of the debugger will have to be saved every time we switch between usercode <-> dbg code. I think we'll need to make a new PoC with a basic interpreter working to validate the ideas. You must be right regarding adding a macro to save/restore registers before entering/exiting dbg managed code. Thanks =) |
Sorry, forgot to answer that question. The The content of the variable will be stored in a register only if the compiler handle The storage location of a variable is architecture/compiler dependent. We cannot rely on that. ken Thompson compilers just ignores I'm not 100% sure, but using register keyword could have the drawback of disabling specific gcc code optimizations also. GCC manual says that after setting storage class of a variable to register, this register will be unused even with variable isn't used anymore (dead code elimination do not free it). |
You must try void read_regs(Regs *regs)
{
register int eax __asm__ ("eax");
register int ebx __asm__ ("ebx");
}
Got it. About parsing assembly directly, what do you think of a set of options for setting and loading registers and memory? It may be hard to make a parse or grammars for every architecture/ISA. Instead of
we could run
The parsing would be simpler. And the core functions to set a register wouldn't be complex because it will bind to the correct assembly in the specified architecture tree.
I think it will be necessary. We'll have to manage signals. |
We need to stick with a standard or stick with a compiler implementation. I prefer the first option and I have strong feelings about C99 ... it generates complex binaries only to make syntax sugars in source code (like for(int i = 0; i < x; i++) ... and other syntax constructs inherited from C++). |
Interesting. I need to think more about it. This will need to generate arch dependent code anyway, but maybe simplify things. |
Guys, just so you know about this discussion. |
asm("__geteip: mov eax, [esp]\n" | ||
" ret\n" | ||
"call __geteip"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's awesome.... i didn't know asm allowed to define function like that =)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That inlined assembly works as a label definition. ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the old school asm
directive. C and asm was cool before gcc introduced the extended inline asm thing...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GCC dislikes that because people unaware of calling convention could mess things up trying to set local C variables from inside asm
directives... To do that, you need to set things directly to ebp+8, ebp+16, and so on (arch dependent) and this could change depending on compiler optimizations (and compiler flags). To pack local variables, the order of variables in the stack could change also.
Then, to use oldish asm
directive best to leave it alone inside the function (as I did). Never put C and asm together inside same function...
Perhaps the syntax may be
We could stick with C89 and build the internal hacks as we need them. |
@geyslan Good =) The bad point is that we cannot just paste assembly code in the debugger interpreter and get things executed on the fly =( |
@tiago4orion I see, it's an issue. I have tought that we could use commands or a generic assembly for all archs. However, I think the last idea (own assembly) will demand more work. |
Who knows both of them? Noobs like me will be able to use the "easier" commands that translate themselves in actual assembly. I liked it. =) |
@CoolerVoid Hey my friend. Happy to see you here =) |
Your proposed syntax could be the architecture independent way of using the debugger =) |
@CoolerVoid I'll definitely watch this presentation. Thanks ! |
Just reading A New C Compiler from Ken, got this:
It's very nice. 👍 |
@CoolerVoid I think that |
PoC to discuss several things:
and so on.
@geyslan