[완료] c언어 소스상에서 shell에 입력하는 방법이 있나요?

idrukawa의 이미지

xxx.cpp 소스상에서 root 로 로그인 하는 코딩을 하고 싶습니다.

간단히 이런 식으로 코딩을 해봤는데요...

char *cmd;

sprintf(cmd, "su -");
popen(cmd, "r");

sprintf(cmd, "passroot");
popen(cmd, "r");

이런 결과가 나옵니다.

password: sh: passroot:notfount

popen() 명령을 수행하면 하면, 내부적으로
]$ sh (무식하지만 제가 직접 쉘에 sh라고 입력해봤습니다.ㅋ)
하고 cmd에 들어있는 string을 입력하는 것 같습니다.

해서 제가 원하는것은
cmd에 있는 string을 shell에 그대로 입력하는 방법을 알고 싶습니다.
(char *cmd에 abcd가 들어있다면 abcd만 shell에 입력되도록...)

출력 결과를 보건데 Password: 만 재대로 입력하면 root로 로그인이 될 것 같습니다만...
도와주세요~~

세벌의 이미지

cppig1995의 이미지

system 함수를 사용하시면 됩니다. 다만, 명령 해석기[shell]를 거치지 않으려면 exec 계열의 함수를 사용하셔야 합니다.

하지만 root 비밀 번호를 프로그램에 노출시키는 것보다는 setuid 속성이나 setreuid 함수를 사용하시는 게 좋겠다는 생각이 듭니다.

> What is the difference between software and hard water?
} Bugs drown in hard water, but live forever in software.

Real programmers /* don't */ comment their code.
If it was hard to write, it should be /* hard to */ read.

nthroot의 이미지

system 이나 popen은 말고 직접 Password를 입력 받으시고
getpwid()류의 함수로 패스워드 인증을 하시고 setuid()로 권한 상승을 하시면...될것 같네요.

------식은이 처------
길이 끝나는 저기엔 아무 것도 없어요. 희망이고 나발이고 아무 것도 없어.

idrukawa의 이미지

다음과 같은 방법으로 문제를 해결했습니다.

1. root 계정에서...
]# chown root a
]# chgrp root a
]# chmod 4755 a

2. user 계정에서...
]$ ls -al
-rwsr-xr-x 1 root root size month day time a

3. 실행
]$ ./a
renice -20 pid

----------------------------------------------------------------------

제가 작성한 a 라는 프로그램은
target process의 nice 값을 변경하는 프로그램입니다.
그래서 root 권한이 필요했는데
세 분의 답글덕분에 해결했습니다.

너무 감사드립니다^^

bushi의 이미지

댓글 첨부 파일: 
첨부파일 크기
Plain text icon wrapcmd_pty.c.txt3.73 KB

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.