Skip to content

Commit df99b0e

Browse files
committed
Initial commit
0 parents  commit df99b0e

File tree

10 files changed

+385
-0
lines changed

10 files changed

+385
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
name: Bug report
3+
about: Create a report to help us improve
4+
title: ''
5+
labels: ''
6+
assignees: ''
7+
8+
---
9+
10+
**Describe the bug**
11+
A clear and concise description of what the bug is.
12+
13+
**To Reproduce**
14+
Steps to reproduce the behavior:
15+
1. Go to '...'
16+
2. Click on '....'
17+
3. Scroll down to '....'
18+
4. See error
19+
20+
**Expected behavior**
21+
A clear and concise description of what you expected to happen.
22+
23+
**Screenshots**
24+
If applicable, add screenshots to help explain your problem.
25+
26+
**Desktop (please complete the following information):**
27+
- OS: [e.g. iOS]
28+
- Browser [e.g. chrome, safari]
29+
- Version [e.g. 22]
30+
31+
**Smartphone (please complete the following information):**
32+
- Device: [e.g. iPhone6]
33+
- OS: [e.g. iOS8.1]
34+
- Browser [e.g. stock browser, safari]
35+
- Version [e.g. 22]
36+
37+
**Additional context**
38+
Add any other context about the problem here.

.gitignore

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py,cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
.pybuilder/
76+
target/
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
85+
# pyenv
86+
# For a library or package, you might want to ignore these files since the code is
87+
# intended to run in multiple environments; otherwise, check them in:
88+
# .python-version
89+
90+
# pipenv
91+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94+
# install all needed dependencies.
95+
#Pipfile.lock
96+
97+
# poetry
98+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99+
# This is especially recommended for binary packages to ensure reproducibility, and is more
100+
# commonly ignored for libraries.
101+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102+
#poetry.lock
103+
104+
# pdm
105+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106+
#pdm.lock
107+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108+
# in version control.
109+
# https://pdm.fming.dev/#use-with-ide
110+
.pdm.toml
111+
112+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
113+
__pypackages__/
114+
115+
# Celery stuff
116+
celerybeat-schedule
117+
celerybeat.pid
118+
119+
# SageMath parsed files
120+
*.sage.py
121+
122+
# Environments
123+
.env
124+
.venv
125+
env/
126+
venv/
127+
ENV/
128+
env.bak/
129+
venv.bak/
130+
131+
# Spyder project settings
132+
.spyderproject
133+
.spyproject
134+
135+
# Rope project settings
136+
.ropeproject
137+
138+
# mkdocs documentation
139+
/site
140+
141+
# mypy
142+
.mypy_cache/
143+
.dmypy.json
144+
dmypy.json
145+
146+
# Pyre type checker
147+
.pyre/
148+
149+
# pytype static type analyzer
150+
.pytype/
151+
152+
# Cython debug symbols
153+
cython_debug/
154+
155+
# PyCharm
156+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
157+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158+
# and can be added to the global gitignore or merged into this file. For a more nuclear
159+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
160+
#.idea/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Abhijeet
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Boot Image Extractor
2+
3+
Boot Image Extractor is a standalone python script designed to extract the boot image from Android devices with root access. It supports both single and dual-slotted devices.
4+
5+
## Prerequisites
6+
7+
- Python 3
8+
- `pyfiglet` library
9+
- Root access on the Android device
10+
11+
## Contributing
12+
13+
Contributions to the Boot Image Extractor are welcome. Please feel free to fork the repository, make your changes, and submit a pull request.
14+
15+
## License
16+
17+
This script is released under the [MIT License](LICENSE).
18+
19+
## Support
20+
21+
If you encounter any issues or have questions, please open an issue on the repository's issue tracker.

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pyfiglet

scripts/__init__.py

Whitespace-only changes.

