Skip to content
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

EMBA ChatGPT integration #602

Merged
merged 234 commits into from
Jul 23, 2023
Merged
Show file tree
Hide file tree
Changes from 231 commits
Commits
Show all changes
234 commits
Select commit Hold shift + click to select a range
900c18b
initial-test
BenediktMKuehne Apr 27, 2023
482a892
add json template
BenediktMKuehne Apr 27, 2023
c26e80b
ask_chatgpt function
BenediktMKuehne Apr 27, 2023
7831934
add test script
BenediktMKuehne Apr 27, 2023
a1ea7bd
add logs
BenediktMKuehne Apr 27, 2023
2256b4a
remove personal key
BenediktMKuehne Apr 27, 2023
880f5b3
update
BenediktMKuehne Apr 27, 2023
1cdcc96
add key for testing
BenediktMKuehne Apr 27, 2023
dc45bed
rename config_dir
BenediktMKuehne Apr 27, 2023
7d48502
fix quoting
BenediktMKuehne Apr 27, 2023
847f6e5
fix response in log on error
BenediktMKuehne Apr 27, 2023
1d78745
update gpt-feature
BenediktMKuehne May 2, 2023
f77c987
Merge branch 'master' into gpt-module
BenediktMKuehne May 2, 2023
a052710
fix missing s in help text
BenediktMKuehne May 3, 2023
6c7a281
update chatgpt stuff
BenediktMKuehne May 3, 2023
89151b5
update chatgpt feature
BenediktMKuehne May 3, 2023
f022c2a
fix missspell
BenediktMKuehne May 3, 2023
21da112
adding some debug stuff for now
BenediktMKuehne May 3, 2023
9a4a418
fix bad substitution
BenediktMKuehne May 3, 2023
11ffc00
adding some debug stuff for now
BenediktMKuehne May 3, 2023
595c9a0
update chatgpt feature
BenediktMKuehne May 3, 2023
cb5503e
update template
BenediktMKuehne May 4, 2023
5d1484f
update chatgpt
BenediktMKuehne May 4, 2023
1ba9205
update ask_chatgpt
BenediktMKuehne May 4, 2023
7c265d6
testing edit
BenediktMKuehne May 4, 2023
7d70068
update chatgpt
BenediktMKuehne May 4, 2023
156ca8b
fix csv write
BenediktMKuehne May 4, 2023
bf4d00c
update chatgpt
BenediktMKuehne May 4, 2023
746bf35
update chatgpt
BenediktMKuehne May 4, 2023
c8deaca
Merge branch 'fix-libssl-ubuntu' into gpt-module
BenediktMKuehne May 4, 2023
c3ed2ea
update gpt csv writes in modules s20-s23
BenediktMKuehne May 4, 2023
b88e589
fix path splitting
BenediktMKuehne May 4, 2023
1f42b2f
fix ask_chatgpt
BenediktMKuehne May 5, 2023
d9204e9
update path print
BenediktMKuehne May 8, 2023
87f843a
fix path find
BenediktMKuehne May 8, 2023
0126880
fix find path
BenediktMKuehne May 8, 2023
03636c6
fix find path
BenediktMKuehne May 8, 2023
1a092f8
fix find path
BenediktMKuehne May 8, 2023
fe13a05
update ask_chatgpt
BenediktMKuehne May 8, 2023
98fb1c4
update gpt option
BenediktMKuehne May 8, 2023
84acd02
reporting for gpt
BenediktMKuehne May 8, 2023
3235ab3
update reporting func
BenediktMKuehne May 12, 2023
78f8e1b
change double escape problem
BenediktMKuehne May 15, 2023
19a87f0
update escape problem
BenediktMKuehne May 15, 2023
e84628d
Merge remote-tracking branch 'origin/master' into gpt-module
BenediktMKuehne May 15, 2023
cf3755e
update gpt stuff
BenediktMKuehne May 23, 2023
e693e50
update
BenediktMKuehne Jun 2, 2023
423939c
Moving internet access stuff to second container
BenediktMKuehne Jun 12, 2023
5ca3ffa
remove no_log
BenediktMKuehne Jun 12, 2023
0291deb
no more network for analiser
BenediktMKuehne Jun 12, 2023
159683e
add debug to see the container number
BenediktMKuehne Jun 12, 2023
fafbcb6
update dep
BenediktMKuehne Jun 12, 2023
8e042ff
update compose dep
BenediktMKuehne Jun 12, 2023
3346321
update
BenediktMKuehne Jun 12, 2023
a7d41a8
update
BenediktMKuehne Jun 12, 2023
8fea180
update
BenediktMKuehne Jun 12, 2023
7e76a7f
update
BenediktMKuehne Jun 12, 2023
8727b4d
detach service
BenediktMKuehne Jun 12, 2023
fe4262d
update
BenediktMKuehne Jun 12, 2023
08b77b3
update
BenediktMKuehne Jun 12, 2023
074b9de
update
BenediktMKuehne Jun 15, 2023
38b44c1
update
BenediktMKuehne Jun 16, 2023
71d3bb3
remove old report thing
BenediktMKuehne Jun 16, 2023
1625d71
update reporting architecture
BenediktMKuehne Jun 19, 2023
4166987
update container destroy
BenediktMKuehne Jun 19, 2023
950a5fc
update container and network init before
BenediktMKuehne Jun 19, 2023
61a302c
update
BenediktMKuehne Jun 19, 2023
92a7fdd
update networking
BenediktMKuehne Jun 19, 2023
ea3dc40
bigger interval between iterations
BenediktMKuehne Jun 19, 2023
6995573
ch questions for gpt
BenediktMKuehne Jun 19, 2023
bff947e
update
BenediktMKuehne Jun 22, 2023
e77cda9
update
BenediktMKuehne Jun 22, 2023
2d2bdaf
update q-module according to naming convention
BenediktMKuehne Jun 22, 2023
e0b75a9
update
BenediktMKuehne Jun 26, 2023
b5baafa
update
BenediktMKuehne Jun 26, 2023
a1dcb0b
add dep check quest container
BenediktMKuehne Jun 26, 2023
36476a8
update check both container in series
BenediktMKuehne Jun 26, 2023
7418a4f
update check both container in series
BenediktMKuehne Jun 26, 2023
c519d4c
kill quest if running only
BenediktMKuehne Jun 26, 2023
dc271aa
rm s115 dep for s20
BenediktMKuehne Jun 26, 2023
3b935cd
update variable workflow for q02
BenediktMKuehne Jun 26, 2023
3f04a25
update quest container
BenediktMKuehne Jun 26, 2023
c2d5cb1
fix quest container
BenediktMKuehne Jun 26, 2023
afb6049
re-add firmware dir
BenediktMKuehne Jun 26, 2023
f146653
rm double permission print
BenediktMKuehne Jun 26, 2023
bb10527
Merge branch 'master' into gpt-module
BenediktMKuehne Jun 26, 2023
79b0546
add wait for Q02 in reporter phase
BenediktMKuehne Jun 26, 2023
096e817
Merge branch 'gpt-module' of https://github.com/BenediktMKuehne/emba …
BenediktMKuehne Jun 26, 2023
dbeeb5a
rm privs for quest-container
BenediktMKuehne Jun 26, 2023
ac29aaf
update
BenediktMKuehne Jun 26, 2023
255f8da
update
BenediktMKuehne Jun 26, 2023
0e3eff9
update security
BenediktMKuehne Jun 27, 2023
136b397
update resolver output_file find
BenediktMKuehne Jun 28, 2023
f502588
change anchor-resolve printing
BenediktMKuehne Jun 28, 2023
08cf3e3
rm \" from response
BenediktMKuehne Jun 29, 2023
0763782
rm quotes from final response
BenediktMKuehne Jun 29, 2023
37c9808
add break if api-key exceeds quota
BenediktMKuehne Jun 29, 2023
2aa551e
remove $ from response
BenediktMKuehne Jun 29, 2023
60d0add
Merge branch 'gpt-module' of https://github.com/BenediktMKuehne/emba …
BenediktMKuehne Jun 29, 2023
73f9404
ch response quoting
BenediktMKuehne Jun 30, 2023
8b8c901
ch response quoting
BenediktMKuehne Jun 30, 2023
b988136
update response problem
BenediktMKuehne Jun 30, 2023
d8d829b
re-order Q02 csvs
BenediktMKuehne Jul 3, 2023
24d27cd
reorder csv
BenediktMKuehne Jul 3, 2023
e0dc887
update q02 reporting
BenediktMKuehne Jul 5, 2023
1884dba
update
BenediktMKuehne Jul 5, 2023
37000f6
ch find html file name
BenediktMKuehne Jul 5, 2023
500f349
replace in all files
BenediktMKuehne Jul 5, 2023
9570de8
update linking
BenediktMKuehne Jul 6, 2023
a4a4e41
fix origin module link
BenediktMKuehne Jul 6, 2023
9c805e8
update ref link
BenediktMKuehne Jul 6, 2023
b378836
update reverse_link
BenediktMKuehne Jul 6, 2023
eae03ec
update reverse link
BenediktMKuehne Jul 6, 2023
5e6b33f
fix SC2001 issue
BenediktMKuehne Jul 6, 2023
929acf0
escape sed command
BenediktMKuehne Jul 6, 2023
b58cdd0
make rev link relative
BenediktMKuehne Jul 6, 2023
a2ce0ec
make link visible
BenediktMKuehne Jul 7, 2023
18fbb93
update q02 links
BenediktMKuehne Jul 7, 2023
c1fb11d
add wiki link
BenediktMKuehne Jul 7, 2023
08e7ba2
fix quoting
BenediktMKuehne Jul 7, 2023
1cec95f
restructure q02
BenediktMKuehne Jul 7, 2023
c6196bd
move dep-check for container 2
BenediktMKuehne Jul 7, 2023
d19232a
move OpenAI-API test into dep check
BenediktMKuehne Jul 7, 2023
1192165
fix API-key check
BenediktMKuehne Jul 7, 2023
714a757
ch test.log location
BenediktMKuehne Jul 7, 2023
64eae3d
fix API test
BenediktMKuehne Jul 7, 2023
acab9bc
add output to gpt-profile
BenediktMKuehne Jul 7, 2023
067de01
cleanup
BenediktMKuehne Jul 7, 2023
ec04590
instant fail if gpt option set but no api key
BenediktMKuehne Jul 7, 2023
e360668
add quest container kill to interrrupt handler
BenediktMKuehne Jul 7, 2023
e7ddf45
always check openai api key but no error
BenediktMKuehne Jul 7, 2023
6423264
fix path
BenediktMKuehne Jul 7, 2023
e2190a4
read gpt env only if uncommented lines inside
BenediktMKuehne Jul 7, 2023
49f8038
fix unbound vars
BenediktMKuehne Jul 10, 2023
4789370
update cleaner func for quest container
BenediktMKuehne Jul 10, 2023
e986ae7
add indent
BenediktMKuehne Jul 10, 2023
b2b5bdb
fix unbound var in cleaner
BenediktMKuehne Jul 10, 2023
3475cba
shorten output
BenediktMKuehne Jul 10, 2023
c6732a9
update dep check exit cond
BenediktMKuehne Jul 10, 2023
251be97
fix code indenting
BenediktMKuehne Jul 10, 2023
9032ef0
fix result counter q02
BenediktMKuehne Jul 10, 2023
c0dc0fc
disable html for f05
BenediktMKuehne Jul 10, 2023
f74127f
rm double /
BenediktMKuehne Jul 10, 2023
b4299b2
fix file copy
BenediktMKuehne Jul 10, 2023
4e3b85d
fix var name
BenediktMKuehne Jul 12, 2023
b18a3b1
update
BenediktMKuehne Jul 13, 2023
4d320cf
Merge branch 'gpt-module' of https://github.com/BenediktMKuehne/emba …
BenediktMKuehne Jul 13, 2023
eacf41b
update incrementation syntax
BenediktMKuehne Jul 13, 2023
5cb70f0
add output for quota
BenediktMKuehne Jul 13, 2023
d74a303
update dep check to catch incorrect openAI api key
BenediktMKuehne Jul 13, 2023
f147a22
add newline to config file
BenediktMKuehne Jul 13, 2023
c115c8e
tweak output
BenediktMKuehne Jul 13, 2023
f5fba8d
add quest container global to defaults
BenediktMKuehne Jul 13, 2023
6b12e65
add error output in dep check
BenediktMKuehne Jul 14, 2023
9bfa57e
add sleep for rate limiting
BenediktMKuehne Jul 14, 2023
f6865a4
update
BenediktMKuehne Jul 14, 2023
94d86c7
update openai check to only use one request
BenediktMKuehne Jul 17, 2023
cf98d3c
make gpt_config.env vars global
BenediktMKuehne Jul 17, 2023
39af31a
fix create dir on repeat
BenediktMKuehne Jul 17, 2023
387a81e
catch for openai server_error
BenediktMKuehne Jul 17, 2023
4f7140e
fix server_error handling
BenediktMKuehne Jul 17, 2023
7904a67
spacing
BenediktMKuehne Jul 17, 2023
216fc67
fix printf error
BenediktMKuehne Jul 17, 2023
3ea0453
add catch for rate_limit
BenediktMKuehne Jul 17, 2023
3c8f21c
add main_log print for terminating resolver
BenediktMKuehne Jul 17, 2023
6d965a0
only print in quest container
BenediktMKuehne Jul 17, 2023
cd113e2
fix resolver waiting
BenediktMKuehne Jul 17, 2023
1b37808
remove dots from links
BenediktMKuehne Jul 18, 2023
d3a14d0
change main-log quest container output
BenediktMKuehne Jul 18, 2023
81c62b9
stop if testing phase done
BenediktMKuehne Jul 18, 2023
f2441c1
update config for gpt
BenediktMKuehne Jul 18, 2023
b24e02e
rename to .template
BenediktMKuehne Jul 18, 2023
9c77d37
output tweaks
BenediktMKuehne Jul 18, 2023
0018a91
only run Q02 if API key provided
BenediktMKuehne Jul 18, 2023
76b3d9c
add timeout for q02
BenediktMKuehne Jul 18, 2023
6e7380c
rm test files
BenediktMKuehne Jul 18, 2023
9894060
update dep check
BenediktMKuehne Jul 18, 2023
d270f10
fix dep check print
BenediktMKuehne Jul 18, 2023
11b142f
fix dep check print
BenediktMKuehne Jul 18, 2023
2a915a5
add TODO for openai checks in s22
BenediktMKuehne Jul 18, 2023
d72dd91
Merge branch 'master' into gpt-module
BenediktMKuehne Jul 18, 2023
1b58af7
add gpt test to s22 semgrep
BenediktMKuehne Jul 18, 2023
6c36d49
rm string
BenediktMKuehne Jul 18, 2023
5432740
fix
BenediktMKuehne Jul 18, 2023
8f615db
strict mode adjustments
BenediktMKuehne Jul 18, 2023
e2180e0
fixes
BenediktMKuehne Jul 19, 2023
7fe528b
ch cp for input files
BenediktMKuehne Jul 19, 2023
8a5fe3c
ch gpt prio
BenediktMKuehne Jul 19, 2023
5a8f854
update q02 related stuff
BenediktMKuehne Jul 19, 2023
89c0540
continue on all unknown server errors
BenediktMKuehne Jul 19, 2023
3d1df03
:)
BenediktMKuehne Jul 19, 2023
2d7650c
small fixes
m-1-k-3 Jul 19, 2023
e8ecfdd
adjust origin module finder for links
BenediktMKuehne Jul 19, 2023
f5659ce
Merge branch 'gpt-module' of https://github.com/BenediktMKuehne/emba …
BenediktMKuehne Jul 19, 2023
1ee8987
fix org module
BenediktMKuehne Jul 19, 2023
2cc90ca
fix wrong output file problem
BenediktMKuehne Jul 19, 2023
94ff7dd
gpt response output, links, python prio
m-1-k-3 Jul 19, 2023
5cfe57f
php prio
m-1-k-3 Jul 19, 2023
714ad0e
min to max
BenediktMKuehne Jul 19, 2023
0079e7f
add focus on semgrep line
BenediktMKuehne Jul 19, 2023
8e4ebdd
formatting
m-1-k-3 Jul 19, 2023
0f5152a
formatting
m-1-k-3 Jul 19, 2023
a1de663
fix input-output file
BenediktMKuehne Jul 19, 2023
79953e5
Merge branch 'gpt-module' of https://github.com/BenediktMKuehne/emba …
BenediktMKuehne Jul 19, 2023
f2339fc
add fixme und debug print
BenediktMKuehne Jul 19, 2023
1ed95af
Update F50_base_aggregator.sh
BenediktMKuehne Jul 19, 2023
22560c8
Update F20_vul_aggregator.sh
BenediktMKuehne Jul 19, 2023
6645abd
check if file is available before moving on
m-1-k-3 Jul 19, 2023
0e9f8d2
style
m-1-k-3 Jul 19, 2023
fd280f2
style
m-1-k-3 Jul 19, 2023
eeabbe5
update comments
BenediktMKuehne Jul 19, 2023
e657fa1
Merge branch 'gpt-module' of https://github.com/BenediktMKuehne/emba …
BenediktMKuehne Jul 19, 2023
35a9920
Merge remote-tracking branch 'origin/master' into gpt-module
BenediktMKuehne Jul 21, 2023
fcc1fc1
add catch for connection reset
BenediktMKuehne Jul 21, 2023
75d7395
update module blacklist
m-1-k-3 Jul 21, 2023
f7449a2
update sleep timing
BenediktMKuehne Jul 21, 2023
c98a435
Merge branch 'gpt-module' of https://github.com/BenediktMKuehne/emba …
BenediktMKuehne Jul 21, 2023
1d173da
handle rate limit
m-1-k-3 Jul 21, 2023
5d1d17b
error.code vs error.message
m-1-k-3 Jul 21, 2023
3b545f6
print sleep note
m-1-k-3 Jul 21, 2023
f6ae176
output
m-1-k-3 Jul 21, 2023
a9994de
add dep check output for quest container
BenediktMKuehne Jul 21, 2023
d781e9b
Merge branch 'gpt-module' of https://github.com/BenediktMKuehne/emba …
BenediktMKuehne Jul 21, 2023
c1fe7b0
update
BenediktMKuehne Jul 21, 2023
e8315bc
adjust output
BenediktMKuehne Jul 21, 2023
95a9c02
remove sleep
BenediktMKuehne Jul 21, 2023
ec906b2
add retries for rate limit
BenediktMKuehne Jul 21, 2023
150b660
fix name
BenediktMKuehne Jul 21, 2023
a56f6b3
GPT key check
m-1-k-3 Jul 22, 2023
89166e6
quest cont dep check
m-1-k-3 Jul 22, 2023
483a75f
comments
m-1-k-3 Jul 22, 2023
244857f
AI integration in final aggregator
m-1-k-3 Jul 22, 2023
3433bb4
improve Q module integration in web reporter
m-1-k-3 Jul 22, 2023
66c7cf0
html style for qmodules
m-1-k-3 Jul 23, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ config/emba_updater
config/emba_updater_data
config/module_blacklist.txt
config/jtr_wordlist.txt
config/gpt_config.env
2 changes: 1 addition & 1 deletion check_project.sh
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ check_tools() {
done
if ! [[ -d ./external/semgrep-rules/bash ]]; then
echo -e "\\n""${RED}""${BOLD}""Please install semgrep-rules to directory ./external to perform all checks""${NC}""\\n"
echo -e "${ORANGE}git clone https://github.com/returntocorp/semgrep-rules.git external/semgrep-rule${NC}"
echo -e "${ORANGE}git clone https://github.com/returntocorp/semgrep-rules.git external/semgrep-rules${NC}"
exit 1
fi
}
Expand Down
3 changes: 3 additions & 0 deletions config/gpt_config.env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# OPENAI_API_KEY=""
# GPT_QUESTION="For the following code I need you to tell me how an attacker could exploit it and point out all vulnerabilities."
# MAXMIUM_GPT_PRIO=3
8 changes: 8 additions & 0 deletions config/gpt_template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"model": "gpt-3.5-turbo",
m-1-k-3 marked this conversation as resolved.
Show resolved Hide resolved
"messages": [
{
"role": "user",
"content":
"Testing123"
}]}
51 changes: 49 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,64 @@ services:
- /boot:/boot:ro
environment:
- USER
- CONTAINER_NUMBER=1
networks:
- emba_runs
devices:
- /dev/fuse:/dev/fuse:rwm
cap_add:
- SYS_ADMIN
networks:
- emba_runs
ulimits:
core:
hard: 0
soft: 0

