W dniu 28.09.2022 o 17:52, JiiPee pisze:
I have two versions of function obfuscate_application(). The problem
occurs in /* commented out */ version (in random places of the code),
the version not commented out (older, below) works fine.
/*
bool obfuscator::obfuscate_application(bool remember_and_replace_strings)
{ // main algorithm
runtime_error err("save_cache() failed");
TRACE("ready to remember 3rd party identifiers");
if (!third_party_dirs.empty() && !php_cache.is_third_party_cached())
{
OUTPUT("Remembering 3rd party identifiers - this will take some
time but it is performed only once" << endl);
for (auto third_party_dir = third_party_dirs.begin();
third_party_dir != third_party_dirs.end(); ++third_party_dir)
{
if (!perform_remember_third_party_identifiers(L"",
*third_party_dir))
{
return false;
}
if (!php_cache.save_third_party_cache(*third_party_dir,
magic_str, third_party_identifiers[L""]))
{
error_messages.push_back("Data not written to file
dirtyphp_cache_3rd_parties.xml.");
throw err;
} else {
TRACE("saved cache: " << *third_party_dir);
}
}
}
TRACE("ready to remember framework identifiers");
if (framework_dir != L"" && !php_cache.is_framework_cached())
{
TRACE("framework directory: " << shortwstr2str(framework_dir));
OUTPUT("Remembering framework identifiers - this will take some
time but it is performed only once" << endl);
if (!perform_remember_framework_identifiers(L"", framework_dir))
{
return false;
}
if (!php_cache.save_framework_cache(framework_dir, magic_str,
framework_identifiers[L""]))
{
error_messages.push_back("Data not written to file
dirtyphp_cache_frameworks.xml.");
throw err;
} else {
TRACE("saved cache: " << framework_dir);
}
}
TRACE("ready to remember vendors identifiers");
if (vendor_dir != L"")
{
TRACE("vendor directory: " << shortwstr2str(vendor_dir));
if (!php_cache.is_vendor_cached())
{
if (!php_cache.save_vendor_cache(vendor_dir, implode(",",
vendor_frameworks), implode(L",", vendor_frameworks_dirs), implode(L",",
vendor_third_parties_dirs)))
{
error_messages.push_back("Data not written to file
dirtyphp_cache_vendors.xml.");
throw err;
} else {
TRACE("saved cache: " << vendor_dir);
}
}
string text = "Remembering vendor's identifiers - this will
take some time but it is performed only once...";
bool outputted = false; // output text only before first dir
auto dir = vendor_frameworks_dirs.begin();
for (; dir != vendor_frameworks_dirs.end(); )
{
TRACE("1");
*dir;
TRACE("2");
// TRACE("vendor framework dir: " << *dir);
if (!php_cache.is_vendor_subdir_cached(vendor_dir +
dir_separator + *dir)) {
if (!outputted) {
OUTPUT(text << endl);
outputted = true;
}
if (!perform_remember_framework_identifiers(*dir,
vendor_dir + dir_separator + *dir))
{
return false;
}
if (!php_cache.save_framework_cache(vendor_dir +
dir_separator + *dir, magic_str, framework_identifiers[*dir]))
{
error_messages.push_back("Data not written to file
dirtyphp_cache_frameworks.xml.");
throw err;
} else {
TRACE("saved cache: " << *dir);
}
}
++dir;
}
for (auto dir = vendor_third_parties_dirs.begin(); dir !=
vendor_third_parties_dirs.end(); ++dir)
{
if (!php_cache.is_vendor_subdir_cached(vendor_dir +
dir_separator + *dir))
{
TRACE("vendor third party dir: " << *dir);
if (!outputted) {
OUTPUT(text << endl);
outputted = true;
}
if (!perform_remember_third_party_identifiers(*dir,
vendor_dir + dir_separator + *dir))
{
return false;
}
if (!php_cache.save_third_party_cache(vendor_dir +
dir_separator + *dir, magic_str, third_party_identifiers[*dir]))
{
error_messages.push_back("Data not written to file
dirtyphp_cache_3rd_parties.xml.");
throw err;
} else {
TRACE("saved cache: " << *dir);
}
}
}
if (outputted) {
text = "...End of remembering vendor's identifiers";
OUTPUT(text << endl);
}
}
TRACE("1:" << (vendor_frameworks_dirs.size() == 0));
TRACE("2:" << (framework_identifiers.find(L"") ==
framework_identifiers.end()));
assert(vendor_frameworks_dirs.size() == 0 ||
framework_identifiers.find(L"") == framework_identifiers.end());
TRACE("ready to remember identifiers");
if (obfuscate_variables_in_views)
{
TRACE("obfuscating variables in views");
}
else
{
TRACE("not obfuscating variables in views");
}
if (!php_cache.is_application_cached())
{
if (subdirs.empty())
{
if (!perform_remember_identifiers(php_result_dir))
{
return false;
}
}
else
{
for (auto subdir = subdirs.begin(); subdir !=
subdirs.end(); ++subdir)
{
if (!perform_remember_identifiers(*subdir))
{
return false;
}
}
}
// !!! KONIECZNIE NAPRAWIĆ BŁĄD: bez strs: <strings
filepath="src/Kernel.php" />
if (!php_cache.save_application_cache(php_application_dir,
php_result_dir, cache_options.c_str(), magic_str, identifiers))
{
error_messages.push_back("Data not written to file
dirtyphp_cache_applications.xml.");
throw err;
} else {
TRACE("saved cache: " << php_application_dir);
}
}
TRACE("ready to generate random identifiers");
perform_generate_random_identifiers();
TRACE("ready to replace identifiers");
if (subdirs.empty())
{
if (!perform_replace_identifiers(php_result_dir))
{
return false;
}
}
else
{
for (auto subdir = subdirs.begin(); subdir != subdirs.end();
++subdir)
{
if (!perform_replace_identifiers(*subdir))
{
return true;
}
}
}
TRACE("before checking remember_and_replace_strings");
if (remember_and_replace_strings)
{
TRACE("ready to remember and replace strings");
if (php_cache.is_application_cached())
{
// skopiuj apostrophed_strings z cache:
//
assert(php_cache.application_apostrophed_strings.size() > 0);
for (auto &p : php_cache.application_apostrophed_strings)
{
apostrophed_strings_map *ptr = new
apostrophed_strings_map();
for (auto q = (p.second)->begin(); q !=
(p.second)->end(); ++q)
{
assert(!ends_with(q->first,
apostrophed_strings_map::random_identifiers_suffix));
ptr->insert(*q);
}
apostrophed_strings.insert(make_pair(p.first, ptr));
// ptr deallocated in obfuscator::~obfuscator()
}
TRACE("apostrophed_strings filled from cache, " <<
apostrophed_strings.size() << " maps");
}
else
{
TRACE("apostrophed_strings not filled from cache, maps will
be allocated in perform_remember_and_replace_strings()");
}
if (subdirs.empty())
{
TRACE("calling perform_remember_and_replace_strings() for
php_result_dir");
if (!perform_remember_and_replace_strings(php_result_dir))
{
return false;
}
}
else
{
for (auto subdir = subdirs.begin(); subdir !=
subdirs.end(); ++subdir)
{
TRACE("calling perform_remember_and_replace_strings()
for subdir: " << *subdir);
if (!perform_remember_and_replace_strings(*subdir))
{
return false;
}
}
}
}
*/
/* if (!php_cache.save_framework_cache(framework_dir, magic_str,
framework_identifiers)) {
error_messages.push_back("Error writing to file
dirtyphp_cache_frameworks.xml.");
throw runtime_error("save_cache() failed");
}
if (!php_cache.save_third_party_cache(third_party_dir, magic_str,
third_party_identifiers)) {
error_messages.push_back("Error writing to file
dirtyphp_cache_3rd_parties.xml.");
throw runtime_error("save_cache() failed");
}
if (!php_cache.save_application_cache(php_application_dir,
cache_options.c_str(), magic_str, identifiers)) {
error_messages.push_back("Error writing to file
dirtyphp_cache_applications.xml.");
throw runtime_error("save_cache() failed");
}
*/
/*
TRACE("done");
return true;
}
*/
bool obfuscator::obfuscate_application(bool
remember_and_replace_strings) { // main algorithm
TRACE("ready to remember 3rd party identifiers");
if (!third_party_dirs.empty() && !php_cache.is_third_party_cached()) {
OUTPUT("Remembering 3rd party identifiers - this will take some
time but it is performed only once" << endl);
// for (wstring third_party_dir : third_party_dirs) {
for (auto third_party_dir = third_party_dirs.begin();
third_party_dir != third_party_dirs.end(); ++third_party_dir) {
if (!perform_remember_third_party_identifiers(L"",
*third_party_dir)) {
return false;
}
}
}
TRACE("ready to remember framework identifiers");
if (framework_dir != L"" && !php_cache.is_framework_cached()) {
TRACE("framework directory: " << shortwstr2str(framework_dir));
OUTPUT("Remembering framework identifiers - this will take some
time but it is performed only once" << endl);
if (!perform_remember_framework_identifiers(L"", framework_dir)) {
return false;
}
}
TRACE("ready to remember vendors identifiers");
if (vendor_dir != L"") {
TRACE("vendor directory: " << shortwstr2str(vendor_dir));
// for (wstring dir : vendor_frameworks_dirs) {
for (auto dir = vendor_frameworks_dirs.begin(); dir !=
vendor_frameworks_dirs.end(); ++dir) {
if (!php_cache.is_vendor_subdir_cached(*dir)) {
if (!perform_remember_framework_identifiers(*dir,
vendor_dir + dir_separator + *dir)) {
return false;
}
}
}
// for (wstring dir : vendor_third_parties_dirs) {
for (auto dir = vendor_third_parties_dirs.begin(); dir !=
vendor_third_parties_dirs.end(); ++dir) {
if (!php_cache.is_vendor_subdir_cached(*dir)) {
if (!perform_remember_third_party_identifiers(*dir,
vendor_dir + dir_separator + *dir)) {
return false;
}
}
}
}
assert(vendor_frameworks_dirs.size() == 0 ||
framework_identifiers.find(L"") == framework_identifiers.end());
TRACE("ready to remember identifiers");
if (obfuscate_variables_in_views) {
TRACE("obfuscating variables in views");
} else {
TRACE("not obfuscating variables in views");
}
if (!php_cache.is_application_cached()) {
if (subdirs.empty()) {
if (!perform_remember_identifiers(php_result_dir)) {
return false;
}
} else {
// for (wstring subdir : subdirs) {
for (auto subdir = subdirs.begin(); subdir !=
subdirs.end(); ++subdir) {
if (!perform_remember_identifiers(*subdir)) {
return false;
}
}
}
}
TRACE("ready to generate random identifiers");
perform_generate_random_identifiers();
TRACE("ready to replace identifiers");
if (subdirs.empty()) {
if (!perform_replace_identifiers(php_result_dir)) {
return false;
}
} else {
// for (wstring subdir : subdirs) {
for (auto subdir = subdirs.begin(); subdir != subdirs.end();
++subdir) {
if (!perform_replace_identifiers(*subdir)) {
return true;
}
}
}
if (remember_and_replace_strings) {
TRACE("ready to remember and replace strings");
if (php_cache.is_application_cached()) {
// skopiuj apostrophed_strings z cache:
// assert(php_cache.application_apostrophed_strings.size() > 0);
for (auto &p : php_cache.application_apostrophed_strings) {
apostrophed_strings_map *ptr = new
apostrophed_strings_map();
for (auto q = (p.second)->begin(); q !=
(p.second)->end(); ++q) {
ptr->insert(*q);
}
apostrophed_strings.insert(make_pair(p.first, ptr));
// ptr deallocated in obfuscator::~obfuscator()
}
TRACE("apostrophed_strings filled from cache, " <<
apostrophed_strings.size() << " maps");
} else {
TRACE("apostrophed_strings not filled from cache, maps will
be allocated in perform_remember_and_replace_strings()");
}
if (subdirs.empty()) {
if (!perform_remember_and_replace_strings(php_result_dir)) {
return false;
}
} else {
// for (wstring subdir : subdirs) {
for (auto subdir = subdirs.begin(); subdir !=
subdirs.end(); ++subdir) {
if (!perform_remember_and_replace_strings(*subdir)) {
return false;
}
}
}
}
runtime_error err("save_cache() failed");
if (framework_dir != L"") {
if (!php_cache.save_framework_cache(framework_dir, magic_str,
framework_identifiers[L""])) {
error_messages.push_back("Error writing to file
dirtyphp_cache_frameworks.xml.");
throw err;
}
}
// for (wstring subdir : vendor_frameworks_dirs) {
for (auto subdir = vendor_frameworks_dirs.begin(); subdir !=
vendor_frameworks_dirs.end(); ++subdir) {
if (!php_cache.save_framework_cache(vendor_dir + dir_separator
+ *subdir, magic_str, framework_identifiers[*subdir])) {
error_messages.push_back("Error writing to file
dirtyphp_cache_frameworks.xml.");
throw err;
}
}
if (!third_party_dirs.empty()) {
// for (wstring third_party_dir : third_party_dirs) {
for (auto third_party_dir = third_party_dirs.begin();
third_party_dir != third_party_dirs.end(); ++third_party_dir) {
if (!php_cache.save_third_party_cache(*third_party_dir,
magic_str, third_party_identifiers[L""])) {
error_messages.push_back("Error writing to file
dirtyphp_cache_3rd_parties.xml.");
throw err;
}
}
}
// for (wstring subdir : vendor_third_parties_dirs) { // mimo że
vendor_third_parties_dirs == [ L"composer", L"psr" ] subdir przyjmuje
wartość L"" a nie powinno - BŁĄD!!!
for (auto subdir = vendor_third_parties_dirs.begin(); subdir !=
vendor_third_parties_dirs.end(); ++subdir) { // mimo że
vendor_third_parties_dirs == [ L"composer", L"psr" ] subdir przyjmuje
wartość L"" a nie powinno - BŁĄD!!!
if (!php_cache.save_third_party_cache(vendor_dir +
dir_separator + *subdir, magic_str, third_party_identifiers[*subdir])) {
error_messages.push_back("Error writing to file
dirtyphp_cache_3rd_parties.xml.");
throw err;
}
}
if (vendor_dir != L"") {
if (!php_cache.save_vendor_cache(vendor_dir, implode(",",
vendor_frameworks), implode(L",", vendor_frameworks_dirs), implode(L",",
vendor_third_parties_dirs))) {
error_messages.push_back("Error writing to file
dirtyphp_cache_vendors.xml.");
throw err;
}
}
// !!! KONIECZNIE NAPRAWIĆ BŁĄD: bez strs: <strings
filepath="src/Kernel.php" />
if (!php_cache.save_application_cache(php_application_dir,
php_result_dir, cache_options.c_str(), magic_str, identifiers)) {
error_messages.push_back("Error writing to file
dirtyphp_cache_applications.xml.");
throw err;
}
TRACE("done");
return true;
}