scripts/boot_image_extractor.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env python3
2+
3+
"""A script to extract boot image from either single or dual slotted Android devices with root access."""
4+
5+
import os
6+
import sys
7+
import pyfiglet
8+
import subprocess
9+
10+
def print_banner(name):
11+
max_width = os.get_terminal_size().columns
12+
banner = pyfiglet.figlet_format(name, font='small', width=max_width)
13+
print(banner.center(max_width))
14+
15+
def exit_with_error(error, reason):
16+
print("\nError:", error)
17+
print("\nReason:", reason)
18+
sys.exit(1)
19+
20+
def extract_boot_image_dual_slot(boot_a_path, boot_b_path):
21+
active_slot = subprocess.getoutput('getprop ro.boot.slot_suffix')
22+
print("\nIt is recommended to extract the boot image according to the current active slot, which is ({}).\n".format(active_slot))
23+
24+
while True:
25+
chosen_slot = input("Which boot slot image would you like to extract? (a/b): ").lower()
26+
if chosen_slot == 'a':
27+
boot_image_path = boot_a_path
28+
break
29+
elif chosen_slot == 'b':
30+
boot_image_path = boot_b_path
31+
break
32+
else:
33+
print("Invalid input. Please choose either 'a' or 'b'.\n")
34+
continue
35+
36+
print("\nExtracting the boot image from {}...".format(boot_image_path))
37+
try:
38+
subprocess.check_call(['dd', 'if={}'.format(boot_image_path), 'of=./boot{}.img'.format(active_slot)])
39+
print("Boot image successfully extracted and saved in your {} directory.".format(os.path.basename(os.getcwd())))
40+
except subprocess.CalledProcessError:
41+
exit_with_error("Failed to extract the boot image", "dd command failed")
42+
43+
def extract_boot_image_single_slot(boot_path):
44+
print("\nExtracting the boot image from {}...".format(boot_path))
45+
try:
46+
subprocess.check_call(['dd', 'if={}'.format(boot_path), 'of=./boot.img'])
47+
print("Boot image successfully extracted and saved in your {} directory.".format(os.path.basename(os.getcwd())))
48+
except subprocess.CalledProcessError:
49+
exit_with_error("Failed to extract the boot image", "dd command failed")
50+
51+
def main():
52+
if os.geteuid() != 0:
53+
exit_with_error("Insufficient privileges", "This script requires root access. Please run as root or use sudo.")
54+
55+
print_banner("Boot Image Extractor")
56+
57+
boot_names = ['boot', 'boot_a', 'boot_b']
58+
for name in boot_names:
59+
path = subprocess.getoutput('find /dev/block -type l -name {} -print | head -n 1'.format(name))
60+
if path:
61+
print("{} = {}".format(name, path))
62+
if name == 'boot_a':
63+
boot_a_path = path
64+
elif name == 'boot_b':
65+
boot_b_path = path
66+
else:
67+
boot_path = path
68+
69+
if 'boot_a_path' in locals() and 'boot_b_path' in locals():
70+
print("\nDevice has dual boot slots.")
71+
extract_boot_image_dual_slot(boot_a_path, boot_b_path)
72+
elif 'boot_path' in locals():
73+
print("\nDevice has a single boot slot.")
74+
extract_boot_image_single_slot(boot_path)
75+
else:
76+
exit_with_error("No boot slots found", "unable to find the symlinked boot slot files")
77+
78+
if __name__ == '__main__':
79+
main()

setup.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# BootImageExtractor Setup File
2+
# -----------------------------
3+
#
4+
# Install with: pip install .
5+
#
6+
# Usage: sudo boot_image_extractor.py or su -c 'boot_image_extractor.py'
7+
#
8+
9+
from setuptools import setup
10+
11+
setup(
12+
name='Boot-Image-Extractor',
13+
scripts=['scripts/boot_image_extractor.py'],
14+
install_requires=[
15+
'pyfiglet',
16+
],
17+
)

tests/__init__.py

Whitespace-only changes.

tests/test_boot_image_extractor.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import unittest
2+
import os
3+
import sys
4+
from unittest.mock import patch, MagicMock
5+
6+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
7+
8+
9+
class TestBootImageExtractor(unittest.TestCase):
10+
"""
11+
Provides unit tests for the Boot Image Extractor script to ensure its proper functionality
12+
in various scenarios. Additional test cases can be added to improve test coverage.
13+
"""
14+
15+
def test_print_banner(self):
16+
from scripts.boot_image_extractor import print_banner
17+
with patch('builtins.print') as mock_print:
18+
print_banner("Test Banner")
19+
mock_print.assert_called_once()
20+
21+
@patch('builtins.print')
22+
@patch('subprocess.getoutput')
23+
@patch('os.geteuid', MagicMock(return_value=0))
24+
def test_extract_boot_image_single_slot(self, mock_getoutput, mock_print):
25+
mock_getoutput.return_value = '/dev/block/boot'
26+
with patch('subprocess.check_call', MagicMock()) as mock_check_call:
27+
with patch('os.path.basename', MagicMock(return_value='test')):
28+
with patch('subprocess.getoutput', MagicMock(return_value='a')):
29+
from scripts.boot_image_extractor import extract_boot_image_single_slot
30+
extract_boot_image_single_slot('boot_path')
31+
mock_check_call.assert_called_with(['dd', 'if=boot_path', 'of=./boot.img'])
32+
33+
@patch('builtins.print')
34+
@patch('subprocess.getoutput')
35+
@patch('os.geteuid', MagicMock(return_value=0))
36+
def test_extract_boot_image_dual_slot(self, mock_getoutput, mock_print):
37+
mock_getoutput.side_effect = ['/dev/block/boot_a', '/dev/block/boot_b', 'a']
38+
with patch('builtins.input', return_value='a'):
39+
with patch('subprocess.check_call', MagicMock()) as mock_check_call:
40+
with patch('os.path.basename', MagicMock(return_value='test')):
41+
with patch('subprocess.getoutput', MagicMock(return_value='a')):
42+
from scripts.boot_image_extractor import extract_boot_image_dual_slot
43+
extract_boot_image_dual_slot('boot_a_path', 'boot_b_path')
44+
mock_check_call.assert_called_with(['dd', 'if=boot_a_path', 'of=./boota.img'])
45+
46+
47+
if __name__ == '__main__':
48+
unittest.main()

0 commit comments

Comments
 (0)