한국어
Qt
 

General and Desktop QProcess 보안 권고 리뷰

makersweb 2022.09.18 15:50 조회 수 : 597

올해 초에 Qt Project의 보안팀은 QProcess를 사용하여 전체 경로 없이 다른 응용 프로그램을 시작할 때 발생할 수 있는 보안관련 패치를 제공했었다.

여기서는 이 문제를 재현해보고 QProcess의 올바른 사용법을 알아본다. 아직 패치하지 않았거나 하기 어려운 개발자는 검토해 보시길.

테스트 환경:
Windows 11
MinGW 64-bit
Qt 5.15.2

이 문제는 PATH 환경 변수에서 목적의 실행 파일을 찾기 전에 먼저 해당 앱의 디렉토리를 검색하기 때문에 발생한다. 어떤 나쁜 마음을 먹은 공격자가 같은 이름의 악성 실행 파일을 앱과 같은 위치에 배치하고 정보를 가로 챌 수 있다.

다음과 같이 사용하는 경우가 문제의 원인이다.

QProcess p;
p.start("application", args);

예를 들어 애플리케이션이 시스템에 설치된 압축해제 프로그램을 사용하려고 한다. (bandizip 을 미리 설치해 두었다.)

#include <QCoreApplication>
#include <QProcess>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QStringList args;
    args.append("x"); // Unzip
    args.append("-p:Passw0rd"); // Password
    args.append("-o:" + a.applicationDirPath() + "/dest"); // Target folder
    args.append(a.applicationDirPath() + "/sample.zip"); // Archive

    QProcess p;
    p.start("bandizip", args);
    p.waitForFinished(-1);
}

압축파일의 경로 및 패스워드등이 전달된다. 압축파일이 문제없이 잘 풀린다.

하지만 이 문제를 노리고 데이터를 가로채는 프로그램을 앱과 같은 위치에 배치했다고 치자.

가짜 bandizip 구현

#include <QCoreApplication>
#include <QFile>
#include <QString>
#include <QDebug>
#include <QTextStream>

void write(const QString &filename, const QStringList &args)
{
    QFile file(filename);
    // Trying to open in WriteOnly and Text mode
    if(!file.open(QFile::WriteOnly |
                  QFile::Text))
    {
        qDebug() << " Could not open file for writing";
        return;
    }

    // To write text, we use operator<<(),
    // which is overloaded to take
    // a QTextStream on the left
    // and data types (including QString) on the right

    QTextStream out(&file);

    for(const auto &arg : args) {
        out << arg << "\n";
    }

    file.flush();
    file.close();
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    write("stealArgs.txt", a.arguments());

    return 0;
}

이 프로그램은 명령줄 옵션을 전달받고 파일에 쓰는 일을 한다.

이제 예제 앱을 실행하면 사용자가 기대하지 않은일이 벌어질 수 있다. 다음 이미지는 stealArgs.txt 파일의 내용이다.

stealArgs.png

패치하거나 최신버전을 사용하면 좋겠지만 당장어렵다면 애플리케이션의 전체 경로를 확인하여 이런 위험을 줄일 수 있다. 이 목적에 부합하는 QStandardPaths::findExecutable 를 사용하면 PATH 환경 변수를 검색하고 결과적으로 안전한 사용 경로를 제공받을 수 있다.

예제 코드는 다음과 같다.

#include <QCoreApplication>
#include <QProcess>
#include <QDebug>

#include <QStandardPaths>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    auto bandizipPath = QStandardPaths::findExecutable("bandizip");

    if( !bandizipPath.isEmpty() ){
        QStringList args;
        args.append("x"); // Unzip
        args.append("-p:Passw0rd"); // Password
        args.append("-o:" + a.applicationDirPath() + "/dest"); // Target folder
        args.append(a.applicationDirPath() + "/sample.zip"); // Archive

        QProcess p;
        p.start(bandizipPath, args);
        p.waitForFinished(-1);

        qDebug() << "finished";
    } else {
        qDebug() << "not found command.";
    }
}
번호 제목 글쓴이 날짜 조회 수
공지 Qt프로그래밍(QtQuick) Beginner를 위한 글 읽는 순서 운영자 2019.01.05 85855
179 Qt 응용프로그램에 Web 구성 요소를 표시 with Servo newfile makersweb 2024.04.27 0
178 Qt Creator 에서 GitHub Copilot 사용하기 file makersweb 2024.04.13 137
177 QtQuick 애플리케이션에 Rive 애니메이션 통합 makersweb 2024.03.31 291
176 Qbs 프로젝트를 정의하기 위해 사용되는 몇가지 중요한 아이템들 file makersweb 2019.10.13 296
175 Qt 6.4에 추가될 Qt Quick 3D Physics file makersweb 2022.08.07 343
174 HTTPS URL을 연결할 때 SslHandshakeFailedError 오류 makersweb 2022.07.31 355
173 Qt Android 앱에 AdMob 배너달기 file makersweb 2021.12.04 392
172 그래픽 소프트웨어에서 디자인 내보내기 (Exporting Designs from Graphics Software) j2doll 2020.12.25 413
171 Binding 타입으로 객체 속성 간 묶기 makersweb 2022.03.04 420
170 Base64로 인코딩된 파일을 복원 makersweb 2023.08.06 425
169 VirtualKeyboard 스타일 커스터 마이징 makersweb 2022.03.13 462
168 Android 애플리케이션 서명 구성 file makersweb 2023.12.17 474
167 Qt Quick Controls 2에 네이티브 데스크탑 스타일 추가 file makersweb 2020.11.23 486
166 하드디스크 드라이브 여유 공간 계산 file makersweb 2023.01.15 487
165 앱을 종료할 때 QML 바인딩 오류를 피하는 방법 makersweb 2021.08.08 498
164 안드로이드용 Qt 6.2 makersweb 2021.10.02 504
163 성능 고려 및 제안 사항 makersweb 2022.03.07 505
162 QRhi 에 대해서 file makersweb 2023.12.29 506
161 QML의 사용자 정의 Image makersweb 2023.09.17 534
160 Qt Safe Renderer 개요 file makersweb 2022.09.08 539