Skip to content

Commit

Permalink
Fix problem with a few function call in one line
Browse files Browse the repository at this point in the history
  • Loading branch information
ReKreker authored and ReKreker committed Oct 16, 2022
1 parent b861098 commit 809c5f6
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.2 (16 Oct 2022)
## Changes
- switch from \_\_LINE\_\_ to \_\_COUNTER\_\_ in definition of imported func

# 1.1 (15 Oct 2022)
## Changes
- add changelog
Expand Down
11 changes: 6 additions & 5 deletions docs/common errors.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#### Multiple calls the same func at one line
```│translation.c:7:51: error: duplicate label ‘func_name_jmp_10’```
#### Dont use stack-based strings
Wrap any string in \_s macro to create stack-based strings with no xref to strings' segment.

How to fix:
```printf(_s("Leet is %d\n"), arg1*100+arg2/100); printf(_s("Trigger error"));```
```
printf("Hello, world!");
```
to
```
printf(_s("Leet is %d\n"), arg1*100+arg2/100);
printf(_s("Test"));
printf(_s("Hello, world!"));
```
4 changes: 2 additions & 2 deletions example/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ compile:
rm *.o

hook:
./../shooker ./ ./
./../src/shooker/__main__.py ./ ./

run:
LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:./ ./leet_add

gdb:
LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:./ gdb -q ./leet_add
LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:./ gdb -q ./leet_add
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "shooker"
version = "1.1.4"
version = "1.2"
authors = [
{ name="ReKreker" },
]
Expand Down
8 changes: 6 additions & 2 deletions src/shooker/internal/asm.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,13 @@ def patch_sub_values(self, func_cont: list, func_offs: int) -> list:
if lea_ip != 0 and sub_instr is not None and call_ip != 0:
sub_value = int.from_bytes(sub_instr.bytes[2:], "little")
relative_offs = func_offs + lea_ip - sub_value
sub_bytes = sub_instr.bytes[:2] + relative_offs.to_bytes(4, "little")
sub_bytes = sub_instr.bytes[:2] + relative_offs.to_bytes(
4, "little"
)

cont[sub_instr.address : sub_instr.address + sub_instr.size] = sub_bytes
cont[
sub_instr.address : sub_instr.address + sub_instr.size
] = sub_bytes

# zeroing to continue parse function
lea_ip, call_ip = 0, 0
Expand Down
19 changes: 8 additions & 11 deletions src/shooker/internal/cmpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,8 @@ def include_func(self, name: str, proto: str, addr: int) -> None:
if not name or not proto or not addr:
raise NotFound("Name/proto/addr for include func")

# use Labels as Values https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html
# and Statements and Expressions https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
label = f"UNIQ_LINE({name}_jmp)"
decl = (
f"#define {name} (({proto.replace('FUNC', '(*)')})"
+ "({"
+ f"{label}:(long)&&{label}-{addr};"
+ "}))"
f"#define {name} GET_FUNC({proto.replace('FUNC', '(*)')}, {name}, {addr})"
)
logging.debug(f"Include func {decl}")
self.inc_fncs.append(decl)
Expand All @@ -47,9 +41,12 @@ def assemble_transl(self) -> None:
self.code += "\n#define _s(string) ((char *)(const char []){string})\n"

# asm-trick for unique jump label for relative jump funcs
self.code += "\n#define CONCAT_(prefix, suffix) prefix##suffix"
self.code += "\n#define CONCAT(prefix, suffix) CONCAT_(prefix, suffix)"
self.code += "\n#define UNIQ_LINE(prefix) CONCAT(prefix##_, __LINE__)\n"
# use Labels as Values https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html
# and Statements and Expressions https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
self.code += "\n#define GET_FUNC_(proto, func_name, addr, cnt)" +\
" ((proto)({func_name##_jmp_##cnt:(long)&&func_name##_jmp_##cnt-addr;}))"
self.code += "\n#define GET_FUNC(proto, func_name, addr)" +\
" GET_FUNC_(proto, func_name, addr, __COUNTER__)\n"

# func declaration stuff
self.code += "\n".join(self.inc_fncs) + "\n"
Expand All @@ -72,7 +69,7 @@ def compile_transl(self, txt_addr: int) -> FuncsInfo:
# uncomment to look translation.c
# __import__("IPython").embed()

logging.debug("="*70 + "\n"+ self.code + "\n" + "="*70)
logging.debug("=" * 70 + "\n" + self.code + "\n" + "=" * 70)

cmd = [
self.cc,
Expand Down
2 changes: 1 addition & 1 deletion src/shooker/internal/inj.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def shook_sect_init(self) -> None:
section = Section(".shook", SECTION_TYPES.PROGBITS)
section += SECTION_FLAGS.EXECINSTR
section += SECTION_FLAGS.WRITE
section.content = [0]*0x500
section.content = [0] * 0x500
self.bin.add(section, loaded=True)

def shook_sect_fill(self, content: list) -> None:
Expand Down

0 comments on commit 809c5f6

Please sign in to comment.