emba_quest:
image: embeddedanalyzer/emba:latest
read_only: true
tmpfs:
- /tmp
- /root/.config/
- /root/.local/
- /root/.cache/
- /root/.cache/pypoetry/virtualenvs:exec
- /root
- /root/.cargo/bin:exec
- /run/lock
- /var
- /var/run
- /var/tmp
- /var/lock
- /var/lib/nikto:exec
BenediktMKuehne marked this conversation as resolved.
Show resolved Hide resolved
- /external/arachni/arachni-1.6.1.3-0.6.1.1/bin/../.system/arachni-ui-web/config/component_cache
- /external/arachni/arachni-1.6.1.3-0.6.1.1/bin/../.system/arachni-ui-web/db
- /external/arachni/arachni-1.6.1.3-0.6.1.1/bin/../.system/arachni-ui-web/tmp
- /external/arachni/arachni-1.6.1.3-0.6.1.1/bin/../.system/../logs
BenediktMKuehne marked this conversation as resolved.
Show resolved Hide resolved
- /external/arachni/arachni-1.6.1.3-0.6.1.1/bin/../.system/home
build: .
# /dev is needed for the system emulator (L10)
# /lib/modules is needed for modules which are loading kernel modules (e.g. extractors)
# /boot is needed for modules which are loading kernel modules (e.g. extractors like P10)
volumes:
- ${FIRMWARE}/:/firmware:ro
m-1-k-3 marked this conversation as resolved.
Show resolved Hide resolved
- ${LOG}/:/logs
- ${EMBA}/:/emba:ro
- ${EMBA}/external/linux_kernel_sources/:/external/linux_kernel_sources
- /etc/localtime:/etc/localtime:ro
- /dev:/dev
# - /lib/modules:/lib/modules:ro
# - /boot:/boot:ro
environment:
- USER
- CONTAINER_NUMBER=2
network_mode: host
ulimits:
core:
hard: 0
soft: 0
security_opt:
- no-new-privileges:true

