Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
KOLANICH committed Oct 19, 2023
0 parents commit 3d6d8b7
Show file tree
Hide file tree
Showing 16 changed files with 512 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
charset = utf-8
indent_style = tab
indent_size = 4
insert_final_newline = true
end_of_line = lf

[*.{yml,yaml}]
indent_style = space
indent_size = 2
1 change: 1 addition & 0 deletions .github/.templateMarker
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
KOLANICH/python_project_boilerplate.py
8 changes: 8 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 2
updates:
- package-ecosystem: "pip"
directory: "/"
schedule:
interval: "daily"
allow:
- dependency-type: "all"
15 changes: 15 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: CI
on:
push:
branches: [master]
pull_request:
branches: [master]

jobs:
build:
runs-on: ubuntu-22.04
steps:
- name: typical python workflow
uses: KOLANICH-GHActions/typical-python-workflow@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
__pycache__
*.py[co]
/*.egg-info
*.srctrlbm
*.srctrldb
build
dist
.eggs
monkeytype.sqlite3
/.ipynb_checkpoints
51 changes: 51 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
image: registry.gitlab.com/kolanich-subgroups/docker-images/fixed_python:latest

variables:
DOCKER_DRIVER: overlay2
SAST_ANALYZER_IMAGE_TAG: latest
SAST_DISABLE_DIND: "true"
SAST_CONFIDENCE_LEVEL: 5
CODECLIMATE_VERSION: latest

include:
- template: SAST.gitlab-ci.yml
- template: Code-Quality.gitlab-ci.yml
- template: License-Management.gitlab-ci.yml

build:
tags:
- shared
- linux
stage: build
variables:
GIT_DEPTH: "1"
PYTHONUSERBASE: ${CI_PROJECT_DIR}/python_user_packages

before_script:
- export PATH="$PATH:$PYTHONUSERBASE/bin" # don't move into `variables`
- apt-get update
# todo:
#- apt-get -y install
#- pip3 install --upgrade
#- python3 ./fix_python_modules_paths.py

script:
- python3 -m build -nw bdist_wheel
- mv ./dist/*.whl ./dist/ELFRelocs-0.CI-py3-none-any.whl
- pip3 install --upgrade ./dist/*.whl
- coverage run --source=ELFRelocs -m --branch pytest --junitxml=./rspec.xml ./tests/test.py
- coverage report -m
- coverage xml

coverage: "/^TOTAL(?:\\s+\\d+){4}\\s+(\\d+%).+/"

cache:
paths:
- $PYTHONUSERBASE

artifacts:
paths:
- dist
reports:
junit: ./rspec.xml
cobertura: ./coverage.xml
1 change: 1 addition & 0 deletions Code_Of_Conduct.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
No codes of conduct!
88 changes: 88 additions & 0 deletions ELFRelocs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from enum import IntEnum

from .relocEnums import *
from .relocImpls import *


class SpecialSectionIndex(IntEnum):
UNDEF = 0
LORESERVE = 0xFF00
LOPROC = 0xFF00
BEFORE = 0xFF00
AFTER = 0xFF01
HIPROC = 0xFF1F
ABS = 0xFFF1
COMMON = 0xFFF2
HIRESERVE = 0xFFFF


class RelocWrapper:
def __init__(self, reloc, table):
self._reloc = reloc
self.type = table(reloc.entry["r_info_type"])

@property
def offset(reloc):
raise NotImplementedError()

@property
def addend(reloc):
raise NotImplementedError()

@property
def symbol_shndx(reloc):
raise NotImplementedError()

@property
def symbol_value(reloc): # reloc.symbol.st_value
raise NotImplementedError()


class Relocator:
def __init__(self, arch):
self.table = archTableMapping[arch]

def relocFromPointerRawAddr(self, raw) -> RelocWrapper:
raise NotImplementedError()

@property
def B(self):
return None

@property
def GOT(self):
return None

@property
def L(self):
return None

def G(self, *args):
return None

def Z(self, *args):
return None

def computeRelocatedPtr(self, ptrVirtualAddr, ptrValue):
reloc = self.relocFromPointerRawAddr(ptrVirtualAddr)
if reloc:
symVirtAddr = ptrValue + reloc.symbol_value
P = ptrVirtualAddr + reloc.offset

rImplName = reloc.type.name
if hasattr(ELF_Relocs_Impls, rImplName):
impl = getattr(ELF_Relocs_Impls, rImplName)

#print(rImplName, impl)

res = impl(symVirtAddr, reloc, P, GOT=self.GOT, G=self.G(), Z=self.Z(), L=self.L, B=self.B)

if isinstance(res, tuple):
format, relocatedRawPtr = res
else:
relocatedRawPtr = res
format = None

return format, relocatedRawPtr
else:
return None, ptrValue
38 changes: 38 additions & 0 deletions ELFRelocs/libs/LIEF.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
__all__ = ("LIEFRelocator",)
from ELFMachine import ELFMachine

from .. import Relocator, RelocWrapper


class LIEFRelocWrapper(RelocWrapper):
def __init__(self, reloc, table, symbol):
self._reloc = reloc
self.type = table(int(reloc.type))

@property
def offset(self):
return self._reloc.address

@property
def addend(self):
return self._reloc.addend

@property
def symbol_value(self):
return self._reloc.symbol.value


class LIEFRelocator(Relocator):
def __init__(self, liefBinary):
arch = ELFMachine(int(liefBinary.concrete.header.machine_type))
super().__init__(arch)
self.liefBinary = liefBinary

@property
def B(self):
return self.liefBinary.concrete.imagebase

def relocFromPointerRawAddr(self, raw) -> RelocWrapper:
backendReloc = self.liefBinary.concrete.get_relocation(raw)
if backendReloc is not None:
return LIEFRelocWrapper(backendReloc, self.table)
68 changes: 68 additions & 0 deletions ELFRelocs/relocEnums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from enum import IntEnum

from ELFMachine import ELFMachine


class ELF_I686_RELOCS(IntEnum):
# common
NONE = 0

PC32 = 2
GOT32 = 3
PLT32 = 4
COPY = 5
GLOB_DAT = 6
JMP_SLOT = 7
RELATIVE = 8

GOTOFF = 9
GOTPC = 10
THIRTYTWO_PLT = 11

# i686

SIXTEEN = 20
PC16 = 21
EIGHT = 22
PC8 = 23
SIZE32 = 38


class ELF_AMD64_RELOCS(IntEnum):
# common
NONE = 0

PC32 = 2
GOT32 = 3
PLT32 = 4
COPY = 5
GLOB_DaT = 6
JMP_SLOT = 7
RELATIVE = 8

GOTOFF = 9
GOTPC = 10
THIRTYTWO_PLT = 11
SIZE32 = 32

# x86_64
SIXTIFOUR = 1
GOTPCREL = 9
THIRTYTWO = 10
THIRTYTWOS = 11

SIXTEEN = 12
PC16 = 13
EIGHT = 14
PC8 = 15
PC64 = 24

GOTOFF64 = 25
GOTPC32 = 26
SIZE64 = 33


archTableMapping = {
ELFMachine.X86_64: ELF_AMD64_RELOCS,
ELFMachine.EM_386: ELF_AMD64_RELOCS,
}
Loading

0 comments on commit 3d6d8b7

Please sign in to comment.