Repository navigation
Expand file tree
/
Copy pathshell.cpp
More file actions
167 lines (139 loc) · 4.83 KB
/
Copy pathshell.cpp
File metadata and controls
167 lines (139 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "shell.hpp"
#include <algorithm>
#include <fstream>
#include <iostream>
#include <sstream>
#include "bridge.hpp"
#include "device.hpp"
#include "logger.hpp"
PSVR2Shell::PSVR2Shell(PSVR2Device* device, KernelRW* kernel_rw, PSVR2Bridge* bridge) : usb(device), krw(kernel_rw), bridge(bridge) {}
std::vector<std::string> PSVR2Shell::split_args(const std::string& input) {
std::istringstream iss(input);
std::vector<std::string> tokens;
std::string token;
while (iss >> token) tokens.push_back(token);
return tokens;
}
void PSVR2Shell::cmdloop() {
std::string line;
while (true) {
std::cout << prompt;
if (!std::getline(std::cin, line)) break;
auto args = split_args(line);
if (args.empty()) continue;
std::string cmd = args[0];
if (cmd == "exit" || cmd == "quit") break;
execute_command(args);
}
}
void PSVR2Shell::execute_command(const std::vector<std::string>& args) {
if (args.empty()) return;
std::string cmd = args[0];
std::vector<std::string> cmd_args(args.begin() + 1, args.end());
if (cmd == "get")
do_get(cmd_args);
else if (cmd == "write")
do_write(cmd_args);
else if (cmd == "reboot")
do_reboot(cmd_args);
else if (cmd == "shell")
do_shell(cmd_args);
else if (cmd == "upload")
do_upload(cmd_args);
else if (cmd == "download")
do_download(cmd_args);
else if (cmd == "sessions")
bridge->list_sessions();
else if (cmd == "attach")
do_attach(cmd_args);
else if (cmd == "kill")
do_kill(cmd_args);
else
LOG_ERROR << "[-] Unknown command: " << cmd << "\n";
}
void PSVR2Shell::do_get(const std::vector<std::string>& args) {
if (args.empty()) {
LOG_ERROR << "Usage: get <hex_address> [size]\n";
return;
}
uint64_t addr = std::stoull(args[0], nullptr, 16);
size_t size = args.size() > 1 ? std::stoull(args[1], nullptr, 0) : 0x1000;
std::string fname = "dump_" + args[0] + ".bin";
LOG_INFO << "[*] Dumping memory 0x" << std::hex << addr << " (" << std::dec << size << "B)...\n";
std::ofstream file(fname, std::ios::binary);
size_t done = 0;
while (done < size) {
size_t cl = std::min((size_t)0x1000, size - done);
auto data = krw->read(addr + done, cl);
if (data.empty()) {
LOG_ERROR << " [FAIL]\n";
return;
}
file.write(reinterpret_cast<char*>(data.data()), data.size());
done += data.size();
}
LOG_INFO << "[+] Saved " << fname << "\n";
}
void PSVR2Shell::do_write(const std::vector<std::string>& args) {
if (args.size() != 2) {
LOG_ERROR << "Usage: write <addr> <u64_value>\n";
return;
}
uint64_t addr = std::stoull(args[0], nullptr, 16);
uint64_t val = std::stoull(args[1], nullptr, 16);
LOG_INFO << "[*] Writing 0x" << std::hex << val << " → 0x" << addr << "\n";
if (krw->write_u64_slow(addr, val)) {
LOG_INFO << "[+] Done.\n";
} else {
LOG_ERROR << "[-] Failed.\n";
}
}
void PSVR2Shell::do_reboot(const std::vector<std::string>& args) {
LOG_WARN << "[!] Rebooting device via USB COMMAND RESTART...\n";
if (usb->force_reboot()) {
LOG_INFO << " Device is rebooting. Exiting shell.\n";
exit(0);
} else {
LOG_ERROR << "[-] Failed to send reboot command.\n";
}
}
void PSVR2Shell::do_upload(const std::vector<std::string>& args) {
if (args.size() != 2) {
LOG_ERROR << "Usage: upload <local_path> <remote_path>\n";
return;
}
bridge->upload(args[0], args[1]);
}
void PSVR2Shell::do_download(const std::vector<std::string>& args) {
if (args.size() != 2) {
LOG_ERROR << "Usage: download <remote_path> <local_path>\n";
return;
}
bridge->download(args[0], args[1]);
}
void PSVR2Shell::do_shell(const std::vector<std::string>& args) {
// Determine which shell to run. Default to "sh" if no args provided.
std::string shell_cmd = "sh";
if (!args.empty()) {
shell_cmd = "";
for (const auto& a : args) shell_cmd += a + " ";
shell_cmd.pop_back(); // Remove trailing space
}
bridge->shell(shell_cmd);
}
void PSVR2Shell::do_attach(const std::vector<std::string>& args) {
if (args.empty()) {
LOG_ERROR << "Usage: attach <session_id>\n";
return;
}
uint32_t session_id = std::stoul(args[0]);
bridge->shell("", session_id);
}
void PSVR2Shell::do_kill(const std::vector<std::string>& args) {
if (args.empty()) {
LOG_ERROR << "Usage: kill <session_id>\n";
return;
}
uint32_t session_id = std::stoul(args[0]);
bridge->kill_session(session_id);
}