Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,739,156 Members 46,732 Now Online
XDA Developers Android and Mobile Development Forum

[Shell][mksh][Scripting][Tweaks-Upgrade][flash.zip][bins]::mksh-FULL:::grep:::busybox

Tip us?
 
7175
Old
(Last edited by 7175; Yesterday at 05:38 AM.) Reason: update
#1  
7175's Avatar
Senior Member - OP
Thanks Meter 231
Posts: 191
Join Date: Feb 2013
Default [Shell][mksh][Scripting][Tweaks-Upgrade][flash.zip][bins]::mksh-FULL:::grep:::busybox

:::BRIEF DESCRIPTION:::
mksh is the official, currently-developed shell used for android. It is not an app nor a virus (lol) like some people seem to think, looking at a quick xda search. And as the core shell for android, it is used to shell superuser, so if you're rooted and get a message saying "mksh requests root", that basically just means that superuser is being run correctly and is using the default Android shell that is in every android device. (GOOD)
If you get a message saying bash(or any other shell) requested root, and you're NOT in the terminal interactively. That most likely is NOT GOOD! Almost all bash scripts I've seen will run shelled with mksh, so be safe and shebang with mksh. You'll have better performance and features, and when you see bash requesting root, you'll know your security is likely compromised if you allow it, instead of sitting there wandering whether to allow/block it.


::: mksh Resources/links :::

mirbsd mksh main page: https://www.mirbsd.org/mksh.htm

mksh man page: https://www.mirbsd.org/htman/i386/man1/mksh.htm


Now that that's out of the way, I've attached a recovery flash zip to this post containing a full mksh(R48 August 2013 from CFX ROM), full mkshrc modded for android, bunch of fpath functions like a much needed basic sudo, parallel sudo(sexe), required libc.so and libc_malloc*.so libs, and alternate busybox.ndk and standalone grep bins. The pexec, grep bins are from alireza7991. The mksh binary was compiled with O3 aliasing, Clanged CodeSourcery toolchain by CFX. It's very stable. An R49 variant would be nice if I could get someone to compile it.