networks:
emba_runs:
name: emba_runs
Expand Down
68 changes: 67 additions & 1 deletion emba
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
INVOCATION_PATH="."
MODULE_COUNT=0


import_helper()
{
local HELPERS=()
Expand Down Expand Up @@ -108,7 +109,26 @@ check_cve_search_job() {
done
}

# $1: module group letter [P, S, L, F]
check_quest_container() {
print_ln "no_log"
print_output "[*] Checking Quest container ${QUEST_CONTAINER} dependencies \\n" "no_log"
# wait for quest container to finish network checks
while [[ "$(docker container inspect -f '{{.State.Running}}' "${QUEST_CONTAINER}")" == "true" ]]; do
if docker logs "${QUEST_CONTAINER}" | grep -q 'Elementary'; then
break
fi
sleep 0.1
done
reset
print_ln "no_log"
print_output "[*] Quest container ${QUEST_CONTAINER} dependency checks: \\n" "no_log"
docker logs "${QUEST_CONTAINER}"
print_output "[*] Finished Quest container ${QUEST_CONTAINER} dependency checks \\n" "no_log"
print_ln "no_log"
reset
}

# $1: module group letter [P, S, L, F, Q]
# $2: 0=single thread 1=multithread
# $3: HTML=1 - generate html file
run_modules()
Expand Down Expand Up @@ -702,9 +722,28 @@ main() {
if [[ "${ONLY_DEP}" -gt 0 ]]; then
EMBA="${INVOCATION_PATH}" FIRMWARE="${FIRMWARE_PATH}" docker-compose run --rm emba -c './emba -f /firmware -i "$@"' _ "${ARGUMENTS[@]}"
D_RETURN=$?
EMBA="${INVOCATION_PATH}" FIRMWARE="${FIRMWARE_PATH}" docker-compose run --rm emba_quest -c './emba -f /firmware -i "$@"' _ "${ARGUMENTS[@]}"
if [[ $? -ne ${D_RETURN} ]]; then
D_RETURN=1
fi
else
local QUEST_CONTAINER_=""
QUEST_CONTAINER_="$(EMBA="${INVOCATION_PATH}" FIRMWARE="${FIRMWARE_PATH}" LOG="${LOG_DIR}" docker-compose run --detach --rm emba_quest -c './emba -l /logs -f /firmware -i "$@"' _ "${ARGUMENTS[@]}")"
export QUEST_CONTAINER="${QUEST_CONTAINER_}"
print_output "[*] Quest container ${QUEST_CONTAINER_} started and detached.\\n" "main"
print_ln "no_log"

check_quest_container "${QUEST_CONTAINER}" &
QUEST_PID="$!"
store_kill_pids "${QUEST_PID}"
disown "${QUEST_PID}" 2> /dev/null || true

EMBA="${INVOCATION_PATH}" FIRMWARE="${FIRMWARE_PATH}" LOG="${LOG_DIR}" docker-compose run --rm emba -c './emba -l /logs -f /firmware -i "$@"' _ "${ARGUMENTS[@]}"
D_RETURN=$?
# stop quest container
if [[ "$( docker container inspect -f '{{.State.Status}}' "${QUEST_CONTAINER_}" )" == "running" ]]; then
docker kill "${QUEST_CONTAINER_}"
fi
fi
enable_strict_mode "${STRICT_MODE}" 0

Expand Down Expand Up @@ -739,6 +778,29 @@ main() {
trap 'initial_status_bar' WINCH
fi

#######################################################################################
# Quests (Q-modules)
#######################################################################################

if [[ "${CONTAINER_NUMBER}" -eq 2 ]] ; then
while ! grep -q "Pre-checking phase started" "${LOG_DIR}"/"${MAIN_LOG_FILE}"; do
sleep 1
done
run_modules "Q" "${THREADED}" "0"
[[ "${THREADED}" -eq 1 ]] && wait_for_pid "${WAIT_PIDS[@]}"
print_output "[*] Quest container done" "main"
exit 0
elif [[ "${USE_DOCKER}" -eq 0 && "${IN_DOCKER}" -eq 0 ]] ; then
while ! grep -q "Pre-checking phase started" "${LOG_DIR}"/"${MAIN_LOG_FILE}"; do
sleep 1
done
# dev-mode
run_modules "Q" "${THREADED}" "0" &
export Q_MOD_PID="$!"
elif [[ "${CONTAINER_NUMBER}" -ne 1 ]]; then
docker logs "${QUEST_CONTAINER}" --follow | grep "ok" && print_output "[*] Quest container ${QUEST_CONTAINER_} failed the dependancy check" "main"
fi

#######################################################################################
# Pre-Check (P-modules)
#######################################################################################
Expand Down Expand Up @@ -849,6 +911,10 @@ main() {

write_notification "Reporting phase ended"

if [[ "${USE_DOCKER}" -eq 0 && "${THREADED}" -eq 1 && "${IN_DOCKER}" -eq 0 ]] && [[ -n "${Q_MOD_PID}" ]]; then
wait_for_pid "${Q_MOD_PID}"
fi

if [[ "${TESTING_DONE}" -eq 1 ]]; then
if [[ "${FINAL_FW_RM}" -eq 1 && -d "${LOG_DIR}"/firmware ]]; then
print_output "[*] Removing temp firmware directory\\n" "no_log"
Expand Down
6 changes: 6 additions & 0 deletions helpers/helpers_emba_defaults.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ set_defaults() {
export QRUNTIME="20s"

export SHELLCHECK=1

export QUEST_CONTAINER=""
export GPT_OPTION=0 # 0 -> off 1-> unpayed plan 2 -> no rate-limit
export GPT_QUESTION="For the following code I need you to tell me how an attacker could exploit it and point out all vulnerabilities:"
export MINIMUM_GPT_PRIO=2 # [3 downto 0] 3 -> everything gets checked; 0 -> nothing gets checked

export SHORT_PATH=0 # short paths in cli output
export THREADED=0 # 0 -> single thread
# 1 -> multi threaded
Expand Down
89 changes: 88 additions & 1 deletion helpers/helpers_emba_dependency_check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,94 @@ dependency_check()
{
module_title "Dependency check" "no_log"

print_ln "no_log"

#######################################################################################
## Quest Container
#######################################################################################
print_output "[*] Network connection:" "no_log"
if [[ "${CONTAINER_NUMBER}" -ne 1 ]]; then
print_output " Internet connection - \\c" "no_log"
if ! ping 8.8.8.8 -q -c 1 -W 1 &>/dev/null ; then
echo -e "$RED""not ok""$NC"
print_output "[-] ERROR: Quest container has no internet connection!" "no_log"
exit 1
else
echo -e "$GREEN""ok""$NC"
fi
if [[ -f "${CONFIG_DIR}/gpt_config.env" ]]; then
if grep -v -q "#" "${CONFIG_DIR}/gpt_config.env"; then
# readin gpt_config.env
while read -r LINE; do
if [[ "${LINE}" == *'='* ]] && [[ "${LINE}" != '#'* ]]; then
export "$(echo "${LINE}" | xargs)"
fi
done < "${CONFIG_DIR}/gpt_config.env"
fi
fi
if [[ -z "${OPENAI_API_KEY}" ]]; then
print_output "$(indent "ChatGPT-API key not set - ${ORANGE}see https://github.com/e-m-b-a/emba/wiki/AI-supported-firmware-analysis for more information${NC}")" "no_log"
# The following if clause is currently not working! We have not loaded the profile in this stage
# TODO: Find a workaround!
if [[ "${GPT_OPTION}" -eq 1 ]]; then
DEP_ERROR=1
fi
else
local RETRIES_=0
# on the host we try it only 10 times:
local MAX_RETRIES=10
if [[ "$IN_DOCKER" -eq 1 ]]; then
# within the Quest container we can keep trying it as it does not matter if the container starts up later
MAX_RETRIES=200
fi
local SLEEPTIME=30
while true; do
local HTTP_CODE_=400
print_output " OpenAI-API key - \\c" "no_log"
HTTP_CODE_=$(curl https://api.openai.com/v1/chat/completions -H "Content-Type: application/json" \
-H "Authorization: Bearer ${OPENAI_API_KEY}" \
-d @"${CONFIG_DIR}/gpt_template.json" --write-out "%{http_code}" -o /tmp/chatgpt-test.json -sS)

if [[ "${HTTP_CODE_}" -eq 200 ]] ; then
echo -e "$GREEN""ok""$NC"
rm /tmp/chatgpt-test.json
break
else
if jq '.error.code' /tmp/chatgpt-test.json | grep -q "rate_limit_exceeded" ; then
# rate limit handling - if we got a response like:
# Please try again in 20s
echo -e "$RED""not ok (rate limit issues)""$NC"
if jq '.error.message' /tmp/chatgpt-test.json | grep -q "Please try again in " ; then
# print_output "GPT API test #${RETRIES_} - \\c" "no_log"
sleep "${SLEEPTIME}"s
# sleeptime gets adjusted on every failure
SLEEPTIME=$((SLEEPTIME+5))
((RETRIES_+=1))
[[ "${RETRIES_}" -lt "${MAX_RETRIES}" ]] && continue
fi
fi
echo -e "$RED""not ok""$NC"
print_output "[-] ChatGPT error while testing the API-Key: ${OPENAI_API_KEY}" "no_log"
print_output "[-] ERROR response: $(cat /tmp/chatgpt-test.json)" "no_log"
# Note: we are running into issues in the case where the key can't be verified, but GPT is not enabled at all
# In such a case we will fail the check without the need of GPT
DEP_ERROR=1
fi
if grep -q "Testing phase ended" "${LOG_DIR}"/"${MAIN_LOG_FILE}"; then
print_output " Testing phase ended - \\c" "no_log"
echo -e "$RED""exit now""$NC"
DEP_ERROR=1
fi
done
fi
else
print_output " Isolation - ${GREEN}""ok""${NC}" "no_log"
fi
if [[ "${CONTAINER_NUMBER}" -eq 2 ]] ; then
if [[ $ONLY_DEP -gt 0 ]] && [[ $FORCE -ne 0 ]]; then
exit 0
fi
fi
print_ln "no_log"
#######################################################################################
# Elementary checks
Expand All @@ -207,7 +295,6 @@ dependency_check()
# As the container runs as root we should not run into issues within the container.
# Outside the container we can run mostly without root privs - this is currently under evaluation
# Some other nice features like restarting the mongod will not work without root privs.
print_output " user permissions" "no_log"
if [[ $QEMULATION -eq 1 && $EUID -ne 0 ]] || [[ $USE_DOCKER -eq 1 && $EUID -ne 0 ]] || [[ $FULL_EMULATION -eq 1 && $EUID -ne 0 ]]; then
if [[ $QEMULATION -eq 1 && $USE_DOCKER -eq 0 ]] || [[ $FULL_EMULATION -eq 1 && $USE_DOCKER -eq 0 ]]; then
print_output " user permission - emulation mode - \\c" "no_log"
Expand Down
7 changes: 6 additions & 1 deletion helpers/helpers_emba_helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ cleaner() {
print_output "[*] Interrupt detected!" "no_log"
fi
print_output "[*] Final cleanup started." "no_log"

if [[ "$IN_DOCKER" -eq 0 ]] && [[ -n "${QUEST_CONTAINER}" ]]; then
if [[ "$( docker container inspect -f '{{.State.Status}}' "${QUEST_CONTAINER}" )" == "running" ]]; then
print_output "[*] Stopping Quest Container ..." "no_log"
docker kill "${QUEST_CONTAINER}"
fi
fi
# stop inotifywait on host
if [[ "$IN_DOCKER" -eq 0 ]] && pgrep -f "inotifywait.*$LOG_DIR.*" &> /dev/null 2>&1; then
print_output "[*] Stopping inotify ..."
Expand Down
3 changes: 1 addition & 2 deletions helpers/helpers_emba_internet_access.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# EMBA is licensed under GPLv3
#
# Author(s): Michael Messner
# Author(s): Michael Messner, Benedikt Kuehne

# Description: Multiple useful helpers used to access online resources

Expand Down Expand Up @@ -132,4 +132,3 @@ kernel_downloader() {
write_log "$OUTPUTTER" "$LOG_DIR/kernel_downloader.log"
done
}

40 changes: 40 additions & 0 deletions helpers/helpers_emba_print.sh
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,46 @@ print_notification() {
done
}

# writes inputs into csv for chatgpt
# Args: "${GPT_INPUT_FILE_}" "${GPT_ANCHOR_}" "GPT-Prio-${GPT_PRIO_}" "${GPT_QUESTION_}" "${GPT_OUTPUT_FILE_}" "cost=${GPT_TOKENS_}" "${GPT_RESPONSE_}"
write_csv_gpt() {
local CSV_ITEMS=("$@")
if ! [[ -d "$CSV_DIR" ]]; then
print_output "[-] WARNING: CSV directory ${ORANGE}${CSV_DIR}${NC} not found"
return
fi
printf '%s;' "${CSV_ITEMS[@]}" >> "$CSV_DIR/q02_openai_question.csv" || true
printf '\n' >> "$CSV_DIR/q02_openai_question.csv" || true
}

# writes inputs into tmp csv for chatgpt
# Args: "${GPT_INPUT_FILE_}" "${GPT_ANCHOR_}" "GPT-Prio-${GPT_PRIO_}" "${GPT_QUESTION_}" "${GPT_OUTPUT_FILE_}" "cost=${GPT_TOKENS_}" "${GPT_RESPONSE_}"
write_csv_gpt_tmp() {
local CSV_ITEMS=("$@")
if ! [[ -d "$CSV_DIR" ]]; then
print_output "[-] WARNING: CSV directory ${ORANGE}${CSV_DIR}${NC} not found"
return
fi
printf '%s;' "${CSV_ITEMS[@]}" >> "$CSV_DIR/q02_openai_question.csv.tmp" || true
printf '\n' >> "$CSV_DIR/q02_openai_question.csv.tmp" || true
}

write_anchor_gpt() {
if [[ $HTML -eq 1 ]] ; then
local LINK
LINK="${1:-}"
LINK="$(format_log "[ASK_GPT] ""$LINK" 1)"
local LOG_FILE_ALT="${2:-}"
if [[ "$LOG_FILE_ALT" != "no_log" ]] && [[ "$LOG_FILE_ALT" != "main" ]] ; then
if [[ -f "$LOG_FILE_ALT" ]] ; then
echo -e "$LINK" | tee -a "$LOG_FILE_ALT" >/dev/null
else
echo -e "$LINK" | tee -a "$LOG_FILE" >/dev/null
fi
fi
fi
}

print_running_modules() {
sleep 1h
while true; do
Expand Down
Loading