(Installer uses a modded version of Chainfire's great no-edify script.)


Features scripted into mkshrc:

--sudo: auto detects supersu for mount master (highly recommended) or CWM su
e.g.:
Code:
Select Code
sudo mount -o rw,remount -t f2fs /system /system
sudo cat <<< $'\n' >> /system/build.prop
sudo 'sysrw; for i in /system/*; do chown 0 "$i" ; done; sysro'
--suid: (set user id) Uses chpst to run a prog setsuid, this is broken in most busyboxes so I included one with a working chpst. "suid" is probably needed as much or even more than a sudo. Hopefully it gets added to the two "superuser" programs sometime. Usage:::
e.g.:
Code:
Select Code
suid 1000 sh
--Better tab completion: unjams clustered words:
e.g.: <[cursor]> is cursor position
Code:
Select Code
ec<[cursor]>12345 + tab ---> echo 12345
--Added "control + u" to "cut-to-beginning of line" like bash.

--TMPDIR: mounts a tmpdir at /tmp for all users and random generates subdirs for individual secured tmpdirs. This fixes here-docs, here-strings, and here-executions. To get a /tmp directory mounted, you need to run su once upon booting android. Just type "ss" upon first starting up terminal emulator.
e.g.:
Code:
Select Code
su << 'eeooff'
sysrw; chgrp 2000 /system/bin; sysro
! (( $? )) && print -- -changed them bin group-
eeooff
--Basic "help" command: Android lacks this and the doc is still under development I believe. I added all the basic info(cmd forms) for mksh bultins to a command help(aliased to h):
e.g.:
Code:
Select Code
help mkn
> mknod [-m mode] name b|c major minor
> mknod [-m mode] name p
or
Code:
Select Code
h ''
(all help is printed, grep from here if you want)

--Very complicated-programmed and colorful PS1 prompt, it looks good and I thought it through. Programming shows mksh potential for rich shell programming. It auto-turns off xtrace within itself so that 50 lines don't get printed each return with set -x. mksh small will be autodetected and give a different white and black classic ksh prompt.

--ton of ultra lazy aliases, got my android scripting speed up to ~70% my normal pc speed.

--full mkshrc functions from official mkshrc like pushd, popd, dirs, more ,hd, Lb64encode, Lb64decode, hash optimizations


The latest full R49 source with original mkshrc and complete mksh manual is completed. It'll be installed to /system/etc/mksh...zip and /system/etc/mksh...pdf. I use Document Viewer from fdroid to view it.


The scripting in this is very sophisticated and complex, especially the inherent full mkshrc functions. It would make a good scripting learning tool to say the least. I know I've learned twice what I knew from bash scripting back 3 months ago.


I've been working on modding the full mkshrc a while, so I appreciate any error reports. Shouldn't be any problems though.

Modifying the mkshrc file / mksh shell has system-wide effects, so PLEASE nandroid backup as usual!!! This should be COMPATIBLE with all androids, but has only been tested on my Galaxy Nexus thus far.


EDIT: It appears the libc required for full mksh isnt always friendly with a few apps like GTA SA, so I made it so that mksh small is installed to system, and I added a function to run full mksh installed to /data/media/shell. Just type:
"mkmk" or "mkmksh" for full mksh. This is added as an fpath function now so any *ksh can run it that has FPATH defaulted or set to /etc/fpath.

Also reprogrammed sudo with pexec, so it can do parallel executions. pexec, grep, and a full busybox are included.

For Jack Palevich's terminal emulator or connectbot, telnet, etc, I use this as my su'd "shell" command under >>Options>>Shell:
Code:
Select Code
/system/xbin/su --shell /system/bin/mksh --command exec /system/bin/pexec -z -15 --shell /system/bin/mksh -m 'export LD_LIBRARY_PATH=/data/media/shell:"$LD_LIBRARY_PATH"; export PATH=/data/media/shell:"$PATH"; mksh'
What's safer though is to just use a non-su shell command:
Code:
Select Code
/system/bin/mksh -c 'export LD_LIBRARY_PATH=/data/media/shell:"$LD_LIBRARY_PATH"; export PATH=/data/media/shell:"$PATH"; mksh'
...and then use sudo (also aliased as "xx" for ease) for your su commands. This is more linux'y.
Attached Files
File Type: zip mkmksh_07_17_2014.zip - [Click for QR Code] (3.62 MB, 36 views)
The Following 3 Users Say Thank You to 7175 For This Useful Post: [ Click to Expand ]
 
7175
Old
#2  
7175's Avatar
Senior Member - OP
Thanks Meter 231
Posts: 191
Join Date: Feb 2013
reserved
 
7175
Old
#3  
7175's Avatar
Senior Member - OP
Thanks Meter 231
Posts: 191
Join Date: Feb 2013
reserved
|Device| = Galaxy Nexus 'toro' |Batt's| =3800, 1850, 2100b (NFC-stripped) |Radio's| = FK-LTE/FA-cdma
|Bootloader| = PRIMEMD04
|ROM's| = Carbon(d.d. f2fs) + PAC |RECOVERY| = TWRP 2.7.x.x |SU| = SuperSU!
|Kernels| = Dirty V, Fancy, Grakern --- FIOPS'd BFQ; Core:307; VAUX3:2500; COLOR:[Contrast=-2;Gamma=-4;+{-8; -5; -5}; x{175; 175; 200}]


Excellent GNEX[Ref][Guide] -- ART-compatible Apps -- Xposed Repo -- [Tweak]mksh/-rc FULL+ installer -- init.d && cron
 
E:V:A
Old
#4  
E:V:A's Avatar
Recognized Developer
Thanks Meter 1704
Posts: 1,301
Join Date: Dec 2011
Location: -∇ϕ
@7175 : Hello! Look like a bunch of hard work there. Very nice.

1. Would you have any clue to what's going on HERE?
2. Would you know if I can just install over my old mksh binary to get this working, or do I need "everything"?
3. And since I've heard that /system/bin/mksh will soon be permanently only /system/bin/sh perhaps this need to be changed?
MSM8960 Info, Architecture and Bootloader(s)
El Grande Partition Table Reference
How to talk to the Modem with AT commands

[REF][ServiceMode] How to make your Samsung perform dog tricks
[REF|R&D|RF] RF/Radio properties of Samsung ServiceMode

Want to know when your phone is getting tracked or tapped?

Help us develop the IMSI Catcher / Spy Detector!
(To be part of the EFF & The Guardian Project toolsets.)
_______________________________
If you like what I do, just click THANKS!
Everything I do is free, altruism is the way!
ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
I do not answer support related PM's.

The Following User Says Thank You to E:V:A For This Useful Post: [ Click to Expand ]
 
7175
Old
(Last edited by 7175; 27th July 2014 at 09:23 AM.)
#5  
7175's Avatar
Senior Member - OP
Thanks Meter 231
Posts: 191
Join Date: Feb 2013
Hey E.VA. thanks for checking out the thread. If you want to just test out R48 mksh, just copy(from zip root directory) the lib*.so from /data/media/shell to /system/lib and then copy /data/media/shell/mksh to /system/bin (and chmod 0555 /system/bin/mksh; chmod 0444 /system/lib/*.so). Then you'll have your whole system running full mksh R48.

As far as the ssh problem with android, this definitely becomes grey-zone material to my knowledge, which is limited in this area. I'm pretty sure android has udev amputated so it lacks a fully working /dev/tty. This could probably be patched over though with some terminal work, mknod action. I've gotten ssh working under "terminal ide", but haven't used the stock ssh stuff in /system/bin. Now that you mention this though, I will try and get stock ssh going as well as openssh which had a similar problem last time I tried (thx for mentioning your support thread, I'll be checking that out).

Here's what I searched to shed a little light on that ssh /dev/tty issue: duckduckgo.com/html5/?q=ssh+%27dev%2Ftty%27+error.
It looks like you've done a ton of searching though.

For su commands, I would try using, e.g.,
Code:
Select Code
su exec command
...opposed to "su -c", or better e.g.,
Code:
Select Code
su -c exec /system/bin/sh -c 'command'
...and see if that gives any different results.
I just know from experience "su -c" hasn't always cut it.


*edit: Couple other things worth trying:
1) preappend command with environment variable: global TTY=/dev/pts/0 ...
2) use the -T option when calling mksh, i,e. ssh ... -e mksh -T /dev/pts/0 ...
|Device| = Galaxy Nexus 'toro' |Batt's| =3800, 1850, 2100b (NFC-stripped) |Radio's| = FK-LTE/FA-cdma
|Bootloader| = PRIMEMD04
|ROM's| = Carbon(d.d. f2fs) + PAC |RECOVERY| = TWRP 2.7.x.x |SU| = SuperSU!
|Kernels| = Dirty V, Fancy, Grakern --- FIOPS'd BFQ; Core:307; VAUX3:2500; COLOR:[Contrast=-2;Gamma=-4;+{-8; -5; -5}; x{175; 175; 200}]


Excellent GNEX[Ref][Guide] -- ART-compatible Apps -- Xposed Repo -- [Tweak]mksh/-rc FULL+ installer -- init.d && cron
 
E:V:A
Old
(Last edited by E:V:A; 28th July 2014 at 06:23 PM.)
#6  
E:V:A's Avatar
Recognized Developer
Thanks Meter 1704
Posts: 1,301
Join Date: Dec 2011
Location: -∇ϕ
@7175 : Hey thanks for looking into this. I will definitely try your suggestions.
However, I've already tried the -T flag to ssh, but never by specifying the pts to use, as it should be dynamically allocated by the ptmx. (As it could be already in use, by something else, like ATE.) Also, what does "global" do?

You also mentioned "stock ssh" in /system/bin. I've never seen that. Perhaps that could be a better solution, unless its' even more crippled. Does it also support sshd (i.e. ssh -D)?

In the mean time, is there any particular reason why you decided to dynamically link mksh? (I assumed it would have been more portable with static linking?)

I see that you did a huge amount of work on that mkshrc script. Looks crazy (in a good way)!

Finally, and aside, I also noticed that when I start mksh from ssh session, there are some aliases in there, that is not specified in the mkshrc, nor the .profile of my SSH server, so where the heck do they come from?

Code:
Select Code
u0_a202@MSM8960:home # alias
autoload='typeset -fu'
functions='typeset -f'
hash='alias -t'
history='fc -l'
integer='typeset -i'
local=typeset
login='exec login'
nameref='typeset -n'
nohup='nohup '
r='fc -e -'
source='PATH=$PATH:. command .'
type='whence -v'
Perhaps they're hardcoded or sourced from somewhere? Have you seen this?
MSM8960 Info, Architecture and Bootloader(s)
El Grande Partition Table Reference
How to talk to the Modem with AT commands

[REF][ServiceMode] How to make your Samsung perform dog tricks
[REF|R&D|RF] RF/Radio properties of Samsung ServiceMode

Want to know when your phone is getting tracked or tapped?

Help us develop the IMSI Catcher / Spy Detector!
(To be part of the EFF & The Guardian Project toolsets.)
_______________________________
If you like what I do, just click THANKS!
Everything I do is free, altruism is the way!
ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
I do not answer support related PM's.

The Following User Says Thank You to E:V:A For This Useful Post: [ Click to Expand ]
 
7175
Old
#7  
7175's Avatar
Senior Member - OP
Thanks Meter 231
Posts: 191
Join Date: Feb 2013
Quote:
Originally Posted by E:V:A View Post
@7175 : Hey thanks for looking into this. I will definitely try your suggestions.
However, I've already tried the -T flag to ssh, but never by specifying the pts to use, as it should be dynamically allocated by the ptmx. (As it could be already in use, by something else, like ATE.) Also, what does "global" do?

You also mentioned "stock ssh" in /system/bin. I've never seen that. Perhaps that could be a better solution, unless its' even more crippled. Does it also support sshd (i.e. ssh -D)?

In the mean time, is there any particular reason why you decided to dynamically link mksh? (I assumed it would have been more portable with static linking?)

I see that you did a huge amount of work on that mkshrc script. Looks crazy (in a good way)!

Finally, and aside, I also noticed that when I start mksh from ssh session, there are some aliases in there, that is not specified in the mkshrc, nor the .profile of my SSH server, so where the heck do they come from?

Code:
Select Code
u0_a202@MSM8960:home # alias
autoload='typeset -fu'
functions='typeset -f'
hash='alias -t'
history='fc -l'
integer='typeset -i'
local=typeset
login='exec login'
nameref='typeset -n'
nohup='nohup '
r='fc -e -'
source='PATH=$PATH:. command .'
type='whence -v'
Perhaps they're hardcoded or sourced from somewhere? Have you seen this?
Thanks man lol it's a big rc. It was 50KB before I converted a bunch of the functions to fpath functions in /etc/fpath and stripped most of the comments. It should give a pretty quick shell in terminal though with the ulimit and renicing tweaks. The PS1 prompt is pretty awesome, probably the best thing in the mkshrc I included.

"global" is a function just like "typeset" or "declare" in bash. The difference is that global prevents an accessed variable from being localized/ different than the global value in a function. This is a mksh function, bash will use "declare -g". There are a couple of example usage in the mkshrc, like with the set -x spam fix for the command prompt.

I checked and stock ssh in /system/bin has the "-D" flag. I would also check rolo's zshaolin zsh project which comes with some afterstock ssh stuff, ssh keygen, ssh keysearch, different ssh etc.

Yeah I just symlinked sh to save space really and for portability to try out other shells like bash and zsh(this works!) to run the system. Recently I switched to using a copy of mksh as sh since "chattr +i" wasn't protecting the symlink from being over-linked.

The aliases you posted are in fact hard-coded. If you do "strings /system/bin/mksh | grep -C7 autoload" or something similar, you'll see them in the binary. I always wondered where those came from too until I read through the mksh manual over a couple months, that's some pretty dense reading lol. mksh is a superb shell though, so it's definitely well worth it.

Sorry I can't be of more help with the ssh /dev/tty problem. I will keep my eyes open though, and try not to be so lazy using telnet for all my remote connections. I mean that's basically using the internet without a condom lol.


(*Also, just something I should mention about the install zip contents: not all the /etc/fpath functions are finished yet. I still gotta tweak the ssh ports from /etc/ssh and finish my xargs. Most should work though and are safe and won't interfer with shell usage, even if autoloaded.)
|Device| = Galaxy Nexus 'toro' |Batt's| =3800, 1850, 2100b (NFC-stripped) |Radio's| = FK-LTE/FA-cdma
|Bootloader| = PRIMEMD04
|ROM's| = Carbon(d.d. f2fs) + PAC |RECOVERY| = TWRP 2.7.x.x |SU| = SuperSU!
|Kernels| = Dirty V, Fancy, Grakern --- FIOPS'd BFQ; Core:307; VAUX3:2500; COLOR:[Contrast=-2;Gamma=-4;+{-8; -5; -5}; x{175; 175; 200}]


Excellent GNEX[Ref][Guide] -- ART-compatible Apps -- Xposed Repo -- [Tweak]mksh/-rc FULL+ installer -- init.d && cron

Tags
busybox, grep, ksh, mksh, shell
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


TRENDING IN THEMER...